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
|
=head1 perlalias.pl - Perl-based command aliases for irssi
This script provides an /alias-like function that uses small pieces of perl code to carry out the commands.
=head2 Usage
Install into irssi script directory and /run perlalias and/or put into autorun.
=head2 Commands
=over
=item /perlalias
Syntax: /perlalias [[[-]<alias>] [<code>]]
Parameters: A name of the alias and the perl code to execute.
If you prepend the alias with -, it will remove the alias.
If you give no arguments, the list of defined aliases will be displayed.
Description:
Creates or updates an alias. Like any perl code, multiple statements must be separated using ; characters.
No replacement of parameter values is done: any $text is a perl variable.
The arguments given to the /alias when typed are put into $_ and are also split on whitespace and put into @_.
In addition, the variables $server and $witem will refer to the active server and window item respectively.
Examples:
/PERLALIAS UNACT foreach my $w (Irssi::windows) { $w->activity(0); }
=back
=over
=item /perlunalias
Syntax: /perlunalias <alias>
Parameters: The alias to remove.
Description:
Removes the given alias.
=back
Additionally, all aliases added are linked to perlalias.pl: if it is unloaded, the aliases will be removed.
Aliases can be saved and reloaded with the usual /save and /reload (including autosave). Saved aliases are loaded at script load.
=head2 ChangeLog
=over
=item 1.0
First version.
=back
=cut
use strict;
use warnings FATAL => qw(all);
use Irssi;
use Irssi::Irc;
use Carp ();
#use Cwd;
use POSIX qw(strftime);
{ package Irssi::Nick; } # Keeps trying to look for this package but for some reason it doesn't get loaded.
our $VERSION = '1.2';
our %IRSSI = (
authors => 'aquanight',
contact => 'aquanight@gmail.com',
name => 'perlalias',
description => 'Quickly create commands from short perl blocks',
license => 'public domain'
);
# Bound commands
my %cmds; # Contains command entries. The entry has three items:
# textcmd => Plaintext of the command to execute, which is used for loading/saving
# cmpcmd => Compiled command, for executing.
# tag => Our tag which we need to remove the command
# Package we execute all the commands within, to keep them away from our bits.
package Irssi::Script::perlalias::aliaspkg {
}
sub DESTROY {
Symbol::delete_package("Irssi::Script::perlalias::aliaspkg::");
}
# Alias executor
sub exec_perlalias {
my ($cmd, $data, $server, $witem) = @_;
exists $cmds{$cmd} or return;
defined $cmds{$cmd}->{cmpcmd} or return;
local $_ = $data;
$cmds{$cmd}->{cmpcmd}->($server, $witem, split / +/, $data);
}
# Bind a command
sub setup_command {
my ($cmd, $data) = @_;
# Compile the script.
my $code = qq{package Irssi::Scripts::perlalias::aliaspkg;\nno warnings;\nsub {my \$server = shift; my \$witem = shift;\n#line 1 "perlalias $cmd"\n$data}\n};
my $proc = eval $code;
if ($@) {
Irssi::printformat(MSGLEVEL_CLIENTERROR, perlalias_compile_error => $cmd);
Irssi::print(MSGLEVEL_CLIENTERROR, $@);
return "";
}
if (exists($cmds{$cmd})) {
my $entry = $cmds{$cmd};
$entry->{textcmd} = $data;
$entry->{cmpcmd} = $proc;
}
else {
my $entry = {};
my $tag = sub { exec_perlalias $cmd, @_; };
foreach my $existing_cmd (Irssi::commands()) {
if ($existing_cmd->{cmd} eq $cmd) {
Irssi::print_format(MSGLEVEL_CLIENTERROR, perlalias_cmd_in_use => $cmd);
return "";
}
}
$entry->{textcmd} = $data;
$entry->{cmpcmd} = $proc;
$entry->{tag} = sub { exec_perlalias $cmd, @_; };
Irssi::command_bind($cmd, $entry->{tag});
$cmds{$cmd} = $entry;
}
return 1;
}
sub remove_command {
my ($cmd) = @_;
if (exists($cmds{$cmd})) {
my $entry = $cmds{$cmd};
$entry->{tag}//die "Missing the tag we need to remove the alias!!!";
Irssi::command_unbind($cmd, $entry->{tag});
delete $cmds{$cmd};
return 1;
}
else {
Irssi::printformat(MSGLEVEL_CLIENTERROR, perlalias_not_found => $cmd);
return "";
}
}
sub list_commands {
my ($prefix) = @_;
my @whichones = sort grep /^\Q$prefix\E/, keys %cmds;
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'perlaliaslist_header');
for my $name (@whichones) {
my $entry = $cmds{$name};
Irssi::printformat(MSGLEVEL_CLIENTCRAP, perlaliaslist_line => $name, $entry->{textcmd});
}
}
sub cmd_perlalias {
my ($data, $server, $witem) = @_;
my ($command, $script) = split /\s+/, $data, 2;
if (($command//"") eq "") {
list_commands "";
}
elsif ($command =~ m/^-/) {
$command = substr($command, 1);
if (remove_command($command)) { Irssi::printformat(MSGLEVEL_CLIENTNOTICE, perlalias_removed => $command); }
}
elsif (($script//"") eq "") {
list_commands $command;
}
else {
if (setup_command($command, $script)) { Irssi::printformat(MSGLEVEL_CLIENTNOTICE, perlalias_added => $command); }
}
}
sub cmd_perlunalias {
my ($data, $server, $witem) = @_;
if (remove_command $data) { Irssi::printformat(MSGLEVEL_CLIENTNOTICE, perlalias_removed => $data); }
}
sub sig_setup_saved {
my ($main, $auto) = @_;
my $file = Irssi::get_irssi_dir() . "/perlalias";
open my $fd, '>', $file or return;
for my $cmd (keys %cmds) {
my $entry = $cmds{$cmd};
printf $fd "%s\t%s\n", $cmd, $entry->{textcmd};
}
close $fd;
}
sub sig_setup_reread {
my $file = Irssi::get_irssi_dir() . "/perlalias";
open my $fd, "<", $file or return;
my $ln;
my %newcmds;
while (defined($ln = <$fd>)) {
chomp $ln;
my ($cmd, $script) = split /\t/, $ln, 2;
if (exists $newcmds{$cmd}) {
Irssi::print(MSGLEVEL_CLIENTERROR, "There is a duplicate record in the PerlAlias save file.");
Irssi::print(MSGLEVEL_CLIENTERROR, "Offending alias: $cmd");
Irssi::print(MSGLEVEL_CLIENTERROR, "Previous definition: " . $newcmds{$cmd});
Irssi::print(MSGLEVEL_CLIENTERROR, "Duplicate definition: $script");
}
$newcmds{$cmd} = $script;
}
# Scrub the existing list. Update existings, remove any that aren't in the config, then we'll add any that's new.
my @currentcmds = keys %cmds;
for my $cmd (@currentcmds) {
if (exists $newcmds{$cmd}) {
setup_command($cmd, $newcmds{$cmd});
}
else {
remove_command($cmd);
}
delete $newcmds{$cmd};
}
# By this point all that should be in newcmds is any ... new commands.
for my $cmd (keys %newcmds) {
setup_command($cmd, $newcmds{$cmd});
}
close $fd;
}
sub sig_complete_perlalias {
my ($lst, $win, $word, $line, $want_space) = @_;
$word//return;
$line//return;
$lst//return;
if ($line ne '') {
my $def = $cmds{$line};
$def//return;
push @$lst, $def->{textcmd};
Irssi::signal_stop();
}
else {
push @$lst, (grep /^\Q$word\E/i, keys %cmds);
Irssi::signal_stop();
}
}
sub sig_complete_perlunalias {
my ($lst, $win, $word, $line, $want_space) = @_;
$lst//return;
$word//return;
push @$lst, (grep /^\Q$word\E/i, keys %cmds);
}
Irssi::signal_register({"complete command " => [qw(glistptr_char* Irssi::UI::Window string string intptr)]});
Irssi::signal_add("complete command perlalias" => \&sig_complete_perlalias);
Irssi::signal_add("complete command perlunalias" => \&sig_complete_perlunalias);
Irssi::signal_add("setup saved" => \&sig_setup_saved);
Irssi::signal_add("setup reread" => \&sig_setup_reread);
Irssi::command_bind(perlalias => \&cmd_perlalias);
Irssi::command_bind(perlunalias => \&cmd_perlunalias);
my %formats = (
# $0 Name of alias
'perlalias_compile_error' => '{error Error compiling alias {hilight $0}:}',
# $0 Name of alias
'perlalias_exec_error' => '{error Error executing alias {hilight $0}:}',
'perlalias_cmd_in_use' => 'Command {hilight $0} is already in use',
'perlalias_added' => 'PerlAlias {hilight $0} added',
'perlalias_removed' => 'PerlAlias {hilight $0} removed',
'perlalias_not_found' => 'PerlAlias {hilight $0} not found',
'perlaliaslist_header' => '%#PerlAliases:',
# $0 Name of alias, $1 alias text
'perlaliaslist_line' => '%#$[10]0 $1',
);
Irssi::theme_register([%formats]);
sig_setup_reread;
|