-
Notifications
You must be signed in to change notification settings - Fork 235
/
matryoshka.pl
336 lines (306 loc) · 6.93 KB
/
matryoshka.pl
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
use strict;
use vars qw($VERSION %IRSSI);
use Irssi;
use Storable qw/freeze thaw/;
use Compress::Zlib;
use Crypt::CBC;
use Crypt::Cipher::AES;
use File::Glob ':bsd_glob';
use CPAN::Meta::YAML;
$VERSION = '0.01';
%IRSSI = (
authors => 'bw1',
name => 'matryoshka',
description => 'a password matryoshka',
license => 'Public Domain',
url => 'https://scripts.irssi.org/',
changed => '2019-12-03',
modules => 'Storable Compress::Zlib Crypt::CBC Crypt::Cipher::AES File::Glob '.
'CPAN::Meta::YAML',
commands=> 'matryoshka',
);
my $help = << "END";
%9Name%9
$IRSSI{name}
%9Version%9
$VERSION
%9description%9
$IRSSI{description}
%9example%9
/matryoshka expando add mynet /msg -mynet nickserv IDENTIFY account password
/network add -autosendcmd '\$mynet' mynet
/matryoshka startup add /connect ircserver port password nick
%9Settings%9
%Umatryoshka_file%U
%Umatryoshka_key%U
%Umatryoshka_key_file%U
%9Commands%9
%Usave%U
%Udump%U
%Uexpando%U
list
add <expando name> <string>
remove <expando name>
update
%Ustartup%U
list
add <command string>
remove <number>
run
%Uhelp%U
%9Warning%9
The passwords are not save in this store!
END
my ($matryoshka_file, $matryoshka_key, $matryoshka_key_file);
my $db;
my $change=0;
my $key;
my %expandos;
my %expandosf;
sub cmd {
my ($args, $server, $witem)=@_;
my @args = split /\s+/,$args;
my $arg = shift @args;
my $cmd=0;
if ( $arg eq 'save') {
$change=1;
savekeyfile();
savefile();
$cmd++;
}
if ( $arg eq 'dump') {
cmd_dump($server, $witem, @args);
$cmd++;
}
if ( $arg eq 'expando' ) {
scmd_expando($server, $witem, @args);
$cmd++;
}
if ( $arg eq 'startup' ) {
scmd_startup($server, $witem, @args);
$cmd++;
}
if ( $arg eq 'help' || $cmd == 0 ) {
Irssi::print($help, MSGLEVEL_CLIENTCRAP);
}
}
sub scmd_startup {
my ($server, $witem, @args)=@_;
my $arg= shift @args;
if ($arg eq 'list') {
sscmd_s_list($server, $witem, @args);
}
if ($arg eq 'add') {
sscmd_s_add($server, $witem, @args);
}
if ($arg eq 'remove') {
sscmd_s_remove($server, $witem, @args);
}
if ($arg eq 'run') {
startup_run();
}
}
sub sscmd_s_list {
my ($server, $witem, @args)=@_;
if (exists $db->{startup} ) {
my $c=1;
foreach (@{$db->{startup}}) {
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'startup_theme',
$c, $_ );
$c++;
}
}
}
sub sscmd_s_add {
my ($server, $witem, @args)=@_;
if (!exists $db->{startup} ) {
$db->{startup}=[];
}
push @{$db->{startup}}, join(" ", @args);
$change=1;
}
sub sscmd_s_remove {
my ($server, $witem, @args)=@_;
my $ex= shift @args;
splice @{$db->{startup}}, $ex-1, 1;
$change=1;
}
sub startup_run {
if (exists $db->{startup} ) {
foreach (@{$db->{startup}}) {
Irssi::command($_);
}
}
}
sub scmd_expando {
my ($server, $witem, @args)=@_;
my $arg= shift @args;
if ($arg eq 'list') {
sscmd_e_list($server, $witem, @args);
}
if ($arg eq 'add') {
sscmd_e_add($server, $witem, @args);
}
if ($arg eq 'remove') {
sscmd_e_remove($server, $witem, @args);
}
if ($arg eq 'update') {
updateexpandos();
}
}
sub sscmd_e_list {
my ($server, $witem, @args)=@_;
if (exists $db->{expando} ) {
foreach (sort keys %{$db->{expando}}) {
Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'expando_theme',
$_, $db->{expando}->{$_});
}
}
}
sub sscmd_e_add {
my ($server, $witem, @args)=@_;
if (!exists $db->{expando} ) {
$db->{expando}={};
}
my $ex= shift @args;
$db->{expando}->{$ex}= join " ", @args;
$change=1;
}
sub sscmd_e_remove {
my ($server, $witem, @args)=@_;
my $ex= shift @args;
delete $db->{expando}->{$ex};
$change=1;
}
sub cmd_dump {
my ($server, $witem, @args)=@_;
my $yml= CPAN::Meta::YAML->new;
push @$yml, $db;
print $yml->write_string;
}
sub cmd_help {
my ($args, $server, $witem)=@_;
$args=~ s/\s+//g;
if ($IRSSI{name} eq $args) {
Irssi::print($help, MSGLEVEL_CLIENTCRAP);
Irssi::signal_stop();
}
}
sub loadfile {
my $fn= filename($matryoshka_file);
if ( -e $fn) {
my ($serial, $comp, $cyper);
open my $fi, '<', $fn;
local $/;
$cyper= <$fi>;
close $fi;
my $cbc2 = Crypt::CBC->new( -cipher=>'Cipher::AES', -key=>$key );
my $cyper2 = $cbc2->decrypt($cyper);
my $cbc = Crypt::CBC->new( -cipher=>'Cipher::AES', -key=>$matryoshka_key );
$comp = $cbc->decrypt($cyper2);
$serial= uncompress( $comp );
$db= thaw($serial);
$change=0;
}
}
sub savefile {
my $fn= filename($matryoshka_file);
my $serial= freeze( $db );
my $comp;
if ($change != 0) {
$comp= compress( $serial, 9 );
my $cbc = Crypt::CBC->new( -cipher=>'Cipher::AES', -key=>$matryoshka_key);
my $ciphertext = $cbc->encrypt($comp);
my $cbc2 = Crypt::CBC->new( -cipher=>'Cipher::AES', -key=>$key);
my $ciphertext2 = $cbc2->encrypt($ciphertext);
open my $fa, '>', $fn;
print $fa $ciphertext2;
close $fa;
}
}
sub loadkeyfile {
my $fn= filename($matryoshka_key_file);
if (-e $fn) {
open my $fi, '<', $fn;
local $/;
$key= <$fi>;
close $fi;
} else {
savekeyfile();
}
}
sub savekeyfile {
my $fn= filename($matryoshka_key_file);
$key= Crypt::CBC->random_bytes(16);
$change=1;
open my $fa, '>', $fn;
print $fa $key;
close $fa;
}
sub filename {
my ($fn)= @_;
if ($fn !~ m#/#) {
$fn= Irssi::get_irssi_dir().'/'.$fn;
} else {
$fn= bsd_glob($fn);
}
return $fn;
}
sub updateexpandos {
foreach (keys %{$db->{expando}}) {
if (!defined $expandos{$_}) {
my $fustr = '$expandosf{'.$_.'}= sub { return "'. $db->{expando}->{$_}.'";}';
eval($fustr);
Irssi::expando_create $_, $expandosf{$_}, { };
$expandos{$_}=2;
} else {
$expandos{$_}=2;
}
}
foreach (keys %expandos) {
if ( $expandos{$_} == 1) {
delete $expandos{$_};
Irssi::expando_destroy $_;
} else {
$expandos{$_}=1;
}
}
}
sub sig_setup_changed {
$matryoshka_file= Irssi::settings_get_str($IRSSI{name}.'_file');
$matryoshka_key= Irssi::settings_get_str($IRSSI{name}.'_key');
if ( $matryoshka_key eq '' ) {
$matryoshka_key= Crypt::CBC->random_bytes(16);
Irssi::settings_set_str($IRSSI{name}.'_key', $matryoshka_key);
$change=1;
}
$matryoshka_key_file= Irssi::settings_get_str($IRSSI{name}.'_key_file');
}
sub UNLOAD {
savefile();
}
Irssi::theme_register([
'expando_theme', '{hilight $0} $1',
'startup_theme', '{hilight $0} $1',
]);
Irssi::signal_add('setup changed', \&sig_setup_changed);
Irssi::settings_add_str($IRSSI{name} ,$IRSSI{name}.'_file', 'matryoshka.store');
Irssi::settings_add_str($IRSSI{name} ,$IRSSI{name}.'_key', '');
Irssi::settings_add_str($IRSSI{name} ,$IRSSI{name}.'_key_file', '.matryoshka.key.file');
Irssi::command_bind($IRSSI{name}, \&cmd);
foreach ( qw/save dump expando startup/ ){
Irssi::command_bind($IRSSI{name}.' '.$_, \&cmd);
}
foreach ( qw/list add remove update/ ){
Irssi::command_bind($IRSSI{name}.' expando '.$_, \&cmd);
}
foreach ( qw/list add remove run/ ){
Irssi::command_bind($IRSSI{name}.' startup '.$_, \&cmd);
}
Irssi::command_bind('help', \&cmd_help);
sig_setup_changed();
loadkeyfile();
loadfile();
updateexpandos();
startup_run();