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
|
=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</SCRIPT LOAD F<filename>>.
=head1 USAGE
None, since it doesn't actually do anything.
=head1 AUTHORS
Copyright E<copy> 2011 Tom Feist C<E<lt>shabble+irssi@metavore.orgE<gt>>
=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();
|