/[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.14 - (show annotations) (download) (as text)
Tue Dec 21 17:35:09 1999 UTC (19 years, 8 months ago) by fabian
Branch: MAIN
Changes since 1.13: +16 -18 lines
File MIME type: text/x-chdr
Tothwolf's id-header

1 /*
2 * main.c -- handles:
3 * core event handling
4 * signal handling
5 * command line arguments
6 * context and assert debugging
7 *
8 * dprintf'ized, 15nov1995
9 *
10 * $Id$
11 */
12 /*
13 * Copyright (C) 1997 Robey Pointer
14 * Copyright (C) 1999 Eggheads
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 */
30 /*
31 * The author (Robey Pointer) can be reached at: robey@netcom.com
32 * NOTE: Robey is no long working on this code, there is a discussion
33 * list avaliable at eggheads@eggheads.org.
34 */
35
36 #include "main.h"
37 #include <time.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <netdb.h>
42 #include <setjmp.h>
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 #include "chan.h"
50 #include "modules.h"
51 #include "tandem.h"
52 #ifdef CYGWIN_HACKS
53 #include <windows.h>
54 BOOL FreeConsole(VOID);
55 #endif
56
57 #ifndef _POSIX_SOURCE
58 /* solaris needs this */
59 #define _POSIX_SOURCE 1
60 #endif
61
62 extern char origbotname[];
63 extern int dcc_total;
64 extern struct dcc_t *dcc;
65 extern int conmask;
66 extern struct userrec *userlist;
67 extern int cache_hit, cache_miss;
68 extern char userfile[];
69 extern struct chanset_t *chanset;
70 extern char botnetnick[];
71 extern log_t *logs;
72 extern Tcl_Interp *interp;
73 extern int max_logs;
74 extern tcl_timer_t *timer, *utimer;
75 extern jmp_buf alarmret;
76 extern int quick_logs; /* dw */
77
78 /*
79 * Please use the PATCH macro instead of directly altering the version
80 * string from now on (it makes it much easier to maintain patches).
81 * Also please read the README file regarding your rights to distribute
82 * modified versions of this bot.
83 */
84
85 char egg_version[1024] = "1.5.1";
86 int egg_numver = 1050100;
87
88 char notify_new[121] = ""; /* person to send a note to for new users */
89 int default_flags = 0; /* default user flags and */
90 int default_uflags = 0; /* default userdefinied flags for people */
91 /* who say 'hello' or for .adduser */
92
93 int backgrd = 1; /* run in the background? */
94 int con_chan = 0; /* foreground: constantly display channel
95 * stats? */
96 int term_z = 0; /* foreground: use the terminal as a party
97 * line? */
98 char configfile[121] = "eggdrop.conf"; /* name of the config file */
99 char helpdir[121]; /* directory of help files (if used) */
100 char textdir[121] = ""; /* directory for text files that get dumped */
101 int keep_all_logs = 0; /* never erase logfiles, no matter how old
102 * they are? */
103 time_t online_since; /* unix-time that the bot loaded up */
104 int make_userfile = 0; /* using bot in make-userfile mode? (first
105 * user to 'hello' becomes master) */
106 char owner[121] = ""; /* permanent owner(s) of the bot */
107 char pid_file[40]; /* name of the file for the pid to be
108 * stored in */
109 int save_users_at = 0; /* how many minutes past the hour to
110 * save the userfile? */
111 int notify_users_at = 0; /* how many minutes past the hour to
112 * notify users of notes? */
113 int switch_logfiles_at = 300; /* when (military time) to switch logfiles */
114 char version[81]; /* version info (long form) */
115 char ver[41]; /* version info (short form) */
116 char egg_xtra[2048]; /* patch info */
117 int use_stderr = 1; /* send stuff to stderr instead of logfiles? */
118 int do_restart = 0; /* .restart has been called, restart asap */
119 int die_on_sighup = 0; /* die if bot receives SIGHUP */
120 int die_on_sigterm = 0; /* die if bot receives SIGTERM */
121 int resolve_timeout = 15; /* hostname/address lookup timeout */
122 time_t now; /* duh, now :) */
123 int otraffic_irc = 0; /* traffic stats */
124 int otraffic_irc_today = 0;
125 int otraffic_bn = 0;
126 int otraffic_bn_today = 0;
127 int otraffic_dcc = 0;
128 int otraffic_dcc_today = 0;
129 int otraffic_filesys = 0;
130 int otraffic_filesys_today = 0;
131 int otraffic_trans = 0;
132 int otraffic_trans_today = 0;
133 int otraffic_unknown = 0;
134 int otraffic_unknown_today = 0;
135 int itraffic_irc = 0;
136 int itraffic_irc_today = 0;
137 int itraffic_bn = 0;
138 int itraffic_bn_today = 0;
139 int itraffic_dcc = 0;
140 int itraffic_dcc_today = 0;
141 int itraffic_trans = 0;
142 int itraffic_trans_today = 0;
143 int itraffic_unknown = 0;
144 int itraffic_unknown_today = 0;
145
146 #ifdef DEBUG_CONTEXT
147 /* context storage for fatal crashes */
148 char cx_file[16][30];
149 char cx_note[16][256];
150 int cx_line[16];
151 int cx_ptr = 0;
152 #endif
153
154 void fatal(char *s, int recoverable)
155 {
156 int i;
157
158 putlog(LOG_MISC, "*", "* %s", s);
159 flushlogs();
160 for (i = 0; i < dcc_total; i++)
161 killsock(dcc[i].sock);
162 unlink(pid_file);
163 if (!recoverable)
164 exit(1);
165 }
166
167 int expmem_chanprog(), expmem_users(), expmem_misc(), expmem_dccutil(),
168 expmem_botnet(), expmem_tcl(), expmem_tclhash(), expmem_net(),
169 expmem_modules(int), expmem_language(), expmem_tcldcc();
170
171 /* for mem.c : calculate memory we SHOULD be using */
172 int expected_memory()
173 {
174 int tot;
175
176 Context;
177 tot = expmem_chanprog() + expmem_users() + expmem_misc() +
178 expmem_dccutil() + expmem_botnet() + expmem_tcl() + expmem_tclhash() +
179 expmem_net() + expmem_modules(0) + expmem_language() + expmem_tcldcc();
180 return tot;
181 }
182
183 static void check_expired_dcc()
184 {
185 int i;
186
187 for (i = 0; i < dcc_total; i++)
188 if (dcc[i].type && dcc[i].type->timeout_val &&
189 ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
190 if (dcc[i].type->timeout)
191 dcc[i].type->timeout(i);
192 else if (dcc[i].type->eof)
193 dcc[i].type->eof(i);
194 else
195 continue;
196 /* only timeout 1 socket per cycle, too risky for more */
197 return;
198 }
199 }
200
201 #ifdef DEBUG_CONTEXT
202 static int nested_debug = 0;
203
204 void write_debug()
205 {
206 int x;
207 char s[80];
208 int y;
209
210 if (nested_debug) {
211 /* yoicks, if we have this there's serious trouble */
212 /* all of these are pretty reliable, so we'll try these */
213 /* dont try and display context-notes in here, it's _not_ safe <cybah> */
214 x = creat("DEBUG.DEBUG", 0644);
215 setsock(x, SOCK_NONSOCK);
216 if (x >= 0) {
217 strcpy(s, ctime(&now));
218 dprintf(-x, "Debug (%s) written %s", ver, s);
219 dprintf(-x, "Please report problem to eggheads@eggheads.org");
220 dprintf(-x, "after a visit to http://www.eggheads.org/bugs.html");
221 dprintf(-x, "Full Patch List: %s\n", egg_xtra);
222 dprintf(-x, "Context: ");
223 cx_ptr = cx_ptr & 15;
224 for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
225 dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
226 dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
227 killsock(x);
228 close(x);
229 }
230 exit(1); /* dont even try & tell people about, that may
231 * have caused the fault last time */
232 } else
233 nested_debug = 1;
234 putlog(LOG_MISC, "*", "* Last context: %s/%d [%s]", cx_file[cx_ptr],
235 cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
236 putlog(LOG_MISC, "*", "* Please REPORT this BUG!");
237 putlog(LOG_MISC, "*", "* Check doc/BUG-REPORT on how to do so.");
238 x = creat("DEBUG", 0644);
239 setsock(x, SOCK_NONSOCK);
240 if (x < 0) {
241 putlog(LOG_MISC, "*", "* Failed to write DEBUG");
242 } else {
243 strcpy(s, ctime(&now));
244 dprintf(-x, "Debug (%s) written %s", ver, s);
245 dprintf(-x, "Full Patch List: %s\n", egg_xtra);
246 #ifdef STATIC
247 dprintf(-x, "STATICALLY LINKED\n");
248 #endif
249 strcpy(s, "info library");
250 if (interp && (Tcl_Eval(interp, s) == TCL_OK))
251 dprintf(-x, "Using tcl library: %s (header version %s)\n",
252 interp->result, TCL_VERSION);
253 dprintf(-x, "Compile flags: %s\n", CCFLAGS);
254 dprintf(-x, "Link flags : %s\n", LDFLAGS);
255 dprintf(-x, "Strip flags : %s\n", STRIPFLAGS);
256 dprintf(-x, "Context: ");
257 cx_ptr = cx_ptr & 15;
258 for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
259 dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
260 (cx_note[y][0]) ? cx_note[y] : "");
261 dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
262 (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
263 tell_dcc(-x);
264 dprintf(-x, "\n");
265 debug_mem_to_dcc(-x);
266 killsock(x);
267 close(x);
268 putlog(LOG_MISC, "*", "* Wrote DEBUG");
269 }
270 }
271 #endif
272
273 static void got_bus(int z)
274 {
275 #ifdef DEBUG_CONTEXT
276 write_debug();
277 #endif
278 fatal("BUS ERROR -- CRASHING!", 1);
279 #ifdef SA_RESETHAND
280 kill(getpid(), SIGBUS);
281 #else
282 exit(1);
283 #endif
284 }
285
286 static void got_segv(int z)
287 {
288 #ifdef DEBUG_CONTEXT
289 write_debug();
290 #endif
291 fatal("SEGMENT VIOLATION -- CRASHING!", 1);
292 #ifdef SA_RESETHAND
293 kill(getpid(), SIGSEGV);
294 #else
295 exit(1);
296 #endif
297 }
298
299 static void got_fpe(int z)
300 {
301 #ifdef DEBUG_CONTEXT
302 write_debug();
303 #endif
304 fatal("FLOATING POINT ERROR -- CRASHING!", 0);
305 }
306
307 static void got_term(int z)
308 {
309 write_userfile(-1);
310 check_tcl_event("sigterm");
311 if (die_on_sigterm) {
312 botnet_send_chat(-1, botnetnick, "ACK, I've been terminated!");
313 fatal("TERMINATE SIGNAL -- SIGNING OFF", 0);
314 } else {
315 putlog(LOG_MISC, "*", "RECEIVED TERMINATE SIGNAL (IGNORING)");
316 }
317 }
318
319 static void got_quit(int z)
320 {
321 check_tcl_event("sigquit");
322 putlog(LOG_MISC, "*", "RECEIVED QUIT SIGNAL (IGNORING)");
323 return;
324 }
325
326 static void got_hup(int z)
327 {
328 write_userfile(-1);
329 check_tcl_event("sighup");
330 if (die_on_sighup) {
331 fatal("HANGUP SIGNAL -- SIGNING OFF", 0);
332 } else
333 putlog(LOG_MISC, "*", "Received HUP signal: rehashing...");
334 do_restart = -2;
335 return;
336 }
337
338 static void got_alarm(int z)
339 {
340 /* a call to resolver (gethostbyname, etc) timed out */
341 longjmp(alarmret, 1);
342 /* STUPID STUPID STUPID */
343 /* return; */
344 }
345
346 /* got ILL signal -- log context and continue */
347 static void got_ill(int z)
348 {
349 check_tcl_event("sigill");
350 #ifdef DEBUG_CONTEXT
351 putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
352 cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
353 #endif
354 }
355
356 #ifdef DEBUG_CONTEXT
357 /* Context */
358 void eggContext(char *file, int line, char *module)
359 {
360 char x[100];
361
362 if (!module)
363 sprintf(x, "%s", file);
364 else
365 sprintf(x, "%s:%s", module, file);
366 x[30] = 0;
367 cx_ptr = ((cx_ptr + 1) & 15);
368 strcpy(cx_file[cx_ptr], x);
369 cx_line[cx_ptr] = line;
370 cx_note[cx_ptr][0] = 0;
371 }
372
373 /* ContextNote */
374 void eggContextNote(char *file, int line, char *module, char *note)
375 {
376 char x[100];
377
378 if (!module)
379 sprintf(x, "%s", file);
380 else
381 sprintf(x, "%s:%s", module, file);
382 x[30] = 0;
383 cx_ptr = ((cx_ptr + 1) & 15);
384 strcpy(cx_file[cx_ptr], x);
385 cx_line[cx_ptr] = line;
386 strncpy(cx_note[cx_ptr], note, 255);
387 cx_note[cx_ptr][255] = 0;
388 }
389 #endif
390
391 #ifdef DEBUG_ASSERT
392 /* Assert */
393 void eggAssert(char *file, int line, char *module, int expr)
394 {
395 if (!(expr)) {
396 #ifdef DEBUG_CONTEXT
397 write_debug();
398 #endif
399 if (!module) {
400 putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
401 } else {
402 putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
403 }
404 fatal("ASSERT FAILED -- CRASHING!", 1);
405 }
406 }
407 #endif
408
409 static void do_arg(char *s)
410 {
411 int i;
412
413 if (s[0] == '-')
414 for (i = 1; i < strlen(s); i++) {
415 if (s[i] == 'n')
416 backgrd = 0;
417 if (s[i] == 'c') {
418 con_chan = 1;
419 term_z = 0;
420 }
421 if (s[i] == 't') {
422 con_chan = 0;
423 term_z = 1;
424 }
425 if (s[i] == 'm')
426 make_userfile = 1;
427 if (s[i] == 'v') {
428 char x[256], *z = x;
429
430 strcpy(x, egg_version);
431 newsplit(&z);
432 newsplit(&z);
433 printf("%s\n", version);
434 if (z[0])
435 printf(" (patches: %s)\n", z);
436 exit(0);
437 }
438 if (s[i] == 'h') {
439 printf("\n%s\n\n", version);
440 printf(EGG_USAGE);
441 printf("\n");
442 exit(0);
443 }
444 } else
445 strcpy(configfile, s);
446 }
447
448 void backup_userfile()
449 {
450 char s[150];
451
452 putlog(LOG_MISC, "*", USERF_BACKUP);
453 strcpy(s, userfile);
454 strcat(s, "~bak");
455 copyfile(userfile, s);
456 }
457
458 /* timer info: */
459 static int lastmin = 99;
460 static time_t then;
461 static struct tm *nowtm;
462
463 /* rally BB, this is not QUITE as bad as it seems <G> */
464 /* ONCE A SECOND */
465 static void core_secondly()
466 {
467 static int cnt = 0;
468 int miltime;
469
470 do_check_timers(&utimer); /* secondly timers */
471 cnt++;
472 if (cnt >= 10) { /* every 10 seconds */
473 cnt = 0;
474 check_expired_dcc();
475 if (con_chan && !backgrd) {
476 dprintf(DP_STDOUT, "\033[2J\033[1;1H");
477 tell_verbose_status(DP_STDOUT);
478 do_module_report(DP_STDOUT, 0, "server");
479 do_module_report(DP_STDOUT, 0, "channels");
480 tell_mem_status_dcc(DP_STDOUT);
481 }
482 }
483 Context;
484 nowtm = localtime(&now);
485 if (nowtm->tm_min != lastmin) {
486 int i = 0;
487
488 /* once a minute */
489 lastmin = (lastmin + 1) % 60;
490 call_hook(HOOK_MINUTELY);
491 check_expired_ignores();
492 autolink_cycle(NULL); /* attempt autolinks */
493 /* in case for some reason more than 1 min has passed: */
494 while (nowtm->tm_min != lastmin) {
495 /* timer drift, dammit */
496 debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm->tm_min);
497 Context;
498 i++;
499 lastmin = (lastmin + 1) % 60;
500 call_hook(HOOK_MINUTELY);
501 }
502 if (i > 1)
503 putlog(LOG_MISC, "*", "(!) timer drift -- spun %d minutes", i);
504 miltime = (nowtm->tm_hour * 100) + (nowtm->tm_min);
505 Context;
506 if (((int) (nowtm->tm_min / 5) * 5) == (nowtm->tm_min)) { /* 5 min */
507 call_hook(HOOK_5MINUTELY);
508 check_botnet_pings();
509 Context;
510 if (quick_logs == 0) {
511 flushlogs();
512 check_logsize();
513 }
514 if (miltime == 0) { /* at midnight */
515 char s[128];
516 int j;
517
518 s[my_strcpy(s, ctime(&now)) - 1] = 0;
519 putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
520 backup_userfile();
521 for (j = 0; j < max_logs; j++) {
522 if (logs[j].filename != NULL && logs[j].f != NULL) {
523 fclose(logs[j].f);
524 logs[j].f = NULL;
525 }
526 }
527 }
528 }
529 Context;
530 if (nowtm->tm_min == notify_users_at)
531 call_hook(HOOK_HOURLY);
532 Context; /* these no longer need checking since they are
533 * all check vs minutely settings and we only
534 * get this far on the minute */
535 if (miltime == switch_logfiles_at) {
536 call_hook(HOOK_DAILY);
537 if (!keep_all_logs) {
538 putlog(LOG_MISC, "*", MISC_LOGSWITCH);
539 for (i = 0; i < max_logs; i++)
540 if (logs[i].filename) {
541 char s[1024];
542
543 if (logs[i].f) {
544 fclose(logs[i].f);
545 logs[i].f = NULL;
546 }
547 simple_sprintf(s, "%s.yesterday", logs[i].filename);
548 unlink(s);
549 movefile(logs[i].filename, s);
550 }
551 }
552 }
553 }
554 }
555
556 static void core_minutely()
557 {
558 Context;
559 check_tcl_time(nowtm);
560 do_check_timers(&timer);
561 Context;
562 if (quick_logs != 0) {
563 flushlogs();
564 check_logsize();
565 }
566 }
567
568 static void core_hourly()
569 {
570 Context;
571 write_userfile(-1);
572 }
573
574 static void event_rehash()
575 {
576 Context;
577 check_tcl_event("rehash");
578 }
579
580 static void event_prerehash()
581 {
582 Context;
583 check_tcl_event("prerehash");
584 }
585
586 static void event_save()
587 {
588 Context;
589 check_tcl_event("save");
590 }
591
592 static void event_logfile()
593 {
594 Context;
595 check_tcl_event("logfile");
596 }
597
598 static void event_resettraffic()
599 {
600 Context;
601 otraffic_irc += otraffic_irc_today;
602 itraffic_irc += itraffic_irc_today;
603 otraffic_bn += otraffic_bn_today;
604 itraffic_bn += itraffic_bn_today;
605 otraffic_dcc += otraffic_dcc_today;
606 itraffic_dcc += itraffic_dcc_today;
607 otraffic_unknown += otraffic_unknown_today;
608 itraffic_unknown += itraffic_unknown_today;
609 otraffic_trans += otraffic_trans_today;
610 itraffic_trans += itraffic_trans_today;
611 otraffic_irc_today = otraffic_bn_today = 0;
612 otraffic_dcc_today = otraffic_unknown_today = 0;
613 itraffic_irc_today = itraffic_bn_today = 0;
614 itraffic_dcc_today = itraffic_unknown_today = 0;
615 itraffic_trans_today = 0;
616 }
617
618 void kill_tcl();
619 extern module_entry *module_list;
620 void restart_chons();
621
622 #ifdef STATIC
623 void check_static(char *, char *(*)());
624
625 #include "mod/static.h"
626 #endif
627 int init_mem(), init_dcc_max(), init_userent(), init_misc(), init_bots(),
628 init_net(), init_modules(), init_tcl(int, char **),
629 init_language(int);
630
631 void patch(char *str)
632 {
633 char *p = strchr(egg_version, '+');
634
635 if (!p)
636 p = &egg_version[strlen(egg_version)];
637 sprintf(p, "+%s", str);
638 egg_numver++;
639 sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
640 }
641
642 int main(int argc, char **argv)
643 {
644 int xx, i;
645 char buf[520], s[520];
646 FILE *f;
647 struct sigaction sv;
648 struct chanset_t *chan;
649
650 /* initialise context list */
651 for (i = 0; i < 16; i++) {
652 Context;
653 }
654 #include "patch.h"
655 /* version info! */
656 sprintf(ver, "eggdrop v%s", egg_version);
657 sprintf(version, "Eggdrop v%s (c)1997 Robey Pointer (c)1999 Eggheads", egg_version);
658 /* now add on the patchlevel (for Tcl) */
659 sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
660 strcat(egg_version, egg_xtra);
661 Context;
662 #ifdef STOP_UAC
663 {
664 int nvpair[2];
665
666 nvpair[0] = SSIN_UACPROC;
667 nvpair[1] = UAC_NOPRINT;
668 setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
669 }
670 #endif
671 /* set up error traps: */
672 sv.sa_handler = got_bus;
673 sigemptyset(&sv.sa_mask);
674 #ifdef SA_RESETHAND
675 sv.sa_flags = SA_RESETHAND;
676 #else
677 sv.sa_flags = 0;
678 #endif
679 sigaction(SIGBUS, &sv, NULL);
680 sv.sa_handler = got_segv;
681 sigaction(SIGSEGV, &sv, NULL);
682 #ifdef SA_RESETHAND
683 sv.sa_flags = 0;
684 #endif
685 sv.sa_handler = got_fpe;
686 sigaction(SIGFPE, &sv, NULL);
687 sv.sa_handler = got_term;
688 sigaction(SIGTERM, &sv, NULL);
689 sv.sa_handler = got_hup;
690 sigaction(SIGHUP, &sv, NULL);
691 sv.sa_handler = got_quit;
692 sigaction(SIGQUIT, &sv, NULL);
693 sv.sa_handler = SIG_IGN;
694 sigaction(SIGPIPE, &sv, NULL);
695 sv.sa_handler = got_ill;
696 sigaction(SIGILL, &sv, NULL);
697 sv.sa_handler = got_alarm;
698 sigaction(SIGALRM, &sv, NULL);
699 /* initialize variables and stuff */
700 now = time(NULL);
701 chanset = NULL;
702 nowtm = localtime(&now);
703 lastmin = nowtm->tm_min;
704 srandom(now);
705 init_mem();
706 init_language(1);
707 if (argc > 1)
708 for (i = 1; i < argc; i++)
709 do_arg(argv[i]);
710 printf("\n%s\n", version);
711 init_dcc_max();
712 init_userent();
713 init_misc();
714 init_bots();
715 init_net();
716 init_modules();
717 init_tcl(argc, argv);
718 init_language(0);
719 #ifdef STATIC
720 link_statics();
721 #endif
722 Context;
723 strcpy(s, ctime(&now));
724 s[strlen(s) - 1] = 0;
725 strcpy(&s[11], &s[20]);
726 putlog(LOG_ALL, "*", "--- Loading %s (%s)", ver, s);
727 chanprog();
728 Context;
729 if (encrypt_pass == 0) {
730 printf(MOD_NOCRYPT);
731 exit(1);
732 }
733 i = 0;
734 for (chan = chanset; chan; chan = chan->next)
735 i++;
736 putlog(LOG_MISC, "*", "=== %s: %d channels, %d users.",
737 botnetnick, i, count_users(userlist));
738 cache_miss = 0;
739 cache_hit = 0;
740 Context;
741 sprintf(pid_file, "pid.%s", botnetnick);
742 Context;
743 /* check for pre-existing eggdrop! */
744 f = fopen(pid_file, "r");
745 if (f != NULL) {
746 fgets(s, 10, f);
747 xx = atoi(s);
748 kill(xx, SIGCHLD); /* meaningless kill to determine if pid
749 * is used */
750 if (errno != ESRCH) {
751 printf(EGG_RUNNING1, origbotname);
752 printf(EGG_RUNNING2, pid_file);
753 exit(1);
754 }
755 }
756 Context;
757 #ifndef CYGWIN_HACKS
758 /* move into background? */
759 if (backgrd) {
760 xx = fork();
761 if (xx == -1)
762 fatal("CANNOT FORK PROCESS.", 0);
763 if (xx != 0) {
764 FILE *fp;
765
766 /* need to attempt to write pid now, not later */
767 unlink(pid_file);
768 fp = fopen(pid_file, "w");
769 if (fp != NULL) {
770 fprintf(fp, "%u\n", xx);
771 if (fflush(fp)) {
772 /* kill bot incase a botchk is run from crond */
773 printf(EGG_NOWRITE, pid_file);
774 printf(" Try freeing some disk space\n");
775 fclose(fp);
776 unlink(pid_file);
777 exit(1);
778 }
779 fclose(fp);
780 } else
781 printf(EGG_NOWRITE, pid_file);
782 printf("Launched into the background (pid: %d)\n\n", xx);
783 #if HAVE_SETPGID
784 setpgid(xx, xx);
785 #endif
786 exit(0);
787 }
788 }
789 #endif
790 use_stderr = 0; /* stop writing to stderr now */
791 xx = getpid();
792 if ((xx != 0) && (!backgrd)) {
793 FILE *fp;
794
795 /* write pid to file */
796 unlink(pid_file);
797 fp = fopen(pid_file, "w");
798 if (fp != NULL) {
799 fprintf(fp, "%u\n", xx);
800 if (fflush(fp)) {
801 /* let the bot live since this doesn't appear to be a botchk */
802 printf(EGG_NOWRITE, pid_file);
803 fclose(fp);
804 unlink(pid_file);
805 }
806 fclose(fp);
807 } else
808 printf(EGG_NOWRITE, pid_file);
809 }
810 if (backgrd) {
811 /* ok, try to disassociate from controlling terminal */
812 /* (finger cross) */
813 #if HAVE_SETPGID && !defined(CYGWIN_HACKS)
814 setpgid(0, 0);
815 #endif
816 /* close out stdin/out/err */
817 freopen("/dev/null", "r", stdin);
818 freopen("/dev/null", "w", stdout);
819 freopen("/dev/null", "w", stderr);
820 #ifdef CYGWIN_HACKS
821 FreeConsole();
822 #endif
823 /* tcl wants those file handles kept open */
824 /* close(0); close(1); close(2); */
825 }
826 /* terminal emulating dcc chat */
827 if ((!backgrd) && (term_z)) {
828 int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
829
830 dcc[n].addr = iptolong(getmyip());
831 dcc[n].sock = STDOUT;
832 dcc[n].timeval = now;
833 dcc[n].u.chat->con_flags = conmask;
834 dcc[n].u.chat->strip_flags = STRIP_ALL;
835 dcc[n].status = STAT_ECHO;
836 strcpy(dcc[n].nick, "HQ");
837 strcpy(dcc[n].host, "llama@console");
838 dcc[n].user = get_user_by_handle(userlist, "HQ");
839 /* make sure there's an innocuous HQ user if needed */
840 if (!dcc[n].user) {
841 adduser(userlist, "HQ", "none", "-", USER_PARTY);
842 dcc[n].user = get_user_by_handle(userlist, "HQ");
843 }
844 setsock(STDOUT, 0); /* entry in net table */
845 dprintf(n, "\n### ENTERING DCC CHAT SIMULATION ###\n\n");
846 dcc_chatter(n);
847 }
848 then = now;
849 online_since = now;
850 autolink_cycle(NULL); /* hurry and connect to tandem bots */
851 add_help_reference("cmds1.help");
852 add_help_reference("cmds2.help");
853 add_help_reference("core.help");
854 add_hook(HOOK_SECONDLY, core_secondly);
855 add_hook(HOOK_MINUTELY, core_minutely);
856 add_hook(HOOK_HOURLY, core_hourly);
857 add_hook(HOOK_REHASH, event_rehash);
858 add_hook(HOOK_PRE_REHASH, event_prerehash);
859 add_hook(HOOK_USERFILE, event_save);
860 add_hook(HOOK_DAILY, event_logfile);
861 add_hook(HOOK_DAILY, event_resettraffic);
862
863 debug0("main: entering loop");
864 while (1) {
865 int socket_cleanup = 0;
866
867 Context;
868 #if !defined(HAVE_PRE7_5_TCL) && !defined(HAVE_TCL_THREADS)
869 /* process a single tcl event */
870 Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
871 #endif
872 /* lets move some of this here, reducing the numer of actual
873 * calls to periodic_timers */
874 now = time(NULL);
875 random(); /* woop, lets really jumble things */
876 if (now != then) { /* once a second */
877 call_hook(HOOK_SECONDLY);
878 then = now;
879 }
880 Context;
881 /* only do this every so often */
882 if (!socket_cleanup) {
883 /* clean up sockets that were just left for dead */
884 for (i = 0; i < dcc_total; i++) {
885 if (dcc[i].type == &DCC_LOST) {
886 dcc[i].type = (struct dcc_table *) (dcc[i].sock);
887 lostdcc(i);
888 i--;
889 }
890 }
891 /* check for server or dcc activity */
892 dequeue_sockets();
893 socket_cleanup = 5;
894 } else
895 socket_cleanup--;
896 /* new net routines: help me mary! */
897 xx = sockgets(buf, &i);
898 if (xx >= 0) { /* non-error */
899 int idx;
900
901 Context;
902 for (idx = 0; idx < dcc_total; idx++)
903 if (dcc[idx].sock == xx) {
904 if (dcc[idx].type && dcc[idx].type->activity) {
905 /* traffic stats */
906 if (dcc[idx].type->name) {
907 if (!strncmp(dcc[idx].type->name, "BOT", 3))
908 itraffic_bn_today += strlen(buf) + 1;
909 else if (!strcmp(dcc[idx].type->name, "SERVER"))
910 itraffic_irc_today += strlen(buf) + 1;
911 else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
912 itraffic_dcc_today += strlen(buf) + 1;
913 else if (!strncmp(dcc[idx].type->name, "FILES", 5))
914 itraffic_dcc_today += strlen(buf) + 1;
915 else if (!strcmp(dcc[idx].type->name, "SEND"))
916 itraffic_trans_today += strlen(buf) + 1;
917 else if (!strncmp(dcc[idx].type->name, "GET", 3))
918 itraffic_trans_today += strlen(buf) + 1;
919 else
920 itraffic_unknown_today += strlen(buf) + 1;
921 }
922 dcc[idx].type->activity(idx, buf, i);
923 } else
924 putlog(LOG_MISC, "*",
925 "!!! untrapped dcc activity: type %s, sock %d",
926 dcc[idx].type->name, dcc[idx].sock);
927 break;
928 }
929 } else if (xx == -1) { /* EOF from someone */
930 int idx;
931
932 if ((i == STDOUT) && !backgrd)
933 fatal("END OF FILE ON TERMINAL", 0);
934 Context;
935 for (idx = 0; idx < dcc_total; idx++)
936 if (dcc[idx].sock == i) {
937 if (dcc[idx].type && dcc[idx].type->eof)
938 dcc[idx].type->eof(idx);
939 else {
940 putlog(LOG_MISC, "*",
941 "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED",
942 i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*");
943 killsock(i);
944 lostdcc(idx);
945 }
946 idx = dcc_total + 1;
947 }
948 if (idx == dcc_total) {
949 putlog(LOG_MISC, "*",
950 "(@) EOF socket %d, not a dcc socket, not anything.", i);
951 close(i);
952 killsock(i);
953 }
954 } else if ((xx == -2) && (errno != EINTR)) { /* select() error */
955 Context;
956 putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno);
957 for (i = 0; i < dcc_total; i++) {
958 if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno = EBADF)) {
959 if (dcc[i].type == &DCC_LOST)
960 dcc[i].type = (struct dcc_table *) (dcc[i].sock);
961 putlog(LOG_MISC, "*",
962 "DCC socket %d (type %d, name '%s') expired -- pfft",
963 dcc[i].sock, dcc[i].type, dcc[i].nick);
964 killsock(dcc[i].sock);
965 lostdcc(i);
966 i--;
967 }
968 }
969 } else if (xx == (-3)) {
970 call_hook(HOOK_IDLE);
971 socket_cleanup = 0; /* if we've been idle, cleanup & flush */
972 }
973 if (do_restart) {
974 if (do_restart == -2)
975 rehash();
976 else {
977 /* unload as many modules as possible */
978 int f = 1;
979 module_entry *p;
980 Function x;
981 char xx[256];
982
983 while (f) {
984 f = 0;
985 for (p = module_list; p != NULL; p = p->next) {
986 dependancy *d = dependancy_list;
987 int ok = 1;
988
989 while (ok && d) {
990 if (d->needed == p)
991 ok = 0;
992 d = d->next;
993 }
994 if (ok) {
995 strcpy(xx, p->name);
996 if (module_unload(xx, origbotname) == NULL) {
997 f = 1;
998 break;
999 }
1000 }
1001 }
1002 }
1003 p = module_list;
1004 if (p && p->next && p->next->next)
1005 /* should be only 2 modules now -
1006 * blowfish & eggdrop */
1007 putlog(LOG_MISC, "*", MOD_STAGNANT);
1008 Context;
1009 flushlogs();
1010 Context;
1011 kill_tcl();
1012 init_tcl(argc, argv);
1013 init_language(0);
1014 x = p->funcs[MODCALL_START];
1015 x(0);
1016 rehash();
1017 restart_chons();
1018 }
1019 do_restart = 0;
1020 }
1021 }
1022 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23