コード例 #1
0
ファイル: plugin.go プロジェクト: icedream/vpnbot
// Creates a new plugin instance.
func New(b bot.Bot, isupportPlugin *isupport.Plugin) *Plugin {
	plugin := &Plugin{
		bot:      b,
		isupport: isupportPlugin,
	}

	// ("."|"!"|"+"|"@")"bots"
	b.HandleFunc("privmsg",
		func(conn *client.Conn, line *client.Line) {
			// Check if this came from a channel named "bots" or "vpnbot"
			chanPrefixes, _ := plugin.isupport.Supports().ChanTypes()
			prefixes, channelName := isupport.SplitIrcPrefix(line.Args[0], chanPrefixes)
			if len(prefixes) <= 0 {
				return // target not a channel
			}
			if !strings.EqualFold(channelName, "bots") &&
				!strings.EqualFold(channelName, "vpnbot") {
				return
			}

			// Check if this is the "bots" command (with either prefix below)
			words := strings.Split(line.Args[1], " ")
			prefixes, firstWord := isupport.SplitIrcPrefix(words[0], []rune(".!+@"))
			if len(prefixes) <= 0 {
				return // not a command
			}
			if !strings.EqualFold(firstWord, "bots") {
				return // not the bots command
			}

			// Report in!
			plugin.bot.Privmsg(line.Args[0],
				fmt.Sprintf(
					"Reporting in! [Go] See %v12%vhttps://github.com/icedream/vpnbot%v%v",
					"\x03", "\x1F", "\x1F", "\x03"))
		})

	return plugin
}
コード例 #2
0
ファイル: plugin.go プロジェクト: icedream/vpnbot
func (p *Plugin) WhoIs(nick string) (resp *WhoIsResponse, err error) {
	newResp := new(WhoIsResponse)
	whoisCompletionChan := make(chan interface{})

	availablePrefixes, _ := p.isupportPlugin.Supports().Prefix()

	defer p.bot.HandleFunc(ircReplyAway,
		func(conn *client.Conn, line *client.Line) {
			if len(line.Args) < 3 {
				return
			}
			if line.Args[1] != nick {
				return
			}
			newResp.AwayMessage = line.Args[2]
			newResp.IsAway = true
		}).Remove()
	defer p.bot.HandleFunc(ircReplyWhoIsChannels,
		func(conn *client.Conn, line *client.Line) {
			if len(line.Args) < 3 {
				return
			}
			if line.Args[1] != nick {
				return
			}
			if newResp.Channels == nil {
				newResp.Channels = map[string]WhoIsChannelMode{}
			}
			for _, prefixedChannel := range line.Args[2:] {
				prefixes, channel := isupport.SplitIrcPrefix(prefixedChannel, availablePrefixes.Symbols)
				var prefix WhoIsChannelMode = WhoIsChannelModeNone
				if len(prefixes) > 0 {
					prefix = WhoIsChannelMode(prefixes[0])
				}
				newResp.Channels[channel] = prefix
			}
		}).Remove()
	defer p.bot.HandleFunc(ircReplyWhoIsIdle,
		func(conn *client.Conn, line *client.Line) {
			if len(line.Args) < 3 {
				return
			}
			if line.Args[1] != nick {
				return
			}

			// Idle duration
			v, err := strconv.ParseInt(line.Args[2], 10, 64)
			if err != nil {
				return
			}
			newResp.IdleDuration = time.Duration(v) * time.Second

			if len(line.Args) > 3 {
				// Sign on time
				v, err := strconv.ParseInt(line.Args[3], 10, 64)
				if err != nil {
					return
				}
				newResp.SignOnTime = time.Unix(v, 0)
			}
		}).Remove()
	defer p.bot.HandleFunc(ircReplyWhoIsOperator,
		func(conn *client.Conn, line *client.Line) {
			if len(line.Args) < 2 {
				return
			}
			if line.Args[1] != nick {
				return
			}
			newResp.IsOperator = true
		}).Remove()
	defer p.bot.HandleFunc(ircReplyWhoIsServer,
		func(conn *client.Conn, line *client.Line) {
			if len(line.Args) < 4 {
				return
			}
			if line.Args[1] != nick {
				return
			}
			newResp.Server = WhoIsServerInfo{
				Host: line.Args[2],
				Info: line.Args[3],
			}
		}).Remove()
	defer p.bot.HandleFunc(ircReplyWhoIsUser,
		func(conn *client.Conn, line *client.Line) {
			if len(line.Args) < 6 {
				return
			}
			if line.Args[1] != nick {
				return
			}
			newResp.Nick = line.Args[1]
			newResp.Ident = line.Args[2]
			newResp.Host = line.Args[3]
			// line.Args[4] == "*"
			newResp.Realname = line.Args[5]
		}).Remove()
	defer p.bot.HandleFunc(ircReplyEndOfWhoIs,
		func(conn *client.Conn, line *client.Line) {
			if len(line.Args) < 2 {
				return
			}
			if line.Args[1] != nick {
				return
			}
			close(whoisCompletionChan)
		}).Remove()
	defer p.bot.HandleFunc(ircErrNoNickGiven,
		func(conn *client.Conn, line *client.Line) {
			if line.Nick != nick {
				return
			}
			err = ErrInvalidNick
			close(whoisCompletionChan)
		}).Remove()
	defer p.bot.HandleFunc(ircErrNoSuchNick,
		func(conn *client.Conn, line *client.Line) {
			if line.Nick != nick {
				return
			}
			err = ErrNoSuchNick
			close(whoisCompletionChan)
		}).Remove()

	p.bot.Conn().Whois(nick)
	select {
	case <-time.After(30 * time.Second):
		err = errors.New("Request timed out")
	case <-whoisCompletionChan:
	}

	if err == nil {
		resp = newResp
	}

	return
}