1# by Stefan 'tommie' Tomanek <stefan@pico.ruhr.de>
2#
3#
4
5use strict;
6
7use vars qw($VERSION %IRSSI);
8$VERSION = "20170204";
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
19use Irssi 20020324;
20use vars qw(%messages);
21
22sub 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
34sub 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 discard <nick>
41    Discard postponed messages to <nick>
42/postpone list
43    List postponed messages
44";
45    my $text = '';
46    foreach (split(/\n/, $help)) {
47        $_ =~ s/^\/(.*)$/%9\/$1%9/;
48        $text .= $_."\n";
49    }
50    print CLIENTCRAP &draw_box("Postpone", $text, "help", 1);
51}
52
53
54sub event_send_text ($$$) {
55    my ($line, $server, $witem) = @_;
56    return unless ($witem && $witem->{type} eq "CHANNEL");
57    if ($line =~ /^(\w+?): (.*)$/) {
58	my ($target, $msg) = ($1,$2);
59	if ($witem->nick_find($target)) {
60	    # Just leave me alone
61	    return;
62	} else {
63	    $witem->print("%B>>%n %U".$target."%U is not here, message has been postponed: \"".$line."\"", MSGLEVEL_CLIENTCRAP);
64	    push @{$messages{$server->{tag}}{$witem->{name}}{$target}}, $line;
65	    Irssi::signal_stop();
66	}
67    }
68}
69
70sub event_message_join ($$$$) {
71    my ($server, $channel, $nick, $address) = @_;
72    return unless (defined $messages{$server->{tag}});
73    return unless (defined $messages{$server->{tag}}{$channel});
74    return unless (defined $messages{$server->{tag}}{$channel}{$nick});
75    return unless (scalar(@{$messages{$server->{tag}}{$channel}{$nick}}) > 0);
76    my $chan = $server->channel_find($channel);
77    $chan->print("%B>>%n Sending postponed messages for ".$nick, MSGLEVEL_CLIENTCRAP);
78    while (scalar(@{$messages{$server->{tag}}{$channel}{$nick}}) > 0) {
79	my $msg = pop @{$messages{$server->{tag}}{$channel}{$nick}};
80	$server->command('MSG '.$channel.' '.$msg);
81    }
82
83}
84
85sub cmd_postpone ($$$) {
86    my ($args, $server, $witem) = @_;
87    my @arg = split(/ /, $args);
88    if (scalar(@arg) < 1) {
89	#foo
90    } elsif (($arg[0] eq 'discard' || $arg[0] eq 'flush') && defined $arg[1]) {
91	return unless ($witem && $witem->{type} eq "CHANNEL");
92	while (scalar(@{$messages{$server->{tag}}{$witem->{name}}{$arg[1]}}) > 0) {
93	    my $msg = pop @{$messages{$server->{tag}}{$witem->{name}}{$arg[1]}};
94	    $server->command('MSG '.$witem->{name}.' '.$msg) if $arg[0] eq 'flush';
95	}
96    } elsif ($arg[0] eq 'list') {
97	my $text;
98	foreach (keys %messages) {
99	    $text .= $_."\n";
100	    foreach my $channel (keys %{$messages{$_}}) {
101		$text .= " %U".$channel."%U \n";
102		foreach my $nick (sort keys %{$messages{$_}{$channel}}) {
103		    $text .= ' |'.$_."\n" foreach @{$messages{$_}{$channel}{$nick}};
104		}
105	    }
106	}
107	print CLIENTCRAP &draw_box('Postpone', $text, 'messages', 1);
108    } elsif ($arg[0] eq 'help') {
109	show_help();
110    }
111}
112
113Irssi::command_bind('postpone', \&cmd_postpone);
114
115Irssi::signal_add('send text', \&event_send_text);
116Irssi::signal_add('message join', \&event_message_join);
117
118print CLIENTCRAP "%B>>%n Postpone ".$VERSION." loaded: /postpone help for help";
119
120