]>
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 int convert_to_reply (contentline
*, act
, char *);
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("attendee", 0, ATTENDEESW) \
55 X("contenttype", 0, CONTENTTYPESW) \
56 X("nocontenttype", 0, NCONTENTTYPESW) \
57 X("unfold", 0, UNFOLDSW) \
58 X("debug", 0, DEBUGSW) \
59 X("version", 0, VERSIONSW) \
60 X("help", 0, HELPSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(MHICAL
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(MHICAL
, switches
);
70 vevent vevents
= { NULL
, NULL
, NULL
};
71 int parser_status
= 0;
74 main (int argc
, char *argv
[])
76 /* RFC 5322 § 3.3 date-time format, including the optional
77 day-of-week and not including the optional seconds. The
78 zone is required by the RFC but not always output by this
79 format, because RFC 5545 § 3.3.5 allows date-times not
80 bound to any time zone. */
82 act action
= ACT_NONE
;
83 char *infile
= NULL
, *outfile
= NULL
;
84 FILE *inputfile
= NULL
, *outputfile
= NULL
;
85 char *attendee
= NULL
;
86 bool contenttype
= false;
88 vevent
*v
, *nextvevent
;
89 char *form
= "mhical.24hour", *format
= NULL
;
90 char **argp
, **arguments
, *cp
;
92 icaldebug
= 0; /* Global provided by bison (with name-prefix "ical"). */
94 if (nmh_init(argv
[0], true, false)) { return 1; }
96 arguments
= getarguments (invo_name
, argc
, argv
, 1);
102 while ((cp
= *argp
++)) {
104 switch (smatch (++cp
, switches
)) {
106 ambigsw (cp
, switches
);
109 die("-%s unknown", cp
);
113 snprintf (buf
, sizeof buf
, "%s [switches]", invo_name
);
114 print_help (buf
, switches
, 1);
118 print_version(invo_name
);
125 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
126 die("missing argument to %s", argp
[-2]);
127 if (! strcasecmp (cp
, "accept")) {
129 } else if (! strcasecmp (cp
, "decline")) {
130 action
= ACE_DECLINE
;
131 } else if (! strcasecmp (cp
, "tentative")) {
132 action
= ACT_TENTATIVE
;
133 } else if (! strcasecmp (cp
, "delegate")) {
134 action
= ACT_DELEGATE
;
136 die("Unknown action: %s", cp
);
145 if (! (form
= *argp
++) || *form
== '-')
146 die("missing argument to %s", argp
[-2]);
150 if (! (format
= *argp
++) || *format
== '-')
151 die("missing argument to %s", argp
[-2]);
156 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
157 die("missing argument to %s", argp
[-2]);
158 infile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
161 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
162 die("missing argument to %s", argp
[-2]);
163 outfile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
167 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
168 die("missing argument to %s", argp
[-2]);
189 if ((inputfile
= fopen (infile
, "r"))) {
190 icalset_inputfile (inputfile
);
192 adios (infile
, "error opening");
199 if ((outputfile
= fopen (outfile
, "w"))) {
200 icalset_outputfile (outputfile
);
202 adios (outfile
, "error opening");
208 vevents
.last
= &vevents
;
209 /* vevents is accessed by parser as global. */
212 for (v
= &vevents
; v
; v
= nextvevent
) {
213 if (! unfold
&& v
!= &vevents
&& v
->contentlines
&&
214 v
->contentlines
->name
&&
215 strcasecmp (v
->contentlines
->name
, "END") &&
216 v
->contentlines
->value
&&
217 strcasecmp (v
->contentlines
->value
, "VCALENDAR")) {
218 /* Output blank line between vevents. Not before
219 first vevent and not after last. */
220 putc ('\n', outputfile
);
223 if (action
== ACT_NONE
) {
225 dump_unfolded (outputfile
, v
->contentlines
);
227 char *nfs
= new_fs (form
, format
, NULL
);
229 display (outputfile
, v
->contentlines
, nfs
);
233 if (action
== ACT_CANCEL
) {
234 convert_to_cancellation (v
->contentlines
);
237 convert_to_reply (v
->contentlines
, action
, attendee
);
239 if (parser_status
== 0) {
240 output (outputfile
, v
->contentlines
, contenttype
);
244 free_contentlines (v
->contentlines
);
245 nextvevent
= v
->next
;
252 if (fclose (inputfile
) != 0) {
253 advise (infile
, "error closing");
258 if (fclose (outputfile
) != 0) {
259 advise (outfile
, "error closing");
264 return parser_status
> 0 ? 1 : 0;
268 * - Change METHOD from REQUEST to REPLY.
270 * - Remove all ATTENDEE lines for other users (based on ismymbox()).
271 * If more than one address matches ismymbox(), the attendee argument,
272 * from the -attendee switch, must be used to select one.
273 * - For the user's ATTENDEE line:
274 * - Remove ROLE and RSVP parameters.
275 * - Change PARTSTAT value to indicate reply action, e.g., ACCEPTED,
276 * DECLINED, or TENTATIVE.
277 * - Insert action at beginning of SUMMARY value.
278 * - Remove all X- lines.
279 * - Update DTSTAMP with current timestamp.
280 * - Excise VALARM sections.
283 convert_to_reply (contentline
*clines
, act action
, char *attendee
)
285 char *partstat
= NULL
;
286 unsigned int found_my_attendee_line
= 0;
289 convert_common (clines
, action
);
293 partstat
= "ACCEPTED";
296 partstat
= "DECLINED";
299 partstat
= "TENTATIVE";
305 /* Call find_contentline () with node as argument to find multiple
306 matching contentlines. */
308 (node
= find_contentline (node
, "ATTENDEE", 0));
312 ismymbox (NULL
); /* need to prime ismymbox() */
314 /* According to RFC 5545 § 3.3.3, an email address in the
315 value must be a mailto URI. */
316 if (! strncasecmp (node
->value
, "mailto:", 7)) {
317 char *addr
= node
->value
+ 7;
320 /* Skip any leading whitespace. */
321 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
323 addr
= getname (addr
);
324 mn
= getm (addr
, NULL
, 0, NULL
, 0);
326 /* Need to flush getname after use. */
327 while (getname ("")) { continue; }
330 (attendee
== NULL
|| ! strcasecmp(mn
->m_text
, attendee
))) {
331 ++found_my_attendee_line
;
332 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
335 for (v
= p
->values
; v
; v
= v
->next
) {
336 if (! strcasecmp (p
->param_name
, "ROLE") ||
337 ! strcasecmp (p
->param_name
, "RSVP")) {
339 } else if (! strcasecmp (p
->param_name
, "PARTSTAT")) {
341 v
->value
= strdup (partstat
);
346 remove_contentline (node
);
353 if (found_my_attendee_line
== 0) {
354 /* Generate and attach an ATTENDEE line for me. */
357 /* Add it after the ORGANIZER line, or if none, BEGIN:VEVENT line. */
358 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) ||
359 (node
= find_contentline (clines
, "BEGIN", "VEVENT"))) {
360 contentline
*new_node
= add_contentline (node
, "ATTENDEE");
362 add_param_name (new_node
, mh_xstrdup ("PARTSTAT"));
363 add_param_value (new_node
, mh_xstrdup (partstat
));
364 add_param_name (new_node
, mh_xstrdup ("CN"));
365 add_param_value (new_node
, mh_xstrdup (getfullname ()));
366 new_node
->value
= concat ("MAILTO:", getlocalmbox (), NULL
);
368 } else if (found_my_attendee_line
> 1) {
369 inform("Multiple attendees match your address, "
370 "re-run with -attendee switch");
378 * - Change METHOD from REQUEST to CANCEL.
380 * - Insert action at beginning of SUMMARY value.
381 * - Remove all X- lines.
382 * - Update DTSTAMP with current timestamp.
383 * - Change STATUS from CONFIRMED to CANCELLED.
384 * - Increment value of SEQUENCE.
385 * - Excise VALARM sections.
388 convert_to_cancellation (contentline
*clines
)
392 convert_common (clines
, ACT_CANCEL
);
394 if ((node
= find_contentline (clines
, "STATUS", 0)) &&
395 ! strcasecmp (node
->value
, "CONFIRMED")) {
397 node
->value
= mh_xstrdup ("CANCELLED");
400 if ((node
= find_contentline (clines
, "SEQUENCE", 0))) {
401 int sequence
= atoi (node
->value
);
404 (void) snprintf (buf
, sizeof buf
, "%d", sequence
+ 1);
406 node
->value
= mh_xstrdup (buf
);
411 convert_common (contentline
*clines
, act action
)
416 if ((node
= find_contentline (clines
, "METHOD", 0))) {
418 node
->value
= mh_xstrdup (action
== ACT_CANCEL
? "CANCEL" : "REPLY");
421 if ((node
= find_contentline (clines
, "PRODID", 0))) {
423 node
->value
= mh_xstrdup ("nmh mhical v0.5");
426 if ((node
= find_contentline (clines
, "VERSION", 0))) {
428 inform("Version property is missing value, assume 2.0, continuing...");
429 node
->value
= mh_xstrdup ("2.0");
432 if (strcmp (node
->value
, "2.0")) {
433 inform("supports the Version 2.0 specified by RFC 5545 "
434 "but iCalendar object has Version %s, continuing...",
436 node
->value
= mh_xstrdup ("2.0");
440 if ((node
= find_contentline (clines
, "SUMMARY", 0))) {
445 insert
= "Accepted: ";
448 insert
= "Declined: ";
451 insert
= "Tentative: ";
454 die("Delegate replies are not supported");
457 insert
= "Cancelled:";
464 const size_t len
= strlen (insert
) + strlen (node
->value
) + 1;
465 char *tmp
= mh_xmalloc (len
);
467 (void) strncpy (tmp
, insert
, len
);
468 (void) strncat (tmp
, node
->value
, len
- strlen (insert
) - 1);
472 /* Should never get here. */
473 die("Unknown action: %d", action
);
477 if ((node
= find_contentline (clines
, "DTSTAMP", 0))) {
478 const time_t now
= time (NULL
);
481 if (gmtime_r (&now
, &now_tm
)) {
482 /* 17 would be sufficient given that RFC 5545 § 3.3.4
483 supports only a 4 digit year. */
486 if (strftime (buf
, sizeof buf
, "%Y%m%dT%H%M%SZ", &now_tm
)) {
488 node
->value
= mh_xstrdup (buf
);
490 inform("strftime unable to format current time, continuing...");
493 inform("gmtime_r failed on current time, continuing...");
497 /* Excise X- lines and VALARM section(s). */
499 for (node
= clines
; node
; node
= node
->next
) {
500 /* node->name will be NULL if the line was deleted. */
501 if (! node
->name
) { continue; }
504 if (! strcasecmp ("END", node
->name
) &&
505 ! strcasecmp ("VALARM", node
->value
)) {
508 remove_contentline (node
);
510 if (! strcasecmp ("BEGIN", node
->name
) &&
511 ! strcasecmp ("VALARM", node
->value
)) {
513 remove_contentline (node
);
514 } else if (! strncasecmp ("X-", node
->name
, 2)) {
515 remove_contentline (node
);
521 /* Echo the input, but with unfolded lines. */
523 dump_unfolded (FILE *file
, contentline
*clines
)
527 for (node
= clines
; node
; node
= node
->next
) {
528 fputs (node
->input_line
, file
);
533 output (FILE *file
, contentline
*clines
, int contenttype
)
538 /* Generate a Content-Type header to pass the method parameter
539 to mhbuild. Per RFC 5545 Secs. 6 and 8.1, it must be
540 UTF-8. But we don't attempt to do any conversion of the
542 if ((node
= find_contentline (clines
, "METHOD", 0))) {
544 "Content-Type: text/calendar; method=\"%s\"; "
545 "charset=\"UTF-8\"\n\n",
550 for (node
= clines
; node
; node
= node
->next
) {
555 line
= mh_xstrdup (node
->name
);
556 line
= format_params (line
, node
->params
);
559 line
= mh_xrealloc (line
, len
+ 2);
561 line
[len
+ 1] = '\0';
563 line
= fold (add (node
->value
, line
),
564 clines
->cr_before_lf
== CR_BEFORE_LF
);
567 if (clines
->cr_before_lf
!= LF_ONLY
)
576 * Display these fields of the iCalendar event:
580 * - description, except for "\n\n" and in VALARM
582 * - dtstart in local timezone
583 * - dtend in local timezone
584 * - attendees (limited to number specified in initialization)
587 display (FILE *file
, contentline
*clines
, char *nfs
)
589 tzdesc_t timezones
= load_timezones (clines
);
594 int dat
[5] = { 0, 0, 0, INT_MAX
, 0 };
596 charstring_t buffer
= charstring_create (BUFSIZ
);
597 charstring_t attendees
= charstring_create (BUFSIZ
);
598 const unsigned int max_attendees
= 20;
599 unsigned int num_attendees
;
601 /* Don't call on the END:VCALENDAR line. */
602 if (clines
&& clines
->next
) {
603 (void) fmt_compile (nfs
, &fmt
, 1);
606 if ((c
= fmt_findcomp ("method"))) {
607 if ((node
= find_contentline (clines
, "METHOD", 0)) && node
->value
) {
608 c
->c_text
= mh_xstrdup (node
->value
);
612 if ((c
= fmt_findcomp ("organizer"))) {
613 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) &&
615 c
->c_text
= mh_xstrdup (identity (node
));
619 if ((c
= fmt_findcomp ("summary"))) {
620 if ((node
= find_contentline (clines
, "SUMMARY", 0)) && node
->value
) {
621 c
->c_text
= mh_xstrdup (node
->value
);
625 /* Only display DESCRIPTION lines that are outside VALARM section(s). */
627 if ((c
= fmt_findcomp ("description"))) {
628 for (node
= clines
; node
; node
= node
->next
) {
629 /* node->name will be NULL if the line was deleted. */
630 if (node
->name
&& node
->value
&& ! in_valarm
&&
631 ! strcasecmp ("DESCRIPTION", node
->name
) &&
632 strcasecmp (node
->value
, "\\n\\n")) {
633 c
->c_text
= mh_xstrdup (node
->value
);
634 } else if (in_valarm
) {
635 if (! strcasecmp ("END", node
->name
) &&
636 ! strcasecmp ("VALARM", node
->value
)) {
640 if (! strcasecmp ("BEGIN", node
->name
) &&
641 ! strcasecmp ("VALARM", node
->value
)) {
648 if ((c
= fmt_findcomp ("location"))) {
649 if ((node
= find_contentline (clines
, "LOCATION", 0)) &&
651 c
->c_text
= mh_xstrdup (node
->value
);
655 if ((c
= fmt_findcomp ("dtstart"))) {
656 /* Find DTSTART outsize of a VTIMEZONE section. */
657 in_vtimezone
= false;
658 for (node
= clines
; node
; node
= node
->next
) {
659 /* node->name will be NULL if the line was deleted. */
660 if (! node
->name
) { continue; }
663 if (! strcasecmp ("END", node
->name
) &&
664 ! strcasecmp ("VTIMEZONE", node
->value
)) {
665 in_vtimezone
= false;
668 if (! strcasecmp ("BEGIN", node
->name
) &&
669 ! strcasecmp ("VTIMEZONE", node
->value
)) {
671 } else if (! strcasecmp ("DTSTART", node
->name
)) {
672 /* Got it: DTSTART outside of a VTIMEZONE section. */
673 char *datetime
= format_datetime (timezones
, node
);
674 c
->c_text
= datetime
? datetime
: mh_xstrdup(node
->value
);
680 if ((c
= fmt_findcomp ("dtend"))) {
681 if ((node
= find_contentline (clines
, "DTEND", 0)) && node
->value
) {
682 char *datetime
= format_datetime (timezones
, node
);
683 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
684 } else if ((node
= find_contentline (clines
, "DTSTART", 0)) &&
686 /* There is no DTEND. If there's a DTSTART, use it. If it
687 doesn't have a time, assume that the event is for the
688 entire day and append 23:59:59 to it so that it signifies
689 the end of the day. And assume local timezone. */
690 if (strchr(node
->value
, 'T')) {
691 char * datetime
= format_datetime (timezones
, node
);
692 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
695 contentline node_copy
;
698 node_copy
.value
= concat(node_copy
.value
, "T235959", NULL
);
699 datetime
= format_datetime (timezones
, &node_copy
);
700 c
->c_text
= datetime
? datetime
: strdup(node_copy
.value
);
701 free(node_copy
.value
);
706 if ((c
= fmt_findcomp ("attendees"))) {
707 /* Call find_contentline () with node as argument to find multiple
708 matching contentlines. */
709 charstring_append_cstring (attendees
, "Attendees: ");
710 for (node
= clines
, num_attendees
= 0;
711 (node
= find_contentline (node
, "ATTENDEE", 0)) &&
712 num_attendees
++ < max_attendees
;
714 const char *id
= identity (node
);
716 if (num_attendees
> 1) {
717 charstring_append_cstring (attendees
, ", ");
719 charstring_append_cstring (attendees
, id
);
722 if (num_attendees
>= max_attendees
) {
723 unsigned int not_shown
= 0;
726 (node
= find_contentline (node
, "ATTENDEE", 0));
734 (void) snprintf (buf
, sizeof buf
, ", and %d more", not_shown
);
735 charstring_append_cstring (attendees
, buf
);
739 if (num_attendees
> 0) {
740 c
->c_text
= charstring_buffer_copy (attendees
);
744 /* Don't call on the END:VCALENDAR line. */
745 if (clines
&& clines
->next
) {
746 (void) fmt_scan (fmt
, buffer
, INT_MAX
, dat
, NULL
);
747 fputs (charstring_buffer (buffer
), file
);
751 charstring_free (attendees
);
752 charstring_free (buffer
);
753 free_timezones (timezones
);
757 identity (const contentline
*node
)
759 /* According to RFC 5545 § 3.3.3, an email address in the value
760 must be a mailto URI. */
761 if (! strncasecmp (node
->value
, "mailto:", 7)) {
765 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
768 for (v
= p
->values
; v
; v
= v
->next
) {
769 if (! strcasecmp (p
->param_name
, "CN")) {
775 /* Did not find a CN parameter, so output the address. */
776 addr
= node
->value
+ 7;
778 /* Skip any leading whitespace. */
779 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
788 format_params (char *line
, param_list
*p
)
790 for ( ; p
&& p
->param_name
; p
= p
->next
) {
792 size_t num_values
= 0;
794 for (v
= p
->values
; v
; v
= v
->next
) {
795 if (v
->value
) { ++num_values
; }
799 size_t len
= strlen (line
);
801 line
= mh_xrealloc (line
, len
+ 2);
803 line
[len
+ 1] = '\0';
805 line
= add (p
->param_name
, line
);
807 for (v
= p
->values
; v
; v
= v
->next
) {
809 line
= mh_xrealloc (line
, len
+ 2);
810 line
[len
] = v
== p
->values
? '=' : ',';
811 line
[len
+ 1] = '\0';
813 line
= add (v
->value
, line
);
822 fold (char *line
, int uses_cr
)
824 size_t remaining
= strlen (line
);
825 size_t current_line_len
= 0;
826 charstring_t folded_line
= charstring_create (2 * remaining
);
827 const char *cp
= line
;
829 #ifdef MULTIBYTE_SUPPORT
830 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
833 while (*cp
&& remaining
> 0) {
834 #ifdef MULTIBYTE_SUPPORT
835 int char_len
= mbtowc (NULL
, cp
, (size_t) MB_CUR_MAX
< remaining
836 ? (size_t) MB_CUR_MAX
838 if (char_len
== -1) { char_len
= 1; }
840 const int char_len
= 1;
843 charstring_push_back_chars (folded_line
, cp
, char_len
, 1);
844 remaining
-= max(char_len
, 1);
846 /* remaining must be > 0 to pass the loop condition above, so
847 if it's not > 1, it is == 1. */
848 if (++current_line_len
>= 75) {
849 if (remaining
> 1 || (*(cp
+1) != '\0' && *(cp
+1) != '\r' &&
852 if (uses_cr
) { charstring_push_back (folded_line
, '\r'); }
853 charstring_push_back (folded_line
, '\n');
854 charstring_push_back (folded_line
, ' ');
855 current_line_len
= 0;
859 cp
+= max(char_len
, 1);
863 line
= charstring_buffer_copy (folded_line
);
864 charstring_free (folded_line
);