/[cvs]/eggdrop1.9/src/main.c
ViewVC logotype

Annotation of /eggdrop1.9/src/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.120 - (hide annotations) (download) (as text)
Tue Jun 18 06:12:32 2002 UTC (17 years, 2 months ago) by guppy
Branch: MAIN
Changes since 1.119: +1 -2 lines
File MIME type: text/x-chdr

- removed userflag +k
- removed userflag +h
- removed all the code related to .strip since stdarg is going to redo it in a
better fashion using network filters.
- removed woobie since woobie is useless and we would be much better suited with
a "sample" module that actually does something useful

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23