Commit 6488fb16 authored by shammash's avatar shammash

add simple kick/join logic, untested

Signed-off-by: shammash's avatarshammash <shammash@autistici.org>
parent 0f63e8d7
Pipeline #972 passed with stages
in 1 minute and 8 seconds
...@@ -21,6 +21,11 @@ func loggerHandler(_ *irc.Conn, line *irc.Line) { ...@@ -21,6 +21,11 @@ func loggerHandler(_ *irc.Conn, line *irc.Line) {
log.Printf("Received: '%s'", line.Raw) log.Printf("Received: '%s'", line.Raw)
} }
type ChannelState struct {
Channel IRCChannel
BackoffCounter Delayer
}
type IRCNotifier struct { type IRCNotifier struct {
// Nick stores the nickname specified in the config, because irc.Client // Nick stores the nickname specified in the config, because irc.Client
// might change its copy. // might change its copy.
...@@ -41,7 +46,7 @@ type IRCNotifier struct { ...@@ -41,7 +46,7 @@ type IRCNotifier struct {
sessionDownSignal chan bool sessionDownSignal chan bool
PreJoinChannels []IRCChannel PreJoinChannels []IRCChannel
JoinedChannels []IRCChannel JoinedChannels map[string]ChannelState
NickservDelayWait time.Duration NickservDelayWait time.Duration
BackoffCounter Delayer BackoffCounter Delayer
...@@ -74,7 +79,7 @@ func NewIRCNotifier(config *Config, alertNotices chan AlertNotice) (*IRCNotifier ...@@ -74,7 +79,7 @@ func NewIRCNotifier(config *Config, alertNotices chan AlertNotice) (*IRCNotifier
sessionUpSignal: make(chan bool), sessionUpSignal: make(chan bool),
sessionDownSignal: make(chan bool), sessionDownSignal: make(chan bool),
PreJoinChannels: config.IRCChannels, PreJoinChannels: config.IRCChannels,
JoinedChannels: nil, JoinedChannels: make(map[string]ChannelState),
NickservDelayWait: nickservWaitSecs * time.Second, NickservDelayWait: nickservWaitSecs * time.Second,
BackoffCounter: backoffCounter, BackoffCounter: backoffCounter,
} }
...@@ -91,28 +96,54 @@ func NewIRCNotifier(config *Config, alertNotices chan AlertNotice) (*IRCNotifier ...@@ -91,28 +96,54 @@ func NewIRCNotifier(config *Config, alertNotices chan AlertNotice) (*IRCNotifier
notifier.sessionDownSignal <- false 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) notifier.Client.HandleFunc(event, loggerHandler)
} }
return notifier, nil 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() { func (notifier *IRCNotifier) CleanupChannels() {
log.Printf("Deregistering all channels.") log.Printf("Deregistering all channels.")
notifier.JoinedChannels = nil notifier.JoinedChannels = make(map[string]ChannelState)
} }
func (notifier *IRCNotifier) JoinChannel(channel *IRCChannel) { func (notifier *IRCNotifier) JoinChannel(channel *IRCChannel) {
for _, joinedChannel := range notifier.JoinedChannels { if _, joined := notifier.JoinedChannels[channel.Name]; joined == true {
if channel.Name == joinedChannel.Name { return
return
}
} }
log.Printf("Joining %s", channel.Name) log.Printf("Joining %s", channel.Name)
notifier.Client.Join(channel.Name, channel.Password) notifier.Client.Join(channel.Name, channel.Password)
joinedChannel := *channel state := ChannelState{
notifier.JoinedChannels = append(notifier.JoinedChannels, joinedChannel) Channel: *channel,
BackoffCounter: NewBackoff(
ircConnectMaxBackoffSecs, ircConnectBackoffResetSecs,
time.Second),
}
notifier.JoinedChannels[channel.Name] = state
} }
func (notifier *IRCNotifier) JoinChannels() { 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