| 1 | #!perl | ||
| 2 | my $VERSION = 2; | ||
| 3 | |||
| 4 | 1 | ▶ | use v5.10; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Scalar::Util 'reftype'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use POSIX (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use List::Objects::WithUtils; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | use Path::Tiny; | ||
| 13 | use Types::Path::Tiny -all; | ||
| 14 | |||
| 15 | use Digest::MD5 'md5_hex'; | ||
| 16 | use File::ShareDir 'dist_dir'; | ||
| 17 | |||
| 18 | use Bot::Cobalt::Utils 'rplprintf', 'mkpasswd'; | ||
| 19 | use Bot::Cobalt::Frontend::Utils ':all'; | ||
| 20 | use Bot::Cobalt::Frontend::RC 'rc_read', 'rc_write'; | ||
| 21 | |||
| 22 | |||
| 23 | my $Rcfile = path( | ||
| 24 | $ENV{HOME} ? $ENV{HOME} . '/.cobalt2rc' : '.cobalt2rc' | ||
| 25 | ); | ||
| 26 | |||
| 27 | my $NonInteractive = 0; | ||
| 28 | my $SharedEtc; # set to $dist_dir/etc in copy_base_confs if undef | ||
| 29 | |||
| 30 | use Getopt::Long; | ||
| 31 | GetOptions( | ||
| 32 | 'config|rcfile=s' => \$Rcfile, | ||
| 33 | 'noninteractive|n' => \$NonInteractive, | ||
| 34 | |||
| 35 | 'shared-etcdir=s' => \$SharedEtc, | ||
| 36 | |||
| 37 | version => sub { | ||
| 38 | say "Bot::Cobalt installer (version $VERSION)"; | ||
| 39 | exit 0 | ||
| 40 | }, | ||
| 41 | |||
| 42 | help => sub { | ||
| 43 | print map "$_\n", | ||
| 44 | "$0", | ||
| 45 | "", | ||
| 46 | "Initialize a Bot::Cobalt instance; see 'perldoc cobalt2-installer'", | ||
| 47 | "", | ||
| 48 | " --version", | ||
| 49 | " --rcfile=PATH Target rcfile path", | ||
| 50 | " [default: $Rcfile]", | ||
| 51 | "", | ||
| 52 | " --shared-etcdir=DIR Base (distribution) share/etc/ path", | ||
| 53 | ; | ||
| 54 | exit 0 | ||
| 55 | }, | ||
| 56 | ); | ||
| 57 | |||
| 58 | if (defined $SharedEtc) { | ||
| 59 | # user specified a base etcdir | ||
| 60 | $SharedEtc = path($SharedEtc); | ||
| 61 | die "--shared-etcdir specified but no such directory '$SharedEtc'" | ||
| 62 | unless $SharedEtc->exists; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | sub read_format_write { | ||
| 67 | my ($path, $vars) = @_; | ||
| 68 | die "Expected Path::Tiny but got $path" | ||
| 69 | unless is_Path($path); | ||
| 70 | die "Expected HASH of template var replacements but got $vars" | ||
| 71 | unless reftype $vars eq 'HASH'; | ||
| 72 | my $in = $path->slurp_utf8; | ||
| 73 | my $out = rplprintf($in, $vars); | ||
| 74 | $path->spew_utf8($out); | ||
| 75 | $path | ||
| 76 | } | ||
| 77 | |||
| 78 | sub build_dir_skeleton { | ||
| 79 | my ($base, $etc, $var) = @_; | ||
| 80 | die "Expected a base Path object but got '$base'" | ||
| 81 | unless is_Path($base); | ||
| 82 | $etc = path( $base . '/etc' ) unless $etc; | ||
| 83 | $var = path( $base . '/var' ) unless $var; | ||
| 84 | my $dbpath = path( $var . '/db' ); | ||
| 85 | for my $path ($etc, $var, $dbpath) { | ||
| 86 | say " - Creating directory '$path'"; | ||
| 87 | $path->mkpath | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | sub copy_base_confs { | ||
| 92 | my ($target_etc) = @_; | ||
| 93 | die "BUG; expected target etcdir" unless defined $target_etc; | ||
| 94 | $target_etc = path($target_etc) unless is_Path $target_etc; | ||
| 95 | |||
| 96 | $SharedEtc = path(join '/', dist_dir('Bot-Cobalt'), 'etc') | ||
| 97 | unless defined $SharedEtc; | ||
| 98 | |||
| 99 | die "Could not locate ShareDir at '$SharedEtc'" | ||
| 100 | unless $SharedEtc->exists; | ||
| 101 | die "ShareDir at '$SharedEtc' not a directory" | ||
| 102 | unless $SharedEtc->is_dir; | ||
| 103 | |||
| 104 | my $manifest = path(join '/', $SharedEtc, 'Manifest'); | ||
| 105 | die "Could not locate Manifest at '$manifest'" unless $manifest->exists; | ||
| 106 | |||
| 107 | MANIFEST: for my $item ($manifest->lines_utf8) { | ||
| 108 | chomp $item; | ||
| 109 | next MANIFEST unless length $item; | ||
| 110 | my ($relpath, $sum_or_dir) = split / \^ /, $item; | ||
| 111 | if ($sum_or_dir eq 'DIR') { | ||
| 112 | my $newdir = path(join '/', $target_etc, $relpath); | ||
| 113 | say " - Creating directory '$newdir'"; | ||
| 114 | die "Path '$newdir' already exists and is not a directory\n" | ||
| 115 | if $newdir->exists and not $newdir->is_dir; | ||
| 116 | $newdir->mkpath | ||
| 117 | } else { | ||
| 118 | my $oldfile = path(join '/', $SharedEtc, $relpath); | ||
| 119 | my $newfile = path(join '/', $target_etc, $relpath); | ||
| 120 | say " - Copying '$relpath' -> '$newfile'"; | ||
| 121 | die "Could not copy '$relpath'; destination '$newfile' already exists\n" | ||
| 122 | if $newfile->exists; | ||
| 123 | $newfile->spew_utf8( $oldfile->slurp_utf8 ); | ||
| 124 | my $newsum = md5_hex $newfile->slurp_raw; | ||
| 125 | warn "WARNING; checksum mismatch after copy: '$relpath'\n" | ||
| 126 | unless $sum_or_dir eq $newsum; | ||
| 127 | } | ||
| 128 | } # MANIFEST | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub non_interactive { | ||
| 133 | die "Non-interactive mode doesn't exist yet :(\n" | ||
| 134 | # FIXME set up some default var replacements | ||
| 135 | # FIXME env or command line opts for rc paths, basics? | ||
| 136 | # FIXME call rc_write, build_dir_skeleton, copy_base_confs, | ||
| 137 | # read_format_write, chmod auth.conf | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | sub interactive_intro { | ||
| 142 | say $_ for | ||
| 143 | "This is the Bot::Cobalt install helper.", | ||
| 144 | "This script will create a cobalt2rc file specifying the bot's paths.", | ||
| 145 | "It will also help initialize some starter configuration files.", | ||
| 146 | "", | ||
| 147 | "Press ENTER to continue, Ctrl+C to quit." ; | ||
| 148 | |||
| 149 | readline STDIN; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub interactive_rcfile { | ||
| 153 | say $_ for | ||
| 154 | "The current rcfile location is:", | ||
| 155 | "[$Rcfile]" ; | ||
| 156 | $Rcfile = ask_question( prompt => 'Path to rcfile' ) if ask_yesno( | ||
| 157 | prompt => 'Enter a different rcfile location?', | ||
| 158 | default => 'n', | ||
| 159 | ); | ||
| 160 | $Rcfile = path($Rcfile); | ||
| 161 | |||
| 162 | say "Installing rcfile to $Rcfile"; | ||
| 163 | |||
| 164 | say $_ for | ||
| 165 | "Bot::Cobalt needs a place to store config and variable data.", | ||
| 166 | "This can be any path writable by the user running the bot.", | ||
| 167 | "(If it doesn't exist, we will attempt to create it.)" ; | ||
| 168 | my $default_basedir = $ENV{HOME} ? $ENV{HOME} . '/cobalt2' : './cobalt2' ; | ||
| 169 | my $basedir_ans = ask_question( | ||
| 170 | prompt => 'Base directory', | ||
| 171 | default => $default_basedir, | ||
| 172 | ); | ||
| 173 | |||
| 174 | RCFILE: { | ||
| 175 | if ($Rcfile->exists) { | ||
| 176 | warn "WARNING; rcfile exists at '$Rcfile'\n"; | ||
| 177 | my $overwrite = ask_yesno( | ||
| 178 | prompt => 'Overwriting existing rcfile?', default => 'n' | ||
| 179 | ); | ||
| 180 | unless ($overwrite) { | ||
| 181 | say "Skipping rcfile write; attempting to use existing rcfile."; | ||
| 182 | last RCFILE | ||
| 183 | } | ||
| 184 | } | ||
| 185 | rc_write($Rcfile, $basedir_ans); | ||
| 186 | } | ||
| 187 | |||
| 188 | # read back new or old rcfile, build_dir_skeleton + copy_base_confs | ||
| 189 | my ($new_base, $new_etc, $new_var) = map {; path($_) } rc_read($Rcfile); | ||
| 190 | build_dir_skeleton($new_base, $new_etc, $new_var); | ||
| 191 | copy_base_confs($new_etc); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub interactive_cobaltcf { | ||
| 195 | my ($conf_repl) = @_; | ||
| 196 | say " - 'cobalt.conf'"; | ||
| 197 | my $cfvars = $conf_repl->get('cobalt'); | ||
| 198 | |||
| 199 | # CFG_BOT_NICK | ||
| 200 | $cfvars->set( CFG_BOT_NICK => | ||
| 201 | ask_question( prompt => 'Nickname', default => 'cobalt2' ) | ||
| 202 | ); | ||
| 203 | |||
| 204 | # CFG_BOT_USERNAME | ||
| 205 | $cfvars->set( CFG_BOT_USERNAME => | ||
| 206 | ask_question( prompt => 'Username', default => 'cobalt' ) | ||
| 207 | ); | ||
| 208 | |||
| 209 | # CFG_BOT_REALNAME | ||
| 210 | $cfvars->set( CFG_BOT_REALNAME => | ||
| 211 | ask_question( prompt => 'Realname (GECOS)', default => 'Bot::Cobalt' ) | ||
| 212 | ); | ||
| 213 | |||
| 214 | # CFG_CMD_CHAR | ||
| 215 | $cfvars->set( CFG_CMD_CHAR => | ||
| 216 | ask_question( prompt => 'Command prefix character', default => '!' ) | ||
| 217 | ); | ||
| 218 | |||
| 219 | # CFG_SERVER_ADDR | ||
| 220 | $cfvars->set( CFG_SERVER_ADDR => | ||
| 221 | ask_question( prompt => 'Server address', default => 'irc.cobaltirc.org' ) | ||
| 222 | ); | ||
| 223 | |||
| 224 | # CFG_USE_SSL | ||
| 225 | say "If you choose to use SSL, POE::Component::SSLify must be installed."; | ||
| 226 | my $maybe_ssl = ask_yesno( | ||
| 227 | prompt => 'Use SSL for this server?', default => 'n' | ||
| 228 | ); | ||
| 229 | if ($maybe_ssl) { | ||
| 230 | local $@; | ||
| 231 | eval {; require POE::Component::SSLify }; | ||
| 232 | if (my $err = $@) { | ||
| 233 | say $_ for | ||
| 234 | "! POE::Component::SSLify could not be loaded: $err", | ||
| 235 | "! SSL may fail!"; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | $cfvars->set( CFG_USE_SSL => $maybe_ssl ); | ||
| 239 | |||
| 240 | # CFG_SERVER_PORT | ||
| 241 | $cfvars->set( CFG_SERVER_PORT => | ||
| 242 | ask_question( | ||
| 243 | prompt => 'Server port', | ||
| 244 | default => ( $maybe_ssl ? 6697 : 6667 ), | ||
| 245 | ) | ||
| 246 | ); | ||
| 247 | } | ||
| 248 | |||
| 249 | sub interactive_channelscf { | ||
| 250 | my ($conf_repl) = @_; | ||
| 251 | say " - 'channels.conf'"; | ||
| 252 | my $cfvars = $conf_repl->get('channels'); | ||
| 253 | # FIXME audit this situation | ||
| 254 | # ability to add more users? | ||
| 255 | # access to other config opts | ||
| 256 | $cfvars->set( CHAN => | ||
| 257 | ask_question( prompt => 'Default channel', default => '#eris' ) | ||
| 258 | ); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub interactive_authcf { | ||
| 262 | my ($conf_repl) = @_; | ||
| 263 | say " - 'auth.conf'"; | ||
| 264 | my $cfvars = $conf_repl->get('auth'); | ||
| 265 | |||
| 266 | # AUTH_USER | ||
| 267 | say $_ for | ||
| 268 | "You will want at least one SuperUser to control the bot.", | ||
| 269 | "SuperUsers are hard-coded in 'auth.conf'.", | ||
| 270 | "(Other levels of user can be added online later.)" ; | ||
| 271 | $cfvars->set( AUTH_USER => | ||
| 272 | ask_question( prompt => 'Username for your superuser', default => 'MyUser' ) | ||
| 273 | ); | ||
| 274 | |||
| 275 | # AUTH_PASS | ||
| 276 | my $term = POSIX::Termios->new; | ||
| 277 | $term->getattr(0); | ||
| 278 | $term->setlflag( $term->getlflag & ~POSIX::ECHO ); | ||
| 279 | $term->setattr(0); | ||
| 280 | my $ans = ask_question( | ||
| 281 | prompt => 'Password for this superuser', default => '' | ||
| 282 | ); | ||
| 283 | my $crypted = mkpasswd $ans; | ||
| 284 | $cfvars->set( AUTH_PASS => $crypted ); | ||
| 285 | $term->setlflag( $term->getlflag | POSIX::ECHO ); | ||
| 286 | $term->setattr(0); | ||
| 287 | print "\n"; | ||
| 288 | |||
| 289 | # AUTH_MASK | ||
| 290 | $cfvars->set( AUTH_MASK => | ||
| 291 | ask_question( | ||
| 292 | prompt => 'Hostmask for this superuser', default => '*nobody@example.org' | ||
| 293 | ) | ||
| 294 | ); | ||
| 295 | } | ||
| 296 | |||
| 297 | |||
| 298 | sub interactive { | ||
| 299 | # step through interactive configuration; | ||
| 300 | # $conf_repl is passed to each sub and filled: | ||
| 301 | # hash( | ||
| 302 | # cobalt => hash( | ||
| 303 | # $TEMPLATE_VAR => $ANSWER, | ||
| 304 | # ... | ||
| 305 | # ), | ||
| 306 | # ... | ||
| 307 | # ) | ||
| 308 | my @refs = ( | ||
| 309 | \&interactive_intro, | ||
| 310 | \&interactive_rcfile, # set up rcfile & dir skel, copy template confs | ||
| 311 | \&interactive_cobaltcf, # configure etc/cobalt.conf | ||
| 312 | \&interactive_channelscf, # configure etc/channels.conf | ||
| 313 | \&interactive_authcf, # configure etc/auth.conf | ||
| 314 | ); | ||
| 315 | my $conf_repl = hash( | ||
| 316 | cobalt => hash, | ||
| 317 | channels => hash, | ||
| 318 | auth => hash, | ||
| 319 | ); | ||
| 320 | $_->($conf_repl) for @refs; | ||
| 321 | |||
| 322 | # should have a readable rcfile now, load it to verify and get etcdir, | ||
| 323 | # then read/fill/write templates via read_format_write() | ||
| 324 | my (undef, $etcdir) = rc_read($Rcfile); | ||
| 325 | for my $kv ($conf_repl->kv->all) { | ||
| 326 | my ($path_prefix, $repl_hash) = @$kv; | ||
| 327 | my $actual = path( "${etcdir}/${path_prefix}.conf" ); | ||
| 328 | say " - Writing '$actual'"; | ||
| 329 | read_format_write($actual, $repl_hash); | ||
| 330 | } | ||
| 331 | |||
| 332 | my $authcf_path = path(join '/', $etcdir, 'auth.conf'); | ||
| 333 | say " - Adjusting permissions for 'auth.conf' (0600) ..."; | ||
| 334 | $authcf_path->chmod(0600); | ||
| 335 | } | ||
| 336 | |||
| 337 | |||
| 338 | $NonInteractive ? non_interactive : interactive; | ||
| 339 | |||
| 340 | # FIXME more informative quit msg? paths etc | ||
| 341 | say $_ for | ||
| 342 | " - Completed, exiting", | ||
| 343 | "Done!", | ||
| 344 | "(You should check over the files mentioned above manually to verify", | ||
| 345 | " correctness and tune other options.)", | ||
| 346 | "See 'cobalt2 --help' for help on starting the bot." | ||
| 347 | ; | ||
| 348 | |||
| 349 | =pod | ||
| 350 | |||
| 351 | =head1 NAME | ||
| 352 | |||
| 353 | cobalt2-installer - Initialize a Bot::Cobalt instance | ||
| 354 | |||
| 355 | =head1 SYNOPSIS | ||
| 356 | |||
| 357 | cobalt2-installer [ --rcfile=PATH ] | ||
| 358 | |||
| 359 | =head1 DESCRIPTION | ||
| 360 | |||
| 361 | A simple walk-through installer for fresh L<Bot::Cobalt> instances. | ||
| 362 | |||
| 363 | Interactively asks a few questions to prepare a C<rcfile> and associated | ||
| 364 | L<Bot::Cobalt> configuration files; language sets and essential skeleton | ||
| 365 | directories are also copied to the bot home directory specified during the | ||
| 366 | install process. | ||
| 367 | |||
| 368 | Every L<Bot::Cobalt> instance has its own C<rcfile> and directory; to | ||
| 369 | initialize and run multiple instances, specify a new C<rcfile>: | ||
| 370 | |||
| 371 | # Initialize FredBot: | ||
| 372 | cobalt2-installer --rcfile=${HOME}/cobalts/FredBot.rc | ||
| 373 | # Start the newly-configured instance: | ||
| 374 | cobalt2 --rcfile=${HOME}/cobalts/FredBot.rc | ||
| 375 | |||
| 376 | =head1 AUTHOR | ||
| 377 | |||
| 378 | Jon Portnoy <avenj@cobaltirc.org> | ||
| 379 | |||
| 380 | =cut | ||