mirror of
https://salsa.debian.org/mdosch/feed-to-muc.git
synced 2024-11-22 14:08:39 +01:00
Added check if bot is still joined to MUC.
This commit is contained in:
parent
98c1e8bd0a
commit
1eca8a84b4
1 changed files with 56 additions and 9 deletions
|
@ -12,6 +12,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/chilts/sid"
|
||||||
"github.com/mattn/go-xmpp"
|
"github.com/mattn/go-xmpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -108,14 +109,12 @@ func main() {
|
||||||
Debug: false,
|
Debug: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connect to server.
|
||||||
client, err = options.NewClient()
|
client, err = options.NewClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Starting goroutine to ping the server every 30 seconds.
|
|
||||||
go ping(client, config.ServerAddress, config.BotJid)
|
|
||||||
|
|
||||||
// Join the MUC
|
// Join the MUC
|
||||||
mucStatus, err := client.JoinMUCNoHistory(config.Muc, config.MucNick)
|
mucStatus, err := client.JoinMUCNoHistory(config.Muc, config.MucNick)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -127,11 +126,13 @@ func main() {
|
||||||
os.Exit(mucStatus)
|
os.Exit(mucStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Starting goroutine to ping the server every 30 seconds.
|
||||||
|
go pingServer(client, config.ServerAddress, config.BotJid)
|
||||||
|
|
||||||
|
// Starting goroutine to ping the MUC every 30 seconds.
|
||||||
|
go pingMUC(client, config.BotJid, config.Muc, config.MucNick)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
_, err := client.Recv()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(config.Feeds); i++ {
|
for i := 0; i < len(config.Feeds); i++ {
|
||||||
output, err := getArticles(config.Feeds[i], config.MaxArticles)
|
output, err := getArticles(config.Feeds[i], config.MaxArticles)
|
||||||
|
@ -151,13 +152,59 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send a ping every 30 seconds to check if the server is still available.
|
// Send a ping every 30 seconds after last successful ping to check if the MUC is still available.
|
||||||
func ping(client *xmpp.Client, server string, botJid string) {
|
func pingMUC(client *xmpp.Client, botJid string, Muc string, MucNick string) {
|
||||||
for {
|
for {
|
||||||
time.Sleep(30 * time.Second)
|
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(), "get", "<ping xmlns='urn:xmpp:ping'/>")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
|
stanza, err := client.Recv()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check IQs for type result and UID.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
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])
|
err := client.PingC2S(botJid, strings.Split(server, ":")[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue