Connex/connex.pl

282 lines
5.8 KiB
Perl
Raw Normal View History

2024-03-04 20:56:21 -05:00
#!/usr/bin/perl -w
use strict;
use warnings;
use strict;
use Curses::UI;
use Net::Telnet;
use URI::Split;
2024-03-04 20:56:21 -05:00
my $HOST_DEFAULT = "nightfall.city";
my $PATHSPEC_DEFAULT = '';
my $PORT_DEFAULT = 1900;
my $PROT_NEX = "nex://";
my $home_default= $PROT_NEX . $HOST_DEFAULT;
my $host = "nightfall.city";
my $port = "1900";
my $pathspec = "";
my $docname = "";
my $doctype = "txt";
my $dot_ext = ".";
my @history;
my $statusbar;
my $win1;
my $cui = new Curses::UI( -color_support => 1 );
my $connect = new Net::Telnet (Timeout => 10,
Errmode => 'return');
my @menu = (
{ -label => 'File',
-submenu => [
{ -label => 'Go to Link ^G', -value => \&goto_link_dialog },
{ -label => 'Change Site ^C', -value => \&goto_site_dialog },
2024-03-04 20:56:21 -05:00
{ -label => 'Back ^B', -value => \&goto_back },
{ -label => 'History ^H', -value => "\&history_status_dialog" },
2024-03-04 20:56:21 -05:00
{ -label => 'Exit ^Q', -value => \&exit_dialog }
]
},
);
sub exit_dialog()
{
my $return = $cui->dialog(
-message => "Really quit?",
-title => "Are you sure?",
-buttons => ['yes', 'no'],
);
exit(0) if $return;
}
sub goto_link_dialog()
{
my $return = $cui->question(-question => "This is [$host/ $pathspec]. Enter destination link:",
-answer => $pathspec,
);
2024-03-04 20:56:21 -05:00
navigate($return);
}
sub goto_site_dialog()
{
my $return = $cui->question(-question => "This is [$host/ $pathspec]. Visit site:",
-answer => $host,
);
if ($return){
my $site = $return;
if($site ne ''){
$host = $site;
add_history($host);
load($pathspec);
update_status_bar();
}
history_status_dialog();
}
}
sub goto_back()
{
my $fetched = fetch_history();
#if($fetched) {
2024-03-04 20:56:21 -05:00
$pathspec =$fetched;
load($pathspec);
update_status_bar();
history_status_dialog();
#}
2024-03-04 20:56:21 -05:00
}
sub navigate{
my $link = shift;
#if ($link){
# if($link ne ''){
add_history($pathspec); #add last link to history before going forward!
2024-03-04 20:56:21 -05:00
$pathspec = $link;
load($pathspec);
# }else {
# $pathspec = '';
# }
2024-03-04 20:56:21 -05:00
update_status_bar();
#}
2024-03-04 20:56:21 -05:00
}
sub update_status_bar
{
my $browser = $win1->getobj("browser");
2024-03-04 20:56:21 -05:00
my $statusbar = $win1->getobj("status");
$statusbar->text("Current site: $host, current link: [$pathspec]");
$statusbar->draw();
$browser->focus();
2024-03-04 20:56:21 -05:00
}
sub add_history
{
my $link = shift;
print ("link $link");
push(@history, $link);
history_status_dialog();
return;
}
sub fetch_history
{
my $latest;
if(@history) {
$latest = pop(@history);
return $latest;
}else{
return($PATHSPEC_DEFAULT);
}
}
sub history_status_dialog
{
if(@history){
my $browser = $win1->getobj("browser");
$browser->focus();
2024-03-04 20:56:21 -05:00
my $history_list = join(", ", @history);
my $return = $cui->status("$history_list");
2024-03-04 20:56:21 -05:00
}
}
my $menu = $cui->add(
'menu','Menubar',
-menu => \@menu,
-fg => "blue",
);
$win1 = $cui->add(
'win1', 'Window',
-border => 1,
-y => 1,
-bfg => 'red',
-vscrollbar => 'right',
);
my $texteditor = $win1->add("browser", "TextViewer",
-text => "Start Page",
-border => 1,
-padtop => 0,
-padbottom => 3,
-showlines => 0,
-sbborder => 0,
-vscrollbar => 1,
-hscrollbar => 1,
-showhardreturns => 0,
-wrapping => 0, # wrapping slows down the editor :-(
);
$statusbar = $win1->add("status", "TextViewer",
-border => 1,
-bfg => 'red',
-y => -1,
-height => 1,
-width => -1,
-reverse => 1,
-paddingspaces => 1,
-text => "Current site: $HOST_DEFAULT, current link: [/]",
);
#key bindings, should match menu items
$cui->set_binding(sub {$menu->focus()}, "\cX");
$cui->set_binding( \&exit_dialog , "\cQ");
$cui->set_binding( \&goto_link_dialog , "\cG");
$cui->set_binding( \&goto_back , "\cB");
$cui->set_binding( \&goto_site_dialog , "\cC");
#start up
2024-03-04 20:56:21 -05:00
$texteditor->focus();
navigate('');
2024-03-04 20:56:21 -05:00
$cui->mainloop();
#pick apart nex:// urls, which we should be using
#sub parse_url #need to pass in url; and host, pathspec, file variables to be filled
#{
# my $url = @_[0];
# my $host = url;
# host =~ s/^nex://([a-z0-9]*)\/[a-z0-9\/\.]*$/$1/;
# my $pathspec = url;
# pathspec =~ s/^nex://[a-z0-9]*(\/[a-z0-9\/]*)[[a-z0-9\.]*$)/$1/;
# my $file = $url
#
#}
#make nex:// urls from parts, relative urls
#sub construct_valid_url #protocol, host, pathspec
#{
# my $protocol = shift;
# my $host = shift;
# my $pathspec = shift;
# my $file = shift;
# my $url = $protocol;
#need error handling
#what about the end slash and files vs index?
# $url = $protocol . $host;
# if ($pathspec){
# $url = $url . '/' . $pathspec;
# if ($file){
# $url = $url . '.' $file;
# }
# }
# return $url;
#}
sub load
{
my $pathspec = shift;
my @lines;
my $ok;
$connect->host($host);
$connect->port($port);
$ok= $connect->open($host);
$ok= $connect->print($pathspec);
@lines =$connect->getlines(ErrMode=> 'return');
print $connect->eof();
#handle non-existant request?
2024-03-04 20:56:21 -05:00
die unless $connect->eof();
$connect->close();
my $widget= $texteditor;
my $loaded_text ="";
my $currentline;
my $count=0;
foreach $currentline (@lines){
if ($currentline =~ m/^=>/){
$count+=1;
$currentline =~ s/(^=>.*$)/$1 [\>$count\]/;
2024-03-04 20:56:21 -05:00
}
$loaded_text= $loaded_text . $currentline;
}
$widget->text($loaded_text);
}