forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutt_header.c
390 lines (345 loc) · 9.43 KB
/
mutt_header.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* @file
* Manipulate an email's header
*
* @authors
* Copyright (C) 1996-2009,2012 Michael R. Elkins <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page mutt_header Manipulate an email's header
*
* Manipulate an email's header
*/
#include "config.h"
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
#include "mutt/mutt.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "mutt_header.h"
#include "alias.h"
#include "index.h"
#include "muttlib.h"
#include "ncrypt/ncrypt.h"
#include "options.h"
#include "protos.h"
#include "sendlib.h"
/**
* label_ref_dec - Decrease the refcount of a label
* @param m Mailbox
* @param label Label
*/
static void label_ref_dec(struct Mailbox *m, char *label)
{
struct HashElem *elem = mutt_hash_find_elem(m->label_hash, label);
if (!elem)
return;
uintptr_t count = (uintptr_t) elem->data;
if (count <= 1)
{
mutt_hash_delete(m->label_hash, label, NULL);
return;
}
count--;
elem->data = (void *) count;
}
/**
* label_ref_inc - Increase the refcount of a label
* @param m Mailbox
* @param label Label
*/
static void label_ref_inc(struct Mailbox *m, char *label)
{
uintptr_t count;
struct HashElem *elem = mutt_hash_find_elem(m->label_hash, label);
if (!elem)
{
count = 1;
mutt_hash_insert(m->label_hash, label, (void *) count);
return;
}
count = (uintptr_t) elem->data;
count++;
elem->data = (void *) count;
}
/**
* label_message - add an X-Label: field
* @param[in] m Mailbox
* @param[in] e Email
* @param[out] new_label Set to true if this is a new label
* @retval true If the label was added
*/
static bool label_message(struct Mailbox *m, struct Email *e, char *new_label)
{
if (!e)
return false;
if (mutt_str_strcmp(e->env->x_label, new_label) == 0)
return false;
if (e->env->x_label)
label_ref_dec(m, e->env->x_label);
mutt_str_replace(&e->env->x_label, new_label);
if (e->env->x_label)
label_ref_inc(m, e->env->x_label);
e->changed = true;
e->env->changed |= MUTT_ENV_CHANGED_XLABEL;
return true;
}
/**
* mutt_label_message - Let the user label a message
* @param m Mailbox
* @param el List of Emails to label
* @retval num Number of messages changed
*/
int mutt_label_message(struct Mailbox *m, struct EmailList *el)
{
if (!m || !el)
return 0;
char buf[1024] = { 0 };
struct EmailNode *en = STAILQ_FIRST(el);
if (!STAILQ_NEXT(en, entries))
{
// If there's only one email, use its label as a template
if (en->email->env->x_label)
mutt_str_strfcpy(buf, en->email->env->x_label, sizeof(buf));
}
if (mutt_get_field("Label: ", buf, sizeof(buf), MUTT_LABEL /* | MUTT_CLEAR */) != 0)
return 0;
char *new_label = buf;
SKIPWS(new_label);
if (*new_label == '\0')
new_label = NULL;
int changed = 0;
STAILQ_FOREACH(en, el, entries)
{
if (label_message(m, en->email, new_label))
{
changed++;
mutt_set_header_color(m, en->email);
}
}
return changed;
}
/**
* mutt_edit_headers - Let the user edit the message header and body
* @param editor Editor command
* @param body File containing message body
* @param e Email
* @param fcc Buffer for the fcc field
*/
void mutt_edit_headers(const char *editor, const char *body, struct Email *e,
struct Buffer *fcc)
{
char buf[1024];
const char *p = NULL;
int i;
struct Envelope *n = NULL;
time_t mtime;
struct stat st;
struct Buffer *path = mutt_buffer_pool_get();
mutt_buffer_mktemp(path);
FILE *fp_out = mutt_file_fopen(mutt_b2s(path), "w");
if (!fp_out)
{
mutt_perror(mutt_b2s(path));
goto cleanup;
}
mutt_env_to_local(e->env);
mutt_rfc822_write_header(fp_out, e->env, NULL, MUTT_WRITE_HEADER_EDITHDRS, false, false);
fputc('\n', fp_out); /* tie off the header. */
/* now copy the body of the message. */
FILE *fp_in = fopen(body, "r");
if (!fp_in)
{
mutt_perror(body);
mutt_file_fclose(&fp_out);
goto cleanup;
}
mutt_file_copy_stream(fp_in, fp_out);
mutt_file_fclose(&fp_in);
mutt_file_fclose(&fp_out);
if (stat(mutt_b2s(path), &st) == -1)
{
mutt_perror(mutt_b2s(path));
goto cleanup;
}
mtime = mutt_file_decrease_mtime(mutt_b2s(path), &st);
mutt_edit_file(editor, mutt_b2s(path));
stat(mutt_b2s(path), &st);
if (mtime == st.st_mtime)
{
mutt_debug(LL_DEBUG1, "temp file was not modified\n");
/* the file has not changed! */
mutt_file_unlink(mutt_b2s(path));
goto cleanup;
}
mutt_file_unlink(body);
mutt_list_free(&e->env->userhdrs);
/* Read the temp file back in */
fp_in = fopen(mutt_b2s(path), "r");
if (!fp_in)
{
mutt_perror(mutt_b2s(path));
goto cleanup;
}
fp_out = mutt_file_fopen(body, "w");
if (!fp_out)
{
/* intentionally leak a possible temporary file here */
mutt_file_fclose(&fp_in);
mutt_perror(body);
goto cleanup;
}
n = mutt_rfc822_read_header(fp_in, NULL, true, false);
while ((i = fread(buf, 1, sizeof(buf), fp_in)) > 0)
fwrite(buf, 1, i, fp_out);
mutt_file_fclose(&fp_out);
mutt_file_fclose(&fp_in);
mutt_file_unlink(mutt_b2s(path));
/* in case the user modifies/removes the In-Reply-To header with
* $edit_headers set, we remove References: as they're likely invalid;
* we can simply compare strings as we don't generate References for
* multiple Message-Ids in IRT anyways */
#ifdef USE_NNTP
if (!OptNewsSend)
#endif
{
if (!STAILQ_EMPTY(&e->env->in_reply_to) &&
(STAILQ_EMPTY(&n->in_reply_to) ||
(mutt_str_strcmp(STAILQ_FIRST(&n->in_reply_to)->data,
STAILQ_FIRST(&e->env->in_reply_to)->data) != 0)))
{
mutt_list_free(&e->env->references);
}
}
/* restore old info. */
mutt_list_free(&n->references);
STAILQ_SWAP(&n->references, &e->env->references, ListNode);
mutt_env_free(&e->env);
e->env = n;
n = NULL;
mutt_expand_aliases_env(e->env);
/* search through the user defined headers added to see if
* fcc: or attach: or pgp: was specified */
struct ListNode *np = NULL, *tmp = NULL;
STAILQ_FOREACH_SAFE(np, &e->env->userhdrs, entries, tmp)
{
bool keep = true;
size_t plen;
if (fcc && (plen = mutt_str_startswith(np->data, "fcc:", CASE_IGNORE)))
{
p = mutt_str_skip_email_wsp(np->data + plen);
if (*p)
{
mutt_buffer_strcpy(fcc, p);
mutt_buffer_pretty_mailbox(fcc);
}
keep = false;
}
else if ((plen = mutt_str_startswith(np->data, "attach:", CASE_IGNORE)))
{
struct Body *body2 = NULL;
struct Body *parts = NULL;
p = mutt_str_skip_email_wsp(np->data + plen);
if (*p)
{
mutt_buffer_reset(path);
for (; (p[0] != '\0') && (p[0] != ' ') && (p[0] != '\t'); p++)
{
if (p[0] == '\\')
{
if (p[1] == '\0')
break;
p++;
}
mutt_buffer_addch(path, *p);
}
p = mutt_str_skip_email_wsp(p);
mutt_buffer_expand_path(path);
body2 = mutt_make_file_attach(mutt_b2s(path));
if (body2)
{
body2->description = mutt_str_strdup(p);
for (parts = e->content; parts->next; parts = parts->next)
;
parts->next = body2;
}
else
{
mutt_buffer_pretty_mailbox(path);
mutt_error(_("%s: unable to attach file"), mutt_b2s(path));
}
}
keep = false;
}
else if (((WithCrypto & APPLICATION_PGP) != 0) &&
(plen = mutt_str_startswith(np->data, "pgp:", CASE_IGNORE)))
{
e->security = mutt_parse_crypt_hdr(np->data + plen, false, APPLICATION_PGP);
if (e->security)
e->security |= APPLICATION_PGP;
keep = false;
}
if (!keep)
{
STAILQ_REMOVE(&e->env->userhdrs, np, ListNode, entries);
FREE(&np->data);
FREE(&np);
}
}
cleanup:
mutt_buffer_pool_release(&path);
}
/**
* mutt_make_label_hash - Create a Hash Table to store the labels
* @param m Mailbox
*/
void mutt_make_label_hash(struct Mailbox *m)
{
/* 131 is just a rough prime estimate of how many distinct
* labels someone might have in a m. */
m->label_hash = mutt_hash_new(131, MUTT_HASH_STRDUP_KEYS);
}
/**
* mutt_label_hash_add - Add a message's labels to the Hash Table
* @param m Mailbox
* @param e Email
*/
void mutt_label_hash_add(struct Mailbox *m, struct Email *e)
{
if (!m || !m->label_hash)
return;
if (e->env->x_label)
label_ref_inc(m, e->env->x_label);
}
/**
* mutt_label_hash_remove - Remove a message's labels from the Hash Table
* @param m Mailbox
* @param e Email
*/
void mutt_label_hash_remove(struct Mailbox *m, struct Email *e)
{
if (!m || !m->label_hash)
return;
if (e->env->x_label)
label_ref_dec(m, e->env->x_label);
}