forked from dchote/talkiepi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
217 lines (191 loc) · 5.08 KB
/
client.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
package talkiepi
import (
"fmt"
"net"
"os"
"strings"
"time"
"github.com/dchote/gumble/gumble"
"github.com/dchote/gumble/gumbleopenal"
"github.com/dchote/gumble/gumbleutil"
"github.com/kennygrant/sanitize"
)
func (b *Talkiepi) Init() {
b.Config.Attach(gumbleutil.AutoBitrate)
b.Config.Attach(b)
b.Connect()
}
func (b *Talkiepi) CleanUp() {
b.Client.Disconnect()
}
func (b *Talkiepi) Connect() {
var err error
b.ConnectAttempts++
_, err = gumble.DialWithDialer(new(net.Dialer), b.Address, b.Config, &b.TLSConfig)
if err != nil {
fmt.Printf("Connection to %s failed (%s), attempting again in 10 seconds...\n", b.Address, err)
b.ReConnect()
} else {
b.OpenStream()
b.TransmitStart() //start transmitting
fmt.Printf("TransmitStarted...\n")
}
}
func (b *Talkiepi) ReConnect() {
if b.Client != nil {
b.Client.Disconnect()
}
if b.ConnectAttempts < 100 {
go func() {
time.Sleep(10 * time.Second)
b.Connect()
}()
return
} else {
fmt.Fprintf(os.Stderr, "Unable to connect, giving up\n")
os.Exit(1)
}
}
func (b *Talkiepi) OpenStream() {
// Audio
if os.Getenv("ALSOFT_LOGLEVEL") == "" {
os.Setenv("ALSOFT_LOGLEVEL", "0")
}
if stream, err := gumbleopenal.New(b.Client); err != nil {
fmt.Fprintf(os.Stderr, "Stream open error (%s)\n", err)
os.Exit(1)
} else {
b.Stream = stream
}
}
func (b *Talkiepi) ResetStream() {
b.Stream.Destroy()
// Sleep a bit and re-open
time.Sleep(50 * time.Millisecond)
b.OpenStream()
}
func (b *Talkiepi) TransmitStart() {
if b.IsConnected == false {
return
}
b.IsTransmitting = true
b.Stream.StartSource()
}
func (b *Talkiepi) TransmitStop() {
if b.IsConnected == false {
return
}
b.Stream.StopSource()
b.IsTransmitting = false
}
func (b *Talkiepi) OnConnect(e *gumble.ConnectEvent) {
b.Client = e.Client
b.ConnectAttempts = 0
b.IsConnected = true
fmt.Printf("Connected to %s (%d)\n", b.Client.Conn.RemoteAddr(), b.ConnectAttempts)
if e.WelcomeMessage != nil {
fmt.Printf("Welcome message: %s\n", esc(*e.WelcomeMessage))
}
if b.ChannelName != "" {
b.ChangeChannel(b.ChannelName)
}
}
func (b *Talkiepi) OnDisconnect(e *gumble.DisconnectEvent) {
var reason string
switch e.Type {
case gumble.DisconnectError:
reason = "connection error"
}
b.IsConnected = false
if reason == "" {
fmt.Printf("Connection to %s disconnected, attempting again in 10 seconds...\n", b.Address)
} else {
fmt.Printf("Connection to %s disconnected (%s), attempting again in 10 seconds...\n", b.Address, reason)
}
// attempt to connect again
b.ReConnect()
}
func (b *Talkiepi) ChangeChannel(ChannelName string) {
channel := b.Client.Channels.Find(ChannelName)
if channel != nil {
b.Client.Self.Move(channel)
} else {
fmt.Printf("Unable to find channel: %s\n", ChannelName)
}
}
func (b *Talkiepi) OnTextMessage(e *gumble.TextMessageEvent) {
fmt.Printf("Message from %s: %s\n", e.Sender.Name, strings.TrimSpace(esc(e.Message)))
}
func (b *Talkiepi) OnUserChange(e *gumble.UserChangeEvent) {
var info string
switch e.Type {
case gumble.UserChangeConnected:
info = "connected"
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 = "changed name"
case gumble.UserChangeChannel:
info = "changed channel"
case gumble.UserChangeComment:
info = "changed comment"
case gumble.UserChangeAudio:
info = "changed audio"
case gumble.UserChangePrioritySpeaker:
info = "is priority speaker"
case gumble.UserChangeRecording:
info = "changed recording status"
case gumble.UserChangeStats:
info = "changed stats"
}
fmt.Printf("Change event for %s: %s (%d)\n", e.User.Name, info, e.Type)
}
func (b *Talkiepi) OnPermissionDenied(e *gumble.PermissionDeniedEvent) {
var info string
switch e.Type {
case gumble.PermissionDeniedOther:
info = e.String
case gumble.PermissionDeniedPermission:
info = "insufficient permissions"
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"
}
fmt.Printf("Permission denied: %s\n", info)
}
func (b *Talkiepi) OnChannelChange(e *gumble.ChannelChangeEvent) {
}
func (b *Talkiepi) OnUserList(e *gumble.UserListEvent) {
}
func (b *Talkiepi) OnACL(e *gumble.ACLEvent) {
}
func (b *Talkiepi) OnBanList(e *gumble.BanListEvent) {
}
func (b *Talkiepi) OnContextActionChange(e *gumble.ContextActionChangeEvent) {
}
func (b *Talkiepi) OnServerConfig(e *gumble.ServerConfigEvent) {
}
func esc(str string) string {
return sanitize.HTML(str)
}