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