]>
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
[]) {
64 /* RFC 5322 § 3.3 date-time format, including the optional
65 day-of-week and not including the optional seconds. The
66 zone is required by the RFC but not always output by this
67 format, because RFC 5545 § 3.3.5 allows date-times not
68 bound to any time zone. */
70 act action
= ACT_NONE
;
71 char *infile
= NULL
, *outfile
= NULL
;
72 FILE *inputfile
= NULL
, *outputfile
= NULL
;
73 int contenttype
= 0, unfold
= 0;
74 vevent
*v
, *nextvevent
;
75 char *form
= "mhical.24hour", *format
= NULL
;
76 char **argp
, **arguments
, *cp
;
78 icaldebug
= 0; /* Global provided by bison (with name-prefix "ical"). */
80 if (nmh_init(argv
[0], 2)) { return 1; }
82 arguments
= getarguments (invo_name
, argc
, argv
, 1);
88 while ((cp
= *argp
++)) {
90 switch (smatch (++cp
, switches
)) {
92 ambigsw (cp
, switches
);
95 adios (NULL
, "-%s unknown", cp
);
99 snprintf (buf
, sizeof buf
, "%s [switches]", invo_name
);
100 print_help (buf
, switches
, 1);
104 print_version(invo_name
);
111 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
112 adios (NULL
, "missing argument to %s", argp
[-2]);
113 if (! strcasecmp (cp
, "accept")) {
115 } else if (! strcasecmp (cp
, "decline")) {
116 action
= ACE_DECLINE
;
117 } else if (! strcasecmp (cp
, "tentative")) {
118 action
= ACT_TENTATIVE
;
119 } else if (! strcasecmp (cp
, "delegate")) {
120 action
= ACT_DELEGATE
;
122 adios (NULL
, "Unknown action: %s", cp
);
131 if (! (form
= *argp
++) || *form
== '-')
132 adios (NULL
, "missing argument to %s", argp
[-2]);
136 if (! (format
= *argp
++) || *format
== '-')
137 adios (NULL
, "missing argument to %s", argp
[-2]);
142 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
143 adios (NULL
, "missing argument to %s", argp
[-2]);
144 infile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
147 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
148 adios (NULL
, "missing argument to %s", argp
[-2]);
149 outfile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
169 if ((inputfile
= fopen (infile
, "r"))) {
170 icalset_inputfile (inputfile
);
172 adios (infile
, "error opening");
179 if ((outputfile
= fopen (outfile
, "w"))) {
180 icalset_outputfile (outputfile
);
182 adios (outfile
, "error opening");
188 vevents
.last
= &vevents
;
189 /* vevents is accessed by parser as global. */
192 for (v
= &vevents
; v
; v
= nextvevent
) {
193 if (! unfold
&& v
!= &vevents
&& v
->contentlines
&&
194 v
->contentlines
->name
&&
195 strcasecmp (v
->contentlines
->name
, "END") &&
196 v
->contentlines
->value
&&
197 strcasecmp (v
->contentlines
->value
, "VCALENDAR")) {
198 /* Output blank line between vevents. Not before
199 first vevent and not after last. */
200 putc ('\n', outputfile
);
203 if (action
== ACT_NONE
) {
205 dump_unfolded (outputfile
, v
->contentlines
);
207 char *nfs
= new_fs (form
, format
, NULL
);
209 display (outputfile
, v
->contentlines
, nfs
);
213 if (action
== ACT_CANCEL
) {
214 convert_to_cancellation (v
->contentlines
);
216 convert_to_reply (v
->contentlines
, action
);
218 output (outputfile
, v
->contentlines
, contenttype
);
221 free_contentlines (v
->contentlines
);
222 nextvevent
= v
->next
;
229 if (fclose (inputfile
) != 0) {
230 advise (infile
, "error closing");
235 if (fclose (outputfile
) != 0) {
236 advise (outfile
, "error closing");
241 return parser_status
;
245 * - Change METHOD from REQUEST to REPLY.
247 * - Remove all ATTENDEE lines for other users (based on ismymbox ()).
248 * - For the user's ATTENDEE line:
249 * - Remove ROLE and RSVP parameters.
250 * - Change PARTSTAT value to indicate reply action, e.g., ACCEPTED,
251 * DECLINED, or TENTATIVE.
252 * - Insert action at beginning of SUMMARY value.
253 * - Remove all X- lines.
254 * - Update DTSTAMP with current timestamp.
255 * - Remove all DESCRIPTION lines.
256 * - Excise VALARM sections.
259 convert_to_reply (contentline
*clines
, act action
) {
260 char *partstat
= NULL
;
261 int found_my_attendee_line
= 0;
264 convert_common (clines
, action
);
268 partstat
= "ACCEPTED";
271 partstat
= "DECLINED";
274 partstat
= "TENTATIVE";
280 /* Call find_contentline () with node as argument to find multiple
281 matching contentlines. */
283 (node
= find_contentline (node
, "ATTENDEE", 0));
287 ismymbox (NULL
); /* need to prime ismymbox() */
289 /* According to RFC 5545 § 3.3.3, an email address in the
290 value must be a mailto URI. */
291 if (! strncasecmp (node
->value
, "mailto:", 7)) {
292 char *addr
= node
->value
+ 7;
295 /* Skip any leading whitespace. */
296 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
298 addr
= getname (addr
);
299 mn
= getm (addr
, NULL
, 0, NULL
, 0);
301 /* Need to flush getname after use. */
302 while (getname ("")) { continue; }
305 found_my_attendee_line
= 1;
306 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
309 for (v
= p
->values
; v
; v
= v
->next
) {
310 if (! strcasecmp (p
->param_name
, "ROLE") ||
311 ! strcasecmp (p
->param_name
, "RSVP")) {
313 } else if (! strcasecmp (p
->param_name
, "PARTSTAT")) {
315 v
->value
= strdup (partstat
);
320 remove_contentline (node
);
327 if (! found_my_attendee_line
) {
328 /* Generate and attach an ATTENDEE line for me. */
331 /* Add it after the ORGANIZER line, or if none, BEGIN:VEVENT line. */
332 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) ||
333 (node
= find_contentline (clines
, "BEGIN", "VEVENT"))) {
334 contentline
*new_node
= add_contentline (node
, "ATTENDEE");
336 add_param_name (new_node
, mh_xstrdup ("PARTSTAT"));
337 add_param_value (new_node
, mh_xstrdup (partstat
));
338 add_param_name (new_node
, mh_xstrdup ("CN"));
339 add_param_value (new_node
, mh_xstrdup (getfullname ()));
340 new_node
->value
= concat ("MAILTO:", getlocalmbox (), NULL
);
344 /* Call find_contentline () with node as argument to find multiple
345 matching contentlines. */
347 (node
= find_contentline (node
, "DESCRIPTION", 0));
349 /* ACCEPT, at least, replies don't seem to have DESCRIPTIONS. */
350 remove_contentline (node
);
355 * - Change METHOD from REQUEST to CANCEL.
357 * - Insert action at beginning of SUMMARY value.
358 * - Remove all X- lines.
359 * - Update DTSTAMP with current timestamp.
360 * - Change STATUS from CONFIRMED to CANCELLED.
361 * - Increment value of SEQUENCE.
362 * - Excise VALARM sections.
365 convert_to_cancellation (contentline
*clines
) {
368 convert_common (clines
, ACT_CANCEL
);
370 if ((node
= find_contentline (clines
, "STATUS", 0)) &&
371 ! strcasecmp (node
->value
, "CONFIRMED")) {
373 node
->value
= mh_xstrdup ("CANCELLED");
376 if ((node
= find_contentline (clines
, "SEQUENCE", 0))) {
377 int sequence
= atoi (node
->value
);
380 (void) snprintf (buf
, sizeof buf
, "%d", sequence
+ 1);
382 node
->value
= mh_xstrdup (buf
);
387 convert_common (contentline
*clines
, act action
) {
391 if ((node
= find_contentline (clines
, "METHOD", 0))) {
393 node
->value
= mh_xstrdup (action
== ACT_CANCEL
? "CANCEL" : "REPLY");
396 if ((node
= find_contentline (clines
, "PRODID", 0))) {
398 node
->value
= mh_xstrdup ("nmh mhical v0.1");
401 if ((node
= find_contentline (clines
, "VERSION", 0))) {
403 inform("Version property is missing value, assume 2.0, continuing...");
404 node
->value
= mh_xstrdup ("2.0");
407 if (strcmp (node
->value
, "2.0")) {
408 inform("supports the Version 2.0 specified by RFC 5545 "
409 "but iCalendar object has Version %s, continuing...",
411 node
->value
= mh_xstrdup ("2.0");
415 if ((node
= find_contentline (clines
, "SUMMARY", 0))) {
420 insert
= "Accepted: ";
423 insert
= "Declined: ";
426 insert
= "Tentative: ";
429 adios (NULL
, "Delegate replies are not supported");
432 insert
= "Cancelled:";
439 const size_t len
= strlen (insert
) + strlen (node
->value
) + 1;
440 char *tmp
= mh_xmalloc (len
);
442 (void) strncpy (tmp
, insert
, len
);
443 (void) strncat (tmp
, node
->value
, len
- strlen (insert
) - 1);
447 /* Should never get here. */
448 adios (NULL
, "Unknown action: %d", action
);
452 if ((node
= find_contentline (clines
, "DTSTAMP", 0))) {
453 const time_t now
= time (NULL
);
456 if (gmtime_r (&now
, &now_tm
)) {
457 /* 17 would be sufficient given that RFC 5545 § 3.3.4
458 supports only a 4 digit year. */
461 if (strftime (buf
, sizeof buf
, "%Y%m%dT%H%M%SZ", &now_tm
)) {
463 node
->value
= mh_xstrdup (buf
);
465 inform("strftime unable to format current time, continuing...");
468 inform("gmtime_r failed on current time, continuing...");
472 /* Excise X- lines and VALARM section(s). */
474 for (node
= clines
; node
; node
= node
->next
) {
475 /* node->name will be NULL if the line was deleted. */
476 if (! node
->name
) { continue; }
479 if (! strcasecmp ("END", node
->name
) &&
480 ! strcasecmp ("VALARM", node
->value
)) {
483 remove_contentline (node
);
485 if (! strcasecmp ("BEGIN", node
->name
) &&
486 ! strcasecmp ("VALARM", node
->value
)) {
488 remove_contentline (node
);
489 } else if (! strncasecmp ("X-", node
->name
, 2)) {
490 remove_contentline (node
);
496 /* Echo the input, but with unfolded lines. */
498 dump_unfolded (FILE *file
, contentline
*clines
) {
501 for (node
= clines
; node
; node
= node
->next
) {
502 fputs (node
->input_line
, file
);
507 output (FILE *file
, contentline
*clines
, int contenttype
) {
511 /* Generate a Content-Type header to pass the method parameter
512 to mhbuild. Per RFC 5545 Secs. 6 and 8.1, it must be
513 UTF-8. But we don't attempt to do any conversion of the
515 if ((node
= find_contentline (clines
, "METHOD", 0))) {
517 "Content-Type: text/calendar; method=\"%s\"; "
518 "charset=\"UTF-8\"\n\n",
523 for (node
= clines
; node
; node
= node
->next
) {
528 line
= mh_xstrdup (node
->name
);
529 line
= format_params (line
, node
->params
);
532 line
= mh_xrealloc (line
, len
+ 2);
534 line
[len
+ 1] = '\0';
536 line
= fold (add (node
->value
, line
),
537 clines
->cr_before_lf
== CR_BEFORE_LF
);
540 if (clines
->cr_before_lf
!= LF_ONLY
)
549 * Display these fields of the iCalendar event:
553 * - description, except for "\n\n" and in VALARM
555 * - dtstart in local timezone
556 * - dtend in local timezone
557 * - attendees (limited to number specified in initialization)
560 display (FILE *file
, contentline
*clines
, char *nfs
) {
561 tzdesc_t timezones
= load_timezones (clines
);
566 int dat
[5] = { 0, 0, 0, INT_MAX
, 0 };
568 charstring_t buffer
= charstring_create (BUFSIZ
);
569 charstring_t attendees
= charstring_create (BUFSIZ
);
570 const unsigned int max_attendees
= 20;
571 unsigned int num_attendees
;
573 /* Don't call on the END:VCALENDAR line. */
574 if (clines
&& clines
->next
) {
575 (void) fmt_compile (nfs
, &fmt
, 1);
578 if ((c
= fmt_findcomp ("method"))) {
579 if ((node
= find_contentline (clines
, "METHOD", 0)) && node
->value
) {
580 c
->c_text
= mh_xstrdup (node
->value
);
584 if ((c
= fmt_findcomp ("organizer"))) {
585 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) &&
587 c
->c_text
= mh_xstrdup (identity (node
));
591 if ((c
= fmt_findcomp ("summary"))) {
592 if ((node
= find_contentline (clines
, "SUMMARY", 0)) && node
->value
) {
593 c
->c_text
= mh_xstrdup (node
->value
);
597 /* Only display DESCRIPTION lines that are outside VALARM section(s). */
599 if ((c
= fmt_findcomp ("description"))) {
600 for (node
= clines
; node
; node
= node
->next
) {
601 /* node->name will be NULL if the line was deleted. */
602 if (node
->name
&& node
->value
&& ! in_valarm
&&
603 ! strcasecmp ("DESCRIPTION", node
->name
) &&
604 strcasecmp (node
->value
, "\\n\\n")) {
605 c
->c_text
= mh_xstrdup (node
->value
);
606 } else if (in_valarm
) {
607 if (! strcasecmp ("END", node
->name
) &&
608 ! strcasecmp ("VALARM", node
->value
)) {
612 if (! strcasecmp ("BEGIN", node
->name
) &&
613 ! strcasecmp ("VALARM", node
->value
)) {
620 if ((c
= fmt_findcomp ("location"))) {
621 if ((node
= find_contentline (clines
, "LOCATION", 0)) &&
623 c
->c_text
= mh_xstrdup (node
->value
);
627 if ((c
= fmt_findcomp ("dtstart"))) {
628 /* Find DTSTART outsize of a VTIMEZONE section. */
630 for (node
= clines
; node
; node
= node
->next
) {
631 /* node->name will be NULL if the line was deleted. */
632 if (! node
->name
) { continue; }
635 if (! strcasecmp ("END", node
->name
) &&
636 ! strcasecmp ("VTIMEZONE", node
->value
)) {
640 if (! strcasecmp ("BEGIN", node
->name
) &&
641 ! strcasecmp ("VTIMEZONE", node
->value
)) {
643 } else if (! strcasecmp ("DTSTART", node
->name
)) {
644 /* Got it: DTSTART outside of a VTIMEZONE section. */
645 char *datetime
= format_datetime (timezones
, node
);
646 c
->c_text
= datetime
? datetime
: mh_xstrdup(node
->value
);
652 if ((c
= fmt_findcomp ("dtend"))) {
653 if ((node
= find_contentline (clines
, "DTEND", 0)) && node
->value
) {
654 char *datetime
= format_datetime (timezones
, node
);
655 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
656 } else if ((node
= find_contentline (clines
, "DTSTART", 0)) &&
658 /* There is no DTEND. If there's a DTSTART, use it. If it
659 doesn't have a time, assume that the event is for the
660 entire day and append 23:59:59 to it so that it signifies
661 the end of the day. And assume local timezone. */
662 if (strchr(node
->value
, 'T')) {
663 char * datetime
= format_datetime (timezones
, node
);
664 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
667 contentline node_copy
;
670 node_copy
.value
= concat(node_copy
.value
, "T235959", NULL
);
671 datetime
= format_datetime (timezones
, &node_copy
);
672 c
->c_text
= datetime
? datetime
: strdup(node_copy
.value
);
673 free(node_copy
.value
);
678 if ((c
= fmt_findcomp ("attendees"))) {
679 /* Call find_contentline () with node as argument to find multiple
680 matching contentlines. */
681 charstring_append_cstring (attendees
, "Attendees: ");
682 for (node
= clines
, num_attendees
= 0;
683 (node
= find_contentline (node
, "ATTENDEE", 0)) &&
684 num_attendees
++ < max_attendees
;
686 const char *id
= identity (node
);
688 if (num_attendees
> 1) {
689 charstring_append_cstring (attendees
, ", ");
691 charstring_append_cstring (attendees
, id
);
694 if (num_attendees
>= max_attendees
) {
695 unsigned int not_shown
= 0;
698 (node
= find_contentline (node
, "ATTENDEE", 0));
706 (void) snprintf (buf
, sizeof buf
, ", and %d more", not_shown
);
707 charstring_append_cstring (attendees
, buf
);
711 if (num_attendees
> 0) {
712 c
->c_text
= charstring_buffer_copy (attendees
);
716 /* Don't call on the END:VCALENDAR line. */
717 if (clines
&& clines
->next
) {
718 (void) fmt_scan (fmt
, buffer
, INT_MAX
, dat
, NULL
);
719 fputs (charstring_buffer (buffer
), file
);
723 charstring_free (attendees
);
724 charstring_free (buffer
);
725 free_timezones (timezones
);
729 identity (const contentline
*node
) {
730 /* According to RFC 5545 § 3.3.3, an email address in the value
731 must be a mailto URI. */
732 if (! strncasecmp (node
->value
, "mailto:", 7)) {
736 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
739 for (v
= p
->values
; v
; v
= v
->next
) {
740 if (! strcasecmp (p
->param_name
, "CN")) {
746 /* Did not find a CN parameter, so output the address. */
747 addr
= node
->value
+ 7;
749 /* Skip any leading whitespace. */
750 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
759 format_params (char *line
, param_list
*p
) {
760 for ( ; p
&& p
->param_name
; p
= p
->next
) {
762 size_t num_values
= 0;
764 for (v
= p
->values
; v
; v
= v
->next
) {
765 if (v
->value
) { ++num_values
; }
769 size_t len
= strlen (line
);
771 line
= mh_xrealloc (line
, len
+ 2);
773 line
[len
+ 1] = '\0';
775 line
= add (p
->param_name
, line
);
777 for (v
= p
->values
; v
; v
= v
->next
) {
779 line
= mh_xrealloc (line
, len
+ 2);
780 line
[len
] = v
== p
->values
? '=' : ',';
781 line
[len
+ 1] = '\0';
783 line
= add (v
->value
, line
);
792 fold (char *line
, int uses_cr
) {
793 size_t remaining
= strlen (line
);
794 size_t current_line_len
= 0;
795 charstring_t folded_line
= charstring_create (2 * remaining
);
796 const char *cp
= line
;
798 #ifdef MULTIBYTE_SUPPORT
799 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
802 while (*cp
&& remaining
> 0) {
803 #ifdef MULTIBYTE_SUPPORT
804 int char_len
= mbtowc (NULL
, cp
, (size_t) MB_CUR_MAX
< remaining
805 ? (size_t) MB_CUR_MAX
807 if (char_len
== -1) { char_len
= 1; }
809 const int char_len
= 1;
812 charstring_push_back_chars (folded_line
, cp
, char_len
, 1);
813 remaining
-= max(char_len
, 1);
815 /* remaining must be > 0 to pass the loop condition above, so
816 if it's not > 1, it is == 1. */
817 if (++current_line_len
>= 75) {
818 if (remaining
> 1 || (*(cp
+1) != '\0' && *(cp
+1) != '\r' &&
821 if (uses_cr
) { charstring_push_back (folded_line
, '\r'); }
822 charstring_push_back (folded_line
, '\n');
823 charstring_push_back (folded_line
, ' ');
824 current_line_len
= 0;
828 cp
+= max(char_len
, 1);
832 line
= charstring_buffer_copy (folded_line
);
833 charstring_free (folded_line
);