fix #1819: fork_worker: ensure sync'ed workers control terminal
[pve-common.git] / src / PVE / CalendarEvent.pm
1 package PVE::CalendarEvent;
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6 use Time::Local;
7 use PVE::JSONSchema;
8 use PVE::Tools qw(trim);
9
10 # Note: This class implements a parser/utils for systemd like calender exents
11 # Date specification is currently not implemented
12
13 my $dow_names = {
14 sun => 0,
15 mon => 1,
16 tue => 2,
17 wed => 3,
18 thu => 4,
19 fri => 5,
20 sat => 6,
21 };
22
23 PVE::JSONSchema::register_format('pve-calendar-event', \&pve_verify_calendar_event);
24 sub pve_verify_calendar_event {
25 my ($text, $noerr) = @_;
26
27 eval { parse_calendar_event($text); };
28 if (my $err = $@) {
29 return undef if $noerr;
30 die "invalid calendar event '$text' - $err\n";
31 }
32 return $text;
33 }
34
35 # The parser.
36 # returns a $calspec hash which can be passed to compute_next_event()
37 sub parse_calendar_event {
38 my ($event) = @_;
39
40 $event = trim($event);
41
42 if ($event eq '') {
43 die "unable to parse calendar event - event is empty\n";
44 }
45
46 my $parse_single_timespec = sub {
47 my ($p, $max, $matchall_ref, $res_hash) = @_;
48
49 if ($p =~ m/^((?:\*|[0-9]+))(?:\/([1-9][0-9]*))?$/) {
50 my ($start, $repetition) = ($1, $2);
51 if (defined($repetition)) {
52 $repetition = int($repetition);
53 $start = $start eq '*' ? 0 : int($start);
54 die "value '$start' out of range\n" if $start >= $max;
55 die "repetition '$repetition' out of range\n" if $repetition >= $max;
56 while ($start < $max) {
57 $res_hash->{$start} = 1;
58 $start += $repetition;
59 }
60 } else {
61 if ($start eq '*') {
62 $$matchall_ref = 1;
63 } else {
64 $start = int($start);
65 die "value '$start' out of range\n" if $start >= $max;
66 $res_hash->{$start} = 1;
67 }
68 }
69 } elsif ($p =~ m/^([0-9]+)\.\.([1-9][0-9]*)$/) {
70 my ($start, $end) = (int($1), int($2));
71 die "range start '$start' out of range\n" if $start >= $max;
72 die "range end '$end' out of range\n" if $end >= $max || $end < $start;
73 for (my $i = $start; $i <= $end; $i++) {
74 $res_hash->{$i} = 1;
75 }
76 } else {
77 die "unable to parse calendar event '$p'\n";
78 }
79 };
80
81 my $h = undef;
82 my $m = undef;
83
84 my $matchall_minutes = 0;
85 my $matchall_hours = 0;
86 my $minutes_hash = {};
87 my $hours_hash = {};
88
89 my $dowsel = join('|', keys %$dow_names);
90
91 my $dow_hash;
92
93 my $parse_dowspec = sub {
94 my ($p) = @_;
95
96 if ($p =~ m/^($dowsel)$/i) {
97 $dow_hash->{$dow_names->{lc($1)}} = 1;
98 } elsif ($p =~ m/^($dowsel)\.\.($dowsel)$/i) {
99 my $start = $dow_names->{lc($1)};
100 my $end = $dow_names->{lc($2)} || 7;
101 die "wrong order in range '$p'\n" if $end < $start;
102 for (my $i = $start; $i <= $end; $i++) {
103 $dow_hash->{($i % 7)} = 1;
104 }
105 } else {
106 die "unable to parse weekday specification '$p'\n";
107 }
108 };
109
110 my @parts = split(/\s+/, $event);
111
112 if ($parts[0] =~ m/$dowsel/i) {
113 my $dow_spec = shift @parts;
114 foreach my $p (split(',', $dow_spec)) {
115 $parse_dowspec->($p);
116 }
117 } else {
118 $dow_hash = { 0 => 1, 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5=> 1, 6 => 1 };
119 }
120
121 if (scalar(@parts) && $parts[0] =~ m/\-/) {
122 my $date_spec = shift @parts;
123 die "date specification not implemented";
124 }
125
126 my $time_spec = shift(@parts) // "00:00";
127 my $chars = '[0-9*/.,]';
128
129 if ($time_spec =~ m/^($chars+):($chars+)$/) {
130 my ($p1, $p2) = ($1, $2);
131 foreach my $p (split(',', $p1)) {
132 $parse_single_timespec->($p, 24, \$matchall_hours, $hours_hash);
133 }
134 foreach my $p (split(',', $p2)) {
135 $parse_single_timespec->($p, 60, \$matchall_minutes, $minutes_hash);
136 }
137 } elsif ($time_spec =~ m/^($chars)+$/) { # minutes only
138 $matchall_hours = 1;
139 foreach my $p (split(',', $time_spec)) {
140 $parse_single_timespec->($p, 60, \$matchall_minutes, $minutes_hash);
141 }
142
143 } else {
144 die "unable to parse calendar event\n";
145 }
146
147 die "unable to parse calendar event - unused parts\n" if scalar(@parts);
148
149 if ($matchall_hours) {
150 $h = '*';
151 } else {
152 $h = [ sort { $a <=> $b } keys %$hours_hash ];
153 }
154
155 if ($matchall_minutes) {
156 $m = '*';
157 } else {
158 $m = [ sort { $a <=> $b } keys %$minutes_hash ];
159 }
160
161 return { h => $h, m => $m, dow => [ sort keys %$dow_hash ]};
162 }
163
164 sub compute_next_event {
165 my ($calspec, $last, $utc) = @_;
166
167 my $hspec = $calspec->{h};
168 my $mspec = $calspec->{m};
169 my $dowspec = $calspec->{dow};
170
171 $last += 60; # at least one minute later
172
173 while (1) {
174
175 my ($min, $hour, $mday, $mon, $year, $wday);
176 my $startofday;
177
178 if ($utc) {
179 (undef, $min, $hour, $mday, $mon, $year, $wday) = gmtime($last);
180 # gmtime and timegm interpret two-digit years differently
181 $year += 1900;
182 $startofday = timegm(0, 0, 0, $mday, $mon, $year);
183 } else {
184 (undef, $min, $hour, $mday, $mon, $year, $wday) = localtime($last);
185 # localtime and timelocal interpret two-digit years differently
186 $year += 1900;
187 $startofday = timelocal(0, 0, 0, $mday, $mon, $year);
188 }
189
190 $last = $startofday + $hour*3600 + $min*60;
191
192 my $check_dow = sub {
193 foreach my $d (@$dowspec) {
194 return $last if $d == $wday;
195 if ($d > $wday) {
196 return $startofday + ($d-$wday)*86400;
197 }
198 }
199 return $startofday + (7-$wday)*86400; # start of next week
200 };
201
202 if ((my $next = $check_dow->()) != $last) {
203 $last = $next;
204 next; # repeat
205 }
206
207 my $check_hour = sub {
208 return $last if $hspec eq '*';
209 foreach my $h (@$hspec) {
210 return $last if $h == $hour;
211 if ($h > $hour) {
212 return $startofday + $h*3600;
213 }
214 }
215 return $startofday + 24*3600; # test next day
216 };
217
218 if ((my $next = $check_hour->()) != $last) {
219 $last = $next;
220 next; # repeat
221 }
222
223 my $check_minute = sub {
224 return $last if $mspec eq '*';
225 foreach my $m (@$mspec) {
226 return $last if $m == $min;
227 if ($m > $min) {
228 return $startofday +$hour*3600 + $m*60;
229 }
230 }
231 return $startofday + ($hour + 1)*3600; # test next hour
232 };
233
234 if ((my $next = $check_minute->()) != $last) {
235 $last = $next;
236 next; # repeat
237 } else {
238 return $last;
239 }
240 }
241
242 die "unable to compute next calendar event\n";
243 }
244
245 1;