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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
use strict;
use warnings;
{ package Irssi::Nick }
use Irssi qw(command_bind command_bind_first command_runsub
settings_add_str settings_add_int command_set_options
command_parse_options server_find_tag signal_stop signal_continue
timeout_add timeout_remove signal_add_first signal_add_last
current_theme);
use Irssi::TextUI;
use Scalar::Util 'looks_like_number';
use List::Util qw(min max);
use POSIX 'strftime';
our $VERSION = '1.1';
our %IRSSI = (
authors => 'Pablo Martín Báez Echevarría',
contact => 'pab_24n@outlook.com',
name => 'buffer',
description => 'pastes a buffer into a channel or query window line by line with a specific delay between lines',
url => 'http://reirssi.wordpress.com',
license => 'Public Domain',
changed => 'vie jun 17 15:04:25 UYT 2016',
);
my $buffer = [];
my $pastings = {};
my $regex = '^';
my $theme = Irssi::settings_get_str('theme');
my $timestamp_format = current_theme->get_format('fe-common/core', 'timestamp');
my $timestamp_setting = Irssi::settings_get_str('timestamp_format');
{ # copied from Nei's adv_windowlist.pl
my %strip_table = (
# fe-common::core::formats.c:format_expand_styles
# delete format_backs format_fores bold_fores other stuff
(map { $_ => '' } (split //, '04261537' . 'kbgcrmyw' . 'KBGCRMYW' . 'U9_8I:|FnN>#[' . 'pP')),
# escape
(map { $_ => $_ } (split //, '{}%')),
);
sub ir_strip_codes { # strip %codes
my $o = shift;
$o =~ s/(%(%|Z.{6}|z.{6}|X..|x..|.))/exists $strip_table{$2} ? $strip_table{$2} :
$2 =~ m{x(?:0[a-f]|[1-6][0-9a-z]|7[a-x])|z[0-9a-f]{6}}i ? '' : $1/gex;
$o
}
}
sub get_expanded_timestamp {
my $theme = current_theme;
my $timestamp_format = $theme->get_format('fe-common/core', 'timestamp');
(my $timestamp_setting = Irssi::settings_get_str('timestamp_format')) =~ s/%/%%/g;
$timestamp_format =~ s/\$Z/$timestamp_setting/g;
ir_strip_codes( $theme->format_expand($timestamp_format) );
}
push my @expanded_timestamps, {
unix_time => time,
expanded_timestamp => get_expanded_timestamp
};
sub cmd_buffer_help {
print CLIENTCRAP <<HELP
%9Syntax%9:
BUFFER SEARCH [-file <filename>] [-regexp] [-case] [-word] [<pattern>]
BUFFER LOAD [-file <filename>] [-striptime] [-begin <first line>] [-end <last line>]
BUFFER CLEAR
BUFFER PRINT
BUFFER PLAY [-delay <seconds>] [-continue [<id>]]
BUFFER STOP [<id>]
BUFFER REMOVE [<id>]
BUFFER RESUME
%9Description%9:
Pastes a buffer into a channel or query window line by line with a
specific delay between lines. It is inspired by the mIRC /play
command.
%9Parameters%9:
SEARCH: Searches the active window (or a file) for a pattern and
displays the matching lines with its corresponding line
number, in order to make easy to load the desired text.
-file Name of the file where to look at. If omitted,
searchs the scrollback buffer of the current
window.
-regexp The given pattern is a regexp.
-case Performs case sensitive matching.
-word The text must match full words.
-pattern If omitted, it will be the pattern of the
previous search (and if there is no previous
search, the command displays everything).
LOAD: Loads the buffer and gets it ready to be pasted into a
location.
-file Name of the file whose lines will be loaded. If
omitted, loads lines from the current window.
-striptime Tries to strip the timestamp when loading lines
from a window. If it is used together with
-file, it has no effect.
-begin Number of the first line to be loaded. If
omitted, it will be 1.
-end Number of the last line to be loaded. If
omitted, it will be the total number of lines
in the current window/file.
CLEAR: Clears the buffer.
PRINT: Displays the content of the buffer in the status window.
PLAY: Pastes the buffer into the current channel or query window
or wakes up an already existing paste.
-delay Delay (in seconds) between each message.
-continue Continues the paste identified by <id> (run
/BUFFER RESUME to find out the correct
identifier). If this parameter is omitted, it
will paste the entire buffer previously loaded
into the current channel or query window. If
-continue is used not followed by an id, it
wakes up all the paused pastes.
STOP: Stops an active paste.
-id Identifier of the paste that you would like
to stop. If ommitted, it will stop all the
active pastes.
REMOVE: Removes an existing paste.
-id Identifier of the paste that you would like to
remove. If omitted, it will remove all the
existing pastes.
RESUME: Lists the existing pastes and shows information about each
one.
%9Settings%9:
-buffer_delay: Default delay between messages.
-buffer_context_lines If this is set to n, then /BUFFER RESUME will
print n lines before and after the next line
to be pasted.
HELP
;
signal_stop; # To avoid 'No help for buffer' at the end
}
sub open_file {
my $filename = shift;
$filename =~ s/^~/$ENV{HOME}/;
open my $fh, '<', $filename or die "Could not open file '$filename': $!\n";
die "File '$filename' does not look like a text file\n" unless -T $filename;
return $fh;
}
sub send_line {
my ( $timeout_tag, $buff, $index, $server, $target ) = @_;
# Get line
my $line = $buff->[$$index];
$line .= ' ' if $line eq '';
# Send line to target
$server->command("MSG $target $line");
# Increment pointer which stores the next line to be sent
$$index++;
# Remove the paste if it's the last line in the buffer
if ( $$index == @$buff ) {
timeout_remove( $timeout_tag );
delete $pastings->{$timeout_tag};
}
}
sub timeout_function {
my ( $ref ) = @_;
my $timeout_tag = $$ref;
my ( $buff, $servtag, $targ, $pointer ) =
@{$pastings->{$timeout_tag}}{qw/buffer network target counter/};
# Check server and target
my $server = server_find_tag( $servtag );
unless ( $server && $server->{'connected'} ) {
printf CLIENTERROR "Not connected to server '%s'. Paste <%d> will be paused", $servtag, $timeout_tag;
timeout_remove( $timeout_tag );
$pastings->{$timeout_tag}{'status'} = 'paused';
return;
};
my $witem = $server->window_find_item($targ);
unless ( $witem ) {
printf CLIENTERROR "No window named '%s'. Paste <%d> will be paused", $targ, $timeout_tag;
timeout_remove( $timeout_tag );
$pastings->{$timeout_tag}{'status'} = 'paused';
return;
}
# Send line
send_line($timeout_tag, $buff, $pointer, $server, $targ);
}
sub buffer_context_range {
my ( $index, $context_lines, $total ) = @_;
my $first = max( 0, $index - $context_lines );
my $last = min( $total-1, $index + $context_lines );
$first..$last;
}
sub get_timestamp_regexp {
my $unix_time = shift;
my $last = 0;
foreach my $i (0..$#expanded_timestamps) {
$last = $i if ($expanded_timestamps[$i]->{'unix_time'} <= $unix_time) or last;
}
my $frm1 = $expanded_timestamps[$last]->{'expanded_timestamp'};
my $frm2 = $expanded_timestamps[$last-1]->{'expanded_timestamp'} if $last >= 1;
my $timestamp1 = strftime($frm1, localtime($unix_time));
my $timestamp2 = (defined $frm2) ? strftime($frm2, localtime($unix_time)) : '';
qr/\Q$timestamp1\E|\Q$timestamp2\E/;
}
sub cmd_buffer_search {
my ( $args, $server, $witem ) = @_;
my ($options, $pattern) = command_parse_options('buffer search', $args);
if ($pattern) {
my $flags = defined($options->{'case'}) ? '' : '(?i)';
my $b = defined($options->{'word'}) ? '\b' : '';
if (defined $options->{'regexp'} ) {
local $@;
eval {
$regex = qr/$flags$b$pattern$b/;
};
if ($@) {
my ($err) = $@ =~ /^(.*) at .* line \d+\.$/;
print CLIENTERROR "Pattern \/$pattern\/ did not compile: $err";
return;
}
} else {
$regex = qr/$flags$b\Q$pattern\E$b/;
}
}
my @results;
if ( defined $options->{'file'} ) {
my $filename = $options->{'file'};
my $fh;
eval { $fh = open_file($filename) };
if ($@) {
chomp(my $err = $@);
print CLIENTERROR $err;
return;
}
my $num = 1;
while( defined (my $line = <$fh>) ) {
chomp($line);
$line =~ s/\t/' 'x4/ge;
push @results, [$num, $`, $&, $'] if $line =~ $regex;
$num++;
}
close $fh;
} else {
my $current_win = ref $witem ? $witem->window : Irssi::active_win;
my $view = $current_win->view;
my $line = $view->{'buffer'}->{'first_line'};
my $num = 1;
while ( defined $line ) {
push @results, [$num, $`, $&, $'] if $line->get_text(0) =~ $regex;
$line = $line->next;
$num++;
}
}
if (@results) {
my $greatest_line_number = $results[-1][0];
my $digits = length $greatest_line_number;
printf CLIENTCRAP join("\n", ("%%9%${digits}d.%%n%s%%9%%R%s%%n%s")x@results),
map { @$_[0..3] } @results;
}
}
sub cmd_buffer_load {
my ( $args, $server, $witem ) = @_;
my ($options) = command_parse_options('buffer load', $args);
my $start = $options->{'begin'} // 1;
my $end;
my @new_buffer;
if ( defined $options->{'file'} ) {
my $filename = $options->{'file'};
my $fh;
eval { $fh = open_file($filename) };
if ($@) {
chomp(my $err = $@);
print CLIENTERROR $err;
return;
}
my @dump = <$fh>;
close $fh;
my $lines_count = @dump;
$end = $options->{'end'} // $lines_count;
if ($start<1 || $end>$lines_count || $start>$end) {
print CLIENTERROR 'Wrong -start or -end parameters (out of range)';
return;
};
@new_buffer = map{ chomp; s/\t/' 'x4/ge; $_ } @dump[$start-1..$end-1]
} else {
my $current_win = ref $witem ? $witem->window : Irssi::active_win;
my $view = $current_win->view;
my $line = $view->{'buffer'}->{'first_line'};
my $lines_count = $view->{'buffer'}->{'lines_count'};
$end = $options->{'end'} // $lines_count;
if ($start<1 || $end>$lines_count || $start>$end) {
print CLIENTERROR 'Wrong -start or -end parameters (out of range)';
return;
};
my $num = 1;
while ( defined $line ) {
if ( $start<=$num && $num<=$end ) {
chomp(my $line_text = $line->get_text(0));
if ( defined $options->{'striptime'} ) {
my $timestamp_regex = get_timestamp_regexp($line->{'info'}{'time'});
$line_text =~ s/^$timestamp_regex//;
}
push @new_buffer, $line_text;
last if $num == $end;
}
$line = $line->next;
$num++;
}
}
$buffer = \@new_buffer;
print CLIENTCRAP 'Buffer successfully loaded';
}
sub cmd_buffer_clear {
$buffer = [];
print CLIENTCRAP 'Buffer is now empty';
}
sub cmd_buffer_print {
print CLIENTCRAP $_ for @$buffer;
printf CLIENTCRAP "%d lines", scalar @$buffer;
}
sub cmd_buffer_play {
my ( $args, $server, $witem ) = @_;
my ($options) = command_parse_options('buffer play', $args);
my $delay = $options->{'delay'} // Irssi::settings_get_str('buffer_delay');
unless (looks_like_number($delay)) {
print CLIENTERROR 'Delay must be a number';
return;
}
unless ( $delay >= 10e-3 ) {
print CLIENTERROR 'Delay cannot be less than 0.010 seconds (10 milliseconds)';
return;
}
if ( defined $options->{'continue'} ) {
my $id = $options->{'continue'};
if ( $id =~ /^\s*$/ ) { # Empty id. Wake up every paused paste
foreach my $inner_id (keys %$pastings) {
if ( $pastings->{$inner_id}{'status'} eq 'paused' ) {
wake_sleeping_paste( $inner_id, $options->{'delay'} );
}
}
} else { # Not empty id
unless ( defined $pastings->{$id} ) {
print CLIENTERROR 'Not recognized id. See /BUFFER RESUME';
return;
}
wake_sleeping_paste( $id, $options->{'delay'} );
}
} else {
unless ( $server && $server->{'connected'} ) {
print CLIENTERROR 'Not connected to server';
return;
}
unless ( $witem && ($witem->{'type'} eq 'CHANNEL' || $witem->{'type'} eq 'QUERY') ) {
print CLIENTERROR 'No active channel/query in window';
return;
}
unless ( @$buffer ) {
print CLIENTERROR 'Buffer is empty. Nothing to paste';
return;
}
my $servtag = $server->{'tag'};
my $target = $witem->{'name'};
my $counter = 0;
my $timeout_tag;
$timeout_tag = timeout_add( $delay*1000 , 'timeout_function', \$timeout_tag );
$pastings->{$timeout_tag} = {
buffer => $buffer,
status => 'active',
network => $servtag,
target => $target,
counter => \$counter,
delay => $delay,
timestamp => time,
};
send_line($timeout_tag, $buffer, \$counter, $server, $target);
}
}
sub wake_sleeping_paste {
my ( $id, $delay ) = @_;
unless ( $pastings->{$id}{'status'} eq 'paused' ) {
printf CLIENTERROR 'Paste <%d> is already active', $id;
return;
}
my ( $buff, $servtag, $targ, $pointer ) =
@{$pastings->{$id}}{qw/buffer network target counter/};
# Check server and target
my $server = server_find_tag( $servtag );
unless ( $server && $server->{'connected'} ) {
printf CLIENTERROR "Not connected to server '%s'. Paste <%d> will continue to be paused", $servtag, $id;
return;
};
my $witem = $server->window_find_item($targ);
unless ( $witem ) {
printf CLIENTERROR "No window named '%s'. Paste <%d> will continue to be paused", $targ, $id;
return;
}
my $temp = $pastings->{$id};
delete $pastings->{$id};
$temp->{'status'} = 'active';
$temp->{'delay'} = $delay if defined $delay;
$temp->{'timestamp'} = time;
my $timeout_tag;
$timeout_tag = timeout_add( $temp->{'delay'} * 1000 , 'timeout_function', \$timeout_tag );
$pastings->{$timeout_tag} = $temp;
send_line($timeout_tag, $buff, $pointer, $server, $targ);
}
sub cmd_buffer_stop {
my ( $args, $server, $witem ) = @_;
my $id = $args;
if ( $id =~ /^\s*$/ ) { # Empty id. Stop every active paste
foreach my $inner_id (keys %$pastings) {
if ( $pastings->{$inner_id}{'status'} eq 'active' ) {
timeout_remove( $inner_id );
$pastings->{$inner_id}{'status'} = 'paused';
}
}
} else { # Not empty id
unless ( defined $pastings->{$id} ) {
print CLIENTERROR 'Not recognized id. See /BUFFER RESUME';
return;
}
if ( $pastings->{$id}{'status'} eq 'active' ) {
timeout_remove( $id );
$pastings->{$id}{'status'} = 'paused';
}
}
}
sub cmd_buffer_remove {
my ( $args, $server, $witem ) = @_;
my $id = $args;
if ( $id =~ /^\s*$/ ) { # Empty id. Remove every existing paste
foreach my $inner_id (keys %$pastings) {
timeout_remove( $inner_id );
delete $pastings->{$inner_id};
}
} else { # Not empty id
unless ( defined $pastings->{$id} ) {
print CLIENTERROR 'Not recognized id. See /BUFFER RESUME';
return;
}
timeout_remove( $id );
delete $pastings->{$id};
}
}
sub cmd_buffer_resume {
if ( keys %$pastings ) {
my $context_lines = Irssi::settings_get_int('buffer_context_lines');
if ($context_lines < 0) {
print CLIENTERROR 'The number of context lines (surrounding the next line to be sent) must be a positive integer';
return;
}
my $id_string = 'ID';
my $id_colwidth = max(
length($id_string),
max( map{length($_)} keys %$pastings )
);
my $time_string = 'TIMESTAMP';
my $time_colwidth = max(
length($time_string),
max( map{length( strftime('%c', localtime($pastings->{$_}->{'timestamp'})) )} keys %$pastings )
);
my $status_string = 'STATUS';
my $status_colwidth = max(
length($status_string),
max( map{length($pastings->{$_}->{'status'})} keys %$pastings )
);
my $network_string = 'NETWORK';
my $network_colwidth = max(
length($network_string),
max( map{length($pastings->{$_}->{'network'})} keys %$pastings )
);
my $channel_string = 'TARGET';
my $channel_colwidth = max(
length($channel_string),
max( map{length($pastings->{$_}->{'target'})} keys %$pastings )
);
my $delay_string = 'DELAY';
my $delay_colwidth = max(
length($delay_string),
max( map{length($pastings->{$_}->{'delay'})} keys %$pastings )
);
my $pasted_string = 'PASTED LINES';
my $pasted_colwidth = max(
length($pasted_string),
max( map{length( ${$pastings->{$_}->{'counter'}} )} keys %$pastings )
);
my $pending_string = 'PENDING LINES';
my $pending_colwidth = max(
length($pending_string),
max( map{length( @{$pastings->{$_}->{'buffer'}} - ${$pastings->{$_}->{'counter'}} )} keys %$pastings )
);
my $str_format = join (
" "x4,
map { "\%${_}s" }
($id_colwidth,
$time_colwidth,
$status_colwidth,
$network_colwidth,
$channel_colwidth,
$delay_colwidth,
$pasted_colwidth,
$pending_colwidth
)
);
foreach my $id ( sort{$pastings->{$a}->{'timestamp'}<=>$pastings->{$b}->{'timestamp'}} keys %$pastings ) {
printf CLIENTCRAP "%%9" . $str_format,
($id_string,
$time_string,
$status_string,
$network_string,
$channel_string,
$delay_string,
$pasted_string,
$pending_string
);
printf CLIENTCRAP $str_format . "\n",
($id,
strftime('%c', localtime($pastings->{$id}->{'timestamp'})),
$pastings->{$id}->{'status'},
$pastings->{$id}->{'network'},
$pastings->{$id}->{'target'},
$pastings->{$id}->{'delay'},
${$pastings->{$id}->{'counter'}},
@{$pastings->{$id}->{'buffer'}} - ${$pastings->{$id}->{'counter'}}
);
print CLIENTCRAP '%9NEXT LINE TO BE PASTED:';
my $buffref = $pastings->{$id}->{'buffer'};
my $index = ${ $pastings->{$id}->{'counter'} };
my $total = @$buffref;
my @range = buffer_context_range($index, $context_lines, $total);
my $digits = length( max( map{$_ + 1} @range ) );
foreach my $i ( @range ) {
printf CLIENTCRAP "%%9%${digits}d.%%n%10s%s",
$i+1, (($i == $index) ? '===>' : ''), @$buffref[$i];
};
printf CLIENTCRAP "---- End paste <%s>", $id;
}
} else {
print CLIENTCRAP "There aren't any pastes";
return;
}
}
signal_add_first 'send command' => sub {
signal_continue(@_);
my $command = shift;
my $cmdchars = Irssi::settings_get_str('cmdchars');
if ( $command =~ /^\Q$cmdchars\Eformat\s+timestamp/i ) {
my $current_timestamp_format = current_theme->get_format('fe-common/core', 'timestamp');
if ( $current_timestamp_format ne $timestamp_format ) {
push @expanded_timestamps, {
unix_time => time,
expanded_timestamp => get_expanded_timestamp
};
$timestamp_format = $current_timestamp_format;
}
}
};
signal_add_last 'setup changed' => sub {
my $current_theme = Irssi::settings_get_str('theme');
my $current_timestamp_setting = Irssi::settings_get_str('timestamp_format');
if ( $current_theme ne $theme || $current_timestamp_setting ne $timestamp_setting ) {
push @expanded_timestamps, {
unix_time => time,
expanded_timestamp => get_expanded_timestamp
};
$theme = $current_theme;
$timestamp_setting = $current_timestamp_setting;
}
};
command_bind 'buffer' => sub {
my ( $data, $server, $item ) = @_;
$data =~ s/\s+$//g;
command_runsub('buffer', $data, $server, $item);
};
command_bind_first help => sub { &cmd_buffer_help if $_[0] =~ /^buffer\s*$/i };
command_bind 'buffer search' => 'cmd_buffer_search';
command_set_options 'buffer search' => '-file regexp case word';
command_bind 'buffer load' => 'cmd_buffer_load';
command_set_options 'buffer load' => '-file striptime @begin @end';
command_bind 'buffer clear' => 'cmd_buffer_clear';
command_bind 'buffer print' => 'cmd_buffer_print';
command_bind 'buffer play' => 'cmd_buffer_play';
command_set_options 'buffer play' => '-delay -continue';
command_bind 'buffer stop' => 'cmd_buffer_stop';
command_bind 'buffer remove' => 'cmd_buffer_remove';
command_bind 'buffer resume' => 'cmd_buffer_resume';
settings_add_str 'buffer', 'buffer_delay', '1';
settings_add_int 'buffer', 'buffer_context_lines', '2';
|