From 0d5b08147d5d28838f3bd364ac4064b1cfa6b803 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Fri, 31 May 2019 10:40:40 +0200 Subject: [PATCH] Added support for private messages. --- feed-to-muc.go | 108 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 74 insertions(+), 34 deletions(-) diff --git a/feed-to-muc.go b/feed-to-muc.go index 337d35d..f4d7e50 100644 --- a/feed-to-muc.go +++ b/feed-to-muc.go @@ -224,46 +224,78 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str 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]) + var command string - // 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. + // Check for room mention of the bots nick if the the message type is groupchat. + if v.Type == "groupchat" { + // Get first word of the message and transform it to lower case. + 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) { - break + 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 + } } + // As the first word is the mention of the bots nickname, the command is + // the second word in a groupchat message. + command = strings.ToLower(strings.Split(v.Text, " ")[1]) + // If the message type is chat (e.g. private message), the command is the + // first word. + } else if v.Type == "chat" { + command = strings.ToLower(strings.Split(v.Text, " ")[0]) + } else { + break } - // Check for the second word (command). - switch strings.ToLower(strings.Split(v.Text, " ")[1]) { + // Check for the command. + switch command { // 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\": List feeds I'm following.\n" + - "\"source\": Show source code URL."}) - if err != nil { - log.Fatal("Error: Failed sending message to MUC:", err) + if v.Type == "groupchat" { + _, err = client.Send(xmpp.Chat{Remote: muc, + Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + + ": The following commands are available:\n" + + "\"feeds\": List feeds I'm following.\n" + + "\"source\": Show source code URL."}) + if err != nil { + log.Fatal("Error: Failed sending message to MUC:", err) + } + } else if v.Type == "chat" { + _, err = client.Send(xmpp.Chat{Remote: v.Remote, + Type: "chat", Text: "The following commands are available:\n" + + "\"feeds\": List feeds I'm following.\n" + + "\"source\": Show source code URL."}) + if err != nil { + log.Fatal("Error: Failed sending message to ", v.Remote, ": ", 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("Error: Failed sending message to MUC:", err) + if v.Type == "groupchat" { + _, 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("Error: Failed sending message to MUC:", err) + } + } else if v.Type == "chat" { + _, err = client.Send(xmpp.Chat{Remote: v.Remote, + Type: "chat", Text: "My source can be found at " + + "https://salsa.debian.org/mdosch-guest/feed-to-muc"}) + if err != nil { + log.Fatal("Error: Failed sending message to ", v.Remote, ": ", err) + } } // Reply with the list of monitored feeds for `feeds`. case "feeds": @@ -273,11 +305,19 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str 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("Error: Failed sending message to MUC:", err) + if v.Type == "groupchat" { + _, 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("Error: Failed sending message to MUC:", err) + } + } else if v.Type == "chat" { + _, err = client.Send(xmpp.Chat{Remote: v.Remote, + Type: "chat", Text: "Feeds I'm following:\n" + feedList}) + if err != nil { + log.Fatal("Error: Failed sending message to ", v.Remote, ": ", err) + } } }