diff --git a/feed-to-muc.go b/feed-to-muc.go index 508740e..cbd1232 100644 --- a/feed-to-muc.go +++ b/feed-to-muc.go @@ -16,9 +16,17 @@ import ( "github.com/mattn/go-xmpp" ) +// Variables defined globally as they are used by functions pingMUC +// and processStanzas. +var ( + id string + err error + pingSent time.Time + pingReceived bool +) + func main() { - var err error var configPath, configFile string type configuration struct { @@ -132,6 +140,9 @@ func main() { // Starting goroutine to ping the MUC every 30 seconds. go pingMUC(client, config.BotJid, config.Muc, config.MucNick) + // Starting goroutine to process received stanzas. + go processStanzas(client) + for { // Check all configured feeds for new articles and send // new articles to configured MUC. @@ -163,59 +174,22 @@ func pingMUC(client *xmpp.Client, botJid string, Muc string, MucNick string) { 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) } - pingSent := time.Now() - pingReceived := false + pingSent = time.Now() + pingReceived = false // Check for result IQ as long as there was no reply yet. for (time.Since(pingSent).Seconds() < 10.0) && (pingReceived == false) { - // 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. - switch v := stanza.(type) { - case xmpp.IQ: - if (v.Type == "error") && (v.ID == id) { - log.Fatal("MUC not available.") - } - - if (v.Type == "result") && (v.ID == id) { - pingReceived = true - } - - if v.Type == "get" { - // Reply to disco#info requests according to https://xmpp.org/extensions/xep-0030.html. - if strings.Contains(string(v.Query), - "") == true { - _, err := client.RawInformation(client.JID(), v.From, v.ID, - "result", ""+ - ""+ - "") - if err != nil { - log.Fatal(err) - } - } else { - // Send error replies for all other IQs. - _, err := client.RawInformation(client.JID(), v.From, v.ID, "error", string(v.Query)+ - "") - if err != nil { - log.Fatal(err) - } - } - } - - default: + time.Sleep(1 * time.Second) + if pingReceived == true { break } + } // Quit if no ping reply was received. if pingReceived == false { @@ -236,3 +210,55 @@ func pingServer(client *xmpp.Client, server string, botJid string) { } } } + +func processStanzas(client *xmpp.Client) { + 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. + switch v := stanza.(type) { + case xmpp.IQ: + if (v.Type == "error") && (v.ID == id) { + log.Fatal("MUC not available.") + } + + if (v.Type == "result") && (v.ID == id) { + pingReceived = true + } + + if v.Type == "get" { + // Reply to disco#info requests according to https://xmpp.org/extensions/xep-0030.html. + if strings.Contains(string(v.Query), + "") == true { + _, err := client.RawInformation(client.JID(), v.From, v.ID, + "result", ""+ + ""+ + "") + if err != nil { + log.Fatal(err) + } + } else if strings.Contains(string(v.Query), "") == true { + // Reply to pings. + _, err := client.RawInformation(client.JID(), v.From, v.ID, "result", "") + if err != nil { + log.Fatal(err) + } + } else { + // Send error replies for all other IQs. + _, err := client.RawInformation(client.JID(), v.From, v.ID, "error", + "") + if err != nil { + log.Fatal(err) + } + } + } + + default: + break + } + } +}