1 ###RUN_PERL: #!/usr/bin/perl
4 # viewer is generated from viewer.1.pl.
8 # Copyright (C) 2016, 2017, 2019, 2020, 2023, 2024 Balthasar SzczepaĆski
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU Affero General Public License as
12 # published by the Free Software Foundation, either version 3 of the
13 # License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Affero General Public License for more details.
20 # You should have received a copy of the GNU Affero General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 # use Encode::Locale ('decode_argv');
26 use Encode ('encode', 'decode');
28 ###PERL_LIB: use lib /botm/lib/bsta
34 'open_encoded', '_x_encoded',
39 'STATE', 'TEXT_MODE', 'INTF_STATE',
40 'fail_method', 'fail_content_type', 'redirect',
41 'get_remote_addr', 'get_frame', 'get_password',
44 'write_index', 'write_static_goto', 'write_static_viewer_page',
46 'read_frame_data', 'read_default', 'read_noaccess',
47 'read_words_list', 'read_settings', 'read_story', 'read_goto',
48 'read_state', 'write_state',
52 ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
54 ###PERL_CGI_PATH: CGI_PATH = /bsta/
55 ###PERL_CGI_GOTO_PATH: CGI_GOTO_PATH = /bsta/g
56 ###PERL_CGI_VIEWER_PATH: CGI_VIEWER_PATH = /bsta/v
58 ###PERL_DATA_STATE_PATH: DATA_STATE_PATH = /botm/data/bsta/state
60 ###PERL_WWW_PATH: WWW_PATH = /botm/www/
62 binmode STDIN, ':encoding(UTF-8)';
63 binmode STDOUT, ':encoding(UTF-8)';
64 binmode STDERR, ':encoding(UTF-8)';
84 my $prev_frame_data_path;
85 my $next_frame_data_path;
102 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
103 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
105 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
109 exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
112 %http = read_header_env(\%ENV);
113 %cgi = url_query_decode($ENV{'QUERY_STRING'});
115 if ($method eq 'POST') {
116 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
117 my %cgi_post = url_query_decode( <STDIN> );
118 %cgi = merge_settings(\%cgi, \%cgi_post);
120 # multipart not supported
122 exit fail_content_type($method, $http{'content-type'});
127 (defined $cgi{'f'}) &&
130 ) { # GOTO with no value
131 exit redirect($method, CGI_GOTO_PATH(), HTTP_STATUS->{'see_other'});
135 $no_cgi = (scalar (keys %cgi) == 0);
137 $IP = get_remote_addr();
138 $frame = get_frame(\%cgi);
139 $password = get_password(\%cgi);
141 %settings = read_settings();
142 %default = read_default();
145 %frame_data= read_frame_data($frame);
148 $password_ok = ($password eq $settings{'password'});
150 # state & activation logic
151 if (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
154 %state = read_state($fh);
157 $frame = int($state{'last'}) + $frame +1;
160 %frame_data = read_frame_data($frame);
165 (int($state{'state'}) == STATE->{'waiting'}) &&
166 ($frame == int($state{'last'})) &&
167 ($method ne 'HEAD') &&
170 # register IP for progress
171 my %new_state = %state;
173 ($state{'ip1'} eq $IP) ||
174 ($state{'ip2'} eq $IP) ||
175 ($state{'ip3'} eq $IP)
178 if ($state{'ip1'} eq '') {
179 $new_state{'ip1'} = $IP;
181 elsif ($state{'ip2'} eq '') {
182 $new_state{'ip2'} = $IP;
184 elsif ($state{'ip3'} eq '') {
185 $new_state{'ip3'} = $IP;
186 $new_state{'state'} = STATE->{'ready'};
189 $new_state{'state'} = STATE->{'ready'};
191 if ($new_state{'state'} == STATE->{'ready'}) {
192 write_static_goto(\%new_state, \%settings, '');
193 write_static_viewer_page(
199 '', # prev frame data
200 \%frame_data, # next frame data,
204 write_state($fh, \%new_state);
208 (int($state{'state'}) == STATE->{'inactive'}) &&
213 # NOTE: at this point frame 0 is already ONGed.
215 my $ong_time = int($settings{'firstongtime'});
218 %story = read_story();
219 %goto_list = read_goto();
222 (int($story{'state'}) == INTF_STATE->{'>|'} ) &&
223 (int($story{'pass'}) == 1)
225 # conditions met; ACTIVATE!
228 $state{'state'} = STATE->{'waiting'};
230 $state{'ip1'} = '0.0.0.0';
231 $state{'ip2'} = '0.0.0.0';
233 $state{'nextong'} = (int($time / 3600) + int($settings{'firstongtime'})) * 3600 ;
234 $state{'ongtime'} = $ong_time;
236 # prepare to ONG frame 1
250 $r = write_index(\%state, \%settings);
253 $r = write_static_goto(\%state, \%settings, \%goto_list);
256 $r = write_state($fh, \%state);
259 # FAILED ONG! Story as if it was inactive!
260 $state{'state'} = STATE->{'inactive'};
266 # FAILED GET STATE! Story as if it was inactive!
267 $state{'state'} = STATE->{'inactive'};
272 $state{'state'} = STATE->{'inactive'};
277 (int($state{'state'}) >= STATE->{'waiting'}) &&
278 ($frame <= int($state{'last'})) &&
285 # no CGI - static page is OK
287 exit redirect($method, CGI_PATH(), HTTP_STATUS->{'see_other'});
289 elsif ($frame < int($state{'last'})) {
290 my $page_file = get_page_file($frame, \%frame_data, \%settings);
292 join_path(PATH_SEPARATOR(), WWW_PATH() , $page_file)
294 my $static_url = merge_url(
295 {'path' => CGI_PATH()},
296 {'path' => $page_file}
298 exit redirect($method, $static_url, HTTP_STATUS->{'see_other'});
302 if ($force_redirect) {
303 my $redirect_url = merge_url(
304 {'path' => CGI_VIEWER_PATH()},
308 delete $cgi{'f'}; # to avoid infinite loop
309 $redirect_url = merge_url(
310 {'path' => $redirect_url},
314 exit redirect($method, $redirect_url, HTTP_STATUS->{'see_other'});
318 %prev_frame_data = read_frame_data($frame-1, \%default);
321 %prev_frame_data = %default;
323 %next_frame_data = read_frame_data($frame+1, \%default);
324 %frame_data = merge_settings(\%default, \%frame_data);
327 # replace frame data with fail state replacement
328 %frame_data = read_noaccess(\%default);
331 $timer = int($state{'nextong'}) - $time;
332 $ongtime = int($state{'ongtime'});
334 $ongtime = int($settings{'ongtime'})
337 $show_command = ($timer < ($ongtime*3600/3));
338 if ($state{'state'} >= STATE->{'ready'}) {
341 elsif ($state{'ip3'} ne '') {
344 elsif ($state{'ip2'} ne '') {
347 elsif ($state{'ip1'} ne '') {
354 $text_mode = int($cgi{'b'});
355 if($text_mode > TEXT_MODE->{'words'}) {
356 $text_mode = TEXT_MODE->{'normal'};
358 $words_page = int($cgi{'i'});
359 $goto = int($cgi{'g'});
361 %words_data = read_words_list(
363 ($text_mode != TEXT_MODE->{'words'})
367 print http_header_status(HTTP_STATUS->{'forbidden'});
369 print "Content-type: text/html; charset=UTF-8\n\n";
370 if($method eq 'HEAD') {
380 'password_ok' => $password_ok,
381 'timer_unlocked'=> $timer_unlocked,
384 'show_command' => $show_command,
385 'text_mode' => $text_mode,
386 'words_page' => $words_page,
392 $access ? \%prev_frame_data : \%frame_data,
393 $access ? \%next_frame_data : \%frame_data,