This file is indexed.

/usr/share/perl5/Net/OpenSRS.pm is in libnet-opensrs-perl 0.06-1.

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
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
=head1 NAME

Net::OpenSRS - Domain registration via the Tucows OpenSRS HTTPS XML API

=head1 Description

This is a wrapper interface to the DNS portions of the Tucows OpenSRS
HTTPS XML API.

The client library distributed by OpenSRS can be difficult to integrate
into a custom environment, and their web interface becomes quickly
tedious with heavy usage. This is a clean and relatively quick library
to perform the most common API methods described in the OpenSRS API
documentation.

=head1 Examples

 use Net::OpenSRS;

 my $key = 'Your_API_Key_From_The_Reseller_Interface';
 my $srs = Net::OpenSRS->new();

 $srs->environment('live');
 $srs->set_key( $key );

 $srs->set_manage_auth( 'manage_username', 'manage_password' );

 my $cookie = $srs->get_cookie( 'spime.net' );
 if ($cookie) {
     print "Cookie:  $cookie\n";
 } else {
     print $srs->last_response() . "\n";
 }

 # do a batch of domain locks
 $srs->bulk_lock([ 'example.com', 'example.net', ... ]);

 # renew a domain
 my $result = $srs->renew_domain( 'example.com' );
 ...

=head1 Notes

=head2 Prerequisites

This module requires some setup in the OpenSRS reseller environment
before it will work correctly.

=over 4

=item Reseller account

You need to have an OpenSRS account, of course.  If you aren't an
OpenSRS reseller, this module will be of limited use to you. :)

=item Script API network access

The machine(s) using this module need to have their public IP addresses
added to your 'Script API allow' list in the OpenSRS web interface.
(You'll only need to do this once, assuming your IP doesn't change.)

=item API key generation

You'll need to pregenerate your API keys - also in the the OpenSRS web
interface.  These keys are used for all reseller API authentication.

=back

=head2 Assumptions

OpenSRS allows for a variety of ways to organize your domains.  Because
of this, writing a 'one size fits all' module is rather difficult.
Instead, we make a few assumptions regarding the way people use their
OpenSRS reseller accounts.

**** These assumptions will ultimately determine if this module is right for
you!  Please read them carefully! ****

=over 4

=item Management 'master' account.

We assume that all domains are under one global management owner
account.  If customers want access to the management interface, we're
operating under the idea that you create subaccounts for them -
retainting the master account information for your own use.  (If you
aren't doing this, it really makes things easier for you in the long
run.)

For example, 'spime.net' is my master management account.  Before doing
any register_domain() calls, I call master_domain('spime.net') - then
any transfers or registrations from that point forward are linked to
'spime.net'.  If a customer wants access to the SRS web management
interface, I can then just create a subaccount for just their domain,
so I retain absolute control -- in the event a customer forgets their
password, I'm covered.

=item Usernames

We assume that your management username 'master' account is identical to
your reseller username, and just the passwords differ.

=item Default registration info

We assume you've properly set up default technical contact information,
including your default nameservers, in the OpenSRS reseller web
interface.

=item Return codes

Unless otherwise noted, all methods return true on success, false on
failure, and undefined on caller error.

=back

=head2 Default environment

This library defaults to the TEST environment. (horizon.)  Many API
methods don't work in the test environment (SET COOKIE being the most
notable example, as any API method relying on a cookie doesn't work
either.)  Neither does batch processing.  Most everything else should be
ok.  ( See environment() )

=head2 The '$c' variable

Many methods require customer information.  I leave the method of
fetching this information entirely to you.  All examples below that show
a $c variable expect a hashref (or object) that contain these keys:

    my $c = {
        firstname => 'John',
        lastname  => 'Doe',
        city      => 'Portland',
        state     => 'Oregon',
        country   => 'US',
        address   => '555 Someplace Street',
        email     => 'john@example.com',
        phone     => '503-555-1212',
        company   => 'n/a'
    };

=cut

package Net::OpenSRS;

use strict;
use warnings;
use LWP::UserAgent;
use XML::Simple;
use Digest::MD5;
use Date::Calc qw/ Add_Delta_Days Today This_Year /;

our $VERSION = '0.06';
my $rv;
*hash = \&Digest::MD5::md5_hex;

#----------------------------------------------------------------------
# utility methods
#----------------------------------------------------------------------

=head1 Utility methods

=over 4

=item new()

 my $srs = Net::OpenSRS->new();

Create a new Net::OpenSRS object.  There are no options for this
method.

=cut

sub new
{
    my ($class, %opts) = @_;
    my $self = {};
    bless $self, $class;

    $self->{config} = {
        use_test_env  => 1,
        debug         => 0,
        master_domain => undef,

        bulkhost => 'https://batch.opensrs.net:55443',

        # reseller auth keys, as generated via the reseller website.
        live => {
            key  => undef,
            host => 'https://rr-n1-tor.opensrs.net:55443',
        },
        test => {
            key  => undef,
            host => 'https://horizon.opensrs.net:55443',
        }
    };

    return $self;
}

sub debug
{
    my $self = shift;
    return unless $self->debug_level;
    print STDERR shift() . "\n";
}

=item debug_level()

Setting the debug level will print various pieces of information to
STDERR when connecting to OpenSRS.  Use this if something isn't working
the way you think it should be.

=item 0

Disable debugging.

=item 1

Print current environment, host, and HTTP response.

=item 2

Add XML request and response to output.

=item 3

Add SSL debugging to output.

Debugging is off by default.  When called without an argument, returns
the current debug level.

=cut

sub debug_level
{
    my ($self, $level) = @_;
    return $self->{config}->{debug} unless $level;
    $self->{config}->{debug} = $level;
    return;
}

=item last_response()

All Net::OpenSRS methods set the last OpenSRS API reply in a temporary
variable.  You can view the contents of this variable using the
last_response() method.

Note that it is reset on each method call.

Returns the last OpenSRS return code and result string, or if passed any
true value, instead returns the full XML (parsed into a hashref) of the
last OpenSRS return. (perfect for Data::Dumper)

Examples:
   200: Command Successful
   400: Domain example.com does not exist with OpenSRS

=cut

sub last_response
{
    my ($self, $obj) = @_;
    return $obj ? $rv : $self->{last_response} || '';
}

=item set_manage_auth()

 $srs->set_manage_auth( $username, $password );

Set the owner management username and password.  This is used to fetch
cookies, and perform any API methods that require the management cookie.
For specifics on this, see the OpenSRS API documentation.

=cut

sub set_manage_auth
{
    my ($self, $user, $pass) = @_;
    return undef unless $user && $pass;
    $self->{config}->{username} = $user;
    $self->{config}->{password} = $pass;
    return 1;
}

=item set_key()

Tell the OpenSRS object what secret key to use for authentication.
You can generate a new secret key by using the OpenSRS reseller web
interface.  This key is required to perform any API functions.

set_key() is affected by the current environment().  Calling the
set_key() method while in the test environment only sets the key for the
test environment - likewise for the live environment.  To set a key for
the live environment, you need to call environment('live') B<first>.

=cut

sub set_key
{
    my ($self, $key) = @_;
    return undef unless $key;
    $self->{config}->{ $self->environment }->{key} = $key;
    return 1;
}

=item environment()

 my $env = $srs->environment;
 $srs->environment('live');

Without an argument, returns a string - either 'test', or 'live',
depending on the environment the object is currently using.

The test environment is the default.

If passed an argument (either 'test' or 'live') - switches into the
desired environment.  You will need to set_key() if you were previously
using a different environment, or if you hadn't set_key() yet.

=cut

sub environment
{
    my ($self, $env) = @_;
    return ($self->{config}->{use_test_env} ? 'test' : 'live')
        unless $env && $env =~ /(test|live)/i;
    $self->{config}->{use_test_env} = 
        $1 eq 'test' ? 1 : 0;
    return;
}

=item master_domain()

 my $master = $srs->master_domain;
 $srs->master_domain('spime.net');

Without an argument, returns the currently set 'master domain' account.
Otherwise, it sets the master domain.

New transfers and registrations are linked under this domain, for
centralized management.  See the 'Assumptions' section, above.

=cut

sub master_domain
{
    my ($self, $domain) = @_;
    return $self->{config}->{master_domain} unless $domain;
    $self->{config}->{master_domain} = $domain;
    return;
}

# set last status messages/codes in $self,
# for the benefit of the caller.
sub _set_response
{
    my $self = shift;
    $rv->{response_text} =~ s/Error: //;
    $self->{last_response} = $rv->{response_code} . ": " . $rv->{response_text};
    return;
}

#----------------------------------------------------------------------
# SRS API methods
#----------------------------------------------------------------------

=back

=head1 OpenSRS API methods

=over 4

=item bulk_lock() / bulk_unlock()

Locks or unlocks up to 1000 domains at a time.

 my $result = $srs->bulk_lock([ 'example.com', 'example.net' ]);

Returns remote bulk queue id on successful batch submission.

=cut

sub bulk_lock
{
    my $self = shift;
    return $self->_bulk_action( 'lock', @_ );
}

sub bulk_unlock
{
    my $self = shift;
    return $self->_bulk_action( 'unlock', @_ );
}

sub _bulk_action
{
    my ( $self, $toggle, $domains ) = @_;
    return undef unless $toggle =~ /lock|unlock/i && 
                        ref $domains;
    return undef if scalar @$domains >= 1000;

    $rv = $self->make_request(
        {
            batch   => 1,
            action  => 'submit',
            object  => 'bulk_change',
            attributes => {
                change_type => 'domain_lock',
                change_items => $domains,
                op_type => lc $toggle,
            }
        }
    );
    return undef unless $rv;

    $self->_set_response;
    return $rv->{is_success} ? $rv->{bulk_change_req_id} : 0;
}

=item check_queued_request()

 my $result = $srs->check_queued_request( $queue_id );

Requires queue id - returned from batch methods such as bulk_lock().
Always returns hashref of queue command on success.  
Check $srs->last_response() for status progress.

=cut

sub check_queued_request
{
    my ( $self, $id ) = @_;
    return undef unless $id;

    $rv = $self->make_request(
        {
            action  => 'query_queued_request',
            object  => 'domain',
            attributes => {
                request_id => $id,
            }
        }
    );
    return undef unless $rv;

    $self->_set_response;
    return $rv->{attributes}->{request_data};
}

=item check_transfer()

 my $result = $srs->check_transfer( 'example.com' );

Checks the status of a transfer in progress.  Returns hashref of
'contact_email', 'status', and 'last_update_time' for a given domain
transfer.  The 'status' key is always one of the following:

        pending_owner  (waiting on owner confirmation)
        pending_admin  (waiting on opensrs staff confirmation)
        pending_registry  (waiting on register to complete)
        completed  (transfer done)
        cancelled  (reseller cancelled transfer in progress)
        undefined  (no transfer in progress)

If the domain in question has no transfer in progress - instead checks
to see if the domain is capable of transfer.  Returns hashref of
'transferrable' (boolean) and 'reason' (string).

=cut

sub check_transfer
{
    my ( $self, $domain ) = @_;
    return undef unless $domain;

    $rv = $self->make_request(
        {
            action     => 'check_transfer',
            object     => 'domain',
            attributes => {
                domain              => $domain,
                get_request_address => 1,
            }
        }
    );
    return undef unless $rv;

    $self->_set_response;
    if ( $rv->{attributes}->{status} ) {
        return {
            status           => $rv->{attributes}->{status},
            last_update_time => $rv->{attributes}->{unixtime},
            contact_email    => $rv->{attributes}->{request_address}
        };
    }
    else {
        return $rv->{attributes}; #(transferrable bool and reason)
    }
}

=item get_cookie()

OpenSRS management APIs require a cookie to be generated, and sent along
with the API request.

 $cookie = $srs->get_cookie( 'example.com ');
 ($cookie, $expiration_date) = $srs->get_cookie( 'example.com ');

Make sure you've set_manage_auth() before attempting any cookie required
APIs.

Returns cookie on success, undefined on error.  (Check error with
last_response())

In array context, returns cookie and expiration date of the domain.

=cut

sub get_cookie
{
    my ($self, $domain) = @_;
    return undef unless $domain;
    $rv = $self->make_request(
        {
            action     => 'set',
            object     => 'cookie',
            attributes => {
                reg_username => $self->{config}->{username},
                reg_password => $self->{config}->{password},
                domain => $domain
            }
        }
    );
    return undef unless $rv;

    $self->_set_response;
    if ($rv->{is_success}) {
        return
          wantarray
          ? ( $rv->{attributes}->{cookie}, $rv->{attributes}->{expiredate} )
          : $rv->{attributes}->{cookie};
    }
    return undef;
}

=item get_expiring_domains()

 my $results = $srs->get_expiring_domains( 60 );

 Fetch and return OpenSRS hashref of expiring domains, within
 the specified timeperiod.  (In days.)

 Time period defaults to 30 days.

=cut

sub get_expiring_domains
{
    my ($self, $timeframe) = @_;
    $timeframe ||= 30;

    my $today   = join '-', map { sprintf( "%02d", $_ ) } Date::Calc::Today();
    my $expdate = join '-', map { sprintf( "%02d", $_ ) }
      Date::Calc::Add_Delta_Days( ( split '-', $today ), $timeframe );

    $rv = $self->make_request(
        {
            action     => 'get_domains_by_expiredate',
            object     => 'domain',
            attributes => {
                limit    => 1000,
                exp_from => $today,
                exp_to   => $expdate,
            }
        }
    );
    return undef unless $rv;

    $self->_set_response;
    return $rv->{attributes}->{exp_domains} if $rv->{is_success};
    return undef;
}

=item is_available()

Hey OpenSRS! Is this domain registered, or is it available?

 my $result = $srs->is_available( 'example.com ');

Returns true if the domain is available, false if it is already
registered.

=cut

sub is_available
{
    my ($self, $domain) = @_;
    return undef unless $domain;
    $rv = $self->make_request(
        {
            action     => 'lookup',
            object     => 'domain',
            attributes => {
                domain => $domain
            }
        }
    );
    return undef unless $rv;
    $self->_set_response;
    return undef unless $rv->{is_success};
    return $rv->{response_code} == 210 ? 1 : 0;
}

=item register_domain()

 my $result = $srs->register_domain( 'example.com', $c );

Register a new domain.  Default nameserver and tech info used from
OpenSRS settings.

=cut

sub register_domain
{
    my ($self, $domain, $c, $transfer) = @_;
    return undef unless $domain;

    # sanity checks
    unless ($self->{config}->{username}) {
        $self->debug("Management auth not set.");
        return undef;
    }
    unless (ref $c) {
        $self->debug("2nd arg must be a reference to customer info.");
        return undef;
    }

    my $epp_phone = $c->{phone};
    $epp_phone =~ s/[\.\-]//g;
    $epp_phone = '+1.' . $epp_phone;

    # blah, this sucks.
    # it would be really nice if OpenSRS figured out the country -> code
    # conversion on their end of things.
    my %country_codes = (
        'Afghanistan'                            => 'AF',
        'Albania'                                => 'AL',
        'Algeria'                                => 'DZ',
        'American Samoa'                         => 'AS',
        'Andorra'                                => 'AD',
        'Angola'                                 => 'AO',
        'Anguilla'                               => 'AI',
        'Antarctica'                             => 'AQ',
        'Antigua And Barbuda'                    => 'AG',
        'Argentina'                              => 'AR',
        'Armenia'                                => 'AM',
        'Aruba'                                  => 'AW',
        'Australia'                              => 'AU',
        'Austria'                                => 'AT',
        'Azerbaijan'                             => 'AZ',
        'Bahamas'                                => 'BS',
        'Bahrain'                                => 'BH',
        'Bangladesh'                             => 'BD',
        'Barbados'                               => 'BB',
        'Belarus'                                => 'BY',
        'Belgium'                                => 'BE',
        'Belize'                                 => 'BZ',
        'Benin'                                  => 'BJ',
        'Bermuda'                                => 'BM',
        'Bhutan'                                 => 'BT',
        'Bolivia'                                => 'BO',
        'Bosnia Hercegovina'                     => 'BA',
        'Botswana'                               => 'BW',
        'Bouvet Island'                          => 'BV',
        'Brazil'                                 => 'BR',
        'British Indian Ocean Territory'         => 'IO',
        'Brunei Darussalam'                      => 'BN',
        'Bulgaria'                               => 'BG',
        'Burkina Faso'                           => 'BF',
        'Burundi'                                => 'BI',
        'Cambodia'                               => 'KH',
        'Cameroon'                               => 'CM',
        'Canada'                                 => 'CA',
        'Cape Verde'                             => 'CV',
        'Cayman Islands'                         => 'KY',
        'Central African Republic'               => 'CF',
        'Chad'                                   => 'TD',
        'Chile'                                  => 'CL',
        'China'                                  => 'CN',
        'Christmas Island'                       => 'CX',
        'Cocos (Keeling) Islands'                => 'CC',
        'Colombia'                               => 'CO',
        'Comoros'                                => 'KM',
        'Congo'                                  => 'CG',
        'Congo The Democratic Republic Of'       => 'CD',
        'Cook Islands'                           => 'CK',
        'Costa Rica'                             => 'CR',
        'Cote D\'Ivoire'                         => 'CI',
        'Croatia'                                => 'HR',
        'Cuba'                                   => 'CU',
        'Cyprus'                                 => 'CY',
        'Czech Republic'                         => 'CZ',
        'Denmark'                                => 'DK',
        'Djibouti'                               => 'DJ',
        'Dominica'                               => 'DM',
        'Dominican Republic'                     => 'DO',
        'Ecuador'                                => 'EC',
        'Egypt'                                  => 'EG',
        'El Salvador'                            => 'SV',
        'Equatorial Guinea'                      => 'GQ',
        'Eritrea'                                => 'ER',
        'Estonia'                                => 'EE',
        'Ethiopia'                               => 'ET',
        'Falkland Islands (Malvinas)'            => 'FK',
        'Faroe Islands'                          => 'FO',
        'Fiji'                                   => 'FJ',
        'Finland'                                => 'FI',
        'France'                                 => 'FR',
        'French Guiana'                          => 'GF',
        'French Polynesia'                       => 'PF',
        'French Southern Territories'            => 'TF',
        'Gabon'                                  => 'GA',
        'Gambia'                                 => 'GM',
        'Georgia'                                => 'GE',
        'Germany'                                => 'DE',
        'Ghana'                                  => 'GH',
        'Gibraltar'                              => 'GI',
        'Greece'                                 => 'GR',
        'Greenland'                              => 'GL',
        'Grenada'                                => 'GD',
        'Guadeloupe'                             => 'GP',
        'Guam'                                   => 'GU',
        'Guatemela'                              => 'GT',
        'Guinea'                                 => 'GN',
        'Guinea-Bissau'                          => 'GW',
        'Guyana'                                 => 'GY',
        'Haiti'                                  => 'HT',
        'Heard and McDonald Islands'             => 'HM',
        'Honduras'                               => 'HN',
        'Hong Kong'                              => 'HK',
        'Hungary'                                => 'HU',
        'Iceland'                                => 'IS',
        'India'                                  => 'IN',
        'Indonesia'                              => 'ID',
        'Iran (Islamic Republic Of)'             => 'IR',
        'Iraq'                                   => 'IQ',
        'Ireland'                                => 'IE',
        'Israel'                                 => 'IL',
        'Italy'                                  => 'IT',
        'Jamaica'                                => 'JM',
        'Japan'                                  => 'JP',
        'Jordan'                                 => 'JO',
        'Kazakhstan'                             => 'KZ',
        'Kenya'                                  => 'KE',
        'Kiribati'                               => 'KI',
        'Korea, Democratic People\'s Republic Of' => 'KP',
        'Korea, Republic Of'                     => 'KR',
        'Kuwait'                                 => 'KW',
        'Kyrgyzstan'                             => 'KG',
        'Lao People\'s Democratic Republic'      => 'LA',
        'Latvia'                                 => 'LV',
        'Lebanon'                                => 'LB',
        'Lesotho'                                => 'LS',
        'Liberia'                                => 'LR',
        'Libyan Arab Jamahiriya'                 => 'LY',
        'Liechtenstein'                          => 'LI',
        'Lithuania'                              => 'LT',
        'Luxembourg'                             => 'LU',
        'Macau'                                  => 'MO',
        'Macedonia'                              => 'MK',
        'Madagascar'                             => 'MG',
        'Malawi'                                 => 'MW',
        'Malaysia'                               => 'MY',
        'Maldives'                               => 'MV',
        'Mali'                                   => 'ML',
        'Malta'                                  => 'MT',
        'Marshall Islands'                       => 'MH',
        'Martinique'                             => 'MQ',
        'Mauritania'                             => 'MR',
        'Mauritius'                              => 'MU',
        'Mayotte'                                => 'YT',
        'Mexico'                                 => 'MX',
        'Micronesia, Federated States Of'        => 'FM',
        'Moldova, Republic Of'                   => 'MD',
        'Monaco'                                 => 'MC',
        'Mongolia'                               => 'MN',
        'Montserrat'                             => 'MS',
        'Morocco'                                => 'MA',
        'Mozambique'                             => 'MZ',
        'Myanmar'                                => 'MM',
        'Namibia'                                => 'NA',
        'Nauru'                                  => 'NR',
        'Nepal'                                  => 'NP',
        'Netherlands'                            => 'NL',
        'Netherlands Antilles'                   => 'AN',
        'New Caledonia'                          => 'NC',
        'New Zealand'                            => 'NZ',
        'Nicaragua'                              => 'NI',
        'Niger'                                  => 'NE',
        'Nigeria'                                => 'NG',
        'Niue'                                   => 'NU',
        'Norfolk Island'                         => 'NF',
        'Northern Mariana Islands'               => 'MP',
        'Norway'                                 => 'NO',
        'Oman'                                   => 'OM',
        'Pakistan'                               => 'PK',
        'Palau'                                  => 'PW',
        'Palestine'                              => 'PS',
        'Panama'                                 => 'PA',
        'Papua New Guinea'                       => 'PG',
        'Paraguay'                               => 'PY',
        'Peru'                                   => 'PE',
        'Philippines'                            => 'PH',
        'Pitcairn'                               => 'PN',
        'Poland'                                 => 'PL',
        'Portugal'                               => 'PT',
        'Puerto Rico'                            => 'PR',
        'Qatar'                                  => 'QA',
        'Reunion'                                => 'RE',
        'Romania'                                => 'RO',
        'Russian Federation'                     => 'RU',
        'Rwanda'                                 => 'RW',
        'Saint Helena'                           => 'SH',
        'Saint Kitts And Nevis'                  => 'KN',
        'Saint Lucia'                            => 'LC',
        'Saint Pierre and Miquelon'              => 'PM',
        'Saint Vincent and The Grenadines'       => 'VC',
        'Samoa'                                  => 'WS',
        'San Marino'                             => 'SM',
        'Sao Tome and Principe'                  => 'ST',
        'Saudi Arabia'                           => 'SA',
        'Senegal'                                => 'SN',
        'Serbia and Montenegro'                  => 'CS',
        'Seychelles'                             => 'SC',
        'Sierra Leone'                           => 'SL',
        'Singapore'                              => 'SG',
        'Slovakia'                               => 'SK',
        'Slovenia'                               => 'SI',
        'Solomon Islands'                        => 'SB',
        'Somalia'                                => 'SO',
        'South Africa'                           => 'ZA',
        'South Georgia and The Sandwich Islands' => 'GS',
        'Spain'                                  => 'ES',
        'Sri Lanka'                              => 'LK',
        'Sudan'                                  => 'SD',
        'Suriname'                               => 'SR',
        'Svalbard and Jan Mayen Islands'         => 'SJ',
        'Swaziland'                              => 'SZ',
        'Sweden'                                 => 'SE',
        'Switzerland'                            => 'CH',
        'Syrian Arab Republic'                   => 'SY',
        'Taiwan'                                 => 'TW',
        'Tajikista'                              => 'TJ',
        'Tanzania, United Republic Of'           => 'TZ',
        'Thailand'                               => 'TH',
        'Timor-Leste'                            => 'TL',
        'Togo'                                   => 'TG',
        'Tokelau'                                => 'TK',
        'Tonga'                                  => 'TO',
        'Trinidad and Tobago'                    => 'TT',
        'Tunisia'                                => 'TN',
        'Turkey'                                 => 'TR',
        'Turkmenistan'                           => 'TM',
        'Turks and Caicos Islands'               => 'TC',
        'Tuvalu'                                 => 'TV',
        'Uganda'                                 => 'UG',
        'Ukraine'                                => 'UA',
        'United Arab Emirates'                   => 'AE',
        'United Kingdom (GB)'                    => 'GB',
        'United Kingdom (UK)'                    => 'UK',
        'United States'                          => 'US',
        'United States Minor Outlying Islands'   => 'UM',
        'Uruguay'                                => 'UY',
        'Uzbekistan'                             => 'UZ',
        'Vanuatu'                                => 'VU',
        'Vatican City State'                     => 'VA',
        'Venezuela'                              => 'VE',
        'Vietnam'                                => 'VN',
        'Virgin Islands (British)'               => 'VG',
        'Virgin Islands (U.S.)'                  => 'VI',
        'Wallis and Futuna Islands'              => 'WF',
        'Western Sahara'                         => 'EH',
        'Yemen Republic of'                      => 'YE',
        'Zambia'                                 => 'ZM',
        'Zimbabwe'                               => 'ZW'
    );  # end suckage

    # attempt countryname translation if needed
    if ( $c->{country} !~ m/^[A-Z]{2,3}$/ ) {
    	$c->{country} = $country_codes{$c->{country}};

        unless ( defined( $c->{country} ) ) {
            $self->debug("Invalid country.");
            return undef;
        }
    }

    # build contact hashref from customer info.
    my $contact_info = {
        first_name  => $c->{firstname},
        last_name   => $c->{lastname},
        city        => $c->{city},
        state       => $c->{state},
        country     => $c->{country},
        address1    => $c->{address},
        postal_code => $c->{zip},
        email       => $c->{email},
        phone       => $epp_phone,
        org_name    => $c->{company} || 'n/a',
    };

    $rv = $self->make_request(
        {
            action     => 'sw_register',
            object     => 'domain',
            attributes => {
                domain              => $domain,
                custom_nameservers  => 0,
                custom_tech_contact => 0,
                auto_renew          => 0,
                period              => 1,
                f_lock_domain       => 1,
                contact_set         => {
                    admin   => $contact_info,
                    billing => $contact_info,
                    owner   => $contact_info
                },
                reg_username => $self->{config}->{username},
                reg_password => $self->{config}->{password},
                reg_type   => $transfer ? 'transfer' : 'new',
                reg_domain => $self->{config}->{master_domain}, # link domain to the 'master' account
            }
        }
    );
    $self->_set_response;
    return $rv->{is_success};
}

=item renew_domain()

 my $result = $srs->renew_domain( 'example.com', 1 );

Renew a domain for a period of time in years. 1 year is the default.

=cut

sub renew_domain
{
    my ($self, $domain, $years) = @_;
    return undef unless $domain;
    $years ||= 1;

    # sanity checks
    unless ($self->{config}->{username}) {
        $self->debug("Management auth not set.");
        return undef;
    }

    # get current expiration year (why do they need this, again?)
    my (undef, $expiration) = $self->get_cookie( $domain );
    $expiration = $1 if $expiration =~ /^(\d{4})-/;
    $expiration ||= Date::Calc::This_Year();
    
    $rv = $self->make_request(
        {
            action     => 'renew',
            object     => 'domain',
            attributes => {
                domain                => $domain,
                auto_renew            => 0,
                handle                => 'process',
                period                => $years,
                currentexpirationyear => $expiration,
            }
        }
    );
    $self->_set_response;
    return $rv->{is_success};
}

=item revoke_domain()

Revoke a previously registered domain.  This only works if the domain is
still within the grace period as defined by the registrar.
Requires you to have called set_manage_auth() B<first>.

 my $result = $srs->revoke_domain( 'example.com' );

Returns true if the revoke is successful, false otherwise.
Returns undefined on error.

=cut

sub revoke_domain
{
    my ($self, $domain) = @_;
    return undef unless $domain;
    unless ($self->{config}->{username}) {
        $self->debug("Management auth not set.");
        return undef;
    }
    $rv = $self->make_request(
        {
            action     => 'revoke',
            object     => 'domain',
            attributes => {
                reseller => $self->{config}->{username},
                domain => $domain,
            }
        }
    );
    $self->_set_response;
    return $rv->{is_success};
}

=item transfer_domain()

 my $result = $srs->transfer_domain( 'example.com', $c );

Transfer a domain under your control.
Returns true on success, false on failure, and undefined on caller error.

=cut

sub transfer_domain
{
    my $self = shift;
    return $self->register_domain( @_, 1 );
}

=item make_request()

This method is the real workhorse of this module.  If any OpenSRS API
isn't explicity implemented in this module as a method call (such as
get_cookie(), bulk_lock(), etc), you can use make_request() to build and send
the API yourself.

Examples:

 my $result = $srs->make_request(
     {
         batch   => 1,
         action  => 'submit',
         object  => 'bulk_change',
         attributes => {
             change_type => 'domain_lock',
             change_items => [ 'example.com', 'example.net' ],
             op_type => 'lock',
         }
     }
 );

 my $result = $srs->make_request(
     {
         action     => 'lookup',
         object     => 'domain',
         attributes => {
             domain => 'example.com'
         }
     }
 );

Returns a hashref containing parsed XML results from OpenSRS.

Example return:

 {
     'protocol' => 'XCP',
     'object' => 'DOMAIN',
     'response_text' => 'Domain taken',
     'action' => 'REPLY',
     'response_code' => '211',
     'attributes' => {
         'status' => 'taken',
         'match' => {}
     },
     'is_success' => '1'
 }

=cut

# build opensrs xml protocol string.  submit.
# convert xml response to data structure, and return.
sub make_request
{
    my ($self, $data) = @_;
    return undef unless ref $data;

    $self->debug("Using " . $self->environment . " environment.");

    my $key  = $self->{config}->{ $self->environment }->{key};
    my $host = $self->{config}->{ $self->environment }->{host};
    $ENV{HTTPS_DEBUG} = 1 if $self->debug_level > 2;

    unless ($key) {
        $self->debug("Authentication key not set.");
        return undef;
    }

    my $action = uc $data->{action};
    my $object = uc $data->{object};

    # build our XML request.
    # lets not bother with anything super fancy, 
    # everything but the item keys are always static anyway.
    my $xml;
    $xml = <<XML;
<?xml version='1.0' encoding="UTF-8" standalone="no" ?>
<!DOCTYPE OPS_envelope SYSTEM "ops.dtd">
<OPS_envelope>
<header><version>0.9</version></header>
<body>
<data_block>
<dt_assoc>
  <item key="protocol">XCP</item>
  <item key="action">$action</item>
  <item key="object">$object</item>
XML

    $xml .= "  <item key=\"cookie\">$data->{cookie}</item>\n" if $data->{cookie};

$xml .= <<XML;
  <item key="attributes">
    <dt_assoc>
XML

    foreach (sort keys %{ $data->{attributes} }) {
        my $val = $data->{attributes}->{$_};
        $xml .= $self->_format( $val, 4 );
    }
    $xml .= <<XML;
    </dt_assoc>
  </item>
</dt_assoc>
</data_block>
</body>
</OPS_envelope>
XML

    # whoof, ok.  got our request built.  lets ship it off.
    if ($self->debug_level > 1) {
        $self->debug("\nClient Request XML:\n" . '-' x 30);
        $self->debug($xml);
    }

    $host = $self->{config}->{bulkhost} if $data->{batch};
    $self->debug("Making request to $host...");
    my $ua = LWP::UserAgent->new( timeout => 20, agent => "Net::OpenSRS/$VERSION" );
    unless ($ua) {
        $self->debug("Unable to contact remote host.");
        return undef;
    }

    my $res = $ua->post( 
        $host,
        'Content-Type' => 'text/xml',
        'X-Username'   => $self->{config}->{username},
        'X-Signature'  => hash( hash( $xml, $key ), $key ),
        'Content'      => $xml
    );

    my $struct;
    if ( $res->is_success ) {
        $self->debug("HTTP result: " . $res->status_line);
        my $rslt = $res->content;
        # OpenSRS renew response triggers Expat parser error due to spaces in element name
        $rslt =~ s/registration expiration date/registration_expiration_date/g;

        eval { $struct = XML::Simple::XMLin(
                 $rslt,
                 'KeyAttr' => [ 'dt_assoc' ],
                 'GroupTags' => { 'dt_assoc' => 'item',  'dt_array' => 'item' },
               );
        };

        if ($self->debug_level > 1) {
            $self->debug("\nOpenSRS Response XML:\n" . '-' x 30);
            $self->debug($res->content);
            $self->debug('');
        }

        # get the struct looking just how we want it.
        # (de-nastify it.)
        (undef, $struct) = _denastify( $struct->{body}->{data_block} );
    }
    else {
        $self->debug("HTTP error: " . $res->status_line);
        return undef;
    }

    $rv = $struct;
    $self->_set_response;
    return $self->last_response(1);
}

# encode special characters

my %encode_hash = (
  '<' => '&lt;',
  '>' => '&gt;',
  "'" => '&apos;',
  '"' => '&quot;',
  '&' => '&amp;',
);

sub _encode
{
  my $arg = shift;
  return $arg unless ($arg =~/\<|\>|\'|\"|\&/);
  $arg =~ s/(\<|\>|\'|\"|\&)/$encode_hash{$1}/ge;
  $arg
}

# format perl structs into opensrs XML
sub _format
{
    my ($self, $val, $indent) = @_;
    my $xml;

    $indent ||= 6;
    my $sp = ' ' x $indent;

    if ( ref $val eq 'ARRAY' ) {
        my $c = 0;
        $xml .= "$sp<item key=\"$_\">\n";
        $xml .= "$sp  <dt_array>\n";
        foreach (map { _encode($_) } sort @$val) {
            $xml .= "$sp    <item key=\"$c\">$_</item>\n";
            $c++;
        }
        $xml .= "$sp  </dt_array>\n";
        $xml .= "$sp</item>\n";
    }

    elsif ( ref $val eq 'HASH' ) {
        $xml .= "$sp<item key=\"$_\">\n";
        $xml .= "$sp<dt_assoc>\n";
        foreach (sort keys %$val) {
            $xml .= $self->_format( $val->{$_} );
        }
        $xml .= "$sp</dt_assoc>\n";
        $xml .= "$sp</item>\n";
    }

    else {
        $val = _encode($val);
        $xml .= "$sp<item key=\"$_\">$val</item>\n";
    }

    return $xml;
}

sub _denastify {
    my ($arg) = ( shift );

    if ( 0 ) {
      eval { use Data::Dumper };
      warn $@ if $@;
      warn "_denastify\n". Dumper($arg) unless $@;
    }

    if ( ref($arg) eq 'HASH' ) {
        my $value;
        if ( exists( $arg->{content} ) ) {
            $value = $arg->{content};
        } elsif ( exists( $arg->{dt_array} ) ) {
            my $array = $arg->{dt_array};
            $array = [ $array ] unless ref($array) eq 'ARRAY';
            $value = [ map {
                               { map { _denastify($_) } @{ $_->{dt_assoc} } }
                           }
                       @$array
                     ];
        } elsif ( exists( $arg->{dt_assoc} ) ) {
            my $array = $arg->{dt_assoc};
            $array = [ $array ] unless ref($array) eq 'ARRAY';
            $value = { map { _denastify($_) } @$array };
        }
        return ( $arg->{key} => $value );
    }
    ();
}

=back

=head1 Author

Mahlon E. Smith I<mahlon@martini.nu> for Spime Solutions Group
I<(www.spime.net)>

=cut

1;