mirror of
https://salsa.debian.org/mdosch/feed-to-muc.git
synced 2024-11-22 22:18:39 +01:00
Reworked the ping functions and stanza processing.
This commit is contained in:
parent
3bdfea029e
commit
5a5014d449
1 changed files with 70 additions and 44 deletions
|
@ -16,9 +16,17 @@ import (
|
||||||
"github.com/mattn/go-xmpp"
|
"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() {
|
func main() {
|
||||||
|
|
||||||
var err error
|
|
||||||
var configPath, configFile string
|
var configPath, configFile string
|
||||||
|
|
||||||
type configuration struct {
|
type configuration struct {
|
||||||
|
@ -132,6 +140,9 @@ func main() {
|
||||||
// Starting goroutine to ping the MUC every 30 seconds.
|
// Starting goroutine to ping the MUC every 30 seconds.
|
||||||
go pingMUC(client, config.BotJid, config.Muc, config.MucNick)
|
go pingMUC(client, config.BotJid, config.Muc, config.MucNick)
|
||||||
|
|
||||||
|
// Starting goroutine to process received stanzas.
|
||||||
|
go processStanzas(client)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// Check all configured feeds for new articles and send
|
// Check all configured feeds for new articles and send
|
||||||
// new articles to configured MUC.
|
// new articles to configured MUC.
|
||||||
|
@ -163,18 +174,45 @@ func pingMUC(client *xmpp.Client, botJid string, Muc string, MucNick string) {
|
||||||
time.Sleep(30 * time.Second)
|
time.Sleep(30 * time.Second)
|
||||||
|
|
||||||
// Send ping to own MUC participant to check we are still joined.
|
// 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'/>")
|
"get", "<ping xmlns='urn:xmpp:ping'/>")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pingSent := time.Now()
|
pingSent = time.Now()
|
||||||
pingReceived := false
|
pingReceived = false
|
||||||
|
|
||||||
// Check for result IQ as long as there was no reply yet.
|
// Check for result IQ as long as there was no reply yet.
|
||||||
for (time.Since(pingSent).Seconds() < 10.0) && (pingReceived == false) {
|
for (time.Since(pingSent).Seconds() < 10.0) && (pingReceived == false) {
|
||||||
// Receive stanzas. ToDo: Receive stanzas continiously without 30s delay.
|
time.Sleep(1 * time.Second)
|
||||||
|
if pingReceived == true {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
// Quit if no ping reply was received.
|
||||||
|
if pingReceived == false {
|
||||||
|
log.Fatal("MUC not available.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send a ping to the server every 30 seconds to check if the connection is still alive.
|
||||||
|
func pingServer(client *xmpp.Client, server string, botJid string) {
|
||||||
|
for {
|
||||||
|
time.Sleep(30 * time.Second)
|
||||||
|
|
||||||
|
// Send ping to server to check if connection still exists.
|
||||||
|
err := client.PingC2S(botJid, strings.Split(server, ":")[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func processStanzas(client *xmpp.Client) {
|
||||||
|
for { // Receive stanzas. ToDo: Receive stanzas continiously without 30s delay.
|
||||||
stanza, err := client.Recv()
|
stanza, err := client.Recv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -202,11 +240,17 @@ func pingMUC(client *xmpp.Client, botJid string, Muc string, MucNick string) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
} else if strings.Contains(string(v.Query), "<ping xmlns='urn:xmpp:ping'/>") == true {
|
||||||
|
// Reply to pings.
|
||||||
|
_, err := client.RawInformation(client.JID(), v.From, v.ID, "result", "")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Send error replies for all other IQs.
|
// Send error replies for all other IQs.
|
||||||
_, err := client.RawInformation(client.JID(), v.From, v.ID, "error", string(v.Query)+
|
_, err := client.RawInformation(client.JID(), v.From, v.ID, "error",
|
||||||
"<error type='cancel'><feature-not-implemented "+
|
"<error type='cancel'><service-unavailable "+
|
||||||
"xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/><error/>")
|
"xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -217,22 +261,4 @@ func pingMUC(client *xmpp.Client, botJid string, Muc string, MucNick string) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Quit if no ping reply was received.
|
|
||||||
if pingReceived == false {
|
|
||||||
log.Fatal("MUC not available.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send a ping to the server every 30 seconds to check if the connection is still alive.
|
|
||||||
func pingServer(client *xmpp.Client, server string, botJid string) {
|
|
||||||
for {
|
|
||||||
time.Sleep(30 * time.Second)
|
|
||||||
|
|
||||||
// Send ping to server to check if connection still exists.
|
|
||||||
err := client.PingC2S(botJid, strings.Split(server, ":")[0])
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue