]> bicyclesonthemoon.info Git - ott/bsta/blob - info.1.pl
Initial state as of 31.08.2023
[ott/bsta] / info.1.pl
1 ###PERL;
2 #
3 # /bsta/i
4 # info.pl is generated from info.1.pl.
5 # 05.06.2017
6 #
7 # The frame/story info interface
8 #
9 #    Copyright (C) 2017  Balthasar SzczepaƄski
10 #
11 #    This program is free software: you can redistribute it and/or modify
12 #    it under the terms of the GNU Affero General Public License as
13 #    published by the Free Software Foundation, either version 3 of the
14 #    License, or (at your option) any later version.
15 #
16 #    This program is distributed in the hope that it will be useful,
17 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
18 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 #    GNU Affero General Public License for more details.
20 #
21 #    You should have received a copy of the GNU Affero General Public License
22 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 use strict;
25 #use warnings;
26 ###LIB;
27 use bsta_lib qw(failpage gethttpheader getcgi readdatafile printdatafile);
28 use File::Copy;
29
30 ###DATA_PATH;
31 ###DEFAULT_PATH;
32 ###SETTINGS_PATH;
33 ###STATE_PATH;
34 ###NOACCESS_PATH;
35
36 my %http;
37 my %cgi;
38 my %framedata;
39 my %nextframedata;
40 my %default;
41 my %settings;
42 my %state;
43
44 my $time = time();
45 srand ($time-$$);
46
47 my $method;
48 my $frame;
49 my $password;
50 my $passwordOK;
51 my $access;
52 my $showcommand;
53 my $ongtime;
54 my $seconds;
55
56 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
57 ###PATH;
58
59 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
60         $method=$1;
61 }
62 else{
63         exit failpage("Status: 405 Method Not Allowed\nAllow: GET, POST, HEAD\n","405 Method Not Allowed","The interface does not support the $ENV{'REQUEST_METHOD'} method.",$method);
64 }
65
66 %http = gethttpheader (\%ENV);
67 %cgi = getcgi($ENV{'QUERY_STRING'});
68
69 if ($method eq 'POST') {
70         if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
71                 my %cgipost=getcgi( <STDIN> );
72                 foreach my $ind (keys %cgipost) {
73                         $cgi{$ind}=$cgipost{$ind};
74                 }
75         }
76         # multipart not supported
77         else{
78                 exit failpage("Status: 415 Unsupported Media Type\n","415 Unsupported Media Type","Unsupported Content-type: $http{'content-type'}.");
79         }
80 }
81
82 if ($cgi{'f'} =~ /^(.+)$/) {
83         $frame=int($1);
84 }
85 elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
86         $frame=int($1);
87 }
88 else {
89         $frame = '';
90 }
91
92 if ($cgi{'p'} =~ /^(.+)$/) {
93         $password=$1;
94 }
95 else {
96         $password='';
97 }
98
99 %settings=readdatafile(SETTINGS_PATH);
100 %default=readdatafile(DEFAULT_PATH);
101 %framedata=readdatafile(DATA_PATH.$frame);
102 %state=readdatafile(STATE_PATH);
103 if($password eq $settings{'password'}){
104         $passwordOK = 1;
105 }
106 else{
107         $passwordOK = 0;
108 }
109
110 if ($frame eq '') {
111         unless($passwordOK) {
112                 if ($state{'ip1'} ne '') {
113                         $state{'ip1'}=1;
114                 }
115                 if ($state{'ip2'} ne '') {
116                         $state{'ip2'}=1;
117                 }
118                 if ($state{'ip3'} ne '') {
119                         $state{'ip3'}=1;
120                 }
121         }
122         print "Content-type: text/plain\n\n";
123         if($method eq 'HEAD') {
124                 exit;
125         }
126         printdatafile(%state);
127 }
128
129 else {
130         if($frame<0) {
131                 $frame = int($state{'last'}) + $frame +1;
132                 %framedata=readdatafile(DATA_PATH.$frame);
133         }
134         
135         %nextframedata=readdatafile(DATA_PATH.($frame+1));
136         
137         foreach my $ind (keys %default) {
138                 unless(defined($framedata{$ind})){
139                         $framedata{$ind}=$default{$ind};
140                 }
141                 unless(defined($nextframedata{$ind})){
142                         $nextframedata{$ind}=$default{$ind};
143                 }
144         }
145         $seconds=int($state{'nextong'})-$time;
146         $ongtime=int($state{'ongtime'});
147         if($ongtime == 0) {
148                 $ongtime=int($settings{'ongtime'})
149         }
150         $showcommand = ($seconds < ($ongtime*3600/3));
151         
152         if ($passwordOK || (int($state{'state'}) >= 1 && $frame <= int($state{'last'}) && $frame >= 0)) {
153                 $access=1;
154                 
155                 if ($passwordOK || $frame<int($state{'last'}) || (int($state{'state'}) >= 2 && $showcommand)) {
156                         $framedata{'command'}=$nextframedata{'title'};
157                 }
158                 $framedata{'frame'}=sprintf($settings{'frame'},$frame,$framedata{'ext'});
159         }
160         else {
161                 $access=0;
162                 %framedata = readdatafile(NOACCESS_PATH);
163                 foreach my $ind (keys %default) {
164                         unless(defined($framedata{$ind})){
165                                 $framedata{$ind}=$default{$ind};
166                         }
167                 }
168         }
169         
170         # $framedata{'frame'}=sprintf($settings{'frame'},$frame,$framedata{'ext'});
171         
172         print "Content-type: text/plain\n";
173         if(!$access) {
174                 print "Status: 403 Forbidden\n";
175         }
176         print "\n";
177         if($method eq 'HEAD') {
178                 exit;
179         }
180         printdatafile(%framedata);
181 }