html/userhost.pl


   1 # $Id: userhost.pl,v 1.18 2002/07/04 13:18:02 jylefort Exp $
   2 
   3 use Irssi 20020121.2020 ();
   4 $VERSION = "0.23";
   5 %IRSSI = (
   6 	  authors     => 'Jean-Yves Lefort',
   7 	  contact     => 'jylefort\@brutele.be, decadix on IRCNet',
   8 	  name        => 'userhost',
   9 	  description => 'Adds a -cmd option to the /USERHOST builtin command',
  10 	  license     => 'BSD',
  11 	  url         => 'http://void.adminz.be/irssi.shtml',
  12 	  changed     => '$Date: 2002/07/04 13:18:02 $ ',
  13 );
  14 
  15 # usage:
  16 #
  17 #	/USERHOST <nicks> [-cmd <command>]
  18 #
  19 #	-cmd		evaluate the specified Irssi command
  20 #
  21 # percent substitutions in command:
  22 #
  23 #	%n		nick
  24 #	%u		user
  25 #	%h		host
  26 #	%%		a single percent sign
  27 #
  28 # examples:
  29 #
  30 #	/userhost albert -cmd echo %n is %u at %h
  31 #	/userhost john james -cmd exec xterm -e ping %h
  32 #
  33 # changes:
  34 #
  35 #	2002-07-04	release 0.23
  36 #			* signal_add's uses a reference instead of a string
  37 #
  38 #	2002-02-08	release 0.22
  39 #			* safer percent substitutions
  40 #
  41 #	2002-01-27	release 0.21
  42 #			* uses builtin expand
  43 #
  44 #	2002-01-24	release 0.20
  45 #			* now replaces builtin /USERHOST
  46 #
  47 #	2002-01-23	initial release
  48 
  49 use strict;
  50 
  51 # -verbatim- import expand
  52 sub expand {
  53   my ($string, %format) = @_;
  54   my ($len, $attn, $repl) = (length $string, 0);
  55   
  56   $format{'%'} = '%';
  57 
  58   for (my $i = 0; $i < $len; $i++) {
  59     my $char = substr $string, $i, 1;
  60     if ($attn) {
  61       $attn = undef;
  62       if (exists($format{$char})) {
  63 	$repl .= $format{$char};
  64       } else {
  65 	$repl .= '%' . $char;
  66       }
  67     } elsif ($char eq '%') {
  68       $attn = 1;
  69     } else {
  70       $repl .= $char;
  71     }
  72   }
  73   
  74   return $repl;
  75 }
  76 # -verbatim- end
  77 
  78 my $queuedcmd;
  79 
  80 sub userhost_reply {
  81   if ($queuedcmd) {
  82     my ($server, $args, $sender, $sender_address) = @_;
  83     if ($args =~ / :(.*)$/) {
  84       foreach (split(/ /, $1)) {
  85 	$server->command(expand($queuedcmd, "n", $1, "u", $2, "h", $3))
  86 	  if (/(.*)\*?=[-+][-+~]?(.*)@(.*)/);
  87       }
  88     }
  89     $queuedcmd = undef;
  90     Irssi::signal_stop();
  91   }
  92 }
  93 
  94 sub userhost {
  95   my ($args, $server, $item) = @_;
  96   my ($nicks, $command) = split(/ -cmd /, $args);
  97   if ($queuedcmd = $command) {
  98     $server->send_raw("USERHOST :$nicks");
  99     Irssi::signal_stop();
 100   }
 101 }
 102 
 103 Irssi::signal_add("event 302", \&userhost_reply);
 104 Irssi::command_bind("userhost", \&userhost);