forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
952 lines (848 loc) · 25 KB
/
main.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
/**
* @file
* Command line processing
*
* @authors
* Copyright (C) 1996-2007,2010,2013 Michael R. Elkins <me@mutt.org>
* Copyright (C) 1999-2007 Thomas Roessler <roessler@does-not-exist.org>
* Copyright (C) 2004 g10 Code GmbH
*
* @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/>.
*/
#define MAIN_C 1
#include "config.h"
#include <errno.h>
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
#include <limits.h>
#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt.h"
#include "address.h"
#include "alias.h"
#include "body.h"
#include "buffy.h"
#include "envelope.h"
#include "globals.h"
#include "header.h"
#include "keymap.h"
#include "lib/lib.h"
#include "list.h"
#include "mailbox.h"
#include "mutt_curses.h"
#include "mutt_idna.h"
#include "mutt_regex.h"
#include "mutt_socket.h"
#include "ncrypt/ncrypt.h"
#include "options.h"
#include "protos.h"
#include "rfc822.h"
#include "url.h"
#include "version.h"
#ifdef USE_SIDEBAR
#include "sidebar.h"
#endif
#ifdef USE_SASL
#include "mutt_sasl.h"
#endif
#ifdef USE_IMAP
#include "imap/imap.h"
#endif
#ifdef USE_NNTP
#include "nntp.h"
#endif
char **envlist = NULL;
void mutt_exit(int code)
{
mutt_endwin(NULL);
exit(code);
}
// clang-format off
static void usage(void)
{
puts(mutt_make_version());
puts(_("usage: mutt [<options>] [-z] [-f <file> | -yZ]\n"
" mutt [<options>] [-Ex] [-Hi <file>] [-s <subj>] [-bc <addr>] [-a <file> [...] --] <addr> [...]\n"
" mutt [<options>] [-x] [-s <subj>] [-bc <addr>] [-a <file> [...] --] <addr> [...] < message\n"
" mutt [<options>] -p\n"
" mutt [<options>] -A <alias> [...]\n"
" mutt [<options>] -Q <query> [...]\n"
" mutt [<options>] -B\n"
" mutt [<options>] -D [-S]\n"
" mutt -v[v]\n"));
puts(_("options:\n"
" -A <alias> expand the given alias\n"
" -a <file> [...] -- attach file(s) to the message\n"
" the list of files must be terminated with the \"--\" sequence\n"
" -b <address> specify a blind carbon-copy (BCC) address\n"
" -c <address> specify a carbon-copy (CC) address\n"
" -D print the value of all variables to stdout\n"
" -D -S like -D, but hide the value of sensitive variables\n"
" -B run in batch mode (do not start the ncurses UI)"));
#ifdef DEBUG
puts(_(" -d <level> log debugging output to ~/.muttdebug0"));
#endif
puts(_(
" -E edit the draft (-H) or include (-i) file\n"
" -e <command> specify a command to be executed after initialization\n"
" -f <file> specify which mailbox to read\n"
" -F <file> specify an alternate muttrc file\n"
" -g <server> specify a news server (if compiled with NNTP)\n"
" -G select a newsgroup (if compiled with NNTP)\n"
" -H <file> specify a draft file to read header and body from\n"
" -i <file> specify a file which Mutt should include in the body\n"
" -m <type> specify a default mailbox type\n"
" -n causes Mutt not to read the system Muttrc\n"
" -p recall a postponed message"));
puts(_(" -Q <variable> query a configuration variable\n"
" -R open mailbox in read-only mode\n"
" -s <subj> specify a subject (must be in quotes if it has spaces)\n"
" -v show version and compile-time definitions\n"
" -x simulate the mailx send mode\n"
" -y select a mailbox specified in your `mailboxes' list\n"
" -z exit immediately if there are no messages in the mailbox\n"
" -Z open the first folder with new message, exit immediately if none\n"
" -h this help message"));
exit(0);
}
// clang-format on
static void start_curses(void)
{
km_init(); /* must come before mutt_init */
#ifdef USE_SLANG_CURSES
SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
SLtt_set_color(0, NULL, "default", "default");
#if SLANG_VERSION >= 20000
SLutf8_enable(-1);
#endif
#else
/* should come before initscr() so that ncurses 4.2 doesn't try to install
its own SIGWINCH handler */
mutt_signal_init();
#endif
if (initscr() == NULL)
{
puts(_("Error initializing terminal."));
exit(1);
}
/* slang requires the signal handlers to be set after initializing */
mutt_signal_init();
ci_start_color();
keypad(stdscr, true);
cbreak();
noecho();
nonl();
#ifdef HAVE_TYPEAHEAD
typeahead(-1); /* simulate smooth scrolling */
#endif
#ifdef HAVE_META
meta(stdscr, true);
#endif
init_extended_keys();
mutt_reflow_windows();
}
#define MUTT_IGNORE (1 << 0) /* -z */
#define MUTT_BUFFY (1 << 1) /* -Z */
#define MUTT_NOSYSRC (1 << 2) /* -n */
#define MUTT_RO (1 << 3) /* -R */
#define MUTT_SELECT (1 << 4) /* -y */
#ifdef USE_NNTP
#define MUTT_NEWS (1 << 5) /* -g and -G */
#endif
/**
* main - Start NeoMutt
* @param argc Number of command line arguments
* @param argv List of command line arguments
* @param env Copy of the environment
* @retval 0 on success
* @retval 1 on error
*/
int main(int argc, char **argv, char **env)
{
char folder[_POSIX_PATH_MAX] = "";
char *subject = NULL;
char *includeFile = NULL;
char *draftFile = NULL;
char *newMagic = NULL;
struct Header *msg = NULL;
struct ListHead attach = STAILQ_HEAD_INITIALIZER(attach);
struct ListHead commands = STAILQ_HEAD_INITIALIZER(commands);
struct ListHead queries = STAILQ_HEAD_INITIALIZER(queries);
struct ListHead alias_queries = STAILQ_HEAD_INITIALIZER(alias_queries);
int sendflags = 0;
int flags = 0;
int version = 0;
int i;
bool explicit_folder = false;
bool dump_variables = false;
bool hide_sensitive = false;
bool batch_mode = false;
bool edit_infile = false;
extern char *optarg;
extern int optind;
int double_dash = argc, nargc = 1;
/* sanity check against stupid administrators */
if (getegid() != getgid())
{
fprintf(stderr, "%s: I don't want to run with privileges!\n", argv[0]);
exit(1);
}
setlocale(LC_ALL, "");
#ifdef ENABLE_NLS
/* FIXME what about the LOCALES_HACK in mutt_init() [init.c] ? */
{
char *domdir = getenv("TEXTDOMAINDIR");
if (domdir && domdir[0])
bindtextdomain(PACKAGE, domdir);
else
bindtextdomain(PACKAGE, MUTTLOCALEDIR);
textdomain(PACKAGE);
}
#endif
mutt_message = mutt_error; /* send messages to stderr, too */
mutt_perror = mutt_perror_debug;
(void) mutt_rand32();
umask(077);
memset(Options, 0, sizeof(Options));
memset(QuadOptions, 0, sizeof(QuadOptions));
/* Init envlist */
{
char **srcp, **dstp;
int count = 0;
for (srcp = env; srcp && *srcp; srcp++)
count++;
envlist = safe_calloc(count + 1, sizeof(char *));
for (srcp = env, dstp = envlist; srcp && *srcp; srcp++, dstp++)
*dstp = safe_strdup(*srcp);
}
for (optind = 1; optind < double_dash;)
{
/* We're getopt'ing POSIXLY, so we'll be here every time getopt()
* encounters a non-option. That could be a file to attach
* (all non-options between -a and --) or it could be an address
* (which gets collapsed to the front of argv).
*/
for (; optind < argc; optind++)
{
if (argv[optind][0] == '-' && argv[optind][1] != '\0')
{
if (argv[optind][1] == '-' && argv[optind][2] == '\0')
double_dash = optind; /* quit outer loop after getopt */
break; /* drop through to getopt */
}
/* non-option, either an attachment or address */
if (!STAILQ_EMPTY(&attach))
mutt_list_insert_tail(&attach, safe_strdup(argv[optind]));
else
argv[nargc++] = argv[optind];
}
/* USE_NNTP 'g:G' */
if ((i = getopt(argc, argv,
"+A:a:Bb:F:f:c:Dd:l:Ee:g:GH:s:i:hm:npQ:RSvxyzZ")) != EOF)
switch (i)
{
case 'A':
mutt_list_insert_tail(&alias_queries, safe_strdup(optarg));
break;
case 'a':
mutt_list_insert_tail(&attach, safe_strdup(optarg));
break;
case 'F':
/* mutt_str_replace (&Muttrc, optarg); */
mutt_list_insert_tail(&Muttrc, safe_strdup(optarg));
break;
case 'f':
strfcpy(folder, optarg, sizeof(folder));
explicit_folder = true;
break;
case 'b':
case 'c':
if (!msg)
msg = mutt_new_header();
if (!msg->env)
msg->env = mutt_new_envelope();
if (i == 'b')
msg->env->bcc = rfc822_parse_adrlist(msg->env->bcc, optarg);
else
msg->env->cc = rfc822_parse_adrlist(msg->env->cc, optarg);
break;
case 'D':
dump_variables = true;
break;
case 'B':
batch_mode = true;
break;
case 'd':
#ifdef DEBUG
if (mutt_atoi(optarg, &debuglevel_cmdline) < 0 || debuglevel_cmdline <= 0)
{
fprintf(stderr, _("Error: value '%s' is invalid for -d.\n"), optarg);
return 1;
}
printf(_("Debugging at level %d.\n"), debuglevel_cmdline);
#else
printf("%s",
_("DEBUG was not defined during compilation. -d Ignored.\n"));
#endif
break;
case 'E':
edit_infile = true;
break;
case 'e':
mutt_list_insert_tail(&commands, safe_strdup(optarg));
break;
case 'H':
draftFile = optarg;
break;
case 'i':
includeFile = optarg;
break;
case 'l':
#ifdef DEBUG
debugfile_cmdline = optarg;
printf(_("Debugging at file %s.\n"), debugfile_cmdline);
#else
printf(_("DEBUG was not defined during compilation. -l Ignored.\n"));
#endif
break;
case 'm':
/* should take precedence over .muttrc setting, so save it for later */
newMagic = optarg;
break;
case 'n':
flags |= MUTT_NOSYSRC;
break;
case 'p':
sendflags |= SENDPOSTPONED;
break;
case 'Q':
mutt_list_insert_tail(&queries, safe_strdup(optarg));
break;
case 'R':
flags |= MUTT_RO; /* read-only mode */
break;
case 'S':
hide_sensitive = true;
break;
case 's':
subject = optarg;
break;
case 'v':
version++;
break;
case 'x': /* mailx compatible send mode */
sendflags |= SENDMAILX;
break;
case 'y': /* My special hack mode */
flags |= MUTT_SELECT;
break;
#ifdef USE_NNTP
case 'g': /* Specify a news server */
{
char buf[LONG_STRING];
snprintf(buf, sizeof(buf), "set news_server=%s", optarg);
mutt_list_insert_tail(&commands, safe_strdup(buf));
}
case 'G': /* List of newsgroups */
flags |= MUTT_SELECT | MUTT_NEWS;
break;
#endif
case 'z':
flags |= MUTT_IGNORE;
break;
case 'Z':
flags |= MUTT_BUFFY | MUTT_IGNORE;
break;
default:
usage();
}
}
/* collapse remaining argv */
while (optind < argc)
argv[nargc++] = argv[optind++];
optind = 1;
argc = nargc;
switch (version)
{
case 0:
break;
case 1:
print_version();
exit(0);
default:
print_copyright();
exit(0);
}
/* Check for a batch send. */
if (!isatty(0) || !STAILQ_EMPTY(&queries) || !STAILQ_EMPTY(&alias_queries) ||
dump_variables || batch_mode)
{
set_option(OPT_NO_CURSES);
sendflags = SENDBATCH;
}
/* Always create the mutt_windows because batch mode has some shared code
* paths that end up referencing them. */
mutt_init_windows();
/* This must come before mutt_init() because curses needs to be started
before calling the init_pair() function to set the color scheme. */
if (!option(OPT_NO_CURSES))
{
start_curses();
/* check whether terminal status is supported (must follow curses init) */
TSSupported = mutt_ts_capability();
}
/* set defaults and read init files */
mutt_init(flags & MUTT_NOSYSRC, &commands);
mutt_list_free(&commands);
/* Initialize crypto backends. */
crypt_init();
if (newMagic)
mx_set_magic(newMagic);
if (!STAILQ_EMPTY(&queries))
{
for (; optind < argc; optind++)
mutt_list_insert_tail(&queries, safe_strdup(argv[optind]));
return mutt_query_variables(&queries);
}
if (dump_variables)
return mutt_dump_variables(hide_sensitive);
if (!STAILQ_EMPTY(&alias_queries))
{
int rv = 0;
struct Address *a = NULL;
for (; optind < argc; optind++)
mutt_list_insert_tail(&alias_queries, safe_strdup(argv[optind]));
struct ListNode *np;
STAILQ_FOREACH(np, &alias_queries, entries)
{
if ((a = mutt_lookup_alias(np->data)))
{
/* output in machine-readable form */
mutt_addrlist_to_intl(a, NULL);
mutt_write_address_list(a, stdout, 0, 0);
}
else
{
rv = 1;
printf("%s\n", np->data);
}
}
mutt_list_free(&alias_queries);
return rv;
}
if (!option(OPT_NO_CURSES))
{
SETCOLOR(MT_COLOR_NORMAL);
clear();
mutt_error = mutt_curses_error;
mutt_message = mutt_curses_message;
}
/* Create the Maildir directory if it doesn't exist. */
if (!option(OPT_NO_CURSES) && Maildir)
{
struct stat sb;
char fpath[_POSIX_PATH_MAX];
char msg2[STRING];
strfcpy(fpath, Maildir, sizeof(fpath));
mutt_expand_path(fpath, sizeof(fpath));
bool skip = false;
#ifdef USE_IMAP
/* we're not connected yet - skip mail folder creation */
skip |= mx_is_imap(fpath);
#endif
#ifdef USE_NNTP
skip |= mx_is_nntp(fpath);
#endif
if (!skip && stat(fpath, &sb) == -1 && errno == ENOENT)
{
snprintf(msg2, sizeof(msg2), _("%s does not exist. Create it?"), Maildir);
if (mutt_yesorno(msg2, MUTT_YES) == MUTT_YES)
{
if (mkdir(fpath, 0700) == -1 && errno != EEXIST)
mutt_error(_("Can't create %s: %s."), Maildir, strerror(errno));
}
}
}
if (batch_mode)
exit(0);
if (sendflags & SENDPOSTPONED)
{
if (!option(OPT_NO_CURSES))
mutt_flushinp();
ci_send_message(SENDPOSTPONED, NULL, NULL, NULL, NULL);
mutt_free_windows();
mutt_endwin(NULL);
}
else if (subject || msg || sendflags || draftFile || includeFile ||
!STAILQ_EMPTY(&attach) || optind < argc)
{
FILE *fin = NULL;
FILE *fout = NULL;
char buf[LONG_STRING];
char *tempfile = NULL, *infile = NULL;
char *bodytext = NULL, *bodyfile = NULL;
int rv = 0;
char expanded_infile[_POSIX_PATH_MAX];
if (!option(OPT_NO_CURSES))
mutt_flushinp();
if (!msg)
msg = mutt_new_header();
if (!msg->env)
msg->env = mutt_new_envelope();
for (i = optind; i < argc; i++)
{
if (url_check_scheme(argv[i]) == U_MAILTO)
{
if (url_parse_mailto(msg->env, &bodytext, argv[i]) < 0)
{
if (!option(OPT_NO_CURSES))
mutt_endwin(NULL);
fputs(_("Failed to parse mailto: link\n"), stderr);
exit(1);
}
}
else
msg->env->to = rfc822_parse_adrlist(msg->env->to, argv[i]);
}
if (!draftFile && option(OPT_AUTO_EDIT) && !msg->env->to && !msg->env->cc)
{
if (!option(OPT_NO_CURSES))
mutt_endwin(NULL);
fputs(_("No recipients specified.\n"), stderr);
exit(1);
}
if (subject)
msg->env->subject = safe_strdup(subject);
if (draftFile)
{
infile = draftFile;
includeFile = NULL;
}
else if (includeFile)
infile = includeFile;
else
edit_infile = false;
if (infile || bodytext)
{
/* Prepare fin and expanded_infile. */
if (infile)
{
if (mutt_strcmp("-", infile) == 0)
{
if (edit_infile)
{
fputs(_("Cannot use -E flag with stdin\n"), stderr);
exit(1);
}
fin = stdin;
}
else
{
strfcpy(expanded_infile, infile, sizeof(expanded_infile));
mutt_expand_path(expanded_infile, sizeof(expanded_infile));
if ((fin = fopen(expanded_infile, "r")) == NULL)
{
if (!option(OPT_NO_CURSES))
mutt_endwin(NULL);
perror(expanded_infile);
exit(1);
}
}
}
/* Copy input to a tempfile, and re-point fin to the tempfile.
* Note: stdin is always copied to a tempfile, ensuring draftFile
* can stat and get the correct st_size below.
*/
if (!edit_infile)
{
mutt_mktemp(buf, sizeof(buf));
tempfile = safe_strdup(buf);
if ((fout = safe_fopen(tempfile, "w")) == NULL)
{
if (!option(OPT_NO_CURSES))
mutt_endwin(NULL);
perror(tempfile);
safe_fclose(&fin);
FREE(&tempfile);
exit(1);
}
if (fin)
{
mutt_copy_stream(fin, fout);
if (fin != stdin)
safe_fclose(&fin);
}
else if (bodytext)
fputs(bodytext, fout);
safe_fclose(&fout);
if ((fin = fopen(tempfile, "r")) == NULL)
{
if (!option(OPT_NO_CURSES))
mutt_endwin(NULL);
perror(tempfile);
FREE(&tempfile);
exit(1);
}
}
/* If editing the infile, keep it around afterwards so
* it doesn't get unlinked, and we can rebuild the draftFile
*/
else
sendflags |= SENDNOFREEHEADER;
/* Parse the draftFile into the full Header/Body structure.
* Set SENDDRAFTFILE so ci_send_message doesn't overwrite
* our msg->content.
*/
if (draftFile)
{
struct Header *context_hdr = NULL;
struct Envelope *opts_env = msg->env;
struct stat st;
sendflags |= SENDDRAFTFILE;
/* Set up a "context" header with just enough information so that
* mutt_prepare_template() can parse the message in fin.
*/
context_hdr = mutt_new_header();
context_hdr->offset = 0;
context_hdr->content = mutt_new_body();
if (fstat(fileno(fin), &st))
{
perror(draftFile);
exit(1);
}
context_hdr->content->length = st.st_size;
mutt_prepare_template(fin, NULL, msg, context_hdr, 0);
/* Scan for mutt header to set OPT_RESUME_DRAFT_FILES */
struct ListNode *np, *tmp;
STAILQ_FOREACH_SAFE(np, &msg->env->userhdrs, entries, tmp)
{
if (mutt_strncasecmp("X-Mutt-Resume-Draft:", np->data, 20) == 0)
{
if (option(OPT_RESUME_EDITED_DRAFT_FILES))
set_option(OPT_RESUME_DRAFT_FILES);
STAILQ_REMOVE(&msg->env->userhdrs, np, ListNode, entries);
FREE(&np->data);
FREE(&np);
}
}
rfc822_append(&msg->env->to, opts_env->to, 0);
rfc822_append(&msg->env->cc, opts_env->cc, 0);
rfc822_append(&msg->env->bcc, opts_env->bcc, 0);
if (opts_env->subject)
mutt_str_replace(&msg->env->subject, opts_env->subject);
mutt_free_envelope(&opts_env);
mutt_free_header(&context_hdr);
}
/* Editing the includeFile: pass it directly in.
* Note that SENDNOFREEHEADER is set above so it isn't unlinked.
*/
else if (edit_infile)
bodyfile = expanded_infile;
/* For bodytext and unedited includeFile: use the tempfile.
*/
else
bodyfile = tempfile;
if (fin)
safe_fclose(&fin);
}
FREE(&bodytext);
if (!STAILQ_EMPTY(&attach))
{
struct Body *a = msg->content;
while (a && a->next)
a = a->next;
struct ListNode *np;
STAILQ_FOREACH(np, &attach, entries)
{
if (a)
{
a->next = mutt_make_file_attach(np->data);
a = a->next;
}
else
msg->content = a = mutt_make_file_attach(np->data);
if (!a)
{
if (!option(OPT_NO_CURSES))
mutt_endwin(NULL);
fprintf(stderr, _("%s: unable to attach file.\n"), np->data);
mutt_list_free(&attach);
exit(1);
}
}
mutt_list_free(&attach);
}
rv = ci_send_message(sendflags, msg, bodyfile, NULL, NULL);
if (edit_infile)
{
if (includeFile)
msg->content->unlink = false;
else if (draftFile)
{
if (truncate(expanded_infile, 0) == -1)
{
if (!option(OPT_NO_CURSES))
mutt_endwin(NULL);
perror(expanded_infile);
exit(1);
}
if ((fout = safe_fopen(expanded_infile, "a")) == NULL)
{
if (!option(OPT_NO_CURSES))
mutt_endwin(NULL);
perror(expanded_infile);
exit(1);
}
/* If the message was sent or postponed, these will already
* have been done.
*/
if (rv < 0)
{
if (msg->content->next)
msg->content = mutt_make_multipart(msg->content);
mutt_encode_descriptions(msg->content, 1);
mutt_prepare_envelope(msg->env, 0);
mutt_env_to_intl(msg->env, NULL, NULL);
}
mutt_write_rfc822_header(fout, msg->env, msg->content, -1, 0);
if (option(OPT_RESUME_EDITED_DRAFT_FILES))
fprintf(fout, "X-Mutt-Resume-Draft: 1\n");
fputc('\n', fout);
if ((mutt_write_mime_body(msg->content, fout) == -1))
{
if (!option(OPT_NO_CURSES))
mutt_endwin(NULL);
safe_fclose(&fout);
exit(1);
}
safe_fclose(&fout);
}
mutt_free_header(&msg);
}
/* !edit_infile && draftFile will leave the tempfile around */
if (tempfile)
{
unlink(tempfile);
FREE(&tempfile);
}
mutt_free_windows();
if (!option(OPT_NO_CURSES))
mutt_endwin(NULL);
if (rv)
exit(1);
}
else
{
if (flags & MUTT_BUFFY)
{
if (!mutt_buffy_check(false))
{
mutt_endwin(_("No mailbox with new mail."));
exit(1);
}
folder[0] = 0;
mutt_buffy(folder, sizeof(folder));
}
else if (flags & MUTT_SELECT)
{
#ifdef USE_NNTP
if (flags & MUTT_NEWS)
{
set_option(OPT_NEWS);
if (!(CurrentNewsSrv = nntp_select_server(NewsServer, 0)))
{
mutt_endwin(ErrorBuf);
exit(1);
}
}
else
#endif
if (!Incoming)
{
mutt_endwin(_("No incoming mailboxes defined."));
exit(1);
}
folder[0] = 0;
mutt_select_file(folder, sizeof(folder), MUTT_SEL_FOLDER | MUTT_SEL_BUFFY);
if (!folder[0])
{
mutt_endwin(NULL);
exit(0);
}
}
if (!folder[0])
{
if (SpoolFile)
strfcpy(folder, NONULL(SpoolFile), sizeof(folder));
else if (Maildir)
strfcpy(folder, NONULL(Maildir), sizeof(folder));
/* else no folder */
}
#ifdef USE_NNTP
if (option(OPT_NEWS))
{
unset_option(OPT_NEWS);
nntp_expand_path(folder, sizeof(folder), &CurrentNewsSrv->conn->account);
}
else
#endif
mutt_expand_path(folder, sizeof(folder));
mutt_str_replace(&CurrentFolder, folder);
mutt_str_replace(&LastFolder, folder);
if (flags & MUTT_IGNORE)
{
/* check to see if there are any messages in the folder */
switch (mx_check_empty(folder))
{
case -1:
mutt_endwin(strerror(errno));
exit(1);
case 1:
mutt_endwin(_("Mailbox is empty."));
exit(1);
}
}
mutt_folder_hook(folder);
mutt_startup_shutdown_hook(MUTT_STARTUPHOOK);
if ((Context = mx_open_mailbox(
folder, ((flags & MUTT_RO) || option(OPT_READONLY)) ? MUTT_READONLY : 0, NULL)) ||
!explicit_folder)
{
#ifdef USE_SIDEBAR
mutt_sb_set_open_buffy();
#endif
mutt_index_menu();
if (Context)
FREE(&Context);
}
#ifdef USE_IMAP
imap_logout_all();
#endif
#ifdef USE_SASL
mutt_sasl_done();
#endif
mutt_free_opts();
mutt_free_windows();
mutt_endwin(ErrorBuf);
}
exit(0);
}