X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=Xchat%2FXPI%2FNet.pm;h=d4823a82b622e01037ed6c0374e8f69f3d1a5999;hb=HEAD;hp=285e11a9a3be0f45d125ad9d246242cf2805e491;hpb=b9c3e00cbf52ff5ddb0d79fd66a6877f6feb508d;p=perl%2Fscripts%2Fxchat.git diff --git a/Xchat/XPI/Net.pm b/Xchat/XPI/Net.pm index 285e11a..d4823a8 100644 --- a/Xchat/XPI/Net.pm +++ b/Xchat/XPI/Net.pm @@ -3,18 +3,18 @@ package Xchat::XPI::Net; use strict; use warnings; -use IO::Socket::INET; +use Xchat qw<:all>; -use Net::DNS; +use lib get_info 'xchatdir'; +use Xchat::XPI::Utils qw; -use Xchat qw/:all/; +use IO::Socket::INET; -use lib get_info 'xchatdir'; -use Xchat::XPI qw/register init/; +use Net::DNS; -our $VERSION = '0.02'; +our $VERSION = '0.03'; -my ($ph, $res); +my $res; BEGIN { $res = Net::DNS::Resolver->new; @@ -28,24 +28,32 @@ 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 called_from_script { + 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 +63,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,40 +104,41 @@ 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 called_from_script { + 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 = Xchat::XPI->new( - 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;