/[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.25 - (show annotations) (download) (as text)
Mon Jan 17 22:36:06 2000 UTC (19 years, 9 months ago) by fabian
Branch: MAIN
Changes since 1.24: +4 -3 lines
File MIME type: text/x-chdr
Eule's copyright patch, updated by him for 1.5

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