/[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.113 - (show annotations) (download) (as text)
Sun Apr 28 05:53:33 2002 UTC (17 years, 2 months ago) by ite
Branch: MAIN
Changes since 1.112: +9 -1 lines
File MIME type: text/x-chdr
Allow to preload a module from the command line.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23