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