forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.c
499 lines (437 loc) · 14.6 KB
/
message.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/**
* @file
* Process a message for display in the pager
*
* @authors
* Copyright (C) 2021-2023 Richard Russon <[email protected]>
* Copyright (C) 2022 Pietro Cerutti <[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 pager_message Process a message for display in the pager
*
* Process a message for display in the pager
*/
#include "config.h"
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "lib.h"
#include "attach/lib.h"
#include "expando/lib.h"
#include "index/lib.h"
#include "key/lib.h"
#include "menu/lib.h"
#include "ncrypt/lib.h"
#include "question/lib.h"
#include "copy.h"
#include "globals.h"
#include "hdrline.h"
#include "hook.h"
#include "mview.h"
#include "mx.h"
#include "protos.h"
#ifdef USE_AUTOCRYPT
#include "autocrypt/lib.h"
#endif
/// Status bar message when entire message is visible in the Pager
static const char *ExtPagerProgress = N_("all");
/**
* process_protected_headers - Get the protected header and update the index
* @param m Mailbox
* @param e Email to update
*/
static void process_protected_headers(struct Mailbox *m, struct Email *e)
{
struct Envelope *prot_headers = NULL;
const bool c_crypt_protected_headers_read = cs_subset_bool(NeoMutt->sub, "crypt_protected_headers_read");
#ifdef USE_AUTOCRYPT
const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
if (!c_crypt_protected_headers_read && !c_autocrypt)
return;
#else
if (!c_crypt_protected_headers_read)
return;
#endif
/* Grab protected headers to update in the index */
if (e->security & SEC_SIGN)
{
/* Don't update on a bad signature.
*
* This is a simplification. It's possible the headers are in the
* encrypted part of a nested encrypt/signed. But properly handling that
* case would require more complexity in the decryption handlers, which
* I'm not sure is worth it. */
if (!(e->security & SEC_GOODSIGN))
return;
if (mutt_is_multipart_signed(e->body) && e->body->parts)
{
prot_headers = e->body->parts->mime_headers;
}
else if (((WithCrypto & APPLICATION_SMIME) != 0) && mutt_is_application_smime(e->body))
{
prot_headers = e->body->mime_headers;
}
}
if (!prot_headers && (e->security & SEC_ENCRYPT))
{
if (((WithCrypto & APPLICATION_PGP) != 0) &&
(mutt_is_valid_multipart_pgp_encrypted(e->body) ||
mutt_is_malformed_multipart_pgp_encrypted(e->body)))
{
prot_headers = e->body->mime_headers;
}
else if (((WithCrypto & APPLICATION_SMIME) != 0) && mutt_is_application_smime(e->body))
{
prot_headers = e->body->mime_headers;
}
}
/* Update protected headers in the index and header cache. */
if (c_crypt_protected_headers_read && prot_headers && prot_headers->subject &&
!mutt_str_equal(e->env->subject, prot_headers->subject))
{
if (m->subj_hash && e->env->real_subj)
mutt_hash_delete(m->subj_hash, e->env->real_subj, e);
mutt_env_set_subject(e->env, prot_headers->subject);
FREE(&e->env->disp_subj);
if (m->subj_hash)
mutt_hash_insert(m->subj_hash, e->env->real_subj, e);
mx_save_hcache(m, e);
/* Also persist back to the message headers if this is set */
const bool c_crypt_protected_headers_save = cs_subset_bool(NeoMutt->sub, "crypt_protected_headers_save");
if (c_crypt_protected_headers_save)
{
e->env->changed |= MUTT_ENV_CHANGED_SUBJECT;
e->changed = true;
m->changed = true;
}
}
#ifdef USE_AUTOCRYPT
if (c_autocrypt && (e->security & SEC_ENCRYPT) && prot_headers && prot_headers->autocrypt_gossip)
{
mutt_autocrypt_process_gossip_header(e, prot_headers);
}
#endif
}
/**
* email_to_file - Decrypt, decode and weed an Email into a file
* @param msg Raw Email
* @param tempfile Temporary filename for result
* @param m Mailbox
* @param e Email to display
* @param header Header to prefix output (OPTIONAL)
* @param wrap_len Width to wrap lines
* @param cmflags Message flags, e.g. #MUTT_CM_DECODE
* @retval 0 Success
* @retval -1 Error
*
* @note Flags may be added to @a cmflags
*/
static int email_to_file(struct Message *msg, struct Buffer *tempfile,
struct Mailbox *m, struct Email *e, const char *header,
int wrap_len, CopyMessageFlags *cmflags)
{
int rc = 0;
pid_t filterpid = -1;
mutt_parse_mime_message(e, msg->fp);
mutt_message_hook(m, e, MUTT_MESSAGE_HOOK);
char columns[16] = { 0 };
// win_pager might not be visible and have a size yet, so use win_index
snprintf(columns, sizeof(columns), "%d", wrap_len);
envlist_set(&EnvList, "COLUMNS", columns, true);
/* see if crypto is needed for this message. if so, we should exit curses */
if ((WithCrypto != 0) && e->security)
{
if (e->security & SEC_ENCRYPT)
{
if (e->security & APPLICATION_SMIME)
crypt_smime_getkeys(e->env);
if (!crypt_valid_passphrase(e->security))
goto cleanup;
*cmflags |= MUTT_CM_VERIFY;
}
else if (e->security & SEC_SIGN)
{
/* find out whether or not the verify signature */
/* L10N: Used for the $crypt_verify_sig prompt */
if (query_quadoption(_("Verify signature?"), NeoMutt->sub, "crypt_verify_sig") == MUTT_YES)
{
*cmflags |= MUTT_CM_VERIFY;
}
}
}
if (*cmflags & MUTT_CM_VERIFY || e->security & SEC_ENCRYPT)
{
if (e->security & APPLICATION_PGP)
{
if (!TAILQ_EMPTY(&e->env->from))
crypt_pgp_invoke_getkeys(TAILQ_FIRST(&e->env->from));
crypt_invoke_message(APPLICATION_PGP);
}
if (e->security & APPLICATION_SMIME)
crypt_invoke_message(APPLICATION_SMIME);
}
FILE *fp_filter_out = NULL;
buf_mktemp(tempfile);
FILE *fp_out = mutt_file_fopen(buf_string(tempfile), "w");
if (!fp_out)
{
mutt_error(_("Could not create temporary file"));
goto cleanup;
}
const char *const c_display_filter = cs_subset_string(NeoMutt->sub, "display_filter");
if (c_display_filter)
{
fp_filter_out = fp_out;
fp_out = NULL;
filterpid = filter_create_fd(c_display_filter, &fp_out, NULL, NULL, -1,
fileno(fp_filter_out), -1, EnvList);
if (filterpid < 0)
{
mutt_error(_("Can't create display filter"));
mutt_file_fclose(&fp_filter_out);
unlink(buf_string(tempfile));
goto cleanup;
}
}
if (header)
{
fputs(header, fp_out);
fputs("\n\n", fp_out);
}
const bool c_weed = cs_subset_bool(NeoMutt->sub, "weed");
CopyHeaderFlags chflags = (c_weed ? (CH_WEED | CH_REORDER) : CH_NO_FLAGS) |
CH_DECODE | CH_FROM | CH_DISPLAY;
#ifdef USE_NOTMUCH
if (m->type == MUTT_NOTMUCH)
chflags |= CH_VIRTUAL;
#endif
rc = mutt_copy_message(fp_out, e, msg, *cmflags, chflags, wrap_len);
if (((mutt_file_fclose(&fp_out) != 0) && (errno != EPIPE)) || (rc < 0))
{
mutt_error(_("Could not copy message"));
if (fp_filter_out)
{
filter_wait(filterpid);
mutt_file_fclose(&fp_filter_out);
}
mutt_file_unlink(buf_string(tempfile));
goto cleanup;
}
if (fp_filter_out && (filter_wait(filterpid) != 0))
mutt_any_key_to_continue(NULL);
mutt_file_fclose(&fp_filter_out); /* XXX - check result? */
if (WithCrypto)
{
/* update crypto information for this message */
e->security &= ~(SEC_GOODSIGN | SEC_BADSIGN);
e->security |= crypt_query(e->body);
/* Remove color cache for this message, in case there
* are color patterns for both ~g and ~V */
e->attr_color = NULL;
/* Process protected headers and autocrypt gossip headers */
process_protected_headers(m, e);
}
cleanup:
envlist_unset(&EnvList, "COLUMNS");
return rc;
}
/**
* external_pager - Display a message in an external program
* @param mv Mailbox view
* @param e Email to display
* @param command External command to run
* @retval 0 Success
* @retval -1 Error
*/
int external_pager(struct MailboxView *mv, struct Email *e, const char *command)
{
if (!mv || !mv->mailbox)
return -1;
struct Mailbox *m = mv->mailbox;
struct Message *msg = mx_msg_open(m, e);
if (!msg)
return -1;
struct Buffer *buf = buf_pool_get();
const struct Expando *c_pager_format = cs_subset_expando(NeoMutt->sub, "pager_format");
const int screen_width = RootWindow->state.cols;
mutt_make_string(buf, screen_width, c_pager_format, m, -1, e,
MUTT_FORMAT_NO_FLAGS, _(ExtPagerProgress));
struct Buffer *tempfile = buf_pool_get();
CopyMessageFlags cmflags = MUTT_CM_DECODE | MUTT_CM_DISPLAY | MUTT_CM_CHARCONV;
int rc = email_to_file(msg, tempfile, m, e, buf_string(buf), screen_width, &cmflags);
if (rc < 0)
goto cleanup;
mutt_endwin();
struct Buffer *cmd = buf_pool_get();
buf_printf(cmd, "%s %s", command, buf_string(tempfile));
int r = mutt_system(buf_string(cmd));
if (r == -1)
mutt_error(_("Error running \"%s\""), buf_string(cmd));
unlink(buf_string(tempfile));
buf_pool_release(&cmd);
if (!OptNoCurses)
keypad(stdscr, true);
if (r != -1)
mutt_set_flag(m, e, MUTT_READ, true, true);
const bool c_prompt_after = cs_subset_bool(NeoMutt->sub, "prompt_after");
if ((r != -1) && c_prompt_after)
{
mutt_unget_ch(mutt_any_key_to_continue(_("Command: ")));
rc = km_dokey(MENU_PAGER, GETCH_NO_FLAGS);
}
else
{
rc = 0;
}
cleanup:
buf_pool_release(&buf);
mx_msg_close(m, &msg);
buf_pool_release(&tempfile);
return rc;
}
/**
* notify_crypto - Notify the user about the crypto status of the Email
* @param e Email to display
* @param msg Raw Email
* @param cmflags Message flags, e.g. #MUTT_CM_DECODE
*/
static void notify_crypto(struct Email *e, struct Message *msg, CopyMessageFlags cmflags)
{
if ((WithCrypto != 0) && (e->security & APPLICATION_SMIME) && (cmflags & MUTT_CM_VERIFY))
{
if (e->security & SEC_GOODSIGN)
{
if (crypt_smime_verify_sender(e, msg) == 0)
mutt_message(_("S/MIME signature successfully verified"));
else
mutt_error(_("S/MIME certificate owner does not match sender"));
}
else if (e->security & SEC_PARTSIGN)
{
mutt_message(_("Warning: Part of this message has not been signed"));
}
else if (e->security & SEC_SIGN || e->security & SEC_BADSIGN)
{
mutt_error(_("S/MIME signature could NOT be verified"));
}
}
if ((WithCrypto != 0) && (e->security & APPLICATION_PGP) && (cmflags & MUTT_CM_VERIFY))
{
if (e->security & SEC_GOODSIGN)
mutt_message(_("PGP signature successfully verified"));
else if (e->security & SEC_PARTSIGN)
mutt_message(_("Warning: Part of this message has not been signed"));
else if (e->security & SEC_SIGN)
mutt_message(_("PGP signature could NOT be verified"));
}
}
/**
* squash_index_panel - Shrink or hide the Index Panel
* @param m Mailbox
* @param win_index Index Window
* @param win_pager Pager Window
*/
static void squash_index_panel(struct Mailbox *m, struct MuttWindow *win_index,
struct MuttWindow *win_pager)
{
const short c_pager_index_lines = cs_subset_number(NeoMutt->sub, "pager_index_lines");
if (c_pager_index_lines > 0)
{
win_index->size = MUTT_WIN_SIZE_FIXED;
win_index->req_rows = c_pager_index_lines;
win_index->parent->size = MUTT_WIN_SIZE_MINIMISE;
}
window_set_visible(win_index->parent, (c_pager_index_lines > 0));
window_set_visible(win_pager->parent, true);
struct MuttWindow *dlg = dialog_find(win_index);
mutt_window_reflow(dlg);
// Force the menu to reframe itself
struct Menu *menu = win_index->wdata;
menu_set_index(menu, menu_get_index(menu));
}
/**
* expand_index_panel - Restore the Index Panel
* @param win_index Index Window
* @param win_pager Pager Window
*/
static void expand_index_panel(struct MuttWindow *win_index, struct MuttWindow *win_pager)
{
win_index->size = MUTT_WIN_SIZE_MAXIMISE;
win_index->req_rows = MUTT_WIN_SIZE_UNLIMITED;
win_index->parent->size = MUTT_WIN_SIZE_MAXIMISE;
win_index->parent->req_rows = MUTT_WIN_SIZE_UNLIMITED;
window_set_visible(win_index->parent, true);
window_set_visible(win_pager->parent, false);
struct MuttWindow *dlg = dialog_find(win_index);
mutt_window_reflow(dlg);
}
/**
* mutt_display_message - Display a message in the pager
* @param win_index Index Window
* @param shared Shared Index data
* @retval 0 Success
* @retval -1 Error
*/
int mutt_display_message(struct MuttWindow *win_index, struct IndexSharedData *shared)
{
struct MuttWindow *dlg = dialog_find(win_index);
struct MuttWindow *win_pager = window_find_child(dlg, WT_CUSTOM);
struct MuttWindow *win_pbar = window_find_child(dlg, WT_STATUS_BAR);
struct Buffer *tempfile = buf_pool_get();
struct Message *msg = NULL;
squash_index_panel(shared->mailbox, win_index, win_pager);
int rc = PAGER_LOOP_QUIT;
do
{
msg = mx_msg_open(shared->mailbox, shared->email);
if (!msg)
break;
CopyMessageFlags cmflags = MUTT_CM_DECODE | MUTT_CM_DISPLAY | MUTT_CM_CHARCONV;
buf_reset(tempfile);
// win_pager might not be visible and have a size yet, so use win_index
rc = email_to_file(msg, tempfile, shared->mailbox, shared->email, NULL,
win_index->state.cols, &cmflags);
if (rc < 0)
break;
notify_crypto(shared->email, msg, cmflags);
/* Invoke the builtin pager */
struct PagerData pdata = { 0 };
struct PagerView pview = { &pdata };
pdata.fp = msg->fp;
pdata.fname = buf_string(tempfile);
pview.mode = PAGER_MODE_EMAIL;
pview.banner = NULL;
pview.flags = MUTT_PAGER_MESSAGE |
(shared->email->body->nowrap ? MUTT_PAGER_NOWRAP : 0);
pview.win_index = win_index;
pview.win_pbar = win_pbar;
pview.win_pager = win_pager;
rc = dlg_pager(&pview);
mx_msg_close(shared->mailbox, &msg);
} while (rc == PAGER_LOOP_RELOAD);
expand_index_panel(win_index, win_pager);
mx_msg_close(shared->mailbox, &msg);
buf_pool_release(&tempfile);
return rc;
}