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