Skip to content

Commit

Permalink
Add a status command to find out how the other side is doing (fixes #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
26000 committed Mar 5, 2020
1 parent aedfecc commit ddaf777
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
46 changes: 40 additions & 6 deletions irc/irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ func Launch(c *config.Irc, wg *sync.WaitGroup, r *relay.Relay) {
if event.Nick == ircConn.GetNick() {
logger.Printf("Joined %v\n", event.Arguments[0])
if event.Arguments[0] == c.Channel {
go relayMessagesToIRC(r)
go listenService(r, &names)
go relayMessagesToIRC(r) // FIXME: check if already started too
go listenService(r, &names) // FIXME: should be available regardless of whether joined or not
if !nameQueryStarted {
go updateNames()
nameQueryStarted = true
Expand Down Expand Up @@ -519,7 +519,7 @@ func listenService(r *relay.Relay, names *map[string]int) {
for f := range r.TeleServiceCh {
switch f.Command {
case "break":
break
break // FIXME: breaks the switch only
case "announce":
fallthrough
case "bot":
Expand Down Expand Up @@ -555,6 +555,37 @@ func listenService(r *relay.Relay, names *map[string]int) {
ircConn.Quit()
time.Sleep(time.Second)
os.Exit(0)
case "status":
if !ircConn.Connected() {
r.IRCServiceCh <- relay.ServiceMessage{"announce",
[]string{"IRC bot is offline."}}
break
}

var receivedInfo, inChannel bool
text := "IRC bot is online and "

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

ircConn.Whois(ircConn.GetNick())
time.Sleep(time.Duration(1) * time.Second)
ircConn.RemoveCallback("319", cb)

if inChannel {
text += "present in channel."
} else {
text += "(almost certainly) not in channel."
}

r.IRCServiceCh <- relay.ServiceMessage{"announce",
[]string{text}}
}

if ircConf.FloodDelay != 0 {
Expand Down Expand Up @@ -722,14 +753,15 @@ func processCmd(event *irc.Event, r *relay.Relay, names *map[string]int) {
}
switch cmd[1] {
case "help":
texts := make([]string, 10)
texts := make([]string, 11)
texts[0] = "Available commands:"
texts[1] = ircConf.Nick + " \x02help\x0f — show this help"
texts[2] = ircConf.Nick + " \x02ops\x0f — show Telegram group ops"
texts[3] = ircConf.Nick + " \x02count\x0f — show Telegram group user count"
texts[8] = "\x02/ctcp " + ircConn.GetNick() +
texts[8] = ircConf.Nick + " \x02status\x0f — check Telegram bot status"
texts[9] = "\x02/ctcp " + ircConn.GetNick() +
" version\x0f — get version"
texts[9] = "Some of these commands are available in PM."
texts[10] = "Some of these commands are available in PM."
if ircConf.AllowStickers {
texts[7] = ircConf.Nick + " \x02sticker [id]\x0f — send a sticker"
}
Expand Down Expand Up @@ -785,6 +817,8 @@ func processCmd(event *irc.Event, r *relay.Relay, names *map[string]int) {
ircConn.Privmsg(ircConf.Channel, "Insufficient permission.")
}
}
case "status":
r.IRCServiceCh <- relay.ServiceMessage{"status", nil}
}
}

Expand Down
26 changes: 25 additions & 1 deletion telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,26 @@ func listenService(r *relay.Relay, c *config.Telegram) {
[]string{"unbanned " + f.Arguments[1] + "."},
}
}
case "status":
var text string

c := tgbotapi.ChatConfigWithUser{ChatID: c.Group, UserID: bot.Self.ID}
m, err := bot.GetChatMember(c)

if err != nil {
text = "Unable to get status: " + err.Error() + "."
} else {
if m.IsMember() {
text = "Telegram bot is online and present in group."
} else {
text = "Telegram bot is online, but not in group."
}
}

r.TeleServiceCh <- relay.ServiceMessage{
"announce",
[]string{text},
}
}
}
}
Expand Down Expand Up @@ -293,7 +313,8 @@ func processCmd(c *config.Telegram, message *tgbotapi.Message, cmd string, r *re
/help — show this help
/version — show version info
/topic — get IRC channel topic
/ops — view OPs list`
/ops — view OPs list
/status — check the IRC bot status`
if c.AllowInvites {
text += "\n/invite [nick] — invite a user to the IRC channel"
}
Expand All @@ -305,6 +326,9 @@ func processCmd(c *config.Telegram, message *tgbotapi.Message, cmd string, r *re
}
m := tgbotapi.NewMessage(c.Group, text)
sendAndReport(m)
case "status":
f := relay.ServiceMessage{"status", nil}
r.TeleServiceCh <- f
}
}

Expand Down

0 comments on commit ddaf777

Please sign in to comment.