use Sender interface of gosrc.io/xmpp

This commit is contained in:
Martin/Geno 2019-07-15 23:49:56 +02:00
parent 2435b9b73b
commit aa37652fdf
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
9 changed files with 22 additions and 23 deletions

View File

@ -30,7 +30,7 @@ func (r requestBody) String() string {
} }
func init() { 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") log.WithField("type", hookType).Info("loaded")
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
logger := log.WithField("type", hookType) logger := log.WithField("type", hookType)

View File

@ -21,7 +21,7 @@ var eventHeader = map[string]string{
const hookType = "git" const hookType = "git"
func init() { 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") log.WithField("type", hookType).Info("loaded")
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
logger := log.WithField("type", hookType) logger := log.WithField("type", hookType)

View File

@ -24,7 +24,7 @@ var eventHeader = map[string]string{
const hookType = "gitlab" const hookType = "gitlab"
func init() { 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") log.WithField("type", hookType).Info("loaded")
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
event := r.Header.Get("X-Gitlab-Event") event := r.Header.Get("X-Gitlab-Event")

View File

@ -39,7 +39,7 @@ func (r requestBody) String() string {
} }
func init() { 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") log.WithField("type", hookType).Info("loaded")
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
logger := log.WithField("type", hookType) logger := log.WithField("type", hookType)

View File

@ -33,6 +33,7 @@ func main() {
log.SetLevel(config.LogLevel) log.SetLevel(config.LogLevel)
router := xmpp.NewRouter() router := xmpp.NewRouter()
var err error
client, err := xmpp.NewClient(xmpp.Config{ client, err := xmpp.NewClient(xmpp.Config{
Address: config.XMPP.Address, Address: config.XMPP.Address,
Jid: config.XMPP.JID, Jid: config.XMPP.JID,
@ -69,7 +70,7 @@ func main() {
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs sig := <-sigs
closeXMPP() closeXMPP(client)
srv.Close() srv.Close()

View File

@ -17,7 +17,7 @@ import (
const hookType = "prometheus" const hookType = "prometheus"
func init() { 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") log.WithField("type", hookType).Info("loaded")
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
logger := log.WithField("type", hookType) logger := log.WithField("type", hookType)

View File

@ -6,7 +6,7 @@ import (
"gosrc.io/xmpp" "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 var HookRegister map[string]HookHandler

View File

@ -7,7 +7,7 @@ import (
"gosrc.io/xmpp/stanza" "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{ msg := stanza.Message{
Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat}, Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat},
Body: url, 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{ msg := stanza.Message{
Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat}, Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat},
Body: text, Body: text,

26
xmpp.go
View File

@ -6,10 +6,9 @@ import (
"gosrc.io/xmpp/stanza" "gosrc.io/xmpp/stanza"
) )
var client *xmpp.Client
var mucs []string var mucs []string
func notify(text string) { func notify(c xmpp.Sender, text string) {
msg := stanza.Message{ msg := stanza.Message{
Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat}, Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat},
Body: text, Body: text,
@ -17,7 +16,7 @@ func notify(text string) {
for _, muc := range config.StartupNotifyMuc { for _, muc := range config.StartupNotifyMuc {
msg.To = muc msg.To = muc
if err := client.Send(msg); err != nil { if err := c.Send(msg); err != nil {
log.WithFields(map[string]interface{}{ log.WithFields(map[string]interface{}{
"muc": muc, "muc": muc,
"msg": text, "msg": text,
@ -28,7 +27,7 @@ func notify(text string) {
msg.Type = stanza.MessageTypeChat msg.Type = stanza.MessageTypeChat
for _, user := range config.StartupNotifyUser { for _, user := range config.StartupNotifyUser {
msg.To = user msg.To = user
if err := client.Send(msg); err != nil { if err := c.Send(msg); err != nil {
log.WithFields(map[string]interface{}{ log.WithFields(map[string]interface{}{
"user": user, "user": user,
"msg": text, "msg": text,
@ -38,7 +37,7 @@ func notify(text string) {
log.Infof("notify: %s", text) 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) toJID, err := xmpp.NewJid(to)
if err != nil { if err != nil {
@ -49,7 +48,7 @@ func joinMUC(to, nick string) error {
mucs = append(mucs, jid) 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{ Extensions: []stanza.PresExtension{
stanza.MucPresence{ stanza.MucPresence{
History: stanza.History{MaxStanzas: stanza.NewNullableInt(0)}, 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 { 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) log.WithField("muc", muc).Errorf("error on joining muc: %s", err)
} }
} }
for _, hooks := range config.Hooks { for _, hooks := range config.Hooks {
for _, hook := range hooks { for _, hook := range hooks {
for _, muc := range hook.NotifyMuc { 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) log.WithField("muc", muc).Errorf("error on joining muc: %s", err)
} }
} }
} }
} }
notify("started hock2xmpp") notify(c, "started hock2xmpp")
} }
func closeXMPP() { func closeXMPP(c xmpp.Sender) {
notify("stopped of hock2xmpp") notify(c, "stopped of hock2xmpp")
for _, muc := range mucs { for _, muc := range mucs {
if err := client.Send(stanza.Presence{Attrs: stanza.Attrs{ if err := c.Send(stanza.Presence{Attrs: stanza.Attrs{
To: muc, To: muc,
Type: stanza.PresenceTypeUnavailable, Type: stanza.PresenceTypeUnavailable,
}}); err != nil { }}); err != nil {
log.WithField("muc", muc).Errorf("error on leaving muc: %s", err) log.WithField("muc", muc).Errorf("error on leaving muc: %s", err)
} }
} }
} }