2018-07-13 22:53:22 +02:00
|
|
|
/* Copyright 2018 Martin Dosch
|
|
|
|
Licensed under the "MIT License" */
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-07-15 11:03:19 +02:00
|
|
|
"flag"
|
2018-07-13 22:53:22 +02:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2024-01-11 15:54:06 +01:00
|
|
|
"github.com/xmppo/go-xmpp"
|
2018-07-13 22:53:22 +02:00
|
|
|
)
|
|
|
|
|
2019-05-31 23:45:29 +02:00
|
|
|
type configuration struct {
|
|
|
|
ServerAddress string
|
|
|
|
BotJid string
|
|
|
|
Password string
|
|
|
|
Muc string
|
|
|
|
MucNick string
|
|
|
|
MaxArticles int
|
|
|
|
RefreshTime time.Duration
|
|
|
|
NoExcerpt bool
|
|
|
|
Quiet bool
|
|
|
|
Contact string
|
|
|
|
Filter []string
|
2020-07-06 21:06:00 +02:00
|
|
|
FilterMessage []string
|
2019-05-31 23:45:29 +02:00
|
|
|
Feeds []string
|
|
|
|
}
|
|
|
|
|
2018-08-08 11:46:58 +02:00
|
|
|
// Variables defined globally as they are used by functions pingMUC
|
|
|
|
// and processStanzas.
|
|
|
|
var (
|
2019-05-31 23:45:29 +02:00
|
|
|
ID string
|
2018-08-08 11:46:58 +02:00
|
|
|
pingSent time.Time
|
|
|
|
pingReceived bool
|
|
|
|
)
|
|
|
|
|
2018-07-13 22:53:22 +02:00
|
|
|
func main() {
|
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()
|
|
|
|
|
2019-05-31 23:45:29 +02:00
|
|
|
config := openConfig(configFilePtr)
|
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.
|
2019-05-31 23:45:29 +02:00
|
|
|
client, err := options.NewClient()
|
2018-07-13 22:53:22 +02:00
|
|
|
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-05-31 23:45:29 +02: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++ {
|
2020-07-06 21:06:00 +02:00
|
|
|
output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt, config.Filter, config.FilterMessage)
|
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 != "" {
|
2023-09-30 20:42:07 +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
|
|
|
}
|
|
|
|
}
|