]>
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 "sbr/fmt_new.h"
10 #include "sbr/getarguments.h"
11 #include "sbr/concat.h"
12 #include "sbr/smatch.h"
13 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
18 #include "h/icalendar.h"
19 #include "sbr/datetime.h"
20 #include "sbr/icalparse.h"
21 #include "h/fmt_scan.h"
22 #include "h/addrsbr.h"
37 static void convert_to_reply (contentline
*, act
);
38 static void convert_to_cancellation (contentline
*);
39 static void convert_common (contentline
*, act
);
40 static void dump_unfolded (FILE *, contentline
*);
41 static void output (FILE *, contentline
*, int);
42 static void display (FILE *, contentline
*, char *);
43 static const char *identity (const contentline
*) PURE
;
44 static char *format_params (char *, param_list
*);
45 static char *fold (char *, int);
47 #define MHICAL_SWITCHES \
48 X("reply accept|decline|tentative", 0, REPLYSW) \
49 X("cancel", 0, CANCELSW) \
50 X("form formatfile", 0, FORMSW) \
51 X("format string", 5, FMTSW) \
52 X("infile", 0, INFILESW) \
53 X("outfile", 0, OUTFILESW) \
54 X("contenttype", 0, CONTENTTYPESW) \
55 X("nocontenttype", 0, NCONTENTTYPESW) \
56 X("unfold", 0, UNFOLDSW) \
57 X("debug", 0, DEBUGSW) \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(MHICAL
);
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(MHICAL
, switches
);
69 vevent vevents
= { NULL
, NULL
, NULL
};
70 int parser_status
= 0;
73 main (int argc
, char *argv
[])
75 /* RFC 5322 § 3.3 date-time format, including the optional
76 day-of-week and not including the optional seconds. The
77 zone is required by the RFC but not always output by this
78 format, because RFC 5545 § 3.3.5 allows date-times not
79 bound to any time zone. */
81 act action
= ACT_NONE
;
82 char *infile
= NULL
, *outfile
= NULL
;
83 FILE *inputfile
= NULL
, *outputfile
= NULL
;
84 bool contenttype
= false;
86 vevent
*v
, *nextvevent
;
87 char *form
= "mhical.24hour", *format
= NULL
;
88 char **argp
, **arguments
, *cp
;
90 icaldebug
= 0; /* Global provided by bison (with name-prefix "ical"). */
92 if (nmh_init(argv
[0], true, false)) { return 1; }
94 arguments
= getarguments (invo_name
, argc
, argv
, 1);
100 while ((cp
= *argp
++)) {
102 switch (smatch (++cp
, switches
)) {
104 ambigsw (cp
, switches
);
107 die("-%s unknown", cp
);
111 snprintf (buf
, sizeof buf
, "%s [switches]", invo_name
);
112 print_help (buf
, switches
, 1);
116 print_version(invo_name
);
123 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
124 die("missing argument to %s", argp
[-2]);
125 if (! strcasecmp (cp
, "accept")) {
127 } else if (! strcasecmp (cp
, "decline")) {
128 action
= ACE_DECLINE
;
129 } else if (! strcasecmp (cp
, "tentative")) {
130 action
= ACT_TENTATIVE
;
131 } else if (! strcasecmp (cp
, "delegate")) {
132 action
= ACT_DELEGATE
;
134 die("Unknown action: %s", cp
);
143 if (! (form
= *argp
++) || *form
== '-')
144 die("missing argument to %s", argp
[-2]);
148 if (! (format
= *argp
++) || *format
== '-')
149 die("missing argument to %s", argp
[-2]);
154 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
155 die("missing argument to %s", argp
[-2]);
156 infile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
159 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
160 die("missing argument to %s", argp
[-2]);
161 outfile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
181 if ((inputfile
= fopen (infile
, "r"))) {
182 icalset_inputfile (inputfile
);
184 adios (infile
, "error opening");
191 if ((outputfile
= fopen (outfile
, "w"))) {
192 icalset_outputfile (outputfile
);
194 adios (outfile
, "error opening");
200 vevents
.last
= &vevents
;
201 /* vevents is accessed by parser as global. */
204 for (v
= &vevents
; v
; v
= nextvevent
) {
205 if (! unfold
&& v
!= &vevents
&& v
->contentlines
&&
206 v
->contentlines
->name
&&
207 strcasecmp (v
->contentlines
->name
, "END") &&
208 v
->contentlines
->value
&&
209 strcasecmp (v
->contentlines
->value
, "VCALENDAR")) {
210 /* Output blank line between vevents. Not before
211 first vevent and not after last. */
212 putc ('\n', outputfile
);
215 if (action
== ACT_NONE
) {
217 dump_unfolded (outputfile
, v
->contentlines
);
219 char *nfs
= new_fs (form
, format
, NULL
);
221 display (outputfile
, v
->contentlines
, nfs
);
225 if (action
== ACT_CANCEL
) {
226 convert_to_cancellation (v
->contentlines
);
228 convert_to_reply (v
->contentlines
, action
);
230 output (outputfile
, v
->contentlines
, contenttype
);
233 free_contentlines (v
->contentlines
);
234 nextvevent
= v
->next
;
241 if (fclose (inputfile
) != 0) {
242 advise (infile
, "error closing");
247 if (fclose (outputfile
) != 0) {
248 advise (outfile
, "error closing");
253 return parser_status
;
257 * - Change METHOD from REQUEST to REPLY.
259 * - Remove all ATTENDEE lines for other users (based on ismymbox ()).
260 * - For the user's ATTENDEE line:
261 * - Remove ROLE and RSVP parameters.
262 * - Change PARTSTAT value to indicate reply action, e.g., ACCEPTED,
263 * DECLINED, or TENTATIVE.
264 * - Insert action at beginning of SUMMARY value.
265 * - Remove all X- lines.
266 * - Update DTSTAMP with current timestamp.
267 * - Remove all DESCRIPTION lines.
268 * - Excise VALARM sections.
271 convert_to_reply (contentline
*clines
, act action
)
273 char *partstat
= NULL
;
274 bool found_my_attendee_line
= false;
277 convert_common (clines
, action
);
281 partstat
= "ACCEPTED";
284 partstat
= "DECLINED";
287 partstat
= "TENTATIVE";
293 /* Call find_contentline () with node as argument to find multiple
294 matching contentlines. */
296 (node
= find_contentline (node
, "ATTENDEE", 0));
300 ismymbox (NULL
); /* need to prime ismymbox() */
302 /* According to RFC 5545 § 3.3.3, an email address in the
303 value must be a mailto URI. */
304 if (! strncasecmp (node
->value
, "mailto:", 7)) {
305 char *addr
= node
->value
+ 7;
308 /* Skip any leading whitespace. */
309 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
311 addr
= getname (addr
);
312 mn
= getm (addr
, NULL
, 0, NULL
, 0);
314 /* Need to flush getname after use. */
315 while (getname ("")) { continue; }
318 found_my_attendee_line
= true;
319 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
322 for (v
= p
->values
; v
; v
= v
->next
) {
323 if (! strcasecmp (p
->param_name
, "ROLE") ||
324 ! strcasecmp (p
->param_name
, "RSVP")) {
326 } else if (! strcasecmp (p
->param_name
, "PARTSTAT")) {
328 v
->value
= strdup (partstat
);
333 remove_contentline (node
);
340 if (! found_my_attendee_line
) {
341 /* Generate and attach an ATTENDEE line for me. */
344 /* Add it after the ORGANIZER line, or if none, BEGIN:VEVENT line. */
345 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) ||
346 (node
= find_contentline (clines
, "BEGIN", "VEVENT"))) {
347 contentline
*new_node
= add_contentline (node
, "ATTENDEE");
349 add_param_name (new_node
, mh_xstrdup ("PARTSTAT"));
350 add_param_value (new_node
, mh_xstrdup (partstat
));
351 add_param_name (new_node
, mh_xstrdup ("CN"));
352 add_param_value (new_node
, mh_xstrdup (getfullname ()));
353 new_node
->value
= concat ("MAILTO:", getlocalmbox (), NULL
);
357 /* Call find_contentline () with node as argument to find multiple
358 matching contentlines. */
360 (node
= find_contentline (node
, "DESCRIPTION", 0));
362 /* ACCEPT, at least, replies don't seem to have DESCRIPTIONS. */
363 remove_contentline (node
);
368 * - Change METHOD from REQUEST to CANCEL.
370 * - Insert action at beginning of SUMMARY value.
371 * - Remove all X- lines.
372 * - Update DTSTAMP with current timestamp.
373 * - Change STATUS from CONFIRMED to CANCELLED.
374 * - Increment value of SEQUENCE.
375 * - Excise VALARM sections.
378 convert_to_cancellation (contentline
*clines
)
382 convert_common (clines
, ACT_CANCEL
);
384 if ((node
= find_contentline (clines
, "STATUS", 0)) &&
385 ! strcasecmp (node
->value
, "CONFIRMED")) {
387 node
->value
= mh_xstrdup ("CANCELLED");
390 if ((node
= find_contentline (clines
, "SEQUENCE", 0))) {
391 int sequence
= atoi (node
->value
);
394 (void) snprintf (buf
, sizeof buf
, "%d", sequence
+ 1);
396 node
->value
= mh_xstrdup (buf
);
401 convert_common (contentline
*clines
, act action
)
406 if ((node
= find_contentline (clines
, "METHOD", 0))) {
408 node
->value
= mh_xstrdup (action
== ACT_CANCEL
? "CANCEL" : "REPLY");
411 if ((node
= find_contentline (clines
, "PRODID", 0))) {
413 node
->value
= mh_xstrdup ("nmh mhical v0.1");
416 if ((node
= find_contentline (clines
, "VERSION", 0))) {
418 inform("Version property is missing value, assume 2.0, continuing...");
419 node
->value
= mh_xstrdup ("2.0");
422 if (strcmp (node
->value
, "2.0")) {
423 inform("supports the Version 2.0 specified by RFC 5545 "
424 "but iCalendar object has Version %s, continuing...",
426 node
->value
= mh_xstrdup ("2.0");
430 if ((node
= find_contentline (clines
, "SUMMARY", 0))) {
435 insert
= "Accepted: ";
438 insert
= "Declined: ";
441 insert
= "Tentative: ";
444 die("Delegate replies are not supported");
447 insert
= "Cancelled:";
454 const size_t len
= strlen (insert
) + strlen (node
->value
) + 1;
455 char *tmp
= mh_xmalloc (len
);
457 (void) strncpy (tmp
, insert
, len
);
458 (void) strncat (tmp
, node
->value
, len
- strlen (insert
) - 1);
462 /* Should never get here. */
463 die("Unknown action: %d", action
);
467 if ((node
= find_contentline (clines
, "DTSTAMP", 0))) {
468 const time_t now
= time (NULL
);
471 if (gmtime_r (&now
, &now_tm
)) {
472 /* 17 would be sufficient given that RFC 5545 § 3.3.4
473 supports only a 4 digit year. */
476 if (strftime (buf
, sizeof buf
, "%Y%m%dT%H%M%SZ", &now_tm
)) {
478 node
->value
= mh_xstrdup (buf
);
480 inform("strftime unable to format current time, continuing...");
483 inform("gmtime_r failed on current time, continuing...");
487 /* Excise X- lines and VALARM section(s). */
489 for (node
= clines
; node
; node
= node
->next
) {
490 /* node->name will be NULL if the line was deleted. */
491 if (! node
->name
) { continue; }
494 if (! strcasecmp ("END", node
->name
) &&
495 ! strcasecmp ("VALARM", node
->value
)) {
498 remove_contentline (node
);
500 if (! strcasecmp ("BEGIN", node
->name
) &&
501 ! strcasecmp ("VALARM", node
->value
)) {
503 remove_contentline (node
);
504 } else if (! strncasecmp ("X-", node
->name
, 2)) {
505 remove_contentline (node
);
511 /* Echo the input, but with unfolded lines. */
513 dump_unfolded (FILE *file
, contentline
*clines
)
517 for (node
= clines
; node
; node
= node
->next
) {
518 fputs (node
->input_line
, file
);
523 output (FILE *file
, contentline
*clines
, int contenttype
)
528 /* Generate a Content-Type header to pass the method parameter
529 to mhbuild. Per RFC 5545 Secs. 6 and 8.1, it must be
530 UTF-8. But we don't attempt to do any conversion of the
532 if ((node
= find_contentline (clines
, "METHOD", 0))) {
534 "Content-Type: text/calendar; method=\"%s\"; "
535 "charset=\"UTF-8\"\n\n",
540 for (node
= clines
; node
; node
= node
->next
) {
545 line
= mh_xstrdup (node
->name
);
546 line
= format_params (line
, node
->params
);
549 line
= mh_xrealloc (line
, len
+ 2);
551 line
[len
+ 1] = '\0';
553 line
= fold (add (node
->value
, line
),
554 clines
->cr_before_lf
== CR_BEFORE_LF
);
557 if (clines
->cr_before_lf
!= LF_ONLY
)
566 * Display these fields of the iCalendar event:
570 * - description, except for "\n\n" and in VALARM
572 * - dtstart in local timezone
573 * - dtend in local timezone
574 * - attendees (limited to number specified in initialization)
577 display (FILE *file
, contentline
*clines
, char *nfs
)
579 tzdesc_t timezones
= load_timezones (clines
);
584 int dat
[5] = { 0, 0, 0, INT_MAX
, 0 };
586 charstring_t buffer
= charstring_create (BUFSIZ
);
587 charstring_t attendees
= charstring_create (BUFSIZ
);
588 const unsigned int max_attendees
= 20;
589 unsigned int num_attendees
;
591 /* Don't call on the END:VCALENDAR line. */
592 if (clines
&& clines
->next
) {
593 (void) fmt_compile (nfs
, &fmt
, 1);
596 if ((c
= fmt_findcomp ("method"))) {
597 if ((node
= find_contentline (clines
, "METHOD", 0)) && node
->value
) {
598 c
->c_text
= mh_xstrdup (node
->value
);
602 if ((c
= fmt_findcomp ("organizer"))) {
603 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) &&
605 c
->c_text
= mh_xstrdup (identity (node
));
609 if ((c
= fmt_findcomp ("summary"))) {
610 if ((node
= find_contentline (clines
, "SUMMARY", 0)) && node
->value
) {
611 c
->c_text
= mh_xstrdup (node
->value
);
615 /* Only display DESCRIPTION lines that are outside VALARM section(s). */
617 if ((c
= fmt_findcomp ("description"))) {
618 for (node
= clines
; node
; node
= node
->next
) {
619 /* node->name will be NULL if the line was deleted. */
620 if (node
->name
&& node
->value
&& ! in_valarm
&&
621 ! strcasecmp ("DESCRIPTION", node
->name
) &&
622 strcasecmp (node
->value
, "\\n\\n")) {
623 c
->c_text
= mh_xstrdup (node
->value
);
624 } else if (in_valarm
) {
625 if (! strcasecmp ("END", node
->name
) &&
626 ! strcasecmp ("VALARM", node
->value
)) {
630 if (! strcasecmp ("BEGIN", node
->name
) &&
631 ! strcasecmp ("VALARM", node
->value
)) {
638 if ((c
= fmt_findcomp ("location"))) {
639 if ((node
= find_contentline (clines
, "LOCATION", 0)) &&
641 c
->c_text
= mh_xstrdup (node
->value
);
645 if ((c
= fmt_findcomp ("dtstart"))) {
646 /* Find DTSTART outsize of a VTIMEZONE section. */
647 in_vtimezone
= false;
648 for (node
= clines
; node
; node
= node
->next
) {
649 /* node->name will be NULL if the line was deleted. */
650 if (! node
->name
) { continue; }
653 if (! strcasecmp ("END", node
->name
) &&
654 ! strcasecmp ("VTIMEZONE", node
->value
)) {
655 in_vtimezone
= false;
658 if (! strcasecmp ("BEGIN", node
->name
) &&
659 ! strcasecmp ("VTIMEZONE", node
->value
)) {
661 } else if (! strcasecmp ("DTSTART", node
->name
)) {
662 /* Got it: DTSTART outside of a VTIMEZONE section. */
663 char *datetime
= format_datetime (timezones
, node
);
664 c
->c_text
= datetime
? datetime
: mh_xstrdup(node
->value
);
670 if ((c
= fmt_findcomp ("dtend"))) {
671 if ((node
= find_contentline (clines
, "DTEND", 0)) && node
->value
) {
672 char *datetime
= format_datetime (timezones
, node
);
673 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
674 } else if ((node
= find_contentline (clines
, "DTSTART", 0)) &&
676 /* There is no DTEND. If there's a DTSTART, use it. If it
677 doesn't have a time, assume that the event is for the
678 entire day and append 23:59:59 to it so that it signifies
679 the end of the day. And assume local timezone. */
680 if (strchr(node
->value
, 'T')) {
681 char * datetime
= format_datetime (timezones
, node
);
682 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
685 contentline node_copy
;
688 node_copy
.value
= concat(node_copy
.value
, "T235959", NULL
);
689 datetime
= format_datetime (timezones
, &node_copy
);
690 c
->c_text
= datetime
? datetime
: strdup(node_copy
.value
);
691 free(node_copy
.value
);
696 if ((c
= fmt_findcomp ("attendees"))) {
697 /* Call find_contentline () with node as argument to find multiple
698 matching contentlines. */
699 charstring_append_cstring (attendees
, "Attendees: ");
700 for (node
= clines
, num_attendees
= 0;
701 (node
= find_contentline (node
, "ATTENDEE", 0)) &&
702 num_attendees
++ < max_attendees
;
704 const char *id
= identity (node
);
706 if (num_attendees
> 1) {
707 charstring_append_cstring (attendees
, ", ");
709 charstring_append_cstring (attendees
, id
);
712 if (num_attendees
>= max_attendees
) {
713 unsigned int not_shown
= 0;
716 (node
= find_contentline (node
, "ATTENDEE", 0));
724 (void) snprintf (buf
, sizeof buf
, ", and %d more", not_shown
);
725 charstring_append_cstring (attendees
, buf
);
729 if (num_attendees
> 0) {
730 c
->c_text
= charstring_buffer_copy (attendees
);
734 /* Don't call on the END:VCALENDAR line. */
735 if (clines
&& clines
->next
) {
736 (void) fmt_scan (fmt
, buffer
, INT_MAX
, dat
, NULL
);
737 fputs (charstring_buffer (buffer
), file
);
741 charstring_free (attendees
);
742 charstring_free (buffer
);
743 free_timezones (timezones
);
747 identity (const contentline
*node
)
749 /* According to RFC 5545 § 3.3.3, an email address in the value
750 must be a mailto URI. */
751 if (! strncasecmp (node
->value
, "mailto:", 7)) {
755 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
758 for (v
= p
->values
; v
; v
= v
->next
) {
759 if (! strcasecmp (p
->param_name
, "CN")) {
765 /* Did not find a CN parameter, so output the address. */
766 addr
= node
->value
+ 7;
768 /* Skip any leading whitespace. */
769 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
778 format_params (char *line
, param_list
*p
)
780 for ( ; p
&& p
->param_name
; p
= p
->next
) {
782 size_t num_values
= 0;
784 for (v
= p
->values
; v
; v
= v
->next
) {
785 if (v
->value
) { ++num_values
; }
789 size_t len
= strlen (line
);
791 line
= mh_xrealloc (line
, len
+ 2);
793 line
[len
+ 1] = '\0';
795 line
= add (p
->param_name
, line
);
797 for (v
= p
->values
; v
; v
= v
->next
) {
799 line
= mh_xrealloc (line
, len
+ 2);
800 line
[len
] = v
== p
->values
? '=' : ',';
801 line
[len
+ 1] = '\0';
803 line
= add (v
->value
, line
);
812 fold (char *line
, int uses_cr
)
814 size_t remaining
= strlen (line
);
815 size_t current_line_len
= 0;
816 charstring_t folded_line
= charstring_create (2 * remaining
);
817 const char *cp
= line
;
819 #ifdef MULTIBYTE_SUPPORT
820 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
823 while (*cp
&& remaining
> 0) {
824 #ifdef MULTIBYTE_SUPPORT
825 int char_len
= mbtowc (NULL
, cp
, (size_t) MB_CUR_MAX
< remaining
826 ? (size_t) MB_CUR_MAX
828 if (char_len
== -1) { char_len
= 1; }
830 const int char_len
= 1;
833 charstring_push_back_chars (folded_line
, cp
, char_len
, 1);
834 remaining
-= max(char_len
, 1);
836 /* remaining must be > 0 to pass the loop condition above, so
837 if it's not > 1, it is == 1. */
838 if (++current_line_len
>= 75) {
839 if (remaining
> 1 || (*(cp
+1) != '\0' && *(cp
+1) != '\r' &&
842 if (uses_cr
) { charstring_push_back (folded_line
, '\r'); }
843 charstring_push_back (folded_line
, '\n');
844 charstring_push_back (folded_line
, ' ');
845 current_line_len
= 0;
849 cp
+= max(char_len
, 1);
853 line
= charstring_buffer_copy (folded_line
);
854 charstring_free (folded_line
);