From d50a9b156b80d4ccb83810ff1cf1bb9f21bee7ed Mon Sep 17 00:00:00 2001 From: bw1 Date: Sun, 14 Feb 2021 21:13:53 +0100 Subject: [PATCH 01/22] [scriptassist2] init --- scripts/scriptassist2.pl | 266 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 scripts/scriptassist2.pl diff --git a/scripts/scriptassist2.pl b/scripts/scriptassist2.pl new file mode 100644 index 000000000..6e3327ae6 --- /dev/null +++ b/scripts/scriptassist2.pl @@ -0,0 +1,266 @@ +use strict; +use vars qw($VERSION %IRSSI); +use utf8; +use POSIX; +use File::Glob qw/:bsd_glob/; +use CPAN::Meta::YAML; +use File::Fetch; +use Time::Piece; +use Digest::file qw/digest_file_hex/; +use Storable qw/store_fd fd_retrieve freeze thaw/; +use debug; + +use Irssi; + +$VERSION = '0.01'; +%IRSSI = ( + authors => 'bw1', + contact => 'bw1@aol.at', + name => 'scriptassist2', + description => 'This script really does nothing. Sorry.', + license => 'lgpl', + url => 'https://scripts.irssi.org/', + changed => '2021-02-13', + modules => '', + commands=> 'scriptassist2', +); + +my $help = << "END"; +%9Name%9 + $IRSSI{name} +%9Version%9 + $VERSION +%9description%9 + $IRSSI{description} +%9See also%9 + https://perldoc.perl.org/perl.html + https://github.com/irssi/irssi/blob/master/docs/perl.txt + https://github.com/irssi/irssi/blob/master/docs/signals.txt + https://github.com/irssi/irssi/blob/master/docs/formats.txt +END + +# config path +my $path; + +# data root +my $d; +# ->{rconfig}->@ +# ->{rscripts} +# ->{rstat} + +my %bg_process= (); + +sub background { + my ($cmd) =@_; + my ($fh_r, $fh_w); + pipe $fh_r, $fh_w; + my $pid = fork(); + if ($pid ==0 ) { + my @res; + @res= &{$cmd->{cmd}}(@{$cmd->{args}}); + #store_fd \@res, $fh_w; + my $yml=CPAN::Meta::YAML->new(\@res); + print $fh_w $yml->write_string(); + close $fh_w; + POSIX::_exit(1); + } else { + $cmd->{fh_r}=$fh_r; + $bg_process{$pid}=$cmd; + #Irssi::pidwait_add($pid); + my $pipetag; + my @args = ($pid, \$pipetag ); + $pipetag = Irssi::input_add(fileno($fh_r), INPUT_READ, \&sig_pidwait, \@args); + } +} + +sub sig_pidwait { + #my ($pid, $status) = @_; + my ($pid, $pipetag) = @{@_[0]}; + if (exists $bg_process{$pid}) { + #my @res= @{ fd_retrieve($bg_process{$pid}->{fh_r})}; + my $fh_r= $bg_process{$pid}->{fh_r}; + my $ystr = do { local $/; <$fh_r>; }; + close $fh_r; + Irssi::input_remove($$pipetag); + my $yml = CPAN::Meta::YAML->read_string($ystr); + my @res = @{ $yml->[0] }; + $bg_process{$pid}->{res}=[@res]; + if (exists $bg_process{$pid}->{last}) { + foreach my $p (@{$bg_process{$pid}->{last}}) { + &$p($bg_process{$pid}); + } + } else { + Irssi::print(join(" ",@res), MSGLEVEL_CLIENTCRAP); + } + delete $bg_process{$pid}; + } +} + +sub print_box { + my ( $head, $foot, @inside)=@_; + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'box_header', $head); + foreach my $n ( @inside ) { + foreach ( split /\n/, $n ) { + #Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'box_inside', $_); + Irssi::print("%R|%n $_", MSGLEVEL_CLIENTCRAP); + } + } + Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'box_footer', $foot); +} + +sub init { + if ( -e "$path/cache.yml" ) { + my $yml= CPAN::Meta::YAML->read("$path/cache.yml"); + $d= $yml->[0]; + } + + if (! exists $d->{rconfig} ) { + $d->{rconfig}=(); + my %n; + $n{name}="irssi"; + $n{type}="yaml"; + $n{url_db}="https://scripts.irssi.org/scripts.yml"; + $n{url_sc}="https://scripts.irssi.org/scripts"; + push @{$d->{rconfig}}, {%n}; + } +} + +sub save { + my $yml= CPAN::Meta::YAML->new( $d ); + $yml->write("$path/cache.yml"); +} + +sub fetch { + my ($uri)= @_; + my $ff = File::Fetch->new ( + uri => $uri, + ); + my $w= $ff->fetch(to => $path,); + if ( $w ) { + return $ff->file; + } else { + return undef; + } +} + +sub update { + foreach my $n ( @{ $d->{rconfig} } ) { + my $fn=fetch( $n->{url_db} ); + if ( defined $fn && $n->{type} eq 'yaml' ) { + my $di=digest_file_hex("$path/$fn", 'MD5'); + if ( $di ne $d->{rstat}->{$n->{name}}->{digest} ) { + my $yml= CPAN::Meta::YAML->read("$path/$fn"); + my $sl; + foreach my $sn ( @{$yml->[0]} ) { + $sl->{$sn->{filename}}=$sn; + } + $d->{rscripts}->{$n->{name}}= $sl; + } + my $t=localtime(); + $d->{rstat}->{$n->{name}}->{last}= $t->epoch; + $d->{rstat}->{$n->{name}}->{digest}= $di; + unlink "$path/$fn"; + } + } + #my $ser = freeze $d; + #return $ser; + return $d; + #return "hello from uptate"; +} + +sub print_update { + my ( $pn ) = @_; + debug "func hallo"; + #debug $pn->{res}; +} + +sub cmd_reload { + my ($args, $server, $witem)=@_; + init(); +} + +sub cmd_save { + my ($args, $server, $witem)=@_; + save(); +} + +sub cmd_update { + my ($args, $server, $witem)=@_; + my $ser=update(); + debug "fine", length $ser; +} + +sub cmd { + my ($args, $server, $witem)=@_; + if ($args =~ m/^reload/) { + cmd_reload( $args, $server, $witem); + } elsif ($args =~ m/^save/) { + cmd_save( $args, $server, $witem); + } elsif ($args eq 'update') { + background({ + cmd => \&update, + last => [ \&print_update ], + }); + } elsif ($args =~ m/^update2/) { + cmd_update( $args, $server, $witem); + } else { + $args= $IRSSI{name}; + cmd_help( $args, $server, $witem); + } + #if (defined $witem) { + # $witem->printformat(MSGLEVEL_CLIENTCRAP, 'example_theme', + # $path, $path, $path); + #} else { + # Irssi::printformat(MSGLEVEL_CLIENTCRAP, 'example_theme', + # $path, $path, $path); + #} +} + +sub cmd_help { + my ($args, $server, $witem)=@_; + $args=~ s/\s+//g; + if ($IRSSI{name} eq $args) { + print_box($IRSSI{name}, "$IRSSI{name} help", $help); + #Irssi::print($help, MSGLEVEL_CLIENTCRAP); + Irssi::signal_stop(); + } +} + +sub sig_setup_changed { + $path= Irssi::settings_get_str($IRSSI{name}.'_path'); + if ( $path =~ m/^[~\.]/ ) { + $path = bsd_glob($path); + } elsif ($path !~ m#^/# ) { + $path= Irssi::get_irssi_dir()."/$path"; + } + if ( !-e $path ) { + mkdir $path; + } + +} + +sub UNLOAD { + save(); +} + +Irssi::theme_register([ + #'example_theme', '{hilight $0} $1 {error $2}', + 'box_header', '%R,--[%n$*%R]%n', + #'box_inside', '%R|%n $*', + 'box_footer', '%R`--<%n$*%R>->%n', +]); + +Irssi::signal_add('setup changed', \&sig_setup_changed); +#Irssi::signal_add('pidwait', \&sig_pidwait); + +Irssi::settings_add_str($IRSSI{name} ,$IRSSI{name}.'_path', 'scriptassist2'); + +Irssi::command_bind($IRSSI{name}, \&cmd); +my @cmds= qw/reload save update help/; +foreach ( @cmds ) { + Irssi::command_bind("$IRSSI{name} $_", \&cmd); +} +Irssi::command_bind('help', \&cmd_help); + +sig_setup_changed(); +init(); From 1144207c49130d645093294393787e9d4542dffb Mon Sep 17 00:00:00 2001 From: bw1 Date: Sun, 14 Feb 2021 22:33:26 +0100 Subject: [PATCH 02/22] pipe --- scripts/scriptassist2.pl | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/scripts/scriptassist2.pl b/scripts/scriptassist2.pl index 6e3327ae6..4d1676a39 100644 --- a/scripts/scriptassist2.pl +++ b/scripts/scriptassist2.pl @@ -60,29 +60,43 @@ sub background { @res= &{$cmd->{cmd}}(@{$cmd->{args}}); #store_fd \@res, $fh_w; my $yml=CPAN::Meta::YAML->new(\@res); + #my $fh_old = select $fh_w; + #local $|=1; + #select $fh_old; print $fh_w $yml->write_string(); close $fh_w; POSIX::_exit(1); } else { $cmd->{fh_r}=$fh_r; - $bg_process{$pid}=$cmd; - #Irssi::pidwait_add($pid); my $pipetag; my @args = ($pid, \$pipetag ); - $pipetag = Irssi::input_add(fileno($fh_r), INPUT_READ, \&sig_pidwait, \@args); + $pipetag = Irssi::input_add(fileno($fh_r), Irssi::INPUT_READ, \&sig_pipe, \@args); + $cmd->{pipetag} = $pipetag; + $bg_process{$pid}=$cmd; + Irssi::pidwait_add($pid); + } +} + +sub sig_pipe { + my ($pid, $pipetag) = @{$_[0]}; + debug "sig_pipe $pid"; + #debug \%bg_process; + if (exists $bg_process{$pid}) { + my $fh_r= $bg_process{$pid}->{fh_r}; + $bg_process{$pid}->{res_str} .= do { local $/; <$fh_r>; }; + } else { + Irssi::input_remove($$pipetag); } } sub sig_pidwait { - #my ($pid, $status) = @_; - my ($pid, $pipetag) = @{@_[0]}; + my ($pid, $status) = @_; + debug "sig_pidwait $pid"; if (exists $bg_process{$pid}) { #my @res= @{ fd_retrieve($bg_process{$pid}->{fh_r})}; - my $fh_r= $bg_process{$pid}->{fh_r}; - my $ystr = do { local $/; <$fh_r>; }; - close $fh_r; - Irssi::input_remove($$pipetag); - my $yml = CPAN::Meta::YAML->read_string($ystr); + close $bg_process{$pid}->{fh_r}; + Irssi::input_remove($bg_process{$pid}->{pipetag}); + my $yml = CPAN::Meta::YAML->read_string($bg_process{$pid}->{res_str}); my @res = @{ $yml->[0] }; $bg_process{$pid}->{res}=[@res]; if (exists $bg_process{$pid}->{last}) { @@ -164,8 +178,8 @@ sub update { } #my $ser = freeze $d; #return $ser; - return $d; - #return "hello from uptate"; + #return $d; + return "hello from uptate"; } sub print_update { @@ -251,7 +265,7 @@ sub UNLOAD { ]); Irssi::signal_add('setup changed', \&sig_setup_changed); -#Irssi::signal_add('pidwait', \&sig_pidwait); +Irssi::signal_add('pidwait', \&sig_pidwait); Irssi::settings_add_str($IRSSI{name} ,$IRSSI{name}.'_path', 'scriptassist2'); From 2e1c19dadd1b5522d5c1141a3b988e0563bb2d7c Mon Sep 17 00:00:00 2001 From: bw1 Date: Tue, 16 Feb 2021 06:48:26 +0100 Subject: [PATCH 03/22] info --- scripts/scriptassist2.pl | 206 ++++++++++++++++++++++++++++++--------- 1 file changed, 158 insertions(+), 48 deletions(-) diff --git a/scripts/scriptassist2.pl b/scripts/scriptassist2.pl index 4d1676a39..79576febf 100644 --- a/scripts/scriptassist2.pl +++ b/scripts/scriptassist2.pl @@ -7,7 +7,8 @@ use File::Fetch; use Time::Piece; use Digest::file qw/digest_file_hex/; -use Storable qw/store_fd fd_retrieve freeze thaw/; +use Digest::MD5 qw/md5_hex/; +use Text::Wrap; use debug; use Irssi; @@ -32,6 +33,33 @@ $VERSION %9description%9 $IRSSI{description} +%9commands%9 + /scriptassist check + Check all loaded scripts for new available versions + /scriptassist update + Update the selected or all script to the newest version + /scriptassist search + Search the script database + /scriptassist info + Display information about + /scriptassist ratings + Retrieve the average ratings of the the scripts + /scriptassist top + Retrieve the first top rated scripts + /scriptassist new + Display the newest scripts + /scriptassist rate