/usr/share/perl5/Net/Whois/Parser.pm is in libnet-whois-parser-perl 0.08-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | package Net::Whois::Parser;
use strict;
use utf8;
use Net::Whois::Raw;
use Data::Dumper;
our $VERSION = '0.08';
our @EXPORT = qw( parse_whois );
our $DEBUG = 0;
# parsers for parse whois text to data structure
our %PARSERS = (
'DEFAULT' => \&_default_parser,
);
# rules to convert diferent names of same fields to standard name
our %FIELD_NAME_CONV = (
# nameservers
nserver => 'nameservers',
name_server => 'nameservers',
name_serever => 'nameservers',
name_server => 'nameservers',
nameserver => 'nameservers',
dns1 => 'nameservers',
dns2 => 'nameservers',
primary_server => 'nameservers',
secondary_server => 'nameservers',
# domain
domain_name => 'domain',
domainname => 'domain',
# creation_date
created => 'creation_date',
created_on => 'creation_date',
creation_date => 'creation_date',
domain_registration_date => 'creation_date',
domain_created => 'creation_date',
#expiration_date
expire => 'expiration_date',
expire_date => 'expiration_date',
expires => 'expiration_date',
expires_at => 'expiration_date',
expires_on => 'expiration_date',
expiry_date => 'expiration_date',
domain_expiration_date => 'expiration_date',
);
# You can turn this flag to get
# all values of field in all whois answers
our $GET_ALL_VALUES = 0;
# hooks for formating values
our %HOOKS = (
nameservers => [ \&format_nameservers ],
emails => [ sub {my $value = shift; ref $value ? $value : [$value] } ],
);
# From Net::Whois::Raw
sub import {
my $mypkg = shift;
my $callpkg = caller;
no strict 'refs';
# export subs
*{"$callpkg\::$_"} = \&{"$mypkg\::$_"} foreach ((@EXPORT, @_));
}
# fetches whois text
sub _fetch_whois {
my %args = @_;
local $Net::Whois::Raw::CHECK_FAIL = 1;
my @res = eval {
Net::Whois::Raw::whois(
$args{domain},
$args{server} || undef,
$args{which_whois} || 'QRY_ALL'
)
};
return undef if $@;
my $res = ref $res[0] ? $res[0] : [ { text => $res[0], srv => $res[1] } ];
@$res = grep { $_->{text} } @$res;
return scalar @$res ? $res : undef;
}
sub parse_whois {
#TODO warn: Odd number of elements in hash assignment
my %args = @_;
if ( $args{raw} ) {
my $server =
$args{server} ||
Net::Whois::Raw::Common::get_server($args{domain}) ||
'DEFAULT';
my $whois = ref $args{raw} ? $args{raw} : [ { text => $args{raw}, srv => $server } ];
return _process_parse($whois);
}
elsif ( $args{domain} ) {
my $whois = _fetch_whois(%args);
return $whois ? _process_parse($whois) : undef;
}
undef;
}
sub _process_parse {
my ( $whois ) = @_;
my @data = ();
for my $ans ( @$whois ) {
my $parser =
$ans->{srv} && $PARSERS{$ans->{srv}} ?
$PARSERS{$ans->{srv}} : $PARSERS{DEFAULT};
push @data, $parser->($ans->{text});
}
_post_parse(\@data);
}
# standardize data structure
sub _post_parse {
my ( $data ) = @_;
my %res = ();
my $count = 0;
my %flag = ();
for my $hash ( @$data ) {
$count++;
for my $key ( keys %$hash ) {
next unless $hash->{$key};
# change keys to standard names
my $new_key = lc $key;
$new_key =~ s/\s+|\t+|-/_/g;
$new_key =~ s/\.+$//;
if ( exists $FIELD_NAME_CONV{$new_key} ) {
$new_key = $FIELD_NAME_CONV{$new_key};
}
unless ( $GET_ALL_VALUES ) {
if ( exists $res{$new_key} && !$flag{$new_key} ) {
delete $res{$new_key};
$flag{$new_key} = 1;
}
}
# add values to result hash
if ( exists $res{$new_key} ) {
push @{$res{$new_key}}, @{$hash->{$key}};
}
else {
$res{$new_key} = ref $hash->{$key} ? $hash->{$key} : [$hash->{$key}];
}
}
}
# make unique and process hooks
while ( my ( $key, $value ) = each %res ) {
if ( scalar @$value > 1 ) {
@$value = _make_unique(@$value);
}
else {
$value = $value->[0];
}
if ( exists $HOOKS{$key} ) {
for my $hook ( @{$HOOKS{$key}} ) { $value = $hook->($value) }
}
$res{$key} = $value;
}
\%res;
}
sub _make_unique {
my %vals;
grep { not $vals{$_} ++ } @_;
}
## PARSERS ##
# Regular expression built using Jeffrey Friedl's example in
# _Mastering Regular Expressions_ (http://www.ora.com/catalog/regexp/).
my $RFC822PAT = <<'EOF';
[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\
xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xf
f\n\015()]*)*\)[\040\t]*)*(?:(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\x
ff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|"[^\\\x80-\xff\n\015
"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[\040\t]*(?:\([^\\\x80-\
xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80
-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*
)*(?:\.[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\
\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\
x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x8
0-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|"[^\\\x80-\xff\n
\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[\040\t]*(?:\([^\\\x
80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^
\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040
\t]*)*)*@[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([
^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\
\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\
x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-
\xff\n\015\[\]]|\\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()
]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\
x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\04
0\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\
n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\
015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?!
[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\
]]|\\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\
x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\01
5()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)*|(?:[^(\040)<>@,;:".
\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]
)|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[^
()<>@,;:".\\\[\]\x80-\xff\000-\010\012-\037]*(?:(?:\([^\\\x80-\xff\n\0
15()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][
^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)|"[^\\\x80-\xff\
n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[^()<>@,;:".\\\[\]\
x80-\xff\000-\010\012-\037]*)*<[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?
:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-
\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:@[\040\t]*
(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015
()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()
]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\0
40)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\
[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\
xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*
)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80
-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x
80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t
]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\
\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])
*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x
80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80
-\xff\n\015()]*)*\)[\040\t]*)*)*(?:,[\040\t]*(?:\([^\\\x80-\xff\n\015(
)]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\
\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*@[\040\t
]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\0
15()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015
()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(
\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|
\\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80
-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()
]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x
80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^
\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040
\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".
\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff
])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\
\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x
80-\xff\n\015()]*)*\)[\040\t]*)*)*)*:[\040\t]*(?:\([^\\\x80-\xff\n\015
()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\
\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)?(?:[^
(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-
\037\x80-\xff])|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\
n\015"]*)*")[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|
\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))
[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80-\xff
\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\x
ff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(
?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\
000-\037\x80-\xff])|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\
xff\n\015"]*)*")[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\x
ff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)
*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)*@[\040\t]*(?:\([^\\\x80-\x
ff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-
\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)
*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\
]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])*\]
)[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-
\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\x
ff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(
?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80
-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<
>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x8
0-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])*\])[\040\t]*(?:
\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]
*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)
*\)[\040\t]*)*)*>)
EOF
$RFC822PAT =~ s/\n//g;
sub _default_parser {
my ( $raw ) = @_;
my %data;
# transform data to key => value
for my $line ( split /\n/, $raw ) {
chomp $line;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
my ( $key, $value ) = $line =~ /^\s*([\d\w\s._-]+):\s*(.+)$/;
next if !$line || !$value;
$key =~ s/\s+$//;
$value =~ s/\s+$//;
# if we have more then one value for one field we push them into array
$data{$key} = ref $data{$key} eq 'ARRAY' ?
[ @{$data{$key}}, $value ] : [ $value ];
}
# find all emails in the text
my @emails = $raw =~ /($RFC822PAT)/gso;
@emails = map { $_ =~ s/\s+//g; ($_) } @emails;
$data{emails} = exists $data{emails} ?
[ @{$data{emails}}, @emails ] : \@emails;
\%data;
}
## FORMATERS ##
sub format_nameservers {
my ( $value ) = @_;
$value = [$value] unless ref $value;
my @nss;
for my $ns ( @$value ) {
my ( $domain, $ip ) = split /\s+/, $ns;
$domain ||= $ns;
$domain =~ s/\.$//;
$domain = lc $domain;
push @nss, {
domain => $domain,
( $ip ? (ip => $ip) : () )
};
}
\@nss;
}
1;
=head1 NAME
Net::Whois::Parser - module for parsing whois information
=head1 SYNOPSIS
use Net::Whois::Parser;
my $info = parse_whois( domain => $domain );
my $info = parse_whois( raw => $whois_raw_text, domain => $domain );
my $info = parse_whois( raw => $whois_raw_text, server => $whois_server );
$info = {
nameservers => [
{ domain => 'ns.example.com', ip => '123.123.123.123' },
{ domain => 'ns.example.com' },
],
emails => [ 'admin@example.com' ],
domain => 'example.com',
somefield1 => 'value',
somefield2 => [ 'value', 'value2' ],
...
};
# Your own parsers
sub my_parser {
my ( $text ) = @_;
return {
nameservers => [
{ domain => 'ns.example.com', ip => '123.123.123.123' },
{ domain => 'ns.example.com' },
],
emails => [ 'admin@example.com' ],
somefield => 'value',
somefield2 => [ 'value', 'value2' ],
};
}
$Net::Whois::Parser::PARSERS{'whois.example.com'} = \&my_parser;
$Net::Whois::Parser::PARSERS{'DEFAULT'} = \&my_default_parser;
# If you want to get all values of fields from all whois answers
$Net::Whois::Parser::GET_ALL_VALUES = 1;
# example
# Net::Whois::Raw returns 2 answers
$raw = [ { text => 'key: value1' }, { text => 'key: value2'}];
$data = parse_whois(raw => $raw);
# If flag is off parser returns
# { key => 'value2' };
# If flag is on parser returns
# { key => [ 'value1', 'value2' ] };
# If you want to convert some field name to another:
$Net::Whois::Parser::FIELD_NAME_CONV{'Domain name'} = 'domain';
# If you want to format some fields.
# I think it is very useful for dates.
$Net::Whois::Parser::HOOKS{'expiration_date'} = [ \&format_date ];
=head1 DESCRIPTION
Net::Whois::Parser module provides Whois data parsing.
You can add your own parsers for any whois server.
=head1 FUNCTIONS
=over 3
=item parse_whois(%args)
Returns hash of whois data. Arguments:
C<'domain'> -
domain
C<'raw'> -
raw whois text
C<'server'> -
whois server
C<'which_whois'> -
option for Net::Whois::Raw::whois. Default value is QRY_ALL
=back
=head1 CHANGES
See file "Changes" in the distribution
=head1 AUTHOR
Ivan Sokolov, C<< <ivsokolov@cpan.org> >>
=head1 COPYRIGHT & LICENSE
Copyright 2009 Ivan Sokolov
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
|