]>
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/concat.h"
10 #include "sbr/smatch.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
16 #include "h/icalendar.h"
17 #include "sbr/icalparse.h"
18 #include "h/fmt_scan.h"
19 #include "h/addrsbr.h"
34 static void convert_to_reply (contentline
*, act
);
35 static void convert_to_cancellation (contentline
*);
36 static void convert_common (contentline
*, act
);
37 static void dump_unfolded (FILE *, contentline
*);
38 static void output (FILE *, contentline
*, int);
39 static void display (FILE *, contentline
*, char *);
40 static const char *identity (const contentline
*) PURE
;
41 static char *format_params (char *, param_list
*);
42 static char *fold (char *, int);
44 #define MHICAL_SWITCHES \
45 X("reply accept|decline|tentative", 0, REPLYSW) \
46 X("cancel", 0, CANCELSW) \
47 X("form formatfile", 0, FORMSW) \
48 X("format string", 5, FMTSW) \
49 X("infile", 0, INFILESW) \
50 X("outfile", 0, OUTFILESW) \
51 X("contenttype", 0, CONTENTTYPESW) \
52 X("nocontenttype", 0, NCONTENTTYPESW) \
53 X("unfold", 0, UNFOLDSW) \
54 X("debug", 0, DEBUGSW) \
55 X("version", 0, VERSIONSW) \
56 X("help", 0, HELPSW) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(MHICAL
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(MHICAL
, switches
);
66 vevent vevents
= { NULL
, NULL
, NULL
};
67 int parser_status
= 0;
70 main (int argc
, char *argv
[])
72 /* RFC 5322 § 3.3 date-time format, including the optional
73 day-of-week and not including the optional seconds. The
74 zone is required by the RFC but not always output by this
75 format, because RFC 5545 § 3.3.5 allows date-times not
76 bound to any time zone. */
78 act action
= ACT_NONE
;
79 char *infile
= NULL
, *outfile
= NULL
;
80 FILE *inputfile
= NULL
, *outputfile
= NULL
;
81 bool contenttype
= false;
83 vevent
*v
, *nextvevent
;
84 char *form
= "mhical.24hour", *format
= NULL
;
85 char **argp
, **arguments
, *cp
;
87 icaldebug
= 0; /* Global provided by bison (with name-prefix "ical"). */
89 if (nmh_init(argv
[0], true, false)) { return 1; }
91 arguments
= getarguments (invo_name
, argc
, argv
, 1);
97 while ((cp
= *argp
++)) {
99 switch (smatch (++cp
, switches
)) {
101 ambigsw (cp
, switches
);
104 die("-%s unknown", cp
);
108 snprintf (buf
, sizeof buf
, "%s [switches]", invo_name
);
109 print_help (buf
, switches
, 1);
113 print_version(invo_name
);
120 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
121 die("missing argument to %s", argp
[-2]);
122 if (! strcasecmp (cp
, "accept")) {
124 } else if (! strcasecmp (cp
, "decline")) {
125 action
= ACE_DECLINE
;
126 } else if (! strcasecmp (cp
, "tentative")) {
127 action
= ACT_TENTATIVE
;
128 } else if (! strcasecmp (cp
, "delegate")) {
129 action
= ACT_DELEGATE
;
131 die("Unknown action: %s", cp
);
140 if (! (form
= *argp
++) || *form
== '-')
141 die("missing argument to %s", argp
[-2]);
145 if (! (format
= *argp
++) || *format
== '-')
146 die("missing argument to %s", argp
[-2]);
151 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
152 die("missing argument to %s", argp
[-2]);
153 infile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
156 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
157 die("missing argument to %s", argp
[-2]);
158 outfile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
178 if ((inputfile
= fopen (infile
, "r"))) {
179 icalset_inputfile (inputfile
);
181 adios (infile
, "error opening");
188 if ((outputfile
= fopen (outfile
, "w"))) {
189 icalset_outputfile (outputfile
);
191 adios (outfile
, "error opening");
197 vevents
.last
= &vevents
;
198 /* vevents is accessed by parser as global. */
201 for (v
= &vevents
; v
; v
= nextvevent
) {
202 if (! unfold
&& v
!= &vevents
&& v
->contentlines
&&
203 v
->contentlines
->name
&&
204 strcasecmp (v
->contentlines
->name
, "END") &&
205 v
->contentlines
->value
&&
206 strcasecmp (v
->contentlines
->value
, "VCALENDAR")) {
207 /* Output blank line between vevents. Not before
208 first vevent and not after last. */
209 putc ('\n', outputfile
);
212 if (action
== ACT_NONE
) {
214 dump_unfolded (outputfile
, v
->contentlines
);
216 char *nfs
= new_fs (form
, format
, NULL
);
218 display (outputfile
, v
->contentlines
, nfs
);
222 if (action
== ACT_CANCEL
) {
223 convert_to_cancellation (v
->contentlines
);
225 convert_to_reply (v
->contentlines
, action
);
227 output (outputfile
, v
->contentlines
, contenttype
);
230 free_contentlines (v
->contentlines
);
231 nextvevent
= v
->next
;
238 if (fclose (inputfile
) != 0) {
239 advise (infile
, "error closing");
244 if (fclose (outputfile
) != 0) {
245 advise (outfile
, "error closing");
250 return parser_status
;
254 * - Change METHOD from REQUEST to REPLY.
256 * - Remove all ATTENDEE lines for other users (based on ismymbox ()).
257 * - For the user's ATTENDEE line:
258 * - Remove ROLE and RSVP parameters.
259 * - Change PARTSTAT value to indicate reply action, e.g., ACCEPTED,
260 * DECLINED, or TENTATIVE.
261 * - Insert action at beginning of SUMMARY value.
262 * - Remove all X- lines.
263 * - Update DTSTAMP with current timestamp.
264 * - Remove all DESCRIPTION lines.
265 * - Excise VALARM sections.
268 convert_to_reply (contentline
*clines
, act action
)
270 char *partstat
= NULL
;
271 bool found_my_attendee_line
= false;
274 convert_common (clines
, action
);
278 partstat
= "ACCEPTED";
281 partstat
= "DECLINED";
284 partstat
= "TENTATIVE";
290 /* Call find_contentline () with node as argument to find multiple
291 matching contentlines. */
293 (node
= find_contentline (node
, "ATTENDEE", 0));
297 ismymbox (NULL
); /* need to prime ismymbox() */
299 /* According to RFC 5545 § 3.3.3, an email address in the
300 value must be a mailto URI. */
301 if (! strncasecmp (node
->value
, "mailto:", 7)) {
302 char *addr
= node
->value
+ 7;
305 /* Skip any leading whitespace. */
306 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
308 addr
= getname (addr
);
309 mn
= getm (addr
, NULL
, 0, NULL
, 0);
311 /* Need to flush getname after use. */
312 while (getname ("")) { continue; }
315 found_my_attendee_line
= true;
316 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
319 for (v
= p
->values
; v
; v
= v
->next
) {
320 if (! strcasecmp (p
->param_name
, "ROLE") ||
321 ! strcasecmp (p
->param_name
, "RSVP")) {
323 } else if (! strcasecmp (p
->param_name
, "PARTSTAT")) {
325 v
->value
= strdup (partstat
);
330 remove_contentline (node
);
337 if (! found_my_attendee_line
) {
338 /* Generate and attach an ATTENDEE line for me. */
341 /* Add it after the ORGANIZER line, or if none, BEGIN:VEVENT line. */
342 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) ||
343 (node
= find_contentline (clines
, "BEGIN", "VEVENT"))) {
344 contentline
*new_node
= add_contentline (node
, "ATTENDEE");
346 add_param_name (new_node
, mh_xstrdup ("PARTSTAT"));
347 add_param_value (new_node
, mh_xstrdup (partstat
));
348 add_param_name (new_node
, mh_xstrdup ("CN"));
349 add_param_value (new_node
, mh_xstrdup (getfullname ()));
350 new_node
->value
= concat ("MAILTO:", getlocalmbox (), NULL
);
354 /* Call find_contentline () with node as argument to find multiple
355 matching contentlines. */
357 (node
= find_contentline (node
, "DESCRIPTION", 0));
359 /* ACCEPT, at least, replies don't seem to have DESCRIPTIONS. */
360 remove_contentline (node
);
365 * - Change METHOD from REQUEST to CANCEL.
367 * - Insert action at beginning of SUMMARY value.
368 * - Remove all X- lines.
369 * - Update DTSTAMP with current timestamp.
370 * - Change STATUS from CONFIRMED to CANCELLED.
371 * - Increment value of SEQUENCE.
372 * - Excise VALARM sections.
375 convert_to_cancellation (contentline
*clines
)
379 convert_common (clines
, ACT_CANCEL
);
381 if ((node
= find_contentline (clines
, "STATUS", 0)) &&
382 ! strcasecmp (node
->value
, "CONFIRMED")) {
384 node
->value
= mh_xstrdup ("CANCELLED");
387 if ((node
= find_contentline (clines
, "SEQUENCE", 0))) {
388 int sequence
= atoi (node
->value
);
391 (void) snprintf (buf
, sizeof buf
, "%d", sequence
+ 1);
393 node
->value
= mh_xstrdup (buf
);
398 convert_common (contentline
*clines
, act action
)
403 if ((node
= find_contentline (clines
, "METHOD", 0))) {
405 node
->value
= mh_xstrdup (action
== ACT_CANCEL
? "CANCEL" : "REPLY");
408 if ((node
= find_contentline (clines
, "PRODID", 0))) {
410 node
->value
= mh_xstrdup ("nmh mhical v0.1");
413 if ((node
= find_contentline (clines
, "VERSION", 0))) {
415 inform("Version property is missing value, assume 2.0, continuing...");
416 node
->value
= mh_xstrdup ("2.0");
419 if (strcmp (node
->value
, "2.0")) {
420 inform("supports the Version 2.0 specified by RFC 5545 "
421 "but iCalendar object has Version %s, continuing...",
423 node
->value
= mh_xstrdup ("2.0");
427 if ((node
= find_contentline (clines
, "SUMMARY", 0))) {
432 insert
= "Accepted: ";
435 insert
= "Declined: ";
438 insert
= "Tentative: ";
441 die("Delegate replies are not supported");
444 insert
= "Cancelled:";
451 const size_t len
= strlen (insert
) + strlen (node
->value
) + 1;
452 char *tmp
= mh_xmalloc (len
);
454 (void) strncpy (tmp
, insert
, len
);
455 (void) strncat (tmp
, node
->value
, len
- strlen (insert
) - 1);
459 /* Should never get here. */
460 die("Unknown action: %d", action
);
464 if ((node
= find_contentline (clines
, "DTSTAMP", 0))) {
465 const time_t now
= time (NULL
);
468 if (gmtime_r (&now
, &now_tm
)) {
469 /* 17 would be sufficient given that RFC 5545 § 3.3.4
470 supports only a 4 digit year. */
473 if (strftime (buf
, sizeof buf
, "%Y%m%dT%H%M%SZ", &now_tm
)) {
475 node
->value
= mh_xstrdup (buf
);
477 inform("strftime unable to format current time, continuing...");
480 inform("gmtime_r failed on current time, continuing...");
484 /* Excise X- lines and VALARM section(s). */
486 for (node
= clines
; node
; node
= node
->next
) {
487 /* node->name will be NULL if the line was deleted. */
488 if (! node
->name
) { continue; }
491 if (! strcasecmp ("END", node
->name
) &&
492 ! strcasecmp ("VALARM", node
->value
)) {
495 remove_contentline (node
);
497 if (! strcasecmp ("BEGIN", node
->name
) &&
498 ! strcasecmp ("VALARM", node
->value
)) {
500 remove_contentline (node
);
501 } else if (! strncasecmp ("X-", node
->name
, 2)) {
502 remove_contentline (node
);
508 /* Echo the input, but with unfolded lines. */
510 dump_unfolded (FILE *file
, contentline
*clines
)
514 for (node
= clines
; node
; node
= node
->next
) {
515 fputs (node
->input_line
, file
);
520 output (FILE *file
, contentline
*clines
, int contenttype
)
525 /* Generate a Content-Type header to pass the method parameter
526 to mhbuild. Per RFC 5545 Secs. 6 and 8.1, it must be
527 UTF-8. But we don't attempt to do any conversion of the
529 if ((node
= find_contentline (clines
, "METHOD", 0))) {
531 "Content-Type: text/calendar; method=\"%s\"; "
532 "charset=\"UTF-8\"\n\n",
537 for (node
= clines
; node
; node
= node
->next
) {
542 line
= mh_xstrdup (node
->name
);
543 line
= format_params (line
, node
->params
);
546 line
= mh_xrealloc (line
, len
+ 2);
548 line
[len
+ 1] = '\0';
550 line
= fold (add (node
->value
, line
),
551 clines
->cr_before_lf
== CR_BEFORE_LF
);
554 if (clines
->cr_before_lf
!= LF_ONLY
)
563 * Display these fields of the iCalendar event:
567 * - description, except for "\n\n" and in VALARM
569 * - dtstart in local timezone
570 * - dtend in local timezone
571 * - attendees (limited to number specified in initialization)
574 display (FILE *file
, contentline
*clines
, char *nfs
)
576 tzdesc_t timezones
= load_timezones (clines
);
581 int dat
[5] = { 0, 0, 0, INT_MAX
, 0 };
583 charstring_t buffer
= charstring_create (BUFSIZ
);
584 charstring_t attendees
= charstring_create (BUFSIZ
);
585 const unsigned int max_attendees
= 20;
586 unsigned int num_attendees
;
588 /* Don't call on the END:VCALENDAR line. */
589 if (clines
&& clines
->next
) {
590 (void) fmt_compile (nfs
, &fmt
, 1);
593 if ((c
= fmt_findcomp ("method"))) {
594 if ((node
= find_contentline (clines
, "METHOD", 0)) && node
->value
) {
595 c
->c_text
= mh_xstrdup (node
->value
);
599 if ((c
= fmt_findcomp ("organizer"))) {
600 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) &&
602 c
->c_text
= mh_xstrdup (identity (node
));
606 if ((c
= fmt_findcomp ("summary"))) {
607 if ((node
= find_contentline (clines
, "SUMMARY", 0)) && node
->value
) {
608 c
->c_text
= mh_xstrdup (node
->value
);
612 /* Only display DESCRIPTION lines that are outside VALARM section(s). */
614 if ((c
= fmt_findcomp ("description"))) {
615 for (node
= clines
; node
; node
= node
->next
) {
616 /* node->name will be NULL if the line was deleted. */
617 if (node
->name
&& node
->value
&& ! in_valarm
&&
618 ! strcasecmp ("DESCRIPTION", node
->name
) &&
619 strcasecmp (node
->value
, "\\n\\n")) {
620 c
->c_text
= mh_xstrdup (node
->value
);
621 } else if (in_valarm
) {
622 if (! strcasecmp ("END", node
->name
) &&
623 ! strcasecmp ("VALARM", node
->value
)) {
627 if (! strcasecmp ("BEGIN", node
->name
) &&
628 ! strcasecmp ("VALARM", node
->value
)) {
635 if ((c
= fmt_findcomp ("location"))) {
636 if ((node
= find_contentline (clines
, "LOCATION", 0)) &&
638 c
->c_text
= mh_xstrdup (node
->value
);
642 if ((c
= fmt_findcomp ("dtstart"))) {
643 /* Find DTSTART outsize of a VTIMEZONE section. */
644 in_vtimezone
= false;
645 for (node
= clines
; node
; node
= node
->next
) {
646 /* node->name will be NULL if the line was deleted. */
647 if (! node
->name
) { continue; }
650 if (! strcasecmp ("END", node
->name
) &&
651 ! strcasecmp ("VTIMEZONE", node
->value
)) {
652 in_vtimezone
= false;
655 if (! strcasecmp ("BEGIN", node
->name
) &&
656 ! strcasecmp ("VTIMEZONE", node
->value
)) {
658 } else if (! strcasecmp ("DTSTART", node
->name
)) {
659 /* Got it: DTSTART outside of a VTIMEZONE section. */
660 char *datetime
= format_datetime (timezones
, node
);
661 c
->c_text
= datetime
? datetime
: mh_xstrdup(node
->value
);
667 if ((c
= fmt_findcomp ("dtend"))) {
668 if ((node
= find_contentline (clines
, "DTEND", 0)) && node
->value
) {
669 char *datetime
= format_datetime (timezones
, node
);
670 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
671 } else if ((node
= find_contentline (clines
, "DTSTART", 0)) &&
673 /* There is no DTEND. If there's a DTSTART, use it. If it
674 doesn't have a time, assume that the event is for the
675 entire day and append 23:59:59 to it so that it signifies
676 the end of the day. And assume local timezone. */
677 if (strchr(node
->value
, 'T')) {
678 char * datetime
= format_datetime (timezones
, node
);
679 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
682 contentline node_copy
;
685 node_copy
.value
= concat(node_copy
.value
, "T235959", NULL
);
686 datetime
= format_datetime (timezones
, &node_copy
);
687 c
->c_text
= datetime
? datetime
: strdup(node_copy
.value
);
688 free(node_copy
.value
);
693 if ((c
= fmt_findcomp ("attendees"))) {
694 /* Call find_contentline () with node as argument to find multiple
695 matching contentlines. */
696 charstring_append_cstring (attendees
, "Attendees: ");
697 for (node
= clines
, num_attendees
= 0;
698 (node
= find_contentline (node
, "ATTENDEE", 0)) &&
699 num_attendees
++ < max_attendees
;
701 const char *id
= identity (node
);
703 if (num_attendees
> 1) {
704 charstring_append_cstring (attendees
, ", ");
706 charstring_append_cstring (attendees
, id
);
709 if (num_attendees
>= max_attendees
) {
710 unsigned int not_shown
= 0;
713 (node
= find_contentline (node
, "ATTENDEE", 0));
721 (void) snprintf (buf
, sizeof buf
, ", and %d more", not_shown
);
722 charstring_append_cstring (attendees
, buf
);
726 if (num_attendees
> 0) {
727 c
->c_text
= charstring_buffer_copy (attendees
);
731 /* Don't call on the END:VCALENDAR line. */
732 if (clines
&& clines
->next
) {
733 (void) fmt_scan (fmt
, buffer
, INT_MAX
, dat
, NULL
);
734 fputs (charstring_buffer (buffer
), file
);
738 charstring_free (attendees
);
739 charstring_free (buffer
);
740 free_timezones (timezones
);
744 identity (const contentline
*node
)
746 /* According to RFC 5545 § 3.3.3, an email address in the value
747 must be a mailto URI. */
748 if (! strncasecmp (node
->value
, "mailto:", 7)) {
752 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
755 for (v
= p
->values
; v
; v
= v
->next
) {
756 if (! strcasecmp (p
->param_name
, "CN")) {
762 /* Did not find a CN parameter, so output the address. */
763 addr
= node
->value
+ 7;
765 /* Skip any leading whitespace. */
766 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
775 format_params (char *line
, param_list
*p
)
777 for ( ; p
&& p
->param_name
; p
= p
->next
) {
779 size_t num_values
= 0;
781 for (v
= p
->values
; v
; v
= v
->next
) {
782 if (v
->value
) { ++num_values
; }
786 size_t len
= strlen (line
);
788 line
= mh_xrealloc (line
, len
+ 2);
790 line
[len
+ 1] = '\0';
792 line
= add (p
->param_name
, line
);
794 for (v
= p
->values
; v
; v
= v
->next
) {
796 line
= mh_xrealloc (line
, len
+ 2);
797 line
[len
] = v
== p
->values
? '=' : ',';
798 line
[len
+ 1] = '\0';
800 line
= add (v
->value
, line
);
809 fold (char *line
, int uses_cr
)
811 size_t remaining
= strlen (line
);
812 size_t current_line_len
= 0;
813 charstring_t folded_line
= charstring_create (2 * remaining
);
814 const char *cp
= line
;
816 #ifdef MULTIBYTE_SUPPORT
817 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
820 while (*cp
&& remaining
> 0) {
821 #ifdef MULTIBYTE_SUPPORT
822 int char_len
= mbtowc (NULL
, cp
, (size_t) MB_CUR_MAX
< remaining
823 ? (size_t) MB_CUR_MAX
825 if (char_len
== -1) { char_len
= 1; }
827 const int char_len
= 1;
830 charstring_push_back_chars (folded_line
, cp
, char_len
, 1);
831 remaining
-= max(char_len
, 1);
833 /* remaining must be > 0 to pass the loop condition above, so
834 if it's not > 1, it is == 1. */
835 if (++current_line_len
>= 75) {
836 if (remaining
> 1 || (*(cp
+1) != '\0' && *(cp
+1) != '\r' &&
839 if (uses_cr
) { charstring_push_back (folded_line
, '\r'); }
840 charstring_push_back (folded_line
, '\n');
841 charstring_push_back (folded_line
, ' ');
842 current_line_len
= 0;
846 cp
+= max(char_len
, 1);
850 line
= charstring_buffer_copy (folded_line
);
851 charstring_free (folded_line
);