forked from processone/xmpp-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (115 loc) · 3.35 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"log"
"os"
"strconv"
"strings"
"gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
)
const (
serverDomain = iota + 1
recipient
jid
password
serverPort
message
recipientIsRoom
botAlias
defaultServerPort = "5222"
defaultBotAlias = "/github_bot"
)
func main() {
// Find server port from action config or use default one
var port string
if strings.TrimSpace(os.Args[serverPort]) == "" {
port = defaultServerPort
} else {
port = os.Args[serverPort]
}
// Build client and connect to server
config := xmpp.Config{
TransportConfiguration: xmpp.TransportConfiguration{
Address: os.Args[serverDomain] + ":" + port,
},
Jid: os.Args[jid],
Credential: xmpp.Password(os.Args[password]),
StreamLogger: os.Stdout,
Insecure: false,
}
router := xmpp.NewRouter()
client, err := xmpp.NewClient(&config, router, errorHandler)
if err != nil {
log.Fatalf("%+v", err)
}
errorHandler(client.Connect())
// Check if we want to send to a chat room or a single user
// Send presence to connect to chat room, if specified
// Set the correspondentJid
var clientJid *stanza.Jid
isRecipientRoom, err := strconv.ParseBool(os.Args[recipientIsRoom])
if err != nil {
panic("failed to determine if sending to a client or chat room : " + err.Error())
}
if isRecipientRoom {
// Building Jid for the room.
// the "resource". See XEP-0045
if strings.TrimSpace(os.Args[botAlias]) != "" {
if os.Args[botAlias][:1] != "/" {
os.Args[botAlias] = "/" + os.Args[botAlias]
}
clientJid, err = stanza.NewJid(os.Args[recipient] + os.Args[botAlias])
} else {
clientJid, err = stanza.NewJid(os.Args[recipient] + defaultBotAlias)
}
if err != nil {
panic(err)
}
// Sending room presence
errorHandler(joinMUC(client, clientJid))
} else {
clientJid, err = stanza.NewJid(os.Args[recipient])
if err != nil {
panic(err)
}
}
// Send github message to recipient or chat room
m := stanza.Message{Attrs: stanza.Attrs{To: clientJid.Bare(), Type: getMessageType(isRecipientRoom)}, Body: os.Args[message]}
errorHandler(client.Send(m))
// After sending the action message, let's disconnect from the chat room if we were connected to one.
if isRecipientRoom {
errorHandler(leaveMUC(client, clientJid))
}
// And disconnect from the server
errorHandler(client.Disconnect())
}
// errorHandler is the client error handler
func errorHandler(err error) {
if err != nil {
panic(err)
}
}
// joinMUC builds a presence stanza to request joining a chat room
func joinMUC(c xmpp.Sender, toJID *stanza.Jid) error {
return c.Send(stanza.Presence{Attrs: stanza.Attrs{To: toJID.Full()},
Extensions: []stanza.PresExtension{
stanza.MucPresence{
History: stanza.History{MaxStanzas: stanza.NewNullableInt(0)},
}},
})
}
// leaveMUC builds a presence stanza to request leaving a chat room
func leaveMUC(c xmpp.Sender, muc *stanza.Jid) error {
return c.Send(stanza.Presence{Attrs: stanza.Attrs{
To: muc.Full(),
Type: stanza.PresenceTypeUnavailable,
}})
}
// getMessageType figures out the right message type for the github message, depending on what recipient is targeted.
// Message type is important since servers are allowed to ignore messages that do not have an appropriate type
func getMessageType(isCorrespRoom bool) stanza.StanzaType {
if isCorrespRoom {
return stanza.MessageTypeGroupchat
}
return stanza.MessageTypeChat
}