]> diplodocus.org Git - nmh/blob - uip/mhstoresbr.c
pending-release-notes: add mhshow's "-prefer", and mh-format's %(kibi/kilo)
[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 mhstoreinfo_t
45 mhstoreinfo_create (CT *ct, char *pwd, const char *csw, int asw, int vsw) {
46 mhstoreinfo_t info = mh_xmalloc (sizeof *info);
47
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 = getcpy (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 = add (ct->c_storage, NULL);
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 advise (NULL, "missing (at least) last part of multipart message");
345 return NOTOK;
346 }
347
348 if ((base = (CT *) mh_xcalloc ((size_t) (i + 1), sizeof(*base))) == NULL)
349 adios (NULL, "out of memory");
350
351 ctq = base;
352 for (ctp = info->cts; *ctp; ctp++) {
353 p = *ctp;
354 if (p->c_type == CT_MESSAGE && p->c_subtype == ct->c_subtype) {
355 pm = (struct partial *) p->c_ctparams;
356 if (pm->pm_marked)
357 *ctq++ = p;
358 }
359 }
360 *ctq = NULL;
361
362 if (i > 1)
363 qsort ((char *) base, i, sizeof(*base), (qsort_comp) ct_compar);
364
365 cur = 1;
366 for (ctq = base; *ctq; ctq++) {
367 p = *ctq;
368 pm = (struct partial *) p->c_ctparams;
369 if (pm->pm_marked != cur) {
370 if (pm->pm_marked == cur - 1) {
371 admonish (NULL,
372 "duplicate part %d of %d part multipart message",
373 pm->pm_marked, hi);
374 continue;
375 }
376
377 missing_part:
378 advise (NULL,
379 "missing %spart %d of %d part multipart message",
380 cur != hi ? "(at least) " : "", cur, hi);
381 goto losing;
382 }
383 else
384 cur++;
385 }
386 if (hi != --cur) {
387 cur = hi;
388 goto missing_part;
389 }
390
391 /*
392 * Now cycle through the sorted list of messages of type
393 * "message/partial" and save/append them to a file.
394 */
395
396 ctq = base;
397 ct = *ctq++;
398 if (store_content (ct, NULL, info) == NOTOK) {
399 losing:
400 free ((char *) base);
401 return NOTOK;
402 }
403
404 for (; *ctq; ctq++) {
405 p = *ctq;
406 if (store_content (p, ct, info) == NOTOK)
407 goto losing;
408 }
409
410 free ((char *) base);
411 return OK;
412 }
413
414
415 /*
416 * Store content from a message of type "message/external".
417 */
418
419 static int
420 store_external (CT ct, mhstoreinfo_t info)
421 {
422 int result = NOTOK;
423 struct exbody *e = (struct exbody *) ct->c_ctparams;
424 CT p = e->eb_content;
425
426 if (!type_ok (p, 1))
427 return OK;
428
429 /*
430 * Check if the parameters for the external body
431 * specified a filename.
432 */
433 if (info->autosw) {
434 char *cp;
435
436 if ((cp = e->eb_name)
437 && *cp != '/'
438 && *cp != '.'
439 && *cp != '|'
440 && *cp != '!'
441 && !strchr (cp, '%')) {
442 if (!ct->c_storeproc)
443 ct->c_storeproc = add (cp, NULL);
444 if (!p->c_storeproc)
445 p->c_storeproc = add (cp, NULL);
446 }
447 }
448
449 /*
450 * Since we will let the Content structure for the
451 * external body substitute for the current content,
452 * we temporarily change its partno (number inside
453 * multipart), so everything looks right.
454 */
455 p->c_partno = ct->c_partno;
456
457 /* we probably need to check if content is really there */
458 if (ct->c_storage) {
459 /* Support mhstore -outfile. The MIME parser doesn't load
460 c_storage, so we know that p->c_storage is NULL here. */
461 p->c_storage = add (ct->c_storage, NULL);
462 }
463 result = store_switch (p, info);
464
465 p->c_partno = NULL;
466 return result;
467 }
468
469
470 /*
471 * Compare the numbering from two different
472 * message/partials (needed for sorting).
473 */
474
475 static int
476 ct_compar (CT *a, CT *b)
477 {
478 struct partial *am = (struct partial *) ((*a)->c_ctparams);
479 struct partial *bm = (struct partial *) ((*b)->c_ctparams);
480
481 return (am->pm_marked - bm->pm_marked);
482 }
483
484
485 /*
486 * Store contents of a message or message part to
487 * a folder, a file, the standard output, or pass
488 * the contents to a command.
489 *
490 * If the current content to be saved is a followup part
491 * to a collection of messages of type "message/partial",
492 * then field "p" is a pointer to the Content structure
493 * to the first message/partial in the group.
494 */
495
496 static int
497 store_content (CT ct, CT p, mhstoreinfo_t info)
498 {
499 int appending = 0, msgnum = 0;
500 int is_partial = 0, first_partial = 0;
501 int last_partial = 0;
502 char *cp, buffer[BUFSIZ];
503
504 /*
505 * Do special processing for messages of
506 * type "message/partial".
507 *
508 * We first check if this content is of type
509 * "message/partial". If it is, then we need to check
510 * whether it is the first and/or last in the group.
511 *
512 * Then if "p" is a valid pointer, it points to the Content
513 * structure of the first partial in the group. So we copy
514 * the file name and/or folder name from that message. In
515 * this case, we also note that we will be appending.
516 */
517 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
518 struct partial *pm = (struct partial *) ct->c_ctparams;
519
520 /* Yep, it's a message/partial */
521 is_partial = 1;
522
523 /* But is it the first and/or last in the collection? */
524 if (pm->pm_partno == 1)
525 first_partial = 1;
526 if (pm->pm_maxno && pm->pm_partno == pm->pm_maxno)
527 last_partial = 1;
528
529 /*
530 * If "p" is a valid pointer, then it points to the
531 * Content structure for the first message in the group.
532 * So we just copy the filename or foldername information
533 * from the previous iteration of this function.
534 */
535 if (p) {
536 appending = 1;
537 if (! ct->c_storage) {
538 ct->c_storage = add (p->c_storage, NULL);
539
540 /* record the folder name */
541 if (p->c_folder) {
542 ct->c_folder = add (p->c_folder, NULL);
543 }
544 }
545 goto got_filename;
546 }
547 }
548
549 /*
550 * Get storage formatting string.
551 *
552 * 1) If we have storeproc defined, then use that
553 * 2) Else check for a mhn-store-<type>/<subtype> entry
554 * 3) Else check for a mhn-store-<type> entry
555 * 4) Else if content is "message", use "+" (current folder)
556 * 5) Else use string "%m%P.%s".
557 */
558 if ((cp = ct->c_storeproc) == NULL || *cp == '\0') {
559 CI ci = &ct->c_ctinfo;
560
561 cp = context_find_by_type ("store", ci->ci_type, ci->ci_subtype);
562 if (cp == NULL) {
563 cp = ct->c_type == CT_MESSAGE ? "+" : "%m%P.%s";
564 }
565 }
566
567 if (! ct->c_storage) {
568 /*
569 * Check the beginning of storage formatting string
570 * to see if we are saving content to a folder.
571 */
572 if (*cp == '+' || *cp == '@') {
573 char *tmpfilenam, *folder;
574
575 /* Store content in temporary file for now */
576 if ((tmpfilenam = m_mktemp(invo_name, NULL, NULL)) == NULL) {
577 adios(NULL, "unable to create temporary file in %s",
578 get_temp_dir());
579 }
580 ct->c_storage = add (tmpfilenam, NULL);
581
582 /* Get the folder name */
583 if (cp[1])
584 folder = pluspath (cp);
585 else
586 folder = getfolder (1);
587
588 /* Check if folder exists */
589 create_folder(m_mailpath(folder), 0, exit);
590
591 /* Record the folder name */
592 ct->c_folder = add (folder, NULL);
593
594 if (cp[1])
595 free (folder);
596
597 goto got_filename;
598 }
599
600 /*
601 * Parse and expand the storage formatting string
602 * in `cp' into `buffer'.
603 */
604 parse_format_string (ct, cp, buffer, sizeof(buffer), info->dir);
605
606 /*
607 * If formatting begins with '|' or '!', then pass
608 * content to standard input of a command and return.
609 */
610 if (buffer[0] == '|' || buffer[0] == '!')
611 return show_content_aux (ct, 0, buffer + 1, info->dir, NULL);
612
613 /* record the filename */
614 if ((ct->c_storage = clobber_check (add (buffer, NULL), info)) ==
615 NULL) {
616 return NOTOK;
617 }
618 } else {
619 /* The output filename was explicitly specified, so use it. */
620 if ((ct->c_storage = clobber_check (ct->c_storage, info)) ==
621 NULL) {
622 return NOTOK;
623 }
624 }
625
626 got_filename:
627 /* flush the output stream */
628 fflush (stdout);
629
630 /* Now save or append the content to a file */
631 if (output_content_file (ct, appending) == NOTOK)
632 return NOTOK;
633
634 /*
635 * If necessary, link the file into a folder and remove
636 * the temporary file. If this message is a partial,
637 * then only do this if it is the last one in the group.
638 */
639 if (ct->c_folder && (!is_partial || last_partial)) {
640 msgnum = output_content_folder (ct->c_folder, ct->c_storage);
641 (void) m_unlink (ct->c_storage);
642 if (msgnum == NOTOK)
643 return NOTOK;
644 }
645
646 if (info->verbosw) {
647 /*
648 * Now print out the name/number of the message
649 * that we are storing.
650 */
651 if (is_partial) {
652 if (first_partial)
653 fprintf (stderr, "reassembling partials ");
654 if (last_partial)
655 fprintf (stderr, "%s", ct->c_file);
656 else
657 fprintf (stderr, "%s,", ct->c_file);
658 } else {
659 fprintf (stderr, "storing message %s", ct->c_file);
660 if (ct->c_partno)
661 fprintf (stderr, " part %s", ct->c_partno);
662 }
663
664 /*
665 * Unless we are in the "middle" of group of message/partials,
666 * we now print the name of the file, folder, and/or message
667 * to which we are storing the content.
668 */
669 if (!is_partial || last_partial) {
670 if (ct->c_folder) {
671 fprintf (stderr, " to folder %s as message %d\n", ct->c_folder,
672 msgnum);
673 } else if (!strcmp(ct->c_storage, "-")) {
674 fprintf (stderr, " to stdout\n");
675 } else {
676 int cwdlen = strlen (info->cwd);
677
678 fprintf (stderr, " as file %s\n",
679 strncmp (ct->c_storage, info->cwd, cwdlen)
680 || ct->c_storage[cwdlen] != '/'
681 ? ct->c_storage : ct->c_storage + cwdlen + 1);
682 }
683 }
684 }
685
686 return OK;
687 }
688
689
690 /*
691 * Output content to a file
692 */
693
694 static int
695 output_content_file (CT ct, int appending)
696 {
697 int filterstate;
698 char *file, buffer[BUFSIZ];
699 long pos, last;
700 FILE *fp;
701
702 /*
703 * If the pathname is absolute, make sure
704 * all the relevant directories exist.
705 */
706 if (strchr(ct->c_storage, '/')
707 && make_intermediates (ct->c_storage) == NOTOK)
708 return NOTOK;
709
710 if (ct->c_encoding != CE_7BIT) {
711 int cc, fd;
712
713 if (!ct->c_ceopenfnx) {
714 advise (NULL, "don't know how to decode part %s of message %s",
715 ct->c_partno, ct->c_file);
716 return NOTOK;
717 }
718
719 file = appending || !strcmp (ct->c_storage, "-") ? NULL
720 : ct->c_storage;
721 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
722 return NOTOK;
723 if (!strcmp (file, ct->c_storage)) {
724 (*ct->c_ceclosefnx) (ct);
725 return OK;
726 }
727
728 /*
729 * Send to standard output
730 */
731 if (!strcmp (ct->c_storage, "-")) {
732 int gd;
733
734 if ((gd = dup (fileno (stdout))) == NOTOK) {
735 advise ("stdout", "unable to dup");
736 losing:
737 (*ct->c_ceclosefnx) (ct);
738 return NOTOK;
739 }
740 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
741 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
742 appending ? "a" : "w");
743 close (gd);
744 goto losing;
745 }
746 } else {
747 /*
748 * Open output file
749 */
750 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
751 advise (ct->c_storage, "unable to fopen for %s",
752 appending ? "appending" : "writing");
753 goto losing;
754 }
755 }
756
757 /*
758 * Filter the header fields of the initial enclosing
759 * message/partial into the file.
760 */
761 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
762 struct partial *pm = (struct partial *) ct->c_ctparams;
763
764 if (pm->pm_partno == 1)
765 copy_some_headers (fp, ct);
766 }
767
768 for (;;) {
769 switch (cc = read (fd, buffer, sizeof(buffer))) {
770 case NOTOK:
771 advise (file, "error reading content from");
772 break;
773
774 case OK:
775 break;
776
777 default:
778 if ((int) fwrite (buffer, sizeof(*buffer), cc, fp) < cc) {
779 advise ("output_content_file", "fwrite");
780 }
781 continue;
782 }
783 break;
784 }
785
786 (*ct->c_ceclosefnx) (ct);
787
788 if (cc != NOTOK && fflush (fp))
789 advise (ct->c_storage, "error writing to");
790
791 fclose (fp);
792
793 return (cc != NOTOK ? OK : NOTOK);
794 }
795
796 if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
797 advise (ct->c_file, "unable to open for reading");
798 return NOTOK;
799 }
800
801 pos = ct->c_begin;
802 last = ct->c_end;
803 fseek (ct->c_fp, pos, SEEK_SET);
804
805 if (!strcmp (ct->c_storage, "-")) {
806 int gd;
807
808 if ((gd = dup (fileno (stdout))) == NOTOK) {
809 advise ("stdout", "unable to dup");
810 return NOTOK;
811 }
812 if ((fp = fdopen (gd, appending ? "a" : "w")) == NULL) {
813 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd,
814 appending ? "a" : "w");
815 close (gd);
816 return NOTOK;
817 }
818 } else {
819 if ((fp = fopen (ct->c_storage, appending ? "a" : "w")) == NULL) {
820 advise (ct->c_storage, "unable to fopen for %s",
821 appending ? "appending" : "writing");
822 return NOTOK;
823 }
824 }
825
826 /*
827 * Copy a few of the header fields of the initial
828 * enclosing message/partial into the file.
829 */
830 filterstate = 0;
831 if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
832 struct partial *pm = (struct partial *) ct->c_ctparams;
833
834 if (pm->pm_partno == 1) {
835 copy_some_headers (fp, ct);
836 filterstate = 1;
837 }
838 }
839
840 while (fgets (buffer, sizeof(buffer) - 1, ct->c_fp)) {
841 if ((pos += strlen (buffer)) > last) {
842 int diff;
843
844 diff = strlen (buffer) - (pos - last);
845 if (diff >= 0)
846 buffer[diff] = '\0';
847 }
848 /*
849 * If this is the first content of a group of
850 * message/partial contents, then we only copy a few
851 * of the header fields of the enclosed message.
852 */
853 if (filterstate) {
854 switch (buffer[0]) {
855 case ' ':
856 case '\t':
857 if (filterstate < 0)
858 buffer[0] = 0;
859 break;
860
861 case '\n':
862 filterstate = 0;
863 break;
864
865 default:
866 if (!uprf (buffer, XXX_FIELD_PRF)
867 && !uprf (buffer, VRSN_FIELD)
868 && !uprf (buffer, "Subject:")
869 && !uprf (buffer, "Encrypted:")
870 && !uprf (buffer, "Message-ID:")) {
871 filterstate = -1;
872 buffer[0] = 0;
873 break;
874 }
875 filterstate = 1;
876 break;
877 }
878 }
879 fputs (buffer, fp);
880 if (pos >= last)
881 break;
882 }
883
884 if (fflush (fp))
885 advise (ct->c_storage, "error writing to");
886
887 fclose (fp);
888 fclose (ct->c_fp);
889 ct->c_fp = NULL;
890 return OK;
891 }
892
893
894 /*
895 * Add a file to a folder.
896 *
897 * Return the new message number of the file
898 * when added to the folder. Return -1, if
899 * there is an error.
900 */
901
902 static int
903 output_content_folder (char *folder, char *filename)
904 {
905 int msgnum;
906 struct msgs *mp;
907
908 /* Read the folder. */
909 if ((mp = folder_read (folder, 0))) {
910 /* Link file into folder */
911 msgnum = folder_addmsg (&mp, filename, 0, 0, 0, 0, (char *)0);
912 } else {
913 advise (NULL, "unable to read folder %s", folder);
914 return NOTOK;
915 }
916
917 /* free folder structure */
918 folder_free (mp);
919
920 /*
921 * Return msgnum. We are relying on the fact that
922 * msgnum will be -1, if folder_addmsg() had an error.
923 */
924 return msgnum;
925 }
926
927
928 /*
929 * Parse and expand the storage formatting string
930 * pointed to by "cp" into "buffer".
931 */
932
933 static int
934 parse_format_string (CT ct, char *cp, char *buffer, int buflen, char *dir)
935 {
936 int len;
937 char *bp;
938 CI ci = &ct->c_ctinfo;
939
940 /*
941 * If storage string is "-", just copy it, and
942 * return (send content to standard output).
943 */
944 if (cp[0] == '-' && cp[1] == '\0') {
945 strncpy (buffer, cp, buflen);
946 return 0;
947 }
948
949 bp = buffer;
950 bp[0] = '\0';
951
952 /*
953 * If formatting string is a pathname that doesn't
954 * begin with '/', then preface the path with the
955 * appropriate directory.
956 */
957 if (*cp != '/' && *cp != '|' && *cp != '!') {
958 snprintf (bp, buflen, "%s/", dir[1] ? dir : "");
959 len = strlen (bp);
960 bp += len;
961 buflen -= len;
962 }
963
964 for (; *cp; cp++) {
965
966 /* We are processing a storage escape */
967 if (*cp == '%') {
968 switch (*++cp) {
969 case 'a':
970 /*
971 * Insert parameters from Content-Type.
972 * This is only valid for '|' commands.
973 */
974 if (buffer[0] != '|' && buffer[0] != '!') {
975 *bp++ = *--cp;
976 *bp = '\0';
977 buflen--;
978 continue;
979 } else {
980 PM pm;
981 char *s = "";
982
983 for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) {
984 snprintf (bp, buflen, "%s%s=\"%s\"", s,
985 pm->pm_name, get_param_value(pm, '?'));
986 len = strlen (bp);
987 bp += len;
988 buflen -= len;
989 s = " ";
990 }
991 }
992 break;
993
994 case 'm':
995 /* insert message number */
996 snprintf (bp, buflen, "%s", r1bindex (ct->c_file, '/'));
997 break;
998
999 case 'P':
1000 /* insert part number with leading dot */
1001 if (ct->c_partno)
1002 snprintf (bp, buflen, ".%s", ct->c_partno);
1003 break;
1004
1005 case 'p':
1006 /* insert part number withouth leading dot */
1007 if (ct->c_partno)
1008 strncpy (bp, ct->c_partno, buflen);
1009 break;
1010
1011 case 't':
1012 /* insert content type */
1013 strncpy (bp, ci->ci_type, buflen);
1014 break;
1015
1016 case 's':
1017 /* insert content subtype */
1018 strncpy (bp, ci->ci_subtype, buflen);
1019 break;
1020
1021 case '%':
1022 /* insert the character % */
1023 goto raw;
1024
1025 default:
1026 *bp++ = *--cp;
1027 *bp = '\0';
1028 buflen--;
1029 continue;
1030 }
1031
1032 /* Advance bp and decrement buflen */
1033 len = strlen (bp);
1034 bp += len;
1035 buflen -= len;
1036
1037 } else {
1038 raw:
1039 *bp++ = *cp;
1040 *bp = '\0';
1041 buflen--;
1042 }
1043 }
1044
1045 return 0;
1046 }
1047
1048
1049 /*
1050 * Check if the content specifies a filename
1051 * in its MIME parameters.
1052 */
1053
1054 static void
1055 get_storeproc (CT ct)
1056 {
1057 char *cp;
1058 CI ci;
1059
1060 /*
1061 * If the storeproc has already been defined,
1062 * we just return (for instance, if this content
1063 * is part of a "message/external".
1064 */
1065 if (ct->c_storeproc)
1066 return;
1067
1068 /*
1069 * If there's a Content-Disposition header and it has a filename,
1070 * use that (RFC-2183).
1071 */
1072 if (ct->c_dispo) {
1073 if ((cp = get_param(ct->c_dispo_first, "filename", '_', 0))
1074 && *cp != '/'
1075 && *cp != '.'
1076 && *cp != '|'
1077 && *cp != '!'
1078 && !strchr (cp, '%')) {
1079 ct->c_storeproc = add (cp, NULL);
1080 free(cp);
1081 return;
1082 }
1083 if (cp)
1084 free(cp);
1085 }
1086
1087 /*
1088 * Check the attribute/value pairs, for the attribute "name".
1089 * If found, do a few sanity checks and copy the value into
1090 * the storeproc.
1091 */
1092 ci = &ct->c_ctinfo;
1093 if ((cp = get_param(ci->ci_first_pm, "name", '_', 0))
1094 && *cp != '/'
1095 && *cp != '.'
1096 && *cp != '|'
1097 && *cp != '!'
1098 && !strchr (cp, '%')) {
1099 ct->c_storeproc = add (cp, NULL);
1100
1101 }
1102 if (cp)
1103 free(cp);
1104 }
1105
1106
1107 /*
1108 * Copy some of the header fields of the initial message/partial
1109 * message into the header of the reassembled message.
1110 */
1111
1112 static int
1113 copy_some_headers (FILE *out, CT ct)
1114 {
1115 HF hp;
1116
1117 hp = ct->c_first_hf; /* start at first header field */
1118
1119 while (hp) {
1120 /*
1121 * A few of the header fields of the enclosing
1122 * messages are not copied.
1123 */
1124 if (!uprf (hp->name, XXX_FIELD_PRF)
1125 && strcasecmp (hp->name, VRSN_FIELD)
1126 && strcasecmp (hp->name, "Subject")
1127 && strcasecmp (hp->name, "Encrypted")
1128 && strcasecmp (hp->name, "Message-ID"))
1129 fprintf (out, "%s:%s", hp->name, hp->value);
1130 hp = hp->next; /* next header field */
1131 }
1132
1133 return OK;
1134 }
1135
1136 /******************************************************************************/
1137 /* -clobber support */
1138
1139 static
1140 enum clobber_policy_t
1141 clobber_policy (const char *value) {
1142 if (value == NULL || ! strcasecmp (value, "always")) {
1143 } else if (! strcasecmp (value, "auto")) {
1144 return NMH_CLOBBER_AUTO;
1145 } else if (! strcasecmp (value, "suffix")) {
1146 return NMH_CLOBBER_SUFFIX;
1147 } else if (! strcasecmp (value, "ask")) {
1148 return NMH_CLOBBER_ASK;
1149 } else if (! strcasecmp (value, "never")) {
1150 return NMH_CLOBBER_NEVER;
1151 } else {
1152 adios (NULL, "invalid argument, %s, to clobber", value);
1153 }
1154
1155 return NMH_CLOBBER_ALWAYS;
1156 }
1157
1158
1159 static char *
1160 next_version (char *file, enum clobber_policy_t clobber_policy) {
1161 const size_t max_versions = 1000000;
1162 /* 8 = log max_versions + one for - or . + one for null terminator */
1163 const size_t buflen = strlen (file) + 8;
1164 char *buffer = mh_xmalloc (buflen);
1165 size_t version;
1166
1167 char *extension = NULL;
1168 if (clobber_policy == NMH_CLOBBER_AUTO &&
1169 ((extension = strrchr (file, '.')) != NULL)) {
1170 *extension++ = '\0';
1171 }
1172
1173 for (version = 1; version < max_versions; ++version) {
1174 int fd;
1175
1176 switch (clobber_policy) {
1177 case NMH_CLOBBER_AUTO: {
1178 snprintf (buffer, buflen, "%s-%ld%s%s", file, (long) version,
1179 extension == NULL ? "" : ".",
1180 extension == NULL ? "" : extension);
1181 break;
1182 }
1183
1184 case NMH_CLOBBER_SUFFIX:
1185 snprintf (buffer, buflen, "%s.%ld", file, (long) version);
1186 break;
1187
1188 default:
1189 /* Should never get here. */
1190 advise (NULL, "will not overwrite %s, invalid clobber policy", buffer);
1191 free (buffer);
1192 return NULL;
1193 }
1194
1195 /* Actually (try to) create the file here to avoid a race
1196 condition on file naming + creation. This won't solve the
1197 problem with old NFS that doesn't support O_EXCL, though.
1198 Let the umask strip off permissions from 0666 as desired.
1199 That's what fopen () would do if it was creating the file. */
1200 if ((fd = open (buffer, O_CREAT | O_EXCL,
1201 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
1202 S_IROTH | S_IWOTH)) >= 0) {
1203 close (fd);
1204 break;
1205 }
1206 }
1207
1208 free (file);
1209
1210 if (version >= max_versions) {
1211 advise (NULL, "will not overwrite %s, too many versions", buffer);
1212 free (buffer);
1213 buffer = NULL;
1214 }
1215
1216 return buffer;
1217 }
1218
1219
1220 static char *
1221 clobber_check (char *original_file, mhstoreinfo_t info) {
1222 /* clobber policy return value
1223 * -------------- ------------
1224 * -always original_file
1225 * -auto original_file-<digits>.extension
1226 * -suffix original_file.<digits>
1227 * -ask original_file, 0, or another filename/path
1228 * -never 0
1229 */
1230
1231 char *file;
1232 char *cwd = NULL;
1233 int check_again;
1234
1235 if (! strcmp (original_file, "-")) {
1236 return original_file;
1237 }
1238
1239 if (info->clobber_policy == NMH_CLOBBER_ASK) {
1240 /* Save cwd for possible use in loop below. */
1241 char *slash;
1242
1243 cwd = add (original_file, NULL);
1244 slash = strrchr (cwd, '/');
1245
1246 if (slash) {
1247 *slash = '\0';
1248 } else {
1249 /* original_file isn't a full path, which should only happen if
1250 it is -. */
1251 free (cwd);
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 free (cwd);
1352
1353 return file;
1354 }
1355
1356 /* -clobber support */
1357 /******************************************************************************/