1#!/usr/local/bin/perl 2use strict; 3use warnings; 4 5BEGIN { 6 unless (exists $::{"Irssi::"}) { 7 require Pod::Usage; 8 Pod::Usage::pod2usage(-verbose => 2); 9 } 10} 11 12use Irssi; 13our $VERSION = '0.05'; 14our %IRSSI = ( 15 authors => 'aluser', 16 name => 'autovoice', 17 description => 'autovoice', 18 license => 'GPL', 19 ); 20 21our %bad; 22 23=head1 SYNOPSIS 24 25 /script load autovoice 26 /autovoice add #somechannel 27Idle on #somechannel as [half]op, and you will voice people :) 28 29=head1 MOTIVATION 30 31This is certainly not a new concept, but I dislike many implementations of autovoicing because they are not as intelligent as they could be. Blindly voicing everyone who joins your channel is dumb, because it removes the protection that +m is supposed to give you. A troublemake need merely to rejoin the channel to get his voice back. You probably want to voice newcomers to your channel, so a password or hostmask system is no good. Besides, it's intuitive that anybody leaving the channel without voice and quickly rejoining is trying to leverage your autovoicer! So, the main purpose of this script is to automatically detect these people and not voice them. 32 33The other important feature is fine-grained control over where you voice people. You might want to autovoice in efnet #foo but not in dalnet #foo. The C</autovoice add> command gives you C<-server> and C<-ircnet> options to control on which channels you will autovoice, even if the channels have identical names. 34 35I still consider this script to be lightly tested, but I do hope that it is well documented enough that it can be debugged well. 36 37=head1 INSTALL 38 39Just place this script in F<~/.irssi/scripts>. To have it load automatically when you start Irssi, do this: 40 41 mkdir -p ~/.irssi/scripts/autorun 42 ln -s ../autovoice.pl ~/.irssi/scripts/autorun/ 43 44If you haven't figured it out yet, you can run the script outside of Irssi to get a man page type document, like this: 45 46 chmod +x autovoice.pl 47 ./autovoice.pl 48 49=head1 COMMANDS 50 51=over 52 53=item I</autovoice add> 54 55This is a helper to add channels to L<autovoice_channels> for you. 56I'm going to explain this by example: 57 58 /autovoice add #channelfoo 59 /autovoice add -server irc.foo.com #barbarfoo 60 /autovoice add -ircnet EFNet #perlhelp 61 /autovoice add -server irc.efnet.org -ircnet Undernet #irssi 62 63Note that the last example actually adds two "channels" to the setting, both named #irssi. The channel will be valid on Undernet or the server irc.efnet.org. 64 65=item I</autovoice remove> 66 67This is a helper to remove channels from L<autovoice_channels> for you. 68Example: 69 70 /autovoice remove #somechannel 71 /autovoice remove #channel1 #channel2 72 73=item I</autovoice dump> 74 75Mostly for debugging, this dumps the perl hash containing blacklisted nicks to your screen. 76 77=item I</autovoice flush> 78 79Flush the blacklists. 80 81=back 82 83=head1 SETTINGS 84 85=over 86 87=item bool I<autovoice> = ON 88 89Set autovoicing on or off. 90 91=item string I<autovoice_channels> = 92 93Control which channels we will autovoice. The simplest form is 94 95 #channel1 , #channel2 , #channel3 96 97Space before the commas is mandatory; after is optional. For any channel in the list, you may specify a chatnet or a server like this: 98 99 #channel1 , #channel2 =>SOMECHATNET , #channel3 @some.server.com 100 101Space after the channels and before the C<< => >> or C<@> is required. Space after the C<< => >> or C<@> is optional. (not shown) 102 103See L</autovoice add> and L</autovoice remove> for wrappers to this. 104 105=item int I<autovoice_cycletime> = 600 106 107Control the amount of time, in seconds, for which we remember that a nick left a channel without voice. 108 109=item bool I<autovoice_voice_ops> = OFF 110 111Whether or not to give voice to people who already have op or halfop 112 113=item bool I<autovoice_use_ident> = OFF 114 115Whether to distinguish between nicks which have the same host but different user names. (nick![ident@host] vs nick!ident@[host]) 116 117=back 118 119=cut 120 121 122=head1 BUGS 123 124Plenty. 125 126=over 127 128=item 129 130&add will add duplicate channels 131 132=item 133 134Error checking in &add is weak. 135 136=item 137 138Setting L<autovoice_use_ident> causes the existing blacklists to be ineffective. 139 140=item 141 142C<parse_channels> and C<deparse_channels> mix up the ordering of the channels in the autovoice_channels setting. This is a property of the hash used to represent the setting. 143 144=item 145 146remove doesn't let you remove only one channel when several use the same name. 147 148=item 149 150Setting L<autovoice_cycletime> does not change the timing for entries already in the badlist, only for entries made after the setting is changed. As far as I can tell, the alternatives are to A) Have a potentially ever-growing %bad, or B) to run a cleanup on a timer which must traverse all of %bad. 151 152=back 153 154=cut 155 156 157=head1 HACKING 158 159This section is for people interested in tweaking/fixing/improving/developing/hacking this script. It describes every subroutine and data structure in the script. If you do not know Perl you should stop reading here. 160 161Variables ending in C<_rec> are Irssi objects of some sort. I also use C<_text> to indicate normal strings. 162 163=head2 DATA STRUCTURES 164 165=over 166 167=item %bad 168 169This hash holds the badlists for all channels. Each key is a tag as supplied by C<$server_rec->{tag}>. Each value is a hash reference as follows: 170 171Each key is a lowercased channel name as given by C<< lc $channel_rec->{name} >> . Each value is a hash referenc described as follows: 172 173Each key is a lowercased host. If the host was marked bad while autovoice_use_ident was set, it is in the form "username@host.com". If not, it is just "host.com". Each value is a tag as returned by C<Irssi::timeout_add>. This is used to remove the callback which is planning to remove the entry from the badlist after autovoice_cycletime expires. 174 175=item %commands 176 177This hash holds the commands invoked as C<< /autovoice <command> [ arg1 arg2 ... ] >>. Each key is the lowercased name of a command, and each value is a reference to a subroutine. The subroutine should expect an Irssi Server object, a WindowItem object, and a list of user supplied arguments. The case of the arguments is left as supplied by the user. 178 179=back 180 181=cut 182 183=head2 SUBROUTINES 184 185=over 186 187=item I<massjoin($channel_rec, $nicks_ray)> 188 189The nicks in the array referenced by $nicks_ray are joining $channel_rec. This is an irssi signal handler. 190 191=cut 192 193sub massjoin { 194 my ($channel_rec, $nicks_ray) = @_; 195 voicem($channel_rec, @$nicks_ray); 196} 197 198=item I<message_part($server_rec, $channel_text, $nick_text, $addr, $reason)> 199 200A nick is parting a channel. $addr and $reason are not used. This is an irssi signal handler. 201 202=cut 203 204sub message_part { 205 my ($server_rec, $channel_text, $nick_text) = @_; 206 #Irssi::print("chan: $channel_text, nick: $nick_text"); 207 #return unless defined $nick_text; # happens if the part was us 208 no warnings; 209 my $channel_rec = $server_rec->channel_find($channel_text); 210 use warnings; 211 return unless defined $channel_rec; 212 my $nick_rec = $channel_rec->nick_find($nick_text); 213 partem($channel_rec, $nick_rec); 214} 215 216=item I<message_quit($server_rec, $nick_text, $addr, $reason)> 217 218A nick is quiting the server. $addr and $reason are not used. This is an irssi signal handler. 219 220=cut 221 222sub message_quit { 223 my ($server_rec, $nick_text, $addr, $reason) = @_; 224 my $chanstring = $server_rec->get_channels(); 225 $chanstring =~ s/ .*//; #strip channel keys 226 my @channels_text = split /,/, $chanstring; 227 no warnings; 228 my @channels_rec = 229 map { $server_rec->channel_find($_) } @channels_text; 230 use warnings; 231 for (@channels_rec) { 232 my $nick_rec = $_->nick_find($nick_text); 233 if (defined $nick_rec) { 234 partem($_, $nick_rec); 235 } 236 } 237} 238 239=item I<message_kick($server_rec, $channel_text, $nick_text, $addr, $reason)> 240 241Called when a nick is kicked from a channel. This is an Irssi signal handler. 242 243=cut 244 245sub message_kick { 246 my ($server_rec, $channel_text, $nick_text) = @_; 247 my $channel_rec = $server_rec->channel_find($channel_text); 248 return unless defined $channel_rec; 249 my $nick_rec = $channel_rec->nick_find($nick_text); 250 partem($channel_rec, $nick_rec); 251} 252 253=item I<voicem($channel_rec, @nicks)> 254 255This voices all of @nicks on $channel_rec, provided they aren't in the blacklist. 256 257=cut 258 259sub voicem { 260 my ($channel_rec, @nicks) = @_; 261 if (is_auto($channel_rec)) { 262 for my $nick_rec (@nicks) { 263 unless (is_bad($channel_rec, $nick_rec) 264 or $nick_rec->{voice}) { 265 if (get_voiceops() or 266 !($nick_rec->{op} or $nick_rec->{halfop})) { 267 my $nick_text = $nick_rec->{nick}; 268 $channel_rec->command("voice $nick_text"); 269 } 270 } 271 } 272 } 273} 274 275=item I<partem($channel_rec, $nick_rec)> 276 277Called when a nick is leaving a channel, by any means. This is what decides whether the nick does or does not have voice. 278 279=cut 280 281sub partem { 282 my ($channel_rec, $nick_rec) = @_; 283 #$channel_rec->print("partem called"); 284 if (is_auto($channel_rec)) { 285 #$channel_rec->print("this channel is autovoiced."); 286 if (not $nick_rec->{voice} and 287 not $nick_rec->{op} and 288 not $nick_rec->{halfop}) { 289 #$channel_rec->print("nick leaving with no voice"); 290 make_bad($channel_rec, $nick_rec); 291 } else { 292 make_unbad($channel_rec, $nick_rec); 293 } 294 } 295} 296 297=item I<is_bad($channel_rec, $nick_rec)> 298 299Returns 1 if $nick_rec is blacklisted on $channel_rec, 0 otherwise. 300 301=cut 302 303sub is_bad { 304 my ($channel_rec, $nick_rec) = @_; 305 my $server_tag = $channel_rec->{server}->{tag}; 306 my $channel_text = lc $channel_rec->{name}; 307 my $host_text = lc $nick_rec->{host}; 308 if (not get_useident()) { 309 $host_text =~ s/.*?\@//; 310 } 311 #$channel_rec->print("calling is_bad {$server_tag}{$channel_text}{$host_text}"); 312 return 313 exists $bad{$server_tag} && 314 exists $bad{$server_tag}{$channel_text} && 315 exists $bad{$server_tag}{$channel_text}{$host_text}; 316} 317 318=item I<make_bad($channel_rec, $nick_rec)> 319 320Blacklist $nick_rec on $channel_rec for autovoice_cycletime seconds. 321 322=cut 323 324sub make_bad { 325 my ($channel_rec, $nick_rec) = @_; 326 my $tag = $channel_rec->{server}->{tag}; 327 my $channel_text = lc $channel_rec->{name}; 328 my $host_text = lc $nick_rec->{host}; 329 if (not get_useident()) { 330 $host_text =~ s/.*?\@//; 331 } 332 #$channel_rec->print("channel_rec: ".ref($channel_rec)."nick_rec: ".ref($nick_rec).". make bad $tag, $channel_text, $host_text"); 333 Irssi::timeout_remove($bad{$tag}{$channel_text}{$host_text}) 334 if exists $bad{$tag}{$channel_text}{$host_text}; 335 $bad{$tag}{$channel_text}{$host_text} = 336 Irssi::timeout_add(get_cycletime(), 337 'timeout', 338 [ $channel_rec, $nick_rec ]); 339} 340 341=item I<timeout([$channel_rec, $nick_rec])> 342 343This is the irssi timeout callback which removes $nick_rec from the blacklist for $channel_rec when autovoice_cycletime seconds have elapsed. make_unbad finds the tag in the badlist in order to keep this from being called again. Note that it only takes one argument, an array ref 344 345=cut 346 347sub timeout { 348 my ($channel_rec, $nick_rec) = @{$_[0]}; 349 #$channel_rec->print("timing out"); 350 make_unbad($channel_rec, $nick_rec); 351} 352 353=item I<make_unbad($channel_rec, $nick_rec)> 354 355Remove $nick_rec from the blacklist for $channel_rec 356 357=cut 358 359sub make_unbad { 360 my ($channel_rec, $nick_rec) = @_; 361 my $tag = $channel_rec->{server}->{tag}; 362 my $channel_text = lc $channel_rec->{name}; 363 my $host_text = lc $nick_rec->{host}; 364 if (not get_useident()) { 365 $host_text =~ s/.*\@//; 366 } 367 if (exists $bad{$tag}{$channel_text}{$host_text}) { 368 Irssi::timeout_remove($bad{$tag}{$channel_text}{$host_text}); 369 delete $bad{$tag}{$channel_text}{$host_text}; 370 if (not keys %{$bad{$tag}{$channel_text}}) { 371 delete $bad{$tag}{$channel_text}; 372 } 373 if (not keys %{$bad{$tag}}) { 374 delete $bad{$tag}; 375 } 376 } 377} 378 379=item I<parse_channels()> 380 381Examine autovoice_channels and return a hash reference. Each key is a channel name, lowercased. Each value is a hash with one to three keys, 'server', 'chatnet', and/or 'plain'. If server, it holds an array ref with all servers on which the channel is autovoice. If chatnet, it holds an array ref with all the chatnets on which the channel is autovoice. If plain, it just has the value 1. 382 383=cut 384 385sub parse_channels { 386 my $chanstring = lc Irssi::settings_get_str('autovoice_channels'); 387 $chanstring =~ s/^\s+//; 388 $chanstring =~ s/\s+$//; 389 my @fields = split /\s+,\s*/, $chanstring; 390 my %hash; 391 keys %hash = scalar @fields; 392 for (@fields) { 393 if (/\s=>/) { 394 my ($channel, $chatnet) = split /\s+=>\s*/, $_, 2; 395 add_channel_to_parsed(\%hash, $channel, $chatnet, undef); 396 } elsif (/\s\@/) { 397 my ($channel, $server) = split /\s+\@\s*/, $_, 2; 398 add_channel_to_parsed(\%hash, $channel, undef, $server); 399 } else { 400 my ($channel) = /(\S+)/; 401 add_channel_to_parsed(\%hash, $channel, undef, undef); 402 } 403 } 404 return \%hash; 405} 406 407=item I<deparse_channels($hashr)> 408 409Take a hash ref like that produced by parse_channels and convert it into a string suitable for autovoice_channels 410 411=cut 412 413sub deparse_channels { 414 my $hashr = shift; 415 my @fields; 416 for my $channel (keys %$hashr) { 417 my $s = $channel; 418 push(@fields, $s) if exists $hashr->{$channel}->{plain}; 419 if (exists $hashr->{$channel}->{server}) { 420 for (@{$hashr->{$channel}->{server}}) { 421 push(@fields, $s.' @ '.$_); 422 } 423 } 424 if (exists $hashr->{$channel}->{chatnet}) { 425 for (@{$hashr->{$channel}->{chatnet}}) { 426 push(@fields, $s.' => '.$_); 427 } 428 } 429 } 430 return join ' , ', @fields; 431} 432 433=item I<is_auto($channel_rec)> 434 435Returns 1 if $channel_rec is an autovoiced channel as defined by autovoice_channels, 0 otherwise. 436 437=cut 438 439sub is_auto { 440 unless (Irssi::settings_get_bool('autovoice')) { 441 return 0; 442 } 443 my $channel_rec = shift; 444 my $channel_text = lc $channel_rec->{name}; 445 my $parsedchannels = parse_channels(); 446 return 0 unless exists $parsedchannels->{$channel_text}; 447 if (exists $parsedchannels->{$channel_text}->{plain}) { 448 return 1; 449 } elsif (exists $parsedchannels->{$channel_text}->{chatnet}) { 450 #Irssi::print("looking at chatnet @{$parsedchannels->{$channel_text}->{chatnet}}"); 451 for (@{$parsedchannels->{$channel_text}->{chatnet}}) { 452 return 1 if $_ eq lc $channel_rec->{server}->{chatnet}; 453 } 454 return 0; 455 } else { 456 for (@{$parsedchannels->{$channel_text}->{server}}) { 457 return 1 if $_ eq lc $channel_rec->{server}->{address}; 458 } 459 return 0; 460 } 461} 462 463our %commands = ( 464 dump => \&dump, 465 add => \&add, 466 remove => \&remove, 467 flush => \&flush, 468 ); 469 470=item I<autovoice_cmd($data, $server, $witem)> 471 472Irssi command handler which dispatches all the /autovoice * commands. Autovoice commands are given ($server_rec, $witem, @args), where @args is the result of split ' ', $data minus the first element ("autovoice"). Note that the case of @args is not changed. 473 474=cut 475 476sub autovoice_cmd { 477 my ($data, $server, $witem) = @_; 478 my ($cmd, @args) = (split ' ', $data); 479 $cmd = lc $cmd; 480 if (exists $commands{$cmd}) { 481 $commands{$cmd}->($server, $witem, @args) 482 } else { 483 Irssi::print("No such command: autovoice $cmd"); 484 } 485} 486 487=item I<dump($server_rec, $witem, @args)> 488 489Invoked as C</autovoice dump>, this C<require>s Data::Dumper and dumps the blacklist hash to the current window. @args and $server_rec are ignored. 490 491=cut 492 493sub dump { 494 require Data::Dumper; 495 my $witem = $_[1]; 496 my $string = Data::Dumper->Dump([\%bad], ['bad']); 497 chomp $string; 498 if ($witem) { 499 $witem->print($string); 500 } else { 501 Irssi::print($string); 502 } 503} 504 505=item I<add($server_rec, $witem, @args)> 506 507Invoked as C</autovoice add (args)>. This adds channels to autovoice_channels. See L</autovoice add> in COMMANDS for usage. 508 509=cut 510 511sub add { 512 my ($server_rec, $witem, @args) = @_; 513 @args = map {lc} @args; 514 my $parsedchannels = parse_channels(); 515 my ($server, $chatnet, $channel); 516 for (my $i = 0; $i < @args; ++$i) { 517 if ($args[$i] eq '-ircnet') { 518 if (defined $chatnet) { 519 Irssi::print("autovoice add: warning: -ircnet given twice, using the second value."); 520 } 521 $chatnet = $args[$i+1]; 522 splice(@args, $i, 1) 523 } elsif ($args[$i] eq '-server') { 524 if (defined $server) { 525 Irssi::print("autovoice add: warning: -server given twice, using the second value."); 526 } 527 $server = $args[$i+1]; 528 splice(@args, $i, 1); 529 } else { 530 if (defined $channel) { 531 Irssi::print("autovoice add: warning: more than one channel specified, using the last one."); 532 } 533 $channel = $args[$i]; 534 $channel = '#'.$channel 535 unless $server_rec->ischannel($channel); 536 } 537 } 538 unless (defined $channel) { 539 Irssi::print("autovoice add: no channel specified"); 540 return; 541 } 542 add_channel_to_parsed($parsedchannels, $channel, $chatnet, $server); 543 Irssi::settings_set_str('autovoice_channels' => 544 deparse_channels($parsedchannels)); 545 if ($witem) { 546 $witem->command("set autovoice_channels"); 547 } else { 548 Irssi::command("set autovoice_channels"); 549 } 550} 551 552=item I<add_channel_to_parsed($parsedchannels, $channel, $chatnet, $server)> 553 554Adds a channel to a hash ref like that returned by &parse_channels. If $chatnet is defined but $server is not, restrict it to the chatnet. If $server is defined but $chatnet is not, restrict it to the server. If both are defined, add to channels, one restricted to the server and the other to the chatnet. (Both with the same name) If neither is defined, do not restrict the channel to a chatnet or server. 555 556=cut 557 558sub add_channel_to_parsed { 559 my ($parsedchannels, $channel, $chatnet, $server) = @_; 560 if (defined $chatnet) { 561 push @{$parsedchannels->{$channel}->{chatnet}}, $chatnet; 562 } 563 if (defined $server) { 564 push @{$parsedchannels->{$channel}->{server}}, $server; 565 } 566 if (not defined($chatnet) and not defined($server)) { 567 $parsedchannels->{$channel}->{plain} = 1; 568 } 569} 570 571=item I<remove($server_rec, $witem, @args)> 572 573Invoked as 574 575 /autovoice remove [-ircnet IRCNET] [-server SERVER] #chan1 [-ircnet IRCNET] [-server SERVER] #chan2 576 577Removes all channels matching those specified. An -ircnet or -server option only applies to the channel following it, and must be specified before its channel name. A channel without -ircnet or -server options removes all channels with that name. 578 579=cut 580 581sub remove { 582 my ($server_rec, $witem, @args) = @_; 583 my %parsedchannels = %{parse_channels()}; 584 my ($wantserver, $wantchatnet, $server, $chatnet); 585 for (@args) { 586 $_ = lc; 587 if ($wantserver) { 588 $wantserver = 0; 589 $server = $_; 590 } elsif ($wantchatnet) { 591 $wantchatnet = 0; 592 $chatnet = $_; 593 } elsif ($_ eq '-server') { 594 $wantserver = 1; 595 } elsif ($_ eq '-ircnet') { 596 $wantchatnet = 1; 597 } elsif (exists $parsedchannels{$_}) { 598 my $chan = $_; 599 if (defined $server and exists $parsedchannels{$chan}{server}) { 600 @{$parsedchannels{$chan}{server}} = grep {$_ ne $server} @{$parsedchannels{$chan}{server}}; 601 } 602 if (defined $chatnet and exists $parsedchannels{$chan}{chatnet}) { 603 @{$parsedchannels{$chan}{chatnet}} = grep {$_ ne $chatnet} @{$parsedchannels{$chan}{chatnet}}; 604 } 605 if (not defined $server and not defined $chatnet) { 606 delete $parsedchannels{$chan}; 607 } else { 608 if (exists $parsedchannels{$chan}{server} and not @{$parsedchannels{$chan}{server}}) { 609 delete $parsedchannels{$chan}{server}; 610 } 611 if (exists $parsedchannels{$chan}{chatnet} and not @{$parsedchannels{$chan}{chatnet}}) { 612 delete $parsedchannels{$chan}{chatnet}; 613 } 614 } 615 } 616 } 617 Irssi::settings_set_str('autovoice_channels' => 618 deparse_channels(\%parsedchannels)); 619 if ($witem) { 620 $witem->command("set autovoice_channels"); 621 } else { 622 Irssi::command("set autovoice_channels"); 623 } 624} 625 626=item I<flush($server_rec, $witem, @args)> 627 628Flush the badlist. 629 630=cut 631 632sub flush { 633 %bad = (); 634} 635 636=item I<get_cycletime()> 637 638Checks autovoice_cycletime and returns the cycle time in milliseconds. 639 640=cut 641 642sub get_cycletime { 643 1000 * Irssi::settings_get_int("autovoice_cycletime"); 644} 645 646=item I<get_voiceops()> 647 648Return the value of autovoice_voice_ops 649 650=cut 651 652sub get_voiceops { 653 Irssi::settings_get_bool("autovoice_voice_ops"); 654} 655 656=item I<get_useident()> 657 658Return the value of autovoice_use_ident 659 660=cut 661 662sub get_useident { 663 Irssi::settings_get_bool("autovoice_use_ident"); 664} 665 666=back 667 668=cut 669 670Irssi::signal_add_first('message part', 'message_part'); 671Irssi::signal_add_first('message quit', 'message_quit'); 672Irssi::signal_add_first('message kick', 'message_kick'); 673Irssi::signal_add_last('massjoin', 'massjoin'); 674Irssi::settings_add_str('autovoice', 'autovoice_channels' => ""); 675Irssi::settings_add_int('autovoice', 'autovoice_cycletime' => 600); 676Irssi::settings_add_bool('autovoice', 'autovoice_voice_ops' => 0); 677Irssi::settings_add_bool('autovoice', 'autovoice_use_ident' => 0); 678Irssi::settings_add_bool('autovoice', 'autovoice' => 1); 679Irssi::command_bind(autovoice => 'autovoice_cmd'); 680