| 1 | ## Domain Registry Interface, Protocol superclass | ||
| 2 | ## | ||
| 3 | ## Copyright (c) 2005-2011,2013-2014,2016,2018-2019 Patrick Mevzek <netdri@dotandco.com>. All rights reserved. | ||
| 4 | ## | ||
| 5 | ## This file is part of Net::DRI | ||
| 6 | ## | ||
| 7 | ## Net::DRI is free software; you can redistribute it and/or modify | ||
| 8 | ## it under the terms of the GNU General Public License as published by | ||
| 9 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 10 | ## (at your option) any later version. | ||
| 11 | ## | ||
| 12 | ## See the LICENSE file that comes with this distribution for more details. | ||
| 13 | #################################################################################################### | ||
| 14 | |||
| 15 | package Net::DRI::Protocol; | ||
| 16 | |||
| 17 | 80 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 80 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | |||
| 20 | 80 | ▶ | use base qw(Class::Accessor::Chained::Fast Net::DRI::BaseClass); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | __PACKAGE__->mk_accessors(qw(name version commands message default_parameters logging)); | ||
| 22 | |||
| 23 | 80 | ▶ | use DateTime; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 80 | ▶ | use DateTime::Duration; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 80 | ▶ | use DateTime::Format::ISO8601; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 80 | ▶ | use DateTime::Format::Strptime; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | |||
| 28 | 80 | ▶ | use Net::DRI::Exception; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 80 | ▶ | use Net::DRI::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 80 | ▶ | use Net::DRI::Data::Changes; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 80 | ▶ | use Net::DRI::Data::Contact; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | 80 | ▶ | use Net::DRI::Data::ContactSet; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 33 | 80 | ▶ | use Net::DRI::Data::Hosts; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | 80 | ▶ | use Net::DRI::Data::StatusList; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 35 | 0 | ▶ | use Net::DRI::Data::LanguageTag; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 36 | |||
| 37 | =pod | ||
| 38 | |||
| 39 | =head1 NAME | ||
| 40 | |||
| 41 | Net::DRI::Protocol - Superclass of all Net::DRI Protocols | ||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | Please see the README file for details. | ||
| 46 | |||
| 47 | =head1 SUPPORT | ||
| 48 | |||
| 49 | For now, support questions should be sent to: | ||
| 50 | |||
| 51 | E<lt>netdri@dotandco.comE<gt> | ||
| 52 | |||
| 53 | Please also see the SUPPORT file in the distribution. | ||
| 54 | |||
| 55 | =head1 SEE ALSO | ||
| 56 | |||
| 57 | E<lt>http://www.dotandco.com/services/software/Net-DRI/E<gt> | ||
| 58 | |||
| 59 | =head1 AUTHOR | ||
| 60 | |||
| 61 | Patrick Mevzek, E<lt>netdri@dotandco.comE<gt> | ||
| 62 | |||
| 63 | =head1 COPYRIGHT | ||
| 64 | |||
| 65 | Copyright (c) 2005-2011,2013-2014,2016,2018-2019 Patrick Mevzek <netdri@dotandco.com>. | ||
| 66 | All rights reserved. | ||
| 67 | |||
| 68 | This program is free software; you can redistribute it and/or modify | ||
| 69 | it under the terms of the GNU General Public License as published by | ||
| 70 | the Free Software Foundation; either version 2 of the License, or | ||
| 71 | (at your option) any later version. | ||
| 72 | |||
| 73 | See the LICENSE file that comes with this distribution for more details. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | #################################################################################################### | ||
| 78 | |||
| 79 | sub new | ||
| 80 | { | ||
| 81 | my ($class,$ctx)=@_; | ||
| 82 | |||
| 83 | my $self={ capabilities=> {}, | ||
| 84 | factories => { datetime => sub { return DateTime->new(@_); }, | ||
| 85 | duration => sub { return DateTime::Duration->new(@_); }, | ||
| 86 | changes => sub { return Net::DRI::Data::Changes->new(@_); }, | ||
| 87 | contact => sub { return Net::DRI::Data::Contact->new(); }, | ||
| 88 | contactset => sub { return Net::DRI::Data::ContactSet->new(@_); }, | ||
| 89 | hosts => sub { return Net::DRI::Data::Hosts->new(@_); }, | ||
| 90 | status => sub { return Net::DRI::Data::StatusList->new(@_); }, | ||
| 91 | language_tag=> sub { return Net::DRI::Data::LanguageTag->new(@_); }, | ||
| 92 | }, | ||
| 93 | logging => $ctx->{registry}->logging(), | ||
| 94 | logging_ctx => { registry => $ctx->{registry}->name(), profile => $ctx->{profile}, transport_class => $ctx->{transport_class} }, | ||
| 95 | }; | ||
| 96 | |||
| 97 | bless($self,$class); | ||
| 98 | $self->message(undef); | ||
| 99 | $self->default_parameters({}); | ||
| 100 | |||
| 101 | $self->log_setup_channel($class,'protocol',$self->{logging_ctx}); | ||
| 102 | $self->log_output('debug','core',sprintf('Added profile %s for registry %s',$class,$ctx->{registry}->name())); | ||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub log_output | ||
| 107 | { | ||
| 108 | my ($self,$level,$type,$data1)=@_; | ||
| 109 | $self->{logging_ctx}->{protocol}=$self->name().'/'.$self->version() if (! exists $self->{logging_ctx}->{protocol} && defined $self->name()); | ||
| 110 | return $self->logging()->output($level,$type,ref $data1 ? +{ %{$self->{logging_ctx}}, %$data1 } : $data1); | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | sub parse_iso8601 | ||
| 115 | { | ||
| 116 | my ($self,$d)=@_; | ||
| 117 | $self->{iso8601_parser}=DateTime::Format::ISO8601->new() unless exists $self->{iso8601_parser}; | ||
| 118 | return $self->{iso8601_parser}->parse_datetime($d); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub build_strptime_parser | ||
| 122 | { | ||
| 123 | my ($self,@args)=@_; | ||
| 124 | my $key=join("\x{001E}",@args); | ||
| 125 | $self->{strptime_parser}->{$key}=DateTime::Format::Strptime->new(@args) unless exists $self->{strptime_parser}->{$key}; | ||
| 126 | return $self->{strptime_parser}->{$key}; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub create_local_object | ||
| 130 | { | ||
| 131 | my ($self,$what,@args)=@_; | ||
| 132 | return unless defined $self && defined $what; | ||
| 133 | my $fn=$self->factories(); | ||
| 134 | Net::DRI::Exception::err_assert('invalid factory name "'.$what.'"') unless defined $fn && ref $fn eq 'HASH' && exists $fn->{$what} && ref $fn->{$what} eq 'CODE'; | ||
| 135 | return $fn->{$what}->(@args); | ||
| 136 | } | ||
| 137 | |||
| 138 | ## This should not be called multiple times for a given Protocol class (as it will erase the loaded_modules slot) | ||
| 139 | sub _load | ||
| 140 | { | ||
| 141 | my ($self,@classes)=@_; | ||
| 142 | my $etype='protocol/'.$self->name(); | ||
| 143 | my $version=$self->version(); | ||
| 144 | |||
| 145 | my %skip = map { substr($_,1) => 1 } grep { /^-/ } @classes; | ||
| 146 | |||
| 147 | my (%done, @done, @commands); | ||
| 148 | foreach my $class (@classes) | ||
| 149 | { | ||
| 150 | next if exists $done{$class} || $class=~m/^-/ || exists $skip{$class}; | ||
| 151 | $self->log_output('debug','core',sprintf('Loading class "%s"',$class)); | ||
| 152 | Net::DRI::Util::load_module($class,$etype); | ||
| 153 | Net::DRI::Exception::method_not_implemented('register_commands',$class) unless $class->can('register_commands'); | ||
| 154 | my $rh=$class->register_commands($version); | ||
| 155 | $self->{commands_by_class}->{$class}=$rh; | ||
| 156 | push @commands, $rh; | ||
| 157 | |||
| 158 | if ($class->can('capabilities_add')) | ||
| 159 | { | ||
| 160 | my @a=$class->capabilities_add(); | ||
| 161 | if (ref($a[0])) | ||
| 162 | { | ||
| 163 | foreach my $a (@a) { $self->capabilities(@$a); } | ||
| 164 | } else | ||
| 165 | { | ||
| 166 | $self->capabilities(@a); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | $class->setup($self,$version) if $class->can('setup'); | ||
| 170 | $done{$class}=1; | ||
| 171 | push @done,$class; | ||
| 172 | } | ||
| 173 | |||
| 174 | $self->{loaded_modules}=\@done; | ||
| 175 | my %c; | ||
| 176 | foreach my $rh (@commands) | ||
| 177 | { | ||
| 178 | Net::DRI::Util::hash_merge(\%c, $rh); ## { object type => { action type => [ build action, parse action ]+ } } | ||
| 179 | } | ||
| 180 | $self->commands(\%c); | ||
| 181 | return; | ||
| 182 | } | ||
| 183 | |||
| 184 | ## has_module + find_action_in_class should instead better be based on some ID, like the XML namespace in EPP, | ||
| 185 | ## instead of the Perl module names | ||
| 186 | sub has_module | ||
| 187 | { | ||
| 188 | my ($self,$mod)=@_; | ||
| 189 | return 0 unless defined $mod && length $mod; | ||
| 190 | return (grep { $_ eq $mod } @{$self->{loaded_modules}})? 1 : 0; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub find_action_in_class | ||
| 194 | { | ||
| 195 | my ($self,$class,$otype,$oaction)=@_; | ||
| 196 | return unless defined $class && length $class && exists $self->{commands_by_class}->{$class} && exists $self->{commands_by_class}->{$class}->{$otype} && exists $self->{commands_by_class}->{$class}->{$otype}->{$oaction}; | ||
| 197 | return wantarray ? @{$self->{commands_by_class}->{$class}->{$otype}->{$oaction}} : $self->{commands_by_class}->{$class}->{$otype}->{$oaction}->[0]; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub _load_commands | ||
| 201 | { | ||
| 202 | my ($self,$otype,$oaction)=@_; | ||
| 203 | |||
| 204 | my $etype='protocol/'.$self->name(); | ||
| 205 | Net::DRI::Exception->die(1,$etype,7,'Object type and/or action not defined') unless (defined $otype && length $otype && defined $oaction && length $oaction); | ||
| 206 | my $h=$self->commands(); | ||
| 207 | Net::DRI::Exception->die(1,$etype,8,'No actions defined for object of type <'.$otype.'>') unless exists($h->{$otype}); | ||
| 208 | Net::DRI::Exception->die(1,$etype,9,'No action name <'.$oaction.'> defined for object of type <'.$otype.'> in '.ref($self)) unless exists($h->{$otype}->{$oaction}); | ||
| 209 | return $h; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub has_action | ||
| 213 | { | ||
| 214 | my ($self,$otype,$oaction)=@_; | ||
| 215 | return eval { my $h=$self->_load_commands($otype,$oaction); 1; } ? 1 : 0; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub action | ||
| 219 | { | ||
| 220 | my ($self,$otype,$oaction,$trid,@params)=@_; | ||
| 221 | my $h=$self->_load_commands($otype,$oaction); | ||
| 222 | |||
| 223 | ## Create a new message from scratch and loop through all functions registered for given action & type | ||
| 224 | my $msg=$self->create_local_object('message',$trid,$otype,$oaction); | ||
| 225 | Net::DRI::Exception->die(0,'protocol',1,'Unsuccessfull message creation') unless ($msg && ref $msg && $msg->isa('Net::DRI::Protocol::Message')); | ||
| 226 | $self->message($msg); ## store it for later use (in loop below) | ||
| 227 | |||
| 228 | foreach my $t (@{$h->{$otype}->{$oaction}}) | ||
| 229 | { | ||
| 230 | my $pf=$t->[0]; | ||
| 231 | next unless (defined($pf) && (ref($pf) eq 'CODE')); | ||
| 232 | $pf->($self,@params); | ||
| 233 | } | ||
| 234 | |||
| 235 | $self->message(undef); ## needed ? useful ? | ||
| 236 | return $msg; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub reaction | ||
| 240 | { | ||
| 241 | my ($self,$otype,$oaction,$dr,$sent,$oname,$trid)=@_; | ||
| 242 | my $h=$self->_load_commands($otype,$oaction); | ||
| 243 | my $msg=$self->create_local_object('message'); | ||
| 244 | Net::DRI::Exception->die(0,'protocol',1,'Unsuccessfull message creation') unless ($msg && ref($msg) && $msg->isa('Net::DRI::Protocol::Message')); | ||
| 245 | |||
| 246 | my %info; | ||
| 247 | ## TODO is $sent needed here really ? if not remove from API above also ! | ||
| 248 | $msg->parse($dr,\%info,$otype,$oaction,$sent); ## will trigger an Exception by itself if problem ## TODO : add later the whole LocalStorage stuff done when sending ? (instead of otype/oaction/message sent) | ||
| 249 | $self->message($msg); ## store it for later use (in loop below) | ||
| 250 | $info{$otype}->{$oname}->{name}=$oname if ($otype eq 'domain' || $otype eq 'host' || $otype eq 'nsgroup' || $otype eq 'keygroup'); ## TODO : abstract this ? | ||
| 251 | |||
| 252 | if (exists $h->{message} && exists $h->{message}->{result}) | ||
| 253 | { | ||
| 254 | foreach my $t (@{$h->{message}->{result}}) | ||
| 255 | { | ||
| 256 | my $pf=$t->[1]; | ||
| 257 | next unless (defined $pf && ref $pf eq 'CODE'); | ||
| 258 | $pf->($self,$otype,$oaction,$oname,\%info); | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | foreach my $t (@{$h->{$otype}->{$oaction}}) | ||
| 263 | { | ||
| 264 | my $pf=$t->[1]; | ||
| 265 | next unless (defined $pf && ref $pf eq 'CODE'); | ||
| 266 | $pf->($self,$otype,$oaction,$oname,\%info); | ||
| 267 | } | ||
| 268 | |||
| 269 | my $rc=$msg->result_status(); | ||
| 270 | if (defined $rc) | ||
| 271 | { | ||
| 272 | $rc->_set_trid([ $trid ]) unless $rc->trid(); ## if not done inside Protocol::*::Message::result_status, make sure we save at least our transaction id | ||
| 273 | foreach my $v1 (values %info) | ||
| 274 | { | ||
| 275 | next unless ref $v1 eq 'HASH' && keys %$v1; | ||
| 276 | foreach my $v2 (values %{$v1}) | ||
| 277 | { | ||
| 278 | next unless ref $v2 eq 'HASH' && keys %$v2; ## yes, this can happen, with must_reconnect for example | ||
| 279 | next if exists $v2->{result_status}; | ||
| 280 | $v2->{result_status}=$rc; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | } | ||
| 284 | $self->message(undef); ## needed ? useful ? | ||
| 285 | |||
| 286 | $info{session}->{exchange}->{result_from_cache}=0; | ||
| 287 | $info{session}->{exchange}->{protocol}=$self->nameversion(); | ||
| 288 | $info{session}->{exchange}->{trid}=$trid; | ||
| 289 | return ($rc,\%info); | ||
| 290 | } | ||
| 291 | |||
| 292 | sub nameversion | ||
| 293 | { | ||
| 294 | my $self=shift; | ||
| 295 | return $self->name().'/'.$self->version(); | ||
| 296 | } | ||
| 297 | |||
| 298 | sub factories | ||
| 299 | { | ||
| 300 | my ($self,$object,$code)=@_; | ||
| 301 | if (defined $object && defined $code) | ||
| 302 | { | ||
| 303 | $self->{factories}->{$object}=$code; | ||
| 304 | return $self; | ||
| 305 | } | ||
| 306 | return $self->{factories}; | ||
| 307 | } | ||
| 308 | |||
| 309 | sub capabilities | ||
| 310 | { | ||
| 311 | my ($self,$action,$object,$cap)=@_; | ||
| 312 | if (defined($action) && defined($object)) | ||
| 313 | { | ||
| 314 | $self->{capabilities}->{$action}={} unless exists($self->{capabilities}->{$action}); | ||
| 315 | if (defined($cap)) | ||
| 316 | { | ||
| 317 | $self->{capabilities}->{$action}->{$object}=$cap; | ||
| 318 | } else | ||
| 319 | { | ||
| 320 | delete($self->{capabilities}->{$action}->{$object}); | ||
| 321 | } | ||
| 322 | } | ||
| 323 | return $self->{capabilities}; | ||
| 324 | } | ||
| 325 | |||
| 326 | # Handles extracting a given argument, that may be in data refhash for all operations | ||
| 327 | # or inside a Changes object in case of update | ||
| 328 | sub extract_argument | ||
| 329 | { | ||
| 330 | my ($self, $key, $data) = @_; | ||
| 331 | my $arg; | ||
| 332 | |||
| 333 | if ($self->message()->operation()->[1] eq 'update') | ||
| 334 | { | ||
| 335 | $arg = $data->set($key); | ||
| 336 | } elsif (Net::DRI::Util::has_key($data, $key)) | ||
| 337 | { | ||
| 338 | $arg = $data->{$key}; | ||
| 339 | } | ||
| 340 | |||
| 341 | return $arg; | ||
| 342 | } | ||
| 343 | |||
| 344 | #################################################################################################### | ||
| 345 | 1; | ||