# aj.pl: autojoin(+revised join), Version_0.1:[final] -- 5kiodine. (!4896 bytes).
# aj.pl: by: _v9[v9@ice.org] -- for use in X-Chat(built on 1.2.1).
# aj.pl: type "/load /pathto/aj.pl" in X-Chat or put it in ~/.xchat to auto-load on start.

# aj.pl: base vars. (1)
$hd=$ENV{"HOME"};
$pmt="\02***\02";
$lsoffset=4;
$oschar="0";
$ajsendto=">>$hd/.xchat/ajlist";
$ajwriteto=">$hd/.xchat/ajlist";
$ajgetfrom="$hd/.xchat/ajlist";
$ajdisable=">$hd/.xchat/.ajoff";
# aj.pl: subroutines. (2)
sub out{IRC::print "@_\n";}
sub aout{&out("$pmt aj\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 totc{
 my $i=0;
 open(READ, $ajgetfrom);
 my @read=<READ>;
 close(READ);
 while(@read[$i]){$i++;}
 return $i/2;
}
sub getc{
 my $i=-2;
 if (@_[1] eq "desc"){$i++;}
 open(READ, $ajgetfrom);
 my @read=<READ>;
 close(READ);
 return $read[$i+$_[0]*2];
}
sub ajcheck{if (! -f "$hd/.xchat/.ajoff") {&ajjoin();}}
sub ajjoin{
 my $m=IRC::get_info(3);
 if (!$m) {&aout("no connection to autojoin channel(s).");return 1;}
 if (&totc() < 1) {&zout("no autojoin channels to join.");return 1;}
 &aout("attempting to join autojoin channel(s).");
 my $i=0;
 while (&totc() > $i) {
  my $m=&getc($i, "chan");
  IRC::command "/join $m";
  $i++;
  }
 return 1;
}
sub jhandler{
 my @args=split(/ /, $_[0]);my $i=0;my $m;
 while (@args[$i]) {
  $m="";
  if (substr(@args[$i],0,1) ne "#"){$m="#"}
  IRC::send_raw "JOIN $m@args[$i]\n";
  $i++;
 }
 return 1;
}
sub ajhandler{
 @args=split(/ /, shift); #@args is the equivalent of @ARGV.
 if (! -d "$hd/.xchat"){
  my $success=mkdir "$hd/.xchat",022;
  if ($success){&aout("~/.xchat did not exist, created successfully.");}
  else {&aout("error in creation of ~/.xchat, halting.");return 1;}
 }
 if (@args[0] eq "add"){
  if (!@args[2]){&aout("/aj \02add\02 <\02#channel\02> <\02header\02>");return 1;}
  my $chan=$args[1];
  my $rargs=&rargs(2);
  if (substr($chan,0,1) ne "#"){$chan="#$chan";}
  open(READ, $ajsendto) || &aout("error opening $ajsendto.");
  print READ "$chan\n$rargs\n";
  close(READ);
  &aout("added $chan to your autojoin list.");
 }
 elsif (@args[0] eq "del"){
  if (!@args[1] || int(@args[1]) != @args[1]){&aout("/aj \02del\02 [\02#\02]");return 1;}
  if (@args[1] > &totc() || @args[1] < 1){&aout("invalid autojoin number selected.");return 1;}
  my $i=0;my $j=0;
  open(READ, $ajgetfrom) || &aout("error opening $ajgetfrom.");
  my @read=<READ>;
  close(READ);
  open(SEND, $ajwriteto) || &aout("error opening $ajwriteto.");
  while (@read[$i]){
   if (@args[1]*2-2 == $i || $j eq "1"){
    if ($j eq "1"){$j--;}
    else {my @m=@read[$i];$m=@m[0];chomp $m;$j++;}
   }
   else {print SEND "@read[$i]";}
   $i++;
  }
  close(SEND);
  &aout("deleted autojoin number: @args[1]. ($m)");
 }
 elsif (@args[0] eq "list"){
  my $i=1;my $j;
  &aout("listing autojoin channel(s).");
  while ($i <= &totc()){
   my $m=&getc($i, "desc");chomp $m;
   my $n=&getc($i, "chan");chomp $n;
   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 (\02$n\02)");
   $i++;
  }
  if ($i eq "1"){&aout("no channels found.");return 1;}
  &aout("completed autojoin list.");
 }
 elsif (@args[0] eq "join"){&ajjoin();}
 elsif (@args[0] eq "clear"){
  if (! -f "$hd/.xchat/ajlist"){&aout("no autojoin file to clear.");}
  else {
   my $success=unlink "$hd/.xchat/ajlist";
   if ($success){&aout("cleared autojoin file successfully.");}
   else {&aout("autojoin file was cleared un-successfully.");}
  }
 }
 elsif (@args[0] eq "toggle"){
  my $m;
  if (-f "$hd/.xchat/.ajoff"){
   my $success=unlink "$hd/.xchat/.ajoff";
   if (!$success){&aout("autojoin disable file was cleared un-successfully.");}
   $m="enabled"
  }
  else {
   open(READ, $ajdisable) || &aout("error opening $ajdisable.");
   close(READ);
   $m="disabled"
  }
  &aout("autojoin on connect is now: $m.");
 }
 elsif (@args[0] eq "help"){
  &aout("-- \02syntax help for /aj.\02 --");
  &aout("(\02add    \02)\02 add a channel to your autojoin.");
  &aout("(\02del    \02)\02 delete a stored autojoin numeric value.");
  &aout("(\02list   \02)\02 list current channel(s) in storage.");
  &aout("(\02join   \02)\02 join channel(s) in your autojoin.");
  &aout("(\02clear  \02)\02 clear autojoin channel(s) in storage.");
  &aout("(\02toggle \02)\02 turn on/off the on connect autojoin.");
 }
 else {&aout("/aj [\02add\02|\02del\02|\02list\02|\02join\02|\02clear\02|\02toggle\02|\02help\02]");}
 return 1;
}
# aj.pl: init. (3)
IRC::add_command_handler("aj", "ajhandler");
IRC::add_command_handler("autojoin", "ajhandler");
IRC::add_command_handler("join", "jhandler");
IRC::add_command_handler("j", "jhandler");
IRC::add_message_handler("001", "ajcheck"); 
&aout("by _v9[v9\@ice.org], type \"\02/aj help\02\" for syntax help.");
# aj.pl: eof. (4)
