]> bicyclesonthemoon.info Git - ott/bsta/blobdiff - chat.1.pl
use optimised HTML entity encoding
[ott/bsta] / chat.1.pl
index 51fafdb2dd1c491fe2706626c9a9543ab641646c..90ea637323a58d57d762aa154bc5db3e71bdadc0 100644 (file)
--- a/chat.1.pl
+++ b/chat.1.pl
@@ -28,12 +28,13 @@ use Encode ('encode', 'decode');
 
 ###PERL_LIB: use lib /botm/lib/bsta
 use botm_common (
-       'read_data_file', 'write_data_file',
+       'HTTP_STATUS',
        'read_header_env',
        'url_query_decode', 'url_query_encode',
        'merge_url',
        'html_entity_encode_dec',
-       'open_encoded'
+       'open_encoded',
+       'http_header_status'
 );
 use bsta_lib (
        'STATE', 'CHAT_STATE', 'CHAT_ACTION',
@@ -42,7 +43,9 @@ use bsta_lib (
        'print_html_start', 'print_html_end',
        'print_html_head_start', 'print_html_head_end',
        'print_html_body_start', 'print_html_body_end',
-       'merge_settings'
+       'merge_settings',
+       'read_chat', 'write_chat',
+       'read_coincidence', 'read_settings', 'read_state'
 );
 use  File::Copy;
 
@@ -50,9 +53,6 @@ use  File::Copy;
 ###PERL_CGI_COIN_PATH:      CGI_COIN_PATH      = /bsta/coin
 
 ###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_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
 
 ###PERL_WEBSITE_NAME:       WEBSITE_NAME       = Bicycles on the Moon
 
@@ -83,6 +83,7 @@ my $state;
 my $password_ok;
 my @chat_lines;
 my $chat_state;
+my $status;
 my $message;
 my $chat_id;
 my $last_id;
@@ -94,7 +95,7 @@ if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
        $method = $1;
 }
 else {
-       exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
+       exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
 }
 
 %http = read_header_env(\%ENV);
@@ -115,9 +116,9 @@ $IP       = get_remote_addr();
 $page     = get_id(\%cgi, -1);
 $password = get_password(\%cgi);
 
-%coin      = read_data_file(DATA_COIN_PATH());
-%settings  = read_data_file(DATA_SETTINGS_PATH());
-%state     = read_data_file(DATA_STATE_PATH());
+%coin      = read_coincidence();
+%settings  = read_settings();
+%state     = read_state();
 
 $password_ok = ($password eq $settings{'password'});
 
@@ -138,14 +139,22 @@ foreach my $action_id ('join', 'leave', 'nopost', 'file') {
 if ($page < 0) {
        if (open_encoded($fh, "+<", DATA_CHAT_PATH())) {
                if (flock($fh, 2)) {
-                       %chat = read_data_file($fh);
+                       %chat = read_chat($fh);
                        
                        $chat_state = int($chat{'state'});
                        $chat_id    = int($chat{'id'});
                        $last_id    = $chat_id;
                        
-                       if (($action == CHAT_ACTION->{'none'}) && ($words ne '')) {
+                       if ($method ne 'POST') {
+                               #
+                       }
+                       
+                       elsif (
+                               ($action == CHAT_ACTION->{'none'}) &&
+                               ($words ne '')
+                       ) {
                                if (($chat_state < CHAT_STATE->{'ready'}) && !$password_ok) {
+                                       $status = HTTP_STATUS->{'forbidden'};
                                        $message = 'Not connected.';
                                }
                                else {
@@ -156,13 +165,15 @@ if ($page < 0) {
                                                                $chat_state = CHAT_STATE->{'active'};
                                                                $chat{'state'} = $chat_state;
                                                        }
-                                                       write_data_file($fh, '', '', \%chat);
+                                                       write_chat($fh, \%chat);
                                                }
                                                else {
+                                                       $status = HTTP_STATUS->{'bad_request'};
                                                        $message = 'Invalid username.';
                                                }
                                        }
                                        else {
+                                               $status = HTTP_STATUS->{'bad_request'};
                                                $message = 'Invalid text.';
                                        }
                                }
@@ -180,19 +191,23 @@ if ($page < 0) {
                                                                $chat_state = CHAT_STATE->{'ready'};
                                                                $chat{'state'} = $chat_state;
                                                        }
-                                                       write_data_file($fh, '', '', \%chat);
+                                                       write_chat($fh, \%chat);
                                                }
                                                elsif ($words eq '') {
+                                                       $status = HTTP_STATUS->{'bad_request'};
                                                        $message = 'Server ID missing.';
                                                }
                                                elsif ($words !~ /^[0-9]+$/) {
+                                                       $status = HTTP_STATUS->{'bad_request'};
                                                        $message = 'Invalid server ID.';
                                                }
                                                else {
+                                                       $status = HTTP_STATUS->{'not_found'};
                                                        $message = 'No active Coincidence server with this ID.';
                                                }
                                        }
                                        else {
+                                               $status = HTTP_STATUS->{'bad_request'};
                                                $message = 'Invalid username.';
                                        }
                                }
@@ -206,12 +221,12 @@ if ($page < 0) {
                                        if ($username =~ /^[A-Za-z]*$/) {
                                                $chat{'content'} .= 'leave@'.$username.': '.$words."\n";
                                                if ($username ne '') {
-                                                       write_data_file($fh, '', '', \%chat);
+                                                       write_chat($fh, \%chat);
                                                }
                                                else {
                                                        my %new_chat;
                                                        if ($chat_state > 1) {
-                                                               write_data_file(DATA_CHAT_PATH.$chat_id, '', '', \%chat);
+                                                               write_chat($chat_id, \%chat);
                                                                $new_chat{'id'} = $chat_id+1;
                                                        }
                                                        else {
@@ -219,10 +234,11 @@ if ($page < 0) {
                                                        }
                                                        $new_chat{'state'} = CHAT_STATE->{'disconnected'};
                                                        $new_chat{'content'} = '';
-                                                       write_data_file($fh, '', '', \%new_chat);
+                                                       write_chat($fh, \%new_chat);
                                                }
                                        }
                                        else {
+                                               $status = HTTP_STATUS->{'bad_request'};
                                                $message = 'Invalid username.';
                                        }
                                }
@@ -241,20 +257,24 @@ if ($page < 0) {
                                                        $chat_state = CHAT_STATE->{'active'};
                                                        $chat{'state'} = $chat_state;
                                                }
-                                               write_data_file($fh, '', '', \%chat);
+                                               write_chat($fh, \%chat);
                                        }
                                        else {
+                                               $status = HTTP_STATUS->{'bad_request'};
                                                $message = 'Invalid username.';
                                        }
                                }
                                else {
+                                       $status = HTTP_STATUS->{'bad_request'};
                                        $message = 'Invalid text.';
                                }
                        }
+                       
                        @chat_lines = split(/\r?\n/, $chat{'content'});
                }
-               else{
+               else {
                        $chat_state = CHAT_STATE->{'disconnected'};
+                       $status = HTTP_STATUS->{'internal_server_error'};
                        $message = 'Can\'t lock data file!';
                }
                
@@ -262,22 +282,26 @@ if ($page < 0) {
        }
        else {
                $chat_state = CHAT_STATE->{'disconnected'};
+               $status = HTTP_STATUS->{'internal_server_error'};
                $message='Can\'t open data file!';
        }
 }
 # old chat archive
 else {
        $chat_id = $page;
-       %chat = read_data_file(DATA_CHAT_PATH());
+       %chat = read_chat();
        $last_id = int($chat{'id'});
        if ($chat_id < $last_id) {
-               %chat = read_data_file(DATA_CHAT_PATH.$page);
+               %chat = read_chat($page);
                $chat_state = int($chat{'state'});
                @chat_lines = split(/\r?\n/, $chat{'content'});
        }
 }
 
-print "Content-type: text/html\n\n";
+if ($status ne '') {
+       print http_header_status($status);
+}
+print "Content-type: text/html; charset=UTF-8\n\n";
 if($method eq 'HEAD') {
        exit;
 }
@@ -305,17 +329,17 @@ my $newer_url = ($chat_id < ($last_id -1)) ?
 
 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});
+       $coin_url   = merge_url($coin_url  , {'query' => $password_query, 'append_query' => 1, 'preserve_fragment' => 1});
+       $oldest_url = merge_url($oldest_url, {'query' => $password_query, 'append_query' => 1, 'preserve_fragment' => 1});
+       $older_url  = merge_url($older_url , {'query' => $password_query, 'append_query' => 1, 'preserve_fragment' => 1});
+       $newer_url  = merge_url($newer_url , {'query' => $password_query, 'append_query' => 1, 'preserve_fragment' => 1});
 }
 
+my $_password = $password_ok ? html_entity_encode_dec($settings{'password'}, 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);
@@ -349,7 +373,7 @@ 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.'."\n";
 }
 print '    </div>'."\n";