]> granicus.if.org Git - neomutt/blob - mutt_body.c
Fix mutt_write_mime_body() application/pgp-encrypted handling
[neomutt] / mutt_body.c
1 /**
2  * @file
3  * Representation of the body of an email
4  *
5  * @authors
6  * Copyright (C) 2017 Richard Russon <rich@flatcap.org>
7  *
8  * @copyright
9  * This program is free software: you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free Software
11  * Foundation, either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 /**
24  * @page mutt_body Representation of the body of an email
25  *
26  * Representation of the body of an email
27  */
28
29 #include "config.h"
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include "mutt/mutt.h"
34 #include "email/lib.h"
35 #include "mutt_attach.h"
36 #include "muttlib.h"
37 #include "sendlib.h"
38
39 /**
40  * mutt_body_copy - Create a send-mode duplicate from a receive-mode body
41  * @param[in]  fp  FILE pointer to attachments
42  * @param[out] tgt New Body will be saved here
43  * @param[in]  src Source Body to copy
44  * @retval  0 Success
45  * @retval -1 Failure
46  */
47 int mutt_body_copy(FILE *fp, struct Body **tgt, struct Body *src)
48 {
49   if (!tgt || !src)
50     return -1;
51
52   struct Body *b = NULL;
53   bool use_disp;
54   struct Buffer *tmp = mutt_buffer_pool_get();
55
56   if (src->filename)
57   {
58     use_disp = true;
59     mutt_buffer_strcpy(tmp, src->filename);
60   }
61   else
62   {
63     use_disp = false;
64   }
65
66   mutt_adv_mktemp(tmp);
67   if (mutt_save_attachment(fp, src, mutt_b2s(tmp), MUTT_SAVE_NO_FLAGS, NULL) == -1)
68   {
69     mutt_buffer_pool_release(&tmp);
70     return -1;
71   }
72
73   *tgt = mutt_body_new();
74   b = *tgt;
75
76   memcpy(b, src, sizeof(struct Body));
77   TAILQ_INIT(&b->parameter);
78   b->parts = NULL;
79   b->next = NULL;
80
81   b->filename = mutt_str_strdup(mutt_b2s(tmp));
82   b->use_disp = use_disp;
83   b->unlink = true;
84
85   if (mutt_is_text_part(b))
86     b->noconv = true;
87
88   b->xtype = mutt_str_strdup(b->xtype);
89   b->subtype = mutt_str_strdup(b->subtype);
90   b->form_name = mutt_str_strdup(b->form_name);
91   b->d_filename = mutt_str_strdup(b->d_filename);
92   /* mutt_adv_mktemp() will mangle the filename in tmp,
93    * so preserve it in d_filename */
94   if (!b->d_filename && use_disp)
95     b->d_filename = mutt_str_strdup(src->filename);
96   b->description = mutt_str_strdup(b->description);
97
98   /* we don't seem to need the Email structure currently.
99    * XXX this may change in the future */
100
101   if (b->email)
102     b->email = NULL;
103
104   /* copy parameters */
105   struct Parameter *np = NULL, *new_param = NULL;
106   TAILQ_FOREACH(np, &src->parameter, entries)
107   {
108     new_param = mutt_param_new();
109     new_param->attribute = mutt_str_strdup(np->attribute);
110     new_param->value = mutt_str_strdup(np->value);
111     TAILQ_INSERT_HEAD(&b->parameter, new_param, entries);
112   }
113
114   mutt_stamp_attachment(b);
115   mutt_buffer_pool_release(&tmp);
116   return 0;
117 }