forked from Unity-Technologies/qstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.c
1313 lines (1131 loc) · 26.5 KB
/
template.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
/*
* qstat
* by Steve Jankowski
* http://www.qstat.org
*
* Thanks to Per Hammer for the OS/2 patches ([email protected])
* Thanks to John Ross Hunt for the OpenVMS Alpha patches ([email protected])
* Thanks to Scott MacFiggen for the quicksort code ([email protected])
*
* Inspired by QuakePing by Len Norton
*
* Copyright 1996,1997,1998,1999,2000,2001,2002 by Steve Jankowski
*
* Licensed under the Artistic License, see LICENSE.txt for license terms
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#if defined(__hpux) || defined(_AIX) || defined(__FreeBSD__) || defined(__MidnightBSD__)
#include <sys/types.h>
#endif
#ifndef _WIN32
#include <netinet/in.h>
#endif
#include "qstat.h"
#include "xform.h"
#ifdef _WIN32
#define strcasecmp stricmp
#define strncasecmp strnicmp
#endif
#ifdef __hpux
#define STATIC static
#else
#define STATIC
#endif
/*
#ifdef __cplusplus
* extern "C" {
#endif
#ifndef _AIX
* extern unsigned int ntohl(unsigned int n);
#endif
#ifdef __cplusplus
* }
#endif
*/
extern int hostname_lookup;
extern int num_servers_total;
extern int num_servers_timed_out;
extern int num_servers_down;
extern int num_players_total;
extern int max_players_total;
extern int xform_html_names;
extern int xform_hex_player_names;
extern FILE *OF; /* output file */
static char *server_template;
static char *rule_template;
static char *header_template;
static char *trailer_template;
static char *player_template;
static void display_server_var(struct qserver *server, int var);
static void display_player_var(struct player *player, int var, struct qserver *server);
static void display_rule_var(struct rule *rule, int var, struct qserver *server);
static void display_generic_var(int var);
static int parse_var(char *varname, int *varlen);
static int read_template(char *filename, char **template_text);
static void display_string(char *str);
static int is_true(struct qserver *server, struct player *player,
struct rule *rule, char *expr);
#define VARIABLE_CHAR '$'
static char *variable_option;
static int if_skip;
static int if_level;
static int if_skip_save;
static int if_level_save;
int html_mode = 0;
int clear_newlines_mode = 0;
int rule_name_spaces = 0;
struct vardef {
char *var;
int varcode;
int options;
};
#define NO_OPTIONS 0
#define OPTIONS_OK 1
#define EXPR 2
struct vardef variable_defs[] =
{
#define V_HOSTNAME 1
{ "HOSTNAME", V_HOSTNAME, NO_OPTIONS },
#define V_SERVERNAME 2
{ "SERVERNAME", V_SERVERNAME, NO_OPTIONS },
#define V_PING 3
{ "PING", V_PING, NO_OPTIONS },
#define V_PLAYERS 4
{ "PLAYERS", V_PLAYERS, NO_OPTIONS | EXPR },
#define V_MAXPLAYERS 5
{ "MAXPLAYERS", V_MAXPLAYERS, NO_OPTIONS },
#define V_MAP 6
{ "MAP", V_MAP, NO_OPTIONS },
#define V_GAME 7
{ "GAME", V_GAME, NO_OPTIONS | EXPR },
#define V_RETRIES 8
{ "RETRIES", V_RETRIES, NO_OPTIONS },
#define V_IPADDR 9
{ "IPADDR", V_IPADDR, NO_OPTIONS },
#define V_PORT 10
{ "PORT", V_PORT, NO_OPTIONS },
#define V_ARG 11
{ "ARG", V_ARG, NO_OPTIONS },
#define V_QSTATURL 12
{ "QSTATURL", V_QSTATURL, NO_OPTIONS },
#define V_QSTATVERSION 13
{ "QSTATVERSION", V_QSTATVERSION, NO_OPTIONS },
#define V_QSTATAUTHOR 14
{ "QSTATAUTHOR", V_QSTATAUTHOR, NO_OPTIONS },
#define V_QSTATAUTHOREMAIL 15
{ "QSTATAUTHOREMAIL", V_QSTATAUTHOREMAIL, NO_OPTIONS },
#define V_TYPE 16
{ "TYPE", V_TYPE, NO_OPTIONS },
#define V_RULE 17
{ "RULE", V_RULE, OPTIONS_OK | EXPR },
#define V_ALLRULES 18
{ "ALLRULES", V_ALLRULES, NO_OPTIONS },
#define V_PLAYERTEMPLATE 19
{ "PLAYERTEMPLATE", V_PLAYERTEMPLATE, NO_OPTIONS },
#define V_PLAYERNAME 20
{ "PLAYERNAME", V_PLAYERNAME, NO_OPTIONS },
#define V_FRAGS 21
{ "FRAGS", V_FRAGS, NO_OPTIONS },
#define V_PLAYERPING 22
{ "PLAYERPING", V_PLAYERPING, NO_OPTIONS },
#define V_CONNECTTIME 23
{ "CONNECTTIME", V_CONNECTTIME, NO_OPTIONS },
#define V_SKIN 24
{ "SKIN", V_SKIN, NO_OPTIONS },
#define V_SHIRTCOLOR 25
{ "SHIRTCOLOR", V_SHIRTCOLOR, NO_OPTIONS },
#define V_PANTSCOLOR 26
{ "PANTSCOLOR", V_PANTSCOLOR, NO_OPTIONS },
#define V_PLAYERIP 27
{ "PLAYERIP", V_PLAYERIP, NO_OPTIONS },
#define V_IF 28
{ "IF", V_IF, OPTIONS_OK },
#define V_ENDIF 29
{ "ENDIF", V_ENDIF, NO_OPTIONS },
#define V_HTML 35
{ "HTML", V_HTML, NO_OPTIONS },
#define V_FLAG 36
{ "FLAG", V_FLAG, OPTIONS_OK | EXPR },
#define V_UP 37
{ "UP", V_UP, NO_OPTIONS | EXPR },
#define V_DOWN 38
{ "DOWN", V_DOWN, NO_OPTIONS | EXPR },
#define V_IFNOT 39
{ "IFNOT", V_IFNOT, OPTIONS_OK },
#define V_TIMEOUT 40
{ "TIMEOUT", V_TIMEOUT, NO_OPTIONS | EXPR },
#define V_NOW 41
{ "NOW", V_NOW, NO_OPTIONS },
#define V_TOTALSERVERS 42
{ "TOTALSERVERS", V_TOTALSERVERS, NO_OPTIONS },
#define V_TOTALUP 43
{ "TOTALUP", V_TOTALUP, NO_OPTIONS },
#define V_TOTALNOTUP 44
{ "TOTALNOTUP", V_TOTALNOTUP, NO_OPTIONS },
#define V_TOTALPLAYERS 45
{ "TOTALPLAYERS", V_TOTALPLAYERS, NO_OPTIONS },
#define V_TOTALMAXPLAYERS 46
{ "TOTALMAXPLAYERS", V_TOTALMAXPLAYERS, NO_OPTIONS },
#define V_TEAMNUM 49
{ "TEAMNUM", V_TEAMNUM, NO_OPTIONS },
#define V_BACKSLASH 50
{ "\\", V_BACKSLASH, NO_OPTIONS },
#define V_HOSTNOTFOUND 51
{ "HOSTNOTFOUND", V_HOSTNOTFOUND, NO_OPTIONS | EXPR },
#define V_MESH 52
{ "MESH", V_MESH, NO_OPTIONS },
#define V_ISEMPTY 56
{ "ISEMPTY", V_ISEMPTY, NO_OPTIONS | EXPR },
#define V_ISFULL 57
{ "ISFULL", V_ISFULL, NO_OPTIONS | EXPR },
#define V_PACKETLOSS 58
{ "PACKETLOSS", V_PACKETLOSS, NO_OPTIONS },
#define V_ISTEAM 59
{ "ISTEAM", V_ISTEAM, NO_OPTIONS | EXPR },
#define V_TEAMNAME 60
{ "TEAMNAME", V_TEAMNAME, NO_OPTIONS },
#define V_DEFAULTTYPE 61
{ "DEFAULTTYPE", V_DEFAULTTYPE, NO_OPTIONS },
#define V_TYPESTRING 62
{ "TYPESTRING", V_TYPESTRING, NO_OPTIONS },
#define V_FACE 63
{ "FACE", V_FACE, NO_OPTIONS },
#define V_SOLDIEROFFORTUNE 64
{ "SOLDIEROFFORTUNE", V_SOLDIEROFFORTUNE, NO_OPTIONS },
#define V_COLORNUMBERS 65
{ "COLORNUMBERS", V_COLORNUMBERS, NO_OPTIONS },
#define V_COLORNAMES 66
{ "COLORNAMES", V_COLORNAMES, NO_OPTIONS },
#define V_COLORRGB 67
{ "COLORRGB", V_COLORRGB, NO_OPTIONS },
#define V_TIMESECONDS 68
{ "TIMESECONDS", V_TIMESECONDS, NO_OPTIONS },
#define V_TIMECLOCK 69
{ "TIMECLOCK", V_TIMECLOCK, NO_OPTIONS },
#define V_TIMESTOPWATCH 70
{ "TIMESTOPWATCH", V_TIMESTOPWATCH, NO_OPTIONS },
#define V_NOWINT 71
{ "NOWINT", V_NOWINT, NO_OPTIONS },
#define V_ISMASTER 72
{ "ISMASTER", V_ISMASTER, NO_OPTIONS | EXPR },
#define V_HTMLPLAYERNAME 73
{ "HTMLPLAYERNAME", V_HTMLPLAYERNAME, NO_OPTIONS },
#define V_ISBOT 74
{ "ISBOT", V_ISBOT, NO_OPTIONS | EXPR },
#define V_ISALIAS 75
{ "ISALIAS", V_ISALIAS, NO_OPTIONS | EXPR },
#define V_TRIBETAG 76
{ "TRIBETAG", V_TRIBETAG, NO_OPTIONS | EXPR },
#define V_CLEARNEWLINES 77
{ "CLEARNEWLINES", V_CLEARNEWLINES, NO_OPTIONS },
#define V_GAMETYPE 78
{ "GAMETYPE", V_GAMETYPE, NO_OPTIONS },
#define V_DEATHS 79
{ "DEATHS", V_DEATHS, NO_OPTIONS | EXPR },
#define V_TYPEPREFIX 80
{ "TYPEPREFIX", V_TYPEPREFIX, NO_OPTIONS },
#define V_RULENAMESPACES 81
{ "RULENAMESPACES", V_RULENAMESPACES, NO_OPTIONS },
#define V_RULETEMPLATE 82
{ "RULETEMPLATE", V_RULETEMPLATE, NO_OPTIONS },
#define V_RULENAME 83
{ "RULENAME", V_RULENAME, OPTIONS_OK | EXPR },
#define V_RULEVALUE 84
{ "RULEVALUE", V_RULEVALUE, OPTIONS_OK | EXPR },
#define V_PLAYERSTATID 85
{ "PLAYERSTATID", V_PLAYERSTATID, NO_OPTIONS },
#define V_PLAYERLEVEL 86
{ "PLAYERLEVEL", V_PLAYERLEVEL, NO_OPTIONS },
#define V_TOTALUTILIZATION 87
{ "TOTALUTILIZATION", V_TOTALUTILIZATION, NO_OPTIONS },
#define V_SCORE 88
{ "SCORE", V_SCORE, NO_OPTIONS },
};
int
read_qserver_template(char *filename)
{
return (read_template(filename, &server_template));
}
int
read_rule_template(char *filename)
{
return (read_template(filename, &rule_template));
}
int
read_header_template(char *filename)
{
return (read_template(filename, &header_template));
}
int
read_trailer_template(char *filename)
{
return (read_template(filename, &trailer_template));
}
int
read_player_template(char *filename)
{
return (read_template(filename, &player_template));
}
STATIC int
read_template(char *filename, char **template_text)
{
FILE *file;
int length, rc;
file = fopen(filename, "r");
if (file == NULL) {
perror(filename);
return (-1);
}
fseek(file, 0, SEEK_END);
length = ftell(file);
fseek(file, 0, SEEK_SET);
*template_text = (char *)malloc(length + 1);
rc = fread(*template_text, 1, length, file);
if ((rc == 0) && (length > 0)) {
perror(filename);
fclose(file);
free(*template_text);
*template_text = NULL;
return (-1);
}
(*template_text)[rc] = '\0';
return (0);
}
int
have_server_template()
{
return (server_template != NULL);
}
int
have_header_template()
{
return (header_template != NULL);
}
int
have_trailer_template()
{
return (trailer_template != NULL);
}
void
template_display_server(struct qserver *server)
{
char *t = server_template;
int var, varlen;
if_level = 0;
if_skip = 0;
for ( ; *t; t++) {
if (*t != VARIABLE_CHAR) {
if (!if_skip) {
putc(*t, OF);
}
continue;
}
var = parse_var(t, &varlen);
if (var == -1) {
if (!if_skip) {
putc(VARIABLE_CHAR, OF);
}
continue;
}
if (var == V_BACKSLASH) {
t += 2;
if (*t == '\r') {
if (*++t == '\n') {
t++;
}
} else if (*t == '\n') {
t++;
}
t--;
continue;
}
if (((var == V_IF) || (var == V_IFNOT)) && (variable_option != NULL)) {
int truth = (var == V_IF) ? 1 : 0;
if (!if_skip && (is_true(server, NULL, NULL, variable_option) == truth)) {
if_level++;
} else {
if_skip++;
}
} else if (var == V_ENDIF) {
if (if_skip) {
if_skip--;
} else if (if_level) {
if_level--;
}
}
if (!if_skip) {
display_server_var(server, var);
}
t += varlen;
}
}
void
template_display_players(struct qserver *server)
{
struct player *player;
for (player = server->players; player != NULL; player = player->next) {
template_display_player(server, player);
}
}
void
template_display_player(struct qserver *server, struct player *player)
{
char *t = player_template;
int var, varlen;
if (player_template == NULL) {
return;
}
if_level = 0;
if_skip = 0;
for ( ; *t; t++) {
if (*t != VARIABLE_CHAR) {
if (!if_skip) {
putc(*t, OF);
}
continue;
}
var = parse_var(t, &varlen);
if (var == -1) {
if (!if_skip) {
putc(VARIABLE_CHAR, OF);
}
continue;
}
if (var == V_BACKSLASH) {
t += 2;
if (*t == '\r') {
if (*++t == '\n') {
t++;
}
} else if (*t == '\n') {
t++;
}
t--;
continue;
}
if (((var == V_IF) || (var == V_IFNOT)) && (variable_option != NULL)) {
int truth = (var == V_IF) ? 1 : 0;
if (!if_skip && (is_true(server, player, NULL, variable_option) == truth)) {
if_level++;
} else {
if_skip++;
}
} else if (var == V_ENDIF) {
if (if_skip) {
if_skip--;
} else if (if_level) {
if_level--;
}
}
if (!if_skip) {
display_player_var(player, var, server);
}
t += varlen;
}
}
void
template_display_rules(struct qserver *server)
{
struct rule *rule;
for (rule = server->rules; rule != NULL; rule = rule->next) {
template_display_rule(server, rule);
}
}
void
template_display_rule(struct qserver *server, struct rule *rule)
{
char *t = rule_template;
int var, varlen;
if (rule_template == NULL) {
return;
}
if_level = 0;
if_skip = 0;
for ( ; *t; t++) {
if (*t != VARIABLE_CHAR) {
if (!if_skip) {
putc(*t, OF);
}
continue;
}
var = parse_var(t, &varlen);
if (var == -1) {
if (!if_skip) {
putc(VARIABLE_CHAR, OF);
}
continue;
}
if (var == V_BACKSLASH) {
t += 2;
if (*t == '\r') {
if (*++t == '\n') {
t++;
}
} else if (*t == '\n') {
t++;
}
t--;
continue;
}
if (((var == V_IF) || (var == V_IFNOT)) && (variable_option != NULL)) {
int truth = (var == V_IF) ? 1 : 0;
if (!if_skip && (is_true(server, NULL, rule, variable_option) == truth)) {
if_level++;
} else {
if_skip++;
}
} else if (var == V_ENDIF) {
if (if_skip) {
if_skip--;
} else if (if_level) {
if_level--;
}
}
if (!if_skip) {
display_rule_var(rule, var, server);
}
t += varlen;
}
}
void
template_display_header()
{
char *t = header_template;
int var, varlen;
if_level = 0;
if_skip = 0;
for ( ; *t; t++) {
if (*t != VARIABLE_CHAR) {
putc(*t, OF);
continue;
}
var = parse_var(t, &varlen);
if (var == -1) {
putc(VARIABLE_CHAR, OF);
continue;
}
if (var == V_BACKSLASH) {
t += 2;
if (*t == '\r') {
if (*++t == '\n') {
t++;
}
} else if (*t == '\n') {
t++;
}
t--;
continue;
}
display_generic_var(var);
t += varlen;
}
}
void
template_display_trailer()
{
char *t = trailer_template;
int var, varlen;
if_level = 0;
if_skip = 0;
for ( ; *t; t++) {
if (*t != VARIABLE_CHAR) {
putc(*t, OF);
continue;
}
var = parse_var(t, &varlen);
if (var == -1) {
putc(VARIABLE_CHAR, OF);
continue;
}
if (var == V_BACKSLASH) {
t += 2;
if (*t == '\r') {
if (*++t == '\n') {
t++;
}
} else if (*t == '\n') {
t++;
}
t--;
continue;
}
display_generic_var(var);
t += varlen;
}
}
STATIC void
display_server_var(struct qserver *server, int var)
{
char *game;
int full_data = 1;
if ((server->server_name == DOWN) || (server->server_name == TIMEOUT) ||
(server->server_name == SYSERROR) ||
(server->error != NULL) || server->type->master) {
full_data = 0;
}
switch (var) {
case V_HOSTNAME:
fputs((hostname_lookup) ? server->host_name : server->arg, OF);
break;
case V_SERVERNAME:
fputs(xform_name(server->server_name, server), OF);
break;
case V_PING:
if ((server->server_name != TIMEOUT) && (server->server_name != DOWN) &&
(server->server_name != HOSTNOTFOUND) && (server->server_name != SYSERROR)) {
fprintf(OF, "%d",
server->n_requests ? server->ping_total / server->n_requests : 999);
}
break;
case V_PLAYERS:
if (full_data) {
fprintf(OF, "%d", server->num_players);
}
break;
case V_MAXPLAYERS:
if (full_data) {
fprintf(OF, "%d", server->max_players);
}
break;
case V_MAP:
if (full_data) {
fputs((server->map_name) ? server->map_name : "?", OF);
}
break;
case V_GAME:
if (full_data) {
game = get_qw_game(server);
fputs((game) ? game : "", OF);
}
break;
case V_GAMETYPE:
{
struct rule *rule;
for (rule = server->rules; rule != NULL; rule = rule->next) {
if (strcasecmp(rule->name, "g_gametype") == 0) {
break;
}
}
if (rule != NULL) {
switch (atoi(rule->value)) {
case 0:
fputs("Free For All", OF);
break;
case 1:
fputs("Tournament", OF);
break;
case 3:
fputs("Team Deathmatch", OF);
break;
case 4:
fputs("Capture the Flag", OF);
break;
case 5:
fputs("Fortress or OSP", OF);
break;
case 6:
fputs("Capture and Hold", OF);
break;
case 8:
fputs("Arena", OF);
break;
default:
fputs("?", OF);
break;
}
}
}
break;
case V_RETRIES:
if ((server->server_name != TIMEOUT) && (server->server_name != DOWN) &&
(server->server_name != HOSTNOTFOUND)) {
fprintf(OF, "%d", server->n_retries);
}
break;
case V_IPADDR:
{
unsigned int ipaddr = ntohl(server->ipaddr);
fprintf(OF, "%u.%u.%u.%u", (ipaddr >> 24) & 0xff,
(ipaddr >> 16) & 0xff, (ipaddr >> 8) & 0xff, ipaddr & 0xff);
}
break;
case V_PORT:
fprintf(OF, "%hu", server->port);
break;
case V_ARG:
fputs(server->arg, OF);
break;
case V_TYPE:
fputs(server->type->game_name, OF);
break;
case V_TYPESTRING:
fputs(server->type->type_string, OF);
break;
case V_TYPEPREFIX:
fputs(server->type->type_prefix, OF);
break;
case V_RULE:
{
struct rule *rule;
if (variable_option == NULL) {
break;
}
for (rule = server->rules; rule != NULL; rule = rule->next) {
if (strcasecmp(rule->name, variable_option) == 0) {
display_string(rule->value);
}
}
}
break;
case V_ALLRULES:
{
struct rule *rule;
for (rule = server->rules; rule != NULL; rule = rule->next) {
if (rule != server->rules) {
fputs(", ", OF);
}
display_string(rule->name);
putc('=', OF);
display_string(rule->value);
}
}
break;
case V_PLAYERTEMPLATE:
if_level_save = if_level;
if_skip_save = if_skip;
template_display_players(server);
if_level = if_level_save;
if_skip = if_skip_save;
break;
case V_RULETEMPLATE:
if_level_save = if_level;
if_skip_save = if_skip;
template_display_rules(server);
if_level = if_level_save;
if_skip = if_skip_save;
break;
default:
display_generic_var(var);
}
}
STATIC void
display_player_var(struct player *player, int var, struct qserver *server)
{
switch (var) {
case V_PLAYERNAME:
fputs(xform_name(player->name, server), OF);
break;
case V_HTMLPLAYERNAME:
{
int save_html_names = xform_html_names;
int save_hex_player_names = xform_hex_player_names;
xform_html_names = 1;
xform_hex_player_names = 0;
fputs(xform_name(player->name, server), OF);
xform_html_names = save_html_names;
xform_hex_player_names = save_hex_player_names;
}
break;
case V_TRIBETAG:
fputs(xform_name(player->tribe_tag, server), OF);
break;
case V_FRAGS:
fprintf(OF, "%d", player->frags);
break;
case V_SCORE:
fprintf(OF, "%d", player->score);
break;
case V_DEATHS:
fprintf(OF, "%d", player->deaths);
break;
case V_PLAYERPING:
fprintf(OF, "%d", player->ping);
break;
case V_CONNECTTIME:
fputs(play_time(player->connect_time, 0), OF);
break;
case V_SKIN:
display_string(player->skin);
break;
case V_MESH:
display_string(player->mesh);
break;
case V_FACE:
display_string(player->face);
break;
case V_SHIRTCOLOR:
if (color_names) {
fputs(quake_color(player->shirt_color), OF);
} else {
fprintf(OF, "%d", player->shirt_color);
}
break;
case V_PANTSCOLOR:
if (color_names) {
fputs(quake_color(player->pants_color), OF);
} else {
fprintf(OF, "%d", player->pants_color);
}
break;
case V_PLAYERIP:
if (player->address) {
fputs(player->address, OF);
}
break;
case V_TEAMNUM:
fprintf(OF, "%d", player->team);
break;
case V_PACKETLOSS:
fprintf(OF, "%d", player->packet_loss);
break;
case V_TEAMNAME:
if (player->team_name) {
fprintf(OF, "%s", player->team_name);
}
break;
case V_COLORNUMBERS:
color_names = 0;
break;
case V_COLORNAMES:
color_names = 1;
break;
case V_COLORRGB:
color_names = 2;
break;
case V_TIMESECONDS:
time_format = SECONDS;
break;
case V_TIMECLOCK:
time_format = CLOCK_TIME;
break;
case V_TIMESTOPWATCH:
time_format = STOPWATCH_TIME;
break;
case V_PLAYERSTATID:
case V_PLAYERLEVEL:
fprintf(OF, "%u", player->ship);
break;
default:
display_server_var(server, var);
}
}
STATIC void
display_rule_var(struct rule *rule, int var, struct qserver *server)
{
switch (var) {
case V_RULENAME:
fputs(rule->name, OF);
break;
case V_RULEVALUE:
fputs(xform_name(rule->value, server), OF);
break;
default:
display_server_var(server, var);
}
}
STATIC void
display_generic_var(int var)
{
switch (var) {
case V_QSTATURL:
fputs("http://www.qstat.org", OF);
break;
case V_QSTATVERSION:
fputs(qstat_version, OF);
break;
case V_QSTATAUTHOR:
fputs("Steve Jankowski", OF);
break;
case V_QSTATAUTHOREMAIL:
fputs("[email protected]", OF);
break;
case V_HTML:
html_mode ^= 1;
if (html_mode && (xform_html_names == -1)) {
xform_html_names = 1;
}
break;
case V_CLEARNEWLINES:
clear_newlines_mode ^= 1;
break;
case V_NOW:
{
time_t now = time(0);
char *now_string = ctime(&now);
now_string[strlen(now_string) - 1] = '\0';
fputs(now_string, OF);
break;
}
case V_NOWINT:
fprintf(OF, "%u", (unsigned int)time(0));
break;
case V_TOTALSERVERS:
fprintf(OF, "%d", num_servers_total);
break;
case V_TOTALUP:
fprintf(OF, "%d", num_servers_total - num_servers_timed_out -