package Xchat::b0at::NickRecovery; # prevent collision warnings use strict; use warnings; my $NAME = 'Nick Recovery'; my $VERSION = '003'; my $COMMAND = 'RECOVERY'; my $USAGE = "Usage: $COMMAND -on [nickname]". "\n $COMMAND -off". "\n $COMMAND, to show status"; my $PREFIX = "\02$NAME\02\t"; { # I love code folding, yes I do. # use Xchat 'foo'; importing only works in 2.4.0+ # manually "import" all Xchat:: functions # name is exactly the same unless specified otherwise *register = \&Xchat::register; *hook_server = \&Xchat::hook_server; *hook_command = \&Xchat::hook_command; *hook_print = \&Xchat::hook_print; *hook_timer = \&Xchat::hook_timer; *unhook = \&Xchat::unhook; *xprint = \&Xchat::print; # name differs *xprintf = \&Xchat::printf; # name differs *command = \&Xchat::command; *commandf = \&Xchat::commandf; *find_context = \&Xchat::find_context; *get_context = \&Xchat::get_context; *set_context = \&Xchat::set_context; *get_info = \&Xchat::get_info; *get_prefs = \&Xchat::get_prefs; *emit_print = \&Xchat::emit_print; *nickcmp = \&Xchat::nickcmp; *get_list = \&Xchat::get_list; *user_info = \&Xchat::user_info; *context_info = \&Xchat::context_info; sub EAT_NONE (){Xchat::EAT_NONE} sub EAT_XCHAT (){Xchat::EAT_XCHAT} sub EAT_PLUGIN (){Xchat::EAT_PLUGIN} sub EAT_ALL (){Xchat::EAT_ALL} sub PRI_HIGHEST (){Xchat::PRI_HIGHEST} sub PRI_HIGH (){Xchat::PRI_HIGH} sub PRI_NORM (){Xchat::PRI_NORM} sub PRI_LOW (){Xchat::PRI_LOW} sub PRI_LOWEST (){Xchat::PRI_LOWEST} sub REMOVE (){0} # for timers for all xchat versions sub KEEP (){1} } register($NAME, $VERSION, 'Recover preferred nick when it becomes available.'); hook_command($COMMAND, \&recovery_command, {help_text => $USAGE}); xprint("\02$NAME $VERSION\02 by b0at (use /$COMMAND)"); # set global recovery nickname to first choice in server list my $recover_nick = get_prefs('irc_nick1'); my %hooks = (ison=>undef, quit=>undef,); recovery_command( [$COMMAND, '-on'] ); # turn it on sub ison_wrangler { # requires notify list to have preferred nick in it for (split(' ', substr($_[1][3],1)), undef) { last if not defined $_; return EAT_NONE if nickcmp($recover_nick, $_) == 0; } recover_nick(); return EAT_NONE; } sub quit_wrangler { # check quits for the nick my $nick = $_[0][0]; # the recovery nick just quit recover_nick() if( nickcmp($recover_nick, $nick) == 0 ); return EAT_NONE; } sub recover_nick { # attempt to recover nick if appropriate return if get_info('nick') eq $recover_nick; xprint("$PREFIX Recovering nick '$recover_nick'..."); command("nick $recover_nick"); } sub recovery_command { # handle user input my ($switch, $new_nick) = @{$_[0]}[1..2]; if( not $switch ) { xprint("$PREFIX Status: \02". ( $hooks{ison} ? 'on' : 'off'). "\02, Nick: '$recover_nick'."); } elsif( $switch =~ /^-off$/i ) { unhook($hooks{$_}) and undef($hooks{$_}) for keys %hooks; xprint("$PREFIX Recovery turned off."); } elsif( $switch =~ /^-on$/i ) { if( $hooks{ison} ) { # if already on if( $new_nick and $new_nick ne $recover_nick) { # set a new nick xprint("$PREFIX No longer watching for '$recover_nick', ". "now watching for '$new_nick'."); $recover_nick = $new_nick; } else { # nothing to set xprint("$PREFIX Already watching for '$recover_nick'."); } } # not hooked yet, hook it else { $recover_nick = $new_nick if $new_nick; if( $recover_nick ) { $hooks{ison} = hook_server('303', \&ison_wrangler); $hooks{quit} = hook_print('Quit', \&quit_wrangler); xprint("$PREFIX Now watching for '$recover_nick'."); } else { # no nick specified to watch xprint($USAGE); } } } return EAT_ALL; # exclusive use of the command } 1; __END__ by: b0at license: public domain recovers the first global nick preference