]> bicyclesonthemoon.info Git - ott/bsta/blob - reset.1.pl
change makefile regeneration command
[ott/bsta] / reset.1.pl
1 ###RUN_PERL: #!/usr/bin/perl
2
3 # reset is generated from reset.1.pl.
4 #
5 # Reset BSTA state
6 #
7 # Copyright (C) 2016, 2017, 2018, 2023, 2024  Balthasar SzczepaƄski
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Affero General Public License as
11 # published by the Free Software Foundation, either version 3 of the
12 # License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU Affero General Public License for more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 use strict;
23 use utf8;
24 use Encode::Locale ('decode_argv');
25 use Encode ('encode', 'decode');
26
27 ###PERL_LIB: use lib /botm/lib/bsta
28 use botm_common (
29         'write_data_file',
30         'opendir_encoded', 'readdir_decoded', 'unlink_encoded',
31         'join_path'
32 );
33 use bsta_lib (
34         'STATE', 'INTF_STATE', 'CHAT_STATE',
35         'write_index',
36         'merge_settings',
37         'get_page_file', 'get_frame_file',
38         'read_settings', 'read_default', 'read_frame_data', 'read_attachment', 'read_state'
39 );
40
41 binmode STDIN,  ':encoding(UTF-8)';
42 binmode STDOUT, ':encoding(UTF-8)';
43 binmode STDERR, ':encoding(UTF-8)';
44 decode_argv();
45
46 ###PERL_PATH_SEPARATOR:     PATH_SEPARATOR     = /
47
48 ###PERL_DATA_CHAT_PATH:     DATA_CHAT_PATH     = /botm/data/bsta/chat
49 ###PERL_DATA_DEFAULT_PATH:  DATA_DEFAULT_PATH  = /botm/data/bsta/default
50 ###PERL_DATA_LIST_PATH:     DATA_LIST_PATH     = /botm/data/bsta/list
51 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
52 ###PERL_DATA_STORY_PATH:    DATA_STORY_PATH    = /botm/data/bsta/story
53 ###PERL_DATA_WORDS_PATH:    DATA_WORDS_PATH    = /botm/data/bsta/words/
54
55 ###PERL_WWW_PATH:           WWW_PATH           = /botm/www/
56
57 ###PERL_RESET_PASSWORD:     RESET_PASSWORD     = ''
58
59 my %story;
60 my %state;
61 my %chat;
62 my %settings;
63 my %goto_list;
64 my %default;
65 my $last_frame;
66 my @remove_list;
67
68 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
69 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
70
71 %settings = read_settings();
72 %state    = read_state();
73 unless (
74         ($ARGV[0] ne '') && (
75                 ($ARGV[0] eq $settings{'password'}) ||
76                 ($ARGV[0] eq RESET_PASSWORD())
77         )
78 ) {
79         print STDERR "Wrong password; no reset.\n";
80         exit 1;
81 }
82
83 $last_frame = (int($settings{'last'}) > int($state{'last'})) ? 
84         int($settings{'last'}) : int($state{'last'});
85 %default = read_default();
86
87 for (my $frame=0; $frame<=$last_frame; $frame+=1) {
88         my %frame_data = read_frame_data($frame, \%default);
89         unless ($frame == 0) {
90                 push @remove_list, get_page_file($frame, \%frame_data, \%settings);
91         }
92         push @remove_list, get_frame_file($frame, \%frame_data, \%settings);
93 }
94 push @remove_list, 'goto.htm';
95 for (my $i=0; ;$i+=1) {
96         my %file_data;
97         
98         %file_data = read_attachment($i);
99         if ($file_data{'frame'} eq '') {
100                 last;
101         }
102         if ($file_data{'content'} ne '') {
103                 next;
104         }
105         push @remove_list, $file_data{'filename'};
106 }
107
108 %state = (
109         'state' => STATE->{'inactive'},
110         'last'  => 0,
111         'ongtime' => '',
112         'nextong' => '',
113         'ip1' => '',
114         'ip2' => '',
115         'ip3' => ''
116 );
117 %story = (
118         'id'      => 0,
119         'letter'  => '',
120         'lastip'  => '0.0.0.0',
121         'content' => '',
122         'pass'    => 0,
123         'state'   => INTF_STATE->{'X'}
124 );
125 %chat = (
126         'id'      => 0,
127         'state'   => CHAT_STATE->{'disconnected'},
128         'content' => ''
129 );
130 %goto_list = (
131 );
132
133 write_data_file(DATA_STATE_PATH(), \%state);
134 write_data_file(DATA_STORY_PATH(), \%story);
135 write_data_file(DATA_CHAT_PATH() , \%chat);
136 write_data_file(DATA_LIST_PATH() , \%goto_list);
137
138 write_index(
139         \%state,
140         \%settings,
141         $story{'pass'},
142         $story{'state'},
143         0 # pause
144 );
145
146 if (opendir_encoded(my $dir, DATA_WORDS_PATH())) {
147         while (defined (my $file_name = readdir_decoded($dir))) {
148                 if ($file_name !~ /^[0-9]+$/) {
149                         next;
150                 }
151                 my $full_path = join_path(PATH_SEPARATOR(), DATA_WORDS_PATH(), $file_name);
152                 # print "RM $full_path\n";
153                 unlink_encoded ($full_path);
154         }
155 }
156 foreach my $file_name (@remove_list) {
157         my $full_path = join_path(PATH_SEPARATOR(), WWW_PATH(), $file_name);
158         # print "RM $full_path\n";
159         unlink_encoded ($full_path);
160 }