# Auto-Run Script Ver 1.05 # Developed by Issac Goldstand # Built for X-Chat 1.6.x - Last modified July 30, 2001 # Latest version available at http://www.beamartyr.net/projects/autorun.pl.txt my $autorundir=undef; if ($ENV{OS}=~/win/i) {$autorundir="./autorun/";} else {$autorundir="$ENV{HOME}/.xchat/autorun/";} my $autorunindex = "$autorundir/autorun.conf"; my $autorunversion = "$autorundir/autorun.ver"; my $version="1.05"; my $verbose=0; undef %commands; IRC::print("\0035:: Loading AutoRun Script (Ver $version) ::\003 \n"); IRC::register("AutoRun",$version,"savelists",""); IRC::add_message_handler("001","mhandler"); IRC::add_command_handler("autorun","chandler"); &verifydirstructure; &verifyversion; &loadlists; IRC::print("*** \02For help type /autorun help\02\n"); #Message handlers - IRC Interface sub mhandler { local($line) = shift(@_); $line =~ /:(.*) 001 (.*):(.*)/; local($server,$nick,$msg)=($1,$2,$3); IRC::print("*** \02$nick\02 is now connected to \02$server\02\n"); &doexec($server); return 0; } # Command handlers - User Interface sub chandler { local(@args)=split(/ /,shift); #Get args list local($cmd)=shift(@args); if ($cmd eq "help" || $cmd eq "HELP") {&dohelp(@args);return 1;} elsif ($cmd eq "add" || $cmd eq "ADD") {&add(@args);return 1;} elsif ($cmd eq "list" || $cmd eq "LIST") {&list(@args);return 1;} elsif ($cmd eq "remove" || $cmd eq "REMOVE") {&remove(@args);return 1;} elsif ($cmd eq "exec" || $cmd eq "EXEC") {&doexec(@args);return 1;} elsif ($cmd eq "verbose" || $cmd eq "VERBOSE") {&verbose(@args);return 1;} IRC::print("*** \02AutoRun - Unrecognized command.\02\n*** \02Type /autorun help for detailed help.\02\n"); return 1; } # List save/load sub savelists { $,="\n"; local($i,$server)=(0,undef); open(INDEX,">$autorunindex"); foreach $server (keys %commands) { print INDEX "[$server]\n"; foreach $i ( 0 .. $#{ $commands{$server}}) {print INDEX "$commands{$server}[$i]\n";} } close(INDEX); } sub loadlists { local($i,$server)=(0,undef); open(INDEX,"$autorunindex"); $/="\n"; while () { chop; if (/^\s+?\/\/.*$/){next;} #//comment - can't use # bc of chan names elsif (/^\s*\[(.*)\].*/){$server=$1;next;} #server else {push(@{$commands{$server}},$_);} } close(INDEX); } # Version checking for backwards compatability sub verifyversion #Version check what's running { open(VER,"$autorunversion"); local($ver)=; close(VER); if ($ver<$version) #Lower version OR first run { if (!($ver)) #First time ever (or updated between 1.00a and 1.02) { IRC::print("*** \02AutoRun\02 - \02Version $version\02 - First run detected. Initializing/updating files\n"); local(@mcmd)=&loadlists_1_00; #ATTEMPT to salvage old file $commands{"^*.\$"}=@mcmd; #It didn't work for me, I doubt it &savelists; #will for others. However, due to &setversion; #the fact that this version was undef %commands; #released so soon after the alpha return 1; #version, I'm not going to bother } #I _will_ leave this section for #future updates, however... #Version 1.02 --> Version 1.03 IRC::print("*** \02AutoRun\02 - Upgrade detected $ver --> $version"); IRC::print(" \02AutoRun\02 - Upgrading necessary files. New features include:\n"); if ($ver eq "1.02") { IRC::print(" Version 1.03 --> Verbosity activated.\n"); IRC::print(" Version 1.04 --> No changes.\n"); IRC::print(" Version 1.05 --> Added Win32 Compatibility.\n"); &setversion; } if ($ver eq "1.03") { IRC::print(" Version 1.04 --> No changes.\n"); IRC::print(" Version 1.05 --> Added Win32 Compatibility.\n"); &setversion; } if ($ver eq "1.04") { IRC::print(" Version 1.05 --> Added Win32 Compatibility.\n"); &setversion; } } } sub setversion { open(VER,">$autorunversion"); print VER $version; close(VER); IRC::print(" \02AutoRun\02 - Version $version was succesfully configured.\n"); } sub verifydirstructure #make a dir to save stuff in { if (! -d "$autorundir") { local($success)=mkdir "$autorundir",oct('700'); #man says this is backwards. Experience says ^^^ otherwise... if ($success){IRC::print("*** \02AutoRun\02 - ~/.xchat/autorun did not exist. Created successfully.\n");} else {IRC::print("*** \02AutoRun\02 - Error in creation of ~/.xchat/autorun.\n Save features disabled for this session.\n");return 1;} } } # Helper subs - Keep it less crowded sub dohelp #Help display { IRC::print("*** \02AutoRun Commands\02\n"); IRC::print(" \02add \02 - Adds to autorun script for\n"); IRC::print(" . Also, adds to"); IRC::print(" list of servers, if not already listed.\n"); IRC::print(" \02list [servermask]\02 - Displays current autorun script for"); IRC::print(" [servermask], if specified. Otherwise,\n"); IRC::print(" displays list of registered servermasks\n"); IRC::print(" \02remove \02 - Removes command from autorun script\n"); IRC::print(" Also removes servermask entry if last command\n"); IRC::print(" is removed\n"); IRC::print(" \02exec \02 - Executes script immediately. Note that this\n"); IRC::print(" is NOT a regexp, but rather a hostname\n"); IRC::print(" \02verbose [ON|OFF]\02 - Toggles verbose mode. Default is non-verbose\n"); IRC::print(" mode. Try adding \02/autorun verbose ON\02 to\n"); IRC::print(" servermask ^.*\$ to activate verbosity.\n"); IRC::print("\n*** Note: Servermasks should be regular expressions (based on perl 5)\n"); IRC::print(" Examples: ^irc\\.EFNet\\.net\$, ^.*\\.xchat\\.org\$\n"); IRC::print(" For the non-regexp-educated: Enter any server in format ^irc\\.domain\\.com\$ \n"); IRC::print(" or enter ^.*\$ to run scripts on ALL servers.\n"); } sub add #Add comand { local($server)=shift; if (!($server)){IRC::print("*** \02AutoRun\02 - You must specify a server name\n");return 1;} local(@args)=@_; if (@args) { local($cmd)=join(" ",@args); push(@{$commands{$server}},$cmd); IRC::print("*** \02AutoRun\02 - Added commmand \"$cmd\" to autorun for $server\n"); } else { IRC::print("*** \02AutoRun\02 - You must specify a command to add\n"); } } sub list #List command { local($server)=shift; if ($server) {IRC::print("*** \02AutoRun\02 - Script for server $server\n"); local($i,$lenmis,$j,$prefix)=(0,undef,0,undef); if (!(@{$commands{$server}})) {IRC::print("No commands");} else { foreach $i (1..$#{$commands{$server}}+1) { $lenmis=4-length($i); while($lenmis>$j){$prefix="0$prefix";$j++;} IRC::print("\02$prefix$i\02 $commands{$server}[$i-1]\n"); } } } else {IRC::print("*** \02AutoRun\02 - Currently registered servermasks\n"); foreach $server (keys %commands){IRC::print(" \02$server\02\n");}} if(!(%commands)){IRC::print(" No registered servers");} } sub remove #Remove command { local($server)=shift; if (!($server)) {IRC::print("*** \02AutoRun\02 - Error. You didn't specify a servermask\n");return 1;} if(!(@{$commands{$server}})) {IRC::print("*** \02AutoRun\02 - Error. The specified servermask is not valid.\n");delete($commands{$server});return 1;} local(@args)=@_; local($i,$lenmis,$j,$prefix)=(0,undef,0,undef); if ($args[0]=~/^\d+$/) { if ($args[0]-1>$#{$commands{$server}}){IRC::print("*** \02AutoRun\02 - Error. No command exists with that index\n");return 1;} foreach $i (($args[0]-1)..($#{$commands{$server}})) {$commands{$server}[$i]=$commands{$server}[$i+1];} pop(@{$commands{$server}}); $i=$args[0];$lenmis=4-length($i); while($lenmis>$j){$prefix="0$prefix";$j++;} IRC::print("*** \02AutoRun\02 - Successfully removed command \02$prefix$i\02\n"); do {delete $commands{$server};IRC::print(" Removed empty servermask \"$server\"\n");} unless (@{$commands{$server}}); } else { IRC::print("*** \02AutoRun\02 - You must specify a command number to remove\n"); } } sub doexec #Do now - also called by mhandler to do it then... { local($servername)=shift; local($i,$did,$servermask)=(0,0,undef); foreach $servermask (keys %commands) { next if ($servername!~/$servermask/); if (!($did)){IRC::print("*** \02AutoRun\02 - Executing script for server $servername\n");$did=1;} IRC::print("*** \02AutoRun\02 - \02$servername\02 matches mask \02$servermask\02\n"); foreach $i (0..$#{$commands{$servermask}}) { IRC::print(" $commands{$servermask}[$i]\n") if ($verbose); IRC::command("$commands{$servermask}[$i]"); } } if (!($did)){IRC::print("*** \02AutoRun\02 - Nothing to do for server $servername\n");} } sub verbose #Toggles verbosity { local($cmd)=shift; if (!($cmd)) { local($str)=" \02Autorun\02 - Currently in "; $str=$str."non-" unless ($verbose); $str=$str."verbose mode\n"; IRC::print("$str"); } elsif ($cmd eq "off" || $cmd eq "OFF") { IRC::print(" \02AutoRun\02 - Entering silent mode\n"); $verbose=0; } elsif ($cmd eq "on" || $cmd eq "ON") { IRC::print(" \02AutoRun\02 - Entering verbose mode\n"); $verbose=1; } else { IRC::print(" \02AutoRun\02 - Unknown command. Type \02/autorun help\02 for help\n"); } } # Backwards compatability functions sub loadlists_1_00 { $/="\n"; local($cmds); open(INDEX,"$autorunindex"); while (){chop($_);push(@cmds,$_)}; shift(@cmds); #This is to get rid of a leading empty record which close(INDEX); #keeps pooping up. I have no idea from where it came return(@cmds); } __END__