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_VIEWER_PATH: CGI_VIEWER_PATH = /bsta/v
57 ###PERL_DATA_STATE_PATH: DATA_STATE_PATH = /botm/data/bsta/state
59 ###PERL_WWW_PATH: WWW_PATH = /botm/www/
61 binmode STDIN, ':encoding(UTF-8)';
62 binmode STDOUT, ':encoding(UTF-8)';
63 binmode STDERR, ':encoding(UTF-8)';
83 my $prev_frame_data_path;
84 my $next_frame_data_path;
101 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
102 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
104 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
108 exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
111 %http = read_header_env(\%ENV);
112 %cgi = url_query_decode($ENV{'QUERY_STRING'});
114 if ($method eq 'POST') {
115 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
116 my %cgi_post = url_query_decode( <STDIN> );
117 %cgi = merge_settings(\%cgi, \%cgi_post);
119 # multipart not supported
121 exit fail_content_type($method, $http{'content-type'});
124 $no_cgi = (scalar (keys %cgi) == 0);
126 $IP = get_remote_addr();
127 $frame = get_frame(\%cgi);
128 $password = get_password(\%cgi);
130 %settings = read_settings();
131 %default = read_default();
134 %frame_data= read_frame_data($frame);
137 $password_ok = ($password eq $settings{'password'});
139 # state & activation logic
140 if (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
143 %state = read_state($fh);
146 $frame = int($state{'last'}) + $frame +1;
149 %frame_data = read_frame_data($frame);
154 (int($state{'state'}) == STATE->{'waiting'}) &&
155 ($frame == int($state{'last'})) &&
156 ($method ne 'HEAD') &&
159 # register IP for progress
160 my %new_state = %state;
162 ($state{'ip1'} eq $IP) ||
163 ($state{'ip2'} eq $IP) ||
164 ($state{'ip3'} eq $IP)
167 if ($state{'ip1'} eq '') {
168 $new_state{'ip1'} = $IP;
170 elsif ($state{'ip2'} eq '') {
171 $new_state{'ip2'} = $IP;
173 elsif ($state{'ip3'} eq '') {
174 $new_state{'ip3'} = $IP;
175 $new_state{'state'} = STATE->{'ready'};
178 $new_state{'state'} = STATE->{'ready'};
180 if ($new_state{'state'} == STATE->{'ready'}) {
181 write_static_goto(\%new_state, \%settings, '');
182 write_static_viewer_page(
188 '', # prev frame data
189 \%frame_data, # next frame data,
193 write_state($fh, \%new_state);
197 (int($state{'state'}) == STATE->{'inactive'}) &&
202 # NOTE: at this point frame 0 is already ONGed.
204 my $ong_time = int($settings{'firstongtime'});
207 %story = read_story();
208 %goto_list = read_goto();
211 (int($story{'state'}) == INTF_STATE->{'>|'} ) &&
212 (int($story{'pass'}) == 1)
214 # conditions met; ACTIVATE!
217 $state{'state'} = STATE->{'waiting'};
219 $state{'ip1'} = '0.0.0.0';
220 $state{'ip2'} = '0.0.0.0';
222 $state{'nextong'} = (int($time / 3600) + int($settings{'firstongtime'})) * 3600 ;
223 $state{'ongtime'} = $ong_time;
225 # prepare to ONG frame 1
239 $r = write_index(\%state, \%settings);
242 $r = write_static_goto(\%state, \%settings, \%goto_list);
245 $r = write_state($fh, \%state);
248 # FAILED ONG! Story as if it was inactive!
249 $state{'state'} = STATE->{'inactive'};
255 # FAILED GET STATE! Story as if it was inactive!
256 $state{'state'} = STATE->{'inactive'};
261 $state{'state'} = STATE->{'inactive'};
266 (int($state{'state'}) >= STATE->{'waiting'}) &&
267 ($frame <= int($state{'last'})) &&
274 # no CGI - static page is OK
276 exit redirect($method, CGI_PATH(), HTTP_STATUS->{'see_other'});
278 elsif ($frame < int($state{'last'})) {
279 my $page_file = get_page_file($frame, \%frame_data, \%settings);
281 join_path(PATH_SEPARATOR(), WWW_PATH() , $page_file)
283 my $static_url = merge_url(
284 {'path' => CGI_PATH()},
285 {'path' => $page_file}
287 exit redirect($method, $static_url, HTTP_STATUS->{'see_other'});
291 if ($force_redirect) {
292 my $redirect_url = merge_url(
293 {'path' => CGI_VIEWER_PATH()},
297 delete $cgi{'f'}; # to avoid infinite loop
298 $redirect_url = merge_url(
299 {'path' => $redirect_url},
303 exit redirect($method, $redirect_url, HTTP_STATUS->{'see_other'});
307 %prev_frame_data = read_frame_data($frame-1, \%default);
310 %prev_frame_data = %default;
312 %next_frame_data = read_frame_data($frame+1, \%default);
313 %frame_data = merge_settings(\%default, \%frame_data);
316 # replace frame data with fail state replacement
317 %frame_data = read_noaccess(\%default);
320 $timer = int($state{'nextong'}) - $time;
321 $ongtime = int($state{'ongtime'});
323 $ongtime = int($settings{'ongtime'})
326 $show_command = ($timer < ($ongtime*3600/3));
327 if ($state{'state'} >= STATE->{'ready'}) {
330 elsif ($state{'ip3'} ne '') {
333 elsif ($state{'ip2'} ne '') {
336 elsif ($state{'ip1'} ne '') {
343 $text_mode = int($cgi{'b'});
344 if($text_mode > TEXT_MODE->{'words'}) {
345 $text_mode = TEXT_MODE->{'normal'};
347 $words_page = int($cgi{'i'});
348 $goto = int($cgi{'g'});
350 %words_data = read_words_list(
352 ($text_mode != TEXT_MODE->{'words'})
356 print http_header_status(HTTP_STATUS->{'forbidden'});
358 print "Content-type: text/html; charset=UTF-8\n\n";
359 if($method eq 'HEAD') {
369 'password_ok' => $password_ok,
370 'timer_unlocked'=> $timer_unlocked,
373 'show_command' => $show_command,
374 'text_mode' => $text_mode,
375 'words_page' => $words_page,
381 $access ? \%prev_frame_data : \%frame_data,
382 $access ? \%next_frame_data : \%frame_data,