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
32 'read_data_file', 'write_data_file',
35 'open_encoded', '_x_encoded',
40 'STATE', 'TEXT_MODE', 'INTF_STATE',
41 'fail_method', 'fail_content_type', 'redirect',
42 'get_remote_addr', 'get_frame', 'get_password',
44 'print_viewer_page', 'write_index',
48 ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
50 ###PERL_CGI_PATH: CGI_PATH = /bsta/
51 ###PERL_CGI_VIEWER_PATH: CGI_VIEWER_PATH = /bsta/v
53 ###PERL_DATA_PATH: DATA_PATH = /botm/data/bsta/
54 ###PERL_DATA_DEFAULT_PATH: DATA_DEFAULT_PATH = /botm/data/bsta/default
55 ###PERL_DATA_NOACCESS_PATH: DATA_NOACCESS_PATH = /botm/data/bsta/noaccess
56 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
57 ###PERL_DATA_STATE_PATH: DATA_STATE_PATH = /botm/data/bsta/state
58 ###PERL_DATA_STORY_PATH: DATA_STORY_PATH = /botm/data/bsta/story
59 ###PERL_DATA_WORDS_PATH: DATA_WORDS_PATH = /botm/data/bsta/words/
61 ###PERL_WWW_PATH: WWW_PATH = /botm/www/
63 binmode STDIN, ':encoding(UTF-8)';
64 binmode STDOUT, ':encoding(UTF-8)';
65 binmode STDERR, ':encoding(UTF-8)';
86 my $prev_frame_data_path;
87 my $next_frame_data_path;
103 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
104 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
106 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
110 exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
113 %http = read_header_env(\%ENV);
114 %cgi = url_query_decode($ENV{'QUERY_STRING'});
116 if ($method eq 'POST') {
117 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
118 my %cgi_post = url_query_decode( <STDIN> );
119 %cgi = merge_settings(\%cgi, \%cgi_post);
121 # multipart not supported
123 exit fail_content_type($method, $http{'content-type'});
126 $no_cgi = (scalar (keys %cgi) == 0);
128 $IP = get_remote_addr();
129 $frame = get_frame(\%cgi);
130 $password = get_password(\%cgi);
132 %settings = read_data_file(DATA_SETTINGS_PATH());
133 %default = read_data_file(DATA_DEFAULT_PATH());
136 $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
137 %frame_data= read_data_file($frame_data_path);
140 $password_ok = ($password eq $settings{'password'});
142 # state & activation logic
143 if (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
146 %state = read_data_file($fh);
149 $frame = int($state{'last'}) + $frame +1;
152 $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
153 %frame_data = read_data_file($frame_data_path);
158 (int($state{'state'}) == STATE->{'waiting'}) &&
159 ($frame == int($state{'last'})) &&
160 ($method ne 'HEAD') &&
163 # register IP for progress
164 my %new_state = %state;
166 ($state{'ip1'} eq $IP) ||
167 ($state{'ip2'} eq $IP) ||
168 ($state{'ip3'} eq $IP)
171 if ($state{'ip1'} eq '') {
172 $new_state{'ip1'} = $IP;
174 elsif ($state{'ip2'} eq '') {
175 $new_state{'ip2'} = $IP;
177 elsif ($state{'ip3'} eq '') {
178 $new_state{'ip3'} = $IP;
179 $new_state{'state'} = STATE->{'ready'};
182 $new_state{'state'} = STATE->{'ready'};
184 write_data_file($fh, \%new_state);
188 (int($state{'state'}) == STATE->{'inactive'}) &&
192 # NOTE: at this point frame 0 is already ONGed.
194 my $ong_time = int($settings{'firstongtime'});
197 %story = read_data_file(DATA_STORY_PATH());
200 (int($story{'state'}) == INTF_STATE->{'>|'} ) &&
201 (int($story{'pass'}) == 1)
203 # conditions met; ACTIVATE!
206 $state{'state'} = STATE->{'waiting'};
208 $state{'ip1'} = '0.0.0.0';
209 $state{'ip2'} = '0.0.0.0';
211 $state{'nextong'} = (int($time / 3600) + int($settings{'firstongtime'})) * 3600 ;
212 $state{'ongtime'} = $ong_time;
214 # prepare to ONG frame 1
228 $r = write_index(\%state, \%settings);
231 $r = write_data_file($fh, \%state);
234 # FAILED ONG! Story as if it was inactive!
235 $state{'state'} = STATE->{'inactive'};
241 # FAILED GET STATE! Story as if it was inactive!
242 $state{'state'} = STATE->{'inactive'};
247 $state{'state'} = STATE->{'inactive'};
252 (int($state{'state'}) >= STATE->{'waiting'}) &&
253 ($frame <= int($state{'last'})) &&
260 # no CGI - static page is OK
262 exit redirect($method, CGI_PATH(), HTTP_STATUS->{'see_other'});
264 elsif ($frame < int($state{'last'})) {
266 if ($frame_data{'page'} ne '') {
267 $page_file = $frame_data{'page'};
270 $page_file = sprintf(
276 join_path(PATH_SEPARATOR(), WWW_PATH() , $page_file)
278 my $static_url = merge_url(
279 {'path' => CGI_PATH()},
280 {'path' => $page_file}
282 exit redirect($method, $static_url, HTTP_STATUS->{'see_other'});
286 if ($force_redirect) {
287 my $redirect_url = merge_url(
288 {'path' => CGI_VIEWER_PATH()},
292 $redirect_url = merge_url(
293 {'path' => $redirect_url},
297 exit redirect($method, $redirect_url, HTTP_STATUS->{'see_other'});
301 $prev_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame-1);
302 %prev_frame_data = read_data_file($prev_frame_data_path);
304 $next_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame+1);
305 %next_frame_data = read_data_file($next_frame_data_path);
307 %frame_data = merge_settings(\%default, \%frame_data);
308 %prev_frame_data = merge_settings(\%default, \%prev_frame_data);
309 %next_frame_data = merge_settings(\%default, \%next_frame_data);
312 # replace frame data with fail state replacement
313 %frame_data = read_data_file(DATA_NOACCESS_PATH());
314 %frame_data = merge_settings(\%default, \%frame_data);
317 $timer = int($state{'nextong'}) - $time;
318 $ongtime = int($state{'ongtime'});
320 $ongtime = int($settings{'ongtime'})
323 $show_command = ($timer < ($ongtime*3600/3));
324 if ($state{'state'} >= STATE->{'ready'}) {
327 elsif ($state{'ip3'} ne '') {
330 elsif ($state{'ip2'} ne '') {
333 elsif ($state{'ip1'} ne '') {
340 $text_mode = int($cgi{'b'});
341 if($text_mode > TEXT_MODE->{'words'}) {
342 $text_mode = TEXT_MODE->{'normal'};
344 $words_page = int($cgi{'i'});
346 $words_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $frame);
347 %words_data = read_data_file(
348 $words_data_path, # file
351 ($text_mode != TEXT_MODE->{'words'}), # header only
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
380 $access ? \%prev_frame_data : \%frame_data,
381 $access ? \%next_frame_data : \%frame_data,