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