IRC connection and message sending, untested
This commit is contained in:
parent
704d34d241
commit
a84695ee41
49
irc.go
49
irc.go
|
|
@ -3,6 +3,8 @@ package main
|
|||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/lrstanley/girc"
|
||||
|
|
@ -13,9 +15,11 @@ type IrcBot struct {
|
|||
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
12
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()
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue