]> bicyclesonthemoon.info Git - ott/bsta/blob - info.1.pl
Adapted for new config tool.
[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  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 warnings;
25 ###PERL_LIB: use lib /botm/lib/bsta
26 use bsta_lib qw(failpage gethttpheader getcgi readdatafile printdatafile);
27 use File::Copy;
28
29 ###PERL_DATA_PATH:          DATA_PATH          = /botm/data/bsta
30 ###PERL_DATA_DEFAULT_PATH:  DATA_DEFAULT_PATH  = /botm/data/bsta/default
31 ###PERL_DATA_NOACCESS_PATH: DATA_NOACCESS_PATH = /botm/data/bsta/noaccess
32 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
33 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
34
35 my %http;
36 my %cgi;
37 my %framedata;
38 my %nextframedata;
39 my %default;
40 my %settings;
41 my %state;
42
43 my $time = time();
44 srand ($time-$$);
45
46 my $method;
47 my $frame;
48 my $password;
49 my $passwordOK;
50 my $access;
51 my $showcommand;
52 my $ongtime;
53 my $seconds;
54
55 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
56 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
57
58 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
59         $method=$1;
60 }
61 else{
62         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);
63 }
64
65 %http = gethttpheader (\%ENV);
66 %cgi = getcgi($ENV{'QUERY_STRING'});
67
68 if ($method eq 'POST') {
69         if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
70                 my %cgipost=getcgi( <STDIN> );
71                 foreach my $ind (keys %cgipost) {
72                         $cgi{$ind}=$cgipost{$ind};
73                 }
74         }
75         # multipart not supported
76         else{
77                 exit failpage("Status: 415 Unsupported Media Type\n","415 Unsupported Media Type","Unsupported Content-type: $http{'content-type'}.");
78         }
79 }
80
81 if ($cgi{'f'} =~ /^(.+)$/) {
82         $frame=int($1);
83 }
84 elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
85         $frame=int($1);
86 }
87 else {
88         $frame = '';
89 }
90
91 if ($cgi{'p'} =~ /^(.+)$/) {
92         $password=$1;
93 }
94 else {
95         $password='';
96 }
97
98 %settings=readdatafile(DATA_SETTINGS_PATH);
99 %default=readdatafile(DATA_DEFAULT_PATH);
100 %framedata=readdatafile(DATA_PATH.$frame);
101 %state=readdatafile(DATA_STATE_PATH);
102 if($password eq $settings{'password'}){
103         $passwordOK = 1;
104 }
105 else{
106         $passwordOK = 0;
107 }
108
109 if ($frame eq '') {
110         unless($passwordOK) {
111                 if ($state{'ip1'} ne '') {
112                         $state{'ip1'}=1;
113                 }
114                 if ($state{'ip2'} ne '') {
115                         $state{'ip2'}=1;
116                 }
117                 if ($state{'ip3'} ne '') {
118                         $state{'ip3'}=1;
119                 }
120         }
121         print "Content-type: text/plain\n\n";
122         if($method eq 'HEAD') {
123                 exit;
124         }
125         printdatafile(%state);
126 }
127
128 else {
129         if($frame<0) {
130                 $frame = int($state{'last'}) + $frame +1;
131                 %framedata=readdatafile(DATA_PATH.$frame);
132         }
133         
134         %nextframedata=readdatafile(DATA_PATH.($frame+1));
135         
136         foreach my $ind (keys %default) {
137                 unless(defined($framedata{$ind})){
138                         $framedata{$ind}=$default{$ind};
139                 }
140                 unless(defined($nextframedata{$ind})){
141                         $nextframedata{$ind}=$default{$ind};
142                 }
143         }
144         $seconds=int($state{'nextong'})-$time;
145         $ongtime=int($state{'ongtime'});
146         if($ongtime == 0) {
147                 $ongtime=int($settings{'ongtime'})
148         }
149         $showcommand = ($seconds < ($ongtime*3600/3));
150         
151         if ($passwordOK || (int($state{'state'}) >= 1 && $frame <= int($state{'last'}) && $frame >= 0)) {
152                 $access=1;
153                 
154                 if ($passwordOK || $frame<int($state{'last'}) || (int($state{'state'}) >= 2 && $showcommand)) {
155                         $framedata{'command'}=$nextframedata{'title'};
156                 }
157                 $framedata{'frame'}=sprintf($settings{'frame'},$frame,$framedata{'ext'});
158         }
159         else {
160                 $access=0;
161                 %framedata = readdatafile(DATA_NOACCESS_PATH);
162                 foreach my $ind (keys %default) {
163                         unless(defined($framedata{$ind})){
164                                 $framedata{$ind}=$default{$ind};
165                         }
166                 }
167         }
168         
169         # $framedata{'frame'}=sprintf($settings{'frame'},$frame,$framedata{'ext'});
170         
171         print "Content-type: text/plain\n";
172         if(!$access) {
173                 print "Status: 403 Forbidden\n";
174         }
175         print "\n";
176         if($method eq 'HEAD') {
177                 exit;
178         }
179         printdatafile(%framedata);
180 }