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
|
use strict;
use vars qw($VERSION %IRSSI %HELP);
use Irssi 0 qw
(
active_win server_find_tag signal_stop window_find_name parse_special
);
use Irssi::UI;
use Irssi::TextUI;
$VERSION = '1.1';
%IRSSI =
(
'authors' => 'Marcin Rozycki',
'contact' => 'derwan@irssi.pl',
'name' => 'paste',
'description' => 'Pasting lines to specified targets, type "/paste -help" for help',
'license' => 'GNU GPL v2',
'modules' => '',
'url' => 'http://derwan.irssi.pl',
'changed' => '2018-07-14',
);
$HELP{'paste'} = <<EOF;
PASTE [-help] [-c] [-q] [-msg | -notice] [-<server tag>] [<target>] [<indexes>]
-help: print this help
-c: enable colors
-q: quiet mode (pasted lines are not dispalyed)
-msg: sends messages as msg (as default)
-notice: sends messages as notice
-<server target>: sends messages to specified server
<target>: targets (separated with commas)
<indexes>: indexes of lines to paste ( separated with spaces)
Examples:
/PASTE - pasting to active channel or query
/PASTE -c - pasting to active channel or query with colors
/PASTE -c 1 3-5 - pasting to active channel or query lines 1, 3, 4 and 5
/PASTE -notice - pasting to active item - messages sent as notice, not msg
/PASTE derwan - sends messages to derwan
/PASTE -ircnet -c derwan,#irssi - sends messages (with colors) to derwan and #irssi in IRCNet
Paste window - indexes:
[0] [<index>] [<index from>-<index to>]
Examples:
0 - cancel
4 - line 4
4 8 9 10 - lines 4, 8, 9, 10
4 8-10 - lines 4, 8, 9, 10
Themes:
paste_normal - \$0 line
paste_reverse - \$0 line
paste_count - \$0 count, \$1 server tag, \$2 target
paste_input
paste_no_server - \$0 comment
paste_argument_missing - \$0 option, \$1 comment
paste_argument_unknown - \$0 option, \$1 comment
paste_nothing
Your version is $VERSION - for updates visit $IRSSI{url}
Mail bug reports and suggestions to <$IRSSI{contact}>
EOF
my ( $p );
# paste (str data, rec server, rec window)
sub paste ($$$)
{
buf_destroy();
$p = {};
$p->{color} = 0;
$p->{cmd} = 'msg';
$p->{quiet} = 0;
my $win = active_win();
foreach my $arg ( split /\s+/, $_[0] )
{
( $arg eq '-help' ) and Irssi::print($HELP{'paste'}, MSGLEVEL_CLIENTCRAP), return;
( $arg eq '-c' ) and $p->{color} = 1, next;
( $arg eq '-q' ) and $p->{quiet} = 1, next;
( $arg =~ m/^-(msg|notice)$/ ) and $p->{cmd} = $1, next;
( $arg =~ m/^-(.*)$/ and !$p->{tag} ) and $p->{tag} = $1, next;
( $arg =~ m/^([^-\s]*[^-\d]+[^\s]*)$/ and !$p->{target} ) and $p->{target} = $1, next;
( $arg =~ m/^([1-9]\d*)$/ ) and $p->{l}->{$1} = 1, next;
if ( $arg =~ m/^([1-9]\d*)-(\d+)$/ and $1 <= $2 )
{
map { $p->{l}->{$_} = $p->{buf}->[$_-1] } ( $1 .. $2 );
next;
}
$win->printformat
(
MSGLEVEL_CRAP, 'paste_argument_unknown', $arg, 'type /paste -help for help'
);
buf_destroy(), return;
}
if ( !exists $p->{tag} or !defined $p->{tag} )
{
if ( !ref $_[1] and !ref $win->{server} )
{
$win->printformat
(
MSGLEVEL_CRAP, 'paste_argument_missing', 'server tag', 'type /paste -help for help'
);
buf_destroy(), return;
}
$p->{tag} = ( ref $_[1] ) ? $_[1]->{tag} : $win->{active}->{server}->{tag};
}
elsif ( ! ref server_find_tag($p->{tag}) )
{
$win->printformat
(
MSGLEVEL_CRAP, 'paste_argument_unknown', $p->{tag}, 'not connected to that server'
);
buf_destroy(), return;
}
unless ( exists $p->{target} and defined $p->{target} )
{
if ( !ref $win->{active} or !$win->{active}->{name} )
{
$win->printformat
(
MSGLEVEL_CRAP, 'paste_argument_missing', 'target', 'type /paste -help for help'
);
buf_destroy(), return;
}
$p->{target} = $win->{active}->{name};
}
if ( buf_create() == 0 )
{
$win->printformat
(
MSGLEVEL_CRAP, 'paste_nothing'
);
buf_destroy(), return;
}
foreach my $idx ( keys %{$p->{l}} )
{
$p->{l}->{$idx} = $p->{buf}->[$idx-1];
}
buf_destroy(), return if ( buf_flush() != 0 );
$p->{win} = sprintf('paste.%d', (int(rand(9000))+1000));
my $input = Irssi::Windowitem::window_create($p->{win}, 1);
$input->set_name($p->{win});
$input->set_history($p->{win});
$input->change_server(server_find_tag($p->{tag}));
my $width = $input->{width} - 8;
my $theme = 'normal';
for ( my $idx = $#{$p->{buf}}; $idx >= 0; $idx-- )
{
my $text = $p->{buf}->[$idx]->get_text(0);
$text = sprintf
(
'%03d %'.( length($text) > $width ? '.'.($width-1).'s$' : '-'.$width.'s' ).
' %03d', $idx+1, $text, $idx+1
);
$input->printformat(MSGLEVEL_NOHILIGHT, 'paste_'.$theme, $text);
$theme = $theme eq 'normal' ? 'reverse' : 'normal';
}
$input->printformat(MSGLEVEL_NOHILIGHT, 'paste_input');
$input->set_active();
};
sub buf_create ()
{
return unless ( defined $p and ref $p );
my $win = active_win();
return 0 unless ( ref $win );
my $curline = $win->view()->{buffer}->{cur_line};
return 0 unless ( ref $curline );
for ( my $idx = 0; $idx < 100; $idx++ )
{
last unless ( ref $curline );
push @{$p->{buf}}, $curline;
$curline = $curline->prev();
}
return ( $#{$p->{buf}} >= 0 ? 1 : 0 );
}
sub buf_flush ()
{
return unless ( defined $p and ref $p );
my $serv = server_find_tag($p->{tag});
unless ( ref $serv and $serv->{connected} )
{
active_win()->printformat
(
MSGLEVEL_CRAP, 'paste_no_server', $p->{tag}
);
return -1;
}
my $count = 0;
foreach my $idx ( sort { $b <=> $a } ( keys %{$p->{l}} ) )
{
if ( defined $p->{l}->{$idx} and ref $p->{l}->{$idx} and ++$count )
{
if ( $p->{quiet} == 0 )
{
my $cmd = sprintf
(
'%s %s %s', $p->{cmd}, $p->{target}, convertstr($p->{l}->{$idx}->get_text($p->{color}))
);
$serv->command($cmd);
}
else
{
my $raw = sprintf
(
'%s %s :%s', ( $p->{cmd} eq 'msg' ? 'privmsg' : 'notice' ), $p->{target},
convertstr($p->{l}->{$idx}->get_text($p->{color}))
);
$serv->send_raw($raw);
}
}
}
if ( $count > 0 )
{
active_win()->printformat(MSGLEVEL_CRAP, 'paste_count', $count, $p->{tag}, $p->{target});
}
else
{
active_win()->printformat(MSGLEVEL_CRAP, 'paste_nothing');
}
return $count;
}
# sig_send_command (str data, rec server, rec window)
sub sig_send_command ($$$)
{
unless ( defined $p and ref $p and defined $p->{win} )
{
return;
}
my $win = active_win();
if ( $_[0] eq 0 )
{
buf_destroy(), return;
}
if ( substr($_[0], 0, 1) eq parse_special('$K') )
{
return;
}
unless ( ref $win and $win->{name} eq $p->{win} )
{
return;
}
signal_stop ();
$win->destroy();
delete $p->{win};
foreach my $arg ( split /\s+/, $_[0] )
{
if ( $arg =~ m/^(\d+)$/ and $1 > 0 )
{
$p->{l}->{$1} = $p->{buf}->[$1-1];
}
elsif ( $arg =~ m/^([1-9]\d*)-(\d+)$/ and $1 <= $2 )
{
map { $p->{l}->{$_} = $p->{buf}->[$_-1] } ( $1 .. $2 );
}
else
{
active_win()->printformat(MSGLEVEL_CRAP, 'paste_argument_unknown', $arg, 'type /paste -help for help');
}
}
buf_flush();
buf_destroy();
};
sub buf_destroy ()
{
if ( defined $p and ref $p )
{
@{$p->{buf}} = () if ( defined $p->{buf} and ref $p->{buf} );
%{$p->{l}} = () if ( defined $p->{l} and ref $p->{l} );
if ( defined $p->{win} )
{
my $win = window_find_name($p->{win});
$win->destroy if ( ref $win );
}
undef ( $p );
}
}
# convertstr (str text), str text
# thanks for Stanislaw Halik <weirdo@blindfold.no-ip.com>
sub convertstr ($)
{
if ( $_[0] )
{
$_[0] =~ s/[\004]g\//\003\002\002/g;
$_[0] =~ s/[\004]\?\/+/\0030\002\002/g;
$_[0] =~ s/[\004]0\//\0031\002\002/g;
$_[0] =~ s/[\004]0/\0031\002\002/g;
$_[0] =~ s/[\004]1\//\0032\002\002/g;
$_[0] =~ s/[\004]1/\0032\002\002/g;
$_[0] =~ s/[\004]2\//\0033\002\002/g;
$_[0] =~ s/[\004]2/\0033\002\002/g;
$_[0] =~ s/[\004]<\//\0034\002\002/g;
$_[0] =~ s/[\004]</\0034\002\002/g;
$_[0] =~ s/[\004]4\//\0035\002\002/g;
$_[0] =~ s/[\004]4/\0035\002\002/g;
$_[0] =~ s/[\004]5\//\0036\002\002/g;
$_[0] =~ s/[\004]5/\0036\002\002/g;
$_[0] =~ s/[\004]6\//\0037\002\002/g;
$_[0] =~ s/[\004]6/\0037\002\002/g;
$_[0] =~ s/[\004]>\//\0038\002\002/g;
$_[0] =~ s/[\004]>/\0038\002\002/g;
$_[0] =~ s/[\004]:\//\0039\002\002/g;
$_[0] =~ s/[\004]:/\0039\002\002/g;
$_[0] =~ s/[\004]3\//\00310\002\002/g;
$_[0] =~ s/[\004]3/\00310\002\002/g;
$_[0] =~ s/[\004]\;\//\00311\002\002/g;
$_[0] =~ s/[\004]\;/\00311\002\002/g;
$_[0] =~ s/[\004]9\//\00312\002\002/g;
$_[0] =~ s/[\004]9/\00312\002\002/g;
$_[0] =~ s/[\004]=\//\00313\002\002/g;
$_[0] =~ s/[\004]=/\00313\002\002/g;
$_[0] =~ s/[\004]8\//\00314\002\002/g;
$_[0] =~ s/[\004]8/\00314\002\002/g;
$_[0] =~ s/[\004]7\//\00315\002\002/g;
$_[0] =~ s/[\004]7/\00315\002\002/g;
$_[0] =~ s/[\004]g\//\003\002\002/g;
$_[0] =~ s/[\004]g/\003\002\002/g;
$_[0] =~ s/[\004]8\//\003\002\002/g;
$_[0] =~ s/[\004]8/\003\002\002/g;
}
return $_[0];
}
Irssi::theme_register
([
'paste_normal', '$0-',
'paste_reverse', '%c$0-%n',
'paste_input', '%7%r type indexes of lines to paste (type "0" for cancel or "/paste -help" for help): %8%n',
'paste_count', '%_Irssi%_: {hilight $0} line(s) have been pasted to {nick $2} in $1',
'paste_argument_unknown', '%_Irssi%_: Unknown option: {hilight $0} {comment $1}',
'paste_argument_missing', '%_Irssi%_: Not enough parameters given: $0 {comment $1}',
'paste_no_server', '%_Irssi%_: Not connected to specified server {comment $0}',
'paste_nothing', '%_Irssi%_: Nothing to paste',
]);
Irssi::signal_add_first('send command', 'sig_send_command');
Irssi::command_bind('paste', 'paste');
|