1# QueryResume by Stefan Tomanek <stefan@pico.ruhr.de>
2#
3use strict;
4
5use vars qw($VERSION %IRSSI);
6$VERSION = '2003021201';
7%IRSSI = (
8    authors     => 'Stefan \'tommie\' Tomanek',
9    contact     => 'stefan@pico.ruhr.de',
10    name        => 'QueryResume',
11    description => 'restores the last lines of a query on re-creation',
12    license     => 'GPLv2',
13    modules     => 'Date::Format File::Glob',
14    changed     => $VERSION,
15);
16
17use Irssi 20020324;
18use Date::Format;
19use File::Glob ':glob';
20
21sub draw_box ($$$$) {
22    my ($title, $text, $footer, $colour) = @_;
23    my $box = '';
24    $box .= '%R,--[%n%9%U'.$title.'%U%9%R]%n'."\n";
25    foreach (split(/\n/, $text)) {
26        $box .= '%R|%n '.$_."\n";
27    }
28    $box .= '%R`--<%n'.$footer.'%R>->%n';
29    $box =~ s/%.//g unless $colour;
30    return $box;
31}
32
33sub sig_window_item_new ($$) {
34    my ($win, $witem) = @_;
35    return unless (ref $witem && $witem->{type} eq 'QUERY');
36    my @data;
37    my $filename = Irssi::settings_get_str('autolog_path');
38    my $servertag = $witem->{server}->{tag};
39    my $name = lc $witem->{name};
40    $filename =~ s/(\$tag|\$1)/$servertag/g;
41    $filename =~ s/\$0/$name/g;
42    my @lt = localtime(time);
43    my $zone;
44    $filename = strftime($filename, @lt, $zone);
45    $filename =~ s/(\[|\])/\\$1/g;
46    local *F;
47    open(F, "<", bsd_glob($filename));
48    my $lines = Irssi::settings_get_int('queryresume_lines');
49    foreach (<F>) {
50	unless (/^--- Log/) {
51	    push(@data, $_);
52	    shift(@data) if (@data > $lines);
53	}
54    }
55    my $text;
56    $text .= $_ foreach @data;
57    $text =~ s/%/%%/g;
58    $witem->print(draw_box('QueryResume', $text, $filename, 1), MSGLEVEL_CLIENTCRAP) if $text;
59}
60
61Irssi::settings_add_int($IRSSI{name}, 'queryresume_lines', 10);
62
63Irssi::signal_add('window item new', 'sig_window_item_new');
64
65