File Coverage

blib/lib/SRS/EPP/Command/Info/Contact.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package SRS::EPP::Command::Info::Contact;
2             {
3             $SRS::EPP::Command::Info::Contact::VERSION = '0.22';
4             }
5              
6 1     1   2630 use Moose;
  1         2  
  1         7  
7             extends 'SRS::EPP::Command::Info';
8              
9 1     1   6934 use MooseX::Params::Validate;
  1         3  
  1         10  
10 1     1   471 use SRS::EPP::Session;
  0            
  0            
11             use XML::EPP::Contact;
12             use Data::Dumper;
13             use Digest::MD5 qw(md5_hex);
14              
15             use XML::EPP::Contact::Info::Response;
16             use XML::EPP::Contact::PostalInfo;
17             use XML::EPP::Contact::Addr;
18             use XML::EPP::Contact::Status;
19              
20             # for plugin system to connect
21             sub xmlns {
22             XML::EPP::Contact::Node::xmlns();
23             }
24              
25             sub process {
26             my $self = shift;
27            
28             my ( $session ) = pos_validated_list(
29             \@_,
30             { isa => 'SRS::EPP::Session' },
31             );
32            
33             $self->session($session);
34             my $epp = $self->message;
35             my $payload = $epp->message->argument->payload;
36              
37             # we're not supporting authInfo, so get out of here with an
38             # EPP response
39             if ( $payload->has_auth_info ) {
40             return $self->make_response(code => 2307);
41             }
42              
43             return XML::SRS::Handle::Query->new(
44             handle_id_filter => $payload->id,
45             );
46             }
47              
48             has 'code' => (
49             is => "rw",
50             isa => "Int",
51             );
52              
53             sub zero_pad {
54             my $registrar_id = shift;
55             sprintf("%03d", $registrar_id);
56             }
57              
58             sub notify {
59             my $self = shift;
60            
61             my ( $rs ) = pos_validated_list(
62             \@_,
63             { isa => 'ArrayRef[SRS::EPP::SRSResponse]' },
64             );
65            
66             my $message = $rs->[0]->message;
67             my $response = $message->response;
68              
69             if ( $self->code ) {
70             return $self->make_response(code => $self->code);
71             }
72              
73             unless ($response) {
74              
75             # assume the contact doesn't exist
76             return $self->make_response(code => 2303);
77             }
78              
79             # make the Info::Response object
80             my %addr = (
81             street => [
82             $response->address->address1,
83             $response->address->address2||(),
84             ],
85             city => $response->address->city,
86             cc => $response->address->cc,
87             );
88              
89             # state or province
90             $addr{sp} = $response->address->region
91             if defined $response->address->region;
92              
93             $addr{pc} = $response->address->postcode
94             if defined $response->address->postcode;
95              
96             # Compare the contact's creation date against the audit time,
97             # to tell us if it has been updated
98             my $contact_updated = 0;
99             if (
100             $response->created_date->timestamptz ne
101             $response->audit->when->begin->timestamptz
102             )
103             {
104             $contact_updated = 1;
105             }
106              
107             # generate this required field
108             my $roid = substr(
109             md5_hex(
110             $response->registrar_id . $response->handle_id
111             ),
112             0,
113             12
114             ) . '-CON';
115              
116             # build the response
117             my $r = XML::EPP::Contact::Info::Response->new(
118             id => $response->handle_id,
119             postal_info => [
120             XML::EPP::Contact::PostalInfo->new(
121             name => $response->name,
122             addr => XML::EPP::Contact::Addr->new(
123             %addr,
124             ),
125             type => "int",
126             ),
127             ],
128             roid => $roid,
129             $self->_maybe_phone_number($response->phone, "voice"),
130             $self->_maybe_phone_number($response->fax, "fax"),
131             email => $response->email,
132             created => $response->created_date->timestamptz,
133             client_id => zero_pad($response->registrar_id),
134             creator_id => zero_pad($response->registrar_id),
135             status => [XML::EPP::Contact::Status->new(status => 'ok')],
136             (
137             $contact_updated
138             ? (
139             updated_by_id => zero_pad(
140             $response->audit->registrar_id,
141             ),
142             updated => $response->audit->when->begin->timestamptz,
143             )
144             : ()
145             ),
146             );
147              
148             return $self->make_response(
149             code => 1000,
150             payload => $r,
151             );
152             }
153              
154             sub _maybe_phone_number {
155             my $self = shift;
156             my $srs_number = shift;
157             my $field_name = shift;
158             if ( !$srs_number ) {
159             return ();
160             }
161             else {
162             my $e164 = $self->_translate_phone_number($srs_number);
163             return ($field_name => $e164);
164             }
165             }
166              
167             # Translate a SRS number to an EPP number
168             sub _translate_phone_number {
169             my $self = shift;
170             my $srs_number = shift;
171              
172             # The SRS local number field can contain anything
173             # alphanumeric. We grab anything numeric from the beginning of
174             # the string (including spaces, dashes, etc. which we strip
175             # out) and call that part the phone number. Anything after
176             # that goes into the 'x' field of the E164 object.
177             $srs_number->subscriber =~ m{(^[\d\s\-\.]*)(.*?)$};
178             my ($local_number, $x) = ($1, $2);
179              
180             # If we didn't get anything assigned to either field, our
181             # regex could be wrong. Just stick the whole thing in $x
182             $x = $srs_number->subscriber unless $local_number || $x;
183              
184             # Strip out anything non-numeric from $local_number
185             $local_number =~ s/[^\d]//g;
186              
187             my $global_number = "+" . $srs_number->cc . "."
188             . $srs_number->ndc . $local_number;
189              
190             return XML::EPP::Contact::E164->new(
191             content => $global_number,
192             (
193             $x ? (x => $x) : (),
194             ),
195             );
196             }
197              
198             1;