Browse Source

bot: Added more logging of errors and such, exit on ERROR packet and configurable autorun commands on ready and channel op join.

master 0.3.0
Gisle Aune 6 years ago
parent
commit
e68503c72e
  1. 18
      internal/bot/bot.go
  2. 25
      internal/bot/channel.go
  3. 17
      internal/bot/handler.go
  4. 4
      internal/config/config.go

18
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)

25
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:

17
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() {

4
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

Loading…
Cancel
Save