X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=clones.pl;h=72e1092fc834fb58a09267552976f453fa4fe199;hb=9db5de0ec290fd8b82a9c510e0d627b31e44d9bd;hp=a8b01fb24b868f6c3f29bd9502c0ea5d69bfd5fd;hpb=263b6777b90548951a13bd56419fa911b010eb9b;p=perl%2Fscripts%2Fxchat.git diff --git a/clones.pl b/clones.pl index a8b01fb..72e1092 100755 --- a/clones.pl +++ b/clones.pl @@ -3,13 +3,13 @@ package Xchat::VPIT::Clones; use strict; use warnings; -use Scalar::Util qw/dualvar/; +use Scalar::Util qw; -use Xchat qw/:all/; +use Xchat qw<:all>; use lib get_info 'xchatdir'; use Xchat::XPI; -use Xchat::XPI::Events qw/delay/; +use Xchat::XPI::Events qw; use constant { JOIN_DELAY => 1000, @@ -45,18 +45,17 @@ sub clone_list_str { sub host { my $userhost = lc $_[0]; - my ($host) = $userhost =~ /@([^@]+)$/; - return $host || $userhost; + return $userhost =~ /@([^@]+)$/ ? $1 : $userhost; } sub add { my ($nick, $host, $serv, $chan) = @_; my $hosts = $users{$serv}{$chan}; - $users{$serv}{$chan} = $hosts = { } if not defined $hosts; + $users{$serv}{$chan} = $hosts = { } unless defined $hosts; my $clones = $hosts->{$host}; $nick = PRE . $nick . POST; if (defined $clones) { - if ((index $clones, $nick) < $[) { + if (index($clones, $nick) < $[) { $hosts->{$host} = dualvar int($clones) + 1, $clones . $nick; } } else { @@ -179,10 +178,13 @@ hook_server 'KICK', sub { my $chan = lc $_[0][2]; if (nickcmp get_info('nick'), $nick) { my $userinfo = user_info $nick; - return EAT_NONE unless $userinfo; my $userhost = $userinfo->{host}; - my $clones = remove $nick, host($userhost), $serv, $chan; - print $ph clone_leave_str $userhost, $nick, $clones if $clones > 0; + if (defined $userhost and length $userhost) { + # If this isn't true, the kick happened before the first WHO response was + # received and the nick isn't in the database yet. + my $clones = remove $nick, host($userhost), $serv, $chan; + print $ph clone_leave_str $userhost, $nick, $clones if $clones > 0; + } } else { flush $serv, $chan; } @@ -229,7 +231,7 @@ hook_print 'Disconnected', sub { my %servers = map { $_->{server} => 1 } grep { $_->{flags} & 9 && not $_->{flags} & 2 } get_list 'channels'; - delete $users{$_} for grep { !$servers{$_} } keys %users; + delete $users{$_} for grep { !$servers{$_} } keys %users; delete $fetched{$_} for grep { !$servers{$_} } keys %fetched; return EAT_NONE; }; @@ -237,15 +239,24 @@ hook_print 'Disconnected', sub { sub nick_cb { my ($old, $new) = @{$_[0]}; my $userinfo = user_info $new; - replace $old => $new, host($userinfo->{host}), - get_info('server'), lc get_info('channel') if $userinfo; + $userinfo = user_info $old unless defined $userinfo; + my $userhost = $userinfo->{host}; + if (defined $userhost and length $userhost) { + # If the host isn't defined, the first WHO response hasn't been received yet, + # so the old nick isn't even in our database. + # Otherwise, the new nick would be added right now, and the old one would be + # when the WHO responses arrives (which may still refer to the old nick). + replace $old => $new, host($userhost), + get_info('server'), lc get_info('channel'); + } return EAT_NONE; } hook_print $_, \&nick_cb for ('Change Nick', 'Your Nick Changing'); hook_command 'CLSCAN', sub { - my ($serv, $chan) = (get_info('server'), lc get_info('channel')); + my $serv = get_info 'server'; + my $chan = lc get_info 'channel'; if (!$fetched{$serv}{$chan} && !fetch($serv, $chan)) { print $ph "Data still not available\n"; } elsif (!scan($serv, $chan)) { @@ -256,12 +267,14 @@ hook_command 'CLSCAN', sub { help_text => 'Scan for clones in the current channel' }; -$ph = new Xchat::XPI name => 'Clones scanner', - tag => 'Clones', - desc => 'Automatic & on-demand clones scanner', - author => 'Vincent Pit (VPIT)', - email => 'perl@profvince.com', - url => 'http://www.profvince.com', - unload => sub { undef %users }; +$ph = Xchat::XPI->new( + name => 'Clones scanner', + tag => 'Clones', + desc => 'Automatic & on-demand clones scanner', + author => 'Vincent Pit (VPIT)', + email => 'perl@profvince.com', + url => 'http://www.profvince.com', + unload => sub { undef %users }, +); 1;