413f5e171da0adca4742d97bd6323bc0799e7cea
[silc.git] / apps / irssi / scripts / autoop.pl
1 # /AUTOOP <*|#channel> [<nickmasks>]
2
3 use Irssi;
4 use strict;
5
6 my (%opnicks, %temp_opped);
7
8 sub cmd_autoop {
9         my ($data) = @_;
10         my ($channel, $masks) = split(" ", $data, 2);
11
12         if ($channel eq "") {
13                 if (!%opnicks) {
14                         Irssi::print("Usage: /AUTOOP <*|#channel> [<nickmasks>]");
15                         Irssi::print("No-one's being auto-opped currently.");
16                         return;
17                 }
18
19                 Irssi::print("Currently auto-opping in channels:");
20                 foreach $channel (keys %opnicks) {
21                         $masks = $opnicks{$channel};
22
23                         if ($channel eq "*") {
24                                 Irssi::print("All channels: $masks");
25                         } else {
26                                 Irssi::print("$channel: $masks");
27                         }
28                 }
29                 return;
30         }
31
32         if ($masks eq "") {
33                 $masks = "<no-one>";
34                 delete $opnicks{$channel};
35         } else {
36                 $opnicks{$channel} = $masks;
37         }
38         if ($channel eq "*") {
39                 Irssi::print("Now auto-opping in all channels: $masks");
40         } else {
41                 Irssi::print("$channel: Now auto-opping: $masks");
42         }
43 }
44
45 sub autoop {
46         my ($channel, $masks, @nicks) = @_;
47         my ($server, $nickrec);
48
49         $server = $channel->{server};
50         foreach $nickrec (@nicks) {
51                 my $nick = $nickrec->{nick};
52                 my $host = $nickrec->{host};
53
54                 if (!$temp_opped{$nick} &&
55                     $server->masks_match($masks, $nick, $host)) {
56                         $channel->command("/op $nick");
57                         $temp_opped{$nick} = 1;
58                 }
59         }
60 }
61
62 sub event_massjoin {
63         my ($channel, $nicks_list) = @_;
64         my @nicks = @{$nicks_list};
65
66         return if (!$channel->{chanop});
67
68         undef %temp_opped;
69
70         # channel specific
71         my $masks = $opnicks{$channel->{name}};
72         autoop($channel, $masks, @nicks) if ($masks);
73
74         # for all channels
75         $masks = $opnicks{"*"};
76         autoop($channel, $masks, @nicks) if ($masks);
77 }
78
79 Irssi::command_bind('autoop', 'cmd_autoop');
80 Irssi::signal_add_last('massjoin', 'event_massjoin');