1 ###RUN_PERL: #!/usr/bin/perl
4 # viewer is generated from viewer.1.pl.
8 # Copyright (C) 2016-2017, 2019-2020, 2023 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 'fail_method', 'fail_content_type',
38 'get_remote_addr', 'get_frame', 'get_password',
40 'print_viewer_page', 'write_index'
44 ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
46 ###PERL_DATA_PATH: DATA_PATH = /botm/data/bsta/
47 ###PERL_DATA_DEFAULT_PATH: DATA_DEFAULT_PATH = /botm/data/bsta/default
48 ###PERL_DATA_LIST_PATH: DATA_LIST_PATH = /botm/data/bsta/list
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
54 ###PERL_WWW_PATH: WWW_PATH = /botm/www/1190/bsta/
56 binmode STDIN, ':encoding(UTF-8)';
57 binmode STDOUT, ':encoding(UTF-8)';
58 binmode STDERR, ':encoding(UTF-8)';
77 my $next_frame_data_path;
89 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
90 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
92 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
96 exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
99 %http = read_header_env(\%ENV);
100 %cgi = url_query_decode($ENV{'QUERY_STRING'});
101 if ($method eq 'POST') {
102 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
103 my %cgi_post=url_query_decode( <STDIN> );
104 foreach my $ind (keys %cgi_post) {
105 $cgi{$ind} = $cgi_post{$ind};
108 # multipart not supported
110 exit fail_content_type($http{'content-type'}, $method);
113 $IP = get_remote_addr();
114 $frame = get_frame(\%cgi);
115 $password = get_password(\%cgi);
117 %settings = read_data_file(DATA_SETTINGS_PATH());
118 %default = read_data_file(DATA_DEFAULT_PATH());
121 $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
122 %frame_data= read_data_file($frame_data_path);
125 $password_ok = ($password eq $settings{'password'}) {
127 # state & activation logic
128 if (open ($state_file, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
129 if (flock($state_file, 2)) {
131 %state = read_data_file($state_file);
134 $frame = int($state{'last'}) + $frame +1;
135 $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
136 %frame_data = read_data_file($frame_data_path);
140 (int($state{'state'}) == STATE->{'waiting'}) &&
141 ($frame == int($state{'last'})) &&
142 ($method ne 'HEAD') &&
145 # register IP for progress
146 my %new_state = %state;
148 ($state{'ip1'} eq $IP) ||
149 ($state{'ip1'} eq $IP) ||
150 ($state{'ip1'} eq $IP)
153 if ($state{'ip1'} eq '') {
154 $new_state{'ip1'} = $IP;
156 elsif ($state{'ip2'} eq '') {
157 $new_state{'ip2'} = $IP;
159 elsif ($state{'ip3'} eq '') {
160 $new_state{'ip3'} = $IP;
162 write_data_file($state_file, '', '', \%new_state);
166 (int($state{'state'}) == STATE->{'inactive'}) &&
170 # NOTE: at this point frame 0 is already ONGed.
176 %story = read_data_file(DATA_STORY_PATH());
177 %goto_list = read_data_file(DATA_LIST_PATH);
180 (int($story{'state'}) == 0x11) && # buttons 0 + 4
181 (int($story{'pass'}) == 1)
183 # conditions met; ACTIVATE!
185 # update ONG time of frame 1
186 $frame_data{'ongtime'} = $time;
187 write_data_file($frame_data_path, '', '', \%frame_data);
190 $state{'state'} = STATE->{'waiting'};
192 $state{'ip1'} = '0.0.0.0';
193 $state{'ip2'} = '0.0.0.0';
195 $state{'nextong'} = (int($time / 3600) + int($settings{'firstongtime'})) * 3600 ;
196 $state{'ongtime'} = int($settings{'firstongtime'});
198 # prepare to ONG frame 1
200 # determine frame file & paths
201 unless (defined($frame_data{'ext'})){
202 $frame_data{'ext'} = $default{'ext'};
204 $frame_file = sprintf($settings{'frame'}, $frame, $frame_data{'ext'});
205 $in_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame_file);
206 $out_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $frame_file);
208 # update the GOTO list with frame 1
209 $goto_list{'title-1'} = $frame_data{'title'};
210 $goto_list{'ongtime-1'} = $frame_data{'ongtime'};
212 if (copy ($inpath, $outpath)) {
213 write_index('viewer', \%state, \%settings);
214 write_data_file($state_file, '','', \%state);
215 write_data_file(DATA_LIST_PATH(),'','', \%goto_list);
218 # FAILED ONG! Story as if it was inactive!
219 $state{'state'} = STATE->{'inactive'};
225 # FAILED GET STATE! Story as if it was inactive!
226 $state{'state'} = STATE->{'inactive'};
231 $state{'state'} = STATE->{'inactive'};
234 $next_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame+1);
235 %next_frame_data = read_data_file($next_frame_data_path);
238 %frame_data = merge_settings(\%default, \%frame_data);
239 %next_frame_data = merge_settings(\%default, \%next_frame_data);
241 $timer = int($state{'nextong'}) - $time;
242 $ongtime = int($state{'ongtime'});
244 $ongtime = int($settings{'ongtime'})
247 $show_command = ($timer < ($ongtime*3600/3));
248 if ($state{'state'} >= STATE->{'ready'}) {
251 elsif ($state{'ip3'} ne '') {
254 elsif ($state{'ip2'} ne '') {
257 elsif ($state{'ip1'} ne '') {
266 (int($state{'state'}) >= STATE->{'waiting'}) &&
267 ($frame <= int($state{'last'})) &&
275 # replace frame data with fail state replacement
276 %frame_data = read_data_file(DATA_NOACCESS_PATH());
277 %frame_data = merge_settings(\%default, \%frame_data);
280 $text_mode = int($cgi{'b'});
282 $text_mode = TEXT_MODE->{'normal'};
285 print "Content-type: text/html\n";
287 print "Status: 403 Forbidden\n";
290 if($method eq 'HEAD') {
299 'password_ok' => $password_ok,
300 'timer_unlocked'=> $timer_unlocked,
303 'show_command' => $show_command,