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