lib/NOLookup/RDAP/RDAPLookup/Whois.pm untested

filter: Statementstmt 0.0% 0 / 488 Branchbran 0.0% 0 / 298 Conditioncond 0.0% 0 / 112 MC/DCmcdc 0.0% 0 / 66 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 995 CC 150 SCAR 62.1
CC
150
Cov
0%
rdap_entity_obj_as_norid_whois_string
992.0
rdap_vcard_as_norid_whois_string
756.0
rdap_domain_obj_as_norid_whois_string
702.0
CRAP 499.4
1 package NOLookup::RDAP::RDAPLookup::Whois;
2  
3 use warnings;
4 use strict;
5 use POSIX qw(locale_h);
6 use base qw(NOLookup::RDAP::RDAPLookup);
7 use NOLookup::RDAP::RDAPLookup qw / $RDAP_LOOKUP_ERR_NO_MATCH /;
8 use NOLookup::Whois::WhoisLookup;
9  
10 use Data::Dumper;
11 $Data::Dumper::Indent=1;
12  
13 our $VERSION = $NOLookup::VERSION;
14  
15 =head2 result_as_norid_whois_string
16  
17 Format result as old style Norid whois output.
18  
19 Uses internal helper formatting functions.
20  
21 =cut
22  
23 sub result_as_norid_whois_string {
24 my ($self, $check, $nameservers, $entity, $expand, $lookup_reg_ent) = @_;
25  
26 my (@errors, $errs);
27  
28 my $rs = "";
29  
30 # A check has no data, let the caller handle it.
31 return if ($check);
32
33 my $response = $self->result;
34  
35 if ($response->isa('Net::RDAP::Error')) {
36 push(@errors, sprintf("%03u (%s)", $response->errorCode, $response->title));
37 return $rs, \@errors;
38 }
39  
40 ($rs, $errs) = $self->rdap_notice_as_norid_whois_string($response);
41  
42 if ($response->isa('Net::RDAP::SearchResult')) {
43  
44 my $ix = 0;
45 foreach my $nso ($response->nameservers) {
46 my ($rst, $errs) = $self->rdap_ns_obj_as_norid_whois_string($nso, $ix, $check, $nameservers, $entity);
47 $rs .= $rst if ($rst);
48 push @errors, @$errs if ($errs && @$errs);
49 ++$ix;
50 }
51  
52 $ix = 0;
53 foreach my $do ( $response->domains) {
54 my ($rst, $errs) =
55 $self->rdap_domain_obj_as_norid_whois_string($do, $ix, $check, $nameservers, $entity, 1, $expand);
56 $rs .= $rst if ($rst);
57 push @errors, @$errs if ($errs && @$errs);
58 ++$ix;
59 }
60  
61 $ix = 0;
62 foreach my $o ($response->entities) {
63 my ($rst, $errs) =
64 $self->rdap_entity_obj_as_norid_whois_string($o, $ix, $check, $nameservers, $entity, 1, $expand);
65 $rs .= $rst if ($rst);
66 push @errors, @$errs if ($errs && @$errs);
67 ++$ix;
68 }
69  
70 } elsif ($response->class eq 'nameserver') {
71  
72 my ($rst, $errs) = $self->rdap_ns_obj_as_norid_whois_string($response, 0, $check, $nameservers, $entity);
73 $rs .= $rst if ($rst);
74 push @errors, @$errs if ($errs && @$errs);
75  
76 } elsif ($response->class eq 'entity') {
77  
78 #print STDERR "** AN ENTITY OBJ: ix: 0, ",
79 # ", ns : ", $nameservers || 0,
80 # ", entity: ", $entity || 0,
81 # ", search: ", 0,
82 # ", expand: ", $expand || 0,
83 # "\n";
84  
85 my ($rst, $errs) = $self->rdap_entity_obj_as_norid_whois_string($response, 0, $check, $nameservers, $entity, 0, $expand);
86 $rs .= $rst if ($rst);
87 push @errors, @$errs if ($errs && @$errs);
88  
89 } elsif ($response->class eq 'domain') {
90 my ($rst, $errs) = $self->rdap_domain_obj_as_norid_whois_string($response, 0, $check, $nameservers, $entity, 0, $expand);
91 $rs .= $rst if ($rst);
92 push @errors, @$errs if ($errs && @$errs);
93  
94 } else {
95 die "Unexpected response class, please report it as a bug!";
96 }
97
98 return $rs, \@errors;
99  
100 }
101  
102 =head2 rdap_ns_obj_as_norid_whois_string
103  
104 Return whois formatted string.
105  
106 =cut
107  
108 sub rdap_ns_obj_as_norid_whois_string {
109 my ($self, $nso, $ix, $check, $nameservers, $entity) = @_;
110  
111 my (@errors, @wa);
112  
113 unless ('nameserver' eq $nso->class) {
114 push @errors, "No nameserver object";
115 return undef, \@errors;
116 }
117  
118 if ($ix == 0) {
119 # add whois object header
120 push @wa, "";
121 push @wa, "";
122 if ($nameservers) {
123 # Multiple nameserver objects
124 push @wa, "Hosts matching the search parameter";
125 rdap_page_info_as_norid_whois_string($self, \@wa);
126 } else {
127 # single NS
128 push @wa, "Host information";
129 }
130 }
131 push @wa, "";
132  
133 if ($self->is_a_search) {
134 my $cnt = $ix+1;
135 push @wa, "[$cnt]";
136 }
137 push @wa, "NORID Handle...............: " . $nso->handle;
138  
139 ##
140 # $nso is a single nameserver, present the name
141 #print "RDL: NSO single: ", Dumper $nso;
142
143 my ($name, $xname) = $self->rdap_get_obj_name($nso);
144  
145 push @wa, "Name Server Hostname.......: " . $name;
146  
147 if ($xname ne $name) {
148 # Not likely to have this for an ns under .no,
149 # since ace coded ns is not allowd.
150 push @errors, "Unexpected nameserver ACE name";
151 push @wa, "ACE Name Server Hostname...: " . $xname;
152 }
153
154 my @addrs = $nso->addresses;
155 if (scalar(@addrs) > 0) {
156 foreach my $ip (@addrs) {
157 push @wa, "Name Server IP-address.....: " . $ip->ip;
158 }
159 }
160
161 my @ent = $nso->entities;
162 if (scalar(@ent) > 0) {
163 foreach my $en (@ent) {
164 my @roles = $en->roles;
165 if (scalar(@roles)) {
166 my $role = $roles[0];
167 if ($role eq 'registrar') {
168 push @wa, "Registrar Handle...........: " . uc($en->handle);
169 } elsif ($role eq 'technical') {
170 push @wa, "Tech-c Handle..............: " . $en->handle;
171 }
172 }
173 }
174 }
175  
176 push @wa, "";
177  
178 # Add Additional dates only if single ns
179 unless ($nameservers) {
180 my @events = $nso->events;
181 if (scalar(@events)) {
182 push @wa, "Additional information:";
183 my ($create_date, $update_date);
184 foreach my $event (@events) {
185 # DateTime object is UTC, convert to localtime
186 my $to = $event->date;
187 $to->set_time_zone('Europe/Oslo');
188
189 if ($event->action eq 'registration') {
190 $create_date = substr(scalar($to->date), 0, 10);
191 push @wa, "Created: " . $create_date;
192 } else {
193 push @wa, "Last updated: " . substr(scalar($to->date), 0, 10);
194 $update_date = 1;
195 }
196 }
197 unless ($update_date) {
198 # Dispay update as same as create when not yet updated
199 push @wa, "Last updated: " . $create_date;
200 }
201 }
202
203 push @wa, "";
204 }
205
206 return join("\n", @wa), \@errors;;
207  
208 }
209  
210 =head2 rdap_notice_as_norid_whois_string
211  
212 Return whois formatted string.
213  
214 =cut
215  
216 sub rdap_notice_as_norid_whois_string {
217 my ($self, $response) = @_;
218  
219 my (@errors, @wa);
220
221 my @notices = $response->notices;
222  
223 if (scalar(@notices) > 0) {
224 foreach my $notice (@notices) {
225  
226 push @wa, $notice->title . ":";
227  
228 foreach my $link ($notice->links) {
229 push @wa, $link->href->as_string;
230 }
231 push @wa, "";
232 push @wa, $notice->description;
233 }
234 }
235 return '% ' . join("\n% ", @wa), \@errors;
236 }
237  
238 =head2 rdap_entity_obj_as_norid_whois_string
239  
240 Return whois formatted string.
241  
242 $search indicates if a search is done, in which case the entity is a
243 set of at least one.
244  
245 The header if built differently dependent of $nameservers and $search.
246  
247 $lookup_reg_ent triggers lookup of registrar entity, if missing.
248  
249 =cut
250  
251 sub rdap_entity_obj_as_norid_whois_string {
252 my ($self, $eo, $ix, $check, $nameservers, $entity, $search, $expand, $lookup_reg_ent) = @_;
253  
254 my (@errors, @wa, @doms);
255  
256 #print STDERR "ENTITY OBJ map2whois: ix: $ix, ns: ", $nameservers || 0,
257 # ", entity : ", $entity || 0,
258 # ", search : ", $search || 0,
259 # ", expand : ", $expand || 0,
260 # ", lookup_reg_ent: ", $lookup_reg_ent || 0,
261 # "\n";
262
263 unless ($eo->isa('Net::RDAP::Object::Entity')) {
264 push @errors, "Not an entity isa object";
265 return undef, \@errors;
266 }
267  
268 unless ('entity' eq $eo->class) {
269 push @errors, "No entity object";
270 return undef, \@errors;
271 }
272  
273 # TODO: Type of entity needs to be checked dependene of type/identity
274 push @wa, "";
275 push @wa, "";
276  
277 #print STDERR "rdap_entity_obj_as_norid_whois_string(), eo: ", Dumper $eo, "\n";
278  
279 # If there are entities inside an entity, it carries the registrar info,
280 # if not, it is a registrar entity directly
281 my @ent = $eo->entities;
282 my $handle = $eo->handle;
283 my $htype = $self->norid_handle_type($handle);
284  
285 if ($ix == 0) {
286 if ($search) {
287 push @wa, "Contacts matching the search parameter";
288 rdap_page_info_as_norid_whois_string($self, \@wa);
289 } else {
290 push @wa, ucfirst($htype) . " Information";
291 }
292 push @wa, "";
293 }
294 if ($search) {
295 my $cnt = $ix+1;
296 push @wa, "[$cnt]";
297 }
298  
299 #print STDERR "ENTITY REG map2whois\n";
300 #print STDERR " number ent found: ", scalar(@ent), "\n";
301  
302 if ($htype eq 'registrar') {
303 push @wa, "Registrar Handle...........: " . uc($handle);
304  
305 } else {
306 push @wa, "NORID Handle...............: " . uc($handle);
307  
308 if (($lookup_reg_ent || $search) && !@ent && ($expand || $entity)) {
309  
310 $self->rdap_get_entities_by_entity_handle($handle, $check,
311 $nameservers, \@ent, \@errors);
312 }
313  
314 if (scalar(@ent) > 0 && ($expand || $entity) &&
315 ($htype eq 'organization' || $htype eq 'person')) {
316  
317 $self->rdap_get_domains_by_entity_handle($handle, $check,
318 $nameservers, \@doms, \@errors);
319 }
320 }
321  
322 foreach my $en (@ent) {
323 #print STDERR "en: ", Dumper $en, "\n";
324
325 my $ehandle = $en->handle;
326 my $ehtype = $self->norid_handle_type($ehandle);
327 die "Unexpected ehandle type here: '$ehtype' for $ehandle" if ($ehtype ne 'registrar');
328
329 my @roles = $en->roles;
330  
331 #print STDERR "en role: ", Dumper \@roles, "\n";
332  
333 if (scalar(@roles)) {
334 my $role = $roles[0];
335 if ($role eq 'registrar') {
336 push @wa, "Registrar Handle...........: " . uc($ehandle);
337 } elsif ($role eq 'technical') {
338 push @wa, "Tech-c Handle..............: " . $ehandle;
339 die "Unexpected combination for role $role - only registrar expected here";
340 } else {
341 die "Unexpected combination for role $role - only registrar expected here";
342 }
343 }
344 }
345  
346 #print STDERR "eo: ", Dumper $eo, "\n";
347  
348 my @ids = $eo->ids;
349 if (scalar(@ids)) {
350 foreach my $id (@ids) {
351 if ($id->type) {
352 if ($id->type eq 'Norwegian organization number') {
353 push @wa, "Id Type....................: organization_number";
354 } elsif ($id->type eq 'Norid local identifier') {
355 push @wa, "Id Type....................: local_id_number";
356 } elsif ($id->type eq 'Norid person identifier') {
357 push @wa, "Id Type....................: anonymous_person_identifier";
358 } elsif ($id->type eq 'Norid registrar ID') {
359 push @wa, "Id Type....................: local_registrar_identifier";
360 }
361 }
362 if ($id->identifier) {
363 push @wa, "Id Number..................: " . $id->identifier;
364 }
365 }
366 }
367 #print "ENTITY OBJ \n";
368  
369 my @cards = $eo->vcard;
370  
371 if (scalar(@cards)) {
372 foreach my $card (@cards) {
373 # @cards may contain a single undef entry if no cards were found
374 next unless $card;
375 my ($vcs, $errs) = $self->rdap_vcard_as_norid_whois_string($card, $htype, $eo);
376 push @wa, $vcs if $vcs;
377  
378 push @errors, @$errs if ($errs && @$errs);
379 }
380 }
381  
382 #push @wa, "" unless ($search);
383  
384 # Dates not presented in whois for a reg handle.
385 unless ($htype eq 'registrar') {
386 if ($eo->events) {
387 my ($evs, $errs) = $self->rdap_events_as_norid_whois_string($eo);
388 if ($evs) {
389 push @wa, "";
390 push @wa, $evs;
391 }
392 push @errors, @$errs if ($errs && @$errs);
393 }
394 }
395  
396 # List the domains when relevant
397 if (scalar(@doms)) {
398 my $ds = "";
399 my @dns;
400  
401 foreach my $dom (@doms) {
402  
403 my ($name, $xname) = $self->rdap_get_obj_name($dom);
404  
405 if ($xname) {
406 push @dns, $xname;
407 } else {
408 push @dns, $name;
409 }
410 }
411  
412 # extend domain list with both total number and domain list
413 if (@dns) {
414 push @wa, "";
415 push @wa, "Total Number of Domains....: " . scalar(@doms);
416 push @wa, "Domains....................: " . join(" ", sort(@dns));
417 push @wa, "";
418 }
419 }
420  
421 #print "ENTITY OBJ DONE\n";
422
423 return join("\n", @wa), \@errors;
424  
425 }
426  
427  
428 =head2 rdap_domain_obj_as_norid_whois_string
429  
430 Return whois formatted string.
431  
432 $search indicates if a search is done, in which case the domain obj is a
433 set of at least one.
434  
435 $expand indicates if extra lookup for registrar info should be made,
436 since registrar info may not be included.
437  
438 The header if built differently dependent of $nameservers and $search.
439  
440 =cut
441  
442 sub rdap_domain_obj_as_norid_whois_string {
443 my ($self, $do, $ix, $check, $nameservers, $entity, $search, $expand, $lookup_reg_ent) = @_;
444  
445 #print STDERR "ix: $ix, rdap_domain_obj_as_norid_whois_string()\n";
446  
447 my (@errors, @wa);
448  
449 unless ($do->isa('Net::RDAP::Object::Domain')) {
450 push @errors, "Not a domain isa object";
451 return undef, \@errors;
452 }
453
454 unless ('domain' eq $do->class) {
455 push @errors, "No domain class object";
456 return undef, \@errors;
457 }
458
459 push @wa, "";
460 push @wa, "";
461  
462 # If there are entities inside a domain object, they carry
463 # the registrar info, holder, tech info.
464 # If not, it is a registrar entity directly
465  
466 my $handle = $do->handle;
467 my $htype = $self->norid_handle_type($handle);
468  
469 if ($ix == 0) {
470 if ($search) {
471 # This search did not exist in the old whois
472 push @wa, "Domains matching the search parameter";
473 rdap_page_info_as_norid_whois_string($self, \@wa);
474 } else {
475 push @wa, ucfirst($htype) . " Information";
476 }
477 push @wa, "";
478 }
479 if ($search) {
480 my $cnt = $ix+1;
481 push @wa, "[$cnt]";
482 }
483  
484 push @wa, "NORID Handle...............: " . uc($do->handle);
485  
486 my ($name, $xname) = $self->rdap_get_obj_name($do);
487  
488 #print STDERR "Doman name is '$name'\n";
489  
490 push @wa, "Domain Name................: " . $xname;
491
492 if ($xname ne $name) {
493 # Not likely to have this for an ns under .no,
494 # since ace coded ns is not allowd.
495 push @wa, "ACE Domain Name............: " . $name;
496 }
497  
498 my $hoe;
499
500 my @ent = $do->entities;
501  
502 if ($search && !@ent && ($expand || $entity)) {
503 #print STDERR "DOMAIN obj, but no embedded entities, lookup entity on domain name: '$name'\n";
504  
505 $self->rdap_get_entities_by_domain_name($name, $check,
506 $nameservers, \@ent, \@errors);
507 }
508  
509 if (scalar(@ent) > 0) {
510  
511 #print STDERR "DOMAIN obj, embedded entities found\n";
512 #print STDERR " ent's: ", Dumper \@ent, "n";
513
514 my (@ha, @ta, @ra);
515 foreach my $en (@ent) {
516 my @roles = $en->roles;
517 if (scalar(@roles)) {
518 my $role = $roles[0];
519 if ($role eq 'registrar') {
520 push @ra, "Registrar Handle...........: " . uc($en->handle);
521 } elsif ($role eq 'technical') {
522 push @ta, "Tech-c Handle..............: " . $en->handle;
523 } elsif ($role eq 'registrant') {
524 push @ha, "Domain Holder Handle.......: " . $en->handle;
525 $hoe = $en;
526  
527 } else {
528 die "unexpected role: $role";
529 }
530 }
531 }
532 push @wa, @ha if (@ha);
533 push @wa, @ra if (@ra);
534 push @wa, @ta if (@ta);
535 }
536  
537 my @ns = $do->nameservers;
538 if (scalar(@ns) > 0) {
539 foreach my $ns (sort { lc($a->name->name) cmp lc($b->name->name) } @ns) {
540 push @wa, "Name Server Handle.........: " . $ns->handle;
541 #push @wa, "Name Server Handle.........: " . $ns->handle . " (" . $ns->name->name . ")";
542 }
543 }
544  
545 my @ds = $do->ds;
546 if (scalar(@ds) > 0) {
547 push @wa, "DNSSEC.....................: Signed";
548  
549 my $ix = 1;
550 foreach my $ds (@ds) {
551 push @wa, "DS Key Tag $ix...........: " . $ds->keytag;
552 push @wa, "Algorithm $ix...........: " . $ds->algorithm;
553 push @wa, "Digest Type $ix...........: " . $ds->digtype;
554 push @wa, "Digest $ix...........: " . $ds->digest;
555 ++$ix;
556 }
557 }
558  
559 # Keys are not expected for .no, but hey, present them
560 my @keys = $do->keys;
561 if (scalar(@keys) > 0) {
562 push @wa, "DNSSEC Keys";
563 foreach my $key (@keys) {
564 push @wa, "Key Name $ix..........: " . $key->name;
565 push @wa, "Key Flags $ix..........: " . $key->flags;
566 push @wa, "Key Protocol $ix..........: " . $key->protocol;
567 push @wa, "Key Algorithm $ix..........: " . $key->algorithm;
568 push @wa, "Key Key $ix..........: " . $key->key;
569 }
570 }
571  
572 unless ($search) {
573 push @wa, "";
574
575 my @events = $do->events;
576 if (scalar(@events)) {
577 push @wa, "Additional information:";
578 my ($create_date, $update_date);
579 foreach my $event (@events) {
580 # DateTime object is UTC, convert to localtime
581 my $to = $event->date;
582 $to->set_time_zone('Europe/Oslo');
583  
584 if ($event->action eq 'registration') {
585 $create_date = substr(scalar($to->date), 0, 10);
586 push @wa, "Created: " . $create_date;
587 } else {
588 push @wa, "Last updated: " . substr(scalar($to->date), 0, 10);
589 $update_date = 1;
590 }
591 }
592 unless ($update_date) {
593 # Dispay update as same as create when not yet updated
594 push @wa, "Last updated: " . $create_date;
595 }
596 }
597 }
598  
599 if ($hoe && !$search) {
600 # We have the domain info above, attach registrant/holder info
601 # to simulate the output from Norid whois on a domain lookup.
602  
603 #print STDERR "hoe entity as norid string\n";
604 #print STDERR "hoe entity, self: ", Dumper $self;
605  
606 my ($rst, $errs) = $self->rdap_entity_obj_as_norid_whois_string($hoe, 0, $check, $nameservers, 1, 0, $expand, 0);
607  
608 push @wa, $rst if ($rst);
609 push @errors, @$errs if ($errs && @$errs);
610 }
611  
612 return join("\n", @wa), \@errors;
613  
614 }
615  
616 =head2 rdap_events_as_norid_whois_string
617  
618 Format events as norid whois string.
619  
620 =cut
621  
622 sub rdap_events_as_norid_whois_string {
623 my ($self, $eo) = @_;
624  
625 my (@errors, @wa);
626  
627 my @events = $eo->events;
628
629 if (scalar(@events)) {
630 push @wa, "Additional information:";
631 my ($create_date, $update_date);
632 foreach my $event (@events) {
633 # DateTime object is UTC, convert to localtime
634 my $to = $event->date;
635 $to->set_time_zone('Europe/Oslo');
636  
637 if ($event->action eq 'registration') {
638 $create_date = substr(scalar($to->date), 0, 10);
639 push @wa, "Created: " . $create_date;
640 } else {
641 push @wa, "Last updated: " . substr(scalar($to->date), 0, 10);
642 $update_date = 1;
643 }
644 }
645 unless ($update_date) {
646 # Dispay update as same as create when not yet updated
647 push @wa, "Last updated: " . $create_date;
648 }
649 }
650 return join("\n", @wa), \@errors;
651 }
652  
653 =head2 rdap_vcard_as_norid_whois_string
654  
655 Format vcard object(s) as whois string.
656  
657 Ref. https://tools.ietf.org/html/rfc6350#section-6.3.1 to see order of
658 things.
659  
660 =cut
661  
662 sub rdap_vcard_as_norid_whois_string {
663 my ($self, $card, $type, $eo) = @_;
664  
665 unless ($eo) {
666 die "VCARD eo is missing as argument";
667 }
668 unless ($eo->isa('Net::RDAP::Object::Entity')) {
669 die "VCARD eo is not an entity object";
670 }
671  
672 my (@errors, @wa);
673  
674 if ($type eq 'registrar') {
675 push @wa, "Registrar Name.............: " . $card->full_name if ($card->full_name);
676  
677 } else {
678 push @wa, "Type.......................: " . $type;
679  
680 if ($type eq 'organization') {
681 # When full layer access is achived, two names should be present in the card:
682 # - the legal name should be found in fn
683 # - the org. name should be in the 'organization' field.
684 push @wa, "Organization Name..........: " . $card->organization if ($card->organization);
685 }
686 push @wa, "Name.......................: " . $card->full_name if ($card->full_name);
687 }
688  
689 my @addresses = @{$card->addresses};
690 if (scalar(@addresses) > 0) {
691  
692 foreach my $address (@addresses) {
693 my @lines;
694 my $ix = 0;
695 foreach my $element (@{$address->{'address'}}) {
696 my @val = ('ARRAY' eq ref($element) ? @{$element} : $element);
697  
698 foreach my $al (@val) {
699 next unless $al;
700  
701 if ($ix < 3) {
702 if ($type eq 'registrar') {
703 # 0: PoBox, 1: extended address, 2: street address
704 push @wa, "Address....................: " . $al;
705 } else {
706 push @wa, "Post Address...............: " . $al;
707 }
708
709 } elsif ($ix == 3) {
710 # city: GDPR layer only
711 push @wa, "Postal Area................: " . $al;
712  
713 } elsif ($ix == 4) {
714 # region, not used by us?
715 push @wa, "Postal Region..............: " . $al;
716  
717 } elsif ($ix == 5) {
718 # pcode / zip : GDPR layer only
719 push @wa, "Postal Code................: " . $al;
720 } elsif ($ix == 6) {
721 # pcode / zip
722 push @wa, "Country....................: " . $al;
723
724 } else {
725 die "Should not end at this index $ix";
726 }
727 }
728 ++$ix;
729 }
730
731 }
732 }
733  
734 foreach my $number (@{$card->phones}) {
735  
736 my @types = ('ARRAY' eq ref($number->{'type'}) ? @{$number->{'type'}} : ($number->{'type'}));
737  
738 # Separate between fax, voice and cell phone
739 my $type = 'voice';
740 foreach my $t (@types) {
741 $t = lc($t);
742 if ($t eq 'fax' || $t eq 'cell') {
743 $type = $t;
744 last;
745 }
746 }
747  
748 # strip the 'tel:' prefix
749 my $no = $number->{'number'};
750 $no =~ s/^tel://;
751  
752 if ($type eq 'fax') {
753 push @wa, "Fax Number.................: $no";
754 } elsif ($type eq 'voice') {
755 push @wa, "Phone Number...............: $no";
756 } elsif ($type eq 'cell') {
757 push @wa, "Mobile Phone Number........: $no";
758 }
759 }
760  
761 my $ix = 0;
762 foreach my $email (@{$card->email_addresses}) {
763 if ($email->{'type'}) {
764 # Do not know when I get here, so die till we know
765 die "what is this email type? " . Dumper $email->{'type'};
766 }
767 if ($ix == 0) {
768 push @wa, "Email Address..............: " . $email->{'address'};
769 } else {
770 push @wa, "Additional Email...........: " . $email->{'address'};
771 }
772 ++$ix;
773 }
774  
775 # Hack: Web address (url) for an entity is not supported via the card
776 # object, ref. code for vcard() in
777 # https://metacpan.org/source/GBROWN/Net-RDAP-0.14/lib/Net/RDAP/Object/Entity.pm
778 # which does not process the url info.
779 #
780 # Therefore just fetch the url from inside the $eo instead.
781 #
782 # TODO: This should be fixed in Net::RDAP::Object::Entity, a bug
783 # report has been filed with a request for support.
784 # (Could also be fixed by local subclassing here, but we
785 # prefer a central fix)
786 #
787 my $vca = $eo->{vcardArray};
788  
789 if ($vca && $vca->[0] && $vca->[0] eq 'vcard' && $vca->[1]) {
790 foreach my $vc (@{$vca->[1]}) {
791 if ($vc->[0] && $vc->[0] eq 'url') {
792 if ($vc->[0] && $vc->[0] eq 'url' &&
793 $vc->[2] && $vc->[2] eq 'uri' &&
794 $vc->[3]) {
795 push @wa, "Web Address................: " . $vc->[3];
796 }
797 }
798 }
799 }
800
801 return join("\n", @wa), \@errors;
802 }
803  
804 =head2 rdap_page_info_as_norid_whois_string
805  
806 Format and insert page info, if requested.
807  
808 =cut
809  
810 sub rdap_page_info_as_norid_whois_string {
811 my ($self, $wa) = @_;
812  
813 #print STDERR "rdap_page_info_as_norid_whois_string() called\n";
814  
815 return unless ($self->{insert_page_info});
816  
817 my $nopages_int = $self->total_no_pages || 1;
818  
819 #print STDERR "rdap_page_info_as_norid_whois_string() inserting\n";
820 #print STDERR "page_size : ", $self->page_size, "\n";
821 #print STDERR "total_size : ", $self->total_size, "\n";
822 #print STDERR "page_number: ", $self->page_number, "\n";
823 #print STDERR "nopages_int: $nopages_int\n";
824  
825 push @$wa, "";
826 push @$wa, "Page Number................: " . ($self->page_number || 1) . "/$nopages_int";
827 push @$wa, "Number Of Hits Shown.......: " . $self->size;
828 push @$wa, "Number Of Total Hits.......: " . $self->total_size;
829  
830 return if ($self->size == $self->total_size);
831  
832 # cur_page not interesting as I see it?
833 if ($self->first_page) {
834 #print STDERR " first_page: ", $self->first_page, "\n";
835 push @$wa, "First Page.................: " . $self->first_page;
836 }
837 if ($self->cur_page) {
838 #print STDERR " cur_page: ", $self->cur_page, "\n";
839 push @$wa, "Current Page...............: " . $self->cur_page;
840 }
841 if ($self->prev_page) {
842 #print STDERR " prev_page: ", $self->prev_page, "\n";
843 push @$wa, "Previous Page..............: " . $self->prev_page;
844 }
845
846 if ($self->size < $self->total_size && $self->next_page) {
847 #print STDERR " next_page: ", $self->next_page, "\n";
848 push @$wa, "Next Page..................: " . $self->next_page;
849 }
850 }
851  
852  
853 =head2 norid_whois_parse
854  
855 Convert a whois formatted result string to a whois object the same way
856 as NOLookup::Whois::WhoisLookup does.
857  
858 Returns ($wh, $do, $ho), all NOLookup::Whois::WhoisLookup objects.
859  
860 =cut
861  
862 sub norid_whois_parse {
863 my ($self, $text) = @_;
864  
865 my ($wh, $do, $ho);
866
867 # Parse whois and map values into objects.
868 $wh = NOLookup::Whois::WhoisLookup->new;
869 $wh->_parse($text);
870  
871 # Also include do and ho if present
872 if ($text =~ m/\nDomain Information\n/) {
873
874 # If a domain name, or a domain handle, is looked up, the
875 # whois server may also return the holder info as a second
876 # block. The below code parses the domain and holder info and
877 # returns the data in separate objects.
878 #
879
880 # Domain info is first block. Holder contact info is second
881 # block, but only if the full (but limited) registrarwhois
882 # service is used. Split the text and make two objects.
883
884 my ($dmy, $dtxt, $htxt) = split ('NORID Handle', $text);
885  
886 $do = NOLookup::Whois::WhoisLookup->new;
887  
888 #print STDERR "\n------\nparse domain text: '$dtxt'\n";
889 $do->_parse("\nNORID Handle" . $dtxt);
890  
891 if ($htxt) {
892 $ho = NOLookup::Whois::WhoisLookup->new;
893 #print STDERR "\n------\nparse holder text: '$htxt'\n";
894 $ho->_parse("\nNORID Handle" . $htxt);
895 }
896 #print STDERR "wh: ", Dumper $wh if ($wh);
897 #print STDERR "do: ", Dumper $do if ($do);
898 #print STDERR "ho: ", Dumper $ho if ($ho);
899  
900 return $wh, $do, $ho;
901  
902 }
903  
904 if ($text =~ m/\nHosts matching the search parameter\n/) {
905 # Set a method telling that a name_server_list is found,
906 # which is only the case when a host name is looked up.
907 $wh->{name_server_list} = 1;
908 }
909  
910 #print STDERR "\n\n====\nwh after $query: ", Dumper $wh;
911 return $wh;
912  
913 }
914  
915 =head2 rdap_get_domain_object_as_whois
916  
917 Scan domain object and do the rdap2whois on it.
918  
919 $result must point to a domain result.
920  
921 =cut
922  
923 sub rdap_get_domain_object_as_whois {
924 my ($self, $result) = @_;
925  
926 #print STDERR "rdap_get_domain_object_as_whois()\n";
927  
928 my @errors;
929
930 # We expect only a domain name result
931 unless ($result->isa('Net::RDAP::Object::Domain')) {
932 push @errors, "Not an Net::RDAP::Object::Domain object";
933 return undef, \@errors;
934 }
935 unless ($result->class eq 'domain') {
936 push @errors, "Not a 'domain' class";
937 return undef, \@errors;
938 }
939  
940 my ($rst, $errs) = $self->rdap_domain_obj_as_norid_whois_string($result, 0);
941  
942 unless ($rst) {
943 push @errors, "No whois string, not able to build domain_object_as_whois";
944 push @errors, @$errs if ($errs && @$errs);
945 return undef, \@errors;
946 }
947  
948 my ($wh, $do) = $self->norid_whois_parse($rst);
949  
950 return $do, \@errors;
951  
952 }
953  
954 =head2 rdap_get_entity_objects_as_whois
955  
956 Scan registrant, registrar and tech entity objects and do the rdap2whois on them.
957  
958 =cut
959  
960 sub rdap_get_entity_objects_as_whois {
961 my ($self, $result) = @_;
962
963 my ($ho, $regobj, @tcs, @errors);
964  
965 # Collect domain contact entities, registrant, registrar and tech-c's.
966 my @ent = $result->entities;
967
968 my $ix = 0;
969 if (scalar(@ent) > 0) {
970 $ix = 0;
971 foreach my $en (@ent) {
972 my ($rst, $errs) = $self->rdap_entity_obj_as_norid_whois_string($en, $ix);
973 unless ($rst) {
974 push @errors, "No whois string, not able to build entity_objects_as_whois";
975 push @errors, @$errs if ($errs && @$errs);
976 next;
977 }
978  
979 ++$ix;
980 my $eo = $self->norid_whois_parse($rst);
981
982 my @roles = $en->roles;
983 if (scalar(@roles)) {
984 my $role = $roles[0];
985  
986 #print STDERR "ROLE: $role\n";
987  
988 if ($role eq 'registrar') {
989 $regobj = $eo;
990 } elsif ($role eq 'technical') {
991 push @tcs, $eo;
992 } elsif ($role eq 'registrant') {
993 $ho = $eo;
994 } else {
995 # unexpected role
996 push @errors, "Unexpected role: $role, technical, registrar or registrant expected!";
997 }
998 }
999 }
1000 }
1001 return $ho, $regobj, \@tcs, \@errors;
1002 }
1003  
1004 =head2 rdap_get_nameserver_objects_as_whois
1005  
1006 Scan NS objects and do the rdap2whois on them.
1007  
1008 =cut
1009  
1010 sub rdap_get_nameserver_objects_as_whois {
1011 my ($self, $result) = @_;
1012  
1013 my (@nss, @zcs, %zeen, @errors);
1014  
1015 my $ix = 0;
1016
1017 foreach my $nso ($result->nameservers) {
1018  
1019 #print STDERR "convert nameserver: ", Dumper $nso;
1020
1021 my ($rst, $errs) = $self->rdap_ns_obj_as_norid_whois_string($nso, $ix);
1022 next unless $rst;
1023 unless ($rst) {
1024 push @errors, "No whois string, not able to build nameserver_objects_as_whois";
1025 push @errors, @$errs if ($errs && @$errs);
1026 next;
1027 }
1028  
1029 ++$ix;
1030 my $wnso = $self->norid_whois_parse($rst);
1031  
1032 # ns tech contact is contained as an entity inside the NS object,
1033 # there should be only one contact in the current NO data model.
1034 my @ent = $nso->entities;
1035  
1036 #print "ns ent array: ", Dumper \@ent;
1037 if (scalar(@ent) > 0) {
1038 $ix = 0;
1039 foreach my $en (@ent) {
1040 next unless $en;
1041  
1042 #print STDERR "ns entity obj: ", Dumper $en;
1043 my $nsh = $en->handle;
1044  
1045 if ($ix > 1) {
1046 push @errors, "Unexpected number of tech-contacts ($ix) for name server: $nsh!";
1047 }
1048  
1049 my ($rst, $errs) = $self->rdap_entity_obj_as_norid_whois_string($en, $ix);
1050 unless ($rst) {
1051 push @errors, "No whois string, not able to build nameserver tech entity objects_as_whois";
1052 push @errors, @$errs if ($errs && @$errs);
1053 next;
1054 }
1055  
1056 ++$ix;
1057 my $eo = $self->norid_whois_parse($rst);
1058
1059 my @roles = $en->roles;
1060
1061 if (scalar(@roles)) {
1062 my $role = $roles[0];
1063 if ($role eq 'technical') {
1064 $wnso->{tech_c_name} = $eo->name;
1065 $wnso->{tech_c_type} = $eo->type;
1066 push @zcs, $eo if ($eo && !$zeen{$nsh});
1067 $zeen{$nsh} = 1;
1068 } else {
1069 # unexpected role for an ns contact
1070 push @errors, "Unexpected ns object tech role: $role, technical expected!";
1071 }
1072 }
1073 }
1074 }
1075 push @nss, $wnso if ($wnso);
1076 }
1077  
1078 return (\@nss, \@zcs, \@errors);
1079
1080 }
1081  
1082  
1083 =pod
1084  
1085 =encoding ISO-8859-1
1086  
1087 =head1 NAME
1088  
1089 NOLookup::RDAP::RDAPLookup::Whois
1090  
1091 Offer a number of utility whois methods to simulate an
1092 rdap2whois world, building output and objects as the old whois
1093 formatted service.
1094  
1095 =head1 SYNOPSIS
1096  
1097 use Encode;
1098 use NOLookup::RDAP::RDAPLookup::Whois;
1099
1100 # Default API service URL
1101 my $SERVICE_URL = "https://rdap.norid.no";
1102  
1103 # Example 1: Domain name lookup
1104 # Decode the query when needed, like for IDNs
1105 # or names with national characters.
1106  
1107 my $q = 'norid.no';
1108 #$q = decode('ISO8859-1', 'øl.no');
1109  
1110 my $bo = NOLookup::RDAP::RDAPLookup->new(
1111 {
1112 service_url => 'https://rdap.norid.no',
1113 debug => 0,
1114 use_cache => 0,
1115 });
1116  
1117 # test HEAD operation for existence
1118 $bo->lookup($q, 1, 0, 0);
1119 if ($bo->error) {
1120 print "HEAD: Error, error / status: ",
1121 $bo->error . "/" . $bo->status) . "\n";
1122 }
1123  
1124 # test GET operations
1125 $bo->lookup($q, 0, 0, 0);
1126 if ($bo->error) {
1127 print "GET: Error, error / status: ",
1128 $bo->error . "/" . $bo->status) . "\n";
1129 }
1130
1131 # result of lookup is in $bo->result
1132 # This result contains response objects built by Net::RDAP
1133  
1134 my $res = $bo->result;
1135 print "handle: ", $bo->handle, "\n";
1136  
1137 * See bin/no_rdap.pl for more information on usage.
1138  
1139 * See various formatting/helper functions in this file for how to
1140 access the various objects returned by Net::RDAP.
1141  
1142 =head1 DESCRIPTION
1143  
1144 This module provides an object oriented API for use with the
1145 Norid RDAP service. It uses the Net::RDAP module from Cpan
1146 internally to fetch information from the Norid RDAP.
1147  
1148 =head1 SUPPORT
1149  
1150 For now, support questions should be sent to:
1151  
1152 E<lt>(nospam)info(at)norid.noE<gt>
1153  
1154 =head1 SEE ALSO
1155  
1156 L<https://www.norid.no/en>
1157 L<https://teknisk.norid.no/en/integrere-mot-norid/rdap-tjenesten>
1158 L<https://teknisk.norid.no/en/integrere-mot-norid/whois>
1159  
1160 =head1 CAVEATS
1161  
1162 =head1 AUTHOR
1163  
1164 Trond Haugen, E<lt>(nospam)info(at)norid.noE<gt>
1165  
1166 =head1 COPYRIGHT
1167  
1168 Copyright (c) 2020- Trond Haugen <(nospam)info(at)norid.no>.
1169 All rights reserved.
1170  
1171 This program is free software; you can redistribute it and/or modify
1172 it under the terms of the GNU General Public License as published by
1173 the Free Software Foundation; either version 2 of the License, or
1174 (at your option) any later version.
1175  
1176 =head1 LICENSE
1177  
1178 This library is free software. You can redistribute it and/or modify
1179 it under the same terms as Perl itself.
1180  
1181 1;