aboutsummaryrefslogtreecommitdiffstats
path: root/FICS/adminproc.c
blob: 08a3d1e3561f004300edf9dc64c99fe8c673be05 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
/*
 * fics - An internet chess server
 * Copyright (c) 1993 Richard V. Nash
 *
 * 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 GNU General Public License for more
 * details.
 */

#include "stdinclude.h"
#include "common.h"

#include <sys/param.h>

#include "adminproc.h"
#include "command.h"
#include "comproc.h"
#include "fics_getsalt.h"
#include "gamedb.h"
#include "gameproc.h"
#include "multicol.h"
#include "network.h"
#include "obsproc.h"
#include "playerdb.h"
#include "ratings.h"
#include "rmalloc.h"
#include "talkproc.h"
#include "utils.h"

#define PASSLEN 8

PUBLIC int num_anews = -1;

/*
 * adjudicate
 *
 * Usage: adjudicate white_player black_player result
 *
 *   Adjudicates a saved (stored) game between white_player and black_player.
 *   The result is one of: abort, draw, white, black.  "Abort" cancels the game
 *   (no win, loss or draw), "white" gives white_player the win, "black" gives
 *   black_player the win, and "draw" gives a draw.
 */
PUBLIC int com_adjudicate(int p, param_list param)
{
  int wp, wconnected, bp, bconnected, g, inprogress, confused = 0;

  ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);
  if (!FindPlayer(p, param[0].val.word, &wp, &wconnected))
    return COM_OK;
  if (!FindPlayer(p, param[1].val.word, &bp, &bconnected)) {
    if (!wconnected)
     player_remove(wp);
    return COM_OK;
  }

  inprogress = ((parray[wp].game >=0) &&(parray[wp].opponent == bp));

  if (inprogress) {
    g = parray[wp].game;
  } else {
    g = game_new();
    if (game_read(g, wp, bp) < 0) {
      confused = 1;
      pprintf(p, "There is no stored game %s vs. %s\n", parray[wp].name, parray[bp].name);
    } else {
      garray[g].white = wp;
      garray[g].black = bp;
    }
  }
  if (!confused) {
    if (strstr("abort", param[2].val.word) != NULL) {
      game_ended(g, WHITE, END_ADJABORT);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been aborted.",
	       parray[wp].name, parray[wp].name, parray[bp].name);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been aborted.",
	       parray[bp].name, parray[wp].name, parray[bp].name);
    } else if (strstr("draw", param[2].val.word) != NULL) {
      game_ended(g, WHITE, END_ADJDRAW);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a draw", parray[wp].name, parray[wp].name, parray[bp].name);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a draw", parray[bp].name, parray[wp].name, parray[bp].name);
    } else if (strstr("white", param[2].val.word) != NULL) {
      game_ended(g, WHITE, END_ADJWIN);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a win", parray[wp].name, parray[wp].name, parray[bp].name);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a loss", parray[bp].name, parray[wp].name, parray[bp].name);
    } else if (strstr("black", param[2].val.word) != NULL) {
      game_ended(g, BLACK, END_ADJWIN);
      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a loss", parray[wp].name, parray[wp].name, parray[bp].name);

      pcommand(p, "message %s Your game \"%s vs. %s\" has been adjudicated "
	       "as a win", parray[bp].name, parray[wp].name, parray[bp].name);
    } else {
      confused = 1;
      pprintf(p, "Result must be one of: abort draw white black\n");
    }
  }
  if (!confused) {
    pprintf(p, "Game adjudicated.\n");
    if (!inprogress) {
      game_delete(wp, bp);
    } else {
      return (COM_OK);
    }
  }
  game_remove(g);
  if (!wconnected)
    player_remove(wp);
  if (!bconnected)
    player_remove(bp);
  return COM_OK;
}

/*
 * create_news_file:  Creates either a general or and admin news
 *                    file, depending upon the admin switch.
 */
PRIVATE int create_news_file(int p, param_list param, int admin)
{
  FILE *fp;
  char filename[MAX_FILENAME_SIZE];

  ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

  if (admin) {
    if (param[0].val.integer > num_anews)
      pprintf(p, "There must be an admin news index #%d before you can create the file.", param[0].val.integer);
    else {
      sprintf(filename, "%s/adminnews.%d", news_dir, param[0].val.integer);
      fp = fopen(filename, "w");
      fprintf(fp, "%s\n", param[1].val.string);
      fclose(fp);
    }
  } else {
    if (param[0].val.integer > num_news)
      pprintf(p, "There must be a news index #%d before you can create the file.", param[0].val.integer);
    else {
      sprintf(filename, "%s/news.%d", news_dir, param[0].val.integer);
      fp = fopen(filename, "w");
      fprintf(fp, "%s\n", param[1].val.string);
      fclose(fp);
    }
  }

  return COM_OK;
}

PRIVATE int add_item(char *new_item, char *filename)
{
  FILE *new_fp, *old_fp;
  char tmp_file[MAX_FILENAME_SIZE];
  char junk[MAX_LINE_SIZE];

  sprintf(tmp_file, "%s/.tmp.idx", news_dir);
  new_fp = fopen(tmp_file, "w");
  old_fp = fopen(filename, "r");

  if (!new_fp || !old_fp)
    return 0;

  fprintf(new_fp, "%s", new_item);
  while (1) {
    fgets(junk, MAX_LINE_SIZE, old_fp);
    if (feof(old_fp))
      break;
    fprintf(new_fp, "%s", junk);
  }
  fclose(new_fp);
  fclose(old_fp);
  remove(filename);
  rename(tmp_file, filename);

  return 1;
}

/*
 * Adds a new item to either the general or admin news index file,
 * depending upon the admin switch.
 */
PRIVATE int
create_news_index(int p, param_list param, int admin)
{
	char	 filename[MAX_FILENAME_SIZE] = { '\0' };
	char	 new_item[MAX_LINE_SIZE] = { '\0' };

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (admin) {
		if (strlen(param[0].val.string) > 50) {
			pprintf(p, "Sorry, you must limit an index to 50 "
			    "charaters! Admin news index not created.\n");
		} else {
			num_anews++;

			snprintf(new_item, sizeof new_item, "%ld %d %s\n",
			    (long int)time(NULL),
			    num_anews,
			    param[0].val.string);
			snprintf(filename, sizeof filename,
			    "%s/newadminnews.index", news_dir);

			if (add_item(new_item, filename)) {
				pprintf(p, "Index for admin news item #%d "
				    "created.\n", num_anews);
				pprintf(p, "Please use 'canewsf' to include "
				    "more info.\n");
			} else {
				pprintf(p, "Something went wrong creating item."
				    "\nNotify Marsalis.\n");
			}
		}
	} else {
		if (strlen(param[0].val.string) > 50) {
			pprintf(p, "Sorry, you must limit an index to 50 "
			    "charaters! News index not created.\n");
		} else {
			num_news++;

			snprintf(filename, sizeof filename, "%s/newnews.index",
			    news_dir);
			snprintf(new_item, sizeof new_item, "%ld %d %s\n",
			    (long int)time(NULL),
			    num_news,
			    param[0].val.string);

			if (add_item(new_item, filename)) {
				pprintf(p, "Index for news item #%d created.\n",
				    num_news);
				pprintf(p, "Please use 'cnewsf' to include "
				    "more info.\n");
			} else {
				pprintf(p, "Something went wrong creating item."
				    "\nNotify Marsalis.\n");
			}
		}
	}

	return COM_OK;
}

/* cnewsi
 *
 * Usage: cnewsi message
 *
 *
 *   This command adds a new item to the news index.  The message is limited to
 *   45 characters for formating purposes.  In essence, the news index works
 *   like a newspaper headline, giving the user enough information to know
 *   whether they should read the entire news file for that item.  After
 *   creating the news item, the command reports the news item number along
 *   with a reminder to create a news file if necessary.
 */
PUBLIC int com_cnewsi(int p, param_list param)
{
  return create_news_index(p, param, 0);
}

/* cnewsf
 *
 * Usage: cnewsf # message
 *
 *   This command allows you to add additional information about a news item
 *   that had previously been created using 'cnewsi'.  The '#' is the number
 *   of the news index and 'message' is the additional text.  You can also
 *   modify a previous news item description and thus update the news item
 *   easily.
 */
PUBLIC int com_cnewsf(int p, param_list param)
{
  return create_news_file(p, param, 0);
}

PUBLIC int com_canewsi(int p, param_list param)
{
  return create_news_index(p, param, 1);
}

PUBLIC int com_canewsf(int p, param_list param)
{
  return create_news_file(p, param, 1);
}

/*
 * anews
 *
 *
 * Usage: anews [#, all]
 *
 *   This command is used to display anews (admin news) entries.  The
 *   entries are numbered.  "Anews #" displays that anews item.  "Anews
 *   all" will display all items.
 *
 */
PUBLIC int
com_anews(int p, param_list param)
{
	FILE		*fp;
	char		*junkp;
	char		 count[10];
	char		 filename[MAX_FILENAME_SIZE];
	char		 junk[MAX_LINE_SIZE];
	int		 found = 0;
	long int	 lval;
	time_t		 crtime;

	sprintf(filename, "%s/newadminnews.index", news_dir);

	if ((fp = fopen(filename, "r")) == NULL) {
		fprintf(stderr, "Cant find news index.\n");
		return COM_OK;
	}

	if (param[0].type == 0) {
		/*
		 * No params - then just display index over news.
		 */

		sprintf(filename, "%s/newadminnews.index", news_dir);

		pprintf(p, "Index of recent admin news items:\n");
		fgets(junk, MAX_LINE_SIZE, fp);
		sscanf(junk, "%ld %s", &lval, count);
		rscan_news2(fp, p, 9);

		junkp = junk;
		junkp = nextword(junkp);
		junkp = nextword(junkp);

		crtime = lval;
		pprintf(p, "%3s (%s) %s", count, fix_time(strltime(&crtime)),
		    junkp);
		fclose(fp);
	} else if (param[0].type == TYPE_WORD &&
	    !strcmp(param[0].val.word, "all")) {
		/*
		 * Param all - displays all news items.
		 */

		pprintf(p, "Index of all admin news items:\n");
		fgets(junk, MAX_LINE_SIZE, fp);
		sscanf(junk, "%ld %s", &lval, count);
		rscan_news(fp, p, 0);

		junkp = junk;
		junkp = nextword(junkp);
		junkp = nextword(junkp);

		crtime = lval;
		pprintf(p, "%3s (%s) %s", count, fix_time(strltime(&crtime)),
		    junkp);
		fclose(fp);
	} else {
		while (!feof(fp) && !found) {
			junkp = junk;
			fgets(junk, MAX_LINE_SIZE, fp);

			if (feof(fp))
				break;

			if (strlen(junk) > 1) {
				sscanf(junkp, "%ld %s", &lval, count);
				crtime = lval;

				if (!strcmp(count, param[0].val.word)) {
					found = 1;

					junkp = nextword(junkp);
					junkp = nextword(junkp);

					pprintf(p, "ANEWS %3s (%s) %s\n", count,
					    fix_time(strltime(&crtime)), junkp);
				}
			}
		}

		fclose(fp);

		if (!found) {
			pprintf(p, "Bad index number!\n");
			return COM_OK;
		}

		sprintf(filename, "%s/adminnews.%s", news_dir,
		    param[0].val.word);

		if ((fp = fopen(filename, "r")) == NULL) {
			pprintf(p, "No more info.\n");
			return COM_OK;
		}

		fclose(fp);
		sprintf(filename, "adminnews.%s", param[0].val.word);

		if (psend_file(p, news_dir, filename) < 0) {
			pprintf(p, "Internal error - couldn't send news file!"
			    "\n");
		}
	}

	return COM_OK;
}

PUBLIC int
strcmpwild(char *mainstr, char *searchstr)
{
	if (strlen(mainstr) < strlen(searchstr))
		return 1;
	for (size_t i = 0; i < strlen(mainstr); i++) {
		if (searchstr[i] == '*')
			return 0;
		if (mainstr[i] != searchstr[i])
			return 1;
	}

	return 0;
}

/*
 * chkip
 *
 * Usage: chkip ip_address
 *
 *   This command returns the names of all users currently logged on
 *   from a given IP address.
 */
PUBLIC int com_checkIP(int p, param_list param)
{
  char *ipstr = param[0].val.word;
  int p1;

  ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);
  pprintf(p, "Matches the following player(s): \n\n");
  for (p1 = 0; p1 < p_num; p1++)
    if (!strcmpwild(dotQuad(parray[p1].thisHost), ipstr) && (parray[p1].status != PLAYER_EMPTY))
      pprintf(p, "%16.16s %s\n", parray[p1].name, dotQuad(parray[p1].thisHost));
  return COM_OK;
}

PUBLIC int com_checkSOCKET(int p, param_list param)
{
  int fd = param[0].val.integer;
  int p1, flag;

  ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);
  flag = 0;
  for (p1 = 0; p1 < p_num; p1++) {
    if (parray[p1].socket == fd) {
      flag = 1;
      pprintf(p, "Socket %d is used by %s\n", fd, parray[p1].name);
    }
  }
  if (!flag)
    pprintf(p, "Socket %d is unused!\n", fd);
  return COM_OK;
}

/*
 * chkpl
 *
 * Usage: chkpl handle
 *
 *   This command displays server information about a given user.  Items
 * displayed are:
 *
 *    number X in parray of size Y
 *    name
 *    login
 *    fullName
 *    emailAddress
 *    socket
 *    registered
 *    last_tell
 *    last_channel
 *    logon_time
 *    adminLevel
 *    thisHost
 *    lastHost
 *    num_comments
 */
PUBLIC int
com_checkPLAYER(int p, param_list param)
{
	char	*player = param[0].val.word;
	int	 p1;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	p1 = player_search(p, param[0].val.word);

	if (!p1)
		return COM_OK;

	if (p1 < 0) {
		p1 = (-p1) - 1;
		pprintf(p, "%s is not logged in.\n", player);
		stolower(player);

		pprintf(p, "name = %s\n", parray[p1].name);
		pprintf(p, "login = %s\n", parray[p1].login);
		pprintf(p, "fullName = %s\n", (parray[p1].fullName ?
		    parray[p1].fullName : "(none)"));
		pprintf(p, "emailAddress = %s\n", (parray[p1].emailAddress ?
		    parray[p1].emailAddress : "(none)"));
		pprintf(p, "adminLevel = %d\n", parray[p1].adminLevel);
#if 0
		pprintf(p, "network_player = %d\n", parray[p1].network_player);
#endif
		pprintf(p, "lastHost = %s\n", dotQuad(parray[p1].lastHost));
		pprintf(p, "num_comments = %d\n", parray[p1].num_comments);

		player_remove(p1);
		return COM_OK;
	} else {
		p1 = p1 - 1;

		pprintf(p, "%s is number %d in parray of size %d\n", player, p1,
		    (p_num + 1));
		pprintf(p, "name = %s\n", parray[p1].name);
		pprintf(p, "login = %s\n", parray[p1].login);
		pprintf(p, "fullName = %s\n", (parray[p1].fullName ?
		    parray[p1].fullName : "(none)"));
		pprintf(p, "emailAddress = %s\n", (parray[p1].emailAddress ?
		    parray[p1].emailAddress : "(none)"));
		pprintf(p, "socket = %d\n", parray[p1].socket);
		pprintf(p, "registered = %d\n", parray[p1].registered);
		pprintf(p, "last_tell = %d\n", parray[p1].last_tell);
		pprintf(p, "last_channel = %d\n", parray[p1].last_channel);
		pprintf(p, "logon_time = %s",
		    ctime((time_t *) &parray[p1].logon_time));
		pprintf(p, "adminLevel = %d\n", parray[p1].adminLevel);
#if 0
		pprintf(p, "network_player = %d\n", parray[p1].network_player);
#endif
		pprintf(p, "thisHost = %s\n", dotQuad(parray[p1].thisHost));
		pprintf(p, "lastHost = %s\n", dotQuad(parray[p1].lastHost));
		pprintf(p, "num_comments = %d\n", parray[p1].num_comments);
	}

	return COM_OK;
}

/*
 * chkts
 *
 * Usage: chkts
 *
 *   This command displays all current users who are using timeseal.
 */
PUBLIC int
com_checkTIMESEAL(int p, param_list param)
{
	int p1, count = 0;

	/* XXX: maybe unused */
	(void) p1;
	(void) count;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	pprintf(p, "The following player(s) are using timeseal:\n\n");

#ifdef TIMESEAL
	for (p1 = 0; p1 < p_num; p1++) {
		if (parray[p1].status != PLAYER_EMPTY &&
		    con[parray[p1].socket].timeseal) {
			pprintf(p, "%s\n", parray[p1].name);
			count++;
		}
	}
	pprintf(p, "\nNumber of people using timeseal:  %d\n", count);
#endif

	return COM_OK;
}

PUBLIC int
com_checkGAME(int p,param_list param)
{
	char		 tmp[10 + 1 + 7];	// enough to store number
						// 'black: ' and '\0'
	int		 found = 0;
	int		 p1, g, link;
	multicol	*m;
	time_t		 startTime;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (g_num == 0) {
		pprintf(p, "No games are currently linked into the 'garray' "
		    "structure.\n");
		return COM_OK;
	}

	if (param[0].type == TYPE_WORD) {    // a player name
		if ((p1 = player_find_part_login(param[0].val.word)) < 0) {
			pprintf(p, "%s doesn't appear to be logged in.\n",
			    param[0].val.word);
			pprintf(p, "Searching through garray to find matching "
			    "game numbers.\n");
			pprintf(p, "Use chkgame <number> to view the results."
			    "\n");

			m = multicol_start(g_num * 2);	// Obviously no more
							// than that

			for (g = 0; g < g_num; g++) {
				multicol_store(m, tmp);

				if (!strcasecmp(garray[g].white_name,param[0].val.word))  {
					sprintf(tmp, "White: %d", g);
					multicol_store(m, tmp);
					found = 1;
				}
				if (!strcasecmp(garray[g].black_name,param[0].val.word))  {
					sprintf(tmp, "Black: %d", g);
					multicol_store(m, tmp);
					found = 1;
				}
			}

			if (found)
				multicol_pprint(m, p, parray[p].d_width, 2);
			else
				pprintf(p,"No matching games were found.\n");
			multicol_end(m);

			return COM_OK;
		}

		if ((g = parray[p1].game) < 0) {
			pprintf(p, "%s doesn't appear to be playing a game.\n",
			    parray[p1].name);
			pprintf(p, "Searching through garray to find matching "
			    "game numbers.\n");
			pprintf(p, "Use chkgame <number> to view the results."
			    "\n");

			m = multicol_start(g_num * 2);	// Obviously no more
							// than that

			for (g = 0; g < g_num; g++) {
				if (garray[g].white == p1) {
					sprintf(tmp, "White: %d", g);
					multicol_store(m, tmp);
					found = 1;
				}

				if (garray[g].black == p1) {
					sprintf(tmp, "Black: %d", g);
					multicol_store(m,tmp);
					found = 1;
				}
			}

			if (found)
				multicol_pprint(m, p, parray[p].d_width, 2);
			else
				pprintf(p, "No matching games were found.\n");
			multicol_end(m);

			return COM_OK;
		}
	} else {
		if ((g = param[0].val.integer - 1) < 0 || g >= g_num) {
			pprintf(p, "The current range of game numbers is 1 to "
			    "%d.\n", g_num);
			return COM_OK;
		}
	}

	startTime = untenths(garray[g].timeOfStart);

	pprintf(p, "Current stored info for game %d (garray[%d]):\n", (g + 1),
	    g);
	pprintf(p, "Initial white time: %d    Initial white increment %d\n",
	    (garray[g].wInitTime / 600),
	    (garray[g].wIncrement / 10));
	pprintf(p, "Initial black time: %d    Initial black increment %d\n",
	    (garray[g].bInitTime / 600),
	    (garray[g].bIncrement / 10));
	pprintf(p, "Time of starting: %s\n", strltime(&startTime));
	pprintf(p, "Game is: %s (%d) vs. %s (%d)\n",
	    garray[g].white_name,
	    garray[g].white_rating,
	    garray[g].black_name,
	    garray[g].black_rating);
	pprintf(p, "White parray entry: %d    Black parray entry %d\n",
	    garray[g].white,
	    garray[g].black);

	if ((link = garray[g].link) >= 0) {
		pprintf(p, "Bughouse linked to game: %d\n",
		    (garray[g].link + 1));
		pprintf(p, "Partner is playing game: %s (%d) vs. %s (%d)\n",
		    garray[link].white_name,
		    garray[link].white_rating,
		    garray[link].black_name,
		    garray[link].black_rating);
	} else {
		pprintf(p, "Game is not bughouse or link to partner's game not "
		    "found.\n");
	}

	pprintf(p, "Game is %s\n", (garray[g].rated ? "rated" : "unrated"));
	pprintf(p, "Game is %s\n", (garray[g].private ? "private" :
	    "not private"));

	if (garray[g].type == TYPE_UNTIMED)
		pprintf(p, "Games is of type: untimed\n");
	else if (garray[g].type == TYPE_BLITZ)
		pprintf(p, "Games is of type: blitz\n");
	else if (garray[g].type == TYPE_STAND)
		pprintf(p, "Games is of type: standard\n");
	else if (garray[g].type == TYPE_NONSTANDARD)
		pprintf(p, "Games is of type: non-standard\n");
	else if (garray[g].type == TYPE_WILD)
		pprintf(p, "Games is of type: wild\n");
	else if (garray[g].type == TYPE_LIGHT)
		pprintf(p, "Games is of type: lightning\n");
	else if (garray[g].type == TYPE_BUGHOUSE)
		pprintf(p, "Games is of type: bughouse\n");
	else
		pprintf(p, "Games is of type: Unknown - Error!\n");

	pprintf(p, "%d halfmove(s) have been made\n", garray[g].numHalfMoves);

	if (garray[g].status == GAME_ACTIVE)
		game_update_time(g);

	pprintf(p, "White's time %s    Black's time ",
	    tenth_str((garray[g].wTime > 0 ? garray[g].wTime : 0), 0));
	pprintf(p, "%s\n",
	    tenth_str((garray[g].bTime > 0 ? garray[g].bTime : 0), 0));
	pprintf(p, "The clock is%sticking\n", ((garray[g].clockStopped ||
	    garray[g].status != GAME_ACTIVE) ? " not " : " "));

	if (garray[g].status == GAME_EMPTY)
		pprintf(p, "Game status: GAME_EMPTY\n");
	else if (garray[g].status == GAME_NEW)
		pprintf(p, "Game status: GAME_NEW\n");
	else if (garray[g].status == GAME_ACTIVE)
		pprintf(p, "Game status: GAME_ACTIVE\n");
	else if (garray[g].status == GAME_EXAMINE)
		pprintf(p, "Game status: GAME_EXAMINE\n");
	else
		pprintf(p, "Game status: Unknown - Error!\n");
	return COM_OK;
}

/*
 * remplayer
 *
 * Usage:  remplayer name
 *
 *   Removes an account.  A copy of its files are saved under .rem.* which can
 *   be found in the appropriate directory (useful in case of an accident).
 *
 *   The account's details, messages, games and logons are all saved as
 *   'zombie' files.  These zombie accounts are not listed in handles or
 *   totals.
 */
PUBLIC int
com_remplayer(int p, param_list param)
{
	char	*player = param[0].val.word;
	char	 playerlower[MAX_LOGIN_NAME];
	int	 p1, lookup;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	strcpy(playerlower, player);
	stolower(playerlower);
	p1 = player_new();
	lookup = player_read(p1, playerlower);

	if (!lookup) {
		if (parray[p].adminLevel <= parray[p1].adminLevel &&
		    !player_ishead(p)) {
			pprintf(p, "You can't remove an admin with a level "
			    "higher than or equal to yourself.\n");
			player_remove(p1);
			return COM_OK;
		}
	}

	player_remove(p1);

	if (lookup) {
		pprintf(p, "No player by the name %s is registered.\n", player);
		return COM_OK;
	}

	if (player_find_bylogin(playerlower) >= 0) {
		pprintf(p, "A player by that name is logged in.\n");
		return COM_OK;
	}

	if (!player_kill(playerlower)) {
		pprintf(p, "Player %s removed.\n", player);
		UpdateRank(TYPE_BLITZ, NULL, NULL, player);
		UpdateRank(TYPE_STAND, NULL, NULL, player);
		UpdateRank(TYPE_WILD, NULL, NULL, player);
	} else {
		pprintf(p, "Remplayer failed.\n");
	}
	return COM_OK;
}

/*
 * raisedead
 *
 * Usage:  raisedead oldname [newname]
 *
 *   Restores an account that has been previously removed using "remplayer".
 *   The zombie files from which it came are removed.  Under most
 *   circumstances, you restore the account to the same handle it had
 *   before (oldname).  However, in some circumstances you may need to
 *   restore the account to a different handle, in which case you include
 *   "newname" as the new handle.  After "raisedead", you may need to use the
 *   "asetpasswd" command to get the player started again as a registered
 *   user, especially if the account had been locked
 *   by setting the password to *.
 */
PUBLIC int
com_raisedead(int p, param_list param)
{
	char	*player = param[0].val.word;
	char	 newplayerlower[MAX_LOGIN_NAME];
	char	 playerlower[MAX_LOGIN_NAME];
	int	 p1, p2, lookup;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	strcpy(playerlower, player);
	stolower(playerlower);

	if (player_find_bylogin(playerlower) >= 0) {
		pprintf(p, "A player by that name is logged in.\n");
		pprintf(p, "Can't raise until they leave.\n");
		return COM_OK;
	}

	p1 = player_new();
	lookup = player_read(p1, playerlower);
	player_remove(p1);

	if (!lookup) {
		pprintf(p, "A player by the name %s is already registered.\n",
		    player);
		pprintf(p, "Obtain a new handle for the dead person.\n");
		pprintf(p, "Then use raisedead [oldname] [newname].\n");
		return COM_OK;
	}

	if (param[1].type == TYPE_NULL) {
		if (!player_raise(playerlower)) {
			pprintf(p, "Player %s raised from dead.\n", player);

			p1 = player_new();

			if (!player_read(p1, playerlower)) {
				if (parray[p1].s_stats.rating > 0) {
					UpdateRank(TYPE_STAND, player,
					    &parray[p1].s_stats, player);
				}
				if (parray[p1].b_stats.rating > 0) {
					UpdateRank(TYPE_BLITZ, player,
					    &parray[p1].b_stats, player);
				}
				if (parray[p1].w_stats.rating > 0) {
					UpdateRank(TYPE_WILD, player,
					    &parray[p1].w_stats, player);
				}
			}

			player_remove(p1);
		} else {
			pprintf(p, "Raisedead failed.\n");
		}

		return COM_OK;
	} else {
		char *newplayer = param[1].val.word;

		strcpy(newplayerlower, newplayer);
		stolower(newplayerlower);

		if (player_find_bylogin(newplayerlower) >= 0) {
			pprintf(p, "A player by the requested name is "
			    "logged in.\n");
			pprintf(p, "Can't reincarnate until they leave.\n");
			return COM_OK;
		}

		p2 = player_new();
		lookup = player_read(p2, newplayerlower);
		player_remove(p2);

		if (!lookup) {
			pprintf(p, "A player by the name %s is already "
			    "registered.\n", player);
			pprintf(p, "Obtain another new handle for the dead "
			    "person.\n");
			return COM_OK;
		}

		if (!player_reincarn(playerlower, newplayerlower)) {
			pprintf(p, "Player %s reincarnated to %s.\n", player,
			    newplayer);

			p2 = player_new();

			if (!player_read(p2, newplayerlower)) {
				strfree(parray[p2].name);
				parray[p2].name = xstrdup(newplayer);
				player_save(p2);

				if (parray[p2].s_stats.rating > 0) {
					UpdateRank(TYPE_STAND, newplayer,
					    &parray[p2].s_stats, newplayer);
				}
				if (parray[p2].b_stats.rating > 0) {
					UpdateRank(TYPE_BLITZ, newplayer,
					    &parray[p2].b_stats, newplayer);
				}
				if (parray[p2].w_stats.rating > 0) {
					UpdateRank(TYPE_WILD, newplayer,
					    &parray[p2].w_stats, newplayer);
				}
			}

			player_remove(p2);
		} else {
			pprintf(p, "Raisedead failed.\n");
		}
	}

	return COM_OK;
}

/*
 * addplayer
 *
 * Usage: addplayer playername emailaddress realname
 *
 *   Adds a local player to the server with the handle of "playername".  For
 *   example:
 *
 *      addplayer Hawk u940456@daimi.aau.dk Henrik Gram
 */
PUBLIC int
com_addplayer(int p, param_list param)
{
	char	*newemail = param[1].val.word;
	char	*newname = param[2].val.string;
	char	*newplayer = param[0].val.word;
	char	 newplayerlower[MAX_LOGIN_NAME];
	char	 password[PASSLEN + 1];
	char	 salt[FICS_SALT_SIZE];
	char	 text[2048];
	int	 i;
	int	 p1;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (strlen(newplayer) >= MAX_LOGIN_NAME) {
		pprintf(p, "Player name is too long\n");
		return COM_OK;
	}

	if (strlen(newplayer) < 3) {
		pprintf(p, "Player name is too short\n");
		return COM_OK;
	}

	if (!alphastring(newplayer)) {
		pprintf(p, "Illegal characters in player name. "
		    "Only A-Za-z allowed.\n");
		return COM_OK;
	}

	strcpy(newplayerlower, newplayer);
	stolower(newplayerlower);

	p1 = player_new();

	if (!player_read(p1, newplayerlower)) {
		pprintf(p, "A player by the name %s is already registered.\n",
		    newplayerlower);
		player_remove(p1);
		return COM_OK;
	}

	parray[p1].name            = xstrdup(newplayer);
	parray[p1].login           = xstrdup(newplayerlower);
	parray[p1].fullName        = xstrdup(newname);
	parray[p1].emailAddress    = xstrdup(newemail);

	if (strcmp(newemail, "none")) {
		for (i = 0; i < PASSLEN; i++)
			password[i] = ('a' + arc4random_uniform(26));
		password[i] = '\0';

		strcpy(salt, fics_getsalt());

		parray[p1].passwd = xstrdup(crypt(password, salt));
	} else {
		password[0] = '\0';
		parray[p1].passwd = xstrdup(password);
	}

	parray[p1].registered	= 1;
	parray[p1].rated	= 1;

	player_add_comment(p, p1, "Player added by addplayer.");
	player_save(p1);
	player_remove(p1);

	pprintf(p, "Added: >%s< >%s< >%s< >%s<\n", newplayer, newname, newemail,
	    password);

	if (strcmp(newemail, "none")) {
		sprintf(text, "\nYour player account has been created.\n\n"
		    "Login Name: %s\n"
		    "Full Name: %s\n"
		    "Email Address: %s\n"
		    "Initial Password: %s\n\n"

		    "If any of this information is incorrect, please\n"
		    "contact the administrator to get it corrected.\n\n"

		    "You may change your password with the password\n"
		    "command on the server.\n\n"

		    "Please be advised that if this is an unauthorized\n"
		    "duplicate account for you, by using it you take\n"
		    "the risk of being banned from accessing this chess\n"
		    "server.\n\n"

		    "To connect to the server and use this account:\n\n"
		    "\ttelnet %s 5000\n\n"
		    "and enter your handle name and password.\n\n"

		    "Regards,\n\nThe FICS admins\n", newplayer, newname,
		    newemail, password, fics_hostname);

		mail_string_to_address(newemail, "FICS Account Created", text);

		if ((p1 = player_find_part_login(newplayer)) >= 0) {
			pprintf_prompt(p1, "\n\nYou are now registered! "
			    "Confirmation together with\npassword is sent to "
			    "your email address.\n\n");
			return COM_OK;
		}

		return COM_OK;
	} else {
		if ((p1 = player_find_part_login(newplayer)) >= 0) {
			pprintf_prompt(p1, "\n\nYou are now registered! "
			    "You have NO password!\n\n");
			return COM_OK;
		}
	}

	return COM_OK;
}

PUBLIC int
com_pose(int p, param_list param)
{
	int	p1;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if ((p1 = player_find_part_login(param[0].val.word)) < 0) {
		pprintf(p, "%s is not logged in.\n", param[0].val.word);
		return COM_OK;
	}

	if ((parray[p].adminLevel <= parray[p1].adminLevel) &&
	    !player_ishead(p)) {
		pprintf(p, "You can only pose as players below your adminlevel."
		    "\n");
		return COM_OK;
	}

	pprintf(p, "Command issued as %s\n", parray[p1].name);
	pcommand(p1, "%s\n", param[1].val.string);
	return COM_OK;
}

/*
 * asetv
 *
 * Usage: asetv user instructions
 *
 *   This command executes "set" instructions as if they had been made by the
 *   user indicated.  For example, "asetv DAV shout 0" would set DAV's shout
 *   variable to 0.
 */
PUBLIC int
com_asetv(int p, param_list param)
{
	int	p1;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if ((p1 = player_find_part_login(param[0].val.word)) < 0) {
		pprintf(p, "%s is not logged in.\n", param[0].val.word);
		return COM_OK;
	}

	if ((parray[p].adminLevel <= parray[p1].adminLevel) &&
	    !player_ishead(p)) {
		pprintf(p, "You can only aset players below your adminlevel."
		    "\n");
		return COM_OK;
	}

	pprintf(p, "Command issued as %s\n", parray[p1].name);
	pcommand(p1, "set %s\n", param[1].val.string);
	return COM_OK;
}

/*
 * announce
 *
 * Usage: announce message
 *
 *   Broadcasts your message to all logged on users.  Announcements reach all
 *   users and cannot be censored in any way (such as by "set shout 0").
 */
PUBLIC int
com_announce(int p, param_list param)
{
	int	count = 0;
	int	p1;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (!printablestring(param[0].val.string)) {
		pprintf(p, "Your message contains some unprintable "
		    "character(s).\n");
		return COM_OK;
	}

	for (p1 = 0; p1 < p_num; p1++) {
		if (p1 == p)
			continue;
		if (parray[p1].status != PLAYER_PROMPT)
			continue;
		count++;
		pprintf_prompt(p1, "\n\n    **ANNOUNCEMENT** from %s: %s\n\n",
		    parray[p].name, param[0].val.string);
	}

	pprintf(p, "\n(%d) **ANNOUNCEMENT** from %s: %s\n\n", count,
	    parray[p].name, param[0].val.string);
	return COM_OK;
}

/*
 * annunreg
 *
 * Usage:  annunreg message
 *
 *   Broadcasts your message to all logged on unregistered users, and admins,
 *   too.  Announcements reach all unregistered users and admins and cannot be
 *   censored in any way (such as by "set shout 0").
 */
PUBLIC int
com_annunreg(int p, param_list param)
{
	int	count = 0;
	int	p1;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (!printablestring(param[0].val.string)) {
		pprintf(p, "Your message contains some unprintable "
		    "character(s).\n");
		return COM_OK;
	}

	for (p1 = 0; p1 < p_num; p1++) {
		if (p1 == p)
			continue;
		if (parray[p1].status != PLAYER_PROMPT)
			continue;
		if ((parray[p1].registered) &&
		    (parray[p1].adminLevel < ADMIN_ADMIN))
			continue;
		count++;
		pprintf_prompt(p1, "\n\n    **UNREG ANNOUNCEMENT** from %s: %s"
		    "\n\n", parray[p].name, param[0].val.string);
	}

	pprintf(p, "\n(%d) **UNREG ANNOUNCEMENT** from %s: %s\n\n", count,
	    parray[p].name, param[0].val.string);
	return COM_OK;
}

PUBLIC int
com_muzzle(int p, param_list param)
{
	pprintf(p, "Obsolete command: Please use +muzzle and -muzzle.\n");
	return COM_OK;
}

PUBLIC int
com_cmuzzle(int p, param_list param)
{
	pprintf(p, "Obsolete command: Please use +cmuzzle and -cmuzzle.\n");
	return COM_OK;
}

/*
 * asetpasswd
 *
 * Usage: asetpasswd player {password,*}
 *
 *   This command sets the password of the player to the password given.
 *   If '*' is specified then the player's account is locked, and no password
 *   will work until a new one is set by asetpasswd.
 *
 *   If the player is connected, he is told of the new password and the name
 *   of the admin who changed it, or likewise of his account status.  An
 *   email message is mailed to the player's email address as well.
 */
PUBLIC int
com_asetpasswd(int p, param_list param)
{
	char	 salt[FICS_SALT_SIZE];
	char	 subject[400];
	char	 text[10100];
	int	 p1, connected;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (!FindPlayer(p, param[0].val.word, &p1, &connected))
		return COM_OK;

	if ((parray[p].adminLevel <= parray[p1].adminLevel) && !player_ishead(p)) {
		pprintf(p, "You can only set password for players below your "
		    "adminlevel.\n");

		if (!connected)
			player_remove(p1);
		return COM_OK;
	}

	if (!parray[p1].registered) {
		pprintf(p, "You cannot set the password of an unregistered "
		    "player!\n");
		return COM_OK;
	}

	if (parray[p1].passwd)
		rfree(parray[p1].passwd);

	if (param[1].val.word[0] == '*') {
		parray[p1].passwd = xstrdup(param[1].val.word);
		pprintf(p, "Account %s locked!\n", parray[p1].name);
		sprintf(text, "Password of %s is now useless.  Your account at "
		    "our FICS has been locked.\n", parray[p1].name);
	} else {
		strcpy(salt, fics_getsalt());

		parray[p1].passwd = xstrdup(crypt(param[1].val.word, salt));

		sprintf(text, "Password of %s changed to \"%s\".\n",
		    parray[p1].name, param[1].val.word);
		pprintf(p, "%s", text);
	}

	if (param[1].val.word[0] == '*') {
		sprintf(subject, "FICS: %s has locked your account.",
		    parray[p].name);
		if (connected)
			pprintf_prompt(p1, "\n%s\n", subject);
	} else {
		sprintf(subject, "FICS: %s has changed your password.",
		    parray[p].name);
		if (connected)
			pprintf_prompt(p1, "\n%s\n", subject);
	}

	mail_string_to_address(parray[p1].emailAddress, subject, text);
	player_save(p1);

	if (!connected)
		player_remove(p1);
	return COM_OK;
}

/*
 * asetemail
 *
 * Usage: asetemail player [address]
 *
 *   Sets the email address of the player to the address given.  If the
 *   address is omited, then the player's email address is cleared.  The
 *   person's email address is revealed to them when they use the "finger"
 *   command, but no other users -- except admins -- will have another
 *   player's email address displayed.
 */
PUBLIC int
com_asetemail(int p, param_list param)
{
	int	p1, connected;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (!FindPlayer(p, param[0].val.word, &p1, &connected))
		return COM_OK;

	if ((parray[p].adminLevel <= parray[p1].adminLevel) &&
	    !player_ishead(p)) {
		pprintf(p, "You can only set email addr for players below "
		    "your adminlevel.\n");

		if (!connected)
			player_remove(p1);
		return COM_OK;
	}

	if (parray[p1].emailAddress)
		rfree(parray[p1].emailAddress);

	if (param[1].type == TYPE_NULL) {
		parray[p1].emailAddress = NULL;
		pprintf(p, "Email address for %s removed\n", parray[p1].name);
	} else {
		parray[p1].emailAddress = xstrdup(param[1].val.word);
		pprintf(p, "Email address of %s changed to \"%s\".\n",
		    parray[p1].name,
		    param[1].val.word);
	}

	player_save(p1);

	if (connected) {
		if (param[1].type == TYPE_NULL) {
			pprintf_prompt(p1, "\n\n%s has removed your email "
			    "address.\n\n", parray[p].name);
		} else {
			pprintf_prompt(p1, "\n\n%s has changed your email "
			    "address.\n\n", parray[p].name);
		}
	} else {
		player_remove(p1);
	}

	return COM_OK;
}

/*
 * asetrealname
 *
 * Usage:  asetrealname user newname
 *
 *   This command sets the user's real name (as displayed to admins on finger
 *   notes) to "newname".
 */
PUBLIC int
com_asetrealname(int p, param_list param)
{
	int	p1, connected;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (!FindPlayer(p, param[0].val.word, &p1, &connected))
		return COM_OK;

	if ((parray[p].adminLevel <= parray[p1].adminLevel) &&
	    !player_ishead(p)) {
		pprintf(p, "You can only set real names for players below your "
		    "adminlevel.\n");

		if (!connected)
			player_remove(p1);
		return COM_OK;
	}

	if (parray[p1].fullName)
		rfree(parray[p1].fullName);

	if (param[1].type == TYPE_NULL) {
		parray[p1].fullName = NULL;
		pprintf(p, "Real name for %s removed\n", parray[p1].name);
	} else {
		parray[p1].fullName = xstrdup(param[1].val.word);
		pprintf(p, "Real name of %s changed to \"%s\".\n",
		    parray[p1].name,
		    param[1].val.word);
	}

	player_save(p1);

	if (connected) {
		if (param[1].type == TYPE_NULL) {
			pprintf_prompt(p1, "\n\n%s has removed your real name."
			    "\n\n", parray[p].name);
		} else {
			pprintf_prompt(p1, "\n\n%s has changed your real name."
			    "\n\n", parray[p].name);
		}
	} else {
		player_remove(p1);
	}

	return COM_OK;
}

/*
 * asethandle
 *
 * Usage: asethandle oldname newname
 *
 *   This command changes the handle of the player from oldname to
 *   newname.  The various player information, messages, logins, comments
 *   and games should be automatically transferred to the new account.
 */
PUBLIC int
com_asethandle(int p, param_list param)
{
	char	*newplayer = param[1].val.word;
	char	*player = param[0].val.word;
	char	 newplayerlower[MAX_LOGIN_NAME] = { '\0' };
	char	 playerlower[MAX_LOGIN_NAME] = { '\0' };
	int	 p1;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	strcpy(playerlower, player);
	stolower(playerlower);
	strcpy(newplayerlower, newplayer);
	stolower(newplayerlower);

	if (player_find_bylogin(playerlower) >= 0) {
		pprintf(p, "A player by that name is logged in.\n");
		return COM_OK;
	}

	if (player_find_bylogin(newplayerlower) >= 0) {
		pprintf(p, "A player by that new name is logged in.\n");
		return COM_OK;
	}

	p1 = player_new();

	if (player_read(p1, playerlower)) {
		pprintf(p, "No player by the name %s is registered.\n", player);
		player_remove(p1);
		return COM_OK;
	} else {
		if ((parray[p].adminLevel <= parray[p1].adminLevel) &&
		    !player_ishead(p)) {
			pprintf(p, "You can't set handles for an admin with "
			    "a level higher than or equal to yourself.\n");
			player_remove(p1);
			return COM_OK;
		}
	}

	player_remove(p1);
	p1 = player_new();

	if ((!player_read(p1, newplayerlower)) &&
	    (strcmp(playerlower, newplayerlower))) {
		pprintf(p, "Sorry that handle is already taken.\n");
		player_remove(p1);
		return COM_OK;
	}

	player_remove(p1);

	if ((!player_rename(playerlower, newplayerlower)) &&
	    (!player_read(p1, newplayerlower))) {
		pprintf(p, "Player %s renamed to %s.\n", player, newplayer);
		strfree(parray[p1].name);
		parray[p1].name = xstrdup(newplayer);
		player_save(p1);

		if (parray[p1].s_stats.rating > 0) {
			UpdateRank(TYPE_STAND, newplayer, &parray[p1].s_stats,
			    player);
		}
		if (parray[p1].b_stats.rating > 0) {
			UpdateRank(TYPE_BLITZ, newplayer, &parray[p1].b_stats,
			    player);
		}
		if (parray[p1].w_stats.rating > 0) {
			UpdateRank(TYPE_WILD, newplayer, &parray[p1].w_stats,
			    player);
		}
	} else {
		pprintf(p, "Asethandle failed.\n");
	}

	player_remove(p1);
	return COM_OK;
}

/*
 * asetadmin
 *
 * Usage: asetadmin player AdminLevel
 *
 *   Sets the admin level of the player with the following restrictions.
 *   1. You can only set the admin level of players lower than yourself.
 *   2. You can only set the admin level to a level that is lower than
 *      yourself.
 */
PUBLIC int
com_asetadmin(int p, param_list param)
{
	int	p1, connected;

	ASSERT(parray[p].adminLevel >= ADMIN_GOD);

	if (!FindPlayer(p, param[0].val.word,&p1, &connected))
		return COM_OK;

	if ((parray[p].adminLevel <= parray[p1].adminLevel) &&
	    !player_ishead(p)) {
		pprintf(p, "You can only set adminlevel for players below your "
		    "adminlevel.\n");

		if (!connected)
			player_remove(p1);
		return COM_OK;
	}

	if ((parray[p1].login) == (parray[p].login)) {
		pprintf(p, "You can't change your own adminlevel.\n");
		return COM_OK;
	}

	if ((param[1].val.integer >= parray[p].adminLevel) &&
	    !player_ishead(p)) {
		pprintf(p, "You can't promote someone to or above your "
		    "adminlevel.\n");

		if (!connected)
			player_remove(p1);
		return COM_OK;
	}

	parray[p1].adminLevel = param[1].val.integer;
	pprintf(p, "Admin level of %s set to %d.\n",
	    parray[p1].name,
	    parray[p1].adminLevel);
	player_save(p1);

	if (connected) {
		pprintf_prompt(p1, "\n\n%s has set your admin level to %d.\n\n",
		    parray[p].name,
		    parray[p1].adminLevel);
	} else {
		player_remove(p1);
	}

	return COM_OK;
}

PRIVATE void
SetRating(int p1, param_list param, statistics *s)
{
	s->rating = param[1].val.integer;

	if (s->ltime == 0L)
		s->sterr = 70.0;

	if (param[2].type == TYPE_INT) {
		s->win = param[2].val.integer;
		if (param[3].type == TYPE_INT) {
			s->los = param[3].val.integer;
			if (param[4].type == TYPE_INT) {
				s->dra = param[4].val.integer;
				if (param[5].type == TYPE_INT) {
					s->sterr = (double) param[5].val.integer;
				}
			}
		}
	}

	s->num = s->win + s->los + s->dra;

	if (s->num == 0) {
		s->ltime = 0L;
	} else {
		s->ltime = time(NULL);
	}
}

/*
 * asetblitz
 *
 * Usage: asetblitz handle rating won lost drew RD
 *
 *   This command allows admins to set a user's statistics for Blitz games.
 *   The parameters are self-explanatory: rating, # of wins, # of losses,
 *   # of draws, and ratings deviation.
 */
PUBLIC int
com_asetblitz(int p, param_list param)
{
	int	p1, connected;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (!FindPlayer(p, param[0].val.word, &p1, &connected))
		return COM_OK;

	SetRating(p1, param, &parray[p1].b_stats);
	player_save(p1);
	UpdateRank(TYPE_BLITZ, parray[p1].name, &parray[p1].b_stats,
	    parray[p1].name);
	pprintf(p, "Blitz rating for %s modified.\n", parray[p1].name);

	if (!connected)
		player_remove(p1);
	return COM_OK;
}

/*
 * asetwild
 *
 * Usage: asetwild handle rating won lost drew RD
 *
 *   This command allows admins to set a user's statistics for Wild games.
 *   The parameters are self-explanatory: rating, # of wins, # of losses,
 *   # of draws, and ratings deviation.
 */
PUBLIC int
com_asetwild(int p, param_list param)
{
	int	p1, connected;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (!FindPlayer(p, param[0].val.word, &p1, &connected))
		return COM_OK;

	SetRating(p1, param, &parray[p1].w_stats);
	player_save(p1);
	UpdateRank(TYPE_WILD, parray[p1].name, &parray[p1].w_stats,
	    parray[p1].name);
	pprintf(p, "Wild rating for %s modified.\n", parray[p1].name);

	if (!connected)
		player_remove(p1);
	return COM_OK;
}

/*
 * asetstd
 *
 * Usage: asetstd handle rating won lost drew RD
 *
 *   This command allows admins to set a user's statistics for Standard games.
 *   The parameters are self-explanatory: rating, # of wins, # of losses, # of
 *   draws, and ratings deviation.
 */
PUBLIC int
com_asetstd(int p, param_list param)
{
	int	p1, connected;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (!FindPlayer(p, param[0].val.word, &p1, &connected))
		return COM_OK;

	SetRating(p1, param, &parray[p1].s_stats);
	player_save(p1);
	UpdateRank(TYPE_STAND, parray[p1].name, &parray[p1].s_stats,
	    parray[p1].name);
	pprintf(p, "Standard rating for %s modified.\n", parray[p1].name);

	if (!connected)
		player_remove(p1);
	return COM_OK;
}

/*
 * asetlight
 *
 * Usage: asetlight handle rating won lost drew RD
 *
 *   This command allows admins to set a user's statistics for Lightning games.
 *   The parameters are self-explanatory: rating, # of wins, # of losses, # of
 *   draws, and ratings deviation.
 */
PUBLIC int
com_asetlight(int p, param_list param)
{
	int	p1, connected;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (!FindPlayer(p, param[0].val.word, &p1, &connected))
		return COM_OK;

	SetRating(p1, param, &parray[p1].l_stats);
	player_save(p1);
	pprintf(p, "Lightning rating for %s modified.\n", parray[p1].name);

	if (!connected)
		player_remove(p1);
	return COM_OK;
}

/*
 * nuke
 *
 * Usage: nuke user
 *
 *   This command disconnects the user from the server.  The user is informed
 *   that she/he has been nuked by the admin named and a comment is
 *   automatically placed in the user's files (if she/he is a registered
 *   user, of course).
 */
PUBLIC int
com_nuke(int p, param_list param)
{
	int	p1, fd;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if ((p1 = player_find_part_login(param[0].val.word)) < 0) {
		pprintf(p, "%s isn't logged in.\n", param[0].val.word);
	} else {
		if ((parray[p].adminLevel > parray[p1].adminLevel) ||
		    player_ishead(p)) {
			pprintf(p, "Nuking: %s\n", param[0].val.word);
			pprintf(p, "Please leave a comment explaining why %s "
			    "was nuked.\n", parray[p1].name);
			pprintf(p1, "\n\n**** You have been kicked out by %s! "
			    "****\n\n", parray[p].name);
			pcommand(p, "addcomment %s Nuked\n", parray[p1].name);
			fd = parray[p1].socket;
			process_disconnection(fd);
			net_close_connection(fd);
			return COM_OK;
		} else {
			pprintf(p, "You need a higher adminlevel to nuke %s!\n",
			    param[0].val.word);
		}
	}
	return COM_OK;
}

/*
 * summon
 *
 * Usage: summon player
 *
 *   This command gives a beep and a message to the player indicating that you
 *   want to talk with him/her.  The command is useful for waking someone up,
 *   for example a sleepy admin or an ignorant player.
 */
PUBLIC int
com_summon(int p, param_list param)
{
	int	p1;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if ((p1 = player_find_part_login(param[0].val.word)) < 0) {
		pprintf(p, "%s isn't logged in.\n", param[0].val.word);
		return COM_OK;
	} else {
		pprintf(p1, "\a\n");
		pprintf_highlight(p1, "%s", parray[p].name);
		pprintf_prompt(p1, " needs to talk with you. Use tell %s "
		    "<message> to reply.\a\n", parray[p].name);
		pprintf(p, "Summoning sent to %s.\n", parray[p1].name);
		return COM_OK;
	}
}

/*
 * addcomment
 *
 * Usage: addcomment user comment
 *
 *   Places "comment" in the user's comments.  If a user has comments, the
 *   number of comments is indicated to admins using the "finger" command.
 *   The comments themselves are displayed by the "showcomments" command.
 */
PUBLIC int
com_addcomment(int p, param_list param)
{
	int	p1, connected;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (!FindPlayer(p, param[0].val.word, &p1, &connected))
		return COM_OK;
	if (player_add_comment(p, p1, param[1].val.string)) {
		pprintf(p, "Error adding comment!\n");
	} else {
		pprintf(p, "Comment added for %s.\n", parray[p1].name);
		player_save(p1);
	}

	if (!connected)
		player_remove(p1);
	return COM_OK;
}

/*
 * showcomment
 *
 * Usage: showcomment user
 *
 *   This command will display all of the comments added to the user's account.
 */
PUBLIC int
com_showcomment(int p, param_list param)
{
	int	p1, connected;

	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);
	ASSERT(param[0].type == TYPE_WORD);

	if (!FindPlayer(p, param[0].val.word, &p1, &connected))
		return COM_OK;

	player_show_comments(p, p1);

	if (!connected)
		player_remove(p1);
	return COM_OK;
}

/*
 * admin
 *
 * Usage: admin
 *
 *   This command toggles your admin symbol (*) on/off.  This symbol appears
 *   in who listings.
 */
PUBLIC int
com_admin(int p, param_list param)
{
	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	parray[p].i_admin = !(parray[p].i_admin);

	if (parray[p].i_admin) {
		pprintf(p, "Admin mode (*) is now shown\n");
	} else {
		pprintf(p, "Admin mode (*) is now not shown\n");
	}

	return COM_OK;
}

/*
 * quota
 *
 * Usage: quota [n]
 *
 *   The command sets the number of seconds (n) for the shout quota, which
 *   affects only those persons on the shout quota list.  If no parameter
 *   (n) is given, the current setting is displayed.
 */
PUBLIC int
com_quota(int p, param_list param)
{
	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (param[0].type == TYPE_NULL) {
		pprintf(p, "The current shout quota is 2 shouts per %d "
		    "seconds.\n", quota_time);
		return COM_OK;
	}

	quota_time = param[0].val.integer;

	pprintf(p, "The shout quota is now 2 shouts per %d seconds.\n",
	    quota_time);
	return COM_OK;
}

/*
 * asetmaxplayer
 *
 * Usage: asetmaxplayer [n]
 *
 *   The command sets the maximum number of players (n) who can connect.
 */
PUBLIC int
com_asetmaxplayer(int p, param_list param)
{
	ASSERT(parray[p].adminLevel >= ADMIN_ADMIN);

	if (param[0].type != TYPE_NULL) {
		pprintf(p, "Previously %d total conenctions allowed...\n",
		    max_connections);
		max_connections = param[0].val.integer;

		if ((max_connections > MAX_PLAYER) ||
		    (max_connections > getdtablesize() - 4)) {
			max_connections = MIN(MAX_PLAYER, getdtablesize() - 4);
			pprintf(p, "Value too high. System OS limits us to "
			    "%d.\n", max_connections);
			pprintf(p, "For saftey's sake, it should not be "
			    "higher than %d.\n", max_connections - 2);
		}
	}

	pprintf(p, "There are currently %d regular and %d admin connections "
	    "available,\n", max_connections - 10, 10);
	pprintf(p, "with %d maximum logins before unregistered login "
	    "restrictions begin.\n", MAX(max_connections - 50, 200));

	pprintf(p, "Total allowed connections: %d.\n", max_connections);
	return COM_OK;
}