The new logbot, not committed from the wrong terminal window this time.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

179 lines
3.9 KiB

package bot
import (
"context"
"log"
"strings"
"time"
"git.aiterp.net/rpdata/logbot3/internal/models/channels"
"github.com/gissleh/irc"
)
var botKey = "git.aiterp.net/rpdata/logbot.Bot.key"
// The Bot is the IRC client.
type Bot struct {
client *irc.Client
ctx context.Context
ctxCancel context.CancelFunc
loopCtx context.Context
loopCancel context.CancelFunc
channels map[string]*Channel
}
// New creates a new Bot.
func New(ctx context.Context, nick string, alternatives []string, user string, realName string) *Bot {
client := irc.New(ctx, irc.Config{
Nick: nick,
User: user,
RealName: realName,
Alternatives: alternatives,
SendRate: 2,
SkipSSLVerification: false,
})
client.AddHandler(handler)
bot := &Bot{
client: client,
channels: make(map[string]*Channel),
}
client.SetValue(botKey, bot)
return bot
}
// Connect connects the bot to the IRC server. This will disconnect already
// established connections.
func (bot *Bot) Connect(server string, ssl bool, maxRetries int) (err error) {
if bot.ctxCancel != nil {
bot.ctxCancel()
}
bot.ctx, bot.ctxCancel = context.WithCancel(bot.client.Context())
bot.channels = make(map[string]*Channel)
retries := 0
for maxRetries == 0 || retries < maxRetries {
err = bot.client.Connect(server, ssl)
if err != nil {
log.Println("Connect failed:", err.Error())
if maxRetries > 0 && retries < maxRetries {
retries++
log.Printf("Retrying in 5s (Retry %d/%d)", retries, maxRetries)
} else {
log.Println("Retrying in 5s (No retry limit)")
}
time.Sleep(time.Second * 10)
continue
}
return nil
}
return err
}
func (bot *Bot) ClientState() irc.ClientState {
return bot.client.State()
}
func (bot *Bot) addChannel(channelName string) *Channel {
if bot.channels[channelName] != nil {
return bot.channels[channelName]
}
channel := newChannel(bot, channelName, bot.client)
go channel.loop()
bot.channels[channelName] = channel
return channel
}
func (bot *Bot) handlePost(channelName string, post ChannelPost) {
channel := bot.channels[channelName]
if channel == nil {
channel = bot.addChannel(channelName)
}
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)
ticker := time.NewTicker(time.Second * 10)
defer ticker.Stop()
for {
select {
case <-ticker.C:
{
loggedChannels, err := channels.ListLogged(bot.ctx)
if err != nil {
log.Println("Failed to list logged channels:", err)
continue
}
loggedMap := make(map[string]bool)
for _, channel := range loggedChannels {
loggedMap[channel.Name] = true
}
clientMap := make(map[string]bool)
for _, channel := range bot.client.Channels() {
clientMap[channel.Name()] = true
}
joins := make([]string, 0, len(loggedMap))
parts := make([]string, 0, len(clientMap))
for key := range loggedMap {
if !clientMap[key] {
joins = append(joins, key)
}
}
for key := range clientMap {
if !loggedMap[key] {
parts = append(parts, key)
}
}
if len(parts) > 0 {
log.Println("Leaving", strings.Join(parts, ", "))
bot.client.Part(parts...)
}
if len(joins) > 0 {
log.Println("Joining", strings.Join(joins, ", "))
bot.client.Join(joins...)
}
}
case <-bot.loopCtx.Done():
{
log.Println("Spinning down bot loop.")
return
}
}
}
}
func (bot *Bot) stopLoop() {
if bot.loopCancel != nil {
bot.loopCancel()
}
}