]>
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/getarguments.h"
10 #include "sbr/concat.h"
11 #include "sbr/smatch.h"
12 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/error.h"
17 #include "h/icalendar.h"
18 #include "sbr/icalparse.h"
19 #include "h/fmt_scan.h"
20 #include "h/addrsbr.h"
35 static void convert_to_reply (contentline
*, act
);
36 static void convert_to_cancellation (contentline
*);
37 static void convert_common (contentline
*, act
);
38 static void dump_unfolded (FILE *, contentline
*);
39 static void output (FILE *, contentline
*, int);
40 static void display (FILE *, contentline
*, char *);
41 static const char *identity (const contentline
*) PURE
;
42 static char *format_params (char *, param_list
*);
43 static char *fold (char *, int);
45 #define MHICAL_SWITCHES \
46 X("reply accept|decline|tentative", 0, REPLYSW) \
47 X("cancel", 0, CANCELSW) \
48 X("form formatfile", 0, FORMSW) \
49 X("format string", 5, FMTSW) \
50 X("infile", 0, INFILESW) \
51 X("outfile", 0, OUTFILESW) \
52 X("contenttype", 0, CONTENTTYPESW) \
53 X("nocontenttype", 0, NCONTENTTYPESW) \
54 X("unfold", 0, UNFOLDSW) \
55 X("debug", 0, DEBUGSW) \
56 X("version", 0, VERSIONSW) \
57 X("help", 0, HELPSW) \
59 #define X(sw, minchars, id) id,
60 DEFINE_SWITCH_ENUM(MHICAL
);
63 #define X(sw, minchars, id) { sw, minchars, id },
64 DEFINE_SWITCH_ARRAY(MHICAL
, switches
);
67 vevent vevents
= { NULL
, NULL
, NULL
};
68 int parser_status
= 0;
71 main (int argc
, char *argv
[])
73 /* RFC 5322 § 3.3 date-time format, including the optional
74 day-of-week and not including the optional seconds. The
75 zone is required by the RFC but not always output by this
76 format, because RFC 5545 § 3.3.5 allows date-times not
77 bound to any time zone. */
79 act action
= ACT_NONE
;
80 char *infile
= NULL
, *outfile
= NULL
;
81 FILE *inputfile
= NULL
, *outputfile
= NULL
;
82 bool contenttype
= false;
84 vevent
*v
, *nextvevent
;
85 char *form
= "mhical.24hour", *format
= NULL
;
86 char **argp
, **arguments
, *cp
;
88 icaldebug
= 0; /* Global provided by bison (with name-prefix "ical"). */
90 if (nmh_init(argv
[0], true, false)) { return 1; }
92 arguments
= getarguments (invo_name
, argc
, argv
, 1);
98 while ((cp
= *argp
++)) {
100 switch (smatch (++cp
, switches
)) {
102 ambigsw (cp
, switches
);
105 die("-%s unknown", cp
);
109 snprintf (buf
, sizeof buf
, "%s [switches]", invo_name
);
110 print_help (buf
, switches
, 1);
114 print_version(invo_name
);
121 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
122 die("missing argument to %s", argp
[-2]);
123 if (! strcasecmp (cp
, "accept")) {
125 } else if (! strcasecmp (cp
, "decline")) {
126 action
= ACE_DECLINE
;
127 } else if (! strcasecmp (cp
, "tentative")) {
128 action
= ACT_TENTATIVE
;
129 } else if (! strcasecmp (cp
, "delegate")) {
130 action
= ACT_DELEGATE
;
132 die("Unknown action: %s", cp
);
141 if (! (form
= *argp
++) || *form
== '-')
142 die("missing argument to %s", argp
[-2]);
146 if (! (format
= *argp
++) || *format
== '-')
147 die("missing argument to %s", argp
[-2]);
152 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
153 die("missing argument to %s", argp
[-2]);
154 infile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
157 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
158 die("missing argument to %s", argp
[-2]);
159 outfile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
179 if ((inputfile
= fopen (infile
, "r"))) {
180 icalset_inputfile (inputfile
);
182 adios (infile
, "error opening");
189 if ((outputfile
= fopen (outfile
, "w"))) {
190 icalset_outputfile (outputfile
);
192 adios (outfile
, "error opening");
198 vevents
.last
= &vevents
;
199 /* vevents is accessed by parser as global. */
202 for (v
= &vevents
; v
; v
= nextvevent
) {
203 if (! unfold
&& v
!= &vevents
&& v
->contentlines
&&
204 v
->contentlines
->name
&&
205 strcasecmp (v
->contentlines
->name
, "END") &&
206 v
->contentlines
->value
&&
207 strcasecmp (v
->contentlines
->value
, "VCALENDAR")) {
208 /* Output blank line between vevents. Not before
209 first vevent and not after last. */
210 putc ('\n', outputfile
);
213 if (action
== ACT_NONE
) {
215 dump_unfolded (outputfile
, v
->contentlines
);
217 char *nfs
= new_fs (form
, format
, NULL
);
219 display (outputfile
, v
->contentlines
, nfs
);
223 if (action
== ACT_CANCEL
) {
224 convert_to_cancellation (v
->contentlines
);
226 convert_to_reply (v
->contentlines
, action
);
228 output (outputfile
, v
->contentlines
, contenttype
);
231 free_contentlines (v
->contentlines
);
232 nextvevent
= v
->next
;
239 if (fclose (inputfile
) != 0) {
240 advise (infile
, "error closing");
245 if (fclose (outputfile
) != 0) {
246 advise (outfile
, "error closing");
251 return parser_status
;
255 * - Change METHOD from REQUEST to REPLY.
257 * - Remove all ATTENDEE lines for other users (based on ismymbox ()).
258 * - For the user's ATTENDEE line:
259 * - Remove ROLE and RSVP parameters.
260 * - Change PARTSTAT value to indicate reply action, e.g., ACCEPTED,
261 * DECLINED, or TENTATIVE.
262 * - Insert action at beginning of SUMMARY value.
263 * - Remove all X- lines.
264 * - Update DTSTAMP with current timestamp.
265 * - Remove all DESCRIPTION lines.
266 * - Excise VALARM sections.
269 convert_to_reply (contentline
*clines
, act action
)
271 char *partstat
= NULL
;
272 bool found_my_attendee_line
= false;
275 convert_common (clines
, action
);
279 partstat
= "ACCEPTED";
282 partstat
= "DECLINED";
285 partstat
= "TENTATIVE";
291 /* Call find_contentline () with node as argument to find multiple
292 matching contentlines. */
294 (node
= find_contentline (node
, "ATTENDEE", 0));
298 ismymbox (NULL
); /* need to prime ismymbox() */
300 /* According to RFC 5545 § 3.3.3, an email address in the
301 value must be a mailto URI. */
302 if (! strncasecmp (node
->value
, "mailto:", 7)) {
303 char *addr
= node
->value
+ 7;
306 /* Skip any leading whitespace. */
307 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
309 addr
= getname (addr
);
310 mn
= getm (addr
, NULL
, 0, NULL
, 0);
312 /* Need to flush getname after use. */
313 while (getname ("")) { continue; }
316 found_my_attendee_line
= true;
317 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
320 for (v
= p
->values
; v
; v
= v
->next
) {
321 if (! strcasecmp (p
->param_name
, "ROLE") ||
322 ! strcasecmp (p
->param_name
, "RSVP")) {
324 } else if (! strcasecmp (p
->param_name
, "PARTSTAT")) {
326 v
->value
= strdup (partstat
);
331 remove_contentline (node
);
338 if (! found_my_attendee_line
) {
339 /* Generate and attach an ATTENDEE line for me. */
342 /* Add it after the ORGANIZER line, or if none, BEGIN:VEVENT line. */
343 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) ||
344 (node
= find_contentline (clines
, "BEGIN", "VEVENT"))) {
345 contentline
*new_node
= add_contentline (node
, "ATTENDEE");
347 add_param_name (new_node
, mh_xstrdup ("PARTSTAT"));
348 add_param_value (new_node
, mh_xstrdup (partstat
));
349 add_param_name (new_node
, mh_xstrdup ("CN"));
350 add_param_value (new_node
, mh_xstrdup (getfullname ()));
351 new_node
->value
= concat ("MAILTO:", getlocalmbox (), NULL
);
355 /* Call find_contentline () with node as argument to find multiple
356 matching contentlines. */
358 (node
= find_contentline (node
, "DESCRIPTION", 0));
360 /* ACCEPT, at least, replies don't seem to have DESCRIPTIONS. */
361 remove_contentline (node
);
366 * - Change METHOD from REQUEST to CANCEL.
368 * - Insert action at beginning of SUMMARY value.
369 * - Remove all X- lines.
370 * - Update DTSTAMP with current timestamp.
371 * - Change STATUS from CONFIRMED to CANCELLED.
372 * - Increment value of SEQUENCE.
373 * - Excise VALARM sections.
376 convert_to_cancellation (contentline
*clines
)
380 convert_common (clines
, ACT_CANCEL
);
382 if ((node
= find_contentline (clines
, "STATUS", 0)) &&
383 ! strcasecmp (node
->value
, "CONFIRMED")) {
385 node
->value
= mh_xstrdup ("CANCELLED");
388 if ((node
= find_contentline (clines
, "SEQUENCE", 0))) {
389 int sequence
= atoi (node
->value
);
392 (void) snprintf (buf
, sizeof buf
, "%d", sequence
+ 1);
394 node
->value
= mh_xstrdup (buf
);
399 convert_common (contentline
*clines
, act action
)
404 if ((node
= find_contentline (clines
, "METHOD", 0))) {
406 node
->value
= mh_xstrdup (action
== ACT_CANCEL
? "CANCEL" : "REPLY");
409 if ((node
= find_contentline (clines
, "PRODID", 0))) {
411 node
->value
= mh_xstrdup ("nmh mhical v0.1");
414 if ((node
= find_contentline (clines
, "VERSION", 0))) {
416 inform("Version property is missing value, assume 2.0, continuing...");
417 node
->value
= mh_xstrdup ("2.0");
420 if (strcmp (node
->value
, "2.0")) {
421 inform("supports the Version 2.0 specified by RFC 5545 "
422 "but iCalendar object has Version %s, continuing...",
424 node
->value
= mh_xstrdup ("2.0");
428 if ((node
= find_contentline (clines
, "SUMMARY", 0))) {
433 insert
= "Accepted: ";
436 insert
= "Declined: ";
439 insert
= "Tentative: ";
442 die("Delegate replies are not supported");
445 insert
= "Cancelled:";
452 const size_t len
= strlen (insert
) + strlen (node
->value
) + 1;
453 char *tmp
= mh_xmalloc (len
);
455 (void) strncpy (tmp
, insert
, len
);
456 (void) strncat (tmp
, node
->value
, len
- strlen (insert
) - 1);
460 /* Should never get here. */
461 die("Unknown action: %d", action
);
465 if ((node
= find_contentline (clines
, "DTSTAMP", 0))) {
466 const time_t now
= time (NULL
);
469 if (gmtime_r (&now
, &now_tm
)) {
470 /* 17 would be sufficient given that RFC 5545 § 3.3.4
471 supports only a 4 digit year. */
474 if (strftime (buf
, sizeof buf
, "%Y%m%dT%H%M%SZ", &now_tm
)) {
476 node
->value
= mh_xstrdup (buf
);
478 inform("strftime unable to format current time, continuing...");
481 inform("gmtime_r failed on current time, continuing...");
485 /* Excise X- lines and VALARM section(s). */
487 for (node
= clines
; node
; node
= node
->next
) {
488 /* node->name will be NULL if the line was deleted. */
489 if (! node
->name
) { continue; }
492 if (! strcasecmp ("END", node
->name
) &&
493 ! strcasecmp ("VALARM", node
->value
)) {
496 remove_contentline (node
);
498 if (! strcasecmp ("BEGIN", node
->name
) &&
499 ! strcasecmp ("VALARM", node
->value
)) {
501 remove_contentline (node
);
502 } else if (! strncasecmp ("X-", node
->name
, 2)) {
503 remove_contentline (node
);
509 /* Echo the input, but with unfolded lines. */
511 dump_unfolded (FILE *file
, contentline
*clines
)
515 for (node
= clines
; node
; node
= node
->next
) {
516 fputs (node
->input_line
, file
);
521 output (FILE *file
, contentline
*clines
, int contenttype
)
526 /* Generate a Content-Type header to pass the method parameter
527 to mhbuild. Per RFC 5545 Secs. 6 and 8.1, it must be
528 UTF-8. But we don't attempt to do any conversion of the
530 if ((node
= find_contentline (clines
, "METHOD", 0))) {
532 "Content-Type: text/calendar; method=\"%s\"; "
533 "charset=\"UTF-8\"\n\n",
538 for (node
= clines
; node
; node
= node
->next
) {
543 line
= mh_xstrdup (node
->name
);
544 line
= format_params (line
, node
->params
);
547 line
= mh_xrealloc (line
, len
+ 2);
549 line
[len
+ 1] = '\0';
551 line
= fold (add (node
->value
, line
),
552 clines
->cr_before_lf
== CR_BEFORE_LF
);
555 if (clines
->cr_before_lf
!= LF_ONLY
)
564 * Display these fields of the iCalendar event:
568 * - description, except for "\n\n" and in VALARM
570 * - dtstart in local timezone
571 * - dtend in local timezone
572 * - attendees (limited to number specified in initialization)
575 display (FILE *file
, contentline
*clines
, char *nfs
)
577 tzdesc_t timezones
= load_timezones (clines
);
582 int dat
[5] = { 0, 0, 0, INT_MAX
, 0 };
584 charstring_t buffer
= charstring_create (BUFSIZ
);
585 charstring_t attendees
= charstring_create (BUFSIZ
);
586 const unsigned int max_attendees
= 20;
587 unsigned int num_attendees
;
589 /* Don't call on the END:VCALENDAR line. */
590 if (clines
&& clines
->next
) {
591 (void) fmt_compile (nfs
, &fmt
, 1);
594 if ((c
= fmt_findcomp ("method"))) {
595 if ((node
= find_contentline (clines
, "METHOD", 0)) && node
->value
) {
596 c
->c_text
= mh_xstrdup (node
->value
);
600 if ((c
= fmt_findcomp ("organizer"))) {
601 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) &&
603 c
->c_text
= mh_xstrdup (identity (node
));
607 if ((c
= fmt_findcomp ("summary"))) {
608 if ((node
= find_contentline (clines
, "SUMMARY", 0)) && node
->value
) {
609 c
->c_text
= mh_xstrdup (node
->value
);
613 /* Only display DESCRIPTION lines that are outside VALARM section(s). */
615 if ((c
= fmt_findcomp ("description"))) {
616 for (node
= clines
; node
; node
= node
->next
) {
617 /* node->name will be NULL if the line was deleted. */
618 if (node
->name
&& node
->value
&& ! in_valarm
&&
619 ! strcasecmp ("DESCRIPTION", node
->name
) &&
620 strcasecmp (node
->value
, "\\n\\n")) {
621 c
->c_text
= mh_xstrdup (node
->value
);
622 } else if (in_valarm
) {
623 if (! strcasecmp ("END", node
->name
) &&
624 ! strcasecmp ("VALARM", node
->value
)) {
628 if (! strcasecmp ("BEGIN", node
->name
) &&
629 ! strcasecmp ("VALARM", node
->value
)) {
636 if ((c
= fmt_findcomp ("location"))) {
637 if ((node
= find_contentline (clines
, "LOCATION", 0)) &&
639 c
->c_text
= mh_xstrdup (node
->value
);
643 if ((c
= fmt_findcomp ("dtstart"))) {
644 /* Find DTSTART outsize of a VTIMEZONE section. */
645 in_vtimezone
= false;
646 for (node
= clines
; node
; node
= node
->next
) {
647 /* node->name will be NULL if the line was deleted. */
648 if (! node
->name
) { continue; }
651 if (! strcasecmp ("END", node
->name
) &&
652 ! strcasecmp ("VTIMEZONE", node
->value
)) {
653 in_vtimezone
= false;
656 if (! strcasecmp ("BEGIN", node
->name
) &&
657 ! strcasecmp ("VTIMEZONE", node
->value
)) {
659 } else if (! strcasecmp ("DTSTART", node
->name
)) {
660 /* Got it: DTSTART outside of a VTIMEZONE section. */
661 char *datetime
= format_datetime (timezones
, node
);
662 c
->c_text
= datetime
? datetime
: mh_xstrdup(node
->value
);
668 if ((c
= fmt_findcomp ("dtend"))) {
669 if ((node
= find_contentline (clines
, "DTEND", 0)) && node
->value
) {
670 char *datetime
= format_datetime (timezones
, node
);
671 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
672 } else if ((node
= find_contentline (clines
, "DTSTART", 0)) &&
674 /* There is no DTEND. If there's a DTSTART, use it. If it
675 doesn't have a time, assume that the event is for the
676 entire day and append 23:59:59 to it so that it signifies
677 the end of the day. And assume local timezone. */
678 if (strchr(node
->value
, 'T')) {
679 char * datetime
= format_datetime (timezones
, node
);
680 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
683 contentline node_copy
;
686 node_copy
.value
= concat(node_copy
.value
, "T235959", NULL
);
687 datetime
= format_datetime (timezones
, &node_copy
);
688 c
->c_text
= datetime
? datetime
: strdup(node_copy
.value
);
689 free(node_copy
.value
);
694 if ((c
= fmt_findcomp ("attendees"))) {
695 /* Call find_contentline () with node as argument to find multiple
696 matching contentlines. */
697 charstring_append_cstring (attendees
, "Attendees: ");
698 for (node
= clines
, num_attendees
= 0;
699 (node
= find_contentline (node
, "ATTENDEE", 0)) &&
700 num_attendees
++ < max_attendees
;
702 const char *id
= identity (node
);
704 if (num_attendees
> 1) {
705 charstring_append_cstring (attendees
, ", ");
707 charstring_append_cstring (attendees
, id
);
710 if (num_attendees
>= max_attendees
) {
711 unsigned int not_shown
= 0;
714 (node
= find_contentline (node
, "ATTENDEE", 0));
722 (void) snprintf (buf
, sizeof buf
, ", and %d more", not_shown
);
723 charstring_append_cstring (attendees
, buf
);
727 if (num_attendees
> 0) {
728 c
->c_text
= charstring_buffer_copy (attendees
);
732 /* Don't call on the END:VCALENDAR line. */
733 if (clines
&& clines
->next
) {
734 (void) fmt_scan (fmt
, buffer
, INT_MAX
, dat
, NULL
);
735 fputs (charstring_buffer (buffer
), file
);
739 charstring_free (attendees
);
740 charstring_free (buffer
);
741 free_timezones (timezones
);
745 identity (const contentline
*node
)
747 /* According to RFC 5545 § 3.3.3, an email address in the value
748 must be a mailto URI. */
749 if (! strncasecmp (node
->value
, "mailto:", 7)) {
753 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
756 for (v
= p
->values
; v
; v
= v
->next
) {
757 if (! strcasecmp (p
->param_name
, "CN")) {
763 /* Did not find a CN parameter, so output the address. */
764 addr
= node
->value
+ 7;
766 /* Skip any leading whitespace. */
767 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
776 format_params (char *line
, param_list
*p
)
778 for ( ; p
&& p
->param_name
; p
= p
->next
) {
780 size_t num_values
= 0;
782 for (v
= p
->values
; v
; v
= v
->next
) {
783 if (v
->value
) { ++num_values
; }
787 size_t len
= strlen (line
);
789 line
= mh_xrealloc (line
, len
+ 2);
791 line
[len
+ 1] = '\0';
793 line
= add (p
->param_name
, line
);
795 for (v
= p
->values
; v
; v
= v
->next
) {
797 line
= mh_xrealloc (line
, len
+ 2);
798 line
[len
] = v
== p
->values
? '=' : ',';
799 line
[len
+ 1] = '\0';
801 line
= add (v
->value
, line
);
810 fold (char *line
, int uses_cr
)
812 size_t remaining
= strlen (line
);
813 size_t current_line_len
= 0;
814 charstring_t folded_line
= charstring_create (2 * remaining
);
815 const char *cp
= line
;
817 #ifdef MULTIBYTE_SUPPORT
818 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
821 while (*cp
&& remaining
> 0) {
822 #ifdef MULTIBYTE_SUPPORT
823 int char_len
= mbtowc (NULL
, cp
, (size_t) MB_CUR_MAX
< remaining
824 ? (size_t) MB_CUR_MAX
826 if (char_len
== -1) { char_len
= 1; }
828 const int char_len
= 1;
831 charstring_push_back_chars (folded_line
, cp
, char_len
, 1);
832 remaining
-= max(char_len
, 1);
834 /* remaining must be > 0 to pass the loop condition above, so
835 if it's not > 1, it is == 1. */
836 if (++current_line_len
>= 75) {
837 if (remaining
> 1 || (*(cp
+1) != '\0' && *(cp
+1) != '\r' &&
840 if (uses_cr
) { charstring_push_back (folded_line
, '\r'); }
841 charstring_push_back (folded_line
, '\n');
842 charstring_push_back (folded_line
, ' ');
843 current_line_len
= 0;
847 cp
+= max(char_len
, 1);
851 line
= charstring_buffer_copy (folded_line
);
852 charstring_free (folded_line
);