/[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.135 - (hide annotations) (download) (as text)
Sat Feb 15 05:04:58 2003 UTC (16 years, 6 months ago) by wcc
Branch: MAIN
Changes since 1.134: +4 -6 lines
File MIME type: text/x-chdr
* Removed share.mod (will be part of the new botnet module).
* More transfer removal.
* More botnet removal.
* Regenerated gettext stuff.
* More doc stuff.
* Removed eggdrop.conf (replaced by config.tcl for now).

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23