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