-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathsv_demo_qtv.c
1425 lines (1158 loc) · 32 KB
/
sv_demo_qtv.c
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the included (GNU.txt) GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
$Id: sv_demo_qtv.c 774 2008-03-10 22:12:59Z qqshka $
*/
// sv_demo_qtv.c - misc QTV's code
#include "qwsvdef.h"
cvar_t qtv_streamport = {"qtv_streamport", "0"};
cvar_t qtv_maxstreams = {"qtv_maxstreams", "1"};
cvar_t qtv_password = {"qtv_password", ""};
cvar_t qtv_pendingtimeout = {"qtv_pendingtimeout", "5"}; // 5 seconds must be enough
cvar_t qtv_streamtimeout = {"qtv_streamtimeout", "45"}; // 45 seconds
static mvddest_t *SV_InitStream (int socket1, netadr_t na, char *userinfo)
{
static int lastdest = 0;
int count;
mvddest_t *dst;
char name[sizeof(dst->qtvname)];
// extract name
strlcpy(name, Info_ValueForKey(userinfo, "name"), sizeof(name));
count = 0;
for (dst = demo.dest; dst; dst = dst->nextdest)
{
if (dst->desttype == DEST_STREAM)
{
if (name[0] && !strcasecmp(name, dst->qtvname))
return NULL; // duplicate name, well empty names may still duplicates...
count++;
}
}
if (count >= (int)qtv_maxstreams.value)
return NULL; //sorry
dst = (mvddest_t *) Q_malloc (sizeof(mvddest_t));
dst->desttype = DEST_STREAM;
dst->socket = socket1;
dst->maxcachesize = 65536; //is this too small?
dst->cache = (char *) Q_malloc(dst->maxcachesize);
dst->io_time = Sys_DoubleTime();
dst->id = ++lastdest;
dst->na = na;
strlcpy(dst->qtvname, name, sizeof(dst->qtvname));
if (dst->qtvname[0])
Con_Printf ("Connected to QTV(%s)\n", dst->qtvname);
else
Con_Printf ("Connected to QTV\n");
return dst;
}
static void SV_MVD_InitPendingStream (int socket1, netadr_t na)
{
mvdpendingdest_t *dst;
unsigned int i;
dst = (mvdpendingdest_t*) Q_malloc(sizeof(mvdpendingdest_t));
dst->socket = socket1;
dst->io_time = Sys_DoubleTime();
dst->na = na;
strlcpy(dst->challenge, NET_AdrToString(dst->na), sizeof(dst->challenge));
for (i = strlen(dst->challenge); i < sizeof(dst->challenge)-1; i++)
dst->challenge[i] = rand()%(127-33) + 33; //generate a random challenge
dst->nextdest = demo.pendingdest;
demo.pendingdest = dst;
}
static int MVD_StreamStartListening (int port)
{
int sock;
struct sockaddr_in address;
#ifdef SOCKET_CLOSE_TIME
struct linger lingeropt;
#endif
// int fromlen;
unsigned long nonblocking = true;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons((short)port);
if ((sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
Con_Printf ("MVD_StreamStartListening: socket: (%i): %s\n", qerrno, strerror(qerrno));
return INVALID_SOCKET;
}
#ifdef SOCKET_CLOSE_TIME
// hard close: in case of closesocket(), socket will be closen after SOCKET_CLOSE_TIME or earlier
memset(&lingeropt, 0, sizeof(lingeropt));
lingeropt.l_onoff = 1;
lingeropt.l_linger = SOCKET_CLOSE_TIME;
if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&lingeropt, sizeof(lingeropt)) == -1)
{
Con_Printf ("MVD_StreamStartListening: setsockopt SO_LINGER: (%i): %s\n", qerrno, strerror (qerrno));
closesocket(sock);
return INVALID_SOCKET;
}
#endif
if (ioctlsocket(sock, FIONBIO, &nonblocking) == -1)
{
Con_Printf ("MVD_StreamStartListening: ioctl FIONBIO: (%i): %s\n", qerrno, strerror(qerrno));
closesocket(sock);
return INVALID_SOCKET;
}
if(bind(sock, (struct sockaddr *)&address, sizeof(address)) == -1)
{
Con_Printf ("MVD_StreamStartListening: bind: (%i): %s\n", qerrno, strerror(qerrno));
closesocket(sock);
return INVALID_SOCKET;
}
if(listen(sock, 2) == -1)
{
Con_Printf ("MVD_StreamStartListening: listen: (%i): %s\n", qerrno, strerror(qerrno));
closesocket(sock);
return INVALID_SOCKET;
}
if (!TCP_Set_KEEPALIVE(sock))
{
Con_Printf ("MVD_StreamStartListening: TCP_Set_KEEPALIVE: failed\n");
closesocket(sock);
return INVALID_SOCKET;
}
return sock;
}
void SV_MVDCloseStreams(void)
{
mvddest_t *d;
mvdpendingdest_t *p;
for (d = demo.dest; d; d = d->nextdest)
if (!d->error && d->desttype == DEST_STREAM)
d->error = true; // mark demo stream dest to close later
for (p = demo.pendingdest; p; p = p->nextdest)
if (!p->error)
p->error = true; // mark pending dest to close later
}
static int listensocket = INVALID_SOCKET;
static int listenport = 0;
static double warned_time = 0;
static void SV_CheckQTVPort(void)
{
qbool changed;
int streamport = bound(0, (int)qtv_streamport.value, 64000); // so user can't specifie something stupid
// if we have non zero stream port, but fail to open listen socket, repeat open listen socket after some time
changed = ( streamport != listenport || (streamport && listensocket == INVALID_SOCKET && warned_time + 10 < Sys_DoubleTime()) );
// port not changed
if (!changed)
return;
SV_MVDCloseStreams(); // also close ative connects if any, this will help actually close listen socket, so later we can bind to port again
warned_time = Sys_DoubleTime(); // so we repeat warning time to time
if (listensocket != INVALID_SOCKET)
{
Con_Printf("Closing TCP port %d for QTV\n", listenport);
closesocket(listensocket); // so we close socket
listensocket = INVALID_SOCKET; // and mark as closed
}
// port was changed, lets remember
listenport = streamport;
if (!listenport)
return; // they just wanna turn it off
if ((listensocket = MVD_StreamStartListening(listenport)) == INVALID_SOCKET)
Con_Printf("WARNING: Cannot open TCP port %d for QTV\n", listenport);
else
Con_Printf("Opening TCP port %d for QTV\n", listenport);
}
void SV_MVDStream_Poll (void)
{
int client;
netadr_t na;
struct sockaddr_storage addr;
socklen_t addrlen;
#ifdef SOCKET_CLOSE_TIME
struct linger lingeropt;
#endif
int count;
mvddest_t *dest;
unsigned long _true = true;
if (sv.state != ss_active)
return;
SV_CheckQTVPort(); // open/close/switch qtv port
if (listensocket == INVALID_SOCKET) // we can't accept connection from QTV
{
SV_MVDCloseStreams(); // also close ative connects if any, this will help actually close listen socket, so later we can bind to port again
return;
}
addrlen = sizeof(addr);
client = accept (listensocket, (struct sockaddr *)&addr, &addrlen);
if (client == INVALID_SOCKET)
return;
#ifdef SOCKET_CLOSE_TIME
// hard close: in case of closesocket(), socket will be closen after SOCKET_CLOSE_TIME or earlier
memset(&lingeropt, 0, sizeof(lingeropt));
lingeropt.l_onoff = 1;
lingeropt.l_linger = SOCKET_CLOSE_TIME;
if (setsockopt(client, SOL_SOCKET, SO_LINGER, (void*)&lingeropt, sizeof(lingeropt)) == -1)
{
Con_Printf ("SV_MVDStream_Poll: setsockopt SO_LINGER: (%i): %s\n", qerrno, strerror (qerrno));
closesocket(client);
return;
}
#endif
if (ioctlsocket (client, FIONBIO, &_true) == SOCKET_ERROR) {
Con_Printf ("SV_MVDStream_Poll: ioctl FIONBIO: (%i): %s\n", qerrno, strerror (qerrno));
closesocket(client);
return;
}
if (!TCP_Set_KEEPALIVE(client))
{
Con_Printf ("SV_MVDStream_Poll: TCP_Set_KEEPALIVE: failed\n");
closesocket(client);
return;
}
if ((int)qtv_maxstreams.value > 0)
{
count = 0;
for (dest = demo.dest; dest; dest = dest->nextdest)
{
if (dest->desttype == DEST_STREAM)
{
count++;
}
}
if (count >= (int)qtv_maxstreams.value)
{ //sorry
char *goawaymessage = "QTVSV 1\nERROR: This server enforces a limit on the number of proxies connected at any one time. Please try again later\n\n";
send(client, goawaymessage, strlen(goawaymessage), 0);
closesocket(client);
return;
}
}
SockadrToNetadr(&addr, &na);
Con_Printf("MVD streaming client connected from %s\n", NET_AdrToString(na));
SV_MVD_InitPendingStream(client, na);
}
void SV_MVD_RunPendingConnections (void)
{
unsigned short ushort_result;
char *e;
int len;
mvdpendingdest_t *p;
mvdpendingdest_t *np;
char userinfo[1024] = {0};
if (!demo.pendingdest)
return;
for (p = demo.pendingdest; p; p = p->nextdest)
if (p->io_time + qtv_pendingtimeout.value <= Sys_DoubleTime())
{
Con_Printf("Pending dest timeout\n");
p->error = true;
}
while (demo.pendingdest && demo.pendingdest->error)
{
np = demo.pendingdest->nextdest;
if (demo.pendingdest->socket != -1)
closesocket(demo.pendingdest->socket);
Q_free(demo.pendingdest);
demo.pendingdest = np;
}
for (p = demo.pendingdest; p && p->nextdest; p = p->nextdest)
{
if (p->nextdest->error)
{
np = p->nextdest->nextdest;
if (p->nextdest->socket != -1)
closesocket(p->nextdest->socket);
Q_free(p->nextdest);
p->nextdest = np;
}
}
for (p = demo.pendingdest; p; p = p->nextdest)
{
if (p->outsize && !p->error)
{
len = send(p->socket, p->outbuffer, p->outsize, 0);
if (len == 0) //client died
{
// p->error = true;
// man says: The calls return the number of characters sent, or -1 if an error occurred.
// so 0 is legal or what?
}
else if (len > 0) //we put some data through
{
p->io_time = Sys_DoubleTime(); // update IO activity
//move up the buffer
p->outsize -= len;
memmove(p->outbuffer, p->outbuffer+len, p->outsize );
}
else
{ //error of some kind. would block or something
if (qerrno != EWOULDBLOCK && qerrno != EAGAIN)
p->error = true;
}
}
if (!p->error)
{
len = recv(p->socket, p->inbuffer + p->insize, sizeof(p->inbuffer) - p->insize - 1, 0);
if (len > 0)
{ //fixme: cope with extra \rs
char *end;
p->io_time = Sys_DoubleTime(); // update IO activity
p->insize += len;
p->inbuffer[p->insize] = 0;
for (end = p->inbuffer; ; end++)
{
if (*end == '\0')
{
end = NULL;
break; //not enough data
}
if (end[0] == '\n')
{
if (end[1] == '\n')
{
end[1] = '\0';
break;
}
}
}
if (end)
{ //we found the end of the header
char *start, *lineend;
int versiontouse = 0;
int raw = 0;
char password[256] = "";
typedef enum {
QTVAM_NONE,
QTVAM_PLAIN,
QTVAM_CCITT,
QTVAM_MD4,
} authmethod_t;
authmethod_t authmethod = QTVAM_NONE;
start = p->inbuffer;
lineend = strchr(start, '\n');
if (!lineend)
{
// char *e;
// e = "This is a QTV server.";
// send(p->socket, e, strlen(e), 0);
p->error = true;
continue;
}
*lineend = '\0';
COM_ParseToken(start, NULL);
start = lineend+1;
if (strcmp(com_token, "QTV"))
{ //it's an error if it's not qtv.
p->error = true;
lineend = strchr(start, '\n');
continue;
}
for(;;)
{
lineend = strchr(start, '\n');
if (!lineend)
break;
*lineend = '\0';
start = COM_ParseToken(start, NULL);
if (*start == ':')
{
//VERSION: a list of the different qtv protocols supported. Multiple versions can be specified. The first is assumed to be the prefered version.
//RAW: if non-zero, send only a raw mvd with no additional markup anywhere (for telnet use). Doesn't work with challenge-based auth, so will only be accepted when proxy passwords are not required.
//AUTH: specifies an auth method, the exact specs varies based on the method
// PLAIN: the password is sent as a PASSWORD line
// MD4: the server responds with an "AUTH: MD4\n" line as well as a "CHALLENGE: somerandomchallengestring\n" line, the client sends a new 'initial' request with CHALLENGE: MD4\nRESPONSE: hexbasedmd4checksumhere\n"
// CCITT: same as md4, but using the CRC stuff common to all quake engines.
// if the supported/allowed auth methods don't match, the connection is silently dropped.
//SOURCE: which stream to play from, DEFAULT is special. Without qualifiers, it's assumed to be a tcp address.
//COMPRESSION: Suggests a compression method (multiple are allowed). You'll get a COMPRESSION response, and compression will begin with the binary data.
start = start+1;
Con_Printf("qtv, got (%s) (%s)\n", com_token, start);
if (!strcmp(com_token, "VERSION"))
{
start = COM_ParseToken(start, NULL);
if (atoi(com_token) == 1)
versiontouse = 1;
}
else if (!strcmp(com_token, "RAW"))
{
start = COM_ParseToken(start, NULL);
raw = atoi(com_token);
}
else if (!strcmp(com_token, "PASSWORD"))
{
start = COM_ParseToken(start, NULL);
strlcpy(password, com_token, sizeof(password));
}
else if (!strcmp(com_token, "AUTH"))
{
authmethod_t thisauth;
start = COM_ParseToken(start, NULL);
if (!strcmp(com_token, "NONE"))
thisauth = QTVAM_PLAIN;
else if (!strcmp(com_token, "PLAIN"))
thisauth = QTVAM_PLAIN;
else if (!strcmp(com_token, "CCITT"))
thisauth = QTVAM_CCITT;
else if (!strcmp(com_token, "MD4"))
thisauth = QTVAM_MD4;
else
{
thisauth = QTVAM_NONE;
Con_DPrintf("qtv: received unrecognised auth method (%s)\n", com_token);
}
if (authmethod < thisauth)
authmethod = thisauth;
}
else if (!strcmp(com_token, "SOURCE"))
{
//servers don't support source, and ignore it.
//source is only useful for qtv proxy servers.
}
else if (!strcmp(com_token, "COMPRESSION"))
{
//compression not supported yet
}
else if (!strcmp(com_token, "USERINFO"))
{
start = COM_ParseToken(start, NULL);
strlcpy(userinfo, com_token, sizeof(userinfo));
}
else
{
//not recognised.
}
}
start = lineend+1;
}
len = (end - p->inbuffer)+2;
p->insize -= len;
memmove(p->inbuffer, p->inbuffer + len, p->insize);
p->inbuffer[p->insize] = 0;
e = NULL;
if (p->hasauthed)
{
}
else if (!*qtv_password.string)
p->hasauthed = true; //no password, no need to auth.
else if (*password)
{
switch (authmethod)
{
case QTVAM_NONE:
e = ("QTVSV 1\n"
"PERROR: You need to provide a common auth method.\n\n");
break;
case QTVAM_PLAIN:
p->hasauthed = !strcmp(qtv_password.string, password);
break;
case QTVAM_CCITT:
CRC_Init(&ushort_result);
CRC_AddBlock(&ushort_result, (byte *) p->challenge, strlen(p->challenge));
CRC_AddBlock(&ushort_result, (byte *) qtv_password.string, strlen(qtv_password.string));
p->hasauthed = (ushort_result == Q_atoi(password));
break;
case QTVAM_MD4:
{
char hash[512];
int md4sum[4];
snprintf (hash, sizeof(hash), "%s%s", p->challenge, qtv_password.string);
Com_BlockFullChecksum (hash, strlen(hash), (unsigned char*)md4sum);
snprintf (hash, sizeof(hash), "%X%X%X%X", md4sum[0], md4sum[1], md4sum[2], md4sum[3]);
p->hasauthed = !strcmp(password, hash);
}
break;
default:
e = ("QTVSV 1\n"
"PERROR: FTEQWSV bug detected.\n\n");
break;
}
if (!p->hasauthed && !e)
{
if (raw)
e = "";
else
e = ("QTVSV 1\n"
"PERROR: Bad password.\n\n");
}
}
else
{
//no password, and not automagically authed
switch (authmethod)
{
case QTVAM_NONE:
if (raw)
e = "";
else
e = ("QTVSV 1\n"
"PERROR: You need to provide a common auth method.\n\n");
break;
case QTVAM_PLAIN:
p->hasauthed = !strcmp(qtv_password.string, password);
break;
case QTVAM_CCITT:
e = ("QTVSV 1\n"
"AUTH: CCITT\n"
"CHALLENGE: ");
send(p->socket, e, strlen(e), 0);
send(p->socket, p->challenge, strlen(p->challenge), 0);
e = "\n\n";
send(p->socket, e, strlen(e), 0);
continue;
case QTVAM_MD4:
e = ("QTVSV 1\n"
"AUTH: MD4\n"
"CHALLENGE: ");
send(p->socket, e, strlen(e), 0);
send(p->socket, p->challenge, strlen(p->challenge), 0);
e = "\n\n";
send(p->socket, e, strlen(e), 0);
continue;
default:
e = ("QTVSV 1\n"
"PERROR: FTEQWSV bug detected.\n\n");
break;
}
}
if (e)
{
}
else if (!versiontouse)
{
e = ("QTVSV 1\n"
"PERROR: Incompatable version (valid version is v1)\n\n");
}
else if (raw)
{
if (p->hasauthed == false)
{
e = "";
}
else
{
mvddest_t *tmpdest;
if ((tmpdest = SV_InitStream(p->socket, p->na, userinfo)))
{
if (!SV_MVD_Record(tmpdest, false))
DestClose(tmpdest, false); // can't start record for some reason, close dest then
p->socket = -1; //so it's not cleared wrongly.
}
else
{
// RAW mode, can't sent error, right?
// e = ("QTVSV 1\n"
// "ERROR: Can't init stream, probably server reach a limit on the number of proxies connected at any one time.\n\n");
}
}
p->error = true;
}
else
{
if (p->hasauthed == true)
{
mvddest_t *tmpdest;
if ((tmpdest = SV_InitStream(p->socket, p->na, userinfo)))
{
e = ("QTVSV 1\n"
"BEGIN\n\n");
send(p->socket, e, strlen(e), 0);
e = NULL;
if (!SV_MVD_Record(tmpdest, false))
DestClose(tmpdest, false); // can't start record for some reason, close dest then
p->socket = -1; //so it's not cleared wrongly.
}
else
{
e = ("QTVSV 1\n"
"ERROR: Can't init stream, probably server reach a limit on the number of proxies connected at any one time.\n\n");
}
}
else
{
e = ("QTVSV 1\n"
"PERROR: You need to provide a password.\n\n");
}
p->error = true;
}
if (e)
{
send(p->socket, e, strlen(e), 0);
p->error = true;
}
}
}
else if (len == 0)
p->error = true;
else
{ //error of some kind. would block or something
int err;
err = qerrno;
if (err != EWOULDBLOCK && err != EAGAIN)
p->error = true;
}
}
}
}
//============================================================
//
// QTV user input
//
//============================================================
// { qtv commands
// say [say_game] text
// say_team [say_game] text
// say_game text
void QTVcmd_Say_f(mvddest_t *d)
{
qbool gameStarted;
client_t *client;
int j;
char *p;
char text[1024], text2[1024], *cmd;
if (Cmd_Argc () < 2)
return;
if (!strcasecmp(Info_ValueForKey(svs.info, "status"), "Countdown"))
gameStarted = false; // if status is "Countdown" then game is not started yet
else
gameStarted = GameStarted();
p = Cmd_Args();
if (*p == '"' && (j = strlen(p)) > 2)
{
p[j-1] = 0;
p++;
}
cmd = Cmd_Argv(0);
// strip leading say_game but not in case of "cmd say_game say_game"
if (strcmp(cmd, "say_game") && !strncasecmp(p, "say_game ", sizeof("say_game ") - 1))
{
p += sizeof("say_game ") - 1;
}
if (!strcmp(cmd, "say_game"))
cmd = "say"; // this makes qtv_%s_game looks right
if (!strcmp(cmd, "say_team"))
gameStarted = true; // send to specs only
if (gameStarted)
cmd = "say_team"; // we can accept only this command, since we will send to specs only
// for clients and demo
snprintf(text, sizeof(text), "#0:qtv_%s_game:#%d:%s: %s\n", cmd, d->id, d->qtvname, p);
// for server console and logs
snprintf(text2, sizeof(text2), "qtv: #0:qtv_%s_game:#%d:%s: %s\n", cmd, d->id, d->qtvname, p);
for (j = 0, client = svs.clients; j < MAX_CLIENTS; j++, client++)
{
if (client->state != cs_spawned)
continue;
if (gameStarted && !client->spectator)
continue; // game started, don't send QTV chat to players, specs still get QTV chat
SV_ClientPrintf2(client, PRINT_CHAT, "%s", text);
}
if (sv.mvdrecording)
{
if (MVDWrite_Begin (dem_all, 0, strlen(text)+3))
{
MVD_MSG_WriteByte (svc_print);
MVD_MSG_WriteByte (PRINT_CHAT);
MVD_MSG_WriteString (text);
}
}
Sys_Printf("%s", text2);
SV_Write_Log(CONSOLE_LOG, 1, text2);
}
// }
// {
static qtvuser_t *QTVsv_UserById(mvddest_t *d, int id)
{
qtvuser_t *current;
for (current = d->qtvuserlist; current; current = current->next)
if (current->id == id)
return current;
return NULL;
}
static void QTVsv_SetUser(qtvuser_t *to, qtvuser_t *from)
{
*to = *from;
}
static int QTVsv_UsersCount(mvddest_t *d)
{
qtvuser_t *current;
int c = 0;
for (current = d->qtvuserlist; current; current = current->next)
c++;
return c;
}
// allocate data and set fields, perform linkage to qtvuserlist
// Well, instead of QTVsv_NewUser(int id, char *name, ...) I pass params with single qtvuser_t *user struct, well its OK for current struct.
static qtvuser_t *QTVsv_NewUser(mvddest_t *d, qtvuser_t *user)
{
// check, may be user alredy exist, so reuse it
qtvuser_t *newuser = QTVsv_UserById(d, user->id);
if (!newuser)
{
// user does't exist, alloc data
if (QTVsv_UsersCount(d) > 2048)
{
Con_Printf("QTVsv_NewUser: too much users, haxors? Dropping dest\n");
d->error = true; // drop dest later
return NULL;
}
newuser = Q_malloc(sizeof(*newuser)); // alloc
QTVsv_SetUser(newuser, user);
// perform linkage
newuser->next = d->qtvuserlist;
d->qtvuserlist = newuser;
}
else
{
// we do not need linkage, just save current
qtvuser_t *oldnext = newuser->next; // we need save this before assign all fields
QTVsv_SetUser(newuser, user);
newuser->next = oldnext;
}
return newuser;
}
// free data, perform unlink if requested
static void QTVsv_FreeUser(mvddest_t *d, qtvuser_t *user, qbool unlink)
{
if (!user)
return;
if (unlink)
{
qtvuser_t *next, *prev, *current;
prev = NULL;
current = d->qtvuserlist;
for ( ; current; )
{
next = current->next;
if (user == current)
{
if (prev)
prev->next = next;
else
d->qtvuserlist = next;
break;
}
prev = current;
current = next;
}
}
Q_free(user);
}
// free whole qtvuserlist
void QTVsv_FreeUserList(mvddest_t *d)
{
qtvuser_t *next, *current;
current = d->qtvuserlist;
for ( ; current; current = next)
{
next = current->next;
QTVsv_FreeUser(d, current, false);
}
d->qtvuserlist = NULL;
}
#define QTV_EVENT_PREFIX "QTV: "
// user join qtv
void QTVsv_JoinEvent(mvddest_t *d, qtvuser_t *user)
{
// make it optional message
// if (!qtv_event_join.string[0])
// return;
// do not show "user joined" at moment of connection to QTV, it mostly QTV just spammed userlist to us.
// if (cls.state <= ca_demostart)
// return;
if (QTVsv_UserById(d, user->id))
{
// we alredy have this user, do not double trigger
return;
}
Con_Printf("%s%s: %s%s\n", QTV_EVENT_PREFIX, d->qtvname, user->name, /*qtv_event_join.string*/ " join");
}
// user leaved/left qtv
void QTVsv_LeaveEvent(mvddest_t *d, qtvuser_t *user)
{
qtvuser_t *olduser;
// make it optional message
// if (!qtv_event_leave.string[0])
// return;
if (!(olduser = QTVsv_UserById(d, user->id)))
{
// we do not have this user
return;
}
Con_Printf("%s%s: %s%s\n", QTV_EVENT_PREFIX, d->qtvname, olduser->name, /*qtv_event_leave.string*/ " left");
}
// user changed name on qtv
void QTVsv_ChangeEvent(mvddest_t *d, qtvuser_t *user)
{
qtvuser_t *olduser;
// well, too spammy, make it as option
// if (!qtv_event_changename.string[0])
// return;
if (!(olduser = QTVsv_UserById(d, user->id)))
{
// we do not have this user yet
Con_DPrintf("qtv: change event without olduser\n");
return;
}
Con_Printf("%s%s: %s%s%s\n", QTV_EVENT_PREFIX, d->qtvname, olduser->name, /*qtv_event_changename.string*/ " changed name to ", user->name);
}
static void QTVcmd_QtvUserList_f(mvddest_t *d)
{
qtvuser_t tmpuser;
qtvuserlist_t action;
int cnt = 1;
memset(&tmpuser, 0, sizeof(tmpuser));
// action id [\"name\"]
// Cmd_TokenizeString( s );
action = atoi( Cmd_Argv( cnt++ ) );
tmpuser.id = atoi( Cmd_Argv( cnt++ ) );
strlcpy(tmpuser.name, Cmd_Argv( cnt++ ), sizeof(tmpuser.name)); // name is optional in some cases
switch ( action )
{
case QUL_ADD:
QTVsv_JoinEvent(d, &tmpuser);
QTVsv_NewUser(d, &tmpuser);
break;
case QUL_CHANGE:
QTVsv_ChangeEvent(d, &tmpuser);
QTVsv_NewUser(d, &tmpuser);
break;
case QUL_DEL:
QTVsv_LeaveEvent(d, &tmpuser);
QTVsv_FreeUser(d, QTVsv_UserById(d, tmpuser.id), true);
break;
default:
Con_Printf("Parse_QtvUserList: unknown action %d\n", action);