]> git.vpit.fr Git - perl/scripts/xchat.git/blobdiff - Xchat/XPI/Net.pm
Pretend that the xchat functions in XPI are called from the script
[perl/scripts/xchat.git] / Xchat / XPI / Net.pm
index 9c83b14594c253cb2c4c90da880027972dc8aed0..d4823a82b622e01037ed6c0374e8f69f3d1a5999 100644 (file)
@@ -3,12 +3,15 @@ package Xchat::XPI::Net;
 use strict;
 use warnings;
 
+use Xchat qw<:all>;
+
+use lib get_info 'xchatdir';
+use Xchat::XPI::Utils qw<called_from_script>;
+
 use IO::Socket::INET;
 
 use Net::DNS;
 
-use Xchat qw<:all>;
-
 our $VERSION = '0.03';
 
 my $res;
@@ -32,10 +35,12 @@ sub resolve {
  return unless $sock;
  $sock->autoflush(1);
 
- return Xchat::hook_fd($sock, \&_dns_recv, {
-  flags => FD_READ,
-  data  => [ $callback, $args ],
- });
+ return called_from_script {
+  Xchat::hook_fd($sock, \&_dns_recv, {
+   flags => FD_READ,
+   data  => [ $callback, $args ],
+  });
+ }
 }
 
 sub _dns_recv {
@@ -62,16 +67,21 @@ my %whois_servers = (
  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, $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]+)\.*$/) {
+ } elsif ($host =~ /([^\.]+)\.+([a-z]+)[\.\s]*$/) {
   $host   = $1 . '.' . $2;
   $server = $whois_servers{domain}{$2};
   $server = $whois_servers{domain}{default} unless $server;
@@ -102,10 +112,12 @@ sub whois {
  print $sock "$host\x0D\x0A";
  $sock->shutdown(1); # stop writing
 
- return Xchat::hook_fd($sock, \&_whois_recv, {
-  flags => FD_READ,
-  data  => [ $callback, $args ],
- });
+ return called_from_script {
+  Xchat::hook_fd($sock, \&_whois_recv, {
+   flags => FD_READ,
+   data  => [ $callback, $args ],
+  });
+ }
 }
 
 sub _whois_recv {