This file is indexed.

/usr/share/perl5/Net/DRI/Shell.pm is in libnet-dri-perl 0.96-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
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
## Domain Registry Interface, Shell interface
##
## Copyright (c) 2008-2010 Patrick Mevzek <netdri@dotandco.com>. All rights reserved.
##
## This file is part of Net::DRI
##
## Net::DRI is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## See the LICENSE file that comes with this distribution for more details.
#
# 
#
####################################################################################################

package Net::DRI::Shell;

use strict;
use warnings;

use Net::DRI;
use Net::DRI::Util;
use Net::DRI::Protocol::ResultStatus;
use Term::ReadLine; ## see also Term::Shell
use Time::HiRes ();
use IO::Handle ();

our $VERSION=do { my @r=(q$Revision: 1.10 $=~/\d+/g); sprintf("%d".".%02d" x $#r, @r); };

exit __PACKAGE__->run(@ARGV) if (!caller() || caller() eq 'PAR'); ## This is a modulino :-)

=pod

=head1 NAME

Net::DRI::Shell - Command Line Shell for Net::DRI, with batch features and autocompletion support

=head1 SYNOPSYS

 perl -I../../ ./Shell.pm
 or
 perl -MNet::DRI::Shell -e 'Net::DRI::Shell->run()'
 or in your programs
 use Net::DRI::Shell;
 Net::DRI::Shell->run();

 Welcome to Net::DRI shell, version 1.07
 Net::DRI object created with a cache TTL of 10 seconds and logging into files in current directory

 NetDRI> add_registry registry=EURid clID=YOURLOGIN
 NetDRI(EURid)> add_current_profile name=profile1 type=epp defer=0 client_login=YOURLOGIN client_password=YOURPASSWORD
 Profile profile1 added successfully (1000/COMMAND_SUCCESSFUL) SUCCESS
 NetDRI(EURid,profile1)> domain_info example.eu
 Command completed successfully (1000/1000) SUCCESS
 NetDRI(EURid,profile1)> get_info_all

 ... all data related to the domain name queried ...

 NetDRI(EURid,profile1)> domain_check whatever.eu
 Command completed successfully (1000/1000) SUCCESS
 NetDRI(EURid,profile1)> get_info_all

 ... all data related to the domain name queried ...

 NetDRI(EURid,profile1)> show profiles
 EURid: profile1
 NetDRI(EURid,profile1)> quit


=head1 DESCRIPTION

This is a shell to be able to use Net::DRI without writing any code.

Most of the time commands are the name of methods to use on the Net::DRI object,
with some extra ones and some variations in API to make passing parameters simpler.

=head1 AVAILABLE COMMANDS

After having started this shell, the available commands are the following.

=head2 SESSION COMMANDS

=head3 add_registry registry=REGISTRYNAME clID=YOURLOGIN

Replace REGISTRYNAME with the Net::DRI::DRD module you want to use, and YOURLOGIN
with your client login for this registry.

=head3 add_current_profile name=profile1 type=epp defer=0 client_login=YOURLOGIN client_password=YOURPASSWORD

This will really connect to the registry, replace YOURLOGIN by your client login at registry,
and YOURPASSWORD by the associated password. You may have to add parameters remote_host= and remote_port=
to connect to other endpoints than the hardcoded default which is most of the time the registry OT&E server,
and not the production one !

=head3 add registry=REGISTRYNAME clID=YOURLOGIN name=profile1 type=epp defer=0 client_login=YOURLOGIN client_password=YOURPASSWORD

This is a shortcut, doing the equivalent of add_registry, and then add_current_profile.

=head3 get_info_all

After each call to the registry, like domain_info or domain_check, this will list all available data
retrieved from registry. Things are pretty-printed as much as possible. You should call get_info_all
right after your domain_something call otherwise if you do another operation previous information
is lost. This is done automatically for you on the relevant commands, but you can also use it
manually at any time.

=head3 show profiles

Show the list of registries and associated profiles currently in use (opened in this shell with
add_registry + add_current_profile, or add).

=head3 show tlds

Show the list of TLDs handled by the currently selected registry.

=head3 show periods

Show the list of allowed periods (domain name durations) for the currently selected registry.

=head3 show objects

Show the list of managed objects types at the currently selected registry.

=head3 show types

Show the list of profile types at the currently selected registry

=head3 show status

Show the list of available status for the currently selected registry, to use
as status name in some commands below (domain_update_status_* domain_update 
host_update_status_* host_update contact_update_status_* contact_update).

=head3 show config

This will show all current config options. See C<set> command below for the list of config options.

=head3 set OPTION=VALUE

The set command can be used to change some options inside the shell.

The current list of available options is:

=head4 verbose

Set this option to 1 if you want a dump of all data retrieved from registry automatically after each operation, including failed ones, and including
all displaying raw data exchanged with registry.

=head3 target REGISTRYNAME PROFILENAME

Switch to registry REGISTRYNAME (from currently available registries) and profile PROFILENAME (from currently available
profiles in registry REGISTRYNAME).

=head3 run FILENAME

Will open the local FILENAME and read in it commands and execute all of them; you can also
start your shell with a filename as argument and its commands will be run at beginning of
session before giving the control back. They will be displayed (username and password will be
masked) with their results.

=head3 record FILENAME

If called with a filename argument, all subsequent commands, and their results will be printed in the filename given.
If called without argument, it stops a current recording session.

=head3 !cmd

All command line starting with a bang (!) will be treated as local commands to run through the local underlying OS shell.

Example: !ls -l
will display the content of the current directory.

=head3 help

Returns a succinct list of available commands.

=head3 quit

Leave the shell.

=head2 DOMAIN COMMANDS

=head3 domain_create DOMAIN [duration=X] [ns=HOSTNAMEA IPA1 IPA2 ... HOSTNAMEB IPB1 IPB2 ...] [admin=SRID1] [registrant=SRID2] [billing=SRID3] [tech=SRID4] [auth=X]

Create the given domain name. See above for the duration format to use. Admin, registrant, billing and tech
contact ids are mandatory or optional depending on the registry. They may be repeated (except registrant)
for registries allowing multiple contacts per role.

=head3 domain_info DOMAIN

Do a domain_info call to the registry for the domain YOURDOMAIN ; most of the the registries
prohibit getting information on domain names you do not sponsor.

=head3 domain_check DOMAIN

Do a domain_check call to the registry for the domain ANYDOMAIN ; you can check any domain,
existing or not, if you are the sponsoring registrar or not.

=head3 domain_exist DOMAIN

A kind of simpler domain_check, just reply by YES or NO for the given domain name.

=head3 domain_transfer_start DOMAIN auth=AUTHCODE [duration=PERIOD]

=head3 domain_transfer_stop DOMAIN [auth=AUTHCODE]

=head3 domain_transfer_query DOMAIN [auth=AUTHCODE]

=head3 domain_transfer_accept DOMAIN [auth=AUTHCODE]

=head3 domain_transfer_refuse DOMAIN [auth=AUTHCODE]

Start, or stop an incoming transfer, query status of a current running transfer, accept or refuse an outgoing domain name transfer.

The AUTHCODE is mandatory or optional, depending on the registry.

The duration is optional and can be specified (the allowed values depend on the registry) as Ayears or Bmonths
where A and B are integers for the number of years or months (this can be abbreviated as Ay or Bm).

=head3 domain_update_ns_set DOMAIN HOSTNAMEA IPA1 IPA2 ... HOSTNAMEB IPB1 IPB2 ...

=head3 domain_update_ns_add DOMAIN HOSTNAMEA IPA1 IPA2 ... HOSTNAMEB IPB1 IPB2 ...

=head3 domain_update_ns_del DOMAIN HOSTNAMEA IPA1 IPA2 ... HOSTNAMEB IPB1 IPB2 ...

Set the current list of nameservers associated to this DOMAIN, add to the current list or delete from the current list.

=head3 domain_update_status_set DOMAIN STATUS1 STATUS2 ...

=head3 domain_update_status_add DOMAIN STATUS1 STATUS2 ...

=head3 domain_update_status_del DOMAIN STATUS1 STATUS2 ...

Set the current list of status associated to this DOMAIN, add to the current
list or delete from the current list. First parameter is the domain name, then status names,
as needed.

The status names are those in the list given back by the show status command (see above).

=head3 domain_update_contact_set DOMAIN SRVID1 SRVID2 ...

=head3 domain_update_contact_add DOMAIN SRVID2 SRVID2 ...

=head3 domain_update_contact_del DOMAIN SRVID1 SRVID2 ...

Set the current list of contacts associated to this DOMAIN, add to the current list or delete from the current list
by providing the contact server ids.

=head3 domain_update DOMAIN +status=S1 -status=S2 +admin=C1 -tech=C2 -billing=C3 registrant=C4 auth=A +ns=... -ns=...

Combination of the previous methods, plus ability to change authInfo and other parameters depending on registry.

=head3 domain_renew DOMAIN [duration=X] [current_expiration=YYYY-MM-DD]

Renew the given domain name. Duration and current expiration are optional. See above for the duration format to use.

=head3 domain_delete DOMAIN

Delete the given domain name.


=head2 HOST COMMANDS

For registries handling nameservers as separate objects.

=head3 host_create HOSTNAME IP1 IP2 ...

Create the host named HOSTNAME at the registry with the list of IP (IPv4 and IPv6
depending on registry support) given.

=head3 host_delete HOSTNAME

=head3 host_info HOSTNAME

=head3 host_check HOSTNAME

Various operations on host objects.

=head3 host_update_ip_set HOSTNAME IP1 IP2 ...

=head3 host_update_ip_add HOSTNAME IP1 IP2 ...

=head3 host_update_ip_del HOSTNAME IP1 IP2 ...

Set the current list of IP addresses associated to this HOSTNAME, add to the current
list or delete from the current list. First parameter is the nameserver hostname, then IP addresses,
as needed.

=head3 host_update_status_set HOSTNAME STATUS1 STATUS2 ...

=head3 host_update_status_add HOSTNAME STATUS1 STATUS2 ...

=head3 host_update_status_del HOSTNAME STATUS1 STATUS2 ...

Set the current list of status associated to this HOSTNAME, add to the current
list or delete from the current list. First parameter is the nameserver hostname, then status names,
as needed.

The status names are those in the list given back by the show status command (see above).

=head3 host_update HOSTNAME +ip=IP1 +ip=IP2 -ip=IP3 +status=STATUS1 -status=STATUS2 name=NEWNAME

Combines the previous operations.

=head3 host_update_name_set HOSTNAME NEWNAME

Change the current name of host objects from HOSTNAME to NEWNAME.


=head2 CONTACT COMMANDS

For registries handling contacts as separate objects.

=head3 contact_create name=X org=Y street=Z1 street=Z2 email=A voice=B ...

Create a new contact object.

The list of mandatory attributes depend on the registry. Some attributes (like street) may appear multiple times.

Some registry allow setting an ID (using srid=yourchoice), others create the ID, in which case you need
to do a get_info_all after contact_create to retrieve the given server ID.

=head3 contact_delete SRID

=head3 contact_info SRID

=head3 contact_check SRID

Various operations on contacts.

=head3 contact_update_status_set SRID STATUS1 STATUS2 ...

=head3 contact_update_status_add SRID STATUS1 STATUS2 ...

=head3 contact_update_status_del SRID STATUS1 STATUS2 ...

Set the current list of status associated to this contact SRID, add to the current
list or delete from the current list. First parameter is the contact server ID, then status names,
as needed.

The status names are those in the list given back by the show status command (see above).

=head3 contact_update SRID name=X org=Y ... +status=... -status=...

Change some contacts attributes, as well as statuses.

=head3 contact_transfer_start SRID

=head3 contact_transfer_stop SRID

=head3 contact_transfer_query SRID

=head3 contact_transfer_accept SRID

=head3 contact_transfer_refuse SRID

Start, or stop an incoming transfer, query status of a current running transfer, accept or refuse an outgoing contact transfer.


=head2 MESSAGE COMMANDS

For registries handling messages, like EPP poll features.

=head3 message_retrieve [ID]

Retrieve a message waiting at registry.

=head3 message_delete [ID]

Delete a message waiting at registry.

=head3 message_waiting

Notifies if messages are waiting at registry.

=head3 message_count

Get the numbers of messages waiting at the registry.

=head1 COMPLETION

If Term::Readline::Gnu or Term::Readline::Perl are installed, it will be automatically used by this shell 
to provide standard shell autocompletion for commands and parameters.

All commands described above will be available through autocompletion. As you use them,
all parameters (domain names, contacts, hostnames, local files) will also be stored
and provided to later autocompletion calls (with the [TAB] key).

It will also autocomplete registry= and type= parameters during add/add_registry, from
a basic default set of values: registry= values are taken from a basic Net::DRI install
without taking into account any private DRD module, and type= values are a default set,
not checked against registry= value.
Same for target calls, where registry and/or profile name will be autocompleted as possible.

It will even autocomplete TLD on domain names for your current registry after your typed
the first label and a dot (and eventually some other characters), during any domain name operation.
Same for durations and status values.

Contacts and nameservers will also be autocompleted when used in any domain_* operation.

Contacts attributes will be autocompleted during contact_create based on the current registry & profile.

Information retrieved with domain_info calls will also be used in later autocompletion tries,
regarding contact ids and hostnames. During a contact creation, the registry returned contact id
is also added for later autocompletion tries.

For autocompletion, contacts are specific to each registry. Hostnames are common to all registries,
as are domain names, but domain names are checked against the available TLDs of the current registry when used 
for autocompletion.

=head1 LOGGING

By default, all operations will have some logging information done in files stored in
the working directory. There will be a core.log file for all operations and then one
file per tuple (registry,profile).

=head1 BATCH OPERATIONS

Batch operations are available for some domain name commands: domain_create,
domain_delete, domain_renew, domain_check, domain_info, domain_transfer and
all domain_update commands. It can be used on a list of domain names for which
all other parameters needed by the command are the same.

To do that, just use the command normally as outlined above, but instead of the
domain name, put a file path, with at least one / (so for a file "batch.txt" in the
current directory, use "./batch.txt").

If you use backticks such as `command` for the domain name, the command will 
be started locally and its output will be used just like a file.

The shell will then apply the command and its parameters on the domain names
listed in the specified file: you should have one domain name per line, blank
lines and lines starting with # are ignored.

At the same place a new file is created with a name derived from the given name
in which the result of each domain name command will be written. If "input" is 
the filename used, the results will be written to "input.PID.TIME.results"
where PID is the program id of the running shell for these commands and TIME the
Unix epoch when the batch started.

As output the shell will give a summary of the number of operations done
for each possible outcome (success or error), as well as time statistics.

=head1 SUPPORT

For now, support questions should be sent to:

E<lt>netdri@dotandco.comE<gt>

Please also see the SUPPORT file in the distribution.

=head1 SEE ALSO

E<lt>http://www.dotandco.com/services/software/Net-DRI/E<gt>

=head1 AUTHOR

Patrick Mevzek, E<lt>netdri@dotandco.comE<gt>

=head1 COPYRIGHT

Copyright (c) 2008-2010 Patrick Mevzek <netdri@dotandco.com>.
All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

See the LICENSE file that comes with this distribution for more details.

=cut

####################################################################################################

sub run
{
 my (@args)=@_;
 my $term=Term::ReadLine->new('Net::DRI shell');
 my $ctx={ term    => $term,
           term_features => $term->Features(),
           term_attribs => $term->Attribs(),
           dprompt => 'NetDRI',
           output  => $term->OUT() || \*STDOUT,
           record_filename => undef,
           record_filehandle => undef,
           config => { verbose => 0 },
           completion => { domains => {}, contacts => {}, hosts => {}, files => {} },
         };
 if (exists $ctx->{term_features}->{ornaments}) { $term->ornaments(1); }
 $ctx->{term_attribs}->{completion_function}=sub { return complete($ctx,@_); };
 $ctx->{prompt}=$ctx->{dprompt};

 output($ctx,"Welcome to Net::DRI shell, pid $$, version $VERSION\n");

 $ctx->{dri}=Net::DRI->new({cache_ttl => 10,logging=>['files',{level => 'info'}]});
 output($ctx,"Net::DRI object created with a cache TTL of 10 seconds and logging into files in current directory\n\n");

 $ctx->{file_quit}=0;
 shift(@args) if ($args[0] eq 'Net::DRI::Shell');
 handle_line($ctx,'run '.$args[0]) if (@args);

 unless ($ctx->{file_quit})
 {
  delete($ctx->{file_quit});
  while (defined(my $l=$ctx->{term}->readline($ctx->{prompt}.'> '))) 
  {
   last if handle_line($ctx,$l);
  }
 }

 $ctx->{dri}->end();
 return 0; ## TODO : should reflect true result of last command ?
}

sub output
{
 my $ctx=shift;
 print { $ctx->{output} } @_;
 output_record($ctx,@_);
}

sub output_record
{
 my $ctx=shift;
 return unless defined($ctx->{record_filehandle});
 return if (@_==1 && ($_[0] eq '.' || $_[0] eq "\n"));
 my $l=$ctx->{last_line};
 print { $ctx->{record_filehandle} } scalar(localtime(time)),"\n\n",(defined($l)? ($l,"\n\n") : ('')),@_,"\n\n";
 $ctx->{last_line}=undef;
}

sub handle_file
{
 my ($ctx,$file)=@_;
 output($ctx,'Executing commands from file '.$file." :\n");
 $ctx->{completion}->{files}->{$file}=time();
 open(my $ch,'<',$file) or die "Unable to open $file : $!";
 while(defined(my $l=<$ch>))
 {
  chomp($l);
  next if ($l=~m/^\s*$/ || $l=~m/^#/);
  my $pl=$l;
  $pl=~s/(clID|client_login|client_password)=\S+/$1=********/g;
  output($ctx,$pl."\n");
  if (handle_line($ctx,$l))
  {
   $ctx->{file_quit}=1;
   last;
  }
 }
 close($ch) or die $!;
 return;
}

sub handle_line
{
 my ($ctx,$l)=@_;
 return 0 if ($l=~m/^\s*$/);

 $l=~s/^\s*//;
 $l=~s/\s*$//;

 return 1 if ($l eq 'quit' || $l eq 'q' || $l eq 'exit');

 my ($rc,$msg);

 eval
 {
  ($rc,$msg)=process($ctx,$l);
  $msg.="\n".dump_info($ctx,scalar $rc->get_data_collection()) if (defined($rc) && (($l=~m/^(?:(?:domain|contact|host)_?(?:check|info|create)|domain_renew) / && (!defined($msg) || index($msg,'on average')==-1) && $rc->is_success()) || $ctx->{config}->{verbose}==1));
 };

 $ctx->{last_line}=$l;
 if ($@)
 {
  output($ctx,"An error happened:\n",ref($@)? $@->msg() : $@,"\n");
 } else
 {
  my @r;
  if (defined($rc))
  {
   push @r,$rc->as_string(1),"\n";
  }
  push @r,$msg if (defined($msg));
  if (defined($rc) && $rc->is_closing() && $ctx->{dri}->transport()->has_state())
  {
   $ctx->{dri}->transport()->current_state(0);
   push @r,'Server connection closed, will try to reconnect during next command.'; ## TODO : this is triggered also for type=das, but shouldn't !
  }
  output($ctx,@r,"\n");
 }

 $ctx->{term}->addhistory($l);
 $ctx->{last_line}=undef;
 return 0;
}

sub complete
{
 my ($ctx,$text,$line,$start)=@_; ## $text is last space separated word, $line the whole line, $start the position of the cursor in the line

 ## Command completion
 if ($start==0) ## command completion
 {
  return sort { $a cmp $b } grep { /^$text/ } qw/quit exit help run record message_retrieve message_delete domain_create domain_renew domain_delete domain_check domain_info domain_transfer_start domain_transfer_stop domain_transfer_query domain_transfer_accept domain_transfer_refuse domain_update_ns_set domain_update_ns_add domain_update_ns_del domain_update_status_set domain_update_status_add domain_update_status_del domain_update_contact_set domain_update_contact_add domain_update_contact_del domain_update host_create host_delete host_info host_check host_update_ip_set host_update_ip_add host_update_ip_del host_update_status_set host_update_status_add host_update_status_del host_update_name_set host_update contact_create contact_info contact_check contact_delete contact_update contact_update_status_set contact_update_status_add contact_update_status_del contact_transfer_start contact_transfer_stop contact_transfer_query contact_transfer_accept contact_transfer_refuse set add add_registry target add_current_profile add_profile show get_info get_info_all message_waiting message_count domain_exist/;
 }

 ## Parameter completion
 my ($cmd)=($line=~m/^(\S+)\s/);
 if ($cmd eq 'show') { return sort { $a cmp $b } grep { /^$text/ } qw/profiles tlds periods objects types status config/; }
 if ($cmd eq 'set')  { return map { $_.'=' } sort { $a cmp $b } grep { /^$text/ } keys(%{$ctx->{config}}); }
 if ($cmd eq 'run' || $cmd eq 'record') { return sort { $ctx->{completion}->{files}->{$b} <=> $ctx->{completion}->{files}->{$a} || $a cmp $b } grep { /^$text/ } keys(%{$ctx->{completion}->{files}}); }

 if ($cmd eq 'add' || $cmd eq 'add_registry' || $cmd eq 'add_current_profile' || $cmd eq 'add_profile')
 {
  if (substr($line,$start-9,9) eq 'registry=')
  {
   my ($reg)=($text=~m/registry=(\S*)/);
   $reg||='';
   return sort { $a cmp $b } grep { /^$reg/ } $ctx->{dri}->installed_registries();
  } elsif (substr($line,$start-5,5) eq 'type=')
  {
   my ($type)=($text=~m/type=(\S*)/);
   $type||='';
   return sort { $a cmp $b } grep { /^$type/ } (defined $ctx->{dri}->registry_name()? $ctx->{dri}->registry()->driver()->profile_types() : qw/epp rrp rri dchk whois das ws email/);
  } else
  {
   my @p;
   @p=qw/registry clID/ if $cmd eq 'add_registry';
   @p=qw/type name/ if ($cmd=~m/^add_(?:current_)?profile$/);
   @p=qw/registry clID type name/ if $cmd eq 'add';
   return map { $_.'=' } grep { /^$text/ } @p;
  }
 }

 if ($cmd eq 'target')
 {
  my $regs=$ctx->{dri}->available_registries_profiles(0);
  if (my ($reg)=($line=~m/^target\s+(\S+)\s+\S*$/))
  {
   return sort { $a cmp $b } grep { /^$text/ } (exists $regs->{$reg} ? @{$regs->{$reg}} : ());
  } elsif ($line=~m/^target\s+\S*$/)
  {
   return sort { $a cmp $b } grep { /^$text/ } keys(%$regs);
  }
 }

 if (substr($line,$start-9,9) eq 'duration=')
 {
  return () unless defined $ctx->{dri}->registry_name();
  my ($p)=($text=~m/duration=(\S*)/);
  $p||='';
  my %p;
  foreach my $pd ($ctx->{dri}->registry()->driver()->periods())
  {
   my $d=$pd->in_units('years');
   if ($d > 0) { $p{$d.'Y'}=12*$d; next; }
   $d=$pd->in_units('months');
   if ($d > 0) { $p{$d.'M'}=$d; next; }
  }
  return sort { $p{$a} <=> $p{$b} } grep { /^$p/ } keys(%p); ## this is the correct ascending order, but it seems something else upstream is reordering it differently
 }

 if ($line=~m/^domain_\S+\s+\S*$/)
 {
  my @p=grep { /^$text/ } keys(%{$ctx->{completion}->{domains}});
  if (defined $ctx->{dri}->registry())
  {
   my @tlds=$ctx->{dri}->registry()->driver()->tlds();
   my $tlds=join('|',map { quotemeta($_) } @tlds);
   @p=grep { /\.(?:$tlds)$/i } @p;
   my $idx=index($text,'.');
   if ( $idx >= 0 )
   {
    my $base=substr($text,0,$idx);
    push @p,map { $base.'.'.$_ } @tlds;
   }
  }
  return sort { ( $ctx->{completion}->{domains}->{$b} || 0) <=> ( $ctx->{completion}->{domains}->{$a} || 0 ) || $a cmp $b } @p;
 }

 my @ct=qw/registrant admin tech billing/; ## How to retrieve non core contact types ?
 my $capa;
 if ($ctx->{dri}->registry_name() && $ctx->{dri}->available_profile() && $ctx->{dri}->protocol())
 {
  @ct=('registrant',$ctx->{dri}->protocol()->core_contact_types()) if $ctx->{dri}->protocol()->can('core_contact_types');
  $capa=$ctx->{dri}->protocol()->capabilities();
 }
 my $ctre=join('|',@ct);

 if ($cmd eq 'domain_create') ## If we are here, we are sure the domain name has been completed already, due to previous test block
 {
  if (substr($line,$start-3,3) eq 'ns=')
  {
   my ($ns)=($text=~m/ns=(\S*)/);
   $ns||='';
   return _complete_hosts($ctx,$ns);
  } elsif (grep { substr($line,$start-(1+length($_)),1+length($_)) eq $_.'=' } @ct)
  {
   my ($c)=($text=~m/(?:${ctre})=(\S*)/);
   $c||='';
   return _complete_contacts($ctx,$c);
  } else
  {
   return map { $_.'=' } grep { /^$text/ } (qw/duration ns auth/,@ct);
  }
 }

 if ($cmd eq 'domain_update') ## see previous comment
 {
  if (substr($line,$start-4,4)=~m/^[-+]ns=$/)
  {
   my ($ns)=($text=~m/ns=(\S*)/);
   $ns||='';
   return _complete_hosts($ctx,$ns);
  } elsif (grep { substr($line,$start-(1+length($_)),1+length($_)) eq $_.'=' } @ct) #####
  {
   my ($c)=($text=~m/(?:${ctre})=(\S*)/);
   $c||='';
   return _complete_contacts($ctx,$c);
  } elsif (substr($line,$start-8,8)=~m/^[-+]status=$/)
  {
   my $o=$ctx->{dri}->local_object('status');
   if (! defined $o) { return (); }
   my ($s)=($text=~m/status=(\S*)/);
   $s||='';
   return sort { $a cmp $b } grep { /^$s/ } map { 'no'.$_ } $o->possible_no();
  } else
  {
   $text=~s/\+/[+]/g;
   return map { $_.'=' } sort { $a cmp $b } grep { /^$text/ } (map { if (/^([+-])contact$/) { map { $1.$_ } @ct } else { $_; } } _complete_capa2list($capa,'domain_update'));
  }
 }

 if ($line=~m/^domain_update_ns_\S+\s+\S+\s+\S*/) { return _complete_hosts($ctx,$text); }

 if ($line=~m/^(?:domain|host|contact)_update_status_\S+\s+\S+\s+\S*/)
 {
  my $o=$ctx->{dri}->local_object('status');
  if (! defined $o) { return (); }
  return sort { $a cmp $b } grep { /^$text/ } map { 'no'.$_ } $o->possible_no();
 }

 if ($line=~m/^domain_update_contact_\S+\s+\S+\s+\S*/) { return _complete_contacts($ctx,$text); }

 if (my ($trans)=($line=~m/^domain_transfer_(\S+)\s+\S+\s+\S*/))
 {
  my @p=qw/auth/;
  push @p,'duration' if $trans eq 'start';
  return map { $_.'=' } sort { $a cmp $b } grep { /^$text/ } @p;
 }

 if ($cmd eq 'contact_create')
 {
  return () unless (defined $ctx->{dri}->registry_name() && defined $ctx->{dri}->profile());
  my $c=$ctx->{dri}->local_object('contact');
  if (! defined $c) { return (); }
  return map { $_.'=' } sort { $a cmp $b } grep { /^$text/ } $c->attributes();
 }

 if ($line=~m/^contact_\S+\s+\S*$/) { return _complete_contacts($ctx,$text); }

 if ($cmd eq 'contact_update')
 {
  return () unless (defined $ctx->{dri}->registry_name() && defined $ctx->{dri}->profile());
  my $c=$ctx->{dri}->local_object('contact');
  $text=~s/\+/[+]/g;
  return map { $_.'=' } sort { $a cmp $b } grep { /^$text/ } (defined $c ? $c->attributes() : (),_complete_capa2list($capa,'contact_update'));
 }

 if ($line=~m/^host_\S+\s+\S*$/) { return _complete_hosts($ctx,$text); }
 if (my ($h)=($line=~m/^host_update_name_set\s+\S+\s+(\S*)$/)) { return _complete_hosts($ctx,$h); }
 if ($cmd eq 'host_update')
 {
  if (substr($line,$start-5,5) eq 'name=')
  {
   my ($ns)=($text=~m/name=(\S*)/);
   $ns||='';
   return _complete_hosts($ctx,$ns);
  } elsif ( substr($line,$start-8,8)=~m/^[-+]status=$/ )
  {
   my $o=$ctx->{dri}->local_object('status');
   if (! defined $o) { return (); }
   my ($s)=($text=~m/status=(\S*)/);
   $s||='';
   return sort { $a cmp $b } grep { /^$s/ } map { 'no'.$_ } $o->possible_no();
  } else
  {
   $text=~s/\+/[+]/g;
   return map { $_.'=' } sort { $a cmp $b } grep { /^$text/ } (_complete_capa2list($capa,'host_update'));
  }
 }

 return ();
}

sub _complete_capa2list
{
 my ($capa,$what)=@_;
 return () unless (defined $capa && exists($capa->{$what}));
 my @r;
 while(my ($k,$ra)=each(%{$capa->{$what}}))
 {
  foreach my $t (@$ra)
  {
   if ($t eq 'add') { push @r,'+'.$k; } elsif ($t eq 'del') { push @r,'-'.$k; } elsif ($t eq 'set') { push @r,$k; }
  }
 }
 return @r;
}

sub _complete_hosts    { my ($ctx,$text)=@_; return sort { $ctx->{completion}->{hosts}->{$b} <=> $ctx->{completion}->{hosts}->{$a} || $a cmp $b } grep { /^$text/ } keys(%{$ctx->{completion}->{hosts}}); }
sub _complete_contacts
{
 my ($ctx,$text)=@_;
 my @c=grep { /^$text/ } keys(%{$ctx->{completion}->{contacts}});
 my $creg=$ctx->{dri}->registry_name();
 if (defined $creg) { @c=grep { defined $ctx->{completion}->{contacts}->{$_}->[1] && $ctx->{completion}->{contacts}->{$_}->[1] eq $creg } @c; } ## Filtering per registry
 return sort { $ctx->{completion}->{contacts}->{$b}->[0] <=> $ctx->{completion}->{contacts}->{$a}->[0] || $a cmp $b } @c;
}

sub process
{
 my ($ctx,$wl)=@_;
 my ($rc,$m);

 my ($cmd,$params)=split(/\s+/,$wl,2);
 $params='' unless defined($params);
 my @p=split(/\s+/,$params);
 my %p;
 my @g=($params=~m/\s*(\S+)=(\S[^=]*)(?:\s|$)/g);
 while (@g)
 {
  my $n=shift(@g);
  my $v=shift(@g);
  if (exists($p{$n}))
  {
   $p{$n}=[$p{$n}] unless (ref($p{$n}) eq 'ARRAY');
   push @{$p{$n}},$v;
  } else
  {
   $p{$n}=$v;
  }
 }

 foreach my $k (grep { /\./ } keys(%p))
 {
  my ($tk,$sk)=split(/\./,$k,2);
  $p{$tk}={} unless exists($p{$tk});
  $p{$tk}->{$sk}=$p{$k};
  delete($p{$k});
 }

 return do_local($ctx,$cmd,\@p,\%p) if ($cmd=~m/^!/);
 return help($ctx,$cmd,\@p,\%p) if ($cmd eq 'help');
 return handle_file($ctx,$p[0]) if ($cmd eq 'run');
 return record($ctx,$p[0])      if ($cmd eq 'record');
 return do_dri($ctx,$cmd,\@p,\%p) if ($cmd=~m/^message_(?:retrieve|delete)$/);
 return do_domain($ctx,$cmd,\@p,\%p) if ($cmd=~m/^domain_(?:check)$/);
 return do_domain_transfer($ctx,$cmd,\@p,\%p) if ($cmd=~m/^domain_transfer_(?:start|stop|query|accept|refuse)$/);
 return do_domain_update($ctx,$cmd,\@p,\%p) if ($cmd eq 'domain_update');
 return do_domain_update_ns($ctx,$cmd,\@p,\%p) if ($cmd=~m/^domain_update_ns_(?:add|del|set)$/);
 return do_domain_update_status($ctx,$cmd,\@p,\%p) if ($cmd=~m/^domain_update_status_(?:add|del|set)$/);
 return do_domain_update_contact($ctx,$cmd,\@p,\%p) if ($cmd=~m/^domain_update_contact_(?:add|del|set)$/);

 if ($cmd eq 'domain_info')
 {
  my @r=do_domain($ctx,$cmd,\@p,\%p);
  if (defined $r[0] && $r[0]->is_success())
  {
   my $ns=$ctx->{dri}->get_info('ns');
   if (defined $ns) { foreach my $name ($ns->get_names()) { $ctx->{completion}->{hosts}->{$name}=time(); } }
   $ns=$ctx->{dri}->get_info('host');
   if (defined $ns) { foreach my $name ($ns->get_names()) { $ctx->{completion}->{hosts}->{$name}=time(); } }
   my $cs=$ctx->{dri}->get_info('contact');
   if (defined $cs)
   {
    foreach my $t ($cs->types())
    {
     foreach my $cc ($cs->get($t)) { $ctx->{completion}->{contacts}->{$cc->srid()}=[time(),$ctx->{dri}->registry_name()]; }
    }
   }
  }
  return @r;
 }

 if ($cmd=~m/^host_(?:create|delete|info|check|update|update_(?:ip|status|name)_(?:add|del|set))$/)
 {
  return (undef,'Registry does not support host objects') unless $ctx->{dri}->has_object('ns');
  return do_host($ctx,$cmd,\@p,\%p);
 }

 if ($cmd=~m/^contact_(?:create|delete|info|check|update|update_status_(?:add|del|set)|transfer_(?:start|stop|query|accept|refuse))$/)
 {
  return (undef,'Registry does not support contact objects') unless $ctx->{dri}->has_object('contact');
  my @r=do_contact($ctx,$cmd,\@p,\%p);
  if ($cmd eq 'contact_create' && defined $r[0] && $r[0]->is_success())
  {
   my $id=$ctx->{dri}->get_info('id');
   if (defined $id) { $ctx->{completion}->{contacts}->{$id}=[time(),$ctx->{dri}->registry_name()]; }
  }
  return @r;
 }

 {
  no strict 'refs'; ## no critic (ProhibitNoStrict)
  my $sub='do_'.$cmd;
  return $sub->($ctx,$cmd,\@p,\%p) if (exists(&$sub));
 }

 ## Fallback for all domain extension commands
 return do_domain_extension($ctx,$cmd,\@p,\%p) if ($cmd=~m/^domain_\S+/);

 return (undef,'Unknown command '.$cmd);
}

sub do_local
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 $cmd=~s/^!//;
 my $s=$cmd.' '.join(' ',@$ra);
 my $out=qx($s);
 return (undef,defined($out)? $out : 'Local command failed: '.$!);
}

sub help
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $m=<<EOF;
Available commands (parameters after the first one can be in any order):

help
add registry=REGISTRYNAME type=TYPE [cLID=YOURLOGIN]
add_registry registry=REGISTRYNAME [clID=YOURLOGIN]
add_current_profile name=PROFILENAME type=TYPE defer=0 client_login=YOURLOGIN client_password=YOURPASSWORD
get_info_all
show profiles
show tlds
show periods
show objects
show status
show config
set P=X
target X Y
run FILENAME
record FILENAME
quit
domain_create DOMAIN [duration=X] [ns=HOSTNAMEA IPA1 IPA2 ... HOSTNAMEB IPB1 IPB2 ...] [admin=SRID1] [registrant=SRID2] [billing=SRID3] [tech=SRID4] [auth=X]
domain_info DOMAIN
domain_check DOMAIN
domain_exist DOMAIN
domain_transfer_start DOMAIN auth=AUTHCODE [duration=PERIOD]
domain_transfer_stop DOMAIN [auth=AUTHCODE]
domain_transfer_query DOMAIN [auth=AUTHCODE]
domain_transfer_accept DOMAIN [auth=AUTHCODE]
domain_transfer_refuse DOMAIN [auth=AUTHCODE]
domain_update_ns_set DOMAIN HOSTNAMEA IPA1 IPA2 ... HOSTNAMEB IPB1 IPB2 ...
domain_update_ns_add DOMAIN HOSTNAMEA IPA1 IPA2 ... HOSTNAMEB IPB1 IPB2 ...
domain_update_ns_del DOMAIN HOSTNAMEA IPA1 IPA2 ... HOSTNAMEB IPB1 IPB2 ...
domain_update_status_set DOMAIN STATUS1 STATUS2 ...
domain_update_status_add DOMAIN STATUS1 STATUS2 ...
domain_update_status_del DOMAIN STATUS1 STATUS2 ...
domain_update_contact_set DOMAIN SRVID1 SRVID2 ...
domain_update_contact_add DOMAIN SRVID2 SRVID2 ...
domain_update_contact_del DOMAIN SRVID1 SRVID2 ...
domain_update DOMAIN +status=S1 -status=S2 +admin=C1 -tech=C2 -billing=C3 registrant=C4 auth=A +ns=... -ns=...
domain_renew DOMAIN [duration=X] [current_expiration=YYYY-MM-DD]
domain_delete DOMAIN
host_create HOSTNAME IP1 IP2 ...
host_delete HOSTNAME
host_info HOSTNAME
host_check HOSTNAME
host_update_ip_set HOSTNAME IP1 IP2 ...
host_update_ip_add HOSTNAME IP1 IP2 ...
host_update_ip_del HOSTNAME IP1 IP2 ...
host_update_status_set HOSTNAME STATUS1 STATUS2 ...
host_update_status_add HOSTNAME STATUS1 STATUS2 ...
host_update_status_del HOSTNAME STATUS1 STATUS2 ...
host_update_name_set HOSTNAME NEWNAME
host_update HOSTNAME +ip=IP1 +ip=IP2 -ip=IP3 +status=STATUS1 -status=STATUS2 name=NEWNAME ...
contact_create name=X org=Y street=Z1 street=Z2 email=A voice=B ...
contact_delete SRID
contact_info SRID
contact_check SRID
contact_update_status_set SRID STATUS1 STATUS2 ...
contact_update_status_add SRID STATUS1 STATUS2 ...
contact_update_status_del SRID STATUS1 STATUS2 ...
contact_update SRID name=X org=Y ... +status=... -status=...
contact_transfer_start SRID
contact_transfer_stop SRID
contact_transfer_query SRID
contact_transfer_accept SRID
contact_transfer_refuse SRID
message_retrieve [ID]
message_delete [ID]
message_waiting
message_count
EOF
 return (undef,$m);
}

sub record
{
 my ($ctx,$n)=@_;
 my $m='';

 ## Need to stop the current one in all cases ! (true record stop or a new record start)
 if (defined($ctx->{record_filehandle}))
 {
  close($ctx->{record_filehandle});
  $ctx->{record_filehandle}=undef;
  $m='Stopped recording session to '.$ctx->{record_filename}."\n";
 }

 if (defined($n) && $n)
 {
  $ctx->{completion}->{files}->{$n}=time();
  open(my $fh,'>',$n) or return (undef,$m.'Unable to write local file '.$n.' : '.$!);
  $fh->autoflush(1); ## this is thanks to IO::Handle
  $ctx->{record_filehandle}=$fh;
  $ctx->{record_filename}=$n;
  $m.='Started recording session to '.$ctx->{record_filename};
 }
 return (undef,$m? $m : 'Usage: record FILENAME (to start recording session to local FILENAME) or record (to stop current recording)');
}

## For local options, like verbose
sub do_set
{
 my($ctx,$cmd,$ra,$rh)=@_;
 $ctx->{config}={ %{$ctx->{config}},%$rh };
 return;
}

sub do_add
{
 my($ctx,$cmd,$ra,$rh)=@_;
 return (undef,'Usage: add registry=REGISTRYNAME type=PROTOCOLTYPE [clID=LOGIN] [name=PROFILENAME] [...]') unless (Net::DRI::Util::has_key($rh,'registry') && Net::DRI::Util::has_key($rh,'type'));
 my %r=(registry => $rh->{registry});
 $r{clID}=$rh->{clID} if exists($rh->{clID});
 my @r=do_add_registry($ctx,'add_registry',$ra,\%r);
 if (! defined $r[0] || ! $r[0]->is_success()) { return @r; }
 unless (exists($rh->{name}) && defined($rh->{name}))
 {
  my @p=$ctx->{dri}->available_profiles();
  $rh->{name}=lc($rh->{registry}).(1+@p);
 }
 delete($rh->{registry});
 delete($rh->{clID});
 return do_add_current_profile($ctx,'add_current_profile',$ra,$rh);
}

sub do_add_registry
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 return (undef,'Usage: add_registry registry=REGISTRYNAME [clID=LOGIN]') unless Net::DRI::Util::has_key($rh,'registry');
 my $reg=$rh->{registry};
 delete($rh->{registry});
 if (! grep { $reg eq $_ } $ctx->{dri}->available_registries() ) { $ctx->{dri}->add_registry($reg,$rh); }
 $ctx->{dri}->target($reg);
 $ctx->{prompt}=$ctx->{dprompt}.'('.$reg.')';
 return (Net::DRI::Protocol::ResultStatus->new_generic_success('Registry "'.$reg.'" added successfully'),undef);
}

sub do_target
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 $ctx->{dri}->target(@$ra);
 $ctx->{prompt}=$ctx->{dprompt}.'('.join(',',@$ra).')';
 return;
}

sub do_add_current_profile
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 return (undef,'Usage: add_current_profile name=PROFILENAME type=SERVICENAME [defer=0] [client_login=YOURLOGIN] [client_password=YOURPASSWORD]') unless (Net::DRI::Util::has_key($rh,'name') && Net::DRI::Util::has_key($rh,'type'));
 my $name=$rh->{name};
 my $type=$rh->{type};
 my $rp=defined $rh->{protocol}? $rh->{protocol} : {};
 delete(@{$rh}{qw/name type protocol/});
 my $rc=$ctx->{dri}->$cmd($name,$type,$rh,$rp);
 if ($rc->is_success() && $cmd eq 'add_current_profile')
 {
  my @t=$ctx->{dri}->registry();
  $ctx->{prompt}=$ctx->{dprompt}.'('.$t[0].','.$t[1]->profile().')';
 }
 return ($rc,undef);
}

sub do_add_profile { return do_add_current_profile(@_); }

sub do_show
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $m='Usage: show profiles|tlds|periods|objects|types|status|config';
 return (undef,$m) unless @$ra;
 if ($ra->[0] eq 'profiles')
 {
  my $rp=$ctx->{dri}->available_registries_profiles(1);
  $m='';
  foreach my $reg (sort(keys(%$rp)))
  {
   $m.=$reg.': '.join(' ',@{$rp->{$reg}})."\n";
  }
 } elsif ($ra->[0] eq 'tlds')
 {
  $m=join("\n",$ctx->{dri}->registry()->driver()->tlds());
 } elsif ($ra->[0] eq 'periods' || $ra->[0] eq 'durations')
 {
  $m=join("\n",map { pretty_string($_,0); } $ctx->{dri}->registry()->driver()->periods());
 } elsif ($ra->[0] eq 'objects')
 {
  $m=join("\n",$ctx->{dri}->registry()->driver()->object_types());
 } elsif ($ra->[0] eq 'types')
 {
  $m=join("\n",$ctx->{dri}->registry()->driver()->profile_types());
 } elsif ($ra->[0] eq 'status')
 {
  my $o=$ctx->{dri}->local_object('status');
  $m=defined($o)? join("\n",map { 'no'.$_ } $o->possible_no()) : 'No status objects';
 } elsif ($ra->[0] eq 'config')
 {
  $m='';
  foreach my $k (sort(keys(%{$ctx->{config}})))
  {
   $m.=$k.'='.$ctx->{config}->{$k}."\n";
  }
 }
 return (undef,$m);
}

sub do_get_info
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $m=$ctx->{dri}->get_info(@$ra); 
 return (undef,pretty_string($m,0));
}

sub do_get_info_all
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $rp=$ctx->{dri}->get_info_all(@$ra);
 my $m='';
 foreach my $k (sort(keys(%$rp)))
 {
  $m.=$k.': '.pretty_string($rp->{$k},0)."\n";
 }
 return (undef,$m);
}

sub do_dri
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 return ($ctx->{dri}->$cmd(@$ra),undef);
}

sub do_message_waiting
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $e=$ctx->{dri}->$cmd(@$ra);
 return (undef,'Unable to find if messages are waiting at the registry') unless defined($e);
 return (undef,'Messages waiting at the registry? '.($e? 'YES' : 'NO'));
}

sub do_message_count
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $e=$ctx->{dri}->$cmd(@$ra);
 return (undef,'Unable to find the number of messages waiting at the registry') unless defined($e);
 return (undef,'Number of messages waiting at the registry: '.$e);
}

## Try to handle all domain commands defined in extensions, with some heuristics
sub do_domain_extension
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $dom=shift(@$ra);
 build_auth($rh);
 build_duration($ctx,$rh);
 $rh->{status}=build_status($ctx,ref $rh->{status}? $rh->{status} : [ $rh->{status} ] ) if exists($rh->{status});
 $rh->{contact}=build_contactset($ctx,$rh->{contact}) if (exists $rh->{contact});
 return wrap_command_domain($ctx,$cmd,$dom,$rh);
}

sub do_domain
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $dom=shift(@$ra);
 return wrap_command_domain($ctx,$cmd,$dom,$rh);
}

sub do_domain_exist
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $dom=lc($ra->[0]);
 $ctx->{completion}->{domains}->{$dom}=time();
 my $e=$ctx->{dri}->$cmd($dom);
 return (undef,'Unable to find if domain name '.$dom.' exists') unless defined($e);
 return (undef,'Does domain name '.$dom.' exists at registry? '.($e? 'YES' : 'NO'));
}

sub do_domain_transfer
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 build_auth($rh);
 build_duration($ctx,$rh);
 $rh->{contact}=build_contactset($ctx,$rh->{contact}) if exists $rh->{contact}; ## Some registries need contacts during transfer, this is not core EPP, but it does not create drawbacks, so we support it here
 return wrap_command_domain($ctx,$cmd,$ra->[0],$rh);
}

sub do_domain_update
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $dom=shift(@$ra);
 my $toc=$ctx->{dri}->local_object('changes');
 my ($radd,$rdel,$rset)=build_update($ctx,$rh);
 foreach my $k (keys %$radd) { $toc->add($k,$radd->{$k}); }
 foreach my $k (keys %$rdel) { $toc->del($k,$rdel->{$k}); }
 foreach my $k (keys %$rset) { $toc->set($k,$rset->{$k}); }
 return wrap_command_domain($ctx,$cmd,$dom,$toc);
}

sub do_domain_update_ns
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $dom=shift(@$ra);
 my $ns=build_hosts($ctx,$ra);
 return wrap_command_domain($ctx,$cmd,$dom,$ns);
}

sub do_domain_update_status
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $dom=shift(@$ra);
 my $s=build_status($ctx,$ra);
 return wrap_command_domain($ctx,$cmd,$dom,$s);
}

sub do_domain_update_contact
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $dom=shift(@$ra);
 my $cs=$ctx->{dri}->local_object('contactset');
 while(my ($type,$ids)=each(%$rh))
 {
  foreach my $id (ref($ids)? @$ids : ($ids))
  {
   $cs->add($ctx->{dri}->local_object('contact')->srid($id),$type);
   $ctx->{completion}->{contacts}->{$id}=[time(),$ctx->{dri}->registry_name()];
  }
 }
 return wrap_command_domain($ctx,$cmd,$dom,$cs);
}

sub do_domain_create
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $dom=shift(@$ra);
 build_duration($ctx,$rh);
 build_auth($rh);
 $rh->{ns}=build_hosts($ctx,[split(/\s+/,ref $rh->{ns} ? join(' ',@{$rh->{ns}}) : $rh->{ns})]) if exists($rh->{ns});
 my @ct=qw/registrant admin tech billing/; ## How to retrieve non core contact types ?
 @ct=('registrant',$ctx->{dri}->protocol()->core_contact_types()) if ($ctx->{dri}->protocol() && $ctx->{dri}->protocol()->can('core_contact_types'));
 my %c;
 foreach my $t (@ct)
 {
  next unless exists $rh->{$t};
  $c{$t}=$rh->{$t};
  delete $rh->{$t} ;
 }
 $rh->{contact}=build_contactset($ctx,\%c) if (%c);
 $rh->{pure_create}=1;
 return wrap_command_domain($ctx,$cmd,$dom,$rh);
}

sub do_domain_renew
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $dom=shift(@$ra);
 build_duration($ctx,$rh);
 if (exists($rh->{current_expiration}))
 {
  my @t=split(/-/,$rh->{current_expiration});
  $rh->{current_expiration}=$ctx->{dri}->local_object('datetime','year' => $t[0], 'month' => $t[1], 'day' => $t[2]);
 }
 return wrap_command_domain($ctx,$cmd,$dom,$rh);
}

sub do_domain_delete
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my $dom=shift(@$ra);
 $rh->{pure_delete}=1;
 return wrap_command_domain($ctx,$cmd,$dom,$rh);
}

sub do_host
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my @p;
 if ($cmd eq 'host_create')
 {
  @p=build_hosts($ctx,$ra);
 } elsif ($cmd=~m/^host_update_ip_(?:add|del|set)$/)
 {
  my $h=shift(@$ra);
  @p=($h,build_hosts($ctx,[ $h, @$ra ]));
 } elsif ($cmd=~m/^host_update_status_(?:add|del|set)$/)
 {
  my $h=shift(@$ra);
  @p=($h,build_status($ctx,$ra));
 } elsif ($cmd eq 'host_update')
 {
  my $h=shift(@$ra);
  my $toc=$ctx->{dri}->local_object('changes');
  my ($radd,$rdel,$rset)=build_update($ctx,$rh);
  if (keys %$radd) { foreach my $k (keys %$radd) { if ($k eq 'ip') { $radd->{$k}=build_hosts($ctx,[$h,ref $radd->{$k} ? @{$radd->{$k}} : ($radd->{$k})]); } $toc->add($k,$radd->{$k}); } }
  if (keys %$rdel) { foreach my $k (keys %$rdel) { if ($k eq 'ip') { $rdel->{$k}=build_hosts($ctx,[$h,ref $rdel->{$k} ? @{$rdel->{$k}} : ($rdel->{$k})]); } $toc->del($k,$rdel->{$k}); } }
  if (keys %$rset) { foreach my $k (keys %$rset) { $toc->set($k,$rset->{$k}); } }
  $ctx->{completion}->{hosts}->{$rset->{'name'}}=time() if exists $rset->{'name'};
  @p=($h,$toc);
 } else
 {
  @p=@$ra;
 }
 $ctx->{completion}->{hosts}->{$p[0]}=time();
 $ctx->{completion}->{hosts}->{$p[1]}=time() if $cmd eq 'host_update_name_set';
 return ($ctx->{dri}->$cmd(@p),undef);
}

sub do_contact
{
 my ($ctx,$cmd,$ra,$rh)=@_;
 my @p;
 my $c=$ctx->{dri}->local_object('contact');
 build_auth($rh);
 if ($cmd eq 'contact_create')
 {
  $rh->{street}=[$rh->{street}] if (exists($rh->{street}) && !ref($rh->{street}));
  $rh->{srid}=$rh->{id} if (exists($rh->{id}) && ! exists($rh->{srid}));
  $rh->{srid}=$ra->[0] if (@$ra && $ra->[0]!~m/=/ && ! exists $rh->{srid});
  build_contact($ctx,$c,$rh);
 } elsif ($cmd=~m/^contact_update_status_(?:add|del|set)$/)
 {
  my $id=shift(@$ra);
  $c->srid($id);
  $ctx->{completion}->{contacts}->{$id}=[time(),$ctx->{dri}->registry_name()];
  @p=(build_status($ctx,$ra));
 } elsif ($cmd eq 'contact_update')
 {
  my $id=shift(@$ra);
  $c->srid($id);
  $ctx->{completion}->{contacts}->{$id}=[time(),$ctx->{dri}->registry_name()];
  my ($radd,$rdel,$rset)=build_update($ctx,$rh);
  my $toc=$ctx->{dri}->local_object('changes');
  if (keys %$rset)
  {
   my $c2=$ctx->{dri}->local_object('contact');
   build_contact($ctx,$c2,$rset);
   $toc->set('info',$c2);
  }
  if (keys %$radd) { foreach my $k (keys %$radd) { $toc->add($k,$radd->{$k}); } }
  if (keys %$rdel) { foreach my $k (keys %$rdel) { $toc->del($k,$rdel->{$k}); } }
  @p=($toc);
 } else
 {
  my $id=shift(@$ra);
  $c->srid($id);
  $ctx->{completion}->{contacts}->{$id}=[time(),$ctx->{dri}->registry_name()];
  @p=@$ra;
 }
 return ($ctx->{dri}->$cmd($c,@p),undef);
}

####################################################################################################

sub wrap_command_domain
{
 my $ctx=shift;
 my $cmd=shift;
 my $dom=shift;
 return (undef,'Undefined domain name') unless (defined($dom) && $dom);

 my ($fin,$fout,$res);

 if ($dom=~m!/!) ## Local file
 {
  return (undef,'Local file '.$dom.' does not exist or unreadable') unless (-e $dom && -r _);
  $res=$dom.'.'.$$.'.'.time().'.results'; ## TODO choose a predictable filename ? if so, use an option
  open($fin,'<',$dom)  or return (undef,'Unable to read local file '.$dom.' : '.$!);
  open($fout,'>',$res) or return (undef,'Unable to write (for results) local file '.$res.' : '.$!);
 } elsif ($dom=~m/`.+`/) ## Local executable
 {
  $dom=~s/`(.+)`/$1/;
  $res=$cmd.'.'.$$.'.'.time().'.results'; ## see above
  open($fin,'-|',$dom) or return (undef,'Unable to execute local command '.$dom.' : '.$!);
  open($fout,'>',$res) or return (undef,'Unable to write (for results) local file '.$res.' : '.$!);
 }

 unless (defined($fin) && defined($fout)) ## Pure unique domain name
 {
  $ctx->{completion}->{domains}->{$dom}=time();
  return (undef,'Invalid domain name: '.$dom) unless Net::DRI::Util::is_hostname($dom);
  return ($ctx->{dri}->$cmd(lc($dom),@_),undef);
 }

 my $withinfo=($cmd eq 'domain_check' || $cmd eq 'domain_info')? 1 : 0;
 my @rc;
 my $tstart=Time::HiRes::time();
 while(defined(my $l=<$fin>))
 {
  chomp($l);
  my @r=($l);
  $ctx->{completion}->{domains}->{$l}=time();
  if (Net::DRI::Util::is_hostname($l))
  {
   my $rc=$ctx->{dri}->$cmd(lc($l),@_);
   push @r,$rc->as_string(1);
   push @r,$ctx->{dri}->get_info_all() if $withinfo;
  } else
  {
   push @r,'Invalid domain name';
  }
  push @rc,\@r;
  output($ctx,'.');
 }
 my $tstop=Time::HiRes::time();
 output($ctx,"\n");
 close($fin);

 my %r;
 ## We write the whole file at the end for better performances (but we opened it right at the beginning to test its writability)
 foreach my $rc (@rc)
 {
  my $l=shift(@$rc);
  my $rcm=shift(@$rc);
  $rcm=~s/\n/ /g;
  if ($cmd eq 'domain_check')
  {
   my $rh=shift(@$rc);
   $rcm.=' | exist='.(defined $rh->{exist} ? $rh->{exist} : '?').' exist_reason='.(defined $rh->{exist_reason} ? $rh->{exist_reason} : ''); ## exist should always be defined !
  } elsif ($cmd eq 'domain_info')
  {
   my $rh=shift(@$rc);
   $rcm.=' | '.join(' ',map { $_.'=['.pretty_string($rh->{$_},0).']' } qw/clID crDate exDate contact ns status auth/);
   if (exists $rh->{ns})      { foreach my $nsname ($rh->{ns}->get_names()) { $ctx->{completion}->{hosts}->{$nsname}=time(); } }
   if (exists $rh->{contact}) { foreach my $cid ($rh->{contact}->get_all()) { $ctx->{completion}->{contacts}->{$cid}=[time(),$ctx->{dri}->registry_name()]; } }
  }
  print { $fout } $l,' ',$rcm,"\n";
  $r{$rcm}++;
 }
 close($fout);
 my $t=@rc;

 my $m=join("\n",map { sprintf('%d/%d (%.02f%%) : %s',$r{$_},$t,100*$r{$_}/$t,$_) } sort { $a cmp $b } keys(%r));
 $m.="\n".sprintf('%d operations in %d seconds, on average %.2f op/s = %.3f s/op',$t,$tstop-$tstart,$t/($tstop-$tstart),($tstop-$tstart)/$t); ## Warning, substring "on average" is used in handle_line(), do not change it
 $m.="\nResults in local file: $res";
 return (undef,$m);
}

####################################################################################################

sub build_contactset
{
 my ($ctx,$rh)=@_;
 my $cs=$ctx->{dri}->local_object('contactset');
 while(my ($t,$ids)=each(%$rh))
 {
  foreach my $c (ref($ids)? @{$ids} : ($ids))
  {
   $cs->add($ctx->{dri}->local_object('contact')->srid($c),$t);
   $ctx->{completion}->{contacts}->{$c}=[time(),$ctx->{dri}->registry_name()];
  }
 }
 return $cs;
}

sub build_contact
{
 my ($ctx,$c,$rh)=@_;
 no strict 'refs'; ## no critic (ProhibitNoStrict)
 while(my ($m,$v)=each(%$rh))
 {
  $c->$m($v);
 }
 if (exists $rh->{srid}) { $ctx->{completion}->{contacts}->{$rh->{srid}}=[time(),$ctx->{dri}->registry_name()]; }
 if (exists $rh->{id})   { $ctx->{completion}->{contacts}->{$rh->{id}}  =[time(),$ctx->{dri}->registry_name()]; }

 return $c;
}

sub build_status
{
 my ($ctx,$ra)=@_;
 my $s=$ctx->{dri}->local_object('status');
 foreach (@$ra) { s/^no//; $s->no($_); }
 return $s;
}

sub build_hosts
{
 my ($ctx,$ra)=@_;
 my $ns=$ctx->{dri}->local_object('hosts');
 my $i=-1;
 my @r;
 foreach my $o (@$ra)
 {
  $r[++$i]=[] if ($o=~m/[a-z]/i); ## new hostname (safe since at least the TLD is not numeric)
  push @{$r[$i]},$o if $i >= 0; ## the test here makes us skip IP addresses at beginning before first name (a situation that should not happen anyway)
 }
 foreach my $rns (@r)
 {
  my $name=shift(@$rns);
  $ns->add($name,$rns);
  $ctx->{completion}->{hosts}->{$name}=time();
 }
 return $ns;
}

sub build_auth
{
 my $rd=shift;
 return unless (exists($rd->{auth}) && ! ref($rd->{auth}));
 $rd->{auth}={ pw => $rd->{auth} };
}

sub build_duration
{
 my ($ctx,$rd)=@_;
 return unless exists($rd->{duration});
 my ($v,$u)=($rd->{duration}=~m/^(\d+)(\S+)$/);
 $rd->{duration}=$ctx->{dri}->local_object('duration','years'  => $v) if ($u=~m/^y(?:ears?)?$/i);
 $rd->{duration}=$ctx->{dri}->local_object('duration','months' => $v) if ($u=~m/^m(?:onths?)?$/i);
}

sub build_update
{
 my ($ctx,$rd)=@_;
 my (%a,%r);

 ## Some normalizations
 foreach my $k (grep { /^[+-]?status$/ } keys(%$rd)) { $rd->{$k}=build_status($ctx,ref $rd->{$k} ? $rd->{$k} : [ $rd->{$k} ]); }
 foreach my $k (grep { /^[+-]?ns$/ }     keys(%$rd)) { $rd->{$k}=build_hosts($ctx,[ map { split(/\s+/,$_) } ref $rd->{$k} ? @{$rd->{$k}} : ($rd->{$k})]); }
 build_auth($rd);

 my @ct=qw/admin tech billing/; ## How to retrieve non core contact types ?
 @ct=$ctx->{dri}->protocol()->core_contact_types() if ($ctx->{dri}->protocol() && $ctx->{dri}->protocol()->can('core_contact_types'));
 my $ctr=join('|',@ct);
 foreach my $op (qw/+ -/)
 {
  my %c;
  foreach my $k (grep { /^[${op}](?:${ctr})$/ } keys(%$rd) )
  {
   $c{substr($k,1)}=$rd->{$k};
   delete($rd->{$k});
  }
  next unless %c;
  $rd->{$op.'contact'}=build_contactset($ctx,\%c);
 }
 $rd->{registrant}=build_contact($ctx,$ctx->{dri}->local_object('contact'),{srid => $rd->{registrant}}) if exists $rd->{registrant};

 ## Now split in two hashes
 foreach my $k (grep { /^\+/ } keys(%$rd))
 {
  $a{substr($k,1)}=$rd->{$k};
  delete($rd->{$k});
 }
 foreach my $k (grep { /^-/ } keys(%$rd))
 {
  $r{substr($k,1)}=$rd->{$k};
  delete($rd->{$k});
 }

 return (\%a,\%r,$rd);
}

sub pretty_string
{
 my ($v,$full)=@_;
 $full||=0;
 unless(ref($v))
 {
  return '<undef>' unless defined($v);
  $v=~s/\s*$//;
  return $v unless ($v=~m/^<\?xml /);
  my $vi=Net::DRI::Util::xml_indent($v);
  $vi=~s/\n/\n\t\t/g;
  return $vi;
 }
 return join(' ',@$v) if (ref($v) eq 'ARRAY');
 return join(' ',map { $_.'='.$v->{$_} } keys(%$v)) if (ref($v) eq 'HASH');
 return ($full? "Ns:\n": '').$v->as_string(1) if ($v->isa('Net::DRI::Data::Hosts'));
 return ($full? "Contact:\n" : '').$v->as_string() if ($v->isa('Net::DRI::Data::Contact'));
 if ($v->isa('Net::DRI::Data::ContactSet'))
 {
  my @v;
  foreach my $t ($v->types())
  {
   push @v,$t.'='.join(',',map { pretty_string($_,$full) } $v->get($t));
  }
  return ($full? "ContactSet:\n" : '').join(' ',@v);
 }
 return ($full? "Status:\n" : '').join(' ',$v->list_status()) if ($v->isa('Net::DRI::Data::StatusList'));
 return ($full? "Command result:\n" : '').$v->as_string(1) if ($v->isa('Net::DRI::Protocol::ResultStatus'));
 return ($full? "Date:\n" : '').$v->set_time_zone('UTC')->strftime('%Y-%m-%d %T').' UTC' if ($v->isa('DateTime'));
 return ($full? "Duration:\n" : '').sprintf('P%dY%dM%dDT%dH%dM%dS',$v->in_units(qw/years months days hours minutes seconds/)) if ($v->isa('DateTime::Duration')); ## ISO8601
 return $v;
}

sub dump_info
{
 my ($ctx,$rh)=@_;
 my @r;
 foreach my $k1 (sort(keys(%$rh)))
 {
  foreach my $k2 (sort(keys(%{$rh->{$k1}})))
  {
   next if ($k1 eq 'session' && $k2 eq 'exchange' && $ctx->{config}->{verbose}==0);
   push @r,$k1.','.$k2;
   foreach my $k3 (sort(keys(%{$rh->{$k1}->{$k2}})))
   {
    push @r,"\t".$k3.': '.pretty_string($rh->{$k1}->{$k2}->{$k3},0);
   }
   push @r,'';
  }
 }
 return join("\n",@r);
}

####################################################################################################
1;