1 package Xchat::XPI::Net;
12 use lib get_info 'xchatdir';
13 use Xchat::XPI qw<register init>;
15 our $VERSION = '0.03';
20 $res = Net::DNS::Resolver->new;
23 $res->tcp_timeout($timeout);
24 $res->udp_timeout($timeout);
27 $res->nameservers( [ split ' ', $nameservers ] ) if $nameservers;
31 my ($host, $callback) = @_[0, 1];
32 return unless $host && $callback;
34 my $sock = $res->bgsend($host);
37 my $hook = Xchat::hook_fd($sock, \&_dns_recv, { flags => FD_READ, data => [ $callback, $args ] } );
43 my ($callback, $args) = @{$_[2]};
44 my $p = $res->bgread($fd);
48 &$callback($p, $args);
54 default => 'whois.internic.net',
55 arpa => 'whois.arin.net',
56 # mil => 'whois.nic.mil',
58 ipv4 => { default => 'whois.ripe.net' },
59 ipv6 => { default => 'whois.6bone.net' }
63 my ($host, $callback) = @_[0, 1];
64 return unless $host && $callback;
67 if ($host =~ /^\s*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(\/\d{1,2})?$/) {
68 $server = $whois_servers{ipv4}{default};
69 } elsif ($host =~ /^\s*[\d:]*(\/\d{1,2})?\s*$/) {
70 $server = $whois_servers{ipv6}{default};
71 } elsif ($host =~ /([^\.]+)\.+([a-z]+)\.*$/) {
73 $server = $whois_servers{domain}{$2};
74 $server = $whois_servers{domain}{default} if !$server;
76 return unless $server;
79 # resolve($server, \&_whois_send, [ \$hook, $host, $callback, $args ] );
85 # my @a = grep { $_->type eq 'A' && $_->address } $p->answer;
87 # my ($hookref, $host, $callback, $args) = @{$_[1]};
88 # my $server = $a[rand(@a)]->address;
90 my $sock = IO::Socket::INET->new(
96 print $sock "$host\x0D\x0A";
97 $sock->shutdown(1); # stop writing
98 $hook = Xchat::hook_fd($sock, \&_whois_recv, { flags => FD_READ, data => [ $callback, $args ] } );
104 my ($callback, $args) = @{$_[2]};
105 my $raw = do { local $/; <$fd>; };
109 &$callback($raw, $args);
113 use base qw<Exporter>;
116 our %EXPORT_TAGS = ('funcs' => [ qw<resolve whois> ]);
117 our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
118 $EXPORT_TAGS{'all'} = [ @EXPORT_OK ];