]> bicyclesonthemoon.info Git - ott/bsta/blob - ong.1.pl
update config; drop unneeded oldlogs.1.pl
[ott/bsta] / ong.1.pl
1 ###RUN_PERL: #!/usr/bin/perl
2
3 # ong.pl is generated from ong.1.pl.
4 #
5 # The ONG bot
6 #
7 # Copyright (C) 2016, 2017, 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         'open_encoded',
30 );
31 use bsta_lib (
32         'STATE',
33         'ong',
34         'write_static_viewer_page', 'write_index', 'write_static_goto',
35         'read_default', 'read_frame_data', 'read_settings', 'read_goto',
36         'read_state', 'write_state'
37 );
38
39 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
40
41 binmode STDIN,  ':encoding(UTF-8)';
42 binmode STDOUT, ':encoding(UTF-8)';
43 binmode STDERR, ':encoding(UTF-8)';
44 # decode_argv();
45
46 my $time = time();
47 srand ($time-$$);
48
49 my %settings;
50 my %state;
51 my %new_state;
52 # my %goto_list;
53 my %default;
54 my %frame_data;
55 my %frame_1_data;
56 my %frame_2_data;
57 my %frame_3_data;
58
59 my $fh;
60 my $ongstate;
61 my $frame;
62 my $next_ong;
63 my $ongtime;
64 my $static_timer;
65 my $timer;
66 my $last;
67 my $r = 0;
68
69 $ongtime = int($time / 3600) * 3600; # check time as if it was last full hour!
70
71 print $time.' - '.$ongtime."\n";
72
73 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
74 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
75
76 unless (open_encoded($fh, "+<:encoding(UTF-8)", DATA_STATE_PATH())) {
77         print "NO STATEFILE\n\n";
78         exit;
79 }
80 unless (flock($fh, 2)) {
81         print "NO STATELOCK\n\n";
82         close ($fh);
83         exit;
84 }
85 %state = read_state($fh);
86 $ongstate = int($state{'state'});
87 print 'state: '.$ongstate."\n";
88 unless ($ongstate > STATE->{'inactive'}) {
89         print "INACTIVE\n\n";
90         close ($fh);
91         exit;
92 }
93 $next_ong = int($state{'nextong'});
94 print 'ongtime: '.$next_ong."\n";
95
96 unless ($ongtime >= $next_ong) {
97         print "WAIT\n\n";
98         close ($fh);
99         exit;
100 }
101
102 %settings = read_settings();
103 $static_timer = int($settings{'ongtime'});
104 $timer        = int($settings{'dynamicongtime'});
105 $last         = int($settings{'last'});
106 $frame        = int($state   {'last'})+1;
107
108 if (($timer > 0) && ($frame < $last)) {
109         $timer = int($timer / ($last - $frame));
110 }
111 else {
112         $timer=0;
113 }
114
115 if ($static_timer > $timer) {
116         $timer = $static_timer;
117 }
118 $next_ong = $ongtime + ($timer*3600);
119 $state{'nextong'} = $next_ong;
120 print 'next ongtime: '.$next_ong.' (+'.$timer.")\n";
121 $state{'ongtime'}=$timer;
122
123 if ($ongstate == STATE->{'ready'}) {
124         print 'next frame: '.$frame."\n";
125         
126         %default = read_default();
127         %frame_data = read_frame_data($frame);
128         # %goto_list = read_goto());
129         
130         %new_state = %state;
131         $new_state{'last'}   = $frame;
132         $new_state{'state'}  = 1;
133         $new_state{'ip1'}    = '';
134         $new_state{'ip2'}    = '';
135         $new_state{'ip3'}    = '';
136         $new_state{'ongtime'}= $timer;
137         
138         if ($frame >= 1) {
139                 %frame_1_data = read_frame_data($frame-1);
140         }
141         if ($frame >= 2) {
142                 %frame_2_data = read_frame_data($frame-2);
143         }
144         if ($frame >= 3) {
145                 %frame_3_data = read_frame_data($frame-3);
146         }
147         
148         $r = ong (
149                 $frame, # frame ID
150                 $time,  # ONG time
151                 $timer, # timer to next ONG
152                 0,      # update
153                 1,      # print
154                 \%settings,
155                 \%default,
156                 \%frame_data,
157                 '' # \%goto_list
158         );
159         if ($r && ($frame >= 2)) {
160                 print 'static page '.($frame-1);
161                 $r = write_static_viewer_page(
162                         $frame-1, # frame ID
163                         \%new_state,
164                         \%settings,
165                         \%default,
166                         \%frame_1_data, # frame data
167                         \%frame_2_data, # prev frame data
168                         \%frame_data,   # next frame data
169                         '' # words data
170                 );
171                 print (($r) ? " OK\n" : " FAIL\n");
172         }
173         if ($r && ($frame >= 3)) {
174                 print 'static page '.($frame-2);
175                 $r = write_static_viewer_page(
176                         $frame-2, # frame ID
177                         \%new_state,
178                         \%settings,
179                         \%default,
180                         \%frame_2_data, # frame data
181                         \%frame_3_data, # prev frame data
182                         \%frame_1_data, # next frame data
183                         '' # words data
184                 );
185                 print (($r) ? " OK\n" : " FAIL\n");
186         }
187         if ($r && ($frame <= 2)) {
188                 print 'index';
189                 $r = write_index(
190                         \%new_state,
191                         \%settings
192                 );
193                 print (($r) ? " OK\n" : " FAIL\n");
194         }
195         if ($r) {
196                 print 'static GOTO';
197                 $r = write_static_goto(
198                         \%new_state,
199                         \%settings,
200                         '' # \%goto_list
201                 );
202                 print (($r) ? " OK\n" : " FAIL\n");
203         }
204         if ($r) {
205                 %state = %new_state;
206                 print "ONG\n\n";
207         }
208         else {
209                 print "NO ONG\n\n";
210         }
211 }
212 write_state($fh, \%state);
213 close ($fh);