]>
diplodocus.org Git - nmh/blob - uip/mhical.c
1 /* mhical.c -- operate on an iCalendar request
3 * This code is Copyright (c) 2014, by the authors of nmh.
4 * See the COPYRIGHT file in the root directory of the nmh
5 * distribution for complete copyright information.
9 #include "h/icalendar.h"
10 #include "sbr/icalparse.h"
11 #include <h/fmt_scan.h>
12 #include "h/addrsbr.h"
27 static void convert_to_reply (contentline
*, act
);
28 static void convert_to_cancellation (contentline
*);
29 static void convert_common (contentline
*, act
);
30 static void dump_unfolded (FILE *, contentline
*);
31 static void output (FILE *, contentline
*, int);
32 static void display (FILE *, contentline
*, char *);
33 static const char *identity (const contentline
*) PURE
;
34 static char *format_params (char *, param_list
*);
35 static char *fold (char *, int);
37 #define MHICAL_SWITCHES \
38 X("reply accept|decline|tentative", 0, REPLYSW) \
39 X("cancel", 0, CANCELSW) \
40 X("form formatfile", 0, FORMSW) \
41 X("format string", 5, FMTSW) \
42 X("infile", 0, INFILESW) \
43 X("outfile", 0, OUTFILESW) \
44 X("contenttype", 0, CONTENTTYPESW) \
45 X("nocontenttype", 0, NCONTENTTYPESW) \
46 X("unfold", 0, UNFOLDSW) \
47 X("debug", 0, DEBUGSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(MHICAL
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(MHICAL
, switches
);
59 vevent vevents
= { NULL
, NULL
, NULL
};
60 int parser_status
= 0;
63 main (int argc
, char *argv
[])
65 /* RFC 5322 § 3.3 date-time format, including the optional
66 day-of-week and not including the optional seconds. The
67 zone is required by the RFC but not always output by this
68 format, because RFC 5545 § 3.3.5 allows date-times not
69 bound to any time zone. */
71 act action
= ACT_NONE
;
72 char *infile
= NULL
, *outfile
= NULL
;
73 FILE *inputfile
= NULL
, *outputfile
= NULL
;
74 bool contenttype
= false;
76 vevent
*v
, *nextvevent
;
77 char *form
= "mhical.24hour", *format
= NULL
;
78 char **argp
, **arguments
, *cp
;
80 icaldebug
= 0; /* Global provided by bison (with name-prefix "ical"). */
82 if (nmh_init(argv
[0], true, false)) { return 1; }
84 arguments
= getarguments (invo_name
, argc
, argv
, 1);
90 while ((cp
= *argp
++)) {
92 switch (smatch (++cp
, switches
)) {
94 ambigsw (cp
, switches
);
97 die("-%s unknown", cp
);
101 snprintf (buf
, sizeof buf
, "%s [switches]", invo_name
);
102 print_help (buf
, switches
, 1);
106 print_version(invo_name
);
113 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
114 die("missing argument to %s", argp
[-2]);
115 if (! strcasecmp (cp
, "accept")) {
117 } else if (! strcasecmp (cp
, "decline")) {
118 action
= ACE_DECLINE
;
119 } else if (! strcasecmp (cp
, "tentative")) {
120 action
= ACT_TENTATIVE
;
121 } else if (! strcasecmp (cp
, "delegate")) {
122 action
= ACT_DELEGATE
;
124 die("Unknown action: %s", cp
);
133 if (! (form
= *argp
++) || *form
== '-')
134 die("missing argument to %s", argp
[-2]);
138 if (! (format
= *argp
++) || *format
== '-')
139 die("missing argument to %s", argp
[-2]);
144 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
145 die("missing argument to %s", argp
[-2]);
146 infile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
149 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
150 die("missing argument to %s", argp
[-2]);
151 outfile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
171 if ((inputfile
= fopen (infile
, "r"))) {
172 icalset_inputfile (inputfile
);
174 adios (infile
, "error opening");
181 if ((outputfile
= fopen (outfile
, "w"))) {
182 icalset_outputfile (outputfile
);
184 adios (outfile
, "error opening");
190 vevents
.last
= &vevents
;
191 /* vevents is accessed by parser as global. */
194 for (v
= &vevents
; v
; v
= nextvevent
) {
195 if (! unfold
&& v
!= &vevents
&& v
->contentlines
&&
196 v
->contentlines
->name
&&
197 strcasecmp (v
->contentlines
->name
, "END") &&
198 v
->contentlines
->value
&&
199 strcasecmp (v
->contentlines
->value
, "VCALENDAR")) {
200 /* Output blank line between vevents. Not before
201 first vevent and not after last. */
202 putc ('\n', outputfile
);
205 if (action
== ACT_NONE
) {
207 dump_unfolded (outputfile
, v
->contentlines
);
209 char *nfs
= new_fs (form
, format
, NULL
);
211 display (outputfile
, v
->contentlines
, nfs
);
215 if (action
== ACT_CANCEL
) {
216 convert_to_cancellation (v
->contentlines
);
218 convert_to_reply (v
->contentlines
, action
);
220 output (outputfile
, v
->contentlines
, contenttype
);
223 free_contentlines (v
->contentlines
);
224 nextvevent
= v
->next
;
231 if (fclose (inputfile
) != 0) {
232 advise (infile
, "error closing");
237 if (fclose (outputfile
) != 0) {
238 advise (outfile
, "error closing");
243 return parser_status
;
247 * - Change METHOD from REQUEST to REPLY.
249 * - Remove all ATTENDEE lines for other users (based on ismymbox ()).
250 * - For the user's ATTENDEE line:
251 * - Remove ROLE and RSVP parameters.
252 * - Change PARTSTAT value to indicate reply action, e.g., ACCEPTED,
253 * DECLINED, or TENTATIVE.
254 * - Insert action at beginning of SUMMARY value.
255 * - Remove all X- lines.
256 * - Update DTSTAMP with current timestamp.
257 * - Remove all DESCRIPTION lines.
258 * - Excise VALARM sections.
261 convert_to_reply (contentline
*clines
, act action
)
263 char *partstat
= NULL
;
264 bool found_my_attendee_line
= false;
267 convert_common (clines
, action
);
271 partstat
= "ACCEPTED";
274 partstat
= "DECLINED";
277 partstat
= "TENTATIVE";
283 /* Call find_contentline () with node as argument to find multiple
284 matching contentlines. */
286 (node
= find_contentline (node
, "ATTENDEE", 0));
290 ismymbox (NULL
); /* need to prime ismymbox() */
292 /* According to RFC 5545 § 3.3.3, an email address in the
293 value must be a mailto URI. */
294 if (! strncasecmp (node
->value
, "mailto:", 7)) {
295 char *addr
= node
->value
+ 7;
298 /* Skip any leading whitespace. */
299 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
301 addr
= getname (addr
);
302 mn
= getm (addr
, NULL
, 0, NULL
, 0);
304 /* Need to flush getname after use. */
305 while (getname ("")) { continue; }
308 found_my_attendee_line
= true;
309 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
312 for (v
= p
->values
; v
; v
= v
->next
) {
313 if (! strcasecmp (p
->param_name
, "ROLE") ||
314 ! strcasecmp (p
->param_name
, "RSVP")) {
316 } else if (! strcasecmp (p
->param_name
, "PARTSTAT")) {
318 v
->value
= strdup (partstat
);
323 remove_contentline (node
);
330 if (! found_my_attendee_line
) {
331 /* Generate and attach an ATTENDEE line for me. */
334 /* Add it after the ORGANIZER line, or if none, BEGIN:VEVENT line. */
335 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) ||
336 (node
= find_contentline (clines
, "BEGIN", "VEVENT"))) {
337 contentline
*new_node
= add_contentline (node
, "ATTENDEE");
339 add_param_name (new_node
, mh_xstrdup ("PARTSTAT"));
340 add_param_value (new_node
, mh_xstrdup (partstat
));
341 add_param_name (new_node
, mh_xstrdup ("CN"));
342 add_param_value (new_node
, mh_xstrdup (getfullname ()));
343 new_node
->value
= concat ("MAILTO:", getlocalmbox (), NULL
);
347 /* Call find_contentline () with node as argument to find multiple
348 matching contentlines. */
350 (node
= find_contentline (node
, "DESCRIPTION", 0));
352 /* ACCEPT, at least, replies don't seem to have DESCRIPTIONS. */
353 remove_contentline (node
);
358 * - Change METHOD from REQUEST to CANCEL.
360 * - Insert action at beginning of SUMMARY value.
361 * - Remove all X- lines.
362 * - Update DTSTAMP with current timestamp.
363 * - Change STATUS from CONFIRMED to CANCELLED.
364 * - Increment value of SEQUENCE.
365 * - Excise VALARM sections.
368 convert_to_cancellation (contentline
*clines
)
372 convert_common (clines
, ACT_CANCEL
);
374 if ((node
= find_contentline (clines
, "STATUS", 0)) &&
375 ! strcasecmp (node
->value
, "CONFIRMED")) {
377 node
->value
= mh_xstrdup ("CANCELLED");
380 if ((node
= find_contentline (clines
, "SEQUENCE", 0))) {
381 int sequence
= atoi (node
->value
);
384 (void) snprintf (buf
, sizeof buf
, "%d", sequence
+ 1);
386 node
->value
= mh_xstrdup (buf
);
391 convert_common (contentline
*clines
, act action
)
396 if ((node
= find_contentline (clines
, "METHOD", 0))) {
398 node
->value
= mh_xstrdup (action
== ACT_CANCEL
? "CANCEL" : "REPLY");
401 if ((node
= find_contentline (clines
, "PRODID", 0))) {
403 node
->value
= mh_xstrdup ("nmh mhical v0.1");
406 if ((node
= find_contentline (clines
, "VERSION", 0))) {
408 inform("Version property is missing value, assume 2.0, continuing...");
409 node
->value
= mh_xstrdup ("2.0");
412 if (strcmp (node
->value
, "2.0")) {
413 inform("supports the Version 2.0 specified by RFC 5545 "
414 "but iCalendar object has Version %s, continuing...",
416 node
->value
= mh_xstrdup ("2.0");
420 if ((node
= find_contentline (clines
, "SUMMARY", 0))) {
425 insert
= "Accepted: ";
428 insert
= "Declined: ";
431 insert
= "Tentative: ";
434 die("Delegate replies are not supported");
437 insert
= "Cancelled:";
444 const size_t len
= strlen (insert
) + strlen (node
->value
) + 1;
445 char *tmp
= mh_xmalloc (len
);
447 (void) strncpy (tmp
, insert
, len
);
448 (void) strncat (tmp
, node
->value
, len
- strlen (insert
) - 1);
452 /* Should never get here. */
453 die("Unknown action: %d", action
);
457 if ((node
= find_contentline (clines
, "DTSTAMP", 0))) {
458 const time_t now
= time (NULL
);
461 if (gmtime_r (&now
, &now_tm
)) {
462 /* 17 would be sufficient given that RFC 5545 § 3.3.4
463 supports only a 4 digit year. */
466 if (strftime (buf
, sizeof buf
, "%Y%m%dT%H%M%SZ", &now_tm
)) {
468 node
->value
= mh_xstrdup (buf
);
470 inform("strftime unable to format current time, continuing...");
473 inform("gmtime_r failed on current time, continuing...");
477 /* Excise X- lines and VALARM section(s). */
479 for (node
= clines
; node
; node
= node
->next
) {
480 /* node->name will be NULL if the line was deleted. */
481 if (! node
->name
) { continue; }
484 if (! strcasecmp ("END", node
->name
) &&
485 ! strcasecmp ("VALARM", node
->value
)) {
488 remove_contentline (node
);
490 if (! strcasecmp ("BEGIN", node
->name
) &&
491 ! strcasecmp ("VALARM", node
->value
)) {
493 remove_contentline (node
);
494 } else if (! strncasecmp ("X-", node
->name
, 2)) {
495 remove_contentline (node
);
501 /* Echo the input, but with unfolded lines. */
503 dump_unfolded (FILE *file
, contentline
*clines
)
507 for (node
= clines
; node
; node
= node
->next
) {
508 fputs (node
->input_line
, file
);
513 output (FILE *file
, contentline
*clines
, int contenttype
)
518 /* Generate a Content-Type header to pass the method parameter
519 to mhbuild. Per RFC 5545 Secs. 6 and 8.1, it must be
520 UTF-8. But we don't attempt to do any conversion of the
522 if ((node
= find_contentline (clines
, "METHOD", 0))) {
524 "Content-Type: text/calendar; method=\"%s\"; "
525 "charset=\"UTF-8\"\n\n",
530 for (node
= clines
; node
; node
= node
->next
) {
535 line
= mh_xstrdup (node
->name
);
536 line
= format_params (line
, node
->params
);
539 line
= mh_xrealloc (line
, len
+ 2);
541 line
[len
+ 1] = '\0';
543 line
= fold (add (node
->value
, line
),
544 clines
->cr_before_lf
== CR_BEFORE_LF
);
547 if (clines
->cr_before_lf
!= LF_ONLY
)
556 * Display these fields of the iCalendar event:
560 * - description, except for "\n\n" and in VALARM
562 * - dtstart in local timezone
563 * - dtend in local timezone
564 * - attendees (limited to number specified in initialization)
567 display (FILE *file
, contentline
*clines
, char *nfs
)
569 tzdesc_t timezones
= load_timezones (clines
);
574 int dat
[5] = { 0, 0, 0, INT_MAX
, 0 };
576 charstring_t buffer
= charstring_create (BUFSIZ
);
577 charstring_t attendees
= charstring_create (BUFSIZ
);
578 const unsigned int max_attendees
= 20;
579 unsigned int num_attendees
;
581 /* Don't call on the END:VCALENDAR line. */
582 if (clines
&& clines
->next
) {
583 (void) fmt_compile (nfs
, &fmt
, 1);
586 if ((c
= fmt_findcomp ("method"))) {
587 if ((node
= find_contentline (clines
, "METHOD", 0)) && node
->value
) {
588 c
->c_text
= mh_xstrdup (node
->value
);
592 if ((c
= fmt_findcomp ("organizer"))) {
593 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) &&
595 c
->c_text
= mh_xstrdup (identity (node
));
599 if ((c
= fmt_findcomp ("summary"))) {
600 if ((node
= find_contentline (clines
, "SUMMARY", 0)) && node
->value
) {
601 c
->c_text
= mh_xstrdup (node
->value
);
605 /* Only display DESCRIPTION lines that are outside VALARM section(s). */
607 if ((c
= fmt_findcomp ("description"))) {
608 for (node
= clines
; node
; node
= node
->next
) {
609 /* node->name will be NULL if the line was deleted. */
610 if (node
->name
&& node
->value
&& ! in_valarm
&&
611 ! strcasecmp ("DESCRIPTION", node
->name
) &&
612 strcasecmp (node
->value
, "\\n\\n")) {
613 c
->c_text
= mh_xstrdup (node
->value
);
614 } else if (in_valarm
) {
615 if (! strcasecmp ("END", node
->name
) &&
616 ! strcasecmp ("VALARM", node
->value
)) {
620 if (! strcasecmp ("BEGIN", node
->name
) &&
621 ! strcasecmp ("VALARM", node
->value
)) {
628 if ((c
= fmt_findcomp ("location"))) {
629 if ((node
= find_contentline (clines
, "LOCATION", 0)) &&
631 c
->c_text
= mh_xstrdup (node
->value
);
635 if ((c
= fmt_findcomp ("dtstart"))) {
636 /* Find DTSTART outsize of a VTIMEZONE section. */
637 in_vtimezone
= false;
638 for (node
= clines
; node
; node
= node
->next
) {
639 /* node->name will be NULL if the line was deleted. */
640 if (! node
->name
) { continue; }
643 if (! strcasecmp ("END", node
->name
) &&
644 ! strcasecmp ("VTIMEZONE", node
->value
)) {
645 in_vtimezone
= false;
648 if (! strcasecmp ("BEGIN", node
->name
) &&
649 ! strcasecmp ("VTIMEZONE", node
->value
)) {
651 } else if (! strcasecmp ("DTSTART", node
->name
)) {
652 /* Got it: DTSTART outside of a VTIMEZONE section. */
653 char *datetime
= format_datetime (timezones
, node
);
654 c
->c_text
= datetime
? datetime
: mh_xstrdup(node
->value
);
660 if ((c
= fmt_findcomp ("dtend"))) {
661 if ((node
= find_contentline (clines
, "DTEND", 0)) && node
->value
) {
662 char *datetime
= format_datetime (timezones
, node
);
663 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
664 } else if ((node
= find_contentline (clines
, "DTSTART", 0)) &&
666 /* There is no DTEND. If there's a DTSTART, use it. If it
667 doesn't have a time, assume that the event is for the
668 entire day and append 23:59:59 to it so that it signifies
669 the end of the day. And assume local timezone. */
670 if (strchr(node
->value
, 'T')) {
671 char * datetime
= format_datetime (timezones
, node
);
672 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
675 contentline node_copy
;
678 node_copy
.value
= concat(node_copy
.value
, "T235959", NULL
);
679 datetime
= format_datetime (timezones
, &node_copy
);
680 c
->c_text
= datetime
? datetime
: strdup(node_copy
.value
);
681 free(node_copy
.value
);
686 if ((c
= fmt_findcomp ("attendees"))) {
687 /* Call find_contentline () with node as argument to find multiple
688 matching contentlines. */
689 charstring_append_cstring (attendees
, "Attendees: ");
690 for (node
= clines
, num_attendees
= 0;
691 (node
= find_contentline (node
, "ATTENDEE", 0)) &&
692 num_attendees
++ < max_attendees
;
694 const char *id
= identity (node
);
696 if (num_attendees
> 1) {
697 charstring_append_cstring (attendees
, ", ");
699 charstring_append_cstring (attendees
, id
);
702 if (num_attendees
>= max_attendees
) {
703 unsigned int not_shown
= 0;
706 (node
= find_contentline (node
, "ATTENDEE", 0));
714 (void) snprintf (buf
, sizeof buf
, ", and %d more", not_shown
);
715 charstring_append_cstring (attendees
, buf
);
719 if (num_attendees
> 0) {
720 c
->c_text
= charstring_buffer_copy (attendees
);
724 /* Don't call on the END:VCALENDAR line. */
725 if (clines
&& clines
->next
) {
726 (void) fmt_scan (fmt
, buffer
, INT_MAX
, dat
, NULL
);
727 fputs (charstring_buffer (buffer
), file
);
731 charstring_free (attendees
);
732 charstring_free (buffer
);
733 free_timezones (timezones
);
737 identity (const contentline
*node
)
739 /* According to RFC 5545 § 3.3.3, an email address in the value
740 must be a mailto URI. */
741 if (! strncasecmp (node
->value
, "mailto:", 7)) {
745 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
748 for (v
= p
->values
; v
; v
= v
->next
) {
749 if (! strcasecmp (p
->param_name
, "CN")) {
755 /* Did not find a CN parameter, so output the address. */
756 addr
= node
->value
+ 7;
758 /* Skip any leading whitespace. */
759 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
768 format_params (char *line
, param_list
*p
)
770 for ( ; p
&& p
->param_name
; p
= p
->next
) {
772 size_t num_values
= 0;
774 for (v
= p
->values
; v
; v
= v
->next
) {
775 if (v
->value
) { ++num_values
; }
779 size_t len
= strlen (line
);
781 line
= mh_xrealloc (line
, len
+ 2);
783 line
[len
+ 1] = '\0';
785 line
= add (p
->param_name
, line
);
787 for (v
= p
->values
; v
; v
= v
->next
) {
789 line
= mh_xrealloc (line
, len
+ 2);
790 line
[len
] = v
== p
->values
? '=' : ',';
791 line
[len
+ 1] = '\0';
793 line
= add (v
->value
, line
);
802 fold (char *line
, int uses_cr
)
804 size_t remaining
= strlen (line
);
805 size_t current_line_len
= 0;
806 charstring_t folded_line
= charstring_create (2 * remaining
);
807 const char *cp
= line
;
809 #ifdef MULTIBYTE_SUPPORT
810 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
813 while (*cp
&& remaining
> 0) {
814 #ifdef MULTIBYTE_SUPPORT
815 int char_len
= mbtowc (NULL
, cp
, (size_t) MB_CUR_MAX
< remaining
816 ? (size_t) MB_CUR_MAX
818 if (char_len
== -1) { char_len
= 1; }
820 const int char_len
= 1;
823 charstring_push_back_chars (folded_line
, cp
, char_len
, 1);
824 remaining
-= max(char_len
, 1);
826 /* remaining must be > 0 to pass the loop condition above, so
827 if it's not > 1, it is == 1. */
828 if (++current_line_len
>= 75) {
829 if (remaining
> 1 || (*(cp
+1) != '\0' && *(cp
+1) != '\r' &&
832 if (uses_cr
) { charstring_push_back (folded_line
, '\r'); }
833 charstring_push_back (folded_line
, '\n');
834 charstring_push_back (folded_line
, ' ');
835 current_line_len
= 0;
839 cp
+= max(char_len
, 1);
843 line
= charstring_buffer_copy (folded_line
);
844 charstring_free (folded_line
);