summaryrefslogtreecommitdiffstats
path: root/scripts/queryresume.pl
blob: b4b090eeb92c997cf8104bc39c813f102b398469 (plain)
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
# QueryResume by Stefan Tomanek <stefan@pico.ruhr.de>
#
use strict;

use vars qw($VERSION %IRSSI);
$VERSION = '2003021201';
%IRSSI = (
    authors     => 'Stefan \'tommie\' Tomanek',
    contact     => 'stefan@pico.ruhr.de',
    name        => 'QueryResume',
    description => 'restores the last lines of a query on re-creation',
    license     => 'GPLv2',
    modules     => 'Date::Format File::Glob',
    changed     => $VERSION,
);  

use Irssi 20020324;
use Date::Format;
use File::Glob ':glob';

sub draw_box ($$$$) {
    my ($title, $text, $footer, $colour) = @_;
    my $box = '';
    $box .= '%R,--[%n%9%U'.$title.'%U%9%R]%n'."\n";
    foreach (split(/\n/, $text)) {
        $box .= '%R|%n '.$_."\n";
    }
    $box .= '%R`--<%n'.$footer.'%R>->%n';
    $box =~ s/%.//g unless $colour;
    return $box;
}

sub sig_window_item_new ($$) {
    my ($win, $witem) = @_;
    return unless (ref $witem && $witem->{type} eq 'QUERY');
    my @data;
    my $filename = Irssi::settings_get_str('autolog_path');
    my $servertag = $witem->{server}->{tag};
    my $name = lc $witem->{name};
    $filename =~ s/(\$tag|\$1)/$servertag/g;
    $filename =~ s/\$0/$name/g;
    my @lt = localtime(time);
    my $zone;
    $filename = strftime($filename, @lt, $zone);
    $filename =~ s/(\[|\])/\\$1/g;
    local *F;
    open(F, "<", bsd_glob($filename));
    my $lines = Irssi::settings_get_int('queryresume_lines');
    foreach (<F>) {
	unless (/^--- Log/) {
	    push(@data, $_);
	    shift(@data) if (@data > $lines);
	}
    }
    my $text;
    $text .= $_ foreach @data;
    $text =~ s/%/%%/g;
    $witem->print(draw_box('QueryResume', $text, $filename, 1), MSGLEVEL_CLIENTCRAP) if $text;
}

Irssi::settings_add_int($IRSSI{name}, 'queryresume_lines', 10);

Irssi::signal_add('window item new', 'sig_window_item_new');