1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
// $Id$ 
 
#if constant(roxenp) 
inherit /*Variable*/.Variable; 
 
//! This class implements a scheduler widget with three main states, 
//! never index, index every n:th hour or index every n:th x-day at y 
//! o'clock. In the "index every n:th hour" case the range is 1 to 23. 
//! In the "index every n:th x-day at y o'clock" the n-range is 
//! 1 to 9, the units are day and all the weekdays. The time range for 
//! y is all hours in a day. 
 
// Locale macros 
//<locale-token project="roxen_config"> LOCALE </locale-token> 
 
#define LOCALE(X,Y)    \ 
  ([string](mixed)Locale.translate("roxen_config",roxenp()->locale->get(),X,Y)) 
 
#else 
 
// Test mode. 
 
#define LOCALE(X, Y)  (Y) 
 
array(int) val = ({ }); 
 
array(int) query() 
{ 
  return val; 
} 
 
// Deterministic timezone... 
#define localtime(X)  gmtime(X) 
 
int main(int argc, array(string) argv) 
{ 
  int successes; 
  int failures; 
  foreach(({ 
            ({ ({ 0, 2, 1, 6, 3, 0, }), 
               // Disabled. 
               ({ 0, -1 }), 
            }), 
            ({ ({ 1, 2, 1, 6, 3, 0, }), 
               // Every other hour. 
               // 2022-06-22T14:11:43 (Wed)  ==>  2022-06-22T16:11:43 (Wed) 
               ({ 1655907103, 1655914303 }), 
            }), 
            ({ ({ 2, 2, 1, 6, 3, 0, }), 
               // Every Friday at 03:00. 
               // 2022-06-22T14:11:43 (Wed)  ==>  2022-06-24T03:00:00 (Fri) 
               ({ 1655907103, 1656039600 }), 
               // 2022-06-23T14:11:43 (Thu)  ==>  2022-06-24T03:00:00 (Fri) 
               ({ 1655993503, 1656039600 }), 
               // 2022-06-24T02:11:43 (Fri)  ==>  2022-06-24T03:00:00 (Fri) 
               ({ 1656036703, 1656039600 }), 
               // 2022-06-24T03:11:43 (Fri)  ==>  2022-07-01T03:00:00 (Fri) 
               ({ 1656040303, 1656644400 }), 
               // 2022-06-24T03:21:43 (Fri)  ==>  2022-07-01T03:00:00 (Fri) 
               ({ 1656040903, 1656644400 }), 
               // 2022-06-24T03:51:43 (Fri)  ==>  2022-07-01T03:00:00 (Fri) 
               ({ 1656042703, 1656644400 }), 
               // 2022-06-24T04:11:43 (Fri)  ==>  2022-07-01T03:00:00 (Fri) 
               ({ 1656043903, 1656644400 }), 
               // 2022-06-24T14:11:43 (Fri)  ==>  2022-07-01T03:00:00 (Fri) 
               ({ 1656079903, 1656644400 }), 
            }), 
            ({ ({ 2, 2, 1, 6, 3, 45, }), 
               // Every Friday at 03:45. 
               // 2022-06-22T14:11:43 (Wed)  ==>  2022-06-24T03:45:00 (Fri) 
               ({ 1655907103, 1656042300 }), 
               // 2022-06-23T14:11:43 (Thu)  ==>  2022-06-24T03:45:00 (Fri) 
               ({ 1655993503, 1656042300 }), 
               // 2022-06-24T02:11:43 (Fri)  ==>  2022-06-24T03:45:00 (Fri) 
               ({ 1656036703, 1656042300 }), 
               // 2022-06-24T03:11:43 (Fri)  ==>  2022-06-24T03:45:00 (Fri) 
               ({ 1656040303, 1656042300 }), 
               // 2022-06-24T03:21:43 (Fri)  ==>  2022-06-24T03:45:00 (Fri) 
               ({ 1656040903, 1656042300 }), 
               // 2022-06-24T03:51:43 (Fri)  ==>  2022-07-01T03:45:00 (Fri) 
               ({ 1656042703, 1656647100 }), 
               // 2022-06-24T04:11:43 (Fri)  ==>  2022-07-01T03:45:00 (Fri) 
               ({ 1656043903, 1656647100 }), 
               // 2022-06-24T14:11:43 (Fri)  ==>  2022-07-01T03:45:00 (Fri) 
               ({ 1656079903, 1656647100 }), 
            }), 
            ({ ({ 2, 1, 1, 0, 3, 0 }),  // PI-172 adjusted from 02:00 to 03:00 
               // Every day at 03:00. 
               // 2022-06-22T14:11:43 (Wed)  ==>  2022-06-23T03:00:00 (Thu) 
               ({ 1655907103, 1655953200 }), 
               // 2022-06-23T14:11:43 (Thu)  ==>  2022-06-24T03:00:00 (Fri) 
               ({ 1655993503, 1656039600 }), 
               // 2022-06-24T02:11:43 (Fri)  ==>  2022-06-24T03:00:00 (Fri) 
               ({ 1656036703, 1656039600 }), 
               // 2022-06-24T03:11:43 (Fri)  ==>  2022-06-25T03:00:00 (Sat) 
               ({ 1656040303, 1656126000 }), 
               // 2022-06-24T03:21:43 (Fri)  ==>  2022-06-25T03:00:00 (Sat) 
               ({ 1656040903, 1656126000 }), // Borken 2022-06-25T03:15:00 
               // 2022-06-24T03:51:43 (Fri)  ==>  2022-06-25T03:00:00 (Sat) 
               ({ 1656042703, 1656126000 }), // Borken 2022-06-25T03:45:00 
               // 2022-06-24T04:11:43 (Fri)  ==>  2022-06-25T03:00:00 (Sat) 
               ({ 1656043903, 1656126000 }), 
               // 2022-06-24T14:11:43 (Fri)  ==>  2022-06-25T03:00:00 (Sat) 
               ({ 1656079903, 1656126000 }), 
            }), 
          }), array(array(int)) test) { 
    val = test[0]; 
    while(1) { 
      foreach(test[1..], [int when, int expected]) { 
        int got = get_next(when); 
        if (got != expected) { 
          failures++; 
          werror("Test failed for %O\n" 
                 "When: %d\n" 
                 "%O\n" 
                 "Expected: %d\n" 
                 "%O\n" 
                 "Got: %d\n" 
                 "%O\n", 
                 test, 
                 when, localtime(when), 
                 expected, localtime(expected), 
                 got, localtime(got)); 
        } else { 
          successes++; 
        } 
      } 
      if ((sizeof(val) > 5) && !val[5]) { 
        // Redo in compat mode. 
        val = val[..4]; 
        continue; 
      } 
      break; 
    } 
  } 
  werror("Succeeded on %d, Failed on %d.\n", successes, failures); 
  return !!failures; 
} 
 
#endif 
 
#define VALS_SORT             0 
#define VALS_REPEAT_HOURS   1 
#define VALS_REPEAT_COUNT   2 
#define VALS_DAY            3 
#define VALS_HOUR           4 
#define VALS_MINUTE         5 
 
protected multiset(int(0..2)) valid_sorts = (< 0, 1, 2 >); 
 
//! Transforms the form variables given in the @[vl] attribute 
//! to the internal time representation as follows. 
//! 
//! @array 
//!   @elem int(0..2) sort 
//!     @int 
//!       @value 0 
//!         Never 
//!       @value 1 
//!         Every x hour 
//!       @value 2 
//!         Every x y at z 
//!     @endint 
//! 
//!   @elem int(1..23) hour 
//!     Number of hours between restarts. 
//! 
//!   @elem int(1..9) everynth 
//!     Number of days or weeks to skip between restarts. 
//! 
//!   @elem int(0..7) day 
//!     @int 
//!       @value 0 
//!         Day 
//!       @value 1 
//!         Sunday 
//!       @value 2..7 
//!         Rest of weekdays 
//!     @endint 
//! 
//!   @elem int(0..23) time_hour 
//!     Time at which to restart (hour). 
//! 
//!   @elem int(0..59)|void time_min 
//!     Time at which to restart (minute). 
//!     If not present at minute 0 (compat). 
//! @endarray 
array transform_from_form( string what, mapping vl ) 
{ 
  array res = query() + ({}); 
  if(sizeof(res) <= VALS_HOUR) { 
    res = ({ 0, 2, 1, 6, 3, -1 }); 
  } else if (sizeof(res) <= VALS_MINUTE) { 
    // Compat. 
    res += ({ 0 }); 
  } 
 
  res[VALS_SORT] = (int)what; 
  for(int i=1; i <= VALS_MINUTE; i++) { 
    res[i] = (int)vl[(string)i]; 
    res[i] = max( ({ 0, 1, 1, 0, 0, 0 })[i], res[i] ); 
    res[i] = min( ({ 2, 23, 9, 7, 23, 59 })[i], res[i] ); 
  } 
 
  if (!res[VALS_MINUTE]) { 
    // Compat. 
    res = res[..VALS_HOUR]; 
  } 
 
  return res; 
} 
 
array verify_set_from_form(array val) 
{ 
  if ((sizeof(val) >= VALS_SORT) && 
      !valid_sorts[val[VALS_SORT]]) { 
    throw("Invalid operation mode.\n"); 
  } 
  return ::verify_set_from_form(val); 
} 
 
protected int mktime(mapping m) 
{ 
  int t = predef::mktime(m); 
  if (m->timezone) { 
    // Compensate for cases where predef::mktime() is broken. 
    // Cf [WS-469]. 
    t += t - predef::mktime(localtime(t)); 
  } 
  return t; 
} 
 
private mapping next_or_same_day(mapping from, int day, int hour, int minute) 
{ 
  if(from->wday==day && from->hour<hour) 
    return from; 
  if(from->wday==day && from->hour == hour && from->min<minute) 
    return from; 
  return next_day(from, day); 
} 
 
private mapping next_day(mapping from, int day) 
{ 
  int num_days = ((6 + day - from->wday) % 7) + 1; 
 
  // NB: Use a time in the middle of the date to ensure that we 
  //     don't miss the next day due to DST or similar. 
  //     Adjust the hour back to 00 afterwards. 
  from->hour = 12; 
  mapping m = localtime(mktime(from) + num_days * 3600 * 24); 
  m->hour = from->hour = 0; 
  m->min = from->min = 0; 
  return m; 
} 
 
private mapping next_or_same_time(mapping from, int hour, int minute, 
                                  void|int delta) 
{ 
  if (from->hour == hour) { 
    if (minute < 0) { 
      return from; 
    } 
    if ((from->min - (from->min % 15)) == minute) { 
      return from; 
    } 
  } 
  return next_time(from, hour, minute, delta); 
} 
 
private mapping next_time(mapping from, int hour, int minute, void|int delta) 
{ 
  if(from->hour<hour) { 
    from->hour = hour; 
    if (minute < 0) { 
      from->min = 0; 
    } else { 
      from->min = minute; 
    } 
    return from; 
  } else if ((from->hour == hour) && (from->min < minute)) { 
    from->min = minute; 
    return from; 
  } 
  from->min = minute; 
  return localtime(mktime(from) + (24 - from->hour + hour)*3600 + delta); 
} 
 
int get_next( int last ) 
//! Get the next time that matches this schedule, starting from the 
//! posix time @[last]. If last is 0, time(1) will be used instead. 
//! 
//! @returns 
//!  When the next scheduled event is, represented by a posix time integer. 
//!  Note that the returned time may already have occured, so all return 
//!  values < time() essentially means go ahead and do it right away. 
//!  Minutes and seconds are cleared in the return value, so if the scheduler 
//!  is set to every day at 5 o'clock, and this method is called at 5:42 it 
//!  will return the posix time representing 5:00, unless of course @[last] 
//!  was set to a posix time >= 5:00. 
//!  Returns @tt{-1@} if the schedule is disabled (@tt{"Never"@}). 
{ 
  array vals = query(); 
  if (sizeof(vals) == VALS_MINUTE) { 
    vals += ({ 0 }); 
  } 
  if( !vals[VALS_SORT] ) 
    return -1; 
 
  // Every n:th hour. 
  if( vals[VALS_SORT] == 1 ) 
    if( !last ) 
      return time(1); 
    else 
      return last + 3600 * vals[VALS_REPEAT_HOURS]; 
 
  mapping m = localtime( last || time(1) ); 
  m->sec = 0; 
  m->min -= (m->min % 15); 
  if( !vals[VALS_DAY] ) { 
    // Every n:th day at x. 
    if (!last) 
    { 
      for(int i; i<vals[VALS_REPEAT_COUNT]; i++) 
        m = next_or_same_time( m, vals[VALS_HOUR], vals[VALS_MINUTE] ); 
      return mktime(m); 
    } 
    else 
    { 
      for(int i; i<vals[VALS_REPEAT_COUNT]; i++) 
        m = next_time( m, vals[VALS_HOUR], vals[VALS_MINUTE] ); 
      return mktime(m); 
    } 
  } 
 
  // Every x-day at y. 
  if (!last) 
  { 
    for(int i; i<vals[VALS_REPEAT_COUNT]; i++) 
    { 
      m = next_or_same_time( next_or_same_day( m, vals[VALS_DAY]-1, 
                                               vals[VALS_HOUR]+1, 
                                               vals[VALS_MINUTE] ), 
                             vals[VALS_HOUR], vals[VALS_MINUTE], 6*24*3600 ); 
    } 
  } 
  else 
  { 
    for(int i; i<vals[VALS_REPEAT_COUNT]; i++) 
    { 
      m = next_or_same_time( next_or_same_day( m, vals[VALS_DAY]-1, 
                                               vals[VALS_HOUR], 
                                               vals[VALS_MINUTE] ), 
                             vals[VALS_HOUR], vals[VALS_MINUTE], 6*24*3600 ); 
    } 
  } 
  return mktime(m); 
} 
 
#if constant(roxenp) 
 
private string checked( int pos, int alt ) 
{ 
  if(alt==query()[pos]) 
    return " checked='checked'"; 
  return ""; 
} 
 
string render_form( RequestID id, void|mapping additional_args ) 
{ 
  string res, inp1, inp2, inp3, inp4; 
  array vals = query(); 
  if (sizeof(vals) == VALS_MINUTE) { 
    vals += ({ 0 }); 
  } 
 
  res = "<table>"; 
 
  if (valid_sorts[0]) { 
    res += 
      "<tr valign='top'><td><input name='" + path() + "' value='0' type='radio' " + 
      checked(0,0) + " /></td><td>" + LOCALE(482, "Never") + "</td></tr>\n"; 
  } 
 
  if (valid_sorts[1]) { 
    inp1 = HTML.select(path()+"1", "123456789"/1 + "1011121314151617181920212223"/2, (string)vals[VALS_REPEAT_HOURS]); 
 
    res += "<tr valign='top'><td><input name='" + path() + "' value='1' type='radio' " + 
      checked(0,1) + " /></td><td>" + sprintf( LOCALE(483, "Every %s hour(s)."), inp1) + 
      "</td></tr>\n"; 
  } 
 
  if (valid_sorts[2]) { 
    inp1 = HTML.select(path()+"2", "123456789"/1, (string)vals[VALS_REPEAT_COUNT]); 
    inp2 = HTML.select(path()+"3", ({ 
      ({ "0", LOCALE(484, "Day") }), 
      ({ "1", LOCALE(485, "Sunday") }), 
      ({ "2", LOCALE(486, "Monday") }), 
      ({ "3", LOCALE(487, "Tuesday") }), 
      ({ "4", LOCALE(488, "Wednesday") }), 
      ({ "5", LOCALE(489, "Thursday") }), 
      ({ "6", LOCALE(490, "Friday") }), 
      ({ "7", LOCALE(491, "Saturday") }) }), (string)vals[VALS_DAY]); 
    inp3 = HTML.select(path()+"4", 
                       "000102030405060708091011121314151617181920212223"/2, 
                       sprintf("%02d", vals[VALS_HOUR])); 
    inp4 = HTML.select(path()+"5", 
                       "00153045"/2, 
                       sprintf("%02d", vals[VALS_MINUTE])); 
 
    res += "<tr valign='top'><td><input name='" + path() + "' value='2' type='radio' " + 
      checked(0,2) + " /></td>\n<td>" + 
      sprintf(LOCALE(492, "Every %s %s at %s:%s o'clock."), 
              inp1, inp2, inp3, inp4) + 
      "</td></tr>\n"; 
  } 
 
  res += "</table>"; 
 
  return res; 
} 
 
string render_view( RequestID id, void|mapping additional_args ) 
{ 
  array res = query(); 
  if (sizeof(res) == VALS_MINUTE) { 
    res += ({ 0 }); 
  } 
  switch(res[VALS_SORT]) { 
    case 0: 
      return LOCALE(482, "Never"); 
    case 1: 
      return sprintf(LOCALE(493, "Every %d hour."), res[VALS_REPEAT_HOURS]); 
    case 2: 
      string period = ({ 
        LOCALE(484, "Day"), 
        LOCALE(485, "Sunday"), 
        LOCALE(486, "Monday"), 
        LOCALE(487, "Tuesday"), 
        LOCALE(488, "Wednesday"), 
        LOCALE(489, "Thursday"), 
        LOCALE(490, "Friday"), 
        LOCALE(491, "Saturday") 
      })[res[VALS_DAY]]; 
 
      return sprintf(LOCALE(494, "Every %d %s at %02d:%02d"), 
                     res[VALS_REPEAT_COUNT], period, 
                     res[VALS_HOUR], res[VALS_MINUTE]); 
    default: 
      return LOCALE(495, "Error in stored value."); 
  } 
} 
 
protected void create(array(int) default_value, void|int flags, 
                      void|LocaleString std_name, void|LocaleString std_doc, 
                      multiset(int(0..2))|void valid_sorts) 
{ 
  if (valid_sorts) { 
    this_program::valid_sorts &= valid_sorts; 
    if (!sizeof(this_program::valid_sorts)) { 
      error("Invalid set of operation modes for Schedule: %O\n", valid_sorts); 
    } 
  } 
  if (sizeof(default_value||({})) && 
      !this_program::valid_sorts[default_value[VALS_SORT]]) { 
    error("Invalid default mode for Schedule: %O\n", default_value[VALS_SORT]); 
  } 
  ::create(default_value, flags, std_name, std_doc); 
} 
 
#endif