Skip to content
This repository was archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1143 from duckduckgo/mintsoft/iplookup_takeover
Browse files Browse the repository at this point in the history
IPLookup: will now match IPv4 or IPv6 by itself as well as query formats
  • Loading branch information
Jag Talon committed Oct 7, 2014
2 parents c7a820a + 25711bf commit 56f1477
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/DDG/Spice/IPLookup.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,29 @@ topics "sysadmin";
attribution github => ['https://github.com/mintsoft', 'mintsoft'],
web => 'http://www.robtex.com/';

triggers startend => 'ip lookup', 'iplookup', 'reverse dns', 'reverse ip', 'dns', 'whois';
my $trig = qr#(?:ip lookup)|(?:iplookup)|(?:reverse (?:dns|ip)(?: lookup)?)|(?:dns)|(?:whois)#i;
my $IPv4_re = qr/(?:[0-9]{1,3}\.){3}(?:[0-9]{1,3})/;

my $regex = qr/^(?:$trig (?:of|for|on)?[ ]?)?($IPv4_re|$IPv6_re)(?: $trig)?$/;
triggers query_raw => $regex;

spice to => 'https://www.robtex.com/ext/xapiq/?q=$1&filter=none&s=wip,asip,ddg&r=json';
spice wrap_jsonp_callback => 1;

sub is_ipv4($)
{
my ($ip) = @_;
return undef unless $ip =~ /^([0-9]{1,3}\.){3}([0-9]{1,3})$/;
return undef unless $ip =~ qr/^$IPv4_re$/;
my @octlets = split /\./, $ip;
for (@octlets) {
return undef if int($_) > 255 || int($_) < 0;
}
return 1;
}

handle remainder => sub {
s/for|of|lookup//;
s/\s//;
return $_ if (is_ipv4($_) || /^$IPv6_re$/);
handle query_raw => sub {
my $ip = $1;
return $ip if (is_ipv4($ip) || $ip =~ /^$IPv6_re$/);
return;
};

Expand Down
8 changes: 8 additions & 0 deletions t/IPLookup.t
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ ddg_spice_test(
call_type => 'include',
caller => 'DDG::Spice::IPLookup',
),
'5.6.7.8' => test_spice(
'/js/spice/iplookup/5.6.7.8',
call_type => 'include',
caller => 'DDG::Spice::IPLookup'
),
'1.2.3.4/24' => undef,
'1.2.3.4 255.255.0.0' => undef,
'1.2.3.4/255.255.0.0' => undef,
);

done_testing;
Expand Down

0 comments on commit 56f1477

Please sign in to comment.