/[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.109 - (show annotations) (download) (as text)
Fri Mar 22 16:01:20 2002 UTC (17 years, 5 months ago) by ite
Branch: MAIN
Changes since 1.108: +8 -7 lines
File MIME type: text/x-chdr
* Created eggdrop's library context.
* Moved registry stuff to libeggdrop.
* Some headers vs. prototypes cleanups.

1 /*
2 * main.c -- handles:
3 * core event handling
4 * signal handling
5 * command line arguments
6 *
7 * $Id: main.c,v 1.108 2002/02/25 02:59:50 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 #include "script.h"
60
61 #include "lib/adns/adns.h"
62
63 #ifdef CYGWIN_HACKS
64 #include <windows.h>
65 #endif
66
67 #ifndef _POSIX_SOURCE
68 /* Solaris needs this */
69 #define _POSIX_SOURCE 1
70 #endif
71
72 extern char userfile[], botnetnick[];
73 extern int dcc_total, conmask, cache_hit, cache_miss;
74 extern struct dcc_t *dcc;
75 extern struct userrec *userlist;
76 extern struct chanset_t *chanset;
77 extern jmp_buf alarmret;
78
79
80 /*
81 * Please use the PATCH macro instead of directly altering the version
82 * string from now on (it makes it much easier to maintain patches).
83 * Also please read the README file regarding your rights to distribute
84 * modified versions of this bot.
85 */
86
87 char egg_version[1024] = VERSION;
88 int egg_numver = VERSION_NUM;
89
90 eggdrop_t *egg = NULL; /* Eggdrop's context */
91
92 char notify_new[121] = ""; /* Person to send a note to for new users */
93 int default_flags = 0; /* Default user flags and */
94 int default_uflags = 0; /* Default userdefinied flags for people
95 who say 'hello' or for .adduser */
96
97 int backgrd = 1; /* Run in the background? */
98 int con_chan = 0; /* Foreground: constantly display channel
99 stats? */
100 int term_z = 0; /* Foreground: use the terminal as a party
101 line? */
102 char configfile[121] = "eggdrop.conf"; /* Name of the config file */
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 'v':
282 strncpyz(x, egg_version, sizeof x);
283 newsplit(&z);
284 newsplit(&z);
285 printf("%s\n", version);
286 if (z[0])
287 printf(" (patches: %s)\n", z);
288 bg_send_quit(BG_ABORT);
289 exit(0);
290 break; /* this should never be reached */
291 case 'h':
292 printf("\n%s\n\n", version);
293 printf(_("Command line arguments:\n\
294 -h help\n\
295 -v print version and exit\n\
296 -n don't go into the background\n\
297 -c (with -n) display channel stats every 10 seconds\n\
298 -t (with -n) use terminal to simulate dcc-chat\n\
299 -m userfile creation mode\n\
300 optional config filename (default 'eggdrop.conf')\n"));
301 printf("\n");
302 bg_send_quit(BG_ABORT);
303 exit(0);
304 break; /* this should never be reached */
305 }
306 } else
307 strncpyz(configfile, s, sizeof configfile);
308 }
309
310 void backup_userfile(void)
311 {
312 char s[125];
313
314 putlog(LOG_MISC, "*", _("Backing up user file..."));
315 snprintf(s, sizeof s, "%s~bak", userfile);
316 copyfile(userfile, s);
317 }
318
319 /* Timer info */
320 static int lastmin = 99;
321 static struct tm nowtm;
322
323 /* Called once a second.
324 */
325 static void core_secondly()
326 {
327 static int cnt = 0;
328 int miltime;
329
330 call_hook(HOOK_SECONDLY); /* Will be removed later */
331 cnt++;
332 if (cnt >= 10) { /* Every 10 seconds */
333 cnt = 0;
334 check_expired_dcc();
335 if (con_chan && !backgrd) {
336 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
337 tell_verbose_status(DP_STDOUT);
338 do_module_report(DP_STDOUT, 0, "server");
339 do_module_report(DP_STDOUT, 0, "channels");
340 }
341 }
342 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
343 if (nowtm.tm_min != lastmin) {
344 int i = 0;
345
346 /* Once a minute */
347 lastmin = (lastmin + 1) % 60;
348 call_hook(HOOK_MINUTELY);
349 check_expired_ignores();
350 autolink_cycle(NULL); /* Attempt autolinks */
351 /* In case for some reason more than 1 min has passed: */
352 while (nowtm.tm_min != lastmin) {
353 /* Timer drift, dammit */
354 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
355 i++;
356 lastmin = (lastmin + 1) % 60;
357 call_hook(HOOK_MINUTELY);
358 }
359 if (i > 1)
360 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
361 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
362 if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
363 call_hook(HOOK_5MINUTELY);
364 if (!miltime) { /* At midnight */
365 char s[25];
366
367 strncpyz(s, ctime(&now), sizeof s);
368 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
369 call_hook(HOOK_BACKUP);
370 call_hook(HOOK_DAILY);
371 }
372 }
373 if (nowtm.tm_min == notify_users_at)
374 call_hook(HOOK_HOURLY);
375 }
376 }
377
378 static void core_minutely()
379 {
380 check_bind_time(&nowtm);
381 }
382
383 static void core_hourly()
384 {
385 write_userfile(-1);
386 }
387
388 static void event_rehash()
389 {
390 check_bind_event("rehash");
391 }
392
393 static void event_prerehash()
394 {
395 check_bind_event("prerehash");
396 }
397
398 static void event_save()
399 {
400 check_bind_event("save");
401 }
402
403 static void event_logfile()
404 {
405 check_bind_event("logfile");
406 }
407
408 static void event_resettraffic()
409 {
410 otraffic_irc += otraffic_irc_today;
411 itraffic_irc += itraffic_irc_today;
412 otraffic_bn += otraffic_bn_today;
413 itraffic_bn += itraffic_bn_today;
414 otraffic_dcc += otraffic_dcc_today;
415 itraffic_dcc += itraffic_dcc_today;
416 otraffic_unknown += otraffic_unknown_today;
417 itraffic_unknown += itraffic_unknown_today;
418 otraffic_trans += otraffic_trans_today;
419 itraffic_trans += itraffic_trans_today;
420 otraffic_irc_today = otraffic_bn_today = 0;
421 otraffic_dcc_today = otraffic_unknown_today = 0;
422 itraffic_irc_today = itraffic_bn_today = 0;
423 itraffic_dcc_today = itraffic_unknown_today = 0;
424 itraffic_trans_today = otraffic_trans_today = 0;
425 }
426
427 static void event_loaded()
428 {
429 check_bind_event("loaded");
430 }
431
432 void kill_tcl();
433 extern module_entry *module_list;
434 void restart_chons();
435
436 int init_userent(), init_net(),
437 init_tcl(int, char **);
438 void botnet_init();
439 void dns_init();
440 void binds_init();
441 void dcc_init();
442
443 void patch(const char *str)
444 {
445 char *p = strchr(egg_version, '+');
446
447 if (!p)
448 p = &egg_version[strlen(egg_version)];
449 sprintf(p, "+%s", str);
450 egg_numver++;
451 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
452 }
453
454 /*
455 static inline void garbage_collect(void)
456 {
457 static u_8bit_t run_cnt = 0;
458
459 if (run_cnt == 3)
460 garbage_collect_tclhash();
461 else
462 run_cnt++;
463 }
464 */
465
466 int main(int argc, char **argv)
467 {
468 int xx, i;
469 char buf[520], s[25];
470 FILE *f;
471 struct sigaction sv;
472 struct chanset_t *chan;
473 egg_timeval_t howlong;
474
475 #ifdef DEBUG
476 /* Make sure it can write core, if you make debug. Else it's pretty
477 * useless (dw)
478 */
479 {
480 #include <sys/resource.h>
481 struct rlimit cdlim;
482
483 cdlim.rlim_cur = RLIM_INFINITY;
484 cdlim.rlim_max = RLIM_INFINITY;
485 setrlimit(RLIMIT_CORE, &cdlim);
486 }
487 #endif
488
489 #ifdef ENABLE_NLS
490 setlocale(LC_MESSAGES, "");
491 bindtextdomain(PACKAGE, LOCALEDIR);
492 textdomain(PACKAGE);
493 #endif
494
495 #include "patch.h"
496 /* Version info! */
497 snprintf(ver, sizeof ver, "eggdrop v%s", egg_version);
498 snprintf(version, sizeof version, "Eggdrop v%s (C) 1997 Robey Pointer (C) 2002 Eggheads",
499 egg_version);
500 /* Now add on the patchlevel (for Tcl) */
501 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
502 strcat(egg_version, egg_xtra);
503 #ifdef STOP_UAC
504 {
505 int nvpair[2];
506
507 nvpair[0] = SSIN_UACPROC;
508 nvpair[1] = UAC_NOPRINT;
509 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
510 }
511 #endif
512
513 /* Set up error traps: */
514 sv.sa_handler = got_bus;
515 sigemptyset(&sv.sa_mask);
516 #ifdef SA_RESETHAND
517 sv.sa_flags = SA_RESETHAND;
518 #else
519 sv.sa_flags = 0;
520 #endif
521 sigaction(SIGBUS, &sv, NULL);
522 sv.sa_handler = got_segv;
523 sigaction(SIGSEGV, &sv, NULL);
524 #ifdef SA_RESETHAND
525 sv.sa_flags = 0;
526 #endif
527 sv.sa_handler = got_fpe;
528 sigaction(SIGFPE, &sv, NULL);
529 sv.sa_handler = got_term;
530 sigaction(SIGTERM, &sv, NULL);
531 sv.sa_handler = got_hup;
532 sigaction(SIGHUP, &sv, NULL);
533 sv.sa_handler = got_quit;
534 sigaction(SIGQUIT, &sv, NULL);
535 sv.sa_handler = SIG_IGN;
536 sigaction(SIGPIPE, &sv, NULL);
537 sv.sa_handler = got_ill;
538 sigaction(SIGILL, &sv, NULL);
539 sv.sa_handler = got_alarm;
540 sigaction(SIGALRM, &sv, NULL);
541
542 /* Initialize variables and stuff */
543 timer_get_time(&egg_timeval_now);
544 now = egg_timeval_now.sec;
545 chanset = NULL;
546 memcpy(&nowtm, localtime(&now), sizeof(struct tm));
547 lastmin = nowtm.tm_min;
548 srandom(now % (getpid() + getppid()));
549 if (argc > 1)
550 for (i = 1; i < argc; i++)
551 do_arg(argv[i]);
552 printf("\n%s\n", version);
553 printf(" *** WARNING: Do NOT run this DEVELOPMENT version for any purpose other than testing.\n\n");
554
555 /* Don't allow eggdrop to run as root */
556 if (((int) getuid() == 0) || ((int) geteuid() == 0))
557 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
558
559 egg = eggdrop_new();
560 script_init(egg);
561 binds_init();
562 modules_init();
563 logfile_init();
564 timer_init();
565 dns_init();
566 core_binds_init();
567 dcc_init();
568 init_userent();
569 botnet_init();
570 init_net();
571
572 if (backgrd)
573 bg_prepare_split();
574
575 init_tcl(argc, argv);
576
577 strncpyz(s, ctime(&now), sizeof s);
578 strcpy(&s[11], &s[20]);
579 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
580 chanprog();
581 if (!encrypt_pass) {
582 printf(_("You have installed modules but have not selected an encryption\n\
583 module, please consult the default config file for info.\n"));
584 bg_send_quit(BG_ABORT);
585 exit(1);
586 }
587 i = 0;
588 for (chan = chanset; chan; chan = chan->next)
589 i++;
590 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
591 botnetnick, i, count_users(userlist));
592 cache_miss = 0;
593 cache_hit = 0;
594
595 if (!pid_file[0])
596 snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
597
598 /* Check for pre-existing eggdrop! */
599 f = fopen(pid_file, "r");
600 if (f != NULL) {
601 fgets(s, 10, f);
602 xx = atoi(s);
603 kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
604 is used */
605 if (errno != ESRCH) {
606 printf(_("I detect %s already running from this directory.\n"), botnetnick);
607 printf(_("If this is incorrect, erase the %s\n"), pid_file);
608 bg_send_quit(BG_ABORT);
609 exit(1);
610 }
611 }
612
613 /* Move into background? */
614 if (backgrd) {
615 #ifndef CYGWIN_HACKS
616 bg_do_split();
617 } else { /* !backgrd */
618 #endif
619 xx = getpid();
620 if (xx != 0) {
621 FILE *fp;
622
623 /* Write pid to file */
624 unlink(pid_file);
625 fp = fopen(pid_file, "w");
626 if (fp != NULL) {
627 fprintf(fp, "%u\n", xx);
628 if (fflush(fp)) {
629 /* Let the bot live since this doesn't appear to be a botchk */
630 printf(_("* Warning! Could not write %s file!\n"), pid_file);
631 fclose(fp);
632 unlink(pid_file);
633 } else
634 fclose(fp);
635 } else
636 printf(_("* Warning! Could not write %s file!\n"), pid_file);
637 #ifdef CYGWIN_HACKS
638 printf("Launched into the background (pid: %d)\n\n", xx);
639 #endif
640 }
641 }
642
643 use_stderr = 0; /* Stop writing to stderr now */
644 if (backgrd) {
645 /* Ok, try to disassociate from controlling terminal (finger cross) */
646 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
647 setpgid(0, 0);
648 #endif
649 /* Tcl wants the stdin, stdout and stderr file handles kept open. */
650 freopen("/dev/null", "r", stdin);
651 freopen("/dev/null", "w", stdout);
652 freopen("/dev/null", "w", stderr);
653 #ifdef CYGWIN_HACKS
654 FreeConsole();
655 #endif
656 }
657
658 /* Terminal emulating dcc chat */
659 if (!backgrd && term_z) {
660 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
661
662 dcc[n].addr[0] = '\0';
663 dcc[n].sock = STDOUT;
664 dcc[n].timeval = now;
665 dcc[n].u.chat->con_flags = conmask;
666 dcc[n].u.chat->strip_flags = STRIP_ALL;
667 dcc[n].status = STAT_ECHO;
668 strcpy(dcc[n].nick, "HQ");
669 strcpy(dcc[n].host, "llama@console");
670 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
671 /* Make sure there's an innocuous HQ user if needed */
672 if (!dcc[n].user) {
673 userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_PARTY);
674 dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
675 }
676 setsock(STDOUT, 0); /* Entry in net table */
677 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
678 dcc_chatter(n);
679 }
680
681 online_since = now;
682 autolink_cycle(NULL); /* Hurry and connect to tandem bots */
683 add_help_reference("cmds1.help");
684 add_help_reference("cmds2.help");
685 add_help_reference("core.help");
686 howlong.sec = 1;
687 howlong.usec = 0;
688 timer_create_repeater(&howlong, (Function) core_secondly);
689 add_hook(HOOK_MINUTELY, (Function) core_minutely);
690 add_hook(HOOK_HOURLY, (Function) core_hourly);
691 add_hook(HOOK_REHASH, (Function) event_rehash);
692 add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
693 add_hook(HOOK_USERFILE, (Function) event_save);
694 add_hook(HOOK_BACKUP, (Function) backup_userfile);
695 add_hook(HOOK_DAILY, (Function) event_logfile);
696 add_hook(HOOK_DAILY, (Function) event_resettraffic);
697 add_hook(HOOK_LOADED, (Function) event_loaded);
698
699 call_hook(HOOK_LOADED);
700
701 debug0("main: entering loop");
702 while (1) {
703 int socket_cleanup = 0;
704
705 #if !defined(HAVE_PRE7_5_TCL)
706 /* Process a single tcl event */
707 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
708 #endif
709
710 /* Lets move some of this here, reducing the numer of actual
711 * calls to periodic_timers
712 */
713 timer_get_time(&egg_timeval_now);
714 now = egg_timeval_now.sec;
715 random(); /* Woop, lets really jumble things */
716 timer_run();
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 timer_get_time(&egg_timeval_now);
735 if (xx >= 0) { /* Non-error */
736 int idx;
737
738 for (idx = 0; idx < dcc_total; idx++)
739 if (dcc[idx].sock == xx) {
740 if (dcc[idx].type && dcc[idx].type->activity) {
741 /* Traffic stats */
742 if (dcc[idx].type->name) {
743 if (!strncmp(dcc[idx].type->name, "BOT", 3))
744 itraffic_bn_today += strlen(buf) + 1;
745 else if (!strcmp(dcc[idx].type->name, "SERVER"))
746 itraffic_irc_today += strlen(buf) + 1;
747 else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
748 itraffic_dcc_today += strlen(buf) + 1;
749 else if (!strncmp(dcc[idx].type->name, "FILES", 5))
750 itraffic_dcc_today += strlen(buf) + 1;
751 else if (!strcmp(dcc[idx].type->name, "SEND"))
752 itraffic_trans_today += strlen(buf) + 1;
753 else if (!strncmp(dcc[idx].type->name, "GET", 3))
754 itraffic_trans_today += strlen(buf) + 1;
755 else
756 itraffic_unknown_today += strlen(buf) + 1;
757 }
758 dcc[idx].type->activity(idx, buf, i);
759 } else
760 putlog(LOG_MISC, "*",
761 "!!! untrapped dcc activity: type %s, sock %d",
762 dcc[idx].type->name, dcc[idx].sock);
763 break;
764 }
765 } else if (xx == -1) { /* EOF from someone */
766 int idx;
767
768 if (i == STDOUT && !backgrd)
769 fatal("END OF FILE ON TERMINAL", 0);
770 for (idx = 0; idx < dcc_total; idx++)
771 if (dcc[idx].sock == i) {
772 if (dcc[idx].type && dcc[idx].type->eof)
773 dcc[idx].type->eof(idx);
774 else {
775 putlog(LOG_MISC, "*",
776 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
777 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
778 killsock(i);
779 lostdcc(idx);
780 }
781 idx = dcc_total + 1;
782 }
783 if (idx == dcc_total) {
784 putlog(LOG_MISC, "*",
785 "(@) EOF socket %d, not a dcc socket, not anything.", i);
786 close(i);
787 killsock(i);
788 }
789 } else if (xx == -2 && errno != EINTR) { /* select() error */
790 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
791 for (i = 0; i < dcc_total; i++) {
792 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
793 putlog(LOG_MISC, "*",
794 "DCC socket %d (type %d, name '%s') expired -- pfft",
795 dcc[i].sock, dcc[i].type, dcc[i].nick);
796 killsock(dcc[i].sock);
797 lostdcc(i);
798 i--;
799 }
800 }
801 } else if (xx == -3) {
802 call_hook(HOOK_IDLE);
803 socket_cleanup = 0; /* If we've been idle, cleanup & flush */
804 }
805
806 if (do_restart) {
807 if (do_restart == -2)
808 rehash();
809 else {
810 /* Unload as many modules as possible */
811 int f = 1;
812 module_entry *p;
813 Function x;
814 char xx[256];
815
816 /* oops, I guess we should call this event before tcl is restarted */
817 check_bind_event("prerestart");
818
819 while (f) {
820 f = 0;
821 for (p = module_list; p != NULL; p = p->next) {
822 dependancy *d = dependancy_list;
823 int ok = 1;
824
825 while (ok && d) {
826 if (d->needed == p)
827 ok = 0;
828 d = d->next;
829 }
830 if (ok) {
831 strcpy(xx, p->name);
832 if (module_unload(xx, botnetnick) == NULL) {
833 f = 1;
834 break;
835 }
836 }
837 }
838 }
839 p = module_list;
840 if (p && p->next && p->next->next)
841 /* Should be only 2 modules now - blowfish (or some other
842 encryption module) and eggdrop. */
843 putlog(LOG_MISC, "*", _("Stagnant module; there WILL be memory leaks!"));
844 flushlogs();
845 kill_tcl();
846 timer_destroy_all(); /* Destroy all timers. */
847 init_tcl(argc, argv);
848 /* We expect the encryption module as the current module pointed
849 * to by `module_list'.
850 */
851 x = p->funcs[MODCALL_START];
852 /* `NULL' indicates that we just recently restarted. The module
853 * is expected to re-initialise as needed.
854 */
855 x(NULL);
856 rehash();
857 restart_chons();
858 call_hook(HOOK_LOADED);
859 }
860 do_restart = 0;
861 }
862 }
863 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23