mirror of
https://salsa.debian.org/mdosch/feed-to-muc.git
synced 2024-12-22 21:05:25 +01:00
Added a few interactions (help, source, feeds).
This commit is contained in:
parent
195a4099e7
commit
7fc8907608
1 changed files with 66 additions and 5 deletions
|
@ -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", "<ping xmlns='urn:xmpp:ping'/>")
|
||||
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.")
|
||||
|
|
Loading…
Reference in a new issue