calendar.c revision 1.30 1 /* $NetBSD: calendar.c,v 1.30 2004/01/05 23:23:34 jmmv Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)calendar.c 8.4 (Berkeley) 1/7/95";
41 #endif
42 __RCSID("$NetBSD: calendar.c,v 1.30 2004/01/05 23:23:34 jmmv Exp $");
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/stat.h>
48 #include <sys/uio.h>
49 #include <sys/wait.h>
50
51 #include <ctype.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <pwd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <time.h>
60 #include <tzfile.h>
61 #include <unistd.h>
62
63 #include "pathnames.h"
64
65 #ifndef TRUE
66 #define TRUE 1
67 #endif
68 #ifndef FALSE
69 #define FALSE 0
70 #endif
71
72 static unsigned short lookahead = 1, weekend = 2;
73 static char *fname = "calendar", *datestr = NULL;
74 static struct passwd *pw;
75 static int doall;
76 static char path[MAXPATHLEN + 1];
77
78 /* 1-based month, 0-based days, cumulative */
79 static int daytab[][14] = {
80 { 0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 },
81 { 0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
82 };
83 static struct tm *tp;
84 static int *cumdays, offset, yrdays;
85 static char dayname[10];
86
87 static struct iovec header[] = {
88 { "From: ", 6 },
89 { NULL, 0 },
90 { " (Reminder Service)\nTo: ", 24 },
91 { NULL, 0 },
92 { "\nSubject: ", 10 },
93 { NULL, 0 },
94 { "'s Calendar\nPrecedence: bulk\n\n", 30 },
95 };
96
97 static char *days[] = {
98 "sun", "mon", "tue", "wed", "thu", "fri", "sat", NULL,
99 };
100
101 static char *months[] = {
102 "jan", "feb", "mar", "apr", "may", "jun",
103 "jul", "aug", "sep", "oct", "nov", "dec", NULL,
104 };
105
106 int main(int, char **);
107
108 static void atodays(int, char *, unsigned short *);
109 static void cal(void);
110 static void closecal(FILE *);
111 static int getday(char *);
112 static int getfield(char *, char **, int *);
113 static void getmmdd(struct tm *, char *);
114 static int getmonth(char *);
115 static int isnow(char *);
116 static FILE *opencal(void);
117 static void settime(void);
118 static void usage(void) __attribute__((__noreturn__));
119
120 int
121 main(argc, argv)
122 int argc;
123 char *argv[];
124 {
125 int ch;
126 const char *caldir;
127
128 while ((ch = getopt(argc, argv, "-ad:f:l:w:")) != -1)
129 switch (ch) {
130 case '-': /* backward contemptible */
131 case 'a':
132 if (getuid()) {
133 errno = EPERM;
134 err(1, NULL);
135 }
136 doall = 1;
137 break;
138 case 'd':
139 datestr = optarg;
140 break;
141 case 'f':
142 fname = optarg;
143 break;
144 case 'l':
145 atodays(ch, optarg, &lookahead);
146 break;
147 case 'w':
148 atodays(ch, optarg, &weekend);
149 break;
150 case '?':
151 default:
152 usage();
153 }
154 argc -= optind;
155 argv += optind;
156
157 if (argc)
158 usage();
159
160 settime();
161 if (doall) {
162 while ((pw = getpwent()) != NULL) {
163 (void)setegid(pw->pw_gid);
164 (void)seteuid(pw->pw_uid);
165 if (!chdir(pw->pw_dir))
166 cal();
167 (void)seteuid(0);
168 }
169 } else if ((caldir = getenv("CALENDAR_DIR")) != NULL) {
170 if(!chdir(caldir))
171 cal();
172 } else {
173 cal();
174 }
175 exit(0);
176 }
177
178 static void
179 cal(void)
180 {
181 int printing;
182 FILE *fp;
183 char *line;
184
185 if ((fp = opencal()) == NULL)
186 return;
187 while ((line = fparseln(stdin, NULL, NULL, NULL, FPARSELN_UNESCCOMM))
188 != NULL) {
189 if (line[0] == '\0')
190 continue;
191 if (line[0] != '\t')
192 printing = isnow(line) ? 1 : 0;
193 if (printing)
194 (void)fprintf(fp, "%s\n", line);
195 free(line);
196
197 }
198 closecal(fp);
199 }
200
201
202 static void
203 settime(void)
204 {
205 time_t now;
206
207 (void)time(&now);
208 tp = localtime(&now);
209 if (datestr) {
210 getmmdd(tp, datestr);
211 }
212 if (isleap(tp->tm_year + TM_YEAR_BASE)) {
213 yrdays = DAYSPERLYEAR;
214 cumdays = daytab[1];
215 } else {
216 yrdays = DAYSPERNYEAR;
217 cumdays = daytab[0];
218 }
219 /* Friday displays Monday's events */
220 offset = tp->tm_wday == 5 ? lookahead + weekend : lookahead;
221 header[5].iov_base = dayname;
222 header[5].iov_len = strftime(dayname, sizeof(dayname), "%A", tp);
223 }
224
225 /*
226 * Possible date formats include any combination of:
227 * 3-charmonth (January, Jan, Jan)
228 * 3-charweekday (Friday, Monday, mon.)
229 * numeric month or day (1, 2, 04)
230 *
231 * Any character may separate them, or they may not be separated. Any line,
232 * following a line that is matched, that starts with "whitespace", is shown
233 * along with the matched line.
234 */
235 static int
236 isnow(endp)
237 char *endp;
238 {
239 int day, flags, month, v1, v2;
240
241 #define F_ISMONTH 0x01
242 #define F_ISDAY 0x02
243 flags = 0;
244 /* didn't recognize anything, skip it */
245 if (!(v1 = getfield(endp, &endp, &flags)))
246 return (0);
247 if (flags & F_ISDAY || v1 > 12) {
248 /* found a day */
249 day = v1;
250 month = tp->tm_mon + 1;
251 } else if (flags & F_ISMONTH) {
252 month = v1;
253 /* if no recognizable day, assume the first */
254 if (!(day = getfield(endp, &endp, &flags)))
255 day = 1;
256 } else {
257 v2 = getfield(endp, &endp, &flags);
258 if (flags & F_ISMONTH) {
259 day = v1;
260 month = v2;
261 } else {
262 /* F_ISDAY set, v2 > 12, or no way to tell */
263 month = v1;
264 /* if no recognizable day, assume the first */
265 day = v2 ? v2 : 1;
266 }
267 }
268 if (flags & F_ISDAY)
269 day = tp->tm_mday + (((day - 1) - tp->tm_wday + 7) % 7);
270 day = cumdays[month] + day;
271
272 /* if today or today + offset days */
273 if (day >= tp->tm_yday && day <= tp->tm_yday + offset)
274 return (1);
275 /* if number of days left in this year + days to event in next year */
276 if (yrdays - tp->tm_yday + day <= offset)
277 return (1);
278 return (0);
279 }
280
281 static int
282 getfield(p, endp, flags)
283 char *p, **endp;
284 int *flags;
285 {
286 int val;
287 char *start, savech;
288
289 #define FLDCHAR(a) (*p != '\0' && !isdigit((unsigned char)*p) && \
290 !isalpha((unsigned char)*p) && *p != '*')
291
292 for (; FLDCHAR(*p); ++p)
293 continue;
294 if (*p == '*') { /* `*' is current month */
295 *flags |= F_ISMONTH;
296 *endp = p+1;
297 return (tp->tm_mon + 1);
298 }
299 if (isdigit((unsigned char)*p)) {
300 val = strtol(p, &p, 10); /* if 0, it's failure */
301 for (; FLDCHAR(*p); ++p)
302 continue;
303 *endp = p;
304 return (val);
305 }
306 for (start = p; *p != '\0' && isalpha((unsigned char)*++p);)
307 continue;
308 savech = *p;
309 *p = '\0';
310 if ((val = getmonth(start)) != 0) {
311 *flags |= F_ISMONTH;
312 } else if ((val = getday(start)) != 0) {
313 *flags |= F_ISDAY;
314 } else {
315 *p = savech;
316 return (0);
317 }
318 for (*p = savech; FLDCHAR(*p); ++p)
319 continue;
320 *endp = p;
321 return (val);
322 }
323
324 static FILE *
325 opencal(void)
326 {
327 int fd, pdes[2];
328
329 /* open up calendar file as stdin */
330 if (!freopen(fname, "rf", stdin)) {
331 if (doall)
332 return (NULL);
333 err(1, "Cannot open `%s'", fname);
334 }
335 if (pipe(pdes) < 0) {
336 warn("Cannot open pipe");
337 return (NULL);
338 }
339 switch (fork()) {
340 case -1: /* error */
341 (void)close(pdes[0]);
342 (void)close(pdes[1]);
343 return (NULL);
344 case 0:
345 /* child -- stdin already setup, set stdout to pipe input */
346 if (pdes[1] != STDOUT_FILENO) {
347 (void)dup2(pdes[1], STDOUT_FILENO);
348 (void)close(pdes[1]);
349 }
350 (void)close(pdes[0]);
351 (void)execl(_PATH_CPP, "cpp", "-traditional", "-P", "-I.",
352 "-I" _PATH_CALENDARS, NULL);
353 err(1, "Cannot exec `%s'", _PATH_CPP);
354 /*NOTREACHED*/
355 }
356 /* parent -- set stdin to pipe output */
357 (void)dup2(pdes[0], STDIN_FILENO);
358 (void)close(pdes[0]);
359 (void)close(pdes[1]);
360
361 /* not reading all calendar files, just set output to stdout */
362 if (!doall)
363 return (stdout);
364
365 /* set output to a temporary file, so if no output don't send mail */
366 (void)snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP);
367 if ((fd = mkstemp(path)) < 0) {
368 warn("Cannot create temporary file");
369 return (NULL);
370 }
371 return (fdopen(fd, "w+"));
372 }
373
374 static void
375 closecal(fp)
376 FILE *fp;
377 {
378 struct stat sbuf;
379 int nread, pdes[2], status;
380 char buf[1024];
381
382 if (!doall)
383 return;
384
385 (void)rewind(fp);
386 if (fstat(fileno(fp), &sbuf) || !sbuf.st_size)
387 goto done;
388 if (pipe(pdes) < 0)
389 goto done;
390 switch (fork()) {
391 case -1: /* error */
392 (void)close(pdes[0]);
393 (void)close(pdes[1]);
394 goto done;
395 case 0:
396 /* child -- set stdin to pipe output */
397 if (pdes[0] != STDIN_FILENO) {
398 (void)dup2(pdes[0], STDIN_FILENO);
399 (void)close(pdes[0]);
400 }
401 (void)close(pdes[1]);
402 (void)execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
403 "\"Reminder Service\"", "-f", "root", NULL);
404 err(1, "Cannot exec `%s'", _PATH_SENDMAIL);
405 /*NOTREACHED*/
406 }
407 /* parent -- write to pipe input */
408 (void)close(pdes[0]);
409
410 header[1].iov_base = header[3].iov_base = (void *)pw->pw_name;
411 header[1].iov_len = header[3].iov_len = strlen(pw->pw_name);
412 writev(pdes[1], header, 7);
413 while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0)
414 (void)write(pdes[1], buf, nread);
415 (void)close(pdes[1]);
416
417 done: (void)fclose(fp);
418 (void)unlink(path);
419 while (wait(&status) >= 0)
420 continue;
421 }
422
423 static int
424 getmonth(s)
425 char *s;
426 {
427 char **p;
428
429 for (p = months; *p; ++p)
430 if (!strncasecmp(s, *p, 3))
431 return ((p - months) + 1);
432 return (0);
433 }
434
435 static int
436 getday(s)
437 char *s;
438 {
439 char **p;
440
441 for (p = days; *p; ++p)
442 if (!strncasecmp(s, *p, 3))
443 return ((p - days) + 1);
444 return (0);
445 }
446
447 static void
448 atodays(int ch, char *optarg, unsigned short *days)
449 {
450 int u;
451
452 u = atoi(optarg);
453 if ((u < 0) || (u > 366)) {
454 warnx("-%c %d out of range 0-366, ignored.", ch, u);
455 } else {
456 *days = u;
457 }
458 }
459
460 #define todigit(x) ((x) - '0')
461 #define ATOI2(x) (todigit((x)[0]) * 10 + todigit((x)[1]))
462 #define ISDIG2(x) (isdigit((unsigned char)(x)[0]) && isdigit((unsigned char)(x)[1]))
463
464 static void
465 getmmdd(struct tm *tp, char *ds)
466 {
467 int ok = FALSE;
468 struct tm ttm;
469
470 ttm = *tp;
471 ttm.tm_isdst = -1;
472
473 if (ISDIG2(ds)) {
474 ttm.tm_mon = ATOI2(ds) - 1;
475 ds += 2;
476 }
477
478 if (ISDIG2(ds)) {
479 ttm.tm_mday = ATOI2(ds);
480 ds += 2;
481
482 ok = TRUE;
483 }
484
485 if (ok) {
486 if (ISDIG2(ds) && ISDIG2(ds + 2)) {
487 ttm.tm_year = ATOI2(ds) * 100 - TM_YEAR_BASE;
488 ds += 2;
489 ttm.tm_year += ATOI2(ds);
490 } else if (ISDIG2(ds)) {
491 ttm.tm_year = ATOI2(ds);
492 if (ttm.tm_year < 69)
493 ttm.tm_year += 2000 - TM_YEAR_BASE;
494 else
495 ttm.tm_year += 1900 - TM_YEAR_BASE;
496 }
497 }
498
499 if (ok && (mktime(&ttm) < 0)) {
500 ok = FALSE;
501 }
502
503 if (ok) {
504 *tp = ttm;
505 } else {
506 warnx("Can't convert `%s' to date, ignored.", ds);
507 usage();
508 }
509 }
510
511 static void
512 usage(void)
513 {
514 (void)fprintf(stderr, "usage: %s [-a] [-d MMDD[[YY]YY]"
515 " [-f fname] [-l days] [-w days]\n", getprogname());
516 exit(1);
517 }
518