]> diplodocus.org Git - nmh/blob - uip/mhstoresbr.c
Added const to argument of getcpy().
[nmh] / uip / mhstoresbr.c
1
2 /*
3 * mhstoresbr.c -- routines to save/store the contents of MIME messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <h/mts.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/utils.h>
19
20
21 /*
22 * The list of top-level contents to display
23 */
24 extern CT *cts;
25
26 int autosw = 0;
27
28 /*
29 * Cache of current directory. This must be
30 * set before these routines are called.
31 */
32 char *cwd;
33
34 /*
35 * The directory in which to store the contents.
36 */
37 static char *dir;
38
39 /*
40 * Type for a compare function for qsort. This keeps
41 * the compiler happy.
42 */
43 typedef int (*qsort_comp) (const void *, const void *);
44
45
46 /* mhmisc.c */
47 int part_ok (CT, int);
48 int type_ok (CT, int);
49 void flush_errors (void);
50
51 /* mhshowsbr.c */
52 int show_content_aux (CT, int, int, char *, char *);
53
54 /*
55 * prototypes
56 */
57 void store_all_messages (CT *);
58
59 /*
60 * static prototypes
61 */
62 static void store_single_message (CT);
63 static int store_switch (CT);
64 static int store_generic (CT);
65 static int store_application (CT);
66 static int store_multi (CT);
67 static int store_partial (CT);
68 static int store_external (CT);
69 static int ct_compar (CT *, CT *);
70 static int store_content (CT, CT);
71 static int output_content_file (CT, int);
72 static int output_content_folder (char *, char *);
73 static int parse_format_string (CT, char *, char *, int, char *);
74 static void get_storeproc (CT);
75 static int copy_some_headers (FILE *, CT);
76 static char *clobber_check (char *);
77
78 /*
79 * Main entry point to store content
80 * from a collection of messages.
81 */
82
83 void
84 store_all_messages (CT *cts)
85 {
86 CT ct, *ctp;
87 char *cp;
88
89 /*
90 * Check for the directory in which to
91 * store any contents.
92 */
93 if ((cp = context_find (nmhstorage)) && *cp)
94 dir = getcpy (cp);
95 else
96 dir = getcpy (cwd);
97
98 for (ctp = cts; *ctp; ctp++) {
99 ct = *ctp;
100 store_single_message (ct);
101 }
102
103 flush_errors ();
104 }
105
106
107 /*
108 * Entry point to store the content
109 * in a (single) message
110 */
111
112 static void
113 store_single_message (CT ct)
114 {
115 if (type_ok (ct, 1)) {
116 umask (ct->c_umask);
117 store_switch (ct);
118 if (ct->c_fp) {
119 fclose (ct->c_fp);
120 ct->c_fp = NULL;
121 }
122 if (ct->c_ceclosefnx)
123 (*ct->c_ceclosefnx) (ct);
124 }
125 }
126
127
128 /*
129 * Switching routine to store different content types
130 */
131
132 static int
133 store_switch (CT ct)
134 {
135 switch (ct->c_type) {
136 case CT_MULTIPART:
137 return store_multi (ct);
138 break;
139
140 case CT_MESSAGE:
141 switch (ct->c_subtype) {
142 case MESSAGE_PARTIAL:
143 return store_partial (ct);
144 break;
145
146 case MESSAGE_EXTERNAL:
147 return store_external (ct);
148
149 case MESSAGE_RFC822:
150 default:
151 return store_generic (ct);
152 break;
153 }
154 break;
155
156 case CT_APPLICATION:
157 return store_application (ct);
158 break;
159
160 case CT_TEXT:
161 case CT_AUDIO:
162 case CT_IMAGE:
163 case CT_VIDEO:
164 return store_generic (ct);
165 break;
166
167 default:
168 adios (NULL, "unknown content type %d", ct->c_type);
169 break;
170 }
171
172 return OK; /* NOT REACHED */
173 }
174
175
176 /*
177 * Generic routine to store a MIME content.
178 * (audio, video, image, text, message/rfc822)
179 */
180
181 static int
182 store_generic (CT ct)
183 {
184 /*
185 * Check if the content specifies a filename.
186 * Don't bother with this for type "message"
187 * (only "message/rfc822" will use store_generic).
188 */
189 if (autosw && ct->c_type != CT_MESSAGE)
190 get_storeproc (ct);
191
192 return store_content (ct, NULL);
193 }
194
195
196 /*
197 * Store content of type "application"
198 */
199
200 static int
201 store_application (CT ct)
202 {
203 char **ap, **ep;
204 CI ci = &ct->c_ctinfo;
205
206 /* Check if the content specifies a filename */
207 if (autosw)
208 get_storeproc (ct);
209
210 /*
211 * If storeproc is not defined, and the content is type
212 * "application/octet-stream", we also check for various
213 * attribute/value pairs which specify if this a tar file.
214 */
215 if (!ct->c_storeproc && ct->c_subtype == APPLICATION_OCTETS) {
216 int tarP = 0, zP = 0, gzP = 0;
217
218 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
219 /* check for "type=tar" attribute */
220 if (!strcasecmp (*ap, "type")) {
221 if (strcasecmp (*ep, "tar"))
222 break;
223
224 tarP = 1;
225 continue;
226 }
227
228 /* check for "conversions=compress" attribute */
229 if ((!strcasecmp (*ap, "conversions") || !strcasecmp (*ap, "x-conversions"))
230 && (!strcasecmp (*ep, "compress") || !strcasecmp (*ep, "x-compress"))) {
231 zP = 1;
232 continue;
233 }
234 /* check for "conversions=gzip" attribute */
235 if ((!strcasecmp (*ap, "conversions") || !strcasecmp (*ap, "x-conversions"))
236 && (!strcasecmp (*ep, "gzip") || !strcasecmp (*ep, "x-gzip"))) {
237 gzP = 1;
238 continue;
239 }
240 }
241
242 if (tarP) {
243 ct->c_showproc = add (zP ? "%euncompress | tar tvf -"
244 : (gzP ? "%egzip -dc | tar tvf -"
245 : "%etar tvf -"), NULL);
246 if (!ct->c_storeproc) {
247 if (autosw) {
248 ct->c_storeproc = add (zP ? "| uncompress | tar xvpf -"
249 : (gzP ? "| gzip -dc | tar xvpf -"
250 : "| tar xvpf -"), NULL);
251 ct->c_umask = 0022;
252 } else {
253 ct->c_storeproc= add (zP ? "%m%P.tar.Z"
254 : (gzP ? "%m%P.tar.gz"
255 : "%m%P.tar"), NULL);
256 }
257 }
258 }
259 }
260
261 return store_content (ct, NULL);
262 }
263
264
265 /*
266 * Store the content of a multipart message
267 */
268
269 static int
270 store_multi (CT ct)
271 {
272 int result;
273 struct multipart *m = (struct multipart *) ct->c_ctparams;
274 struct part *part;
275
276 result = NOTOK;
277 for (part = m->mp_parts; part; part = part->mp_next) {
278 CT p = part->mp_part;
279
280 if (part_ok (p, 1) && type_ok (p, 1)) {
281 result = store_switch (p);
282 if (result == OK && ct->c_subtype == MULTI_ALTERNATE)
283 break;
284 }
285 }
286
287 return result;
288 }
289
290
291 /*
292 * Reassemble and store the contents of a collection
293 * of messages of type "message/partial".
294 */
295
296 static int
297 store_partial (CT ct)
298 {
299 int cur, hi, i;
300 CT p, *ctp, *ctq;
301 CT *base;
302 struct partial *pm, *qm;
303
304 qm = (struct partial *) ct->c_ctparams;
305 if (qm->pm_stored)
306 return OK;
307
308 hi = i = 0;
309 for (ctp = cts; *ctp; ctp++) {
310 p = *ctp;
311 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
312 pm = (struct partial *) p->c_ctparams;
313 if (!pm->pm_stored
314 && strcmp (qm->pm_partid, pm->pm_partid) == 0) {
315 pm->pm_marked = pm->pm_partno;
316 if (pm->pm_maxno)
317 hi = pm->pm_maxno;
318 pm->pm_stored = 1;
319 i++;
320 }
321 else
322 pm->pm_marked = 0;
323 }
324 }
325
326 if (hi == 0) {
327 advise (NULL, "missing (at least) last part of multipart message");
328 return NOTOK;
329 }
330
331 if ((base = (CT *) calloc ((size_t) (i + 1), sizeof(*base))) == NULL)
332 adios (NULL, "out of memory");
333
334 ctq = base;
335 for (ctp = cts; *ctp; ctp++) {
336 p = *ctp;
337 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
338 pm = (struct partial *) p->c_ctparams;
339 if (pm->pm_marked)
340 *ctq++ = p;
341 }
342 }
343 *ctq = NULL;
344
345 if (i > 1)
346 qsort ((char *) base, i, sizeof(*base), (qsort_comp) ct_compar);
347
348 cur = 1;
349 for (ctq = base; *ctq; ctq++) {
350 p = *ctq;
351 pm = (struct partial *) p->c_ctparams;
352 if (pm->pm_marked != cur) {
353 if (pm->pm_marked == cur - 1) {
354 admonish (NULL,
355 "duplicate part %d of %d part multipart message",
356 pm->pm_marked, hi);
357 continue;
358 }
359
360 missing_part:
361 advise (NULL,
362 "missing %spart %d of %d part multipart message",
363 cur != hi ? "(at least) " : "", cur, hi);
364 goto losing;
365 }
366 else
367 cur++;
368 }
369 if (hi != --cur) {
370 cur = hi;
371 goto missing_part;
372 }
373
374 /*
375 * Now cycle through the sorted list of messages of type
376 * "message/partial" and save/append them to a file.
377 */
378
379 ctq = base;
380 ct = *ctq++;
381 if (store_content (ct, NULL) == NOTOK) {
382 losing:
383 free ((char *) base);
384 return NOTOK;
385 }
386
387 for (; *ctq; ctq++) {
388 p = *ctq;
389 if (store_content (p, ct) == NOTOK)
390 goto losing;
391 }
392
393 free ((char *) base);
394 return OK;
395 }
396
397
398 /*
399 * Store content from a message of type "message/external".
400 */
401
402 static int
403 store_external (CT ct)
404 {
405 int result = NOTOK;
406 struct exbody *e = (struct exbody *) ct->c_ctparams;
407 CT p = e->eb_content;
408
409 if (!type_ok (p, 1))
410 return OK;
411
412 /*
413 * Check if the parameters for the external body
414 * specified a filename.
415 */
416 if (autosw) {
417 char *cp;
418
419 if ((cp = e->eb_name)
420 && *cp != '/'
421 && *cp != '.'
422 && *cp != '|'
423 && *cp != '!'
424 && !strchr (cp, '%')) {
425 if (!ct->c_storeproc)
426 ct->c_storeproc = add (cp, NULL);
427 if (!p->c_storeproc)
428 p->c_storeproc = add (cp, NULL);
429 }
430 }
431
432 /*
433 * Since we will let the Content structure for the
434 * external body substitute for the current content,
435 * we temporarily change its partno (number inside
436 * multipart), so everything looks right.
437 */
438 p->c_partno = ct->c_partno;
439
440 /* we probably need to check if content is really there */
441 result = store_switch (p);
442
443 p->c_partno = NULL;
444 return result;
445 }
446
447
448 /*
449 * Compare the numbering from two different
450 * message/partials (needed for sorting).
451 */
452
453 static int
454 ct_compar (CT *a, CT *b)
455 {
456 struct partial *am = (struct partial *) ((*a)->c_ctparams);
457 struct partial *bm = (struct partial *) ((*b)->c_ctparams);
458
459 return (am->pm_marked - bm->pm_marked);
460 }
461
462
463 /*
464 * Store contents of a message or message part to
465 * a folder, a file, the standard output, or pass
466 * the contents to a command.
467 *
468 * If the current content to be saved is a followup part
469 * to a collection of messages of type "message/partial",
470 * then field "p" is a pointer to the Content structure
471 * to the first message/partial in the group.
472 */
473
474 static int
475 store_content (CT ct, CT p)
476 {
477 int appending = 0, msgnum = 0;
478 int is_partial = 0, first_partial = 0;
479 int last_partial = 0;
480 char *cp, buffer[BUFSIZ];
481
482 /*
483 * Do special processing for messages of
484 * type "message/partial".
485 *
486 * We first check if this content is of type
487 * "message/partial". If it is, then we need to check
488 * whether it is the first and/or last in the group.
489 *
490 * Then if "p" is a valid pointer, it points to the Content
491 * structure of the first partial in the group. So we copy
492 * the file name and/or folder name from that message. In
493 * this case, we also note that we will be appending.
494 */
495 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
496 struct partial *pm = (struct partial *) ct->c_ctparams;
497
498 /* Yep, it's a message/partial */
499 is_partial = 1;
500
501 /* But is it the first and/or last in the collection? */
502 if (pm->pm_partno == 1)
503 first_partial = 1;
504 if (pm->pm_maxno && pm->pm_partno == pm->pm_maxno)
505 last_partial = 1;
506
507 /*
508 * If "p" is a valid pointer, then it points to the
509 * Content structure for the first message in the group.
510 * So we just copy the filename or foldername information
511 * from the previous iteration of this function.
512 */
513 if (p) {
514 appending = 1;
515 ct->c_storage = add (p->c_storage, NULL);
516
517 /* record the folder name */
518 if (p->c_folder) {
519 ct->c_folder = add (p->c_folder, NULL);
520 }
521 goto got_filename;
522 }
523 }
524
525 /*
526 * Get storage formatting string.
527 *
528 * 1) If we have storeproc defined, then use that
529 * 2) Else check for a mhn-store-<type>/<subtype> entry
530 * 3) Else check for a mhn-store-<type> entry
531 * 4) Else if content is "message", use "+" (current folder)
532 * 5) Else use string "%m%P.%s".
533 */
534 if ((cp = ct->c_storeproc) == NULL || *cp == '\0') {
535 CI ci = &ct->c_ctinfo;
536
537 snprintf (buffer, sizeof(buffer), "%s-store-%s/%s",
538 invo_name, ci->ci_type, ci->ci_subtype);
539 if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
540 snprintf (buffer, sizeof(buffer), "%s-store-%s", invo_name, ci->ci_type);
541 if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
542 cp = ct->c_type == CT_MESSAGE ? "+" : "%m%P.%s";
543 }
544 }
545 }
546
547 /*
548 * Check the beginning of storage formatting string
549 * to see if we are saving content to a folder.
550 */
551 if (*cp == '+' || *cp == '@') {
552 char *tmpfilenam, *folder;
553
554 /* Store content in temporary file for now */
555 tmpfilenam = m_mktemp(invo_name, NULL, NULL);
556 ct->c_storage = add (tmpfilenam, NULL);
557
558 /* Get the folder name */
559 if (cp[1])
560 folder = pluspath (cp);
561 else
562 folder = getfolder (1);
563
564 /* Check if folder exists */
565 create_folder(m_mailpath(folder), 0, exit);
566
567 /* Record the folder name */
568 ct->c_folder = add (folder, NULL);
569
570 if (cp[1])
571 free (folder);
572
573 goto got_filename;
574 }
575
576 /*
577 * Parse and expand the storage formatting string
578 * in `cp' into `buffer'.
579 */
580 parse_format_string (ct, cp, buffer, sizeof(buffer), dir);
581
582 /*
583 * If formatting begins with '|' or '!', then pass
584 * content to standard input of a command and return.
585 */
586 if (buffer[0] == '|' || buffer[0] == '!')
587 return show_content_aux (ct, 1, 0, buffer + 1, dir);
588
589 /* record the filename */
590 if ((ct->c_storage = clobber_check (add (buffer, NULL))) == NULL) {
591 return NOTOK;
592 }
593
594 got_filename:
595 /* flush the output stream */
596 fflush (stdout);
597
598 /* Now save or append the content to a file */
599 if (output_content_file (ct, appending) == NOTOK)
600 return NOTOK;
601
602 /*
603 * If necessary, link the file into a folder and remove
604 * the temporary file. If this message is a partial,
605 * then only do this if it is the last one in the group.
606 */
607 if (ct->c_folder && (!is_partial || last_partial)) {
608 msgnum = output_content_folder (ct->c_folder, ct->c_storage);
609 unlink (ct->c_storage);
610 if (msgnum == NOTOK)
611 return NOTOK;
612 }
613
614 /*
615 * Now print out the name/number of the message
616 * that we are storing.
617 */
618 if (is_partial) {
619 if (first_partial)
620 fprintf (stderr, "reassembling partials ");
621 if (last_partial)
622 fprintf (stderr, "%s", ct->c_file);
623 else
624 fprintf (stderr, "%s,", ct->c_file);
625 } else {
626 fprintf (stderr, "storing message %s", ct->c_file);
627 if (ct->c_partno)
628 fprintf (stderr, " part %s", ct->c_partno);
629 }
630
631 /*
632 * Unless we are in the "middle" of group of message/partials,
633 * we now print the name of the file, folder, and/or message
634 * to which we are storing the content.
635 */
636 if (!is_partial || last_partial) {
637 if (ct->c_folder) {
638 fprintf (stderr, " to folder %s as message %d\n", ct->c_folder, msgnum);
639 } else if (!strcmp(ct->c_storage, "-")) {
640 fprintf (stderr, " to stdout\n");
641 } else {
642 int cwdlen;
643
644 cwdlen = strlen (cwd);
645 fprintf (stderr, " as file %s\n",
646 strncmp (ct->c_storage, cwd, cwdlen)
647 || ct->c_storage[cwdlen] != '/'
648 ? ct->c_storage : ct->c_storage + cwdlen + 1);
649 }
650 }
651
652 return OK;
653 }
654
655
656 /*
657 * Output content to a file
658 */
659
660 static int
661 output_content_file (CT ct, int appending)
662 {
663 int filterstate;
664 char *file, buffer[BUFSIZ];
665 long pos, last;
666 FILE *fp;
667
668 /*
669 * If the pathname is absolute, make sure
670 * all the relevant directories exist.
671 */
672 if (strchr(ct->c_storage, '/')
673 && make_intermediates (ct->c_storage) == NOTOK)
674 return NOTOK;
675
676 if (ct->c_encoding != CE_7BIT) {
677 int cc, fd;
678
679 if (!ct->c_ceopenfnx) {
680 advise (NULL, "don't know how to decode part %s of message %s",
681 ct->c_partno, ct->c_file);
682 return NOTOK;
683 }
684
685 file = appending || !strcmp (ct->c_storage, "-") ? NULL
686 : ct->c_storage;
687 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
688 return NOTOK;
689 if (!strcmp (file, ct->c_storage)) {
690 (*ct->c_ceclosefnx) (ct);
691 return OK;
692 }
693
694 /*
695 * Send to standard output
696 */
697 if (!strcmp (ct->c_storage, "-")) {
698 int gd;
699
700 if ((gd = dup (fileno (stdout))) == NOTOK) {
701 advise ("stdout", "unable to dup");
702 losing:
703 (*ct->c_ceclosefnx) (ct);
704 return NOTOK;
705 }
706 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
707 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
708 appending ? "a" : "w");
709 close (gd);
710 goto losing;
711 }
712 } else {
713 /*
714 * Open output file
715 */
716 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
717 advise (ct->c_storage, "unable to fopen for %s",
718 appending ? "appending" : "writing");
719 goto losing;
720 }
721 }
722
723 /*
724 * Filter the header fields of the initial enclosing
725 * message/partial into the file.
726 */
727 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
728 struct partial *pm = (struct partial *) ct->c_ctparams;
729
730 if (pm->pm_partno == 1)
731 copy_some_headers (fp, ct);
732 }
733
734 for (;;) {
735 switch (cc = read (fd, buffer, sizeof(buffer))) {
736 case NOTOK:
737 advise (file, "error reading content from");
738 break;
739
740 case OK:
741 break;
742
743 default:
744 fwrite (buffer, sizeof(*buffer), cc, fp);
745 continue;
746 }
747 break;
748 }
749
750 (*ct->c_ceclosefnx) (ct);
751
752 if (cc != NOTOK && fflush (fp))
753 advise (ct->c_storage, "error writing to");
754
755 fclose (fp);
756
757 return (cc != NOTOK ? OK : NOTOK);
758 }
759
760 if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
761 advise (ct->c_file, "unable to open for reading");
762 return NOTOK;
763 }
764
765 pos = ct->c_begin;
766 last = ct->c_end;
767 fseek (ct->c_fp, pos, SEEK_SET);
768
769 if (!strcmp (ct->c_storage, "-")) {
770 int gd;
771
772 if ((gd = dup (fileno (stdout))) == NOTOK) {
773 advise ("stdout", "unable to dup");
774 return NOTOK;
775 }
776 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
777 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
778 appending ? "a" : "w");
779 close (gd);
780 return NOTOK;
781 }
782 } else {
783 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
784 advise (ct->c_storage, "unable to fopen for %s",
785 appending ? "appending" : "writing");
786 return NOTOK;
787 }
788 }
789
790 /*
791 * Copy a few of the header fields of the initial
792 * enclosing message/partial into the file.
793 */
794 filterstate = 0;
795 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
796 struct partial *pm = (struct partial *) ct->c_ctparams;
797
798 if (pm->pm_partno == 1) {
799 copy_some_headers (fp, ct);
800 filterstate = 1;
801 }
802 }
803
804 while (fgets (buffer, sizeof(buffer) - 1, ct->c_fp)) {
805 if ((pos += strlen (buffer)) > last) {
806 int diff;
807
808 diff = strlen (buffer) - (pos - last);
809 if (diff >= 0)
810 buffer[diff] = '\0';
811 }
812 /*
813 * If this is the first content of a group of
814 * message/partial contents, then we only copy a few
815 * of the header fields of the enclosed message.
816 */
817 if (filterstate) {
818 switch (buffer[0]) {
819 case ' ':
820 case '\t':
821 if (filterstate < 0)
822 buffer[0] = 0;
823 break;
824
825 case '\n':
826 filterstate = 0;
827 break;
828
829 default:
830 if (!uprf (buffer, XXX_FIELD_PRF)
831 && !uprf (buffer, VRSN_FIELD)
832 && !uprf (buffer, "Subject:")
833 && !uprf (buffer, "Encrypted:")
834 && !uprf (buffer, "Message-ID:")) {
835 filterstate = -1;
836 buffer[0] = 0;
837 break;
838 }
839 filterstate = 1;
840 break;
841 }
842 }
843 fputs (buffer, fp);
844 if (pos >= last)
845 break;
846 }
847
848 if (fflush (fp))
849 advise (ct->c_storage, "error writing to");
850
851 fclose (fp);
852 fclose (ct->c_fp);
853 ct->c_fp = NULL;
854 return OK;
855 }
856
857
858 /*
859 * Add a file to a folder.
860 *
861 * Return the new message number of the file
862 * when added to the folder. Return -1, if
863 * there is an error.
864 */
865
866 static int
867 output_content_folder (char *folder, char *filename)
868 {
869 int msgnum;
870 struct msgs *mp;
871
872 /* Read the folder. */
873 if ((mp = folder_read (folder, 0))) {
874 /* Link file into folder */
875 msgnum = folder_addmsg (&mp, filename, 0, 0, 0, 0, (char *)0);
876 } else {
877 advise (NULL, "unable to read folder %s", folder);
878 return NOTOK;
879 }
880
881 /* free folder structure */
882 folder_free (mp);
883
884 /*
885 * Return msgnum. We are relying on the fact that
886 * msgnum will be -1, if folder_addmsg() had an error.
887 */
888 return msgnum;
889 }
890
891
892 /*
893 * Parse and expand the storage formatting string
894 * pointed to by "cp" into "buffer".
895 */
896
897 static int
898 parse_format_string (CT ct, char *cp, char *buffer, int buflen, char *dir)
899 {
900 int len;
901 char *bp;
902 CI ci = &ct->c_ctinfo;
903
904 /*
905 * If storage string is "-", just copy it, and
906 * return (send content to standard output).
907 */
908 if (cp[0] == '-' && cp[1] == '\0') {
909 strncpy (buffer, cp, buflen);
910 return 0;
911 }
912
913 bp = buffer;
914 bp[0] = '\0';
915
916 /*
917 * If formatting string is a pathname that doesn't
918 * begin with '/', then preface the path with the
919 * appropriate directory.
920 */
921 if (*cp != '/' && *cp != '|' && *cp != '!') {
922 snprintf (bp, buflen, "%s/", dir[1] ? dir : "");
923 len = strlen (bp);
924 bp += len;
925 buflen -= len;
926 }
927
928 for (; *cp; cp++) {
929
930 /* We are processing a storage escape */
931 if (*cp == '%') {
932 switch (*++cp) {
933 case 'a':
934 /*
935 * Insert parameters from Content-Type.
936 * This is only valid for '|' commands.
937 */
938 if (buffer[0] != '|' && buffer[0] != '!') {
939 *bp++ = *--cp;
940 *bp = '\0';
941 buflen--;
942 continue;
943 } else {
944 char **ap, **ep;
945 char *s = "";
946
947 for (ap = ci->ci_attrs, ep = ci->ci_values;
948 *ap; ap++, ep++) {
949 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
950 len = strlen (bp);
951 bp += len;
952 buflen -= len;
953 s = " ";
954 }
955 }
956 break;
957
958 case 'm':
959 /* insert message number */
960 snprintf (bp, buflen, "%s", r1bindex (ct->c_file, '/'));
961 break;
962
963 case 'P':
964 /* insert part number with leading dot */
965 if (ct->c_partno)
966 snprintf (bp, buflen, ".%s", ct->c_partno);
967 break;
968
969 case 'p':
970 /* insert part number withouth leading dot */
971 if (ct->c_partno)
972 strncpy (bp, ct->c_partno, buflen);
973 break;
974
975 case 't':
976 /* insert content type */
977 strncpy (bp, ci->ci_type, buflen);
978 break;
979
980 case 's':
981 /* insert content subtype */
982 strncpy (bp, ci->ci_subtype, buflen);
983 break;
984
985 case '%':
986 /* insert the character % */
987 goto raw;
988
989 default:
990 *bp++ = *--cp;
991 *bp = '\0';
992 buflen--;
993 continue;
994 }
995
996 /* Advance bp and decrement buflen */
997 len = strlen (bp);
998 bp += len;
999 buflen -= len;
1000
1001 } else {
1002 raw:
1003 *bp++ = *cp;
1004 *bp = '\0';
1005 buflen--;
1006 }
1007 }
1008
1009 return 0;
1010 }
1011
1012
1013 /*
1014 * Check if the content specifies a filename
1015 * in its MIME parameters.
1016 */
1017
1018 static void
1019 get_storeproc (CT ct)
1020 {
1021 char **ap, **ep, *cp;
1022 CI ci;
1023
1024 /*
1025 * If the storeproc has already been defined,
1026 * we just return (for instance, if this content
1027 * is part of a "message/external".
1028 */
1029 if (ct->c_storeproc)
1030 return;
1031
1032 /*
1033 * If there's a Content-Disposition header and it has a filename,
1034 * use that (RFC-2183).
1035 */
1036 if (ct->c_dispo) {
1037 char *cp = strchr (ct->c_dispo, ';');
1038 CI ci = calloc (1, sizeof *ci);
1039 int status;
1040 int found_filename = 0;
1041
1042 if (cp && parse_header_attrs (ct->c_file, strlen (invo_name) + 2, &cp,
1043 ci, &status) == OK) {
1044 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1045 if (! strcasecmp (*ap, "filename")
1046 && *(cp = *ep) != '/'
1047 && *cp != '.'
1048 && *cp != '|'
1049 && *cp != '!'
1050 && !strchr (cp, '%')) {
1051 ct->c_storeproc = add (cp, NULL);
1052 found_filename = 1;
1053 }
1054 free (*ap);
1055 }
1056 }
1057
1058 free (ci);
1059 if (found_filename) return;
1060 }
1061
1062 /*
1063 * Check the attribute/value pairs, for the attribute "name".
1064 * If found, do a few sanity checks and copy the value into
1065 * the storeproc.
1066 */
1067 ci = &ct->c_ctinfo;
1068 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1069 if (! strcasecmp (*ap, "name")
1070 && *(cp = *ep) != '/'
1071 && *cp != '.'
1072 && *cp != '|'
1073 && *cp != '!'
1074 && !strchr (cp, '%')) {
1075 ct->c_storeproc = add (cp, NULL);
1076 return;
1077 }
1078 }
1079 }
1080
1081
1082 /*
1083 * Copy some of the header fields of the initial message/partial
1084 * message into the header of the reassembled message.
1085 */
1086
1087 static int
1088 copy_some_headers (FILE *out, CT ct)
1089 {
1090 HF hp;
1091
1092 hp = ct->c_first_hf; /* start at first header field */
1093
1094 while (hp) {
1095 /*
1096 * A few of the header fields of the enclosing
1097 * messages are not copied.
1098 */
1099 if (!uprf (hp->name, XXX_FIELD_PRF)
1100 && strcasecmp (hp->name, VRSN_FIELD)
1101 && strcasecmp (hp->name, "Subject")
1102 && strcasecmp (hp->name, "Encrypted")
1103 && strcasecmp (hp->name, "Message-ID"))
1104 fprintf (out, "%s:%s", hp->name, hp->value);
1105 hp = hp->next; /* next header field */
1106 }
1107
1108 return OK;
1109 }
1110
1111 /******************************************************************************/
1112 /* -clobber support */
1113
1114 enum clobber_policy_t {
1115 NMH_CLOBBER_ALWAYS,
1116 NMH_CLOBBER_AUTO,
1117 NMH_CLOBBER_SUFFIX,
1118 NMH_CLOBBER_ASK,
1119 NMH_CLOBBER_NEVER
1120 };
1121
1122 static enum clobber_policy_t clobber_policy = NMH_CLOBBER_ALWAYS;
1123
1124 int files_not_clobbered = 0;
1125
1126 int
1127 save_clobber_policy (const char *value) {
1128 if (! strcasecmp (value, "always")) {
1129 clobber_policy = NMH_CLOBBER_ALWAYS;
1130 } else if (! strcasecmp (value, "auto")) {
1131 clobber_policy = NMH_CLOBBER_AUTO;
1132 } else if (! strcasecmp (value, "suffix")) {
1133 clobber_policy = NMH_CLOBBER_SUFFIX;
1134 } else if (! strcasecmp (value, "ask")) {
1135 clobber_policy = NMH_CLOBBER_ASK;
1136 } else if (! strcasecmp (value, "never")) {
1137 clobber_policy = NMH_CLOBBER_NEVER;
1138 } else {
1139 return 1;
1140 }
1141
1142 return 0;
1143 }
1144
1145
1146 static char *
1147 next_version (char *file, enum clobber_policy_t clobber_policy) {
1148 const size_t max_versions = 1000000;
1149 /* 8 = log max_versions + one for - or . + one for null terminator */
1150 const size_t buflen = strlen (file) + 8;
1151 char *buffer = mh_xmalloc (buflen);
1152 size_t version;
1153
1154 char *extension = NULL;
1155 if (clobber_policy == NMH_CLOBBER_AUTO &&
1156 ((extension = strrchr (file, '.')) != NULL)) {
1157 *extension++ = '\0';
1158 }
1159
1160 for (version = 1; version < max_versions; ++version) {
1161 int fd;
1162
1163 switch (clobber_policy) {
1164 case NMH_CLOBBER_AUTO: {
1165 snprintf (buffer, buflen, "%s-%ld%s%s", file, (long) version,
1166 extension == NULL ? "" : ".",
1167 extension == NULL ? "" : extension);
1168 break;
1169 }
1170
1171 case NMH_CLOBBER_SUFFIX:
1172 snprintf (buffer, buflen, "%s.%ld", file, (long) version);
1173 break;
1174
1175 default:
1176 /* Should never get here. */
1177 advise (NULL, "will not overwrite %s, invalid clobber policy", buffer);
1178 free (buffer);
1179 ++files_not_clobbered;
1180 return NULL;
1181 }
1182
1183 /* Actually (try to) create the file here to avoid a race
1184 condition on file naming + creation. This won't solve the
1185 problem with old NFS that doesn't support O_EXCL, though.
1186 Let the umask strip off permissions from 0666 as desired.
1187 That's what fopen () would do if it was creating the file. */
1188 if ((fd = open (buffer, O_CREAT | O_EXCL,
1189 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
1190 S_IROTH | S_IWOTH)) >= 0) {
1191 close (fd);
1192 break;
1193 }
1194 }
1195
1196 free (file);
1197
1198 if (version >= max_versions) {
1199 advise (NULL, "will not overwrite %s, too many versions", buffer);
1200 free (buffer);
1201 buffer = NULL;
1202 ++files_not_clobbered;
1203 }
1204
1205 return buffer;
1206 }
1207
1208
1209 static char *
1210 clobber_check (char *original_file) {
1211 /* clobber policy return value
1212 * -------------- ------------
1213 * -always file
1214 * -auto file-<digits>.extension
1215 * -suffix file.<digits>
1216 * -ask file, 0, or another filename/path
1217 * -never 0
1218 */
1219
1220 char *file;
1221 char *cwd = NULL;
1222 int check_again;
1223
1224 if (clobber_policy == NMH_CLOBBER_ASK) {
1225 /* Save cwd for possible use in loop below. */
1226 char *slash;
1227
1228 cwd = add (original_file, NULL);
1229 slash = strrchr (cwd, '/');
1230
1231 if (slash) {
1232 *slash = '\0';
1233 } else {
1234 /* original_file wasn't a full path, which shouldn't happen. */
1235 cwd = NULL;
1236 }
1237 }
1238
1239 do {
1240 struct stat st;
1241
1242 file = original_file;
1243 check_again = 0;
1244
1245 switch (clobber_policy) {
1246 case NMH_CLOBBER_ALWAYS:
1247 break;
1248
1249 case NMH_CLOBBER_SUFFIX:
1250 case NMH_CLOBBER_AUTO:
1251 if (stat (file, &st) == OK) {
1252 file = next_version (original_file, clobber_policy);
1253 }
1254 break;
1255
1256 case NMH_CLOBBER_ASK:
1257 if (stat (file, &st) == OK) {
1258 enum answers { NMH_YES, NMH_NO, NMH_RENAME };
1259 static struct swit answer[4] = {
1260 { "yes", 0, NMH_YES }, { "no", 0, NMH_NO }, { "rename", 0, NMH_RENAME }, { NULL, 0, 0 } };
1261 char **ans;
1262
1263 if (isatty (fileno (stdin))) {
1264 char *prompt =
1265 concat ("Overwrite \"", file, "\" [y/n/rename]? ", NULL);
1266 ans = getans (prompt, answer);
1267 free (prompt);
1268 } else {
1269 /* Overwrite, that's what nmh used to do. And warn. */
1270 advise (NULL, "-clobber ask but no tty, so overwrite %s", file);
1271 break;
1272 }
1273
1274 switch ((enum answers) smatch (*ans, answer)) {
1275 case NMH_YES:
1276 break;
1277 case NMH_NO:
1278 free (file);
1279 file = NULL;
1280 ++files_not_clobbered;
1281 break;
1282 case NMH_RENAME: {
1283 char buf[PATH_MAX];
1284 printf ("Enter filename or full path of the new file: ");
1285 if (fgets (buf, sizeof buf, stdin) == NULL ||
1286 buf[0] == '\0') {
1287 file = NULL;
1288 ++files_not_clobbered;
1289 } else {
1290 char *newline = strchr (buf, '\n');
1291 if (newline) {
1292 *newline = '\0';
1293 }
1294 }
1295
1296 free (file);
1297
1298 if (buf[0] == '/') {
1299 /* Full path, use it. */
1300 file = add (buf, NULL);
1301 } else {
1302 /* Relative path. */
1303 file = cwd ? concat (cwd, "/", buf, NULL) : add (buf, NULL);
1304 }
1305
1306 check_again = 1;
1307 break;
1308 }
1309 }
1310 }
1311 break;
1312
1313 case NMH_CLOBBER_NEVER:
1314 if (stat (file, &st) == OK) {
1315 /* Keep count of files that would have been clobbered,
1316 and return that as process exit status. */
1317 advise (NULL, "will not overwrite %s with -clobber never", file);
1318 free (file);
1319 file = NULL;
1320 ++files_not_clobbered;
1321 }
1322 break;
1323 }
1324
1325 original_file = file;
1326 } while (check_again);
1327
1328 if (cwd) {
1329 free (cwd);
1330 }
1331
1332 return file;
1333 }
1334
1335 /* -clobber support */
1336 /******************************************************************************/