forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
1325 lines (1161 loc) · 35.5 KB
/
commands.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* Manage where the email is piped to external commands
*
* @authors
* Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
* Copyright (C) 2000-2004,2006 Thomas Roessler <roessler@does-not-exist.org>
*
* @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 commands Manage where the email is piped to external commands
*
* Manage where the email is piped to external commands
*/
#include "config.h"
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt/mutt.h"
#include "config/lib.h"
#include "email/lib.h"
#include "conn/conn.h"
#include "mutt.h"
#include "alias.h"
#include "context.h"
#include "copy.h"
#include "curs_lib.h"
#include "filter.h"
#include "format_flags.h"
#include "globals.h"
#include "hdrline.h"
#include "hook.h"
#include "icommands.h"
#include "keymap.h"
#include "mailbox.h"
#include "mutt_curses.h"
#include "mutt_logging.h"
#include "mutt_menu.h"
#include "mutt_parse.h"
#include "mutt_window.h"
#include "muttlib.h"
#include "mx.h"
#include "ncrypt/ncrypt.h"
#include "options.h"
#include "pager.h"
#include "protos.h"
#include "sendlib.h"
#include "sort.h"
#ifdef USE_IMAP
#include "imap/imap.h"
#endif
#ifdef USE_NOTMUCH
#include "notmuch/mutt_notmuch.h"
#endif
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
/* These Config Variables are only used in commands.c */
unsigned char C_CryptVerifySig; ///< Config: Verify PGP or SMIME signatures
char *C_DisplayFilter; ///< Config: External command to pre-process an email before display
bool C_PipeDecode; ///< Config: Decode the message when piping it
char *C_PipeSep; ///< Config: Separator to add between multiple piped messages
bool C_PipeSplit; ///< Config: Run the pipe command on each message separately
bool C_PrintDecode; ///< Config: Decode message before printing it
bool C_PrintSplit; ///< Config: Print multiple messages separately
bool C_PromptAfter; ///< Config: Pause after running an external pager
static const char *ExtPagerProgress = "all";
/** The folder the user last saved to. Used by ci_save_message() */
static char LastSaveFolder[PATH_MAX] = "";
/**
* update_protected_headers - Get the protected header and update the index
* @param cur Email to update
*/
static void update_protected_headers(struct Email *cur)
{
struct Envelope *prot_headers = NULL;
regmatch_t pmatch[1];
if (!C_CryptProtectedHeadersRead)
return;
/* Grab protected headers to update in the index */
if (cur->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 (!(cur->security & SEC_GOODSIGN))
return;
if (mutt_is_multipart_signed(cur->content) && cur->content->parts)
{
prot_headers = cur->content->parts->mime_headers;
}
else if (((WithCrypto & APPLICATION_SMIME) != 0) && mutt_is_application_smime(cur->content))
{
prot_headers = cur->content->mime_headers;
}
}
if (!prot_headers && (cur->security & SEC_ENCRYPT))
{
if (((WithCrypto & APPLICATION_PGP) != 0) &&
(mutt_is_valid_multipart_pgp_encrypted(cur->content) ||
mutt_is_malformed_multipart_pgp_encrypted(cur->content)))
{
prot_headers = cur->content->mime_headers;
}
else if (((WithCrypto & APPLICATION_SMIME) != 0) && mutt_is_application_smime(cur->content))
{
prot_headers = cur->content->mime_headers;
}
}
/* Update protected headers in the index and header cache. */
if (prot_headers && prot_headers->subject &&
mutt_str_strcmp(cur->env->subject, prot_headers->subject))
{
if (Context->mailbox->subj_hash && cur->env->real_subj)
mutt_hash_delete(Context->mailbox->subj_hash, cur->env->real_subj, cur);
mutt_str_replace(&cur->env->subject, prot_headers->subject);
FREE(&cur->env->disp_subj);
if (regexec(C_ReplyRegex->regex, cur->env->subject, 1, pmatch, 0) == 0)
cur->env->real_subj = cur->env->subject + pmatch[0].rm_eo;
else
cur->env->real_subj = cur->env->subject;
if (Context->mailbox->subj_hash)
mutt_hash_insert(Context->mailbox->subj_hash, cur->env->real_subj, cur);
mx_save_hcache(Context->mailbox, cur);
/* Also persist back to the message headers if this is set */
if (C_CryptProtectedHeadersSave)
{
cur->env->changed |= MUTT_ENV_CHANGED_SUBJECT;
cur->changed = 1;
Context->mailbox->changed = 1;
}
}
}
/**
* mutt_display_message - Display a message in the pager
* @param cur Header of current message
* @retval 0 Success
* @retval -1 Error
*/
int mutt_display_message(struct Email *cur)
{
char tempfile[PATH_MAX], buf[1024];
int rc = 0;
bool builtin = false;
CopyMessageFlags cmflags = MUTT_CM_DECODE | MUTT_CM_DISPLAY | MUTT_CM_CHARCONV;
CopyHeaderFlags chflags;
pid_t filterpid = -1;
int res;
snprintf(buf, sizeof(buf), "%s/%s", TYPE(cur->content), cur->content->subtype);
mutt_parse_mime_message(Context->mailbox, cur);
mutt_message_hook(Context->mailbox, cur, MUTT_MESSAGE_HOOK);
/* see if crypto is needed for this message. if so, we should exit curses */
if ((WithCrypto != 0) && cur->security)
{
if (cur->security & SEC_ENCRYPT)
{
if (cur->security & APPLICATION_SMIME)
crypt_smime_getkeys(cur->env);
if (!crypt_valid_passphrase(cur->security))
return 0;
cmflags |= MUTT_CM_VERIFY;
}
else if (cur->security & SEC_SIGN)
{
/* find out whether or not the verify signature */
/* L10N: Used for the $crypt_verify_sig prompt */
if (query_quadoption(C_CryptVerifySig, _("Verify signature?")) == MUTT_YES)
{
cmflags |= MUTT_CM_VERIFY;
}
}
}
if (cmflags & MUTT_CM_VERIFY || cur->security & SEC_ENCRYPT)
{
if (cur->security & APPLICATION_PGP)
{
if (cur->env->from)
crypt_pgp_invoke_getkeys(cur->env->from);
crypt_invoke_message(APPLICATION_PGP);
}
if (cur->security & APPLICATION_SMIME)
crypt_invoke_message(APPLICATION_SMIME);
}
mutt_mktemp(tempfile, sizeof(tempfile));
FILE *fp_filter_out = NULL;
FILE *fp_out = mutt_file_fopen(tempfile, "w");
if (!fp_out)
{
mutt_error(_("Could not create temporary file"));
return 0;
}
if (C_DisplayFilter && *C_DisplayFilter)
{
fp_filter_out = fp_out;
fp_out = NULL;
filterpid = mutt_create_filter_fd(C_DisplayFilter, &fp_out, NULL, NULL, -1,
fileno(fp_filter_out), -1);
if (filterpid < 0)
{
mutt_error(_("Cannot create display filter"));
mutt_file_fclose(&fp_filter_out);
unlink(tempfile);
return 0;
}
}
if (!C_Pager || (mutt_str_strcmp(C_Pager, "builtin") == 0))
builtin = true;
else
{
struct HdrFormatInfo hfi;
hfi.ctx = Context;
hfi.mailbox = Context->mailbox;
hfi.pager_progress = ExtPagerProgress;
hfi.email = cur;
mutt_make_string_info(buf, sizeof(buf), MuttIndexWindow->cols,
NONULL(C_PagerFormat), &hfi, 0);
fputs(buf, fp_out);
fputs("\n\n", fp_out);
}
chflags = (C_Weed ? (CH_WEED | CH_REORDER) : 0) | CH_DECODE | CH_FROM | CH_DISPLAY;
#ifdef USE_NOTMUCH
if (Context->mailbox->magic == MUTT_NOTMUCH)
chflags |= CH_VIRTUAL;
#endif
res = mutt_copy_message_ctx(fp_out, Context->mailbox, cur, cmflags, chflags);
if (((mutt_file_fclose(&fp_out) != 0) && (errno != EPIPE)) || (res < 0))
{
mutt_error(_("Could not copy message"));
if (fp_filter_out)
{
mutt_wait_filter(filterpid);
mutt_file_fclose(&fp_filter_out);
}
mutt_file_unlink(tempfile);
return 0;
}
if (fp_filter_out && (mutt_wait_filter(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 */
cur->security &= ~(SEC_GOODSIGN | SEC_BADSIGN);
cur->security |= crypt_query(cur->content);
/* Remove color cache for this message, in case there
* are color patterns for both ~g and ~V */
cur->pair = 0;
/* Grab protected headers and update the header and index */
update_protected_headers(cur);
}
if (builtin)
{
if ((WithCrypto != 0) && (cur->security & APPLICATION_SMIME) && (cmflags & MUTT_CM_VERIFY))
{
if (cur->security & SEC_GOODSIGN)
{
if (crypt_smime_verify_sender(cur) == 0)
mutt_message(_("S/MIME signature successfully verified"));
else
mutt_error(_("S/MIME certificate owner does not match sender"));
}
else if (cur->security & SEC_PARTSIGN)
mutt_message(_("Warning: Part of this message has not been signed"));
else if (cur->security & SEC_SIGN || cur->security & SEC_BADSIGN)
mutt_error(_("S/MIME signature could NOT be verified"));
}
if ((WithCrypto != 0) && (cur->security & APPLICATION_PGP) && (cmflags & MUTT_CM_VERIFY))
{
if (cur->security & SEC_GOODSIGN)
mutt_message(_("PGP signature successfully verified"));
else if (cur->security & SEC_PARTSIGN)
mutt_message(_("Warning: Part of this message has not been signed"));
else if (cur->security & SEC_SIGN)
mutt_message(_("PGP signature could NOT be verified"));
}
struct Pager info = { 0 };
/* Invoke the builtin pager */
info.email = cur;
info.ctx = Context;
rc = mutt_pager(NULL, tempfile, MUTT_PAGER_MESSAGE, &info);
}
else
{
int r;
char cmd[STR_COMMAND];
mutt_endwin();
snprintf(cmd, sizeof(cmd), "%s %s", NONULL(C_Pager), tempfile);
r = mutt_system(cmd);
if (r == -1)
mutt_error(_("Error running \"%s\""), cmd);
unlink(tempfile);
if (!OptNoCurses)
keypad(stdscr, true);
if (r != -1)
mutt_set_flag(Context->mailbox, cur, MUTT_READ, true);
if ((r != -1) && C_PromptAfter)
{
mutt_unget_event(mutt_any_key_to_continue(_("Command: ")), 0);
rc = km_dokey(MENU_PAGER);
}
else
rc = 0;
}
return rc;
}
/**
* ci_bounce_message - Bounce an email
* @param m Mailbox
* @param el List of Emails to bounce
*/
void ci_bounce_message(struct Mailbox *m, struct EmailList *el)
{
if (!m || !el || STAILQ_EMPTY(el))
return;
char prompt[128];
char scratch[128];
char buf[8192] = { 0 };
struct Address *addr = NULL;
char *err = NULL;
int rc;
int msg_count = 0;
struct EmailNode *en = NULL;
STAILQ_FOREACH(en, el, entries)
{
/* RFC5322 mandates a From: header,
* so warn before bouncing messages without one */
if (!en->email->env->from)
mutt_error(_("Warning: message contains no From: header"));
msg_count++;
}
if (msg_count == 1)
mutt_str_strfcpy(prompt, _("Bounce message to: "), sizeof(prompt));
else
mutt_str_strfcpy(prompt, _("Bounce tagged messages to: "), sizeof(prompt));
rc = mutt_get_field(prompt, buf, sizeof(buf), MUTT_ALIAS);
if (rc || !buf[0])
return;
addr = mutt_addr_parse_list2(addr, buf);
if (!addr)
{
mutt_error(_("Error parsing address"));
return;
}
addr = mutt_expand_aliases(addr);
if (mutt_addrlist_to_intl(addr, &err) < 0)
{
mutt_error(_("Bad IDN: '%s'"), err);
FREE(&err);
mutt_addr_free(&addr);
return;
}
buf[0] = '\0';
mutt_addr_write(buf, sizeof(buf), addr, true);
#define EXTRA_SPACE (15 + 7 + 2)
snprintf(scratch, sizeof(scratch),
ngettext("Bounce message to %s", "Bounce messages to %s", msg_count), buf);
if (mutt_strwidth(prompt) > MuttMessageWindow->cols - EXTRA_SPACE)
{
mutt_simple_format(prompt, sizeof(prompt), 0, MuttMessageWindow->cols - EXTRA_SPACE,
FMT_LEFT, 0, scratch, sizeof(scratch), 0);
mutt_str_strcat(prompt, sizeof(prompt), "...?");
}
else
snprintf(prompt, sizeof(prompt), "%s?", scratch);
if (query_quadoption(C_Bounce, prompt) != MUTT_YES)
{
mutt_addr_free(&addr);
mutt_window_clearline(MuttMessageWindow, 0);
mutt_message(ngettext("Message not bounced", "Messages not bounced", msg_count));
return;
}
mutt_window_clearline(MuttMessageWindow, 0);
struct Message *msg = NULL;
STAILQ_FOREACH(en, el, entries)
{
msg = mx_msg_open(m, en->email->msgno);
if (!msg)
{
rc = -1;
break;
}
rc = mutt_bounce_message(msg->fp, en->email, addr);
mx_msg_close(m, &msg);
if (rc < 0)
break;
}
mutt_addr_free(&addr);
/* If no error, or background, display message. */
if ((rc == 0) || (rc == S_BKG))
mutt_message(ngettext("Message bounced", "Messages bounced", msg_count));
}
/**
* pipe_set_flags - Generate flags for copy header/message
* @param[in] decode If true decode the message
* @param[in] print If true, mark the message for printing
* @param[out] cmflags Flags, see #CopyMessageFlags
* @param[out] chflags Flags, see #CopyHeaderFlags
*/
static void pipe_set_flags(bool decode, bool print, CopyMessageFlags *cmflags,
CopyHeaderFlags *chflags)
{
if (decode)
{
*cmflags |= MUTT_CM_DECODE | MUTT_CM_CHARCONV;
*chflags |= CH_DECODE | CH_REORDER;
if (C_Weed)
{
*chflags |= CH_WEED;
*cmflags |= MUTT_CM_WEED;
}
}
if (print)
*cmflags |= MUTT_CM_PRINTING;
}
/**
* pipe_msg - Pipe a message
* @param m Mailbox
* @param e Email to pipe
* @param fp File to write to
* @param decode If true, decode the message
* @param print If true, message is for printing
*/
static void pipe_msg(struct Mailbox *m, struct Email *e, FILE *fp, bool decode, bool print)
{
CopyMessageFlags cmflags = MUTT_CM_NO_FLAGS;
CopyHeaderFlags chflags = CH_FROM;
pipe_set_flags(decode, print, &cmflags, &chflags);
if ((WithCrypto != 0) && decode && e->security & SEC_ENCRYPT)
{
if (!crypt_valid_passphrase(e->security))
return;
endwin();
}
if (decode)
mutt_parse_mime_message(m, e);
mutt_copy_message_ctx(fp, m, e, cmflags, chflags);
}
/**
* pipe_message - Pipe message to a command
* @param m Mailbox
* @param el List of Emails to pipe
* @param cmd Command to pipe to
* @param decode Should the message be decrypted
* @param print True if this is a print job
* @param split Should a separator be sent between messages?
* @param sep Separator string
* @retval 0 Success
* @retval 1 Error
*
* The following code is shared between printing and piping.
*/
static int pipe_message(struct Mailbox *m, struct EmailList *el, char *cmd,
bool decode, bool print, bool split, const char *sep)
{
if (!m || !el)
return 1;
struct EmailNode *en = STAILQ_FIRST(el);
if (!en)
return 1;
int rc = 0;
pid_t pid;
FILE *fp_out = NULL;
if (!STAILQ_NEXT(en, entries))
{
/* handle a single message */
mutt_message_hook(m, en->email, MUTT_MESSAGE_HOOK);
if ((WithCrypto != 0) && decode)
{
mutt_parse_mime_message(m, en->email);
if ((en->email->security & SEC_ENCRYPT) &&
!crypt_valid_passphrase(en->email->security))
return 1;
}
mutt_endwin();
pid = mutt_create_filter(cmd, &fp_out, NULL, NULL);
if (pid < 0)
{
mutt_perror(_("Can't create filter process"));
return 1;
}
OptKeepQuiet = true;
pipe_msg(m, en->email, fp_out, decode, print);
mutt_file_fclose(&fp_out);
rc = mutt_wait_filter(pid);
OptKeepQuiet = false;
}
else
{
/* handle tagged messages */
if ((WithCrypto != 0) && decode)
{
STAILQ_FOREACH(en, el, entries)
{
mutt_message_hook(m, en->email, MUTT_MESSAGE_HOOK);
mutt_parse_mime_message(m, en->email);
if ((en->email->security & SEC_ENCRYPT) &&
!crypt_valid_passphrase(en->email->security))
{
return 1;
}
}
}
if (split)
{
STAILQ_FOREACH(en, el, entries)
{
mutt_message_hook(m, en->email, MUTT_MESSAGE_HOOK);
mutt_endwin();
pid = mutt_create_filter(cmd, &fp_out, NULL, NULL);
if (pid < 0)
{
mutt_perror(_("Can't create filter process"));
return 1;
}
OptKeepQuiet = true;
pipe_msg(m, en->email, fp_out, decode, print);
/* add the message separator */
if (sep)
fputs(sep, fp_out);
mutt_file_fclose(&fp_out);
if (mutt_wait_filter(pid) != 0)
rc = 1;
OptKeepQuiet = false;
}
}
else
{
mutt_endwin();
pid = mutt_create_filter(cmd, &fp_out, NULL, NULL);
if (pid < 0)
{
mutt_perror(_("Can't create filter process"));
return 1;
}
OptKeepQuiet = true;
STAILQ_FOREACH(en, el, entries)
{
mutt_message_hook(m, en->email, MUTT_MESSAGE_HOOK);
pipe_msg(m, en->email, fp_out, decode, print);
/* add the message separator */
if (sep)
fputs(sep, fp_out);
}
mutt_file_fclose(&fp_out);
if (mutt_wait_filter(pid) != 0)
rc = 1;
OptKeepQuiet = false;
}
}
if ((rc != 0) || C_WaitKey)
mutt_any_key_to_continue(NULL);
return rc;
}
/**
* mutt_pipe_message - Pipe a message
* @param m Mailbox
* @param el List of Emails to pipe
*/
void mutt_pipe_message(struct Mailbox *m, struct EmailList *el)
{
if (!m || !el)
return;
char buf[1024] = { 0 };
if ((mutt_get_field(_("Pipe to command: "), buf, sizeof(buf), MUTT_CMD) != 0) ||
(buf[0] == '\0'))
{
return;
}
mutt_expand_path(buf, sizeof(buf));
pipe_message(m, el, buf, C_PipeDecode, false, C_PipeSplit, C_PipeSep);
}
/**
* mutt_print_message - Print a message
* @param m Mailbox
* @param el List of Emails to print
*/
void mutt_print_message(struct Mailbox *m, struct EmailList *el)
{
if (!m || !el)
return;
if (C_Print && (!C_PrintCommand || !*C_PrintCommand))
{
mutt_message(_("No printing command has been defined"));
return;
}
int msg_count = 0;
struct EmailNode *en = NULL;
STAILQ_FOREACH(en, el, entries)
{
msg_count++;
}
if (query_quadoption(C_Print, (msg_count == 1) ?
_("Print message?") :
_("Print tagged messages?")) != MUTT_YES)
{
return;
}
if (pipe_message(m, el, C_PrintCommand, C_PrintDecode, true, C_PrintSplit, "\f") == 0)
mutt_message(ngettext("Message printed", "Messages printed", msg_count));
else
{
mutt_message(ngettext("Message could not be printed",
"Messages could not be printed", msg_count));
}
}
/**
* mutt_select_sort - Ask the user for a sort method
* @param reverse If true make it a reverse sort
* @retval num Sort type, see #SortType
*/
int mutt_select_sort(bool reverse)
{
enum SortType method = C_Sort; /* save the current method in case of abort */
enum SortType new_sort = C_Sort;
switch (mutt_multi_choice(reverse ?
/* L10N: The highlighted letters must match the "Sort" options */
_("Rev-Sort "
"(d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/"
"(u)nsort/si(z)e/s(c)ore/s(p)am/(l)abel?: ") :
/* L10N: The highlighted letters must match the "Rev-Sort" options */
_("Sort "
"(d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/"
"(u)nsort/si(z)e/s(c)ore/s(p)am/(l)abel?: "),
/* L10N: These must match the highlighted letters from "Sort" and "Rev-Sort" */
_("dfrsotuzcpl")))
{
case -1: /* abort - don't resort */
return -1;
case 1: /* (d)ate */
new_sort = SORT_DATE;
break;
case 2: /* (f)rm */
new_sort = SORT_FROM;
break;
case 3: /* (r)ecv */
new_sort = SORT_RECEIVED;
break;
case 4: /* (s)ubj */
new_sort = SORT_SUBJECT;
break;
case 5: /* t(o) */
new_sort = SORT_TO;
break;
case 6: /* (t)hread */
new_sort = SORT_THREADS;
break;
case 7: /* (u)nsort */
new_sort = SORT_ORDER;
break;
case 8: /* si(z)e */
new_sort = SORT_SIZE;
break;
case 9: /* s(c)ore */
new_sort = SORT_SCORE;
break;
case 10: /* s(p)am */
new_sort = SORT_SPAM;
break;
case 11: /* (l)abel */
new_sort = SORT_LABEL;
break;
}
if (reverse)
new_sort |= SORT_REVERSE;
cs_str_native_set(Config, "sort", new_sort, NULL);
return (C_Sort != method) ? 0 : -1; /* no need to resort if it's the same */
}
/**
* mutt_shell_escape - invoke a command in a subshell
*/
void mutt_shell_escape(void)
{
char buf[1024];
buf[0] = '\0';
if (mutt_get_field(_("Shell command: "), buf, sizeof(buf), MUTT_CMD) != 0)
return;
if ((buf[0] == '\0') && C_Shell)
mutt_str_strfcpy(buf, C_Shell, sizeof(buf));
if (buf[0] == '\0')
return;
mutt_window_clearline(MuttMessageWindow, 0);
mutt_endwin();
fflush(stdout);
int rc = mutt_system(buf);
if (rc == -1)
mutt_debug(LL_DEBUG1, "Error running \"%s\"!", buf);
if ((rc != 0) || C_WaitKey)
mutt_any_key_to_continue(NULL);
mutt_mailbox_check(Context->mailbox, MUTT_MAILBOX_CHECK_FORCE);
}
/**
* mutt_enter_command - enter a neomutt command
*/
void mutt_enter_command(void)
{
char buf[1024] = { 0 };
/* if enter is pressed after : with no command, just return */
if ((mutt_get_field(":", buf, sizeof(buf), MUTT_COMMAND) != 0) || !buf[0])
return;
struct Buffer *err = mutt_buffer_alloc(256);
struct Buffer *token = mutt_buffer_alloc(256);
/* check if buf is a valid icommand, else fall back quietly to parse_rc_lines */
enum CommandResult rc = mutt_parse_icommand(buf, err);
if (!mutt_buffer_is_empty(err))
{
/* since errbuf could potentially contain printf() sequences in it,
* we must call mutt_error() in this fashion so that vsprintf()
* doesn't expect more arguments that we passed */
if (rc == MUTT_CMD_ERROR)
mutt_error("%s", err->data);
else
mutt_warning("%s", err->data);
}
else if (rc != MUTT_CMD_SUCCESS)
{
rc = mutt_parse_rc_line(buf, token, err);
if (!mutt_buffer_is_empty(err))
{
if (rc == MUTT_CMD_SUCCESS) /* command succeeded with message */
mutt_message("%s", err->data);
else /* error executing command */
mutt_error("%s", err->data);
}
}
/* else successful command */
mutt_buffer_free(&token);
mutt_buffer_free(&err);
}
/**
* mutt_display_address - Display the address of a message
* @param env Envelope containing address
*/
void mutt_display_address(struct Envelope *env)
{
const char *pfx = NULL;
char buf[128];
struct Address *addr = mutt_get_address(env, &pfx);
if (!addr)
return;
/* Note: We don't convert IDNA to local representation this time.
* That is intentional, so the user has an opportunity to copy &
* paste the on-the-wire form of the address to other, IDN-unable
* software. */
buf[0] = '\0';
mutt_addr_write(buf, sizeof(buf), addr, false);
mutt_message("%s: %s", pfx, buf);
}
/**
* set_copy_flags - Set the flags for a message copy
* @param[in] e Email
* @param[in] decode If true, decode the message
* @param[in] decrypt If true, decrypt the message
* @param[out] cmflags Flags, see #CopyMessageFlags
* @param[out] chflags Flags, see #CopyHeaderFlags
*/
static void set_copy_flags(struct Email *e, bool decode, bool decrypt,
CopyMessageFlags *cmflags, CopyHeaderFlags *chflags)
{
*cmflags = MUTT_CM_NO_FLAGS;
*chflags = CH_UPDATE_LEN;
if ((WithCrypto != 0) && !decode && decrypt && (e->security & SEC_ENCRYPT))
{
if (((WithCrypto & APPLICATION_PGP) != 0) && mutt_is_multipart_encrypted(e->content))
{
*chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
*cmflags = MUTT_CM_DECODE_PGP;
}
else if (((WithCrypto & APPLICATION_PGP) != 0) &&
mutt_is_application_pgp(e->content) & SEC_ENCRYPT)
{
decode = 1;
}
else if (((WithCrypto & APPLICATION_SMIME) != 0) &&
mutt_is_application_smime(e->content) & SEC_ENCRYPT)
{
*chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
*cmflags = MUTT_CM_DECODE_SMIME;
}
}
if (decode)
{
*chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
*cmflags = MUTT_CM_DECODE | MUTT_CM_CHARCONV;
if (!decrypt) /* If decode doesn't kick in for decrypt, */
{
*chflags |= CH_DECODE; /* then decode RFC2047 headers, */
if (C_Weed)
{
*chflags |= CH_WEED; /* and respect $weed. */
*cmflags |= MUTT_CM_WEED;
}
}
}
}
/**
* mutt_save_message_ctx - Save a message to a given mailbox
* @param e Email
* @param delete If true, delete the original
* @param decode If true, decode the message
* @param decrypt If true, decrypt the message
* @param m Mailbox to save to
* @retval 0 Success
* @retval -1 Error
*/
int mutt_save_message_ctx(struct Email *e, bool delete, bool decode,
bool decrypt, struct Mailbox *m)
{
CopyMessageFlags cmflags = MUTT_CM_NO_FLAGS;
CopyHeaderFlags chflags = CH_NO_FLAGS;
int rc;
set_copy_flags(e, decode, decrypt, &cmflags, &chflags);
if (decode || decrypt)
mutt_parse_mime_message(Context->mailbox, e);
rc = mutt_append_message(m, Context->mailbox, e, cmflags, chflags);
if (rc != 0)
return rc;
if (delete)
{
mutt_set_flag(Context->mailbox, e, MUTT_DELETE, true);
mutt_set_flag(Context->mailbox, e, MUTT_PURGE, true);
if (C_DeleteUntag)
mutt_set_flag(Context->mailbox, e, MUTT_TAG, false);
}
return 0;
}
/**
* mutt_save_message - Save an email
* @param m Mailbox
* @param el List of Emails to save
* @param delete If true, delete the original (save)
* @param decode If true, decode the message
* @param decrypt If true, decrypt the message
* @retval 0 Copy/save was successful
* @retval -1 Error/abort
*/
int mutt_save_message(struct Mailbox *m, struct EmailList *el, bool delete,
bool decode, bool decrypt)
{
if (!el || STAILQ_EMPTY(el))
return -1;
bool need_passphrase = false;
int app = 0;
char buf[PATH_MAX];
const char *prompt = NULL;
struct Context *savectx = NULL;
struct stat st;
struct EmailNode *en = STAILQ_FIRST(el);
bool single = !STAILQ_NEXT(en, entries);
if (delete)
{
if (decode)
prompt = single ? _("Decode-save to mailbox") : _("Decode-save tagged to mailbox");
else if (decrypt)
prompt = single ? _("Decrypt-save to mailbox") : _("Decrypt-save tagged to mailbox");
else
prompt = single ? _("Save to mailbox") : _("Save tagged to mailbox");
}
else