#!/usr/bin/perl
# sbot.pl(7): sbot.pl(IRC ScrambleBot)-v[4.0] : v9[v9@fakehalo.org]. (!26139b)

# sbot.pl: base vars. (1)
$version="4.0";				# sbot version value.
$botname="scrambi";			# nickname on irc.
$dport="6667";				# default port if none is provided.
$ircuser="scrambi";			# irc user.
$ident="scrambi";			# if no identd, this is used.
$realname="$botname: ScrambleBot [v$version]";		# name shown in a whois.
$c="#scrambi";				# channel for bot to join.
$ckey="";				# if channel is +k, put key here.
$quitline="sbot: exiting cleanly.";	# quitline used when exiting irc.
$passhd="pass";				# what you message to get kill/add access.
$passwd=rand(1);			# passwd for kill/add/server access, random by default.
$trigger="!scramble";			# start trigger.
$atrigger="!guess";			# guess trigger.
$rtrigger="!reset";			# reset/restart trigger.
$ltrigger="!launch";			# verbose launch time(s) trigger.
$ztrigger="!add";			# add word trigger. (master only)
$strigger="!server";			# server change trigger. (master only)
$dtrigger="!die";			# bot kill trigger. (master only)
$guesses=3;				# number of guesses before ignored.
$flood=2;				# delay in seconds between next read.
$rejoin=10;				# if join fails, rejoin every ? sec(s).
$timeout=30;				# time before resets are allowed in sec(s).
$pause=3;				# pause for showing setup.
$pmt=":::";				# prompt for output to server.
$lpmt="[1\;30mþ[0\;37mþ[1\;37mþ[0m";		# local UI prompt.
$header="[37\;40m[[36\;40msbot[37\;40m]:[0m";	# local UI header.
$dlpmt="þþþ";				# dumb terminal version of lpmt.
$dheader="[sbot]:";			# dumb terminal version of dheader.
$reconnect=1;				# 0/1: rconnect when disconnected?
$public=1;				# 0/1: show onjoin advert?
$rdelay=10;				# attempt rejoin every ?? sec(s).
$ver_reply="mIRC32 v5.9 K.Mardam-Bey";	# version reply. (undef for no reply)
$ver_form="VERSION";			# if ver_reply, version reply header.
$wordfile="wordlist";			# where the word(s) are stored.
$pidfile="sbot.pid";			# where the pid of the bot is stored.
$bgfile="sbot.sh";			# temporary file to launch bot to bg.
# sbot.pl: extended vars. (2)
$OS=$ENV{"OSTYPE"};if(!$OS){$OS="Unknown OS";}
@progname=split(/\//,$0);$progname=$progname[-1]; 
# sbot.pl: plug-ins. (3)
use Socket;
# sbot.pl: sigs. (4)
$SIG{'INT'}=\&cleanexit;
$SIG{'TSTP'}=\&cleanexit;
$SIG{'ALRM'}=\&allowreset;
# sbot.pl: subroutines. (5)
sub sraw{print READ "@_\n";}
sub sraw2c{&sraw("PRIVMSG $c :@_");sleep(1);}
sub sout{if(!$sout){print STDERR "\n$lpmt $header @_";}}
sub south{&sout("@_ ($pause\s)");&pause;&clear;&e0;exit(0);}
sub clear{
 if(!$dumb){
  my($j,$p,$q);@p=split(/:/,$ENV{"PATH"});
  $j=0;while($p[$j]&&!$q){if(-f"$p[$j]/clear"){$q="$p[$j]/clear";}$j++;}
  if($q){system($q);}else{print STDERR "[H[2J";}
 }
}
sub sconnect{
 ($s,$p)=split(/ /,shift);
 if(!$s){&sout("missing parameter(s): \"sconnect <server> [port]\"");return 1;}
 if($p<1){$p=$dport;if(!$p){$p=6667;}}
 &sout("! attempting to launch bot: ^C or ^Z to shutdown. !");
 my($ia,$pa,$proto,$buf,$read);&sout("connecting to: $s($p).");
 $ia=inet_aton($s);$pa=sockaddr_in($p,$ia);
 $proto=getprotobyname('tcp');$buf=select READ;$|=1;select $buf;
 socket(READ,PF_INET,SOCK_STREAM,$proto)||&south("error creating socket: $s($p) -> $!");
 connect(READ,$pa)||&south("error connecting to: $s($p) -> $!");
 $slaunchtime=localtime;&sout("connected to: $s($p).");&sout("sending login information.");
 &sraw("USER $ircuser $ident $s :$realname\nNICK $botname");
 while(defined($read=<READ>)){&ihandler("$read");}
 &sout("connection closed\, eof.");&nostats;
 if($reconnect){
  &sout("reconnect delay: $rdelay.");sleep($rdelay);
  &sout("reconnecting to $s($p).");&sconnect("$s $p");
 }
}
sub ihandler{
 @args=split(/ /,shift);my $n=$args[1];
 if($sinfo){print "$lpmt [0\;32m$s[1\;32m![0m$header @args";}
 if($args[0] eq "PING"){
  &sout("ping from $s\, ponging back.");chomp $args[1];
  &sraw("PONG $args[1]",substr($args[1],1,length($args[1])));
 }
 elsif($args[1] eq "KICK"&&$args[2] eq $c&&$args[3] eq $botname){
  &stats;my($n,$h)=split(/!/,substr($args[0],1,length($args[0])-1));  
  &sout("kicked from $c by $n($h)\, attempting to rejoin.");
  if($game){&sout("ending current game status.");$game=0;}
  &sraw("JOIN $c :$ckey");
 }
 elsif($n eq "001"){&stats;&sout("logged on to $s($p)\, joining $c.");&sraw("JOIN $c :$ckey");}
 elsif($n==366){
  if($args[3] eq $c){
   &cstats;if($public){
    &sraw2c("$pmt ScrambleBot($progname)+perl: v$version. [$s($p)] -",&words,"word(s).");
    &sraw2c("$pmt (user) commands: $trigger\, $atrigger\, $rtrigger\, $ltrigger.");
    &sraw2c("$pmt (mast) commands: $ztrigger\, $strigger\, $dtrigger.");
    &sout("joined $c successfully. (announced)");
   }
   else{&sout("joined $c successfully. (quiet)");}
  }
  else{
   &sraw("PRIVMSG $args[3] :$pmt Woops! I don\'t belong in this channel! ($args[3]!=$c)\nPART $args[3]");
   &sout("joined $args[3] successfully. (not supported\, parting)");
  }
 }
 elsif($n==431||$n==432){&sout("\" $botname \" is invalid or non-existant\, changing to \"",&newbot("sb"),"\".");&sraw("NICK $botname");}
 elsif($n==433){&sout("\" $botname \" is in use, changing to \"",&newbot("$botname"),"\".");&sraw("NICK $botname");}
 elsif($n==461){&sout("not enough user info provided\, attempting to connect with default info.");&sraw("USER sbot sbot $s :$progname: default.");}
 elsif($args[3] eq $c){
  if($n==403){&sout("cannot join\, $c is invalid. (*)");&cleanexit("channel_invalid");}
  elsif($n==471){&sout("cannot join\, $c limit reached. (+l)");&retry("channel_limit");}
  elsif($n==472){&sout("cannot join\, $c unknown mode. (+?)");&retry("channel_unknown_mode");}
  elsif($n==473){&sout("cannot join\, $c is invite only. (+i)");&retry("channel_invite_only");}
  elsif($n==474){&sout("cannot join\, banned from $c. (+b)");&retry("channel_ban");}
  elsif($n==475){&sout("cannot join\, $c needs valid key. (+k)");&retry("channel_key");}
 }
 elsif($args[1] eq "PRIVMSG"&&substr($args[3],0,1) eq ":"&&ord(substr($args[3],1,1))==1&&lc(substr($args[3],2,7)) eq "version"&&ord(substr($args[3],9,1))==1){
  my($n,$h)=split(/!/,substr($args[0],1,length($args[0])-1));  
  if($ver_reply){
   if(substr($args[2],0,1) eq "#"){&sout("ctcp version request from $n($h)\, sending reply. ($args[2])");}
   else{&sout("ctcp version request from $n($h)\, sending reply.");}
   my $a=chr(1);&sraw("NOTICE $n :$a$ver_form $ver_reply$a");
   sleep($flood);
  }
  else{
   if(substr($args[2],0,1) eq "#"){&sout("ctcp version request from $n($h)\, halted reply. ($args[2])");}
   else{&sout("ctcp version request from $n($h)\, halted reply.");}
  }
 }
 elsif($args[1] eq "PRIVMSG"&&substr($args[2],0,1) ne "#"&&lc(substr($args[3],1,length($passhd))) eq lc($passhd)&&substr($args[4],0,length($passwd)) eq $passwd){
  ($mn,$mh)=split(/!/,substr($args[0],1,length($args[0])-1));
  &sraw("NOTICE $mn :valid passwd\, accepting add($ztrigger), server($strigger) and die($dtrigger) requests from $mn($mh).");
  &sraw2c("$pmt master set to: $mn($mh).");
  &sout("valid passwd request\, allowing master requests from: $mn($mh).");
 }
 elsif($args[1] eq "PRIVMSG"&&$args[2] eq $c){
  my($n,$h)=split(/!/,substr($args[0],1,length($args[0])-1));  
  if($game){
   if(substr($args[3],0,length($trigger)+1) eq ":$trigger"){
    if($reset){&sraw2c("$n\, game is already in progress. (type $rtrigger to reset)");}
    else{&sraw2c("$n\, game is already in progress. (wait $timeout to reset)");}
   }
   elsif(substr($args[3],0,length($rtrigger)+1) eq ":$rtrigger"){
    if($reset){
     &sraw2c("$pmt timeout($timeout) occured\, allowing reset. -- it was \"$word[0]\". (type $trigger to play again)");
     $game=0;&sout("$n($h) reset the game.");
    }
    else{
     &sraw2c("$pmt timeout($timeout) has yet to occur\, wait.");
     &sout("$n($h) tried to reset the game\, but timeout($timeout) has not occured.");
    }
   }
   elsif(substr($args[3],0,length($atrigger)+1) eq ":$atrigger"){
    if(!$args[4]){&sraw2c("$pmt syntax: $atrigger <answer>");}
    else{
     my($r,$out);$r=4;while($args[$r]){$out.=" $args[$r]";$r++;}
     $out=substr($out,1,length($word[0]));
     if($out eq $word[0]){
      my $num=@{$userbase{$h}}[0];
      if($num<$guesses){
       &sraw2c("$n is correct\, the answer was \"$word[0]\"! (type $trigger to play again)");
       &sout("$n($h) guessed correct: $out.");
       if($lwin eq "$n($h)"){if(!$wi){$wi=0;}$wi++;}
       else{undef($wi);}
       if($wi>$swi&&$swin){$swin=$lwin;$swi=$wi;}
       $lwin="$n($h)";if(!$swin){$swin=$lwin;}$game=0;
      }
     }
     else{
      my $num=@{$userbase{$h}}[0];
      if(!$num){$num=0;}
      if($num<$guesses){
       $num++;$userbase{$h}=[$num];
       &sraw2c("$n\, wrong. (guess @{$userbase{$h}} of $guesses)");
       if($num>=$guesses){&sraw2c("$pmt ignoring $n($h) till next game.");}
       chomp $out;&sout("$n($h) guessed wrong: $out.");
      }
     }
    }
   }
   elsif(substr($args[3],0,length($ltrigger)+1) eq ":$ltrigger"){
    &sraw2c("$pmt wait till game ends for launch information.");
    &sout("user attempted to obtain launch information. (aborted)");
   }
   elsif(substr($args[3],0,length($dtrigger)+1) eq ":$dtrigger"){
    &sraw2c("$pmt wait till game ends to kill the bot.");
    &sout("$n($h) attempted to kill the bot\, in game. (aborted)");
   }
   elsif(substr($args[3],0,length($ztrigger)+1) eq ":$ztrigger"){
    &sraw2c("$pmt wait till game ends to add information.");
    &sout("$n($h) attempted to kill add information\, in game. (aborted)");
   }
   elsif(substr($args[3],0,length($strigger)+1) eq ":$strigger"){
    &sraw2c("$pmt wait till game ends to connect to a new server.");
    &sout("$n($h) attempted to connect to new server\, in game. (aborted)");
   }
  }
  else{
   if(substr($args[3],0,length($trigger)+1) eq ":$trigger"){
    &sraw2c("$pmt $n($h) started new scramble game. [v$version] -",&words,"word(s).");
    if($lwin){
     if($wi){&sraw2c("$pmt $lwin was the previous winner($wi).");}
     else{&sraw2c("$pmt $lwin was the previous winner.");}
    }
    if($swin&&$swi){&sraw2c("$pmt $swin has the longest streak($swi).");}
    &sraw2c("$pmt $atrigger to guess - $rtrigger to reset.");
    undef(*userbase);undef(*word);undef(*reset);@word=split(/:/,&getword);
    if(!$word[1]){$word[0]=substr($word[0],0,length($word[0])-1);$word[1]="no help provided.";}
    $sword=&sword($word[0]);&sraw2c("$pmt unscramble: \"$sword\" -- $word[1]");
    alarm($timeout);$game=1;$glaunchtime=localtime;
    &sout("game started by $n($h)\, the answer is: \"$word[0]\".");
   }
   elsif(substr($args[3],0,length($ltrigger)+1) eq ":$ltrigger"){
    if(!$launchtime&&!$slaunchtime&&!$glaunchtime){&sraw2c("$pmt no launch information.");}
    else{
     if($launchtime){&sraw2c("$pmt launch time: $launchtime.");}
     if($slaunchtime){&sraw2c("$pmt server($s) launch time: $slaunchtime.");}
     if($glaunchtime){&sraw2c("$pmt last game launch time: $glaunchtime.");}
    }
    &sout("user obtained launch information.");
   }
   elsif(substr($args[3],0,length($dtrigger)+1) eq ":$dtrigger"){
    if($n eq $mn&&$h eq $mh){
     &sraw2c("$pmt access granted\, ending session."); 
     &sout("ending session due to request: $n($h).");
     &cleanexit("remote kill by: $n($h)");
    } 
    else{
     if($mn&&$mh){&sraw2c("$pmt not the master[$mn($mh)]\, try /msg $botname $passhd <passwd>.");}
     else{&sraw2c("$pmt no master\, try /msg $botname $passhd <passwd>.");}
     &sout("$n($h) attempted to kill the bot\, passwd needed. (aborted)");
    }
   }
   elsif(substr($args[3],0,length($strigger)+1) eq ":$strigger"){
    if($n eq $mn&&$h eq $mh){
     if(!$args[4]){&sraw2c("$pmt syntax: $strigger <server> [port]");}
     else{
      my($r,$out,$twordfile,@o);$r=4;while($args[$r]){$out.=" $args[$r]";$r++;}
      @o=split(chr(13),substr($out,1,length($out)-1));
      chomp $o[0];&sraw2c("$pmt trying new server(port): \"$o[0]\".");
      &sout("$n($h) requested connect a new server(port)\, delay($rdelay): \"$o[0]\".");
      &sraw("QUIT :$quitline [$o[0]]");
      sleep($rdelay);&nostats;&sconnect("$o[0]");
     }
    }
    else{
     if($mn&&$mh){&sraw2c("$pmt not the master[$mn($mh)]\, try /msg $botname $passhd <passwd>.");}
     else{&sraw2c("$pmt no master\, try /msg $botname $passhd <passwd>.");}
     &sout("$n($h) attempted to connect to a new server\, passwd needed. (aborted)");
    }
   }
   elsif(substr($args[3],0,length($ztrigger)+1) eq ":$ztrigger"){
    if($n eq $mn&&$h eq $mh){
     if(!$args[4]){&sraw2c("$pmt syntax: $ztrigger <word[:description]>");}
     else{
      my($r,$out,$twordfile,@o);$r=4;while($args[$r]){$out.=" $args[$r]";$r++;}
      @o=split(chr(13),substr($out,1,length($out)-1));chomp $o[0];
      &sraw2c("$pmt adding information to wordlist($wordfile): \"$o[0]\".");
      &sout("adding information from $n($h): \"$o[0]\".");
      open(ADD,">>$wordfile")||&sout("error opening wordlist: $wordfile\, but continued.");
      print ADD "$o[0]\n";close(ADD);
     }
    } 
    else{
     if($mn&&$mh){&sraw2c("$pmt not the master[$mn($mh)]\, try /msg $botname $passhd <passwd>.");}
     else{&sraw2c("$pmt no master\, try /msg $botname $passhd <passwd>.");}
     &sout("$n($h) attempted to add information\, passwd needed. (aborted)");
    }
   }
  }
  sleep($flood);
 }
}
sub echk{
 my($j,$p,$q);@p=split(/:/,$ENV{"PATH"});
 $j=0;while($p[$j]&&!$q){if(-f"$p[$j]/stty"){$q="$p[$j]/stty";}$j++;}
 return $q;
}
sub e0{my $e=&echk;if(-f$e){system("$e echo");}}
sub e1{my $e=&echk;if(-f$e){system("$e -echo");}}
sub newbot{
 my($bn,$i,$j);$bn=substr(shift,0,8);$i=0;
 while(length($bn)<=8){$j=int(rand(9));$bn="$bn$j";$i++;}
 $botname=$bn;
}
sub getword{
 open(WLREAD,$wordfile)||&south("error opening wordlist: $wordfile.");
 my @read=<WLREAD>;close(WLREAD);
 my $i=0;while($read[$i]){$i++;}
 return @read[int(rand($i))-1];
}
sub retry{
 if(@_){&sout("unable to join $c: $_[0]\, holding($rejoin).");}
 else{&sout("unable to join $c\, wait($rejoin).");}
 sleep($rejoin);&sraw("JOIN $c :$ckey");
}
sub sword{
 if(length($_[0])<2){return $_[0];}my($w,$o,$i,@a);
 while($o eq $w){
  $w=$_[0];undef($o);$i=0;while(length($_[0])>$i){$a[$i]=substr($w,$i,1);$i++;}$i=0;
  while(length($_[0])>$i){if(int(rand(2))){$o="$a[$i]$o";}else{$o.=$a[$i];}$i++;}
 }
 return $o;
}
sub words{
 open(TOTAL,$wordfile)||&sout("error opening wordlist: $wordfile\, but continued.");
 my(@tr,$r);@tr=<TOTAL>;$r=@tr;close(TOTAL);if($r){return $r;}else{return 0;}
}
sub allowreset{
 if($game){
  $reset=1;&sraw2c("$pmt allowing resets\, timeout($timeout) has occured. (HINT: begins with \'" . substr($word[0],0,1) . "\')");
  &sout("allowing resets\, timeout($timeout) has occured.");
 }
}
sub base{if(!$dumb){my $ww=&words;print STDERR "[2\;21r[2\;1H[K[3\;1H[K[4\;1H[K[5\;1H[K[6\;1H[K[7\;1H[K[8\;1H[K[9\;1H[K[10\;1H[K[11\;1H[K[12\;1H[K[13\;1H[K[14\;1H[K[15\;1H[K[16\;1H[K[17\;1H[K[18\;1H[K[19\;1H[K[20\;1H[K[21\;1H[K[2\;1H[47\;1H[K[0\;1H[K[1\;1H[42\;1H[K[22\;1H(U[1\;1H[0\;0m[37\;44m                                                                                [0\;0HScrambleBot[36\;44m-[37\;44mVersion$version[36\;44m([37\;44msbot[36\;44m->[37\;44mperl[36\;44m)[37\;44m: console -> streaming information. [36\;44m([37\;44m$ww\w[36\;44m)[37\;44m [24\;1H[37\;44m                                                                                [23\;1H                                                                                [22\;1H[37\;44m                                                                                [1\;1H[2H[1\;1H[2\;1H[21\;0H[0\;0m";}}
sub stats{if(!$dumb){print STDERR "[24\;1H[37\;44m                                                                                [23\;1H                                                                                [22\;1H[37\;44m                                                                                [22\;1H [36\;44m[[37\;44m$botname[36\;44m([37\;44m$s[36\;44m[[37\;44m$p[36\;44m])][37\;44m on: none[36\;44m()[37\;44m þ [36\;44m[[37\;44m^C/^Z[36\;44m][37\;44m[23\;1H [36\;44m[[37\;44mmain triggers[36\;44m([37\;44m$trigger $atrigger $rtrigger $ltrigger[36\;44m)][37\;44m[24\;1H [36\;44m[[37\;44mflood[36\;44m([37\;44m$flood[36\;44m[36\;44m)][37\;44m[37\;44m [36\;44m[[37\;44mreset[36\;44m([37\;44m$timeout[36\;44m)][37\;44m [36\;44m[[37\;44mrejoin[36\;44m([37\;44m$rejoin[36\;44m)][37\;44m [36\;44m[[37\;44mguesses[36\;44m([37\;44m$guesses[36\;44m)][37\;44m [36\;44m[[37\;44mwait[36\;44m([37\;44m$pause[36\;44m)][37\;44m [36\;44m[[37\;44mpid[36\;44m([37\;44m$$[36\;44m)][37\;44m[1\;1H[2H[1\;1H[2\;1H[21\;0H[0\;0m";}}
sub cstats{if(!$dumb){print STDERR "[24\;1H[37\;44m                                                                                [23\;1H                                                                                [22\;1H[37\;44m                                                                                [22\;1H [36\;44m[[37\;44m$botname[36\;44m([37\;44m$s[36\;44m[[37\;44m$p[36\;44m])][37\;44m on: $c[36\;44m([37\;44m$ckey[36\;44m)[37\;44m þ [36\;44m[[37\;44m^C/^Z[36\;44m][37\;44m[23\;1H [36\;44m[[37\;44mmain triggers[36\;44m([37\;44m$trigger $atrigger $rtrigger $ltrigger[36\;44m)][37\;44m[24\;1H [36\;44m[[37\;44mflood[36\;44m([37\;44m$flood[36\;44m[36\;44m)][37\;44m[37\;44m [36\;44m[[37\;44mreset[36\;44m([37\;44m$timeout[36\;44m)][37\;44m [36\;44m[[37\;44mrejoin[36\;44m([37\;44m$rejoin[36\;44m)][37\;44m [36\;44m[[37\;44mguesses[36\;44m([37\;44m$guesses[36\;44m)][37\;44m [36\;44m[[37\;44mwait[36\;44m([37\;44m$pause[36\;44m)][37\;44m [36\;44m[[37\;44mpid[36\;44m([37\;44m$$[36\;44m)][37\;44m[1\;1H[2H[1\;1H[2\;1H[21\;0H[0\;0m";}}
sub nostats{if(!$dumb){print STDERR "[24\;1H[37\;44m                                                                                [23\;1H                                                                                [22\;1H[37\;44m                                                                                [22\;1H [36\;44m[[37\;44m<Unknown>[36\;44m([37\;44m<Unknown>[36\;44m[[37\;44m<Unknown>[36\;44m])][37\;44m on: none[36\;44m()[37\;44m þ [36\;44m[[37\;44m^C/^Z[36\;44m][37\;44m[1\;1H[2H[1\;1H[2\;1H[21\;0H[0\;0m";}}
sub pause{if($pause>0){print STDERR "\n$lpmt $header pause routine($pause) -> ["if(!$sout);my $d=$pause;while($d){print STDERR "."if(!$sout);sleep(1);$d--;}print STDERR "]"if(!$sout);}}
sub cleanexit{
 if($TSTP||$INT){&clear;&e0;exit(0);}
 elsif($_[0] eq "TSTP"&&!$TSTP){ 
  $TSTP++;
  if(<READ>){&sout("shutting down socket(direct exit), forced shutdown.");}
  else{&sout("no socket to shutdown\, forced shutdown.");}
  &sout("exiting cleanly. ($pause\s)");&pause;&clear;&e0;exit(0);
 }
 elsif($_[0] eq "INT"&&!$INT){
  $INT++;&sout("attempting to shutdown socket.");
  &sraw("QUIT :$quitline");&sout("socket finished.");
  if(<READ>){&sout("shutting down socket\, general shutdown.");shutdown(READ,2)||&south("error shutting down socket.");}
  else{&sout("no socket to shutdown\, general shutdown.");}
  &sout("exiting cleanly. ($pause\s)");&pause;&clear;&e0;exit(0);
 }
 else{
  if(@_){&sout("internal shutdown due to: $_[0].");}
  else{&sout("internal shutdown due to unknown event(s).");}
  &sraw("QUIT :$quitline");&sout("socket finished.");
  if(<READ>){&sout("shutting down socket\, internal shutdown.");shutdown(READ,2)||&south("error shutting down socket.");}
  else{&sout("no socket to shutdown\, internal shutdown.");}
  &sout("exiting cleanly. ($pause\s)");&pause;&clear;&e0;exit(0);
 }
}
# sbot.pl: init. (6)
&e1;print "\n[$$] ScrambleBot(v$version). -- by: _v9[v9\@fakehalo.org].\n[$$] Irc gamebot(scramble) server. -- coded in perl5+.\n[$$] For information on parameters use \"--help\".\n\n";
if(!-f$wordfile){print "-- cannot connect without a wordlist: \"$wordfile\" not found. --\n";&e0;exit(0);}
if(!@ARGV){print "syntax: $progname <server [port]> [--showinfo] [--skipsetup] [--skipoutput] [--hs nick ident host value] [--dumb] [--background] [--help]\nexiting cleanly.\n";&e0;exit(0);}
else{
 $i=0;while($ARGV[$i]){
  if($ARGV[$i] eq "--help"){&clear;print "-- syntax: $progname <server [port]> [--showinfo] [--skipsetup] [--skipoutput] [--hs nick ident host value] [--dumb] [--background] [--help] --\n\n-- \"--showinfo\" is to show all output from the server. --\n-- \"--skipsetup\" is to skip the verbose setup on startup. --\n-- \"--skipoutput\" is to skip verbose messages\, nulls almost everything. --\n-- \"--hs\" is to make a default high score. (ex. [--hs ted ted sbot.com 25]) --\n-- \"--dumb\" is to revert to raw text output mode. (dumb terminal) --\n-- \"--background\" is to launch the bot into the background. (last parameter) --\n-- \"--help\" what you\'re viewing now. --\n-- \"^C\" to shutdown bot cleanly. - \"^Z\" to force shutdown. --\n-- wordlist format is \"word[:description]\" in your wordfile. --\nexiting cleanly.\n";&e0;exit(0);}
  elsif($ARGV[$i] eq "--showinfo"&&!$sinfo){print "-- switch enabled($ARGV[$i]): showing server output. --\n";$sinfo=1;}
  elsif($ARGV[$i] eq "--skipsetup"&&!$ssetup){print "-- switch enabled($ARGV[$i]): skipping setup output. --\n";$ssetup=1;}
  elsif($ARGV[$i] eq "--skipoutput"&&!$sout){print "-- switch enabled($ARGV[$i]): skipping almost all output. --\n";$sout=1;}
  elsif($ARGV[$i] eq "--hs"&&!$shs){
   if(!$ARGV[$i+4]){print "-- switch failed($ARGV[$i]): missing parameters. [--hs name ident host value] --\n";}
   elsif($ARGV[$i+4]<1){print "-- switch failed($ARGV[$i]): invalid value\, must be numeric. [--hs name ident host value] --\n";}
   else{
    $swin="$ARGV[$i+1]($ARGV[$i+2]\@$ARGV[$i+3])";$swi=$ARGV[$i+4];
    print "-- switch enabled($ARGV[$i]): set highscore to $ARGV[$i+1]($swi). --\n";
    $i=$i+4;$shs=1;
   }
  }
  elsif($ARGV[$i] eq "--dumb"&&!$dumb){print "-- switch enabled($ARGV[$i]): dumb terminal mode. --\n";$lpmt=$dlpmt;$header=$dheader;$dumb=1;}
  elsif($ARGV[$i] eq "--background"){
   if($i==0){print "-- switch failed($ARGV[$i]): not enough information to background. --\n";&e0;exit(0);}
   print "-- switch enabled($ARGV[$i]): attempting to background the bot. --\n";
   my($repargs,$nohup,$j,@p);$j=0;while($j<$i){$repargs="$repargs $ARGV[$j]";$j++;}
   @p=split(/:/,$ENV{"PATH"});$j=0;while($p[$j]&&!$nohup){if(-f"$p[$j]/nohup"){$nohup="$p[$j]/nohup";print "-- found nohup($nohup)\, adding to background command line. --\n";}$j++;}
   if(!$nohup){print "-- did not find nohup, proceeding anyways. --\n";}
   else{$nohup="$nohup ";}open(BG,">$bgfile")||&south("error opening background exec file: $bgfile.");
   print BG "echo \"-- entered launch script($bgfile)\, proceeding. --\"\n$nohup\perl $0$repargs 1>/dev/null 2>&1 &\necho \"-- completed background process. --\"\n";
   close(BG);print "-- exiting current and attempting to launch background process. --\n";
   system("/bin/sh $bgfile");unlink($bgfile)||&south("error removing background file: $bgfile.");&e0;exit(0);
  }
  $i++;
 }
}
&clear;&base;&nostats;
if(!$dumb){print "\n[0m[1\;30m [0\;32m [1\;32m°[0\;36m ° [1\;30mÜ[0\;36mÜ[46\;1\;30mß[40\;36m°[0\;36mß[1\;34mÜ[0\;36mÜ[37m [1\;30mþ\n[0\;32m°[1\;32m°²[0\;36m°²ÜÜÜ[46\;1\;36m°[40m²[0\;36mßß[1\;30m Ü\n[0\;32m±[1\;32m±[46mß[0\;36m²ÜÜ[46\;1\;36m°°²[40mß[0\;36mß [1\;30mß[0m  [1\;37mScrambleBot[0m:[1\;37m $progname[30m([0m$version[1\;30m)[0m:[1\;37m perl [0m--[1\;37m [30m([0m$OS[1\;30m)\nß[42mÜ[0\;36mÜ[46\;1\;36mÜÜÛ²[40mÜ[47m²[46mÝ[0\;36mÜ [47\;30m²[40\;37m°[1\;30m°\n..[0\;36m [1\;30mß[36mß[0\;36mß [1\;37mß[36mß[0\;36mß [1\;30m °°[K\n\n";}
if($pause>0){&sout("holding($pause).");&pause;}
if(!$ssetup){
 if($botname&&$ircuser&&$ident){&sout("bot info              : $botname:$ircuser:$ident.");}
 if($realname){&sout("real name             : \"$realname\".");}
 if($quitline){&sout("quit line             : \"$quitline\".");}
 if($c){
  if($ckey){&sout("channel               : $c($ckey).");}
  else{&sout("channel               : $c.");}
 }
 if($port){&sout("default port          : $port.");}
 if($trigger){&sout("start trigger         : $trigger.");}
 if($atrigger){&sout("guess trigger         : $atrigger.");}
 if($rtrigger){&sout("reset trigger         : $rtrigger.");}
 if($ltrigger){&sout("ltime trigger         : $ltrigger.");}
 if($ztrigger){&sout("add trigger           : $ztrigger.");}
 if($strigger){&sout("server trigger        : $strigger.");}
 if($dtrigger){&sout("die trigger           : $dtrigger.");}
 if($guesses){&sout("guesses allowed       : $guesses.");}
 if($flood){&sout("flood delay           : $flood.");}
 if($rejoin){&sout("rejoin/retry delay    : $rejoin.");}
 if($timeout){&sout("reset timeout         : $timeout.");}
 if($public){&sout("onjoin announce is    : enabled.");}
 else{&sout("onjoin announce is    : disabled.");}
 if($reconnect){&sout("serv reconnect is     : enabled.");}
 else{&sout("serv reconnect is     : disabled.");}
 if($ver_reply){&sout("version reply         : \"$ver_reply\".");}
 else{&sout("version reply         : disabled.");}
 if($pause>0){&sout("holding($pause).");&pause;}
}
if(-f$pidfile){
 &sout("-- file \"$pidfile\" exists\, attempting to kill pid(s). --");
 open(PID,$pidfile)||&south("error opening pid file: $pidfile.");
 my @read=<PID>;close(PID);my $i=0;
 while($read[$i]){
  chomp $read[$i];if($read[$i]==$$){&sout("** skipping kill due to active pid($$): $read[$i]. **");}
  else{
   &sout("** attempting to kill pid: $read[$i]. **");my $k=kill(9,$read[$i]);
   if($k){&sout("** pid($read[$i]) killed successfully. **");}
   else{&sout("** pid($read[$i]) kill failed. **");}
  }
  $i++;
 }
 &sout("-- file \"$pidfile\" exists\, attempting remove pidfile. --");
 my $success=unlink($pidfile);
 if($success){&sout("** removed pid file: $pidfile. **");}
 else{&south("** error removing pid file: $pidfile. **");}
}
&sout("-- logging pid value($$) to: $pidfile. --");
open(PID,">>$pidfile")||&south("error opening pid file: $pidfile.");
print PID "$$\n";close(PID);$launchtime=localtime;
&sconnect("@ARGV");&clear;&e0;exit(0);
# sbot.pl: eof. (7)
