aboutsummaryrefslogtreecommitdiffstats
path: root/docs/cmpusers.pl
blob: d6080e42f844a0601e8c5c2a407d188ee636e1c5 (plain) (blame)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use strict;
use warnings;

my $VERSION = "0.3";
my %IRSSI = (
  authors => 'Bazerka <bazerka@quakenet.org>',
  contact => 'bazerka@quakenet.org',
  name => 'cmpusers',
  description => 'compare channels for two users',
  licence => 'BSD',
  url => 'n/a',
);

use Irssi;
use Data::Dumper;

sub request_whois($$);
sub event_whois_user($$);
sub event_whois_channels($$);
sub event_whois_end($$);
sub event_whois_nosuchnick($$);
sub event_whois_timeout($$);
sub compare_channels();
sub cleanup();
sub to_casemap($);
sub cmd_cmpusers($$$);

my $debug = 0;

my $running = 0;
my @nick_list = ();
my %nick_info = ();
my %isupport = ();

# bahamut - % hidden chan;
my $custom_chanprefix = '%';


sub request_whois ($$) {
  my ($server, $nick) = @_;

  $server->redirect_event(
    'whois',
    1,
    $nick,
    0,
    'redir cmpusers_whois_timeout',
    {
      'event 311' => 'redir cmpusers_whois_user',
      'event 318' => 'redir cmpusers_whois_end',
      'event 319' => 'redir cmpusers_whois_channels',
      'event 401' => 'redir cmpusers_whois_nosuchnick',
      ''          => 'event empty',
    }
  );

  $server->send_raw("WHOIS $nick");
}


sub event_whois_user ($$) {
  my ($server, $data) = @_;

  my ($nick, $user, $host) = (split / +/, $data, 6)[1,2,3];
  my $casenick = to_casemap($nick);

  $nick_info{$casenick} = {
    'nick'     => $nick,
    'username' => $user,
    'hostname' => $host,
    'channels' => {},
  };

  Irssi::print("cmpusers whois_user: $nick!$user\@$host", MSGLEVEL_CLIENTCRAP) if $debug;
}


sub event_whois_channels ($$) {
  my ($server, $data) = @_;

  my ($nick, $channels) = (split / +/, $data, 3)[1,2];
  my $casenick = to_casemap($nick);
  $channels =~ s/^://;

  my @channel_list = split / /, $channels;

  foreach my $channel (@channel_list) {
    my $prefix = '';
    my $unknown_prefix = '';
    my $stripped_channel;

    Irssi::print("cmpusers whois_channels: channel \"$channel\"", MSGLEVEL_CLIENTCRAP) if $debug;
    for (my $i=0; $i<length($channel); $i++) {
      my $char = substr($channel, $i, 1);
      Irssi::print("cmpusers whois_channels: char \"$char\"", MSGLEVEL_CLIENTCRAP) if $debug;;

      if (index ($custom_chanprefix, $char) > -1) {
        $stripped_channel .= $char;
        Irssi::print("cmpusers whois_channels: stripped_channel \"$stripped_channel\"", MSGLEVEL_CLIENTCRAP) if $debug;
        next;
      }

      if (index ($isupport{'CHANTYPES'}, $char) > -1) {
        $stripped_channel .= substr($channel, $i, length($channel));
        Irssi::print("cmpusers whois_channels: stripped_chan \"$stripped_channel\"", MSGLEVEL_CLIENTCRAP) if $debug;
        last;
      }
      if (index ($isupport{'PREFIX'}, $char) > -1) {
        $prefix .= $char;
        Irssi::print("cmpusers whois_channels: prefix \"$prefix\"", MSGLEVEL_CLIENTCRAP) if $debug;
      } else {
        $unknown_prefix .= $char;
        Irssi::print("cmpusers whois_channels: unknown_prefix \"$prefix\"", MSGLEVEL_CLIENTCRAP) if $debug;
      }
    }

    $nick_info{$casenick}{'channels'}{$stripped_channel}{prefix} = $prefix;
    $nick_info{$casenick}{'channels'}{$stripped_channel}{unknown_prefix} = $unknown_prefix;
    Irssi::print("cmpuser whois_channels: stripped_channel \"$stripped_channel\"", MSGLEVEL_CLIENTCRAP) if $debug;
  }

  Irssi::print("cmpusers whois_channels: $nick - $channels", MSGLEVEL_CLIENTCRAP) if $debug;
}


sub event_whois_end ($$) {
  my ($server, $data) = @_;
  my ($nick) = (split / +/, $data, 3)[1];
  Irssi::print("cmpusers whois_end: $nick", MSGLEVEL_CLIENTCRAP) if $debug;

  return if $running == 0; # catch 318 -> 401 (nosuchnick followed by endofwhois)

  if (to_casemap($nick) eq $nick_list[0]) {
    request_whois($server, $nick_list[1]);
  } else {
    compare_channels();
  }
}


sub event_whois_nosuchnick ($$) {
  my ($server, $data) = @_;
  my $nick = (split / +/, $data, 4)[1];
  Irssi::active_win->print("cmpusers error: no such nick $nick - aborting.", MSGLEVEL_CLIENTCRAP);
  cleanup();
}


sub event_whois_timeout($$) {
  my ($server, $data) = @_;
  Irssi::print("cmpusers whois_timeout", MSGLEVEL_CLIENTCRAP) if $debug;
  Irssi::active_win->print("cmpusers error: whois timed out or failed. please try again.", MSGLEVEL_CLIENTCRAP);
  cleanup();
}


sub cleanup () {
  $running = 0;
  print Dumper \@nick_list if $debug;
  print Dumper \%nick_info if $debug;
  print Dumper \%isupport if $debug;
  @nick_list = ();
  %nick_info = ();
  %isupport = ();
  Irssi::print("cmpusers cleanup", MSGLEVEL_CLIENTCRAP) if $debug;
}


sub to_casemap ($) {
  my $nick = shift;
  my $type = $isupport{'CASEMAPPING'};

  for (my $i = 0; $i < length($nick); $i++) {
    my $ord = ord(substr($nick, $i, 1));

    if ($type == 1) { # rfc1459
      substr($nick, $i, 1) = ($ord >= 65 && $ord <= 94) ? chr($ord + 32) : chr($ord);
    } elsif ($type == 2) { # ascii
      substr($nick, $i, 1) = ($ord >= 65 && $ord <= 90) ? chr($ord + 32) : chr($ord);
    } else { # unknown
    }
  }

  return $nick;
}


sub compare_channels() {
  my @intersection = my @uniq_chans_0 = my @uniq_chans_1 = ();
  my %count = ();

  foreach my $element ( keys %{$nick_info{$nick_list[0]}{'channels'}}, keys %{$nick_info{$nick_list[1]}{'channels'}} ) {
    $count{$element}++;
    # nasty :<
    if ( exists $nick_info{$nick_list[0]}{'channels'}{$element} && (!exists $nick_info{$nick_list[1]}{'channels'}{$element})) {
      push @uniq_chans_0, $element;
    } elsif ((!exists $nick_info{$nick_list[0]}{'channels'}{$element}) && exists $nick_info{$nick_list[1]}{'channels'}{$element}) {
      push @uniq_chans_1, $element;
    }
  }
  foreach my $element (keys %count) {
    push @intersection, $element if $count{$element} == 2;
  }

  my $common_chans = join " ", sort @intersection;
  my $uniq0 = join " ", sort @uniq_chans_0;
  my $uniq1 = join " ", sort @uniq_chans_1;

  # workaround Irssi's crappy % handling. No, really, this irssi issue needs fixing.
  $common_chans =~ s/%/%%/g;
  $uniq0 =~ s/%/%%/g;
  $uniq1 =~ s/%/%%/g;

  Irssi::active_win->print(sprintf('Comparing user <%s!%s@%s> with user <%s!%s@%s>',
                                   $nick_info{$nick_list[0]}{'nick'},
                                   $nick_info{$nick_list[0]}{'username'},
                                   $nick_info{$nick_list[0]}{'hostname'},
                                   $nick_info{$nick_list[1]}{'nick'},
                                   $nick_info{$nick_list[1]}{'username'},
                                   $nick_info{$nick_list[1]}{'hostname'}), MSGLEVEL_CLIENTCRAP );

  Irssi::active_win->print(sprintf("Common channels for both users: %s", $common_chans), MSGLEVEL_CLIENTCRAP);
  Irssi::active_win->print(sprintf("Unique channels for %s: %s", $nick_info{$nick_list[0]}{'nick'}, $uniq0), MSGLEVEL_CLIENTCRAP);
  Irssi::active_win->print(sprintf("Unique channels for %s: %s", $nick_info{$nick_list[1]}{'nick'}, $uniq1), MSGLEVEL_CLIENTCRAP);
  cleanup();
}


sub cmd_cmpusers($$$) {
  my ($args, $server, $witem) = @_;
  $args = lc $args;
  my @nicks = split /\s+/, $args;

  if (scalar @nicks != 2) {
    Irssi::active_win->print("cmpusers error: please supply two nickname arguments.", MSGLEVEL_CLIENTCRAP);
    return;
  }

  if ($nicks[0] eq $nicks[1]) {
    Irssi::active_win->print("cmpusers error: please supply two unique nicknames.", MSGLEVEL_CLIENTCRAP);
    return;
  }

  if (!defined $server) {
    Irssi::active_win->print("cmpusers error: active window not connected to a server.", MSGLEVEL_CLIENTCRAP);
    return;
  }

  if ($server->{'chat_type'} ne 'IRC') {
    Irssi::active_win->print("cmpusers error: server is not of type IRC.", MSGLEVEL_CLIENTCRAP);
  }

  if ($running) {
    Irssi::active_win->print("cmpusers error: a request is currently being processed - please try again shortly.", MSGLEVEL_CLIENTCRAP);
    return;
  }

  $running = 1;

  if ($server->{'isupport_sent'}) {
    if (defined $server->isupport('PREFIX')) {
      my $prefix = $server->isupport('PREFIX');
      my $l = (length($prefix) - 2) / 2;
      $isupport{'PREFIX'} = substr($prefix, $l + 2, $l);
      Irssi::print("cmpusers prefix: $prefix", MSGLEVEL_CLIENTCRAP) if $debug;
    }
    if (defined $server->isupport('CASEMAPPING')) {
      my $casemap = $server->isupport('CASEMAPPING');
      $isupport{'CASEMAPPING'} = $casemap eq 'rfc1459' ? 1
                               : $casemap eq 'ascii'   ? 2
                               : 3;
      Irssi::print("cmpusers casemap: $casemap", MSGLEVEL_CLIENTCRAP) if $debug;
    }
    if (defined $server->isupport('CHANTYPES')) {
      $isupport{'CHANTYPES'} = $server->isupport('CHANTYPES');
      Irssi::print("cmpusers chantypes: $isupport{CHANTYPES}", MSGLEVEL_CLIENTCRAP) if $debug;
    }
  }

  for (my $i=0; $i <= $#nicks; $i++) {
    $nicks[$i] = to_casemap($nicks[$i]);
  }

  push @nick_list, @nicks;
  request_whois($server, $nicks[0]);
}


Irssi::signal_add(
  {
    'redir cmpusers_whois_user'       => \&event_whois_user,
    'redir cmpusers_whois_channels'   => \&event_whois_channels,
    'redir cmpusers_whois_end'        => \&event_whois_end,
    'redir cmpusers_whois_nosuchnick' => \&event_whois_nosuchnick,
    'redir cmpusers_whois_timeout'    => \&event_whois_timeout,
  }
);


Irssi::command_bind("cmpusers", \&cmd_cmpusers);
Irssi::print(sprintf("Loaded %s v%s...", $IRSSI{'name'}, $VERSION));