File: notify.c

package info (click to toggle)
epic4 1%3A2.6-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,612 kB
  • ctags: 5,402
  • sloc: ansic: 55,998; makefile: 656; sh: 158; perl: 30
file content (644 lines) | stat: -rw-r--r-- 15,350 bytes parent folder | download | duplicates (10)
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
/* $EPIC: notify.c,v 1.25 2003/11/20 23:38:13 jnelson Exp $ */
/*
 * notify.c: a few handy routines to notify you when people enter and leave irc 
 *
 * Copyright (c) 1990 Michael Sandroff.
 * Copyright (c) 1991, 1992 Troy Rollo.
 * Copyright (c) 1992-1996 Matthew Green.
 * Copyright  1997, 2003 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.
 */

/*
 * Revamped by lynX - Dec '91
 * Expanded to allow notify on every server by hop, Feb 1996.
 * Mostly rewritten by jfn (esl), Dec 1997.
 */

#define NEED_SERVER_LIST
#include "irc.h"
#include "alist.h"
#include "notify.h"
#include "ircaux.h"
#include "hook.h"
#include "ssl.h"
#include "server.h"
#include "output.h"
#include "parse.h"
#include "vars.h"
#include "who.h"
#include "clock.h"
#include "timer.h"
#include "window.h"
#include "input.h"

void 	batch_notify_userhost		(const char *nick);
void 	dispatch_notify_userhosts	(int);
void 	notify_userhost_dispatch	(int, UserhostItem *f, const char *, const char *);
void 	notify_userhost_reply		(int, const char *nick, const char *uh);

/* NotifyList: the structure for the notify stuff */
typedef	struct	notify_stru
{
	char	*nick;			/* Who are we watching? */
	u_32int_t hash;			/* Hash of the nick */
	int	flag;			/* Is the person on irc? */
} NotifyItem;

#if 0		/* Declared in notify.h */
typedef struct	notify_alist
{
	NotifyItem **	list;
	int		max;
	int		max_alloc;
	alist_func	func;
	hash_type	hash;
	char *		ison;
} NotifyList;
#endif

static 	void	ison_notify (int refnum, char *AskedFor, char *AreOn);
static 	void	rebuild_notify_ison 	(int server);


#define NOTIFY_LIST(s)		(&(s->notify_list))
#define NOTIFY_MAX(s)  		(s->notify_list.max)
#define NOTIFY_ITEM(s, i) 	(s->notify_list.list[i])

static void	show_notify_list (int all)
{
	Server *s;
	int	i;
	char	*list = (char *) 0;
	size_t	clue;

	if (!(s = get_server(from_server)))
		return;

	for (i = 0, clue = 0; i < NOTIFY_MAX(s); i++)
	{
		if (NOTIFY_ITEM(s, i)->flag)
		    malloc_strcat_wordlist_c(&list, space, NOTIFY_ITEM(s, i)->nick, &clue);
	}

	if (list)
		say("Currently present: %s", list);

	if (all)
	{
		new_free(&list);
		for (i = 0, clue = 0; i < NOTIFY_MAX(s); i++)
		{
			if (!NOTIFY_ITEM(s, i)->flag)
			    malloc_strcat_wordlist_c(&list, space, NOTIFY_ITEM(s, i)->nick, &clue);
		}
		if (list) 
			say("Currently absent: %s", list);
	}
	new_free(&list);
}

static	void	rebuild_all_ison (void)
{
	int i;

	for (i = 0; i < number_of_servers; i++)
		rebuild_notify_ison(i);
}

static void	rebuild_notify_ison (int refnum)
{
	Server *s;
	char *stuff;
	int i;
	size_t clue = 0;

	if (!(s = get_server(refnum)))
		return;		/* No server, no go */

	stuff = NOTIFY_LIST(s)->ison;

	if (NOTIFY_LIST(s)->ison)
		NOTIFY_LIST(s)->ison[0] = 0;

	for (i = 0; i < NOTIFY_MAX(s); i++)
	{
		malloc_strcat_wordlist_c(&(NOTIFY_LIST(s)->ison),
			space, NOTIFY_ITEM(s, i)->nick, &clue);
	}
}


/* notify: the NOTIFY command.  Does the whole ball-o-wax */
BUILT_IN_COMMAND(notify)
{
	Server		*s;
	char		*nick,
			*list = (char *) 0,
			*ptr;
	int		no_nicks = 1;
	int		do_ison = 0;
	int		shown = 0;
	NotifyItem	*new_n;
	int		refnum;
	int		added = 0;
	size_t		clue = 0;

	malloc_strcpy(&list, empty_string);
	while ((nick = next_arg(args, &args)))
	{
	    for (no_nicks = 0; nick; nick = ptr)
	    {
		shown = 0;
		if ((ptr = strchr(nick, ',')))
		    *ptr++ = '\0';

		if (*nick == '-')
		{
		    nick++;
		    if (*nick)
		    {
			for (refnum = 0; refnum < number_of_servers; refnum++)
			{
			    if (!(s = get_server(refnum)))
				continue;

			    if ((new_n = (NotifyItem *)remove_from_array(
					(array *)NOTIFY_LIST(s), nick)))
			    {
				new_free(&(new_n->nick));
				new_free((char **)&new_n);

				if (!shown)
				{
				    say("%s removed from notify list", nick);
				    shown = 1;
				}
			    }
			    else
			    {
				if (!shown)
				{
				    say("%s is not on the notify list", nick);
				    shown = 1;
				}
			    }
			}
		    }
		    else
		    {
			for (refnum = 0; refnum < number_of_servers; refnum++)
			{
			    if (!(s = get_server(refnum)))
				continue;

			    while ((new_n = (NotifyItem *)array_pop(
						(array *)NOTIFY_LIST(s), 0)))
			    {
				new_free(&new_n->nick);
				new_free((char **)&new_n);
			    }
			}
			say("Notify list cleared");
		    }
		}
		else
		{
		    /* compatibility */
		    if (*nick == '+')
			nick++;

		    if (!*nick)
		    {
			show_notify_list(0);
			continue;
		    }

		    if (strchr(nick, '*'))
		    {
			say("Wildcards not allowed in NOTIFY nicknames!");
			continue;
		    }

		    for (refnum = 0; refnum < number_of_servers; refnum++)
		    {
			if (!(s = get_server(refnum)))
			    continue;

			if ((new_n = (NotifyItem *)array_lookup(
					(array *)NOTIFY_LIST(s), nick, 0, 0)))
			{
			    continue;	/* Already there! */
			}

			new_n = (NotifyItem *)new_malloc(sizeof(NotifyItem));
			new_n->nick = malloc_strdup(nick);
			new_n->flag = 0;
			add_to_array((array *)NOTIFY_LIST(s), 
					(array_item *)new_n);
			added = 1;
		     }

		    if (added)
		    {
			malloc_strcat_wordlist_c(&list, space, new_n->nick, &clue);
			do_ison = 1;
		    }

		    say("%s added to the notification list", nick);
		}
	    } /* End of for */
	} /* End of while */

	if (do_ison && get_int_var(DO_NOTIFY_IMMEDIATELY_VAR))
	{
	    for (refnum = 0; refnum < number_of_servers; refnum++)
	    {
		if (!(s = get_server(refnum)))
		    continue;

		if (is_server_registered(refnum))
		    if (list && *list)
			isonbase(refnum, list, ison_notify);
	    }
	}

	new_free(&list);
	rebuild_all_ison();

	if (no_nicks)
	    show_notify_list(1);
}

static void	ison_notify (int refnum, char *AskedFor, char *AreOn)
{
	char	*NextAsked;
	char	*NextGot;

	if (x_debug & DEBUG_NOTIFY)
		yell("Checking notify: we asked for [%s] and we got back [%s]", AskedFor, AreOn);

	NextGot = next_arg(AreOn, &AreOn);
	if (x_debug & DEBUG_NOTIFY)
		yell("Looking for [%s]", NextGot ? NextGot : "<Nobody>");
	while ((NextAsked = next_arg(AskedFor, &AskedFor)) != NULL)
	{
		if (NextGot && !my_stricmp(NextAsked, NextGot))
		{
			if (x_debug & DEBUG_NOTIFY)
				yell("OK.  Found [%s].", NextAsked);
			notify_mark(refnum, NextAsked, 1, 1);
			NextGot = next_arg(AreOn, &AreOn);
			if (x_debug & DEBUG_NOTIFY)
				yell("Looking for [%s]", NextGot ? NextGot : "<Nobody>");
		}
		else
		{
			if (x_debug & DEBUG_NOTIFY)
				yell("Well, [%s] doesnt look to be on.", NextAsked);
			notify_mark(refnum, NextAsked, 0, 1);
		}
	}

	if (x_debug & DEBUG_NOTIFY)
		if (AreOn && *AreOn)
			yell("Hrm.  There's stuff left in AreOn, and there shouldn't be. [%s]", AreOn);

	dispatch_notify_userhosts(refnum);
}

/*
 * do_notify:  This goes through the master notify list and collects it
 * into groups of 500 character sets.  Then it sends this out to each 
 * connected server.  It repeats this until the whole list has been parsed.
 */
void 	do_notify (void)
{
	Server 		*s;
	int 		old_from_server = from_server;
	int		servnum;
#if 0
static	time_t		last_notify = 0;
	int		interval = get_int_var(NOTIFY_INTERVAL_VAR);
#endif

	if (!number_of_servers)
		return;

	if (x_debug & DEBUG_NOTIFY)
		yell("do_notify() was called...");

#if 0
	if (time(NULL) < last_notify)
		last_notify = time(NULL);
	else if (!interval || interval > (time(NULL) - last_notify))
	{
		if (x_debug & DEBUG_NOTIFY)
			yell("Not time for notify yet [%ld] [%ld]",
				time(NULL), last_notify+interval);

		return;		/* Not yet */
	}
	last_notify = time(NULL);
#endif

	for (servnum = 0; servnum < number_of_servers; servnum++)
	{
		if (!(s = get_server(servnum)))
			continue;

		if (!is_server_registered(servnum))
		{
		    if (x_debug & DEBUG_NOTIFY)
			yell("Server [%d] is not connected so we "
			     "will not issue an ISON for it.", servnum);
		    continue;
		}

		from_server = servnum;
		if (NOTIFY_LIST(s)->ison && *NOTIFY_LIST(s)->ison)
		{
			isonbase(servnum, NOTIFY_LIST(s)->ison, ison_notify);
			if (x_debug & DEBUG_NOTIFY)
			    yell("Notify ISON issued for server [%d]", servnum);
		}
		else if (NOTIFY_MAX(s))
		{
			if (x_debug & DEBUG_NOTIFY)
				yell("Server [%d]'s notify list is"
					"empty and it shouldn't be.",
						servnum);
			rebuild_all_ison();
		}
		else if (x_debug & DEBUG_NOTIFY)
			yell("Server [%d]'s notify list is empty.",
				servnum);
	}

	if (x_debug & DEBUG_NOTIFY)
		yell("do_notify() is all done...");
	from_server = old_from_server;
	return;
}

/*
 * notify_mark: This marks a given person on the notify list as either on irc
 * (if flag is 1), or not on irc (if flag is 0).  If the person's status has
 * changed since the last check, a message is displayed to that effect.  If
 * the person is not on the notify list, this call is ignored 
 * doit if passed as 0 means it comes from a join, or a msg, etc, not from
 * an ison reply.  1 is the other..
 *
 * We implicitly assume that if this is called with 'doit' set to 0 that
 * the caller has already "registered" the user.  What that means is that
 * when the caller does a notify_mark for a single user, a USERHOST request
 * for that user should yeild cached userhost information.  This is a terrible
 * thing to assume, but it wont break if cached userhosts go away, we'd just
 * be doing an extra server request -- which we're already doing anyhow! --
 * so in the worst case this cant be any worse than it is already.
 */
void 	notify_mark (int refnum, const char *nick, int flag, int doit)
{
	Server		*s;
	NotifyItem 	*tmp;
	int		count;
	int		loc;

	if (!(s = get_server(refnum)))
		return;

	if ((tmp = (NotifyItem *)find_array_item(
				(array *)NOTIFY_LIST(s), nick, 
				&count, &loc)) && count < 0)
	{
		if (flag)
		{
			if (tmp->flag != 1)
			{
			    if (get_int_var(NOTIFY_USERHOST_AUTOMATIC_VAR))
			        batch_notify_userhost(nick);
			    else
				notify_userhost_reply(refnum, nick, NULL);
			}
			tmp->flag = 1;

			if (!doit)
				dispatch_notify_userhosts(refnum);
		}
		else
		{
			if (tmp->flag == 1)
			{
			    if (do_hook(NOTIFY_SIGNOFF_LIST, "%s", nick))
				say("Signoff by %s detected", nick);
			}
			tmp->flag = 0;
		}
	}
	else
	{
		if (x_debug & DEBUG_NOTIFY)
			yell("Notify_mark called for [%s], they dont seem to exist!", nick);
	}
}


static char *	batched_notify_userhosts = NULL;
static int 	batched_notifies = 0;

void 	batch_notify_userhost (const char *nick)
{
	malloc_strcat_wordlist(&batched_notify_userhosts, space, nick);
	batched_notifies++;
}

void 	dispatch_notify_userhosts (int refnum)
{
	if (batched_notify_userhosts)
	{
		if (x_debug & DEBUG_NOTIFY)
			yell("Dispatching notifies to server [%d], [%s]", from_server, batched_notify_userhosts);
		userhostbase(refnum, batched_notify_userhosts, notify_userhost_dispatch, 1);
		new_free(&batched_notify_userhosts);
		batched_notifies = 0;
	}
}

void 	notify_userhost_dispatch (int refnum, UserhostItem *stuff, const char *nick, const char *text)
{
	char uh[BIG_BUFFER_SIZE + 1];

	snprintf(uh, sizeof uh, "%s@%s", stuff->user, stuff->host);
	notify_userhost_reply(refnum, stuff->nick, uh);
}

void 	notify_userhost_reply (int refnum, const char *nick, const char *uh)
{
	Server *s;
	NotifyItem *tmp;

	if (!uh)
		uh = empty_string;

	if (!(s = get_server(refnum)))
		return;

	if ((tmp = (NotifyItem *)array_lookup((array *)NOTIFY_LIST(s), nick, 0, 0)))
	{
		if (do_hook(NOTIFY_SIGNON_LIST, "%s %s", nick, uh))
		{
			if (!*uh)
				say("Signon by %s!%s detected", nick, uh);
			else
				say("Signon by %s detected", nick);
		}

		/*
		 * copy the correct case of the nick
		 * into our array  ;)
		 */
		malloc_strcpy(&tmp->nick, nick);
		malloc_strcpy(&last_notify_nick, nick);
	}
}


void 	save_notify (FILE *fp)
{
	Server *s = NULL;
	int i;

	for (i = 0; i < number_of_servers; i++)
		if ((s = get_server(i)) && NOTIFY_MAX(s))
			break;
	if (!s)
		return;			/* No notify list. */

	fprintf(fp, "NOTIFY");
	for (i = 0; i < NOTIFY_MAX(s); i++)
		fprintf(fp, " %s", NOTIFY_ITEM(s, i)->nick);
	fprintf(fp, "\n");
}

void 	make_notify_list (int refnum)
{
	Server *s, *sp = NULL;
	NotifyItem *tmp;
	char *list = NULL;
	int i;
	size_t clue = 0;

	if (!(s = get_server(refnum)))
		return;

	s->notify_list.list = NULL;
	s->notify_list.max = 0;
	s->notify_list.max_alloc = 0;
	s->notify_list.func = (alist_func)my_stricmp;
	s->notify_list.hash = HASH_INSENSITIVE;
	s->notify_list.ison = NULL;

	for (i = 0; i < number_of_servers; i++)
		if ((sp = get_server(i)) && NOTIFY_MAX(sp))
			break;
	if (!sp)
		return;			/* No notify list to copy. */

	for (i = 0; i < NOTIFY_MAX(sp); i++)
	{
		tmp = (NotifyItem *)new_malloc(sizeof(NotifyItem));
		tmp->nick = malloc_strdup(NOTIFY_ITEM(sp, i)->nick);
		tmp->flag = 0;

		add_to_array ((array *)NOTIFY_LIST(s), (array_item *)tmp);
		malloc_strcat_wordlist_c(&list, space, tmp->nick, &clue);
	}

	if (list)
	{
		isonbase(refnum, list, ison_notify);
		new_free(&list);
	}
}

void	destroy_notify_list (int refnum)
{
	Server *s;
	NotifyItem *	item;

	if (!(s = get_server(refnum)))
		return;

	while (NOTIFY_MAX(s))
	{
		item = (NotifyItem *) array_pop((array *)NOTIFY_LIST(s), 0);
		if (item) 
			new_free(&item->nick);
		new_free(&item);
	}
	new_free(&(NOTIFY_LIST(s)->ison));
	new_free(NOTIFY_LIST(s));
}

char *	get_notify_nicks (int refnum, int showon)
{
	Server *s;
	char *list = NULL;
	int i;
	size_t rvclue=0;

	if (!(s = get_server(refnum)))
		return malloc_strdup(empty_string);

	for (i = 0; i < NOTIFY_MAX(s); i++)
	{
		if (showon == -1 || showon == NOTIFY_ITEM(s, i)->flag)
			malloc_strcat_wordlist_c(&list, space, NOTIFY_ITEM(s, i)->nick, &rvclue);
	}

	return (list ? list : malloc_strdup(empty_string));
}

/***************************************************************************/
char 	notify_timeref[] = "NOTTIM";

void	notify_systimer (void)
{
	do_notify();
}

void    set_notify_interval (const void *stuff)
{
	update_system_timer(notify_timeref);
}

void    set_notify (const void *stuff)
{
	update_system_timer(notify_timeref);
}