File: timer.c

package info (click to toggle)
epic5 3.0.3-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 5,328 kB
  • sloc: ansic: 75,810; makefile: 648; ruby: 227; python: 215; sh: 78; perl: 13
file content (1348 lines) | stat: -rw-r--r-- 36,015 bytes parent folder | download
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
/*
 4 timer.c -- handles timers in ircII
 *
 * Copyright (c) 1991, 1992 Troy Rollo.
 * Copyright (c) 1992-1996 Matthew Green.
 * Copyright 1997, 2015 EPIC Software Labs.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notices, the above paragraph (the one permitting redistribution),
 *    this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the author(s) may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 * This used to be in edit.c, and it used to only allow you to
 * register ircII commands to be executed later.  I needed more
 * generality then that, specifically the ability to register
 * any function to be called, so i pulled it all together into
 * this file and called it timer.c
 */

#include "irc.h"
#include "ircaux.h"
#include "window.h"
#include "timer.h"
#include "hook.h"
#include "output.h"
#include "commands.h"
#include "server.h"
#include "screen.h"
#include "functions.h"

	int 	timer_exists (const char *ref);
	int 	remove_timer (const char *ref);
static	void 	remove_all_timers (void);
static	void	remove_timers_by_domref (int domain, int domref);
static	void	list_timers (const char *command);

/*
 * timercmd - The /TIMER command
 *
 * Usage:
 *  Listing Timers:
 *	/TIMER			
 *		Show all pending user-created TIMERs
 * 
 *  Deleting Timers:
 *	/TIMER -DELETE <refnum>
 *		Delete timer with name <refnum>
 *	/TIMER -DELETE ALL
 *		Delete all timers
 *	/TIMER -DELETE_FOR_WINDOW <window>
 *		Delete all WINDOW timers that are bound to <window>.
 *		<Winref> must be a number.  Use $winnum() to get that.
 *
 *  Updating Timers:
 *	/TIMER -UPDATE -REFNUM <timeref> [other arguments below]
 *		(a) To update a Timer, you must use the -REFNUM argument
 *		    to specify the timer to update.  
 *		(b) Updating a <timeref> that does not exist is not an error
 *		    if you specify a timeout; it will be created for you.
 *		
 *  Creating a Timer:
 *	/TIMER [options below] <seconds> <commands>
 *		Run <commands> after <seconds> seconds.
 *		(a) If you run a /TIMER whenever the client is handling
 *		    server data, the /TIMER will attach to the current server
 *		    and whatever its current window is at that time. ("Server")
 *		(b) If you run a /TIMER whenever the client is handling
 *		    user input, the /TIMER will attach to the current window
 *		    and whatever its current server is at that time. ("Window")
 *		(c) Otherwise, the /TIMER will run in whatever the current
 *		    context is at the time it goes off. ("General")
 *
 *	/TIMER -REFNUM
 *		The TIMER should have a specific name (default: auto-generated)
 *	/TIMER -REPEAT
 *		The TIMER should run this many times (default: 1 time)
 *		The magic value -1 means "run forever"
 *	/TIMER -SNAP
 *		The TIMER should run "at the top of" the interval.
 *		EG, /TIMER -REPEAT -1 -SNAP 60 {echo It's a new minute!} 
 *		will run at the top of every minute.  The first execution
 *		will happen "early" to make it work out.
 *
 *	/TIMER -WINDOW
 *		The TIMER should be a WINDOW Timer (default: auto-detected)
 *	/TIMER -SERVER
 *		The TIMER should be a SERVER Timer (default: auto-detected)
 *	/TIMER -GENERAL
 *		The TIMER should be a GENERAL Timer (default: auto-detected)
 *	/TIMER -CANCELABLE
 *		If the TIMER cannot restore its concept (because it is a 
 *		WINDOW Timer and the window has gone away; or because it is
 *		a SERVER Timer and the server has been deleted), the Timer
 *		will not execute when it goes off. (default: Window Timers
 *		and Server Timers become General Timers if they can't restore
 *		their context)
 */
BUILT_IN_COMMAND(timercmd)
{
	char *		waittime;
	char *		flag;
	const char *	want = empty_string;
	char *		ptr;
	double		interval;
	long		events = -2;
	int		update = 0;
	size_t		len;
	TimerDomain	domain;
	int		domref;
	int		cancelable = 0;
	int		snap = 0;

	if (parsing_server_index != NOSERV)
	{
		domain = SERVER_TIMER;
		domref = parsing_server_index;
	}
	else
	{
		domain = WINDOW_TIMER;
		domref = get_window_refnum(0);
	}

	while (*args == '-' || *args == '/')
	{
	    flag = next_arg(args, &args);
	    len = strlen(flag + 1);

	    if (!my_strnicmp(flag + 1, "DELETE", len))
	    {
		if (!(ptr = next_arg(args, &args)))
		    say("%s: Need a timer reference number for -DELETE",
				command);

		/* 
		 * Try to delete what was given.  If that
		 * doesn't work, see if it is "delete all".
		 * Delete_timer returns -1 on error.
		 */
		else if (timer_exists(ptr) == 1)
			remove_timer(ptr);

		/*
		 * Check to see if it is /timer -delete all
		 *
		 * Ugh.  Yes, this hack really _is_ required.
		 * Please don't change it without talking to
		 * me first to understand why.
		 */
		else if (*ptr && !my_strnicmp(ptr, "ALL", strlen(ptr)))
			remove_all_timers();
		else if (*ptr)
			say("The timer \"%s\" doesn't exist!", ptr);
		else
			say("/TIMER -DELETE got confused and chose not to do anything.");	/* This used to be a panic */

		return;
	    }
	    else if (!my_strnicmp(flag + 1, "DELETE_FOR_WINDOW", len))
	    {
		if (!(ptr = next_arg(args, &args)) || !is_number(ptr))
		{
		    say("%s: Need a window number for -DELETE_FOR_WINDOW", 
				command);
		    return;
		}

		domref = atol(ptr);
		remove_timers_by_domref(WINDOW_TIMER, domref);
		return;
	    }
	    else if (!my_strnicmp(flag+1, "REFNUM", len))	/* REFNUM */
	    {
		want = next_arg(args, &args);
		if (!want || !*want)
		{
			say("%s: Missing argument to -REFNUM", command);
			return;
		}

		continue;
	    }
	    else if (!my_strnicmp(flag+1, "REPEAT", len))	/* REPEAT */
	    {
		char *na = next_arg(args, &args);
		if (!na || !*na)
		{
			say("%s: Missing argument to -REPEAT", command);
			return;
		}
		if (!strcmp(na, "*") || !strcmp(na, "-1"))
			events = -1;
		else if ((events = my_atol(na)) == 0)
			return;	 /* Repeating 0 times is a noop */

		continue;
	    }
	    else if (!my_strnicmp(flag + 1, "UPDATE", len))	/* UPDATE */
		update = 1;

	    else if (!my_strnicmp(flag + 1, "LIST", len))	/* LIST */
	    {
		list_timers(command);
		return;
	    }
	    else if (!my_strnicmp(flag + 1, "WINDOW", len))	/* WINDOW */
	    {
		char 	*na;
		int	window = 0;

		domain = WINDOW_TIMER;
		if ((na = next_arg(args, &args)))
			window = lookup_window(na);

		if (window < 1)
		{
		    if (my_stricmp(na, "-1"))
		    {
			say("%s: That window doesnt exist!", command);
			return;
		    }
		    else
			domref = -1;
		}
		else
			domref = window;
	    }
	    else if (!my_strnicmp(flag + 1, "SERVER", len))	/* SERVER */
	    {
		char 	*na;

		domain = SERVER_TIMER;
		domref = from_server;

		if ((na = next_arg(args, &args)))
		{
		    if ((domref = str_to_servref(na)) == NOSERV)
		    {
			/* -1 defaults to from_server anyways */
			if (!my_stricmp(na, "-1"))
			    domref = from_server;
			else
			{
			    say("%s: Server %s doesnt exist!", command, na);
			    return;
			}
		    }
		}
	    }
	    else if (!my_strnicmp(flag + 1, "SNAP", len))	/* SNAP */
	    {
		snap = 1;
	    }
	    else if (!my_strnicmp(flag + 1, "GENERAL", len))	/* GENERAL */
	    {
		domain = GENERAL_TIMER;
		domref = -1;
	    }
	    else if (!my_strnicmp(flag + 1, "CANCELABLE", len))	/* CANCELABLE */
	    {
		cancelable = 1;
	    }
	    else
	    {
		say("%s: %s no such flag", command, flag);
		return;
	    }
	}


	/* else check to see if we have no args -> list */
	waittime = next_arg(args, &args);
	if (update || waittime)
	{
		int	i;

		if (update && ((i = timer_exists(want)) <= 0))
		{
			/* If the timer doesn't exist, ignore -update */
			if (i == 0 && waittime)
				update = 0;

			/* 
			 * Otherwise, either you didn't use -refnum, or
			 * you tried to update a timer that doesn't exist,
			 * but didn't provide a timeout, so we can't 
			 * create a timer for you.
			 */
			else
			{
				say("%s: To use -UPDATE you must specify a "
						"valid refnum", command);
				return;
			}
		}

		if (!waittime)
			interval = -1;
		else
			interval = atof(waittime);

		if (!update && events == -2)
			events = 1;
/*
		else if (events == -2)
			events = -1;
*/

		add_timer(update, want, interval, events, NULL, args, subargs, 
				domain, domref, cancelable, snap);
	}
	else
		list_timers(command);

	return;
}

/*
 * This is put here on purpose -- we dont want any of the above functions
 * to have any knowledge of this struct.
 */
#define REFNUM_MAX 10
typedef struct  timerlist_stru
{
	char	*ref;
        Timeval time;
	int	(*callback) (void *);
	void *	callback_data;
        char *	command;
	char	*subargs;
	struct	timerlist_stru *prev;
        struct  timerlist_stru *next;
	long	events;
	Timeval	interval;
	int	domain;
	int	domref;
	int	cancelable;
	long	fires;
	char *	package;
}       Timer;

static 	Timer *		PendingTimers;

static Timer *	new_timer (void);
static Timer *	clone_timer (Timer *otimer);
static void	delete_timer (Timer *otimer);
static int	schedule_timer (Timer *ntimer);
static int	unlink_timer (Timer *timer);
static Timer *	get_timer (const char *ref);

/*
 * new_timer - Create a blank Timer that can be filled in.
 *
 * Arguments:
 *	none
 *
 * Return Value:
 *	A new (Timer *) that can be filled in and passed to schedule_timer().
 * 	After a Timer is executed or cancelled, it should be passed to 
 *	  delete_timer().
 */
static Timer *	new_timer (void)
{
	Timer *	ntimer;

	ntimer = (Timer *) new_malloc(sizeof(Timer));
	ntimer->ref = NULL;
	ntimer->time.tv_sec = 0;
	ntimer->time.tv_usec = 0;
	ntimer->callback = NULL;
	ntimer->callback_data = NULL;
	ntimer->command = NULL;
	ntimer->subargs = NULL;
	ntimer->prev = NULL;
	ntimer->next = NULL;
	ntimer->events = 0;
	ntimer->interval.tv_sec = 0;
	ntimer->interval.tv_usec = 0;
	ntimer->domain = GENERAL_TIMER;
	ntimer->domref = -1;
	ntimer->cancelable = 0;
	ntimer->fires = 0;
	ntimer->package = NULL;
	return ntimer;
}

/*
 * clone_timer -  Create a new Timer that is an exact copy of an existing
 *		  Timer, suitable for re-scheduling.
 * 
 * Argument:
 *	otimer - A Timer that should be cloned
 *
 * Return Value:
 *	A new (Timer *) that is identical to 'otimer', that you can pass
 *	   to schedule_timer()
 *
 * Notes:
 *	Cloning a timer is necessary to allow you to Delete (/timer -delete)
 *	a Repeating Timer (/timer -repeat).  If you deleted a repeating timer,
 *	that would cause problems in ExecuteTimers().  So what we do is treat
 *	every Timer as a single-fire:
 *	  1. Schedule a Timer
 *	  2. Timer Goes Off
 *	  3. Timer is removed from Schedule List
 *	  4. If Timer is a Repeating Timer, it is Cloned, and the Clone is
 *	     scheduled for its next execution
 *	  5. The Timer is executed
 * 	In this way, when you do /timer -delete within a repeating timer,
 *	it deletes the Clone, without clobbering the active timer.
 *
 *	In the same way, updating a Timer doesn't actually change the timer,
 *	but changes a clone of that Timer, causing the new (cloned) Timer to
 *	be created and the old Timer to be deleted.  That makes it easier
 *	to ensure the updated timer gets to the right spot on the Timer list.
 */
static Timer *	clone_timer (Timer *otimer)
{
	Timer *ntimer = new_timer();

	malloc_strcpy(&ntimer->ref, otimer->ref);
	ntimer->time = otimer->time;
	if ((ntimer->callback = otimer->callback))
		ntimer->callback_data = otimer->callback_data;
	else
		ntimer->command = malloc_strdup(otimer->command);
	ntimer->subargs = malloc_strdup(otimer->subargs);
	ntimer->prev = NULL;
	ntimer->next = NULL;
	ntimer->events = otimer->events;
	ntimer->interval = otimer->interval;
	ntimer->domain = otimer->domain;
	ntimer->domref = otimer->domref;
	ntimer->cancelable = otimer->cancelable;
	ntimer->fires = otimer->fires;
	if (otimer->package)
		ntimer->package = malloc_strdup(otimer->package);
	return ntimer;
}

/*
 * delete_timer: clean up after a Timer that is no longer needed.
 *		 You must _not_ submit a Timer that is still on the schedule.
 *
 * Arguments:
 *	ntimer - An unneeded Timer that is not on the Timer list.
 *		 Deleting a still-scheduled Timer is an error.
 */
static void	delete_timer (Timer *otimer)
{
	Timer *tmp;

	/* 
	 * First we make sure 'otimer' is not still scheduled
	 * before we go free()ing it.
	 * (This is a violation of the API, but we're forgiving)
	 * (The other option is to make this return failure, but 
	 *  since this is a void function, we'll just DTRT)
	 */
	for (tmp = PendingTimers; tmp; tmp = tmp->next)
	{
		if (tmp == otimer)
		{
			yell("delete_timer: Warning: Deleting a timer that "
				"is still scheduled.  Unscheduling it.");
			unlink_timer(otimer);
			break;
		}
	}

	if (!otimer->callback)
	{
		new_free((char **)&otimer->command);
		new_free((char **)&otimer->subargs);
	}
	new_free(&otimer->package);
	new_free(&otimer->ref);
	new_free((char **)&otimer);
}

/*
 * schedule_timer: Submit a completed Timer to be executed later.
 *		   You must not change the Timer after it is submitted.
 *
 * Arguments:
 *	ntimer - A completely filled-in timer that needs to be executed.
 *	  	(a) ntimer->time must point to when the timer is to go off.
 *		(b) ntimer->prev and ->next will be overwritten.
 *		(c) You must not change 'ntimer' after this returns.
 *		(d) ntimer must not already be scheduled.
 *
 * Return Value:
 *	0 - The timer has been scheduled (always succeeds)
 */
static int	schedule_timer (Timer *ntimer)
{
	Timer *tmp, *prev;

	ntimer->fires = 0;

	/*
	 * If 'ntimer' is already scheduled, we will desschedule it,
	 * so that it may be re-inserted in the correct place.
	 */
	for (tmp = PendingTimers; tmp; tmp = tmp->next)
	{
		if (tmp == ntimer)
		{
			yell("schedule_timer: Warning: Scheduling a timer "
				"that is already scheduled.  Fixing that.");
			unlink_timer(ntimer);
			break;
		}
	}

	/* Figure out the correct place */
	for (tmp = PendingTimers; tmp; tmp = tmp->next)
	{
		if (time_diff(tmp->time, ntimer->time) < 0)
			break;
	}

	/* 'ntimer' is earlier than all the timers in PendingTimers */
	if (tmp == PendingTimers)
	{
		ntimer->prev = NULL;
		ntimer->next = PendingTimers;
		if (PendingTimers)
			PendingTimers->prev = ntimer;
		PendingTimers = ntimer;
	}
	/* 'ntimer' is earlier than 'tmp' but later than 'tmp->prev' */
	else if (tmp)
	{
		prev = tmp->prev;
		ntimer->next = tmp;
		ntimer->prev = prev;
		tmp->prev = ntimer;
		prev->next = ntimer;
	}
	/* 'ntimer' is later than every timer in PendingTimers */
	else
	{
		for (tmp = PendingTimers; tmp->next; tmp = tmp->next)
			;
		tmp->next = ntimer;
		ntimer->prev = tmp;
	}

	return 0;
}

/*
 * unlink_timer - Remove a Timer from the TimerList ("unschedule it")
 * 
 * Arguments:
 *	timer	- A Timer on the TimerList.
 *		  (a) It is an error to unlink a timer not on the timer list.
 *		      Doing so may result in a crash.
 *
 * Return Value:
 *	-1	- The timer was not scheduled (no change to 'timer')
 *	 0	- The timer is de-scheduled 
 */
static int	unlink_timer (Timer *timer)
{
	Timer *tmp, *prev, *next;

	/*
	 * We only modify 'timer' if it is actually scheduled.
	 * unlinking an unscheduled timer is a safe no-op.
	 */
	for (tmp = PendingTimers; tmp; tmp = tmp->next)
	{
		if (tmp == timer)
		{
			prev = timer->prev;
			next = timer->next;

			if (prev)
				prev->next = next;
			else
				PendingTimers = next;

			if (next)
				next->prev = prev;

			return 0;
		}
	}

	/* I didn't find that timer on the schedule list */
	return -1;
}

/*
 * get_timer - Return a schedule Timer using its refname.
 *
 * Arguments:
 *	ref	- A Timer refname (timer->ref, from /TIMER -REF)
 *		 (a) ref must not be NULL or an empty string.
 *
 * Return Value:
 *	NULL	 - No scheduled timer exists by that name
 *	non-NULL - A Scheduled Timer by the given name.
 *		(a) You must _NOT_ change a Scheduled Timer unless
 *		    you unlink_timer() it first.
 *		(b) The best practice for changing a Scheduled Timer:
 *		    1) x = get_timer();
 *		    2) y = clone_timer(x);
 *		    3) unlink_timer(x);
 *		    4) <make changes to y>
 *		    5) schedule_timer(y);
 *		    6) delete_timer(x);
 */
static	Timer *get_timer (const char *ref)
{
	Timer *tmp;

	/* 'ref' must be a non-empty string */
	if (!ref || !*ref)
		return NULL;

	for (tmp = PendingTimers; tmp; tmp = tmp->next)
	{
		if (!my_stricmp(tmp->ref, ref))
			return tmp;
	}

	return NULL;
}

/*
 * timer_exists - Verify if a refnum is in use by a scheduled Timer.
 *
 * Arguments:
 *	ref	- A Timer Refname
 *
 * Return Value:
 *	-1	- 'ref' is an invalid refnum
 *	 0	- 'ref' is available for use (no scheduled timer exists)
 *	 1	- 'ref' is not available for use (already is in use)
 */
int 	timer_exists (const char *ref)
{
	if (!ref || !*ref)
		return -1;

	if (get_timer(ref))
		return 1;
	else
		return 0;
}

/*
 * dump_timers - show all timers in case of emergency
 */
void    dump_timers (void)
{
        Timer   *tmp;
        Timeval current;
        double  time_left;

        yell("*X*X*X*X*X*X*X*X*X* WARNING *X*X*X*X*X*X*X*X*X*X");
        yell("POLLING LOOP DETECTED -- IMPORTANT DEBUGGING INFO");
        yell("MAKE SURE TO SAVE THIS VERY IMPORTANT INFORMATION!");
        yell("%s", empty_string);
        say("Timer     Seconds   Events Command");

        get_time(&current);
        for (tmp = PendingTimers; tmp; tmp = tmp->next)
        {
                time_left = time_diff(current, tmp->time);
                if (time_left <= 0)
                    yell("--> %-10s %-10.2f %-7ld %ld %s", 
                                tmp->ref, time_left, tmp->events, 
				tmp->fires,
                                tmp->callback ? "SYSTEM" : tmp->command);
                else
                    yell("    %-10s %-10.2f %-7ld %ld %s", 
                                tmp->ref, time_left, tmp->events,
				tmp->fires,
                                tmp->callback ? "SYSTEM" : tmp->command);
        }
        yell("Make sure to give this list to hop on #epic on efnet!");
        yell("*X*X*X*X*X*X*X*X*X* WARNING *X*X*X*X*X*X*X*X*X*X");
}


/*
 * list_timers:  Display a list of all the TIMER commands that are
 * pending to be executed.
 */
static	void	list_timers (const char *command)
{
	Timer	*tmp;
	Timeval	current;
	double	time_left;
	int	timer_count = 0;

	get_time(&current);
	for (tmp = PendingTimers; tmp; tmp = tmp->next)
	{
		if (tmp->callback)
			continue;

		if (timer_count == 0)
			say("%-10s %-10s %-7s %s", 
				"Timer", "Seconds", "Events", "Command");

		timer_count++;
		time_left = time_diff(current, tmp->time);
		if (time_left < 0)
			time_left = 0;
		say("%-10s %-10.2f %-7ld %s", tmp->ref, time_left, 
					tmp->events, tmp->command);
	}

	if (timer_count == 0)
		say("%s: No commands pending to be executed", command);
}

/*
 * create_timer_ref:  returns the lowest unused reference number for a timer
 * All refnums that are not already in use are valid.
 *
 * The user is allowed to use any string as a refnum, we dont really care.
 * Automatically assigned refnums (when the user doesnt specify one) will
 * always be one more than the highest pending refnum.
 *
 * "refnum_gets" must be REFNUM_MAX + 1 bytes by definition of API.
 */
static	int	create_timer_ref (const char *refnum_wanted, char **refnum_gets)
{
	Timer	*tmp;
	char	*refnum_want;
	int	i, pts;

	refnum_want = LOCAL_COPY(refnum_wanted);

	/* If the user doesnt care */
	if (*refnum_want == 0)
	{
		/* So ... we count the number of times that exist. */
		for (pts = 0, tmp = PendingTimers; tmp; tmp = tmp->next)
			pts++;

		/* 
		 * Now, for all the numbers (0 .. [timer count + 1]), 
		 * at least one of those numbers *has* to be available,
		 */ 
		for (i = 0; i <= pts + 1; i++)
		{
			/* Are any timers named 'i'? */
			for (tmp = PendingTimers; tmp; tmp = tmp->next)
			{
				if (!is_number(tmp->ref))
					continue;
				if (i == my_atol(tmp->ref))
					break;
			}

			/* 
			 * If 'tmp' is null, then we didn't find a refnum 'i'.
			 * So 'i' is our winner!
			 */
			if (tmp == NULL)
			{
				malloc_sprintf(refnum_gets, "%d", i);
				break;
			}
		}
	}
	else
	{
		/* See if the refnum is available */
		if (get_timer(refnum_want))
			return -1;		/* Already in use */

		malloc_strcpy(refnum_gets, refnum_want);
	}

	return 0;
}

/*
 * Remove a timer.  This does cleanup only if the timer is a 
 * user-defined timer, otherwise no clean up is done (the caller
 * is responsible to handle it)  This shouldnt output an error,
 * it should be more general and return -1 and let the caller
 * handle it.  Probably will be that way in a future release.
 */
int	remove_timer (const char *ref)
{
	Timer	*tmp;

	if (!(tmp = get_timer(ref)))
	{
		say("TIMER: Can't delete (%s), no such refnum", ref);
		return -1;
	}

	unlink_timer(tmp);
	delete_timer(tmp);
	return 0;
}

static void 	remove_all_timers (void)
{
	Timer *ref, *next;

	for (ref = PendingTimers; ref; ref = next)
	{
		next = ref->next;
		if (ref->callback)
			continue;
		unlink_timer(ref);
		delete_timer(ref);
	}
}

static	void	remove_timers_by_domref (int domain, int domref)
{
	Timer *ref, *next;

	for (ref = PendingTimers; ref; ref = next)
	{
		next = ref->next;
		if (ref->callback)
			continue;
		if (ref->domain != domain)
			continue;
		if (ref->domref != domref)
			continue;
		unlink_timer(ref);
		delete_timer(ref);
	}
}


/*
 * You call this to register a timer callback.
 *
 * The arguments:
 *  update:      This should be 1 if we're updating the specified refnum
 *  refnum_want: The refnum requested.  
 *		 (1) User-supplied for /TIMER timers
 *		 (2) the empty string for system timers ("dont care")
 *  interval:	 How long until the timer should fire; 
 *		 (1) for repeating timers (events != 1), the timer will 
 *		     fire with this interval.
 *		 (2) for "snap" timers, the first fire will be the next
 *		     time time() % interval == 0.
 *  events:	 The number of times this event should fire.  
 *		 (1) The value -1 means "repeat forever"
 *		 (2) Timers automatically delete after they fire the
 *		     requested number of times.
 *
 * Scenario 1: You want to run ircII commands (/TIMER timers)
 * | callback:	 NULL
 * | commands:	 some ircII commands to run when the timer goes off
 * | subargs:	 what to use to expand $0's, etc in the 'what' variable.
 *
 * Scenario 2: You want to call an internal function (system timers)
 * | callback:	 function to call when timer goes off
 * | commands:	 argument to pass to "callback" function.  Should be some
 * |		 non-auto storage, perhaps a struct or a malloced char *
 * |		 array.  The caller is responsible for disposing of this
 * |		 area when it is called, since the timer mechanism does not
 * |		 know anything of the nature of the argument.
 * | subargs:	 should be NULL, its ignored anyhow.
 *
 *  domain:	What the TIMER should bind to:
 *		(a) SERVER_TIMER  - 'domref' refers to a server refnum.
 *			Each time this timer runs, set from_server to 'domref'
 *			and set the current window to whatever 'domref's 
 *			current window is at that time.
 *		(b) WINDOW_TIME - 'domref' refers to a window refnum.
 *			Each time this timer runs, set current_window to
 *			'domref' and set the current server to whatever 
 *			'domref's server is at that time.
 *		(c) GENERAL_TIMER - 'domref' is ignored.  
 *			Do not save or restore from_server or current_window: 
 *			run in whatever the context is when it goes off.
 *  domref:	Either a server refnum, window refnum, or -1 (see 'domain')
 *  cancelable:	A "Cancelable" timer will not fire if its context cannot be
 *		restored (ie, the server it is bound to is disconnected or
 *		deleted; or the window it is bound to is deleted).  A normal
 *		(non-cancelable) timer will turn into a GENERAL_TIMER if its
 *		context cannot be restored.
 *  snap:	A "snap" timer runs every time (time() % interval == 0).
 *		This is useful for things that (eg) run at the top of every 
 *		minute (60), hour (3600), or day (86400)
 */
char *add_timer (int update, const char *refnum_want, double interval, long events, int (callback) (void *), void *commands, const char *subargs, TimerDomain domain, int domref, int cancelable, int snap)
{
	Timer	*ntimer, *otimer = NULL;
	char *	refnum_got = NULL;
	Timeval right_now;
	char *	retval;

	right_now = get_time(NULL);

	/*
	 * We do this first, because if 'interval' is invalid, we don't
	 * want to do the expensive clone/create/delete operation.
 	 * It is ineligant to check for this error here.
	 */
	if (update == 1 && interval == -1)	/* Not changing the interval */
		(void) 0;	/* XXX sigh */
	else if (interval < 0.01 && events == -1)
	{
		say("You can't infinitely repeat a timer that runs more "
			"than 100 times a second.");
		return NULL;
	}

	/* 
	 * If we say we're updating; but the timer does not exist, 
	 * then we're not updating. ;-)
	 */
	if (update)
	{
	    if (!(otimer = get_timer(refnum_want)))
		update = 0;		/* Ok so we're not updating! */
	}

	/*
	 * Arrange for an appropriate Timer to be in 'ntimer'.
	 */
	if (update)
	{
		unlink_timer(otimer);
		ntimer = clone_timer(otimer);
		delete_timer(otimer);
	}
	else
	{
		if (create_timer_ref(refnum_want, &refnum_got) == -1)
		{
			say("TIMER: Refnum '%s' already exists", refnum_want);
			return NULL;
		}

		ntimer = new_timer();
		ntimer->ref = refnum_got;
                if (current_package())
			ntimer->package = malloc_strdup(current_package());
	}

	/* Update the interval */
	if (update == 1 && interval == -1)
		(void) 0;	/* XXX sigh - not updating interval */
	else
	{
		ntimer->interval = double_to_timeval(interval);
		if (snap)
		{
			double x = time_to_next_interval(interval);
			ntimer->time = time_add(right_now, double_to_timeval(x));
		}
		else
			ntimer->time = time_add(right_now, ntimer->interval);
	}

	/* Update the repeat events */
	if (update == 1 && events == -2)
		(void) 0;	/* XXX sigh - not updating events */
	else
		ntimer->events = events;


	/* Update the callback */
	if (callback)
	{
		/* Delete the previous timer, if necessary */
		if (ntimer->command)
			new_free(&ntimer->command);
		if (ntimer->subargs)
			new_free(&ntimer->subargs);
		ntimer->callback = callback;

		/* Unfortunately, command is "sometimes const". */
		ntimer->callback_data = commands;
		ntimer->subargs = NULL;
	}
	else
	{
		ntimer->callback = NULL;
		malloc_strcpy(&ntimer->command, (const char *)commands);
		malloc_strcpy(&ntimer->subargs, subargs);
	}

	/* Update the domain refnum */
	ntimer->domain = domain;
	if (update == 1 && domref == -1)
		(void) 0;	/* XXX sigh - not updating domref */
	else
		ntimer->domref = domref;

	/* Update the cancelable */
	if (update == 1 && cancelable == -1)
		(void) 0;	/* XXX sigh - not updating cancelable */
	else
		ntimer->cancelable = cancelable;


	/* Schedule up the new/updated timer! */
	schedule_timer(ntimer);
	retval = ntimer->ref;
	return retval;		/* Eliminates a specious warning from gcc */
}


/*
 * TimerTimeout:  Called from irc_io to help create the timeout
 * part of the call to select.
 */
Timeval	TimerTimeout (void)
{
	Timeval	forever = {9999, 0};
	Timeval right_away = {0, 0};
	Timeval	current;
	Timeval	timeout_in;

	/* This, however, should never happen. */
	if (!PendingTimers)
		return forever;

	get_time(&current);
	timeout_in = time_subtract(current, PendingTimers->time);
	PendingTimers->fires++;
	if (time_diff(right_away, timeout_in) < 0)
		timeout_in = right_away;
	return timeout_in;
}

/*
 * ExecuteTimers:  checks to see if any currently pending timers have
 * gone off, and if so, execute them, delete them, etc, setting the
 * current_exec_timer, so that we can't remove the timer while its
 * still executing.
 *
 * changed the behavior: timers will not hook while we are waiting.
 */
void 	ExecuteTimers (void)
{
	Timeval	right_now;
	Timer *	current, *next;
	int	old_from_server = from_server;

	get_time(&right_now);
	while (PendingTimers && time_diff(right_now, PendingTimers->time) < 0)
	{
		int	old_refnum;

		old_refnum = get_window_refnum(0);
		current = PendingTimers;
		unlink_timer(current);

		/* Reschedule the timer if necessary */
		if (current->events < 0 || (current->events != 1))
		{
			next = clone_timer(current);
			if (next->events != -1)
				next->events--;

			next->time = time_add(next->time, next->interval);
			schedule_timer(next);
		}

		if (current->domain == SERVER_TIMER)
		{
		    if (!is_server_valid(current->domref))
		    {
			if (current->cancelable)
			    goto advance;
			/* Otherwise, pretend you were a  "GENERAL" type */
		    }
		    else
		    {
			from_server = current->domref;
			make_window_current_by_refnum(get_server_current_window(from_server));
		    }
		}
		else if (current->domain == WINDOW_TIMER)
		{
		    if (get_window_refnum(current->domref) < 1)
		    {
			if (current->cancelable)
			    goto advance;
			/* Otherwise, pretend you were a "GENERAL" type */
		    }
		    else
		    {
			make_window_current_by_refnum(current->domref);
			from_server = get_window_server(0);
		    }
		}
		else
		{
		    /* General timers focus on the current window. */
		    if (get_window_refnum(0) > 0)
		    {
			if (get_window_server(0) != from_server)
			    from_server = get_window_server(0);
		    }
		    else
		    {
			if (from_server != NOSERV)
			    make_window_current_by_refnum(get_server_current_window(from_server));
		    }
		}

		/* 
		 * If a callback function was registered, then
		 * we use it.  If no callback function was registered,
		 * then we call the lambda function.
		 */
		get_time(&right_now);
		now = right_now;
		if (current->callback)
			(*current->callback)(current->callback_data);
		else
			call_lambda_command("TIMER", current->command,
							current->subargs);

		from_server = old_from_server;
		make_window_current_by_refnum(old_refnum);
advance:
		delete_timer(current);
	}
}


/* Used by function_timerctl */
/*
 * $timerctl(REFNUM refnum)
 * $timerctl(ADD <refnum> <interval> <events> <commands> <subargs> <window>)
 * $timerctl(DELETE <refnum>)
 * $timerctl(GET <refnum> [LIST])
 * $timerctl(SET <refnum> [ITEM] [VALUE])
 * $timerctl(REFNUMS)
 *
 * [LIST] and [ITEM] are one of the following
 *	TIMEOUT		The precise time the timer will be executed
 *	COMMAND		The commands that will be executed
 *	SUBARGS		The vaule of $* used when this timer is executed
 *	REPEATS		The number of times this timer will be executed
 *	INTERVAL	The interval of time between executions
 *	SERVER		The server this timer bound to
 *	WINDOW		The window this timer bound to
 *	PACKAGE		The /load package the timer bound to
 */
char *	timerctl (char *input)
{
	char *	refstr;
	char *	listc;
	Timer *	t;
	int	len;

	GET_FUNC_ARG(listc, input);
	len = strlen(listc);
	if (!my_strnicmp(listc, "REFNUM", len)) {
		GET_FUNC_ARG(refstr, input);
		if (!(t = get_timer(refstr)))
			RETURN_EMPTY;
		RETURN_STR(t->ref);
	} else if (!my_strnicmp(listc, "REFNUMS", len)) {
		char *	retval = NULL;
		for (t = PendingTimers; t; t = t->next)
			malloc_strcat_word(&retval, space, t->ref, DWORD_DWORDS);
		RETURN_MSTR(retval);
	} else if (!my_strnicmp(listc, "ADD", len)) {
		RETURN_EMPTY;		/* XXX - Not implemented yet. */
	} else if (!my_strnicmp(listc, "DELETE", len)) {
		GET_FUNC_ARG(refstr, input);
		if (!(t = get_timer(refstr)))
			RETURN_EMPTY;
		if (t->callback)
			RETURN_EMPTY;
		RETURN_INT(remove_timer(refstr));
	} else if (!my_strnicmp(listc, "GET", len)) {
		GET_FUNC_ARG(refstr, input);
		if (!(t = get_timer(refstr)))
			RETURN_EMPTY;

		GET_FUNC_ARG(listc, input);
		len = strlen(listc);
		if (!my_strnicmp(listc, "TIMEOUT", len)) {
			return malloc_sprintf(NULL, "%ld %ld", (long) t->time.tv_sec,
						    (long) t->time.tv_usec);
		} else if (!my_strnicmp(listc, "COMMAND", len)) {
			if (t->callback)
				RETURN_EMPTY;
			RETURN_STR(t->command);
		} else if (!my_strnicmp(listc, "SUBARGS", len)) {
			if (t->callback)
				RETURN_EMPTY;
			RETURN_STR(t->subargs);
		} else if (!my_strnicmp(listc, "REPEATS", len)) {
			RETURN_INT(t->events);
		} else if (!my_strnicmp(listc, "INTERVAL", len)) {
			return malloc_sprintf(NULL, "%ld %ld", (long) t->interval.tv_sec,
						    (long) t->interval.tv_usec);
		} else if (!my_strnicmp(listc, "SERVER", len)) {
			if (t->domain != SERVER_TIMER)
				RETURN_INT(-1);
			RETURN_INT(t->domref);
		} else if (!my_strnicmp(listc, "WINDOW", len)) {
			if (t->domain != WINDOW_TIMER)
				RETURN_INT(-1);
			RETURN_INT(t->domref);
		} else if (!my_strnicmp(listc, "PACKAGE", len)) {
			RETURN_STR(t->package);
		}
	} else if (!my_strnicmp(listc, "SET", len)) {
		GET_FUNC_ARG(refstr, input);
		if (!(t = get_timer(refstr)))
			RETURN_EMPTY;

		/* Changing internal system timers is strictly prohibited */
		if (t->callback)
			RETURN_EMPTY;

		GET_FUNC_ARG(listc, input);
		len = strlen(listc);
		if (!my_strnicmp(listc, "TIMEOUT", len)) {
			time_t	tv_sec;
			long	tv_usec;

			GET_INT_ARG(tv_sec, input);
			GET_INT_ARG(tv_usec, input);
			t->time.tv_sec = tv_sec;
			t->time.tv_usec = tv_usec;
		} else if (!my_strnicmp(listc, "COMMAND", len)) {
			malloc_strcpy((char **)&t->command, input);
		} else if (!my_strnicmp(listc, "SUBARGS", len)) {
			malloc_strcpy(&t->subargs, input);
		} else if (!my_strnicmp(listc, "REPEATS", len)) {
			long	repeats;

			GET_INT_ARG(repeats, input);
			t->events = repeats;
		} else if (!my_strnicmp(listc, "INTERVAL", len)) {
			time_t	tv_sec;
			long	tv_usec;

			GET_INT_ARG(tv_sec, input);
			GET_INT_ARG(tv_usec, input);
			t->interval.tv_sec = tv_sec;
			t->interval.tv_usec = tv_usec;
		} else if (!my_strnicmp(listc, "SERVER", len)) {
			int	refnum;

			GET_INT_ARG(refnum, input);
			t->domain = SERVER_TIMER;
			t->domref = refnum;
		} else if (!my_strnicmp(listc, "WINDOW", len)) {
			int	refnum;

			GET_INT_ARG(refnum, input);
			t->domain = WINDOW_TIMER;
			t->domref = refnum;
		} else if (!my_strnicmp(listc, "PACKAGE", len)) {
			malloc_strcpy(&t->package, input);
		}
	} else
		RETURN_EMPTY;

	RETURN_EMPTY;
}

/*
 * The /WINDOW NUMBER command actually swaps the refnums of two windows:
 * It's possible that 'newref' isn't in use, so that's ok.
 */
void    timers_swap_windows (unsigned oldref, unsigned newref)
{
	Timer *ref;

	for (ref = PendingTimers; ref; ref = ref->next)
        {
                if (ref->domain != WINDOW_TIMER)
                        continue;

		/* Window refnums are "unsigned",
		 *  but timer domain refnums aren't */
		if (ref->domref == (int)newref)
			ref->domref = oldref;
		else if (ref->domref == (int)oldref)
			ref->domref = newref;
        }
}

void    timers_merge_windows (unsigned oldref, unsigned newref)
{
	Timer *ref;

	for (ref = PendingTimers; ref; ref = ref->next)
        {
                if (ref->domain != WINDOW_TIMER)
                        continue;

		if (ref->domref == (int)oldref)
			ref->domref = newref;
        }
}

void	unload_timers (char *filename)
{
	Timer *ref;

	for (ref = PendingTimers; ref; )
	{
		if (!my_stricmp(ref->package, filename))
		{
			unlink_timer(ref);
			delete_timer(ref);
			ref = PendingTimers;
		}
		else
			ref = ref->next;
	}
}