feed-to-muc/feed-to-muc.go

238 lines
5.8 KiB
Go
Raw Normal View History

2018-07-13 22:53:22 +02:00
/* Copyright 2018 Martin Dosch
Licensed under the "MIT License" */
package main
import (
"encoding/json"
"flag"
2018-07-13 22:53:22 +02:00
"log"
"os"
"os/user"
"strings"
"time"
"github.com/chilts/sid"
2018-07-13 22:53:22 +02:00
"github.com/mattn/go-xmpp"
)
func main() {
var err error
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
Feeds []string
}
// 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 {
log.Fatal("Error: ", err)
}
}
} else { // Get the current user.
curUser, err := user.Current()
2018-07-13 22:53:22 +02:00
if err != nil {
log.Fatal("Error: ", err)
return
2018-07-13 22:53:22 +02:00
}
// Get home directory.
home := curUser.HomeDir
2018-07-13 22:53:22 +02:00
if home == "" {
log.Fatal("Error: No home directory available.")
return
}
2018-07-13 22:53:22 +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 {
log.Fatal("Error: ", err)
}
2018-07-13 22:53:22 +02:00
}
}
configFile = configPath + "config.json"
2018-07-13 22:53:22 +02:00
}
// Check that config file is existing.
if _, err := os.Stat(configFile); os.IsNotExist(err) {
2018-07-13 22:53:22 +02:00
log.Fatal("Error: ", err)
}
// Read configuration file into variable config.
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 {
log.Fatal("Error: ", err)
}
if _, err := os.Stat(configFile); os.IsNotExist(err) {
2018-07-13 22:53:22 +02:00
err = os.MkdirAll(configPath, 0700)
if err != nil {
log.Fatal("Error: ", err)
}
}
var client *xmpp.Client
options := xmpp.Options{
Host: config.ServerAddress,
User: config.BotJid,
Password: config.Password,
NoTLS: true,
StartTLS: true,
Debug: false,
}
// Connect to server.
2018-07-13 22:53:22 +02:00
client, err = options.NewClient()
if err != nil {
log.Fatal(err)
}
// Join the MUC
mucStatus, err := client.JoinMUCNoHistory(config.Muc, config.MucNick)
if err != nil {
log.Fatal(err)
}
// Exit if Status is > 300, see https://xmpp.org/registrar/mucstatus.html
if mucStatus > 300 {
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)
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++ {
output, err := getArticles(config.Feeds[i], config.MaxArticles)
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.
2018-07-13 22:53:22 +02:00
log.Fatal(err)
}
if output != "" {
2018-08-02 09:33:19 +02:00
_, err = client.Send(xmpp.Chat{Remote: config.Muc,
Type: "groupchat", Text: output})
if err != nil {
// ToDo: Save message for resend.
2018-08-02 09:33:19 +02:00
// Exit if message can not be sent.
log.Fatal(err)
}
2018-07-13 22:53:22 +02:00
}
}
2018-08-02 09:33:19 +02:00
// Wait 30 seconds before checking feeds again.
2018-07-13 22:53:22 +02:00
time.Sleep(30 * time.Second)
}
}
// 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) {
for {
time.Sleep(30 * time.Second)
// Send ping to own MUC participant to check we are still joined.
2018-08-02 09:33:19 +02:00
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. 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),
"<query xmlns='http://jabber.org/protocol/disco#info'/>") == true {
_, 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 {
log.Fatal(err)
}
} else {
// Send error replies for all other IQs.
_, err := client.RawInformation(client.JID(), v.From, v.ID, "error",
string(v.Query)+"<error/>")
if err != nil {
log.Fatal(err)
}
}
}
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) {
2018-07-13 22:53:22 +02:00
for {
time.Sleep(30 * time.Second)
// 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 {
log.Fatal(err)
}
}
}