annotate .irssi/scripts/revolve.pl @ 401:89c7236a0899

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