]> bicyclesonthemoon.info Git - ott/bsta/blob - ong.1.pl
Adapted for new config tool.
[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  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 warnings;
24 ###PERL_LIB: use lib /botm/lib/bsta
25 use bsta_lib qw(entityencode readdatafile writedatafile urlencode);
26 use File::Copy;
27
28 ###PERL_DATA_PATH:          DATA_PATH    = /botm/data/bsta
29 ###PERL_DATA_DEFAULT_PATH:  DATA_DEFAULT_PATH = /botm/data/bsta/default
30 ###PERL_DATA_SETTINGS_PATH: DATA_SETTINGS_PATH = /botm/data/bsta/settings
31 ###PERL_DATA_STATE_PATH:    DATA_STATE_PATH    = /botm/data/bsta/state
32 ###PERL_WWW_PATH:           WWW_PATH           = /botm/www/1190/bsta/
33 ###PERL_DATA_LIST_PATH:     DATA_LIST_PATH     = /botm/data/bsta/list
34
35 my %framedata;
36 my %nextframedata;
37 my %default;
38 my %settings;
39 my %state;
40 my %gotolist;
41
42 my $statefile;
43 my $ongstate;
44 my $frame;
45 my $nextong;
46 my $inpath;
47 my $outpath;
48 my $ongtime;
49 my $static;
50 my $dynamic;
51 my $last;
52
53 my $time = time();
54 srand ($time-$$);
55 $ongtime = int($time / 3600) * 3600;
56
57 print $time.' - '.$ongtime."\n";
58
59 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
60 ###PERL_SET_PATH: $ENV{'PATH'} = /usr/local/bin:/usr/bin:/bin;
61
62
63 if (open ($statefile,"+<",DATA_STATE_PATH)){
64         if (flock($statefile,2)) {
65                 %state=readdatafile($statefile);
66                 $ongstate=int($state{'state'});
67                 print 'state: '.$ongstate."\n";
68                 if($ongstate > 0) {
69                         $nextong = int($state{'nextong'});
70                         print 'ongtime: '.$nextong."\n";
71                         
72                         if($ongtime >= $nextong) {
73                                 %settings=readdatafile(DATA_SETTINGS_PATH);
74                                 $static=int($settings{'ongtime'});
75                                 $dynamic=int($settings{'dynamicongtime'});
76                                 $last=int($settings{'last'});
77                                 $frame=int($state{'last'})+1;
78                                 
79                                 if($dynamic > 0 && $frame < $last) {
80                                         $dynamic = int($dynamic/($last-$frame));
81                                 }
82                                 else {
83                                         $dynamic=0;
84                                 }
85                                 
86                                 if($static>$dynamic){
87                                         $dynamic=$static;
88                                 }
89                                 $nextong=$ongtime+($dynamic*3600);
90                                 $state{'nextong'}=$nextong;
91                                 print 'next ongtime: '.$nextong.' (+'.$dynamic.")\n";
92                                 $state{'ongtime'}=$dynamic;
93                                 
94                                 if($ongstate == 2) {
95                                         print 'next frame: '.$frame."\n";
96                                         %framedata=readdatafile(DATA_PATH.$frame);
97                                         %default=readdatafile(DATA_DEFAULT_PATH);
98                                         %gotolist=readdatafile(DATA_LIST_PATH);
99                                         
100                                         $framedata{'ongtime'}=$time;
101                                         $framedata{'timer'}=$dynamic;
102                                         $gotolist{'title-'.$frame}=$framedata{'title'};
103                                         $gotolist{'ongtime-'.$frame}=$framedata{'ongtime'};
104                                         writedatafile(DATA_PATH.$frame,%framedata);
105                                         writedatafile(DATA_LIST_PATH,%gotolist);
106                                         
107                                         foreach my $ind (keys %default) {
108                                                 unless(defined($framedata{$ind})){
109                                                         $framedata{$ind}=$default{$ind};
110                                                 }
111                                                 unless(defined($nextframedata{$ind})){
112                                                         $nextframedata{$ind}=$default{$ind};
113                                                 }
114                                         }
115                                         
116                                         $inpath = DATA_PATH.sprintf($settings{'frame'},$frame,$framedata{'ext'});
117                                         $outpath = WWW_PATH.sprintf($settings{'frame'},$frame,$framedata{'ext'});
118                                         
119                                         print $inpath.' -> '.$outpath."\n";
120                                         
121                                         # ALSO-ATTACHMENTS!!! (here)
122                                         
123                                         if(copy ($inpath, $outpath)) {
124                                                 $state{'last'}=$frame;
125                                                 $state{'state'}=1;
126                                                 $state{'ip1'}='';
127                                                 $state{'ip2'}='';
128                                                 $state{'ip3'}='';
129                                                 $state{'ongtime'}=$dynamic;
130                                                 print "ONG\n";
131                                         }
132                                         else {
133                                                 print "NO ONG\n";
134                                         }
135                                 }
136                                 writedatafile($statefile,%state);
137                         }
138                         else {
139                                 print "WAIT\n";
140                         }
141                 }
142                 else {
143                         print "INACTIVE\n";
144                 }
145         }
146         else {
147                 print "NO STATELOCK\n";
148         }
149         close ($statefile);
150 }
151 else {
152         print "NO STATEFILE\n";
153 }
154 print "\n";