4a735efc0656ee30fa93ed97ce8df51deed89842
[apps/madmutt.git] / main.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 1999-2002 Thomas Roessler <roessler@does-not-exist.org>
5  * Copyright (C) 2004 g10 Code GmbH
6  *
7  * Parts written/modified by:
8  * Nico Golde <nion@muttng.org>
9  * Andreas Krennmair <ak@synflood.at>
10  *
11  * This file is part of mutt-ng, see http://www.muttng.org/.
12  * It's licensed under the GNU General Public License,
13  * please see the file GPL in the top level source directory.
14  */
15
16 #define MAIN_C 1
17
18 #if HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <locale.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <sys/stat.h>
28 #include <sys/utsname.h>
29
30 #include <lib-lib/mem.h>
31 #include <lib-lib/str.h>
32 #include <lib-lib/macros.h>
33 #include <lib-lib/file.h>
34 #include <lib-lib/debug.h>
35
36 #include <lib-sys/mutt_signal.h>
37
38 #include <lib-ui/curses.h>
39
40 #include "mutt.h"
41 #include "mx.h"
42 #include "buffy.h"
43 #include "sort.h"
44 #include <lib-crypt/crypt.h>
45 #include "keymap.h"
46 #include "url.h"
47 #include "mutt_idna.h"
48 #include "xterm.h"
49
50 #ifdef HAVE_GETOPT_H
51 #include <getopt.h>
52 #else
53 extern char *optarg;
54 extern int optind;
55 #endif
56
57 #ifdef HAVE_LIBIDN
58 #include <stringprep.h>
59 #endif
60
61 #ifdef USE_NNTP
62 #include "nntp/nntp.h"
63 #endif
64
65 #ifdef USE_LIBESMTP
66 #include <libesmtp.h>
67 #endif
68
69 #ifdef USE_HCACHE
70 #if HAVE_QDBM
71 #include <depot.h>
72 #elif HAVE_GDBM
73 #include <gdbm.h>
74 #elif HAVE_DB4
75 #include <db.h>
76 #endif
77 #endif
78
79 #ifdef USE_GNUTLS
80 #include <gnutls/gnutls.h>
81 #endif
82
83 #ifdef CRYPT_BACKEND_GPGME
84 #include <gpgme.h>
85 #endif
86
87 #ifdef USE_SSL
88 #include <openssl/opensslv.h>
89 #endif
90
91 static const char *Notice = N_("\
92 Copyright (C) 1996-2002 Michael R. Elkins and others.\n\
93 Madmutt comes with ABSOLUTELY NO WARRANTY; for details type `madmutt -vv'.\n\
94 Mutt is free software, and you are welcome to redistribute it\n\
95 under certain conditions; type `madmutt -vv' for details.\n");
96
97 static const char *Copyright_Mutt = N_("\
98 Copyright (C) 1996-2004 Michael R. Elkins <me@mutt.org>\n\
99 Copyright (C) 1996-2002 Brandon Long <blong@fiction.net>\n\
100 Copyright (C) 1997-2005 Thomas Roessler <roessler@does-not-exist.org>\n\
101 Copyright (C) 1998-2005 Werner Koch <wk@isil.d.shuttle.de>\n\
102 Copyright (C) 1999-2005 Brendan Cully <brendan@kublai.com>\n\
103 Copyright (C) 1999-2002 Tommi Komulainen <Tommi.Komulainen@iki.fi>\n\
104 Copyright (C) 2000-2002 Edmund Grimley Evans <edmundo@rano.org>\n");
105
106 static const char *Copyright_Mutt_NG = N_("\
107 Copyright (C) 2005:\n\
108   Parts were written/modified by:\n\
109      Andreas Krennmair <ak@synflood.at>\n\
110      Nico Golde <nico@ngolde.de>\n\
111      Rocco Rutte <pdmef@cs.tu-berlin.de>\n\
112 \n\
113 Lots of others not mentioned here contributed lots of code,\n\
114 fixes, and suggestions.");
115
116 static const char* Copyright_GPL_1 = N_("\
117 \n\
118     This program is free software; you can redistribute it and/or modify\n\
119     it under the terms of the GNU General Public License as published by\n\
120     the Free Software Foundation; either version 2 of the License, or\n\
121     (at your option) any later version.\n");
122
123 static const char* Copyright_GPL_2 = N_("\
124     This program is distributed in the hope that it will be useful,\n\
125     but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
126     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
127     GNU General Public License for more details.\n");
128
129 static const char* Copyright_GPL_3 = N_("\
130     You should have received a copy of the GNU General Public License\n\
131     along with this program; if not, write to the Free Software\n\
132     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
133 ");
134
135 void mutt_exit (int code)
136 {
137   mutt_endwin (NULL);
138   exit (code);
139 }
140
141 static void mutt_usage (void)
142 {
143   puts (mutt_make_version (1));
144
145   puts
146     _("usage: madmutt [ -nRyzZ ] [ -e <cmd> ] [ -F <file> ] [ -m <type> ] [ -f <file> ]\n\
147        madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -Q <query> [ -Q <query> ] [...]\n\
148        madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -A <alias> [ -A <alias> ] [...]\n\
149        madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -t");
150   puts
151     _("\
152        madmutt [ -nR ] [ -e <cmd> ] [ -F <file> ] -T\n\
153        madmutt [ -nx ] [ -e <cmd> ] [ -a <file> ] [ -F <file> ] [ -H <file> ] [ -i <file> ] [ -s <subj> ] [ -b <addr> ] [ -c <addr> ] <addr> [ ... ]\n\
154        madmutt [ -n ] [ -e <cmd> ] [ -F <file> ] -p\n\
155        madmutt -v[v]\n\
156 \n\
157 options:\n\
158   -A <alias>\texpand the given alias\n\
159   -a <file>\tattach a file to the message\n\
160   -b <address>\tspecify a blind carbon-copy (BCC) address\n\
161   -c <address>\tspecify a carbon-copy (CC) address");
162 #if DEBUG
163   puts _("  -d <level>\tlog debugging output to ~/.madmuttdebug0");
164 #endif
165   puts _("\
166   -e <command>\tspecify a command to be executed after initialization\n\
167   -f <file>\tspecify which mailbox to read\n\
168   -F <file>\tspecify an alternate muttrngc file\n\
169   -g <server>\tspecify a newsserver (if compiled with NNTP)\n\
170   -G\t\tselect a newsgroup (if compiled with NNTP)\n\
171   -H <file>\tspecify a draft file to read header and body from\n\
172   -i <file>\tspecify a file which Madmutt should include in the body");
173   puts _("\
174   -d <level>\t specify debugging level of Madmutt\n\
175   -m <type>\tspecify a default mailbox type\n\
176   -n\t\tcauses Madmutt not to read the system Madmuttrc\n\
177   -p\t\trecall a postponed message\n\
178   -Q <variable>\tquery a configuration variable\n\
179   -R\t\topen mailbox in read-only mode\n\
180   -s <subj>\tspecify a subject (must be in quotes if it has spaces)");
181   puts _("\
182   -t\t\tprint the value of all variables to stdout\n\
183   -T\t\tprint the value of all changed variables to stdout\n\
184   -v\t\tshow version and compile-time definitions\n\
185   -y\t\tselect a mailbox specified in your `mailboxes' list\n\
186   -z\t\texit immediately if there are no messages in the mailbox\n\
187   -Z\t\topen the first folder with new message, exit immediately if none\n\
188   -h\t\tthis help message");
189
190   exit (0);
191 }
192
193 static void show_version (void)
194 {
195   struct utsname uts;
196 #ifdef USE_LIBESMTP
197   char buf[STRING];
198 #endif
199
200   puts (mutt_make_version (1));
201   puts (_(Notice));
202
203   uname (&uts);
204
205 #ifdef _AIX
206   printf ("System:\n  %s %s.%s", uts.sysname, uts.version, uts.release);
207 #elif defined (SCO)
208   printf ("System:\n  SCO %s", uts.release);
209 #else
210   printf ("System:\n  %s %s", uts.sysname, uts.release);
211 #endif
212
213   printf (" (%s)\nExternal Libraries:\n", uts.machine);
214
215 #ifdef NCURSES_VERSION
216   printf ("  ncurses %s\n", NCURSES_VERSION);
217 #elif defined(USE_SLANG_CURSES)
218   printf ("  slang %d\n", SLANG_VERSION);
219 #endif
220
221 #ifdef _LIBICONV_VERSION
222   printf ("  libiconv %d.%d\n", _LIBICONV_VERSION >> 8,
223           _LIBICONV_VERSION & 0xff);
224 #endif
225
226 #ifdef HAVE_LIBIDN
227   printf ("  libidn %s (compiled with %s)\n",
228           stringprep_check_version (NULL), STRINGPREP_VERSION);
229 #endif
230
231 #ifdef USE_LIBESMTP
232   smtp_version (buf, sizeof (buf), 0);
233   printf ("  libESMTP %s\n", buf);
234 #endif
235
236 #ifdef USE_HCACHE
237 #if HAVE_QDBM
238   printf ("  qdbm %s\n", dpversion);
239 #elif HAVE_GDBM
240   printf ("  gdbm %s\n", gdbm_version);
241 #elif HAVE_DB4
242   printf ("  DB4 %d.%d.%d\n", DB_VERSION_MAJOR, DB_VERSION_MINOR,
243           DB_VERSION_PATCH);
244 #endif
245 #endif
246
247 #ifdef USE_GNUTLS
248   printf ("  gnutls %s\n", LIBGNUTLS_VERSION);
249 #endif
250
251 #ifdef USE_SSL
252   printf ("  %s\n",OPENSSL_VERSION_TEXT);
253 #endif
254
255 #ifdef CRYPT_BACKEND_GPGME
256   printf ("  gpgme %s\n", GPGME_VERSION);
257 #endif
258
259   puts (_("Compile Options:"));
260
261 #ifdef DEBUG
262   puts ("  +DEBUG");
263 #else
264   puts ("  -DEBUG");
265 #endif
266
267   puts (
268 #ifdef HOMESPOOL
269          "  +HOMESPOOL  "
270 #else
271          "  -HOMESPOOL  "
272 #endif
273 #ifdef USE_SETGID
274          "+USE_SETGID  "
275 #else
276          "-USE_SETGID  "
277 #endif
278 #ifdef USE_DOTLOCK
279          "+USE_DOTLOCK  "
280 #else
281          "-USE_DOTLOCK  "
282 #endif
283 #ifdef USE_FCNTL
284          "+USE_FCNTL  "
285 #else
286          "-USE_FCNTL  "
287 #endif
288 #ifdef USE_FLOCK
289          "+USE_FLOCK   "
290 #else
291          "-USE_FLOCK   "
292 #endif
293 #ifdef USE_HCACHE
294          "+USE_HCACHE  "
295 #else
296          "-USE_HCACHE  "
297 #endif
298     );
299   puts (
300 #ifdef USE_NNTP
301          "+USE_NNTP  "
302 #else
303          "-USE_NNTP  "
304 #endif
305 #ifdef USE_GSS
306          "+USE_GSS  "
307 #else
308          "-USE_GSS  "
309 #endif
310 #ifdef USE_SSL
311          "+USE_SSL  "
312 #else
313          "-USE_SSL  "
314 #endif
315 #ifdef USE_GNUTLS
316          "+USE_GNUTLS  "
317 #else
318          "-USE_GNUTLS  "
319 #endif
320 #ifdef USE_SASL
321          "+USE_SASL  "
322 #else
323          "-USE_SASL  "
324 #endif
325 #ifdef USE_LIBESMTP
326          "+USE_LIBESMTP  "
327 #else
328          "-USE_LIBESMTP  "
329 #endif
330          "\n  "
331 #ifdef HAVE_REGCOMP
332          "+HAVE_REGCOMP  "
333 #else
334          "-HAVE_REGCOMP  "
335 #endif
336          "\n  "
337 #ifdef HAVE_COLOR
338          "+HAVE_COLOR  "
339 #else
340          "-HAVE_COLOR  "
341 #endif
342 #ifdef HAVE_START_COLOR
343          "+HAVE_START_COLOR  "
344 #else
345          "-HAVE_START_COLOR  "
346 #endif
347 #ifdef HAVE_TYPEAHEAD
348          "+HAVE_TYPEAHEAD  "
349 #else
350          "-HAVE_TYPEAHEAD  "
351 #endif
352 #ifdef HAVE_BKGDSET
353          "+HAVE_BKGDSET  "
354 #else
355          "-HAVE_BKGDSET  "
356 #endif
357          "\n  "
358 #ifdef HAVE_CURS_SET
359          "+HAVE_CURS_SET  "
360 #else
361          "-HAVE_CURS_SET  "
362 #endif
363 #ifdef HAVE_META
364          "+HAVE_META  "
365 #else
366          "-HAVE_META  "
367 #endif
368 #ifdef HAVE_RESIZETERM
369          "+HAVE_RESIZETERM  "
370 #else
371          "-HAVE_RESIZETERM  "
372 #endif
373     );
374
375   puts (
376 #ifdef CRYPT_BACKEND_GPGME
377          "+CRYPT_BACKEND_GPGME  "
378 #else
379          "-CRYPT_BACKEND_GPGME  "
380 #endif
381 #ifdef BUFFY_SIZE
382          "+BUFFY_SIZE "
383 #else
384          "-BUFFY_SIZE "
385 #endif
386          "\n  "
387 #ifdef ENABLE_NLS
388          "+ENABLE_NLS  "
389 #else
390          "-ENABLE_NLS  "
391 #endif
392 #ifdef HAVE_WC_FUNCS
393          "+HAVE_WC_FUNCS  "
394 #else
395          "-HAVE_WC_FUNCS  "
396 #endif
397 #ifdef HAVE_LANGINFO_CODESET
398          "+HAVE_LANGINFO_CODESET  "
399 #else
400          "-HAVE_LANGINFO_CODESET  "
401 #endif
402 #ifdef HAVE_LANGINFO_YESEXPR
403          "+HAVE_LANGINFO_YESEXPR  "
404 #else
405          "-HAVE_LANGINFO_YESEXPR  "
406 #endif
407          "\n  "
408 #if HAVE_ICONV
409          "+HAVE_ICONV  "
410 #else
411          "-HAVE_ICONV  "
412 #endif
413 #if ICONV_NONTRANS
414          "+ICONV_NONTRANS  "
415 #else
416          "-ICONV_NONTRANS  "
417 #endif
418 #if HAVE_LIBIDN
419          "+HAVE_LIBIDN  "
420 #else
421          "-HAVE_LIBIDN  "
422 #endif
423 #if HAVE_GETSID
424          "+HAVE_GETSID  "
425 #else
426          "-HAVE_GETSID  "
427 #endif
428 #if HAVE_GETADDRINFO
429          "+HAVE_GETADDRINFO  "
430 #else
431          "-HAVE_GETADDRINFO  "
432 #endif
433     );
434
435   puts (_("Built-In Defaults:"));
436
437 #ifdef ISPELL
438   printf ("  +ISPELL=\"%s\"\n", ISPELL);
439 #else
440   puts ("  -ISPELL");
441 #endif
442
443   printf ("  +SENDMAIL=\"%s\"\n", SENDMAIL);
444   printf ("  +MAILPATH=\"%s\"\n", MAILPATH);
445   printf ("  +PKGDATADIR=\"%s\"\n", PKGDATADIR);
446   printf ("  +PKGDOCDIR=\"%s\"\n", PKGDOCDIR);
447   printf ("  +SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
448 #ifdef MIXMASTER
449   printf ("  +MIXMASTER=\"%s\"\n\n", MIXMASTER);
450 #else
451   puts ("  -MIXMASTER\n");
452 #endif
453
454   puts (_("MadMutt is based on Madmutt wich was based on Mutt before\n"));
455
456   exit (0);
457 }
458
459 static void start_curses (void)
460 {
461   km_init ();                   /* must come before mutt_init */
462
463 #ifdef USE_SLANG_CURSES
464   SLtt_Ignore_Beep = 1;         /* don't do that #*$@^! annoying visual beep! */
465   SLsmg_Display_Eight_Bit = 128;        /* characters above this are printable */
466   SLtt_set_color (0, NULL, "default", "default");
467 #else
468   /* should come before initscr() so that ncurses 4.2 doesn't try to install
469      its own SIGWINCH handler */
470   mutt_signal_initialize ();
471 #endif
472   if (initscr () == NULL) {
473     puts _("Error initializing terminal.");
474
475     exit (1);
476   }
477   mutt_signal_initialize ();
478   ci_start_color ();
479   keypad (stdscr, TRUE);
480   cbreak ();
481   noecho ();
482 #if HAVE_TYPEAHEAD
483   typeahead (-1);               /* simulate smooth scrolling */
484 #endif
485 #if HAVE_META
486   meta (stdscr, TRUE);
487 #endif
488 }
489
490 #define M_IGNORE  (1<<0)        /* -z */
491 #define M_BUFFY   (1<<1)        /* -Z */
492 #define M_NOSYSRC (1<<2)        /* -n */
493 #define M_RO      (1<<3)        /* -R */
494 #define M_SELECT  (1<<4)        /* -y */
495 #ifdef USE_NNTP
496 #define M_NEWS    (1<<5)        /* -g and -G */
497 #endif
498
499 int main (int argc, char **argv)
500 {
501   char folder[_POSIX_PATH_MAX] = "";
502   char *subject = NULL;
503   char *includeFile = NULL;
504   char *draftFile = NULL;
505   char *newMagic = NULL;
506   HEADER *msg = NULL;
507   LIST *attach = NULL;
508   LIST *commands = NULL;
509   LIST *queries = NULL;
510   LIST *alias_queries = NULL;
511   int sendflags = 0;
512   int flags = 0;
513   int version = 0;
514   int i;
515   int explicit_folder = 0;
516   int dump_variables = -1;
517
518   /* initialize random number for tmp file creation */ 
519   srand ((unsigned int) time (NULL));
520   
521   /* sanity check against stupid administrators */
522   
523   if (getegid () != getgid ()) {
524     fprintf (stderr, "%s: I don't want to run with privileges!\n", argv[0]);
525     exit (1);
526   }
527
528 #ifdef ENABLE_NLS
529   /* FIXME what about init.c:1439 ? */
530   setlocale (LC_ALL, "");
531   bindtextdomain (PACKAGE, MUTTLOCALEDIR);
532   textdomain (PACKAGE);
533 #endif
534
535   setlocale (LC_CTYPE, "");
536
537   mutt_error = mutt_nocurses_error;
538   mutt_message = mutt_nocurses_error;
539   SRAND (time (NULL));
540   umask (077);
541
542   p_clear(Options, countof(Options));
543   p_clear(QuadOptions, countof(QuadOptions));
544
545 #ifdef USE_NNTP
546   while ((i =
547           getopt (argc, argv,
548                   "A:a:b:F:f:c:d:e:g:GH:s:i:hm:npQ:RTtvyzZ")) != EOF)
549 #else
550   while ((i =
551           getopt (argc, argv, "A:a:b:F:f:c:d:e:H:s:i:hm:npQ:RTtvyzZ")) != EOF)
552 #endif
553     switch (i) {
554     case 'A':
555       alias_queries = mutt_add_list (alias_queries, optarg);
556       break;
557     case 'a':
558       if (strlen(optarg)<=512)
559         attach = mutt_add_list (attach, optarg);
560       else{
561         printf("too long arguments. exiting ...\n");
562         exit(1);
563       }
564       break;
565
566     case 'F':
567       m_strreplace(&Muttrc, optarg);
568       break;
569
570     case 'f':
571       m_strcpy(folder, sizeof(folder), optarg);
572       explicit_folder = 1;
573       break;
574
575     case 'b':
576     case 'c':
577       if (!msg)
578         msg = header_new();
579       if (!msg->env)
580         msg->env = envelope_new();
581       if (i == 'b')
582         msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
583       else
584         msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
585       break;
586
587     case 'd':
588       {
589         char buf[LONG_STRING];
590         int level = atoi (optarg);
591
592         snprintf (buf, sizeof (buf), "set debug_level=%d", level);
593         commands = mutt_add_list (commands, buf);
594         debug_setlevel (level);
595       }
596       break;
597
598     case 't':
599       dump_variables = 2;
600       break;
601
602     case 'T':
603       dump_variables = 1;
604       break;
605
606     case 'e':
607       commands = mutt_add_list (commands, optarg);
608       break;
609
610     case 'H':
611       draftFile = optarg;
612       break;
613
614     case 'i':
615       includeFile = optarg;
616       break;
617
618     case 'm':
619       /* should take precedence over .muttrc setting, so save it for later */
620       newMagic = optarg;
621       break;
622
623     case 'n':
624       flags |= M_NOSYSRC;
625       break;
626
627     case 'p':
628       sendflags |= SENDPOSTPONED;
629       break;
630
631     case 'Q':
632       queries = mutt_add_list (queries, optarg);
633       break;
634
635     case 'R':
636       flags |= M_RO;            /* read-only mode */
637       break;
638
639     case 's':
640       subject = optarg;
641       break;
642
643     case 'v':
644       version++;
645       break;
646
647     case 'y':                  /* My special hack mode */
648       flags |= M_SELECT;
649       break;
650
651 #ifdef USE_NNTP
652     case 'g':                  /* Specify a newsserver */
653       {
654         char buf[LONG_STRING];
655
656         snprintf (buf, sizeof (buf), "set nntp_host=%s", optarg);
657         commands = mutt_add_list (commands, buf);
658       }
659
660     case 'G':                  /* List of newsgroups */
661       flags |= M_SELECT | M_NEWS;
662       break;
663 #endif
664
665     case 'z':
666       flags |= M_IGNORE;
667       break;
668
669     case 'Z':
670       flags |= M_BUFFY | M_IGNORE;
671       break;
672
673     default:
674       mutt_usage ();
675     }
676
677   switch (version) {
678   case 0:
679     break;
680   case 1:
681     show_version ();
682     break;
683   default:
684     puts (mutt_make_version (1));
685     puts (_(Copyright_Mutt));
686     puts (_(Copyright_Mutt_NG));
687     puts (_(Copyright_GPL_1));
688     puts (_(Copyright_GPL_2));
689     puts (_(Copyright_GPL_3));
690     exit (0);
691   }
692
693   /* Check for a batch send. */
694   if (!isatty (0) || queries || alias_queries || dump_variables > 0) {
695     set_option (OPTNOCURSES);
696     sendflags = SENDBATCH;
697   }
698
699   /* This must come before mutt_init() because curses needs to be started
700      before calling the init_pair() function to set the color scheme.  */
701   if (!option (OPTNOCURSES))
702     start_curses ();
703
704   /* set defaults and read init files */
705   mx_init ();
706   mutt_init (flags & M_NOSYSRC, commands);
707   mutt_free_list (&commands);
708
709   /* Initialize crypto */
710   crypt_init ();
711
712   if (queries)
713     return mutt_query_variables (queries);
714   if (dump_variables > 0)
715     return (mutt_dump_variables (dump_variables-1));
716
717   if (alias_queries) {
718     int rv = 0;
719     address_t *a;
720
721     for (; alias_queries; alias_queries = alias_queries->next) {
722       if ((a = mutt_lookup_alias (alias_queries->data))) {
723         /* output in machine-readable form */
724         mutt_addrlist_to_idna (a, NULL);
725         mutt_write_address_list (a, stdout, 0, 0);
726       }
727       else {
728         rv = 1;
729         printf ("%s\n", alias_queries->data);
730       }
731     }
732     return rv;
733   }
734
735   if (newMagic)
736     mx_set_magic (newMagic);
737
738   if (!option (OPTNOCURSES)) {
739     SETCOLOR (MT_COLOR_NORMAL);
740     clear ();
741     mutt_error = mutt_curses_error;
742     mutt_message = mutt_curses_message;
743   }
744
745   /* Create the Maildir directory if it doesn't exist. */
746   if (!option (OPTNOCURSES) && Maildir) {
747     struct stat sb;
748     char fpath[_POSIX_PATH_MAX];
749     char mesg[STRING];
750
751     m_strcpy(fpath, sizeof(fpath), Maildir);
752     mutt_expand_path (fpath, sizeof (fpath));
753     /* we're not connected yet - skip mail folder creation */
754     if (mx_get_magic (fpath) != M_IMAP)
755       if (stat (fpath, &sb) == -1 && errno == ENOENT) {
756         snprintf (mesg, sizeof (mesg), _("%s does not exist. Create it?"),
757                   Maildir);
758         if (mutt_yesorno (mesg, M_YES) == M_YES) {
759           if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
760             mutt_error (_("Can't create %s: %s."), Maildir, strerror (errno));
761         }
762       }
763   }
764
765   if (sendflags & SENDPOSTPONED) {
766     if (!option (OPTNOCURSES))
767       mutt_flushinp ();
768     ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
769     mutt_endwin (NULL);
770   }
771   else if (subject || msg || sendflags || draftFile || includeFile || attach
772            || optind < argc) {
773     FILE *fin = NULL;
774     char buf[LONG_STRING];
775     char *tempfile = NULL, *infile = NULL;
776     char *bodytext = NULL;
777
778     if (!option (OPTNOCURSES))
779       mutt_flushinp ();
780
781     if (!msg)
782       msg = header_new();
783
784     if (draftFile)
785       infile = draftFile;
786     else {
787       if (!msg->env)
788         msg->env = envelope_new();
789
790       for (i = optind; i < argc; i++) {
791         if (url_check_scheme (argv[i]) == U_MAILTO)
792           url_parse_mailto (msg->env, &bodytext, argv[i]);
793         else
794           msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
795       }
796
797       if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc) {
798         if (!option (OPTNOCURSES))
799           mutt_endwin (NULL);
800         fputs (_("No recipients specified.\n"), stderr);
801         exit (1);
802       }
803
804       if (subject)
805         msg->env->subject = m_strdup(subject);
806
807       if (includeFile)
808         infile = includeFile;
809     }
810
811     if (infile || bodytext) {
812       if (infile) {
813         if (m_strcmp("-", infile) == 0)
814           fin = stdin;
815         else {
816           char path[_POSIX_PATH_MAX];
817
818           m_strcpy(path, sizeof(path), infile);
819           mutt_expand_path (path, sizeof (path));
820           if ((fin = fopen (path, "r")) == NULL) {
821             if (!option (OPTNOCURSES))
822               mutt_endwin (NULL);
823             perror (path);
824             exit (1);
825           }
826         }
827       }
828       else
829         fin = NULL;
830
831       mutt_mktemp (buf);
832       tempfile = m_strdup(buf);
833
834       if (draftFile)
835         msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
836
837       /* is the following if still needed? */
838
839       if (tempfile) {
840         FILE *fout;
841
842         if ((fout = safe_fopen (tempfile, "w")) == NULL) {
843           if (!option (OPTNOCURSES))
844             mutt_endwin (NULL);
845           perror (tempfile);
846           fclose (fin);
847           p_delete(&tempfile);
848           exit (1);
849         }
850         if (fin)
851           mutt_copy_stream (fin, fout);
852         else if (bodytext)
853           fputs (bodytext, fout);
854         fclose (fout);
855         if (fin && fin != stdin)
856           fclose (fin);
857       }
858     }
859
860     p_delete(&bodytext);
861
862     if (attach) {
863       LIST *t = attach;
864       BODY *a = NULL;
865
866       while (t) {
867         if (a) {
868           a->next = mutt_make_file_attach (t->data);
869           a = a->next;
870         }
871         else
872           msg->content = a = mutt_make_file_attach (t->data);
873         if (!a) {
874           if (!option (OPTNOCURSES))
875             mutt_endwin (NULL);
876           fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
877           mutt_free_list (&attach);
878           exit (1);
879         }
880         t = t->next;
881       }
882       mutt_free_list (&attach);
883     }
884
885     ci_send_message (sendflags, msg, tempfile, NULL, NULL);
886
887     if (!option (OPTNOCURSES))
888       mutt_endwin (NULL);
889   }
890   else {
891     if (flags & M_BUFFY) {
892       if (!buffy_check (0)) {
893         mutt_endwin _("No mailbox with new mail.");
894
895         exit (1);
896       }
897       folder[0] = 0;
898       buffy_next (folder, sizeof (folder));
899     }
900     else if (flags & M_SELECT) {
901 #ifdef USE_NNTP
902       if (flags & M_NEWS) {
903         set_option (OPTNEWS);
904         if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer))) {
905           mutt_endwin (Errorbuf);
906           exit (1);
907         }
908       }
909       else
910 #endif
911       if (!Incoming) {
912         mutt_endwin _("No incoming mailboxes defined.");
913
914         exit (1);
915       }
916       folder[0] = 0;
917       mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
918       if (!folder[0]) {
919         mutt_endwin (NULL);
920         exit (0);
921       }
922     }
923
924     if (!folder[0])
925       m_strcpy(folder, sizeof(folder), NONULL(Spoolfile));
926
927 #ifdef USE_NNTP
928     if (option (OPTNEWS)) {
929       unset_option (OPTNEWS);
930       nntp_expand_path (folder, sizeof (folder),
931                         &CurrentNewsSrv->conn->account);
932     }
933     else
934 #endif
935       mutt_expand_path (folder, sizeof (folder));
936
937     m_strreplace(&CurrentFolder, folder);
938     m_strreplace(&LastFolder, folder);
939
940     if (flags & M_IGNORE) {
941       /* check to see if there are any messages in the folder */
942       switch (mx_check_empty (folder)) {
943       case -1:
944         mutt_endwin (strerror (errno));
945         exit (1);
946       case 1:
947         mutt_endwin _("Mailbox is empty.");
948         exit (1);
949       }
950     }
951
952     mutt_folder_hook (folder);
953
954     if ((Context = mx_open_mailbox (folder, ((flags & M_RO)
955                                              || option (OPTREADONLY)) ?
956                                     M_READONLY : 0, NULL))
957         || !explicit_folder) {
958       mutt_index_menu ();
959       if (option (OPTXTERMSETTITLES))
960         mutt_xterm_set_title (NONULL (XtermLeave));
961       if (Context)
962         p_delete(&Context);
963     }
964     mutt_endwin (Errorbuf);
965   }
966
967   exit (0);
968 }