From 7298b237e2a9b5876efcecd981d0477b61d58f38 Mon Sep 17 00:00:00 2001 From: Tom Feist Date: Fri, 2 Dec 2011 11:19:36 +0000 Subject: renamed foreach-guard to molly-guard and added a bunch of extra todo items. It doesn't actually do much of anything yet though. added a bunch of documentation about the /foreach protection mode. Should probably test it first though, since it's been a while. this is actually the other part of the rename of molly-guard. --- foreach-guard/foreach-guard.pl | 208 ---------------------------------- molly-guard/molly_guard.pl | 247 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 247 insertions(+), 208 deletions(-) delete mode 100644 foreach-guard/foreach-guard.pl create mode 100644 molly-guard/molly_guard.pl diff --git a/foreach-guard/foreach-guard.pl b/foreach-guard/foreach-guard.pl deleted file mode 100644 index bcebef6..0000000 --- a/foreach-guard/foreach-guard.pl +++ /dev/null @@ -1,208 +0,0 @@ -=pod - -=head1 NAME - -foreach-guard.pl - confirm that you really mean to send a (non-)command to all channels. - -=head1 DESCRIPTION - - - -=head1 INSTALLATION - -Copy into your F<~/.irssi/scripts/> directory and load with -C>. - -=head1 USAGE - -None, since it doesn't actually do anything. - -=head1 AUTHORS - -Copyright E 2011 Tom Feist Cshabble+irssi@metavore.orgE> - -=head1 LICENCE - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -=head1 BUGS - -=head1 TODO - -Use this template to make an actual script. - -=cut - -use strict; -use warnings; - -use Irssi; -use Irssi::Irc; -use Irssi::TextUI; - -use Data::Dumper; - -our $VERSION = '0.1'; -our %IRSSI = ( - authors => 'shabble', - contact => 'shabble+irssi@metavore.org', - name => 'foreach-guard', - description => '', - license => 'MIT', - updated => '$DATE' - ); - -my $CMD = '_foreach_safe'; -my $DEBUG = 0; -my $cmdchars; -my $match_cmdchars; -my $cmd_confirm_list; -my $txt_confirm_list; - -sub init { - Irssi::settings_add_bool('foreach_guard', - 'foreach_guard_debug', - 0); - Irssi::settings_add_str ('foreach_guard', - 'foreach_guard_confirm_commands', - ''); - Irssi::settings_add_str ('foreach_guard', - 'foreach_guard_confirm_text', - ''); - - Irssi::theme_register([ - 'script_loaded', - '%R>>%n %_Scriptinfo:%_ Loaded {hilight $0} v$1 by $2.' - ]); - Irssi::signal_add ('setup changed', \&sig_setup_changed); - Irssi::signal_add_first('complete word', \&sig_complete_word); - - # override the default foreach with an alias pointing to our command. - Irssi::command("^alias foreach $CMD"); - - sig_setup_changed(); - - Irssi::command_bind($CMD, \&cmd_foreach_safe_dispatch); - for my $target (qw/channel query server window/) { - my $coderef = __PACKAGE__->can("cmd${CMD}_$target"); - die "Cannot aquire coderef for $target" unless ref($coderef) eq 'CODE'; - Irssi::command_bind("$CMD $target", $coderef); - } - Irssi::printformat(MSGLEVEL_CLIENTCRAP, - 'script_loaded', $IRSSI{name}, $VERSION, $IRSSI{authors}); -} - -sub sig_setup_changed { - $DEBUG = Irssi::settings_get_bool('foreach_guard_debug'); - _debug("settings changed"); - $cmdchars = Irssi::settings_get_str('cmdchars'); - my $tmp = join('|', map { quotemeta } split('', $cmdchars)); - _debug("tmp: $tmp"); - $match_cmdchars = qr/^($tmp)(\S*)/; - _debug("Match cmdchars set to: %s", $match_cmdchars); - - $cmd_confirm_check = { map { $_ => 1 } split /\s+/, - Irssi::settings_get_str('foreach_guard_confirm_commands') }; - - $txt_confirm_check = { map { $_ => 1 } split /\s+/, - Irssi::settings_get_str('foreach_guard_confirm_text') }; - -} - -sub sig_complete_word { - my ($strings, $window, $word, $line_start, $want_space) = @_; - _debug("Tab complete called with: " . join(", ", @_)); - return unless - $line_start =~ m/foreach (channel|query|server|window)/; - - my $target = $1; - _debug("target: $1, word: $word"); - - if ($word =~ qr/$match_cmdchars/i) { - my ($cmdchar, $cmd_prefix) = ($1, $2); - _debug("completion char: $cmdchar, Prefix: $cmd_prefix"); - - my @commands - = grep { m/^\Q${cmdchar}${cmd_prefix}/ } - map { $cmdchar . $_->{cmd} } Irssi::commands; - - _debug("Matching Commands: " . join(", ", @commands)); - @$strings = @commands; - $$want_space = 1; - - Irssi::signal_stop(); - } else { - return; - } -} - -sub cmd_foreach_safe_dispatch { - my ($input, $server, $witem) = @_; - - if ($witem) { - $witem->print("W: foreach_safe called with $input"); - } else { - print("foreach_safe called with $input"); - } - #$data =~ s/\s+$//g; # strip trailing whitespace. - Irssi::command_runsub($CMD, $input, $server, $witem); -} - -sub require_confirmation { - my ($target, $input) = @_; - if (_is_command($input)) { - return exists $cmd_confirm_check->{$target}; - } else { - return exists $txt_confirm_check->{$target}; - } -} - - -sub cmd_foreach_safe_channel { - my ($args, $server, $witem) = @_; - -} - -sub cmd_foreach_safe_query { - my ($args, $server, $witem) = @_; - -} -sub cmd_foreach_safe_server { - my ($args, $server, $witem) = @_; - -} -sub cmd_foreach_safe_window { - my ($args, $server, $witem) = @_; - -} - -sub _is_command { - my ($string) = @_; - return m/$match_cmdchars/; -} - -sub _debug { - return unless $DEBUG; - my ($msg, @args) = @_; - - my $str = sprintf("DBG: $msg", @args); - print($str); -} - -init(); diff --git a/molly-guard/molly_guard.pl b/molly-guard/molly_guard.pl new file mode 100644 index 0000000..fb45142 --- /dev/null +++ b/molly-guard/molly_guard.pl @@ -0,0 +1,247 @@ +=pod + +=head1 NAME + +molly_guard.pl - confirm that you really mean to do things that could have +potentially dangerous (or embarassing) effects. + +=head1 DESCRIPTION + +Named after the plastic cover installed over some Big Red Switches With +Major Consequences. See L. +Attempts to stop you shooting yourself in the foot, face, or other body-part +during normal day-to-day use of Irssi. + +By default, it protects you from the following potential mishaps: + +=over 4 + +=item * C [not a command]> + +C will pass I to every I specified. Usually, +this is used to, for example, run a command in every window. A common mistake is +not including a command char such as C in your I, in which case I +is sent as text to every window or channel it can be. This is almost always bad. + +=item * I + +=back + +=head1 INSTALLATION + +Copy into your F<~/.irssi/scripts/> directory and load with +C>. + +=head1 USAGE + +None, since it doesn't actually do anything. + +=head1 AUTHORS + +Copyright E 2011 Tom Feist Cshabble+irssi@metavore.orgE> + +=head1 LICENCE + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +=head1 BUGS + +=head1 TODO + +=over 4 + +=item * Commands to protect: + +=over 4 + +=item * C + +=item * C (in cases where C is probably meant) + +=item * C (way too easy to tab-complete instead of /UPTIME) + +=item * C, C (obvious) + +=item * ... + +=back + +=back + +=cut + +use strict; +use warnings; + +use Irssi; +use Irssi::Irc; +use Irssi::TextUI; + +use Data::Dumper; + +our $VERSION = '0.2'; +our %IRSSI = ( + authors => 'shabble', + contact => 'shabble+irssi@metavore.org', + name => 'molly_guard', + description => 'A script to protect users from accidentally invoking' + . ' commands which may perform undesired actions. See ' + . 'http://catb.org/jargon/html/M/molly-guard.html', + license => 'MIT', + updated => '$DATE' + ); + +my $CMD = '_foreach_safe'; +my $DEBUG = 0; +my $cmdchars; +my $match_cmdchars; +my $cmd_confirm_list; +my $txt_confirm_list; + +sub init { + Irssi::settings_add_bool('molly_guard', + 'molly_guard_debug', + 0); + Irssi::settings_add_str ('molly_guard', + 'molly_guard_confirm_commands', + ''); + Irssi::settings_add_str ('molly_guard', + 'molly_guard_confirm_text', + ''); + + Irssi::theme_register([ + 'script_loaded', + '%R>>%n %_Scriptinfo:%_ Loaded {hilight $0} v$1 by $2.' + ]); + Irssi::signal_add ('setup changed', \&sig_setup_changed); + Irssi::signal_add_first('complete word', \&sig_complete_word); + + # override the default foreach with an alias pointing to our command. + Irssi::command("^alias foreach $CMD"); + + sig_setup_changed(); + + Irssi::command_bind($CMD, \&cmd_foreach_safe_dispatch); + for my $target (qw/channel query server window/) { + my $coderef = __PACKAGE__->can("cmd${CMD}_$target"); + die "Cannot aquire coderef for $target" unless ref($coderef) eq 'CODE'; + Irssi::command_bind("$CMD $target", $coderef); + } + Irssi::printformat(MSGLEVEL_CLIENTCRAP, + 'script_loaded', $IRSSI{name}, $VERSION, $IRSSI{authors}); +} + +sub sig_setup_changed { + $DEBUG = Irssi::settings_get_bool('molly_guard_debug'); + _debug("settings changed"); + $cmdchars = Irssi::settings_get_str('cmdchars'); + my $tmp = join('|', map { quotemeta } split('', $cmdchars)); + _debug("tmp: $tmp"); + $match_cmdchars = qr/^($tmp)(\S*)/; + _debug("Match cmdchars set to: %s", $match_cmdchars); + + $cmd_confirm_check = { map { $_ => 1 } split /\s+/, + Irssi::settings_get_str('molly_guard_confirm_commands') }; + + $txt_confirm_check = { map { $_ => 1 } split /\s+/, + Irssi::settings_get_str('molly_guard_confirm_text') }; + +} + +sub sig_complete_word { + my ($strings, $window, $word, $line_start, $want_space) = @_; + _debug("Tab complete called with: " . join(", ", @_)); + return unless + $line_start =~ m/foreach (channel|query|server|window)/; + + my $target = $1; + _debug("target: $1, word: $word"); + + if ($word =~ qr/$match_cmdchars/i) { + my ($cmdchar, $cmd_prefix) = ($1, $2); + _debug("completion char: $cmdchar, Prefix: $cmd_prefix"); + + my @commands + = grep { m/^\Q${cmdchar}${cmd_prefix}/ } + map { $cmdchar . $_->{cmd} } Irssi::commands; + + _debug("Matching Commands: " . join(", ", @commands)); + @$strings = @commands; + $$want_space = 1; + + Irssi::signal_stop(); + } else { + return; + } +} + +sub cmd_foreach_safe_dispatch { + my ($input, $server, $witem) = @_; + + if ($witem) { + $witem->print("W: foreach_safe called with $input"); + } else { + print("foreach_safe called with $input"); + } + #$data =~ s/\s+$//g; # strip trailing whitespace. + Irssi::command_runsub($CMD, $input, $server, $witem); +} + +sub require_confirmation { + my ($target, $input) = @_; + if (_is_command($input)) { + return exists $cmd_confirm_check->{$target}; + } else { + return exists $txt_confirm_check->{$target}; + } +} + + +sub cmd_foreach_safe_channel { + my ($args, $server, $witem) = @_; + +} + +sub cmd_foreach_safe_query { + my ($args, $server, $witem) = @_; + +} +sub cmd_foreach_safe_server { + my ($args, $server, $witem) = @_; + +} +sub cmd_foreach_safe_window { + my ($args, $server, $witem) = @_; + +} + +sub _is_command { + my ($string) = @_; + return m/$match_cmdchars/; +} + +sub _debug { + return unless $DEBUG; + my ($msg, @args) = @_; + + my $str = sprintf("DBG: $msg", @args); + print($str); +} + +init(); -- cgit v1.2.3