From aa37652fdf0a53298d7bb3d6275944860a86b792 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Mon, 15 Jul 2019 23:49:56 +0200 Subject: [PATCH] use Sender interface of gosrc.io/xmpp --- circleci/main.go | 2 +- git/main.go | 2 +- gitlab/main.go | 2 +- grafana/main.go | 2 +- main.go | 3 ++- prometheus/main.go | 2 +- runtime/hooks.go | 2 +- runtime/xmpp.go | 4 ++-- xmpp.go | 26 ++++++++++++-------------- 9 files changed, 22 insertions(+), 23 deletions(-) diff --git a/circleci/main.go b/circleci/main.go index f85b7d1..2c585fa 100644 --- a/circleci/main.go +++ b/circleci/main.go @@ -30,7 +30,7 @@ func (r requestBody) String() string { } func init() { - runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { + runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { log.WithField("type", hookType).Info("loaded") return func(w http.ResponseWriter, r *http.Request) { logger := log.WithField("type", hookType) diff --git a/git/main.go b/git/main.go index 35db749..738d47b 100644 --- a/git/main.go +++ b/git/main.go @@ -21,7 +21,7 @@ var eventHeader = map[string]string{ const hookType = "git" func init() { - runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { + runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { log.WithField("type", hookType).Info("loaded") return func(w http.ResponseWriter, r *http.Request) { logger := log.WithField("type", hookType) diff --git a/gitlab/main.go b/gitlab/main.go index 0d10b7b..ce27460 100644 --- a/gitlab/main.go +++ b/gitlab/main.go @@ -24,7 +24,7 @@ var eventHeader = map[string]string{ const hookType = "gitlab" func init() { - runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { + runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { log.WithField("type", hookType).Info("loaded") return func(w http.ResponseWriter, r *http.Request) { event := r.Header.Get("X-Gitlab-Event") diff --git a/grafana/main.go b/grafana/main.go index 47ecc25..eb7b15c 100644 --- a/grafana/main.go +++ b/grafana/main.go @@ -39,7 +39,7 @@ func (r requestBody) String() string { } func init() { - runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { + runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { log.WithField("type", hookType).Info("loaded") return func(w http.ResponseWriter, r *http.Request) { logger := log.WithField("type", hookType) diff --git a/main.go b/main.go index 170f87f..231b06f 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,7 @@ func main() { log.SetLevel(config.LogLevel) router := xmpp.NewRouter() + var err error client, err := xmpp.NewClient(xmpp.Config{ Address: config.XMPP.Address, Jid: config.XMPP.JID, @@ -69,7 +70,7 @@ func main() { signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) sig := <-sigs - closeXMPP() + closeXMPP(client) srv.Close() diff --git a/prometheus/main.go b/prometheus/main.go index 880fa21..0867d9d 100644 --- a/prometheus/main.go +++ b/prometheus/main.go @@ -17,7 +17,7 @@ import ( const hookType = "prometheus" func init() { - runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { + runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { log.WithField("type", hookType).Info("loaded") return func(w http.ResponseWriter, r *http.Request) { logger := log.WithField("type", hookType) diff --git a/runtime/hooks.go b/runtime/hooks.go index 70102db..dca595a 100644 --- a/runtime/hooks.go +++ b/runtime/hooks.go @@ -6,7 +6,7 @@ import ( "gosrc.io/xmpp" ) -type HookHandler func(*xmpp.Client, []Hook) func(http.ResponseWriter, *http.Request) +type HookHandler func(xmpp.Sender, []Hook) func(http.ResponseWriter, *http.Request) var HookRegister map[string]HookHandler diff --git a/runtime/xmpp.go b/runtime/xmpp.go index 69579c3..e2c05fa 100644 --- a/runtime/xmpp.go +++ b/runtime/xmpp.go @@ -7,7 +7,7 @@ import ( "gosrc.io/xmpp/stanza" ) -func NotifyImage(client *xmpp.Client, hook Hook, url string, desc string) { +func NotifyImage(client xmpp.Sender, hook Hook, url string, desc string) { msg := stanza.Message{ Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat}, Body: url, @@ -38,7 +38,7 @@ func NotifyImage(client *xmpp.Client, hook Hook, url string, desc string) { } } -func Notify(client *xmpp.Client, hook Hook, text, html string) { +func Notify(client xmpp.Sender, hook Hook, text, html string) { msg := stanza.Message{ Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat}, Body: text, diff --git a/xmpp.go b/xmpp.go index 29d5334..d2188af 100644 --- a/xmpp.go +++ b/xmpp.go @@ -6,10 +6,9 @@ import ( "gosrc.io/xmpp/stanza" ) -var client *xmpp.Client var mucs []string -func notify(text string) { +func notify(c xmpp.Sender, text string) { msg := stanza.Message{ Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat}, Body: text, @@ -17,7 +16,7 @@ func notify(text string) { for _, muc := range config.StartupNotifyMuc { msg.To = muc - if err := client.Send(msg); err != nil { + if err := c.Send(msg); err != nil { log.WithFields(map[string]interface{}{ "muc": muc, "msg": text, @@ -28,7 +27,7 @@ func notify(text string) { msg.Type = stanza.MessageTypeChat for _, user := range config.StartupNotifyUser { msg.To = user - if err := client.Send(msg); err != nil { + if err := c.Send(msg); err != nil { log.WithFields(map[string]interface{}{ "user": user, "msg": text, @@ -38,7 +37,7 @@ func notify(text string) { log.Infof("notify: %s", text) } -func joinMUC(to, nick string) error { +func joinMUC(c xmpp.Sender, to, nick string) error { toJID, err := xmpp.NewJid(to) if err != nil { @@ -49,7 +48,7 @@ func joinMUC(to, nick string) error { mucs = append(mucs, jid) - return client.Send(stanza.Presence{Attrs: stanza.Attrs{To: jid}, + return c.Send(stanza.Presence{Attrs: stanza.Attrs{To: jid}, Extensions: []stanza.PresExtension{ stanza.MucPresence{ History: stanza.History{MaxStanzas: stanza.NewNullableInt(0)}, @@ -58,34 +57,33 @@ func joinMUC(to, nick string) error { } -func postStartup(c xmpp.StreamClient) { +func postStartup(c xmpp.Sender) { for _, muc := range config.StartupNotifyMuc { - if err := joinMUC(muc, config.Nickname); err != nil { + if err := joinMUC(c, muc, config.Nickname); err != nil { log.WithField("muc", muc).Errorf("error on joining muc: %s", err) } } for _, hooks := range config.Hooks { for _, hook := range hooks { for _, muc := range hook.NotifyMuc { - if err := joinMUC(muc, config.Nickname); err != nil { + if err := joinMUC(c, muc, config.Nickname); err != nil { log.WithField("muc", muc).Errorf("error on joining muc: %s", err) } } } } - notify("started hock2xmpp") + notify(c, "started hock2xmpp") } -func closeXMPP() { - notify("stopped of hock2xmpp") +func closeXMPP(c xmpp.Sender) { + notify(c, "stopped of hock2xmpp") for _, muc := range mucs { - if err := client.Send(stanza.Presence{Attrs: stanza.Attrs{ + if err := c.Send(stanza.Presence{Attrs: stanza.Attrs{ To: muc, Type: stanza.PresenceTypeUnavailable, }}); err != nil { log.WithField("muc", muc).Errorf("error on leaving muc: %s", err) } } - }