This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
forked from adocilesloth/TS3StudioPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlayComs.cpp
628 lines (588 loc) · 13.3 KB
/
overlayComs.cpp
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*******************************************
A Docile Sloth 2016 ([email protected])
*******************************************/
extern "C"
{
#include "ts3plugin.h"
//#include "isStreamRecord.h"
#include "obs-frontend-api\obs-frontend-api.h"
};
#include <sstream>
#include "overlayComs.hpp"
#include "slothSock.hpp"
#include "utility.hpp"
#include "originalFeatures.hpp"
using namespace std;
void runOverlay(atomic<bool> &close)
{
int iname;
bool bname;
bool bright;
bool btalker;
int italker;
vector<wstring> vname;
vector<int> vtime;
bool bsaid = false;
bool allOK = false;
slothSock sock;
//char* adrs;
bool iResult;
char reci1[253]; //181
char reci2[64];
vector<char> reci3;
int reci3Size;
char reci4[256];
//char reci5[256];
char reciA[64];
char* whoami = "whoami\n";
char* schandlerlist = "serverconnectionhandlerlist\n";
//string location
string identstart;
wstring widentstart;
string identend;
wstring widentend;
wstring talkstart = L"client_flag_talking=";
size_t startpos;
size_t endpos;
string tempstr;
wstring wtempstr;
//useful things
string cid;
string list;
wstring wlist;
const char *cllist;
vector<wstring> name;
int namesize;
vector<bool> talk;
bool discon = true;
bool noserv = true;
bool add = true;
bool remove = false;
bool bCom = false;
wstring userName;
bool bMnD = false;
vector<string> schandlerid;
psleep(1000);
char* apikey = getAPIKey();
//blog(LOG_WARNING, apikey);
tempstr = "auth apikey=";
tempstr.append(apikey);
tempstr.append("\n");
const char* auth = tempstr.c_str();
wstringstream overlay;
string overlayString;
blog(LOG_INFO, "TS3: Overlay: Started");
#if _WIN32
while(!close)
#else
while(close)
#endif
{
//connect
//Only ned to connect once as long as everything is okay.
blog(LOG_WARNING, "TS3: Starting Connection");
char* adrs = getAddress();
if(!sock.connectToHost(25639, adrs))
{
if(discon)
{
blog(LOG_WARNING, "TS3: Connection Failure: Check TS3 is running and ClientQuery Plugin is enabled");
discon = false;
//force Communicate, MuteandDeafen and ChannelSwitch
bCom = false;
bMnD = false;
add = true;
}
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
//delete adrs;
blog(LOG_INFO, "TS3: Connected");
discon = true;
iResult = sock.recvAll(reci1, 253, 0); //get TS3 Client... 181
if(!iResult)
{
blog(LOG_WARNING, "TS3: Overlay: First Recieve Failure");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
//authorise
iResult = sock.sendAll(auth, (int)strlen(auth), 0);
if(!iResult)
{
blog(LOG_WARNING, "TS3: Overlay: auth Send Failure");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
iResult = sock.recvAll(reciA, 64, 0, "msg=");
if(!iResult)
{
blog(LOG_WARNING, "TS3: Overlay: auth Recieve Failure");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
tempstr = reciA;
identstart = "id=";
startpos = tempstr.find(identstart);
if(startpos == -1)
{
if(noserv) //if initial disconnection
{
blog(LOG_WARNING, "TS3: Overlay: Invalid API Key Return");
noserv = false;
bCom = false;
bMnD = false;
add = false;
}
memset(reciA, 0, 64);
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
else if(tempstr.substr(startpos, 7) == "id=1538")
{
//blog(LOG_WARNING, "id=1538");
if(noserv) //if initial disconnection
{
blog(LOG_WARNING, "TS3: Overlay: Invalid API Key");
noserv = false;
bCom = false;
bMnD = false;
add = false;
}
memset(reciA, 0, 64);
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
allOK = true;
while(allOK)
{
if(obs_frontend_streaming_active() || obs_frontend_recording_active())
{
if(add)
{
iResult = sock.sendAll(schandlerlist, (int)strlen(schandlerlist), 0); //send serverconnectionhandlerlist
if(!iResult)
{
blog(LOG_WARNING, "TS3: Overlay: schandlerlist Send Failure");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
iResult = sock.recvAll(reci4, 256, 0, "msg="); //get serverconnectionhandlerlist
if (!iResult)
{
blog(LOG_WARNING, "TS3: Overlay: schandlerlist Recieve Failure");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
tempstr = reci4;
memset(reci4, 0, 256);
while(tempstr.size() > 12)
{
identstart = "sch";
startpos = tempstr.find(identstart);
if(startpos >= 0 && startpos < tempstr.size())
{
schandlerid.push_back(tempstr.substr(startpos, 13));
}
else
{
break;
}
tempstr = tempstr.substr(startpos + 13, tempstr.size() - (startpos + 13));
}
if(!bCom)
{
userName = Communicate(1, sock, schandlerid);
if(userName != L"poop")
{
wReplaceAll(userName, L"\\s", L" ");
bCom = true;
}
else
{
bCom = false;
}
}
if(!bMnD)
{
bMnD = MuteandDeafen(1, sock, schandlerid);
}
if(bCom && bMnD)
{
bCom = false;
bMnD = false;
add = false;
remove = true;
}
schandlerid.clear();
}
}
else
{
if(remove)
{
iResult = sock.sendAll(schandlerlist, (int)strlen(schandlerlist), 0); //send serverconnectionhandlerlist
if(!iResult)
{
blog(LOG_WARNING, "TS3: Overlay: schandlerlist Send Failure");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
iResult = sock.recvAll(reci4, 256, 0, "msg="); //get serverconnectionhandlerlist
if (!iResult)
{
blog(LOG_WARNING, "TS3: Overlay: schandlerlist Recieve Failure");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
tempstr = reci4;
memset(reci4, 0, 256);
while(tempstr.size() > 12)
{
identstart = "sch";
startpos = tempstr.find(identstart);
if(startpos > 0 && startpos < tempstr.size())
{
schandlerid.push_back(tempstr.substr(startpos, 13));
}
else
{
break;
}
tempstr = tempstr.substr(startpos + 13, tempstr.size() - (startpos + 13));
}
if(!bCom)
{
userName = Communicate(0, sock, schandlerid);
if(userName != L"poop")
{
wReplaceAll(userName, L"\\s", L" ");
bCom = true;
}
else
{
bCom = false;
}
}
if(!bMnD)
{
bMnD = MuteandDeafen(0, sock, schandlerid);
}
if(bCom && bMnD)
{
bCom = false;
bMnD = false;
add = true;
remove = false;
}
schandlerid.clear();
}
}
//get cid
iResult = sock.sendAll(whoami, (int)strlen(whoami), 0); //send whoami
if (!iResult)
{
blog(LOG_WARNING, "TS3: Overlay: whoami Send Failure");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
//psleep(50);
iResult = sock.recvAll(reci2, 64 ,0 ,"msg="); //get whoami
if (!iResult)
{
blog(LOG_WARNING, "TS3: Overlay: whoami Recieve Failure");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
tempstr = reci2;
identstart = "msg=";
startpos = tempstr.find(identstart);
if(tempstr.substr(startpos, 5) == "msg=n")
{
if(noserv) //if initial disconnection
{
blog(LOG_WARNING, "TS3: Overlay: Not Connected to TS3 Server");
noserv = false;
//force Communicate, MuteandDeafen and ChannelSwitch
bCom = false;
bMnD = false;
add = false;
}
memset(reci2, 0, 64);
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
tempstr = " ";
if(!noserv)
{
blog(LOG_INFO, "TS3: Overlay: Now Connected to TS3 Server");
add = true;
noserv = true;
}
identstart = "cid=";
identend = "\n";
cid = reci2;
startpos = cid.find(identstart);
//blog(LOG_WARNING, reci2); //--------------------------------------------------------------------------
//break;
if(startpos == -1)
{
blog(LOG_WARNING, "TS3: Overlay: cid startpos == -1");
overlay.str(L" "); //empty client list
allOK = false;
//clear buffer;
///iResult = sock.recvAll(reci5, 256, 0);
goto skip;
}
endpos = cid.find(identend);
if(endpos < 0)
{
blog(LOG_WARNING, "TS3: Overlay: cid endpos < 0");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
cid = cid.substr(startpos, endpos - startpos);
//set up channelclientlist
tempstr = "channelclientlist ";
tempstr.append(cid);
tempstr.append(" -voice\n");
cllist = tempstr.c_str();
//recieve setup
iname = getNumberOfNames();
reci3Size = iname * 360;
reci3.resize(reci3Size);
//get channelclientlist
iResult = sock.sendAll(cllist, (int)strlen(cllist), 0); //send channelcli...
if (!iResult)
{
blog(LOG_WARNING, "TS3: Overlay: channelclientlist Send Failure");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
iResult = sock.recvAll(&reci3[0], reci3.size(), 0, "msg="); //recieve channelcli...
if (!iResult)
{
blog(LOG_WARNING, "TS3: Overlay: channelclientlist Recieve Failure");
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
widentstart = L"name=";
widentend = L"client_type";
list.assign(reci3.begin(), reci3.end());
//blog(LOG_WARNING, list.c_str()); //----------------------------------------------------------------------
wlist = s2ws(list);
//generate client list
for(int i = 0; i < iname; i++)
{
startpos = wlist.find(widentstart);
if(startpos == -1)
{
break;
}
endpos = wlist.find(widentend);
if(endpos < 0)
{
break;
}
wtempstr = wlist.substr(startpos + 5, endpos-startpos-6);
name.push_back(wtempstr);
startpos = wlist.find(talkstart);
if(startpos == -1)
{
talk.push_back(false);
break;
}
else if(wlist.substr(startpos + 20, 1) == L"0")
{
talk.push_back(false);
}
else if(wlist.substr(startpos + 20, 1) == L"1")
{
talk.push_back(true);
}
wlist = wlist.substr(startpos+25);
}
//only show talker time keeping
btalker = getOnlyShowTalker();
if(btalker)
{
italker = getHideNameAfter()/100;
if(static_cast<int>(vtime.size()) > 0)
{
for(int i = static_cast<int>(vtime.size()) - 1; i > -1; i--)
{
if(vtime[i] > italker)
{
vtime.erase(vtime.begin() + i);
vname.erase(vname.begin() + i);
}
else
{
vtime[i]++;
}
}
}
}
//print client list
bname = getHideSelf();
bright = getRightOfSymbol();
namesize = name.size();
if(namesize < 1)
{
overlay.str(L" "); //empty client list
allOK = false;
goto skip;
}
else if(namesize < iname)
{
iname = namesize;
}
overlay.str(L" "); //empty client list
//normal
if(!btalker)
{
for(int i = 0; i < iname; i++)
{
if(name[i].empty())
{
break;
}
wReplaceAll(name[i], L"\\s", L" ");
if(bname && name[i] == userName)
{
//do nothing
}
else
{
wstring wsTmp(name[i].begin(), name[i].end());
if(!bright)
{
if(talk[i])
{
overlay << L"\u25CF" << wsTmp << L"\n";
}
else
{
overlay << L"\u25CB" << wsTmp << L"\n";
}
}
else
{
if(talk[i])
{
overlay << wsTmp << L"\u25CF" << L"\n";
}
else
{
overlay << wsTmp << L"\u25CB" << L"\n";
}
}
}
}//end for(int i...
}
//Only show talkers
else
{
for(int i = 0; i < iname; i++)
{
if(name[i].empty())
{
break;
}
wReplaceAll(name[i], L"\\s", L" ");
if(bname && name[i] == userName)
{
//do nothing
}
else
{
wstring wsTmp(name[i].begin(), name[i].end());
if(talk[i])
{
bsaid = false;
if(static_cast<int>(vname.size()) > 0)
{
for(int j = 0; j < static_cast<int>(vname.size()); j++)
{
if(wsTmp == vname[j])
{
vtime[j] = 0;
bsaid = true;
break;
}
}
}
if(!bsaid)
{
vname.push_back(wsTmp);
vtime.push_back(0);
}
else
{
bsaid = false;
}
}//end if(talk[i])
}
}//end for(int i...
//Print
if(static_cast<int>(vname.size()) > 0)
{
for(int j = 0; j < static_cast<int>(vname.size()); j++)
{
overlay << vname[j] << L"\n";
}
}
}//end Only Show talkers
//reset variables
memset(reci1, 0, 253); //181
memset(reci2, 0, 64);
memset(reciA, 0, 64);
reci3.clear();
cid.clear();
list.clear();
name.clear();
talk.clear();
identstart.clear();
identend.clear();
startpos = 0;
endpos = 0;
tempstr.clear();
skip:
if(overlay.str() == L" ")
{
overlayString = " ";
}
else
{
overlayString = ws2s(overlay.str());
}
sendOverlay(overlayString.c_str());
psleep(100);
}
blog(LOG_INFO, "TS3: Closing Connection");
sock.closeConnection();
psleep(1000);
}//end while(!close)
//shutdown:
sock.closeConnection();
delete whoami;
delete schandlerlist;
blog(LOG_INFO, "TS3: Overlay: Stopped");
return;
}