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