From c548cdf277319bb06c86718f4034059806075ebe Mon Sep 17 00:00:00 2001 From: mdosch Date: Sun, 26 May 2019 20:20:34 +0200 Subject: [PATCH] Added a few interactions (help, source, feeds). --- feed-to-muc.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/feed-to-muc.go b/feed-to-muc.go index 4594ea8..ded5462 100644 --- a/feed-to-muc.go +++ b/feed-to-muc.go @@ -138,7 +138,7 @@ func main() { go pingMUC(client, config.BotJid, config.Muc, config.MucNick) // Starting goroutine to process received stanzas. - go processStanzas(client) + go processStanzas(client, config.Muc, config.MucNick, config.Feeds) // Set RefreshTime to 30 seconds if not defined. if config.RefreshTime == 0 { @@ -171,12 +171,12 @@ func main() { } // Send a ping every 30 seconds after last successful ping to check if the MUC is still available. -func pingMUC(client *xmpp.Client, botJid string, Muc string, MucNick string) { +func pingMUC(client *xmpp.Client, botJid string, muc string, mucNick string) { for { time.Sleep(30 * time.Second) // Send ping to own MUC participant to check we are still joined. - id, err = client.RawInformation(botJid, Muc+"/"+MucNick, sid.Id(), + id, err = client.RawInformation(botJid, muc+"/"+mucNick, sid.Id(), "get", "") if err != nil { log.Fatal(err) @@ -213,15 +213,76 @@ func pingServer(client *xmpp.Client, server string, botJid string) { } } -func processStanzas(client *xmpp.Client) { +func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []string) { for { // Receive stanzas. ToDo: Receive stanzas continiously without 30s delay. stanza, err := client.Recv() if err != nil { log.Fatal(err) } - // Check IQs for ping results and disco#info queries. + // Check stanzas, maybe we want to reply. switch v := stanza.(type) { + // Reply to requests for source and feeds. + case xmpp.Chat: + // Get first word of the message and transform it to lower case. + var mention string + mention = strings.ToLower(strings.Split(v.Text, " ")[0]) + + // If it is not the bots nick remove one trailing character as + // a lot of clients append `:` or `,` to mentions. + if mention != strings.ToLower(mucNick) { + mentionLength := len(mention) + // Leave if mentionLength is <= 0 + if mentionLength <= 0 { + break + } + mention = mention[:mentionLength-1] + // Leave if the message is not addressed to the bot. + if mention != strings.ToLower(mucNick) { + break + } + } + + // Check for the second word (command). + switch strings.ToLower(strings.Split(v.Text, " ")[1]) { + + // Reply with a short summary of available commands for `help`. + case "help": + _, err = client.Send(xmpp.Chat{Remote: muc, + Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + + ": The following commands are available:\n" + + "feeds\t" + "List feeds I'm following.\n" + + "source\t" + "Show source code URL."}) + if err != nil { + log.Fatal(err) + } + // Reply with repo address for `source`. + case "source": + _, err = client.Send(xmpp.Chat{Remote: muc, + Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + + ": My source can be found at " + + "https://salsa.debian.org/mdosch-guest/feed-to-muc"}) + if err != nil { + log.Fatal(err) + } + // Reply with the list of monitored feeds for `feeds`. + case "feeds": + var feedList string + for _, feed := range feeds { + // Add next feed element and a newline. + feedList = feedList + feed + "\n" + } + + _, err = client.Send(xmpp.Chat{Remote: muc, + Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + + ": Feeds I'm following:\n" + feedList}) + if err != nil { + log.Fatal(err) + } + + } + + // Reply to pings and disco queries. case xmpp.IQ: if (v.Type == "error") && (v.ID == id) { log.Fatal("MUC not available.")