]> bicyclesonthemoon.info Git - ott/bsta/blob - info.1.pl
hyperlinks to static previous/next page
[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         'HTTP_STATUS',
31         'http_header_status',
32         'read_header_env',
33         'read_data_file', 'write_data_file',
34         'url_query_decode',
35         'join_path'
36 );
37 use bsta_lib (
38         'STATE',
39         'fail_method', 'fail_content_type',
40         'get_password',
41         'merge_settings'
42 );
43
44 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
45
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/
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 %info_data;
65 my %next_frame_data;
66 my %default;
67 my %settings;
68 my %state;
69
70 my $method;
71 my $frame = '';
72 my $attachment = '';
73 my $words = '';
74 my $password;
75 my $password_ok;
76 my $access;
77 my $show_command;
78 my $ongtime;
79 my $timer;
80 my $info_data_path;
81 my $next_frame_data_path;
82 my $ong_state;
83 my $last_frame;
84
85 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
86 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
87
88 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
89         $method = $1;
90 }
91 else{
92         exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
93 }
94
95 %http = read_header_env(\%ENV);
96 %cgi = url_query_decode($ENV{'QUERY_STRING'});
97
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);
102         }
103         # multipart not supported
104         else{
105                 exit fail_content_type($method, $http{'content-type'});
106         }
107 }
108
109 if ($cgi{'f'} =~ /^.+$/) {
110         $frame = int($&);
111 }
112 elsif ($cgi{'a'} =~ /^.+$/) {
113         $attachment = int($&);
114 }
115 elsif ($cgi{'i'} =~ /^.+$/) {
116         $attachment = int($&);
117 }
118 elsif ($cgi{'w'} =~ /^.+$/) {
119         $words = $&;
120 }
121 elsif ($ENV{'PATH_INFO'} =~ /^\/a(.+)$/) {
122         $attachment = int($1);
123 }
124 elsif ($ENV{'PATH_INFO'} =~ /^\/w(.+)$/) {
125         $words = $1;
126 }
127 elsif ($ENV{'PATH_INFO'} =~ /^\/f?(.+)$/) {
128         $frame = int($1);
129 }
130
131 $password = get_password(\%cgi);
132
133 %settings   = read_data_file(DATA_SETTINGS_PATH());
134 %default    = read_data_file(DATA_DEFAULT_PATH());
135 %state      = read_data_file(DATA_STATE_PATH());
136
137 $ong_state  = int($state{'state'});
138 $last_frame = int($state{'last'});
139
140 $password_ok = ($password eq $settings{'password'});
141
142 # comment info, not frame
143 if ($words ne '') {
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]+$/) {
147                 $frame = int($&);
148                 if ($info_data{'posts'} eq '') {
149                         $info_data{'posts'} = 0;
150                 }
151         }
152         else {
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'});
157                 }
158         }
159 }
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;
164 }
165 # frame info
166 elsif ($frame ne '') {
167         if ($frame < 0) {
168                 $frame = $last_frame + $frame +1;
169         }
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);
176         
177         $timer   = int($state{'nextong'}) - $time;
178         $ongtime = int($state{'ongtime'});
179         if($ongtime == 0) {
180                 $ongtime = int($settings{'ongtime'})
181         }
182         $show_command = ($timer < ($ongtime * 3600 / 3));
183 }
184 # state info, not frame
185 else {
186         unless ($password_ok) {
187                 # just show if IP was saved, not its value
188                 if ($state{'ip1'} ne '') {
189                         $state{'ip1'} = 1;
190                 }
191                 if ($state{'ip2'} ne '') {
192                         $state{'ip2'} = 1;
193                 }
194                 if ($state{'ip3'} ne '') {
195                         $state{'ip3'} = 1;
196                 }
197         }
198         print "Content-type: text/plain; charset=UTF-8\n\n";
199         if ($method eq 'HEAD') {
200                 exit;
201         }
202         write_data_file(
203                 \*STDOUT, \%state, '',
204                 0, 0, 0,
205                 '>>', 1
206         );
207         
208         exit;
209 }
210
211 if (
212         $password_ok || (
213                 ($ong_state >= STATE->{'waiting'}) &&
214                 ($frame <= $last_frame) &&
215                 ($frame >= 0)
216         )
217 ) {
218         $access = 1;
219         
220         if (
221                 ($words eq '') &&
222                 ($attachment eq '') &&
223                 ($info_data{'command'} eq '') && (
224                         $password_ok ||
225                         ($frame < $last_frame) || (
226                                 ($ong_state >= STATE->{'ready'}) &&
227                                 $show_command
228                         )
229                 )
230         ) {
231                 $info_data{'command'} = $next_frame_data{'title'};
232         }
233 }
234 else {
235         $access = 0;
236         if (($attachment ne '') || ($words ne '')) {
237                 %info_data = ();
238         }
239         else {
240                 %info_data = read_data_file(DATA_NOACCESS_PATH());
241                 %info_data = merge_settings(\%default, \%info_data);
242         }
243 }
244 if (
245         ($info_data{'frame'} eq '') &&
246         ($attachment eq '') &&
247         ($words eq '')
248 ) {
249         $info_data{'frame'} = sprintf($settings{'frame'}, $frame, $info_data{'ext'});
250 }
251
252 print "Content-type: text/plain; charset=UTF-8\n";
253 if (!$access) {
254         print http_header_status(HTTP_STATUS->{'forbidden'});
255 }
256 print "\n";
257 if($method eq 'HEAD') {
258         exit;
259 }
260 write_data_file(
261         \*STDOUT, \%info_data, '',
262         0, 0, 0,
263         '>>', 1
264 );