/
mumblebee.go
328 lines (291 loc) 路 7.28 KB
/
mumblebee.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* Copyright (C) 2014-2017 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Martin Schlierf <go@koma666.de>
*/
// Package mumblebee is a Bee that can connect to a Mumble/XMPP server.
package mumblebee
import (
"crypto/tls"
"net"
"strconv"
"strings"
"github.com/muesli/beehive/bees"
"layeh.com/gumble/gumble"
"layeh.com/gumble/gumbleutil"
)
// MumbleBee is a Bee that can connect to a Mumble server.
type MumbleBee struct {
bees.Bee
// channel signaling mumble connection status
connectedState chan bool
client *gumble.Client
server string
user string
password string
insecure bool
channel string
}
// Action triggers the action passed to it.
func (mod *MumbleBee) Action(action bees.Action) []bees.Placeholder {
outs := []bees.Placeholder{}
switch action.Name {
case "send_to_user":
user, text := "", ""
action.Options.Bind("user", &user)
action.Options.Bind("text", &text)
mod.client.Users.Find(user).Send(text)
case "send_to_channel":
channel, text := "", ""
action.Options.Bind("channel", &channel)
action.Options.Bind("text", &text)
if channel != "" {
mod.client.Channels.Find(channel).Send(text, false)
} else {
mod.client.Self.Channel.Send(text, false)
}
case "move":
channel := ""
action.Options.Bind("channel", &channel)
mod.move(channel)
default:
panic("Unknown action triggered in " + mod.Name() + ": " + action.Name)
}
return outs
}
func (mod *MumbleBee) move(channel string) {
channel = strings.TrimSpace(channel)
if len(channel) > 0 {
channels := strings.Split(channel, "/")
if channels[0] == "Root" {
channels = channels[1:]
}
result := mod.client.Channels.Find(channels...)
if result != nil {
mod.client.Self.Move(result)
mod.Logln("Moved to channel:", channel)
} else {
mod.Logln("Moved to channel:", channel, " failed. (channel not found)")
}
}
}
// Run executes the Bee's event loop.
func (mod *MumbleBee) Run(eventChan chan bees.Event) {
if len(mod.server) == 0 {
return
}
// channel signaling mumble connection status
mod.connectedState = make(chan bool)
host, port, err := net.SplitHostPort(mod.server)
if err != nil {
host = mod.server
port = strconv.Itoa(gumble.DefaultPort)
}
config := gumble.NewConfig()
config.Username = mod.user
config.Password = mod.password
address := net.JoinHostPort(host, port)
var tlsConfig tls.Config
if mod.insecure {
tlsConfig.InsecureSkipVerify = true
}
config.Attach(gumbleutil.Listener{
TextMessage: func(e *gumble.TextMessageEvent) {
ev := bees.Event{
Bee: mod.Name(),
Name: "message",
Options: []bees.Placeholder{
{
Name: "channels",
Type: "[]string",
Value: e.Channels,
},
{
Name: "user",
Type: "string",
Value: e.Sender.Name,
},
{
Name: "text",
Type: "string",
Value: e.Message,
},
},
}
eventChan <- ev
},
})
config.Attach(gumbleutil.Listener{
Disconnect: func(e *gumble.DisconnectEvent) {
go func() {
mod.connectedState <- false
}()
},
})
config.Attach(gumbleutil.Listener{
Connect: func(e *gumble.ConnectEvent) {
go func() {
mod.connectedState <- true
}()
},
})
config.Attach(gumbleutil.Listener{
UserChange: func(e *gumble.UserChangeEvent) {
eventType := ""
if e.Type.Has(gumble.UserChangeConnected) {
eventType = "user_connected"
} else if e.Type.Has(gumble.UserChangeDisconnected) {
eventType = "user_disconnected"
} else if e.Type.Has(gumble.UserChangeRegistered) {
eventType = "user_registered"
} else {
return
}
ev := bees.Event{
Bee: mod.Name(),
Name: eventType,
Options: []bees.Placeholder{
{
Name: "user",
Type: "string",
Value: e.User.Name,
},
},
}
eventChan <- ev
},
})
config.Attach(gumbleutil.Listener{
UserChange: func(e *gumble.UserChangeEvent) {
eventType := ""
if e.Type.Has(gumble.UserChangeKicked) {
eventType = "user_banned"
} else if e.Type.Has(gumble.UserChangeBanned) {
eventType = "user_kicked"
} else {
return
}
ev := bees.Event{
Bee: mod.Name(),
Name: eventType,
Options: []bees.Placeholder{
{
Name: "user",
Type: "string",
Value: e.User.Name,
},
{
Name: "admin",
Type: "string",
Value: e.Actor.Name,
},
{
Name: "message",
Type: "string",
Value: e.String,
},
},
}
eventChan <- ev
},
})
config.Attach(gumbleutil.Listener{
UserChange: func(e *gumble.UserChangeEvent) {
if e.Type.Has(gumble.UserChangeChannel) {
ev := bees.Event{
Bee: mod.Name(),
Name: "user_changed_channel",
Options: []bees.Placeholder{
{
Name: "user",
Type: "string",
Value: e.User.Name,
},
{
Name: "channel",
Type: "string",
Value: strings.Join(gumbleutil.ChannelPath(e.User.Channel), "/"),
},
},
}
eventChan <- ev
}
},
})
/*/ ToDo: Use own client certs
if *certificateFile != "" {
if *keyFile == "" {
keyFile = certificateFile
}
if certificate, err := tls.LoadX509KeyPair(*certificateFile, *keyFile); err != nil {
fmt.Printf("%s: %s\n", os.Args[0], err)
os.Exit(1)
} else {
tlsConfig.Certificates = append(tlsConfig.Certificates, certificate)
}
}
//*/
config.Attach(gumbleutil.AutoBitrate)
connecting := false
disconnected := true
waitForDisconnect := false
for {
// loop on mumble connection events
if disconnected {
if waitForDisconnect {
return
}
if !connecting {
connecting = true
mod.Logln("Connecting to Mumble:", mod.server)
mod.client, err = gumble.DialWithDialer(new(net.Dialer), address, config, &tlsConfig)
mod.Logln("Connected to:", mod.server)
if err != nil {
mod.Logln("Failed to connect to Mumble:", mod.server, err)
connecting = false
}
}
}
select {
case status := <-mod.connectedState:
if status {
mod.Logln("Connected to Mumble:", mod.server)
connecting = false
disconnected = false
mod.move(mod.channel)
} else {
mod.Logln("Disconnected from Mumble:", mod.server)
connecting = false
disconnected = true
}
case <-mod.SigChan:
if !waitForDisconnect {
mod.client.Disconnect()
}
waitForDisconnect = true
}
}
}
// ReloadOptions parses the config options and initializes the Bee.
func (mod *MumbleBee) ReloadOptions(options bees.BeeOptions) {
mod.SetOptions(options)
options.Bind("address", &mod.server)
options.Bind("channel", &mod.channel)
options.Bind("user", &mod.user)
options.Bind("password", &mod.password)
options.Bind("insecure", &mod.insecure)
}