/[cvs]/eggdrop1.9/src/main.c
ViewVC logotype

Contents of /eggdrop1.9/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.104 - (show annotations) (download) (as text)
Mon Jan 14 02:23:27 2002 UTC (17 years, 6 months ago) by ite
Branch: MAIN
Changes since 1.103: +3 -5 lines
File MIME type: text/x-chdr
* Merged bits of tothwolf cleaunps from his alpha patch.
* Removed some unneeded externs in some files.

1 /*
2 * main.c -- handles:
3 * core event handling
4 * signal handling
5 * command line arguments
6 *
7 * $Id: main.c,v 1.103 2001/12/29 21:21:18 guppy Exp $
8 */
9 /*
10 * Copyright (C) 1997 Robey Pointer
11 * Copyright (C) 1999, 2000, 2001 Eggheads Development Team
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 */
27 /*
28 * The author (Robey Pointer) can be reached at: robey@netcom.com
29 * NOTE: Robey is no long working on this code, there is a discussion
30 * list available at eggheads@eggheads.org.
31 */
32
33 #include "main.h"
34 #include <fcntl.h>
35 #include <time.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <netdb.h>
39 #include <setjmp.h>
40
41 #include <locale.h>
42
43 #ifdef STOP_UAC /* osf/1 complains a lot */
44 #include <sys/sysinfo.h>
45 #define UAC_NOPRINT 0x00000001 /* Don't report unaligned fixups */
46 #endif
47 /* Some systems have a working sys/wait.h even though configure will
48 * decide it's not bsd compatable. Oh well.
49 */
50
51 #include "chan.h"
52 #include "modules.h"
53 #include "tandem.h"
54 #include "bg.h"
55 #include "egg_timer.h"
56 #include "core_binds.h"
57
58 #include "lib/adns/adns.h"
59
60 #ifdef CYGWIN_HACKS
61 #include <windows.h>
62 #endif
63
64 #ifndef _POSIX_SOURCE
65 /* Solaris needs this */
66 #define _POSIX_SOURCE 1
67 #endif
68
69 extern char userfile[], botnetnick[];
70 extern int dcc_total, conmask, cache_hit, cache_miss;
71 extern struct dcc_t *dcc;
72 extern struct userrec *userlist;
73 extern struct chanset_t *chanset;
74 extern jmp_buf alarmret;
75
76
77 /*
78 * Please use the PATCH macro instead of directly altering the version
79 * string from now on (it makes it much easier to maintain patches).
80 * Also please read the README file regarding your rights to distribute
81 * modified versions of this bot.
82 */
83
84 char egg_version[1024] = VERSION;
85 int egg_numver = VERSION_NUM;
86
87 char notify_new[121] = ""; /* Person to send a note to for new users */
88 int default_flags = 0; /* Default user flags and */
89 int default_uflags = 0; /* Default userdefinied flags for people
90 who say 'hello' or for .adduser */
91
92 int backgrd = 1; /* Run in the background? */
93 int con_chan = 0; /* Foreground: constantly display channel
94 stats? */
95 int term_z = 0; /* Foreground: use the terminal as a party
96 line? */
97 char configfile[121] = "eggdrop.conf"; /* Name of the config file */
98 char helpdir[121]; /* Directory of help files (if used) */
99 char textdir[121] = ""; /* Directory for text files that get dumped */
100 time_t online_since; /* Unix-time that the bot loaded up */
101 int make_userfile = 0; /* Using bot in make-userfile mode? (first
102 user to 'hello' becomes master) */
103 char owner[121] = ""; /* Permanent owner(s) of the bot */
104 char pid_file[120]; /* Name of the file for the pid to be
105 stored in */
106 int save_users_at = 0; /* How many minutes past the hour to
107 save the userfile? */
108 int notify_users_at = 0; /* How many minutes past the hour to
109 notify users of notes? */
110 char version[81]; /* Version info (long form) */
111 char ver[41]; /* Version info (short form) */
112 char egg_xtra[2048]; /* Patch info */
113 int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
114 int do_restart = 0; /* .restart has been called, restart asap */
115 int die_on_sighup = 0; /* die if bot receives SIGHUP */
116 int die_on_sigterm = 1; /* die if bot receives SIGTERM */
117 int resolve_timeout = 15; /* hostname/address lookup timeout */
118 char quit_msg[1024]; /* quit message */
119 time_t now; /* duh, now :) */
120
121 /* Traffic stats
122 */
123 unsigned long otraffic_irc = 0;
124 unsigned long otraffic_irc_today = 0;
125 unsigned long otraffic_bn = 0;
126 unsigned long otraffic_bn_today = 0;
127 unsigned long otraffic_dcc = 0;
128 unsigned long otraffic_dcc_today = 0;
129 unsigned long otraffic_filesys = 0;
130 unsigned long otraffic_filesys_today = 0;
131 unsigned long otraffic_trans = 0;
132 unsigned long otraffic_trans_today = 0;
133 unsigned long otraffic_unknown = 0;
134 unsigned long otraffic_unknown_today = 0;
135 unsigned long itraffic_irc = 0;
136 unsigned long itraffic_irc_today = 0;
137 unsigned long itraffic_bn = 0;
138 unsigned long itraffic_bn_today = 0;
139 unsigned long itraffic_dcc = 0;
140 unsigned long itraffic_dcc_today = 0;
141 unsigned long itraffic_trans = 0;
142 unsigned long itraffic_trans_today = 0;
143 unsigned long itraffic_unknown = 0;
144 unsigned long itraffic_unknown_today = 0;
145
146 void fatal(const char *s, int recoverable)
147 {
148 int i;
149
150 putlog(LOG_MISC, "*", "* %s", s);
151 flushlogs();
152 for (i = 0; i < dcc_total; i++)
153 if (dcc[i].sock >= 0)
154 killsock(dcc[i].sock);
155 unlink(pid_file);
156 if (!recoverable) {
157 bg_send_quit(BG_ABORT);
158 exit(1);
159 }
160 }
161
162 static void check_expired_dcc()
163 {
164 int i;
165
166 for (i = 0; i < dcc_total; i++)
167 if (dcc[i].type && dcc[i].type->timeout_val &&
168 ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
169 if (dcc[i].type->timeout)
170 dcc[i].type->timeout(i);
171 else if (dcc[i].type->eof)
172 dcc[i].type->eof(i);
173 else
174 continue;
175 /* Only timeout 1 socket per cycle, too risky for more */
176 return;
177 }
178 }
179
180 static void got_bus(int z)
181 {
182 fatal("BUS ERROR -- CRASHING!", 1);
183 #ifdef SA_RESETHAND
184 kill(getpid(), SIGBUS);
185 #else
186 bg_send_quit(BG_ABORT);
187 exit(1);
188 #endif
189 }
190
191 static void got_segv(int z)
192 {
193 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
194 #ifdef SA_RESETHAND
195 kill(getpid(), SIGSEGV);
196 #else
197 bg_send_quit(BG_ABORT);
198 exit(1);
199 #endif
200 }
201
202 static void got_fpe(int z)
203 {
204 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
205 }
206
207 static void got_term(int z)
208 {
209 write_userfile(-1);
210 check_bind_event("sigterm");
211 if (die_on_sigterm) {
212 fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
213 } else {
214 putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
215 }
216 }
217
218 static void got_quit(int z)
219 {
220 check_bind_event("sigquit");
221 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
222 return;
223 }
224
225 static void got_hup(int z)
226 {
227 write_userfile(-1);
228 check_bind_event("sighup");
229 if (die_on_sighup) {
230 fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
231 } else
232 putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
233 do_restart = -2;
234 return;
235 }
236
237 /* A call to resolver (gethostbyname, etc) timed out
238 */
239 static void got_alarm(int z)
240 {
241 longjmp(alarmret, 1);
242
243 /* -Never reached- */
244 }
245
246 /* Got ILL signal
247 */
248 static void got_ill(int z)
249 {
250 check_bind_event("sigill");
251 }
252
253 static void do_arg(char *s)
254 {
255 char x[1024], *z = x;
256 int i;
257
258 if (s[0] == '-')
259 for (i = 1; i < strlen(s); i++) {
260 switch (s[i]) {
261 case 'n':
262 backgrd = 0;
263 break;
264 case 'c':
265 con_chan = 1;
266 term_z = 0;
267 break;
268 case 't':
269 con_chan = 0;
270 term_z = 1;
271 break;
272 case 'm':
273 make_userfile = 1;
274 break;
275 case 'v':
276 strncpyz(x, egg_version, sizeof x);
277 newsplit(&z);
278 newsplit(&z);
279 printf("%s\n", version);
280 if (z[0])
281 printf(" (patches: %s)\n", z);
282 bg_send_quit(BG_ABORT);
283 exit(0);
284 break; /* this should never be reached */
285 case 'h':
286 printf("\n%s\n\n", version);
287 printf(_("Command line arguments:\n\
288 -h help\n\
289 -v print version and exit\n\
290 -n don't go into the background\n\
291 -c (with -n) display channel stats every 10 seconds\n\
292 -t (with -n) use terminal to simulate dcc-chat\n\
293 -m userfile creation mode\n\
294 optional config filename (default 'eggdrop.conf')\n"));
295 printf("\n");
296 bg_send_quit(BG_ABORT);
297 exit(0);
298 break; /* this should never be reached */
299 }
300 } else
301 strncpyz(configfile, s, sizeof configfile);
302 }
303
304 void backup_userfile(void)
305 {
306 char s[125];
307
308 putlog(LOG_MISC, "*", _("Backing up user file..."));
309 snprintf(s, sizeof s, "%s~bak", userfile);
310 copyfile(userfile, s);
311 }
312
313 /* Timer info */
314 static int lastmin = 99;
315 static time_t then;
316 static struct tm nowtm;
317
318 /* Called once a second.
319 */
320 static void core_secondly()
321 {
322 static int cnt = 0;
323 int miltime;
324
325 call_hook(HOOK_SECONDLY); /* Will be removed later */
326 cnt++;
327 if (cnt >= 10) { /* Every 10 seconds */
328 cnt = 0;
329 check_expired_dcc();
330 if (con_chan && !backgrd) {
331 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
332 tell_verbose_status(DP_STDOUT);
333 do_module_report(DP_STDOUT, 0, "server");
334 do_module_report(DP_STDOUT, 0, "channels");
335 }
336 }
337 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
338 if (nowtm.tm_min != lastmin) {
339 int i = 0;
340
341 /* Once a minute */
342 lastmin = (lastmin + 1) % 60;
343 call_hook(HOOK_MINUTELY);
344 check_expired_ignores();
345 autolink_cycle(NULL); /* Attempt autolinks */
346 /* In case for some reason more than 1 min has passed: */
347 while (nowtm.tm_min != lastmin) {
348 /* Timer drift, dammit */
349 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
350 i++;
351 lastmin = (lastmin + 1) % 60;
352 call_hook(HOOK_MINUTELY);
353 }
354 if (i > 1)
355 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
356 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
357 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
358 call_hook(HOOK_5MINUTELY);
359 if (!miltime) { /* At midnight */
360 char s[25];
361
362 strncpyz(s, ctime(&now), sizeof s);
363 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
364 call_hook(HOOK_BACKUP);
365 call_hook(HOOK_DAILY);
366 }
367 }
368 if (nowtm.tm_min == notify_users_at)
369 call_hook(HOOK_HOURLY);
370 }
371 }
372
373 static void core_minutely()
374 {
375 check_bind_time(&nowtm);
376 }
377
378 static void core_hourly()
379 {
380 write_userfile(-1);
381 }
382
383 static void event_rehash()
384 {
385 check_bind_event("rehash");
386 }
387
388 static void event_prerehash()
389 {
390 check_bind_event("prerehash");
391 }
392
393 static void event_save()
394 {
395 check_bind_event("save");
396 }
397
398 static void event_logfile()
399 {
400 check_bind_event("logfile");
401 }
402
403 static void event_resettraffic()
404 {
405 otraffic_irc += otraffic_irc_today;
406 itraffic_irc += itraffic_irc_today;
407 otraffic_bn += otraffic_bn_today;
408 itraffic_bn += itraffic_bn_today;
409 otraffic_dcc += otraffic_dcc_today;
410 itraffic_dcc += itraffic_dcc_today;
411 otraffic_unknown += otraffic_unknown_today;
412 itraffic_unknown += itraffic_unknown_today;
413 otraffic_trans += otraffic_trans_today;
414 itraffic_trans += itraffic_trans_today;
415 otraffic_irc_today = otraffic_bn_today = 0;
416 otraffic_dcc_today = otraffic_unknown_today = 0;
417 itraffic_irc_today = itraffic_bn_today = 0;
418 itraffic_dcc_today = itraffic_unknown_today = 0;
419 itraffic_trans_today = otraffic_trans_today = 0;
420 }
421
422 static void event_loaded()
423 {
424 check_bind_event("loaded");
425 }
426
427 void kill_tcl();
428 extern module_entry *module_list;
429 void restart_chons();
430
431 int init_userent(), init_net(),
432 init_modules(), init_tcl(int, char **);
433 void timer_init();
434 void logfile_init();
435 void botnet_init();
436 void dns_init();
437 void script_init();
438 void binds_init();
439 void dcc_init();
440
441 void patch(const char *str)
442 {
443 char *p = strchr(egg_version, '+');
444
445 if (!p)
446 p = &egg_version[strlen(egg_version)];
447 sprintf(p, "+%s", str);
448 egg_numver++;
449 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
450 }
451
452 /*
453 static inline void garbage_collect(void)
454 {
455 static u_8bit_t run_cnt = 0;
456
457 if (run_cnt == 3)
458 garbage_collect_tclhash();
459 else
460 run_cnt++;
461 }
462 */
463
464 int main(int argc, char **argv)
465 {
466 int xx, i;
467 char buf[520], s[25];
468 FILE *f;
469 struct sigaction sv;
470 struct chanset_t *chan;
471 egg_timeval_t howlong;
472
473 #ifdef DEBUG
474 /* Make sure it can write core, if you make debug. Else it's pretty
475 * useless (dw)
476 */
477 {
478 #include <sys/resource.h>
479 struct rlimit cdlim;
480
481 cdlim.rlim_cur = RLIM_INFINITY;
482 cdlim.rlim_max = RLIM_INFINITY;
483 setrlimit(RLIMIT_CORE, &cdlim);
484 }
485 #endif
486
487 #ifdef ENABLE_NLS
488 setlocale(LC_MESSAGES, "");
489 bindtextdomain(PACKAGE, LOCALEDIR);
490 textdomain(PACKAGE);
491 #endif
492
493 #include "patch.h"
494 /* Version info! */
495 snprintf(ver, sizeof ver, "eggdrop v%s", egg_version);
496 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2001 Eggheads",
497 egg_version);
498 /* Now add on the patchlevel (for Tcl) */
499 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
500 strcat(egg_version, egg_xtra);
501 #ifdef STOP_UAC
502 {
503 int nvpair[2];
504
505 nvpair[0] = SSIN_UACPROC;
506 nvpair[1] = UAC_NOPRINT;
507 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
508 }
509 #endif
510
511 /* Set up error traps: */
512 sv.sa_handler = got_bus;
513 sigemptyset(&sv.sa_mask);
514 #ifdef SA_RESETHAND
515 sv.sa_flags = SA_RESETHAND;
516 #else
517 sv.sa_flags = 0;
518 #endif
519 sigaction(SIGBUS, &sv, NULL);
520 sv.sa_handler = got_segv;
521 sigaction(SIGSEGV, &sv, NULL);
522 #ifdef SA_RESETHAND
523 sv.sa_flags = 0;
524 #endif
525 sv.sa_handler = got_fpe;
526 sigaction(SIGFPE, &sv, NULL);
527 sv.sa_handler = got_term;
528 sigaction(SIGTERM, &sv, NULL);
529 sv.sa_handler = got_hup;
530 sigaction(SIGHUP, &sv, NULL);
531 sv.sa_handler = got_quit;
532 sigaction(SIGQUIT, &sv, NULL);
533 sv.sa_handler = SIG_IGN;
534 sigaction(SIGPIPE, &sv, NULL);
535 sv.sa_handler = got_ill;
536 sigaction(SIGILL, &sv, NULL);
537 sv.sa_handler = got_alarm;
538 sigaction(SIGALRM, &sv, NULL);
539
540 /* Initialize variables and stuff */
541 now = time(NULL);
542 chanset = NULL;
543 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
544 lastmin = nowtm.tm_min;
545 srandom(now % (getpid() + getppid()));
546 if (argc > 1)
547 for (i = 1; i < argc; i++)
548 do_arg(argv[i]);
549 printf("\n%s\n", version);
550 printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
551
552 /* Don't allow eggdrop to run as root */
553 if (((int) getuid() == 0) || ((int) geteuid() == 0))
554 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
555
556 script_init();
557 binds_init();
558 init_modules();
559 logfile_init();
560 timer_init();
561 dns_init();
562 core_binds_init();
563 dcc_init();
564 init_userent();
565 botnet_init();
566 init_net();
567
568 if (backgrd)
569 bg_prepare_split();
570
571 init_tcl(argc, argv);
572
573 strncpyz(s, ctime(&now), sizeof s);
574 strcpy(&s[11], &s[20]);
575 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
576 chanprog();
577 if (!encrypt_pass) {
578 printf(_("You have installed modules but have not selected an encryption\n\
579 module, please consult the default config file for info.\n"));
580 bg_send_quit(BG_ABORT);
581 exit(1);
582 }
583 i = 0;
584 for (chan = chanset; chan; chan = chan->next)
585 i++;
586 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
587 botnetnick, i, count_users(userlist));
588 cache_miss = 0;
589 cache_hit = 0;
590
591 if (!pid_file[0])
592 snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
593
594 /* Check for pre-existing eggdrop! */
595 f = fopen(pid_file, "r");
596 if (f != NULL) {
597 fgets(s, 10, f);
598 xx = atoi(s);
599 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
600 is used */
601 if (errno != ESRCH) {
602 printf(_("I detect %s already running from this directory.\n"), botnetnick);
603 printf(_("If this is incorrect, erase the %s\n"), pid_file);
604 bg_send_quit(BG_ABORT);
605 exit(1);
606 }
607 }
608
609 /* Move into background? */
610 if (backgrd) {
611 #ifndef CYGWIN_HACKS
612 bg_do_split();
613 } else { /* !backgrd */
614 #endif
615 xx = getpid();
616 if (xx != 0) {
617 FILE *fp;
618
619 /* Write pid to file */
620 unlink(pid_file);
621 fp = fopen(pid_file, "w");
622 if (fp != NULL) {
623 fprintf(fp, "%u\n", xx);
624 if (fflush(fp)) {
625 /* Let the bot live since this doesn't appear to be a botchk */
626 printf(_("* Warning! Could not write %s file!\n"), pid_file);
627 fclose(fp);
628 unlink(pid_file);
629 } else
630 fclose(fp);
631 } else
632 printf(_("* Warning! Could not write %s file!\n"), pid_file);
633 #ifdef CYGWIN_HACKS
634 printf("Launched into the background (pid: %d)\n\n", xx);
635 #endif
636 }
637 }
638
639 use_stderr = 0; /* Stop writing to stderr now */
640 if (backgrd) {
641 /* Ok, try to disassociate from controlling terminal (finger cross) */
642 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
643 setpgid(0, 0);
644 #endif
645 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
646 freopen("/dev/null", "r", stdin);
647 freopen("/dev/null", "w", stdout);
648 freopen("/dev/null", "w", stderr);
649 #ifdef CYGWIN_HACKS
650 FreeConsole();
651 #endif
652 }
653
654 /* Terminal emulating dcc chat */
655 if (!backgrd && term_z) {
656 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
657
658 dcc[n].addr[0] = '\0';
659 dcc[n].sock = STDOUT;
660 dcc[n].timeval = now;
661 dcc[n].u.chat->con_flags = conmask;
662 dcc[n].u.chat->strip_flags = STRIP_ALL;
663 dcc[n].status = STAT_ECHO;
664 strcpy(dcc[n].nick, "HQ");
665 strcpy(dcc[n].host, "llama@console");
666 dcc[n].user = get_user_by_handle(userlist, "HQ");
667 /* Make sure there's an innocuous HQ user if needed */
668 if (!dcc[n].user) {
669 userlist = adduser(userlist, "HQ", "none", "-", USER_PARTY);
670 dcc[n].user = get_user_by_handle(userlist, "HQ");
671 }
672 setsock(STDOUT, 0); /* Entry in net table */
673 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
674 dcc_chatter(n);
675 }
676
677 then = now;
678 online_since = now;
679 autolink_cycle(NULL); /* Hurry and connect to tandem bots */
680 add_help_reference("cmds1.help");
681 add_help_reference("cmds2.help");
682 add_help_reference("core.help");
683 howlong.sec = 1;
684 howlong.usec = 0;
685 timer_create_repeater(&howlong, (Function) core_secondly);
686 add_hook(HOOK_MINUTELY, (Function) core_minutely);
687 add_hook(HOOK_HOURLY, (Function) core_hourly);
688 add_hook(HOOK_REHASH, (Function) event_rehash);
689 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
690 add_hook(HOOK_USERFILE, (Function) event_save);
691 add_hook(HOOK_BACKUP, (Function) backup_userfile);
692 add_hook(HOOK_DAILY, (Function) event_logfile);
693 add_hook(HOOK_DAILY, (Function) event_resettraffic);
694 add_hook(HOOK_LOADED, (Function) event_loaded);
695
696 call_hook(HOOK_LOADED);
697
698 debug0("main: entering loop");
699 while (1) {
700 int socket_cleanup = 0;
701
702 #if !defined(HAVE_PRE7_5_TCL)
703 /* Process a single tcl event */
704 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
705 #endif
706
707 /* Lets move some of this here, reducing the numer of actual
708 * calls to periodic_timers
709 */
710 now = time(NULL);
711 random(); /* Woop, lets really jumble things */
712 timer_run();
713 if (now != then) { /* Once a second */
714 /* call_hook(HOOK_SECONDLY); */
715 then = now;
716 }
717
718 /* Only do this every so often. */
719 if (!socket_cleanup) {
720 socket_cleanup = 5;
721
722 /* Remove dead dcc entries. */
723 dcc_remove_lost();
724
725 /* Check for server or dcc activity. */
726 dequeue_sockets();
727 } else
728 socket_cleanup--;
729
730 /* Free unused structures. */
731 /* garbage_collect(); */
732
733 xx = sockgets(buf, &i);
734 if (xx >= 0) { /* Non-error */
735 int idx;
736
737 for (idx = 0; idx < dcc_total; idx++)
738 if (dcc[idx].sock == xx) {
739 if (dcc[idx].type && dcc[idx].type->activity) {
740 /* Traffic stats */
741 if (dcc[idx].type->name) {
742 if (!strncmp(dcc[idx].type->name, "BOT", 3))
743 itraffic_bn_today += strlen(buf) + 1;
744 else if (!strcmp(dcc[idx].type->name, "SERVER"))
745 itraffic_irc_today += strlen(buf) + 1;
746 else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
747 itraffic_dcc_today += strlen(buf) + 1;
748 else if (!strncmp(dcc[idx].type->name, "FILES", 5))
749 itraffic_dcc_today += strlen(buf) + 1;
750 else if (!strcmp(dcc[idx].type->name, "SEND"))
751 itraffic_trans_today += strlen(buf) + 1;
752 else if (!strncmp(dcc[idx].type->name, "GET", 3))
753 itraffic_trans_today += strlen(buf) + 1;
754 else
755 itraffic_unknown_today += strlen(buf) + 1;
756 }
757 dcc[idx].type->activity(idx, buf, i);
758 } else
759 putlog(LOG_MISC, "*",
760 "!!! untrapped dcc activity: type %s, sock %d",
761 dcc[idx].type->name, dcc[idx].sock);
762 break;
763 }
764 } else if (xx == -1) { /* EOF from someone */
765 int idx;
766
767 if (i == STDOUT && !backgrd)
768 fatal("END OF FILE ON TERMINAL", 0);
769 for (idx = 0; idx < dcc_total; idx++)
770 if (dcc[idx].sock == i) {
771 if (dcc[idx].type && dcc[idx].type->eof)
772 dcc[idx].type->eof(idx);
773 else {
774 putlog(LOG_MISC, "*",
775 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
776 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
777 killsock(i);
778 lostdcc(idx);
779 }
780 idx = dcc_total + 1;
781 }
782 if (idx == dcc_total) {
783 putlog(LOG_MISC, "*",
784 "(@) EOF socket %d, not a dcc socket, not anything.", i);
785 close(i);
786 killsock(i);
787 }
788 } else if (xx == -2 && errno != EINTR) { /* select() error */
789 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
790 for (i = 0; i < dcc_total; i++) {
791 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
792 putlog(LOG_MISC, "*",
793 "DCC socket %d (type %d, name '%s') expired -- pfft",
794 dcc[i].sock, dcc[i].type, dcc[i].nick);
795 killsock(dcc[i].sock);
796 lostdcc(i);
797 i--;
798 }
799 }
800 } else if (xx == -3) {
801 call_hook(HOOK_IDLE);
802 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
803 }
804
805 if (do_restart) {
806 if (do_restart == -2)
807 rehash();
808 else {
809 /* Unload as many modules as possible */
810 int f = 1;
811 module_entry *p;
812 Function x;
813 char xx[256];
814
815 /* oops, I guess we should call this event before tcl is restarted */
816 check_bind_event("prerestart");
817
818 while (f) {
819 f = 0;
820 for (p = module_list; p != NULL; p = p->next) {
821 dependancy *d = dependancy_list;
822 int ok = 1;
823
824 while (ok && d) {
825 if (d->needed == p)
826 ok = 0;
827 d = d->next;
828 }
829 if (ok) {
830 strcpy(xx, p->name);
831 if (module_unload(xx, botnetnick) == NULL) {
832 f = 1;
833 break;
834 }
835 }
836 }
837 }
838 p = module_list;
839 if (p && p->next && p->next->next)
840 /* Should be only 2 modules now - blowfish (or some other
841 encryption module) and eggdrop. */
842 putlog(LOG_MISC, "*", _("Stagnant module; there WILL be memory leaks!"));
843 flushlogs();
844 kill_tcl();
845 timer_destroy_all(); /* Destroy all timers. */
846 init_tcl(argc, argv);
847 /* We expect the encryption module as the current module pointed
848 * to by `module_list'.
849 */
850 x = p->funcs[MODCALL_START];
851 /* `NULL' indicates that we just recently restarted. The module
852 * is expected to re-initialise as needed.
853 */
854 x(NULL);
855 rehash();
856 restart_chons();
857 call_hook(HOOK_LOADED);
858 }
859 do_restart = 0;
860 }
861 }
862 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23