File: hook.c

package info (click to toggle)
ircii-pana 1%3A1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,048 kB
  • ctags: 14,567
  • sloc: ansic: 130,654; sql: 6,041; makefile: 4,313; cpp: 1,270; tcl: 1,230; sh: 638; java: 151
file content (1858 lines) | stat: -rw-r--r-- 42,746 bytes parent folder | download | duplicates (2)
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
/*
 * hook.c: Does those naughty hook functions. 
 *
 * Written By Michael Sandrof
 * Rewritten by Jeremy Nelson and then rewritten to use
 * hash'd lists by Colten Edwards
 * Copyright(c) 1997 
 */

#define __hook_c
#include "irc.h"
static char cvsrevision[] = "$Id: hook.c,v 1.1.1.1 2003/04/11 01:09:07 dan Exp $";
CVS_REVISION(hook_c)
#include "struct.h"

#include "hook.h"
#include "vars.h"
#include "ircaux.h"
#include "if.h"
#include "alias.h"
#include "list.h"
#include "window.h"
#include "server.h"
#include "output.h"
#include "commands.h"
#include "parse.h"
#include "misc.h"
#include "stack.h"
#ifdef WANT_TCL
#include "tcl_bx.h"
#endif
#define MAIN_SOURCE
#include "modval.h"

#include <stdarg.h>

/*
 * The various ON levels: SILENT means the DISPLAY will be OFF and it will
 * suppress the default action of the event, QUIET means the display will be
 * OFF but the default action will still take place, NORMAL means you will be
 * notified when an action takes place and the default action still occurs,
 * NOISY means you are notified when an action occur plus you see the action
 * in the display and the default actions still occurs 
 */
static	char	*noise_level[] = { "UNKNOWN", "SILENT", "QUIET", "NORMAL", "NOISY" };

#define HF_NORECURSE	0x0001
#define HF_DEBUG	0x0010


/*
 * NumericList: a special list type to dynamically handle numeric hook
 * requests 
 */
typedef struct numericlist_stru
{
struct	numericlist_stru *next;
	int	numeric;
	char	name[4];
	Hook	*list;
	char	*filename;
} NumericList;

NumericList *numeric_list 	= NULL;

#ifdef WANT_DLL
NumericList *dll_numeric_list	= NULL;
#endif

/* hook_functions: the list of all hook functions available */
HookFunc hook_functions[] =
{
	{ "ACTION",		NULL,	3,	0,	0 },
	{ "BANS",		NULL,	4,	0,	0 },
	{ "BANS_FOOTER",	NULL,	2,	0,	0 },
	{ "BANS_HEADER",	NULL,	4,	0,	0 },

	{ "CDCC_NOTE",		NULL,	3,	0,	0 },
	{ "CDCC_PACK",		NULL,	7,	0,	0 },
	{ "CDCC_POSTPACK",	NULL,	8,	0,	0 },
	{ "CDCC_PREPACK",	NULL,	14,	0,	0 },
	{ "CDCC_QUEUE",		NULL,	5,	0,	0 },
	{ "CDCC_QUEUEH",	NULL,	3,	0,	0 },
	{ "CDCC_SEND_NICK",	NULL,	3,	0,	0 },
	{ "CDCC_STATS",		NULL,	1,	0,	0 },
	
	{ "CHANOP",		NULL,	1,	0,	0 },	
	{ "CHANNEL_NICK",	NULL,	3,	0,	0 },
	{ "CHANNEL_SIGNOFF",	NULL,	3,	0,	0 },
	{ "CHANNEL_STATS",	NULL,	32,	0,	0 },
	{ "CHANNEL_SWITCH",	NULL,	1,	0,	0 },
	{ "CHANNEL_SYNCH",	NULL,	2,	0,	0 },
	{ "CLONE",		NULL,	2,	0,	0 },
	{ "CONNECT",		NULL,	1,	0,	0 },
	{ "CTCP",		NULL,	4,	0,	0 },
	{ "CTCP_REPLY",		NULL,	3,	0,	0 },
	{ "DCC_CHAT",		NULL,	2,	0,	0 },
	{ "DCC_CONNECT",        NULL,	2,      0,      0 },
	{ "DCC_ERROR",          NULL,	6,      0,      0 },
	{ "DCC_HEADER",		NULL,	7,	0,	0 },
	{ "DCC_LOST",           NULL,   2,      0,      0 },
	{ "DCC_OFFER",		NULL,	1,	0,	0 },
	{ "DCC_POST",		NULL,	7,	0,	0 },
	{ "DCC_RAW",		NULL,	3,	0,	0 },
	{ "DCC_REQUEST",        NULL,   4,      0,      0 },
	{ "DCC_STAT",		NULL,	7,	0,	0 },
	{ "DCC_STATF",		NULL,	7,	0,	0 },
	{ "DCC_STATF1",		NULL,	5,	0,	0 },
	{ "DCC_TRANSFER_STAT",	NULL,	13,	0,	0 },
	{ "DCC_UPDATE",		NULL,	1,	0,	0 },
	{ "DEBUG",		NULL,	1,	0,	0 },
	{ "DESYNC_MESSAGE",	NULL,	2,	0,	0 },
	{ "DISCONNECT",		NULL,	1,	0,	0 },
	{ "EBANS",		NULL,	4,	0,	0 },
	{ "EBANS_FOOTER",	NULL,	2,	0,	0 },
	{ "EBANS_HEADER",	NULL,	4,	0,	0 },
	{ "ENCRYPTED_NOTICE",   NULL,   3,      0,      0 },
	{ "ENCRYPTED_PRIVMSG",  NULL,   3,      0,      0 },
	{ "EXEC",		NULL,	2,	0,	0 },
	{ "EXEC_ERRORS",	NULL,	2,	0,	0 },
	{ "EXEC_EXIT",		NULL,	2,	0,	0 },
	{ "EXEC_PROMPT",	NULL,	2,	0,	0 },
	{ "EXIT",               NULL,   1,      0,      0 },
	{ "FLOOD",		NULL,	4,	0,	0 },
	{ "FTP",		NULL,	1,	0,	0 },
	{ "HELP",		NULL,	2,	0,	0 },
	{ "HELPSUBJECT",	NULL,	2,	0,	0 },
	{ "HELPTOPIC",		NULL,	1,	0,	0 }, 
	{ "HOOK",		NULL,	1,	0,	0 },
	{ "IDLE",		NULL,	1,	0,	0 },
	{ "INPUT",		NULL,	1,	0,	0 },
	{ "INVITE",		NULL,	2,	0,	0 },
	{ "JOIN",		NULL,	3,	0,	0 },
	{ "JOIN_ME",		NULL,	1,	0,	0 },
	{ "KICK",		NULL,	3,	0,	0 },
	{ "LEAVE",		NULL,	2,	0,	0 },
	{ "LEAVE_ME",		NULL,	1,	0,	0 },
	{ "LIST",		NULL,	3,	0,	0 },
	{ "LLOOK_ADDED",	NULL,	2,	0,	0 },
	{ "LLOOK_JOIN",		NULL,	2,	0,	0 },
	{ "LLOOK_SPLIT",	NULL,	2,	0,	0 },
	{ "MAIL",		NULL,	2,	0,	0 },
	{ "MODE",		NULL,	3,	0,	0 },
	{ "MODE_STRIPPED",	NULL,	3,	0,	0 },
	{ "MODULE",		NULL,	1,	0,	0 },
	{ "MSG",		NULL,	2,	0,	0 },
	{ "MSG_GROUP",		NULL,	3,	0,	0 },
	{ "MSGLOG",		NULL,	4,	0,	0 },
	{ "NAMES",		NULL,	2,	0,	0 },
	{ "NETSPLIT",		NULL,	1,	0,	0 },
	{ "NICK_COMP",		NULL,	1,	0,	0 },
	{ "NICKNAME",		NULL,	2,	0,	0 },
	{ "NOTE",		NULL,	3,	0,	0 },
	{ "NOTICE",		NULL,	2,	0,	0 },
	{ "NOTIFY",		NULL,	2,	0,	0 },
	{ "NOTIFY_HEADER",	NULL,	2,	0,	0 },
	{ "NOTIFY_SIGNOFF",	NULL,	1,	0,	0 },
	{ "NOTIFY_SIGNON",	NULL,	1,	0,	0 },
	{ "NSLOOKUP",		NULL,	3,	0,	0 },
	{ "ODD_SERVER_STUFF",	NULL,	3,	0,	0 },
	{ "PASTE",		NULL,	2,	0,	0 },
	{ "PUBLIC",		NULL,	3,	0,	0 },
	{ "PUBLIC_AR",		NULL,	3,	0,	0 },
	{ "PUBLIC_MSG",		NULL,	3,	0,	0 },
	{ "PUBLIC_NOTICE",	NULL,	3,	0,	0 },
	{ "PUBLIC_OTHER",	NULL,	3,	0,	0 },
	{ "PUBLIC_OTHER_AR",	NULL,	3,	0,	0 },

	{ "RAW_IRC",		NULL,	1,	0,	0 },
	{ "REDIRECT",		NULL,	2,	0,	0 },
	{ "REPLY_AR",		NULL,	1,	0,	0 },
	{ "SAVEFILE",		NULL,	2,	0,	0 },
	{ "SAVEFILEPOST",	NULL,	2,	0,	0 },
	{ "SAVEFILEPRE",	NULL,	2,	0,	0 },
	{ "SEND_ACTION",	NULL,	2,	0,	HF_NORECURSE },
	{ "SEND_CTCP",		NULL,	3,	0,	HF_NORECURSE },
	{ "SEND_DCC_CHAT",	NULL,	2,	0,	HF_NORECURSE },
	{ "SEND_MSG",		NULL,	2,	0,	HF_NORECURSE },
	{ "SEND_NOTICE",	NULL,	2,	0,	HF_NORECURSE },
	{ "SEND_PUBLIC",	NULL,	2,	0,	HF_NORECURSE },
	{ "SEND_TO_SERVER",	NULL,	3,	0,	0 },
	{ "SERVER_NOTICE_FAKES",NULL,	3,	0,	0 },
	{ "SERVER_NOTICE_FAKES_MYCHANNEL",NULL,3,0,	0 },
	{ "SERVER_NOTICE_FOREIGN_KILL", NULL, 4, 0, 	0 },
	{ "SERVER_NOTICE_KILL",	NULL,	4,	0,	0 },
	{ "SERVER_NOTICE",	NULL,	1,	0,	0 },
	{ "SERVER_NOTICE_LOCAL_KILL",NULL,4,	0,	0 },
	{ "SERVER_NOTICE_SERVER_KILL",NULL, 4,	0,	0 },
	{ "SET",		NULL,	2,	0,	0 },
	{ "SHITLIST",		NULL,	6,	0,	0 },
	{ "SHITLIST_FOOTER",	NULL,	1,	0,	0 },
	{ "SHITLIST_HEADER",	NULL,	6,	0,	0 },
	{ "SHOWIDLE_FOOTER",	NULL,	1,	0,	0 },
	{ "SHOWIDLE_HEADER",	NULL,	2,	0,	0 },
	{ "SHOWIDLE",		NULL,	4,	0,	0 },
	{ "SIGNOFF",		NULL,	1,	0,	0 },
	{ "SILENCE",		NULL,	2,	0,	0 },
	{ "SOCKET",		NULL,	3,	0,	0 },
	{ "SOCKET_NOTIFY",	NULL,	3,	0,	0 },
	{ "STAT",		NULL,	5,	0,	0 },
	{ "STAT_FOOTER",	NULL,	1,	0,	0 },
	{ "STAT_HEADER",	NULL,	5,	0,	0 },
	{ "STATUS_UPDATE",	NULL,	2,	0,	0 },
	{ "SWITCH_CHANNELS",	NULL,	3,	0,	0 },
	{ "TIMER",		NULL,	1,	0,	0 },
	{ "TIMER_HOUR",		NULL,	1,	0,	0 },
	{ "TOPIC",		NULL,	2,	0,	0 },
	{ "URLGRAB",		NULL,	1,	0,	0 },
	{ "USAGE",		NULL,	2,	0,	0 },
	{ "USERLIST",		NULL,	4,	0,	0 },
	{ "USERLIST_FOOTER",	NULL,	1,	0,	0 },
	{ "USERLIST_HEADER",	NULL,	5,	0,	0 },
	{ "USERS",		NULL,	5,	0,	0 },
	{ "USERS_FOOTER",	NULL,	1,	0,	0 },
	{ "USERS_HEADER",	NULL,	5,	0,	0 },
	{ "USERS_IP",		NULL,	3,	0,	0 },
	{ "USERS_SERVER",	NULL,	2,	0,	0 },
	{ "USERS_SERVER_HEADER",NULL,	2,	0,	0 },
	{ "WALL",		NULL,	2,	0,	0 },
	{ "WALLOP",		NULL,	3,	0,	0 },
	{ "WATCH",		NULL,	3,	0,	0 },
	{ "WHO",		NULL,	6,	0,	0 },
	{ "WHOLEFT",		NULL,	6,	0,	0 },
	{ "WHOLEFT_FOOTER",	NULL,	1,	0,	0 },
	{ "WHOLEFT_HEADER",	NULL,	6,	0,	0 },
	{ "WIDELIST",		NULL,	1,	0,	0 },
	{ "WINDOW",		NULL,	2,	0,	HF_NORECURSE },
	{ "WINDOW_CREATE",	NULL,	1,	0,	0 },
	{ "WINDOW_FOCUS",	NULL,	4,	0,	0 },
	{ "WINDOW_KILL",	NULL,	1,	0,	0 },
	{ "WINDOW_SWAP",	NULL,	1,	0,	0 },
	{ "YELL",		NULL,	1,	0,	0 }
};

	void 	    hook_add_to_list 	(Hook **list, Hook *item);
static Hook *	    hook_remove_from_list 	(Hook **list, char *item, int sernum);
static void 	    add_numeric_list 	(NumericList *item);
static NumericList *find_numeric_list 	(int numeric);
static NumericList *remove_numeric_list (int numeric);

char *current_package(void) { return empty_string; }

/* 
 * name of the current hook that's running. a hook within a hook, might
 * screw this up though.
 */
char hook_name[BIG_BUFFER_SIZE+1] = {0};
extern int last_function_call_level;


/*
 * This converts a user-specified string of unknown composition and
 * returns a string that contains at minimum "params" number of words 
 * in it.  For any words that are forcibly added on, the last word will
 * be a * (so it can match any number of words), and any previous words
 * will be a % (so it forces the correct number of words to be caught.)
 */
static char *	fill_it_out (char *str, int params)
{
	char	buffer[BIG_BUFFER_SIZE + 1];
	char	*arg,
		*ptr;
	int	i = 0;

	ptr = LOCAL_COPY(str);
	*buffer = 0;

	while ((arg = next_arg(ptr, &ptr)) != NULL)
	{
		if (*buffer)
			strmcat(buffer, space, BIG_BUFFER_SIZE);
		strmcat(buffer, arg, BIG_BUFFER_SIZE);
		if (++i == params)
			break;
	}

	for (; i < params; i++)
		strmcat(buffer, (i < params-1) ? " %" : " *", BIG_BUFFER_SIZE);

	if (*ptr)
	{
		strmcat(buffer, space, BIG_BUFFER_SIZE);
		strmcat(buffer, ptr, BIG_BUFFER_SIZE);
	}
	return m_strdup(buffer);
}


#define INVALID_HOOKNUM -1001

/*
 * find_hook: returns the numerical value for a specified hook name
 */
static int 	find_hook (char *name, int *first)
{
	int which = INVALID_HOOKNUM, i, len, cnt;

	if (first)
		*first = -1;

	if (!name || !(len = strlen(name)))
	{
		say("You must specify an event type!");
		return INVALID_HOOKNUM;
	}

	upper(name);

	for (cnt = 0, i = 0; i < NUMBER_OF_LISTS; i++)
	{
		if (!strncmp(name, hook_functions[i].name, len))
		{
			if (first && *first == -1)
				*first = i;

			if (strlen(hook_functions[i].name) == len)
			{
				cnt = 1;
				which = i;
				break;
			}
			else
			{
				cnt++;
				which = i;
			}
		}
		else if (cnt)
			break;
	}

	if (cnt == 0)
	{
		if (is_number(name))
		{
			which = atol(name);

			if ((which < 0) || (which > 999))
			{
				say("Numerics must be between 001 and 999");
				return INVALID_HOOKNUM;
			}
			which = -which;
		}
		else
		{
			say("No such ON function: %s", name);
			return INVALID_HOOKNUM;
		}
	}
	else if (cnt > 1)
	{
		say("Ambiguous ON function: %s", name);
		return INVALID_HOOKNUM;
	}

	return which;
}




/* * * * * ADDING A HOOK * * * * * */
/*
 * This adds a numeric hook to the numeric hook list.  The composition of that
 * list is a linked list of "numeric holders" which simply indicate the 
 * numeric of that list, and a pointer to the actual list itself.  The actual
 * list itself is just a linked list of events sorted by serial number, then
 * by "nick".
 */
static void add_numeric_hook (int numeric, char *nick, char *stuff, Noise noisy, int not, int sernum, int flexible)
{
	NumericList *entry;
	Hook	*new;

	if (!(entry = find_numeric_list(numeric)))
	{
		entry = (NumericList *) new_malloc(sizeof(NumericList));
		entry->numeric = numeric;
		sprintf(entry->name, "%3.3u", numeric);
		entry->next = NULL;
		entry->list = NULL;
		add_numeric_list(entry);
	}

	if (!(new = hook_remove_from_list(&entry->list, nick, sernum)))
	{
		new = (Hook *)new_malloc(sizeof(Hook));
		new->nick = NULL;
		new->stuff = NULL;
	}

	malloc_strcpy(&new->nick, nick);
	malloc_strcpy(&new->stuff, stuff);
	new->noisy = noisy;
	new->not = not;
	new->sernum = sernum;
	new->flexible = flexible;
	new->global = loading_global;
	malloc_strcpy(&new->filename, current_package());
	new->next = NULL;

	upper(new->nick);
	hook_add_to_list(&entry->list, new);
}

#ifdef WANT_DLL
static void add_numeric_dll_hook (int numeric, Noise noise, int serial, char *nick, char *package, int (*func)(int, char *, char **))
{
	NumericList *entry;
	Hook	*new;

	if (!(entry = find_numeric_list(numeric)))
	{
		entry = (NumericList *) new_malloc(sizeof(NumericList));
		entry->numeric = numeric;
		sprintf(entry->name, "%3.3u", numeric);
		entry->next = NULL;
		entry->list = NULL;
		add_numeric_list(entry);
	}
	new = (Hook *)new_malloc(sizeof(Hook));
	malloc_strcpy(&new->nick, nick);
	malloc_strcpy(&new->filename, package);
	new->num_func = func;
	new->noisy = noise;
	new->sernum = serial;
	upper(new->nick);
	hook_add_to_list(&entry->list, new);
}

void add_dll_hook(int which, Noise noise, char *nick, char *package, int (*func1)(int, char *, char **), int (*func2)(char *, char *, char **))
{
Hook *new;
static int serial = 0;
	serial++;
	nick = fill_it_out(nick, which < 0 ? 1 : hook_functions[which].params);
	if (which < 0)
	{
		add_numeric_dll_hook(which, noise, serial, nick, package, func1);
		return;
	}
	new = (Hook *)new_malloc(sizeof(Hook));
	malloc_strcpy(&new->nick, nick);
	malloc_strcpy(&new->filename, package);
	new->num_func = func1;
	new->hook_func = func2;
	new->noisy = noise;
	new->sernum = serial;
	upper(new->nick);
	hook_add_to_list(&hook_functions[which].list, new);
}
#endif

/*
 * add_hook: Given an index into the hook_functions array, this adds a new
 * entry to the list as specified by the rest of the parameters.  The new
 * entry is added in alphabetical order (by nick). 
 */
static void add_hook (int which, char *nick, char *stuff, Noise noisy, int not, int sernum, int flexible)
{
	Hook	*new;

	if (which < 0)
	{
		add_numeric_hook(-which, nick, stuff, noisy, 
					not, sernum, flexible);
		return;
	}

	if (!(new = hook_remove_from_list(&hook_functions[which].list, nick, sernum)))
	{
		new = (Hook *)new_malloc(sizeof(Hook));
		new->nick = NULL;
		new->stuff = NULL;
	}

	malloc_strcpy(&new->nick, nick);
	malloc_strcpy(&new->stuff, stuff);
	new->noisy = noisy;
	new->not = not;
	new->sernum = sernum;
	new->flexible = flexible;
	new->global = loading_global;
	malloc_strcpy(&new->filename, current_package());
	new->next = NULL;

	upper(new->nick);
	new->debug = (hook_functions[which].flags & HF_DEBUG) ? HF_DEBUG : 0;
	hook_add_to_list(&hook_functions[which].list, new);
}




/* * * * * * REMOVING A HOOK * * * * * * * */
static void remove_numeric_hook (int numeric, char *nick, int sernum, int quiet)
{
	NumericList *hook;
	Hook	*tmp,
		*next;

	if ((hook = find_numeric_list(numeric)))
	{
		if (nick)
		{
			if ((tmp = hook_remove_from_list(&hook->list, nick, sernum)))
			{
				if (!quiet)
				{
					say("%c%s%c removed from %d list",
					    (tmp->flexible?'\'':'"'), nick, 
					    (tmp->flexible?'\'':'"'), numeric);
				}
				new_free(&(tmp->nick));
				new_free(&tmp->filename);
				new_free(&(tmp->stuff));
				new_free((char **)&tmp);

				if (!hook->list)
				{
					if ((hook = remove_numeric_list(numeric)))
						new_free((char **)&hook);
				}
				return;
			}
		}
		else
		{
			remove_numeric_list(numeric);
			for (tmp = hook->list; tmp; tmp = next)
			{
				next = tmp->next;
				tmp->not = 1;
				new_free(&(tmp->nick));
				new_free(&(tmp->stuff));
				new_free(&tmp->filename);
				new_free((char **)&tmp);
			}
			hook->list = NULL;
			new_free((char **)&hook);
			if (!quiet)
				say("The %d list is empty", numeric);
			return;
		}
	}

	if (quiet)
		return;

	if (nick)
		say("\"%s\" is not on the %d list", nick, numeric);
	else
		say("The %d list is empty", numeric);
}
 

static void remove_hook (int which, char *nick, int sernum, int quiet)
{
	Hook	*tmp,
		*next;

	if (which < 0)
	{
		remove_numeric_hook(-which, nick, sernum, quiet);
		return;
	}
	if (nick)
	{
		if ((tmp = hook_remove_from_list(&hook_functions[which].list, nick, sernum)))
		{
			if (!quiet)
				say("%c%s%c removed from %s list", 
					(tmp->flexible?'\'':'"'), nick,
					(tmp->flexible?'\'':'"'),
					hook_functions[which].name);

			new_free(&(tmp->nick));
			new_free(&(tmp->stuff));
			new_free(&tmp->filename);
			new_free((char **)&tmp); /* XXX why? */
		}
		else if (!quiet)
			say("\"%s\" is not on the %s list", nick,
					hook_functions[which].name);
	}
	else
	{
		Hook *prev = NULL;
		Hook *top = NULL;

		for (tmp = hook_functions[which].list; tmp; prev=tmp, tmp=next)
		{
			next = tmp->next;

			/* 
			 * If given a non-zero sernum, then we clean out
			 * only those hooks that are at that level.
			 */
			if (sernum && tmp->sernum != sernum)
			{
				if (!top)
					top = tmp;
				continue;
			}

			if (prev)
				prev->next = tmp->next;
			tmp->not = 1;
			new_free(&(tmp->nick));
			new_free(&(tmp->stuff));
			new_free(&tmp->filename);
			new_free((char **)&tmp);
		}
		hook_functions[which].list = top;
		if (!quiet)
		{
			if (sernum)
				say("The %s <%d> list is empty", hook_functions[which].name, sernum);
			else
				say("The %s list is empty", hook_functions[which].name);
		}
	}
}

/* Used to bulk-erase all of the currently scheduled ONs */
void    flush_on_hooks (void)
{
        int x;
        int old_display = window_display;
        
        window_display = 0;
        for (x = 1; x < 999; x++)
		remove_numeric_hook(x, NULL, x, 1);
        for (x = 0; x < NUMBER_OF_LISTS; x++)
		remove_hook(x, NULL, 0, 1); 
        window_display = old_display;
}

void	unload_on_hooks (char *filename)
{
	int		x;
	NumericList 	*tmp;
	Hook		*list, *next;

	int old_display = window_display;
	window_display = 0;

	for (x = 1; x < 999; x++)
	{
	    if ((tmp = find_numeric_list(x)))
	    {
		for (list = tmp->list; list; list = next)
		{
		    next = list->next;
		    if (!strcmp(list->filename, filename))
			remove_numeric_hook(x, list->nick, list->sernum, 1);
		}
	    }
	}

	for (x = 0; x < NUMBER_OF_LISTS; x++)
	{
		for (list = hook_functions[x].list; list; list = next)
		{
		    next = list->next;
		    if (!strcmp(list->filename, filename))
			remove_hook(x, list->nick, list->sernum, 1);
		}
	}

	window_display = old_display;
}

void debug_hook(char *name, int x)
{
int cnt = 0, i;

	for (cnt = 0, i = 0; i < NUMBER_OF_LISTS; i++)
	{
		if (!strcmp(name, hook_functions[i].name))
		{
			Hook *tmp;
			if (x)
				hook_functions[i].flags |= HF_DEBUG;
			else
				hook_functions[i].flags &= ~(HF_DEBUG);
			for (tmp = hook_functions[i].list; tmp; tmp = tmp->next)
			{
				if (x)
					tmp->debug |= HF_DEBUG;
				else
					tmp->debug &= ~(HF_DEBUG);
				cnt++;
			}
			break;
		}
	}

	if (cnt == 0)
	{
		int which;
		if (is_number(name))
		{
			NumericList *tmp;
			which = atol(name);

			if ((which < 0) || (which > 999))
				return;
			
			if ((tmp = find_numeric_list(which)))
			{
				Hook *list;
				for (list = tmp->list; list; list = list->next, cnt++)
				{
					if (x)
						list->debug |= HF_DEBUG;
					else
						list->debug &= ~(HF_DEBUG);
					cnt++;
				}
			}
		}
	}
}


/* * * * * * SHOWING A HOOK * * * * * * */
/* show_hook shows a single hook */
static void 	show_hook (Hook *list, char *name)
{
char *hooks = fget_string_var(FORMAT_HOOK_FSET);
	if (hooks)
		put_it("%s",
			convert_output_format(hooks, 
				"%s %s %c %s %c %s %s %d",
				    list->filename[0] ? list->filename : "*",
				    name,
				    (list->flexible?'\'':'"'), list->nick, 
				    (list->flexible?'\'':'"'), 
				    (list->not ? "nothing" : list->stuff ? list->stuff:"DLL"),
				    noise_level[list->noisy],
				    list->sernum));
	else
		say("[%s] On %s from %c%s%c do %s [%s] <%d>",
		    list->filename[0] ? list->filename : "*",
		    name,
		    (list->flexible?'\'':'"'), list->nick, 
		    (list->flexible?'\'':'"'), 
		    (list->not ? "nothing" : list->stuff ? list->stuff:"DLL"),
		    noise_level[list->noisy],
		    list->sernum);
}

/*
 * show_numeric_list: If numeric is 0, then all numeric lists are displayed.
 * If numeric is non-zero, then that particular list is displayed.  The total
 * number of entries displayed is returned 
 */
static int show_numeric_list (int numeric)
{
	NumericList *tmp;
	Hook	*list;
	char	buf[4];
	int	cnt = 0;

	if (numeric)
	{
		sprintf(buf, "%3.3u", numeric);
		if ((tmp = find_numeric_list(numeric)))
		{
			for (list = tmp->list; list; list = list->next, cnt++)
				show_hook(list, tmp->name);
		}
	}
	else
	{
		for (tmp = numeric_list; tmp; tmp = tmp->next)
		{
			for (list = tmp->list; list; list = list->next, cnt++)
				show_hook(list, tmp->name);
		}
	}
	return (cnt);
}

/*
 * show_list: Displays the contents of the list specified by the index into
 * the hook_functions array.  This function returns the number of entries in
 * the list displayed 
 */
static int show_list (int which)
{
	Hook	*list;
	int	cnt = 0;

	/* Less garbage when issueing /on without args. (lynx) */
	for (list = hook_functions[which].list; list; list = list->next, cnt++)
		show_hook(list, hook_functions[which].name);
	return (cnt);
}



/* * * * * * * * EXECUTING A HOOK * * * * * * */
#define NO_ACTION_TAKEN		-1
#define SUPPRESS_DEFAULT	 0
#define DONT_SUPPRESS_DEFAULT	 1
#define RESULT_PENDING		 2
#define RESULT_NEXT		 3

/*
 * do_hook: This is what gets called whenever a MSG, INVITES, WALL, (you get
 * the idea) occurs.  The nick is looked up in the appropriate list. If a
 * match is found, the stuff field from that entry in the list is treated as
 * if it were a command. First it gets expanded as though it were an alias
 * (with the args parameter used as the arguments to the alias).  After it
 * gets expanded, it gets parsed as a command.  This will return as its value
 * the value of the noisy field of the found entry, or -1 if not found. 
 */

int 	BX_do_hook (int which, char *format, ...)
{
	Hook		*tmp = NULL,
			*next = NULL, 
			**list;
	char		buffer		[BIG_BUFFER_SIZE * 10 + 1],
			*name 		= NULL;
	int		retval 		= DONT_SUPPRESS_DEFAULT;
	unsigned	display		= window_display;
	int		i;
	Hook		*hook_array	[2048] = { 0 };
	int		hook_num = 0;
	char		*result = NULL;
	int		old_debug_count = debug_count;
	
#ifdef WANT_TCL
	int		tcl_ret = 0;
#endif	
	/*
	 * Figure out where the hooks are for the event type were asserting
	 */
	/* Numeric list */
	if (which < 0)
	{
		NumericList *hook;

		if ((hook = find_numeric_list(-which)))
		{
			name = hook->name;
			list = &hook->list;
		}
		else
			list = NULL;
	}

	/* Named list */
	else
	{
		/*
		 * If we're already executing the type, and we're
		 * specifically not supposed to allow recursion, then
		 * dont allow recursion. ;-)
		 */
		if (hook_functions[which].mark && 
		    (hook_functions[which].flags & HF_NORECURSE))
			list = NULL;
		else
		{
			list = &(hook_functions[which].list);
			name = hook_functions[which].name;
			strncpy(hook_name, hook_functions[which].name, BIG_BUFFER_SIZE);
		}
	}


	/*
	 * Press the buffer using the specified format string and args
	 * We do this here so that we dont waste time doing the vsnprintf
	 * if we're not going to do any matching.  So for types where the
	 * user has no hooks, its a cheapie call.
	 */
	if (format)
	{
		va_list args;
		va_start (args, format);
		vsnprintf(buffer, BIG_BUFFER_SIZE * 5, format, args);
		va_end(args);
	}
	else
		ircpanic("do_hook: format is NULL");

#ifdef WANT_TCL
	if (tcl_interp)
		tcl_ret = check_on_hook(which, format?buffer:NULL);
#endif

	/*
	 * No hooks to look at?  No problem.  Drop out.
	 */
	if (!list)
	{
		*hook_name = 0;
		return NO_ACTION_TAKEN;
	}

	/*
	 * Mark the event as being executed.  This is used to suppress
 	 * unwanted recursion in some /on's.
	 */
	if (which >= 0)
		hook_functions[which].mark++;


	/* not attached, so dont "fix" it */
	{
		int 	currser 	= 0, 
			oldser 		= INT_MIN,
			currmatch 	= 0, 
			oldmatch 	= 0;
		Hook *	bestmatch 	= NULL;

		/*
		 * Walk the list of hooks for this event
		 */
		for (tmp = *list; tmp; tmp = tmp->next)
		{
			char *	tmpnick = NULL;
			int 	sa;

			/*
			 * save the current serial number
			 */
			currser = tmp->sernum;

			/*
			 * Is this a different serial number than the
			 * last hook?  If it is, then we save the previous
			 * serial number's best hook to the hook_array.
			 */
			if (currser != oldser)
			{
				oldser = currser;
				currmatch = oldmatch = 0;
				if (bestmatch)
					hook_array[hook_num++] = bestmatch;
				bestmatch = NULL;
			}

			/*
			 * If this is a flexible hook, expand the nick stuff
			 */
			if (tmp->flexible)
				tmpnick = expand_alias(tmp->nick, empty_string,
							 &sa, NULL);
			else
				tmpnick = tmp->nick;


			/*
			 * Check to see if the pattern matches the text
			 */
			currmatch = wild_match(tmpnick, buffer);

			/*
			 * If it is the "best match" so far, then we mark
			 * its "value" and save a pointer to it.
			 */
			if (currmatch > oldmatch)
			{
				oldmatch = currmatch;
				bestmatch = tmp;
			}

			/*
			 * Clean up after flexible hooks
			 */
			if (tmp->flexible)
				new_free(&tmpnick);
		}

		/*
		 * Ok. we've walked the list.  If the last hook had a best
		 * match, use that one too. =)
		 */
		if (bestmatch)
			hook_array[hook_num++] = bestmatch;
	}


	/*
	 * Now we walk the list of collected hook events that are to be run
	 */
	for (i = 0; i < hook_num; i++)
	{
		const char *	saved_who_from;
		unsigned long	saved_who_level;
		char *		name_copy;
		char *		stuff_copy;
		int		old_alias_debug = alias_debug;
		char		*result1 = NULL;
				
		/*
		 * This should never happen.
		 */
		if (!(tmp = hook_array[i]))
			ircpanic("hook_array[%d] is null", i);
			
		/* 
		 * Check to see if this hook is supposed to supress the
		 * default action for the event.
		 */

hook_next:
		if (tmp->noisy == SILENT && tmp->sernum == 0)
			retval = SUPPRESS_DEFAULT;
		else if (tmp->noisy == UNKNOWN && tmp->sernum == 0)
			retval = RESULT_PENDING;
		/*
		 * If this is a negated event, or there isnt anything to be
		 * executed, then we dont bother.  Just go on to the next one
		 */
		if (tmp->not || !tmp->stuff || !*tmp->stuff)
		{
			if (!tmp->stuff)
			{
				if (tmp->hook_func)
					(tmp->hook_func)(name, buffer, NULL);
				else if (tmp->num_func)
					(tmp->num_func)(which, buffer, NULL);
			}	
			continue;
		}
		/*
		 * If this is a NORMAL or NOISY hook, then we tell the user
		 * that we're going to execute the hook.
		 */
		if (tmp->noisy > QUIET)
			say("%s activated by %c%s%c", 
				name, tmp->flexible ? '\'' : '"',
				buffer, tmp->flexible ? '\'' : '"');

		if ((tmp->debug & HF_DEBUG) && (internal_debug & DEBUG_HOOK) && !in_debug_yell)
		{
			debugyell("ON %s activated [%s]", name, buffer);
			alias_debug++;
		}
		/*
		 * Save some information that may be reset in the 
		 * execution, turn off the display if the user specified.
		 */
		save_display_target(&saved_who_from, &saved_who_level);
		if (tmp->noisy < NOISY)
			window_display = 0;
		
		name_copy = LOCAL_COPY(name);
		stuff_copy = LOCAL_COPY(tmp->stuff);
		
		if (tmp->noisy == UNKNOWN)
			result = parse_line_with_return(name_copy, stuff_copy, buffer, 0, 0);
		else
		{
			/*
			 * Ok.  Go and run the code.  It is imperitive to note
			 * that "tmp" may be deleted by the code executed here,
			 * so it is absolutely forbidden to reference "tmp" after
			 * this point.
			 */
			next = tmp->next;
			will_catch_return_exceptions++;
			result1 = parse_line_with_return(name_copy, stuff_copy, buffer, 0, 0);
			will_catch_return_exceptions--;
			return_exception = 0;
		}
		if (retval == RESULT_PENDING)
		{
			if (result && atol(result))
				retval = SUPPRESS_DEFAULT;
			else
				retval = DONT_SUPPRESS_DEFAULT;
		}
		new_free(&result);
		/*
		 * Clean up the stuff that may have been mangled by the
		 * execution.
		 */
		restore_display_target(saved_who_from, saved_who_level);
		if (result1 && *result1 && !my_stricmp(result1, "next"))
		{
			new_free(&result1);
			{
				if ((tmp = next))
					goto hook_next;
				else
					retval = RESULT_NEXT;
			}
		}
		new_free(&result1);
		alias_debug = old_alias_debug;
		window_display = display;
	}

	/*
	 * Mark the event as not currently being done here.
	 */
	if (which >= 0)
		hook_functions[which].mark--;

	if (oper_command && *buffer)
		memset(buffer, 0, strlen(buffer)-1);
       	/*
	 * And return the user-specified suppression level
	 */
	if (old_debug_count == 1)
		debug_count = 1;
	*hook_name = 0;
	return retval;
}

/* 
 * shook: the SHOOK command -- this probably doesnt belong here,
 * and shook is probably a stupid name.  It simply asserts a fake
 * hook event for a given type.  Fraught with peril!
 */
BUILT_IN_COMMAND(shookcmd)
{
	int which;
	char *arg = next_arg(args, &args);

	if ((which = find_hook(arg, NULL)) == INVALID_HOOKNUM)
		return;
	else
		do_hook(which, "%s", args);
}


/* * * * * * SCHEDULING AN EVENT * * * * * * * */
/*
 * The ON command:
 * Format:		/ON [#][+-^]TYPE ['] [SERNUM] NICK ['] [{] STUFF [}]
 *
 * The "ON" command mainly takes three arguments.  The first argument
 * is the "type" of callback that you want to schedule.  This is either
 * a three digit number, of it is one of the strings so enumerated at the
 * top of this file in hook_list.  The second argument is the "nick" or
 * "pattern" that is to be used to match against future events.  If the
 * "nick" matches the text that is later passed to do_hook() with the given
 * "type", then the commands in "stuff" will be executed.
 *
 * If "nick" is enclosed in single quotes ('), then it is a "flexible" 
 * pattern, and will be expanded before it is matched against the text
 * in do_hook.  Otherwise, the string so specified is "static", and is
 * used as-is in do_hook().
 *
 * Within each type, there are at least 65,535 different "serial numbers",
 * (there actually are MAX_INT of them, but by convention, only 16 bit
 * serial numbers are used, from -32,768 to 32,767) which may be used to 
 * schedule any number of events at the given serial number.
 *
 * Each time an assertion occurs for a given "type", at most one of the
 * scheduled events is executed for each of the distinct serial numbers that
 * are in use for that event.  The event to be executed is the one at a 
 * given serial number that "best" matches the text passed to do_hook().
 * While in theory, up to MAX_INT events could be executed for a given single
 * assertion, in practice, a hard limit of 2048 events per assertion is 
 * enforced.
 * 
 * The runtime behavior of the event being scheduled can be modified by
 * specifying a character at the beginning of the "type" argument.  If you
 * want to schedule an event at a serial number, then the first character
 * must be a hash (#).  The argument immediately FOLLOWING the "type"
 * argument, and immediately PRECEEDING the "nick" argument must be an 
 * integer number, and is used for the serial number for this event.
 *
 * The "verbosity" of the event may also be modified by specifying at most
 * one of the following characters:  
 *	A caret (^) is the SILENT level, and indicates that the event is to
 *		be executed with no output (window_display is turned off),
 *		and the "default action" (whatever that is) for the event is
 *		to be suppressed.  The default action is actually only 
 *		suppressed if the SILENT level is specified for serial number
 *		zero.  This is the most common level used for overriding the
 *		output of most /on's.
 *	A minus (-) is the QUIET level, and is the same as the SILENT level,
 *		except that the default action (whatever that is) is not to
 *		be suppressed.
 *	No character is the "normal" case, and is the same as the "minus"
 *		level, with the addition that the client will inform you that
 *		the event was executed.  This is useful for debugging.
 *	A plus (+) is the same as the "normal" (no character specified),
 *		except that the output is not suppressed (window_display is 
 *		not changed.)
 */
BUILT_IN_COMMAND(oncmd)
{
	char	*func,
		*nick,
		*serial;
	Noise	noisy		= NORMAL;
	int	not		= 0,
		sernum		= 0,
		remove		= 0,
		which		= INVALID_HOOKNUM;
	int	flex		= 0;
	char	type;
	int	first;

	/*
	 * Get the type of event to be scheduled
	 */
	if ((func = next_arg(args, &args)) != NULL)
	{
		/*
		 * Check to see if this has a serial number.
		 */
		if (*func == '#')
		{
			if (!(serial = next_arg(args, &args)))
			{
				say("No serial number specified");
				return;
			}
			sernum = atol(serial);
			func++;
		}

		/*
		 * Get the verbosity level, if any.
		 */
		switch (*func)
		{
			case '?':
				noisy = UNKNOWN;
				func++;
				break;
			case '-':
				noisy = QUIET;
				func++;
				break;
			case '^':
				noisy = SILENT;
				func++;
				break;
			case '+':
				noisy = NOISY;
				func++;
				break;
			default:
				noisy = NORMAL;
				break;
		}

		
		/*
		 * Check to see if the event type is valid
		 */
		if ((which = find_hook(func, &first)) == INVALID_HOOKNUM)
		{
			/*
			 * Ok.  So either the user specified an invalid type
			 * or they specified an ambiguous type.  Either way,
			 * we're not going to be going anywhere.  So we have
			 * free reign to mangle 'args' at this point.
			 */

			int len;

			/*
			 * If first is -1, then it was an unknown type.
			 * An error has already been output, just return here
			 */
			if (first == -1)
				return;

			/*
			 * Otherwise, its an ambiguous type.  If they were 
			 * trying to register the hook, then theyve already
			 * gotten the error message, just return;
			 */
			if (new_new_next_arg(args, &args, &type))
				return;

			/*
			 * Ok.  So they probably want a listing.
			 */
			say("ON listings:");
			len = strlen(func);
			while (!my_strnicmp(func, hook_functions[first].name, len))
			{
				show_list(first);
				first++;
				if (!hook_functions[first].name)
					break;
			}

			return;
		}

		/*
		 * Check to see if this is a removal event or if this
		 * is a negated event.
		 */
		switch (*args)
		{
			case '-':
				remove = 1;
				args++;
				break;
			case '^':
				not = 1;
				args++;
				break;
		}

		/*
		 * Grab the "nick"
		 */
		if ((nick = new_new_next_arg(args, &args, &type)))
		{
			char *exp;

			/*
			 * Pad it to the appropriate number of args
			 */
			if (which < 0)
				nick = fill_it_out(nick, 1);
			else
				nick = fill_it_out(nick, hook_functions[which].params);

			/*
			 * If nick is empty, something is very wrong.
			 */
			if (!*nick)
			{
				say("No expression specified");
				new_free(&nick);
				return;
			}

			/*
			 * If we're doing a removal, do the deed.
			 */
			if (remove)
			{
				remove_hook(which, nick, sernum, 0);
				new_free(&nick);
				return;
			}

			/*
			 * Take a note if its flexible or not.
			 */
			if (type == '\'')
				flex = 1;
			else
				flex = 0;

			
			/*
			 * If this is a negative event, then we dont want
			 * to take any action for it.
			 */
			if (not)
				args = empty_string;


			/*
			 * Slurp up any whitespace after the nick
			 */
			while (my_isspace(*args))
				args++;

			/*
			 * Then slurp up the body ("text")
			 */
			if (*args == '{') /* } */
			{
				if (!(exp = next_expr(&args, '{'))) /* } */
				{
					say("Unmatched brace in ON");
					new_free(&nick);
					return;
				}
			}
			else
				exp = args;

			/*
			 * Schedule the event
			 */
			add_hook(which, nick, exp, noisy, not, sernum, flex);

			/*
			 * Tell the user that we're done.
			 */
			if (which < 0)
				say("On %3.3u from %c%s%c do %s [%s] <%d>",
				    -which, type, nick, type, 
				    (not ? "nothing" : exp),
				    noise_level[noisy], sernum);
			else
				say("On %s from %c%s%c do %s [%s] <%d>",
					hook_functions[which].name, 
					type, nick, type,
					(not ? "nothing" : exp),
					noise_level[noisy], sernum);

			/*
			 * Clean up after the nick
			 */
			new_free(&nick);
		}

		/*
		 * No "nick" argument was specified.  That means the user
		 * either is deleting all of the events of a type, or it
		 * wants to list all the events of a type.
		 */
		else
		{
			/*
			 * if its a removal, do the deed
			 */
			if (remove)
			{
				remove_hook(which, NULL, sernum, 0);
				return;
			}
	
			/*
			 * The help files say that an "/on 0" shows all
			 * of the numeric ONs.  Since the ACTION hook is
			 * number 0, we have to check to see if the first
			 * character of "func" is a zero or not.  If it is,
			 * we output all of the numeric functions.
			 */
			if (*func == '0')
			{
				if (!show_numeric_list(0))
				    say("All numeric ON lists are empty.");
			}
			else if (which < 0)
			{
				if (!show_numeric_list(-which))
				    say("The %3.3u list is empty.", -which);
			}
			else if (!show_list(which))
				say("The %s list is empty.", 
					hook_functions[which].name);
		}
	}

	/*
	 * No "Type" argument was specified.  That means the user wants to
	 * list all of the ONs currently scheduled.
	 */
	else
	{
		int	total = 0;

		say("ON listings:");

		/*
		 * Show the named events
		 */
		for (which = 0; which < NUMBER_OF_LISTS; which++)
			total += show_list(which);

		/*
		 * Show the numeric events
		 */
		total += show_numeric_list(0);

		if (!total)
			say("All ON lists are empty.");
	}
}


/* * * * * * * * * * SAVING A HOOK * * * * * * * * * * */
static	void	write_hook (FILE *fp, Hook *hook, char *name)
{
	char	*stuff = NULL;
	char	flexi = '"';

	if (hook->flexible)
		flexi = '\'';

	switch (hook->noisy)
	{
		case SILENT:
			stuff = "^";
			break;
		case QUIET:
			stuff = "-";
			break;
		case NORMAL:
			stuff = empty_string;
			break;
		case NOISY:
			stuff = "+";
			break;
		case UNKNOWN:
			stuff = "?";
			break;
	}

	if (hook->sernum)
		fprintf(fp, "ON #%s%s %d", stuff, name, hook->sernum);
	else
		fprintf(fp, "ON %s%s", stuff, name);

	fprintf(fp, " %c%s%c {%s}\n", flexi, hook->nick, flexi, hook->stuff);
}

/*
 * save_hooks: for use by the SAVE command to write the hooks to a file so it
 * can be interpreted by the LOAD command 
 */
void	save_hooks (FILE *fp, int do_all)
{
	Hook	*list;
	NumericList *numeric;
	int	which;

	for (which = 0; which < NUMBER_OF_LISTS; which++)
	{
		for (list = hook_functions[which].list; list; list = list->next)
			if (!list->global || do_all)
				write_hook(fp,list, hook_functions[which].name);
	}
	for (numeric = numeric_list; numeric; numeric = numeric->next)
	{
		for (list = numeric->list; list; list = list->next)
			if (!list->global)
				write_hook(fp, list, numeric->name);
	}
}



/* * * * * * * * * * STACKING A HOOK * * * * * * * * */
typedef struct  onstacklist
{
	int     which;
	Hook    *list;
	struct onstacklist *next;
}       OnStack;

static	OnStack	*	on_stack = NULL;

void	do_stack_on (int type, char *args)
{
	int	which;
	Hook	*list;
	NumericList *nhook = NULL, *nptr;

	if (!on_stack && (type == STACK_POP || type == STACK_LIST))
	{
		say("ON stack is empty!");
		return;
	}
	if (!args || !*args)
	{
		say("Missing event type for STACK ON");
		return;
	}

	if ((which = find_hook(args, NULL)) == INVALID_HOOKNUM)
		return;		/* Error message already outputted */

	if (which < 0)
	{
		if ((nhook = find_numeric_list(-which)))
			list = nhook->list;
		else
			list = NULL;
	}
	else
		list = hook_functions[which].list;


	if (type == STACK_PUSH)
	{
		OnStack	*new;
		new = (OnStack *) new_malloc(sizeof(OnStack));
		new->which = which;
		new->list = list;
		new->next = on_stack;
		on_stack = new;

		if (which < 0)
		{
			if (nhook && numeric_list)
				remove_numeric_list(-which);
		}
		else
			hook_functions[which].list = NULL;

		return;
	}

	else if (type == STACK_POP)
	{
		OnStack	*p, *tmp = (OnStack *) 0;

		for (p = on_stack; p; tmp = p, p = p->next)
		{
			if (p->which == which)
			{
				if (p == on_stack)
					on_stack = p->next;
				else
					tmp->next = p->next;
				break;
			}
		}
		if (!p)
		{
			say("No %s on the stack", args);
			return;
		}

		if ((which < 0 && nhook) || 
		    (which >= 0 && hook_functions[which].list))
			remove_hook(which, NULL, 0, 1);	/* free hooks */

		if (which < 0)
		{
			/* look -- do we have any hooks already for this numeric? */
			if (!(nptr = find_numeric_list(-which)))
			{
				if (p->list)	/* not just a placeholder? */
				{
					/* No. make a new list and put the stack on it */
					nptr = (NumericList *) new_malloc(sizeof(NumericList));
					nptr->list = p->list;
					nptr->next = NULL;
					nptr->numeric = -which;
					sprintf(nptr->name, "%3.3u", -which);
					add_numeric_list(nptr);
				}
			}
			else
			{
				remove_hook(which, NULL, 0, 1);
				nptr->list = p->list;
			}
		}
		else
			hook_functions[which].list = p->list;

		new_free((char **)&p);
		return;
	}

	else if (type == STACK_LIST)
	{
		int	slevel = 0;
		OnStack	*osptr;

		for (osptr = on_stack; osptr; osptr = osptr->next)
		{
			if (osptr->which == which)
			{
				Hook	*hptr;

				slevel++;
				say("Level %d stack", slevel);
				for (hptr = osptr->list; hptr; hptr = hptr->next)
					show_hook(hptr, args);
			}
		}

		if (!slevel)
			say("The STACK ON %s list is empty", args);
		return;
	}
	say("Unknown STACK ON type ??");
}



/* List manips especially for on's. */
void hook_add_to_list (Hook **list, Hook *item)
{
	Hook *tmp, *last = NULL;

	for (tmp = *list; tmp; last = tmp, tmp = tmp->next)
	{
		if (tmp->sernum < item->sernum)
			continue;
		else if ((tmp->sernum == item->sernum) && my_stricmp(tmp->nick, item->nick) < 0)
			continue;
		else
			break;
	}

	if (last)
	{
		item->next = last->next;
		last->next = item;
	}
	else
	{
		item->next = *list;
		*list = item;
	}
}


static Hook *hook_remove_from_list (Hook **list, char *item, int sernum)
{
	Hook *tmp, *last = NULL;

	for (tmp = *list; tmp; last = tmp, tmp = tmp->next)
	{
		if ((tmp->sernum == sernum) && !my_stricmp(tmp->nick, item))
		{
			if (last)
				last->next = tmp->next;
			else
				*list = tmp->next;
			return tmp;
		}
	}
	return NULL;
}


static void add_numeric_list (NumericList *item)
{
	NumericList *tmp, *last = NULL;

	for (tmp = numeric_list; tmp; last = tmp, tmp = tmp->next)
	{
		if (tmp->numeric > item->numeric)
			break;
	}

	if (last)
	{
		last->next = item;
		item->next = tmp;
	}
	else
	{
		item->next = numeric_list;
		numeric_list = item;
	}
}

static NumericList *find_numeric_list (int numeric)
{
	NumericList *tmp, *last = NULL;

	for (tmp = numeric_list; tmp; last = tmp, tmp = tmp->next)
	{
		if (tmp->numeric == numeric)
			return tmp;
	}

	return NULL;
}

static NumericList *remove_numeric_list (int numeric)
{
	NumericList *tmp, *last = NULL;

	for (tmp = numeric_list; tmp; last = tmp, tmp = tmp->next)
	{
		if (tmp->numeric == numeric)
			break;
	}

	if (tmp)
	{
		if (last)
			last->next = tmp->next;
		else
			numeric_list = numeric_list->next;
	}
	return tmp;
}

#ifdef WANT_DLL
#if 0
static NumericList *remove_numeric_dll_list(int numeric)
{
	NumericList *tmp, *last = NULL;

	for (tmp = numeric_list; tmp; last = tmp, tmp = tmp->next)
	{
		if (tmp->numeric == numeric)
			break;
	}

	if (last)
		last->next = tmp->next;
	else
		numeric_list = numeric_list->next;

	return tmp;
}

static Hook *remove_dll_hook_list(Hook **entry)
{
	Hook *tmp, *last = NULL;

	for (tmp = *list; tmp; last = tmp, tmp = tmp->next)
	{
		if (last)
			last->next = tmp->next;
		else
			*list = tmp->next;
		return tmp;
	}
	return NULL;
}
#endif

int remove_dll_hook(char *package)
{
	NumericList *tmp;
	Hook *list, *next = NULL;
	int x, ret = 0;
	for (x = 1; x < 999; x++)
	{
		if ((tmp = find_numeric_list(x)))
		{
			for (list = tmp->list; list; list = next)
			{	
				next = list->next;
				if (!my_stricmp(list->filename, package))
				{
					remove_numeric_hook(x, list->nick, list->sernum, 1);
					ret++;
				}
			}
		}
	}

	for (x = 0; x < NUMBER_OF_LISTS; x++)
	{
		for (list = hook_functions[x].list; list; list = next)
		{
			next = list->next;
			if (!my_stricmp(list->filename, package))
			{
				remove_hook(x, list->nick, list->sernum, 1);
				ret++;
			}
		}
	}
	return ret;
}

#endif