]>
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"
26 static void convert_to_reply (contentline
*, act
);
27 static void convert_to_cancellation (contentline
*);
28 static void convert_common (contentline
*, act
);
29 static void dump_unfolded (FILE *, contentline
*);
30 static void output (FILE *, contentline
*, int);
31 static void display (FILE *, contentline
*, char *);
32 static const char *identity (const contentline
*);
33 static char *format_params (char *, param_list
*);
34 static char *fold (char *, int);
36 #define MHICAL_SWITCHES \
37 X("reply accept|decline|tentative", 0, REPLYSW) \
38 X("cancel", 0, CANCELSW) \
39 X("form formatfile", 0, FORMSW) \
40 X("format string", 5, FMTSW) \
41 X("infile", 0, INFILESW) \
42 X("outfile", 0, OUTFILESW) \
43 X("contenttype", 0, CONTENTTYPESW) \
44 X("nocontenttype", 0, NCONTENTTYPESW) \
45 X("unfold", 0, UNFOLDSW) \
46 X("debug", 0, DEBUGSW) \
47 X("version", 0, VERSIONSW) \
48 X("help", 0, HELPSW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(MHICAL
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(MHICAL
, switches
);
58 vevent vevents
= { NULL
, NULL
, NULL
};
61 main (int argc
, char *argv
[]) {
62 /* RFC 5322 § 3.3 date-time format, including the optional
63 day-of-week and not including the optional seconds. The
64 zone is required by the RFC but not always output by this
65 format, because RFC 5545 § 3.3.5 allows date-times not
66 bound to any time zone. */
68 act action
= ACT_NONE
;
69 char *infile
= NULL
, *outfile
= NULL
;
70 FILE *inputfile
= NULL
, *outputfile
= NULL
;
71 int contenttype
= 0, unfold
= 0;
72 vevent
*v
, *nextvevent
;
73 char *form
= "mhical.24hour", *format
= NULL
;
74 char **argp
, **arguments
, *cp
;
76 icaldebug
= 0; /* Global provided by bison (with name-prefix "ical"). */
78 if (nmh_init(argv
[0], 2)) { return 1; }
80 arguments
= getarguments (invo_name
, argc
, argv
, 1);
86 while ((cp
= *argp
++)) {
88 switch (smatch (++cp
, switches
)) {
90 ambigsw (cp
, switches
);
93 adios (NULL
, "-%s unknown", cp
);
97 snprintf (buf
, sizeof buf
, "%s [switches]", invo_name
);
98 print_help (buf
, switches
, 1);
102 print_version(invo_name
);
109 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
110 adios (NULL
, "missing argument to %s", argp
[-2]);
111 if (! strcasecmp (cp
, "accept")) {
113 } else if (! strcasecmp (cp
, "decline")) {
114 action
= ACE_DECLINE
;
115 } else if (! strcasecmp (cp
, "tentative")) {
116 action
= ACT_TENTATIVE
;
117 } else if (! strcasecmp (cp
, "delegate")) {
118 action
= ACT_DELEGATE
;
120 adios (NULL
, "Unknown action: %s", cp
);
129 if (! (form
= *argp
++) || *form
== '-')
130 adios (NULL
, "missing argument to %s", argp
[-2]);
134 if (! (format
= *argp
++) || *format
== '-')
135 adios (NULL
, "missing argument to %s", argp
[-2]);
140 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
141 adios (NULL
, "missing argument to %s", argp
[-2]);
142 infile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
145 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
146 adios (NULL
, "missing argument to %s", argp
[-2]);
147 outfile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
167 if ((inputfile
= fopen (infile
, "r"))) {
168 icalset_inputfile (inputfile
);
170 adios (infile
, "error opening");
177 if ((outputfile
= fopen (outfile
, "w"))) {
178 icalset_outputfile (outputfile
);
180 adios (outfile
, "error opening");
186 vevents
.last
= &vevents
;
187 /* vevents is accessed by parser as global. */
190 for (v
= &vevents
; v
; v
= nextvevent
) {
191 if (! unfold
&& v
!= &vevents
&& v
->contentlines
&&
192 v
->contentlines
->name
&&
193 strcasecmp (v
->contentlines
->name
, "END") &&
194 v
->contentlines
->value
&&
195 strcasecmp (v
->contentlines
->value
, "VCALENDAR")) {
196 /* Output blank line between vevents. Not before
197 first vevent and not after last. */
198 putc ('\n', outputfile
);
201 if (action
== ACT_NONE
) {
203 dump_unfolded (outputfile
, v
->contentlines
);
205 char *nfs
= new_fs (form
, format
, NULL
);
207 display (outputfile
, v
->contentlines
, nfs
);
211 if (action
== ACT_CANCEL
) {
212 convert_to_cancellation (v
->contentlines
);
214 convert_to_reply (v
->contentlines
, action
);
216 output (outputfile
, v
->contentlines
, contenttype
);
219 free_contentlines (v
->contentlines
);
220 nextvevent
= v
->next
;
227 if (fclose (inputfile
) != 0) {
228 advise (infile
, "error closing");
233 if (fclose (outputfile
) != 0) {
234 advise (outfile
, "error closing");
243 * - Change METHOD from REQUEST to REPLY.
245 * - Remove all ATTENDEE lines for other users (based on ismymbox ()).
246 * - For the user's ATTENDEE line:
247 * - Remove ROLE and RSVP parameters.
248 * - Change PARTSTAT value to indicate reply action, e.g., ACCEPTED,
249 * DECLINED, or TENTATIVE.
250 * - Insert action at beginning of SUMMARY value.
251 * - Remove all X- lines.
252 * - Update DTSTAMP with current timestamp.
253 * - Remove all DESCRIPTION lines.
254 * - Excise VALARM sections.
257 convert_to_reply (contentline
*clines
, act action
) {
258 char *partstat
= NULL
;
259 int found_my_attendee_line
= 0;
262 convert_common (clines
, action
);
266 partstat
= "ACCEPTED";
269 partstat
= "DECLINED";
272 partstat
= "TENTATIVE";
278 /* Call find_contentline () with node as argument to find multiple
279 matching contentlines. */
281 (node
= find_contentline (node
, "ATTENDEE", 0));
285 ismymbox (NULL
); /* need to prime ismymbox() */
287 /* According to RFC 5545 § 3.3.3, an email address in the
288 value must be a mailto URI. */
289 if (! strncasecmp (node
->value
, "mailto:", 7)) {
290 char *addr
= node
->value
+ 7;
293 /* Skip any leading whitespace. */
294 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
296 addr
= getname (addr
);
297 mn
= getm (addr
, NULL
, 0, NULL
, 0);
299 /* Need to flush getname after use. */
300 while (getname ("")) { continue; }
303 found_my_attendee_line
= 1;
304 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
307 for (v
= p
->values
; v
; v
= v
->next
) {
308 if (! strcasecmp (p
->param_name
, "ROLE") ||
309 ! strcasecmp (p
->param_name
, "RSVP")) {
311 } else if (! strcasecmp (p
->param_name
, "PARTSTAT")) {
313 v
->value
= strdup (partstat
);
318 remove_contentline (node
);
325 if (! found_my_attendee_line
) {
326 /* Generate and attach an ATTENDEE line for me. */
329 /* Add it after the ORGANIZER line, or if none, BEGIN:VEVENT line. */
330 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) ||
331 (node
= find_contentline (clines
, "BEGIN", "VEVENT"))) {
332 contentline
*new_node
= add_contentline (node
, "ATTENDEE");
334 add_param_name (new_node
, mh_xstrdup ("PARTSTAT"));
335 add_param_value (new_node
, mh_xstrdup (partstat
));
336 add_param_name (new_node
, mh_xstrdup ("CN"));
337 add_param_value (new_node
, mh_xstrdup (getfullname ()));
338 new_node
->value
= concat ("MAILTO:", getlocalmbox (), NULL
);
342 /* Call find_contentline () with node as argument to find multiple
343 matching contentlines. */
345 (node
= find_contentline (node
, "DESCRIPTION", 0));
347 /* ACCEPT, at least, replies don't seem to have DESCRIPTIONS. */
348 remove_contentline (node
);
353 * - Change METHOD from REQUEST to CANCEL.
355 * - Insert action at beginning of SUMMARY value.
356 * - Remove all X- lines.
357 * - Update DTSTAMP with current timestamp.
358 * - Change STATUS from CONFIRMED to CANCELLED.
359 * - Increment value of SEQUENCE.
360 * - Excise VALARM sections.
363 convert_to_cancellation (contentline
*clines
) {
366 convert_common (clines
, ACT_CANCEL
);
368 if ((node
= find_contentline (clines
, "STATUS", 0)) &&
369 ! strcasecmp (node
->value
, "CONFIRMED")) {
371 node
->value
= mh_xstrdup ("CANCELLED");
374 if ((node
= find_contentline (clines
, "SEQUENCE", 0))) {
375 int sequence
= atoi (node
->value
);
378 (void) snprintf (buf
, sizeof buf
, "%d", sequence
+ 1);
380 node
->value
= mh_xstrdup (buf
);
385 convert_common (contentline
*clines
, act action
) {
389 if ((node
= find_contentline (clines
, "METHOD", 0))) {
391 node
->value
= mh_xstrdup (action
== ACT_CANCEL
? "CANCEL" : "REPLY");
394 if ((node
= find_contentline (clines
, "PRODID", 0))) {
396 node
->value
= mh_xstrdup ("nmh mhical v0.1");
399 if ((node
= find_contentline (clines
, "VERSION", 0))) {
401 inform("Version property is missing value, assume 2.0, continuing...");
402 node
->value
= mh_xstrdup ("2.0");
405 if (strcmp (node
->value
, "2.0")) {
406 inform("supports the Version 2.0 specified by RFC 5545 "
407 "but iCalendar object has Version %s, continuing...",
409 node
->value
= mh_xstrdup ("2.0");
413 if ((node
= find_contentline (clines
, "SUMMARY", 0))) {
418 insert
= "Accepted: ";
421 insert
= "Declined: ";
424 insert
= "Tentative: ";
427 adios (NULL
, "Delegate replies are not supported");
430 insert
= "Cancelled:";
437 const size_t len
= strlen (insert
) + strlen (node
->value
) + 1;
438 char *tmp
= mh_xmalloc (len
);
440 (void) strncpy (tmp
, insert
, len
);
441 (void) strncat (tmp
, node
->value
, len
- strlen (insert
) - 1);
445 /* Should never get here. */
446 adios (NULL
, "Unknown action: %d", action
);
450 if ((node
= find_contentline (clines
, "DTSTAMP", 0))) {
451 const time_t now
= time (NULL
);
454 if (gmtime_r (&now
, &now_tm
)) {
455 /* 17 would be sufficient given that RFC 5545 § 3.3.4
456 supports only a 4 digit year. */
459 if (strftime (buf
, sizeof buf
, "%Y%m%dT%H%M%SZ", &now_tm
)) {
461 node
->value
= mh_xstrdup (buf
);
463 inform("strftime unable to format current time, continuing...");
466 inform("gmtime_r failed on current time, continuing...");
470 /* Excise X- lines and VALARM section(s). */
472 for (node
= clines
; node
; node
= node
->next
) {
473 /* node->name will be NULL if the line was deleted. */
474 if (! node
->name
) { continue; }
477 if (! strcasecmp ("END", node
->name
) &&
478 ! strcasecmp ("VALARM", node
->value
)) {
481 remove_contentline (node
);
483 if (! strcasecmp ("BEGIN", node
->name
) &&
484 ! strcasecmp ("VALARM", node
->value
)) {
486 remove_contentline (node
);
487 } else if (! strncasecmp ("X-", node
->name
, 2)) {
488 remove_contentline (node
);
494 /* Echo the input, but with unfolded lines. */
496 dump_unfolded (FILE *file
, contentline
*clines
) {
499 for (node
= clines
; node
; node
= node
->next
) {
500 fputs (node
->input_line
, file
);
505 output (FILE *file
, contentline
*clines
, int contenttype
) {
509 /* Generate a Content-Type header to pass the method parameter
510 to mhbuild. Per RFC 5545 Secs. 6 and 8.1, it must be
511 UTF-8. But we don't attempt to do any conversion of the
513 if ((node
= find_contentline (clines
, "METHOD", 0))) {
515 "Content-Type: text/calendar; method=\"%s\"; "
516 "charset=\"UTF-8\"\n\n",
521 for (node
= clines
; node
; node
= node
->next
) {
526 line
= mh_xstrdup (node
->name
);
527 line
= format_params (line
, node
->params
);
530 line
= mh_xrealloc (line
, len
+ 2);
532 line
[len
+ 1] = '\0';
534 line
= fold (add (node
->value
, line
),
535 clines
->cr_before_lf
== CR_BEFORE_LF
);
537 if (clines
->cr_before_lf
== LF_ONLY
) {
538 fprintf (file
, "%s\n", line
);
540 fprintf (file
, "%s\r\n", line
);
548 * Display these fields of the iCalendar event:
552 * - description, except for "\n\n" and in VALARM
554 * - dtstart in local timezone
555 * - dtend in local timezone
556 * - attendees (limited to number specified in initialization)
559 display (FILE *file
, contentline
*clines
, char *nfs
) {
560 tzdesc_t timezones
= load_timezones (clines
);
565 int dat
[5] = { 0, 0, 0, INT_MAX
, 0 };
567 charstring_t buffer
= charstring_create (BUFSIZ
);
568 charstring_t attendees
= charstring_create (BUFSIZ
);
569 const unsigned int max_attendees
= 20;
570 unsigned int num_attendees
;
572 /* Don't call on the END:VCALENDAR line. */
573 if (clines
&& clines
->next
) {
574 (void) fmt_compile (nfs
, &fmt
, 1);
577 if ((c
= fmt_findcomp ("method"))) {
578 if ((node
= find_contentline (clines
, "METHOD", 0)) && node
->value
) {
579 c
->c_text
= mh_xstrdup (node
->value
);
583 if ((c
= fmt_findcomp ("organizer"))) {
584 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) &&
586 c
->c_text
= mh_xstrdup (identity (node
));
590 if ((c
= fmt_findcomp ("summary"))) {
591 if ((node
= find_contentline (clines
, "SUMMARY", 0)) && node
->value
) {
592 c
->c_text
= mh_xstrdup (node
->value
);
596 /* Only display DESCRIPTION lines that are outside VALARM section(s). */
598 if ((c
= fmt_findcomp ("description"))) {
599 for (node
= clines
; node
; node
= node
->next
) {
600 /* node->name will be NULL if the line was deleted. */
601 if (node
->name
&& node
->value
&& ! in_valarm
&&
602 ! strcasecmp ("DESCRIPTION", node
->name
) &&
603 strcasecmp (node
->value
, "\\n\\n")) {
604 c
->c_text
= mh_xstrdup (node
->value
);
605 } else if (in_valarm
) {
606 if (! strcasecmp ("END", node
->name
) &&
607 ! strcasecmp ("VALARM", node
->value
)) {
611 if (! strcasecmp ("BEGIN", node
->name
) &&
612 ! strcasecmp ("VALARM", node
->value
)) {
619 if ((c
= fmt_findcomp ("location"))) {
620 if ((node
= find_contentline (clines
, "LOCATION", 0)) &&
622 c
->c_text
= mh_xstrdup (node
->value
);
626 if ((c
= fmt_findcomp ("dtstart"))) {
627 /* Find DTSTART outsize of a VTIMEZONE section. */
629 for (node
= clines
; node
; node
= node
->next
) {
630 /* node->name will be NULL if the line was deleted. */
631 if (! node
->name
) { continue; }
634 if (! strcasecmp ("END", node
->name
) &&
635 ! strcasecmp ("VTIMEZONE", node
->value
)) {
639 if (! strcasecmp ("BEGIN", node
->name
) &&
640 ! strcasecmp ("VTIMEZONE", node
->value
)) {
642 } else if (! strcasecmp ("DTSTART", node
->name
)) {
643 /* Got it: DTSTART outside of a VTIMEZONE section. */
644 char *datetime
= format_datetime (timezones
, node
);
645 c
->c_text
= datetime
? datetime
: mh_xstrdup(node
->value
);
651 if ((c
= fmt_findcomp ("dtend"))) {
652 if ((node
= find_contentline (clines
, "DTEND", 0)) && node
->value
) {
653 char *datetime
= format_datetime (timezones
, node
);
654 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
655 } else if ((node
= find_contentline (clines
, "DTSTART", 0)) &&
657 /* There is no DTEND. If there's a DTSTART, use it. If it
658 doesn't have a time, assume that the event is for the
659 entire day and append 23:59:59 to it so that it signifies
660 the end of the day. And assume local timezone. */
661 if (strchr(node
->value
, 'T')) {
662 char * datetime
= format_datetime (timezones
, node
);
663 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
666 contentline node_copy
;
669 node_copy
.value
= concat(node_copy
.value
, "T235959", NULL
);
670 datetime
= format_datetime (timezones
, &node_copy
);
671 c
->c_text
= datetime
? datetime
: strdup(node_copy
.value
);
672 free(node_copy
.value
);
677 if ((c
= fmt_findcomp ("attendees"))) {
678 /* Call find_contentline () with node as argument to find multiple
679 matching contentlines. */
680 charstring_append_cstring (attendees
, "Attendees: ");
681 for (node
= clines
, num_attendees
= 0;
682 (node
= find_contentline (node
, "ATTENDEE", 0)) &&
683 num_attendees
++ < max_attendees
;
685 const char *id
= identity (node
);
687 if (num_attendees
> 1) {
688 charstring_append_cstring (attendees
, ", ");
690 charstring_append_cstring (attendees
, id
);
693 if (num_attendees
>= max_attendees
) {
694 unsigned int not_shown
= 0;
697 (node
= find_contentline (node
, "ATTENDEE", 0));
705 (void) snprintf (buf
, sizeof buf
, ", and %d more", not_shown
);
706 charstring_append_cstring (attendees
, buf
);
710 if (num_attendees
> 0) {
711 c
->c_text
= charstring_buffer_copy (attendees
);
715 /* Don't call on the END:VCALENDAR line. */
717 (void) fmt_scan (fmt
, buffer
, INT_MAX
, dat
, NULL
);
718 fputs (charstring_buffer (buffer
), file
);
722 charstring_free (attendees
);
723 charstring_free (buffer
);
724 free_timezones (timezones
);
728 identity (const contentline
*node
) {
729 /* According to RFC 5545 § 3.3.3, an email address in the value
730 must be a mailto URI. */
731 if (! strncasecmp (node
->value
, "mailto:", 7)) {
735 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
738 for (v
= p
->values
; v
; v
= v
->next
) {
739 if (! strcasecmp (p
->param_name
, "CN")) {
745 /* Did not find a CN parameter, so output the address. */
746 addr
= node
->value
+ 7;
748 /* Skip any leading whitespace. */
749 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
758 format_params (char *line
, param_list
*p
) {
759 for ( ; p
&& p
->param_name
; p
= p
->next
) {
761 size_t num_values
= 0;
763 for (v
= p
->values
; v
; v
= v
->next
) {
764 if (v
->value
) { ++num_values
; }
768 size_t len
= strlen (line
);
770 line
= mh_xrealloc (line
, len
+ 2);
772 line
[len
+ 1] = '\0';
774 line
= add (p
->param_name
, line
);
776 for (v
= p
->values
; v
; v
= v
->next
) {
778 line
= mh_xrealloc (line
, len
+ 2);
779 line
[len
] = v
== p
->values
? '=' : ',';
780 line
[len
+ 1] = '\0';
782 line
= add (v
->value
, line
);
791 fold (char *line
, int uses_cr
) {
792 size_t remaining
= strlen (line
);
793 size_t current_line_len
= 0;
794 charstring_t folded_line
= charstring_create (2 * remaining
);
795 const char *cp
= line
;
797 #ifdef MULTIBYTE_SUPPORT
798 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
801 while (*cp
&& remaining
> 0) {
802 #ifdef MULTIBYTE_SUPPORT
803 int char_len
= mbtowc (NULL
, cp
, (size_t) MB_CUR_MAX
< remaining
804 ? (size_t) MB_CUR_MAX
806 if (char_len
== -1) { char_len
= 1; }
808 const int char_len
= 1;
811 charstring_push_back_chars (folded_line
, cp
, char_len
, 1);
812 remaining
-= max(char_len
, 1);
814 /* remaining must be > 0 to pass the loop condition above, so
815 if it's not > 1, it is == 1. */
816 if (++current_line_len
>= 75) {
817 if (remaining
> 1 || (*(cp
+1) != '\0' && *(cp
+1) != '\r' &&
820 if (uses_cr
) { charstring_push_back (folded_line
, '\r'); }
821 charstring_push_back (folded_line
, '\n');
822 charstring_push_back (folded_line
, ' ');
823 current_line_len
= 0;
827 cp
+= max(char_len
, 1);
831 line
= charstring_buffer_copy (folded_line
);
832 charstring_free (folded_line
);