| 1 | #!perl | ||||||||||
| 2 | ## simplistic cobalt2 Bot::Cobalt::Core frontend | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use 5.10.1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
| |||||||||||
| 9 | 1 | ▶ | pop @INC if $INC[-1] eq '.'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 10 | } | ||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | ## Default rcfile location. | ||||||||||
| 15 | my $rcfile = $ENV{HOME} ? | ||||||||||
| 16 | File::Spec->catfile( $ENV{HOME}, ".cobalt2rc" ) | ||||||||||
| 17 | : ".cobalt2rc" ; | ||||||||||
| 18 | |||||||||||
| 19 | my $opt_debug = 0; | ||||||||||
| 20 | my $opt_detach = 1; | ||||||||||
| 21 | my $loglevel = 'info'; | ||||||||||
| 22 | my ($etcdir, $vardir, $basedir); | ||||||||||
| 23 | |||||||||||
| 24 | # FIXME switch to Daemon::Control? drops Proc::PID::File | ||||||||||
| 25 | # and all the custom double-forkness | ||||||||||
| 26 | 1 | ▶ | use Proc::PID::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | 0 | ▶ | use Bot::Cobalt::Frontend::RC qw/rc_read/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||||||||||
| 29 | |||||||||||
| 30 | use Getopt::Long; | ||||||||||
| 31 | GetOptions( | ||||||||||
| 32 | ## Path to .cobalt2rc | ||||||||||
| 33 | 'rcfile=s' => \$rcfile, | ||||||||||
| 34 | 'config=s' => \$rcfile, | ||||||||||
| 35 | |||||||||||
| 36 | ## Override .cobalt2rc | ||||||||||
| 37 | 'rundir=s' => \$basedir, | ||||||||||
| 38 | 'base=s' => \$basedir, | ||||||||||
| 39 | |||||||||||
| 40 | ## Invocation opts | ||||||||||
| 41 | 'debug:+' => \$opt_debug, ## Overrides loglevel= | ||||||||||
| 42 | 'detach!' => \$opt_detach, | ||||||||||
| 43 | 'daemon!' => \$opt_detach, | ||||||||||
| 44 | 'loglevel=s' => \$loglevel, | ||||||||||
| 45 | |||||||||||
| 46 | ## Informational | ||||||||||
| 47 | version => sub { | ||||||||||
| 48 | require Bot::Cobalt; | ||||||||||
| 49 | my $vers = $Bot::Cobalt::VERSION // 'vcs'; | ||||||||||
| 50 | print "cobalt $vers\n"; | ||||||||||
| 51 | exit 0 | ||||||||||
| 52 | }, | ||||||||||
| 53 | |||||||||||
| 54 | help => \&show_help, | ||||||||||
| 55 | ); | ||||||||||
| 56 | |||||||||||
| 57 | sub show_help { | ||||||||||
| 58 | print( | ||||||||||
| 59 | "cobalt2 invocation help \n", | ||||||||||
| 60 | " --version \n", | ||||||||||
| 61 | " Display current Bot::Cobalt::Core version \n", | ||||||||||
| 62 | |||||||||||
| 63 | "\n", | ||||||||||
| 64 | " Execution:\n", | ||||||||||
| 65 | " --nodetach / --nodaemon \n", | ||||||||||
| 66 | " Run in the foreground (do not daemonize) \n", | ||||||||||
| 67 | " --loglevel=LOGLEVEL \n", | ||||||||||
| 68 | " Specify log verbosity. Defaults to 'info' \n", | ||||||||||
| 69 | " Valid levels, most verbose to least: \n", | ||||||||||
| 70 | " debug info notice warn err crit alert emerg \n", | ||||||||||
| 71 | " --debug / --nodebug / --debug=LEVEL\n", | ||||||||||
| 72 | " Enable debug output. Overrides loglevel. \n", | ||||||||||
| 73 | " Higher levels offer more verbosity. \n", | ||||||||||
| 74 | |||||||||||
| 75 | ## FIXME; note on POCOIRC_DEBUG and POE debug opts ? | ||||||||||
| 76 | |||||||||||
| 77 | "\n", | ||||||||||
| 78 | " Paths:\n", | ||||||||||
| 79 | " --rcfile=/path/to/rcfile \n", | ||||||||||
| 80 | " Specify a rcfile. Defaults to \$HOME/.cobalt2rc \n", | ||||||||||
| 81 | " --base=/path/to/basedir \n", | ||||||||||
| 82 | " Specify base path for 'etc/' and 'var/' for this instance\n", | ||||||||||
| 83 | " Overrides rcfile. \n", | ||||||||||
| 84 | ); | ||||||||||
| 85 | |||||||||||
| 86 | exit 0 | ||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | sub _rc_check { | ||||||||||
| 90 | unless (-e $rcfile) { | ||||||||||
| 91 | say ">! rcfile $rcfile not found."; | ||||||||||
| 92 | say ">! You can specify one via --rcfile="; | ||||||||||
| 93 | say ">! If this is your first time running cobalt2, try `cobalt2-installer`"; | ||||||||||
| 94 | die "rcfile not found" | ||||||||||
| 95 | } else { | ||||||||||
| 96 | return 1 | ||||||||||
| 97 | } | ||||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | sub _check_dirs { | ||||||||||
| 101 | my %paths = ( | ||||||||||
| 102 | etcdir => $etcdir, | ||||||||||
| 103 | vardir => $vardir, | ||||||||||
| 104 | ); | ||||||||||
| 105 | for my $thisname (keys %paths) { | ||||||||||
| 106 | my $thispath = $paths{$thisname}; | ||||||||||
| 107 | unless (-e $thispath) { | ||||||||||
| 108 | say ">! $thisname ($thispath) doesn't appear to exist."; | ||||||||||
| 109 | say ">! Your rcfile ($rcfile) may be broken."; | ||||||||||
| 110 | say ">! Perhaps try `cobalt2-installer`"; | ||||||||||
| 111 | die "$thisname not a directory" | ||||||||||
| 112 | } | ||||||||||
| 113 | } | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | sub _check_cfs { | ||||||||||
| 117 | ## Check if required confs exist in etcdir | ||||||||||
| 118 | ## Otherwise suggest cobalt2-installer | ||||||||||
| 119 | |||||||||||
| 120 | my @required = qw/ cobalt.conf channels.conf plugins.conf /; | ||||||||||
| 121 | |||||||||||
| 122 | for my $file (@required) { | ||||||||||
| 123 | unless (-e File::Spec->catfile($etcdir, $file) ) { | ||||||||||
| 124 | say ">! Missing core conf: $file"; | ||||||||||
| 125 | say ">! (etcdir: $etcdir)"; | ||||||||||
| 126 | say ">! You may want to try `cobalt2-installer`"; | ||||||||||
| 127 | die "missing core conf: $file" | ||||||||||
| 128 | } | ||||||||||
| 129 | } | ||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | |||||||||||
| 133 | sub _start_cobalt { | ||||||||||
| 134 | my $pid = Proc::PID::File->new( | ||||||||||
| 135 | dir => $vardir, | ||||||||||
| 136 | name => 'cobalt', | ||||||||||
| 137 | ); | ||||||||||
| 138 | die "cobalt appears to be already running\n" | ||||||||||
| 139 | if $pid->alive; | ||||||||||
| 140 | |||||||||||
| 141 | ## POSIX fork dance | ||||||||||
| 142 | use POSIX (); | ||||||||||
| 143 | if ($opt_detach) | ||||||||||
| 144 | { | ||||||||||
| 145 | say "Starting cobalt in background"; | ||||||||||
| 146 | my $fork = fork; | ||||||||||
| 147 | exit 1 if not defined $fork; | ||||||||||
| 148 | exit 0 if $fork; | ||||||||||
| 149 | POSIX::setsid(); | ||||||||||
| 150 | $fork = fork; | ||||||||||
| 151 | exit 1 if not defined $fork; | ||||||||||
| 152 | exit 0 if $fork; | ||||||||||
| 153 | chdir('/'); | ||||||||||
| 154 | open(STDIN, '<', '/dev/null'); | ||||||||||
| 155 | open(STDOUT, '>>', '/dev/null'); | ||||||||||
| 156 | open(STDERR, '>>', '/dev/null'); | ||||||||||
| 157 | umask(022); | ||||||||||
| 158 | } | ||||||||||
| 159 | $pid->touch(); | ||||||||||
| 160 | |||||||||||
| 161 | require Bot::Cobalt::Conf; | ||||||||||
| 162 | require Bot::Cobalt::Core; | ||||||||||
| 163 | |||||||||||
| 164 | ## FIXME could take paths to specific confs now | ||||||||||
| 165 | my $cfg = Bot::Cobalt::Conf->new( | ||||||||||
| 166 | etc => $etcdir, | ||||||||||
| 167 | debug => $opt_debug, | ||||||||||
| 168 | ); | ||||||||||
| 169 | |||||||||||
| 170 | Bot::Cobalt::Core->instance( | ||||||||||
| 171 | cfg => $cfg, | ||||||||||
| 172 | var => $vardir, | ||||||||||
| 173 | loglevel => $loglevel, | ||||||||||
| 174 | debug => $opt_debug, | ||||||||||
| 175 | detached => $opt_detach, | ||||||||||
| 176 | )->init; | ||||||||||
| 177 | |||||||||||
| 178 | POE::Kernel->run; | ||||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | |||||||||||
| 182 | say "-> debug ON, overrides loglevel" if $opt_debug; | ||||||||||
| 183 | ## Bot::Cobalt::Core does this anyway, but just for the validator: | ||||||||||
| 184 | $loglevel = $opt_debug ? 'debug' : lc $loglevel ; | ||||||||||
| 185 | ## Check specified loglevel | ||||||||||
| 186 | my @loglevels = qw/debug info notice warn warning | ||||||||||
| 187 | err error crit critical alert | ||||||||||
| 188 | emerg emergency/; | ||||||||||
| 189 | |||||||||||
| 190 | unless ($loglevel && grep { $_ eq $loglevel } @loglevels) { | ||||||||||
| 191 | say("Invalid loglevel ($loglevel)"); | ||||||||||
| 192 | say("Possible loglevels, most verbose to least: ".join(' ',@loglevels)); | ||||||||||
| 193 | say("Setting loglevel to INFO"); | ||||||||||
| 194 | |||||||||||
| 195 | $loglevel = 'info'; | ||||||||||
| 196 | } | ||||||||||
| 197 | |||||||||||
| 198 | |||||||||||
| 199 | if ($basedir) { | ||||||||||
| 200 | say ">! Using --basedir=${basedir}"; | ||||||||||
| 201 | ## A basedir was specified, disregard cobalt2rc | ||||||||||
| 202 | unless (-e $basedir) { | ||||||||||
| 203 | die "basedir $basedir specified but nonexistant"; | ||||||||||
| 204 | } | ||||||||||
| 205 | |||||||||||
| 206 | $etcdir = File::Spec->catdir($basedir, "etc"); | ||||||||||
| 207 | $vardir = File::Spec->catdir($basedir, "var"); | ||||||||||
| 208 | |||||||||||
| 209 | } else { | ||||||||||
| 210 | ## no basedir specified, try rcfile | ||||||||||
| 211 | _rc_check(); | ||||||||||
| 212 | ($basedir, $etcdir, $vardir) = rc_read($rcfile); | ||||||||||
| 213 | } | ||||||||||
| 214 | |||||||||||
| 215 | |||||||||||
| 216 | _check_dirs(); | ||||||||||
| 217 | _check_cfs(); | ||||||||||
| 218 | _start_cobalt(); | ||||||||||
| 219 | |||||||||||
| 220 | __END__ | ||||||||||