IRC connection and message sending, untested
This commit is contained in:
parent
704d34d241
commit
a84695ee41
57
irc.go
57
irc.go
|
|
@ -3,19 +3,23 @@ package main
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
"slices"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/lrstanley/girc"
|
"github.com/lrstanley/girc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IrcBot struct {
|
type IrcBot struct {
|
||||||
client *girc.Client
|
client *girc.Client
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
s SatelNameGetter
|
s SatelNameGetter
|
||||||
config AppConfig
|
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 {
|
if config.Irc == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -26,13 +30,48 @@ func MakeIrcBot(config AppConfig, logger *log.Logger, s SatelNameGetter) *IrcBot
|
||||||
User: config.Irc[0].User,
|
User: config.Irc[0].User,
|
||||||
Name: config.Irc[0].Name,
|
Name: config.Irc[0].Name,
|
||||||
Out: logger.Writer(),
|
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 {
|
func (bot *IrcBot) Connect() {
|
||||||
return []girc.Channel{}
|
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) {
|
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
|
// do it only if we have more than one message to send
|
||||||
time.Sleep(time.Millisecond * 500)
|
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,
|
sendToMatterbridge := MakeSendToMatterbridgeSync(s, config,
|
||||||
log.New(os.Stderr, "SendToMatterbridge", log.Lmicroseconds),
|
log.New(os.Stderr, "SendToMatterbridge", log.Lmicroseconds),
|
||||||
ircTpl)
|
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).
|
collect.Then(filterByLastSeen).
|
||||||
Then(filterByTypeOrIndex).
|
Then(filterByTypeOrIndex).
|
||||||
|
|
@ -102,7 +108,8 @@ func main() {
|
||||||
convert.ConvertTo(notifyViaHttp).
|
convert.ConvertTo(notifyViaHttp).
|
||||||
Then(throttle).
|
Then(throttle).
|
||||||
Then(sendToTg).
|
Then(sendToTg).
|
||||||
Then(sendToMatterbridge)
|
Then(sendToMatterbridge).
|
||||||
|
Then(sendToIrc)
|
||||||
|
|
||||||
collect.Collect(s.Events, &wg, func() {})
|
collect.Collect(s.Events, &wg, func() {})
|
||||||
|
|
||||||
|
|
@ -114,6 +121,9 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Print("Closing...")
|
logger.Print("Closing...")
|
||||||
|
if ircBot != nil {
|
||||||
|
ircBot.Close()
|
||||||
|
}
|
||||||
close(closeDebugTools)
|
close(closeDebugTools)
|
||||||
close(tgEvents)
|
close(tgEvents)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,18 @@ func (sendToTg *SendToTelegramSync) Call(msg GenericMessage) {
|
||||||
sendToTg.CallNext(msg)
|
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 {
|
type SendToMatterbridgeSync struct {
|
||||||
SyncFilterImpl[GenericMessage]
|
SyncFilterImpl[GenericMessage]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue