]> bicyclesonthemoon.info Git - ott/bsta/commitdiff
reworked Coincidence chat
authorb <rowerynaksiezycu@gmail.com>
Sat, 30 Dec 2023 22:58:40 +0000 (22:58 +0000)
committerb <rowerynaksiezycu@gmail.com>
Sat, 30 Dec 2023 22:58:40 +0000 (22:58 +0000)
2words.1.pl
bsta_lib.1.pm
chat.1.pl
reset.1.pl

index ce6d445858566a9af6b758f3fb9fa5f897bef577..453f4d5700562f053ef39dd937def1bbf41222f0 100644 (file)
@@ -114,12 +114,13 @@ delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
        $method = $1;
 }
-else{
+else {
        exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
 }
 
 %http = read_header_env(\%ENV);
 %cgi = url_query_decode($ENV{'QUERY_STRING'});
+
 if ($method eq 'POST') {
        if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
                my %cgi_post = url_query_decode( <STDIN> );
@@ -130,6 +131,7 @@ if ($method eq 'POST') {
                exit fail_content_type($method, $http{'content-type'});
        }
 }
+
 $IP = get_remote_addr();
 $page = get_id(\%cgi);
 if ($cgi{'words'} ne '') {
index db60e49c3ecd533d32e36886222d2516dda6f20f..c16899c5871528f61b863f9e952208cc55982235 100644 (file)
@@ -36,7 +36,7 @@ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 our @ISA         = qw(Exporter);
 our @EXPORT      = ();
 our @EXPORT_OK   = (
-       'STATE', 'TEXT_MODE', 'INTF_STATE',
+       'STATE', 'TEXT_MODE', 'INTF_STATE', 'CHAT_STATE', 'CHAT_ACTION',
        'failpage',
        'fail_method', 'fail_content_type', 'fail_open_file', 'fail_attachment', 'fail_500',
        'redirect',
@@ -121,6 +121,19 @@ use constant TEXT_MODE => {
        'bb'     => 1,
        'info'   => 2,
 };
+use constant CHAT_STATE => {
+       'disconnected' => 0,
+       'ready'        => 1,
+       'active'       => 2,
+};
+use constant CHAT_ACTION => {
+       'none'   => 0,
+       'join'   => 1,
+       'leave'  => 2,
+       'nopost' => 3,
+       'file'   => 4,
+};
+
 
 use constant tags_bbcode => {
        'ht'    => '',
@@ -345,7 +358,10 @@ sub get_remote_addr {
 
 # functions to get ID/number etc.
 sub get_id {
-       (my $cgi, my $cgi_name) = @_;
+       (my $cgi, my $default, my $cgi_name) = @_;
+               if ($default eq '') {
+               $default = 0;
+       }
        if ($cgi_name eq '') {
                $cgi_name = 'i';
        }
@@ -357,14 +373,14 @@ sub get_id {
                return int($1);
        }
        else {
-               return 0;
+               return int($default);
        }
 }
 
 # function to obtain frame number
 sub get_frame {
-       (my $cgi) = @_;
-       return get_id($cgi, 'f');
+       (my $cgi, my $default) = @_;
+       return get_id($cgi, $default, 'f');
 }
 
 # function to obtain password
index 5449aad43f4b42ad7d69d38f45761bfeace36cfa..f7297f9675b9fb32236e4a447c787049bfe178f8 100644 (file)
--- a/chat.1.pl
+++ b/chat.1.pl
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 use strict;
-#use warnings;
+use utf8;
 ###PERL_LIB: use lib /botm/lib/bsta
-use bsta_lib qw(failpage gethttpheader getcgi entityencode readdatafile writedatafile urlencode);
+# use Encode::Locale ('decode_argv');
+use Encode ('encode', 'decode');
+
+###PERL_LIB: use lib /botm/lib/bsta
+use botm_common (
+       'read_data_file', 'write_data_file',
+       'read_header_env',
+       'url_query_decode', 'url_query_encode',
+       'merge_url',
+       'html_entity_encode_dec',
+);
+use bsta_lib (
+       'CHAT_STATE', 'CHAT_ACTION',
+       'fail_method', 'fail_content_type',
+       'get_remote_addr', 'get_id', 'get_password',
+       'print_html_start', 'print_html_end',
+       'print_html_head_start', 'print_html_head_end',
+       'print_html_body_start', 'print_html_body_end',
+       'merge_settings'
+);
 use  File::Copy;
 
 ###PERL_CGI_PATH:           CGI_PATH           = /bsta/
 ###PERL_CGI_COIN_PATH:      CGI_COIN_PATH      = /bsta/coin
-###PERL_CGI_CSS_PATH:       CGI_CSS_PATH       = /bsta/bsta.css
-###PERL_CGI_LOGO_PATH:      CGI_LOGO_PATH      = /bsta/botmlogo.png
 
 ###PERL_DATA_CHAT_PATH:     DATA_CHAT_PATH     = /botm/data/bsta/chat
 ###PERL_DATA_COIN_PATH:     DATA_COIN_PATH     = /botm/data/bsta/coincidence
+###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
 
-###PERL_WEBSITE:            WEBSITE            = 1190.bicyclesonthemoon.info
 ###PERL_WEBSITE_NAME:       WEBSITE_NAME       = Bicycles on the Moon
-###PERL_FAVICON_PATH:       FAVICON_PATH       = /img/favicon.png
+
+binmode STDIN,  ':encoding(UTF-8)';
+binmode STDOUT, ':encoding(UTF-8)';
+binmode STDERR, ':encoding(UTF-8)';
+# decode_argv();
 
 my %http;
 my %cgi;
 my %coin;
 my %chat;
+my %settings;
 
 my $time = time();
 srand ($time-$$);
@@ -49,148 +71,121 @@ srand ($time-$$);
 my $method;
 my $IP;
 my $page;
-my $words;
-my $username;
-my $action;
+my $words = '';
+my $username = '';
+my $action = CHAT_ACTION->{'none'};
 my $password;
-my $chatfile;
+my $fh;
 my $state;
-my $passwordOK;
-my @chatlines;
-my $chatstate;
+my $password_ok;
+my @chat_lines;
+my $chat_state;
 my $message;
-my $chatid;
-my $lastid;
+my $chat_id;
+my $last_id;
 
 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
 
 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
-       $method=$1;
+       $method = $1;
 }
-else{
-       exit failpage("Status: 405 Method Not Allowed\nAllow: GET, POST, HEAD\n","405 Method Not Allowed","The interface does not support the $ENV{'REQUEST_METHOD'} method.",$method);
+else {
+       exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
 }
 
-%http = gethttpheader (\%ENV);
-%cgi = getcgi($ENV{'QUERY_STRING'});
+%http = read_header_env(\%ENV);
+%cgi = url_query_decode($ENV{'QUERY_STRING'});
 
 if ($method eq 'POST') {
        if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
-               my %cgipost=getcgi( <STDIN> );
-               foreach my $ind (keys %cgipost) {
-                       $cgi{$ind}=$cgipost{$ind};
-               }
+               my %cgi_post = url_query_decode( <STDIN> );
+               %cgi = merge_settings(\%cgi, \%cgi_post);
        }
        # multipart not supported
        else{
-               exit failpage("Status: 415 Unsupported Media Type\n","415 Unsupported Media Type","Unsupported Content-type: $http{'content-type'}.");
+               exit fail_content_type($method, $http{'content-type'});
        }
 }
 
-if ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
-       $page=int($1);
-}
-else {
-       $page=-1;
-}
+$IP       = get_remote_addr();
+$page     = get_id(\%cgi, -1);
+$password = get_password(\%cgi);
 
-if ($ENV{'REMOTE_ADDR'} =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/) {
-       $IP=$1;
-}
-else {
-       $IP='0.0.0.0';
-}
+%coin      = read_data_file(DATA_COIN_PATH());
+%settings  = read_data_file(DATA_SETTINGS_PATH());
+
+$password_ok = ($password eq $settings{'password'});
 
 if ($cgi{'words'} ne '') {
-       $words=$cgi{'words'};
-}
-if ($cgi{'username'} ne '') {
-       $username=$cgi{'username'};
-}
-if ($cgi{'join'} ne '') {
-       $action=1;
-}
-elsif ($cgi{'leave'} ne '') {
-       $action=2;
-}
-elsif ($cgi{'nopost'} ne '') {
-       $action=3;
-}
-elsif ($cgi{'file'} ne '') {
-       $action=4;
+       $words = $cgi{'words'};
 }
-else {
-       $action=0;
-}
-if ($cgi{'p'} ne '') {
-       $password=$cgi{'p'};
-}
-
-%coin=readdatafile(DATA_COIN_PATH);
-
-if($password eq $coin{'password'}){
-       $passwordOK = 1;
+if ($password_ok && ($cgi{'username'} ne '')) {
+       $username = $cgi{'username'};
 }
-else{
-       $passwordOK = 0;
-       $username = '';
+foreach my $action_id ('join', 'leave', 'nopost', 'file') {
+       if ($cgi{$action_id} ne '') {
+               $action = CHAT_ACTION->{$action_id};
+               last;
+       }
 }
 
-if($page < 0) {
-       if (open ($chatfile,"+<",DATA_CHAT_PATH)){
-               if (flock($chatfile,2)) {
-                       %chat=readdatafile($chatfile);
+# ongoing chat
+if ($page < 0) {
+       if (open ($fh, "+<", encode('locale_fs', DATA_CHAT_PATH()))){
+               if (flock($fh, 2)) {
+                       %chat = read_data_file($fh);
                        
-                       $chatstate=int($chat{'state'});
-                       $chatid=int($chat{'id'});
-                       $lastid=$chatid;
+                       $chat_state = int($chat{'state'});
+                       $chat_id    = int($chat{'id'});
+                       $last_id    = $chat_id;
                        
-                       if($action==0 && $cgi{'words'} ne '') {
-                               if($chatstate < 1 && !$passwordOK) {
+                       if (($action == CHAT_ACTION->{'none'}) && ($words ne '')) {
+                               if (($chat_state < CHAT_STATE->{'ready'}) && !$password_ok) {
                                        $message = 'Not connected.';
                                }
                                else {
-                                       if ($cgi{'words'} !~ /[\r\n]/) {
-                                               if($username =~ /^[A-Za-z]*$/) {
-                                                       $chat{'content'}=$chat{'content'}.$username.': '.$cgi{'words'}."\n";
-                                                       if($chatstate < 2) {
-                                                               $chat{'state'} = 2;
-                                                               $chatstate = 2;
+                                       if ($words !~ /[\r\n]/) {
+                                               if ($username =~ /^[A-Za-z]*$/) {
+                                                       $chat{'content'} .= $username.': '.$words."\n";
+                                                       if ($chat_state < CHAT_STATE->{'active'}) {
+                                                               $chat_state = CHAT_STATE->{'active'};
+                                                               $chat{'state'} = $chat_state;
                                                        }
-                                                       writedatafile($chatfile,%chat);
+                                                       write_data_file($fh, '', '', \%chat);
                                                }
                                                else {
-                                                       $message='Invalid username.';
+                                                       $message = 'Invalid username.';
                                                }
                                        }
                                        else {
-                                               $message='Invalid text.';
+                                               $message = 'Invalid text.';
                                        }
                                }
                        }
-                       elsif ($action==1) {
-                               if($chatstate > 0 && !$passwordOK) {
+                       
+                       elsif ($action == CHAT_ACTION->{'join'}) {
+                               if (($chat_state > CHAT_STATE->{'disconnected'}) && !$password_ok) {
                                        $message = 'Already connected.';
                                }
                                else {
-                                       if($username =~ /^[A-Za-z]*$/) {
-                                               if ($passwordOK || $cgi{'words'} eq $coin{'server'}) {
-                                                       $chat{'content'}=$chat{'content'}.'join@'.$username.': '.$cgi{'words'}."\n";
-                                                       if($chatstate < 1) {
-                                                               $chat{'state'} = 1;
-                                                               $chatstate = 1;
+                                       if ($username =~ /^[A-Za-z]*$/) {
+                                               if ($password_ok || $words eq $coin{'server'}) {
+                                                       $chat{'content'} .= 'join@'.$username.': '.$words."\n";
+                                                       if ($chat_state < CHAT_STATE->{'ready'}) {
+                                                               $chat_state = CHAT_STATE->{'ready'};
+                                                               $chat{'state'} = $chat_state;
                                                        }
-                                                       writedatafile($chatfile,%chat);
+                                                       write_data_file($fh, '', '', \%chat);
                                                }
-                                               elsif ($cgi{'words'} eq '') {
-                                                       $message='Server ID missing.';
+                                               elsif ($words eq '') {
+                                                       $message = 'Server ID missing.';
                                                }
-                                               elsif ($cgi{'words'} !~ /^[0-9]+$/) {
-                                                       $message='Invalid server ID.';
+                                               elsif ($words !~ /^[0-9]+$/) {
+                                                       $message = 'Invalid server ID.';
                                                }
                                                else {
-                                                       $message='No active Coincidence server with this ID.';
+                                                       $message = 'No active Coincidence server with this ID.';
                                                }
                                        }
                                        else {
@@ -198,30 +193,29 @@ if($page < 0) {
                                        }
                                }
                        }
-                       elsif ($action==2) {
-                               if($chatstate < 1 && !$passwordOK) {
+                       
+                       elsif ($action == CHAT_ACTION->{'leave'}) {
+                               if (($chat_state < CHAT_STATE->{'ready'}) && !$password_ok) {
                                        $message = 'Already disconnected.';
                                }
                                else {
-                                       if($username =~ /^[A-Za-z]*$/) {
-                                               $chat{'content'}=$chat{'content'}.'leave@'.$username.': '.$cgi{'words'}."\n";
-                                               if($username ne '') {
-                                                       writedatafile($chatfile,%chat);
-                                               }
-                                               elsif ($chatstate > 1) {
-                                                       writedatafile(DATA_CHAT_PATH.$chatid,%chat);
-                                                       my %newchat;
-                                                       $newchat{'id'}=$chatid+1;
-                                                       $newchat{'state'}=0;
-                                                       $newchat{'content'}='';
-                                                       writedatafile($chatfile,%newchat);
+                                       if ($username =~ /^[A-Za-z]*$/) {
+                                               $chat{'content'} .= 'leave@'.$username.': '.$words."\n";
+                                               if ($username ne '') {
+                                                       write_data_file($fh, '', '', \%chat);
                                                }
                                                else {
-                                                       my %newchat;
-                                                       $newchat{'id'}=$chatid;
-                                                       $newchat{'state'}=0;
-                                                       $newchat{'content'}='';
-                                                       writedatafile($chatfile,%newchat);
+                                                       my %new_chat;
+                                                       if ($chat_state > 1) {
+                                                               write_data_file(DATA_CHAT_PATH.$chat_id, '', '', \%chat);
+                                                               $new_chat{'id'} = $chat_id+1;
+                                                       }
+                                                       else {
+                                                               $new_chat{'id'} = $chat_id;
+                                                       }
+                                                       $new_chat{'state'} = CHAT_STATE->{'disconnected'};
+                                                       $new_chat{'content'} = '';
+                                                       write_data_file($fh, '', '', \%new_chat);
                                                }
                                        }
                                        else {
@@ -229,165 +223,201 @@ if($page < 0) {
                                        }
                                }
                        }
-                       elsif($action==4 && $cgi{'file'} ne '' && $cgi{'words'} ne '' &&  $passwordOK) {
-                               if ($cgi{'words'} !~ /[\r\n]/) {
-                                       if($username =~ /^[A-Za-z]*$/) {
-                                               $chat{'content'}=$chat{'content'}.'file@'.$username.': '.$cgi{'words'}."\n";
-                                               if($chatstate < 2) {
-                                                       $chat{'state'} = 2;
-                                                       $chatstate = 2;
+                       
+                       elsif (
+                               ($action == CHAT_ACTION->{'file'}) &&
+                               ($cgi{'file'} ne '') &&
+                               ($words ne '') && 
+                               $password_ok
+                       ) {
+                               if ($words !~ /[\r\n]/) {
+                                       if ($username =~ /^[A-Za-z]*$/) {
+                                               $chat{'content'} .= 'file@'.$username.': '.$words."\n";
+                                               if ($chat_state < CHAT_STATE->{'active'}) {
+                                                       $chat_state = CHAT_STATE->{'active'};
+                                                       $chat{'state'} = $chat_state;
                                                }
-                                               writedatafile($chatfile,%chat);
+                                               write_data_file($fh, '', '', \%chat);
                                        }
                                        else {
-                                               $message='Invalid username.';
+                                               $message = 'Invalid username.';
                                        }
                                }
                                else {
-                                       $message='Invalid text.';
+                                       $message = 'Invalid text.';
                                }
                        }
-                       
-                       @chatlines = split(/\r?\n/,$chat{'content'});
+                       @chat_lines = split(/\r?\n/, $chat{'content'});
                }
                else{
-                       $chatstate=0;
-                       $message='Can\'t lock data file!';
+                       $chat_state = CHAT_STATE->{'disconnected'};
+                       $message = 'Can\'t lock data file!';
                }
                
-               
-               close($chatfile);
+               close($fh);
        }
        else {
-               $chatstate=0;
+               $chat_state = CHAT_STATE->{'disconnected'};
                $message='Can\'t open data file!';
        }
 }
+# old chat archive
 else {
-       # %chat=readdatafile(DATA_CHAT_PATH);
-       # $chatid=int($chat{'id'})-$page;
-       %chat=readdatafile(DATA_CHAT_PATH);
-       $lastid=int($chat{'id'});
-       %chat=readdatafile(DATA_CHAT_PATH.$page);
-       $chatid=int($chat{'id'});
-       $chatstate=int($chat{'state'});
-       @chatlines = split(/\r?\n/,$chat{'content'});
+       $chat_id = $page;
+       %chat = read_data_file(DATA_CHAT_PATH());
+       $last_id = int($chat{'id'});
+       if ($chat_id < $last_id) {
+               %chat = read_data_file(DATA_CHAT_PATH.$page);
+               $chat_state = int($chat{'state'});
+               @chat_lines = split(/\r?\n/, $chat{'content'});
+       }
 }
 
-
-# print "Content-type: text/plain\n\n";
-# print DATA_CHAT_PATH."\n";
-# print 'state: '.$chat{'state'}."\n";
-# print 'id: '.$chat{'id'}."\n\n";
-# print $chat{'content'};
-
 print "Content-type: text/html\n\n";
 if($method eq 'HEAD') {
        exit;
 }
 
-print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">'."\n";
-print '<html lang="en"><head>'."\n";
-print '<title>Coincidence &bull; '.WEBSITE_NAME.'</title>'."\n";
-print '<meta http-equiv="Content-type" content="text/html; charset=UTF-8">'."\n";
-print '<link rel="icon" type="image/png" href="'.FAVICON_PATH.'">'."\n";
-print '<link rel="stylesheet" href="'.CGI_CSS_PATH.'">'."\n";
-print '</head><body>'."\n";
-print '<a href="/"><img id="botmlogo" src="'.CGI_LOGO_PATH.'" alt="'.WEBSITE.'"></a>'."\n";
-print '<div id="all">'."\n";
-
-print '<div id="inst" class="ins">'."\n";
-
-print '<div id="title">'."\n";
-print '<H1 id="titletext">Coincidence</H1>'."\n";
-print '</div>'."\n";
-
-print '<div id="storypuzzle">'."\n";
+if ($username eq '') {
+       $username = $coin{'name'};
+}
+
+my $base_url = CGI_PATH();
+my $coin_url = CGI_COIN_PATH();
+my $form_url = $coin_url;
+my $oldest_url = merge_url(
+       {'path' => $coin_url},
+       {'path' => 0}
+);
+my $older_url = merge_url(
+       {'path' => $coin_url},
+       {'path' => $chat_id -1}
+);
+my $newer_url = ($chat_id < ($last_id -1)) ?
+       merge_url(
+               {'path' => $coin_url},
+               {'path' => $chat_id +1}
+       ) : $coin_url;
+
+if ($password_ok) {
+       my $password_query = url_query_encode({'p', $settings{'password'}});
+       $coin_url   = merge_url($coin_url  , {'query' => $password_query, 'append_query' => 1});
+       $oldest_url = merge_url($oldest_url, {'query' => $password_query, 'append_query' => 1});
+       $older_url  = merge_url($older_url , {'query' => $password_query, 'append_query' => 1});
+       $newer_url  = merge_url($newer_url , {'query' => $password_query, 'append_query' => 1});
+}
+
+my $abbr = abbr_name($username);
+my $_website_name = html_entity_encode_dec(WEBSITE_NAME() , 1);
+my $_server       = html_entity_encode_dec($coin    {'server'}  , 1);
+my $_key          = html_entity_encode_dec($coin    {'key'}     , 1);
+my $_password     = html_entity_encode_dec($settings{'password'}, 1);
+my $_cgi_username = html_entity_encode_dec($cgi     {'username'}, 1);
+my $_username     = html_entity_encode_dec($username  , 1);
+my $_abbr         = html_entity_encode_dec($abbr      , 1);
+my $_message      = html_entity_encode_dec($message   , 1);
+my $_base_url     = html_entity_encode_dec($base_url  , 1);
+my $_coin_url     = html_entity_encode_dec($coin_url  , 1);
+my $_form_url     = html_entity_encode_dec($form_url  , 1);
+my $_oldest_url   = html_entity_encode_dec($oldest_url, 1);
+my $_older_url    = html_entity_encode_dec($older_url , 1);
+my $_newer_url    = html_entity_encode_dec($newer_url , 1);
+
+print_html_start(\*STDOUT);
+print_html_head_start(\*STDOUT);
+
+print '  <title>Coincidence &bull; '.$_website_name.'</title>'."\n";
+
+print_html_head_end(\*STDOUT);
+print_html_body_start(\*STDOUT);
+
+print '   <div id="inst" class="ins">'."\n";
+
+print '    <div id="title">'."\n";
+print '     <H1 id="titletext">Coincidence</H1>'."\n";
+print '    </div>'."\n";
+
+print '    <div id="storypuzzle">'."\n";
 if ($page >= 0) {
-       print 'Before: '.$chatid."\n";
+       print '     Before: '.$chat_id."\n";
 }
-elsif ($chatstate>0){
-       print 'Connected to server <span class="br">'.entityencode($coin{'server'}).'</span> as user <span class="ni">'.entityencode(($username ne '')?$username:$coin{'name'}).'</span> (<span class="ni">'.entityencode(abbrname(($username ne '')?$username:$coin{'name'})).'</span>), public key <span class="br">'.entityencode($coin{'key'}).'</span>.'."\n";
+elsif ($chat_state > CHAT_STATE->{'disconnected'}) {
+       print '     Connected to server <span class="br">'.$_server.'</span> as user <span class="ni">'.$_username.'</span> (<span class="ni">'.$_abbr.'</span>), public key <span class="br">'.$_key.'</span>.'."\n";
 }
 else{
-       print 'Not connected.';
+       print '     Not connected.';
 }
-print '</div>'."\n";
-print '<div id="command">'."\n";
+print '    </div>'."\n";
+
+print '    <div id="command">'."\n";
 if ($message ne '') {
-       print '<span class="br">'.entityencode($message).'</span>'."\n";
+       print '     <span class="br">'.$_message.'</span>'."\n";
 }
 if ($page < 0) {
-       print '<form method="post" action="'.CGI_COIN_PATH.'">'."\n";
-       if ($passwordOK) {
-               print '<input class="intxc" type="text" name="words">'."\n";
-               print '<input class="inbt" type="submit" value="Send">'."\n";
-               print "|\n";
-               print '<input class="intx" type="text" name="username" value="'.entityencode($username).'">'."\n";
-               print '<input class="inbt" type="submit" name="nopost" value="Refresh">'."\n";
-               print '<input class="inbt" type="submit" name="join" value="Connect">'."\n";
-               print '<input class="inbt" type="submit" name="leave" value="Disconnect">'."\n";
-               print '<input class="inbt" type="submit" name="file" value="Send file">'."\n";
-               print '<input type="hidden" name="p" value="'.entityencode($coin{'password'}).'">'."\n";
+       print '     <form method="post" action="'.$_form_url.'">'."\n";
+       if ($password_ok) {
+               print '      <input class="intxc" type="text" name="words">'."\n";
+               print '      <input class="inbt" type="submit" value="Send">'."\n";
+               print "      |\n";
+               print '      <input class="intx" type="text" name="username" value="'.$_cgi_username.'">'."\n";
+               print '      <input class="inbt" type="submit" name="nopost" value="Refresh">'."\n";
+               print '      <input class="inbt" type="submit" name="join" value="Connect">'."\n";
+               print '      <input class="inbt" type="submit" name="leave" value="Disconnect">'."\n";
+               print '      <input class="inbt" type="submit" name="file" value="Send file">'."\n";
+               print '      <input type="hidden" name="p" value="'.$_password.'">'."\n";
        }
-       elsif ($chatstate>0) {
-               print '<input class="intxc" type="text" name="words">'."\n";
-               print '<input class="inbt" type="submit" value="Send">'."\n";
-               print "|\n";
-               print '<input class="inbt" type="submit" name="nopost" value="Refresh">'."\n";
-               print '<input class="inbt" type="submit" name="leave" value="Disconnect">'."\n";
+       elsif ($chat_state > CHAT_STATE->{'disconnected'}) {
+               print '      <input class="intxc" type="text" name="words">'."\n";
+               print '      <input class="inbt" type="submit" value="Send">'."\n";
+               print "      |\n";
+               print '      <input class="inbt" type="submit" name="nopost" value="Refresh">'."\n";
+               print '      <input class="inbt" type="submit" name="leave" value="Disconnect">'."\n";
        }
        else {
-               print '<input class="intx" type="text" name="words">'."\n";
-               print '<input class="inbt" type="submit" name="join" value="Connect">'."\n";
+               print '      <input class="intx" type="text" name="words">'."\n";
+               print '      <input class="inbt" type="submit" name="join" value="Connect">'."\n";
        }
-       print '</form>'."\n";
+       print '     </form>'."\n";
 }
-print '</div>'."\n";
+print '    </div>'."\n";
 
-print '</div><div id="insb" class="ins">'."\n";
+print '   </div>'."\n";
+print '   <div id="insb" class="ins">'."\n";
 
-print '<div id="chat">'."\n";
+print '    <div id="chat">'."\n";
 if ($page < 0) {
-       for (my $i = @chatlines-1; $i>=0; --$i) {
-               print chatline($chatlines[$i])."<br>\n";
+       for (my $i = @chat_lines-1; $i>=0; --$i) {
+               print '     '.chat_line($chat_lines[$i])."<br>\n";
        }
 }
 else {
-       for (my $i = 0; $i<@chatlines; ++$i) {
-               print chatline($chatlines[$i])."<br>\n";
+       for (my $i = 0; $i<@chat_lines; ++$i) {
+               print '     '.chat_line($chat_lines[$i])."<br>\n";
        }
 }
-print '</div>'."\n";
+print '    </div>'."\n";
 
-print '<div id="underlinks">'."\n";
-print '<a href="'.CGI_PATH.'">BSTA</a> | <a href="'.CGI_COIN_PATH.($passwordOK?('?p='.urlencode($coin{'password'})):'').'">Once again</a>';
-if ($chatid > 0) {
-       print ' | <a href="'.CGI_COIN_PATH.'/'.($chatid-1).($passwordOK?('?p='.urlencode($coin{'password'})):'').'">Before</a>';
+print '    <div id="underlinks">'."\n";
+print '     <a href="'.$_base_url.'">BSTA</a> | <a href="'.$_coin_url.'">Once again</a>';
+if ($chat_id > 0) {
+       print ' | <a href="'.$_older_url.'">Before</a>';
 }
-if ($chatid < $lastid) {
-       print ' | <a href="'.CGI_COIN_PATH.'/'.(($chatid < $lastid-1)?($chatid +1):'').($passwordOK?('?p='.urlencode($coin{'password'})):'').'">Unbefore</a>';
+if ($chat_id < $last_id) {
+       print ' | <a href="'.$_newer_url.'">Unbefore</a>';
 }
-if ($chatid > 0) {
-       print ' | <a href="'.CGI_COIN_PATH.'/0'.($passwordOK?('?p='.urlencode($coin{'password'})):'').'">Initially</a>';
+if ($chat_id > 0) {
+       print ' | <a href="'.$_oldest_url.'">Initially</a>';
 }
 print ' | (This interface is only a demo, a proof of concept. It is very limited. No autorefresh, no private chat, etc. For full functionality use the actual Coincidence client.)'."\n";
-print "\n";
-print '</div>'."\n";
+print '    </div>'."\n";
 
-print '</div>'."\n";
-print '</div>'."\n";
-print '<a href="/" class="cz">'.WEBSITE.'</a>'."\n";
-print '</body></html>'."\n";
+print '   </div>'."\n";
 
+print_html_body_end(\*STDOUT);
+print_html_end(\*STDOUT);
 
 
-# print DATA_CHAT_PATH."\n";
-# print 'state: '.$chat{'state'}."\n";
-# print 'id: '.$chat{'id'}."\n\n";
-# print $chat{'content'};
-
-sub abbrname {
+sub abbr_name {
        (my $name) = @_;
        my $abbr;
        
@@ -403,33 +433,44 @@ sub abbrname {
        return $abbr;
 }
 
-sub chatline {
+sub chat_line {
        (my $line) = @_;
-       my $action;
-       my $name;
-       my $text;
        
        if ($line =~ /^([a-z]*@)?([A-Za-z]*): (.*)$/) {
-               $action=$1;
-               $name=$2;
-               $text=$3;
+               my $action = $1;
+               my $name = $2;
+               my $text = $3;
+               my $color;
+               if ($name eq '') {
+                       $name = $coin{'name'};
+                       $color = 'ni';
+               }
+               else {
+                       $color = 'br';
+               }
+               $abbr = abbr_name($name);
+               
+               my $_name   = html_entity_encode_dec($name          , 1);
+               my $_abbr   = html_entity_encode_dec($abbr          , 1);
+               my $_text   = html_entity_encode_dec($text          , 1);
+               my $_server = html_entity_encode_dec($coin{'server'}, 1);
                
-               if($action ne ''){
+               if($action ne '') {
                        if ($action eq 'join@') {
-                               return entityencode(($name ne '')?$name:$coin{'name'}).' ('.entityencode(abbrname(($name ne '')?$name:$coin{'name'})).') joined the public chat on server '.entityencode($coin{'server'}).'.';
+                               return "$_name ($_abbr) joined the public chat on server $_server.";
                        }
                        elsif ($action eq 'leave@') {
-                               return entityencode(($name ne '')?$name:$coin{'name'}).' ('.entityencode(abbrname(($name ne '')?$name:$coin{'name'})).') left the public chat on server '.entityencode($coin{'server'}).'.';
+                               return "$_name ($_abbr) left the public chat on server $_server.";
                        }
                        elsif ($action eq 'file@') {
-                               return entityencode(($name ne '')?$name:$coin{'name'}).' ('.entityencode(abbrname(($name ne '')?$name:$coin{'name'})).') sent the file '.entityencode($text).'.';
+                               return "$_name ($_abbr)  sent the file $_text.";
                        }
                        else {
                                return 'E:E:E';
                        }
                }
                else {
-                       return '<span class="'.(($name ne '')?'br':'ni').'">'.entityencode(abbrname(($name ne '')?$name:$coin{'name'})).': '.entityencode($text).'</span>';
+                       return "<span class=\"$color\">$_abbr: $_text</span>";
                }
        }
        else {
index 7c8fada0db58c7e9cb05585818c76c47b3906c53..7d1a5349059bce03f2607abedda267d6746ada18 100644 (file)
@@ -29,7 +29,7 @@ use botm_common (
        'read_data_file', 'write_data_file',
 );
 use bsta_lib (
-       'STATE', 'INTF_STATE',
+       'STATE', 'INTF_STATE', 'CHAT_STATE',
        'write_index'
 );
 
@@ -83,7 +83,7 @@ unless (
 );
 %chat = (
        'id'      => 0,
-       'state'   => 0, # TODO chat state enum?
+       'state'   => CHAT_STATE->{'disconnected'},
        'content' => ''
 );