-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmpp.go
89 lines (76 loc) · 1.95 KB
/
xmpp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"github.com/bdlm/log"
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
)
var mucs []string
func notify(c xmpp.Sender, text string) {
msg := stanza.Message{
Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat},
Body: text,
}
for _, muc := range config.StartupNotifyMuc {
msg.To = muc
if err := c.Send(msg); err != nil {
log.WithFields(map[string]interface{}{
"muc": muc,
"msg": text,
}).Errorf("error on startup notify: %s", err)
}
}
msg.Type = stanza.MessageTypeChat
for _, user := range config.StartupNotifyUser {
msg.To = user
if err := c.Send(msg); err != nil {
log.WithFields(map[string]interface{}{
"user": user,
"msg": text,
}).Errorf("error on startup notify: %s", err)
}
}
log.Infof("notify: %s", text)
}
func joinMUC(c xmpp.Sender, to, nick string) error {
toJID, err := xmpp.NewJid(to)
if err != nil {
return err
}
toJID.Resource = nick
jid := toJID.Full()
mucs = append(mucs, jid)
return c.Send(stanza.Presence{Attrs: stanza.Attrs{To: jid},
Extensions: []stanza.PresExtension{
stanza.MucPresence{
History: stanza.History{MaxStanzas: stanza.NewNullableInt(0)},
}},
})
}
func postStartup(c xmpp.Sender) {
for _, muc := range config.StartupNotifyMuc {
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(c, muc, config.Nickname); err != nil {
log.WithField("muc", muc).Errorf("error on joining muc: %s", err)
}
}
}
}
notify(c, "started hock2xmpp")
}
func closeXMPP(c xmpp.Sender) {
notify(c, "stopped of hock2xmpp")
for _, muc := range mucs {
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)
}
}
}