]> bicyclesonthemoon.info Git - ott/bsta/blobdiff - info.1.pl
move merge_settings to common library
[ott/bsta] / info.1.pl
index 87720052ab5cf8ff06f1cb2360e6367b0d2356ec..58a675fc51381708ecc28dd61556ccd33c189b99 100644 (file)
--- a/info.1.pl
+++ b/info.1.pl
@@ -5,7 +5,7 @@
 #
 # The frame/story info interface
 #
-# Copyright (C) 2017, 2023  Balthasar Szczepański
+# Copyright (C) 2017, 2023, 2024  Balthasar Szczepański
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 use strict;
-#use warnings;
+use utf8;
+# use Encode::Locale ('decode_argv');
+use Encode ('encode', 'decode');
+
 ###PERL_LIB: use lib /botm/lib/bsta
-use bsta_lib qw(failpage gethttpheader getcgi readdatafile printdatafile);
-use File::Copy;
+use botm_common (
+       'HTTP_STATUS',
+       'fail_method', 'fail_content_type',
+       'http_header_status',
+       'read_header_env',
+       'write_data_file',
+       'url_query_decode',
+       'merge_settings'
+);
+use bsta_lib (
+       'STATE',
+       'get_password',
+       'get_page_file', 'get_frame_file',
+       'read_frame_data', 'read_default', 'read_noaccess',
+       'read_settings', 'read_default', 'read_state',
+       'read_words_list', 'read_words', 'read_attachment'
+);
+
+binmode STDIN,  ':encoding(UTF-8)';
+binmode STDOUT, ':encoding(UTF-8)';
+binmode STDERR, ':encoding(UTF-8)';
+# decode_argv();
 
-###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta
-###PERL_DATA_DEFAULT_PATH:  DATA_DEFAULT_PATH  = /botm/data/bsta/default
-###PERL_DATA_NOACCESS_PATH: DATA_NOACCESS_PATH = /botm/data/bsta/noaccess
-###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
-###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
+my $time = time();
+srand ($time-$$);
 
 my %http;
 my %cgi;
-my %framedata;
-my %nextframedata;
+my %info_data;
+my %next_frame_data;
 my %default;
 my %settings;
 my %state;
 
-my $time = time();
-srand ($time-$$);
-
 my $method;
-my $frame;
+my $frame = '';
+my $attachment = '';
+my $words = '';
 my $password;
-my $passwordOK;
+my $password_ok;
 my $access;
-my $showcommand;
+my $show_command;
 my $ongtime;
-my $seconds;
+my $timer;
+my $ong_state;
+my $last_frame;
 
 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);
+       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 ($cgi{'f'} =~ /^(.+)$/) {
-       $frame=int($1);
+if ($cgi{'f'} =~ /^.+$/) {
+       $frame = int($&);
 }
-elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
-       $frame=int($1);
+elsif ($cgi{'a'} =~ /^.+$/) {
+       $attachment = int($&);
 }
-else {
-       $frame = '';
+elsif ($cgi{'i'} =~ /^.+$/) {
+       $attachment = int($&);
 }
-
-if ($cgi{'p'} =~ /^(.+)$/) {
-       $password=$1;
+elsif ($cgi{'w'} =~ /^.+$/) {
+       $words = $&;
 }
-else {
-       $password='';
+elsif ($ENV{'PATH_INFO'} =~ /^\/a\/?(.+)$/) {
+       $attachment = int($1);
 }
-
-%settings=readdatafile(DATA_SETTINGS_PATH);
-%default=readdatafile(DATA_DEFAULT_PATH);
-%framedata=readdatafile(DATA_PATH.$frame);
-%state=readdatafile(DATA_STATE_PATH);
-if($password eq $settings{'password'}){
-       $passwordOK = 1;
+elsif ($ENV{'PATH_INFO'} =~ /^\/w\/?(.+)$/) {
+       $words = $1;
 }
-else{
-       $passwordOK = 0;
+elsif ($ENV{'PATH_INFO'} =~ /^\/(f\/?)?(.+)$/) {
+       $frame = int($2);
 }
 
-if ($frame eq '') {
-       unless($passwordOK) {
+$password = get_password(\%cgi);
+
+%settings   = read_settings();
+%default    = read_default();
+%state      = read_state(());
+
+$ong_state  = int($state{'state'});
+$last_frame = int($state{'last'});
+
+$password_ok = ($password eq $settings{'password'});
+
+# comment info, not frame
+if ($words ne '') {
+       if ($words =~ /^[0-9]+$/) {
+               %info_data = read_words_list(
+                       int($&), # id
+                       0, # header only
+                       1 # not as list
+               );
+               $frame = int($&);
+               if ($info_data{'posts'} eq '') {
+                       $info_data{'posts'} = 0;
+               }
+       }
+       elsif ($words =~ /^[0-9]+\.[0-9\.]+$/) {
+               %info_data = read_words($&);
+               $frame = ($info_data{'frame'} ne '') ? int($info_data{'frame'}) : -1;
+               unless ($password_ok) {
+                       delete($info_data{'password'});
+                       delete($info_data{'key'});
+               }
+       }
+}
+# attachment info, not frame
+elsif ($attachment ne '') {
+       %info_data = read_attachment($attachment);
+       $frame = ($info_data{'frame'} ne '') ? int($info_data{'frame'}) : -1;
+}
+# frame info
+elsif ($frame ne '') {
+       if ($frame < 0) {
+               $frame = $last_frame + $frame +1;
+       }
+       %info_data       = read_frame_data($frame, \%default);
+       %next_frame_data = read_frame_data($frame+1, \%default);
+       
+       $timer   = int($state{'nextong'}) - $time;
+       $ongtime = int($state{'ongtime'});
+       if($ongtime == 0) {
+               $ongtime = int($settings{'ongtime'})
+       }
+       $show_command = ($timer < ($ongtime * 3600 / 3));
+}
+# state info, not frame
+else {
+       unless ($password_ok) {
+               # just show if IP was saved, not its value
                if ($state{'ip1'} ne '') {
-                       $state{'ip1'}=1;
+                       $state{'ip1'} = 1;
                }
                if ($state{'ip2'} ne '') {
-                       $state{'ip2'}=1;
+                       $state{'ip2'} = 1;
                }
                if ($state{'ip3'} ne '') {
-                       $state{'ip3'}=1;
+                       $state{'ip3'} = 1;
                }
        }
-       print "Content-type: text/plain\n\n";
-       if($method eq 'HEAD') {
+       print "Content-type: text/plain; charset=UTF-8\n\n";
+       if ($method eq 'HEAD') {
                exit;
        }
-       printdatafile(%state);
+       write_data_file(
+               \*STDOUT, \%state, '',
+               0, 0, 0,
+               '>>', 1
+       );
+       
+       exit;
 }
 
-else {
-       if($frame<0) {
-               $frame = int($state{'last'}) + $frame +1;
-               %framedata=readdatafile(DATA_PATH.$frame);
-       }
-       
-       %nextframedata=readdatafile(DATA_PATH.($frame+1));
+if (
+       $password_ok || (
+               ($ong_state >= STATE->{'waiting'}) &&
+               ($frame <= $last_frame) &&
+               ($frame >= 0)
+       )
+) {
+       $access = 1;
        
-       foreach my $ind (keys %default) {
-               unless(defined($framedata{$ind})){
-                       $framedata{$ind}=$default{$ind};
-               }
-               unless(defined($nextframedata{$ind})){
-                       $nextframedata{$ind}=$default{$ind};
-               }
-       }
-       $seconds=int($state{'nextong'})-$time;
-       $ongtime=int($state{'ongtime'});
-       if($ongtime == 0) {
-               $ongtime=int($settings{'ongtime'})
+       if (
+               ($words eq '') &&
+               ($attachment eq '') &&
+               ($info_data{'command'} eq '') && (
+                       $password_ok ||
+                       ($frame < $last_frame) || (
+                               ($ong_state >= STATE->{'ready'}) &&
+                               $show_command
+                       )
+               )
+       ) {
+               $info_data{'command'} = $next_frame_data{'title'};
        }
-       $showcommand = ($seconds < ($ongtime*3600/3));
-       
-       if ($passwordOK || (int($state{'state'}) >= 1 && $frame <= int($state{'last'}) && $frame >= 0)) {
-               $access=1;
-               
-               if ($passwordOK || $frame<int($state{'last'}) || (int($state{'state'}) >= 2 && $showcommand)) {
-                       $framedata{'command'}=$nextframedata{'title'};
-               }
-               $framedata{'frame'}=sprintf($settings{'frame'},$frame,$framedata{'ext'});
+}
+else {
+       $access = 0;
+       if (($attachment ne '') || ($words ne '')) {
+               %info_data = ();
        }
        else {
-               $access=0;
-               %framedata = readdatafile(DATA_NOACCESS_PATH);
-               foreach my $ind (keys %default) {
-                       unless(defined($framedata{$ind})){
-                               $framedata{$ind}=$default{$ind};
-                       }
-               }
+               %info_data = read_noaccess(\%default);
        }
-       
-       # $framedata{'frame'}=sprintf($settings{'frame'},$frame,$framedata{'ext'});
-       
-       print "Content-type: text/plain\n";
-       if(!$access) {
-               print "Status: 403 Forbidden\n";
+}
+if (
+       ($attachment eq '') &&
+       ($words eq '')
+) {
+       if ($info_data{'frame'} eq '') {
+               $info_data{'frame'} = get_frame_file($frame, \%info_data, \%settings)
        }
-       print "\n";
-       if($method eq 'HEAD') {
-               exit;
+       if ($info_data{'page'} eq '') {
+               unless (($access) && ($frame < $last_frame)) {
+                       $info_data{'page'} = '';
+               }
+               else {
+                       $info_data{'page'} = get_page_file($frame, \%info_data, \%settings);
+               }
        }
-       printdatafile(%framedata);
 }
+
+print "Content-type: text/plain; charset=UTF-8\n";
+if (!$access) {
+       print http_header_status(HTTP_STATUS->{'forbidden'});
+}
+print "\n";
+if($method eq 'HEAD') {
+       exit;
+}
+write_data_file(
+       \*STDOUT, \%info_data, '',
+       0, 0, 0,
+       '>>', 1
+);