From 8c0d1b78230d73e1c300dd65b107d681eb9df950 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Tue, 25 Jun 2019 03:26:53 +0200 Subject: [PATCH] first version with gosrc.io/xmpp --- circleci/main.go | 6 ++-- git/main.go | 6 ++-- gitlab/main.go | 4 +-- grafana/main.go | 6 ++-- main.go | 66 +++++++++----------------------------- prometheus/main.go | 4 +-- runtime/config.go | 13 ++------ runtime/hooks.go | 2 +- runtime/xmpp.go | 64 ++++++++++++++++++------------------- runtime/xmpp_test.go | 1 - xmpp.go | 75 ++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 140 insertions(+), 107 deletions(-) delete mode 100644 runtime/xmpp_test.go create mode 100644 xmpp.go diff --git a/circleci/main.go b/circleci/main.go index 89d4bee..f85b7d1 100644 --- a/circleci/main.go +++ b/circleci/main.go @@ -7,7 +7,7 @@ import ( libHTTP "dev.sum7.eu/genofire/golang-lib/http" "github.com/bdlm/log" - xmpp "github.com/mattn/go-xmpp" + "gosrc.io/xmpp" "dev.sum7.eu/genofire/hook2xmpp/runtime" ) @@ -47,12 +47,14 @@ func init() { }) ok := false + msg := request.String() + for _, hook := range hooks { if request.Payload.VCSURL != hook.Secret { continue } logger.Infof("run hook") - runtime.Notify(client, hook, request.String()) + runtime.Notify(client, hook, msg, msg) ok = true } if !ok { diff --git a/git/main.go b/git/main.go index ddaa962..35db749 100644 --- a/git/main.go +++ b/git/main.go @@ -7,8 +7,8 @@ import ( libHTTP "dev.sum7.eu/genofire/golang-lib/http" "github.com/bdlm/log" - xmpp "github.com/mattn/go-xmpp" "github.com/mitchellh/mapstructure" + "gosrc.io/xmpp" "dev.sum7.eu/genofire/hook2xmpp/runtime" ) @@ -62,12 +62,14 @@ func init() { }) ok := false + msg := request.String(event) + for _, hook := range hooks { if secret != hook.Secret { continue } logger.Infof("run hook") - runtime.Notify(client, hook, request.String(event)) + runtime.Notify(client, hook, msg, msg) ok = true } if !ok { diff --git a/gitlab/main.go b/gitlab/main.go index a0bc260..0d10b7b 100644 --- a/gitlab/main.go +++ b/gitlab/main.go @@ -10,7 +10,7 @@ import ( libHTTP "dev.sum7.eu/genofire/golang-lib/http" "github.com/bdlm/log" - xmpp "github.com/mattn/go-xmpp" + "gosrc.io/xmpp" "dev.sum7.eu/genofire/hook2xmpp/runtime" ) @@ -131,7 +131,7 @@ func init() { continue } logger.Infof("run hook") - runtime.Notify(client, hook, msg) + runtime.Notify(client, hook, msg, msg) ok = true } if !ok { diff --git a/grafana/main.go b/grafana/main.go index 053c108..47ecc25 100644 --- a/grafana/main.go +++ b/grafana/main.go @@ -6,7 +6,7 @@ import ( libHTTP "dev.sum7.eu/genofire/golang-lib/http" "github.com/bdlm/log" - xmpp "github.com/mattn/go-xmpp" + "gosrc.io/xmpp" "dev.sum7.eu/genofire/hook2xmpp/runtime" ) @@ -65,12 +65,14 @@ func init() { }) ok = false + msg := request.String() + for _, hook := range hooks { if secret != hook.Secret { continue } - runtime.Notify(client, hook, request.String()) + runtime.Notify(client, hook, msg, msg) if request.ImageURL != "" { runtime.NotifyImage(client, hook, request.ImageURL, request.String()) } else { diff --git a/main.go b/main.go index fdbcbcf..b633b23 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ import ( "dev.sum7.eu/genofire/golang-lib/file" "github.com/bdlm/log" - "github.com/mattn/go-xmpp" + "gosrc.io/xmpp" _ "dev.sum7.eu/genofire/hook2xmpp/circleci" _ "dev.sum7.eu/genofire/hook2xmpp/git" @@ -19,39 +19,34 @@ import ( "dev.sum7.eu/genofire/hook2xmpp/runtime" ) +var config *runtime.Config + func main() { configFile := "config.conf" flag.StringVar(&configFile, "config", configFile, "path of configuration file") flag.Parse() - config := &runtime.Config{} - if err := file.ReadTOML(configFile, config); err != nil { log.WithField("tip", "maybe call me with: hook2xmpp--config /etc/hook2xmpp.conf").Panicf("error on read config: %s", err) } log.SetLevel(config.LogLevel) - // load config - options := xmpp.Options{ - Host: config.XMPP.Host, - User: config.XMPP.Username, - Resource: config.XMPP.Resource, - Password: config.XMPP.Password, - StartTLS: config.XMPP.StartTLS, - NoTLS: config.XMPP.NoTLS, - Debug: config.XMPP.Debug, - Session: config.XMPP.Session, - Status: config.XMPP.Status, - StatusMessage: config.XMPP.StatusMessage, - } - client, err := options.NewClient() + router := xmpp.NewRouter() + client, err := xmpp.NewClient(xmpp.Config{ + Jid: config.XMPP.JID, + Password: config.XMPP.Password, + }, router) + if err != nil { log.Panicf("error on startup xmpp client: %s", err) } - go runtime.Start(client) - + cm := xmpp.NewStreamManager(client, postStartup) + go func() { + cm.Run() + log.Panic("closed connection") + }() for hookType, getHandler := range runtime.HookRegister { hooks, ok := config.Hooks[hookType] if ok { @@ -68,45 +63,14 @@ func main() { } }() - var mucs []string - for _, muc := range config.StartupNotifyMuc { - mucs = append(mucs, muc) - client.JoinMUCNoHistory(muc, config.Nickname) - } - for _, hooks := range config.Hooks { - for _, hook := range hooks { - for _, muc := range hook.NotifyMuc { - mucs = append(mucs, muc) - client.JoinMUCNoHistory(muc, config.Nickname) - } - } - } - - notify := func(msg string) { - for _, muc := range config.StartupNotifyMuc { - client.SendHtml(xmpp.Chat{Remote: muc, Type: "groupchat", Text: msg}) - } - for _, user := range config.StartupNotifyUser { - client.SendHtml(xmpp.Chat{Remote: user, Type: "chat", Text: msg}) - } - } - - log.Infof("started hock2xmpp with %s", client.JID()) - notify("startup of hock2xmpp") - // Wait for system signal sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) sig := <-sigs - notify("stopped of hock2xmpp") - - for _, muc := range mucs { - client.LeaveMUC(muc) - } + closeXMPP() srv.Close() - client.Close() log.Infof("closed by receiving: %s", sig) } diff --git a/prometheus/main.go b/prometheus/main.go index 116bc9f..880fa21 100644 --- a/prometheus/main.go +++ b/prometheus/main.go @@ -8,8 +8,8 @@ import ( libHTTP "dev.sum7.eu/genofire/golang-lib/http" "github.com/bdlm/log" - xmpp "github.com/mattn/go-xmpp" "github.com/prometheus/alertmanager/notify/webhook" + "gosrc.io/xmpp" "dev.sum7.eu/genofire/hook2xmpp/runtime" ) @@ -51,7 +51,7 @@ func init() { continue } logger.Infof("run hook") - runtime.Notify(client, hook, content) + runtime.Notify(client, hook, content, content) ok = true } if !ok { diff --git a/runtime/config.go b/runtime/config.go index cb470a2..0295d97 100644 --- a/runtime/config.go +++ b/runtime/config.go @@ -7,16 +7,9 @@ type Config struct { WebserverBind string `toml:"webserver_bind"` XMPP struct { - Host string `toml:"host"` - Username string `toml:"username"` - Resource string `toml:"resource"` - Password string `toml:"password"` - Debug bool `toml:"debug"` - NoTLS bool `toml:"no_tls"` - StartTLS bool `toml:"start_tls"` - Session bool `toml:"session"` - Status string `toml:"status"` - StatusMessage string `toml:"status_message"` + Address string `toml:"address"` + JID string `toml:"jid"` + Password string `toml:"password"` } `toml:"xmpp"` Nickname string `toml:"nickname"` diff --git a/runtime/hooks.go b/runtime/hooks.go index 83cddf6..70102db 100644 --- a/runtime/hooks.go +++ b/runtime/hooks.go @@ -3,7 +3,7 @@ package runtime import ( "net/http" - xmpp "github.com/mattn/go-xmpp" + "gosrc.io/xmpp" ) type HookHandler func(*xmpp.Client, []Hook) func(http.ResponseWriter, *http.Request) diff --git a/runtime/xmpp.go b/runtime/xmpp.go index f06462e..53bfe85 100644 --- a/runtime/xmpp.go +++ b/runtime/xmpp.go @@ -1,51 +1,47 @@ package runtime import ( - "fmt" - - "github.com/bdlm/log" - xmpp "github.com/mattn/go-xmpp" + "gosrc.io/xmpp" ) -func Start(client *xmpp.Client) { - for { - m, err := client.Recv() - if err != nil { - continue - } - switch v := m.(type) { - case xmpp.Chat: - if v.Type == "chat" { - log.Debugf("from %s: %s", v.Remote, v.Text) - } - if v.Type == "groupchat" { - } - case xmpp.Presence: - // do nothing - } - } -} func NotifyImage(client *xmpp.Client, hook Hook, url string, desc string) { - msg := fmt.Sprintf(` - %s - - %s - %s - - `, url, url, desc) + msg := xmpp.Message{ + Attrs: xmpp.Attrs{Type: xmpp.MessageTypeGroupchat}, + Body: url, + Extensions: []xmpp.MsgExtension{ + xmpp.OOB{URL: url, Desc: desc}, + }, + } for _, muc := range hook.NotifyMuc { - client.SendOrg(fmt.Sprintf(msg, muc, "groupchat")) + msg.To = muc + client.Send(msg) } + + msg.Type = xmpp.MessageTypeChat for _, user := range hook.NotifyUser { - client.SendOrg(fmt.Sprintf(msg, user, "chat")) + msg.To = user + client.Send(msg) } } -func Notify(client *xmpp.Client, hook Hook, msg string) { + +func Notify(client *xmpp.Client, hook Hook, text, html string) { + msg := xmpp.Message{ + Attrs: xmpp.Attrs{Type: xmpp.MessageTypeGroupchat}, + Body: text, + Extensions: []xmpp.MsgExtension{ + xmpp.HTML{Body: xmpp.HTMLBody{InnerXML: html}}, + }, + } + for _, muc := range hook.NotifyMuc { - client.SendHtml(xmpp.Chat{Remote: muc, Type: "groupchat", Text: msg}) + msg.To = muc + client.Send(msg) } + + msg.Type = xmpp.MessageTypeChat for _, user := range hook.NotifyUser { - client.SendHtml(xmpp.Chat{Remote: user, Type: "chat", Text: msg}) + msg.To = user + client.Send(msg) } } diff --git a/runtime/xmpp_test.go b/runtime/xmpp_test.go deleted file mode 100644 index 7ccdf5f..0000000 --- a/runtime/xmpp_test.go +++ /dev/null @@ -1 +0,0 @@ -package runtime diff --git a/xmpp.go b/xmpp.go new file mode 100644 index 0000000..e964bdb --- /dev/null +++ b/xmpp.go @@ -0,0 +1,75 @@ +package main + +import ( + "github.com/bdlm/log" + "gosrc.io/xmpp" +) + +var client *xmpp.Client +var mucs []string + +func notify(text string) { + msg := xmpp.Message{ + Attrs: xmpp.Attrs{Type: xmpp.MessageTypeGroupchat}, + Body: text, + } + + for _, muc := range config.StartupNotifyMuc { + msg.To = muc + client.Send(msg) + } + + msg.Type = xmpp.MessageTypeChat + for _, user := range config.StartupNotifyUser { + msg.To = user + client.Send(msg) + } + log.Infof("notify: %s", text) +} + +func joinMUC(to, nick string) error { + + maxstanzas := 0 + toJID, err := xmpp.NewJid(to) + if err != nil { + return err + } + toJID.Resource = nick + jid := toJID.Full() + + mucs = append(mucs, jid) + + return client.Send(xmpp.Presence{Attrs: xmpp.Attrs{To: jid}, + Extensions: []xmpp.PresExtension{ + xmpp.MucPresence{ + History: xmpp.History{MaxStanzas: &maxstanzas}, + }}, + }) + +} + +func postStartup(c xmpp.StreamClient) { + for _, muc := range config.StartupNotifyMuc { + joinMUC(muc, config.Nickname) + } + for _, hooks := range config.Hooks { + for _, hook := range hooks { + for _, muc := range hook.NotifyMuc { + joinMUC(muc, config.Nickname) + } + } + } + notify("started hock2xmpp") +} + +func closeXMPP() { + notify("stopped of hock2xmpp") + + for _, muc := range mucs { + client.Send(xmpp.Presence{Attrs: xmpp.Attrs{ + To: muc, + Type: xmpp.PresenceTypeUnavailable, + }}) + } + +}