6 use vars qw($VERSION %IRSSI $DEBUG);
8 # 0.05 -- Add IPv6 support
10 $VERSION = q$Revision$;
11 %IRSSI = (authors => 'Don Armstrong',
13 description => 'Provides /ak /aq /ab /abr /abrn /arn /amb /amr /at',
18 $DEBUG = 1 unless defined $DEBUG;
20 my ($actions, %defaults);
22 %defaults = (GET_OP => 1, # Should we try to get opped when we auto_bleh?
23 USE_CHANSERV => 1, # Should we use chanserv to get opped?
24 EXPIRE => 6000, # Do not try to do anything if the action is more than 6000 seconds old.
25 DEOP => 1, # Automatically deop after we've done whatever we were supposed to do.
26 TIMEOUT => 10, # Timeout /at bans after 10 minutes
27 SMELLSLIKEFN => qr/freenode.net/,
30 my %command_bindings = (ak => 'cmd_ak',
48 my ($data, $server, $witem) = @_;
49 return do_auto_bleh([qw(timeout)],$data,$server,$witem);
53 my ($data, $server, $witem) = @_;
54 return do_auto_bleh([qw(kick)],$data,$server,$witem);
58 my ($data, $server, $witem) = @_;
59 return do_auto_bleh([qw(kick ban)],$data,$server,$witem);
62 my ($data, $server, $witem) = @_;
63 return do_auto_bleh([qw(kick ban notice)],$data,$server,$witem);
67 my ($data, $server, $witem) = @_;
68 my @nicks = split /\s+/, $data;
71 do_auto_bleh([qw(ban)],$_,$server,$witem);
76 my ($data, $server, $witem) = @_;
77 return do_auto_bleh([qw(ban)],$data,$server,$witem);
81 my ($data, $server, $witem) = @_;
82 return do_auto_bleh([qw(quiet)],$data,$server,$witem);
87 my ($data, $server, $witem) = @_;
88 my @nicks = split /\s+/, $data;
91 do_auto_bleh([qw(remove)],$_,$server,$witem);
96 my ($data, $server, $witem) = @_;
97 return do_auto_bleh([qw(remove)],$data,$server,$witem);
100 my ($data, $server, $witem) =@_;
101 return do_auto_bleh([qw(remove ban)],$data,$server,$witem);
104 my ($data, $server, $witem) =@_;
105 return do_auto_bleh([qw(remove ban notice)],$data,$server,$witem);
108 my ($data, $server, $witem) =@_;
109 return do_auto_bleh([qw(remove,notice)],$data,$server,$witem);
114 my ($cmd, $data, $server, $witem, $duration) = @_;
116 if (!$server || !$server->{connected}) {
117 Irssi::print("Not connected to server");
121 if ($witem->{type} ne 'CHANNEL') {
122 Irssi::print("Can't autokick on a non-channel. [$witem->{type}]");
126 if (ref($cmd) eq 'HASH') {
129 elsif (ref($cmd) eq 'ARRAY') {
130 $cmd = {map {$_,1} @$cmd};
132 elsif (not ref($cmd)) {
133 $cmd = {map {$_,1} split /\s*,\s*/, $cmd};
136 die "Cmd: $cmd option to do_auto_bleh is not a supported type";
139 # Fix up options for opn which doesn't do quiet or remove;
140 # turn them into ban and kick, respectively.
141 if ($server->{address} !~ $defaults{SMELLSLIKEFN}) {
142 my %fn_mapping = (remove => 'kick',
145 timeout => 'btimeout',
147 for my $key (keys %fn_mapping) {
150 $$cmd{$fn_mapping{$key}} = 1;
156 # Irssi::print(Dumper($data,$server,$witem));
157 # set the network that we're on, the channel and the nick to kick
158 # once we've been opped
160 # Heh. Looks like $data needs to be sanitized a bit.
161 $data =~ /^\s*([^\s]+)\s*(\d+)?\s*(.+?|)\s*$/;
165 $timeout = $defaults{TIMEOUT} if not defined $timeout or $timeout eq '';
166 $reason = 'you should know better' if not defined $reason or $reason eq '';
168 my $nick_rec = $witem->nick_find($nick);
169 if (not defined $nick_rec) {
170 Irssi::print("Unable to find nick: $nick");
173 my $hostname = $nick_rec->{host} if defined $nick_rec;
174 Irssi::print("Unable to find hostname for $nick") if not defined $hostname or $hostname eq '';
175 $hostname =~ s/.+\@//;
177 Irssi::print("Nick set to '$nick' from '$data', reason set to '$reason'.") if $DEBUG;
178 my $action = {type => $cmd,
180 nick_rec => $nick_rec,
181 network => $witem->{server}->{chatnet},
182 server => $witem->{server},
185 channel => $witem->{name},
187 hostname => $hostname,
190 Irssi::print(i_want($action)) if $DEBUG;
191 if ($witem->{chanop}) { # we seem to be opped.
192 take_action($action,$server,$witem);
195 $actions->{$data.$action->{inserted}}=$action;
196 get_op($server, $action->{channel}) if $defaults{GET_OP};
202 my ($server,$channel) = @_;
204 Irssi::print("MSG chanserv op $channel");
205 $server->command("MSG chanserv op $channel") if $defaults{USE_CHANSERV};
211 return "I've wanted to $action->{type} $action->{nick} off $action->{channel} on $action->{network} since $action->{inserted}";
215 my ($action,$server,$channel) = @_;
217 my $type = $action->{type};
218 # Now support multiple actions against a single nick (to FE, kick
219 # ban, or remove ban). See /abr foo
220 if ($type->{timeout}) {
221 Irssi::print("Quieting $action->{nick} on $action->{channel} with hostname $action->{hostname} for $action->{timeout} minutes");
222 $server->send_raw("MODE $action->{channel} +q *!*@".$action->{hostname}) if $action->{hostname} ne ''; #quiet hostname
223 $bans_to_remove{"$action->{nick}$action->{inserted}"} = $action;
225 if ($type->{btimeout}) {
226 Irssi::print("Quieting $action->{nick} on $action->{channel} with hostname $action->{hostname} for $action->{timeout} minutes");
227 $server->send_raw("MODE $action->{channel} +b *!*@".$action->{hostname}) if $action->{hostname} ne ''; #quiet hostname
228 $bans_to_remove{"$action->{nick}$action->{inserted}"} = $action;
230 if ($type->{quiet}) {
231 Irssi::print("Quieting $action->{nick} on $action->{channel} with hostname $action->{hostname}") if $DEBUG;
233 if ($action->{hostname}) {
234 $server->send_raw("MODE $action->{channel} +b %*!*@".$action->{hostname}) if $action->{hostname} ne ''; #quiet hostname
239 Irssi::print("Banning $action->{nick} from $action->{channel} with hostname $action->{hostname}") if $DEBUG;
240 $server->send_raw("MODE $action->{channel} +b *!*@".$action->{hostname}) if $action->{hostname} ne ''; # ban hostname
243 Irssi::print("Kicking $action->{nick} from $action->{channel}") if $DEBUG;
244 if ($action->{reason} =~ /\s/) {
245 $server->send_raw("KICK $action->{channel} $action->{nick} :$action->{reason}");
248 $server->send_raw("KICK $action->{channel} $action->{nick} $action->{reason}");
251 if ($type->{remove}) {
252 Irssi::print("Removing $action->{nick} from $action->{channel}") if $DEBUG;
253 if ($action->{reason} =~ /\s/) {
254 $server->send_raw("REMOVE $action->{channel} $action->{nick} :$action->{reason}");
257 $server->send_raw("REMOVE $action->{channel} $action->{nick} $action->{reason}");
260 if ($type->{notice}) {
261 Irssi::print("Noticing $action->{nick} with $action->{reason}") if $DEBUG;
262 $server->command("NOTICE $action->{nick} $action->{reason}");
265 if ($type->{unquiet}) {
266 Irssi::print("Unquieting $action->{nick} on $action->{channel} with hostname $action->{hostname}") if $DEBUG;
267 $server->command("MODE $action->{channel} -q *!*@".$action->{hostname});
270 if ($type->{unban}) {
271 Irssi::print("Unbanning $action->{nick} on $action->{channel} with hostname $action->{hostname}") if $DEBUG;
272 $server->command("MODE $action->{channel} -b *!*@".$action->{hostname});
280 my $channel = $rec->{channel};
281 my $server = $rec->{server};
283 #$server->command("MODE $channel->{name} -o $channel->{ownick}->{nick}");
284 if ($channel->{chanop}) {
285 Irssi::print("MODE $channel->{name} -o $channel->{ownnick}->{nick}");
286 $channel->command("/deop $channel->{ownnick}->{nick}");
290 sub sig_mode_change {
291 my ($channel,$nick) = @_;
293 #Irssi::print(Dumper($nick));
294 #Irssi::print(Dumper($channel));
296 # Are there any actions to process?
297 # See if we got opped.
298 return if scalar(keys %$actions) eq 0;
300 if ($channel->{server}->{nick} eq $nick->{nick} and $nick->{op}) {
301 # Ok, we've been opped, or we are opped now, so do whatever we're supposed to do.
302 Irssi::print("We've been opped") if $DEBUG;
303 # We seem to need some sort of delay here for the chanop stuff to catch up
304 Irssi::timeout_add_once(400,'attempt_actions',undef);
307 Irssi::print("Fooey. Not opped.") if $DEBUG;
311 sub attempt_actions {
315 foreach (keys %$actions) {
316 #Irssi::print(Dumper($actions->{$_}));
317 # See if this action is too old
318 if (time - $actions->{$_}->{inserted} > $defaults{EXPIRE}) {
319 Irssi::print("Expiring action: \"".i_want($actions->{$_})."\" because of time");
320 delete $actions->{$_};
323 Irssi::print(i_want($actions->{$_})) if $DEBUG;
324 # Find the server to take action on
325 my $server = Irssi::server_find_chatnet($actions->{$_}->{network});
326 Irssi::print("Unable to find server for chatnet: $actions->{$_}->{network}") and return if not defined $server;
327 Irssi::print("Found server for chatnet: $actions->{$_}->{network}") if $DEBUG;
328 # Find the channel to take action on
329 my $s_channel = $server->channel_find($actions->{$_}->{channel});
330 Irssi::print("Unable to find channel for channel: $actions->{$_}->{channel}") and return if not defined $s_channel;
331 Irssi::print("Found channel for channel: $actions->{$_}->{channel}") if $DEBUG;
332 # Are we opped on that channel?
333 if ($s_channel->{chanop}) { # Yes? Take the action against the user.
334 Irssi::print("We are opped on the channel!") if $DEBUG;
335 take_action($actions->{$_},$server,$s_channel);
336 push @deop_array,{server=>$server,channel=>$s_channel} if $defaults{DEOP};
337 delete $actions->{$_}; # Do not repeat this action.
340 Irssi::print("We are not opped on the channel.") if $DEBUG;
343 foreach (@deop_array) {
349 sub try_to_remove_bans {
350 return unless keys %bans_to_remove;
351 for my $key (keys %bans_to_remove) {
352 if (($bans_to_remove{$key}{inserted} + $bans_to_remove{$key}{timeout}*60) < time) {
353 $bans_to_remove{$key}{type} = {unquiet => 1}; #unquiet
354 $actions->{$key} = $bans_to_remove{$key};
355 delete $bans_to_remove{$key};
356 get_op($actions->{$key}{server}, $actions->{$key}{channel}) if $defaults{GET_OP};
361 # call the try to remove bans function every minute
362 Irssi::timeout_add(1000*60,'try_to_remove_bans',undef);
363 Irssi::signal_add_last('nick mode changed','sig_mode_change');
364 my ($command,$function);
366 while (($command,$function) = each %command_bindings) {
367 Irssi::command_bind($command,$function);