-###PERL;
-#
-# /bsta/2words
+###RUN_PERL: #!/usr/bin/perl
+
# reset is generated from reset.1.pl.
-# 26.03.2018
#
# Reset BSTA state
#
-# Copyright (C) 2016 - 2018 Balthasar Szczepański
+# Copyright (C) 2016, 2017, 2018, 2023, 2024 Balthasar Szczepański
#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU Affero General Public License for more details.
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
#
-# You should have received a copy of the GNU Affero General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
-#use warnings;
-###LIB;
-use bsta_lib qw(writedatafile);
-
-###STORY_PATH;
-###WEBSITE;
-###CGI_PATH;
-###INDEX_PATH;
-###INTF_DATE;
-###STATE_PATH;
-###COIN_DATE;
-###CHAT_PATH;
-
-my %story = (
- 'id' => '0',
- 'letter' => '',
- 'lastip' => '0.0.0.0',
- 'content' => '',
- 'pass' => '0',
- 'state' => '0'
+use utf8;
+use Encode::Locale ('decode_argv');
+use Encode ('encode', 'decode');
+
+###PERL_LIB: use lib /botm/lib/bsta
+use botm_common (
+ 'write_data_file',
+ 'opendir_encoded', 'readdir_decoded', 'unlink_encoded',
+ 'join_path'
);
-my %state = (
- 'state' => '0',
- 'last' => '0',
+use bsta_lib (
+ 'STATE', 'INTF_STATE', 'CHAT_STATE',
+ 'write_index',
+ 'merge_settings',
+ 'get_page_file', 'get_frame_file',
+ 'read_settings', 'read_default', 'read_frame_data', 'read_attachment', 'read_state'
+);
+
+binmode STDIN, ':encoding(UTF-8)';
+binmode STDOUT, ':encoding(UTF-8)';
+binmode STDERR, ':encoding(UTF-8)';
+decode_argv();
+
+###PERL_PATH_SEPARATOR: PATH_SEPARATOR = /
+
+###PERL_DATA_CHAT_PATH: DATA_CHAT_PATH = /botm/data/bsta/chat
+###PERL_DATA_DEFAULT_PATH: DATA_DEFAULT_PATH = /botm/data/bsta/default
+###PERL_DATA_LIST_PATH: DATA_LIST_PATH = /botm/data/bsta/list
+###PERL_DATA_STATE_PATH: DATA_STATE_PATH = /botm/data/bsta/state
+###PERL_DATA_STORY_PATH: DATA_STORY_PATH = /botm/data/bsta/story
+###PERL_DATA_WORDS_PATH: DATA_WORDS_PATH = /botm/data/bsta/words/
+
+###PERL_WWW_PATH: WWW_PATH = /botm/www/
+
+###PERL_RESET_PASSWORD: RESET_PASSWORD = ''
+
+my %story;
+my %state;
+my %chat;
+my %settings;
+my %goto_list;
+my %default;
+my $last_frame;
+my @remove_list;
+
+delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
+###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
+
+%settings = read_settings();
+%state = read_state();
+unless (
+ ($ARGV[0] ne '') && (
+ ($ARGV[0] eq $settings{'password'}) ||
+ ($ARGV[0] eq RESET_PASSWORD())
+ )
+) {
+ print STDERR "Wrong password; no reset.\n";
+ exit 1;
+}
+
+$last_frame = (int($settings{'last'}) > int($state{'last'})) ?
+ int($settings{'last'}) : int($state{'last'});
+%default = read_default();
+
+for (my $frame=0; $frame<=$last_frame; $frame+=1) {
+ my %frame_data = read_frame_data($frame, \%default);
+ unless ($frame == 0) {
+ push @remove_list, get_page_file($frame, \%frame_data, \%settings);
+ }
+ push @remove_list, get_frame_file($frame, \%frame_data, \%settings);
+}
+push @remove_list, 'goto.htm';
+for (my $i=0; ;$i+=1) {
+ my %file_data;
+
+ %file_data = read_attachment($i);
+ if ($file_data{'frame'} eq '') {
+ last;
+ }
+ if ($file_data{'content'} ne '') {
+ next;
+ }
+ push @remove_list, $file_data{'filename'};
+}
+
+%state = (
+ 'state' => STATE->{'inactive'},
+ 'last' => 0,
'ongtime' => '',
'nextong' => '',
'ip1' => '',
'ip2' => '',
'ip3' => ''
);
-my %chat = (
- 'id' => '0',
- 'state' => '0',
+%story = (
+ 'id' => 0,
+ 'letter' => '',
+ 'lastip' => '0.0.0.0',
+ 'content' => '',
+ 'pass' => 0,
+ 'state' => INTF_STATE->{'X'}
+);
+%chat = (
+ 'id' => 0,
+ 'state' => CHAT_STATE->{'disconnected'},
'content' => ''
);
+%goto_list = (
+);
-delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
-###PATH;
-
-writedatafile(STATE_PATH,%state);
-writedatafile(STORY_PATH,%story);
-writedatafile(CHAT_PATH,%chat);
-
-writeindex(INDEX_PATH);
-
-#function borrowed from 2words.pl - keep consistent!
-sub writeindex {
- (my $indexpath) = @_;
- my $indexfile;
- my $indexof;
-
- if(ref($indexpath)) {
- $indexfile=$indexpath;
- unless (seek($indexfile, 0, 0)) {
- return 0;
- }
- }
- else {
- unless (open ($indexfile, ">", $indexpath)) {
- return 0;
+write_data_file(DATA_STATE_PATH(), \%state);
+write_data_file(DATA_STORY_PATH(), \%story);
+write_data_file(DATA_CHAT_PATH() , \%chat);
+write_data_file(DATA_LIST_PATH() , \%goto_list);
+
+write_index(
+ \%state,
+ \%settings,
+ $story{'pass'},
+ $story{'state'},
+ 0 # pause
+);
+
+if (opendir_encoded(my $dir, DATA_WORDS_PATH())) {
+ while (defined (my $file_name = readdir_decoded($dir))) {
+ if ($file_name !~ /^[0-9]+$/) {
+ next;
}
+ my $full_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $file_name);
+ # print "RM $full_path\n";
+ unlink_encoded ($full_path);
}
-
- $indexof = CGI_PATH;
- $indexof =~ s/\/$//g;
-
- print $indexfile '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">'."\n";
- print $indexfile '<html>'."\n";
- print $indexfile ' <head>'."\n";
- print $indexfile ' <title>Index of '.$indexof.'</title>'."\n";
- print $indexfile ' </head>'."\n";
- print $indexfile ' <body>'."\n";
- print $indexfile '<h1>Index of '.$indexof.'</h1>'."\n";
- print $indexfile '<table><tr><th><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr><tr><th colspan="5"><hr></th></tr>'."\n";
- print $indexfile '<tr><td valign="top"><img src="/icons/back.gif" alt="[DIR]"></td><td><a href="/">Parent Directory</a></td><td> </td><td align="right"> - </td><td> </td></tr>'."\n";
- print $indexfile '<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="2words/">2words/</a></td><td align="right">'.INTF_DATE.' </td><td align="right"> - </td><td> </td></tr>'."\n";
- print $indexfile '<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="coin/">coin/</a></td><td align="right">'.COIN_DATE.' </td><td align="right"> - </td><td> Coincidence </td></tr>'."\n";
- print $indexfile '<tr><th colspan="5"><hr></th></tr>'."\n";
- print $indexfile '</table>'."\n";
- print $indexfile '<address>Apache/2.2.22 (Debian) Server at '.WEBSITE.' Port 80</address>'."\n";
- print $indexfile '</body></html>'."\n";
-
-
- unless (ref($indexpath)) {
- close ($indexfile);
- }
- else {
- truncate ($indexfile , tell($indexfile));
- }
-
- return 1;
+}
+foreach my $file_name (@remove_list) {
+ my $full_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $file_name);
+ # print "RM $full_path\n";
+ unlink_encoded ($full_path);
}