# QueryResume(d) by Bernd Kilga # # Based on QueryResume by Stefan 'tommie' Tomanek # # Perl crash course + regexp magic by tramsan # # # use strict; use vars qw($VERSION %IRSSI); $VERSION = '200719021851'; %IRSSI = ( authors => 'Bernd \'frame\' Kilga', contact => 'beta@console.cc', name => 'QueryResumed', description => 'restores the last lines of a window on re-creation', license => 'GPLv2', modules => 'File::Glob', changed => $VERSION, ); use Irssi 20020324; use File::Glob ':glob'; sub sig_window_item_new ($$) { my ($win, $witem) = @_; my $debug = 0; my @data_final; my $replace_tag = $witem->{server}->{tag}; my $replace_name = lc $witem->{name}; my $log_file_pattern = Irssi::settings_get_str('autolog_path'); my $lines = Irssi::settings_get_int('queryresumed_lines'); my $show_log_seperator = Irssi::settings_get_bool('queryresumed_show_log_seperator'); my $query_only = Irssi::settings_get_bool('queryresumed_query_only'); if ($query_only) { return unless (ref $witem && $witem->{type} eq 'QUERY'); } else { return unless (ref $witem); } $log_file_pattern =~ s/(\$tag|\$1)/$replace_tag/g; $log_file_pattern =~ s/\$0/$replace_name/g; # provided by Joerg Jaspert - but it doesn't work on non Debian Machines. # $log_file_pattern =~ s/\$tag\b|\$\{tag\}|\$1\b|\$\{1\}/$replace_tag/g; # $log_file_pattern =~ s/\$0\b|\$\{0\}/$replace_name/g; $log_file_pattern =~ s/(\[|\])/\\$1/g; $log_file_pattern =~ s/\%Y%m%d/\*/g; # FIXME: lacks support for all RFC date wildcards if ($debug) { $witem->print("log file pattern: " . $log_file_pattern); } my @files = bsd_glob($log_file_pattern, GLOB_TILDE) or $!; if ($debug) { $witem->print("number of files found: " . ($#files + 1)); } for ( my $i = 0; $i <= $#files; $i++ ) { if ($#data_final <= ($lines + 1)) { if ($debug) { $witem->print("processing file: " . $files[$#files-$i]); } local *F; open(F, $files[$#files-$i]) or $!; my @data; foreach (reverse ) { if ( index($_, "-!- Irssi:") == -1 && index($_, "--- Log closed") == -1 && index($_, "--- Log opened") == -1 && index($_, "*** You are connected") == -1 && index($_, "-NICKSERV") == -1 && ($#data_final + $#data) <= ($lines + 2) ) { unshift(@data, $_); } } close(F); if ($#data > -1) { if ($show_log_seperator) { unshift(@data, "--- Log " . $files[$#files-$i] . "\n"); } @data_final = (@data, @data_final); } } } reverse @data_final; my $text = join '', @data_final; my $view = $win->view; if ($debug == 0) { # !debug: $witem->print requires this portion to be disabled $view->remove_all_lines(); $view->redraw(); $win->gui_printtext_after(undef, MSGLEVEL_NEVER, "$text"); $view->redraw(); } } Irssi::settings_add_int('queryresumed', 'queryresumed_lines', 32); Irssi::settings_add_bool('queryresumed', 'queryresumed_show_log_seperator', 0); Irssi::settings_add_bool('queryresumed', 'queryresumed_query_only', 1); Irssi::signal_add('window item new', 'sig_window_item_new');