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
31 'read_data_file', 'write_data_file',
37 'fail_method', 'fail_content_type',
42 ###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
44 ###PERL_DATA_PATH: DATA_PATH = /botm/data/bsta
45 ###PERL_DATA_ATTACH_PATH: DATA_ATTACH_PATH = /botm/data/bsta/a
46 ###PERL_DATA_DEFAULT_PATH: DATA_DEFAULT_PATH = /botm/data/bsta/default
47 ###PERL_DATA_NOACCESS_PATH: DATA_NOACCESS_PATH = /botm/data/bsta/noaccess
48 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
49 ###PERL_DATA_STATE_PATH: DATA_STATE_PATH = /botm/data/bsta/state
51 binmode STDIN, ':encoding(UTF-8)';
52 binmode STDOUT, ':encoding(UTF-8)';
53 binmode STDERR, ':encoding(UTF-8)';
77 my $next_frame_data_path;
81 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
82 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
84 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
88 exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
91 %http = read_header_env(\%ENV);
92 %cgi = url_query_decode($ENV{'QUERY_STRING'});
94 if ($method eq 'POST') {
95 if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
96 my %cgi_post = url_query_decode( <STDIN> );
97 %cgi = merge_settings(\%cgi, \%cgi_post);
99 # multipart not supported
101 exit fail_content_type($method, $http{'content-type'});
105 if ($cgi{'f'} =~ /^.+$/) {
108 elsif ($cgi{'i'} =~ /^.+$/) {
109 $attachment = int($&);
111 elsif ($ENV{'PATH_INFO'} =~ /^\/a(.+)$/) {
112 $attachment = int($1);
114 elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
118 $password = get_password(\%cgi);
120 %settings = read_data_file(DATA_SETTINGS_PATH());
121 %default = read_data_file(DATA_DEFAULT_PATH());
122 %state = read_data_file(DATA_STATE_PATH());
124 $ong_state = int($state{'state'});
125 $last_frame = int($state{'last'});
127 $password_ok = ($password eq $settings{'password'});
129 # attachment info, not frame
130 if ($attachment ne '') {
131 %frame_data = read_data_file(DATA_ATTACH_PATH().$attachment);
132 $frame = ($frame_data{'frame'} ne '') ? int($frame_data{'frame'}) : -1;
135 elsif ($frame ne '') {
137 $frame = $last_frame + $frame +1;
139 $frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame);
140 $next_frame_data_path = join_path(PATH_SEPARATOR(), DATA_PATH(), $frame+1);
141 %frame_data = read_data_file($frame_data_path);
142 %next_frame_data = read_data_file($next_frame_data_path);
143 %frame_data = merge_settings(\%default, \%frame_data);
144 %next_frame_data = merge_settings(\%default, \%next_frame_data);
146 $timer = int($state{'nextong'}) - $time;
147 $ongtime = int($state{'ongtime'});
149 $ongtime = int($settings{'ongtime'})
151 $show_command = ($timer < ($ongtime * 3600 / 3));
153 # state info, not frame
155 unless ($password_ok) {
156 # just show if IP was saved, not its value
157 if ($state{'ip1'} ne '') {
160 if ($state{'ip2'} ne '') {
163 if ($state{'ip3'} ne '') {
167 print "Content-type: text/plain\n\n";
168 if ($method eq 'HEAD') {
172 \*STDOUT, \%state, '',
182 ($ong_state >= STATE->{'waiting'}) &&
183 ($frame <= $last_frame) &&
190 ($attachment eq '') &&
191 ($frame_data{'command'} eq '') && (
193 ($frame < $last_frame) || (
194 ($ong_state >= STATE->{'ready'}) &&
199 $frame_data{'command'} = $next_frame_data{'title'};
204 if ($attachment ne '') {
208 %frame_data = read_data_file(DATA_NOACCESS_PATH());
209 %frame_data = merge_settings(\%default, \%frame_data);
213 ($frame_data{'frame'} eq '') &&
216 $frame_data{'frame'} = sprintf($settings{'frame'}, $frame, $frame_data{'ext'});
219 print "Content-type: text/plain\n";
221 print "Status: 403 Forbidden\n";
224 if($method eq 'HEAD') {
228 \*STDOUT, \%frame_data, '',