1
0
Fork 0

IRC connection and message sending, untested

This commit is contained in:
Michał Rudowicz 2025-03-23 23:29:44 +01:00
parent 704d34d241
commit a84695ee41
3 changed files with 71 additions and 10 deletions

57
irc.go
View File

@ -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)
}
}

12
main.go
View File

@ -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()

View File

@ -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]