Home

Script: commorkers.pl

Analyze channels nicklists to find common lurkers. (for WeeChat ≥ 0.3.0)
Author: Al-Caveman, version 0.2, GPL3 — added: 2015-06-14, updated: 2015-06-22
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Al-Caveman <toraboracaveman@gmail.com>
# Licensed under GPL3
# https://github.com/Al-Caveman/commorkers

use warnings;
use strict;

# register script
weechat::register("commorkers", "Al-Caveman", "0.2", "GPL3", "Suppose that you have joined C many channels, then this script notifies you in each buffer about the movement of lurkers around you across all the C many channels.", "", "");

# option help
my $opt_ignore_nick = "ignore_nicks";
my $opt_ignore_all_triggered_notices = "ignore_all_triggered_notices";
my $opt_ignore_nick_triggered_notices = "ignore_nicks_triggered_notices";
unless (weechat::config_is_set_plugin($opt_ignore_nick)) {
    weechat::config_set_plugin($opt_ignore_nick, "");
}
unless (weechat::config_is_set_plugin($opt_ignore_all_triggered_notices)) {
    weechat::config_set_plugin($opt_ignore_all_triggered_notices, "off");
}
unless (weechat::config_is_set_plugin($opt_ignore_nick_triggered_notices)) {
    weechat::config_set_plugin($opt_ignore_nick_triggered_notices, "");
}

# colors
my $c_channel = weechat::color("chat_channel");
my $c_chat = weechat::color("chat");
my $c_chat_join = weechat::color("chat_prefix_join");
my $c_chat_quit = weechat::color("chat_prefix_quit");
my $c_emph = weechat::color("chat_prefix_error");

my $prefix = "$c_emph+++\t$c_emph";
my $notice = '';
my %notice_buffer;
sub common_lurkers {
    # get args
    my $data = shift;
    my $signal = shift;
    my $signal_data = shift;

    # process args
    my $weechat_signal_nick = weechat::info_get("irc_nick_from_host", $signal_data);
    my $weechat_signal_server;
    my $weechat_signal_channel;

    #weechat::print("", "data: $data");
    #weechat::print("", "signal: $signal");
    #weechat::print("", "signal_data: $signal_data");
    #weechat::print("", "    ".$weechat_signal_nick);

    if (($data eq 'j') || ($data eq 'p') || ($data eq 'q')) {
        $weechat_signal_server = (split(/,/, $signal))[0];
        #weechat::print("", "    server:".$weechat_signal_server);
        if (($data eq 'j') || ($data eq 'p')) {
            $weechat_signal_channel = (split(/ /, $signal_data))[2];
            #weechat::print("", "    channel:".$weechat_signal_channel);
        }
    }

    # get list of nicks to ignore their triggered notices
    my %ignore_nicks_trig_notice;
    if (weechat::config_is_set_plugin($opt_ignore_nick_triggered_notices)) {
        my $option_str = weechat::config_get_plugin($opt_ignore_nick_triggered_notices);
        $option_str =~ s/ //g;
        $ignore_nicks_trig_notice{lc $_} = 1 for grep {$_ !~ m/^ *$/} split(/,/, $option_str);
    }

    # see if triggered notices, as a whole, are disabled
    my $trig_notice_enabled = 1;
    if (weechat::config_is_set_plugin($opt_ignore_all_triggered_notices)) {
        my $option_str = weechat::config_get_plugin($opt_ignore_all_triggered_notices);
        $option_str =~ s/ //g;
        $trig_notice_enabled = 0 if $option_str =~ m/^on$/i;
    }

    # get current buffer
    my $buffer_current = weechat::current_buffer();
    my $buffer_current_name = weechat::buffer_get_string($buffer_current, "name");

    # get list of all buffers and nicks within them
    my %buffernicks;
    init_buffernicks(\%buffernicks);

    # print a masterlist of all common lurkers of the current buffer
    if (($data eq 'i')) {
        if ($buffer_current ne '') {
            my $commorkers_found = 0;
            if ($signal_data) {
                my @nick_buffers = sort grep {(defined $buffernicks{$_}{$signal_data}) && ($_ ne $buffer_current_name)} keys %buffernicks;
                if (scalar @nick_buffers) {
                    my $notice_tmp = "$prefix".weechat::color(weechat::nicklist_nick_get_string($buffer_current, weechat::nicklist_search_nick($buffer_current, "", $signal_data, "color")))."$signal_data$c_emph also lurks in: ".join(", ", map {$c_channel.$_.$c_emph} @nick_buffers);
                    $commorkers_found = 1;
                    weechat::print($buffer_current, $notice_tmp);
                }
            } else {
                # get nicklist of the current buffer
                my @buffer_current_nick_name_list = sort keys %{$buffernicks{$buffer_current_name}};

                # find which of the nicks in the current buffer also exist in other buffers
                for my $buffer_name (grep {$_ ne $buffer_current_name} sort keys %buffernicks) {
                    my @buffer_name_common_lurkers;
                    for my $buffer_current_nick_name (@buffer_current_nick_name_list) {
                        if (defined $buffernicks{$buffer_name}{$buffer_current_nick_name}) {
                            push @buffer_name_common_lurkers, $buffer_current_nick_name;
                        }
                    }
                    if (scalar @buffer_name_common_lurkers) {
                        my $notice_tmp = $prefix."common lurkers from $c_channel$buffer_name$c_chat$c_emph:\n".join(", ", map {weechat::color(weechat::nicklist_nick_get_string($buffer_current, weechat::nicklist_search_nick($buffer_current, "", $_), "color")).$_.$c_chat} @buffer_name_common_lurkers)."\n";
                        $commorkers_found = 1;
                        weechat::print($buffer_current, $notice_tmp);
                    }
                }
            }

            # no common lurkers found at all?
            if ($commorkers_found == 0) {
                    my $notice_tmp = $prefix."no common lurkage found";
                    $commorkers_found = 1;
                    $notice .= $notice_tmp;
                    weechat::print($buffer_current, $notice_tmp);
            }
        } else {
            my $notice_tmp = $prefix."must execute /commorkers in a channel";
            weechat::print($buffer_current, $notice_tmp);
        }
    }

    # handle a join/part event by printing all channels the joining nick is lurking
    # in
    if ((($data eq 'j') || ($data eq 'p')) && $trig_notice_enabled) {
        # get the buffer where the event occurred
        my $buffer_event_name = "$weechat_signal_server.$weechat_signal_channel";
        my $buffer_event = weechat::buffer_search("irc", $buffer_event_name);

        # find channels where the subject nick is lurking in
        my @lurker_common_buffers;
        for my $buffer_name (grep {$_ ne $buffer_event_name} sort keys %buffernicks) {
            if ((defined $buffernicks{$buffer_name}{$weechat_signal_nick}) && !(defined $ignore_nicks_trig_notice{$weechat_signal_nick})) {
                push @lurker_common_buffers, $buffer_name;
            }
        }

        # print notice in the channel where the join/part occurred
        if (scalar @lurker_common_buffers) {
            my $notice_tmp = join(", ", sort map {$c_channel.$_.$c_emph} @lurker_common_buffers)."\n";
            $notice .= $prefix."a lurker from: $notice_tmp";
            #weechat::print($buffer_event, $notice);

            # also print an incremental notification to all buffers were said
            # nick is lurking in
            for my $buffer_nickexists_name (@lurker_common_buffers) {
                my $buffer_nickexists = weechat::buffer_search("", $buffer_nickexists_name);
                my $c_lurker_nick = weechat::color(weechat::nicklist_nick_get_string($buffer_nickexists, weechat::nicklist_search_nick($buffer_nickexists, "", $weechat_signal_nick), "color"));
                my $event_phrase = $c_chat_join."extended$c_emph lurkage to";
                $event_phrase = $c_chat_quit."ceased$c_emph lurkage from" if $data eq 'p';
                weechat::print($buffer_nickexists, $prefix."$c_lurker_nick$weechat_signal_nick$c_emph has $event_phrase: $c_channel$weechat_signal_channel$c_chat");
            }
        }

    }

    # handle a quit event by printing all channels the joining nick is lurking
    # in
    if (($data eq 'q') && $trig_notice_enabled) {
        for my $buffer_event_name (sort grep {defined $buffernicks{$_}{$weechat_signal_nick}} keys %buffernicks) {
            # get the buffer where the event occurred
            my $buffer_event = weechat::buffer_search("irc", $buffer_event_name);

            # find channels where the subject nick is lurking in
            my @lurker_common_buffers;
            for my $buffer_name (grep {$_ ne $buffer_event_name} sort keys %buffernicks) {
                if ((defined $buffernicks{$buffer_name}{$weechat_signal_nick}) && !(defined $ignore_nicks_trig_notice{$weechat_signal_nick})) {
                    push @lurker_common_buffers, $buffer_name;
                }
            }

            # print notice in the channel where the quit occurred
            if (scalar @lurker_common_buffers) {
                my $notice_tmp = $prefix."also lurked in: ".join(", ", sort map {$c_channel.$_.$c_emph} @lurker_common_buffers)."\n";
                $notice_buffer{$buffer_event} = $notice_tmp;
                #weechat::print($buffer_event, $notice_tmp);
            }
        }
    }

    return weechat::WEECHAT_RC_OK;
}

sub init_buffernicks {
    # get args
    my $buffernicks_ref = shift;

    # what are the nicks to ignore?
    my %ignore_nicks;
    if (weechat::config_is_set_plugin($opt_ignore_nick)) {
        my $option_str = weechat::config_get_plugin($opt_ignore_nick);
        $option_str =~ s/ //g;
        $ignore_nicks{lc $_} = 1 for grep {$_ !~ m/^ *$/} split(/,/, $option_str);
    }

    # populate it with new entries
    my $list_buffer = weechat::infolist_get("buffer", "", "");
    if ($list_buffer) {
        while (weechat::infolist_next($list_buffer)) {
            # print buffer name
            my $buffer = weechat::infolist_pointer($list_buffer, "pointer");
            my $buffer_name = weechat::infolist_string($list_buffer, "name");
            my $buffer_name_channel = weechat::buffer_get_string($buffer, "localvar_channel");
            my $buffer_name_server = weechat::buffer_get_string($buffer, "localvar_server");
            my $buffer_mynick = weechat::buffer_get_string($buffer, "localvar_nick");

            # get list of nicks for the buffer at hand
            my $list_nicks = weechat::infolist_get("irc_nick", "", "$buffer_name_server,$buffer_name_channel");
            if ($list_nicks) {
                while (weechat::infolist_next($list_nicks)) {
                    my $nick_name = weechat::infolist_string($list_nicks, "name");
                    if (($nick_name ne $buffer_mynick) && !(defined $ignore_nicks{lc $nick_name})) {
                        ${$buffernicks_ref}{$buffer_name}{$nick_name} = $buffer;
                    }
                }
            }
            weechat::infolist_free($list_nicks);
        }
    }
    weechat::infolist_free($list_buffer);
}

sub print_notice {
    # get args
    my $data = shift;
    my $signal = shift;
    my $signal_data = shift;

    # process args
    my $weechat_signal_nick = weechat::info_get("irc_nick_from_host", $signal_data);
    my $weechat_signal_server;
    my $weechat_signal_channel;

    #weechat::print("", "data: $data");
    #weechat::print("", "signal: $signal");
    #weechat::print("", "signal_data: $signal_data");
    #weechat::print("", "    ".$weechat_signal_nick);

    if (($data eq 'j') || ($data eq 'p') || ($data eq 'q')) {
        $weechat_signal_server = (split(/,/, $signal))[0];
        #weechat::print("", "    server:".$weechat_signal_server);
        if (($data eq 'j') || ($data eq 'p')) {
            $weechat_signal_channel = (split(/ /, $signal_data))[2];
            #weechat::print("", "    channel:".$weechat_signal_channel);
        }
    }

    if ($data eq 'q') {
        for my $buffer_event (keys %notice_buffer) {
            weechat::print($buffer_event, $notice_buffer{$buffer_event});
        }
        %notice_buffer = ();

    } else {
        if ($notice ne '') {
            # get the buffer where the event occurred
            my $buffer_event_name = "$weechat_signal_server.$weechat_signal_channel";
            my $buffer_event = weechat::buffer_search("irc", $buffer_event_name);

            # print notice
            weechat::print($buffer_event, $notice) if ($buffer_event ne '');
            $notice = '';
        }
    }

    return weechat::WEECHAT_RC_OK;
}

# command and signal hooks
weechat::hook_command("commorkers", "Finds common lurkers across different channels", "[NICK]", "NICK: find channels that overlap with NICK (optional argument)", "", "common_lurkers","i");
weechat::hook_signal('*,irc_in_join', "common_lurkers", "j");
weechat::hook_signal('*,irc_in_part', "common_lurkers", "p");
weechat::hook_signal('*,irc_in_quit', "common_lurkers", "q");
weechat::hook_signal('*,irc_in2_join', "print_notice", "j");
weechat::hook_signal('*,irc_in2_part', "print_notice", "p");
weechat::hook_signal('*,irc_in2_quit', "print_notice", "q");