X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=Xchat%2FXPI%2FNet.pm;h=96c4af30f71f4803986e8752adc3b4e0222ae828;hb=c47541f7a3aada1c0b4012a3cf16dee69eb3f9e3;hp=56cd68f6a64fff12cb762a462e8bf97f0c405a8d;hpb=263b6777b90548951a13bd56419fa911b010eb9b;p=perl%2Fscripts%2Fxchat.git diff --git a/Xchat/XPI/Net.pm b/Xchat/XPI/Net.pm index 56cd68f..96c4af3 100644 --- a/Xchat/XPI/Net.pm +++ b/Xchat/XPI/Net.pm @@ -7,14 +7,11 @@ use IO::Socket::INET; use Net::DNS; -use Xchat qw/:all/; +use Xchat qw<:all>; -use lib get_info 'xchatdir'; -use Xchat::XPI qw/register init/; +our $VERSION = '0.03'; -our $VERSION = '0.02'; - -my ($ph, $res); +my $res; BEGIN { $res = Net::DNS::Resolver->new; @@ -28,24 +25,30 @@ BEGIN { } sub resolve { - my ($host, $callback) = @_[0, 1]; - return unless $host && $callback; - my $args = $_[2]; + my ($host, $callback, $args) = @_; + return unless $host and $callback; + my $sock = $res->bgsend($host); return unless $sock; $sock->autoflush(1); - my $hook = Xchat::hook_fd($sock, \&_dns_recv, { flags => FD_READ, data => [ $callback, $args ] } ); - return $hook; + + return Xchat::hook_fd($sock, \&_dns_recv, { + flags => FD_READ, + data => [ $callback, $args ], + }); } sub _dns_recv { - my $fd = $_[0]; + my $fh = $_[0]; my ($callback, $args) = @{$_[2]}; - my $p = $res->bgread($fd); - $fd->shutdown(2); - close $fd; - undef $fd; - &$callback($p, $args); + + my $p = $res->bgread($fh); + $fh->shutdown(2); + close $fh; + undef $fh; + + $callback->($p, $args); + return REMOVE; } @@ -55,25 +58,31 @@ my %whois_servers = ( arpa => 'whois.arin.net', # mil => 'whois.nic.mil', }, - ipv4 => { default => 'whois.ripe.net' }, - ipv6 => { default => 'whois.6bone.net' } + ipv4 => { default => 'whois.ripe.net' }, + ipv6 => { default => 'whois.6bone.net' }, ); +my $ipv4_rx = qr/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/; +my $ipv6_rx = qr/[0-9:]+/; + sub whois { - my ($host, $callback) = @_[0, 1]; - return unless $host && $callback; - my $args = $_[2]; + my ($host, $callback, $args) = @_; + return unless $host and $callback; + my $server; - if ($host =~ /^\s*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\/\d{1,2})?$/) { + if ($host =~ /^\s*($ipv4_rx)(?:\/[0-9]{1,2})?[\s\.]*$/o) { + $host = $1; $server = $whois_servers{ipv4}{default}; - } elsif ($host =~ /^\s*[\d:]*(\/\d{1,2})?\s*$/) { + } elsif ($host =~ /^\s*($ipv6_rx)(?:\/[0-9]{1,2})?[\s\.]*$/o) { + $host = $1; $server = $whois_servers{ipv6}{default}; - } elsif ($host =~ /([^\.]+)\.+([a-z]+)\.*$/) { - $host = $1.'.'.$2; + } elsif ($host =~ /([^\.]+)\.+([a-z]+)[\.\s]*$/) { + $host = $1 . '.' . $2; $server = $whois_servers{domain}{$2}; - $server = $whois_servers{domain}{default} if !$server; + $server = $whois_servers{domain}{default} unless $server; } return unless $server; + my $hook; # resolve($server, \&_whois_send, [ \$hook, $host, $callback, $args ] ); @@ -90,38 +99,39 @@ sub whois { my $sock = IO::Socket::INET->new( PeerAddr => $server, PeerPort => 43, - Proto => 'tcp' - ) or return; + Proto => 'tcp', + ); + return unless $sock; $sock->autoflush(1); + print $sock "$host\x0D\x0A"; $sock->shutdown(1); # stop writing - $hook = Xchat::hook_fd($sock, \&_whois_recv, { flags => FD_READ, data => [ $callback, $args ] } ); - return $hook; + + return Xchat::hook_fd($sock, \&_whois_recv, { + flags => FD_READ, + data => [ $callback, $args ], + }); } sub _whois_recv { - my $fd = $_[0]; + my $fh = $_[0]; my ($callback, $args) = @{$_[2]}; - my $raw = do { local $/; <$fd>; }; - $fd->shutdown(2); - close $fd; - undef $fd; - &$callback($raw, $args); + + my $raw = do { local $/; <$fh>; }; + $fh->shutdown(2); + close $fh; + undef $fh; + + $callback->($raw, $args); + return REMOVE; } -use base qw/Exporter/; +use base qw; our @EXPORT = (); -our %EXPORT_TAGS = ('funcs' => [ qw/resolve whois/ ]); +our %EXPORT_TAGS = ('funcs' => [ qw ]); our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS; $EXPORT_TAGS{'all'} = [ @EXPORT_OK ]; -$ph = new Xchat::XPI name => 'Extended Xchat Perl Interface :: Net', - tag => 'XPI::Net', - desc => 'Asynchronous network tools', - author => 'Vincent Pit (VPIT)', - email => 'perl@profvince.com', - url => 'http://www.profvince.com'; - 1;