Skip to content

Commit

Permalink
Improve IRC status reporting
Browse files Browse the repository at this point in the history
Add a StatusTimeout config variable; make it report a different status
if timed out and not certainly not in channel; trim the RPL_WHOISCHANNELS
arguments before comparing to channel from config.
  • Loading branch information
26000 committed Mar 5, 2020
1 parent 2c6085f commit 5218ad4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
20 changes: 17 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,36 @@ const (
func ReadConfig(path string) (error, *Irc, *Telegram, *Irchuu) {
cfg, err := ini.InsensitiveLoad(path)
cfg.BlockMode = false

tg, irc, irchuu := new(Telegram), new(Irc), new(Irchuu)

err = cfg.Section("telegram").MapTo(tg)
if err != nil {
return err, irc, tg, irchuu
}

err = cfg.Section("irc").MapTo(irc)
if err != nil {
return err, irc, tg, irchuu
}

tg.Prefix = html.EscapeString(tg.Prefix)
tg.Postfix = html.EscapeString(tg.Postfix)

err = cfg.Section("irchuu").MapTo(irchuu)
if err != nil {
return err, irc, tg, irchuu
}

tg.Prefix = html.EscapeString(tg.Prefix)
tg.Postfix = html.EscapeString(tg.Postfix)

irc.IgnoreMap = map[string]bool{}
for _, nickname := range irc.IgnoreList {
irc.IgnoreMap[nickname] = true
}

if irc.StatusTimeout == 0 {
irc.StatusTimeout = 2
}

return nil, irc, tg, irchuu
}

Expand Down Expand Up @@ -210,6 +217,11 @@ announcetopic = true
# list of nicknames to ignore, i. e. messages by these users won't be relayed
ignorelist = ignoredbotnickname1,ignoredbotnickname2
# how many seconds to wait for server to return the list of channels we're on
# (needed for /status command in Telegram; increase if it says you're not in
# channel when you are)
statustimeout = 2
`
return ioutil.WriteFile(file, []byte(config), os.FileMode(0600))
}
Expand Down Expand Up @@ -258,6 +270,8 @@ type Irc struct {
IgnoreList []string
IgnoreMap map[string]bool

StatusTimeout int

Debug bool
}

Expand Down
21 changes: 14 additions & 7 deletions irc/irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,23 +593,30 @@ func listenAlways(r *relay.Relay) {
var receivedInfo, inChannel bool
text := "IRC bot is online and "

cb := ircConn.AddCallback("319", func(event *irc.Event) {
receivedInfo = true
chansCb := ircConn.AddCallback("319", func(event *irc.Event) {
for _, v := range event.Arguments {
if v == ircConf.Channel {
if strings.Trim(v, " ") == ircConf.Channel {
inChannel = true
}
}
})

endCb := ircConn.AddCallback("318", func(event *irc.Event) {
receivedInfo = true
})

ircConn.Whois(ircConn.GetNick())
time.Sleep(time.Duration(1) * time.Second)
ircConn.RemoveCallback("319", cb)
time.Sleep(time.Duration(ircConf.StatusTimeout) * time.Second)
ircConn.RemoveCallback("319", chansCb)
ircConn.RemoveCallback("318", endCb)

if inChannel {
switch {
case inChannel:
text += "present in channel."
} else {
case receivedInfo:
text += "(almost certainly) not in channel."
case !receivedInfo:
text += "unable to determine if it's in channel."
}

r.IRCServiceCh <- relay.ServiceMessage{"announce",
Expand Down

0 comments on commit 5218ad4

Please sign in to comment.