forked from talkkonnect/talkkonnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonevent.go
321 lines (285 loc) · 9.15 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
/*
* 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"
"time"
"github.com/talkkonnect/gumble/gumble"
)
func (b *Talkkonnect) OnConnect(e *gumble.ConnectEvent) {
if IsConnected {
GPIOOutPin("online", "on")
MyLedStripOnlineLEDOn()
return
}
IsConnected = true
GPIOOutPin("online", "on")
MyLedStripOnlineLEDOn()
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("info: Connected to %s Address %s on attempt %d index [%d] ", b.Name, b.Client.Conn.RemoteAddr(), b.ConnectAttempts, AccountIndex)
if e.WelcomeMessage != nil {
var tmessage string = fmt.Sprintf("%v", esc(*e.WelcomeMessage))
for _, line := range strings.Split(strings.TrimSpace(tmessage), "\n") {
log.Println("info: ", strings.TrimSpace(line))
}
}
if Config.Global.Hardware.TargetBoard == "rpi" {
GPIOOutPin("online", "on")
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) {
b.BackLightTimer()
var reason string
switch e.Type {
case gumble.DisconnectError:
reason = "connection error"
}
IsConnected = false
GPIOOutPin("online", "off")
MyLedStripOnlineLEDOff()
if Config.Global.Hardware.TargetBoard == "rpi" {
GPIOOutAll("led/relay", "off")
MyLedStripGPIOOffAll()
}
log.Println("alert: Attempting Reconnect in 5 seconds...")
log.Println("alert: Connection to ", b.Address, "disconnected")
log.Println("alert: Disconnection Reason ", reason)
time.Sleep(5 * time.Second)
b.ReConnect()
}
func (b *Talkkonnect) OnTextMessage(e *gumble.TextMessageEvent) {
b.BackLightTimer()
var eventSound EventSoundStruct = findEventSound("message")
if eventSound.Enabled {
if v, err := strconv.Atoi(eventSound.Volume); err == nil {
localMediaPlayer(eventSound.FileName, v, eventSound.Blocking, 0, 1)
}
}
if len(cleanstring(e.Message)) > 105 {
log.Println("warn: Message Too Long to Be Displayed on Screen")
tmessage = strings.TrimSpace(cleanstring(e.Message)[:105])
} else {
tmessage = 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(tmessage))+") from %v %v\n", sender, tmessage))
for _, tts := range Config.Global.Software.TTS.Sound {
if tts.Action == "message" {
if tts.Enabled {
voiceMessage := fmt.Sprintf("Message from %v %v\n", sender, cleanstring(e.Message))
if Config.Global.Software.TTSMessages.TTSMessageFromTag {
b.TTSPlayerMessage(voiceMessage, Config.Global.Software.TTSMessages.LocalPlay, Config.Global.Software.TTSMessages.PlayIntoStream)
} else {
b.TTSPlayerMessage(cleanstring(e.Message), Config.Global.Software.TTSMessages.LocalPlay, Config.Global.Software.TTSMessages.PlayIntoStream)
}
}
}
}
if Config.Global.Hardware.TargetBoard == "rpi" {
if LCDEnabled {
LcdText[0] = "Msg From " + sender
LcdText[1] = tmessage
LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled {
oledDisplay(false, 2, 1, "Msg From "+sender)
if len(tmessage) <= 21 {
oledDisplay(false, 3, 1, tmessage)
oledDisplay(false, 4, 1, "")
oledDisplay(false, 5, 1, "")
oledDisplay(false, 6, 1, "")
oledDisplay(false, 7, 1, "")
return
}
if len(tmessage) <= 42 {
oledDisplay(false, 3, 1, tmessage[0:21])
oledDisplay(false, 4, 1, tmessage[21:])
oledDisplay(false, 5, 1, "")
oledDisplay(false, 6, 1, "")
oledDisplay(false, 7, 1, "")
return
}
if len(tmessage) <= 63 {
oledDisplay(false, 3, 1, tmessage[0:21])
oledDisplay(false, 4, 1, tmessage[21:42])
oledDisplay(false, 5, 1, tmessage[42:])
oledDisplay(false, 6, 1, "")
oledDisplay(false, 7, 1, "")
return
}
if len(tmessage) <= 84 {
oledDisplay(false, 3, 1, tmessage[0:21])
oledDisplay(false, 4, 1, tmessage[21:42])
oledDisplay(false, 5, 1, tmessage[42:63])
oledDisplay(false, 6, 1, tmessage[63:])
oledDisplay(false, 7, 1, "")
return
}
if len(tmessage) <= 105 {
oledDisplay(false, 3, 1, tmessage[0:20])
oledDisplay(false, 4, 1, tmessage[21:44])
oledDisplay(false, 5, 1, tmessage[42:63])
oledDisplay(false, 6, 1, tmessage[63:84])
oledDisplay(false, 7, 1, tmessage[84:])
return
}
}
}
}
func (b *Talkkonnect) OnUserChange(e *gumble.UserChangeEvent) {
b.BackLightTimer()
var info string
switch e.Type {
case gumble.UserChangeConnected:
info = "connected"
b.ParticipantLEDUpdate(true)
case gumble.UserChangeDisconnected:
info = "disconnected"
b.ParticipantLEDUpdate(true)
case gumble.UserChangeKicked:
info = "kicked"
b.ParticipantLEDUpdate(true)
case gumble.UserChangeBanned:
info = "banned"
case gumble.UserChangeRegistered:
info = "registered"
case gumble.UserChangeUnregistered:
info = "unregistered"
case gumble.UserChangeName:
info = "changed name"
case gumble.UserChangeChannel:
info = "changed channel"
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 len(info) > 0 {
if info == "changed channel" {
b.ParticipantLEDUpdate(false)
}
log.Printf("info: User %vs %v\n", cleanstring(e.User.Name), info)
} else {
b.ParticipantLEDUpdate(true)
}
}
func (b *Talkkonnect) OnPermissionDenied(e *gumble.PermissionDeniedEvent) {
switch e.Type {
case gumble.PermissionDeniedPermission:
log.Printf("warn: Permission Denied For Channel ID %v Channel Name %v\n", e.Channel.ID, e.Channel.Name)
for index, ch := range ChannelsList {
if ch.chanID == int(e.Channel.ID) {
log.Printf("warn: Setting Channel Index %v Channel ID %v Channel Name %v Channel Enter to False\n", index, e.Channel.ID, e.Channel.Name)
ChannelsList[index].chanenterPermissions = false
if ChannelAction == "channelup" {
b.ChannelUp()
}
if ChannelAction == "channeldown" {
b.ChannelDown()
}
break
}
}
case gumble.PermissionDeniedSuperUser:
log.Println("cannot modify SuperUser")
case gumble.PermissionDeniedInvalidChannelName:
log.Println("invalid channel name")
case gumble.PermissionDeniedTextTooLong:
log.Println("text too long")
case gumble.PermissionDeniedTemporaryChannel:
log.Println("temporary channel")
case gumble.PermissionDeniedMissingCertificate:
log.Println("missing certificate")
case gumble.PermissionDeniedInvalidUserName:
log.Println("invalid user name")
case gumble.PermissionDeniedChannelFull:
log.Println("channel full")
case gumble.PermissionDeniedNestingLimit:
log.Println("nesting limit")
case gumble.PermissionDeniedOther:
log.Println("other")
}
}
func (b *Talkkonnect) OnChannelChange(e *gumble.ChannelChangeEvent) {
log.Println("alert: Channel Change Event Detected")
}
func (b *Talkkonnect) OnUserList(e *gumble.UserListEvent) {
log.Println("debug: On User List Event Detected")
}
func (b *Talkkonnect) OnACL(e *gumble.ACLEvent) {
log.Println("debug: On ACL Event Detected")
}
func (b *Talkkonnect) OnBanList(e *gumble.BanListEvent) {
log.Println("debug: OnBanList Event Detected")
}
func (b *Talkkonnect) OnContextActionChange(e *gumble.ContextActionChangeEvent) {
log.Println("debug: OnContextActionChange Event Detected")
}
func (b *Talkkonnect) OnServerConfig(e *gumble.ServerConfigEvent) {
log.Println("debug: OnServerConfigs Event Detected")
}