]> diplodocus.org Git - nmh/blob - uip/mhical.c
Mention inc(1)/msgchk(1) changes
[nmh] / uip / mhical.c
1 /* mhical.c -- operate on an iCalendar request
2 *
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.
6 */
7
8 #include "h/mh.h"
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"
14 #include "sbr/path.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"
23 #include "h/mts.h"
24 #include "h/done.h"
25 #include "h/utils.h"
26 #include <time.h>
27
28 typedef enum act {
29 ACT_NONE,
30 ACT_ACCEPT,
31 ACE_DECLINE,
32 ACT_TENTATIVE,
33 ACT_DELEGATE,
34 ACT_CANCEL
35 } act;
36
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);
46
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) \
61
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(MHICAL);
64 #undef X
65
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(MHICAL, switches);
68 #undef X
69
70 vevent vevents = { NULL, NULL, NULL};
71 int parser_status = 0;
72
73 int
74 main (int argc, char *argv[])
75 {
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. */
81
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;
87 bool unfold = false;
88 vevent *v, *nextvevent;
89 char *form = "mhical.24hour", *format = NULL;
90 char **argp, **arguments, *cp;
91
92 icaldebug = 0; /* Global provided by bison (with name-prefix "ical"). */
93
94 if (nmh_init(argv[0], true, false)) { return 1; }
95
96 arguments = getarguments (invo_name, argc, argv, 1);
97 argp = arguments;
98
99 /*
100 * Parse arguments
101 */
102 while ((cp = *argp++)) {
103 if (*cp == '-') {
104 switch (smatch (++cp, switches)) {
105 case AMBIGSW:
106 ambigsw (cp, switches);
107 done (1);
108 case UNKWNSW:
109 die("-%s unknown", cp);
110
111 case HELPSW: {
112 char buf[128];
113 snprintf (buf, sizeof buf, "%s [switches]", invo_name);
114 print_help (buf, switches, 1);
115 done (0);
116 }
117 case VERSIONSW:
118 print_version(invo_name);
119 done (0);
120 case DEBUGSW:
121 icaldebug = 1;
122 continue;
123
124 case REPLYSW:
125 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
126 die("missing argument to %s", argp[-2]);
127 if (! strcasecmp (cp, "accept")) {
128 action = ACT_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;
135 } else {
136 die("Unknown action: %s", cp);
137 }
138 continue;
139
140 case CANCELSW:
141 action = ACT_CANCEL;
142 continue;
143
144 case FORMSW:
145 if (! (form = *argp++) || *form == '-')
146 die("missing argument to %s", argp[-2]);
147 format = NULL;
148 continue;
149 case FMTSW:
150 if (! (format = *argp++) || *format == '-')
151 die("missing argument to %s", argp[-2]);
152 form = NULL;
153 continue;
154
155 case INFILESW:
156 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
157 die("missing argument to %s", argp[-2]);
158 infile = *cp == '-' ? mh_xstrdup(cp) : path (cp, TFILE);
159 continue;
160 case OUTFILESW:
161 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
162 die("missing argument to %s", argp[-2]);
163 outfile = *cp == '-' ? mh_xstrdup(cp) : path (cp, TFILE);
164 continue;
165
166 case ATTENDEESW:
167 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
168 die("missing argument to %s", argp[-2]);
169 attendee = cp;
170 continue;
171
172 case CONTENTTYPESW:
173 contenttype = true;
174 continue;
175 case NCONTENTTYPESW:
176 contenttype = false;
177 continue;
178
179 case UNFOLDSW:
180 unfold = true;
181 continue;
182 }
183 }
184 }
185
186 free (arguments);
187
188 if (infile) {
189 if ((inputfile = fopen (infile, "r"))) {
190 icalset_inputfile (inputfile);
191 } else {
192 adios (infile, "error opening");
193 }
194 } else {
195 inputfile = stdin;
196 }
197
198 if (outfile) {
199 if ((outputfile = fopen (outfile, "w"))) {
200 icalset_outputfile (outputfile);
201 } else {
202 adios (outfile, "error opening");
203 }
204 } else {
205 outputfile = stdout;
206 }
207
208 vevents.last = &vevents;
209 /* vevents is accessed by parser as global. */
210 icalparse ();
211
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);
221 }
222
223 if (action == ACT_NONE) {
224 if (unfold) {
225 dump_unfolded (outputfile, v->contentlines);
226 } else {
227 char *nfs = new_fs (form, format, NULL);
228
229 display (outputfile, v->contentlines, nfs);
230 free_fs ();
231 }
232 } else {
233 if (action == ACT_CANCEL) {
234 convert_to_cancellation (v->contentlines);
235 } else {
236 parser_status +=
237 convert_to_reply (v->contentlines, action, attendee);
238 }
239 if (parser_status == 0) {
240 output (outputfile, v->contentlines, contenttype);
241 }
242 }
243
244 free_contentlines (v->contentlines);
245 nextvevent = v->next;
246 if (v != &vevents) {
247 free (v);
248 }
249 }
250
251 if (infile) {
252 if (fclose (inputfile) != 0) {
253 advise (infile, "error closing");
254 }
255 free (infile);
256 }
257 if (outfile) {
258 if (fclose (outputfile) != 0) {
259 advise (outfile, "error closing");
260 }
261 free (outfile);
262 }
263
264 return parser_status > 0 ? 1 : 0;
265 }
266
267 /*
268 * - Change METHOD from REQUEST to REPLY.
269 * - Change PRODID.
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.
281 */
282 static int
283 convert_to_reply (contentline *clines, act action, char *attendee)
284 {
285 char *partstat = NULL;
286 unsigned int found_my_attendee_line = 0;
287 contentline *node;
288
289 convert_common (clines, action);
290
291 switch (action) {
292 case ACT_ACCEPT:
293 partstat = "ACCEPTED";
294 break;
295 case ACE_DECLINE:
296 partstat = "DECLINED";
297 break;
298 case ACT_TENTATIVE:
299 partstat = "TENTATIVE";
300 break;
301 default:
302 ;
303 }
304
305 /* Call find_contentline () with node as argument to find multiple
306 matching contentlines. */
307 for (node = clines;
308 (node = find_contentline (node, "ATTENDEE", 0));
309 node = node->next) {
310 param_list *p;
311
312 ismymbox (NULL); /* need to prime ismymbox() */
313
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;
318 struct mailname *mn;
319
320 /* Skip any leading whitespace. */
321 for ( ; isspace ((unsigned char) *addr); ++addr) { continue; }
322
323 addr = getname (addr);
324 mn = getm (addr, NULL, 0, NULL, 0);
325
326 /* Need to flush getname after use. */
327 while (getname ("")) { continue; }
328
329 if (ismymbox (mn) &&
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) {
333 value_list *v;
334
335 for (v = p->values; v; v = v->next) {
336 if (! strcasecmp (p->param_name, "ROLE") ||
337 ! strcasecmp (p->param_name, "RSVP")) {
338 remove_value (v);
339 } else if (! strcasecmp (p->param_name, "PARTSTAT")) {
340 free (v->value);
341 v->value = strdup (partstat);
342 }
343 }
344 }
345 } else {
346 remove_contentline (node);
347 }
348
349 mnfree (mn);
350 }
351 }
352
353 if (found_my_attendee_line == 0) {
354 /* Generate and attach an ATTENDEE line for me. */
355 contentline *node;
356
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");
361
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);
367 }
368 } else if (found_my_attendee_line > 1) {
369 inform("Multiple attendees match your address, "
370 "re-run with -attendee switch");
371 return 1;
372 }
373
374 return 0;
375 }
376
377 /*
378 * - Change METHOD from REQUEST to CANCEL.
379 * - Change PRODID.
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.
386 */
387 static void
388 convert_to_cancellation (contentline *clines)
389 {
390 contentline *node;
391
392 convert_common (clines, ACT_CANCEL);
393
394 if ((node = find_contentline (clines, "STATUS", 0)) &&
395 ! strcasecmp (node->value, "CONFIRMED")) {
396 free (node->value);
397 node->value = mh_xstrdup ("CANCELLED");
398 }
399
400 if ((node = find_contentline (clines, "SEQUENCE", 0))) {
401 int sequence = atoi (node->value);
402 char buf[32];
403
404 (void) snprintf (buf, sizeof buf, "%d", sequence + 1);
405 free (node->value);
406 node->value = mh_xstrdup (buf);
407 }
408 }
409
410 static void
411 convert_common (contentline *clines, act action)
412 {
413 contentline *node;
414 bool in_valarm;
415
416 if ((node = find_contentline (clines, "METHOD", 0))) {
417 free (node->value);
418 node->value = mh_xstrdup (action == ACT_CANCEL ? "CANCEL" : "REPLY");
419 }
420
421 if ((node = find_contentline (clines, "PRODID", 0))) {
422 free (node->value);
423 node->value = mh_xstrdup ("nmh mhical v0.5");
424 }
425
426 if ((node = find_contentline (clines, "VERSION", 0))) {
427 if (! node->value) {
428 inform("Version property is missing value, assume 2.0, continuing...");
429 node->value = mh_xstrdup ("2.0");
430 }
431
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...",
435 node->value);
436 node->value = mh_xstrdup ("2.0");
437 }
438 }
439
440 if ((node = find_contentline (clines, "SUMMARY", 0))) {
441 char *insert = NULL;
442
443 switch (action) {
444 case ACT_ACCEPT:
445 insert = "Accepted: ";
446 break;
447 case ACE_DECLINE:
448 insert = "Declined: ";
449 break;
450 case ACT_TENTATIVE:
451 insert = "Tentative: ";
452 break;
453 case ACT_DELEGATE:
454 die("Delegate replies are not supported");
455 break;
456 case ACT_CANCEL:
457 insert = "Cancelled:";
458 break;
459 default:
460 ;
461 }
462
463 if (insert) {
464 const size_t len = strlen (insert) + strlen (node->value) + 1;
465 char *tmp = mh_xmalloc (len);
466
467 (void) strncpy (tmp, insert, len);
468 (void) strncat (tmp, node->value, len - strlen (insert) - 1);
469 free (node->value);
470 node->value = tmp;
471 } else {
472 /* Should never get here. */
473 die("Unknown action: %d", action);
474 }
475 }
476
477 if ((node = find_contentline (clines, "DTSTAMP", 0))) {
478 const time_t now = time (NULL);
479 struct tm now_tm;
480
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. */
484 char buf[32];
485
486 if (strftime (buf, sizeof buf, "%Y%m%dT%H%M%SZ", &now_tm)) {
487 free (node->value);
488 node->value = mh_xstrdup (buf);
489 } else {
490 inform("strftime unable to format current time, continuing...");
491 }
492 } else {
493 inform("gmtime_r failed on current time, continuing...");
494 }
495 }
496
497 /* Excise X- lines and VALARM section(s). */
498 in_valarm = false;
499 for (node = clines; node; node = node->next) {
500 /* node->name will be NULL if the line was deleted. */
501 if (! node->name) { continue; }
502
503 if (in_valarm) {
504 if (! strcasecmp ("END", node->name) &&
505 ! strcasecmp ("VALARM", node->value)) {
506 in_valarm = false;
507 }
508 remove_contentline (node);
509 } else {
510 if (! strcasecmp ("BEGIN", node->name) &&
511 ! strcasecmp ("VALARM", node->value)) {
512 in_valarm = true;
513 remove_contentline (node);
514 } else if (! strncasecmp ("X-", node->name, 2)) {
515 remove_contentline (node);
516 }
517 }
518 }
519 }
520
521 /* Echo the input, but with unfolded lines. */
522 static void
523 dump_unfolded (FILE *file, contentline *clines)
524 {
525 contentline *node;
526
527 for (node = clines; node; node = node->next) {
528 fputs (node->input_line, file);
529 }
530 }
531
532 static void
533 output (FILE *file, contentline *clines, int contenttype)
534 {
535 contentline *node;
536
537 if (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
541 input. */
542 if ((node = find_contentline (clines, "METHOD", 0))) {
543 fprintf (file,
544 "Content-Type: text/calendar; method=\"%s\"; "
545 "charset=\"UTF-8\"\n\n",
546 node->value);
547 }
548 }
549
550 for (node = clines; node; node = node->next) {
551 if (node->name) {
552 char *line = NULL;
553 size_t len;
554
555 line = mh_xstrdup (node->name);
556 line = format_params (line, node->params);
557
558 len = strlen (line);
559 line = mh_xrealloc (line, len + 2);
560 line[len] = ':';
561 line[len + 1] = '\0';
562
563 line = fold (add (node->value, line),
564 clines->cr_before_lf == CR_BEFORE_LF);
565
566 fputs(line, file);
567 if (clines->cr_before_lf != LF_ONLY)
568 putc('\r', file);
569 putc('\n', file);
570 free (line);
571 }
572 }
573 }
574
575 /*
576 * Display these fields of the iCalendar event:
577 * - method
578 * - organizer
579 * - summary
580 * - description, except for "\n\n" and in VALARM
581 * - location
582 * - dtstart in local timezone
583 * - dtend in local timezone
584 * - attendees (limited to number specified in initialization)
585 */
586 static void
587 display (FILE *file, contentline *clines, char *nfs)
588 {
589 tzdesc_t timezones = load_timezones (clines);
590 bool in_vtimezone;
591 bool in_valarm;
592 contentline *node;
593 struct format *fmt;
594 int dat[5] = { 0, 0, 0, INT_MAX, 0 };
595 struct comp *c;
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;
600
601 /* Don't call on the END:VCALENDAR line. */
602 if (clines && clines->next) {
603 (void) fmt_compile (nfs, &fmt, 1);
604 }
605
606 if ((c = fmt_findcomp ("method"))) {
607 if ((node = find_contentline (clines, "METHOD", 0)) && node->value) {
608 c->c_text = mh_xstrdup (node->value);
609 }
610 }
611
612 if ((c = fmt_findcomp ("organizer"))) {
613 if ((node = find_contentline (clines, "ORGANIZER", 0)) &&
614 node->value) {
615 c->c_text = mh_xstrdup (identity (node));
616 }
617 }
618
619 if ((c = fmt_findcomp ("summary"))) {
620 if ((node = find_contentline (clines, "SUMMARY", 0)) && node->value) {
621 c->c_text = mh_xstrdup (node->value);
622 }
623 }
624
625 /* Only display DESCRIPTION lines that are outside VALARM section(s). */
626 in_valarm = false;
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)) {
637 in_valarm = false;
638 }
639 } else {
640 if (! strcasecmp ("BEGIN", node->name) &&
641 ! strcasecmp ("VALARM", node->value)) {
642 in_valarm = true;
643 }
644 }
645 }
646 }
647
648 if ((c = fmt_findcomp ("location"))) {
649 if ((node = find_contentline (clines, "LOCATION", 0)) &&
650 node->value) {
651 c->c_text = mh_xstrdup (node->value);
652 }
653 }
654
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; }
661
662 if (in_vtimezone) {
663 if (! strcasecmp ("END", node->name) &&
664 ! strcasecmp ("VTIMEZONE", node->value)) {
665 in_vtimezone = false;
666 }
667 } else {
668 if (! strcasecmp ("BEGIN", node->name) &&
669 ! strcasecmp ("VTIMEZONE", node->value)) {
670 in_vtimezone = true;
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);
675 }
676 }
677 }
678 }
679
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)) &&
685 node->value) {
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);
693 } else {
694 char *datetime;
695 contentline node_copy;
696
697 node_copy = *node;
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);
702 }
703 }
704 }
705
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;
713 node = node->next) {
714 const char *id = identity (node);
715
716 if (num_attendees > 1) {
717 charstring_append_cstring (attendees, ", ");
718 }
719 charstring_append_cstring (attendees, id);
720 }
721
722 if (num_attendees >= max_attendees) {
723 unsigned int not_shown = 0;
724
725 for ( ;
726 (node = find_contentline (node, "ATTENDEE", 0));
727 node = node->next) {
728 ++not_shown;
729 }
730
731 if (not_shown > 0) {
732 char buf[32];
733
734 (void) snprintf (buf, sizeof buf, ", and %d more", not_shown);
735 charstring_append_cstring (attendees, buf);
736 }
737 }
738
739 if (num_attendees > 0) {
740 c->c_text = charstring_buffer_copy (attendees);
741 }
742 }
743
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);
748 fmt_free (fmt, 1);
749 }
750
751 charstring_free (attendees);
752 charstring_free (buffer);
753 free_timezones (timezones);
754 }
755
756 static const char *
757 identity (const contentline *node)
758 {
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)) {
762 char *addr;
763 param_list *p;
764
765 for (p = node->params; p && p->param_name; p = p->next) {
766 value_list *v;
767
768 for (v = p->values; v; v = v->next) {
769 if (! strcasecmp (p->param_name, "CN")) {
770 return v->value;
771 }
772 }
773 }
774
775 /* Did not find a CN parameter, so output the address. */
776 addr = node->value + 7;
777
778 /* Skip any leading whitespace. */
779 for ( ; isspace ((unsigned char) *addr); ++addr) { continue; }
780
781 return addr;
782 }
783
784 return "unknown";
785 }
786
787 static char *
788 format_params (char *line, param_list *p)
789 {
790 for ( ; p && p->param_name; p = p->next) {
791 value_list *v;
792 size_t num_values = 0;
793
794 for (v = p->values; v; v = v->next) {
795 if (v->value) { ++num_values; }
796 }
797
798 if (num_values) {
799 size_t len = strlen (line);
800
801 line = mh_xrealloc (line, len + 2);
802 line[len] = ';';
803 line[len + 1] = '\0';
804
805 line = add (p->param_name, line);
806
807 for (v = p->values; v; v = v->next) {
808 len = strlen (line);
809 line = mh_xrealloc (line, len + 2);
810 line[len] = v == p->values ? '=' : ',';
811 line[len + 1] = '\0';
812
813 line = add (v->value, line);
814 }
815 }
816 }
817
818 return line;
819 }
820
821 static char *
822 fold (char *line, int uses_cr)
823 {
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;
828
829 #ifdef MULTIBYTE_SUPPORT
830 if (mbtowc (NULL, NULL, 0)) {} /* reset shift state */
831 #endif
832
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
837 : remaining);
838 if (char_len == -1) { char_len = 1; }
839 #else
840 const int char_len = 1;
841 #endif
842
843 charstring_push_back_chars (folded_line, cp, char_len, 1);
844 remaining -= max(char_len, 1);
845
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' &&
850 *(cp+1) != '\n')) {
851 /* fold */
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;
856 }
857 }
858
859 cp += max(char_len, 1);
860 }
861
862 free (line);
863 line = charstring_buffer_copy (folded_line);
864 charstring_free (folded_line);
865
866 return line;
867 }