// Creates a new plugin instance. func New(b bot.Bot) *Plugin { plugin := &Plugin{ bot: b, } // Handle INVITE b.HandleFunc("invite", func(conn *client.Conn, line *client.Line) { if len(line.Args) < 2 { return } channel := line.Args[1] joinChan := make(chan interface{}) go func() { defer conn.HandleFunc("join", func(conn *client.Conn, line *client.Line) { // Is this us joining somewhere? if line.Nick != conn.Me().Nick { return } // JOIN message should always have a channel sent with it if len(line.Args) < 1 { return } // Is this the channel we got invited to? if line.Args[0] != channel { return } // Yup, we're done here joinChan <- struct{}{} }).Remove() select { case <-time.After(10 * time.Second): // Oops, we timed out logging.Warn("Timed out while waiting for us to join %v", channel) return case <-joinChan: } // We have joined successfully, let's send our hello message! b.Privmsg(channel, "Hi, I'm vpn, I automatically get rid of bad "+ "IP-changing ban evading bots! I need half-op (+h/%) to do "+ "this properly, thank you!") }() // Join and wait until joined b.Join(channel) }) return plugin }
func New(b bot.Bot, whoisPlugin *whois.Plugin, isupportPlugin *isupport.Plugin, tempbanPlugin *tempban.Plugin, nickservPlugin *nickserv.Plugin) *Plugin { if whoisPlugin == nil { panic("whoisPlugin must not be nil") } if isupportPlugin == nil { panic("isupportPlugin must not be nil") } if tempbanPlugin == nil { panic("tempbanPlugin must not be nil") } if nickservPlugin == nil { panic("nickservPlugin must not be nil") } plugin := &Plugin{ bot: b, whois: whoisPlugin, isupport: isupportPlugin, tempban: tempbanPlugin, nickserv: nickservPlugin, lastCheckNicks: map[string]time.Time{}, Admins: []string{}, } b.Conn().HandleFunc("join", plugin.OnJoin) b.Commands().Add("listbans", bot.Command{ Hidden: true, Pub: true, Help: "(only admins) Lists bans the bot set in a channel.", Handler: func(e *bot.Event) { if !plugin.isAdmin(e.Line.Src) { return } channel := e.Args if len(channel) < 1 { b.Privmsg(e.Target, "Need a channel to query.") return } bans := tempbanPlugin.Bans(channel) if len(bans) == 0 { b.Privmsg(e.Target, fmt.Sprintf("No bans set for \x02%v\x02.", channel)) } else { for i, ban := range bans { b.Privmsg(e.Target, fmt.Sprintf("%4v. \x02%-41v\x02 (\x02%v\x02, expires \x02%v\x02)", i+1, ban.Hostmask, ban.Reason, humanize.Time(ban.ExpirationTime))) } } }, }) b.Commands().Add("globalban", bot.Command{ Hidden: true, Pub: true, Help: "(only admins) Globally bans a specific nickname or hostmask with given duration and reason.", Handler: func(e *bot.Event) { if !plugin.isAdmin(e.Line.Src) { return } split := strings.SplitN(e.Args, " ", 3) if len(split) < 3 { b.Privmsg(e.Target, "Need a nickname or hostmask, duration and reason to ban, in this order.") return } nick, durationStr, reason := split[0], split[1], split[2] reason = fmt.Sprintf("Manual global ban: %v", reason) duration, err := time.ParseDuration(durationStr) var banmask string if !strings.Contains(nick, "@") && !strings.Contains(nick, "!") { if err != nil { b.Privmsg(e.Target, fmt.Sprintf("Failed to parse duration: %v", err)) return } // Generate the ban mask from WHOIS information info, err := whoisPlugin.WhoIs(nick) if err != nil { b.Privmsg(e.Target, fmt.Sprintf("Can't get information about this nick: %v", err)) return } banmask = fmt.Sprintf("%v!%v@%v", "*", "*", info.Host) } else { banmask = nick nick = "" } b.Privmsg(e.Target, fmt.Sprintf("Banning \x02%v\x02 until \x02%v\x02 with reason \x02%v\x02.", banmask, humanize.Time(time.Now().Add(duration)), reason)) plugin.banGlobal(plugin.generateBan(nick, banmask, reason, duration)) }, }) b.Commands().Add("globalunban", bot.Command{ Hidden: true, Pub: true, Help: "(only admins) Globally unbans a specific nickname or hostmask.", Handler: func(e *bot.Event) { if !plugin.isAdmin(e.Line.Src) { return } if len(e.Args) <= 0 { b.Privmsg(e.Target, "Need a nickname or hostmask.") return } nick := e.Args var banmask string if !strings.Contains(nick, "@") && !strings.Contains(nick, "!") { // Generate the ban mask from WHOIS information info, err := whoisPlugin.WhoIs(nick) if err != nil { b.Privmsg(e.Target, fmt.Sprintf("Can't get information about this nick: %v", err)) return } banmask = fmt.Sprintf("%v!%v@%v", "*", "*", info.Host) } else { banmask = nick } b.Privmsg(e.Target, fmt.Sprintf("Unbanning \x02%v\x02.", banmask)) plugin.unbanGlobal(banmask) }, }) return plugin }