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

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

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


Revision 1.6.2.1 - (hide annotations) (download) (as text)
Wed Nov 10 13:39:19 2010 UTC (8 years, 11 months ago) by pseudo
Branch: gettext
Changes since 1.6: +46 -31 lines
File MIME type: text/x-chdr
Converted remaining lang #defines in the core to english strings from core.english.lang. Gettextified most of the hardcoded strings.

1 simple 1.1 /*
2     * main.c -- handles:
3     * core event handling
4     * signal handling
5     * command line arguments
6     * context and assert debugging
7     *
8 pseudo 1.6.2.1 * $Id: main.c,v 1.6 2010/10/23 11:16:12 pseudo Exp $
9 simple 1.1 */
10     /*
11     * Copyright (C) 1997 Robey Pointer
12     * Copyright (C) 1999 - 2010 Eggheads Development Team
13     *
14     * This program is free software; you can redistribute it and/or
15     * modify it under the terms of the GNU General Public License
16     * as published by the Free Software Foundation; either version 2
17     * of the License, or (at your option) any later version.
18     *
19     * This program is distributed in the hope that it will be useful,
20     * but WITHOUT ANY WARRANTY; without even the implied warranty of
21     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22     * GNU General Public License for more details.
23     *
24     * You should have received a copy of the GNU General Public License
25     * along with this program; if not, write to the Free Software
26     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27     */
28     /*
29     * The author (Robey Pointer) can be reached at: robey@netcom.com
30     * NOTE: Robey is no long working on this code, there is a discussion
31     * list available at eggheads@eggheads.org.
32     */
33    
34     #include "main.h"
35    
36     #include <fcntl.h>
37     #include <errno.h>
38     #include <signal.h>
39     #include <netdb.h>
40     #include <setjmp.h>
41    
42     #ifdef TIME_WITH_SYS_TIME
43     # include <sys/time.h>
44     # include <time.h>
45     #else
46     # ifdef HAVE_SYS_TIME_H
47     # include <sys/time.h>
48     # else
49     # include <time.h>
50     # endif
51     #endif
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    
58     #include "chan.h"
59     #include "modules.h"
60     #include "tandem.h"
61     #include "bg.h"
62    
63     #ifdef DEBUG /* For debug compile */
64     # include <sys/resource.h> /* setrlimit() */
65     #endif
66    
67     #ifdef CYGWIN_HACKS
68     # include <windows.h>
69     #endif
70    
71     #ifndef _POSIX_SOURCE
72     # define _POSIX_SOURCE 1 /* Solaris needs this */
73     #endif
74    
75     extern char origbotname[], userfile[], botnetnick[];
76     extern int dcc_total, conmask, cache_hit, cache_miss, max_logs, quick_logs,
77     quiet_save;
78     extern struct dcc_t *dcc;
79     extern struct userrec *userlist;
80     extern struct chanset_t *chanset;
81     extern log_t *logs;
82     extern Tcl_Interp *interp;
83     extern tcl_timer_t *timer, *utimer;
84     extern sigjmp_buf alarmret;
85     time_t now;
86     static int argc;
87     static char **argv;
88    
89     /*
90     * Please use the PATCH macro instead of directly altering the version
91     * string from now on (it makes it much easier to maintain patches).
92     * Also please read the README file regarding your rights to distribute
93     * modified versions of this bot.
94     */
95    
96     char egg_version[1024] = "1.8.0";
97     int egg_numver = 1080000;
98    
99     char notify_new[121] = ""; /* Person to send a note to for new users */
100     int default_flags = 0; /* Default user flags */
101     int default_uflags = 0; /* Default user-definied flags */
102    
103     int backgrd = 1; /* Run in the background? */
104     int con_chan = 0; /* Foreground: constantly display channel stats? */
105     int term_z = 0; /* Foreground: use the terminal as a partyline? */
106     int use_stderr = 1; /* Send stuff to stderr instead of logfiles? */
107    
108     char configfile[121] = "eggdrop.conf"; /* Default config file name */
109     char pid_file[120]; /* Name of the pid file */
110     char helpdir[121] = "help/"; /* Directory of help files */
111     char textdir[121] = "text/"; /* Directory for text files */
112    
113     int keep_all_logs = 0; /* Never erase logfiles? */
114     char logfile_suffix[21] = ".%d%b%Y"; /* Format of logfile suffix */
115     int switch_logfiles_at = 300; /* When to switch logfiles */
116    
117     time_t online_since; /* time that the bot was started */
118    
119     int make_userfile = 0; /* Using bot in userfile-creation mode? */
120     char owner[121] = ""; /* Permanent owner(s) of the bot */
121    
122     int save_users_at = 0; /* Minutes past the hour to save the userfile? */
123     int notify_users_at = 0; /* Minutes past the hour to notify users of notes? */
124    
125     char version[81]; /* Version info (long form) */
126     char ver[41]; /* Version info (short form) */
127     char egg_xtra[2048]; /* Patch info */
128    
129     int do_restart = 0; /* .restart has been called, restart ASAP */
130     int resolve_timeout = 15; /* Hostname/address lookup timeout */
131     char quit_msg[1024]; /* Quit message */
132    
133     /* Traffic stats */
134     unsigned long otraffic_irc = 0;
135     unsigned long otraffic_irc_today = 0;
136     unsigned long otraffic_bn = 0;
137     unsigned long otraffic_bn_today = 0;
138     unsigned long otraffic_dcc = 0;
139     unsigned long otraffic_dcc_today = 0;
140     unsigned long otraffic_filesys = 0;
141     unsigned long otraffic_filesys_today = 0;
142     unsigned long otraffic_trans = 0;
143     unsigned long otraffic_trans_today = 0;
144     unsigned long otraffic_unknown = 0;
145     unsigned long otraffic_unknown_today = 0;
146     unsigned long itraffic_irc = 0;
147     unsigned long itraffic_irc_today = 0;
148     unsigned long itraffic_bn = 0;
149     unsigned long itraffic_bn_today = 0;
150     unsigned long itraffic_dcc = 0;
151     unsigned long itraffic_dcc_today = 0;
152     unsigned long itraffic_trans = 0;
153     unsigned long itraffic_trans_today = 0;
154     unsigned long itraffic_unknown = 0;
155     unsigned long itraffic_unknown_today = 0;
156    
157     #ifdef DEBUG_CONTEXT
158     /* Context storage for fatal crashes */
159     char cx_file[16][30];
160     char cx_note[16][256];
161     int cx_line[16];
162     int cx_ptr = 0;
163     #endif
164    
165 pseudo 1.4 #ifdef TLS
166     int ssl_cleanup();
167     #endif
168 simple 1.1
169     void fatal(const char *s, int recoverable)
170     {
171     int i;
172    
173     putlog(LOG_MISC, "*", "* %s", s);
174     flushlogs();
175     for (i = 0; i < dcc_total; i++)
176     if (dcc[i].sock >= 0)
177     killsock(dcc[i].sock);
178 pseudo 1.4 #ifdef TLS
179     ssl_cleanup();
180     #endif
181 simple 1.1 unlink(pid_file);
182 pseudo 1.6 if (recoverable != 1) {
183 simple 1.1 bg_send_quit(BG_ABORT);
184 pseudo 1.6 exit(!recoverable);
185 simple 1.1 }
186     }
187    
188     int expmem_chanprog();
189     int expmem_users();
190     int expmem_misc();
191     int expmem_dccutil();
192     int expmem_botnet();
193     int expmem_tcl();
194     int expmem_tclhash();
195     int expmem_net();
196     int expmem_modules(int);
197     int expmem_language();
198     int expmem_tcldcc();
199     int expmem_tclmisc();
200 pseudo 1.4 int expmem_dns();
201     #ifdef TLS
202     int expmem_tls();
203     #endif
204 simple 1.1
205     /* For mem.c : calculate memory we SHOULD be using
206     */
207     int expected_memory(void)
208     {
209     int tot;
210    
211     tot = expmem_chanprog() + expmem_users() + expmem_misc() + expmem_dccutil() +
212     expmem_botnet() + expmem_tcl() + expmem_tclhash() + expmem_net() +
213     expmem_modules(0) + expmem_language() + expmem_tcldcc() +
214 pseudo 1.4 expmem_tclmisc() + expmem_dns();
215     #ifdef TLS
216     tot += expmem_tls();
217     #endif
218 simple 1.1 return tot;
219     }
220    
221     static void check_expired_dcc()
222     {
223     int i;
224    
225     for (i = 0; i < dcc_total; i++)
226     if (dcc[i].type && dcc[i].type->timeout_val &&
227     ((now - dcc[i].timeval) > *(dcc[i].type->timeout_val))) {
228     if (dcc[i].type->timeout)
229     dcc[i].type->timeout(i);
230     else if (dcc[i].type->eof)
231     dcc[i].type->eof(i);
232     else
233     continue;
234     /* Only timeout 1 socket per cycle, too risky for more */
235     return;
236     }
237     }
238    
239     #ifndef DEBUG_CONTEXT
240     #define write_debug() do {} while (0)
241     #else
242     static int nested_debug = 0;
243    
244     static void write_debug()
245     {
246     int x;
247     char s[25];
248     int y;
249    
250     if (nested_debug) {
251     /* Yoicks, if we have this there's serious trouble!
252     * All of these are pretty reliable, so we'll try these.
253     *
254     * NOTE: dont try and display context-notes in here, it's
255     * _not_ safe <cybah>
256     */
257     x = creat("DEBUG.DEBUG", 0644);
258     setsock(x, SOCK_NONSOCK);
259     if (x >= 0) {
260     strncpyz(s, ctime(&now), sizeof s);
261     dprintf(-x, "Debug (%s) written %s\n", ver, s);
262     dprintf(-x, "Please report problem to bugs@eggheads.org\n");
263     dprintf(-x, "after a visit to http://www.eggheads.org/bugzilla/\n");
264     dprintf(-x, "Full Patch List: %s\n", egg_xtra);
265     dprintf(-x, "Context: ");
266     cx_ptr = cx_ptr & 15;
267     for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
268     dprintf(-x, "%s/%d,\n ", cx_file[y], cx_line[y]);
269     dprintf(-x, "%s/%d\n\n", cx_file[y], cx_line[y]);
270     killsock(x);
271     close(x);
272     }
273     bg_send_quit(BG_ABORT);
274     exit(1); /* Dont even try & tell people about, that may
275     * have caused the fault last time. */
276     } else
277     nested_debug = 1;
278     putlog(LOG_MISC, "*", "* Last context: %s/%d [%s]", cx_file[cx_ptr],
279     cx_line[cx_ptr], cx_note[cx_ptr][0] ? cx_note[cx_ptr] : "");
280     putlog(LOG_MISC, "*", "* Please REPORT this BUG!");
281     putlog(LOG_MISC, "*", "* Check doc/BUG-REPORT on how to do so.");
282     x = creat("DEBUG", 0644);
283     setsock(x, SOCK_NONSOCK);
284     if (x < 0) {
285 pseudo 1.6.2.1 putlog(LOG_MISC, "*", _("* Failed to write DEBUG"));
286 simple 1.1 } else {
287     strncpyz(s, ctime(&now), sizeof s);
288     dprintf(-x, "Debug (%s) written %s\n", ver, s);
289     dprintf(-x, "Full Patch List: %s\n", egg_xtra);
290     #ifdef STATIC
291     dprintf(-x, "STATICALLY LINKED\n");
292     #endif
293    
294     /* info library */
295     dprintf(-x, "Tcl library: %s\n",
296     ((interp) && (Tcl_Eval(interp, "info library") == TCL_OK)) ?
297     tcl_resultstring() : "*unknown*");
298    
299     /* info tclversion/patchlevel */
300     dprintf(-x, "Tcl version: %s (header version %s)\n",
301     ((interp) && (Tcl_Eval(interp, "info patchlevel") == TCL_OK)) ?
302     tcl_resultstring() : (Tcl_Eval(interp, "info tclversion") == TCL_OK) ?
303     tcl_resultstring() : "*unknown*", TCL_PATCH_LEVEL ? TCL_PATCH_LEVEL :
304     "*unknown*");
305    
306     if (tcl_threaded())
307     dprintf(-x, "Tcl is threaded\n");
308 pseudo 1.3 #ifdef IPV6
309     dprintf(-x, "Compiled with IPv6 support\n");
310     #else
311     dprintf(-x, "Compiled without IPv6 support\n");
312     #endif
313 simple 1.1
314 pseudo 1.5 #ifdef TLS
315     dprintf(-x, "Compiled with TLS support\n");
316     #else
317     dprintf(-x, "Compiled without TLS support\n");
318     #endif
319    
320 pseudo 1.3 dprintf(-x, "Configure flags: %s\n", EGG_AC_ARGS);
321 simple 1.1 #ifdef CCFLAGS
322     dprintf(-x, "Compile flags: %s\n", CCFLAGS);
323     #endif
324    
325     #ifdef LDFLAGS
326     dprintf(-x, "Link flags: %s\n", LDFLAGS);
327     #endif
328    
329     #ifdef STRIPFLAGS
330     dprintf(-x, "Strip flags: %s\n", STRIPFLAGS);
331     #endif
332    
333     dprintf(-x, "Context: ");
334     cx_ptr = cx_ptr & 15;
335     for (y = ((cx_ptr + 1) & 15); y != cx_ptr; y = ((y + 1) & 15))
336     dprintf(-x, "%s/%d, [%s]\n ", cx_file[y], cx_line[y],
337     (cx_note[y][0]) ? cx_note[y] : "");
338     dprintf(-x, "%s/%d [%s]\n\n", cx_file[cx_ptr], cx_line[cx_ptr],
339     (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
340     tell_dcc(-x);
341     dprintf(-x, "\n");
342     debug_mem_to_dcc(-x);
343     killsock(x);
344     close(x);
345     putlog(LOG_MISC, "*", "* Wrote DEBUG");
346     }
347     }
348     #endif /* DEBUG_CONTEXT */
349    
350     static void got_bus(int z)
351     {
352     write_debug();
353     fatal("BUS ERROR -- CRASHING!", 1);
354     #ifdef SA_RESETHAND
355     kill(getpid(), SIGBUS);
356     #else
357     bg_send_quit(BG_ABORT);
358     exit(1);
359     #endif
360     }
361    
362     static void got_segv(int z)
363     {
364     write_debug();
365     fatal("SEGMENT VIOLATION -- CRASHING!", 1);
366     #ifdef SA_RESETHAND
367     kill(getpid(), SIGSEGV);
368     #else
369     bg_send_quit(BG_ABORT);
370     exit(1);
371     #endif
372     }
373    
374     static void got_fpe(int z)
375     {
376     write_debug();
377     fatal("FLOATING POINT ERROR -- CRASHING!", 0);
378     }
379    
380     static void got_term(int z)
381     {
382 pseudo 1.6 /* Now we die by default on sigterm, but scripts have the chance to
383     * catch the event themselves and cancel shutdown by returning 1
384     */
385     if (check_tcl_event("sigterm"))
386     return;
387 pseudo 1.6.2.1 kill_bot(_("ACK, I've been terminated!"),
388     _("TERMINATE SIGNAL -- SIGNING OFF"));
389 simple 1.1 }
390    
391     static void got_quit(int z)
392     {
393 pseudo 1.6 if (check_tcl_event("sigquit"))
394     return;
395 pseudo 1.6.2.1 putlog(LOG_MISC, "*", _("Received QUIT signal: restarting..."));
396 pseudo 1.6 do_restart = -1;
397 simple 1.1 return;
398     }
399    
400     static void got_hup(int z)
401     {
402     write_userfile(-1);
403 pseudo 1.6 if (check_tcl_event("sighup"))
404     return;
405 pseudo 1.6.2.1 putlog(LOG_MISC, "*", _("Received HUP signal: rehashing..."));
406 simple 1.1 do_restart = -2;
407     return;
408     }
409    
410     /* A call to resolver (gethostbyname, etc) timed out
411     */
412     static void got_alarm(int z)
413     {
414     siglongjmp(alarmret, 1);
415    
416     /* -Never reached- */
417     }
418    
419     /* Got ILL signal -- log context and continue
420     */
421     static void got_ill(int z)
422     {
423     check_tcl_event("sigill");
424     #ifdef DEBUG_CONTEXT
425     putlog(LOG_MISC, "*", "* Context: %s/%d [%s]", cx_file[cx_ptr],
426     cx_line[cx_ptr], (cx_note[cx_ptr][0]) ? cx_note[cx_ptr] : "");
427     #endif
428     }
429    
430     #ifdef DEBUG_CONTEXT
431     /* Called from the Context macro.
432     */
433     void eggContext(const char *file, int line, const char *module)
434     {
435     char x[31], *p;
436    
437     p = strrchr(file, '/');
438     if (!module) {
439     strncpyz(x, p ? p + 1 : file, sizeof x);
440     } else
441     egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
442     cx_ptr = ((cx_ptr + 1) & 15);
443     strcpy(cx_file[cx_ptr], x);
444     cx_line[cx_ptr] = line;
445     cx_note[cx_ptr][0] = 0;
446     }
447    
448     /* Called from the ContextNote macro.
449     */
450     void eggContextNote(const char *file, int line, const char *module,
451     const char *note)
452     {
453     char x[31], *p;
454    
455     p = strrchr(file, '/');
456     if (!module)
457     strncpyz(x, p ? p + 1 : file, sizeof x);
458     else
459     egg_snprintf(x, 31, "%s:%s", module, p ? p + 1 : file);
460     cx_ptr = ((cx_ptr + 1) & 15);
461     strcpy(cx_file[cx_ptr], x);
462     cx_line[cx_ptr] = line;
463     strncpyz(cx_note[cx_ptr], note, sizeof cx_note[cx_ptr]);
464     }
465     #endif /* DEBUG_CONTEXT */
466    
467     #ifdef DEBUG_ASSERT
468     /* Called from the Assert macro.
469     */
470     void eggAssert(const char *file, int line, const char *module)
471     {
472     write_debug();
473     if (!module)
474     putlog(LOG_MISC, "*", "* In file %s, line %u", file, line);
475     else
476     putlog(LOG_MISC, "*", "* In file %s:%s, line %u", module, file, line);
477     fatal("ASSERT FAILED -- CRASHING!", 1);
478     }
479     #endif
480    
481     static void do_arg(char *s)
482     {
483     char x[512], *z = x;
484     int i;
485    
486     if (s[0] == '-')
487     for (i = 1; i < strlen(s); i++) {
488     switch (s[i]) {
489     case 'n':
490     backgrd = 0;
491     break;
492     case 'c':
493     con_chan = 1;
494     term_z = 0;
495     break;
496     case 't':
497     con_chan = 0;
498     term_z = 1;
499     break;
500     case 'm':
501     make_userfile = 1;
502     break;
503     case 'v':
504     strncpyz(x, egg_version, sizeof x);
505     newsplit(&z);
506     newsplit(&z);
507     printf("%s\n", version);
508     if (z[0])
509 pseudo 1.6.2.1 printf(_(" (patches: %s)\n"), z);
510     printf(_("Configured with: %s\n"), EGG_AC_ARGS);
511     printf(_("Compiled with: "));
512 pseudo 1.3 #ifdef IPV6
513     printf("IPv6, ");
514     #endif
515 pseudo 1.5 #ifdef TLS
516     printf("TLS, ");
517     #endif
518 pseudo 1.6.2.1 printf("%s=%d\n", _("handlen"), HANDLEN);
519 simple 1.1 bg_send_quit(BG_ABORT);
520     exit(0);
521     break; /* this should never be reached */
522     case 'h':
523     printf("\n%s\n\n", version);
524 pseudo 1.6.2.1 printf(_("Usage: eggdrop [options] [config-file]\n\n"
525     "Options:\n"
526     " -h help\n"
527     " -n Don't background; send all log entries to console.\n"
528     " -nc Don't background; display channel stats every 10 seconds.\n"
529     " -nt Don't background; use terminal to simulate DCC chat.\n"
530     " -m Create userfile.\n"
531     " -h Show this help.\n"
532     " -v Show version info, then quit.\n\n"));
533 simple 1.1 printf("\n");
534     bg_send_quit(BG_ABORT);
535     exit(0);
536     break; /* this should never be reached */
537     }
538     } else
539     strncpyz(configfile, s, sizeof configfile);
540     }
541    
542     void backup_userfile(void)
543     {
544     char s[125];
545    
546     if (quiet_save < 2)
547 pseudo 1.6.2.1 putlog(LOG_MISC, "*", _("Backing up user file..."));
548 simple 1.1 egg_snprintf(s, sizeof s, "%s~bak", userfile);
549     copyfile(userfile, s);
550     }
551    
552     /* Timer info */
553     static int lastmin = 99;
554     static time_t then;
555     static struct tm nowtm;
556    
557     /* Called once a second.
558     *
559     * Note: Try to not put any Context lines in here (guppy 21Mar2000).
560     */
561     static void core_secondly()
562     {
563     static int cnt = 0;
564     int miltime;
565    
566     do_check_timers(&utimer); /* Secondly timers */
567     cnt++;
568     if (cnt >= 10) { /* Every 10 seconds */
569     cnt = 0;
570     check_expired_dcc();
571     if (con_chan && !backgrd) {
572     dprintf(DP_STDOUT, "\033[2J\033[1;1H");
573     tell_verbose_status(DP_STDOUT);
574     do_module_report(DP_STDOUT, 0, "server");
575     do_module_report(DP_STDOUT, 0, "channels");
576     tell_mem_status_dcc(DP_STDOUT);
577     }
578     }
579     egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
580     if (nowtm.tm_min != lastmin) {
581     int i = 0;
582    
583     /* Once a minute */
584     lastmin = (lastmin + 1) % 60;
585     call_hook(HOOK_MINUTELY);
586     check_expired_ignores();
587     autolink_cycle(NULL); /* Attempt autolinks */
588     /* In case for some reason more than 1 min has passed: */
589     while (nowtm.tm_min != lastmin) {
590     /* Timer drift, dammit */
591     debug2("timer: drift (lastmin=%d, now=%d)", lastmin, nowtm.tm_min);
592     i++;
593     lastmin = (lastmin + 1) % 60;
594     call_hook(HOOK_MINUTELY);
595     }
596     if (i > 1)
597 pseudo 1.6.2.1 putlog(LOG_MISC, "*", _("(!) timer drift -- spun %d minutes"), i);
598 simple 1.1 miltime = (nowtm.tm_hour * 100) + (nowtm.tm_min);
599     if (((int) (nowtm.tm_min / 5) * 5) == (nowtm.tm_min)) { /* 5 min */
600     call_hook(HOOK_5MINUTELY);
601     check_botnet_pings();
602     if (!quick_logs) {
603     flushlogs();
604     check_logsize();
605     }
606     if (!miltime) { /* At midnight */
607     char s[25];
608     int j;
609    
610     strncpyz(s, ctime(&now), sizeof s);
611     if (quiet_save < 3)
612     putlog(LOG_ALL, "*", "--- %.11s%s", s, s + 20);
613     call_hook(HOOK_BACKUP);
614     for (j = 0; j < max_logs; j++) {
615     if (logs[j].filename != NULL && logs[j].f != NULL) {
616     fclose(logs[j].f);
617     logs[j].f = NULL;
618     }
619     }
620     }
621     }
622     if (nowtm.tm_min == notify_users_at)
623     call_hook(HOOK_HOURLY);
624     /* These no longer need checking since they are all check vs minutely
625     * settings and we only get this far on the minute.
626     */
627     if (miltime == switch_logfiles_at) {
628     call_hook(HOOK_DAILY);
629     if (!keep_all_logs) {
630     if (quiet_save < 3)
631 pseudo 1.6.2.1 putlog(LOG_MISC, "*", _("Switching logfiles..."));
632 simple 1.1 for (i = 0; i < max_logs; i++)
633     if (logs[i].filename) {
634     char s[1024];
635    
636     if (logs[i].f) {
637     fclose(logs[i].f);
638     logs[i].f = NULL;
639     }
640     egg_snprintf(s, sizeof s, "%s.yesterday", logs[i].filename);
641     unlink(s);
642     movefile(logs[i].filename, s);
643     }
644     }
645     }
646     }
647     }
648    
649     static void core_minutely()
650     {
651     check_tcl_time(&nowtm);
652     check_tcl_cron(&nowtm);
653     do_check_timers(&timer);
654     if (quick_logs != 0) {
655     flushlogs();
656     check_logsize();
657     }
658     }
659    
660     static void core_hourly()
661     {
662     write_userfile(-1);
663     }
664    
665     static void event_rehash()
666     {
667     check_tcl_event("rehash");
668     }
669    
670     static void event_prerehash()
671     {
672     check_tcl_event("prerehash");
673     }
674    
675     static void event_save()
676     {
677     check_tcl_event("save");
678     }
679    
680     static void event_logfile()
681     {
682     check_tcl_event("logfile");
683     }
684    
685     static void event_resettraffic()
686     {
687     otraffic_irc += otraffic_irc_today;
688     itraffic_irc += itraffic_irc_today;
689     otraffic_bn += otraffic_bn_today;
690     itraffic_bn += itraffic_bn_today;
691     otraffic_dcc += otraffic_dcc_today;
692     itraffic_dcc += itraffic_dcc_today;
693     otraffic_unknown += otraffic_unknown_today;
694     itraffic_unknown += itraffic_unknown_today;
695     otraffic_trans += otraffic_trans_today;
696     itraffic_trans += itraffic_trans_today;
697     otraffic_irc_today = otraffic_bn_today = 0;
698     otraffic_dcc_today = otraffic_unknown_today = 0;
699     itraffic_irc_today = itraffic_bn_today = 0;
700     itraffic_dcc_today = itraffic_unknown_today = 0;
701     itraffic_trans_today = otraffic_trans_today = 0;
702     }
703    
704     static void event_loaded()
705     {
706     check_tcl_event("loaded");
707     }
708    
709     void kill_tcl();
710     extern module_entry *module_list;
711     void restart_chons();
712    
713     #ifdef STATIC
714     void check_static(char *, char *(*)());
715    
716     #include "mod/static.h"
717     #endif
718     int init_threaddata(int);
719     int init_mem();
720     int init_userent();
721     int init_misc();
722     int init_bots();
723     int init_modules();
724     int init_tcl(int, char **);
725     int init_language(int);
726 pseudo 1.4 #ifdef TLS
727     int ssl_init();
728     #endif
729 simple 1.1
730     static void patch(const char *str)
731     {
732     char *p = strchr(egg_version, '+');
733    
734     if (!p)
735     p = &egg_version[strlen(egg_version)];
736     sprintf(p, "+%s", str);
737     egg_numver++;
738     sprintf(&egg_xtra[strlen(egg_xtra)], " %s", str);
739     }
740    
741     static inline void garbage_collect(void)
742     {
743     static u_8bit_t run_cnt = 0;
744    
745     if (run_cnt == 3)
746     garbage_collect_tclhash();
747     else
748     run_cnt++;
749     }
750    
751     int mainloop(int toplevel)
752     {
753     static int socket_cleanup = 0;
754     int xx, i, eggbusy = 1, tclbusy = 0;
755     char buf[520];
756    
757     /* Lets move some of this here, reducing the numer of actual
758     * calls to periodic_timers
759     */
760     now = time(NULL);
761     /*
762     * FIXME: Get rid of this, it's ugly and wastes lots of cpu.
763     *
764     * pre-1.3.0 Eggdrop had random() in the once a second block below.
765     *
766     * This attempts to keep random() more random by constantly
767     * calling random() and updating the state information.
768     */
769     random(); /* Woop, lets really jumble things */
770    
771     /* If we want to restart, we have to unwind to the toplevel.
772     * Tcl will Panic if we kill the interp with Tcl_Eval in progress.
773     * This is done by returning -1 in tickle_WaitForEvent.
774     */
775     if (do_restart && do_restart != -2 && !toplevel)
776     return -1;
777    
778     /* Once a second */
779     if (now != then) {
780     call_hook(HOOK_SECONDLY);
781     then = now;
782     }
783    
784     /* Only do this every so often. */
785     if (!socket_cleanup) {
786     socket_cleanup = 5;
787    
788     /* Remove dead dcc entries. */
789     dcc_remove_lost();
790    
791     /* Check for server or dcc activity. */
792     dequeue_sockets();
793     } else
794     socket_cleanup--;
795    
796     /* Free unused structures. */
797     garbage_collect();
798    
799     xx = sockgets(buf, &i);
800     if (xx >= 0) { /* Non-error */
801     int idx;
802    
803     for (idx = 0; idx < dcc_total; idx++)
804     if (dcc[idx].sock == xx) {
805     if (dcc[idx].type && dcc[idx].type->activity) {
806     /* Traffic stats */
807     if (dcc[idx].type->name) {
808     if (!strncmp(dcc[idx].type->name, "BOT", 3))
809     itraffic_bn_today += strlen(buf) + 1;
810     else if (!strcmp(dcc[idx].type->name, "SERVER"))
811     itraffic_irc_today += strlen(buf) + 1;
812     else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
813     itraffic_dcc_today += strlen(buf) + 1;
814     else if (!strncmp(dcc[idx].type->name, "FILES", 5))
815     itraffic_dcc_today += strlen(buf) + 1;
816     else if (!strcmp(dcc[idx].type->name, "SEND"))
817     itraffic_trans_today += strlen(buf) + 1;
818     else if (!strncmp(dcc[idx].type->name, "GET", 3))
819     itraffic_trans_today += strlen(buf) + 1;
820     else
821     itraffic_unknown_today += strlen(buf) + 1;
822     }
823     dcc[idx].type->activity(idx, buf, i);
824     } else
825     putlog(LOG_MISC, "*",
826 pseudo 1.6.2.1 _("!!! untrapped dcc activity: type %s, sock %d"),
827 simple 1.1 dcc[idx].type->name, dcc[idx].sock);
828     break;
829     }
830     } else if (xx == -1) { /* EOF from someone */
831     int idx;
832    
833     if (i == STDOUT && !backgrd)
834 pseudo 1.6.2.1 fatal(_("END OF FILE ON TERMINAL"), 0);
835 simple 1.1 for (idx = 0; idx < dcc_total; idx++)
836     if (dcc[idx].sock == i) {
837     if (dcc[idx].type && dcc[idx].type->eof)
838     dcc[idx].type->eof(idx);
839     else {
840 pseudo 1.6.2.1 if (dcc[idx].type)
841     putlog(LOG_MISC, "*", _("*** ATTENTION: DEAD SOCKET (%d) "
842     "OF TYPE %s UNTRAPPED"), i, dcc[idx].type->name);
843     else
844     putlog(LOG_MISC, "*", _("*** ATTENTION: DEAD SOCKET (%d) "
845     "OF UNKNOWN TYPE UNTRAPPED"), i);
846 simple 1.1 killsock(i);
847     lostdcc(idx);
848     }
849     idx = dcc_total + 1;
850     }
851     if (idx == dcc_total) {
852     putlog(LOG_MISC, "*",
853 pseudo 1.6.2.1 _("(@) EOF socket %d, not a dcc socket, not anything."), i);
854 simple 1.1 close(i);
855     killsock(i);
856     }
857     } else if (xx == -2 && errno != EINTR) { /* select() error */
858 pseudo 1.6.2.1 putlog(LOG_MISC, "*", _("* Socket error #%d; recovering."), errno);
859 simple 1.1 for (i = 0; i < dcc_total; i++) {
860     if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno == EBADF)) {
861     putlog(LOG_MISC, "*",
862 pseudo 1.6.2.1 _("DCC socket %d (type %d, name '%s') expired -- pfft"),
863 simple 1.1 dcc[i].sock, dcc[i].type, dcc[i].nick);
864     killsock(dcc[i].sock);
865     lostdcc(i);
866     i--;
867     }
868     }
869     } else if (xx == -3) {
870     call_hook(HOOK_IDLE);
871     socket_cleanup = 0; /* If we've been idle, cleanup & flush */
872     eggbusy = 0;
873     } else if (xx == -5) {
874     eggbusy = 0;
875     tclbusy = 1;
876     }
877    
878     if (do_restart) {
879     if (do_restart == -2)
880     rehash();
881     else if (!toplevel)
882     return -1; /* Unwind to toplevel before restarting */
883     else {
884     /* Unload as many modules as possible */
885     int f = 1;
886     module_entry *p;
887     Function startfunc;
888     char name[256];
889    
890     /* oops, I guess we should call this event before tcl is restarted */
891     check_tcl_event("prerestart");
892    
893     while (f) {
894     f = 0;
895     for (p = module_list; p != NULL; p = p->next) {
896     dependancy *d = dependancy_list;
897     int ok = 1;
898    
899     while (ok && d) {
900     if (d->needed == p)
901     ok = 0;
902     d = d->next;
903     }
904     if (ok) {
905     strcpy(name, p->name);
906     if (module_unload(name, botnetnick) == NULL) {
907     f = 1;
908     break;
909     }
910     }
911     }
912     }
913    
914     /* Make sure we don't have any modules left hanging around other than
915     * "eggdrop" and the two that are supposed to be.
916     */
917     for (f = 0, p = module_list; p; p = p->next) {
918     if (strcmp(p->name, "eggdrop") && strcmp(p->name, "encryption") &&
919     strcmp(p->name, "uptime")) {
920     f++;
921     }
922     }
923     if (f != 0) {
924 pseudo 1.6.2.1 putlog(LOG_MISC, "*", _("Stagnant modules; "
925     "there WILL be memory leaks!"));
926 simple 1.1 }
927    
928     flushlogs();
929     kill_tcl();
930     init_tcl(argc, argv);
931     init_language(0);
932    
933     /* this resets our modules which we didn't unload (encryption and uptime) */
934     for (p = module_list; p; p = p->next) {
935     if (p->funcs) {
936     startfunc = p->funcs[MODCALL_START];
937     startfunc(NULL);
938     }
939     }
940    
941     rehash();
942 pseudo 1.4 #ifdef TLS
943     ssl_cleanup();
944     ssl_init();
945     #endif
946 simple 1.1 restart_chons();
947     call_hook(HOOK_LOADED);
948     }
949     eggbusy = 1;
950     do_restart = 0;
951     }
952    
953     #ifdef USE_TCL_EVENTS
954     if (!eggbusy) {
955     /* Process all pending tcl events */
956     # ifdef REPLACE_NOTIFIER
957     if (Tcl_ServiceAll())
958     tclbusy = 1;
959     # else
960     while (Tcl_DoOneEvent(TCL_DONT_WAIT | TCL_ALL_EVENTS))
961     tclbusy = 1;
962     # endif /* REPLACE_NOTIFIER */
963     #endif /* USE_TCL_EVENTS */
964     }
965    
966     return (eggbusy || tclbusy);
967     }
968    
969     int main(int arg_c, char **arg_v)
970     {
971     int i, xx;
972     char s[25];
973     FILE *f;
974     struct sigaction sv;
975     struct chanset_t *chan;
976     #ifdef DEBUG
977     struct rlimit cdlim;
978     #endif
979     #ifdef STOP_UAC
980     int nvpair[2];
981     #endif
982    
983     /* Make sure it can write core, if you make debug. Else it's pretty
984     * useless (dw)
985     *
986     * Only allow unlimited size core files when compiled with DEBUG defined.
987     * This is not a good idea for normal builds -- in these cases, use the
988     * default system resource limits instead.
989     */
990     #ifdef DEBUG
991     cdlim.rlim_cur = RLIM_INFINITY;
992     cdlim.rlim_max = RLIM_INFINITY;
993     setrlimit(RLIMIT_CORE, &cdlim);
994     #endif
995    
996     #ifdef DEBUG_CONTEXT
997     /* Initialise context list */
998     for (i = 0; i < 16; i++)
999     Context;
1000     #endif
1001    
1002     /* Include patch.h header for patch("...") */
1003     #include "patch.h"
1004    
1005     argc = arg_c;
1006     argv = arg_v;
1007    
1008     /* Version info! */
1009     egg_snprintf(ver, sizeof ver, "eggdrop v%s", egg_version);
1010     egg_snprintf(version, sizeof version,
1011     "Eggdrop v%s (C) 1997 Robey Pointer (C) 2010 Eggheads",
1012     egg_version);
1013     /* Now add on the patchlevel (for Tcl) */
1014     sprintf(&egg_version[strlen(egg_version)], " %u", egg_numver);
1015     strcat(egg_version, egg_xtra);
1016    
1017     /* For OSF/1 */
1018     #ifdef STOP_UAC
1019     /* Don't print "unaligned access fixup" warning to the user */
1020     nvpair[0] = SSIN_UACPROC;
1021     nvpair[1] = UAC_NOPRINT;
1022     setsysinfo(SSI_NVPAIRS, (char *) nvpair, 1, NULL, 0);
1023     #endif
1024    
1025     /* Set up error traps: */
1026     sv.sa_handler = got_bus;
1027     sigemptyset(&sv.sa_mask);
1028     #ifdef SA_RESETHAND
1029     sv.sa_flags = SA_RESETHAND;
1030     #else
1031     sv.sa_flags = 0;
1032     #endif
1033     sigaction(SIGBUS, &sv, NULL);
1034     sv.sa_handler = got_segv;
1035     sigaction(SIGSEGV, &sv, NULL);
1036     #ifdef SA_RESETHAND
1037     sv.sa_flags = 0;
1038     #endif
1039     sv.sa_handler = got_fpe;
1040     sigaction(SIGFPE, &sv, NULL);
1041     sv.sa_handler = got_term;
1042     sigaction(SIGTERM, &sv, NULL);
1043     sv.sa_handler = got_hup;
1044     sigaction(SIGHUP, &sv, NULL);
1045     sv.sa_handler = got_quit;
1046     sigaction(SIGQUIT, &sv, NULL);
1047     sv.sa_handler = SIG_IGN;
1048     sigaction(SIGPIPE, &sv, NULL);
1049     sv.sa_handler = got_ill;
1050     sigaction(SIGILL, &sv, NULL);
1051     sv.sa_handler = got_alarm;
1052     sigaction(SIGALRM, &sv, NULL);
1053    
1054     /* Initialize variables and stuff */
1055     now = time(NULL);
1056     chanset = NULL;
1057     egg_memcpy(&nowtm, localtime(&now), sizeof(struct tm));
1058     lastmin = nowtm.tm_min;
1059     srandom((unsigned int) (now % (getpid() + getppid())));
1060     init_mem();
1061     init_language(1);
1062     if (argc > 1)
1063     for (i = 1; i < argc; i++)
1064     do_arg(argv[i]);
1065    
1066     printf("\n%s\n", version);
1067    
1068     #ifndef CYGWIN_HACKS
1069     /* Don't allow eggdrop to run as root
1070     * This check isn't useful under cygwin and has been
1071     * reported to cause trouble in some situations.
1072     */
1073     if (((int) getuid() == 0) || ((int) geteuid() == 0))
1074 pseudo 1.6.2.1 fatal(_("ERROR: Eggdrop will not run as root!"), 0);
1075 simple 1.1 #endif
1076    
1077     #ifndef REPLACE_NOTIFIER
1078     init_threaddata(1);
1079     #endif
1080     init_userent();
1081     init_misc();
1082     init_bots();
1083     init_modules();
1084     if (backgrd)
1085     bg_prepare_split();
1086     init_tcl(argc, argv);
1087     init_language(0);
1088     #ifdef STATIC
1089     link_statics();
1090     #endif
1091     strncpyz(s, ctime(&now), sizeof s);
1092     strcpy(&s[11], &s[20]);
1093 pseudo 1.6.2.1 putlog(LOG_ALL, "*", _("--- Loading %s (%s)"), ver, s);
1094 simple 1.1 chanprog();
1095     if (!encrypt_pass) {
1096 pseudo 1.6.2.1 printf(_("You have installed modules but have not selected an encryption\n"
1097     "module. Please consult the default config file for info.\n"));
1098 simple 1.1 bg_send_quit(BG_ABORT);
1099     exit(1);
1100     }
1101     i = 0;
1102     for (chan = chanset; chan; chan = chan->next)
1103     i++;
1104 pseudo 1.6.2.1 putlog(LOG_MISC, "*", _("=== %s: %d channels, %d users."),
1105 simple 1.1 botnetnick, i, count_users(userlist));
1106 pseudo 1.4 #ifdef TLS
1107     ssl_init();
1108     #endif
1109 simple 1.1 cache_miss = 0;
1110     cache_hit = 0;
1111     if (!pid_file[0])
1112     egg_snprintf(pid_file, sizeof pid_file, "pid.%s", botnetnick);
1113    
1114     /* Check for pre-existing eggdrop! */
1115     f = fopen(pid_file, "r");
1116     if (f != NULL) {
1117     fgets(s, 10, f);
1118     xx = atoi(s);
1119     i = kill(xx, SIGCHLD); /* Meaningless kill to determine if pid
1120     * is used */
1121     if (i == 0 || errno != ESRCH) {
1122 pseudo 1.6.2.1 printf(_("I detect %s already running from this directory.\n"),
1123     botnetnick);
1124     printf(_("If this is incorrect, erase the '%s'\n"), pid_file);
1125 simple 1.1 bg_send_quit(BG_ABORT);
1126     exit(1);
1127     }
1128     }
1129    
1130     /* Move into background? */
1131     if (backgrd) {
1132     bg_do_split();
1133     } else { /* !backgrd */
1134     xx = getpid();
1135     if (xx != 0) {
1136     FILE *fp;
1137    
1138     /* Write pid to file */
1139     unlink(pid_file);
1140     fp = fopen(pid_file, "w");
1141     if (fp != NULL) {
1142     fprintf(fp, "%u\n", xx);
1143     if (fflush(fp)) {
1144     /* Let the bot live since this doesn't appear to be a botchk */
1145 pseudo 1.6.2.1 printf(_("* Warning! Could not write %s file!\n"), pid_file);
1146 simple 1.1 fclose(fp);
1147     unlink(pid_file);
1148     } else
1149     fclose(fp);
1150     } else
1151 pseudo 1.6.2.1 printf(_("* Warning! Could not write %s file!\n"), pid_file);
1152 simple 1.1 }
1153     }
1154    
1155     use_stderr = 0; /* Stop writing to stderr now */
1156     if (backgrd) {
1157     /* Ok, try to disassociate from controlling terminal (finger cross) */
1158     #ifdef HAVE_SETPGID
1159     setpgid(0, 0);
1160     #endif
1161     /* Tcl wants the stdin, stdout and stderr file handles kept open. */
1162     freopen("/dev/null", "r", stdin);
1163     freopen("/dev/null", "w", stdout);
1164     freopen("/dev/null", "w", stderr);
1165     #ifdef CYGWIN_HACKS
1166     FreeConsole();
1167     #endif
1168     }
1169    
1170     /* Terminal emulating dcc chat */
1171     if (!backgrd && term_z) {
1172     int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
1173    
1174 pseudo 1.2 getvhost(&dcc[n].sockname, AF_INET);
1175 simple 1.1 dcc[n].sock = STDOUT;
1176     dcc[n].timeval = now;
1177     dcc[n].u.chat->con_flags = conmask;
1178     dcc[n].u.chat->strip_flags = STRIP_ALL;
1179     dcc[n].status = STAT_ECHO;
1180     strcpy(dcc[n].nick, "HQ");
1181     strcpy(dcc[n].host, "llama@console");
1182     /* HACK: Workaround not to pass literal "HQ" as a non-const arg */
1183     dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
1184     /* Make sure there's an innocuous HQ user if needed */
1185     if (!dcc[n].user) {
1186     userlist = adduser(userlist, dcc[n].nick, "none", "-", USER_PARTY);
1187     dcc[n].user = get_user_by_handle(userlist, dcc[n].nick);
1188     }
1189     setsock(STDOUT, 0); /* Entry in net table */
1190 pseudo 1.6.2.1 dprintf(n, _("\n### ENTERING DCC CHAT SIMULATION ###\n\n"));
1191 simple 1.1 dcc_chatter(n);
1192     }
1193    
1194     then = now;
1195     online_since = now;
1196     autolink_cycle(NULL); /* Hurry and connect to tandem bots */
1197     add_help_reference("cmds1.help");
1198     add_help_reference("cmds2.help");
1199     add_help_reference("core.help");
1200     add_hook(HOOK_SECONDLY, (Function) core_secondly);
1201     add_hook(HOOK_MINUTELY, (Function) core_minutely);
1202     add_hook(HOOK_HOURLY, (Function) core_hourly);
1203     add_hook(HOOK_REHASH, (Function) event_rehash);
1204     add_hook(HOOK_PRE_REHASH, (Function) event_prerehash);
1205     add_hook(HOOK_USERFILE, (Function) event_save);
1206     add_hook(HOOK_BACKUP, (Function) backup_userfile);
1207     add_hook(HOOK_DAILY, (Function) event_logfile);
1208     add_hook(HOOK_DAILY, (Function) event_resettraffic);
1209     add_hook(HOOK_LOADED, (Function) event_loaded);
1210    
1211     call_hook(HOOK_LOADED);
1212    
1213     debug0("main: entering loop");
1214     while (1) {
1215     mainloop(1);
1216     }
1217     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23