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
31 'read_data_file', 'write_data_file',
37 'STATE', 'TEXT_MODE', 'INTF_STATE',
38 'fail_method', 'fail_content_type',
39 'get_remote_addr', 'get_frame', 'get_password',
41 'print_viewer_page', 'write_index',
45 ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
47 ###PERL_DATA_PATH: DATA_PATH = /botm/data/bsta/
48 ###PERL_DATA_DEFAULT_PATH: DATA_DEFAULT_PATH = /botm/data/bsta/default
49 ###PERL_DATA_NOACCESS_PATH: DATA_NOACCESS_PATH = /botm/data/bsta/noaccess
50 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
51 ###PERL_DATA_STATE_PATH: DATA_STATE_PATH = /botm/data/bsta/state
52 ###PERL_DATA_STORY_PATH: DATA_STORY_PATH = /botm/data/bsta/story
53 ###PERL_DATA_WORDS_PATH: DATA_WORDS_PATH = /botm/data/bsta/words/
55 binmode STDIN, ':encoding(UTF-8)';
56 binmode STDOUT, ':encoding(UTF-8)';
57 binmode STDERR, ':encoding(UTF-8)';
78 my $prev_frame_data_path;
79 my $next_frame_data_path;
93 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
94 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
96 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
100 exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
103 %http = read_header_env(\%ENV);
104 %cgi = url_query_decode($ENV{'QUERY_STRING'});
106 if ($method eq 'POST') {
107 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
108 my %cgi_post = url_query_decode( <STDIN> );
109 %cgi = merge_settings(\%cgi, \%cgi_post);
111 # multipart not supported
113 exit fail_content_type($method, $http{'content-type'});
117 $IP = get_remote_addr();
118 $frame = get_frame(\%cgi);
119 $password = get_password(\%cgi);
121 %settings = read_data_file(DATA_SETTINGS_PATH());
122 %default = read_data_file(DATA_DEFAULT_PATH());
125 $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
126 %frame_data= read_data_file($frame_data_path);
129 $password_ok = ($password eq $settings{'password'});
131 # state & activation logic
132 if (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
135 %state = read_data_file($fh);
138 $frame = int($state{'last'}) + $frame +1;
139 $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
140 %frame_data = read_data_file($frame_data_path);
144 (int($state{'state'}) == STATE->{'waiting'}) &&
145 ($frame == int($state{'last'})) &&
146 ($method ne 'HEAD') &&
149 # register IP for progress
150 my %new_state = %state;
152 ($state{'ip1'} eq $IP) ||
153 ($state{'ip2'} eq $IP) ||
154 ($state{'ip3'} eq $IP)
157 if ($state{'ip1'} eq '') {
158 $new_state{'ip1'} = $IP;
160 elsif ($state{'ip2'} eq '') {
161 $new_state{'ip2'} = $IP;
163 elsif ($state{'ip3'} eq '') {
164 $new_state{'ip3'} = $IP;
165 $new_state{'state'} = STATE->{'ready'};
168 $new_state{'state'} = STATE->{'ready'};
170 write_data_file($fh, \%new_state);
174 (int($state{'state'}) == STATE->{'inactive'}) &&
178 # NOTE: at this point frame 0 is already ONGed.
180 my $ong_time = int($settings{'firstongtime'});
183 %story = read_data_file(DATA_STORY_PATH());
186 (int($story{'state'}) == INTF_STATE->{'>|'} ) &&
187 (int($story{'pass'}) == 1)
189 # conditions met; ACTIVATE!
192 $state{'state'} = STATE->{'waiting'};
194 $state{'ip1'} = '0.0.0.0';
195 $state{'ip2'} = '0.0.0.0';
197 $state{'nextong'} = (int($time / 3600) + int($settings{'firstongtime'})) * 3600 ;
198 $state{'ongtime'} = $ong_time;
200 # prepare to ONG frame 1
214 $r = write_index(\%state, \%settings);
217 $r = write_data_file($fh, \%state);
220 # FAILED ONG! Story as if it was inactive!
221 $state{'state'} = STATE->{'inactive'};
227 # FAILED GET STATE! Story as if it was inactive!
228 $state{'state'} = STATE->{'inactive'};
233 $state{'state'} = STATE->{'inactive'};
236 $timer = int($state{'nextong'}) - $time;
237 $ongtime = int($state{'ongtime'});
239 $ongtime = int($settings{'ongtime'})
242 $show_command = ($timer < ($ongtime*3600/3));
243 if ($state{'state'} >= STATE->{'ready'}) {
246 elsif ($state{'ip3'} ne '') {
249 elsif ($state{'ip2'} ne '') {
252 elsif ($state{'ip1'} ne '') {
261 (int($state{'state'}) >= STATE->{'waiting'}) &&
262 ($frame <= int($state{'last'})) &&
268 $prev_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame-1);
269 %prev_frame_data = read_data_file($prev_frame_data_path);
271 $next_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame+1);
272 %next_frame_data = read_data_file($next_frame_data_path);
274 %frame_data = merge_settings(\%default, \%frame_data);
275 %prev_frame_data = merge_settings(\%default, \%prev_frame_data);
276 %next_frame_data = merge_settings(\%default, \%next_frame_data);
280 # replace frame data with fail state replacement
281 %frame_data = read_data_file(DATA_NOACCESS_PATH());
282 %frame_data = merge_settings(\%default, \%frame_data);
285 $text_mode = int($cgi{'b'});
286 if($text_mode > TEXT_MODE->{'words'}) {
287 $text_mode = TEXT_MODE->{'normal'};
289 $words_page = int($cgi{'i'});
291 $words_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $frame);
292 %words_data = read_data_file(
293 $words_data_path, # file
296 ($text_mode != TEXT_MODE->{'words'}), # header only
300 print "Content-type: text/html\n";
302 print "Status: 403 Forbidden\n";
305 if($method eq 'HEAD') {
315 'password_ok' => $password_ok,
316 'timer_unlocked'=> $timer_unlocked,
319 'show_command' => $show_command,
320 'text_mode' => $text_mode,
321 'words_page' => $words_page
326 $access ? \%prev_frame_data : \%frame_data,
327 $access ? \%next_frame_data : \%frame_data,