]> bicyclesonthemoon.info Git - ott/bsta/blob - opomba.1.pl
improve GOTO handling
[ott/bsta] / opomba.1.pl
1 ###RUN_PERL: #!/usr/bin/perl
2
3 # /bsta/w
4 # opomba is generated from opomba.1.pl.
5 #
6 # The comment posting interface
7 #
8 # Copyright (C) 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         'read_header_env', 'url_query_decode',
32         'write_data_file',
33         'html_entity_encode_dec',
34         'open_encoded',
35         'join_path',
36         'merge_url',
37         'make_id',
38         'http_header_status'
39 );
40 use bsta_lib (
41         'TEXT_MODE', 'STATE',
42         'get_password',
43         'fail_method', 'fail_content_type',
44         'redirect',
45         'print_html_start', 'print_html_end',
46         'print_html_head_start', 'print_html_head_end',
47         'print_html_body_start', 'print_html_body_end',
48         'bb_to_html', 'eval_bb',
49         'merge_settings',
50         'write_index', 'write_static_viewer_page',
51         'read_settings', 'read_state',
52         'read_words', 'write_words',
53         'read_words_list', 'write_words_list'
54         
55 );
56
57 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
58
59 ###PERL_CGI_VIEWER_PATH:    CGI_VIEWER_PATH    = /bsta/v
60 ###PERL_CGI_WORDS_PATH:     CGI_WORDS_PATH     = /bsta/w
61
62 ###PERL_DATA_WORDS_PATH:    DATA_WORDS_PATH    = /botm/data/bsta/words/
63
64 ###PERL_LOG_SPAM_PATH:      LOG_SPAM_PATH      = /botm/log/bsta/words_spam.log
65 ###PERL_LOG_WORDS_PATH:     LOG_WORDS_PATH     = /botm/log/bsta/words.log
66
67 ###PERL_WEBSITE_NAME:       WEBSITE_NAME       = Bicycles on the Moon
68
69 ###PERL_COMMENT_PAGE_LENGTH:COMMENT_PAGE_LENGTH= 16
70
71 binmode STDIN,  ':encoding(UTF-8)';
72 binmode STDOUT, ':encoding(UTF-8)';
73 binmode STDERR, ':encoding(UTF-8)';
74 # decode_argv();
75
76 my $time = time();
77 srand ($time-$$);
78
79 my %http;
80 my %cgi;
81 my %state;
82 my %settings;
83 my %words_data;
84 my %post_data;
85 my %last_post_data;
86
87 my @post_list;
88
89 my $method;
90 my $frame;
91 my $ID;
92 my $last_ID;
93 my $password;
94 my $password_ok;
95 my $access;
96 my $edit = 0;
97 my $remove = 0;
98 my $post = 0;
99 my $quote;
100 my $words_data_path;
101 my $index;
102 my $page;
103 my $ong_state;
104 my $last_frame;
105 my $fh;
106 my $r;
107
108
109 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
110 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
111
112 if ($ENV{'REQUEST_METHOD'} =~ /^(HEAD|GET|POST)$/) {
113         $method = $1;
114 }
115 else {
116         exit fail_method($ENV{'REQUEST_METHOD'}, ['GET', 'POST', 'HEAD']);
117 }
118
119 %http = read_header_env(\%ENV);
120 %cgi = url_query_decode($ENV{'QUERY_STRING'});
121
122 if ($method eq 'POST') {
123         if ($http{'content-type'} eq 'application/x-www-form-urlencoded') {
124                 my %cgi_post = url_query_decode( <STDIN> );
125                 %cgi = merge_settings(\%cgi, \%cgi_post);
126         }
127         # multipart not supported
128         else {
129                 exit fail_content_type($method, $http{'content-type'});
130         }
131 }
132
133 if ($ENV{'PATH_INFO'} =~ /^\/([0-9]+)$/) {
134         $frame = int($1);
135 }
136 elsif ($ENV{'PATH_INFO'} =~ /^\/(.+)$/) {
137         $ID = $1;
138 }
139 if ($cgi{'f'} =~ /^.+$/) {
140         $frame = int($&);
141 }
142 if ($cgi{'i'} =~ /^.+$/) {
143         $ID = $&;
144 }
145 $password = get_password(\%cgi);
146
147 %settings = read_settings();
148 %state    = read_state();
149 $ong_state = int($state{'state'});
150 $last_frame = int($state{'last'});
151
152 $password_ok = ($password eq $settings{'password'});
153
154 if ($cgi{'post'} ne '') {
155         $post = 1;
156 }
157 elsif ($cgi{'edit'} ne '') {
158         $edit = 1;
159         if ($ID eq '') {
160                 $ID = $cgi{'edit'};
161         }
162 }
163 elsif ($cgi{'remove'} ne '') {
164         $remove = 1;
165         if ($ID eq '') {
166                 $ID = $cgi{'remove'};
167         }
168 }
169 else {
170         $edit = 1;
171         if ($cgi{'quote'} ne '') {
172                 $quote = $cgi{'quote'};
173         }
174 }
175
176 if ($ID ne '') {
177         %post_data = read_words($ID);
178         if ($post_data{'frame'} ne '') {
179                 $frame = int($post_data{'frame'});
180         }
181 }
182
183 unless ($frame ne '') {
184         exit output(0, HTTP_STATUS->{'bad_request'}, 'Frame ID not specified.');
185 }
186 $access = (
187         $password_ok || (
188                 ($ong_state >= STATE->{'waiting'}) &&
189                 ($frame <= $last_frame)
190         )
191 );
192 unless ($access) {
193         exit output(0, HTTP_STATUS->{'forbidden'}, 'Not allowed to post this here now');
194 }
195
196 $words_data_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $frame);
197
198 unless (open_encoded($fh, "+<:encoding(UTF-8)", $words_data_path)) {
199         unless (open_encoded($fh, "+>:encoding(UTF-8)", $words_data_path)) {
200                 exit output(0, '500 Internal Server Error', 'Failed opening data file.', 1);
201         }
202 }
203 unless (flock($fh, 2)) {
204         exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed locking data file.', 1);
205 }
206
207 %words_data = read_words_list(
208         $fh, # file
209         0,   # header only
210 );
211
212 @post_list = @{$words_data{'content'}};
213
214 for (my $i=0; $i< scalar(@post_list); $i +=1) {
215         if ($post_list[$i] eq $ID) {
216                 $index = $i;
217                 $page = int($index / COMMENT_PAGE_LENGTH());
218                 last;
219         }
220 }
221
222 if ($remove || ($ID ne '')) {
223         unless ($index ne '') {
224                 close($fh);
225                 exit output(0, HTTP_STATUS->{'not_found'}, $remove ? 'Nothing to remove.' : 'No such message.');
226         }
227         unless ($cgi{'key'} eq $post_data{'key'}) {
228                 close($fh);
229                 exit output(0, HTTP_STATUS->{'bad_request'}, 'Invalid request.');
230         }
231 }
232 if ($remove) {
233         unless (($method eq 'POST') && ($cgi{'i'} ne '')) { # followed a link, not confirmed yet
234                 close($fh);
235                 exit output(0, '', '', 1);
236         }
237 }
238 else {
239         unless (($method eq 'POST') && $post) { # followed a link, not confirmed yet
240                 close($fh);
241                 exit output(0, '', '', 1);
242         }
243 }
244 if (!$remove) {
245         unless ($cgi{'words'} ne '') {
246                 exit output(0, HTTP_STATUS->{'bad_request'}, 'Where are your words?', 1);
247         }
248 }
249 unless ($cgi{'username'} ne '') {
250         close($fh);
251         exit output(0, HTTP_STATUS->{'bad_request'}, 'Missing user name.', 1);
252 }
253 if ($remove || ($ID ne '')) {
254         unless ($cgi{'username'} eq $post_data{'name'}) {
255                 close($fh);
256                 exit output(0, HTTP_STATUS->{'forbidden'}, 'Wrong user name.', 1);
257         }
258 }
259 if ($remove || ($ID ne '')) {
260                 unless ($cgi{'password'} ne '') {
261                 close($fh);
262                 exit output(0, HTTP_STATUS->{'bad_request'}, 'Missing password.', 1);
263         }
264         unless (
265                 ($cgi{'password'} eq $post_data{'password'}) || (
266                         ($cgi{'password'} eq $settings{'password'}) &&
267                         $password_ok
268                 )
269         ) {
270                 close($fh);
271                 exit output(0, HTTP_STATUS->{'forbidden'}, 'Wrong password.', 1);
272         }
273         if ($password_ok) {
274                 $cgi{'password'} = $post_data{'password'};
275         }
276 }
277 unless ($cgi{'password2'} eq '') {
278         close($fh);
279         # no error code to confuse spambot :)
280         output(0, '', 'Please don\'t write anything in the place which should remain empty.', 1);
281         if (open_encoded($fh, ">>:encoding(UTF-8)", LOG_SPAM_PATH())) {
282                 $cgi{'content'} = $cgi{'words'};
283                 $cgi{'empty'} = $cgi{'password2'};
284                 delete($cgi{'words'});
285                 delete ($cgi{'password'});
286                 delete ($cgi{'password2'});
287                 print $fh "$time SPAM $ID\n";
288                 write_data_file(
289                         $fh, \%cgi, '',
290                         0, 0, 0,
291                         '>>', 1
292                 );
293                 print $fh "\n\n";
294                 close ($fh);
295         }
296         exit;
297 }
298
299 # all conditions fulfilled
300
301 if ($remove) {
302         splice @post_list, $index, 1;
303         $words_data{'posts'} = scalar(@post_list);
304         $words_data{'content'} = \@post_list;
305         
306         $r = write_words_list($fh, \%words_data);
307         unless ($r) {
308                 close($fh);
309                 exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed writing data file.');
310         }
311         
312         close ($fh);
313         
314         if (open_encoded($fh, ">>:encoding(UTF-8)", LOG_WORDS_PATH())) {
315                 delete ($post_data{'password'});
316                 print $fh "$time REMOVE $ID\n";
317                 write_data_file(
318                         $fh, \%post_data, '',
319                         0, 0, 0,
320                         '>>', 1
321                 );
322                 print $fh "\n\n";
323                 close ($fh);
324         }
325         
326         $ID = 'insw';
327         exit output(1);
328 }
329
330 if ($ID eq '') {
331         $ID = make_id($frame, 1);
332 }
333 if ($index eq '') {
334         $index = scalar(@post_list);
335         $page = int($index / COMMENT_PAGE_LENGTH());
336         if ($index > 0) {
337                 $last_ID = $post_list[-1];
338                 %last_post_data = read_words($last_ID);
339                 if (
340                         ($cgi{'username'} eq $last_post_data{'name'   }) &&
341                         ($cgi{'words'   } eq $last_post_data{'content'})
342                 ) { # duplicate post
343                         $index -= 1;
344                         $page = int($index / COMMENT_PAGE_LENGTH());
345                         $ID = $last_ID;
346                         close ($fh);
347                         exit output(1);
348                 }
349         }
350         push @post_list, $ID;
351 }
352 $words_data{'posts'} = scalar(@post_list);
353 $words_data{'content'} = \@post_list;
354
355 $post_data{'frame'} = $frame;
356 $post_data{'name'} = $cgi{'username'};
357 $post_data{'password'} = $cgi{'password'};
358 if ($post_data{'posttime'} eq '') {
359         $post_data{'posttime'} = $time;
360 }
361 else {
362         $post_data{'edittime'} = $time;
363 }
364 if ($post_data{'key'} eq '') {
365         my $new_key = '';
366         for (my $i=1; $i<16; $i+=1) {
367                 $new_key .= sprintf('%02X', int(rand(0x100)));
368         }
369         $post_data{'key'} = $new_key;
370 }
371 $post_data{'content'} = $cgi{'words'};
372
373 $r = write_words($ID, \%post_data);
374 unless ($r) {
375         close($fh);
376         exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed writing post file.', 1, 0);
377 }
378
379 $r = write_words_list($fh, \%words_data);
380 unless ($r) {
381         close($fh);
382         exit output(0, HTTP_STATUS->{'internal_server_error'}, 'Failed writing data file.', 1, 0);
383 }
384
385 close($fh);
386
387 if (($frame == 0) && ($ong_state > STATE->{'inactive'})) {
388         write_index(\%state, \%settings);
389 }
390 elsif ($frame >= 1) {
391         write_static_viewer_page(
392                 $frame,
393                 \%state,
394                 \%settings,
395                 '', # default
396                 '', # frame data
397                 '', # prev frame data
398                 '', # next frame data
399                 \%words_data
400         );
401 }
402
403 if (open_encoded($fh, ">>:encoding(UTF-8)", LOG_WORDS_PATH())) {
404         delete ($post_data{'password'});
405         print $fh "$time POST $ID\n";
406         write_data_file(
407                 $fh, \%post_data, '',
408                 0, 0, 0,
409                 '>>', 1
410         );
411         print $fh "\n\n";
412         close ($fh);
413 }
414
415 exit output(1);
416
417
418 sub output {
419         (my $done, my $status, my $message, my $show_content) = @_;
420         
421         my $return_url = merge_url(
422                 {'path' => CGI_VIEWER_PATH()},
423                 {
424                         'path' => $frame,
425                         'query' => {
426                                 'b' => TEXT_MODE->{'words'},
427                                 'i' => $page,
428                                 'p' => ($password_ok ? $settings{'password'} : '')
429                         },
430                         'fragment' => $ID
431                 }
432         );
433         if ($done) {
434                 return redirect($method, $return_url, HTTP_STATUS->{'see_other'});
435         }
436         
437         if ($status ne '') {
438                 print http_header_status($status);
439         }
440         print "Content-type: text/html; charset=UTF-8\n\n";
441         if ($method eq 'HEAD') {
442                 return;
443         }
444         
445         my $title;
446         my $name;
447         my $content;
448         
449         if ($remove) {
450                 $title = 'Remove message "'.$ID.'"';
451         }
452         elsif ($ID ne '') {
453                 $title = 'Edit message "'.$ID.'"';
454         }
455         else {
456                 $title = 'Words';
457         }
458         if ($frame ne '') {
459                 $title = $frame.'. '.$title;
460         }
461         
462         if ($cgi{'username'} ne '') {
463                 $name = $cgi{'username'}
464         }
465         elsif ($post_data{'name'} ne '') {
466                 $name = $post_data{'name'}
467         }
468         else {
469                 $name = '';
470         }
471         
472         if ($cgi{'words'} ne '') {
473                 $content = $cgi{'words'};
474         }
475         elsif ($quote ne '') {
476                 my %quote_data = read_words($quote);
477                 $content = '[quote="'.$quote_data{'name'}.'"]'.$quote_data{'content'}.'[/quote]';
478         }
479         elsif (($cgi{'edit'} ne '') || $remove) {
480                 $content = $post_data{'content'};
481         }
482         else {
483                 $content = '';
484         }
485         
486         my $_password = $password_ok ? html_entity_encode_dec($settings{'password'}, 1) : '';
487         my $_key = html_entity_encode_dec($post_data{'key'}, 1);
488         my $_ID = html_entity_encode_dec($ID, 1);
489         my $_title = html_entity_encode_dec($title, 1);
490         my $_message = html_entity_encode_dec($message, 1);
491         my $_story = html_entity_encode_dec($settings{'story'}, 1);
492         my $_name = html_entity_encode_dec($name, 1);
493         my $_content = html_entity_encode_dec($content, 1);
494         my $_empty = html_entity_encode_dec($cgi{'password2'}, 1);
495         my $_website_name = html_entity_encode_dec(WEBSITE_NAME(), 1);
496         my $_post_url = html_entity_encode_dec(CGI_WORDS_PATH(), 1);
497         my $_return_url = html_entity_encode_dec($return_url, 1);
498                 
499         print_html_start(\*STDOUT);
500         print_html_head_start(\*STDOUT);
501         
502         print '  <title>'.$_title.' &bull; '.$_story.' &bull; '.$_website_name.'</title>';
503         
504         print_html_head_end(\*STDOUT);
505         print_html_body_start(\*STDOUT);
506         
507         print '   <div id="inst" class="ins">'."\n";
508         
509         print '    <div id="title">'."\n";
510         print '     <h1 id="titletext">'.$_title.'</h1>'."\n";
511         print '    </div>'."\n";
512         
513         print '   </div>'."\n";
514         
515         if ($message ne '') {
516                 print '   <div id="insb" class="ins">'."\n";
517                 
518                 print '    <div id="command">'."\n";
519                 print '     <span class="br">'.$_message.'</span>'."\n";
520                 print '    </div>'."\n";
521                 
522                 print '   </div>'."\n";
523         }
524         
525         print '   <div id="insw" class="ins">'."\n";
526         
527         if ($show_content) {
528                 print '    <div class="undertext" id="words">'."\n";
529                 print '     <form method="post" action="'.$_post_url.'">'."\n";
530                 unless ($remove) {
531                         print '      <b>Your words:</b>'."\n";
532                         print '      <textarea class="inta" name="words" rows="4">'.$_content.'</textarea>'."\n";
533                 }
534                 print '      <table cellpadding="0" cellspacing="0" border="0"><tr>'."\n";
535                 print '       <td><b>Your name: </b></td>'."\n";
536                 print '       <td><input class="intx" type="text" name="username" value="'.$_name.'"></td>'."\n";
537                 print '       <td></td>'."\n";
538                 print '      </tr><tr>'."\n";
539                 print '       <td><b>'.(($ID ne '') ? 'Password' : 'Optional password').': </b></td>'."\n";
540                 print '       <td><input class="intx" type="password" name="password" value=""></td>'."\n";
541                 print '       <td>'.(($ID ne '') ? '' : '(if you want to edit later)').'</td>'."\n";
542                 print '      </tr><tr>'."\n";
543                 print '       <td><b>Leave this empty: </b></td>'."\n";
544                 print '       <td><input class="intx" type="text" name="password2" value="'.$_empty.'"></td>'."\n";
545                 if ($remove) {
546                         print '       <td><input class="inbt" type="submit" name="remove" value="Remove"></td>'."\n";
547                 }
548                 else {
549                         print '       <td>'."\n";
550                         print '        <input class="inbt" type="submit" name="post" value="'.(($ID ne '') ? 'Update' : 'Send').'">'."\n";
551                         print '        <input class="inbt" type="submit" name="preview" value="Preview">'."\n";
552                         print '       </td>'."\n";
553                 }
554                 print '      </tr></table>'."\n";
555                 print '      <input type="hidden" name="f" value="'.$frame.'">'."\n";
556                 if ($ID ne '') {
557                         print '      <input type="hidden" name="i" value="'.$_ID.'">'."\n";
558                 }
559                 print '      <input type="hidden" name="key" value="'.$_key.'">'."\n";
560                 if ($password_ok) {
561                         print '      <input type="hidden" name="p" value="'.$_password.'">'."\n";
562                 }
563                 print '     </form>'."\n";
564                 if ($content ne ''){
565                         print '     <br>'."\n";
566                         print '     <div id="preview"class="opomba">'."\n";
567                         print '      <div class="opomba_info">'."\n";
568                         print '       Preview:'."\n";
569                         print '      </div>'."\n";
570                         print '      <div class="opomba_text">'."\n";
571                   print bb_to_html(
572                                 eval_bb(
573                                         $content,
574                                         0,
575                                         $password_ok ? $settings{'password'} : ''
576                                 )
577                         )."\n";
578                         print '      </div>'."\n";
579                         print '     </div>'."\n";
580                 }
581                 print '    </div>'."\n";
582         }
583         print '    <div id="underlinks">'."\n";
584         print '     <a href="'.$_return_url.'">Return</a>'."\n";
585         print '    </div>'."\n";
586         
587         print '   </div>'."\n";
588         
589         print_html_body_end(\*STDOUT, $ong_state == STATE->{'inactive'});
590         print_html_end(\*STDOUT);
591 }