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