html/foreach_user.pl


   1 use Irssi;
   2 use strict;
   3 use vars qw($VERSION %IRSSI);
   4 $VERSION = "1.0";
   5 %IRSSI = (
   6       authors     => "David Leadbeater",
   7       contact     => "dgl\@dgl.cx",
   8       url         => "http://irssi.dgl.cx/",
   9       license     => "GNU GPLv2 or later", 
  10       name        => "foreach user",
  11       description => "Extends the /foreach command to have /foreach user 
  12                 (users in a channel).
  13                 Syntax: /foreach user [hostmask] command.",
  14 );
  15 
  16 # Examples:
  17 # /foreach user /whois $0
  18 # /foreach user *!eviluser@* /k $0 evil!  (consider kicks.pl ;) )
  19 
  20 Irssi::command_bind('foreach user', sub {
  21    my($command) = @_;
  22    return unless length $command;
  23 
  24    my $mask = '*!*@*';
  25    # see if it begins with a mask (kind of assumes cmdchars is /).
  26    if($command !~ m!^/! && $command =~ /^\S+[!@]/) { 
  27       ($mask,$command) = split / /, $command, 2;
  28       # make sure the mask is okay.
  29       $mask .= '@*' if $mask !~ /\@/;
  30       $mask = "*!$mask" if $mask !~ /!/;
  31    }
  32 
  33    my $channel = ref Irssi::active_win ? Irssi::active_win->{active} : '';
  34    return unless ref $channel;
  35 
  36    for my $nick($channel->nicks) {
  37       next unless ref $nick;
  38       next unless $channel->{server}->mask_match_address($mask, $nick->{nick},
  39          $nick->{host} ? $nick->{host} : '');
  40       
  41       # the backtracking is only so $$0 is escaped (don't ask me why...)
  42       (my $tmpcommand = $command) =~ s/(?<!\$)\$(\d)/
  43                   if($1 == 0) {
  44                         $nick->{nick}
  45                   }elsif($1 == 1) {
  46                         $nick->{host}
  47                   }elsif($1 == 2) {
  48                         (split('@',$nick->{host}))[0];
  49                   }elsif($1 == 3) {
  50                         (split('@',$nick->{host}))[1];
  51                   }elsif($1 == 4) {
  52                         $nick->{realname}
  53                   }
  54             /eg;
  55       $tmpcommand =~ s/\$\$(\d)/\$$1/g;
  56       $channel->command($tmpcommand);
  57    }
  58 } );
  59