feed-to-muc/main.go

110 lines
2.6 KiB
Go
Raw Normal View History

/* Copyright Martin Dosch
2018-07-13 22:53:22 +02:00
Licensed under the "MIT License" */
package main
import (
"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
)
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
FilterMessage []string
Feeds []string
}
// Variables defined globally as they are used by functions pingMUC
// and processStanzas.
var (
ID string
pingSent time.Time
pingReceived bool
)
2018-07-13 22:53:22 +02:00
func main() {
// Read path to config from command line option.
configFilePtr := flag.String("config", "none", "path to configuration file")
flag.Parse()
config := openConfig(configFilePtr)
2018-07-13 22:53:22 +02:00
var client *xmpp.Client
options := xmpp.Options{
2024-04-11 12:18:45 +02:00
Host: config.ServerAddress,
User: config.BotJid,
Password: config.Password,
NoTLS: true,
StartTLS: true,
Debug: false,
SSDP: true,
UserAgentSW: "feed-to-muc",
2018-07-13 22:53:22 +02:00
}
// Connect to server.
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
_, 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
}
// 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)
// Starting goroutine to process received stanzas.
go processStanzas(client, config.Muc, config.MucNick, config.Feeds, config.Quiet, config.Contact)
// 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++ {
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,
})
if err != nil {
// 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-13 22:53:22 +02:00
}
}
2018-08-02 09:33:19 +02:00
// Wait 30 seconds before checking feeds again.
time.Sleep(config.RefreshTime * time.Second)
2018-07-13 22:53:22 +02:00
}
}