# quotes.pl: quote relaying, Version_0.1:[final] -- 4kiodine. (!4050 bytes).
# quotes.pl: by: _v9[v9@ice.org] -- for use in X-Chat(built on 1.2.1).
# quotes.pl: type "/load /pathto/quotes.pl" in X-Chat or put it in ~/.xchat to auto-load on start.

# quotes.pl: base vars. (1)
$hd=$ENV{"HOME"};
$pmt="\02***\02";
$lsoffset=4;
$oschar="0";
$qprefix="\02quote\02: ";
$qsendto=">>$hd/.xchat/qlist";
$qwriteto=">$hd/.xchat/qlist";
$qgetfrom="$hd/.xchat/qlist";
# quotes.pl: subroutines. (2)
sub out{IRC::print "@_\n";}
sub qout{&out("$pmt quotes\02.\02pl: @_");}
sub rargs{
 my $i=0;my $out;
 if (@_){$i=@_[0];}
 while (@args[$i]){$out="$out @args[$i]";$i++;}
 return substr($out,1,length($out));
}
sub totq{
 my $i=0;
 open(READ, $qgetfrom);
 my @read=<READ>;
 close(READ);
 while(@read[$i]){$i++;}
 return $i;
}
sub getq{
 my $i=-1;
 open(READ, $qgetfrom);
 my @read=<READ>;
 close(READ);
 return $read[$i+$_[0]];
}
sub qhandler{
 @args=split(/ /, shift); #@args is the equivalent of @ARGV.
 if (! -d "$hd/.xchat"){
  my $success=mkdir "$hd/.xchat",022;
  if ($success){&qout("~/.xchat did not exist, created successfully.");}
  else {&qout("error in creation of ~/.xchat, halting.");return 1;}
 }
 if (@args[0] eq "add"){
  if (!@args[1]){&qout("/quotes \02add\02 <\02quote\02>");return 1;}
  my $rargs=&rargs(1);
  open(READ, $qsendto) || &qout("error opening $qsendto.");
  print READ "$rargs\n";
  close(READ);
  &qout("added \"$rargs\" to your quote list.");
 }
 elsif (@args[0] eq "del"){
  if (!@args[1] || int(@args[1]) != @args[1]){&qout("/quotes \02del\02 [\02#\02]");return 1;}
  if (@args[1] > &totq() || @args[1] < 1){&qout("invalid quote number selected.");return 1;}
  my $i=0;
  open(READ, $qgetfrom) || &qout("error opening $qgetfrom.");
  my @read=<READ>;
  close(READ);
  open(SEND, $qwriteto) || &qout("error opening $qwriteto.");
  while (@read[$i]){
   if (@args[1]-1 != $i){print SEND "@read[$i]";}
   $i++;
  }
  close(SEND);
  &qout("deleted quote number: @args[1].");
 }
 elsif (@args[0] eq "list"){
  my $i=1;
  &qout("listing quote(s).");
  while ($i <= &totq()){
   my $m=&getq($i);chomp $m;
   my $num=$lsoffset-length($i);my $nu=0;my $j;
   while ($num > $nu){$j="$oschar$j";$nu++;}
   &out("$pmt (\02$j$i\02)\02 $m");
   $i++;
  }
  if ($i eq "1"){&qout("no quotes found.");return 1;}
  &qout("completed quote list.");
 }
 elsif (@args[0] eq "say"){
  if (!@args[1] || int(@args[1]) != @args[1]){&qout("/quotes \02op\02 [\02#\02] <\02#channel\02>");return 1;}
  if (@args[1] > &totq() || @args[1] < 1){&qout("invalid quote number selected.");return 1;}
  my $quote=&getq(@args[1]);chomp $quote;
  IRC::command "/say $qprefix$quote (\02#@args[1]\02-\02quotes.pl\02)";
 }
 elsif (@args[0] eq "random"){
  if (&totq() < 1){&zout("no quotes found.");return 1;}
  my $rand=int(rand(&totq()))+1;
  IRC::command "/quotes say $rand";
 }
 elsif (@args[0] eq "clear"){
  if (! -f "$hd/.xchat/qlist"){&qout("no quotes file to clear.");}
  else {
   my $success=unlink "$hd/.xchat/qlist";
   if ($success){&qout("cleared quotes file successfully.");}
   else {&qout("quotes file was cleared un-successfully.");}
  }
 }
 elsif (@args[0] eq "help"){
  &qout("-- \02syntax help for /quotes.\02 --");
  &qout("(\02add    \02)\02 add a quote to your quotelist.");
  &qout("(\02del    \02)\02 delete a stored quote numeric value.");
  &qout("(\02list   \02)\02 list current quote(s) in storage.");
  &qout("(\02say    \02)\02 say a quote to the current window.");
  &qout("(\02random \02)\02 say a random quote to the current window.");
  &qout("(\02clear  \02)\02 clear quote(s) in storage.");
  &qout("(\02#      \02)\02 equivalent of /quotes say [#].");
 }
 elsif (&totq() >= @args[0] && @args[0] > 0) {IRC::command "/quotes say @args[0]";}
 else {&qout("/quotes [\02add\02|\02del\02|\02list\02|\02say\02|\02random\02|\02clear\02|\02#\02|\02help\02]");}
 return 1;
}
# quotes.pl: init. (3)
IRC::add_command_handler("quotes", "qhandler");
&qout("by _v9[v9\@ice.org], type \"\02/quotes help\02\" for syntax help.");
# quotes.pl: eof. (4)
