forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecvcmd.c
1111 lines (989 loc) · 27.8 KB
/
recvcmd.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
* Send/reply with an attachment
*
* @authors
* Copyright (C) 1999-2004 Thomas Roessler <[email protected]>
* Copyright (C) 2019 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 recvcmd Send/reply with an attachment
*
* Send/reply with an attachment
*/
#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "mutt/mutt.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "recvcmd.h"
#include "alias.h"
#include "context.h"
#include "copy.h"
#include "globals.h"
#include "handler.h"
#include "hdrline.h"
#include "mutt_body.h"
#include "mutt_logging.h"
#include "muttlib.h"
#include "options.h"
#include "protos.h"
#include "send.h"
#include "sendlib.h"
#include "state.h"
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
struct Mailbox;
/* These Config Variables are only used in recvcmd.c */
unsigned char C_MimeForwardRest; ///< Config: Forward all attachments, even if they can't be decoded
/**
* check_msg - Are we working with an RFC822 message
* @param b Body of email
* @param err If true, display a message if this isn't an RFC822 message
* @retval true This is an RFC822 message
*
* some helper functions to verify that we are exclusively operating on
* message/rfc822 attachments
*/
static bool check_msg(struct Body *b, bool err)
{
if (!mutt_is_message_type(b->type, b->subtype))
{
if (err)
mutt_error(_("You may only bounce message/rfc822 parts"));
return false;
}
return true;
}
/**
* check_all_msg - Are all the Attachments RFC822 messages?
* @param actx Attachment context
* @param cur Current message
* @param err If true, report errors
* @retval true If all parts are RFC822 messages
*/
static bool check_all_msg(struct AttachCtx *actx, struct Body *cur, bool err)
{
if (cur && !check_msg(cur, err))
return false;
if (!cur)
{
for (short i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged)
{
if (!check_msg(actx->idx[i]->content, err))
return false;
}
}
}
return true;
}
/**
* check_can_decode - Can we decode all tagged attachments?
* @param actx Attachment context
* @param cur Body of email
* @retval true All tagged attachments are decodable
*/
static bool check_can_decode(struct AttachCtx *actx, struct Body *cur)
{
if (cur)
return mutt_can_decode(cur);
for (short i = 0; i < actx->idxlen; i++)
if (actx->idx[i]->content->tagged && !mutt_can_decode(actx->idx[i]->content))
return false;
return true;
}
/**
* count_tagged - Count the number of tagged attachments
* @param actx Attachment context
* @retval num Number of tagged attachments
*/
static short count_tagged(struct AttachCtx *actx)
{
short count = 0;
for (short i = 0; i < actx->idxlen; i++)
if (actx->idx[i]->content->tagged)
count++;
return count;
}
/**
* count_tagged_children - tagged children below a multipart/message attachment
* @param actx Attachment context
* @param i Index of first attachment
* @retval num Number of tagged attachments
*/
static short count_tagged_children(struct AttachCtx *actx, short i)
{
short level = actx->idx[i]->level;
short count = 0;
while ((++i < actx->idxlen) && (level < actx->idx[i]->level))
if (actx->idx[i]->content->tagged)
count++;
return count;
}
/**
* mutt_attach_bounce - Bounce function, from the attachment menu
* @param m Mailbox
* @param fp Handle of message
* @param actx Attachment context
* @param cur Body of email
*/
void mutt_attach_bounce(struct Mailbox *m, FILE *fp, struct AttachCtx *actx, struct Body *cur)
{
if (!m || !fp || !actx)
return;
char prompt[256];
char buf[8192];
char *err = NULL;
int ret = 0;
int p = 0;
if (!check_all_msg(actx, cur, true))
return;
/* one or more messages? */
p = cur ? 1 : count_tagged(actx);
/* RFC5322 mandates a From: header, so warn before bouncing
* messages without one */
if (cur)
{
if (TAILQ_EMPTY(&cur->email->env->from))
{
mutt_error(_("Warning: message contains no From: header"));
mutt_clear_error();
}
}
else
{
for (short i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged)
{
if (TAILQ_EMPTY(&actx->idx[i]->content->email->env->from))
{
mutt_error(_("Warning: message contains no From: header"));
mutt_clear_error();
break;
}
}
}
}
if (p)
mutt_str_strfcpy(prompt, _("Bounce message to: "), sizeof(prompt));
else
mutt_str_strfcpy(prompt, _("Bounce tagged messages to: "), sizeof(prompt));
buf[0] = '\0';
if (mutt_get_field(prompt, buf, sizeof(buf), MUTT_ALIAS) || (buf[0] == '\0'))
return;
struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
mutt_addrlist_parse(&al, buf);
if (TAILQ_EMPTY(&al))
{
mutt_error(_("Error parsing address"));
return;
}
mutt_expand_aliases(&al);
if (mutt_addrlist_to_intl(&al, &err) < 0)
{
mutt_error(_("Bad IDN: '%s'"), err);
FREE(&err);
goto end;
}
buf[0] = '\0';
mutt_addrlist_write(&al, buf, sizeof(buf), true);
#define EXTRA_SPACE (15 + 7 + 2)
/* See commands.c. */
snprintf(prompt, sizeof(prompt) - 4,
ngettext("Bounce message to %s?", "Bounce messages to %s?", p), buf);
if (mutt_strwidth(prompt) > MuttMessageWindow->state.cols - EXTRA_SPACE)
{
mutt_simple_format(prompt, sizeof(prompt) - 4, 0,
MuttMessageWindow->state.cols - EXTRA_SPACE,
JUSTIFY_LEFT, 0, prompt, sizeof(prompt), false);
mutt_str_strcat(prompt, sizeof(prompt), "...?");
}
else
mutt_str_strcat(prompt, sizeof(prompt), "?");
if (query_quadoption(C_Bounce, prompt) != MUTT_YES)
{
mutt_window_clearline(MuttMessageWindow, 0);
mutt_message(ngettext("Message not bounced", "Messages not bounced", p));
goto end;
}
mutt_window_clearline(MuttMessageWindow, 0);
if (cur)
ret = mutt_bounce_message(fp, cur->email, &al);
else
{
for (short i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged)
if (mutt_bounce_message(actx->idx[i]->fp, actx->idx[i]->content->email, &al))
ret = 1;
}
}
if (ret == 0)
mutt_message(ngettext("Message bounced", "Messages bounced", p));
else
mutt_error(ngettext("Error bouncing message", "Error bouncing messages", p));
end:
mutt_addrlist_clear(&al);
}
/**
* mutt_attach_resend - resend-message, from the attachment menu
* @param fp File containing email
* @param actx Attachment context
* @param cur Attachment
*/
void mutt_attach_resend(FILE *fp, struct AttachCtx *actx, struct Body *cur)
{
if (!check_all_msg(actx, cur, true))
return;
if (cur)
mutt_resend_message(fp, Context, cur->email);
else
{
for (short i = 0; i < actx->idxlen; i++)
if (actx->idx[i]->content->tagged)
mutt_resend_message(actx->idx[i]->fp, Context, actx->idx[i]->content->email);
}
}
/**
* find_common_parent - find a common parent message for the tagged attachments
* @param actx Attachment context
* @param nattach Number of tagged attachments
* @retval ptr Parent attachment
* @retval NULL Failure, no common parent
*/
static struct AttachPtr *find_common_parent(struct AttachCtx *actx, short nattach)
{
short i;
short nchildren;
for (i = 0; i < actx->idxlen; i++)
if (actx->idx[i]->content->tagged)
break;
while (--i >= 0)
{
if (mutt_is_message_type(actx->idx[i]->content->type, actx->idx[i]->content->subtype))
{
nchildren = count_tagged_children(actx, i);
if (nchildren == nattach)
return actx->idx[i];
}
}
return NULL;
}
/**
* is_parent - Check whether one attachment is the parent of another
* @param i Index of parent Attachment
* @param actx Attachment context
* @param cur Potential child Attachemnt
* @retval true Attachment
*
* check whether attachment i is a parent of the attachment pointed to by cur
*
* Note: This and the calling procedure could be optimized quite a bit.
* For now, it's not worth the effort.
*/
static int is_parent(short i, struct AttachCtx *actx, struct Body *cur)
{
short level = actx->idx[i]->level;
while ((++i < actx->idxlen) && (actx->idx[i]->level > level))
{
if (actx->idx[i]->content == cur)
return true;
}
return false;
}
/**
* find_parent - Find the parent of an Attachment
* @param actx Attachment context
* @param cur Attachment (OPTIONAL)
* @param nattach Use the nth attachment
* @retval ptr Parent attachment
* @retval NULL No parent exists
*/
static struct AttachPtr *find_parent(struct AttachCtx *actx, struct Body *cur, short nattach)
{
struct AttachPtr *parent = NULL;
if (cur)
{
for (short i = 0; i < actx->idxlen; i++)
{
if (mutt_is_message_type(actx->idx[i]->content->type,
actx->idx[i]->content->subtype) &&
is_parent(i, actx, cur))
{
parent = actx->idx[i];
}
if (actx->idx[i]->content == cur)
break;
}
}
else if (nattach)
parent = find_common_parent(actx, nattach);
return parent;
}
/**
* include_header - Write an email header to a file, optionally quoting it
* @param quote If true, prefix the lines
* @param fp_in File to read from
* @param e Email
* @param fp_out File to write to
* @param prefix Prefix for each line (OPTIONAL)
*/
static void include_header(bool quote, FILE *fp_in, struct Email *e, FILE *fp_out, char *prefix)
{
CopyHeaderFlags chflags = CH_DECODE;
char prefix2[128];
if (C_Weed)
chflags |= CH_WEED | CH_REORDER;
if (quote)
{
if (prefix)
mutt_str_strfcpy(prefix2, prefix, sizeof(prefix2));
else if (!C_TextFlowed)
{
mutt_make_string(prefix2, sizeof(prefix2), 0, NONULL(C_IndentString),
Context, Context->mailbox, e);
}
else
mutt_str_strfcpy(prefix2, ">", sizeof(prefix2));
chflags |= CH_PREFIX;
}
mutt_copy_header(fp_in, e, fp_out, chflags, quote ? prefix2 : NULL, 0);
}
/**
* copy_problematic_attachments - Attach the body parts which can't be decoded
* @param[out] last Body pointer to update
* @param[in] actx Attachment context
* @param[in] force If true, attach parts that can't be decoded
* @retval ptr Pointer to last Body part
*
* This code is shared by forwarding and replying.
*/
static struct Body **copy_problematic_attachments(struct Body **last,
struct AttachCtx *actx, bool force)
{
for (short i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged && (force || !mutt_can_decode(actx->idx[i]->content)))
{
if (mutt_body_copy(actx->idx[i]->fp, last, actx->idx[i]->content) == -1)
return NULL; /* XXXXX - may lead to crashes */
last = &((*last)->next);
}
}
return last;
}
/**
* attach_forward_bodies - forward one or several MIME bodies
* @param fp File to read from
* @param e Email
* @param actx Attachment Context
* @param cur Body of email
* @param nattach Number of tagged attachments
*
* (non-message types)
*/
static void attach_forward_bodies(FILE *fp, struct Email *e, struct AttachCtx *actx,
struct Body *cur, short nattach)
{
bool mime_fwd_all = false;
bool mime_fwd_any = true;
struct Email *e_parent = NULL;
FILE *fp_parent = NULL;
char prefix[256];
enum QuadOption ans = MUTT_NO;
struct Buffer *tmpbody = NULL;
/* First, find the parent message.
* Note: This could be made an option by just
* putting the following lines into an if block. */
struct AttachPtr *parent = find_parent(actx, cur, nattach);
if (parent)
{
e_parent = parent->content->email;
fp_parent = parent->fp;
}
else
{
e_parent = e;
fp_parent = actx->fp_root;
}
struct Email *e_tmp = email_new();
e_tmp->env = mutt_env_new();
mutt_make_forward_subject(e_tmp->env, Context->mailbox, e_parent);
tmpbody = mutt_buffer_pool_get();
mutt_buffer_mktemp(tmpbody);
FILE *fp_tmp = mutt_file_fopen(mutt_b2s(tmpbody), "w");
if (!fp_tmp)
{
mutt_error(_("Can't open temporary file %s"), mutt_b2s(tmpbody));
email_free(&e_tmp);
goto bail;
}
mutt_forward_intro(Context->mailbox, e_parent, fp_tmp);
/* prepare the prefix here since we'll need it later. */
if (C_ForwardQuote)
{
if (C_TextFlowed)
mutt_str_strfcpy(prefix, ">", sizeof(prefix));
else
{
mutt_make_string(prefix, sizeof(prefix), 0, NONULL(C_IndentString),
Context, Context->mailbox, e_parent);
}
}
include_header(C_ForwardQuote, fp_parent, e_parent, fp_tmp, prefix);
/* Now, we have prepared the first part of the message body: The
* original message's header.
*
* The next part is more interesting: either include the message bodies,
* or attach them. */
if ((!cur || mutt_can_decode(cur)) &&
((ans = query_quadoption(C_MimeForward, _("Forward as attachments?"))) == MUTT_YES))
{
mime_fwd_all = true;
}
else if (ans == MUTT_ABORT)
{
goto bail;
}
/* shortcut MIMEFWDREST when there is only one attachment.
* Is this intuitive? */
if (!mime_fwd_all && !cur && (nattach > 1) && !check_can_decode(actx, cur))
{
ans = query_quadoption(
C_MimeForwardRest,
_("Can't decode all tagged attachments. MIME-forward the others?"));
if (ans == MUTT_ABORT)
goto bail;
else if (ans == MUTT_NO)
mime_fwd_any = false;
}
/* initialize a state structure */
struct State st = { 0 };
if (C_ForwardQuote)
st.prefix = prefix;
st.flags = MUTT_CHARCONV;
if (C_Weed)
st.flags |= MUTT_WEED;
st.fp_out = fp_tmp;
/* where do we append new MIME parts? */
struct Body **last = &e_tmp->content;
if (cur)
{
/* single body case */
if (!mime_fwd_all && mutt_can_decode(cur))
{
st.fp_in = fp;
mutt_body_handler(cur, &st);
state_putc(&st, '\n');
}
else
{
if (mutt_body_copy(fp, last, cur) == -1)
goto bail;
}
}
else
{
/* multiple body case */
if (!mime_fwd_all)
{
for (int i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged && mutt_can_decode(actx->idx[i]->content))
{
st.fp_in = actx->idx[i]->fp;
mutt_body_handler(actx->idx[i]->content, &st);
state_putc(&st, '\n');
}
}
}
if (mime_fwd_any && !copy_problematic_attachments(last, actx, mime_fwd_all))
goto bail;
}
mutt_forward_trailer(Context->mailbox, e_parent, fp_tmp);
mutt_file_fclose(&fp_tmp);
fp_tmp = NULL;
/* now that we have the template, send it. */
struct EmailList el = STAILQ_HEAD_INITIALIZER(el);
emaillist_add_email(&el, e_parent);
ci_send_message(SEND_NO_FLAGS, e_tmp, mutt_b2s(tmpbody), NULL, &el);
emaillist_clear(&el);
mutt_buffer_pool_release(&tmpbody);
return;
bail:
if (fp_tmp)
{
mutt_file_fclose(&fp_tmp);
mutt_file_unlink(mutt_b2s(tmpbody));
}
mutt_buffer_pool_release(&tmpbody);
email_free(&e_tmp);
}
/**
* attach_forward_msgs - Forward one or several message-type attachments
* @param fp File handle to attachment
* @param actx Attachment Context
* @param cur Attachment to forward (OPTIONAL)
* @param flags Send mode, see #SendFlags
*
* This is different from the previous function since we want to mimic the
* index menu's behavior.
*
* Code reuse from ci_send_message() is not possible here - it relies on a
* context structure to find messages, while, on the attachment menu, messages
* are referenced through the attachment index.
*/
static void attach_forward_msgs(FILE *fp, struct AttachCtx *actx,
struct Body *cur, SendFlags flags)
{
struct Email *e_cur = NULL;
struct Email *e_tmp = NULL;
enum QuadOption ans;
struct Body **last = NULL;
struct Buffer *tmpbody = NULL;
FILE *fp_tmp = NULL;
CopyHeaderFlags chflags = CH_XMIT;
if (cur)
e_cur = cur->email;
else
{
for (short i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged)
{
e_cur = actx->idx[i]->content->email;
break;
}
}
}
e_tmp = email_new();
e_tmp->env = mutt_env_new();
mutt_make_forward_subject(e_tmp->env, Context->mailbox, e_cur);
tmpbody = mutt_buffer_pool_get();
ans = query_quadoption(C_MimeForward, _("Forward MIME encapsulated?"));
if (ans == MUTT_NO)
{
/* no MIME encapsulation */
mutt_buffer_mktemp(tmpbody);
fp_tmp = mutt_file_fopen(mutt_b2s(tmpbody), "w");
if (!fp_tmp)
{
mutt_error(_("Can't create %s"), mutt_b2s(tmpbody));
goto cleanup;
}
CopyMessageFlags cmflags = MUTT_CM_NO_FLAGS;
if (C_ForwardQuote)
{
chflags |= CH_PREFIX;
cmflags |= MUTT_CM_PREFIX;
}
if (C_ForwardDecode)
{
cmflags |= MUTT_CM_DECODE | MUTT_CM_CHARCONV;
if (C_Weed)
{
chflags |= CH_WEED | CH_REORDER;
cmflags |= MUTT_CM_WEED;
}
}
if (cur)
{
mutt_forward_intro(Context->mailbox, cur->email, fp_tmp);
mutt_copy_message_fp(fp_tmp, fp, cur->email, cmflags, chflags, 0);
mutt_forward_trailer(Context->mailbox, cur->email, fp_tmp);
}
else
{
for (short i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged)
{
mutt_forward_intro(Context->mailbox, actx->idx[i]->content->email, fp_tmp);
mutt_copy_message_fp(fp_tmp, actx->idx[i]->fp,
actx->idx[i]->content->email, cmflags, chflags, 0);
mutt_forward_trailer(Context->mailbox, actx->idx[i]->content->email, fp_tmp);
}
}
}
mutt_file_fclose(&fp_tmp);
}
else if (ans == MUTT_YES) /* do MIME encapsulation - we don't need to do much here */
{
last = &e_tmp->content;
if (cur)
mutt_body_copy(fp, last, cur);
else
{
for (short i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged)
{
mutt_body_copy(actx->idx[i]->fp, last, actx->idx[i]->content);
last = &((*last)->next);
}
}
}
}
else
email_free(&e_tmp);
struct EmailList el = STAILQ_HEAD_INITIALIZER(el);
emaillist_add_email(&el, e_cur);
ci_send_message(flags, e_tmp,
mutt_buffer_is_empty(tmpbody) ? NULL : mutt_b2s(tmpbody), NULL, &el);
emaillist_clear(&el);
e_tmp = NULL; /* ci_send_message frees this */
cleanup:
email_free(&e_tmp);
mutt_buffer_pool_release(&tmpbody);
}
/**
* mutt_attach_forward - Forward an Attachment
* @param fp Handle to the attachment
* @param e Email
* @param actx Attachment Context
* @param cur Current message
* @param flags Send mode, see #SendFlags
*/
void mutt_attach_forward(FILE *fp, struct Email *e, struct AttachCtx *actx,
struct Body *cur, SendFlags flags)
{
if (check_all_msg(actx, cur, false))
attach_forward_msgs(fp, actx, cur, flags);
else
{
const short nattach = count_tagged(actx);
attach_forward_bodies(fp, e, actx, cur, nattach);
}
}
/**
* attach_reply_envelope_defaults - Create the envelope defaults for a reply
* @param env Envelope to fill in
* @param actx Attachment Context
* @param parent Parent Email
* @param flags Flags, see #SendFlags
* @retval 0 Success
* @retval -1 Error
*
* This function can be invoked in two ways.
*
* Either, parent is NULL. In this case, all tagged bodies are of a message type,
* and the header information is fetched from them.
*
* Or, parent is non-NULL. In this case, cur is the common parent of all the
* tagged attachments.
*
* Note that this code is horribly similar to envelope_defaults() from send.c.
*/
static int attach_reply_envelope_defaults(struct Envelope *env, struct AttachCtx *actx,
struct Email *parent, SendFlags flags)
{
struct Envelope *curenv = NULL;
struct Email *e = NULL;
if (!parent)
{
for (short i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged)
{
e = actx->idx[i]->content->email;
curenv = e->env;
break;
}
}
}
else
{
curenv = parent->env;
e = parent;
}
if (!curenv || !e)
{
mutt_error(_("Can't find any tagged messages"));
return -1;
}
#ifdef USE_NNTP
if ((flags & SEND_NEWS))
{
/* in case followup set Newsgroups: with Followup-To: if it present */
if (!env->newsgroups && curenv &&
(mutt_str_strcasecmp(curenv->followup_to, "poster") != 0))
{
env->newsgroups = mutt_str_strdup(curenv->followup_to);
}
}
else
#endif
{
if (parent)
{
if (mutt_fetch_recips(env, curenv, flags) == -1)
return -1;
}
else
{
for (short i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged &&
(mutt_fetch_recips(env, actx->idx[i]->content->email->env, flags) == -1))
{
return -1;
}
}
}
if ((flags & SEND_LIST_REPLY) && TAILQ_EMPTY(&env->to))
{
mutt_error(_("No mailing lists found"));
return -1;
}
mutt_fix_reply_recipients(env);
}
mutt_make_misc_reply_headers(env, curenv);
if (parent)
mutt_add_to_reference_headers(env, curenv);
else
{
for (short i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged)
mutt_add_to_reference_headers(env, actx->idx[i]->content->email->env);
}
}
return 0;
}
/**
* attach_include_reply - This is _very_ similar to send.c's include_reply()
* @param fp File handle to attachment
* @param fp_tmp File handle to temporary file
* @param e Email
*/
static void attach_include_reply(FILE *fp, FILE *fp_tmp, struct Email *e)
{
CopyMessageFlags cmflags = MUTT_CM_PREFIX | MUTT_CM_DECODE | MUTT_CM_CHARCONV;
CopyHeaderFlags chflags = CH_DECODE;
mutt_make_attribution(Context->mailbox, e, fp_tmp);
if (!C_Header)
cmflags |= MUTT_CM_NOHEADER;
if (C_Weed)
{
chflags |= CH_WEED;
cmflags |= MUTT_CM_WEED;
}
mutt_copy_message_fp(fp_tmp, fp, e, cmflags, chflags, 0);
mutt_make_post_indent(Context->mailbox, e, fp_tmp);
}
/**
* mutt_attach_reply - Attach a reply
* @param fp File handle to reply
* @param e Email
* @param actx Attachment Context
* @param e_cur Current message
* @param flags Send mode, see #SendFlags
*/
void mutt_attach_reply(FILE *fp, struct Email *e, struct AttachCtx *actx,
struct Body *e_cur, SendFlags flags)
{
bool mime_reply_any = false;
short nattach = 0;
struct AttachPtr *parent = NULL;
struct Email *e_parent = NULL;
FILE *fp_parent = NULL;
struct Email *e_tmp = NULL;
FILE *fp_tmp = NULL;
struct Buffer *tmpbody = NULL;
char prefix[128];
#ifdef USE_NNTP
if (flags & SEND_NEWS)
OptNewsSend = true;
else
OptNewsSend = false;
#endif
if (!check_all_msg(actx, e_cur, false))
{
nattach = count_tagged(actx);
parent = find_parent(actx, e_cur, nattach);
if (parent)
{
e_parent = parent->content->email;
fp_parent = parent->fp;
}
else
{
e_parent = e;
fp_parent = actx->fp_root;
}
}
if ((nattach > 1) && !check_can_decode(actx, e_cur))
{
const enum QuadOption ans = query_quadoption(
C_MimeForwardRest, _("Can't decode all tagged attachments. "
"MIME-encapsulate the others?"));
if (ans == MUTT_ABORT)
return;
if (ans == MUTT_YES)
mime_reply_any = true;
}
else if (nattach == 1)
mime_reply_any = true;
e_tmp = email_new();
e_tmp->env = mutt_env_new();
if (attach_reply_envelope_defaults(
e_tmp->env, actx, e_parent ? e_parent : (e_cur ? e_cur->email : NULL), flags) == -1)
{
goto cleanup;
}
tmpbody = mutt_buffer_pool_get();
mutt_buffer_mktemp(tmpbody);
fp_tmp = mutt_file_fopen(mutt_b2s(tmpbody), "w");
if (!fp_tmp)
{
mutt_error(_("Can't create %s"), mutt_b2s(tmpbody));
goto cleanup;
}
if (!e_parent)
{
if (e_cur)
attach_include_reply(fp, fp_tmp, e_cur->email);
else
{
for (short i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->tagged)
attach_include_reply(actx->idx[i]->fp, fp_tmp, actx->idx[i]->content->email);
}
}
}
else
{
mutt_make_attribution(Context->mailbox, e_parent, fp_tmp);
struct State st;
memset(&st, 0, sizeof(struct State));
st.fp_out = fp_tmp;
if (C_TextFlowed)
{
mutt_str_strfcpy(prefix, ">", sizeof(prefix));
}
else
{
mutt_make_string(prefix, sizeof(prefix), 0, NONULL(C_IndentString),
Context, Context->mailbox, e_parent);