]> bicyclesonthemoon.info Git - ott/bsta/blob - viewer.1.pl
preload previous page / frame
[ott/bsta] / viewer.1.pl
1 ###RUN_PERL: #!/usr/bin/perl
2
3 # /bsta/v
4 # viewer is generated from viewer.1.pl.
5 #
6 # The viewer interface
7 #
8 # Copyright (C) 2016, 2017, 2019, 2020, 2023, 2024  Balthasar SzczepaƄski
9 #
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.
14 #
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.
19 #
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/>.
22
23 use strict;
24 use utf8;
25 # use Encode::Locale ('decode_argv');
26 use Encode ('encode', 'decode');
27
28 ###PERL_LIB: use lib /botm/lib/bsta
29 use botm_common (
30         'read_header_env',
31         'read_data_file', 'write_data_file',
32         'url_query_decode',
33         'join_path',
34         'open_encoded'
35 );
36 use bsta_lib (
37         'STATE', 'TEXT_MODE', 'INTF_STATE',
38         'fail_method', 'fail_content_type',
39         'get_remote_addr', 'get_frame', 'get_password',
40         'merge_settings',
41         'print_viewer_page', 'write_index',
42         'ong'
43 );
44
45 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
46
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/
54
55 binmode STDIN,  ':encoding(UTF-8)';
56 binmode STDOUT, ':encoding(UTF-8)';
57 binmode STDERR, ':encoding(UTF-8)';
58 # decode_argv();
59
60 my $time = time();
61 srand ($time-$$);
62
63 my %http;
64 my %cgi;
65 my %frame_data;
66 my %prev_frame_data;
67 my %next_frame_data;
68 my %default;
69 my %settings;
70 my %state;
71 my %new_state;
72 my %goto_list;
73 my %words_data;
74
75 my $method;
76 my $frame;
77 my $frame_data_path;
78 my $prev_frame_data_path;
79 my $next_frame_data_path;
80 my $password;
81 my $password_ok;
82 my $IP;
83 my $access;
84 my $timer;
85 my $timer_unlocked;
86 my $fh;
87 my $show_command;
88 my $ongtime;
89 my $text_mode;
90 my $words_page;
91 my $words_data_path;
92
93 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
94 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
95
96 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
97         $method = $1;
98 }
99 else{
100         exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
101 }
102
103 %http = read_header_env(\%ENV);
104 %cgi = url_query_decode($ENV{'QUERY_STRING'});
105
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);
110         }
111         # multipart not supported
112         else{
113                 exit fail_content_type($method, $http{'content-type'});
114         }
115 }
116
117 $IP = get_remote_addr();
118 $frame = get_frame(\%cgi);
119 $password = get_password(\%cgi);
120
121 %settings  = read_data_file(DATA_SETTINGS_PATH());
122 %default   = read_data_file(DATA_DEFAULT_PATH());
123
124 if ($frame >= 0) {
125         $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
126         %frame_data= read_data_file($frame_data_path);
127 }
128
129 $password_ok = ($password eq $settings{'password'});
130
131 # state & activation logic
132 if (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
133         if (flock($fh, 2)) {
134                 
135                 %state = read_data_file($fh);
136                 
137                 if ($frame < 0) {
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);
141                 }
142                 
143                 if (
144                         (int($state{'state'}) == STATE->{'waiting'}) &&
145                         ($frame == int($state{'last'})) &&
146                         ($method ne 'HEAD') &&
147                         (!$password_ok)
148                 ) {
149                         # register IP for progress
150                         my %new_state = %state;
151                         unless (
152                                 ($state{'ip1'} eq $IP) ||
153                                 ($state{'ip2'} eq $IP) ||
154                                 ($state{'ip3'} eq $IP)
155                         )
156                         {
157                                 if ($state{'ip1'} eq '') {
158                                         $new_state{'ip1'} = $IP;
159                                 }
160                                 elsif ($state{'ip2'} eq '') {
161                                         $new_state{'ip2'} = $IP;
162                                 }
163                                 elsif ($state{'ip3'} eq '') {
164                                         $new_state{'ip3'} = $IP;
165                                         $new_state{'state'} = STATE->{'ready'};
166                                 }
167                                 else {
168                                         $new_state{'state'} = STATE->{'ready'};
169                                 }
170                                 write_data_file($fh, \%new_state);
171                         }
172                 }
173                 elsif (
174                         (int($state{'state'}) == STATE->{'inactive'}) &&
175                         ($frame == 1)
176                 ) {
177                         # ready to activate?
178                         # NOTE: at this point frame 0 is already ONGed.
179                         my %story;
180                         my $ong_time = int($settings{'firstongtime'});
181                         my $r;
182                         
183                         %story     = read_data_file(DATA_STORY_PATH());
184                         
185                         if (
186                                 (int($story{'state'}) == INTF_STATE->{'>|'} ) &&
187                                 (int($story{'pass'}) == 1)
188                         ) {
189                                 # conditions met; ACTIVATE!
190                                 
191                                 # set initial state
192                                 $state{'state'} = STATE->{'waiting'};
193                                 $state{'last'}  = 1; 
194                                 $state{'ip1'}   = '0.0.0.0';
195                                 $state{'ip2'}   = '0.0.0.0';
196                                 $state{'ip3'}   = '';
197                                 $state{'nextong'} = (int($time / 3600) + int($settings{'firstongtime'})) * 3600 ;
198                                 $state{'ongtime'} = $ong_time;
199                                 
200                                 # prepare to ONG frame 1
201                                 
202                                 $r = ong(
203                                         1,         # frame ID
204                                         $time,     # ONG time,
205                                         $ong_time, # timer
206                                         0,         # update
207                                         0,         # print
208                                         \%settings,
209                                         \%default,
210                                         \%frame_data,
211                                         ''         # %goto_list
212                                 );
213                                 if ($r) {
214                                         $r = write_index(\%state, \%settings);
215                                 }
216                                 if ($r) {
217                                         $r = write_data_file($fh, \%state);
218                                 }
219                                 unless ($r) {
220                                         # FAILED ONG! Story as if it was inactive!
221                                         $state{'state'} = STATE->{'inactive'};
222                                 }
223                         }
224                 }
225         }
226         else {
227                 # FAILED GET STATE! Story as if it was inactive!
228                 $state{'state'} = STATE->{'inactive'};
229         }
230         close ($fh);
231 }
232 else {
233         $state{'state'} = STATE->{'inactive'};
234 }
235
236 $timer   = int($state{'nextong'}) - $time;
237 $ongtime = int($state{'ongtime'});
238 if($ongtime == 0) {
239         $ongtime = int($settings{'ongtime'})
240 }
241
242 $show_command = ($timer < ($ongtime*3600/3));
243 if ($state{'state'} >= STATE->{'ready'}) {
244         $timer_unlocked = 3;
245 }
246 elsif ($state{'ip3'} ne '') {
247         $timer_unlocked = 3;
248 }
249 elsif ($state{'ip2'} ne '') {
250         $timer_unlocked = 2;
251 }
252 elsif ($state{'ip1'} ne '') {
253         $timer_unlocked = 1;
254 }
255 else {
256         $timer_unlocked = 0;
257 }
258
259 if (
260                 $password_ok || (
261                         (int($state{'state'}) >= STATE->{'waiting'}) &&
262                         ($frame <= int($state{'last'})) &&
263                         ($frame >= 0)
264                 )
265         ) {
266         $access = 1;
267         if ($frame > 0) {
268                 $prev_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame-1);
269                 %prev_frame_data = read_data_file($prev_frame_data_path);
270         }
271         $next_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame+1);
272         %next_frame_data = read_data_file($next_frame_data_path);
273         
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);
277 }
278 else {
279         $access = 0;
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);
283 }
284
285 $text_mode = int($cgi{'b'});
286 if($text_mode > TEXT_MODE->{'words'}) {
287         $text_mode = TEXT_MODE->{'normal'};
288 }
289 $words_page = int($cgi{'i'});
290
291 $words_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $frame);
292 %words_data = read_data_file(
293         $words_data_path, # file
294         '', # encoding
295         0,  # no header
296         ($text_mode != TEXT_MODE->{'words'}), # header only
297         1,  # as list
298 );
299
300 print "Content-type: text/html\n";
301 if(!$access) {
302         print "Status: 403 Forbidden\n";
303 }
304 print "\n";
305 if($method eq 'HEAD') {
306         exit;
307 }
308
309 print_viewer_page (
310         \*STDOUT,
311         {
312                 'launch'        => 0,
313                 'frame'         => $frame,
314                 'access'        => $access,
315                 'password_ok'   => $password_ok,
316                 'timer_unlocked'=> $timer_unlocked,
317                 'timer'         => $timer,
318                 'static'        => 0,
319                 'show_command'  => $show_command,
320                 'text_mode'     => $text_mode,
321                 'words_page'    => $words_page
322         },
323         \%state,
324         \%settings,
325         \%frame_data,
326         $access ? \%prev_frame_data : \%frame_data,
327         $access ? \%next_frame_data : \%frame_data,
328         \%words_data,
329 );