/
main.go
372 lines (315 loc) · 7.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"io/ioutil"
"log"
"os"
"strconv"
"github.com/Rompei/inco"
"github.com/darfk/ts3"
)
// Client is object of client.
type Client struct {
CliID int `json:"cliId"`
CID int `json:"cId"`
Name string `json:"name"`
ChannelName string `json:"channelName"`
IsNotified bool `json:"isNotified"`
}
// NewClient is constructor of Client.
func NewClient(cliID, cID int, name string) *Client {
return &Client{
CliID: cliID,
CID: cID,
Name: name,
}
}
// NewClientFromMap create Client from map.
func NewClientFromMap(m map[string]string) (*Client, error) {
cliID, ok := m["clid"]
if !ok {
return nil, errors.New("information not enough")
}
cliIDInt, err := strconv.Atoi(cliID)
if err != nil {
return nil, err
}
cid, ok := m["cid"]
if !ok {
return nil, errors.New("information not enough")
}
cidInt, err := strconv.Atoi(cid)
if err != nil {
return nil, err
}
name, ok := m["client_nickname"]
if !ok {
return nil, errors.New("information not enough")
}
return NewClient(cliIDInt, cidInt, name), nil
}
func main() {
var (
username string
password string
serverID int
webhookURL string
output string
debug bool
)
flag.StringVar(&username, "u", "", "TS3 server query username")
flag.StringVar(&password, "p", "", "TS3 server query password")
flag.IntVar(&serverID, "id", 1, "Server ID")
flag.StringVar(&webhookURL, "url", "", "WebHookURL")
flag.StringVar(&output, "o", "clients.json", "Output file")
flag.BoolVar(&debug, "d", false, "Debug")
flag.Parse()
if username == "" || password == "" || webhookURL == "" {
panic(errors.New("Not enough options"))
}
client, err := ts3.NewClient(":10011")
if err != nil {
panic(err)
}
defer client.Close()
_, err = client.Exec(ts3.Login(username, password))
if err != nil {
panic(err)
}
_, err = client.Exec(ts3.Use(serverID))
if err != nil {
panic(err)
}
// Get client list.
newState, err := getClients(client)
if err != nil {
panic(err)
}
// Get channel information.
channels := make(map[int]string)
for i := range newState {
find := false
for k, v := range channels {
if newState[i].CID == k {
newState[i].ChannelName = v
find = true
break
}
}
if !find {
channelInfo, err := getChannelInfo(client, newState[i].CID)
if err != nil {
panic(err)
}
channelName := ts3.Unescape(channelInfo["channel_name"])
channels[newState[i].CID] = channelName
newState[i].ChannelName = channelName
}
}
// If output file is not exist, store state and exit.
if _, err := os.Stat(output); err != nil {
if err := storeClients(newState, output); err != nil {
panic(err)
}
os.Exit(0)
}
// Getting old state from output file.
oldState, err := loadClients(output)
if err != nil {
panic(err)
}
// Prepare to notify new clients.
var newClients []Client
for i := range newState {
if isExist, old := findClient(&newState[i], oldState); isExist && !old.IsNotified {
newClients = append(newClients, newState[i])
newState[i].IsNotified = true
} else if isExist && old.CID != newState[i].CID {
newClients = append(newClients, newState[i])
newState[i].IsNotified = true
} else if isExist && old.IsNotified {
newState[i].IsNotified = true
}
}
// Notify logged in clients.
if len(newClients) != 0 {
if debug {
// Debug
channelMap := makeChannelMap(newClients)
text := buildText(channelMap, true)
log.Printf(text)
} else {
if err := notifyNewClients(webhookURL, newClients); err != nil {
panic(err)
}
}
}
// Prepare to notify leaved clients.
var leavedClients []Client
for i := range oldState {
if oldState[i].IsNotified && !matchClient(&oldState[i], newState) {
leavedClients = append(leavedClients, oldState[i])
}
}
if len(leavedClients) != 0 {
if debug {
// Debug
channelMap := makeChannelMap(leavedClients)
text := buildText(channelMap, false)
log.Printf(text)
} else {
// Notify leaved clients.
if err := notifyLeavedClients(webhookURL, leavedClients); err != nil {
panic(err)
}
}
}
// Store new state.
err = storeClients(newState, output)
if err != nil {
panic(err)
}
}
func notifyNewClients(url string, clients []Client) error {
channelMap := makeChannelMap(clients)
text := buildText(channelMap, true)
if text == "" {
return nil
}
err := postToSlack(url, text)
if err != nil {
return err
}
return nil
}
func notifyLeavedClients(url string, clients []Client) error {
channelMap := makeChannelMap(clients)
text := buildText(channelMap, false)
if text == "" {
return nil
}
err := postToSlack(url, text)
if err != nil {
return err
}
return nil
}
func makeChannelMap(clients []Client) map[string][]string {
// Make map from clients based on channels.
channelMap := make(map[string][]string)
for i := range clients {
channelMap[clients[i].ChannelName] = append(channelMap[clients[i].ChannelName], clients[i].Name)
}
return channelMap
}
func buildText(info map[string][]string, login bool) string {
// Build text from channel map.
var w bytes.Buffer
for k, v := range info {
for i := range v {
w.WriteString(v[i])
if i != len(v)-1 {
if i == len(v)-2 {
w.WriteString(" and ")
} else {
w.WriteString(", ")
}
}
}
if login {
w.WriteString(" connected to ")
} else {
w.WriteString(" disconnected from ")
}
w.WriteString(k)
w.WriteString("\n")
}
return w.String()
}
func postToSlack(url, text string) error {
// Posting slack incoming webhooks.
msg := &inco.Message{
Text: text,
}
return inco.Incoming(url, msg)
}
func matchClient(target *Client, clientList []Client) (isExist bool) {
// Search for client is exist in the list.
for i := range clientList {
if target.CliID == clientList[i].CliID {
isExist = true
break
}
}
return
}
func findClient(target *Client, clientList []Client) (isExist bool, found *Client) {
// Search for client is exist and the instance.
for i := range clientList {
if target.CliID == clientList[i].CliID {
isExist = true
found = &clientList[i]
break
}
}
return
}
func getClients(client *ts3.Client) (res []Client, err error) {
// Get client information from the virtual server.
r, err := client.Exec(ts3.ClientList())
if err != nil {
return nil, err
}
for _, param := range r.Params {
if clientType, ok := param["client_type"]; ok && clientType == "0" {
cli, err := NewClientFromMap(param)
if err != nil {
return nil, err
}
res = append(res, *cli)
}
}
return
}
func getChannelInfo(client *ts3.Client, cid int) (map[string]string, error) {
// Getting information of the channel.
cmd := ts3.Command{
Command: "channelinfo",
Params: map[string][]string{
"cid": []string{strconv.Itoa(cid)},
},
}
r, err := client.Exec(cmd)
if err != nil {
return nil, err
}
return r.Params[0], nil
}
func loadClients(fileName string) ([]Client, error) {
// Loading clients from the file.
b, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
var clients []Client
err = json.Unmarshal(b, &clients)
if err != nil {
return nil, err
}
return clients, nil
}
func storeClients(clients []Client, fileName string) error {
// Saving clients into the file.
b, err := json.Marshal(clients)
if err != nil {
return err
}
err = ioutil.WriteFile(fileName, b, 0644)
if err != nil {
return err
}
return nil
}