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