Commit 6488fb16 authored by shammash's avatar shammash

add simple kick/join logic, untested

parent 0f63e8d7
Pipeline #972 passed with stages
in 1 minute 8 seconds
......@@ -21,6 +21,11 @@ func loggerHandler(_ *irc.Conn, line *irc.Line) {
log.Printf("Received: '%s'", line.Raw)
}
type ChannelState struct {
Channel IRCChannel
BackoffCounter Delayer
}
type IRCNotifier struct {
// Nick stores the nickname specified in the config, because irc.Client
// might change its copy.
......@@ -41,7 +46,7 @@ type IRCNotifier struct {
sessionDownSignal chan bool
PreJoinChannels []IRCChannel
JoinedChannels []IRCChannel
JoinedChannels map[string]ChannelState
NickservDelayWait time.Duration
BackoffCounter Delayer
......@@ -74,7 +79,7 @@ func NewIRCNotifier(config *Config, alertNotices chan AlertNotice) (*IRCNotifier
sessionUpSignal: make(chan bool),
sessionDownSignal: make(chan bool),
PreJoinChannels: config.IRCChannels,
JoinedChannels: nil,
JoinedChannels: make(map[string]ChannelState),
NickservDelayWait: nickservWaitSecs * time.Second,
BackoffCounter: backoffCounter,
}
......@@ -91,28 +96,54 @@ func NewIRCNotifier(config *Config, alertNotices chan AlertNotice) (*IRCNotifier
notifier.sessionDownSignal <- false
})
for _, event := range []string{irc.NOTICE, irc.KICK, "433"} {
notifier.Client.HandleFunc(irc.KICK,
func(_ *irc.Conn, line *irc.Line) {
notifier.HandleKick(line.Args[1], line.Args[0])
})
for _, event := range []string{irc.NOTICE, "433"} {
notifier.Client.HandleFunc(event, loggerHandler)
}
return notifier, nil
}
func (notifier *IRCNotifier) HandleKick(nick string, channel string) {
if nick != notifier.Client.Me().Nick {
// received kick info for somebody else
return
}
state, ok := notifier.JoinedChannels[channel]
if ok == false {
log.Printf("Being kicked out of non-joined channel (%s), ignoring", channel)
return
}
log.Printf("Being kicked out of %s, re-joining", channel)
go func() {
state.BackoffCounter.Delay()
notifier.Client.Join(state.Channel.Name, state.Channel.Password)
}()
}
func (notifier *IRCNotifier) CleanupChannels() {
log.Printf("Deregistering all channels.")
notifier.JoinedChannels = nil
notifier.JoinedChannels = make(map[string]ChannelState)
}
func (notifier *IRCNotifier) JoinChannel(channel *IRCChannel) {
for _, joinedChannel := range notifier.JoinedChannels {
if channel.Name == joinedChannel.Name {
return
}
if _, joined := notifier.JoinedChannels[channel.Name]; joined == true {
return
}
log.Printf("Joining %s", channel.Name)
notifier.Client.Join(channel.Name, channel.Password)
joinedChannel := *channel
notifier.JoinedChannels = append(notifier.JoinedChannels, joinedChannel)
state := ChannelState{
Channel: *channel,
BackoffCounter: NewBackoff(
ircConnectMaxBackoffSecs, ircConnectBackoffResetSecs,
time.Second),
}
notifier.JoinedChannels[channel.Name] = state
}
func (notifier *IRCNotifier) JoinChannels() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment