diff --git a/connex.pl b/connex.pl new file mode 100644 index 0000000..59e5544 --- /dev/null +++ b/connex.pl @@ -0,0 +1,275 @@ +#!/usr/bin/perl -w + + +use strict; +use warnings; +use strict; +use Curses::UI; +use Net::Telnet; + +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 }, + { -label => 'Back ^B', -value => \&goto_back }, + { -label => 'History ^H', -value => \&history_status_dialog }, + { -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, + ); + 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) { + $pathspec =$fetched; + load($pathspec); + update_status_bar(); + } + +} + + + +sub navigate{ + my $link = shift; + if ($link){ + if($link ne ''){ + $pathspec = $link; + add_history($pathspec); + load($pathspec); + }else { + $pathspec = ''; + } + update_status_bar(); + } + + +} + + +sub update_status_bar +{ + my $statusbar = $win1->getobj("status"); + $statusbar->text("Current site: $host, current link: [$pathspec]"); + $statusbar->draw(); + +} + + +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); + } + history_status_dialog(); + +} + + +sub history_status_dialog +{ + if(@history){ + my $history_list = join(", ", @history); + my $return = $cui->status(-message => $history_list); + } +} + + +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"); + + + +$texteditor->focus(); +load(''); +$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(); + 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/^=>(.*$)/[$count\]$1/; + } + $loaded_text= $loaded_text . $currentline; + } + $widget->text($loaded_text); + +} +