/[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.116 - (show annotations) (download) (as text)
Sun May 5 15:19:11 2002 UTC (17 years, 5 months ago) by wingman
Branch: MAIN
Changes since 1.115: +15 -11 lines
File MIME type: text/x-chdr
Changes:

 * removed patch.h
 * added proper <filename>.h to (nearly) all <filename.h>
 * moved fixcolon to server.mod and irc.mod
 * moved ip2long to net.h
 * removed some unused function (forgot the name :-P)

Todo:

 * move config.h #include's away from main.h to files which
   needs them
 * clean up lib/eggdrop/module.h
   (now not just main.h is included but nearly all .h files from
    the core, but i guess this fixes itself when the global func
    table is abandon - then the modules must be revised too and
    we can clean up module.h)

And i'll bet i forgot something... ;-)

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23