comparison .irssi/scripts/revolve.pl @ 392:6340c1a17845

Merge.
author Augie Fackler <raf@durin42.com>
date Sat, 19 Mar 2016 13:35:07 -0400
parents 4276071d7cf3
children
comparison
equal deleted inserted replaced
391:24d75bc4a3a1 392:6340c1a17845
1 use Irssi;
2 use strict;
3 use Irssi::TextUI;
4 use vars qw($VERSION %IRSSI);
5
6 $VERSION = "0.0.2";
7 %IRSSI = (
8 authors => 'Ryan Freebern',
9 contact => 'ryan@freebern.org',
10 name => 'revolve',
11 description => 'Summarizes multiple sequential joins/parts/quits.',
12 license => 'GPL v2 or later',
13 url => 'http://github.com/rfreebern/irssi-revolving-door',
14 );
15
16 # Based on compact.pl by Wouter Coekaerts <wouter@coekaerts.be>
17 # http://wouter.coekaerts.be/irssi/scripts/compact.pl.html
18
19 my %summary_lines;
20
21 sub summarize {
22 my ($server, $channel, $nick, $new_nick, $type) = @_;
23
24 my $window = $server->window_find_item($channel);
25 return if (!$window);
26 my $view = $window->view();
27 my $check = $server->{tag} . ':' . $channel;
28
29 $view->set_bookmark_bottom('bottom');
30 my $last = $view->get_bookmark('bottom');
31 my $secondlast = $last->prev();
32
33 # Remove the last line, which should have the join/part/quit message.
34 $view->remove_line($last);
35
36 # If the second-to-last line is a summary line, parse it.
37 my %door = ('Joins' => [], 'Parts' => [], 'Quits' => [], 'Nicks' => []);
38 my @summarized = ();
39 if ($secondlast and %summary_lines and $secondlast->{'_irssi'} == $summary_lines{$check}) {
40 my $summary = Irssi::strip_codes($secondlast->get_text(1));
41 @summarized = split(/ -- /, $summary);
42 foreach my $part (@summarized) {
43 my ($type, $nicks) = split(/: /, $part);
44 $door{$type} = [ split(/, /, $nicks) ];
45 }
46 $view->remove_line($secondlast);
47 }
48
49 if ($type eq '__revolving_door_join') { # Join
50 push(@{$door{'Joins'}}, $nick);
51 @{$door{'Parts'}} = grep { $_ ne $nick } @{$door{'Parts'}} if (scalar @{$door{'Parts'}});
52 @{$door{'Quits'}} = grep { $_ ne $nick } @{$door{'Quits'}} if (scalar @{$door{'Quits'}});
53 } elsif ($type eq '__revolving_door_quit') { # Quit
54 push(@{$door{'Quits'}}, $nick) if (!grep(/^\Q$nick\E$/, @{$door{'Joins'}}));
55 @{$door{'Joins'}} = grep { $_ ne $nick } @{$door{'Joins'}} if (scalar @{$door{'Joins'}});
56 } elsif ($type eq '__revolving_door_part') { # Part
57 push(@{$door{'Parts'}}, $nick) if (!grep(/^\Q$nick\E$/, @{$door{'Joins'}}));;
58 @{$door{'Joins'}} = grep { $_ ne $nick } @{$door{'Joins'}} if (scalar @{$door{'Joins'}});;
59 } else { # Nick
60 my $nick_found = 0;
61 foreach my $known_nick (@{$door{'Nicks'}}) {
62 my ($orig_nick, $current_nick) = split(/ -> /, $known_nick);
63 if ($new_nick eq $orig_nick) { # Changed nickname back to original.
64 @{$door{'Nicks'}} = grep { $_ ne "$orig_nick -> $current_nick" } @{$door{'Nicks'}};
65 $nick_found = 1;
66 last;
67 } elsif ($current_nick eq $nick) {
68 $_ =~ s/\b\Q$current_nick\E\b/$new_nick/ foreach @{$door{'Nicks'}};
69 $nick_found = 1;
70 last;
71 }
72 }
73 if (!$nick_found) {
74 push(@{$door{'Nicks'}}, "$nick -> $new_nick");
75 }
76 # Update nicks in join/part/quit lists.
77 foreach my $part (qw/Joins Parts Quits/) {
78 $_ =~ s/\b\Q$nick\E\b/$new_nick/ foreach @{$door{$part}};
79 }
80 }
81
82 @summarized = ();
83 foreach my $part (qw/Joins Parts Quits Nicks/) {
84 if (scalar @{$door{$part}}) {
85 push @summarized, "\%W$part:\%n " . join(', ', @{$door{$part}});
86 }
87 }
88
89 my $summary = join(' -- ', @summarized);
90 $window->print($summary, MSGLEVEL_NEVER);
91
92 # Get the line we just printed so we can log its ID.
93 $view->set_bookmark_bottom('bottom');
94 $last = $view->get_bookmark('bottom');
95 $summary_lines{$check} = $last->{'_irssi'};
96
97 $view->redraw();
98 }
99
100 sub summarize_join {
101 my ($server, $channel, $nick, $address, $reason) = @_;
102 &summarize($server, $channel, $nick, 0, '__revolving_door_join');
103 }
104
105 sub summarize_quit {
106 my ($server, $nick, $address, $reason) = @_;
107 my @channels = $server->channels();
108 foreach my $channel (@channels) {
109 my $window = $server->window_find_item($channel->{name});
110 next if (!$window);
111 my $view = $window->view();
112 $view->set_bookmark_bottom('bottom');
113 my $last = $view->get_bookmark('bottom');
114 my $last_text = $last->get_text(1);
115 if ($last_text =~ m/\Q$nick\E.*?has quit/) {
116 &summarize($server, $channel->{name}, $nick, 0, '__revolving_door_quit');
117 }
118 }
119 }
120
121 sub summarize_part {
122 my ($server, $channel, $nick, $address, $reason) = @_;
123 &summarize($server, $channel, $nick, 0, '__revolving_door_part');
124 }
125
126 sub summarize_nick {
127 my ($server, $new_nick, $old_nick, $address) = @_;
128 my @channels = $server->channels();
129 foreach my $channel (@channels) {
130 my $channel_nick = $channel->nick_find($new_nick);
131 if (defined $channel_nick) {
132 &summarize($server, $channel->{name}, $old_nick, $new_nick, '__revolving_door_nick');
133 }
134 }
135 }
136
137 Irssi::signal_add_priority('message join', \&summarize_join, Irssi::SIGNAL_PRIORITY_LOW + 1);
138 Irssi::signal_add_priority('message part', \&summarize_part, Irssi::SIGNAL_PRIORITY_LOW + 1);
139 Irssi::signal_add_priority('message quit', \&summarize_quit, Irssi::SIGNAL_PRIORITY_LOW + 1);
140 Irssi::signal_add_priority('message nick', \&summarize_nick, Irssi::SIGNAL_PRIORITY_LOW + 1);