| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/localtime.h" |
| 33 |
#include "asterisk/logger.h" |
| 34 |
#include "asterisk/pbx.h" |
| 35 |
#include "asterisk/strings.h" |
| 36 |
#include "asterisk/utils.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
static int lookup_name(const char *s, const char * const names[], int max) |
| 43 |
{ |
| 44 |
int i; |
| 45 |
|
| 46 |
if (names && *s > '9') { |
| 47 |
for (i = 0; names[i]; i++) { |
| 48 |
if (!strcasecmp(s, names[i])) { |
| 49 |
return i; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
if (sscanf(s, "%2d", &i) == 1 && i >= 1 && i <= max) { |
| 56 |
|
| 57 |
return i - 1; |
| 58 |
} |
| 59 |
return -1; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
static unsigned get_range(char *src, int max, const char * const names[], const char *msg) |
| 66 |
{ |
| 67 |
int start, end; |
| 68 |
unsigned int mask = 0; |
| 69 |
char *part; |
| 70 |
|
| 71 |
|
| 72 |
if (ast_strlen_zero(src) || !strcmp(src, "*")) { |
| 73 |
return (1 << max) - 1; |
| 74 |
} |
| 75 |
|
| 76 |
while ((part = strsep(&src, "&"))) { |
| 77 |
|
| 78 |
char *endpart = strchr(part, '-'); |
| 79 |
if (endpart) { |
| 80 |
*endpart++ = '\0'; |
| 81 |
} |
| 82 |
|
| 83 |
if ((start = lookup_name(part, names, max)) < 0) { |
| 84 |
ast_log(LOG_WARNING, "Invalid %s '%s', skipping element\n", msg, part); |
| 85 |
continue; |
| 86 |
} |
| 87 |
if (endpart) { |
| 88 |
if ((end = lookup_name(endpart, names, max)) < 0) { |
| 89 |
ast_log(LOG_WARNING, "Invalid end %s '%s', skipping element\n", msg, endpart); |
| 90 |
continue; |
| 91 |
} |
| 92 |
} else { |
| 93 |
end = start; |
| 94 |
} |
| 95 |
|
| 96 |
mask |= (1 << end); |
| 97 |
while (start != end) { |
| 98 |
mask |= (1 << start); |
| 99 |
if (++start >= max) { |
| 100 |
start = 0; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
return mask; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
static void get_timerange(struct ast_timing *i, char *times) |
| 109 |
{ |
| 110 |
char *endpart, *part; |
| 111 |
int x; |
| 112 |
int st_h, st_m; |
| 113 |
int endh, endm; |
| 114 |
int minute_start, minute_end; |
| 115 |
|
| 116 |
|
| 117 |
memset(i->minmask, 0, sizeof(i->minmask)); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
if (ast_strlen_zero(times) || !strcmp(times, "*")) { |
| 122 |
|
| 123 |
for (x = 0; x < 48; x++) { |
| 124 |
i->minmask[x] = 0x3fffffff; |
| 125 |
} |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
while ((part = strsep(×, "&"))) { |
| 130 |
if (!(endpart = strchr(part, '-'))) { |
| 131 |
if (sscanf(part, "%2d:%2d", &st_h, &st_m) != 2 || st_h < 0 || st_h > 23 || st_m < 0 || st_m > 59) { |
| 132 |
ast_log(LOG_WARNING, "%s isn't a valid time.\n", part); |
| 133 |
continue; |
| 134 |
} |
| 135 |
i->minmask[st_h * 2 + (st_m >= 30 ? 1 : 0)] |= (1 << (st_m % 30)); |
| 136 |
continue; |
| 137 |
} |
| 138 |
*endpart++ = '\0'; |
| 139 |
|
| 140 |
while (*endpart && !isdigit(*endpart)) { |
| 141 |
endpart++; |
| 142 |
} |
| 143 |
if (!*endpart) { |
| 144 |
ast_log(LOG_WARNING, "Invalid time range starting with '%s-'.\n", part); |
| 145 |
continue; |
| 146 |
} |
| 147 |
if (sscanf(part, "%2d:%2d", &st_h, &st_m) != 2 || st_h < 0 || st_h > 23 || st_m < 0 || st_m > 59) { |
| 148 |
ast_log(LOG_WARNING, "'%s' isn't a valid start time.\n", part); |
| 149 |
continue; |
| 150 |
} |
| 151 |
if (sscanf(endpart, "%2d:%2d", &endh, &endm) != 2 || endh < 0 || endh > 23 || endm < 0 || endm > 59) { |
| 152 |
ast_log(LOG_WARNING, "'%s' isn't a valid end time.\n", endpart); |
| 153 |
continue; |
| 154 |
} |
| 155 |
minute_start = st_h * 60 + st_m; |
| 156 |
minute_end = endh * 60 + endm; |
| 157 |
|
| 158 |
for (x = minute_start; x != minute_end; x = (x + 1) % (24 * 60)) { |
| 159 |
i->minmask[x / 30] |= (1 << (x % 30)); |
| 160 |
} |
| 161 |
|
| 162 |
i->minmask[x / 30] |= (1 << (x % 30)); |
| 163 |
} |
| 164 |
|
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
static const char * const days[] = |
| 169 |
{ |
| 170 |
"sun", |
| 171 |
"mon", |
| 172 |
"tue", |
| 173 |
"wed", |
| 174 |
"thu", |
| 175 |
"fri", |
| 176 |
"sat", |
| 177 |
NULL, |
| 178 |
}; |
| 179 |
|
| 180 |
static const char * const months[] = |
| 181 |
{ |
| 182 |
"jan", |
| 183 |
"feb", |
| 184 |
"mar", |
| 185 |
"apr", |
| 186 |
"may", |
| 187 |
"jun", |
| 188 |
"jul", |
| 189 |
"aug", |
| 190 |
"sep", |
| 191 |
"oct", |
| 192 |
"nov", |
| 193 |
"dec", |
| 194 |
NULL, |
| 195 |
}; |
| 196 |
|
| 197 |
int ast_build_timing(struct ast_timing *i, const char *info_in) |
| 198 |
{ |
| 199 |
char *info; |
| 200 |
int j, num_fields, last_sep = -1; |
| 201 |
|
| 202 |
i->timezone = NULL; |
| 203 |
|
| 204 |
|
| 205 |
if (ast_strlen_zero(info_in)) { |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
info = ast_strdupa(info_in); |
| 211 |
|
| 212 |
|
| 213 |
for (j = 0, num_fields = 1; info[j] != '\0'; j++) { |
| 214 |
if (info[j] == '|' || info[j] == ',') { |
| 215 |
last_sep = j; |
| 216 |
num_fields++; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
if (num_fields == 5) { |
| 222 |
i->timezone = ast_strdup(info + last_sep + 1); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
i->monthmask = 0xfff; |
| 227 |
i->daymask = 0x7fffffffU; |
| 228 |
i->dowmask = 0x7f; |
| 229 |
|
| 230 |
get_timerange(i, strsep(&info, "|,")); |
| 231 |
if (info) |
| 232 |
i->dowmask = get_range(strsep(&info, "|,"), 7, days, "day of week"); |
| 233 |
if (info) |
| 234 |
i->daymask = get_range(strsep(&info, "|,"), 31, NULL, "day"); |
| 235 |
if (info) |
| 236 |
i->monthmask = get_range(strsep(&info, "|,"), 12, months, "month"); |
| 237 |
return 1; |
| 238 |
} |
| 239 |
|
| 240 |
int ast_check_timing(const struct ast_timing *i) |
| 241 |
{ |
| 242 |
return ast_check_timing2(i, ast_tvnow()); |
| 243 |
} |
| 244 |
|
| 245 |
int ast_check_timing2(const struct ast_timing *i, const struct timeval tv) |
| 246 |
{ |
| 247 |
struct ast_tm tm; |
| 248 |
|
| 249 |
ast_localtime(&tv, &tm, i->timezone); |
| 250 |
|
| 251 |
|
| 252 |
if (!(i->monthmask & (1 << tm.tm_mon))) |
| 253 |
return 0; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
if (!(i->daymask & (1 << (tm.tm_mday-1)))) |
| 258 |
return 0; |
| 259 |
|
| 260 |
|
| 261 |
if (!(i->dowmask & (1 << tm.tm_wday))) |
| 262 |
return 0; |
| 263 |
|
| 264 |
|
| 265 |
if ((tm.tm_hour < 0) || (tm.tm_hour > 23)) { |
| 266 |
ast_log(LOG_WARNING, "Insane time...\n"); |
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
if (!(i->minmask[tm.tm_hour * 2 + (tm.tm_min >= 30 ? 1 : 0)] & (1 << (tm.tm_min >= 30 ? tm.tm_min - 30 : tm.tm_min)))) |
| 273 |
return 0; |
| 274 |
|
| 275 |
|
| 276 |
return 1; |
| 277 |
} |
| 278 |
|
| 279 |
int ast_destroy_timing(struct ast_timing *i) |
| 280 |
{ |
| 281 |
if (i->timezone) { |
| 282 |
ast_free(i->timezone); |
| 283 |
i->timezone = NULL; |
| 284 |
} |
| 285 |
return 0; |
| 286 |
} |