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