From e68503c72ea138fb3ddd7bd550c41476f7be8232 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sun, 2 Dec 2018 17:38:04 +0100 Subject: [PATCH] bot: Added more logging of errors and such, exit on ERROR packet and configurable autorun commands on ready and channel op join. --- internal/bot/bot.go | 18 +++++++++++++++++- internal/bot/channel.go | 25 +++++++++++++++++++++++-- internal/bot/handler.go | 17 +++++++++++++++-- internal/config/config.go | 4 ++++ 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/internal/bot/bot.go b/internal/bot/bot.go index 2be8cbf..5d64c80 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -3,6 +3,7 @@ package bot import ( "context" "log" + "strings" "time" "git.aiterp.net/gisle/irc" @@ -78,7 +79,7 @@ func (bot *Bot) addChannel(channelName string) *Channel { return bot.channels[channelName] } - channel := newChannel(bot.ctx, channelName, bot.client) + channel := newChannel(bot, channelName, bot.client) go channel.loop() bot.channels[channelName] = channel @@ -93,6 +94,21 @@ func (bot *Bot) handlePost(channelName string, post ChannelPost) { channel.ch <- post } +func (bot *Bot) runCommands(commands []string, target irc.Target, replacers map[string]string) { + if replacers == nil { + replacers = make(map[string]string) + } + replacers["me"] = bot.client.Nick() + + for _, command := range commands { + for from, to := range replacers { + command = strings.Replace(command, "%"+from, to, -1) + } + + bot.client.EmitInput(command, target) + } +} + func (bot *Bot) loop() { bot.loopCtx, bot.loopCancel = context.WithCancel(bot.ctx) diff --git a/internal/bot/channel.go b/internal/bot/channel.go index d872076..55525eb 100644 --- a/internal/bot/channel.go +++ b/internal/bot/channel.go @@ -7,6 +7,7 @@ import ( "time" "git.aiterp.net/gisle/irc" + "git.aiterp.net/rpdata/logbot3/internal/config" "git.aiterp.net/rpdata/logbot3/internal/models/channels" "git.aiterp.net/rpdata/logbot3/internal/models/logs" "git.aiterp.net/rpdata/logbot3/internal/models/posts" @@ -23,15 +24,17 @@ type Channel struct { name string parentCtx context.Context + parent *Bot client *irc.Client } -func newChannel(parentCtx context.Context, name string, client *irc.Client) *Channel { +func newChannel(parent *Bot, name string, client *irc.Client) *Channel { ctx, cancel := context.WithCancel(context.Background()) return &Channel{ name: name, - parentCtx: parentCtx, + parent: parent, + parentCtx: parent.ctx, client: client, ch: make(chan ChannelPost, 8), @@ -47,6 +50,7 @@ func (channel *Channel) loop() { defer channel.cancel() defer minutely.Stop() + // In case of a crash, it should take ownership of sessions when rejoining. session, err := logs.FindOpen(channel.parentCtx, channel.name) if err == nil { minsUntilDeadline := 1 + int(((time.Hour * 2) - time.Since(session.LatestTime())).Minutes()) @@ -60,6 +64,23 @@ func (channel *Channel) loop() { channel.lastPostTime = session.LatestTime() } + // If the bot is op, run the commands from the configuration. + go func() { + time.Sleep(time.Second) + + commands := config.Get().Commands.OnJoinOp + target := channel.client.Channel(channel.name) + + if len(commands) > 0 && target != nil { + me, ok := target.UserList().User(channel.client.Nick()) + if ok && strings.ContainsRune(me.Modes, 'o') { + channel.parent.runCommands(commands, target, map[string]string{ + "chan": channel.name, + }) + } + } + }() + for { select { case post := <-channel.ch: diff --git a/internal/bot/handler.go b/internal/bot/handler.go index f307cf0..f357c00 100644 --- a/internal/bot/handler.go +++ b/internal/bot/handler.go @@ -2,6 +2,7 @@ package bot import ( "log" + "os" "strings" "git.aiterp.net/rpdata/logbot3/internal/util" @@ -22,6 +23,8 @@ func handler(event *irc.Event, client *irc.Client) { { log.Println("Client is ready!") go bot.loop() + + bot.runCommands(config.Get().Commands.OnReady, bot.client.Status(), nil) } case "client.disconnect": { @@ -114,11 +117,21 @@ func handler(event *irc.Event, client *irc.Client) { { log.Printf("(%s) %s\n", event.Verb(), event.Text) } - case "packet.005", "packet.254", "packet.cap", "packet.431", "packet.432", "packet.433", "packet.436": + case "packet.005", "packet.cap", "packet.254": + { + log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args[1:], " "), event.Text) + } + case "packet.error": { - log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args, " "), event.Text) + log.Printf("(%s) %s\n", event.Verb(), event.Text) + os.Exit(1) } } + + // Always log error numerics. + if len(event.Verb()) == 3 && event.Verb()[0] == '4' || event.Verb()[0] == '9' { + log.Printf("(%s) %s %s\n", event.Verb(), strings.Join(event.Args, " "), event.Text) + } } func init() { diff --git a/internal/config/config.go b/internal/config/config.go index 382a8aa..288afcf 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -26,6 +26,10 @@ type Config struct { Secret string `json:"secret"` } `json:"key"` } `json:"api"` + Commands struct { + OnJoinOp []string `json:"onJoinOp"` + OnReady []string `json:"onReady"` + } } var mutex sync.Mutex