66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/lrstanley/girc"
|
|
)
|
|
|
|
type IrcBot struct {
|
|
client *girc.Client
|
|
logger *log.Logger
|
|
s SatelNameGetter
|
|
config AppConfig
|
|
}
|
|
|
|
func MakeIrcBot(config AppConfig, logger *log.Logger, s SatelNameGetter) *IrcBot {
|
|
if config.Irc == nil {
|
|
return nil
|
|
}
|
|
client := girc.New(girc.Config{
|
|
Server: config.Irc[0].Server,
|
|
Port: config.Irc[0].Port,
|
|
Nick: config.Irc[0].Nick,
|
|
User: config.Irc[0].User,
|
|
Name: config.Irc[0].Name,
|
|
Out: logger.Writer(),
|
|
})
|
|
|
|
return &IrcBot{client, logger, s, config}
|
|
}
|
|
|
|
func (bot *IrcBot) GetInterestingChannels() []girc.Channel {
|
|
return []girc.Channel{}
|
|
}
|
|
|
|
func (bot *IrcBot) SendMessageToAllChannels(msg string) {
|
|
for i, channel := range bot.GetInterestingChannels() {
|
|
if i != 0 {
|
|
// to avoid sending too many messages at once
|
|
// do it only if we have more than one message to send
|
|
time.Sleep(time.Millisecond * 500)
|
|
}
|
|
bot.client.Cmd.Message(channel.Name, msg)
|
|
}
|
|
}
|
|
|
|
type NotifyViaIRCSync struct {
|
|
SyncFilterImpl[GenericMessage]
|
|
|
|
bot *IrcBot
|
|
tpl *template.Template
|
|
}
|
|
|
|
func (bot *IrcBot) GetNotifyViaIRC(tpl *template.Template) *NotifyViaIRCSync {
|
|
return &NotifyViaIRCSync{SyncFilterImpl[GenericMessage]{}, bot, tpl}
|
|
}
|
|
|
|
func (notifyViaIRC *NotifyViaIRCSync) Call(msg GenericMessage) {
|
|
notifyViaIRC.bot.SendMessageToAllChannels(
|
|
msg.Format(notifyViaIRC.tpl, notifyViaIRC.bot.s, notifyViaIRC.bot.logger))
|
|
|
|
notifyViaIRC.CallNext(msg)
|
|
}
|