forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecvattach.c
1749 lines (1580 loc) · 49.2 KB
/
recvattach.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
* Routines for managing attachments
*
* @authors
* Copyright (C) 1996-2000,2002,2007,2010 Michael R. Elkins <[email protected]>
* Copyright (C) 1999-2006 Thomas Roessler <[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 recvattach Routines for managing attachments
*
* Routines for managing attachments
*/
#include "config.h"
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt/mutt.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "recvattach.h"
#include "commands.h"
#include "context.h"
#include "format_flags.h"
#include "globals.h"
#include "handler.h"
#include "hdrline.h"
#include "hook.h"
#include "keymap.h"
#include "mailcap.h"
#include "mutt_attach.h"
#include "mutt_menu.h"
#include "mutt_parse.h"
#include "muttlib.h"
#include "mx.h"
#include "ncrypt/ncrypt.h"
#include "opcodes.h"
#include "options.h"
#include "recvcmd.h"
#include "send.h"
#include "sendlib.h"
#include "state.h"
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
/* These Config Variables are only used in recvattach.c */
char *C_AttachSaveDir; ///< Config: Default directory where attachments are saved
char *C_AttachSaveWithoutPrompting; ///< Config: If true, then don't prompt to save
char *C_AttachSep; ///< Config: Separator to add between saved/printed/piped attachments
bool C_AttachSplit; ///< Config: Save/print/pipe tagged messages individually
bool C_DigestCollapse; ///< Config: Hide the subparts of a multipart/digest
char *C_MessageFormat; ///< Config: printf-like format string for listing attached messages
static void mutt_update_recvattach_menu(struct AttachCtx *actx, struct Menu *menu, bool init);
static const char *Mailbox_is_read_only = N_("Mailbox is read-only");
#define CHECK_READONLY \
if (!Context || !Context->mailbox || Context->mailbox->readonly) \
{ \
mutt_flushinp(); \
mutt_error(_(Mailbox_is_read_only)); \
break; \
}
#define CUR_ATTACH actx->idx[actx->v2r[menu->current]]
static const struct Mapping AttachHelp[] = {
{ N_("Exit"), OP_EXIT }, { N_("Save"), OP_SAVE }, { N_("Pipe"), OP_PIPE },
{ N_("Print"), OP_PRINT }, { N_("Help"), OP_HELP }, { NULL, 0 },
};
static const char *Function_not_permitted =
N_("Function not permitted in attach-message mode");
#define CHECK_ATTACH \
if (OptAttachMsg) \
{ \
mutt_flushinp(); \
mutt_error(_(Function_not_permitted)); \
break; \
}
/**
* mutt_update_v2r - Update the virtual list of attachments
* @param actx Attachment context
*
* Update the record of the number of attachments and the status of the tree.
*/
static void mutt_update_v2r(struct AttachCtx *actx)
{
int vindex, rindex, curlevel;
vindex = 0;
rindex = 0;
while (rindex < actx->idxlen)
{
actx->v2r[vindex++] = rindex;
if (actx->idx[rindex]->content->collapsed)
{
curlevel = actx->idx[rindex]->level;
do
{
rindex++;
} while ((rindex < actx->idxlen) && (actx->idx[rindex]->level > curlevel));
}
else
rindex++;
}
actx->vcount = vindex;
}
/**
* mutt_update_tree - Refresh the list of attachments
* @param actx Attachment context
*/
void mutt_update_tree(struct AttachCtx *actx)
{
char buf[256];
char *s = NULL;
mutt_update_v2r(actx);
for (int vindex = 0; vindex < actx->vcount; vindex++)
{
const int rindex = actx->v2r[vindex];
actx->idx[rindex]->num = vindex;
if ((2 * (actx->idx[rindex]->level + 2)) < sizeof(buf))
{
if (actx->idx[rindex]->level)
{
s = buf + 2 * (actx->idx[rindex]->level - 1);
*s++ = (actx->idx[rindex]->content->next) ? MUTT_TREE_LTEE : MUTT_TREE_LLCORNER;
*s++ = MUTT_TREE_HLINE;
*s++ = MUTT_TREE_RARROW;
}
else
s = buf;
*s = '\0';
}
if (actx->idx[rindex]->tree)
{
if (mutt_str_strcmp(actx->idx[rindex]->tree, buf) != 0)
mutt_str_replace(&actx->idx[rindex]->tree, buf);
}
else
actx->idx[rindex]->tree = mutt_str_strdup(buf);
if (((2 * (actx->idx[rindex]->level + 2)) < sizeof(buf)) &&
actx->idx[rindex]->level)
{
s = buf + 2 * (actx->idx[rindex]->level - 1);
*s++ = (actx->idx[rindex]->content->next) ? '\005' : '\006';
*s++ = '\006';
}
}
}
/**
* attach_format_str - Format a string for the attachment menu - Implements ::format_t
*
* | Expando | Description
* |:--------|:--------------------------------------------------------
* | \%C | Character set
* | \%c | Character set: convert?
* | \%D | Deleted flag
* | \%d | Description
* | \%e | MIME content-transfer-encoding
* | \%f | Filename
* | \%F | Filename for content-disposition header
* | \%I | Content-disposition, either I (inline) or A (attachment)
* | \%m | Major MIME type
* | \%M | MIME subtype
* | \%n | Attachment number
* | \%Q | 'Q', if MIME part qualifies for attachment counting
* | \%s | Size
* | \%t | Tagged flag
* | \%T | Tree chars
* | \%u | Unlink
* | \%X | Number of qualifying MIME parts in this part and its children
*/
const char *attach_format_str(char *buf, size_t buflen, size_t col, int cols,
char op, const char *src, const char *prec,
const char *if_str, const char *else_str,
unsigned long data, MuttFormatFlags flags)
{
char fmt[128];
char charset[128];
struct AttachPtr *aptr = (struct AttachPtr *) data;
bool optional = (flags & MUTT_FORMAT_OPTIONAL);
switch (op)
{
case 'C':
if (!optional)
{
if (mutt_is_text_part(aptr->content) &&
mutt_body_get_charset(aptr->content, charset, sizeof(charset)))
{
mutt_format_s(buf, buflen, prec, charset);
}
else
mutt_format_s(buf, buflen, prec, "");
}
else if (!mutt_is_text_part(aptr->content) ||
!mutt_body_get_charset(aptr->content, charset, sizeof(charset)))
{
optional = false;
}
break;
case 'c':
/* XXX */
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sc", prec);
snprintf(buf, buflen, fmt,
((aptr->content->type != TYPE_TEXT) || aptr->content->noconv) ? 'n' : 'c');
}
else if ((aptr->content->type != TYPE_TEXT) || aptr->content->noconv)
optional = false;
break;
case 'd':
if (!optional)
{
if (aptr->content->description)
{
mutt_format_s(buf, buflen, prec, aptr->content->description);
break;
}
if (mutt_is_message_type(aptr->content->type, aptr->content->subtype) &&
C_MessageFormat && aptr->content->email)
{
char s[128];
mutt_make_string_flags(s, sizeof(s), cols, C_MessageFormat, NULL,
NULL, aptr->content->email,
MUTT_FORMAT_FORCESUBJ | MUTT_FORMAT_ARROWCURSOR);
if (*s)
{
mutt_format_s(buf, buflen, prec, s);
break;
}
}
if (!aptr->content->d_filename && !aptr->content->filename)
{
mutt_format_s(buf, buflen, prec, "<no description>");
break;
}
}
else if (aptr->content->description ||
(mutt_is_message_type(aptr->content->type, aptr->content->subtype) &&
C_MessageFormat && aptr->content->email))
{
break;
}
/* fallthrough */
case 'F':
if (!optional)
{
if (aptr->content->d_filename)
{
mutt_format_s(buf, buflen, prec, aptr->content->d_filename);
break;
}
}
else if (!aptr->content->d_filename && !aptr->content->filename)
{
optional = false;
break;
}
/* fallthrough */
case 'f':
if (!optional)
{
if (aptr->content->filename && (*aptr->content->filename == '/'))
{
struct Buffer *path = mutt_buffer_pool_get();
mutt_buffer_strcpy(path, aptr->content->filename);
mutt_buffer_pretty_mailbox(path);
mutt_format_s(buf, buflen, prec, mutt_b2s(path));
mutt_buffer_pool_release(&path);
}
else
mutt_format_s(buf, buflen, prec, NONULL(aptr->content->filename));
}
else if (!aptr->content->filename)
optional = false;
break;
case 'D':
if (!optional)
snprintf(buf, buflen, "%c", aptr->content->deleted ? 'D' : ' ');
else if (!aptr->content->deleted)
optional = false;
break;
case 'e':
if (!optional)
mutt_format_s(buf, buflen, prec, ENCODING(aptr->content->encoding));
break;
case 'I':
if (!optional)
{
const char dispchar[] = { 'I', 'A', 'F', '-' };
char ch;
if (aptr->content->disposition < sizeof(dispchar))
ch = dispchar[aptr->content->disposition];
else
{
mutt_debug(LL_DEBUG1, "ERROR: invalid content-disposition %d\n",
aptr->content->disposition);
ch = '!';
}
snprintf(buf, buflen, "%c", ch);
}
break;
case 'm':
if (!optional)
mutt_format_s(buf, buflen, prec, TYPE(aptr->content));
break;
case 'M':
if (!optional)
mutt_format_s(buf, buflen, prec, aptr->content->subtype);
else if (!aptr->content->subtype)
optional = false;
break;
case 'n':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, aptr->num + 1);
}
break;
case 'Q':
if (optional)
optional = aptr->content->attach_qualifies;
else
{
snprintf(fmt, sizeof(fmt), "%%%sc", prec);
mutt_format_s(buf, buflen, fmt, "Q");
}
break;
case 's':
{
size_t l;
if (flags & MUTT_FORMAT_STAT_FILE)
{
struct stat st;
stat(aptr->content->filename, &st);
l = st.st_size;
}
else
l = aptr->content->length;
if (!optional)
{
char tmp[128];
mutt_str_pretty_size(tmp, sizeof(tmp), l);
mutt_format_s(buf, buflen, prec, tmp);
}
else if (l == 0)
optional = false;
break;
}
case 't':
if (!optional)
snprintf(buf, buflen, "%c", aptr->content->tagged ? '*' : ' ');
else if (!aptr->content->tagged)
optional = false;
break;
case 'T':
if (!optional)
mutt_format_s_tree(buf, buflen, prec, NONULL(aptr->tree));
else if (!aptr->tree)
optional = false;
break;
case 'u':
if (!optional)
snprintf(buf, buflen, "%c", aptr->content->unlink ? '-' : ' ');
else if (!aptr->content->unlink)
optional = false;
break;
case 'X':
if (optional)
optional = ((aptr->content->attach_count + aptr->content->attach_qualifies) != 0);
else
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, aptr->content->attach_count + aptr->content->attach_qualifies);
}
break;
default:
*buf = '\0';
}
if (optional)
{
mutt_expando_format(buf, buflen, col, cols, if_str, attach_format_str, data,
MUTT_FORMAT_NO_FLAGS);
}
else if (flags & MUTT_FORMAT_OPTIONAL)
{
mutt_expando_format(buf, buflen, col, cols, else_str, attach_format_str,
data, MUTT_FORMAT_NO_FLAGS);
}
return src;
}
/**
* attach_make_entry - Format a menu item for the attachment list - Implements Menu::menu_make_entry()
*/
static void attach_make_entry(char *buf, size_t buflen, struct Menu *menu, int line)
{
struct AttachCtx *actx = menu->data;
mutt_expando_format(buf, buflen, 0, menu->win_index->state.cols,
NONULL(C_AttachFormat), attach_format_str,
(unsigned long) (actx->idx[actx->v2r[line]]), MUTT_FORMAT_ARROWCURSOR);
}
/**
* attach_tag - Tag an attachment - Implements Menu::menu_tag()
*/
int attach_tag(struct Menu *menu, int sel, int act)
{
struct AttachCtx *actx = menu->data;
struct Body *cur = actx->idx[actx->v2r[sel]]->content;
bool ot = cur->tagged;
cur->tagged = ((act >= 0) ? act : !cur->tagged);
return cur->tagged - ot;
}
/**
* prepend_savedir - Add #C_AttachSaveDir to the beginning of a path
* @param buf Buffer for the result
*/
static void prepend_savedir(struct Buffer *buf)
{
if (!buf || !buf->data || (buf->data[0] == '/'))
return;
struct Buffer *tmp = mutt_buffer_pool_get();
if (C_AttachSaveDir)
{
mutt_buffer_addstr(tmp, C_AttachSaveDir);
if (tmp->dptr[-1] != '/')
mutt_buffer_addch(tmp, '/');
}
else
mutt_buffer_addstr(tmp, "./");
mutt_buffer_addstr(tmp, mutt_b2s(buf));
mutt_buffer_copy(buf, tmp);
mutt_buffer_pool_release(&tmp);
}
/**
* has_a_message - Determine if the Body has a message (to save)
* @param[in] body Body of the message
* @retval true if suitable for saving
*/
static bool has_a_message(struct Body *body)
{
return (body->email && (body->encoding != ENC_BASE64) &&
(body->encoding != ENC_QUOTED_PRINTABLE) &&
mutt_is_message_type(body->type, body->subtype));
}
/**
* query_save_attachment - Ask the user if we should save the attachment
* @param[in] fp File handle to the attachment (OPTIONAL)
* @param[in] body Attachment
* @param[in] e Email
* @param[out] directory Where the attachment was saved
* @retval 0 Success
* @retval -1 Failure
*/
static int query_save_attachment(FILE *fp, struct Body *body, struct Email *e, char **directory)
{
char *prompt = NULL;
enum SaveAttach opt = MUTT_SAVE_NO_FLAGS;
int rc = -1;
struct Buffer *buf = mutt_buffer_pool_get();
struct Buffer *tfile = mutt_buffer_pool_get();
if (body->filename)
{
if (directory && *directory)
{
mutt_buffer_concat_path(buf, *directory, mutt_path_basename(body->filename));
}
else
mutt_buffer_strcpy(buf, body->filename);
}
else if (has_a_message(body))
{
mutt_default_save(buf->data, buf->dsize, body->email);
mutt_buffer_fix_dptr(buf);
}
prepend_savedir(buf);
prompt = _("Save to file: ");
while (prompt)
{
if ((mutt_buffer_get_field(prompt, buf, MUTT_FILE | MUTT_CLEAR) != 0) ||
mutt_buffer_is_empty(buf))
{
goto cleanup;
}
prompt = NULL;
mutt_buffer_expand_path(buf);
bool is_message = (fp && has_a_message(body));
if (is_message)
{
struct stat st;
/* check to make sure that this file is really the one the user wants */
rc = mutt_save_confirm(mutt_b2s(buf), &st);
if (rc == 1)
{
prompt = _("Save to file: ");
continue;
}
else if (rc == -1)
goto cleanup;
mutt_buffer_copy(tfile, buf);
}
else
{
rc = mutt_check_overwrite(body->filename, mutt_b2s(buf), tfile, &opt, directory);
if (rc == -1)
goto cleanup;
else if (rc == 1)
{
prompt = _("Save to file: ");
continue;
}
}
mutt_message(_("Saving..."));
if (mutt_save_attachment(fp, body, mutt_b2s(tfile), opt,
(e || !is_message) ? e : body->email) == 0)
{
mutt_message(_("Attachment saved"));
rc = 0;
goto cleanup;
}
else
{
prompt = _("Save to file: ");
continue;
}
}
cleanup:
mutt_buffer_pool_release(&buf);
mutt_buffer_pool_release(&tfile);
return rc;
}
/**
* save_without_prompting - Save the attachment, without prompting each time.
* @param[in] fp File handle to the attachment (OPTIONAL)
* @param[in] body Attachment
* @param[in] e Email
* @retval 0 Success
* @retval -1 Failure
*/
static int save_without_prompting(FILE *fp, struct Body *body, struct Email *e)
{
enum SaveAttach opt = MUTT_SAVE_NO_FLAGS;
int rc = -1;
struct Buffer *buf = mutt_buffer_pool_get();
struct Buffer *tfile = mutt_buffer_pool_get();
if (body->filename)
{
mutt_buffer_strcpy(buf, body->filename);
}
else if (has_a_message(body))
{
mutt_default_save(buf->data, buf->dsize, body->email);
}
prepend_savedir(buf);
mutt_buffer_expand_path(buf);
bool is_message = (fp && has_a_message(body));
if (is_message)
{
mutt_buffer_copy(tfile, buf);
}
else
{
rc = mutt_check_overwrite(body->filename, mutt_b2s(buf), tfile, &opt, NULL);
if (rc == -1) // abort or cancel
goto cleanup;
}
rc = mutt_save_attachment(fp, body, mutt_b2s(tfile), opt,
(e || !is_message) ? e : body->email);
cleanup:
mutt_buffer_pool_release(&buf);
mutt_buffer_pool_release(&tfile);
return rc;
}
/**
* mutt_save_attachment_list - Save a list of attachments
* @param actx Attachment context
* @param fp File handle for the attachment (OPTIONAL)
* @param tag If true, only save the tagged attachments
* @param top First Attachment
* @param e Email
* @param menu Menu listing attachments
*/
void mutt_save_attachment_list(struct AttachCtx *actx, FILE *fp, bool tag,
struct Body *top, struct Email *e, struct Menu *menu)
{
char *directory = NULL;
int rc = 1;
int last = menu ? menu->current : -1;
FILE *fp_out = NULL;
int saved_attachments = 0;
struct Buffer *buf = mutt_buffer_pool_get();
struct Buffer *tfile = mutt_buffer_pool_get();
for (int i = 0; !tag || (i < actx->idxlen); i++)
{
if (tag)
{
fp = actx->idx[i]->fp;
top = actx->idx[i]->content;
}
if (!tag || top->tagged)
{
if (!C_AttachSplit)
{
if (mutt_buffer_is_empty(buf))
{
enum SaveAttach opt = MUTT_SAVE_NO_FLAGS;
mutt_buffer_strcpy(buf, mutt_path_basename(NONULL(top->filename)));
prepend_savedir(buf);
if ((mutt_buffer_get_field(_("Save to file: "), buf, MUTT_FILE | MUTT_CLEAR) != 0) ||
mutt_buffer_is_empty(buf))
{
goto cleanup;
}
mutt_buffer_expand_path(buf);
if (mutt_check_overwrite(top->filename, mutt_b2s(buf), tfile, &opt, NULL))
goto cleanup;
rc = mutt_save_attachment(fp, top, mutt_b2s(tfile), opt, e);
if ((rc == 0) && C_AttachSep && (fp_out = fopen(mutt_b2s(tfile), "a")))
{
fprintf(fp_out, "%s", C_AttachSep);
mutt_file_fclose(&fp_out);
}
}
else
{
rc = mutt_save_attachment(fp, top, mutt_b2s(tfile), MUTT_SAVE_APPEND, e);
if ((rc == 0) && C_AttachSep && (fp_out = fopen(mutt_b2s(tfile), "a")))
{
fprintf(fp_out, "%s", C_AttachSep);
mutt_file_fclose(&fp_out);
}
}
}
else
{
if (tag && menu && top->aptr)
{
menu->oldcurrent = menu->current;
menu->current = top->aptr->num;
menu_check_recenter(menu);
menu->redraw |= REDRAW_MOTION;
menu_redraw(menu);
}
if (C_AttachSaveWithoutPrompting)
{
// Save each file, with no prompting, using the configured 'AttachSaveDir'
rc = save_without_prompting(fp, top, e);
if (rc == 0)
saved_attachments++;
}
else
{
// Save each file, prompting the user for the location each time.
if (query_save_attachment(fp, top, e, &directory) == -1)
break;
}
}
}
if (!tag)
break;
}
FREE(&directory);
if (tag && menu)
{
menu->oldcurrent = menu->current;
menu->current = last;
menu_check_recenter(menu);
menu->redraw |= REDRAW_MOTION;
}
if (!C_AttachSplit && (rc == 0))
mutt_message(_("Attachment saved"));
if (C_AttachSaveWithoutPrompting && (rc == 0))
{
mutt_message(ngettext("Attachment saved", "%d attachments saved", saved_attachments),
saved_attachments);
}
cleanup:
mutt_buffer_pool_release(&buf);
mutt_buffer_pool_release(&tfile);
}
/**
* query_pipe_attachment - Ask the user if we should pipe the attachment
* @param command Command to pipe the attachment to
* @param fp File handle to the attachment (OPTIONAL)
* @param body Attachment
* @param filter Is this command a filter?
*/
static void query_pipe_attachment(char *command, FILE *fp, struct Body *body, bool filter)
{
char tfile[PATH_MAX];
if (filter)
{
char warning[PATH_MAX + 256];
snprintf(warning, sizeof(warning),
_("WARNING! You are about to overwrite %s, continue?"), body->filename);
if (mutt_yesorno(warning, MUTT_NO) != MUTT_YES)
{
mutt_window_clearline(MuttMessageWindow, 0);
return;
}
mutt_mktemp(tfile, sizeof(tfile));
}
else
tfile[0] = '\0';
if (mutt_pipe_attachment(fp, body, command, tfile))
{
if (filter)
{
mutt_file_unlink(body->filename);
mutt_file_rename(tfile, body->filename);
mutt_update_encoding(body);
mutt_message(_("Attachment filtered"));
}
}
else
{
if (filter && tfile[0])
mutt_file_unlink(tfile);
}
}
/**
* pipe_attachment - Pipe the attachment to a command
* @param fp File handle to the attachment (OPTIONAL)
* @param b Attachment
* @param state File state for decoding the attachment
*/
static void pipe_attachment(FILE *fp, struct Body *b, struct State *state)
{
if (!state || !state->fp_out)
return;
if (fp)
{
state->fp_in = fp;
mutt_decode_attachment(b, state);
if (C_AttachSep)
state_puts(state, C_AttachSep);
}
else
{
FILE *fp_in = fopen(b->filename, "r");
if (!fp_in)
{
mutt_perror("fopen");
return;
}
mutt_file_copy_stream(fp_in, state->fp_out);
mutt_file_fclose(&fp_in);
if (C_AttachSep)
state_puts(state, C_AttachSep);
}
}
/**
* pipe_attachment_list - Pipe a list of attachments to a command
* @param command Command to pipe the attachment to
* @param actx Attachment context
* @param fp File handle to the attachment (OPTIONAL)
* @param tag If true, only save the tagged attachments
* @param top First Attachment
* @param filter Is this command a filter?
* @param state File state for decoding the attachments
*/
static void pipe_attachment_list(char *command, struct AttachCtx *actx, FILE *fp, bool tag,
struct Body *top, bool filter, struct State *state)
{
for (int i = 0; !tag || (i < actx->idxlen); i++)
{
if (tag)
{
fp = actx->idx[i]->fp;
top = actx->idx[i]->content;
}
if (!tag || top->tagged)
{
if (!filter && !C_AttachSplit)
pipe_attachment(fp, top, state);
else
query_pipe_attachment(command, fp, top, filter);
}
if (!tag)
break;
}
}
/**
* mutt_pipe_attachment_list - Pipe a list of attachments to a command
* @param actx Attachment context
* @param fp File handle to the attachment (OPTIONAL)
* @param tag If true, only save the tagged attachments
* @param top First Attachment
* @param filter Is this command a filter?
*/
void mutt_pipe_attachment_list(struct AttachCtx *actx, FILE *fp, bool tag,
struct Body *top, bool filter)
{
struct State state = { 0 };
char buf[PATH_MAX];
if (fp)
filter = false; /* sanity check: we can't filter in the recv case yet */
buf[0] = '\0';
/* perform charset conversion on text attachments when piping */
state.flags = MUTT_CHARCONV;
if ((mutt_get_field((filter ? _("Filter through: ") : _("Pipe to: ")), buf,
sizeof(buf), MUTT_CMD) != 0) ||
(buf[0] == '\0'))
{
return;
}
mutt_expand_path(buf, sizeof(buf));
if (!filter && !C_AttachSplit)
{
mutt_endwin();
pid_t pid = filter_create(buf, &state.fp_out, NULL, NULL);
pipe_attachment_list(buf, actx, fp, tag, top, filter, &state);
mutt_file_fclose(&state.fp_out);
if ((filter_wait(pid) != 0) || C_WaitKey)
mutt_any_key_to_continue(NULL);
}
else
pipe_attachment_list(buf, actx, fp, tag, top, filter, &state);
}
/**
* can_print - Do we know how to print this attachment type?
* @param actx Attachment
* @param top Body of email
* @param tag Apply to all tagged Attachments
* @retval true If (all) the Attachment(s) are printable
*/
static bool can_print(struct AttachCtx *actx, struct Body *top, bool tag)
{
char type[256];
for (int i = 0; !tag || (i < actx->idxlen); i++)
{
if (tag)
top = actx->idx[i]->content;
snprintf(type, sizeof(type), "%s/%s", TYPE(top), top->subtype);
if (!tag || top->tagged)
{
if (!mailcap_lookup(top, type, sizeof(type), NULL, MUTT_MC_PRINT))
{
if ((mutt_str_strcasecmp("text/plain", top->subtype) != 0) &&
(mutt_str_strcasecmp("application/postscript", top->subtype) != 0))
{
if (!mutt_can_decode(top))
{
/* L10N: s gets replaced by a MIME type, e.g. "text/plain" or
application/octet-stream. */
mutt_error(_("I don't know how to print %s attachments"), type);
return false;
}
}
}
}
if (!tag)
break;
}
return true;
}
/**
* print_attachment_list - Print a list of Attachments
* @param actx Attachment context
* @param fp File handle to the attachment (OPTIONAL)
* @param tag Apply to all tagged Attachments
* @param top First Attachment
* @param state File state for decoding the attachments
*/
static void print_attachment_list(struct AttachCtx *actx, FILE *fp, bool tag,
struct Body *top, struct State *state)
{
char type[256];
for (int i = 0; !tag || (i < actx->idxlen); i++)
{
if (tag)
{
fp = actx->idx[i]->fp;
top = actx->idx[i]->content;
}
if (!tag || top->tagged)
{
snprintf(type, sizeof(type), "%s/%s", TYPE(top), top->subtype);
if (!C_AttachSplit && !mailcap_lookup(top, type, sizeof(type), NULL, MUTT_MC_PRINT))
{
if ((mutt_str_strcasecmp("text/plain", top->subtype) == 0) ||
(mutt_str_strcasecmp("application/postscript", top->subtype) == 0))
{
pipe_attachment(fp, top, state);
}
else if (mutt_can_decode(top))
{
/* decode and print */
FILE *fp_in = NULL;
struct Buffer *newfile = mutt_buffer_pool_get();
mutt_buffer_mktemp(newfile);
if (mutt_decode_save_attachment(fp, top, mutt_b2s(newfile),
MUTT_PRINTING, MUTT_SAVE_NO_FLAGS) == 0)
{
if (!state->fp_out)
{
mutt_error(
"BUG in print_attachment_list(). Please report this. ");
return;
}
fp_in = fopen(mutt_b2s(newfile), "r");
if (fp_in)
{
mutt_file_copy_stream(fp_in, state->fp_out);