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"
|
|
|
|
|
|
|
|
"github.com/mattn/go-xmpp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
var err error
|
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
|
|
|
|
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 {
|
|
|
|
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)
|
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 {
|
|
|
|
log.Fatal("Error: ", err)
|
|
|
|
}
|
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) {
|
2018-07-13 22:53:22 +02:00
|
|
|
log.Fatal("Error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
log.Fatal("Error: ", err)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err = options.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starting goroutine to ping the server every 30 seconds.
|
|
|
|
go ping(client, config.ServerAddress, config.BotJid)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, err := client.Recv()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(config.Feeds); i++ {
|
2018-07-15 11:03:19 +02:00
|
|
|
output, err := getArticles(config.Feeds[i], config.MaxArticles)
|
2018-07-13 22:53:22 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if output != "" {
|
|
|
|
_, err = client.Send(xmpp.Chat{Remote: config.Muc, Type: "groupchat", Text: output})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a ping every 30 seconds to check if the server is still available.
|
|
|
|
func ping(client *xmpp.Client, server string, botJid string) {
|
|
|
|
for {
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
err := client.PingC2S(botJid, strings.Split(server, ":")[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|