NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
cid.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include <stddef.h>
33#include <stdio.h>
34#include <string.h>
35#include "email/lib.h"
36#include "core/lib.h"
37#include "cid.h"
38#include "attach.h"
39#include "mailcap.h"
40#include "mutt_attach.h"
41
46void cid_map_free(struct CidMap **ptr)
47{
48 if (!ptr || !*ptr)
49 return;
50
51 struct CidMap *cid_map = *ptr;
52
53 FREE(&cid_map->cid);
54 FREE(&cid_map->fname);
55
56 FREE(ptr);
57}
58
65struct CidMap *cid_map_new(const char *cid, const char *filename)
66{
67 if (!cid || !filename)
68 return NULL;
69
70 struct CidMap *cid_map = MUTT_MEM_CALLOC(1, struct CidMap);
71
72 cid_map->cid = mutt_str_dup(cid);
73 cid_map->fname = mutt_str_dup(filename);
74
75 return cid_map;
76}
77
82void cid_map_list_clear(struct CidMapList *cid_map_list)
83{
84 if (!cid_map_list)
85 return;
86
87 while (!STAILQ_EMPTY(cid_map_list))
88 {
89 struct CidMap *cid_map = STAILQ_FIRST(cid_map_list);
90 STAILQ_REMOVE_HEAD(cid_map_list, entries);
91 cid_map_free(&cid_map);
92 }
93}
94
103static void cid_save_attachment(struct Body *b, struct CidMapList *cid_map_list)
104{
105 if (!b || !cid_map_list)
106 return;
107
108 char *id = b->content_id;
109 if (!id)
110 return;
111
112 struct Buffer *tmpfile = buf_pool_get();
113 struct Buffer *cid = buf_pool_get();
114 bool has_tempfile = false;
115 FILE *fp = NULL;
116
117 mutt_debug(LL_DEBUG2, "attachment found with \"Content-ID: %s\"\n", id);
118 /* get filename */
119 char *fname = mutt_str_dup(b->filename);
120 if (b->aptr)
121 fp = b->aptr->fp;
122 mutt_file_sanitize_filename(fname, fp ? true : false);
123 mailcap_expand_filename("%s", fname, tmpfile);
124 FREE(&fname);
125
126 /* save attachment */
127 if (mutt_save_attachment(fp, b, buf_string(tmpfile), 0, NULL) == -1)
128 goto bail;
129 has_tempfile = true;
130 mutt_debug(LL_DEBUG2, "attachment with \"Content-ID: %s\" saved to file \"%s\"\n",
131 id, buf_string(tmpfile));
132
133 /* add Content-ID to filename mapping to list */
134 buf_printf(cid, "cid:%s", id);
135 struct CidMap *cid_map = cid_map_new(buf_string(cid), buf_string(tmpfile));
136 STAILQ_INSERT_TAIL(cid_map_list, cid_map, entries);
137
138bail:
139
140 if ((fp && !buf_is_empty(tmpfile)) || has_tempfile)
142 buf_pool_release(&tmpfile);
144}
145
151void cid_save_attachments(struct Body *body, struct CidMapList *cid_map_list)
152{
153 if (!body || !cid_map_list)
154 return;
155
156 for (struct Body *b = body; b; b = b->next)
157 {
158 if (b->parts)
159 cid_save_attachments(b->parts, cid_map_list);
160 else
161 cid_save_attachment(b, cid_map_list);
162 }
163}
164
170void cid_to_filename(struct Buffer *filename, const struct CidMapList *cid_map_list)
171{
172 if (!filename || !cid_map_list)
173 return;
174
175 FILE *fp_out = NULL;
176 char *pbuf = NULL;
177 char *searchbuf = NULL;
178 char *buf = NULL;
179 char *cid = NULL;
180 size_t blen = 0;
181 struct CidMap *cid_map = NULL;
182
183 struct Buffer *tmpfile = buf_pool_get();
184 struct Buffer *tmpbuf = buf_pool_get();
185
186 FILE *fp_in = mutt_file_fopen(buf_string(filename), "r");
187 if (!fp_in)
188 goto bail;
189
190 /* ensure tmpfile has the same file extension as filename otherwise an
191 * HTML file may be opened as plain text by the viewer */
192 const char *suffix = buf_rfind(filename, ".");
193 if (suffix && *(suffix++))
194 buf_mktemp_pfx_sfx(tmpfile, "neomutt", suffix);
195 else
196 buf_mktemp(tmpfile);
197 fp_out = mutt_file_fopen(buf_string(tmpfile), "w+");
198 if (!fp_out)
199 goto bail;
200
201 /* Read in lines from filename into buf */
202 while ((buf = mutt_file_read_line(buf, &blen, fp_in, NULL, MUTT_RL_NO_FLAGS)) != NULL)
203 {
204 if (mutt_str_len(buf) == 0)
205 {
206 fputs(buf, fp_out);
207 continue;
208 }
209
210 /* copy buf to searchbuf because we need to edit multiple times */
211 searchbuf = mutt_str_dup(buf);
212 buf_reset(tmpbuf);
213
214 /* loop through Content-ID to filename mappings and do search and replace */
215 STAILQ_FOREACH(cid_map, cid_map_list, entries)
216 {
217 pbuf = searchbuf;
218 while ((cid = strstr(pbuf, cid_map->cid)) != NULL)
219 {
220 buf_addstr_n(tmpbuf, pbuf, cid - pbuf);
221 buf_addstr(tmpbuf, cid_map->fname);
222 pbuf = cid + mutt_str_len(cid_map->cid);
223 mutt_debug(LL_DEBUG2, "replaced \"%s\" with \"%s\" in file \"%s\"\n",
224 cid_map->cid, cid_map->fname, buf_string(filename));
225 }
226 buf_addstr(tmpbuf, pbuf);
227 FREE(&searchbuf);
228 searchbuf = buf_strdup(tmpbuf);
229 buf_reset(tmpbuf);
230 }
231
232 /* write edited line to output file */
233 fputs(searchbuf, fp_out);
234 fputs("\n", fp_out);
235 FREE(&searchbuf);
236 }
237
238 mutt_file_set_mtime(buf_string(filename), buf_string(tmpfile));
239
240 /* add filename to TempAtachmentsList so it doesn't get left lying around */
242 /* update filename to point to new file */
243 buf_copy(filename, tmpfile);
244
245bail:
246 FREE(&buf);
247 mutt_file_fclose(&fp_in);
248 mutt_file_fclose(&fp_out);
249 buf_pool_release(&tmpfile);
250 buf_pool_release(&tmpbuf);
251}
Handling of email attachments.
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
size_t buf_addstr_n(struct Buffer *buf, const char *s, size_t len)
Add a string to a Buffer, expanding it if necessary.
Definition: buffer.c:96
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
const char * buf_rfind(const struct Buffer *buf, const char *str)
Find last instance of a substring.
Definition: buffer.c:797
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition: buffer.c:601
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:571
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
struct CidMap * cid_map_new(const char *cid, const char *filename)
Initialise a new CidMap.
Definition: cid.c:65
void cid_map_free(struct CidMap **ptr)
Free a CidMap.
Definition: cid.c:46
static void cid_save_attachment(struct Body *b, struct CidMapList *cid_map_list)
Save attachment if it has a Content-ID.
Definition: cid.c:103
void cid_save_attachments(struct Body *body, struct CidMapList *cid_map_list)
Save all attachments in a "multipart/related" group with a Content-ID.
Definition: cid.c:151
void cid_to_filename(struct Buffer *filename, const struct CidMapList *cid_map_list)
Replace Content-IDs with filenames.
Definition: cid.c:170
void cid_map_list_clear(struct CidMapList *cid_map_list)
Empty a CidMapList.
Definition: cid.c:82
Attachment Content-ID header functions.
Convenience wrapper for the core headers.
Structs that make up an email.
char * mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
Read a line from a file.
Definition: file.c:808
void mutt_file_sanitize_filename(char *path, bool slash)
Replace unsafe characters in a filename.
Definition: file.c:712
void mutt_file_set_mtime(const char *from, const char *to)
Set the modification time of one file from another.
Definition: file.c:1068
#define mutt_file_fclose(FP)
Definition: file.h:138
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:137
#define MUTT_RL_NO_FLAGS
No flags are set.
Definition: file.h:40
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
void mailcap_expand_filename(const char *nametemplate, const char *oldfile, struct Buffer *newfile)
Expand a new filename from a template or existing filename.
Definition: mailcap.c:552
RFC1524 Mailcap routines.
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
int mutt_save_attachment(FILE *fp, struct Body *b, const char *path, enum SaveAttach opt, struct Email *e)
Save an attachment.
Definition: mutt_attach.c:907
void mutt_add_temp_attachment(const char *filename)
Add file to list of temporary attachments.
Definition: mutt_attach.c:1301
Handling of email attachments.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
#define STAILQ_REMOVE_HEAD(head, field)
Definition: queue.h:422
#define STAILQ_FIRST(head)
Definition: queue.h:350
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
#define STAILQ_EMPTY(head)
Definition: queue.h:348
FILE * fp
Used in the recvattach menu.
Definition: attach.h:39
The body of an email.
Definition: body.h:36
char * content_id
Content-Id (RFC2392)
Definition: body.h:58
struct AttachPtr * aptr
Menu information, used in recvattach.c.
Definition: body.h:75
struct Body * next
next attachment in the list
Definition: body.h:72
char * filename
When sending a message, this is the file to which this structure refers.
Definition: body.h:59
String manipulation buffer.
Definition: buffer.h:36
List of Content-ID to filename mappings.
Definition: cid.h:36
char * fname
Filename.
Definition: cid.h:38
char * cid
Content-ID.
Definition: cid.h:37
#define buf_mktemp(buf)
Definition: tmp.h:33
#define buf_mktemp_pfx_sfx(buf, prefix, suffix)
Definition: tmp.h:34