forked from talkkonnect/talkkonnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonevent.go
340 lines (290 loc) · 9.65 KB
/
onevent.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
329
330
331
332
333
334
335
336
337
338
339
340
/*
* talkkonnect headless mumble client/gateway with lcd screen and channel control
* Copyright (C) 2018-2019, Suvir Kumar <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* talkkonnect is the based on talkiepi and barnard by Daniel Chote and Tim Cooper
*
* The Initial Developer of the Original Code is
* Suvir Kumar <[email protected]>
* Portions created by the Initial Developer are Copyright (C) Suvir Kumar. All Rights Reserved.
*
* Contributor(s):
*
* Suvir Kumar <[email protected]>
*
* My Blog is at www.talkkonnect.com
* The source code is hosted at github.com/talkkonnect
*
*
*/
package talkkonnect
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/talkkonnect/gumble/gumble"
)
func (b *Talkkonnect) OnConnect(e *gumble.ConnectEvent) {
if IsConnected {
return
}
IsConnected = true
b.BackLightTimer()
b.Client = e.Client
ConnectAttempts = 1
//serialize tokens send one by one from slice to server
if len(Tokens[AccountIndex]) > 0 {
ATokens := make(gumble.AccessTokens, len(Tokens[AccountIndex]))
for i, value := range Tokens[AccountIndex] {
ATokens[i] = value
}
b.Client.Send(ATokens)
}
log.Printf("debug: Connected to %s Address %s on attempt %d index [%d]\n ", b.Name, b.Client.Conn.RemoteAddr(), b.ConnectAttempts, AccountIndex)
if e.WelcomeMessage != nil {
var message string = fmt.Sprintf("%v", esc(*e.WelcomeMessage))
log.Println("info: Welcome message: ")
for _, line := range strings.Split(strings.TrimSuffix(message, "\n"), "\n") {
log.Println("info: ", line)
}
}
if TargetBoard == "rpi" {
if !LedStripEnabled {
b.LEDOn(b.OnlineLED)
} else {
MyLedStripOnlineLEDOn()
}
if LCDEnabled {
LcdText = [4]string{"nil", "nil", "nil", "nil"}
LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled {
Oled.DisplayOn()
LCDIsDark = false
oledDisplay(true, 0, 0, "") // clear the screen
}
b.ParticipantLEDUpdate(true)
}
if b.ChannelName != "" {
b.ChangeChannel(b.ChannelName)
prevChannelID = b.Client.Self.Channel.ID
}
}
func (b *Talkkonnect) OnDisconnect(e *gumble.DisconnectEvent) {
if !ServerHop {
b.BackLightTimer()
}
var reason string
switch e.Type {
case gumble.DisconnectError:
reason = "connection error"
}
IsConnected = false
if TargetBoard == "rpi" {
if !LedStripEnabled {
b.LEDOffAll()
} else {
MyLedStripLEDOffAll()
}
}
if !ServerHop {
log.Println("alert: Attempting Reconnect in 10 seconds...")
log.Println("alert: Connection to ", b.Address, "disconnected")
log.Println("alert: Disconnection Reason ", reason)
b.ReConnect()
}
}
func (b *Talkkonnect) OnTextMessage(e *gumble.TextMessageEvent) {
b.BackLightTimer()
if len(cleanstring(e.Message)) > 105 {
log.Println("warn: Message Too Long to Be Displayed on Screen")
message = strings.TrimSpace(cleanstring(e.Message)[:105])
} else {
message = strings.TrimSpace(cleanstring(e.Message))
}
var sender string
if e.Sender != nil {
sender = strings.TrimSpace(cleanstring(e.Sender.Name))
log.Println("info: Sender Name is ", sender)
} else {
sender = ""
}
log.Println(fmt.Sprintf("info: Message ("+strconv.Itoa(len(message))+") from %v %v\n", sender, message))
if TTSMessageEnabled {
voiceMessage := fmt.Sprintf("Message from %v %v\n", sender, cleanstring(e.Message))
b.TTSPlayer(voiceMessage, TTSLocalPlay, TTSLocalPlayWithRXLED, TTSPlayIntoStream)
}
if TargetBoard == "rpi" {
if LCDEnabled {
LcdText[0] = "Msg From " + sender
LcdText[1] = message
LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled {
oledDisplay(false, 2, 1, "Msg From "+sender)
if len(message) <= 21 {
oledDisplay(false, 3, 1, message)
oledDisplay(false, 4, 1, "")
oledDisplay(false, 5, 1, "")
oledDisplay(false, 6, 1, "")
oledDisplay(false, 7, 1, "")
} else if len(message) <= 42 {
oledDisplay(false, 3, 1, message[0:21])
oledDisplay(false, 4, 1, message[21:41])
oledDisplay(false, 5, 1, "")
oledDisplay(false, 6, 1, "")
oledDisplay(false, 7, 1, "")
} else if len(message) <= 63 {
oledDisplay(false, 3, 1, message[0:21])
oledDisplay(false, 4, 1, message[21:42])
oledDisplay(false, 5, 1, message[42:])
oledDisplay(false, 6, 1, "")
oledDisplay(false, 7, 1, "")
} else if len(message) <= 84 {
oledDisplay(false, 3, 1, message[0:21])
oledDisplay(false, 4, 1, message[21:42])
oledDisplay(false, 5, 1, message[42:63])
oledDisplay(false, 6, 1, message[63:])
oledDisplay(false, 7, 1, "")
} else if len(message) <= 105 {
oledDisplay(false, 3, 1, message[0:20])
oledDisplay(false, 4, 1, message[21:44])
oledDisplay(false, 5, 1, message[42:63])
oledDisplay(false, 6, 1, message[63:84])
oledDisplay(false, 7, 1, message[84:105])
}
}
}
if EventSoundEnabled {
err := aplayLocal(EventMessageSoundFilenameAndPath, 100)
if err != nil {
log.Println("error: aplayLocal(EventMessageSoundFilenameAndPath) Returned Error: ", err)
}
}
}
func (b *Talkkonnect) OnUserChange(e *gumble.UserChangeEvent) {
b.BackLightTimer()
var info string
switch e.Type {
case gumble.UserChangeConnected:
info = "conn"
case gumble.UserChangeDisconnected:
info = "disconnected!"
case gumble.UserChangeKicked:
info = "kicked"
case gumble.UserChangeBanned:
info = "banned"
case gumble.UserChangeRegistered:
info = "registered"
case gumble.UserChangeUnregistered:
info = "unregistered"
case gumble.UserChangeName:
info = "chg name"
case gumble.UserChangeChannel:
info = "chg channel"
log.Println("info:", cleanstring(e.User.Name), " Changed Channel to ", e.User.Channel.Name)
LcdText[2] = cleanstring(e.User.Name) + "->" + e.User.Channel.Name
LcdText[3] = ""
case gumble.UserChangeComment:
info = "chg comment"
case gumble.UserChangeAudio:
info = "chg audio"
case gumble.UserChangePrioritySpeaker:
info = "is priority"
case gumble.UserChangeRecording:
info = "chg rec status"
case gumble.UserChangeStats:
info = "chg stats"
if info != "chg channel" {
if info != "" {
log.Println("info: User ", cleanstring(e.User.Name), " ", info, "Event type=", e.Type, " channel=", e.User.Channel.Name)
if TTSEnabled && TTSParticipants {
b.Speak("User "+cleanstring(e.User.Name)+info+"Has Changed to "+e.User.Channel.Name, "local", 1, 0, 1)
}
}
} else {
log.Println("info: User ", cleanstring(e.User.Name), " Event type=", e.Type, " channel=", e.User.Channel.Name)
}
LcdText[2] = cleanstring(e.User.Name) + " " + info //+strconv.Atoi(string(e.Type))
}
b.ParticipantLEDUpdate(true)
}
func (b *Talkkonnect) OnPermissionDenied(e *gumble.PermissionDeniedEvent) {
var info string
switch e.Type {
case gumble.PermissionDeniedOther:
info = e.String
case gumble.PermissionDeniedPermission:
info = "insufficient permissions"
LcdText[2] = "insufficient perms"
// Set Upper Boundary
if prevButtonPress == "ChannelUp" && b.Client.Self.Channel.ID == maxchannelid {
log.Println("error: Can't Increment Channel Maximum Channel Reached")
}
// Set Lower Boundary
if prevButtonPress == "ChannelDown" && currentChannelID == 0 {
log.Println("error: Can't Increment Channel Minimum Channel Reached")
}
// Implement Seek Up Until Permissions are Sufficient for User to Join Channel whilst avoiding all null channels
if prevButtonPress == "ChannelUp" && b.Client.Self.Channel.ID+1 < maxchannelid {
prevChannelID++
b.ChannelUp()
LcdText[1] = b.Client.Self.Channel.Name + " (" + strconv.Itoa(len(b.Client.Self.Channel.Users)) + " Users)"
}
// Implement Seek Dwn Until Permissions are Sufficient for User to Join Channel whilst avoiding all null channels
if prevButtonPress == "ChannelDown" && int(b.Client.Self.Channel.ID) > 0 {
prevChannelID--
b.ChannelDown()
LcdText[1] = b.Client.Self.Channel.Name + " (" + strconv.Itoa(len(b.Client.Self.Channel.Users)) + " Users)"
}
if TargetBoard == "rpi" {
if LCDEnabled {
LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled {
oledDisplay(false, 1, 1, LcdText[1])
oledDisplay(false, 2, 1, LcdText[2])
}
}
case gumble.PermissionDeniedSuperUser:
info = "cannot modify SuperUser"
case gumble.PermissionDeniedInvalidChannelName:
info = "invalid channel name"
case gumble.PermissionDeniedTextTooLong:
info = "text too long"
case gumble.PermissionDeniedTemporaryChannel:
info = "temporary channel"
case gumble.PermissionDeniedMissingCertificate:
info = "missing certificate"
case gumble.PermissionDeniedInvalidUserName:
info = "invalid user name"
case gumble.PermissionDeniedChannelFull:
info = "channel full"
case gumble.PermissionDeniedNestingLimit:
info = "nesting limit"
}
LcdText[2] = info
log.Printf("error: Permission denied %v to Join Channel %v\n", info, e.Channel.Name)
}
func (b *Talkkonnect) OnChannelChange(e *gumble.ChannelChangeEvent) {
}
func (b *Talkkonnect) OnUserList(e *gumble.UserListEvent) {
}
func (b *Talkkonnect) OnACL(e *gumble.ACLEvent) {
}
func (b *Talkkonnect) OnBanList(e *gumble.BanListEvent) {
}
func (b *Talkkonnect) OnContextActionChange(e *gumble.ContextActionChangeEvent) {
}
func (b *Talkkonnect) OnServerConfig(e *gumble.ServerConfigEvent) {
}