]> git.donarmstrong.com Git - infobot.git/blob - blootbot
- patch from Morten Brix Pedersen <morten@wtf.dk>. Thanks!
[infobot.git] / blootbot
1 #!/usr/bin/perl
2
3 # infobot  -- copyright kevin lenzo (c) 1997-1999
4 # blootbot -- copyright david sobon (c) 1999-infinity
5
6 use strict;
7 use vars qw($bot_base_dir $bot_src_dir $bot_misc_dir $bot_state_dir
8             $bot_data_dir $bot_config_dir $bot_log_dir $bot_run_dir 
9             $bot_pid $memusage %param
10 );
11
12 BEGIN {
13     if (@ARGV and -f $ARGV[0]) {
14         # source passed config to allow $bot_*_dir to be set.
15         do $ARGV[0];
16     }
17
18     # set any $bot_*_dir var's that aren't already set
19     $bot_base_dir       ||= '.';
20     $bot_config_dir     ||= 'files/';
21     $bot_data_dir       ||= 'files/';
22     $bot_state_dir      ||= 'files/';
23     $bot_run_dir        ||= '.';
24     $bot_src_dir        ||= "$bot_base_dir/src";
25     $bot_log_dir        ||= "$bot_base_dir/log";
26     $bot_misc_dir       ||= "$bot_base_dir/files";
27
28     $bot_pid            = $$;
29
30     require "$bot_src_dir/logger.pl";
31     require "$bot_src_dir/core.pl";
32     require "$bot_src_dir/interface.pl";
33     require "$bot_src_dir/modules.pl";
34
35     # load the configuration (params) file.
36     &setupConfig();
37
38     &showProc();        # to get the first value.
39     &status("Initial memory usage: $memusage kB");
40     &loadCoreModules();
41     &loadDBModules();
42     &loadFactoidsModules();
43     &loadIRCModules();
44
45     &status("Memory usage after loading modules: $memusage kB");
46 }
47
48 # prevent duplicate processes of the same bot
49 &duperuncheck();
50
51 # initialize everything 
52 &startup();     # first time initialization.
53 &setup();
54
55 if (!&IsParam("Interface") or $param{'Interface'} =~ /IRC/) {
56     # launch the irc event loop
57     &ircloop();
58 } else {
59     &cliloop();
60 }
61
62 exit 0;  # just so you don't look farther down in this file :)
63
64 # --- support routines
65
66 # FIXME.
67 #   add arguments, basically '-h' and '--help', heh.
68 #
69
70 # added by the xk
71 sub duperuncheck {
72     my $pid     = $$;
73     my $file    = $file{PID};
74
75     if ( -f $file) {
76         open(PIDFILE,$file) or die "error: cannot open $file.";
77         my $thispid = <PIDFILE> || "NULL\n";
78         close PIDFILE;
79         chop $thispid;
80
81         if ($thispid =~ /^\D$/) {
82             &staus("warning: pidfile is invalid; wiping out.");
83         } else {
84             if ( -d "/proc/$thispid/") {
85                 &ERROR("bot is already running from this directory.");
86                 &ERROR("if this is incorrect, erase '*.pid'.");
87                 &ERROR("verify with 'ps -axu | grep $thispid'.");
88                 exit 1;
89             } else {
90                 &status("warning: stale $file found; wiping.");
91             }
92         }
93     }
94
95     open(PIDFILE,">$file") or die "error: cannot write to $file.";
96     print PIDFILE "$pid\n";
97     close PIDFILE;
98
99     return 0;
100 }
101
102 1;