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