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