2018-07-13 22:53:22 +02:00
|
|
|
/* Copyright 2018 Martin Dosch
|
|
|
|
Licensed under the "MIT License" */
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-07-15 11:03:19 +02:00
|
|
|
"flag"
|
2018-07-13 22:53:22 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-07-28 21:38:48 +02:00
|
|
|
"github.com/chilts/sid"
|
2019-02-01 19:55:06 +01:00
|
|
|
"github.com/mattn/go-xmpp"
|
2018-07-13 22:53:22 +02:00
|
|
|
)
|
|
|
|
|
2018-08-08 11:46:58 +02:00
|
|
|
// Variables defined globally as they are used by functions pingMUC
|
|
|
|
// and processStanzas.
|
|
|
|
var (
|
|
|
|
id string
|
|
|
|
err error
|
|
|
|
pingSent time.Time
|
|
|
|
pingReceived bool
|
|
|
|
)
|
|
|
|
|
2018-07-13 22:53:22 +02:00
|
|
|
func main() {
|
|
|
|
|
2018-07-15 11:03:19 +02:00
|
|
|
var configPath, configFile string
|
2018-07-13 22:53:22 +02:00
|
|
|
|
|
|
|
type configuration struct {
|
|
|
|
ServerAddress string
|
|
|
|
BotJid string
|
|
|
|
Password string
|
|
|
|
Muc string
|
|
|
|
MucNick string
|
|
|
|
MaxArticles int
|
2018-09-05 20:40:41 +02:00
|
|
|
RefreshTime time.Duration
|
2018-09-03 20:42:19 +02:00
|
|
|
NoExcerpt bool
|
2019-05-31 12:04:53 +02:00
|
|
|
Quiet bool
|
|
|
|
Contact string
|
2019-05-31 09:50:46 +02:00
|
|
|
Filter []string
|
2018-07-13 22:53:22 +02:00
|
|
|
Feeds []string
|
|
|
|
}
|
|
|
|
|
2018-07-15 11:03:19 +02:00
|
|
|
// Read path to config from command line option.
|
|
|
|
configFilePtr := flag.String("config", "none", "path to configuration file")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *configFilePtr != "none" {
|
|
|
|
configFile = *configFilePtr
|
|
|
|
} else {
|
|
|
|
// Get systems user config path.
|
|
|
|
osConfigDir := os.Getenv("$XDG_CONFIG_HOME")
|
|
|
|
if osConfigDir != "" {
|
|
|
|
// Create configPath if not yet existing.
|
|
|
|
configPath = osConfigDir + "/.config/feed-to-muc/"
|
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(configPath, 0700)
|
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Can't create config path: ", err)
|
2018-07-15 11:03:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { // Get the current user.
|
|
|
|
curUser, err := user.Current()
|
2018-07-13 22:53:22 +02:00
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Can't get current user: ", err)
|
2018-07-15 11:03:19 +02:00
|
|
|
return
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
2018-07-15 11:03:19 +02:00
|
|
|
// Get home directory.
|
|
|
|
home := curUser.HomeDir
|
2018-07-13 22:53:22 +02:00
|
|
|
|
2018-07-15 11:03:19 +02:00
|
|
|
if home == "" {
|
|
|
|
log.Fatal("Error: No home directory available.")
|
|
|
|
return
|
|
|
|
}
|
2018-07-13 22:53:22 +02:00
|
|
|
|
2018-07-15 11:03:19 +02:00
|
|
|
// Create configPath if not yet existing.
|
|
|
|
configPath = home + "/.config/feed-to-muc/"
|
|
|
|
if _, err := os.Stat(configPath + "config.json"); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(configPath, 0700)
|
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Can't create config path: ", err)
|
2018-07-15 11:03:19 +02:00
|
|
|
}
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-15 11:03:19 +02:00
|
|
|
}
|
|
|
|
configFile = configPath + "config.json"
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that config file is existing.
|
2018-07-15 11:03:19 +02:00
|
|
|
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Can't find config file: ", err)
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read configuration file into variable config.
|
2018-07-15 11:03:19 +02:00
|
|
|
file, _ := os.Open(configFile)
|
2018-07-13 22:53:22 +02:00
|
|
|
defer file.Close()
|
|
|
|
decoder := json.NewDecoder(file)
|
|
|
|
config := configuration{}
|
|
|
|
if err := decoder.Decode(&config); err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Can't decode config: ", err)
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-15 11:03:19 +02:00
|
|
|
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
2018-07-13 22:53:22 +02:00
|
|
|
err = os.MkdirAll(configPath, 0700)
|
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Can't create config path: ", err)
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var client *xmpp.Client
|
|
|
|
|
|
|
|
options := xmpp.Options{
|
|
|
|
Host: config.ServerAddress,
|
|
|
|
User: config.BotJid,
|
|
|
|
Password: config.Password,
|
|
|
|
NoTLS: true,
|
|
|
|
StartTLS: true,
|
|
|
|
Debug: false,
|
|
|
|
}
|
|
|
|
|
2018-07-28 21:38:48 +02:00
|
|
|
// Connect to server.
|
2018-07-13 22:53:22 +02:00
|
|
|
client, err = options.NewClient()
|
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Can't connect to xmpp server: ", err)
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Join the MUC
|
2019-01-16 09:35:00 +01:00
|
|
|
_, err := client.JoinMUCNoHistory(config.Muc, config.MucNick)
|
2018-07-13 22:53:22 +02:00
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Can't join MUC: ", err)
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-28 21:38:48 +02:00
|
|
|
// 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)
|
|
|
|
|
2018-08-08 11:46:58 +02:00
|
|
|
// Starting goroutine to process received stanzas.
|
2019-05-31 12:04:53 +02:00
|
|
|
go processStanzas(client, config.Muc, config.MucNick, config.Feeds, config.Quiet, config.Contact)
|
2018-08-08 11:46:58 +02:00
|
|
|
|
2018-09-05 20:40:41 +02:00
|
|
|
// Set RefreshTime to 30 seconds if not defined.
|
|
|
|
if config.RefreshTime == 0 {
|
|
|
|
config.RefreshTime = 30
|
|
|
|
}
|
|
|
|
|
2018-07-13 22:53:22 +02:00
|
|
|
for {
|
2018-08-02 09:33:19 +02:00
|
|
|
// Check all configured feeds for new articles and send
|
|
|
|
// new articles to configured MUC.
|
2018-07-13 22:53:22 +02:00
|
|
|
for i := 0; i < len(config.Feeds); i++ {
|
2019-05-31 09:50:46 +02:00
|
|
|
output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt, config.Filter)
|
2018-07-13 22:53:22 +02:00
|
|
|
if err != nil {
|
2018-08-02 09:33:19 +02:00
|
|
|
// Exit if an error occurs checking the feeds.
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Can't check feeds for new articles: ", err)
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if output != "" {
|
2018-08-02 09:33:19 +02:00
|
|
|
_, err = client.Send(xmpp.Chat{Remote: config.Muc,
|
|
|
|
Type: "groupchat", Text: output})
|
2018-07-15 21:00:16 +02:00
|
|
|
if err != nil {
|
2018-07-15 21:01:31 +02:00
|
|
|
// ToDo: Save message for resend.
|
2018-08-02 09:33:19 +02:00
|
|
|
// Exit if message can not be sent.
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Can't send message to MUC: ", err)
|
2018-07-15 21:00:16 +02:00
|
|
|
}
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-02 09:33:19 +02:00
|
|
|
// Wait 30 seconds before checking feeds again.
|
2018-09-05 20:40:41 +02:00
|
|
|
time.Sleep(config.RefreshTime * time.Second)
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 21:38:48 +02:00
|
|
|
// Send a ping every 30 seconds after last successful ping to check if the MUC is still available.
|
2019-05-26 20:20:34 +02:00
|
|
|
func pingMUC(client *xmpp.Client, botJid string, muc string, mucNick string) {
|
2018-07-28 21:38:48 +02:00
|
|
|
for {
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
|
|
|
|
// Send ping to own MUC participant to check we are still joined.
|
2019-05-26 20:20:34 +02:00
|
|
|
id, err = client.RawInformation(botJid, muc+"/"+mucNick, sid.Id(),
|
2018-08-02 09:33:19 +02:00
|
|
|
"get", "<ping xmlns='urn:xmpp:ping'/>")
|
2018-07-28 21:38:48 +02:00
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Can't send MUC self ping: ", err)
|
2018-07-28 21:38:48 +02:00
|
|
|
}
|
|
|
|
|
2018-08-08 11:46:58 +02:00
|
|
|
pingSent = time.Now()
|
|
|
|
pingReceived = false
|
2018-07-28 21:38:48 +02:00
|
|
|
|
|
|
|
// Check for result IQ as long as there was no reply yet.
|
2019-06-14 11:43:06 +02:00
|
|
|
for (time.Since(pingSent).Seconds() < 10.0) && !pingReceived {
|
2018-08-08 11:46:58 +02:00
|
|
|
time.Sleep(1 * time.Second)
|
2019-06-14 11:43:06 +02:00
|
|
|
if pingReceived {
|
2018-07-28 21:38:48 +02:00
|
|
|
break
|
|
|
|
}
|
2018-08-08 11:46:58 +02:00
|
|
|
|
2018-07-28 21:38:48 +02:00
|
|
|
}
|
|
|
|
// Quit if no ping reply was received.
|
2019-06-14 11:43:06 +02:00
|
|
|
if !pingReceived {
|
2018-07-28 21:38:48 +02:00
|
|
|
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) {
|
2018-07-13 22:53:22 +02:00
|
|
|
for {
|
|
|
|
time.Sleep(30 * time.Second)
|
2018-07-28 21:38:48 +02:00
|
|
|
|
|
|
|
// Send ping to server to check if connection still exists.
|
2018-07-13 22:53:22 +02:00
|
|
|
err := client.PingC2S(botJid, strings.Split(server, ":")[0])
|
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Client2Server ping failed:", err)
|
2018-07-13 22:53:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-08 11:46:58 +02:00
|
|
|
|
2019-05-31 12:04:53 +02:00
|
|
|
func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []string, quiet bool, contact string) {
|
2018-08-08 11:46:58 +02:00
|
|
|
for { // Receive stanzas. ToDo: Receive stanzas continiously without 30s delay.
|
|
|
|
stanza, err := client.Recv()
|
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Failed receiving Stanzas:", err)
|
2018-08-08 11:46:58 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 20:20:34 +02:00
|
|
|
// Check stanzas, maybe we want to reply.
|
2018-08-08 11:46:58 +02:00
|
|
|
switch v := stanza.(type) {
|
2019-05-26 20:20:34 +02:00
|
|
|
// Reply to requests for source and feeds.
|
|
|
|
case xmpp.Chat:
|
2019-05-31 10:40:40 +02:00
|
|
|
var command string
|
|
|
|
|
|
|
|
// Check for room mention of the bots nick if the the message type is groupchat.
|
|
|
|
if v.Type == "groupchat" {
|
2019-05-31 12:04:53 +02:00
|
|
|
// Leave if option quiet is set.
|
2019-06-14 11:43:06 +02:00
|
|
|
if quiet {
|
2019-05-31 12:04:53 +02:00
|
|
|
break
|
|
|
|
}
|
2019-05-31 10:40:40 +02:00
|
|
|
// Get first word of the message and transform it to lower case.
|
|
|
|
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.
|
2019-05-26 20:20:34 +02:00
|
|
|
if mention != strings.ToLower(mucNick) {
|
2019-05-31 10:40:40 +02:00
|
|
|
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
|
|
|
|
}
|
2019-05-26 20:20:34 +02:00
|
|
|
}
|
2019-05-31 10:40:40 +02:00
|
|
|
// 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
|
2019-05-26 20:20:34 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 10:40:40 +02:00
|
|
|
// Check for the command.
|
|
|
|
switch command {
|
2019-05-26 20:20:34 +02:00
|
|
|
|
|
|
|
// Reply with a short summary of available commands for `help`.
|
|
|
|
case "help":
|
2019-05-31 10:45:56 +02:00
|
|
|
|
2019-05-31 11:15:47 +02:00
|
|
|
reply := "The following commands are available:\n" +
|
2019-05-31 12:04:53 +02:00
|
|
|
"\"contact\": Show contact for this bot.\n" +
|
2019-05-31 10:45:56 +02:00
|
|
|
"\"feeds\": List feeds I'm following.\n" +
|
2019-05-31 10:48:12 +02:00
|
|
|
"\"ping\": Sends back a pong.\n" +
|
2019-05-31 10:45:56 +02:00
|
|
|
"\"source\": Show source code URL."
|
|
|
|
|
2019-05-31 10:40:40 +02:00
|
|
|
if v.Type == "groupchat" {
|
|
|
|
_, err = client.Send(xmpp.Chat{Remote: muc,
|
2019-05-31 10:45:56 +02:00
|
|
|
Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + ": " + reply})
|
2019-05-31 10:40:40 +02:00
|
|
|
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,
|
2019-05-31 10:45:56 +02:00
|
|
|
Type: "chat", Text: reply})
|
2019-05-31 10:40:40 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error: Failed sending message to ", v.Remote, ": ", err)
|
|
|
|
}
|
2019-05-26 20:20:34 +02:00
|
|
|
}
|
2019-05-31 10:45:56 +02:00
|
|
|
|
2019-05-26 20:20:34 +02:00
|
|
|
// Reply with repo address for `source`.
|
|
|
|
case "source":
|
2019-05-31 10:45:56 +02:00
|
|
|
|
|
|
|
reply := "My source can be found at " +
|
|
|
|
"https://salsa.debian.org/mdosch-guest/feed-to-muc"
|
|
|
|
|
2019-05-31 10:40:40 +02:00
|
|
|
if v.Type == "groupchat" {
|
|
|
|
_, err = client.Send(xmpp.Chat{Remote: muc,
|
2019-05-31 10:45:56 +02:00
|
|
|
Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + ": " + reply})
|
2019-05-31 10:40:40 +02:00
|
|
|
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,
|
2019-05-31 10:45:56 +02:00
|
|
|
Type: "chat", Text: reply})
|
2019-05-31 10:40:40 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error: Failed sending message to ", v.Remote, ": ", err)
|
|
|
|
}
|
2019-05-26 20:20:34 +02:00
|
|
|
}
|
|
|
|
// 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"
|
|
|
|
}
|
|
|
|
|
2019-05-31 10:45:56 +02:00
|
|
|
reply := "Feeds I'm following:\n" + feedList
|
|
|
|
|
2019-05-31 10:40:40 +02:00
|
|
|
if v.Type == "groupchat" {
|
|
|
|
_, err = client.Send(xmpp.Chat{Remote: muc,
|
2019-05-31 10:45:56 +02:00
|
|
|
Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + ": " + reply})
|
2019-05-31 10:40:40 +02:00
|
|
|
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,
|
2019-05-31 10:45:56 +02:00
|
|
|
Type: "chat", Text: reply})
|
2019-05-31 10:40:40 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error: Failed sending message to ", v.Remote, ": ", err)
|
|
|
|
}
|
2019-05-26 20:20:34 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 10:48:12 +02:00
|
|
|
case "ping":
|
|
|
|
|
|
|
|
reply := "pong"
|
|
|
|
|
|
|
|
if v.Type == "groupchat" {
|
|
|
|
_, err = client.Send(xmpp.Chat{Remote: muc,
|
|
|
|
Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + ": " + reply})
|
|
|
|
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: reply})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error: Failed sending message to ", v.Remote, ": ", err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-31 12:04:53 +02:00
|
|
|
|
|
|
|
case "contact":
|
|
|
|
|
|
|
|
if contact == "" {
|
|
|
|
contact = "Sorry, no contact information provided."
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Type == "groupchat" {
|
|
|
|
_, err = client.Send(xmpp.Chat{Remote: muc,
|
|
|
|
Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + ": " + contact})
|
|
|
|
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: contact})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error: Failed sending message to ", v.Remote, ": ", err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 20:20:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reply to pings and disco queries.
|
2018-08-08 11:46:58 +02:00
|
|
|
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),
|
2019-06-14 11:43:06 +02:00
|
|
|
"<query xmlns='http://jabber.org/protocol/disco#info'/>") {
|
2018-08-08 11:46:58 +02:00
|
|
|
_, err := client.RawInformation(client.JID(), v.From, v.ID,
|
|
|
|
"result", "<query xmlns='http://jabber.org/protocol/disco#info'>"+
|
|
|
|
"<identity category='client' type='bot' name='feedbot'/>"+
|
|
|
|
"<feature var='http://jabber.org/protocol/disco#info'/></query>")
|
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Failed to reply to disco#info:", err)
|
2018-08-08 11:46:58 +02:00
|
|
|
}
|
2019-06-14 11:43:06 +02:00
|
|
|
} else if strings.Contains(string(v.Query), "<ping xmlns='urn:xmpp:ping'/>") {
|
2018-08-08 11:46:58 +02:00
|
|
|
// Reply to pings.
|
|
|
|
_, err := client.RawInformation(client.JID(), v.From, v.ID, "result", "")
|
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Failed to reply to ping:", err)
|
2018-08-08 11:46:58 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Send error replies for all other IQs.
|
|
|
|
_, err := client.RawInformation(client.JID(), v.From, v.ID, "error",
|
|
|
|
"<error type='cancel'><service-unavailable "+
|
|
|
|
"xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>")
|
|
|
|
if err != nil {
|
2019-05-31 09:06:36 +02:00
|
|
|
log.Fatal("Error: Failed to send error IQ:", err)
|
2018-08-08 11:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|