Added support for private messages.

This commit is contained in:
Martin Dosch 2019-05-31 10:40:40 +02:00
parent 800db2616e
commit 377d469179

View file

@ -225,46 +225,78 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
switch v := stanza.(type) { switch v := stanza.(type) {
// Reply to requests for source and feeds. // Reply to requests for source and feeds.
case xmpp.Chat: case xmpp.Chat:
// Get first word of the message and transform it to lower case. var command string
var mention string
mention = strings.ToLower(strings.Split(v.Text, " ")[0])
// If it is not the bots nick remove one trailing character as // Check for room mention of the bots nick if the the message type is groupchat.
// a lot of clients append `:` or `,` to mentions. if v.Type == "groupchat" {
if mention != strings.ToLower(mucNick) { // Get first word of the message and transform it to lower case.
mentionLength := len(mention) mention := strings.ToLower(strings.Split(v.Text, " ")[0])
// Leave if mentionLength is <= 0
if mentionLength <= 0 { // If it is not the bots nick remove one trailing character as
break // a lot of clients append `:` or `,` to mentions.
}
mention = mention[:mentionLength-1]
// Leave if the message is not addressed to the bot.
if mention != strings.ToLower(mucNick) { 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). // Check for the command.
switch strings.ToLower(strings.Split(v.Text, " ")[1]) { switch command {
// Reply with a short summary of available commands for `help`. // Reply with a short summary of available commands for `help`.
case "help": case "help":
_, err = client.Send(xmpp.Chat{Remote: muc, if v.Type == "groupchat" {
Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + _, err = client.Send(xmpp.Chat{Remote: muc,
": The following commands are available:\n" + Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] +
"\"feeds\": List feeds I'm following.\n" + ": The following commands are available:\n" +
"\"source\": Show source code URL."}) "\"feeds\": List feeds I'm following.\n" +
if err != nil { "\"source\": Show source code URL."})
log.Fatal("Error: Failed sending message to MUC:", err) 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`. // Reply with repo address for `source`.
case "source": case "source":
_, err = client.Send(xmpp.Chat{Remote: muc, if v.Type == "groupchat" {
Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + _, err = client.Send(xmpp.Chat{Remote: muc,
": My source can be found at " + Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] +
"https://salsa.debian.org/mdosch-guest/feed-to-muc"}) ": My source can be found at " +
if err != nil { "https://salsa.debian.org/mdosch-guest/feed-to-muc"})
log.Fatal("Error: Failed sending message to MUC:", err) 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`. // Reply with the list of monitored feeds for `feeds`.
case "feeds": case "feeds":
@ -274,11 +306,19 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
feedList = feedList + feed + "\n" feedList = feedList + feed + "\n"
} }
_, err = client.Send(xmpp.Chat{Remote: muc, if v.Type == "groupchat" {
Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + _, err = client.Send(xmpp.Chat{Remote: muc,
": Feeds I'm following:\n" + feedList}) Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] +
if err != nil { ": Feeds I'm following:\n" + feedList})
log.Fatal("Error: Failed sending message to MUC:", err) 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)
}
} }
} }