]>
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.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
13 #include "h/icalendar.h"
14 #include "sbr/icalparse.h"
15 #include "h/fmt_scan.h"
16 #include "h/addrsbr.h"
31 static void convert_to_reply (contentline
*, act
);
32 static void convert_to_cancellation (contentline
*);
33 static void convert_common (contentline
*, act
);
34 static void dump_unfolded (FILE *, contentline
*);
35 static void output (FILE *, contentline
*, int);
36 static void display (FILE *, contentline
*, char *);
37 static const char *identity (const contentline
*) PURE
;
38 static char *format_params (char *, param_list
*);
39 static char *fold (char *, int);
41 #define MHICAL_SWITCHES \
42 X("reply accept|decline|tentative", 0, REPLYSW) \
43 X("cancel", 0, CANCELSW) \
44 X("form formatfile", 0, FORMSW) \
45 X("format string", 5, FMTSW) \
46 X("infile", 0, INFILESW) \
47 X("outfile", 0, OUTFILESW) \
48 X("contenttype", 0, CONTENTTYPESW) \
49 X("nocontenttype", 0, NCONTENTTYPESW) \
50 X("unfold", 0, UNFOLDSW) \
51 X("debug", 0, DEBUGSW) \
52 X("version", 0, VERSIONSW) \
53 X("help", 0, HELPSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(MHICAL
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(MHICAL
, switches
);
63 vevent vevents
= { NULL
, NULL
, NULL
};
64 int parser_status
= 0;
67 main (int argc
, char *argv
[])
69 /* RFC 5322 § 3.3 date-time format, including the optional
70 day-of-week and not including the optional seconds. The
71 zone is required by the RFC but not always output by this
72 format, because RFC 5545 § 3.3.5 allows date-times not
73 bound to any time zone. */
75 act action
= ACT_NONE
;
76 char *infile
= NULL
, *outfile
= NULL
;
77 FILE *inputfile
= NULL
, *outputfile
= NULL
;
78 bool contenttype
= false;
80 vevent
*v
, *nextvevent
;
81 char *form
= "mhical.24hour", *format
= NULL
;
82 char **argp
, **arguments
, *cp
;
84 icaldebug
= 0; /* Global provided by bison (with name-prefix "ical"). */
86 if (nmh_init(argv
[0], true, false)) { return 1; }
88 arguments
= getarguments (invo_name
, argc
, argv
, 1);
94 while ((cp
= *argp
++)) {
96 switch (smatch (++cp
, switches
)) {
98 ambigsw (cp
, switches
);
101 die("-%s unknown", cp
);
105 snprintf (buf
, sizeof buf
, "%s [switches]", invo_name
);
106 print_help (buf
, switches
, 1);
110 print_version(invo_name
);
117 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
118 die("missing argument to %s", argp
[-2]);
119 if (! strcasecmp (cp
, "accept")) {
121 } else if (! strcasecmp (cp
, "decline")) {
122 action
= ACE_DECLINE
;
123 } else if (! strcasecmp (cp
, "tentative")) {
124 action
= ACT_TENTATIVE
;
125 } else if (! strcasecmp (cp
, "delegate")) {
126 action
= ACT_DELEGATE
;
128 die("Unknown action: %s", cp
);
137 if (! (form
= *argp
++) || *form
== '-')
138 die("missing argument to %s", argp
[-2]);
142 if (! (format
= *argp
++) || *format
== '-')
143 die("missing argument to %s", argp
[-2]);
148 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
149 die("missing argument to %s", argp
[-2]);
150 infile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
153 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
154 die("missing argument to %s", argp
[-2]);
155 outfile
= *cp
== '-' ? mh_xstrdup(cp
) : path (cp
, TFILE
);
175 if ((inputfile
= fopen (infile
, "r"))) {
176 icalset_inputfile (inputfile
);
178 adios (infile
, "error opening");
185 if ((outputfile
= fopen (outfile
, "w"))) {
186 icalset_outputfile (outputfile
);
188 adios (outfile
, "error opening");
194 vevents
.last
= &vevents
;
195 /* vevents is accessed by parser as global. */
198 for (v
= &vevents
; v
; v
= nextvevent
) {
199 if (! unfold
&& v
!= &vevents
&& v
->contentlines
&&
200 v
->contentlines
->name
&&
201 strcasecmp (v
->contentlines
->name
, "END") &&
202 v
->contentlines
->value
&&
203 strcasecmp (v
->contentlines
->value
, "VCALENDAR")) {
204 /* Output blank line between vevents. Not before
205 first vevent and not after last. */
206 putc ('\n', outputfile
);
209 if (action
== ACT_NONE
) {
211 dump_unfolded (outputfile
, v
->contentlines
);
213 char *nfs
= new_fs (form
, format
, NULL
);
215 display (outputfile
, v
->contentlines
, nfs
);
219 if (action
== ACT_CANCEL
) {
220 convert_to_cancellation (v
->contentlines
);
222 convert_to_reply (v
->contentlines
, action
);
224 output (outputfile
, v
->contentlines
, contenttype
);
227 free_contentlines (v
->contentlines
);
228 nextvevent
= v
->next
;
235 if (fclose (inputfile
) != 0) {
236 advise (infile
, "error closing");
241 if (fclose (outputfile
) != 0) {
242 advise (outfile
, "error closing");
247 return parser_status
;
251 * - Change METHOD from REQUEST to REPLY.
253 * - Remove all ATTENDEE lines for other users (based on ismymbox ()).
254 * - For the user's ATTENDEE line:
255 * - Remove ROLE and RSVP parameters.
256 * - Change PARTSTAT value to indicate reply action, e.g., ACCEPTED,
257 * DECLINED, or TENTATIVE.
258 * - Insert action at beginning of SUMMARY value.
259 * - Remove all X- lines.
260 * - Update DTSTAMP with current timestamp.
261 * - Remove all DESCRIPTION lines.
262 * - Excise VALARM sections.
265 convert_to_reply (contentline
*clines
, act action
)
267 char *partstat
= NULL
;
268 bool found_my_attendee_line
= false;
271 convert_common (clines
, action
);
275 partstat
= "ACCEPTED";
278 partstat
= "DECLINED";
281 partstat
= "TENTATIVE";
287 /* Call find_contentline () with node as argument to find multiple
288 matching contentlines. */
290 (node
= find_contentline (node
, "ATTENDEE", 0));
294 ismymbox (NULL
); /* need to prime ismymbox() */
296 /* According to RFC 5545 § 3.3.3, an email address in the
297 value must be a mailto URI. */
298 if (! strncasecmp (node
->value
, "mailto:", 7)) {
299 char *addr
= node
->value
+ 7;
302 /* Skip any leading whitespace. */
303 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
305 addr
= getname (addr
);
306 mn
= getm (addr
, NULL
, 0, NULL
, 0);
308 /* Need to flush getname after use. */
309 while (getname ("")) { continue; }
312 found_my_attendee_line
= true;
313 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
316 for (v
= p
->values
; v
; v
= v
->next
) {
317 if (! strcasecmp (p
->param_name
, "ROLE") ||
318 ! strcasecmp (p
->param_name
, "RSVP")) {
320 } else if (! strcasecmp (p
->param_name
, "PARTSTAT")) {
322 v
->value
= strdup (partstat
);
327 remove_contentline (node
);
334 if (! found_my_attendee_line
) {
335 /* Generate and attach an ATTENDEE line for me. */
338 /* Add it after the ORGANIZER line, or if none, BEGIN:VEVENT line. */
339 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) ||
340 (node
= find_contentline (clines
, "BEGIN", "VEVENT"))) {
341 contentline
*new_node
= add_contentline (node
, "ATTENDEE");
343 add_param_name (new_node
, mh_xstrdup ("PARTSTAT"));
344 add_param_value (new_node
, mh_xstrdup (partstat
));
345 add_param_name (new_node
, mh_xstrdup ("CN"));
346 add_param_value (new_node
, mh_xstrdup (getfullname ()));
347 new_node
->value
= concat ("MAILTO:", getlocalmbox (), NULL
);
351 /* Call find_contentline () with node as argument to find multiple
352 matching contentlines. */
354 (node
= find_contentline (node
, "DESCRIPTION", 0));
356 /* ACCEPT, at least, replies don't seem to have DESCRIPTIONS. */
357 remove_contentline (node
);
362 * - Change METHOD from REQUEST to CANCEL.
364 * - Insert action at beginning of SUMMARY value.
365 * - Remove all X- lines.
366 * - Update DTSTAMP with current timestamp.
367 * - Change STATUS from CONFIRMED to CANCELLED.
368 * - Increment value of SEQUENCE.
369 * - Excise VALARM sections.
372 convert_to_cancellation (contentline
*clines
)
376 convert_common (clines
, ACT_CANCEL
);
378 if ((node
= find_contentline (clines
, "STATUS", 0)) &&
379 ! strcasecmp (node
->value
, "CONFIRMED")) {
381 node
->value
= mh_xstrdup ("CANCELLED");
384 if ((node
= find_contentline (clines
, "SEQUENCE", 0))) {
385 int sequence
= atoi (node
->value
);
388 (void) snprintf (buf
, sizeof buf
, "%d", sequence
+ 1);
390 node
->value
= mh_xstrdup (buf
);
395 convert_common (contentline
*clines
, act action
)
400 if ((node
= find_contentline (clines
, "METHOD", 0))) {
402 node
->value
= mh_xstrdup (action
== ACT_CANCEL
? "CANCEL" : "REPLY");
405 if ((node
= find_contentline (clines
, "PRODID", 0))) {
407 node
->value
= mh_xstrdup ("nmh mhical v0.1");
410 if ((node
= find_contentline (clines
, "VERSION", 0))) {
412 inform("Version property is missing value, assume 2.0, continuing...");
413 node
->value
= mh_xstrdup ("2.0");
416 if (strcmp (node
->value
, "2.0")) {
417 inform("supports the Version 2.0 specified by RFC 5545 "
418 "but iCalendar object has Version %s, continuing...",
420 node
->value
= mh_xstrdup ("2.0");
424 if ((node
= find_contentline (clines
, "SUMMARY", 0))) {
429 insert
= "Accepted: ";
432 insert
= "Declined: ";
435 insert
= "Tentative: ";
438 die("Delegate replies are not supported");
441 insert
= "Cancelled:";
448 const size_t len
= strlen (insert
) + strlen (node
->value
) + 1;
449 char *tmp
= mh_xmalloc (len
);
451 (void) strncpy (tmp
, insert
, len
);
452 (void) strncat (tmp
, node
->value
, len
- strlen (insert
) - 1);
456 /* Should never get here. */
457 die("Unknown action: %d", action
);
461 if ((node
= find_contentline (clines
, "DTSTAMP", 0))) {
462 const time_t now
= time (NULL
);
465 if (gmtime_r (&now
, &now_tm
)) {
466 /* 17 would be sufficient given that RFC 5545 § 3.3.4
467 supports only a 4 digit year. */
470 if (strftime (buf
, sizeof buf
, "%Y%m%dT%H%M%SZ", &now_tm
)) {
472 node
->value
= mh_xstrdup (buf
);
474 inform("strftime unable to format current time, continuing...");
477 inform("gmtime_r failed on current time, continuing...");
481 /* Excise X- lines and VALARM section(s). */
483 for (node
= clines
; node
; node
= node
->next
) {
484 /* node->name will be NULL if the line was deleted. */
485 if (! node
->name
) { continue; }
488 if (! strcasecmp ("END", node
->name
) &&
489 ! strcasecmp ("VALARM", node
->value
)) {
492 remove_contentline (node
);
494 if (! strcasecmp ("BEGIN", node
->name
) &&
495 ! strcasecmp ("VALARM", node
->value
)) {
497 remove_contentline (node
);
498 } else if (! strncasecmp ("X-", node
->name
, 2)) {
499 remove_contentline (node
);
505 /* Echo the input, but with unfolded lines. */
507 dump_unfolded (FILE *file
, contentline
*clines
)
511 for (node
= clines
; node
; node
= node
->next
) {
512 fputs (node
->input_line
, file
);
517 output (FILE *file
, contentline
*clines
, int contenttype
)
522 /* Generate a Content-Type header to pass the method parameter
523 to mhbuild. Per RFC 5545 Secs. 6 and 8.1, it must be
524 UTF-8. But we don't attempt to do any conversion of the
526 if ((node
= find_contentline (clines
, "METHOD", 0))) {
528 "Content-Type: text/calendar; method=\"%s\"; "
529 "charset=\"UTF-8\"\n\n",
534 for (node
= clines
; node
; node
= node
->next
) {
539 line
= mh_xstrdup (node
->name
);
540 line
= format_params (line
, node
->params
);
543 line
= mh_xrealloc (line
, len
+ 2);
545 line
[len
+ 1] = '\0';
547 line
= fold (add (node
->value
, line
),
548 clines
->cr_before_lf
== CR_BEFORE_LF
);
551 if (clines
->cr_before_lf
!= LF_ONLY
)
560 * Display these fields of the iCalendar event:
564 * - description, except for "\n\n" and in VALARM
566 * - dtstart in local timezone
567 * - dtend in local timezone
568 * - attendees (limited to number specified in initialization)
571 display (FILE *file
, contentline
*clines
, char *nfs
)
573 tzdesc_t timezones
= load_timezones (clines
);
578 int dat
[5] = { 0, 0, 0, INT_MAX
, 0 };
580 charstring_t buffer
= charstring_create (BUFSIZ
);
581 charstring_t attendees
= charstring_create (BUFSIZ
);
582 const unsigned int max_attendees
= 20;
583 unsigned int num_attendees
;
585 /* Don't call on the END:VCALENDAR line. */
586 if (clines
&& clines
->next
) {
587 (void) fmt_compile (nfs
, &fmt
, 1);
590 if ((c
= fmt_findcomp ("method"))) {
591 if ((node
= find_contentline (clines
, "METHOD", 0)) && node
->value
) {
592 c
->c_text
= mh_xstrdup (node
->value
);
596 if ((c
= fmt_findcomp ("organizer"))) {
597 if ((node
= find_contentline (clines
, "ORGANIZER", 0)) &&
599 c
->c_text
= mh_xstrdup (identity (node
));
603 if ((c
= fmt_findcomp ("summary"))) {
604 if ((node
= find_contentline (clines
, "SUMMARY", 0)) && node
->value
) {
605 c
->c_text
= mh_xstrdup (node
->value
);
609 /* Only display DESCRIPTION lines that are outside VALARM section(s). */
611 if ((c
= fmt_findcomp ("description"))) {
612 for (node
= clines
; node
; node
= node
->next
) {
613 /* node->name will be NULL if the line was deleted. */
614 if (node
->name
&& node
->value
&& ! in_valarm
&&
615 ! strcasecmp ("DESCRIPTION", node
->name
) &&
616 strcasecmp (node
->value
, "\\n\\n")) {
617 c
->c_text
= mh_xstrdup (node
->value
);
618 } else if (in_valarm
) {
619 if (! strcasecmp ("END", node
->name
) &&
620 ! strcasecmp ("VALARM", node
->value
)) {
624 if (! strcasecmp ("BEGIN", node
->name
) &&
625 ! strcasecmp ("VALARM", node
->value
)) {
632 if ((c
= fmt_findcomp ("location"))) {
633 if ((node
= find_contentline (clines
, "LOCATION", 0)) &&
635 c
->c_text
= mh_xstrdup (node
->value
);
639 if ((c
= fmt_findcomp ("dtstart"))) {
640 /* Find DTSTART outsize of a VTIMEZONE section. */
641 in_vtimezone
= false;
642 for (node
= clines
; node
; node
= node
->next
) {
643 /* node->name will be NULL if the line was deleted. */
644 if (! node
->name
) { continue; }
647 if (! strcasecmp ("END", node
->name
) &&
648 ! strcasecmp ("VTIMEZONE", node
->value
)) {
649 in_vtimezone
= false;
652 if (! strcasecmp ("BEGIN", node
->name
) &&
653 ! strcasecmp ("VTIMEZONE", node
->value
)) {
655 } else if (! strcasecmp ("DTSTART", node
->name
)) {
656 /* Got it: DTSTART outside of a VTIMEZONE section. */
657 char *datetime
= format_datetime (timezones
, node
);
658 c
->c_text
= datetime
? datetime
: mh_xstrdup(node
->value
);
664 if ((c
= fmt_findcomp ("dtend"))) {
665 if ((node
= find_contentline (clines
, "DTEND", 0)) && node
->value
) {
666 char *datetime
= format_datetime (timezones
, node
);
667 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
668 } else if ((node
= find_contentline (clines
, "DTSTART", 0)) &&
670 /* There is no DTEND. If there's a DTSTART, use it. If it
671 doesn't have a time, assume that the event is for the
672 entire day and append 23:59:59 to it so that it signifies
673 the end of the day. And assume local timezone. */
674 if (strchr(node
->value
, 'T')) {
675 char * datetime
= format_datetime (timezones
, node
);
676 c
->c_text
= datetime
? datetime
: strdup(node
->value
);
679 contentline node_copy
;
682 node_copy
.value
= concat(node_copy
.value
, "T235959", NULL
);
683 datetime
= format_datetime (timezones
, &node_copy
);
684 c
->c_text
= datetime
? datetime
: strdup(node_copy
.value
);
685 free(node_copy
.value
);
690 if ((c
= fmt_findcomp ("attendees"))) {
691 /* Call find_contentline () with node as argument to find multiple
692 matching contentlines. */
693 charstring_append_cstring (attendees
, "Attendees: ");
694 for (node
= clines
, num_attendees
= 0;
695 (node
= find_contentline (node
, "ATTENDEE", 0)) &&
696 num_attendees
++ < max_attendees
;
698 const char *id
= identity (node
);
700 if (num_attendees
> 1) {
701 charstring_append_cstring (attendees
, ", ");
703 charstring_append_cstring (attendees
, id
);
706 if (num_attendees
>= max_attendees
) {
707 unsigned int not_shown
= 0;
710 (node
= find_contentline (node
, "ATTENDEE", 0));
718 (void) snprintf (buf
, sizeof buf
, ", and %d more", not_shown
);
719 charstring_append_cstring (attendees
, buf
);
723 if (num_attendees
> 0) {
724 c
->c_text
= charstring_buffer_copy (attendees
);
728 /* Don't call on the END:VCALENDAR line. */
729 if (clines
&& clines
->next
) {
730 (void) fmt_scan (fmt
, buffer
, INT_MAX
, dat
, NULL
);
731 fputs (charstring_buffer (buffer
), file
);
735 charstring_free (attendees
);
736 charstring_free (buffer
);
737 free_timezones (timezones
);
741 identity (const contentline
*node
)
743 /* According to RFC 5545 § 3.3.3, an email address in the value
744 must be a mailto URI. */
745 if (! strncasecmp (node
->value
, "mailto:", 7)) {
749 for (p
= node
->params
; p
&& p
->param_name
; p
= p
->next
) {
752 for (v
= p
->values
; v
; v
= v
->next
) {
753 if (! strcasecmp (p
->param_name
, "CN")) {
759 /* Did not find a CN parameter, so output the address. */
760 addr
= node
->value
+ 7;
762 /* Skip any leading whitespace. */
763 for ( ; isspace ((unsigned char) *addr
); ++addr
) { continue; }
772 format_params (char *line
, param_list
*p
)
774 for ( ; p
&& p
->param_name
; p
= p
->next
) {
776 size_t num_values
= 0;
778 for (v
= p
->values
; v
; v
= v
->next
) {
779 if (v
->value
) { ++num_values
; }
783 size_t len
= strlen (line
);
785 line
= mh_xrealloc (line
, len
+ 2);
787 line
[len
+ 1] = '\0';
789 line
= add (p
->param_name
, line
);
791 for (v
= p
->values
; v
; v
= v
->next
) {
793 line
= mh_xrealloc (line
, len
+ 2);
794 line
[len
] = v
== p
->values
? '=' : ',';
795 line
[len
+ 1] = '\0';
797 line
= add (v
->value
, line
);
806 fold (char *line
, int uses_cr
)
808 size_t remaining
= strlen (line
);
809 size_t current_line_len
= 0;
810 charstring_t folded_line
= charstring_create (2 * remaining
);
811 const char *cp
= line
;
813 #ifdef MULTIBYTE_SUPPORT
814 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
817 while (*cp
&& remaining
> 0) {
818 #ifdef MULTIBYTE_SUPPORT
819 int char_len
= mbtowc (NULL
, cp
, (size_t) MB_CUR_MAX
< remaining
820 ? (size_t) MB_CUR_MAX
822 if (char_len
== -1) { char_len
= 1; }
824 const int char_len
= 1;
827 charstring_push_back_chars (folded_line
, cp
, char_len
, 1);
828 remaining
-= max(char_len
, 1);
830 /* remaining must be > 0 to pass the loop condition above, so
831 if it's not > 1, it is == 1. */
832 if (++current_line_len
>= 75) {
833 if (remaining
> 1 || (*(cp
+1) != '\0' && *(cp
+1) != '\r' &&
836 if (uses_cr
) { charstring_push_back (folded_line
, '\r'); }
837 charstring_push_back (folded_line
, '\n');
838 charstring_push_back (folded_line
, ' ');
839 current_line_len
= 0;
843 cp
+= max(char_len
, 1);
847 line
= charstring_buffer_copy (folded_line
);
848 charstring_free (folded_line
);