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