2 * Copyright (C) 1996-2007 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2007 Thomas Roessler <roessler@does-not-exist.org>
4 * Copyright (C) 2004 g10 Code GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include "mutt_curses.h"
32 #include "mutt_crypt.h"
33 #include "mutt_idna.h"
36 #include "mutt_sasl.h"
40 #include "imap/imap.h"
53 #include <sys/utsname.h>
60 #include <stringprep.h>
63 static const char *ReachingUs = N_("\
64 To contact the developers, please mail to <mutt-dev@mutt.org>.\n\
65 To report a bug, please visit http://bugs.mutt.org/.\n");
67 static const char *Notice = N_("\
68 Copyright (C) 1996-2009 Michael R. Elkins and others.\n\
69 Mutt comes with ABSOLUTELY NO WARRANTY; for details type `mutt -vv'.\n\
70 Mutt is free software, and you are welcome to redistribute it\n\
71 under certain conditions; type `mutt -vv' for details.\n");
73 static const char *Copyright = N_("\
74 Copyright (C) 1996-2007 Michael R. Elkins <me@mutt.org>\n\
75 Copyright (C) 1996-2002 Brandon Long <blong@fiction.net>\n\
76 Copyright (C) 1997-2008 Thomas Roessler <roessler@does-not-exist.org>\n\
77 Copyright (C) 1998-2005 Werner Koch <wk@isil.d.shuttle.de>\n\
78 Copyright (C) 1999-2009 Brendan Cully <brendan@kublai.com>\n\
79 Copyright (C) 1999-2002 Tommi Komulainen <Tommi.Komulainen@iki.fi>\n\
80 Copyright (C) 2000-2002 Edmund Grimley Evans <edmundo@rano.org>\n\
81 Copyright (C) 2006-2009 Rocco Rutte <pdmef@gmx.net>\n\
83 Many others not mentioned here contributed code, fixes,\n\
86 static const char *Licence = N_("\
87 This program is free software; you can redistribute it and/or modify\n\
88 it under the terms of the GNU General Public License as published by\n\
89 the Free Software Foundation; either version 2 of the License, or\n\
90 (at your option) any later version.\n\
92 This program is distributed in the hope that it will be useful,\n\
93 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
94 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
95 GNU General Public License for more details.\n");
96 static const char *Obtaining = N_("\
97 You should have received a copy of the GNU General Public License\n\
98 along with this program; if not, write to the Free Software\n\
99 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
102 void mutt_exit (int code)
108 static void mutt_usage (void)
110 puts (mutt_make_version ());
113 "usage: mutt [<options>] [-z] [-f <file> | -yZ]\n\
114 mutt [<options>] [-x] [-Hi <file>] [-s <subj>] [-bc <addr>] [-a <file> [...] --] <addr> [...]\n\
115 mutt [<options>] [-x] [-s <subj>] [-bc <addr>] [-a <file> [...] --] <addr> [...] < message\n\
116 mutt [<options>] -p\n\
117 mutt [<options>] -A <alias> [...]\n\
118 mutt [<options>] -Q <query> [...]\n\
119 mutt [<options>] -D\n\
124 -A <alias>\texpand the given alias\n\
125 -a <file> [...] --\tattach file(s) to the message\n\
126 \t\tthe list of files must be terminated with the \"--\" sequence\n\
127 -b <address>\tspecify a blind carbon-copy (BCC) address\n\
128 -c <address>\tspecify a carbon-copy (CC) address\n\
129 -D\t\tprint the value of all variables to stdout");
131 puts _(" -d <level>\tlog debugging output to ~/.muttdebug0");
134 " -e <command>\tspecify a command to be executed after initialization\n\
135 -f <file>\tspecify which mailbox to read\n\
136 -F <file>\tspecify an alternate muttrc file\n\
137 -H <file>\tspecify a draft file to read header and body from\n\
138 -i <file>\tspecify a file which Mutt should include in the body\n\
139 -m <type>\tspecify a default mailbox type\n\
140 -n\t\tcauses Mutt not to read the system Muttrc\n\
141 -p\t\trecall a postponed message");
144 -Q <variable>\tquery a configuration variable\n\
145 -R\t\topen mailbox in read-only mode\n\
146 -s <subj>\tspecify a subject (must be in quotes if it has spaces)\n\
147 -v\t\tshow version and compile-time definitions\n\
148 -x\t\tsimulate the mailx send mode\n\
149 -y\t\tselect a mailbox specified in your `mailboxes' list\n\
150 -z\t\texit immediately if there are no messages in the mailbox\n\
151 -Z\t\topen the first folder with new message, exit immediately if none\n\
152 -h\t\tthis help message");
157 static void show_version (void)
161 puts (mutt_make_version());
167 printf ("System: %s %s.%s", uts.sysname, uts.version, uts.release);
169 printf ("System: SCO %s", uts.release);
171 printf ("System: %s %s", uts.sysname, uts.release);
174 printf (" (%s)", uts.machine);
176 #ifdef NCURSES_VERSION
177 printf ("\nncurses: %s (compiled with %s)", curses_version(), NCURSES_VERSION);
178 #elif defined(USE_SLANG_CURSES)
179 printf ("\nslang: %d", SLANG_VERSION);
182 #ifdef _LIBICONV_VERSION
183 printf ("\nlibiconv: %d.%d", _LIBICONV_VERSION >> 8,
184 _LIBICONV_VERSION & 0xff);
188 printf ("\nlibidn: %s (compiled with %s)", stringprep_check_version (NULL),
193 printf ("\nhcache backend: %s", mutt_hcache_backend ());
196 puts (_("\nCompile options:"));
199 printf ("DOMAIN=\"%s\"\n", DOMAIN);
270 #ifdef USE_SSL_OPENSSL
276 #ifdef USE_SSL_GNUTLS
321 #ifdef HAVE_START_COLOR
327 #ifdef HAVE_TYPEAHEAD
353 #ifdef HAVE_RESIZETERM
361 #ifdef CRYPT_BACKEND_CLASSIC_PGP
362 "+CRYPT_BACKEND_CLASSIC_PGP "
364 "-CRYPT_BACKEND_CLASSIC_PGP "
366 #ifdef CRYPT_BACKEND_CLASSIC_SMIME
367 "+CRYPT_BACKEND_CLASSIC_SMIME "
369 "-CRYPT_BACKEND_CLASSIC_SMIME "
371 #ifdef CRYPT_BACKEND_GPGME
372 "+CRYPT_BACKEND_GPGME "
374 "-CRYPT_BACKEND_GPGME "
385 #ifdef SUN_ATTACHMENT
411 #ifdef HAVE_LANGINFO_CODESET
412 "+HAVE_LANGINFO_CODESET "
414 "-HAVE_LANGINFO_CODESET "
418 #ifdef HAVE_LANGINFO_YESEXPR
419 "+HAVE_LANGINFO_YESEXPR "
421 "-HAVE_LANGINFO_YESEXPR "
459 printf ("ISPELL=\"%s\"\n", ISPELL);
464 printf ("SENDMAIL=\"%s\"\n", SENDMAIL);
465 printf ("MAILPATH=\"%s\"\n", MAILPATH);
466 printf ("PKGDATADIR=\"%s\"\n", PKGDATADIR);
467 printf ("SYSCONFDIR=\"%s\"\n", SYSCONFDIR);
468 printf ("EXECSHELL=\"%s\"\n", EXECSHELL);
470 printf ("MIXMASTER=\"%s\"\n", MIXMASTER);
477 mutt_print_patchlist();
482 static void start_curses (void)
484 km_init (); /* must come before mutt_init */
486 #ifdef USE_SLANG_CURSES
487 SLtt_Ignore_Beep = 1; /* don't do that #*$@^! annoying visual beep! */
488 SLsmg_Display_Eight_Bit = 128; /* characters above this are printable */
489 SLtt_set_color(0, NULL, "default", "default");
490 #if SLANG_VERSION >= 20000
494 /* should come before initscr() so that ncurses 4.2 doesn't try to install
495 its own SIGWINCH handler */
498 if (initscr () == NULL)
500 puts _("Error initializing terminal.");
503 #if 1 /* USE_SLANG_CURSES - commenting out suggested in #455. */
504 /* slang requires the signal handlers to be set after initializing */
508 keypad (stdscr, TRUE);
512 typeahead (-1); /* simulate smooth scrolling */
517 init_extended_keys();
520 #define M_IGNORE (1<<0) /* -z */
521 #define M_BUFFY (1<<1) /* -Z */
522 #define M_NOSYSRC (1<<2) /* -n */
523 #define M_RO (1<<3) /* -R */
524 #define M_SELECT (1<<4) /* -y */
526 int main (int argc, char **argv)
528 char folder[_POSIX_PATH_MAX] = "";
529 char *subject = NULL;
530 char *includeFile = NULL;
531 char *draftFile = NULL;
532 char *newMagic = NULL;
535 LIST *commands = NULL;
536 LIST *queries = NULL;
537 LIST *alias_queries = NULL;
542 int explicit_folder = 0;
543 int dump_variables = 0;
546 int double_dash = argc, nargc = 1;
548 /* sanity check against stupid administrators */
550 if(getegid() != getgid())
552 fprintf(stderr, "%s: I don't want to run with privileges!\n",
558 /* FIXME what about init.c:1439 ? */
559 setlocale (LC_ALL, "");
560 bindtextdomain (PACKAGE, MUTTLOCALEDIR);
561 textdomain (PACKAGE);
564 setlocale (LC_CTYPE, "");
566 mutt_error = mutt_nocurses_error;
567 mutt_message = mutt_nocurses_error;
571 memset (Options, 0, sizeof (Options));
572 memset (QuadOptions, 0, sizeof (QuadOptions));
574 for (optind = 1; optind < double_dash; )
576 /* We're getopt'ing POSIXLY, so we'll be here every time getopt()
577 * encounters a non-option. That could be a file to attach
578 * (all non-options between -a and --) or it could be an address
579 * (which gets collapsed to the front of argv).
581 for (; optind < argc; optind++)
583 if (argv[optind][0] == '-' && argv[optind][1] != '\0')
585 if (argv[optind][1] == '-' && argv[optind][2] == '\0')
586 double_dash = optind; /* quit outer loop after getopt */
587 break; /* drop through to getopt */
590 /* non-option, either an attachment or address */
592 attach = mutt_add_list (attach, argv[optind]);
594 argv[nargc++] = argv[optind];
597 if ((i = getopt (argc, argv, "+A:a:b:F:f:c:Dd:e:H:s:i:hm:npQ:RvxyzZ")) != EOF)
601 alias_queries = mutt_add_list (alias_queries, optarg);
604 attach = mutt_add_list (attach, optarg);
608 mutt_str_replace (&Muttrc, optarg);
612 strfcpy (folder, optarg, sizeof (folder));
619 msg = mutt_new_header ();
621 msg->env = mutt_new_envelope ();
623 msg->env->bcc = rfc822_parse_adrlist (msg->env->bcc, optarg);
625 msg->env->cc = rfc822_parse_adrlist (msg->env->cc, optarg);
634 if (mutt_atoi (optarg, &debuglevel) < 0 || debuglevel <= 0)
636 fprintf (stderr, _("Error: value '%s' is invalid for -d.\n"), optarg);
639 printf (_("Debugging at level %d.\n"), debuglevel);
641 printf _("DEBUG was not defined during compilation. Ignored.\n");
646 commands = mutt_add_list (commands, optarg);
654 includeFile = optarg;
658 /* should take precedence over .muttrc setting, so save it for later */
667 sendflags |= SENDPOSTPONED;
671 queries = mutt_add_list (queries, optarg);
675 flags |= M_RO; /* read-only mode */
686 case 'x': /* mailx compatible send mode */
687 sendflags |= SENDMAILX;
690 case 'y': /* My special hack mode */
699 flags |= M_BUFFY | M_IGNORE;
707 /* collapse remaining argv */
708 while (optind < argc)
709 argv[nargc++] = argv[optind++];
721 puts (mutt_make_version ());
725 puts (_(ReachingUs));
729 /* Check for a batch send. */
730 if (!isatty (0) || queries || alias_queries || dump_variables)
732 set_option (OPTNOCURSES);
733 sendflags = SENDBATCH;
736 /* This must come before mutt_init() because curses needs to be started
737 before calling the init_pair() function to set the color scheme. */
738 if (!option (OPTNOCURSES))
741 /* set defaults and read init files */
742 mutt_init (flags & M_NOSYSRC, commands);
743 mutt_free_list (&commands);
745 /* Initialize crypto backends. */
749 mx_set_magic (newMagic);
753 for (; optind < argc; optind++)
754 queries = mutt_add_list (queries, argv[optind]);
755 return mutt_query_variables (queries);
758 return mutt_dump_variables();
764 for (; optind < argc; optind++)
765 alias_queries = mutt_add_list (alias_queries, argv[optind]);
766 for (; alias_queries; alias_queries = alias_queries->next)
768 if ((a = mutt_lookup_alias (alias_queries->data)))
770 /* output in machine-readable form */
771 mutt_addrlist_to_idna (a, NULL);
772 mutt_write_address_list (a, stdout, 0, 0);
777 printf ("%s\n", alias_queries->data);
783 if (!option (OPTNOCURSES))
785 SETCOLOR (MT_COLOR_NORMAL);
787 mutt_error = mutt_curses_error;
788 mutt_message = mutt_curses_message;
791 /* Create the Maildir directory if it doesn't exist. */
792 if (!option (OPTNOCURSES) && Maildir)
795 char fpath[_POSIX_PATH_MAX];
798 strfcpy (fpath, Maildir, sizeof (fpath));
799 mutt_expand_path (fpath, sizeof (fpath));
801 /* we're not connected yet - skip mail folder creation */
802 if (!mx_is_imap (fpath))
804 if (stat (fpath, &sb) == -1 && errno == ENOENT)
806 snprintf (msg, sizeof (msg), _("%s does not exist. Create it?"), Maildir);
807 if (mutt_yesorno (msg, M_YES) == M_YES)
809 if (mkdir (fpath, 0700) == -1 && errno != EEXIST)
810 mutt_error ( _("Can't create %s: %s."), Maildir, strerror (errno));
815 if (sendflags & SENDPOSTPONED)
817 if (!option (OPTNOCURSES))
819 ci_send_message (SENDPOSTPONED, NULL, NULL, NULL, NULL);
822 else if (subject || msg || sendflags || draftFile || includeFile || attach ||
826 char buf[LONG_STRING];
827 char *tempfile = NULL, *infile = NULL;
828 char *bodytext = NULL;
831 if (!option (OPTNOCURSES))
835 msg = mutt_new_header ();
842 msg->env = mutt_new_envelope ();
844 for (i = optind; i < argc; i++)
846 if (url_check_scheme (argv[i]) == U_MAILTO)
848 if (url_parse_mailto (msg->env, &bodytext, argv[i]) < 0)
850 if (!option (OPTNOCURSES))
852 fputs (_("Failed to parse mailto: link\n"), stderr);
857 msg->env->to = rfc822_parse_adrlist (msg->env->to, argv[i]);
860 if (option (OPTAUTOEDIT) && !msg->env->to && !msg->env->cc)
862 if (!option (OPTNOCURSES))
864 fputs (_("No recipients specified.\n"), stderr);
869 msg->env->subject = safe_strdup (subject);
872 infile = includeFile;
875 if (infile || bodytext)
879 if (mutt_strcmp ("-", infile) == 0)
883 char path[_POSIX_PATH_MAX];
885 strfcpy (path, infile, sizeof (path));
886 mutt_expand_path (path, sizeof (path));
887 if ((fin = fopen (path, "r")) == NULL)
889 if (!option (OPTNOCURSES))
899 mutt_mktemp (buf, sizeof (buf));
900 tempfile = safe_strdup (buf);
903 msg->env = mutt_read_rfc822_header (fin, NULL, 1, 0);
905 /* is the following if still needed? */
911 if ((fout = safe_fopen (tempfile, "w")) == NULL)
913 if (!option (OPTNOCURSES))
921 mutt_copy_stream (fin, fout);
923 fputs (bodytext, fout);
925 if (fin && fin != stdin)
941 a->next = mutt_make_file_attach (t->data);
945 msg->content = a = mutt_make_file_attach (t->data);
948 if (!option (OPTNOCURSES))
950 fprintf (stderr, _("%s: unable to attach file.\n"), t->data);
951 mutt_free_list (&attach);
956 mutt_free_list (&attach);
959 rv = ci_send_message (sendflags, msg, tempfile, NULL, NULL);
961 if (!option (OPTNOCURSES))
971 if (!mutt_buffy_check (0))
973 mutt_endwin _("No mailbox with new mail.");
977 mutt_buffy (folder, sizeof (folder));
979 else if (flags & M_SELECT)
982 mutt_endwin _("No incoming mailboxes defined.");
986 mutt_select_file (folder, sizeof (folder), M_SEL_FOLDER | M_SEL_BUFFY);
995 strfcpy (folder, NONULL(Spoolfile), sizeof (folder));
996 mutt_expand_path (folder, sizeof (folder));
998 mutt_str_replace (&CurrentFolder, folder);
999 mutt_str_replace (&LastFolder, folder);
1001 if (flags & M_IGNORE)
1003 /* check to see if there are any messages in the folder */
1004 switch (mx_check_empty (folder))
1007 mutt_endwin (strerror (errno));
1010 mutt_endwin _("Mailbox is empty.");
1015 mutt_folder_hook (folder);
1017 if((Context = mx_open_mailbox (folder, ((flags & M_RO) || option (OPTREADONLY)) ? M_READONLY : 0, NULL))
1018 || !explicit_folder)
1031 mutt_endwin (Errorbuf);