From a84695ee413c19d8be1b68ef916059a50dda7c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Sun, 23 Mar 2025 23:29:44 +0100 Subject: [PATCH] IRC connection and message sending, untested --- irc.go | 57 ++++++++++++++++++++++++++++++++++++++++++-------- main.go | 12 ++++++++++- sender_sync.go | 12 +++++++++++ 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/irc.go b/irc.go index 620d6ba..6030933 100644 --- a/irc.go +++ b/irc.go @@ -3,19 +3,23 @@ package main import ( "html/template" "log" + "slices" + "sync" "time" "github.com/lrstanley/girc" ) type IrcBot struct { - client *girc.Client - logger *log.Logger - s SatelNameGetter - config AppConfig + client *girc.Client + logger *log.Logger + s SatelNameGetter + config AppConfig + wg *sync.WaitGroup + connected bool } -func MakeIrcBot(config AppConfig, logger *log.Logger, s SatelNameGetter) *IrcBot { +func MakeIrcBot(config AppConfig, logger *log.Logger, s SatelNameGetter, wg *sync.WaitGroup) *IrcBot { if config.Irc == nil { return nil } @@ -26,13 +30,48 @@ func MakeIrcBot(config AppConfig, logger *log.Logger, s SatelNameGetter) *IrcBot User: config.Irc[0].User, Name: config.Irc[0].Name, Out: logger.Writer(), + SSL: config.Irc[0].SSL, + SASL: &girc.SASLPlain{ + User: config.Irc[0].SaslUser, + Pass: config.Irc[0].SaslPass, + }, }) + client.Cmd.Join(config.Irc[0].Channels...) - return &IrcBot{client, logger, s, config} + return &IrcBot{client, logger, s, config, wg, false} } -func (bot *IrcBot) GetInterestingChannels() []girc.Channel { - return []girc.Channel{} +func (bot *IrcBot) Connect() { + if bot.connected { + log.Fatal("Tried to connect again to an already connected IRC session! This is a bug.") + } + bot.connected = true + bot.wg.Add(1) + go func() { + for { + if err := bot.client.Connect(); err != nil { + log.Printf("Connect error: %s, reconnecting in 30 seconds...", err) + time.Sleep(30 * time.Second) + } else { + bot.wg.Done() + return + } + } + }() +} + +func (bot *IrcBot) Close() { + bot.client.Close() +} + +func (bot IrcBot) filterOutNotConfiguredChannels(in []string) []string { + return slices.DeleteFunc(in, func(channel string) bool { + return !slices.Contains(bot.config.Irc[0].Channels, channel) + }) +} + +func (bot *IrcBot) GetInterestingChannels() []string { + return bot.filterOutNotConfiguredChannels(bot.client.ChannelList()) } func (bot *IrcBot) SendMessageToAllChannels(msg string) { @@ -42,7 +81,7 @@ func (bot *IrcBot) SendMessageToAllChannels(msg string) { // do it only if we have more than one message to send time.Sleep(time.Millisecond * 500) } - bot.client.Cmd.Message(channel.Name, msg) + bot.client.Cmd.Message(channel, msg) } } diff --git a/main.go b/main.go index a7f255d..252a381 100644 --- a/main.go +++ b/main.go @@ -94,6 +94,12 @@ func main() { sendToMatterbridge := MakeSendToMatterbridgeSync(s, config, log.New(os.Stderr, "SendToMatterbridge", log.Lmicroseconds), ircTpl) + ircBot := MakeIrcBot(config, log.New(os.Stderr, "IrcBot", log.Lmicroseconds), s, &wg) + var sendToIrc SyncFilter[GenericMessage] = MakeSendNowhere() + if ircBot != nil { + ircBot.Connect() + sendToIrc = ircBot.GetNotifyViaIRC(ircTpl) + } collect.Then(filterByLastSeen). Then(filterByTypeOrIndex). @@ -102,7 +108,8 @@ func main() { convert.ConvertTo(notifyViaHttp). Then(throttle). Then(sendToTg). - Then(sendToMatterbridge) + Then(sendToMatterbridge). + Then(sendToIrc) collect.Collect(s.Events, &wg, func() {}) @@ -114,6 +121,9 @@ func main() { } logger.Print("Closing...") + if ircBot != nil { + ircBot.Close() + } close(closeDebugTools) close(tgEvents) wg.Wait() diff --git a/sender_sync.go b/sender_sync.go index d9fbb01..8bcfba0 100644 --- a/sender_sync.go +++ b/sender_sync.go @@ -35,6 +35,18 @@ func (sendToTg *SendToTelegramSync) Call(msg GenericMessage) { sendToTg.CallNext(msg) } +type SendNowhere struct { + SyncFilterImpl[GenericMessage] +} + +func MakeSendNowhere() *SendNowhere { + return &SendNowhere{SyncFilterImpl[GenericMessage]{}} +} + +func (sendNowhere *SendNowhere) Call(msg GenericMessage) { + sendNowhere.CallNext(msg) +} + type SendToMatterbridgeSync struct { SyncFilterImpl[GenericMessage]