Source

dotfiles / bcvi / bcvi

   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
#!/usr/bin/perl
##############################################################################
#
# Script:   bcvi
#
# The 'Back-Channel vim' tool works with SSH to allow commands which are run
# on an SSH server to invoke processes back on the originating SSH client
# machine.
#
# Use 'bcvi --help' for the documentation
#
# Copyright (c) 2007-2010 Grant McLean <grantm@cpan.org>
#

use strict;
use warnings;

require 5.008;

##############################################################################
# This package implements some common functionality required by both the
# client and the server.
#
# It also serves as the entry-point for the command-line script.
##############################################################################

package App::BCVI;
BEGIN {
  $App::BCVI::VERSION = '3.05';
}

use File::Spec;
use File::Path;
use Getopt::Long qw();
use Encode       qw(encode decode);
use IO::Socket::INET;

my %class_map = (
    base   => 'App::BCVI',
    client => 'App::BCVI::Client',
    server => 'App::BCVI::Server',
    pod    => 'App::BCVI::POD',
);

my %response_message = (
    100 => "Ready ($App::BCVI::VERSION)",
    200 => "Success",
    300 => "Response follows",
    900 => "Permission denied",
    910 => "Unrecognised command",
);

my $LF = "\x0A";

my(
    %options, %option_name, %commands, @aliases, @installables,
    %plugin_loaded, @plugins,
);

run(@ARGV) unless caller();       # Don't run anything if loaded via 'require'

sub run {
    App::BCVI->base_init();

    App::BCVI->load_plugins();

    App::BCVI->base_class()->process_command_line(@_);

    exit;
}

sub version      { return $App::BCVI::VERSION; }
sub base_class   { return $class_map{base};    }
sub client_class { return $class_map{client};  }
sub server_class { return $class_map{server};  }
sub pod_class    { return $class_map{pod};     }
sub map_class    { $class_map{$_[1]} = $_[2];  }
sub sock         { shift->{sock};              }

sub installable_files { return @installables;     }
sub message_from_code { $response_message{$_[1]}; }

sub base_init {
    my($class) = @_;

    $class->register_option(
        name        => 'help',
        alias       => '?',
        dispatch_to => 'show_help',
        summary     => 'detailed help message',
        description => <<'END_POD'
Display this documentation.
END_POD
    );

    $class->register_option(
        name        => 'add-aliases',
        dispatch_to => 'add_aliases',
        summary     => 'install bcvi into shell startup files',
        description => <<'END_POD'
Edit the bash startup script to add (or update) the required command aliases
for bcvi.
END_POD
    );

    $class->register_option(
        name        => 'listener',
        alias       => 'l',
        dispatch_to => 'start_listener',
        summary     => 'start in listener mode',
        description => <<'END_POD'
Start a background listener process.  Also generates a new authentication key.
END_POD
    );

    $class->register_option(
        name        => 'install',
        arg_name    => '<hosts>',
        dispatch_to => 'install_to_hosts',
        summary     => 'copy bcvi to named hosts and install aliases',
        description => <<'END_POD'
Copy the C<bcvi> script to the C<$HOME/bin> directory on the specified host
and then remotely invoke it with the C<--add-aliases> option.
END_POD
    );

    $class->register_option(
        name        => 'unpack-term',
        dispatch_to => 'unpack_term',
        summary     => 'unpack the overloaded TERM variable',
        description => <<'END_POD'
This option is intended for use from a F<.profile> script.  It outputs a
snippet of shell script to be passed to C<eval> in the calling shell.
END_POD
    );

    $class->register_option(
        name        => 'wrap-ssh',
        alias       => 's',
        dispatch_to => 'wrap_ssh',
        summary     => 'pass all args after -- to ssh',
        description => <<'END_POD'
A wrapper around invoking ssh to connect to a specified host.  Ensures the
environment is set up to pass the authentication key and other data to the
C<bcvi> client on the remote server.
END_POD
    );

    $class->register_option(
        name        => 'version',
        alias       => 'v',
        dispatch_to => 'show_versions',
        summary     => 'display bcvi version number',
        description => <<'END_POD'
When invoking a command use this option to indicate that the arguments are not
filenames and the translation of relative pathnames to absolute should be
skipped.
END_POD
    );

    $class->register_option(
        name        => 'no-path-xlate',
        alias       => 'n',
        summary     => 'skip translation of args from relative to absolute',
        description => <<'END_POD'
Displays the version number of the bcvi client and if applicable, of the
listener process.
END_POD
    );

    $class->register_option(
        name        => 'port',
        alias       => 'p',
        arg_spec    => '=i',
        arg_name    => '<port>',
        summary     => 'port number for listener/port-forward',
        description => <<'END_POD'
When used with C<--listener>, this is the port the server process will listen
on.  When used with C<--wrap-ssh> this is the port number on the remote machine
that will be forwarded back to the listener process.  The default in both cases
is calculated using the user's numeric UID multiplied by 10 and added to 9.
The intent is to reduce the chance of collisions with other bcvi users.
END_POD
    );

    $class->register_option(
        name        => 'command',
        alias       => 'c',
        arg_spec    => '=s',
        arg_name    => '<cmnd>',
        summary     => 'command to send over back-channel',
        description => <<'END_POD'
Use C<cmnd> as the command to send over the back-channel (default: vi).
Recognised commands are described in L<COMMANDS> below.
END_POD
    );

    $class->register_option(
        name        => 'reuse-auth',
        summary     => "don't generate a new auth key on listener startup",
        description => <<'END_POD'
A new (random) authorisation key is generated when the listener process is
started - this will invalidate the keys in use by existing SSH sessions.
This option is for use when it is necessary to restart the listener process
without invalidating client keys.
END_POD
    );

    $class->register_option(
        name        => 'plugin-help',
        arg_spec    => '=s',
        arg_name    => '<plugin>',
        dispatch_to => 'plugin_help',
        summary     => "display documentation for <plugin>",
        description => <<'END_POD'
The --help output includes a list of installed plugins.  Use this option to
read the documentation for a named plugin.
END_POD
    );


    $class->register_command(
        name        => 'vi',
        description => <<'END_POD'
Invokes C<gvim> on the remote file - after translating the host+path to
an scp URI.  This is the default command if no C<--command> option is
specified.  If multiple filenames are supplied, the first will be opened
in gvim and you should use C<:n> to load the 'next' file.
END_POD
    );

    $class->register_command(
        name        => 'viwait',
        description => <<'END_POD'
This command works exactly the same as C<vi> above, except it waits for the
editor process to exit before bcvi exits on the remote machine.  This is
primarily for use with C<sudoedit>.  Note: when used with C<sudoedit>, the file
will not be updated on the remote machine until you exit the editor on your
workstation.
END_POD
    );

    $class->register_command(
        name        => 'scpd',
        description => <<'END_POD'
Uses C<scp> to copy the specified files or directories to the calling user's
F<~/Desktop>.`
END_POD
    );


    $class->add_home_bin();
    $class->register_aliases(
        'test -n "$(which bcvi)" && eval "$(bcvi --unpack-term)"',
        'test -n "${BCVI_CONF}"  && alias vi="bcvi"',
        'test -n "${BCVI_CONF}"  && alias suvi="EDITOR=\'bcvi -c viwait\' sudoedit"',
        'test -n "${BCVI_CONF}"  && alias bcp="bcvi -c scpd"',
    );

    $class->pod_class->init();

}


sub register_option {
    my $class = shift;
    my $opt   = { @_ };
    my $key   = $opt->{name};

    if(!defined $key or !length $key) {
        die "Can't register option without 'name'";
    }

    my($package, $filename, $line) = caller();
    $opt->{provider} = "$package at $filename line $line";
    my $taken = $options{$key};
    if($taken && !$opt->{force_override}) {
        warn "option '--$key' already registered by $taken->{provider}\n";
    }
    if($opt->{alias}) {
        foreach my $a (map { s/^-+//; $_ } split /\|/, $opt->{alias}) {
            if($option_name{$a} && !$opt->{force_override}) {
                if($taken = $options{$option_name{$a}}) {
                    warn "alias '$a' already registered for option "
                       . "'--$taken->{name}' by $taken->{provider}\n";
                }
            }
            $option_name{$a} = $key;
        }
    }
    $options{$key} = $opt;
}


sub register_command {
    my $class = shift;
    my $cmd   = { @_ };
    my $key   = $cmd->{name};

    if(!defined $key or !length $key) {
        die "Can't register command without 'name'";
    }

    $cmd->{dispatch_to} ||= "execute_$key";

    my($package, $filename, $line) = caller();
    $cmd->{provider} = "$package at $filename line $line";
    warn "option '$key' already registered by $commands{$key}->{provider}\n"
        if $commands{$key} && !$cmd->{force_override};
    $commands{$key} = $cmd;
}


sub each_option {
    my($class, $sub) = @_;

    $sub->($options{$_}) foreach sort keys %options;
}


sub each_command {
    my($class, $sub) = @_;

    $sub->($commands{$_}) foreach sort keys %commands;
}


sub command_handler {
    my($class, $name) = @_;

    return unless defined $name;
    return "execute_commands_pod" if $name eq 'commands_pod';
    my $spec = $commands{$name} or return;
    return $spec->{dispatch_to};
}


sub add_home_bin {
    my $class = shift;
    my $home_bin = $class->home_directory . '/bin';
    $class->register_aliases(
        qq{echo \$PATH | grep -q $home_bin || PATH="$home_bin/\$PATH"},
    );
}


sub register_aliases {
    my $class = shift;
    push @aliases, @_;
}


sub register_installable {
    my $class = shift;
    my($package, $filename, $line) = caller();
    push @installables, $filename;
}


sub shell_aliases {
    my($self) = @_;

    return
        "## START-BCVI\n"
        . join("\n", map { "  $_" } @aliases)
        . "\n## END-BCVI\n";
}


sub load_plugins {
    my($class) = @_;

    my $dir = $class->conf_directory();
    foreach my $file (sort glob("$dir/*.pm")) {
        $class->load_plugin_file($file);
    }
}


sub load_plugin_file {
    my($class, $file) = @_;

    my @parts = File::Spec->splitdir($file);
    my $key = pop @parts;
    return if $plugin_loaded{$key};

    eval { require $file; };
    if($@) {
        die qq{Error loading plugin "$file"\n$@\n}
    }

    $plugin_loaded{$key} = $file;
}


sub hook_client_class {
    my($class) = @_;

    my($calling_class, $calling_file) = caller();
    my $client_class = $class->client_class();
    $class->map_class(client => $calling_class);

    no strict 'refs';
    unshift @{"${calling_class}::ISA"}, $client_class;
    push @plugins, { class => $calling_class, file => $calling_file };
    return 1;
}


sub hook_server_class {
    my($class) = @_;

    my($calling_class, $calling_file) = caller();
    my $server_class = $class->server_class();
    $class->map_class(server => $calling_class);

    no strict 'refs';
    unshift @{"${calling_class}::ISA"}, $server_class;
    push @plugins, { class => $calling_class, file => $calling_file };
    return 1;
}


sub process_command_line {
    my($class, @args) = @_;

    my $opt = $class->option_defaults();
    my @cfg = $class->getopt_config();

    local(@ARGV) = @args;
    Getopt::Long::GetOptions($opt, @cfg) or $class->die_synopsis();

    my $handler = $opt->{listener}
                ? $class->server_class
                : $class->client_class;

    $handler->new(_options => $opt)->dispatch(@ARGV);
}


sub option_defaults {
    return { };
}


sub getopt_config {
    my($class) = @_;

    my @spec;
    $class->each_option(sub {
        my($o) = @_;
        my $def = $o->{name};
        $def .= "|$o->{alias}" if defined $o->{alias};
        $def .= $o->{arg_spec} if defined $o->{arg_spec};
        push @spec, $def;
    });
    return @spec
}


sub die_synopsis {
    my($class, $message) = @_;

    warn "$message\n" if $message;
    $class->pod_class->synopsis();
    exit(1);
}


sub new {
    my $class = shift;

    return bless { @_ }, $class;
}


sub dispatch {
    my($self, @args) = @_;

    if(my $method = $self->dispatch_option) {
        $self->$method(@args);
        exit;
    }
    elsif(!$self->opt('command') and !@args) {
        $self->die_synopsis();
    }

    $self->send_command(@args);
}


sub opt {
    my($self, $key) = @_;

    return $self->{_options}->{$key};
}


sub set_opt {
    my($self, $key, $value) = @_;

    return $self->{_options}->{$key} = $value;
}


sub dispatch_option {
    my($self) = @_;

    my @set;
    $self->each_option(sub {
        my($o) = @_;
        push @set, $o if $o->{dispatch_to} && defined $self->opt($o->{name});
    });
    return unless @set;
    if(@set > 1) {
        @set = map { "--$_->{name}" } @set;
        my $last = pop @set;
        $self->die_synopsis(
            "Which did you want: " . join(', ', @set) . " or $last?"
        );
    }
    return $set[0]->{dispatch_to};
}


sub default_port {
    return( ($< * 10 + 9) % 65536 );
}


sub listen_address {
    return 'localhost';
};


sub default_command {
    return 'vi';
}


sub read_file {
    my($self, $path) = @_;

    return unless -e $path;
    return if -d $path;
    my $data = do {
        open my $fh, '<', $path or die "open($path): $!\n";
        local($/) = undef;
        <$fh>;
    };
    return $data;
}


sub home_directory {
    return (getpwuid($>))[7];
}


sub conf_directory {
    my($self) = @_;

    my $conf_dir = File::Spec->catdir($self->home_directory(), '.config', 'bcvi');
    File::Path::mkpath($conf_dir) unless -d $conf_dir;
    return $conf_dir;
}


sub auth_key_filename {
    return File::Spec->catfile(shift->conf_directory(), 'listener_key');
}


sub listener_port_filename {
    return File::Spec->catfile(shift->conf_directory(), 'listener_port');
}


sub get_listener_auth_key {
    my($self) = @_;

    my $auth_file = $self->auth_key_filename();
    my $auth_key = $self->read_file($auth_file)
        or die "Auth key file does not exist: $auth_file";
    chomp($auth_key);
    return $auth_key;
}


sub list_plugins {
    my($self) = @_;

    my @plugins;
    foreach my $name (sort keys %plugin_loaded) {
        my $path = $plugin_loaded{$name};
        $name =~ s{[.]pm$}{};
        if(my $title = $self->pod_class->extract_title($path)) {
            push @plugins, "$name - $title";
        }
        else {
            push @plugins, "$name - no documentation";
        }
    }
    return @plugins;
}


sub plugin_help {
    my($self) = @_;

    my $plugin = $self->opt('plugin-help');
    if(my $path = $plugin_loaded{"${plugin}.pm"}) {
        $self->pod_class->show_plugin_help($path);
    }
    else {
        die "Can't find plugin: '$plugin'\n";
    }
}


##############################################################################
# The App::BCVI::Client class implements the command-line UI and the client
# side of the BCVI protocol.  You can use inheritance to customise the
# behaviour of this class.
##############################################################################

package App::BCVI::Client;
BEGIN {
  $App::BCVI::Client::VERSION = '3.05';
}

BEGIN { @App::BCVI::Client::ISA = qw( App::BCVI ); }


sub get_connection_details {
    my($self) = @_;

    if(not $ENV{BCVI_CONF}) {
        die "The \$BCVI_CONF environment variable has not been set\n";
    }
    my($alias, $gateway, $port, $auth_key) = split(/:/, $ENV{BCVI_CONF});
    $self->{host_alias}      = $alias;
    $self->{gateway_address} = $gateway;
    $self->{port}            = $port;
    $self->{auth_key}        = $auth_key;
}


sub host_alias       { return shift->{host_alias};       }
sub gateway_address  { return shift->{gateway_address};  }
sub port             { return shift->{port};             }
sub auth_key         { return shift->{auth_key};         }
sub server_version   { return shift->{server_version};   }
sub response_code    { return shift->{response_code};    }
sub response_message { return shift->{response_message}; }
sub response         { return shift->{response};         }


sub send_command {
    my($self, @files) = @_;

    my $command = $self->opt('command') || $self->default_command();

    my $sock = $self->connect_to_listener();

    $sock->write(
        "Auth-Key: "   . $self->auth_key   . $LF .
        "Host-Alias: " . $self->host_alias . $LF .
        "Command: "    . $command          . $LF
    ) or die "Error sending command through backchannel: $!";

    $self->send_body(\@files);
    return if $self->check_response() =~ /^(?:200|300)$/;
    die $self->response_message . "\n";
}


sub send_body {
    my($self, $files) = @_;

    my $translate_paths = ! $self->opt('no-path-xlate');

    my $body = join '', map {
        $_ = File::Spec->rel2abs($_) if $translate_paths;
        "$_$LF";
    } @$files;

    $self->sock->write(
        'Content-Length: ' . length($body) . $LF .
        $LF .
        $body
    ) or die "Error sending command through backchannel: $!";
}


sub check_response {
    my($self) = @_;

    my $response = $self->sock->getline() or die "Server hung up\n";
    if(my($code, $message) = $response =~ m{^(\d\d\d) (.*)$}) {
        ($self->{response_code}, $self->{response_message}) = ($code, $message);
        $self->read_response() if $code eq '300';
        return $code;
    }
    die "Unexpected response: '$response'\n";
}


sub read_response {
    my($self) = @_;

    my $sock = $self->sock();
    my $resp = {};
    while(my($line) = $sock->getline() || '') {
        chomp($line);
        last if $line eq '';
        $line = Encode::decode('utf8', $line);
        if(my($name, $value) = $line =~ m{^(\S+)\s*:\s*(.*)$}) {
            $name =~ s/-/_/;
            $resp->{lc($name)} = $value;
        }
    }
    $self->{response} = $resp;

    my $bytes = $resp->{content_length} || return;;

    my $buf = '';
    while(my $count = $sock->read($buf, $bytes, length($buf))) {
        $bytes -= $count;
        last if $bytes < 1;
    }
    $resp->{body} = $buf;
}


sub connect_to_listener {
    my($self) = @_;

    $self->get_connection_details();

    my $peer = $self->gateway_address . ':' . $self->port;
    my $sock = IO::Socket::INET->new(
        PeerAddr => $peer,
    ) or die "Can't connect to '$peer': $!\n";

    binmode($sock);
    my $welcome = $sock->getline() or die "No listener?\n";
    if($welcome =~ /^100 Ready \(([^)]+)\)/) {
        $self->{server_version} = $1;
    }
    $self->{sock} = $sock;
}


sub show_versions {
    my($self) = @_;

    print "bcvi client: $App::BCVI::VERSION\n";

    if($ENV{BCVI_CONF}) {
        $self->connect_to_listener();
        print "bcvi server: $App::BCVI::VERSION\n";
    }
}


sub show_help {
    my($self) = @_;

    $self->pod_class->full_pod($self);
}


sub commands_pod {
    my($self) = @_;

    eval {
        $self->set_opt(command => 'commands_pod');
        $self->send_command();
    };
    if($@) {
        $@ = '';
        return;
    }
    return $self->response->{body};
}


sub wrap_ssh {
    my($self, @args_in) = @_;

    if(my($target, @args_out) = $self->parse_ssh_args(@args_in)) {
        $self->execute_wrapped_ssh($target, @args_out);
    }
    else {
        warn "bcvi: unable to extract hostname from ssh command line\n";
        $self->execute_ssh(@args_in);
    }
}


sub parse_ssh_args {
    my($self, @args_in) = @_;

    my %need_arg = map { $_ => 1} split //, 'bcDeFiLlmOopRS';

    my(@args_out, @hosts, $user);
    while(@args_in) {
        $_ = shift @args_in;
        if(/^-l(.*)$/) {             # extract username if specified with -l
            $user = $1 ? $1 : $args_in[0];
        }
        if(/^-(.)(.*)$/) {
            push @args_out, $_;
            push @args_out, shift @args_in
                if $need_arg{$1} && !length($2) && @args_in;
        }
        else {
            push @args_out, $_;
            push @hosts,    $_;
        }
    }
    return unless @hosts == 1;
    my($target) = @hosts;
    if($user and $target !~ /@/) {
        $target = $user . '@' . $target;
    }
    return($target, @args_out);
}


sub execute_wrapped_ssh {
    my($self, $target, @args) = @_;

    my $remote_port = $self->opt('port') || $self->default_port();
    my $local_port  = $self->listener_port();
    $ENV{TERM} = "$ENV{TERM}\n"
                 . "BCVI_CONF=${target}:localhost:$remote_port:"
                 . $self->get_listener_auth_key();
    unshift @args, "-R $remote_port:localhost:$local_port";
    $self->execute_ssh(@args);
}


sub execute_ssh {
    my($self, @args) = @_;

    system 'ssh', @args;
}


sub listener_port {
    my($self) = @_;

    my $port = $self->read_file($self->listener_port_filename())
        or return $self->default_port();
    chomp($port);
    return $port;
}


sub unpack_term {
    my($self) = @_;

    my @parts = split /\x0D?\x0A/, $ENV{TERM} || '';
    return unless @parts > 1;
    print "TERM=$parts[0]\n";
    shift @parts;
    foreach (@parts) {
        print if s{^(\w+)=(.*)$}{export $1="$2"\n};
    }
}


sub install_to_hosts {
    my($self, @args) = @_;

    die "You must list one or more target hostnames\n" unless @args;

    $self->install_to_host($_) foreach @args;
}


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

    $self->install_bin_directory($host);
    $self->install_bcvi_script($host);
    $self->install_plugins($host);
    $self->install_remote_aliases($host);
}


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

    if(system("ssh $host test -d ./bin") != 0) {
        print "Creating ~/bin directory on $host\n";
        system("ssh $host mkdir ./bin") == 0
            or die "** mkdir failed on $host";
    }
}


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

    print "Copying bcvi to remote bin directory on $host\n";
    my $output = `scp $0 $host:bin/bcvi 2>&1`;
    if($? != 0) {
        die "** failed to copy bcvi to remote bin directory on $host\n"
            . $output;
    }
}


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

    return unless @installables;
    if(system("ssh $host test -d ./.config/bcvi") != 0) {
        print "Creating plugins directory on $host\n";
        system("ssh $host mkdir -p ./.config/bcvi") == 0
            or die "** mkdir failed on $host";
    }
    print "Copying plugin files to $host\n";
    my $output = `scp @installables $host:.config/bcvi 2>&1`;
    if($? != 0) {
        die "** failed to copy bcvi to remote plugins directory on $host\n"
            . $output;
    }
}


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

    system("ssh $host bin/bcvi --add-aliases") == 0
        or die "** failed to install aliases on $host";
}


sub add_aliases {
    my($self) = @_;

    my $bcvi_commands = $self->shell_aliases();

    $self->update_existing_aliases($bcvi_commands)
    or $self->aliases_initial_install($bcvi_commands);
}


sub update_existing_aliases {
    my($self, $bcvi_commands) = @_;

    foreach my $file ( $self->candidate_rc_files() ) {
        my($script) = $self->read_file($file) or next;
        if(index($script, $bcvi_commands) > -1) {
            print "Found bcvi commands in $file\n";
            return 1;
        }
        if($script =~ s{^## START-BCVI.*^## END-BCVI\r?\n}{$bcvi_commands}sm) {
            open my $fh, '>', $file or die "open($file): $!";
            print $fh $script;
            close($fh);
            print "Updated bcvi commands in $file\n";
            return 1;
        }
        if($script =~ m{^[^#]*\bbcvi\b}m) {
            print "Adhoc bcvi commands found in $file\n"
                  . "*** Manual update may be required.            ***\n"
                  . "*** Consider deleting commands and re-adding. ***\n";
            return 1;
        }
    }
    return;   # No existing aliases found
}


sub aliases_initial_install {
    my($self, $bcvi_commands) = @_;

    my $target = $self->preferred_rc_file();

    open my $fh, '>>', $target or die "open(>>$target): $!";
    print $fh "\n$bcvi_commands\n";
    close($fh);
    print "Added bcvi commands to $target\n";
}


sub candidate_rc_files {
    my($self) = @_;
    my $home = $self->home_directory();
    return(
        "$home/.bashrc_local",
        "$home/.bashrc",
        "$home/.bash_profile_local",
        "$home/.bash_profile",
        "$home/.profile",
        "$home/.common-configs/bashrc",
    );
}


sub preferred_rc_file {
    my($self) = @_;

    # Add to .bashrc_local if it is referenced from .bashrc

    my $home = $self->home_directory();
    my $bashrc       = "$home/.bashrc";
    my $bashrc_local = "$home/.bashrc_local";

    my $script = $self->read_file($bashrc) || '';

    return $script =~ m{/[.]bashrc_local\b}
           ? $bashrc_local
           : $bashrc;
}


##############################################################################
# The App::BCVI::Server class implements the server ('listener') side of the
# BCVI protocol.  You can use inheritance to customise the behaviour of this
# class.
##############################################################################

package App::BCVI::Server;
BEGIN {
  $App::BCVI::Server::VERSION = '3.05';
}

BEGIN { @App::BCVI::Server::ISA = qw( App::BCVI ); }

use Digest::MD5  qw(md5_hex);
use Errno        qw();


sub start_listener {
    my($self) = @_;

    $self->kill_current_listener();
    $self->save_pid();
    $self->generate_auth_key();
    $self->create_listener_socket();
    $self->accept_loop();
    die "bcvi listener accept loop terminated unexpectedly\n";
}


sub auth_key     { shift->{auth_key}; }
sub client_sock  { shift->{client};   }
sub request      { shift->{request};  }
sub calling_host { shift->{request}->{host_alias};  }


sub kill_current_listener {
    my($self) = @_;

    my($pid) = $self->read_file($self->pid_file) or return;
    foreach my $i (1..5) {
        if(kill 0, $pid) {
            kill($i > 2 ? 9 : 1, $pid);
        }
        elsif($!{ESRCH}) {   # no such process
            return;
        }
        elsif($!{EPERM}) {   # pid file was probably stale
            return;
        }
        sleep 1;
    }
}


sub save_pid {
    my($self) = @_;

    my $pid_file = $self->pid_file;
    open my $fh, '>', $pid_file or die "open(>$pid_file): $!";
    print $fh "$$\n";
}


sub save_port {
    my($self, $port) = @_;

    my $port_file = $self->listener_port_filename;
    open my $fh, '>', $port_file or die "open(>$port_file): $!";
    print $fh "$port\n";
}


sub pid_file {
    return File::Spec->catfile(shift->conf_directory(), 'listener_pid');
}


sub generate_auth_key {
    my($self) = @_;

    if($self->opt('reuse-auth')) {
        $self->{auth_key} = $self->get_listener_auth_key();
        return;
    }

    my $data = "$self" . $$ . time() . rand();
    $self->{auth_key} = md5_hex($data);

    my $auth_file = $self->auth_key_filename();
    open my $fh, '>', $auth_file or die "open(>$auth_file): $!";
    print $fh $self->{auth_key}, "\n";
}


sub create_listener_socket {
    my($self) = @_;

    my $port = $self->opt('port') || $self->default_port();
    $self->save_port($port);
    my $local_addr = $self->listen_address . ':' .  $port;
    $self->{sock} = IO::Socket::INET->new(
        LocalAddr => $local_addr,
        ReuseAddr => 1,
        Proto     => 'tcp',
        Listen    => 5,
        Blocking  => 1,
    ) or die "Error creating listener for port '$local_addr': $!";
}


sub accept_loop {
    my($self) = @_;

    $SIG{CHLD} = 'IGNORE';  # let Perl reap the zombies

    my $sock = $self->sock();
    while(1) {
        my $new = $sock->accept();
        next if $!{EINTR};
        if(fork()) {  # In parent
            close $new;
        }
        else {        # In child
            close $sock;
            $self->{sock} = $new;
            $self->dispatch_request();
            exit(0);
        }
    }
}


sub dispatch_request {
    my($self) = @_;

    $self->send_response(100);
    my $req = $self->collect_headers();
    $self->validate_auth_key($req->{auth_key})
        or $self->exit_response(900);
    my $method = $self->command_handler($req->{command})
        or $self->exit_response(910);
    $self->$method();
    $self->send_response(200);
}


sub validate_auth_key {
    my($self, $key) = @_;

    return 1 if $key && $key eq $self->auth_key;
    my $alias = $self->calling_host();
    warn "Invalid Auth-Key in request from $alias\n" if -t 2;
    return;
}


sub send_response {
    my($self, $code) = @_;

    my $message = $self->message_from_code($code) || 'Invalid response code';
    $message = Encode::encode('utf8', $message);
    $self->sock->write(qq{$code $message\x0A});
}


sub exit_response {
    my($self, $code) = @_;

    $self->send_response($code);
    exit(0);
}


sub collect_headers {
    my($self) = @_;

    my $sock = $self->sock();
    my $req  = {};
    while(my($line) = $sock->getline() || '') {
        chomp($line);
        last if $line eq '';
        $line = Encode::decode('utf8', $line);
        if(my($name, $value) = $line =~ m{^(\S+)\s*:\s*(.*)$}) {
            $name =~ s/-/_/;
            $req->{lc($name)} = $value;
        }
    }
    $self->{request} = $req;
}


sub read_request_body {
    my($self) = @_;

    my $bytes = $self->request->{content_length};
    my $sock  = $self->sock();
    my $buf   = '';
    while(my $count = $sock->read($buf, $bytes, length($buf))) {
        $bytes -= $count;
        last if $bytes < 1;
    }
    return $buf;
}


sub get_filenames {
    my($self) = @_;

    return split /\x0a/, Encode::decode('utf8', $self->read_request_body());
}


sub execute_commands_pod {
    my($self) = @_;

    $self->send_response(300);
    my $pod = $self->pod_class->command_detail();
    $pod = Encode::encode('utf8', $pod);
    $self->sock->write(
        'Content-Type: text/pod'          . $LF .
        'Content-Length: ' . length($pod) . $LF .
        $LF .
        $pod
    ) or die "Error sending response body: $!";
}


sub execute_vi {
    my($self) = @_;

    my $alias = $self->calling_host();
    my @files = map { "scp://$alias/$_" } $self->get_filenames();
    system('mvim', '--', @files);
}


sub execute_viwait {
    my($self) = @_;

    my $alias = $self->calling_host();
    my @files = map { "scp://$alias/$_" } $self->get_filenames();
    system('mvim', '-f', '--', @files);
}


sub execute_scpd {
    my($self) = @_;

    my $dest = File::Spec->catdir($self->home_directory(), 'Desktop');

    my $alias = $self->calling_host();
    my @files = map { "$alias:$_" } $self->get_filenames();

    system('scp', '-q', '-r', '--', @files, $dest);
}


##############################################################################
# The App::BCVI::POD class implements POD extraction and formatting on
# platforms where POD::Text is available.
##############################################################################

package App::BCVI::POD;
BEGIN {
  $App::BCVI::POD::VERSION = '3.05';
}

use Pod::Text;

my $pod_skeleton;


sub init {
    my($class) = @_;

    $pod_skeleton = do {
        local($/) = undef;
        <DATA>;
    };
    close(DATA);
}


sub full_pod {
    my($class, $app) = @_;

    my $commands_pod;
    if($app && $app->can('commands_pod')) {
        $commands_pod = $app->commands_pod();
    }
    $commands_pod ||= $class->command_detail();
    my $plugins_pod = $class->plugins_pod($app);

    my $pager = $class->pager();
    my $pod   = $pod_skeleton;
    $pod =~ s{^=for BCVI_OPTIONS_SUMMARY\s*\n}{ $class->option_summary()}me;
    $pod =~ s{^=for BCVI_OPTIONS\s*\n}{ $class->option_detail() }me;
    $pod =~ s{^=for BCVI_COMMANDS\s*\n}{ $commands_pod }me;
    $pod =~ s{^=for BCVI_PLUGINS\s*\n}{ $plugins_pod }me;
    if(Pod::Text->isa('Pod::Simple')) {
        my $parser = Pod::Text->new (sentence => 1, width => 78);
        $parser->output_fh($pager);
        $parser->parse_string_document($pod);
    }
    else {
        open my $pod_fh, '<', \$pod or die "open(\$pod): $!";
        my $parser = Pod::Text->new (sentence => 1, width => 78);
        $parser->parse_from_filehandle($pod_fh, $pager);
    }
}


sub synopsis {
    my($class) = @_;

    my $pod = $pod_skeleton;
    $pod =~ s{\A.*?^=head1 SYNOPSIS\s*?\n}{Usage:}ms;
    $pod =~ s{^=.*\z}{}ms;
    $pod .= $class->option_summary();

    print $pod;
}


sub option_summary {
    my($class) = @_;

    my $w = 12;
    my @lines;
    App::BCVI->base_class->each_option(sub {
        my($o)    = @_;
        my $short = "--$o->{name}";
        $short    .= "|-$o->{alias}"   if $o->{alias};
        $short    .= " $o->{arg_name}" if $o->{arg_name};
        push @lines, [ $short, $o->{summary} ];
        $w = length($short) if length($short) > $w;
    });

    return join('', map { sprintf("  %-*s  %s\n", $w, @$_) } @lines) . "\n";
}


sub option_detail {
    my($class) = @_;

    my @lines = "=over 4\n";
    App::BCVI->base_class->each_option(sub {
        my($o)  = @_;
        my $pod = "\n=item B<--$o->{name}>";
        $pod .= " (alias: -$o->{alias})" if $o->{alias};
        $pod .= " $o->{arg_name}"        if $o->{arg_name};
        $pod .= "\n\n$o->{description}\n";
        push @lines, $pod;
    });
    push @lines, "\n=back\n\n";

    return join '', @lines;
}


sub command_detail {
    my($class) = @_;

    my @lines = "=over 4\n";
    App::BCVI->base_class->each_command(sub {
        my($c)  = @_;
        my $pod = "\n=item B<$c->{name}>";
        $pod .= "\n\n$c->{description}\n";
        push @lines, $pod;
    });
    push @lines, "\n=back\n\n";

    return join '', @lines;
}


sub plugins_pod {
    my($class, $app) = @_;

    if(my @plugin_list = $app->list_plugins()) {
        my $s = @plugin_list == 1 ? '' : 's';
        return join("\n\n",
            "You have the following plugin$s installed:",
            @plugin_list,
            "Use C<< bcvi --plugin-help plugin-name >> for detailed info.",
        ) . "\n\n";
    }

    return "You have no plugins installed.\n\n";
}


sub extract_title {
    my($class, $path) = @_;

    open my $fh, '<', $path or return;
    my $name_section = 0;
    while(<$fh>) {
        chomp;
        if(/^=head1\s+NAME$/) {
            $name_section++;
        }
        elsif($name_section  and  /\S/) {
            s{^.+?\s-\s}{};
            return $_;
        }
    }
    return;
}


sub show_plugin_help {
    my($class, $path) = @_;

    my $pager = $class->pager();
    if(Pod::Text->isa('Pod::Simple')) {
        my $parser = Pod::Text->new (sentence => 1, width => 78);
        $parser->output_fh($pager);
        $parser->parse_file($path);
    }
    else {
        open my $pod_fh, '<', $path or die "open($path): $!";
        my $parser = Pod::Text->new (sentence => 1, width => 78);
        $parser->parse_from_filehandle($pod_fh, $pager);
    }
}


sub pager {
    my @commands = $ENV{PAGER} ? ( $ENV{PAGER} ) : qw(pager less more);
    foreach my $file (@commands) {
        foreach my $dir ( File::Spec->path() ) {
            my $exe_path = File::Spec->catfile($dir, $file);
            if(-x $exe_path) {
                open my $fh, '|-', $exe_path or next;
                return $fh;
            }
        }
    }
    return \*STDOUT;
}


1;

__DATA__

=head1 NAME

bcvi - Back-channel vi, a shell utility to proxy commands back over ssh

=head1 SYNOPSIS

  bcvi [options] [<files>]

  Options:

=for BCVI_OPTIONS_SUMMARY

=head1 DESCRIPTION

This utility works with SSH to allow commands issued on the SSH server host to
be 'proxied' back to the SSH client host.  For example:

=over 4

=item *

user F<sally> establishes an SSH connection from her workstation to a server
named F<pluto> and runs the command C<bcvi .bashrc>

=item *

bcvi tunnels the details back to sally's workstation which then invokes the
command C<gvim scp://pluto//home/sally/.bashrc>

=item *

the result is that sally gets a responsive GUI editor running on her local
machine, but editing a file on the remote machine

=back

See C<< perldoc App::BCVI >> for more examples and background information.

=head1 OPTIONS

=for BCVI_OPTIONS

=head1 COMMANDS

The following commands can be passed back to the listener process.

=for BCVI_COMMANDS

=head1 USING BCVI

You'll need to start a listener process on your workstation (perhaps from
your window manager session startup).

  bcvi -l &

To install the bcvi client to a remote machine:

  bcvi --install <hostname>

To ssh to a server with tunnelling enabled:

  bcvi --wrap-ssh -- hostname

To enable bcvi on all ssh connections:

  alias ssh="bcvi --wrap-ssh --"

On a target server, you'll need to unpack the overloaded TERM variable:

  test -n "$(which bcvi)" && eval "$(bcvi --unpack-term)"

To use vi over the back-channel:

  bcvi filename

The installation to a remote server should set up aliases, e.g.:

  test -n "${BCVI_CONF}"  && alias vi="bcvi"
  test -n "${BCVI_CONF}"  && alias bcp="bcvi -c scpd"

=head1 PLUGINS

=for BCVI_PLUGINS

=head1 COPYRIGHT

Copyright 2007-2010 Grant McLean E<lt>grantm@cpan.orgE<gt>

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut