Added SILC Thread Queue API
[crypto.git] / apps / irssi / scripts / buf.pl
1 use strict;
2 use vars qw($VERSION %IRSSI);
3
4 use Irssi qw(command signal_add signal_add_first active_win
5              settings_get_str settings_get_bool channels windows
6              settings_add_str settings_add_bool get_irssi_dir
7              window_find_refnum signal_stop);
8 $VERSION = '2.13';
9 %IRSSI = (
10     authors     => 'Juerd',
11     contact     => 'juerd@juerd.nl',
12     name        => 'Scroll buffer restorer',
13     description => 'Saves the buffer for /upgrade, so that no information is lost',
14     license     => 'Public Domain',
15     url         => 'http://juerd.nl/irssi/',
16     changed     => 'Mon May 13 19:41 CET 2002',
17     changes     => 'Severe formatting bug removed * oops, I ' .
18                    'exposed Irssi to ircII foolishness * sorry ' .
19                    '** removed logging stuff (this is a fix)',
20     note1       => 'This script HAS TO BE in your scripts/autorun!',
21     note2       => 'Perl support must be static or in startup',
22 );
23
24 # Q: How can I get a very smooth and clean upgrade?
25 #
26 # A: /set -clear upgrade_separator
27 #    /set upgrade_suppress_join ON (default)
28 #    /set channel_sync OFF
29
30 # Q: Can I use color in the upgrade_separator?
31 # Q: Is it possible to save my command history?
32 # Q: Can I prevent the screen from blinking?
33 # Q: Can you make it faster?
34 #
35 # A: Probably not, but if you can do it, tell me how.
36
37 use Irssi::TextUI;
38 use Data::Dumper;
39
40 my %suppress;
41
42 sub upgrade {
43     open BUF, sprintf('>%s/scrollbuffer', get_irssi_dir) or die $!;
44     print BUF join("\0", map $_->{server}->{address} . $_->{name}, channels), "\n";
45     for my $window (windows) {
46         next unless defined $window;
47         next if $window->{name} eq 'status';
48         my $view = $window->view;
49         my $line = $view->get_lines;
50         my $lines  = 0;
51         my $buf = '';
52         if (defined $line){
53             {
54                 $buf .= $line->get_text(1) . "\n";
55                 $line = $line->next;
56                 $lines++;
57                 redo if defined $line;
58             }
59         }
60         printf BUF "%s:%s\n%s", $window->{refnum}, $lines, $buf;
61     }
62     close BUF;
63     unlink sprintf("%s/sessionconfig", get_irssi_dir);
64     command 'layout save';
65     command 'save';
66 }
67
68 sub restore {
69     open BUF, sprintf('<%s/scrollbuffer', get_irssi_dir) or die $!;
70     my @suppress = split /\0/, <BUF>;
71     if (settings_get_bool 'upgrade_suppress_join') {
72         chomp $suppress[-1];
73         @suppress{@suppress} = (2) x @suppress;
74     }
75     active_win->command('^window scroll off');
76     while (my $bla = <BUF>){
77         chomp $bla;
78         my ($refnum, $lines) = split /:/, $bla;
79         next unless $lines;
80         my $window = window_find_refnum $refnum;
81         unless (defined $window){
82             <BUF> for 1..$lines;
83             next;
84         }
85         my $view = $window->view;
86         $view->remove_all_lines();
87         $view->redraw();
88         my $buf = '';
89         $buf .= <BUF> for 1..$lines;
90         my $sep = settings_get_str 'upgrade_separator';
91         $sep .= "\n" if $sep ne '';
92         $window->gui_printtext_after(undef, MSGLEVEL_CLIENTNOTICE, "$buf\cO$sep");
93         $view->redraw();
94     }
95     active_win->command('^window scroll on');
96     active_win->command('^scrollback end');
97 }
98
99 sub suppress {
100     my ($first, $second) = @_;
101     return
102         unless scalar keys %suppress
103         and settings_get_bool 'upgrade_suppress_join';
104     my $key = $first->{address} . 
105         (grep { (s/^://, /^[#!+&]/) } split ' ', $second)[0];
106     if (exists $suppress{$key} and $suppress{$key}--) {
107         signal_stop();
108         delete $suppress{$key} unless $suppress{$key};
109     }
110 }
111
112 settings_add_str  'buffer', 'upgrade_separator'     => '=Upgrade=';
113 settings_add_bool 'buffer', 'upgrade_suppress_join' => 1;
114
115 signal_add_first 'session save'    => 'upgrade';
116 signal_add_first 'session restore' => 'restore';
117 signal_add       'event 366'       => 'suppress';
118 signal_add       'event join'      => 'suppress';
119
120 unless (-f sprintf('%s/scripts/autorun/buf.pl', get_irssi_dir)) {
121     Irssi::print('PUT THIS SCRIPT IN ~/.irssi/scripts/autorun/ BEFORE /UPGRADING!!');
122 }