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() {
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)

View File

@ -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)

View File

@ -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")

View File

@ -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)

View File

@ -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()

View File

@ -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)

View File

@ -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

View File

@ -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,

26
xmpp.go
View File

@ -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)
}
}
}