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
|
use strict;
use utf8;
use HTTP::Tiny;
use File::Glob ':bsd_glob';
use vars qw($VERSION %IRSSI);
use Irssi;
$VERSION = '0.02';
%IRSSI = (
authors => 'bw1',
contact => 'bw1@aol.at',
name => 'fpaste',
description => 'copy infos to fpaste',
license => 'Public Domain',
url => 'https://scripts.irssi.org/',
changed => '2021-01-24',
modules => 'HTTP::Tiny File::Glob',
commands=> 'fpaste',
selfcheckcmd=> 'fpaste -check',
);
my $help = << "END";
%9Name%9
$IRSSI{name}
%9Version%9
$VERSION
%9Syntax%9
/$IRSSI{name} <-file <filename>> [-summary "summary"]
/$IRSSI{name} <-command <command>> [-summary "summary"]
/$IRSSI{name} <-sysinfo> [-summary "summary"]
%9description%9
-file paste the file to fpaste
-command run the command and paste the result
-sysinfo colletct system infos and load them up
-check self check
%9See also%9
http://fpaste.scsys.co.uk/irssi
https://github.com/rcaputo/bot-pastebot
END
my %fpaste_channels =(
'#irssi'=>1,
'#curl'=>1,
'#ledgersmb'=>1,
'#mojo'=>1,
'#ospkg'=>1,
'#perl'=>1,
'#r'=>1,
'#raku'=>1,
);
my $host="http://fpaste.scsys.co.uk";
my $url="$host/paste";
my $buffer;
sub fpasteurl {
my ($res) = @_;
if( $res->{content} =~ m#($host/\d+)#) {
return $1;
} else {
return $res->{url};
}
}
sub paste {
my ($channel, $nick, $summary, $paste)= @_;
my $ht = HTTP::Tiny->new(
agent=>"irssi/$IRSSI{name} $VERSION");
my $data= {
channel=>$channel,
nick=>$nick,
summary=>$summary,
paste=>$paste,
'Paste it'=>'Paste it',
};
my $res =$ht->post_form($url, $data);
return fpasteurl($res);
}
sub fslurp {
my ($filename) =@_;
$filename = bsd_glob $filename;
if ( -e $filename ) {
local $/;
open my $fi,'<',$filename;
my $data=<$fi>;
close $fi;
return $data;
}
}
sub getsetting {
my ($name)= @_;
my $s=$name. ": ";
$s .= Irssi::settings_get_str($name);
$s .= "\n";
return $s;
}
sub scripts {
my %all;
my $s;
foreach (sort grep s/::$//, keys %Irssi::Script::) {
no strict 'refs';
my %info = %{ "Irssi::Script::${_}::IRSSI" };
$info{version} = ${ "Irssi::Script::${_}::VERSION" };
$all{$_}={%info};
}
foreach (sort keys %all) {
$s .= sprintf "%-20s version: $all{$_}->{version}\n",$_;
}
return $s;
}
sub do_capture {
my ($cmd, $witem) = @_;
Irssi::signal_add_first('print text', 'sig_print_text');
if (defined $witem) {
$witem->command($cmd);
} else {
Irssi::command($cmd);
}
Irssi::signal_remove('print text', 'sig_print_text');
}
sub getbuf {
my $s= $buffer;
$buffer='';
$s =~ s/^-!- //m;
return $s;
}
sub sig_print_text {
my ($text_dest, $str, $stripped_str) = @_;
$buffer .= $stripped_str. "\n";
Irssi::signal_stop;
}
sub sysinfo {
my $info;
my $irssi;
$info .= "Irssi\n";
do_capture('eval echo version: $J');
$irssi .= getbuf();
do_capture('eval echo release date: $V');
$irssi .= getbuf();
$irssi .= getsetting('term_charset');
$irssi =~ s/^/ /mg;
$info .= $irssi;
#
my $scr;
$info .= "Scripts\n";
$scr .= scripts();
$scr =~ s/^/ /mg;
$info .= $scr;
#
my $mod;
$info .= "Modules\n";
do_capture('load');
$mod .= getbuf();
$mod =~ s/^/ /mg;
$info .= $mod;
#
$info .= "System\n";
my $sys;
$sys .= "Perl Version: $^V\n";
$sys .= "OS Name: $^O\n";
$sys .= "ENV TERM: $ENV{TERM}\n";
$sys .= "ENV XTERM_LOCALE: $ENV{XTERM_LOCALE}\n";
$sys .= "ENV LANG: $ENV{LANG}\n";
if ($^O eq 'linux') {
$sys .= ` uname -a`."\n";
$sys .= `cat /etc/os-release`. "\n";
}
$sys =~ s/^/ /mg;
$info .= $sys;
return $info;
}
sub self_check {
my ( $res ) = @_;
my $s="ok";
if ( $res !~ m/^http/ ) {
$s= "Error: url ($res)";
}
Irssi::print("fpaste: selfcheck: $s");
my $schs_version = $Irssi::Script::selfcheckhelperscript::VERSION;
Irssi::command("selfcheckhelperscript $s") if ( defined $schs_version );
}
sub cmd {
my ($args, $server, $witem)=@_;
my ($opt, $arg) = Irssi::command_parse_options($IRSSI{name}, $args);
my $channel='(none)';
my ($nick, $result, $summary, $paste, $run, $check);
my $serv= Irssi::active_server();
if ( defined $serv ){
$nick= $server->{nick};
} else {
$nick= Irssi::settings_get_str('nick');
}
if (defined $witem) {
if ($witem->{type} eq 'CHANNEL') {
if ( exists $fpaste_channels{$witem->{name}} ) {
$channel=$witem->{name};
$nick=$server->{nick};
}
}
}
if (exists $opt->{file}) {
$summary=$opt->{file};
$paste= fslurp($opt->{file});
$run=1;
}
if (exists $opt->{command}) {
$summary=$opt->{command};
do_capture($opt->{command}, $witem);
$paste=getbuf();
$run=1;
}
if (exists $opt->{sysinfo}) {
$summary='sysinfo';
$paste=sysinfo();
$run=1;
}
if (exists $opt->{summary}) {
$summary=$opt->{summary};
}
if (exists $opt->{check}) {
$summary='check';
$paste=sysinfo();
$run=1;
$check=1;
}
if ( defined $run ) {
$result= paste($channel, $nick, $summary, $paste);
if ( $check == 1 ) {
self_check($result);
$check=0;
}
if (defined $witem) {
$witem->print($result, MSGLEVEL_CLIENTCRAP);
} else {
Irssi::print($result, MSGLEVEL_CLIENTCRAP);
}
} else {
Irssi::print($help, MSGLEVEL_CLIENTCRAP);
}
}
sub cmd_help {
my ($args, $server, $witem)=@_;
$args=~ s/\s+//g;
if ($IRSSI{name} eq $args) {
Irssi::print($help, MSGLEVEL_CLIENTCRAP);
Irssi::signal_stop();
}
}
Irssi::command_bind($IRSSI{name}, \&cmd);
Irssi::command_bind('help', \&cmd_help);
Irssi::command_set_options($IRSSI{name}, "+file +command sysinfo +summary help check");
|