warawara

XMPP bot for cerca public RSS feed
git clone http://git.permacomputing.net/repos/warawara.git # read-only access
Log | Files | Refs | README

commit 3afd04f909c9bbbb954d1fca96b6fdca8d79cbf7
parent 1c7e5310cd8f612c50755e37e34c1ac82e0d1048
Author: decentral1se <cellarspoon@riseup.net>
Date:   Sun, 17 Nov 2024 16:57:25 +0100

chore: gofmt -l -w warawara.go

Diffstat:
Mwarawara.go | 226++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 113 insertions(+), 113 deletions(-)

diff --git a/warawara.go b/warawara.go @@ -10,146 +10,146 @@ package main import ( - "context" - "crypto/tls" - "encoding/xml" - "flag" - "fmt" - "mellium.im/sasl" - "mellium.im/xmpp" - "mellium.im/xmpp/dial" - "mellium.im/xmpp/jid" - "mellium.im/xmpp/muc" - "mellium.im/xmpp/stanza" - "net/http" - "os" - "time" -) - -var( - feedURL string - ctx context.Context - cancel context.CancelFunc - botJID string - botServer string - botPassword string - session *xmpp.Session - MUCJID string + "context" + "crypto/tls" + "encoding/xml" + "flag" + "fmt" + "mellium.im/sasl" + "mellium.im/xmpp" + "mellium.im/xmpp/dial" + "mellium.im/xmpp/jid" + "mellium.im/xmpp/muc" + "mellium.im/xmpp/stanza" + "net/http" + "os" + "time" +) + +var ( + feedURL string + ctx context.Context + cancel context.CancelFunc + botJID string + botServer string + botPassword string + session *xmpp.Session + MUCJID string ) type itemXML struct { - Title string `xml:"title"` - Link string `xml:"link"` - Description string `xml:"description"` - Date string `xml:"pubDate"` + Title string `xml:"title"` + Link string `xml:"link"` + Description string `xml:"description"` + Date string `xml:"pubDate"` } type channelXML struct { - Items []itemXML `xml:"channel>item"` + Items []itemXML `xml:"channel>item"` } type textMessage struct { - stanza.Message - Body string `xml:"body"` + stanza.Message + Body string `xml:"body"` } func sendMUCMessage(text string) { - to := jid.MustParse(MUCJID) - var msg textMessage = textMessage{ - Message: stanza.Message{ - From: jid.MustParse(botJID), - To: to, - Type:"groupchat"}, - Body: text} - session.Encode(ctx, msg) + to := jid.MustParse(MUCJID) + var msg textMessage = textMessage{ + Message: stanza.Message{ + From: jid.MustParse(botJID), + To: to, + Type: "groupchat"}, + Body: text} + session.Encode(ctx, msg) } func joinXMPP() { - ctx, cancel = context.WithCancel(context.Background()) - //defer cancel() - var err error - // TLS disabled to avoid a 3min timeout before connecting - whyyyyyyy? - dialer := dial.Dialer{NoTLS: true,} - conn, err := dialer.Dial(ctx, "tcp", jid.MustParse(botJID)) - if err != nil { - fmt.Println("Error:", botServer, err) - } - negotiator := xmpp.NewNegotiator(func(*xmpp.Session, *xmpp.StreamConfig) xmpp.StreamConfig { - return xmpp.StreamConfig{ - Features: []xmpp.StreamFeature{ - xmpp.StartTLS(&tls.Config{ - ServerName: botServer, - }), - xmpp.SASL("", botPassword, sasl.ScramSha256Plus, sasl.ScramSha1Plus, sasl.ScramSha256, sasl.ScramSha1, sasl.Plain), - xmpp.BindResource(), - }, - } - }) - session, err = xmpp.NewSession(context.TODO(), jid.MustParse(botServer), jid.MustParse(botJID), conn, 0, negotiator) - if err != nil { - fmt.Println("Error:", botJID, err) - os.Exit(1) - } - fmt.Println("Connected to", botServer, "as", botJID) - err = session.Send(ctx, stanza.Presence{Type: stanza.AvailablePresence}.Wrap(nil)) - if err != nil { - fmt.Println("Error:", err) - } + ctx, cancel = context.WithCancel(context.Background()) + //defer cancel() + var err error + // TLS disabled to avoid a 3min timeout before connecting - whyyyyyyy? + dialer := dial.Dialer{NoTLS: true} + conn, err := dialer.Dial(ctx, "tcp", jid.MustParse(botJID)) + if err != nil { + fmt.Println("Error:", botServer, err) + } + negotiator := xmpp.NewNegotiator(func(*xmpp.Session, *xmpp.StreamConfig) xmpp.StreamConfig { + return xmpp.StreamConfig{ + Features: []xmpp.StreamFeature{ + xmpp.StartTLS(&tls.Config{ + ServerName: botServer, + }), + xmpp.SASL("", botPassword, sasl.ScramSha256Plus, sasl.ScramSha1Plus, sasl.ScramSha256, sasl.ScramSha1, sasl.Plain), + xmpp.BindResource(), + }, + } + }) + session, err = xmpp.NewSession(context.TODO(), jid.MustParse(botServer), jid.MustParse(botJID), conn, 0, negotiator) + if err != nil { + fmt.Println("Error:", botJID, err) + os.Exit(1) + } + fmt.Println("Connected to", botServer, "as", botJID) + err = session.Send(ctx, stanza.Presence{Type: stanza.AvailablePresence}.Wrap(nil)) + if err != nil { + fmt.Println("Error:", err) + } } // TODO: this is in a goroutine so this won't display errors // and won't exit, need to use channels or something... func joinMUC() { - MUC := jid.MustParse(MUCJID + "/warawara") - mucClient := muc.Client{} - _, err := mucClient.Join(ctx, MUC, session) - if err != nil { - fmt.Println("Error:", err) - os.Exit(1) - } + MUC := jid.MustParse(MUCJID + "/warawara") + mucClient := muc.Client{} + _, err := mucClient.Join(ctx, MUC, session) + if err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } } func fetchFeed() { - pDateOld := "" - for { - fmt.Println("trying to fetch feed") - if feed, err := http.Get(feedURL); err != nil { - fmt.Println("Error:", err) - } else { - channel := channelXML{} - if err := xml.NewDecoder(feed.Body).Decode(&channel); err != nil { - fmt.Println("Error:", err) - } else if len(channel.Items) != 0 { - item := channel.Items[0] - pTitle := item.Title - pLink := item.Link - pDescription := item.Description - pDate := item.Date - if pDate != pDateOld { - breakingNews := "🗨️ " + pTitle + " " + pDescription + "\n" + pLink - fmt.Println(breakingNews) - sendMUCMessage(breakingNews) - pDateOld = pDate - } - } - } - time.Sleep(16 * time.Minute) // cerca's limiter kicks in at 15min - } + pDateOld := "" + for { + fmt.Println("trying to fetch feed") + if feed, err := http.Get(feedURL); err != nil { + fmt.Println("Error:", err) + } else { + channel := channelXML{} + if err := xml.NewDecoder(feed.Body).Decode(&channel); err != nil { + fmt.Println("Error:", err) + } else if len(channel.Items) != 0 { + item := channel.Items[0] + pTitle := item.Title + pLink := item.Link + pDescription := item.Description + pDate := item.Date + if pDate != pDateOld { + breakingNews := "🗨️ " + pTitle + " " + pDescription + "\n" + pLink + fmt.Println(breakingNews) + sendMUCMessage(breakingNews) + pDateOld = pDate + } + } + } + time.Sleep(16 * time.Minute) // cerca's limiter kicks in at 15min + } } func main() { - flag.StringVar(&feedURL, "url", "", "RSS feed URL") - flag.StringVar(&botJID, "bot", "", "bot JID") - flag.StringVar(&botServer, "server", "", "server") - flag.StringVar(&botPassword, "password", "", "bot JID password") - flag.StringVar(&MUCJID, "muc", "", "MUC JID") - flag.Parse() + flag.StringVar(&feedURL, "url", "", "RSS feed URL") + flag.StringVar(&botJID, "bot", "", "bot JID") + flag.StringVar(&botServer, "server", "", "server") + flag.StringVar(&botPassword, "password", "", "bot JID password") + flag.StringVar(&MUCJID, "muc", "", "MUC JID") + flag.Parse() - fmt.Println("🫧 w a r a w a r a 🫧\n") - fmt.Println("ctrl-c for emergency shutdown\n") + fmt.Println("🫧 w a r a w a r a 🫧\n") + fmt.Println("ctrl-c for emergency shutdown\n") - joinXMPP() - go joinMUC() + joinXMPP() + go joinMUC() - fetchFeed() + fetchFeed() }