]> git.vpit.fr Git - perl/scripts/xchat.git/blob - clones.pl
Also undef %fetched when clones.pl is unloaded
[perl/scripts/xchat.git] / clones.pl
1 package Xchat::VPIT::Clones;
2
3 use strict;
4 use warnings;
5
6 use Scalar::Util qw<dualvar>;
7
8 use Xchat qw<:all>;
9
10 use lib get_info 'xchatdir';
11 use Xchat::XPI;
12 use Xchat::XPI::Events qw<delay>;
13 use Xchat::XPI::Utils  qw<save_context local_context>;
14
15 use constant {
16  JOIN_DELAY => 1000,
17  PRE        => ' ',
18  POST       => "\n",
19 };
20
21 our $VERSION = '0.04';
22
23 my ($ph, %users, %fetched);
24 my $extractor = PRE . '(.+?)' . POST;
25 $extractor = qr/$extractor/;
26
27 sub clone_enter_str {
28  my ($host, $nick, $clones) = @_;
29
30  my @clones = $clones =~ /$extractor/go;
31
32  return "\002$nick\002 ($host) is a clone of : \002@clones\002\n";
33 }
34
35 sub clone_leave_str {
36  my ($host, $nick, $clones) = @_;
37
38  my @clones = $clones =~ /$extractor/go;
39
40  return "\002$nick\002 ($host) has "
41                        . int($clones) . ' clone' . ($clones > 1 ? 's' : '')
42                        . " left : \002@clones\002\n";
43 }
44
45 sub clone_list_str {
46  my ($host, $clones) = @_;
47
48  my @clones = $clones =~ /$extractor/go;
49
50  return "Clones ($host) : \002@clones\002\n";
51 }
52
53 sub host {
54  my $userhost = lc $_[0];
55
56  return $userhost =~ /@([^@]+)$/ ? $1 : $userhost;
57 }
58
59 sub add {
60  my ($nick, $host, $serv, $chan) = @_;
61
62  my $hosts  = $users{$serv}{$chan};
63  $users{$serv}{$chan} = $hosts = { } unless defined $hosts;
64
65  my $clones = $hosts->{$host};
66
67  $nick = PRE . $nick . POST;
68  if (defined $clones) {
69   if (index($clones, $nick) < $[) {
70    $hosts->{$host} = dualvar int($clones) + 1, $clones . $nick;
71   }
72  } else {
73   $clones = dualvar 0, '';
74   $hosts->{$host} = dualvar 1, $nick;
75  }
76
77  return $clones;
78 }
79
80 sub remove {
81  my ($nick, $host, $serv, $chan) = @_;
82
83  my $hosts  = $users{$serv}{$chan};
84  return 0 unless $hosts;
85
86  my $clones = $hosts->{$host};
87  return 0 unless $clones;
88
89  $nick = PRE . $nick . POST;
90  if ((my $off = index $clones, $nick) >= $[) {
91   my $count = int $clones;
92   if ($count > 1) {
93    substr $clones, $off, length $nick, '';
94    return $hosts->{$host} = dualvar $count - 1, $clones;
95   } else {
96    delete $hosts->{$host};
97   }
98  }
99  return 0;
100 }
101
102 sub replace {
103  my ($old, $new, $host, $serv, $chan) = @_;
104
105  my $hosts = $users{$serv}{$chan};
106  $users{$serv}{$chan} = $hosts = { } unless defined $hosts;
107
108  my $clones = $hosts->{$host};
109
110  $_ = PRE . $_ . POST for $old, $new;
111  if (defined $clones and (my $off = index $clones, $old) >= $[) {
112   my $count = int $clones;
113   substr $clones, $off, length $old, '';
114   $hosts->{$host} = dualvar $count, $clones . $new;
115  } else {
116   $hosts->{$host} = dualvar 1, $new;
117  }
118 }
119
120 sub scan {
121  my ($serv, $chan) = @_;
122
123  return unless $fetched{$serv}{$chan};
124  my $hosts = $users{$serv}{$chan};
125
126  my $count = 0;
127  while (my ($host, $clones) = each %$hosts) {
128   if ($clones > 1) {
129    ++$count;
130    print $ph clone_list_str $host, $clones;
131   }
132  }
133
134  return $count;
135 }
136
137 sub flush {
138  my ($serv, $chan) = @_;
139
140  return 0 unless $serv and $chan and $fetched{$serv}{$chan};
141
142  delete $users{$serv}{$chan};
143  delete $users{$serv} unless keys %{$users{$serv}};
144
145  delete $fetched{$serv}{$chan};
146  delete $fetched{$serv} unless keys %{$fetched{$serv}};
147
148  return 1;
149 }
150
151 sub fetch {
152  my ($serv, $chan) = @_;
153
154  my $users = 0;
155  for (get_list 'users') {
156   my $host = $_->{host};
157   next unless defined $host and length $host;
158   add $_->{nick}, host($host), $serv, $chan;
159   ++$users;
160  }
161  $fetched{$serv}{$chan} = 1 if $users;
162
163  return $users;
164 }
165
166 hook_server '315', sub { # WHO end
167  my $serv = get_info 'server';
168  my $chan = lc $_[0][3];
169
170  return EAT_NONE if $fetched{$serv}{$chan};
171
172  local_context $chan, $serv => sub {
173   fetch $serv, $chan;
174   scan  $serv, $chan;
175  };
176
177  return EAT_NONE;
178 };
179
180 # On join, the who finishes sometimes before the tab opens, so the scan result
181 # isn't always displayed in the proper context. Hence the delay()
182
183 hook_server 'JOIN', sub {
184  my ($nick, $userhost) = ($_[0][0] =~ /^:([^!]+)!(.*)/);
185  my $serv = get_info 'server';
186  my $chan = lc substr $_[0][2], 1; # starts with colon
187
188  my $clones = add $nick, host($userhost), $serv, $chan;
189  return EAT_NONE unless $clones > 0;
190
191  my $printer = sub {
192   print $ph clone_enter_str $userhost, $nick, $clones;
193   1
194  };
195
196  local_context $chan, $serv, $printer or delay JOIN_DELAY, sub {
197   local_context $chan, $serv, $printer;
198  };
199
200  return EAT_NONE;
201 };
202
203 hook_server 'KICK', sub {
204  my $nick = $_[0][3];
205  my $serv = get_info 'server';
206  my $chan = lc $_[0][2];
207
208  if (nickcmp get_info('nick'), $nick) {
209   my $userinfo = user_info $nick;
210   my $userhost = $userinfo->{host};
211   if (defined $userhost and length $userhost) {
212    # If this isn't true, the kick happened before the first WHO response was
213    # received and the nick isn't in the database yet.
214    my $clones = remove $nick, host($userhost), $serv, $chan;
215    print $ph clone_leave_str $userhost, $nick, $clones if $clones > 0;
216   }
217  } else {
218   flush $serv, $chan;
219  }
220
221  return EAT_NONE;
222 };
223
224 hook_server 'PART', sub {
225  my ($nick, $userhost) = ($_[0][0] =~ /^:([^!]+)!(.*)/);
226  my $serv = get_info 'server';
227  my $chan = lc $_[0][2];
228
229  if (nickcmp get_info('nick'), $nick) {
230   my $clones = remove $nick, host($userhost), $serv, $chan;
231   print $ph clone_leave_str $userhost, $nick, $clones if $clones > 0;
232  } else {
233   flush $serv, $chan;
234  }
235
236  return EAT_NONE;
237 };
238
239 hook_server 'QUIT', sub {
240  my ($nick, $userhost) = ($_[0][0] =~ /^:([^!]+)!(.*)/);
241  my $serv  = get_info 'server';
242  my @chans = map [ lc $_->{channel}, $_->{context} ],
243               grep { $_->{type} == 2 and $_->{server} eq $serv }
244                get_list 'channels';
245
246  if (nickcmp get_info('nick'), $nick) {
247   my $guard = save_context;
248   my $host  = host $userhost;
249   for (@chans) {
250    my $clones = remove $nick, $host, $serv, $_->[0];
251    if ($clones > 0) {
252     set_context $_->[1] or next;
253     print $ph clone_leave_str $userhost, $nick, $clones;
254    }
255   }
256  } else {
257   flush $serv, $_->[0] for @chans;
258  }
259
260  return EAT_NONE;
261 };
262
263 hook_print 'Disconnected', sub {
264  my %servers;
265  $servers{$_->{server}} = 1 for grep +($_->{flags} & 0b1011) == 0b1001,
266                                  get_list 'channels';
267
268  delete @users{  grep !$servers{$_}, keys %users};
269  delete @fetched{grep !$servers{$_}, keys %fetched};
270
271  return EAT_NONE;
272 };
273
274 sub nick_cb {
275  my ($old, $new) = @{$_[0]};
276
277  my $userinfo = user_info $new;
278  $userinfo    = user_info $old unless defined $userinfo;
279
280  my $userhost = $userinfo->{host};
281  if (defined $userhost and length $userhost) {
282   # If the host isn't defined, the first WHO response hasn't been received yet,
283   # so the old nick isn't even in our database.
284   # Otherwise, the new nick would be added right now, and the old one would be
285   # when the WHO responses arrives (which may still refer to the old nick).
286   replace $old => $new, host($userhost),
287                         get_info('server'), lc get_info('channel');
288  }
289
290  return EAT_NONE;
291 }
292
293 hook_print $_, \&nick_cb for 'Change Nick', 'Your Nick Changing';
294
295 hook_command 'CLSCAN', sub {
296  my $serv = get_info 'server';
297  my $chan = lc get_info 'channel';
298
299  if (!$fetched{$serv}{$chan} and !fetch($serv, $chan)) {
300   print $ph "Data still not available\n";
301  } elsif (!scan($serv, $chan)) {
302   print $ph "No clones found\n";
303  }
304
305  return EAT_ALL;
306 }, {
307  help_text => 'Scan for clones in the current channel'
308 };
309
310 $ph = Xchat::XPI->new(
311  name   => 'Clones scanner',
312  tag    => 'Clones',
313  desc   => 'Automatic & on-demand clones scanner',
314  author => 'Vincent Pit (VPIT)',
315  email  => 'perl@profvince.com',
316  url    => 'http://www.profvince.com',
317  unload => sub {
318   undef %users;
319   undef %fetched;
320  },
321 );
322
323 1;