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 neo_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/lib.h"
34 #include "email/lib.h"
35 #include "mutt_body.h"
36 #include "attach/lib.h"
37 #include "send/lib.h"
38 #include "muttlib.h"
39 
40 /**
41  * mutt_body_copy - Create a send-mode duplicate from a receive-mode body
42  * @param[in]  fp  FILE pointer to attachments
43  * @param[out] tgt New Body will be saved here
44  * @param[in]  src Source Body to copy
45  * @retval  0 Success
46  * @retval -1 Failure
47  */
mutt_body_copy(FILE * fp,struct Body ** tgt,struct Body * src)48 int mutt_body_copy(FILE *fp, struct Body **tgt, struct Body *src)
49 {
50   if (!tgt || !src)
51     return -1;
52 
53   struct Body *b = NULL;
54   bool use_disp;
55   struct Buffer *tmp = mutt_buffer_pool_get();
56 
57   if (src->filename)
58   {
59     use_disp = true;
60     mutt_buffer_strcpy(tmp, src->filename);
61   }
62   else
63   {
64     use_disp = false;
65   }
66 
67   mutt_adv_mktemp(tmp);
68   if (mutt_save_attachment(fp, src, mutt_buffer_string(tmp), MUTT_SAVE_NO_FLAGS, NULL) == -1)
69   {
70     mutt_buffer_pool_release(&tmp);
71     return -1;
72   }
73 
74   *tgt = mutt_body_new();
75   b = *tgt;
76 
77   memcpy(b, src, sizeof(struct Body));
78   TAILQ_INIT(&b->parameter);
79   b->parts = NULL;
80   b->next = NULL;
81 
82   b->filename = mutt_buffer_strdup(tmp);
83   b->use_disp = use_disp;
84   b->unlink = true;
85 
86   if (mutt_is_text_part(b))
87     b->noconv = true;
88 
89   b->xtype = mutt_str_dup(b->xtype);
90   b->subtype = mutt_str_dup(b->subtype);
91   b->form_name = mutt_str_dup(b->form_name);
92   b->d_filename = mutt_str_dup(b->d_filename);
93   /* mutt_adv_mktemp() will mangle the filename in tmp,
94    * so preserve it in d_filename */
95   if (!b->d_filename && use_disp)
96     b->d_filename = mutt_str_dup(src->filename);
97   b->description = mutt_str_dup(b->description);
98 
99   /* we don't seem to need the Email structure currently.
100    * XXX this may change in the future */
101 
102   if (b->email)
103     b->email = NULL;
104 
105   /* copy parameters */
106   struct Parameter *np = NULL, *new_param = NULL;
107   TAILQ_FOREACH(np, &src->parameter, entries)
108   {
109     new_param = mutt_param_new();
110     new_param->attribute = mutt_str_dup(np->attribute);
111     new_param->value = mutt_str_dup(np->value);
112     TAILQ_INSERT_HEAD(&b->parameter, new_param, entries);
113   }
114 
115   mutt_stamp_attachment(b);
116   mutt_buffer_pool_release(&tmp);
117   return 0;
118 }
119