1 ###RUN_PERL: #!/usr/bin/perl
4 # info.pl is generated from info.1.pl.
6 # The frame/story info interface
8 # Copyright (C) 2017, 2023, 2024 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
33 'read_data_file', 'write_data_file',
39 'fail_method', 'fail_content_type',
44 ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
46 ###PERL_DATA_PATH: DATA_PATH = /botm/data/bsta
47 ###PERL_DATA_ATTACH_PATH: DATA_ATTACH_PATH = /botm/data/bsta/a
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_WORDS_PATH: DATA_WORDS_PATH = /botm/data/bsta/words/
54 binmode STDIN, ':encoding(UTF-8)';
55 binmode STDOUT, ':encoding(UTF-8)';
56 binmode STDERR, ':encoding(UTF-8)';
81 my $next_frame_data_path;
85 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
86 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
88 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
92 exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
95 %http = read_header_env(\%ENV);
96 %cgi = url_query_decode($ENV{'QUERY_STRING'});
98 if ($method eq 'POST') {
99 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
100 my %cgi_post = url_query_decode( <STDIN> );
101 %cgi = merge_settings(\%cgi, \%cgi_post);
103 # multipart not supported
105 exit fail_content_type($method, $http{'content-type'});
109 if ($cgi{'f'} =~ /^.+$/) {
112 elsif ($cgi{'a'} =~ /^.+$/) {
113 $attachment = int($&);
115 elsif ($cgi{'i'} =~ /^.+$/) {
116 $attachment = int($&);
118 elsif ($cgi{'w'} =~ /^.+$/) {
121 elsif ($ENV{'PATH_INFO'} =~ /^\/a(.+)$/) {
122 $attachment = int($1);
124 elsif ($ENV{'PATH_INFO'} =~ /^\/w(.+)$/) {
127 elsif ($ENV{'PATH_INFO'} =~ /^\/f?(.+)$/) {
131 $password = get_password(\%cgi);
133 %settings = read_data_file(DATA_SETTINGS_PATH());
134 %default = read_data_file(DATA_DEFAULT_PATH());
135 %state = read_data_file(DATA_STATE_PATH());
137 $ong_state = int($state{'state'});
138 $last_frame = int($state{'last'});
140 $password_ok = ($password eq $settings{'password'});
142 # comment info, not frame
144 $info_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $words);
145 %info_data = read_data_file($info_data_path);
146 if ($words =~ /^[0-9]+$/) {
148 if ($info_data{'posts'} eq '') {
149 $info_data{'posts'} = 0;
153 $frame = ($info_data{'frame'} ne '') ? int($info_data{'frame'}) : -1;
154 unless ($password_ok) {
155 delete($info_data{'password'});
156 delete($info_data{'key'});
160 # attachment info, not frame
161 elsif ($attachment ne '') {
162 %info_data = read_data_file(DATA_ATTACH_PATH().$attachment);
163 $frame = ($info_data{'frame'} ne '') ? int($info_data{'frame'}) : -1;
166 elsif ($frame ne '') {
168 $frame = $last_frame + $frame +1;
170 $info_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
171 $next_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame+1);
172 %info_data = read_data_file($info_data_path);
173 %next_frame_data = read_data_file($next_frame_data_path);
174 %info_data = merge_settings(\%default, \%info_data);
175 %next_frame_data = merge_settings(\%default, \%next_frame_data);
177 $timer = int($state{'nextong'}) - $time;
178 $ongtime = int($state{'ongtime'});
180 $ongtime = int($settings{'ongtime'})
182 $show_command = ($timer < ($ongtime * 3600 / 3));
184 # state info, not frame
186 unless ($password_ok) {
187 # just show if IP was saved, not its value
188 if ($state{'ip1'} ne '') {
191 if ($state{'ip2'} ne '') {
194 if ($state{'ip3'} ne '') {
198 print "Content-type: text/plain; charset=UTF-8\n\n";
199 if ($method eq 'HEAD') {
203 \*STDOUT, \%state, '',
213 ($ong_state >= STATE->{'waiting'}) &&
214 ($frame <= $last_frame) &&
222 ($attachment eq '') &&
223 ($info_data{'command'} eq '') && (
225 ($frame < $last_frame) || (
226 ($ong_state >= STATE->{'ready'}) &&
231 $info_data{'command'} = $next_frame_data{'title'};
236 if (($attachment ne '') || ($words ne '')) {
240 %info_data = read_data_file(DATA_NOACCESS_PATH());
241 %info_data = merge_settings(\%default, \%info_data);
245 ($attachment eq '') &&
248 if ($info_data{'frame'} eq '') {
249 $info_data{'frame'} = sprintf($settings{'frame'}, $frame, $info_data{'ext'});
251 if ($info_data{'page'} eq '') {
252 unless (($access) && ($frame < $last_frame)) {
253 $info_data{'page'} = '';
255 elsif ($frame == 0) {
256 $info_data{'page'} = 'index.htm';
259 $info_data{'page'} = sprintf($settings{'frame'}, $frame, 'htm');
264 print "Content-type: text/plain; charset=UTF-8\n";
266 print http_header_status(HTTP_STATUS->{'forbidden'});
269 if($method eq 'HEAD') {
273 \*STDOUT, \%info_data, '',