]> git.sthu.org Git - shutils.git/blob - dotfiles/irssi/scripts/postpone.pl
vim: add color scheme
[shutils.git] / dotfiles / irssi / scripts / postpone.pl
1 # by Stefan 'tommie' Tomanek <stefan@pico.ruhr.de>
2 #
3 #
4
5 use strict;
6
7 use vars qw($VERSION %IRSSI);
8 $VERSION = "20030208";
9 %IRSSI = (
10 authors => "Stefan 'tommie' Tomanek",
11 contact => "stefan\@pico.ruhr.de",
12 name => "postpone",
13 description => "Postpones messages sent to a splitted user and resends them when the nick rejoins",
14 license => "GPLv2",
15 changed => "$VERSION",
16 commands => "postpone"
17 );
18
19 use Irssi 20020324;
20 use vars qw(%messages);
21
22 sub draw_box ($$$$) {
23 my ($title, $text, $footer, $colour) = @_;
24 my $box = '';
25 $box .= '%R,--[%n%9%U'.$title.'%U%9%R]%n'."\n";
26 foreach (split(/\n/, $text)) {
27 $box .= '%R|%n '.$_."\n";
28 }
29 $box .= '%R`--<%n'.$footer.'%R>->%n';
30 $box =~ s/%.//g unless $colour;
31 return $box;
32 }
33
34 sub show_help() {
35 my $help="Postpone $VERSION
36 /postpone help
37 Display this help
38 /postpone flush <nick>
39 Flush postponed messages to <nick>
40 /postpone list
41 List postponed messages
42 ";
43 my $text = '';
44 foreach (split(/\n/, $help)) {
45 $_ =~ s/^\/(.*)$/%9\/$1%9/;
46 $text .= $_."\n";
47 }
48 print CLIENTCRAP &draw_box("Postpone", $text, "help", 1);
49 }
50
51
52 sub event_send_text ($$$) {
53 my ($line, $server, $witem) = @_;
54 return unless ($witem && $witem->{type} eq "CHANNEL");
55 if ($line =~ /^(\w+?): (.*)$/) {
56 my ($target, $msg) = ($1,$2);
57 if ($witem->nick_find($target)) {
58 # Just leave me alone
59 return;
60 } else {
61 $witem->print("%B>>%n %U".$target."%U is not here, message has been postponed: \"".$line."\"", MSGLEVEL_CLIENTCRAP);
62 push @{$messages{$server->{tag}}{$witem->{name}}{$target}}, $line;
63 Irssi::signal_stop();
64 }
65 }
66 }
67
68 sub event_message_join ($$$$) {
69 my ($server, $channel, $nick, $address) = @_;
70 return unless (defined $messages{$server->{tag}});
71 return unless (defined $messages{$server->{tag}}{$channel});
72 return unless (defined $messages{$server->{tag}}{$channel}{$nick});
73 return unless (scalar(@{$messages{$server->{tag}}{$channel}{$nick}}) > 0);
74 my $chan = $server->channel_find($channel);
75 $chan->print("%B>>%n Sending postponed messages for ".$nick, MSGLEVEL_CLIENTCRAP);
76 while (scalar(@{$messages{$server->{tag}}{$channel}{$nick}}) > 0) {
77 my $msg = pop @{$messages{$server->{tag}}{$channel}{$nick}};
78 $server->command('MSG '.$channel.' '.$msg);
79 }
80
81 }
82
83 sub cmd_postpone ($$$) {
84 my ($args, $server, $witem) = @_;
85 my @arg = split(/ /, $args);
86 if (scalar(@arg) < 1) {
87 #foo
88 } elsif ($arg[0] eq 'flush' && defined $arg[1]) {
89 return unless ($witem && $witem->{type} eq "CHANNEL");
90 while (scalar(@{$messages{$server->{tag}}{$witem->{name}}{$arg[1]}}) > 0) {
91 my $msg = pop @{$messages{$server->{tag}}{$witem->{name}}{$arg[1]}};
92 $server->command('MSG '.$witem->{name}.' '.$msg);
93 }
94 } elsif ($arg[0] eq 'list') {
95 my $text;
96 foreach (keys %messages) {
97 $text .= $_."\n";
98 foreach my $channel (keys %{$messages{$_}}) {
99 $text .= " %U".$channel."%U \n";
100 foreach my $nick (sort keys %{$messages{$_}{$channel}}) {
101 $text .= ' |'.$_."\n" foreach @{$messages{$_}{$channel}{$nick}};
102 }
103 }
104 }
105 print CLIENTCRAP &draw_box('Postpone', $text, 'messages', 1);
106 } elsif ($arg[0] eq 'help') {
107 show_help();
108 }
109 }
110
111 Irssi::command_bind('postpone', \&cmd_postpone);
112
113 Irssi::signal_add('send text', \&event_send_text);
114 Irssi::signal_add('message join', \&event_message_join);
115
116 print CLIENTCRAP "%B>>%n Postpone ".$VERSION." loaded: /postpone help for help";
117