2017-09-24 18:44:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-30 10:50:07 +00:00
|
|
|
"context"
|
2017-09-24 18:44:39 +00:00
|
|
|
"log"
|
2019-07-30 10:50:07 +00:00
|
|
|
"mellium.im/sasl"
|
|
|
|
"mellium.im/xmpp"
|
|
|
|
"mellium.im/xmpp/dial"
|
|
|
|
mjid "mellium.im/xmpp/jid"
|
|
|
|
"mellium.im/xmpp/stanza"
|
2017-09-24 18:44:39 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2019-07-30 10:50:07 +00:00
|
|
|
func panicOnErr(err error) {
|
2017-09-25 12:59:08 +00:00
|
|
|
if err != nil {
|
2019-07-30 10:50:07 +00:00
|
|
|
panic(err)
|
2017-09-25 12:59:08 +00:00
|
|
|
}
|
2019-07-30 10:50:07 +00:00
|
|
|
}
|
2017-09-24 18:44:39 +00:00
|
|
|
|
2019-07-30 10:50:07 +00:00
|
|
|
func initXMPP(jid mjid.JID, pass string) (*xmpp.Session, error) {
|
2019-08-04 19:03:44 +00:00
|
|
|
dialer := dial.Dialer{NoTLS: true}
|
2019-07-30 10:50:07 +00:00
|
|
|
conn, err := dialer.Dial(context.TODO(), "tcp", jid)
|
2019-08-04 19:03:44 +00:00
|
|
|
if err != nil {
|
2017-09-25 12:59:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-30 10:50:07 +00:00
|
|
|
return xmpp.NegotiateSession(
|
|
|
|
context.TODO(),
|
|
|
|
jid.Domain(),
|
|
|
|
jid,
|
|
|
|
conn,
|
|
|
|
false,
|
|
|
|
xmpp.NewNegotiator(xmpp.StreamConfig{Features: []xmpp.StreamFeature{
|
|
|
|
xmpp.BindResource(),
|
|
|
|
xmpp.StartTLS(true, nil),
|
|
|
|
xmpp.SASL("", pass, sasl.ScramSha1Plus, sasl.ScramSha1, sasl.Plain),
|
|
|
|
}}),
|
|
|
|
)
|
|
|
|
}
|
2017-09-24 18:44:39 +00:00
|
|
|
|
2019-07-30 10:50:07 +00:00
|
|
|
func closeXMPP(session *xmpp.Session) {
|
|
|
|
session.Close()
|
|
|
|
session.Conn().Close()
|
2017-09-25 08:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-09-26 12:39:19 +00:00
|
|
|
// get xmpp credentials and message receivers from env
|
|
|
|
xi := os.Getenv("XMPP_ID")
|
|
|
|
xp := os.Getenv("XMPP_PASS")
|
|
|
|
xr := os.Getenv("XMPP_RECEIVERS")
|
2017-09-25 08:54:06 +00:00
|
|
|
|
2017-09-25 15:58:44 +00:00
|
|
|
// check if xmpp credentials and receiver list are supplied
|
|
|
|
if len(xi) < 1 || len(xp) < 1 || len(xr) < 1 {
|
2017-09-26 12:39:19 +00:00
|
|
|
log.Fatal("XMPP_ID, XMPP_PASS or XMPP_RECEIVERS not set")
|
2017-09-25 08:54:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 10:50:07 +00:00
|
|
|
jid, err := mjid.Parse(xi)
|
|
|
|
panicOnErr(err)
|
2017-09-26 12:39:19 +00:00
|
|
|
|
2019-07-30 10:50:07 +00:00
|
|
|
session, err := initXMPP(jid, xp)
|
|
|
|
panicOnErr(err)
|
2017-09-26 12:39:19 +00:00
|
|
|
|
2019-07-30 10:50:07 +00:00
|
|
|
defer closeXMPP(session)
|
|
|
|
|
2019-08-04 19:03:44 +00:00
|
|
|
panicOnErr(session.Send(context.TODO(), stanza.WrapPresence(mjid.JID{}, stanza.AvailablePresence, nil)))
|
2019-07-30 10:50:07 +00:00
|
|
|
|
|
|
|
panicOnErr(session.Serve(nil))
|
2017-09-24 18:44:39 +00:00
|
|
|
|
2019-07-30 10:50:07 +00:00
|
|
|
/*// create chan for messages (webhooks -> xmpp)
|
2017-09-26 12:39:19 +00:00
|
|
|
messages := make(chan string)
|
2017-09-25 15:58:44 +00:00
|
|
|
|
2017-09-26 12:39:19 +00:00
|
|
|
// wait for messages from the webhooks and send them to all receivers
|
2017-09-25 15:58:44 +00:00
|
|
|
go func() {
|
2017-09-26 12:39:19 +00:00
|
|
|
for m := range messages {
|
2017-09-25 15:58:44 +00:00
|
|
|
for _, r := range strings.Split(xr, ",") {
|
2017-09-26 12:39:19 +00:00
|
|
|
xc.Out <- xmpp.Message{
|
|
|
|
To: r,
|
|
|
|
Body: []xmpp.MessageBody{
|
2018-12-09 15:39:08 +00:00
|
|
|
{
|
2017-09-26 12:39:19 +00:00
|
|
|
Value: m,
|
|
|
|
},
|
|
|
|
},
|
2017-09-25 15:58:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-09-26 12:39:19 +00:00
|
|
|
// initialize handler for grafana alerts
|
|
|
|
http.Handle("/grafana", newMessageHandler(messages, grafanaParserFunc))
|
2017-09-25 15:58:44 +00:00
|
|
|
|
|
|
|
// listen for requests
|
2019-07-30 10:50:07 +00:00
|
|
|
http.ListenAndServe(":4321", nil)*/
|
2017-09-24 18:44:39 +00:00
|
|
|
}
|