summaryrefslogtreecommitdiffstats
path: root/scripts/foreach_user.pl
blob: 23a15915a49e0e4083c16a485b2db8e1cf30485a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use Irssi;
use strict;
use vars qw($VERSION %IRSSI);
$VERSION = "1.0";
%IRSSI = (
      authors     => "David Leadbeater",
      contact     => "dgl\@dgl.cx",
      url         => "http://irssi.dgl.cx/",
      license     => "GNU GPLv2 or later", 
      name        => "foreach user",
      description => "Extends the /foreach command to have /foreach user 
        (users in a channel).
        Syntax: /foreach user [hostmask] command.",
);

# Examples:
# /foreach user /whois $0
# /foreach user *!eviluser@* /k $0 evil!  (consider kicks.pl ;) )

Irssi::command_bind('foreach user', sub {
   my($command) = @_;
   return unless length $command;

   my $mask = '*!*@*';
   # see if it begins with a mask (kind of assumes cmdchars is /).
   if($command !~ m!^/! && $command =~ /^\S+[!@]/) { 
      ($mask,$command) = split / /, $command, 2;
      # make sure the mask is okay.
      $mask .= '@*' if $mask !~ /\@/;
      $mask = "*!$mask" if $mask !~ /!/;
   }

   my $channel = ref Irssi::active_win ? Irssi::active_win->{active} : '';
   return unless ref $channel;

   for my $nick($channel->nicks) {
      next unless ref $nick;
      next unless $channel->{server}->mask_match_address($mask, $nick->{nick},
         $nick->{host} ? $nick->{host} : '');
      
      # the backtracking is only so $$0 is escaped (don't ask me why...)
      (my $tmpcommand = $command) =~ s/(?<!\$)\$(\d)/
         if($1 == 0) {
            $nick->{nick}
         }elsif($1 == 1) {
            $nick->{host}
         }elsif($1 == 2) {
            (split('@',$nick->{host}))[0];
         }elsif($1 == 3) {
            (split('@',$nick->{host}))[1];
         }elsif($1 == 4) {
            $nick->{realname}
         }
      /eg;
      $tmpcommand =~ s/\$\$(\d)/\$$1/g;
      $channel->command($tmpcommand);
   }
} );