]> bicyclesonthemoon.info Git - ott/bsta/blob - info.1.pl
Done comment system
[ott/bsta] / info.1.pl
1 ###RUN_PERL: #!/usr/bin/perl
2
3 # /bsta/i
4 # info.pl is generated from info.1.pl.
5 #
6 # The frame/story info interface
7 #
8 # Copyright (C) 2017, 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 );
35 use bsta_lib (
36         'STATE',
37         'fail_method', 'fail_content_type',
38         'get_password',
39         'merge_settings'
40 );
41
42 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
43
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
50
51 binmode STDIN,  ':encoding(UTF-8)';
52 binmode STDOUT, ':encoding(UTF-8)';
53 binmode STDERR, ':encoding(UTF-8)';
54 # decode_argv();
55
56 my $time = time();
57 srand ($time-$$);
58
59 my %http;
60 my %cgi;
61 my %frame_data;
62 my %next_frame_data;
63 my %default;
64 my %settings;
65 my %state;
66
67 my $method;
68 my $frame = '';
69 my $attachment = '';
70 my $password;
71 my $password_ok;
72 my $access;
73 my $show_command;
74 my $ongtime;
75 my $timer;
76 my $frame_data_path;
77 my $next_frame_data_path;
78 my $ong_state;
79 my $last_frame;
80
81 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
82 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
83
84 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
85         $method = $1;
86 }
87 else{
88         exit fail_method($ENV{'REQUEST_METHOD'}, 'GET, POST, HEAD');
89 }
90
91 %http = read_header_env(\%ENV);
92 %cgi = url_query_decode($ENV{'QUERY_STRING'});
93
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);
98         }
99         # multipart not supported
100         else{
101                 exit fail_content_type($method, $http{'content-type'});
102         }
103 }
104
105 if ($cgi{'f'} =~ /^.+$/) {
106         $frame = int($&);
107 }
108 elsif ($cgi{'i'} =~ /^.+$/) {
109         $attachment = int($&);
110 }
111 elsif ($ENV{'PATH_INFO'} =~ /^\/a(.+)$/) {
112         $attachment = int($1);
113 }
114 elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
115         $frame = int($1);
116 }
117
118 $password = get_password(\%cgi);
119
120 %settings   = read_data_file(DATA_SETTINGS_PATH());
121 %default    = read_data_file(DATA_DEFAULT_PATH());
122 %state      = read_data_file(DATA_STATE_PATH());
123
124 $ong_state  = int($state{'state'});
125 $last_frame = int($state{'last'});
126
127 $password_ok = ($password eq $settings{'password'});
128
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;
133 }
134 # frame info
135 elsif ($frame ne '') {
136         if ($frame < 0) {
137                 $frame = $last_frame + $frame +1;
138         }
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);
145         
146         $timer   = int($state{'nextong'}) - $time;
147         $ongtime = int($state{'ongtime'});
148         if($ongtime == 0) {
149                 $ongtime = int($settings{'ongtime'})
150         }
151         $show_command = ($timer < ($ongtime * 3600 / 3));
152 }
153 # state info, not frame
154 else {
155         unless ($password_ok) {
156                 # just show if IP was saved, not its value
157                 if ($state{'ip1'} ne '') {
158                         $state{'ip1'} = 1;
159                 }
160                 if ($state{'ip2'} ne '') {
161                         $state{'ip2'} = 1;
162                 }
163                 if ($state{'ip3'} ne '') {
164                         $state{'ip3'} = 1;
165                 }
166         }
167         print "Content-type: text/plain\n\n";
168         if ($method eq 'HEAD') {
169                 exit;
170         }
171         write_data_file(
172                 \*STDOUT, \%state, '',
173                 0, 0, 0,
174                 '>>', 1
175         );
176         
177         exit;
178 }
179
180 if (
181         $password_ok || (
182                 ($ong_state >= STATE->{'waiting'}) &&
183                 ($frame <= $last_frame) &&
184                 ($frame >= 0)
185         )
186 ) {
187         $access = 1;
188         
189         if (
190                 ($attachment eq '') &&
191                 ($frame_data{'command'} eq '') && (
192                         $password_ok ||
193                         ($frame < $last_frame) || (
194                                 ($ong_state >= STATE->{'ready'}) &&
195                                 $show_command
196                         )
197                 )
198         ) {
199                 $frame_data{'command'} = $next_frame_data{'title'};
200         }
201 }
202 else {
203         $access = 0;
204         if ($attachment ne '') {
205                 %frame_data = ();
206         }
207         else {
208                 %frame_data = read_data_file(DATA_NOACCESS_PATH());
209                 %frame_data = merge_settings(\%default, \%frame_data);
210         }
211 }
212 if (
213         ($frame_data{'frame'} eq '') &&
214         ($attachment eq '')
215 ) {
216         $frame_data{'frame'} = sprintf($settings{'frame'}, $frame, $frame_data{'ext'});
217 }
218
219 print "Content-type: text/plain\n";
220 if (!$access) {
221         print "Status: 403 Forbidden\n";
222 }
223 print "\n";
224 if($method eq 'HEAD') {
225         exit;
226 }
227 write_data_file(
228         \*STDOUT, \%frame_data, '',
229         0, 0, 0,
230         '>>', 1
231 );