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