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<called_from_script>;
-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;
}
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;
}
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 ] );
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<Exporter>;
our @EXPORT = ();
-our %EXPORT_TAGS = ('funcs' => [ qw/resolve whois/ ]);
+our %EXPORT_TAGS = ('funcs' => [ qw<resolve whois> ]);
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;