#!/usr/bin/perl # Quickstart: # /run colors_per_channel.pl # ...or, copy/link to your ~/.irssi/scripts/autorun # To strip colors from public messages in #badchannel and #otherchannel, # and from private messages from user badnick: # /set no_color_from #badchannel badnick #otherchannel # no_color_from is a space-separated list of channels and/or nicks # you want to strip colors from. # use #channel, &channel, or nick (with no prefix) use warnings; use strict; use Irssi qw/ settings_add_str settings_add_bool settings_add_int settings_get_str settings_get_bool settings_get_int settings_set_str settings_set_bool settings_set_int command command_bind command_unbind signal_emit signal_add_last timeout_add timeout_remove/; our $VERSION = '0.1'; our %IRSSI = ( authors => 'Urchlay', contact => 'Urchlay on NewNet', name => 'urlmanager', description => 'Selectively strip colors in text from certain channels/nicks', license => 'Same as Perl', url => 'none', ); # pub_check_color dynamically changes the hide_colors setting, based on the # nick/channel the message came from. sub pub_check_color { my (undef, undef, $nick, undef, $channel) = @_; # If there's no channel, it means we were called in response to a # "message private" signal (so we should match the nick instead). $channel = $nick if not $channel; # Splitting the setting up every time is sub-optimal: it would be # better to do it once at script load and on signal "setup changed", # but this way is easier to code and understand... and split() isn't # really all that slow. my @list = split " ", settings_get_str('no_color_from'); my $hide = grep { $channel =~ /$_/i } @list; # only change setting if it should be changed. An optimization, since # who knows how many other scripts are loaded and listening to the # "setup changed" signal... if($hide != settings_get_bool('hide_colors')) { settings_set_bool('hide_colors', $hide); signal_emit('setup changed'); } } # Thanks to tarbo on freenode/#irssi for the idea behind this bit... # It allows us to save the original value of hide_colors at script load, # and restore it at script unload. I've commented it in "tutorial mode" # because I think it's an important technique for irssi scripts. # Note to scripters reading this: I'm only saving/restoring one value, # so $old_value is a scalar... but it could just as well be a # hash or an array, if you need to save/restore multiple settings. # Alternately, you could use multiple scalar values... though in that # case, you do NOT need to bless them all in step 3 (just pick one # and bless only it). # Step 1: save old setting (happens at script load) in a variable with # file scope (aka an "our" variable) our $old_value = settings_get_bool('hide_colors'); # Step 2: define DESTROY sub to restore the old setting. Actually this # is an object method that perl automatically calls on any object when it # goes out of scope (see "perldoc perltoot"). If your script does things # like open sockets/files, you could close them in DESTROY as well. sub DESTROY { settings_set_bool('hide_colors', $old_value); signal_emit('setup changed'); } # Step 3: bless the setting into the current package. Doing this will # cause irssi to call the DESTROY sub (method, actually) when $old_value # goes out of scope... which will happen at script unload! # Remember, if you're saving/restoring multiple values in their own # scalars, you only bless ONE of them (otherwise, DESTROY() will get # called multiple times). # An interesting philosophical question: does the use of "bless" mean # this script is object-oriented or not? Discuss :) bless \$old_value, __PACKAGE__; # Rest of script is standard irssi stuff... settings_add_str('colors_per_channel', 'no_color_from', ''); signal_add_last('message public', \&pub_check_color); signal_add_last('message private', \&pub_check_color);