File: Simple.pm

package info (click to toggle)
libnet-epp-perl 0.28-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 384 kB
  • sloc: perl: 2,540; sh: 3; makefile: 2
file content (2227 lines) | stat: -rw-r--r-- 65,302 bytes parent folder | download
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
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
package Net::EPP::Simple;
use Carp;
use Config;
use Digest::SHA qw(sha1_hex);
use List::Util  qw(any);
use Net::EPP;
use Net::EPP::Frame;
use Net::EPP::ResponseCodes;
use Time::HiRes qw(time);
use base        qw(Net::EPP::Client);
use constant EPP_XMLNS => 'urn:ietf:params:xml:ns:epp-1.0';
use vars qw($Error $Code $Message @Log);
use strict;
use warnings;

our $Error   = '';
our $Code    = OK;
our $Message = '';
our @Log     = ();

=pod

=head1 NAME

Net::EPP::Simple - a simple EPP client interface for the most common jobs.

=head1 SYNOPSIS

    #!/usr/bin/perl
    use Net::EPP::Simple;
    use strict;

    my $epp = Net::EPP::Simple->new(
        host => 'epp.nic.tld',
        user => 'my-id',
        pass => 'my-password',
    );

    my $domain = 'example.tld';

    if ($epp->check_domain($domain) == 1) {
        print "Domain is available\n" ;

    } else {
        my $info = $epp->domain_info($domain);
        printf("Domain was registered on %s by %s\n", $info->{crDate}, $info->{crID});

    }

=head1 DESCRIPTION

This module provides a high level interface to EPP. It hides all the boilerplate
of connecting, logging in, building request frames and parsing response frames
behind a simple, Perlish interface.

It is based on the L<Net::EPP::Client> module and uses L<Net::EPP::Frame> to
build request frames.

=head1 CONSTRUCTOR

    my $epp = Net::EPP::Simple->new(%PARAMS);

The constructor accepts the following arguments:

=over

=item * C<host> identifies the EPP server to connect to.

=item * C<port> specifies the port, which defaults to C<700>.

=item * C<user> and C<pass> parameters specify authentication information.

=item * C<newPW> specifies a new password that is set during login.

=item * The C<login_security> parameter can be used to force the use of the
Login Security Extension (see L<RFC 8807|https://www.rfc-editor.org/rfc/rfc8807.html>).
C<Net::EPP::Simple> will automatically use this extension if the server supports
it, but clients may wish to force this behaviour to prevent downgrade attacks.

=item * The C<appname> parameter can be used to specify the value of the
C<E<lt>appE<gt>> element in the Login Security extension (if used). Unless
specified, the name and current version of C<Net::EPP::Simple> will be used.

=item * The C<timeout> parameter controls how long the client waits for a
response from the server before returning an error.

=item * if C<debug> is set, C<Net::EPP::Simple> will output verbose debugging
information on C<STDERR>, including all frames sent to and received from the
server.

=item * C<reconnect> can be used to disable automatic reconnection (it is
enabled by default). Before sending a frame to the server, C<Net::EPP::Simple>
will send a C<E<lt>helloE<gt>> to check that the connection is up, if not, it
will try to reconnect, aborting after the I<n>th time, where I<n> is the value
of C<reconnect> (the default is 3).

=item * C<login> can be used to disable automatic logins. If you set it to C<0>,
you can manually log in using the C<$epp-E<gt>_login()> method.

=item * C<objects> is a reference to an array of the EPP object namespace URIs
that the client requires.

=item * C<stdobj> is a flag saying the client only requires the standard EPP
C<contact-1.0>, C<domain-1.0>, and C<host-1.0> namespaces.

=item * If neither C<objects> nor C<stdobj> is specified then the client will
echo the server's object namespace list.

=item * C<extensions> is a reference to an array of the EPP extension namespace
URIs that the client requires.

=item * C<stdext> is a flag saying the client only requires the standard EPP
C<secDNS-1.1> DNSSEC extension namespace.

=item * If neither C<extensions> nor C<stdext> is specified then the client will
echo the server's extension namespace list.

=item * The C<lang> parameter can be used to specify the language. The default
is "C<en>".

=back

The constructor will establish a connection to the server and retrieve the
greeting (which is available via C<$epp-E<gt>greeting>) and then send a
C<E<lt>loginE<gt>> request.

If the login fails, the constructor will return C<undef> and set
C<$Net::EPP::Simple::Error> and C<$Net::EPP::Simple::Code>.

=head2 CLIENT AND SERVER SSL OPTIONS

RFC 5730 requires that all EPP instances must be protected using "mutual,
strong client-server authentication". In practice, this means that both
client and server must present an SSL certificate, and that they must
both verify the certificate of their peer.

=head3 SERVER CERTIFICATE VERIFICATION

C<Net::EPP::Simple> will verify the certificate presented by a server if
the C<verify>, and either C<ca_file> or C<ca_path> are passed to the
constructor:

    my $epp = Net::EPP::Simple->new(
        host    => 'epp.nic.tld',
        user    => 'my-id',
        pass    => 'my-password',
        verify  => 1,
        ca_file => '/etc/pki/tls/certs/ca-bundle.crt',
        ca_path => '/etc/pki/tls/certs',
    );

C<Net::EPP::Simple> will fail to connect to the server if the
certificate is not valid.

You can disable SSL certificate verification by omitting the C<verify>
argument or setting it to C<undef>. This is strongly discouraged,
particularly in production environments.

You may wish to use L<Mozilla::CA> to provide a value for the C<ca_file>
parameter.

=head3 SSL CIPHER SELECTION

You can restrict the ciphers that you will use to connect to the server
by passing a C<ciphers> parameter to the constructor. This is a colon-
separated list of cipher names and aliases. See L<http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS>
for further details. As an example, the following cipher list is
suggested for clients who wish to ensure high-security connections to
servers:

 HIGH:!ADH:!MEDIUM:!LOW:!SSLv2:!EXP

=head3 CLIENT CERTIFICATES

If you are connecting to an EPP server which requires a client
certificate, you can configure C<Net::EPP::Simple> to use one as
follows:

    my $epp = Net::EPP::Simple->new(
        host        => 'epp.nic.tld',
        user        => 'my-id',
        pass        => 'my-password',
        key         => '/path/to/my.key',
        cert        => '/path/to/my.crt',
        passphrase  => 'foobar123',
    );

C<key> is the filename of the private key, C<cert> is the filename of
the certificate. If the private key is encrypted, the C<passphrase>
parameter will be used to decrypt it.

=cut

sub new {
    my ($package, %params) = @_;
    $params{dom} = 1;

    my $load_config;
    if (exists($params{load_config})) {
        confess('the load_config parameter is deprecated and may be removed in a future version');
        $load_config = $params{load_config};
        $package->_load_config(\%params) if ($load_config);
    }

    $params{port} = (defined($params{port}) && int($params{port}) > 0 ? $params{port} : 700);
    $params{ssl}  = ($params{no_ssl}                                  ? undef         : 1);

    my $self = $package->SUPER::new(%params);

    $self->{user}            = $params{user};
    $self->{pass}            = $params{pass};
    $self->{newPW}           = $params{newPW};
    $self->{debug}           = (defined($params{debug})                                ? int($params{debug})     : undef);
    $self->{timeout}         = (defined($params{timeout}) && int($params{timeout}) > 0 ? $params{timeout}        : 5);
    $self->{reconnect}       = (defined($params{reconnect})                            ? int($params{reconnect}) : 3);
    $self->{'authenticated'} = undef;
    $self->{connect}         = (exists($params{connect}) ? $params{connect} : 1);
    $self->{login}           = (exists($params{login})   ? $params{login}   : 1);
    $self->{key}             = $params{key};
    $self->{cert}            = $params{cert};
    $self->{passphrase}      = $params{passphrase};
    $self->{verify}          = $params{verify};
    $self->{ca_file}         = $params{ca_file};
    $self->{ca_path}         = $params{ca_path};
    $self->{ciphers}         = $params{ciphers};
    $self->{objects}         = $params{objects};
    $self->{stdobj}          = $params{stdobj};
    $self->{extensions}      = $params{extensions};
    $self->{stdext}          = $params{stdext};
    $self->{lang}            = $params{lang} || 'en';
    $self->{login_security}  = $params{login_security};
    $self->{appname}         = $params{appname};

    bless($self, $package);

    if ($self->{connect}) {
        return ($self->_connect($self->{login}) ? $self : undef);

    } else {
        return $self;

    }
}

#
# this functionality is now DEPRECATED
#
sub _load_config {
    my ($package, $params_ref) = @_;

    eval 'use Config::Simple';
    if (!$@) {

        # we have Config::Simple, so let's try to parse the RC file:
        my $rcfile = $ENV{'HOME'} . '/.net-epp-simple-rc';
        if (-e $rcfile) {
            my $config = Config::Simple->new($rcfile);

            # if no host was defined in the constructor, use the default (if specified):
            if (!defined($params_ref->{'host'}) && $config->param('default.default')) {
                $params_ref->{'host'} = $config->param('default.default');
            }

            # if no debug level was defined in the constructor, use the default (if specified):
            if (!defined($params_ref->{'debug'}) && $config->param('default.debug')) {
                $params_ref->{'debug'} = $config->param('default.debug');
            }

            # grep through the file's values for settings for the selected host:
            my %vars = $config->vars;
            foreach my $key (grep { /^$params_ref->{'host'}\./ } keys(%vars)) {
                my $value = $vars{$key};
                $key =~ s/^$params_ref->{'host'}\.//;
                $params_ref->{$key} = $value unless (defined($params_ref->{$key}));
            }
        }
    }
}

sub _connect {
    my ($self, $login) = @_;

    my %params;

    $params{SSL_cipher_list} = $self->{ciphers} if (defined($self->{ssl}) && defined($self->{ciphers}));

    if (defined($self->{key}) && defined($self->{cert}) && defined($self->{ssl})) {
        $self->debug('configuring client certificate parameters');
        $params{SSL_key_file}  = $self->{key};
        $params{SSL_cert_file} = $self->{cert};
        $params{SSL_passwd_cb} = sub { $self->{passphrase} };
    }

    if (defined($self->{ssl}) && defined($self->{verify})) {
        $self->debug('configuring server verification');
        $params{SSL_verify_mode} = 1;
        $params{SSL_ca_file}     = $self->{ca_file};
        $params{SSL_ca_path}     = $self->{ca_path};

    } elsif (defined($self->{ssl})) {
        $params{SSL_verify_mode} = 0;

    }

    $self->debug(sprintf('Attempting to connect to %s:%d', $self->{host}, $self->{port}));
    eval {
        $params{no_greeting} = 1;
        $self->connect(%params);
    };
    if ($@ ne '') {
        chomp($@);
        $@ =~ s/ at .+ line .+$//;
        $self->debug($@);
        $Code  = COMMAND_FAILED;
        $Error = $Message = "Error connecting: " . $@;
        return undef;

    } else {
        $self->debug('Connected OK, retrieving greeting frame');
        $self->{greeting} = $self->get_frame;
        if (ref($self->{greeting}) ne 'Net::EPP::Frame::Response') {
            $Code  = COMMAND_FAILED;
            $Error = $Message = "Error retrieving greeting: " . $@;
            return undef;

        } else {
            $self->debug('greeting frame retrieved OK');

        }
    }

    map { $self->debug('S: ' . $_) } split(/\n/, $self->{greeting}->toString(1));

    if ($login) {
        $self->debug('attempting login');
        return $self->_login;

    } else {
        return 1;

    }
}

sub _login {
    my $self = shift;

    $self->debug(sprintf("Attempting to login as client ID '%s'", $self->{user}));
    my $response = $self->request($self->_prepare_login_frame());

    if (!$response) {
        $Error = $Message = "Error getting response to login request: " . $Error;
        return undef;

    } else {
        $Code    = $self->_get_response_code($response);
        $Message = $self->_get_message($response);

        $self->debug(sprintf('%04d: %s', $Code, $Message));

        if ($Code > 1999) {
            $Error = "Error logging in (response code $Code, message $Message)";
            return undef;

        } else {
            $self->{'authenticated'} = 1;
            return 1;

        }
    }
}

sub _get_uris_from_greeting {
    my $self  = shift;
    my $tag   = shift;
    my $list  = [];
    my $elems = $self->{greeting}->getElementsByTagNameNS(EPP_XMLNS, $tag);
    while (my $elem = $elems->shift) {
        push @$list, $elem->firstChild->data;
    }
    return $list;
}

sub _prepare_login_frame {
    my $self = shift;

    $self->debug('preparing login frame');
    my $login = Net::EPP::Frame::Command::Login->new;

    my @extensions;
    if ($self->{'stdext'}) {
        push(@extensions, Net::EPP::Frame::ObjectSpec->xmlns('secDNS'));

    } elsif ($self->{'extensions'}) {
        @extensions = @{$self->{'extensions'}};

    } else {
        @extensions = @{$self->_get_uris_from_greeting('extURI')};

    }

    $login->clID->appendText($self->{'user'});

    my $loginSecXMLNS = Net::EPP::Frame::ObjectSpec->xmlns('loginSec');

    if ($self->{'login_security'} || $self->server_has_extension($loginSecXMLNS)) {
        push(@extensions, $loginSecXMLNS) unless (any { $loginSecXMLNS eq $_ } @extensions);

        $login->pw->appendText('[LOGIN-SECURITY]');

        my $loginSec = $login->createElementNS($loginSecXMLNS, 'loginSec');

        my $userAgent = $login->createElement('userAgent');
        $loginSec->appendChild($userAgent);

        my $app = $login->createElement('app');
        $app->appendText($self->{'appname'} || sprintf('%s %s', __PACKAGE__, $Net::EPP::VERSION));
        $userAgent->appendChild($app);

        my $tech = $login->createElement('tech');
        $tech->appendText(sprintf('Perl %s', $Config{'version'}));
        $userAgent->appendChild($tech);

        my $os = $login->createElement('os');
        $os->appendText(sprintf('%s %s', ucfirst($Config{'osname'}), $Config{'osvers'}));
        $userAgent->appendChild($os);

        my $pw = $login->createElement('pw');
        $pw->appendText($self->{'pass'});
        $loginSec->appendChild($pw);

        if ($self->{'newPW'}) {
            my $newPW = $login->createElement('newPW');
            $newPW->appendText('[LOGIN-SECURITY]');
            $login->getNode('login')->insertAfter($newPW, $login->pw);

            $newPW = $login->createElement('newPW');
            $newPW->appendText($self->{'newPW'});
            $loginSec->appendChild($newPW);
        }

        $login->extension->appendChild($loginSec);

    } else {
        $login->pw->appendText($self->{pass});

        if ($self->{newPW}) {
            my $newPW = $login->createElement('newPW');
            $newPW->appendText($self->{newPW});
            $login->getNode('login')->insertAfter($newPW, $login->pw);
        }
    }

    $login->version->appendText($self->{greeting}->getElementsByTagNameNS(EPP_XMLNS, 'version')->shift->firstChild->data);
    $login->lang->appendText($self->{lang});

    my $objects = $self->{objects};
    $objects = [map { Net::EPP::Frame::ObjectSpec->xmlns($_) } qw(contact domain host)] if $self->{stdobj};
    $objects = $self->_get_uris_from_greeting('objURI')                                 if not $objects;
    $login->svcs->appendTextChild('objURI', $_) for @$objects;

    if (scalar(@extensions) > 0) {
        my $svcext = $login->createElement('svcExtension');
        $login->svcs->appendChild($svcext);
        $svcext->appendTextChild('extURI', $_) for @extensions;
    }

    return $login;
}

=pod

=head1 AVAILABILITY CHECKS

You can do a simple C<E<lt>checkE<gt>> request for an object like so:

    my $result = $epp->check_domain($domain);

    my $result = $epp->check_host($host);

    my $result = $epp->check_contact($contact);

Each of these methods has the same profile. They will return one of the
following:

=over

=item * C<undef> in the case of an error (check C<$Net::EPP::Simple::Error> and C<$Net::EPP::Simple::Code>).

=item * C<0> if the object is already provisioned.

=item * C<1> if the object is available.

=back

=cut

sub check_domain {
    my ($self, $domain) = @_;
    return $self->_check('domain', $domain);
}

sub check_host {
    my ($self, $host) = @_;
    return $self->_check('host', $host);
}

sub check_contact {
    my ($self, $contact) = @_;
    return $self->_check('contact', $contact);
}

sub _check {
    my ($self, $type, $identifier) = @_;
    my $frame;
    if ($type eq 'domain') {
        $frame = Net::EPP::Frame::Command::Check::Domain->new;
        $frame->addDomain($identifier);

    } elsif ($type eq 'contact') {
        $frame = Net::EPP::Frame::Command::Check::Contact->new;
        $frame->addContact($identifier);

    } elsif ($type eq 'host') {
        $frame = Net::EPP::Frame::Command::Check::Host->new;
        $frame->addHost($identifier);

    } else {
        $Error = "Unknown object type '$type'";
        return undef;
    }

    my $response = $self->_request($frame);

    if (!$response) {
        return undef;

    } else {
        $Code    = $self->_get_response_code($response);
        $Message = $self->_get_message($response);

        if ($Code > 1999) {
            $Error = $self->_get_error_message($response);
            return undef;

        } else {
            my $xmlns = Net::EPP::Frame::ObjectSpec->xmlns($type);
            my $key;
            if ($type eq 'domain' || $type eq 'host') {
                $key = 'name';

            } elsif ($type eq 'contact') {
                $key = 'id';

            }
            return $response->getNode($xmlns, $key)->getAttribute('avail');

        }
    }
}

=pod

=head1 RETRIEVING OBJECT INFORMATION

=head2 DOMAIN OBJECTS

    my $info = $epp->domain_info($domain, $authInfo, $follow);

This method constructs an C<E<lt>infoE<gt>> frame and sends
it to the server, then parses the response into a simple hash ref. If
there is an error, this method will return C<undef>, and you can then
check C<$Net::EPP::Simple::Error> and C<$Net::EPP::Simple::Code>.

If C<$authInfo> is defined, it will be sent to the server as per RFC
5731, Section 3.1.2.

If the C<$follow> parameter is true, then C<Net::EPP::Simple> will also
retrieve the relevant host and contact details for a domain: instead of
returning an object name or ID for the domain's registrant, contact
associations, DNS servers or subordinate hosts, the values will be
replaced with the return value from the appropriate C<host_info()> or
C<contact_info()> command (unless there was an error, in which case the
original object ID will be used instead).

=cut

sub domain_info {
    my ($self, $domain, $authInfo, $follow, $hosts) = @_;
    $hosts = $hosts || 'all';

    my $result = $self->_info('domain', $domain, $authInfo, $hosts);
    return $result if (ref($result) ne 'HASH' || !$follow);

    if (defined($result->{'ns'}) && ref($result->{'ns'}) eq 'ARRAY') {
        for (my $i = 0 ; $i < scalar(@{$result->{'ns'}}) ; $i++) {
            my $info = $self->host_info($result->{'ns'}->[$i]);
            $result->{'ns'}->[$i] = $info if (ref($info) eq 'HASH');
        }
    }

    if (defined($result->{'hosts'}) && ref($result->{'hosts'}) eq 'ARRAY') {
        for (my $i = 0 ; $i < scalar(@{$result->{'hosts'}}) ; $i++) {
            my $info = $self->host_info($result->{'hosts'}->[$i]);
            $result->{'hosts'}->[$i] = $info if (ref($info) eq 'HASH');
        }
    }

    my $info = $self->contact_info($result->{'registrant'});
    $result->{'registrant'} = $info if (ref($info) eq 'HASH');

    foreach my $type (keys(%{$result->{'contacts'}})) {
        my $info = $self->contact_info($result->{'contacts'}->{$type});
        $result->{'contacts'}->{$type} = $info if (ref($info) eq 'HASH');
    }

    return $result;
}

=pod

=head2 HOST OBJECTS

    my $info = $epp->host_info($host);

This method constructs an C<E<lt>infoE<gt>> frame and sends
it to the server, then parses the response into a simple hash ref. If
there is an error, this method will return C<undef>, and you can then
check C<$Net::EPP::Simple::Error> and C<$Net::EPP::Simple::Code>.

=cut

sub host_info {
    my ($self, $host) = @_;
    return $self->_info('host', $host);
}

=pod

=head2 CONTACT OBJECTS

    my $info = $epp->contact_info($contact, $authInfo, $roid);

This method constructs an C<E<lt>infoE<gt>> frame and sends
it to the server, then parses the response into a simple hash ref. If
there is an error, this method will return C<undef>, and you can then
check C<$Net::EPP::Simple::Error> and C<$Net::EPP::Simple::Code>.

If C<$authInfo> is defined, it will be sent to the server as per RFC
RFC 5733, Section 3.1.2.

If the C<$roid> parameter to C<host_info()> is set, then the C<roid>
attribute will be set on the C<E<lt>authInfoE<gt>> element.

=cut

sub contact_info {
    my ($self, $contact, $authInfo, $roid) = @_;
    return $self->_info('contact', $contact, $authInfo, $roid);
}

sub _info {

    # $opt is the "hosts" attribute value for domains or the "roid"
    # attribute for contacts
    my ($self, $type, $identifier, $authInfo, $opt) = @_;
    my $frame;
    if ($type eq 'domain') {
        $frame = Net::EPP::Frame::Command::Info::Domain->new;
        $frame->setDomain($identifier, $opt || 'all');

    } elsif ($type eq 'contact') {
        $frame = Net::EPP::Frame::Command::Info::Contact->new;
        $frame->setContact($identifier);

    } elsif ($type eq 'host') {
        $frame = Net::EPP::Frame::Command::Info::Host->new;
        $frame->setHost($identifier);

    } else {
        $Error = "Unknown object type '$type'";
        return undef;

    }

    if (defined($authInfo) && $authInfo ne '') {
        $self->debug('adding authInfo element to request frame');
        my $el = $frame->createElementNS(Net::EPP::Frame::ObjectSpec->xmlns($type), 'authInfo');
        my $pw = $frame->createElementNS(Net::EPP::Frame::ObjectSpec->xmlns($type), 'pw');
        $pw->appendChild($frame->createTextNode($authInfo));
        $pw->setAttribute('roid', $opt) if ($type eq 'contact' && $opt);
        $el->appendChild($pw);
        $frame->getNode(Net::EPP::Frame::ObjectSpec->xmlns($type), 'info')->appendChild($el);
    }

    my $response = $self->_request($frame);

    if (!$response) {
        return undef;

    } else {
        $Code    = $self->_get_response_code($response);
        $Message = $self->_get_message($response);

        if ($Code > 1999) {
            $Error = $self->_get_error_message($response);
            return undef;

        } else {
            return $self->parse_object_info($type, $response);
        }
    }
}

# An easy-to-subclass method for parsing object info
sub parse_object_info {
    my ($self, $type, $response) = @_;

    my $infData = $response->getNode(Net::EPP::Frame::ObjectSpec->xmlns($type), 'infData');

    if ($type eq 'domain') {

        # secDNS extension only applies to domain objects
        my $secinfo = $response->getNode(Net::EPP::Frame::ObjectSpec->xmlns('secDNS'), 'infData');
        return $self->_domain_infData_to_hash($infData, $secinfo);

    } elsif ($type eq 'contact') {
        return $self->_contact_infData_to_hash($infData);

    } elsif ($type eq 'host') {
        return $self->_host_infData_to_hash($infData);

    } else {
        $Error = "Unknown object type '$type'";
        return undef;

    }
}

sub _get_common_properties_from_infData {
    my ($self, $infData, @extra) = @_;
    my $hash = {};

    my @default = qw(roid clID crID crDate upID upDate trDate);

    foreach my $name (@default, @extra) {
        my $els = $infData->getElementsByLocalName($name);
        $hash->{$name} = $els->shift->textContent if ($els->size > 0);
    }

    my $codes = $infData->getElementsByLocalName('status');
    while (my $code = $codes->shift) {
        push(@{$hash->{status}}, $code->getAttribute('s'));
    }

    return $hash;
}

=pod

=head2 DOMAIN INFORMATION

The hash ref returned by C<domain_info()> will usually look something
like this:

    {
        'contacts' => {
            'admin' => 'contact-id' 'tech' => 'contact-id' 'billing' => 'contact-id'
        },
        'registrant' => 'contact-id',
        'clID'       => 'registrar-id',
        'roid'       => 'tld-12345',
        'status'     => ['ok'],
        'authInfo'   => 'abc-12345',
        'name'       => 'example.tld',
        'trDate'     => '2011-01-18T11:08:03.0Z',
        'ns'         => ['ns0.example.com', 'ns1.example.com',],
        'crDate'     => '2011-02-16T12:06:31.0Z',
        'exDate'     => '2011-02-16T12:06:31.0Z',
        'crID'       => 'registrar-id',
        'upDate'     => '2011-08-29T04:02:12.0Z',
        hosts        => ['ns0.example.tld', 'ns1.example.tld',],
    }

Members of the C<contacts> hash ref may be strings or, if there are
multiple associations of the same type, an anonymous array of strings.
If the server uses the Host Attribute model instead of the Host Object
model, then the C<ns> member will look like this:

    [
        {
            name  => 'ns0.example.com',
            addrs => [
                version => 'v4',
                addr    => '10.0.0.1',
            ],
        },
        {
            name  => 'ns1.example.com',
            addrs => [
                version => 'v4',
                addr    => '10.0.0.2',
            ],
        },
    ]


Note that there may be multiple members in the C<addrs> section and that
the C<version> attribute is optional.

=cut

sub _domain_infData_to_hash {
    my ($self, $infData, $secinfo) = @_;

    my $hash = $self->_get_common_properties_from_infData($infData, 'registrant', 'name', 'exDate');

    my $contacts = $infData->getElementsByLocalName('contact');
    while (my $contact = $contacts->shift) {
        my $type = $contact->getAttribute('type');
        my $id   = $contact->textContent;

        if (ref($hash->{contacts}->{$type}) eq 'STRING') {
            $hash->{contacts}->{$type} = [$hash->{contacts}->{$type}, $id];

        } elsif (ref($hash->{contacts}->{$type}) eq 'ARRAY') {
            push(@{$hash->{contacts}->{$type}}, $id);

        } else {
            $hash->{contacts}->{$type} = $id;

        }

    }

    my $ns = $infData->getElementsByLocalName('ns');
    if ($ns->size == 1) {
        my $el       = $ns->shift;
        my $hostObjs = $el->getElementsByLocalName('hostObj');
        while (my $hostObj = $hostObjs->shift) {
            push(@{$hash->{ns}}, $hostObj->textContent);
        }

        my $hostAttrs = $el->getElementsByLocalName('hostAttr');
        while (my $hostAttr = $hostAttrs->shift) {
            my $host = {};
            $host->{name} = $hostAttr->getElementsByLocalName('hostName')->shift->textContent;
            my $addrs = $hostAttr->getElementsByLocalName('hostAddr');
            while (my $addr = $addrs->shift) {
                push(@{$host->{addrs}}, {version => $addr->getAttribute('ip'), addr => $addr->textContent});
            }
            push(@{$hash->{ns}}, $host);
        }
    }

    my $hosts = $infData->getElementsByLocalName('host');
    while (my $host = $hosts->shift) {
        push(@{$hash->{hosts}}, $host->textContent);
    }

    my $auths = $infData->getElementsByLocalName('authInfo');
    if ($auths->size == 1) {
        my $authInfo = $auths->shift;
        my $pw       = $authInfo->getElementsByLocalName('pw');
        $hash->{authInfo} = $pw->shift->textContent if ($pw->size == 1);
    }

    if (defined $secinfo) {
        if (my $maxSigLife = $secinfo->getElementsByLocalName('maxSigLife')) {
            $hash->{maxSigLife} = $maxSigLife->shift->textContent;
        }
        my $dslist = $secinfo->getElementsByTagName('secDNS:dsData');
        while (my $ds = $dslist->shift) {
            my @ds = map { $ds->getElementsByLocalName($_)->string_value() } qw(keyTag alg digestType digest);
            push @{$hash->{DS}}, "@ds";
        }
        my $keylist = $secinfo->getElementsByLocalName('keyData');
        while (my $key = $keylist->shift) {
            my @key = map { $key->getElementsByLocalName($_)->string_value() } qw(flags protocol alg pubKey);
            push @{$hash->{DNSKEY}}, "@key";
        }
    }

    return $hash;
}

=pod

=head2 HOST INFORMATION

The hash ref returned by C<host_info()> will usually look something like
this:

    {
        'crDate' => '2011-09-17T15:38:56.0Z',
        'clID'   => 'registrar-id',
        'crID'   => 'registrar-id',
        'roid'   => 'tld-12345',
        'status' => ['linked', 'serverDeleteProhibited',],
        'name'   => 'ns0.example.tld',
        'addrs'  => [
            {
                'version' => 'v4',
                'addr'    => '10.0.0.1'
            }
        ]
    }

Note that hosts may have multiple addresses, and that C<version> is
optional.

=cut

sub _host_infData_to_hash {
    my ($self, $infData) = @_;

    my $hash = $self->_get_common_properties_from_infData($infData, 'name');

    my $addrs = $infData->getElementsByLocalName('addr');
    while (my $addr = $addrs->shift) {
        push(@{$hash->{addrs}}, {version => $addr->getAttribute('ip'), addr => $addr->textContent});
    }

    return $hash;
}

=pod

=head2 CONTACT INFORMATION

The hash ref returned by C<contact_info()> will usually look something
like this:

    {
        'id'         => 'contact-id',
        'postalInfo' => {
            'int' => {
                'name' => 'John Doe',
                'org'  => 'Example Inc.',
                'addr' => {
                    'street' => ['123 Example Dr.', 'Suite 100'],
                    'city'   => 'Dulles',
                    'sp'     => 'VA',
                    'pc'     => '20116-6503',
                    'cc'     => 'US',
                }}
        },
        'clID'   => 'registrar-id',
        'roid'   => 'CNIC-HA321983',
        'status' => ['linked', 'serverDeleteProhibited'],
        'voice'  => '+1.7035555555x1234',
        'fax'    => '+1.7035555556',
        'email'  => 'jdoe@example.com',
        'crDate' => '2011-09-23T03:51:29.0Z',
        'upDate' => '1999-11-30T00:00:00.0Z'
    }

There may be up to two members of the C<postalInfo> hash, corresponding
to the C<int> and C<loc> internationalised and localised types.

=cut

sub _contact_infData_to_hash {
    my ($self, $infData) = @_;

    my $hash = $self->_get_common_properties_from_infData($infData, 'email', 'id');

    # remove this as it gets in the way:
    my $els = $infData->getElementsByLocalName('disclose');
    if ($els->size > 0) {
        while (my $el = $els->shift) {
            $el->parentNode->removeChild($el);
        }
    }

    foreach my $name ('voice', 'fax') {
        my $els = $infData->getElementsByLocalName($name);
        if (defined($els) && $els->size == 1) {
            my $el = $els->shift;
            if (defined($el)) {
                $hash->{$name} = $el->textContent;
                $hash->{$name} .= 'x' . $el->getAttribute('x') if (defined($el->getAttribute('x')) && $el->getAttribute('x') ne '');
            }
        }
    }

    my $postalInfo = $infData->getElementsByLocalName('postalInfo');
    while (my $info = $postalInfo->shift) {
        my $ref = {};

        foreach my $name (qw(name org)) {
            my $els = $info->getElementsByLocalName($name);
            $ref->{$name} = $els->shift->textContent if ($els->size == 1);
        }

        my $addrs = $info->getElementsByLocalName('addr');
        if ($addrs->size == 1) {
            my $addr = $addrs->shift;
            foreach my $child ($addr->childNodes) {
                next if (XML::LibXML::XML_ELEMENT_NODE != $child->nodeType);
                if ($child->localName eq 'street') {
                    push(@{$ref->{addr}->{$child->localName}}, $child->textContent);

                } else {
                    $ref->{addr}->{$child->localName} = $child->textContent;

                }
            }
        }

        $hash->{postalInfo}->{$info->getAttribute('type')} = $ref;
    }

    my $auths = $infData->getElementsByLocalName('authInfo');
    if ($auths->size == 1) {
        my $authInfo = $auths->shift;
        my $pw       = $authInfo->getElementsByLocalName('pw');
        $hash->{authInfo} = $pw->shift->textContent if ($pw->size == 1);
    }

    return $hash;
}

=pod

=head1 OBJECT TRANSFERS

The EPP C<E<lt>transferE<gt>> command suppots five different operations:
query, request, cancel, approve, and reject. C<Net::EPP::Simple> makes
these available using the following methods:

    # For domain objects:

    $epp->domain_transfer_query($domain);
    $epp->domain_transfer_cancel($domain);
    $epp->domain_transfer_request($domain, $authInfo, $period);
    $epp->domain_transfer_approve($domain);
    $epp->domain_transfer_reject($domain);

    # For contact objects:

    $epp->contact_transfer_query($contact);
    $epp->contact_transfer_cancel($contact);
    $epp->contact_transfer_request($contact, $authInfo);
    $epp->contact_transfer_approve($contact);
    $epp->contact_transfer_reject($contact);

Most of these methods will just set the value of C<$Net::EPP::Simple::Code>
and return either true or false. However, the C<domain_transfer_request()>,
C<domain_transfer_query()>, C<contact_transfer_request()> and C<contact_transfer_query()>
methods will return a hash ref that looks like this:

    {
        'name'     => 'example.tld',
        'reID'     => 'losing-registrar',
        'acDate'   => '2011-12-04T12:24:53.0Z',
        'acID'     => 'gaining-registrar',
        'reDate'   => '2011-11-29T12:24:53.0Z',
        'trStatus' => 'pending'
    }

=cut

sub _transfer_request {
    my ($self, $op, $type, $identifier, $authInfo, $period) = @_;

    my $class = sprintf('Net::EPP::Frame::Command::Transfer::%s', ucfirst(lc($type)));

    my $frame;
    eval("\$frame = $class->new");
    if ($@ || ref($frame) ne $class) {
        $Error = "Error building request frame: $@";
        $Code  = COMMAND_FAILED;
        return undef;

    } else {
        $frame->setOp($op);
        if ($type eq 'domain') {
            $frame->setDomain($identifier);
            $frame->setPeriod(int($period)) if ($op eq 'request');

        } elsif ($type eq 'contact') {
            $frame->setContact($identifier);

        }

        if ($op eq 'request' || $op eq 'query') {
            $frame->setAuthInfo($authInfo) if ($authInfo ne '');
        }

    }

    my $response = $self->_request($frame);

    if (!$response) {
        return undef;

    } else {
        $Code    = $self->_get_response_code($response);
        $Message = $self->_get_message($response);

        if ($Code > 1999) {
            $Error = $response->msg;
            return undef;

        } elsif ($op eq 'query' || $op eq 'request') {
            my $trnData = $response->getElementsByLocalName('trnData')->shift;
            my $hash    = {};
            foreach my $child ($trnData->childNodes) {
                $hash->{$child->localName} = $child->textContent;
            }

            return $hash;

        } else {
            return 1;

        }
    }
}

sub domain_transfer_query {
    return $_[0]->_transfer_request('query', 'domain', $_[1]);
}

sub domain_transfer_cancel {
    return $_[0]->_transfer_request('cancel', 'domain', $_[1]);
}

sub domain_transfer_request {
    return $_[0]->_transfer_request('request', 'domain', $_[1], $_[2], $_[3]);
}

sub domain_transfer_approve {
    return $_[0]->_transfer_request('approve', 'domain', $_[1]);
}

sub domain_transfer_reject {
    return $_[0]->_transfer_request('reject', 'domain', $_[1]);
}

sub contact_transfer_query {
    return $_[0]->_transfer_request('query', 'contact', $_[1]);
}

sub contact_transfer_cancel {
    return $_[0]->_transfer_request('cancel', 'contact', $_[1]);
}

sub contact_transfer_request {
    return $_[0]->_transfer_request('request', 'contact', $_[1], $_[2]);
}

sub contact_transfer_approve {
    return $_[0]->_transfer_request('approve', 'contact', $_[1]);
}

sub contact_transfer_reject {
    return $_[0]->_transfer_request('reject', 'contact', $_[1]);
}

=pod

=head1 CREATING OBJECTS

The following methods can be used to create a new object at the server:

    $epp->create_domain($domain);
    $epp->create_host($host);
    $epp->create_contact($contact);

The argument for these methods is a hash ref of the same format as that
returned by the info methods above. As a result, cloning an existing
object is as simple as the following:

    my $info = $epp->contact_info($contact);

    # set a new contact ID to avoid clashing with the existing object
    $info->{id} = $new_contact;

    # randomize authInfo:
    $info->{authInfo} = $random_string;

    $epp->create_contact($info);

C<Net::EPP::Simple> will ignore object properties that it does not recognise,
and those properties (such as server-managed status codes) that clients are
not permitted to set.

=head2 CREATING NEW DOMAINS

When creating a new domain object, you may also specify a C<period> key, like
so:

    $epp->create_domain({
        'name'       => 'example.tld',
        'period'     => 2,
        'registrant' => 'contact-id',
        'contacts'   => {
            'tech'    => 'contact-id',
            'admin'   => 'contact-id',
            'billing' => 'contact-id',
        },
        'status' => ['clientTransferProhibited',],
        'ns'     => {'ns0.example.com', 'ns1.example.com',},

        # this will be ignored if the server does not support the TTL extension
        'ttl'    => {'NS' => 3600, 'DS' => 60},
    });

The C<period> key is assumed to be in years rather than months.
C<Net::EPP::Simple> assumes the registry uses the host object model rather than
the host attribute model.

=cut

sub create_domain {
    my ($self, $domain) = @_;

    return $self->_get_response_result($self->_request($self->_prepare_create_domain_frame($domain)));
}

sub _prepare_create_domain_frame {
    my ($self, $domain) = @_;

    my $frame = Net::EPP::Frame::Command::Create::Domain->new;

    $frame->setDomain($domain->{'name'});
    $frame->setPeriod($domain->{'period'})         if (defined($domain->{period}) && $domain->{period} > 0);
    $frame->setNS(@{$domain->{'ns'}})              if $domain->{'ns'} and @{$domain->{'ns'}};
    $frame->setRegistrant($domain->{'registrant'}) if (defined($domain->{registrant}) && $domain->{registrant} ne '');
    $frame->setContacts($domain->{'contacts'});
    $frame->setAuthInfo($domain->{authInfo}) if (defined($domain->{authInfo}) && $domain->{authInfo} ne '');

    if ($domain->{'ttl'} && $self->server_has_extension(Net::EPP::Frame::ObjectSpec->xmlns('ttl'))) {
        $frame->setTTLs($domain->{'ttl'});
    }

    return $frame;
}

=head2 CREATING HOSTS

    $epp->create_host({
        name  => 'ns1.example.tld',
        addrs => [
            {ip => '192.0.2.1', version => 'v4'},
            {ip => '192.0.2.2',  version => 'v4'},
        ],

        # this will be ignored if the server does not support the TTL extension
        'ttl' => {
            'A'    => 3600,
            'AAAA' => 900,
        }
    });

=cut

sub create_host {
    my ($self, $host) = @_;

    return $self->_get_response_result($self->_request($self->_prepare_create_host_frame($host)));
}

sub _prepare_create_host_frame {
    my ($self, $host) = @_;

    my $frame = Net::EPP::Frame::Command::Create::Host->new;
    $frame->setHost($host->{name});
    $frame->setAddr(@{$host->{addrs}});

    if ($host->{'ttl'} && $self->server_has_extension(Net::EPP::Frame::ObjectSpec->xmlns('ttl'))) {
        $frame->setTTLs($host->{'ttl'});
    }

    return $frame;
}

sub create_contact {
    my ($self, $contact) = @_;

    return $self->_get_response_result($self->_request($self->_prepare_create_contact_frame($contact)));
}

sub _prepare_create_contact_frame {
    my ($self, $contact) = @_;

    my $frame = Net::EPP::Frame::Command::Create::Contact->new;

    $frame->setContact($contact->{id});

    if (ref($contact->{postalInfo}) eq 'HASH') {
        foreach my $type (keys(%{$contact->{postalInfo}})) {
            $frame->addPostalInfo($type, $contact->{postalInfo}->{$type}->{name}, $contact->{postalInfo}->{$type}->{org}, $contact->{postalInfo}->{$type}->{addr});
        }
    }

    $frame->setVoice($contact->{voice}) if (defined($contact->{voice}) && $contact->{voice} ne '');
    $frame->setFax($contact->{fax})     if (defined($contact->{fax})   && $contact->{fax} ne '');
    $frame->setEmail($contact->{email});
    $frame->setAuthInfo($contact->{authInfo}) if (defined($contact->{authInfo}) && $contact->{authInfo} ne '');

    if (ref($contact->{status}) eq 'ARRAY') {
        foreach my $status (grep { /^client/ } @{$contact->{status}}) {
            $frame->appendStatus($status);
        }
    }
    return $frame;
}

# Process response code and return result
sub _get_response_result {
    my ($self, $response) = @_;

    return undef if !$response;

    # If there was a response...
    $Code    = $self->_get_response_code($response);
    $Message = $self->_get_message($response);
    if ($Code > 1999) {
        $Error = $response->msg;
        return undef;
    }
    return 1;
}

=head1 UPDATING OBJECTS

The following methods can be used to update an object at the server:

    $epp->update_domain($domain);
    $epp->update_host($host);
    $epp->update_contact($contact);

Each of these methods has the same profile. They will return one of the following:

=over

=item * undef in the case of an error (check C<$Net::EPP::Simple::Error> and C<$Net::EPP::Simple::Code>).

=item * 1 if the update request was accepted.

=back

You may wish to check the value of $Net::EPP::Simple::Code to determine whether the response code was 1000 (OK) or 1001 (action pending).

=cut

=head2 UPDATING DOMAINS

Use C<update_domain($info)> method to update a domain name.

The C<$info> argument should look like this:

    {
        name => $domain,
        chg  => {
            registrant => $new_registrant_id,
            authInfo   => $new_domain_password,

            # this will be ignored if the server does not support the TTL extension
            ttl        => {'NS' => 3600, 'DS' => 60},
        },
        add => {

            ns       => [qw/ns1.example.com ns2.example.com/],
            contacts => {
                tech    => 'contact-id',
                billing => 'contact-id',
                admin   => 'contact-id',
            },

            # Status info, simple form:
            status => [qw/ clientUpdateProhibited clientHold /],

            # Status info may be in more detailed form:
            # status => {
            #    clientUpdateProbhibited  => 'Avoid accidental change',
            #    clientHold               => 'This domain is not delegated',
            # },
        },
        rem => {
            ns       => [...],
            contacts => {
                tech    => 'old_tech_id',
                billing => 'old_billing_id',
                admin   => 'old_admin_id',
            },
            status => [qw/ clientTransferProhibited ... /],
        },
    }

All fields except C<name> are optional.

=cut

sub update_domain {
    my ($self, $domain) = @_;
    return $self->_update('domain', $domain);
}

=head2 UPDATING CONTACTS

Use C<update_contact(info)> method to update a contact object.

The C<$info> argument should look like this:

    {
        id  => $contact_id,
        add => {
            status => [qw/ clientDeleteProhibited /],

            # OR
            # status => {
            #    clientDeleteProhibited  => 'Avoid accidental removal',
            # },
        },
        rem => {
            status => [qw/ clientUpdateProhibited /],
        },
        chg => {
            postalInfo => {
                int => {
                    name => 'John Doe',
                    org  => 'Example Inc.',
                    addr => {
                        street => ['123 Example Dr.', 'Suite 100'],
                        city   => 'Dulles',
                        sp     => 'VA',
                        pc     => '20116-6503',
                        cc     => 'US',
                    },
                },
            },
            voice    => '+1.7035555555x1234',
            fax      => '+1.7035555556',
            email    => 'jdoe@example.com',
            authInfo => 'new-contact-password',
        },
    }

All fields except C<id> are optional.

=cut

sub update_contact {
    my ($self, $contact) = @_;
    return $self->_update('contact', $contact);
}

=head2 UPDATING HOSTS

Use C<update_host($info)> method to update a host object.

The C<$info> argument should look like this:

    {
        name => 'ns1.example.com',
        add  => {
            status => [qw/ clientDeleteProhibited /],

            # OR
            # status => {
            #    clientDeleteProhibited  => 'Avoid accidental removal',
            # },

            addrs => [{ip => '123.45.67.89', version => 'v4'}, {ip => '98.76.54.32', version => 'v4'},],
        },
        rem => {
            status => [qw/ clientUpdateProhibited /],
            addrs  => [{ip => '1.2.3.4', version => 'v4'}, {ip => '5.6.7.8', version => 'v4'},],
        },
        chg => {
            name => 'ns2.example.com',
            # this will be ignored if the server does not support the TTL extension
            ttl    => {NS => 3600, DS => 60},
        },
    }

All fields except C<name> are optional.

=cut

sub update_host {
    my ($self, $host) = @_;
    return $self->_update('host', $host);
}

# Update domain/contact/host information
sub _update {
    my ($self, $type, $info) = @_;

    my %frame_generator = (
        'domain'  => \&_generate_update_domain_frame,
        'contact' => \&_generate_update_contact_frame,
        'host'    => \&_generate_update_host_frame,
    );

    if (!exists $frame_generator{$type}) {
        $Error = "Unknown object type: '$type'";
        return undef;
    }

    my $generator = $frame_generator{$type};
    my $frame     = $self->$generator($info);
    return $self->_get_response_result($self->request($frame));
}

sub _generate_update_domain_frame {
    my ($self, $info) = @_;

    my $frame = Net::EPP::Frame::Command::Update::Domain->new;
    $frame->setDomain($info->{name});

    # 'add' element
    if (exists $info->{add} && ref $info->{add} eq 'HASH') {

        my $add = $info->{add};

        # Add DNS
        if (exists $add->{ns} && ref $add->{ns} eq 'ARRAY') {
            $frame->addNS(@{$add->{ns}});
        }

        # Add contacts
        if (exists $add->{contacts} && ref $add->{contacts} eq 'HASH') {

            my $contacts = $add->{contacts};
            foreach my $type (keys %{$contacts}) {
                $frame->addContact($type, $contacts->{$type});
            }
        }

        # Add status info
        if (exists $add->{status} && ref $add->{status}) {
            if (ref $add->{status} eq 'HASH') {
                while (my ($type, $info) = each %{$add->{status}}) {
                    $frame->addStatus($type, $info);
                }
            } elsif (ref $add->{status} eq 'ARRAY') {
                $frame->addStatus($_) for @{$add->{status}};
            }
        }
    }

    # 'rem' element
    if (exists $info->{rem} && ref $info->{rem} eq 'HASH') {

        my $rem = $info->{rem};

        # DNS
        if (exists $rem->{ns} && ref $rem->{ns} eq 'ARRAY') {
            $frame->remNS(@{$rem->{ns}});
        }

        # Contacts
        if (exists $rem->{contacts} && ref $rem->{contacts} eq 'HASH') {
            my $contacts = $rem->{contacts};

            foreach my $type (keys %{$contacts}) {
                $frame->remContact($type, $contacts->{$type});
            }
        }

        # Status info
        if (exists $rem->{status} && ref $rem->{status} eq 'ARRAY') {
            $frame->remStatus($_) for @{$rem->{status}};
        }
    }

    # 'chg' element
    if (exists $info->{chg} && ref $info->{chg} eq 'HASH') {

        my $chg = $info->{chg};

        if (defined $chg->{registrant}) {
            $frame->chgRegistrant($chg->{registrant});
        }

        if (defined $chg->{authInfo}) {
            $frame->chgAuthInfo($chg->{authInfo});
        }

        if (defined $chg->{ttl} && $self->server_has_extension(Net::EPP::Frame::ObjectSpec->xmlns('ttl'))) {
            $frame->chgTTLs($chg->{ttl});
        }

    }

    return $frame;
}

sub _generate_update_contact_frame {
    my ($self, $info) = @_;

    my $frame = Net::EPP::Frame::Command::Update::Contact->new;
    $frame->setContact($info->{id});

    # Add
    if (exists $info->{add} && ref $info->{add} eq 'HASH') {
        my $add = $info->{add};

        if (exists $add->{status} && ref $add->{status}) {
            if (ref $add->{status} eq 'HASH') {
                while (my ($type, $info) = each %{$add->{status}}) {
                    $frame->addStatus($type, $info);
                }
            } elsif (ref $add->{status} eq 'ARRAY') {
                $frame->addStatus($_) for @{$add->{status}};
            }
        }
    }

    # Remove
    if (exists $info->{rem} && ref $info->{rem} eq 'HASH') {

        my $rem = $info->{rem};

        if (exists $rem->{status} && ref $rem->{status} eq 'ARRAY') {
            $frame->remStatus($_) for @{$rem->{status}};
        }
    }

    # Change
    if (exists $info->{chg} && ref $info->{chg} eq 'HASH') {

        my $chg = $info->{chg};

        # Change postal info
        if (ref $chg->{postalInfo} eq 'HASH') {
            foreach my $type (keys %{$chg->{postalInfo}}) {
                $frame->chgPostalInfo($type, $chg->{postalInfo}->{$type}->{name}, $chg->{postalInfo}->{$type}->{org}, $chg->{postalInfo}->{$type}->{addr});
            }
        }

        # Change voice / fax / email
        for my $contact_type (qw/ voice fax email /) {
            if (defined $chg->{$contact_type}) {
                my $el = $frame->createElement("contact:$contact_type");
                $el->appendText($chg->{$contact_type});
                $frame->chg->appendChild($el);
            }
        }

        # Change auth info
        if ($chg->{authInfo}) {
            $frame->chgAuthInfo($chg->{authInfo});
        }

        # 'disclose' option is still unimplemented
    }

    return $frame;
}

sub _generate_update_host_frame {
    my ($self, $info) = @_;

    my $frame = Net::EPP::Frame::Command::Update::Host->new;
    $frame->setHost($info->{name});

    if (exists $info->{add} && ref $info->{add} eq 'HASH') {
        my $add = $info->{add};

        # Process addresses
        if (exists $add->{addrs} && ref $add->{addrs} eq 'ARRAY') {
            $frame->addAddr(@{$add->{addrs}});
        }

        # Process statuses
        if (exists $add->{status} && ref $add->{status}) {
            if (ref $add->{status} eq 'HASH') {
                while (my ($type, $info) = each %{$add->{status}}) {
                    $frame->addStatus($type, $info);
                }
            } elsif (ref $add->{status} eq 'ARRAY') {
                $frame->addStatus($_) for @{$add->{status}};
            }
        }
    }

    if (exists $info->{rem} && ref $info->{rem} eq 'HASH') {
        my $rem = $info->{rem};

        # Process addresses
        if (exists $rem->{addrs} && ref $rem->{addrs} eq 'ARRAY') {
            $frame->remAddr(@{$rem->{addrs}});
        }

        # Process statuses
        if (exists $rem->{status} && ref $rem->{status}) {
            if (ref $rem->{status} eq 'HASH') {
                while (my ($type, $info) = each %{$rem->{status}}) {
                    $frame->remStatus($type, $info);
                }
            } elsif (ref $rem->{status} eq 'ARRAY') {
                $frame->remStatus($_) for @{$rem->{status}};
            }
        }
    }

    if (exists $info->{chg} && ref $info->{chg} eq 'HASH') {
        if ($info->{chg}->{name}) {
            $frame->chgName($info->{chg}->{name});
        }
    }

    if (exists $info->{chg} && ref $info->{chg} eq 'HASH') {
        if ($info->{chg}->{name}) {
            $frame->chgName($info->{chg}->{name});
        }

        if (defined $info->{chg}->{ttl} && $self->server_has_extension(Net::EPP::Frame::ObjectSpec->xmlns('ttl'))) {
            $frame->chgTTLs($info->{chg}->{ttl});
        }
    }

    return $frame;
}

=pod

=head1 DELETING OBJECTS

The following methods can be used to delete an object at the server:

    $epp->delete_domain($domain);
    $epp->delete_host($host);
    $epp->delete_contact($contact);

Each of these methods has the same profile. They will return one of the following:

=over

=item * undef in the case of an error (check C<$Net::EPP::Simple::Error> and C<$Net::EPP::Simple::Code>).

=item * 1 if the deletion request was accepted.

=back

You may wish to check the value of $Net::EPP::Simple::Code to determine whether the response code was 1000 (OK) or 1001 (action pending).

=cut

sub delete_domain {
    my ($self, $domain) = @_;
    return $self->_delete('domain', $domain);
}

sub delete_host {
    my ($self, $host) = @_;
    return $self->_delete('host', $host);
}

sub delete_contact {
    my ($self, $contact) = @_;
    return $self->_delete('contact', $contact);
}

sub _delete {
    my ($self, $type, $identifier) = @_;
    my $frame;
    if ($type eq 'domain') {
        $frame = Net::EPP::Frame::Command::Delete::Domain->new;
        $frame->setDomain($identifier);

    } elsif ($type eq 'contact') {
        $frame = Net::EPP::Frame::Command::Delete::Contact->new;
        $frame->setContact($identifier);

    } elsif ($type eq 'host') {
        $frame = Net::EPP::Frame::Command::Delete::Host->new;
        $frame->setHost($identifier);

    } else {
        $Error = "Unknown object type '$type'";
        return undef;

    }

    my $response = $self->_request($frame);

    if (!$response) {
        return undef;

    } else {
        $Code    = $self->_get_response_code($response);
        $Message = $self->_get_message($response);

        if ($Code > 1999) {
            $Error = $self->_get_error_message($response);
            return undef;

        } else {
            return 1;

        }
    }
}

=head1 DOMAIN RENEWAL

You can extend the validity period of the domain object by issuing a
renew_domain() command.

 my $result = $epp->renew_domain({
     name         => 'example.com',
     cur_exp_date => '2011-02-05',  # current expiration date
     period       => 2,             # prolongation period in years
 });

Return value is C<1> on success and C<undef> on error.
In the case of error C<$Net::EPP::Simple::Error> contains the appropriate
error message.

=cut

sub renew_domain {
    my ($self, $info) = @_;

    return $self->_get_response_result($self->request($self->_generate_renew_domain_frame($info)));
}

sub _generate_renew_domain_frame {
    my ($self, $info) = @_;

    my $frame = Net::EPP::Frame::Command::Renew::Domain->new;
    $frame->setDomain($info->{name});
    $frame->setCurExpDate($info->{cur_exp_date});
    $frame->setPeriod($info->{period}) if $info->{period};

    return $frame;
}

=pod

=head1 MISCELLANEOUS METHODS

=cut

sub error { $Error }

sub code { $Code }

sub message { $Message }

=pod

    my $greeting = $epp->greeting;

Returns the a L<Net::EPP::Frame::Greeting> object representing the greeting returned by the server.

=cut

sub greeting {
    my $self = shift;
    return $self->{'greeting'};
}

=pod

    $epp->ping;

Checks that the connection is up by sending a C<E<lt>helloE<gt>> to the server. Returns false if no
response is received.

=cut

sub ping {
    my $self     = shift;
    my $hello    = Net::EPP::Frame::Hello->new;
    my $response = $self->request($hello);

    if (UNIVERSAL::isa($response, 'XML::LibXML::Document')) {
        $Code    = 1000;
        $Message = 'Command completed successfully.';
        return 1;

    } else {
        $Code    = 2400;
        $Message = 'Error getting greeting from server.';
        return undef;
    }
}

sub _request {
    my ($self, $frame) = @_;

    if ($self->{reconnect} > 0) {
        $self->debug("reconnect is $self->{reconnect}, pinging");
        if (!$self->ping) {
            $self->debug('connection seems dead, trying to reconnect');
            for (1 .. $self->{reconnect}) {
                $self->debug("attempt #$_");
                if ($self->_connect) {
                    $self->debug("attempt #$_ succeeded");
                    return $self->request($frame);

                } else {
                    $self->debug("attempt #$_ failed, sleeping");
                    sleep($self->{timeout});

                }
            }
            $self->debug('unable to reconnect!');
            return undef;

        } else {
            $self->debug("Connection is up, sending frame");
            return $self->request($frame);

        }

    } else {
        return $self->request($frame);

    }
}

=pod

=head1 OVERRIDDEN METHODS FROM L<Net::EPP::Client>

C<Net::EPP::Simple> overrides some methods inherited from
L<Net::EPP::Client>. These are described below:

=head2 C<request()>

C<Net::EPP::Simple> overrides this method so it can automatically populate
the C<E<lt>clTRIDE<gt>> element with a unique string. It then passes the
frame back up to L<Net::EPP::Client>.

=cut

sub request {
    my ($self, $frame) = @_;

    # Make sure we start with blank variables
    $Code    = undef;
    $Error   = '';
    $Message = '';

    if (!$self->connected) {
        $Code  = COMMAND_FAILED;
        $Error = $Message = 'Not connected';
        $self->debug('cannot send frame if not connected');
        return undef;

    } elsif (!$frame) {
        $Code  = COMMAND_FAILED;
        $Error = $Message = 'Invalid frame';
        $self->debug($Message);
        return undef;

    } else {
        $frame->clTRID->appendText(sha1_hex(ref($self) . time() . $$)) if (UNIVERSAL::isa($frame, 'Net::EPP::Frame::Command'));

        my $type = ref($frame);
        if ($frame =~ /^\//) {
            $type = 'file';

        } else {
            $type = 'string';

        }
        $self->debug(sprintf('sending a %s to the server', $type));
        if (UNIVERSAL::isa($frame, 'XML::LibXML::Document')) {
            map { $self->debug('C: ' . $_) } split(/\n/, $frame->toString(2));

        } else {
            map { $self->debug('C: ' . $_) } split(/\n/, $frame);

        }

        my $response = $self->SUPER::request($frame);

        map { $self->debug('S: ' . $_) } split(/\n/, $response->toString(2)) if (UNIVERSAL::isa($response, 'XML::LibXML::Document'));

        return $response;
    }
}

=pod

=head2 C<get_frame()>

C<Net::EPP::Simple> overrides this method so it can catch timeouts and
network errors. If such an error occurs it will return C<undef>.

=cut

sub get_frame {
    my $self = shift;
    if (!$self->connected) {
        $self->debug('cannot get frame if not connected');
        $Code  = COMMAND_FAILED;
        $Error = $Message = 'Not connected';
        return undef;

    } else {
        my $frame;
        $self->debug(sprintf('reading frame, waiting %d seconds before timeout', $self->{timeout}));
        eval {
            local $SIG{ALRM} = sub { die 'timeout' };
            $self->debug('setting timeout alarm for receiving frame');
            alarm($self->{timeout});
            $frame = $self->SUPER::get_frame();
            $self->debug('unsetting timeout alarm after successful receive');
            alarm(0);
        };
        if ($@ ne '') {
            chomp($@);
            $@ =~ s/ at .+ line .+$//;
            $self->debug("unsetting timeout alarm after alarm was triggered ($@)");
            alarm(0);
            $Code = COMMAND_FAILED;
            if ($@ =~ /^timeout/) {
                $Error = $Message = "get_frame() timed out after $self->{timeout} seconds";

            } else {
                $Error = $Message = "get_frame() received an error: $@";

            }
            return undef;

        } else {
            return bless($frame, 'Net::EPP::Frame::Response');

        }
    }
}

sub send_frame {
    my ($self, $frame, $wfcheck) = @_;
    if (!$self->connected) {
        $self->debug('cannot send frame if not connected');
        $Code    = 2400;
        $Message = 'Not connected';
        return undef;

    } else {
        return $self->SUPER::send_frame($frame, $wfcheck);

    }
}

# Get details error description including code, message and reason
sub _get_error_message {
    my ($self, $doc) = @_;

    my $code  = $self->_get_response_code($doc);
    my $error = "Error $code";

    my $message = $self->_get_message($doc);
    if ($message) {
        $error .= ": $message";
    }

    my $reason = $self->_get_reason($doc);
    if ($reason) {
        $error .= " ($reason)";
    }

    return $error;
}

sub _get_response_code {
    my ($self, $doc) = @_;
    if ($doc->isa('XML::DOM::Document') || $doc->isa('Net::EPP::Frame::Response')) {
        my $els = $doc->getElementsByTagNameNS(EPP_XMLNS, 'result');
        if (defined($els)) {
            my $el = $els->shift;
            return $el->getAttribute('code') if (defined($el));
        }
    }
    return 2400;
}

sub _get_message {
    my ($self, $doc) = @_;
    if ($doc->isa('XML::DOM::Document') || $doc->isa('Net::EPP::Frame::Response')) {
        my $msgs = $doc->getElementsByTagNameNS(EPP_XMLNS, 'msg');
        if (defined($msgs)) {
            my $msg = $msgs->shift;
            return $msg->textContent if (defined($msg));
        }
    }
    return '';
}

sub _get_reason {
    my ($self, $doc) = @_;
    if ($doc->isa('XML::DOM::Document') || $doc->isa('Net::EPP::Frame::Response')) {
        my $reasons = $doc->getElementsByTagNameNS(EPP_XMLNS, 'reason');
        if (defined($reasons)) {
            my $reason = $reasons->shift;
            if (defined($reason)) {
                return $reason->textContent;
            }
        }
    }
    return '';
}

sub logout {
    my $self = shift;
    if ($self->authenticated) {
        $self->debug('logging out');
        my $response = $self->request(Net::EPP::Frame::Command::Logout->new);
        undef($self->{'authenticated'});
        if (!$response) {
            $Code    = COMMAND_FAILED;
            $Message = $Error = 'unknown error';
            return undef;

        } else {
            $Code    = $self->_get_response_code($response);
            $Message = $self->_get_message($response);

        }
    }
    $self->debug('disconnecting from server');
    $self->disconnect;
    return 1;
}

sub DESTROY {
    my $self = shift;
    $self->debug('DESTROY() method called');
    $self->logout if ($self->connected);
}

sub debug {
    my ($self, $msg) = @_;
    my $log = sprintf("%s (%d): %s", scalar(localtime()), $$, $msg);
    push(@Log, $log);
    print STDERR $log . "\n" if (defined($self->{debug}) && $self->{debug} == 1);
}

=pod

    $bool = $epp->server_has_object($xmlns);
    $bool = $epp->server_has_extension($xmlns);

These methods both return a true value if the object/extension identified by
C<$xmlns> was present in the server's greeting.

=cut

sub server_has_object {
    my ($self, $xmlns) = @_;

    if (!$self->greeting) {
        carp('not connected');
        return undef;
    }

    foreach my $objURI ($self->greeting->getElementsByTagName('objURI')) {
        return 1 if ($objURI->textContent eq $xmlns);
    }

    return undef;
}

sub server_has_extension {
    my ($self, $xmlns) = @_;

    if (!$self->greeting) {
        carp('not connected');
        return undef;
    }

    foreach my $extURI ($self->greeting->getElementsByTagName('extURI')) {
        return 1 if ($extURI->textContent eq $xmlns);
    }

    return undef;
}

=pod

    $authenticated = $epp->authenticated;

Returns a boolean if C<Net::EPP::Simple> has successfully authenticated with the server.

=cut

sub authenticated {
    my $self = shift;
    return defined($self->{'authenticated'});
}

1;

=pod

=head1 PACKAGE VARIABLES

=head2 $Net::EPP::Simple::Error

This variable contains an english text message explaining the last error
to occur. This is may be due to invalid parameters being passed to a
method, a network error, or an error response being returned by the
server.

=head2 $Net::EPP::Simple::Message

This variable contains the contains the text content of the
C<E<lt>msgE<gt>> element in the response frame for the last transaction.

=head2 $Net::EPP::Simple::Code

This variable contains the integer result code returned by the server
for the last transaction. A successful transaction will always return an
error code of 1999 or lower, for an unsuccessful transaction it will be
2011 or more. If there is an internal client error (due to invalid
parameters being passed to a method, or a network error) then this will
be set to 2400 (C<COMMAND_FAILED>). See L<Net::EPP::ResponseCodes> for
more information about thes codes.

=head1 COPYRIGHT

This module is (c) 2008 - 2023 CentralNic Ltd and 2024 Gavin Brown. This module
is free software; you can redistribute it and/or modify it under the same terms
as Perl itself.

=cut