forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
554 lines (489 loc) · 16.6 KB
/
functions.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/**
* @file
* Envelope functions
*
* @authors
* Copyright (C) 2022-2023 Richard Russon <[email protected]>
* Copyright (C) 2023 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 envelope_functions Envelope functions
*
* Envelope functions
*/
#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "private.h"
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "alias/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "functions.h"
#include "lib.h"
#include "browser/lib.h"
#include "editor/lib.h"
#include "history/lib.h"
#include "ncrypt/lib.h"
#include "question/lib.h"
#include "hook.h"
#include "mutt_logging.h"
#include "muttlib.h"
#include "wdata.h"
#ifdef USE_AUTOCRYPT
#include "autocrypt/lib.h"
#endif
#ifdef USE_AUTOCRYPT
/**
* autocrypt_compose_menu - Autocrypt compose settings
* @param e Email
* @param sub ConfigSubset
*/
static void autocrypt_compose_menu(struct Email *e, const struct ConfigSubset *sub)
{
/* L10N: The compose menu autocrypt prompt.
(e)ncrypt enables encryption via autocrypt.
(c)lear sets cleartext.
(a)utomatic defers to the recommendation. */
const char *prompt = _("Autocrypt: (e)ncrypt, (c)lear, (a)utomatic?");
e->security |= APPLICATION_PGP;
/* L10N: The letter corresponding to the compose menu autocrypt prompt
(e)ncrypt, (c)lear, (a)utomatic */
const char *letters = _("eca");
int choice = mw_multi_choice(prompt, letters);
switch (choice)
{
case 1:
e->security |= (SEC_AUTOCRYPT | SEC_AUTOCRYPT_OVERRIDE);
e->security &= ~(SEC_ENCRYPT | SEC_SIGN | SEC_OPPENCRYPT | SEC_INLINE);
break;
case 2:
e->security &= ~SEC_AUTOCRYPT;
e->security |= SEC_AUTOCRYPT_OVERRIDE;
break;
case 3:
{
e->security &= ~SEC_AUTOCRYPT_OVERRIDE;
const bool c_crypt_opportunistic_encrypt = cs_subset_bool(sub, "crypt_opportunistic_encrypt");
if (c_crypt_opportunistic_encrypt)
e->security |= SEC_OPPENCRYPT;
break;
}
}
}
#endif
/**
* edit_address_list - Let the user edit the address list
* @param[in] field Field to edit, e.g. #HDR_FROM
* @param[in,out] al AddressList to edit
* @retval true The address list was changed
*/
static bool edit_address_list(enum HeaderField field, struct AddressList *al)
{
struct Buffer *old_list = buf_pool_get();
struct Buffer *new_list = buf_pool_get();
/* need to be large for alias expansion */
buf_alloc(old_list, 8192);
buf_alloc(new_list, 8192);
mutt_addrlist_to_local(al);
mutt_addrlist_write(al, new_list, false);
buf_fix_dptr(new_list);
if (!buf_is_empty(new_list))
buf_addstr(new_list, ", ");
buf_copy(old_list, new_list);
if (mw_get_field(_(Prompts[field]), new_list, MUTT_COMP_NO_FLAGS, HC_ALIAS,
&CompleteAliasOps, NULL) == 0)
{
mutt_addrlist_clear(al);
mutt_addrlist_parse2(al, buf_string(new_list));
mutt_expand_aliases(al);
}
char *err = NULL;
if (mutt_addrlist_to_intl(al, &err) != 0)
{
mutt_error(_("Bad IDN: '%s'"), err);
mutt_refresh();
FREE(&err);
}
const bool rc = !mutt_str_equal(buf_string(new_list), buf_string(old_list));
buf_pool_release(&old_list);
buf_pool_release(&new_list);
return rc;
}
/**
* update_crypt_info - Update the crypto info
* @param wdata Envelope Window data
*/
void update_crypt_info(struct EnvelopeWindowData *wdata)
{
struct Email *e = wdata->email;
const bool c_crypt_opportunistic_encrypt = cs_subset_bool(wdata->sub, "crypt_opportunistic_encrypt");
if (c_crypt_opportunistic_encrypt)
crypt_opportunistic_encrypt(e);
#ifdef USE_AUTOCRYPT
const bool c_autocrypt = cs_subset_bool(wdata->sub, "autocrypt");
if (c_autocrypt)
{
wdata->autocrypt_rec = mutt_autocrypt_ui_recommendation(e, NULL);
/* Anything that enables SEC_ENCRYPT or SEC_SIGN, or turns on SMIME
* overrides autocrypt, be it oppenc or the user having turned on
* those flags manually. */
if (e->security & (SEC_ENCRYPT | SEC_SIGN | APPLICATION_SMIME))
{
e->security &= ~(SEC_AUTOCRYPT | SEC_AUTOCRYPT_OVERRIDE);
}
else
{
if (!(e->security & SEC_AUTOCRYPT_OVERRIDE))
{
if (wdata->autocrypt_rec == AUTOCRYPT_REC_YES)
{
e->security |= (SEC_AUTOCRYPT | APPLICATION_PGP);
e->security &= ~(SEC_INLINE | APPLICATION_SMIME);
}
else
{
e->security &= ~SEC_AUTOCRYPT;
}
}
}
}
#endif
}
// -----------------------------------------------------------------------------
/**
* op_envelope_edit_bcc - Edit the BCC list - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_envelope_edit_bcc(struct EnvelopeWindowData *wdata, int op)
{
if (wdata->is_news)
return FR_NO_ACTION;
if (!edit_address_list(HDR_BCC, &wdata->email->env->bcc))
return FR_NO_ACTION;
update_crypt_info(wdata);
mutt_env_notify_send(wdata->email, NT_ENVELOPE_BCC);
return FR_SUCCESS;
}
/**
* op_envelope_edit_cc - Edit the CC list - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_envelope_edit_cc(struct EnvelopeWindowData *wdata, int op)
{
if (wdata->is_news)
return FR_NO_ACTION;
if (!edit_address_list(HDR_CC, &wdata->email->env->cc))
return FR_NO_ACTION;
update_crypt_info(wdata);
mutt_env_notify_send(wdata->email, NT_ENVELOPE_CC);
return FR_SUCCESS;
}
/**
* op_envelope_edit_fcc - Enter a file to save a copy of this message in - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_envelope_edit_fcc(struct EnvelopeWindowData *wdata, int op)
{
int rc = FR_NO_ACTION;
struct Buffer *fname = buf_pool_get();
buf_copy(fname, wdata->fcc);
struct FileCompletionData cdata = { false, NULL, NULL, NULL };
if (mw_get_field(Prompts[HDR_FCC], fname, MUTT_COMP_CLEAR, HC_MAILBOX,
&CompleteMailboxOps, &cdata) != 0)
{
goto done; // aborted
}
if (mutt_str_equal(wdata->fcc->data, fname->data))
goto done; // no change
buf_copy(wdata->fcc, fname);
buf_pretty_mailbox(wdata->fcc);
mutt_env_notify_send(wdata->email, NT_ENVELOPE_FCC);
rc = FR_SUCCESS;
done:
buf_pool_release(&fname);
return rc;
}
/**
* op_envelope_edit_from - Edit the from field - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_envelope_edit_from(struct EnvelopeWindowData *wdata, int op)
{
if (!edit_address_list(HDR_FROM, &wdata->email->env->from))
return FR_NO_ACTION;
update_crypt_info(wdata);
mutt_env_notify_send(wdata->email, NT_ENVELOPE_FROM);
return FR_SUCCESS;
}
/**
* op_envelope_edit_reply_to - Edit the Reply-To field - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_envelope_edit_reply_to(struct EnvelopeWindowData *wdata, int op)
{
if (!edit_address_list(HDR_REPLYTO, &wdata->email->env->reply_to))
return FR_NO_ACTION;
mutt_env_notify_send(wdata->email, NT_ENVELOPE_REPLY_TO);
return FR_SUCCESS;
}
/**
* op_envelope_edit_subject - Edit the subject of this message - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_envelope_edit_subject(struct EnvelopeWindowData *wdata, int op)
{
int rc = FR_NO_ACTION;
struct Buffer *buf = buf_pool_get();
buf_strcpy(buf, wdata->email->env->subject);
if (mw_get_field(Prompts[HDR_SUBJECT], buf, MUTT_COMP_NO_FLAGS, HC_OTHER, NULL, NULL) != 0)
{
goto done; // aborted
}
if (mutt_str_equal(wdata->email->env->subject, buf_string(buf)))
goto done; // no change
mutt_env_set_subject(wdata->email->env, buf_string(buf));
mutt_env_notify_send(wdata->email, NT_ENVELOPE_SUBJECT);
rc = FR_SUCCESS;
done:
buf_pool_release(&buf);
return rc;
}
/**
* op_envelope_edit_to - Edit the TO list - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_envelope_edit_to(struct EnvelopeWindowData *wdata, int op)
{
if (wdata->is_news)
return FR_NO_ACTION;
if (!edit_address_list(HDR_TO, &wdata->email->env->to))
return FR_NO_ACTION;
update_crypt_info(wdata);
mutt_env_notify_send(wdata->email, NT_ENVELOPE_TO);
return FR_SUCCESS;
}
/**
* op_compose_pgp_menu - Show PGP options - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_compose_pgp_menu(struct EnvelopeWindowData *wdata, int op)
{
const SecurityFlags old_flags = wdata->email->security;
if (!(WithCrypto & APPLICATION_PGP))
return FR_NOT_IMPL;
if (!crypt_has_module_backend(APPLICATION_PGP))
{
mutt_error(_("No PGP backend configured"));
return FR_ERROR;
}
if (((WithCrypto & APPLICATION_SMIME) != 0) && (wdata->email->security & APPLICATION_SMIME))
{
if (wdata->email->security & (SEC_ENCRYPT | SEC_SIGN))
{
if (query_yesorno(_("S/MIME already selected. Clear and continue?"), MUTT_YES) != MUTT_YES)
{
mutt_clear_error();
return FR_NO_ACTION;
}
wdata->email->security &= ~(SEC_ENCRYPT | SEC_SIGN);
}
wdata->email->security &= ~APPLICATION_SMIME;
wdata->email->security |= APPLICATION_PGP;
update_crypt_info(wdata);
}
wdata->email->security = crypt_pgp_send_menu(wdata->email);
update_crypt_info(wdata);
if (wdata->email->security == old_flags)
return FR_NO_ACTION;
mutt_message_hook(NULL, wdata->email, MUTT_SEND2_HOOK);
notify_send(wdata->email->notify, NT_EMAIL, NT_EMAIL_CHANGE, NULL);
return FR_SUCCESS;
}
/**
* op_compose_smime_menu - Show S/MIME options - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_compose_smime_menu(struct EnvelopeWindowData *wdata, int op)
{
const SecurityFlags old_flags = wdata->email->security;
if (!(WithCrypto & APPLICATION_SMIME))
return FR_NOT_IMPL;
if (!crypt_has_module_backend(APPLICATION_SMIME))
{
mutt_error(_("No S/MIME backend configured"));
return FR_ERROR;
}
if (((WithCrypto & APPLICATION_PGP) != 0) && (wdata->email->security & APPLICATION_PGP))
{
if (wdata->email->security & (SEC_ENCRYPT | SEC_SIGN))
{
if (query_yesorno(_("PGP already selected. Clear and continue?"), MUTT_YES) != MUTT_YES)
{
mutt_clear_error();
return FR_NO_ACTION;
}
wdata->email->security &= ~(SEC_ENCRYPT | SEC_SIGN);
}
wdata->email->security &= ~APPLICATION_PGP;
wdata->email->security |= APPLICATION_SMIME;
update_crypt_info(wdata);
}
wdata->email->security = crypt_smime_send_menu(wdata->email);
update_crypt_info(wdata);
if (wdata->email->security == old_flags)
return FR_NO_ACTION;
mutt_message_hook(NULL, wdata->email, MUTT_SEND2_HOOK);
notify_send(wdata->email->notify, NT_EMAIL, NT_EMAIL_CHANGE, NULL);
return FR_SUCCESS;
}
#ifdef USE_AUTOCRYPT
/**
* op_compose_autocrypt_menu - Show autocrypt compose menu options - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_compose_autocrypt_menu(struct EnvelopeWindowData *wdata, int op)
{
const SecurityFlags old_flags = wdata->email->security;
const bool c_autocrypt = cs_subset_bool(wdata->sub, "autocrypt");
if (!c_autocrypt)
return FR_NO_ACTION;
if ((WithCrypto & APPLICATION_SMIME) && (wdata->email->security & APPLICATION_SMIME))
{
if (wdata->email->security & (SEC_ENCRYPT | SEC_SIGN))
{
if (query_yesorno(_("S/MIME already selected. Clear and continue?"), MUTT_YES) != MUTT_YES)
{
mutt_clear_error();
return FR_NO_ACTION;
}
wdata->email->security &= ~(SEC_ENCRYPT | SEC_SIGN);
}
wdata->email->security &= ~APPLICATION_SMIME;
wdata->email->security |= APPLICATION_PGP;
update_crypt_info(wdata);
}
autocrypt_compose_menu(wdata->email, wdata->sub);
update_crypt_info(wdata);
if (wdata->email->security == old_flags)
return FR_NO_ACTION;
mutt_message_hook(NULL, wdata->email, MUTT_SEND2_HOOK);
notify_send(wdata->email->notify, NT_EMAIL, NT_EMAIL_CHANGE, NULL);
return FR_SUCCESS;
}
#endif
// -----------------------------------------------------------------------------
/**
* op_envelope_edit_followup_to - Edit the Followup-To field - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_envelope_edit_followup_to(struct EnvelopeWindowData *wdata, int op)
{
if (!wdata->is_news)
return FR_NO_ACTION;
int rc = FR_NO_ACTION;
struct Buffer *buf = buf_pool_get();
buf_strcpy(buf, wdata->email->env->followup_to);
if (mw_get_field(Prompts[HDR_FOLLOWUPTO], buf, MUTT_COMP_NO_FLAGS, HC_OTHER, NULL, NULL) == 0)
{
mutt_str_replace(&wdata->email->env->followup_to, buf_string(buf));
mutt_env_notify_send(wdata->email, NT_ENVELOPE_FOLLOWUP_TO);
rc = FR_SUCCESS;
}
buf_pool_release(&buf);
return rc;
}
/**
* op_envelope_edit_newsgroups - Edit the newsgroups list - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_envelope_edit_newsgroups(struct EnvelopeWindowData *wdata, int op)
{
if (!wdata->is_news)
return FR_NO_ACTION;
int rc = FR_NO_ACTION;
struct Buffer *buf = buf_pool_get();
buf_strcpy(buf, wdata->email->env->newsgroups);
if (mw_get_field(Prompts[HDR_NEWSGROUPS], buf, MUTT_COMP_NO_FLAGS, HC_OTHER, NULL, NULL) == 0)
{
mutt_str_replace(&wdata->email->env->newsgroups, buf_string(buf));
mutt_env_notify_send(wdata->email, NT_ENVELOPE_NEWSGROUPS);
rc = FR_SUCCESS;
}
buf_pool_release(&buf);
return rc;
}
/**
* op_envelope_edit_x_comment_to - Edit the X-Comment-To field - Implements ::envelope_function_t - @ingroup envelope_function_api
*/
static int op_envelope_edit_x_comment_to(struct EnvelopeWindowData *wdata, int op)
{
const bool c_x_comment_to = cs_subset_bool(wdata->sub, "x_comment_to");
if (!(wdata->is_news && c_x_comment_to))
return FR_NO_ACTION;
int rc = FR_NO_ACTION;
struct Buffer *buf = buf_pool_get();
buf_strcpy(buf, wdata->email->env->x_comment_to);
if (mw_get_field(Prompts[HDR_XCOMMENTTO], buf, MUTT_COMP_NO_FLAGS, HC_OTHER, NULL, NULL) == 0)
{
mutt_str_replace(&wdata->email->env->x_comment_to, buf_string(buf));
mutt_env_notify_send(wdata->email, NT_ENVELOPE_X_COMMENT_TO);
rc = FR_SUCCESS;
}
buf_pool_release(&buf);
return rc;
}
// -----------------------------------------------------------------------------
/**
* EnvelopeFunctions - All the NeoMutt functions that the Envelope supports
*/
static const struct EnvelopeFunction EnvelopeFunctions[] = {
// clang-format off
#ifdef USE_AUTOCRYPT
{ OP_COMPOSE_AUTOCRYPT_MENU, op_compose_autocrypt_menu },
#endif
{ OP_COMPOSE_PGP_MENU, op_compose_pgp_menu },
{ OP_COMPOSE_SMIME_MENU, op_compose_smime_menu },
{ OP_ENVELOPE_EDIT_BCC, op_envelope_edit_bcc },
{ OP_ENVELOPE_EDIT_CC, op_envelope_edit_cc },
{ OP_ENVELOPE_EDIT_FCC, op_envelope_edit_fcc },
{ OP_ENVELOPE_EDIT_FOLLOWUP_TO, op_envelope_edit_followup_to },
{ OP_ENVELOPE_EDIT_FROM, op_envelope_edit_from },
{ OP_ENVELOPE_EDIT_NEWSGROUPS, op_envelope_edit_newsgroups },
{ OP_ENVELOPE_EDIT_REPLY_TO, op_envelope_edit_reply_to },
{ OP_ENVELOPE_EDIT_SUBJECT, op_envelope_edit_subject },
{ OP_ENVELOPE_EDIT_TO, op_envelope_edit_to },
{ OP_ENVELOPE_EDIT_X_COMMENT_TO, op_envelope_edit_x_comment_to },
{ 0, NULL },
// clang-format on
};
/**
* env_function_dispatcher - Perform an Envelope function - Implements ::function_dispatcher_t - @ingroup dispatcher_api
*/
int env_function_dispatcher(struct MuttWindow *win, int op)
{
if (!win || !win->wdata)
return FR_UNKNOWN;
int rc = FR_UNKNOWN;
for (size_t i = 0; EnvelopeFunctions[i].op != OP_NULL; i++)
{
const struct EnvelopeFunction *fn = &EnvelopeFunctions[i];
if (fn->op == op)
{
struct EnvelopeWindowData *wdata = win->wdata;
rc = fn->function(wdata, op);
break;
}
}
if (rc == FR_UNKNOWN) // Not our function
return rc;
const char *result = dispatcher_get_retval_name(rc);
mutt_debug(LL_DEBUG1, "Handled %s (%d) -> %s\n", opcodes_get_name(op), op, NONULL(result));
return rc;
}