Tweak *rfc822-show-all* manual text
[s-mailx.git] / main.c
blob6c32dee7ef71194f6ba2a752036619c9ea9fac4d
1 /*
2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 /* TODO longjmp() globbering as in cmd1.c and cmd3.c (see there)
41 * TODO Problem: Popen doesn't encapsulate all cases of open failures,
42 * TODO may leave child running if fdopen() fails! */
45 * Most strcpy/sprintf functions have been changed to strncpy/snprintf to
46 * correct several buffer overruns (at least one ot them was exploitable).
47 * Sat Jun 20 04:58:09 CEST 1998 Alvaro Martinez Echevarria <alvaro@lander.es>
48 * ---
49 * Note: We set egid to realgid ... and only if we need the egid we will
50 * switch back temporary. Nevertheless, I do not like seg faults.
51 * Werner Fink, <werner@suse.de>
55 #include "config.h"
56 #ifdef HAVE_NL_LANGINFO
57 # include <langinfo.h>
58 #endif
59 #define _MAIL_GLOBS_
60 #include "rcv.h"
61 #include "extern.h"
62 #include <sys/stat.h>
63 #include <sys/ioctl.h>
64 #include <termios.h>
65 #include <unistd.h>
66 #include <fcntl.h>
67 #ifdef HAVE_SETLOCALE
68 # include <locale.h>
69 #endif
72 * Mail -- a mail program
74 * Startup -- interface with user.
77 static sigjmp_buf hdrjmp;
79 char *progname;
80 sighandler_type dflpipe = SIG_DFL;
82 static void hdrstop(int signo);
83 static void setscreensize(int dummy);
85 int
86 main(int argc, char *argv[])
88 const char optstr[] = "A:a:Bb:c:DdEeFfHIiNnO:q:Rr:S:s:T:tu:Vv~",
89 usagestr[] =
90 "Usage:\t%s [-BDdEFintv~] [-A acc] [-a attachment]\n"
91 "\t\t[-b bcc-addr] [-c cc-addr] [-O mtaopt [-O mtaopt-arg]]\n"
92 "\t\t[-q file] [-r from-addr] [-S var[=value]]\n"
93 "\t\t[-s subject] to-addr...\n"
94 "\t%s [-BDdEeHIiNnRv~] [-A acct]\n"
95 "\t\t[-S var[=value]] [-T name] -f [file]\n"
96 "\t%s [-BDdEeiNnRv~] [-A acc] [-S var[=value]] [-u user]\n";
98 int scnt, i;
99 struct name *to, *cc, *bcc, *smopts;
100 struct attachment *attach;
101 char *cp, *subject, *ef, *qf, *fromaddr, *Aflag;
102 char existonly, headersonly, sendflag, nosrc, Eflag, Fflag, tflag;
103 char volatile Nflag;
104 sighandler_type prevint;
107 * Absolutely the first thing we do is save our egid
108 * and set it to the rgid, so that we can safely run
109 * setgid. We use the sgid (saved set-gid) to allow ourselves
110 * to revert to the egid if we want (temporarily) to become
111 * priveliged.
113 effectivegid = getegid();
114 realgid = getgid();
115 if (setgid(realgid) < 0) {
116 perror("setgid");
117 exit(1);
120 starting = 1;
121 progname = strrchr(argv[0], '/');
122 if (progname != NULL)
123 progname++;
124 else
125 progname = argv[0];
127 * Set up a reasonable environment.
128 * Figure out whether we are being run interactively,
129 * start the SIGCHLD catcher, and so forth.
131 safe_signal(SIGCHLD, sigchild);
132 is_a_tty[0] = isatty(0);
133 is_a_tty[1] = isatty(1);
134 if (is_a_tty[0]) {
135 assign("interactive", "");
136 if (is_a_tty[1])
137 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
139 assign("header", "");
140 assign("save", "");
141 #ifdef HAVE_SETLOCALE
142 setlocale(LC_ALL, "");
143 mb_cur_max = MB_CUR_MAX;
144 # if defined HAVE_NL_LANGINFO && defined CODESET
145 if (value("ttycharset") == NULL && (cp = nl_langinfo(CODESET)) != NULL)
146 assign("ttycharset", cp);
147 # endif
148 # if defined HAVE_MBTOWC && defined HAVE_WCTYPE_H
149 if (mb_cur_max > 1) {
150 wchar_t wc;
151 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
152 mbtowc(&wc, "\342\202\254", 3) == 3 &&
153 wc == 0x20AC)
154 utf8 = 1;
156 # endif
157 #else
158 mb_cur_max = 1;
159 #endif
160 #ifdef HAVE_CATGETS
161 # ifdef NL_CAT_LOCALE
162 i = NL_CAT_LOCALE;
163 # else
164 i = 0;
165 # endif
166 catd = catopen(CATNAME, i);
167 #endif
168 #ifdef HAVE_ICONV
169 iconvd = (iconv_t) -1;
170 #endif
171 image = -1;
173 * Now, determine how we are being used.
174 * We successively pick off - flags.
175 * If there is anything left, it is the base of the list
176 * of users to mail to. Argp will be set to point to the
177 * first of these users.
179 scnt = 0;
180 to = cc = bcc = smopts = NULL;
181 attach = NULL;
182 subject = ef = qf = fromaddr = Aflag = NULL;
183 existonly = headersonly = sendflag = nosrc =
184 Eflag = Fflag = tflag = 0;
185 Nflag = 0;
187 while ((i = getopt(argc, argv, optstr)) != EOF) {
188 switch (i) {
189 case 'A':
190 /* Execute an account command later on */
191 Aflag = optarg;
192 break;
193 case 'a':
194 /* Get attachment filenames */
195 attach = add_attachment(attach, optarg, 0);
196 if (attach == NULL) {
197 perror(optarg);
198 exit(1);
200 sendflag = 1;
201 break;
202 case 'B':
203 /* Make 0/1 line buffered */
204 setvbuf(stdin, NULL, _IOLBF, 0);
205 setvbuf(stdout, NULL, _IOLBF, 0);
206 break;
207 case 'b':
208 /* Get Blind Carbon Copy Recipient list */
209 bcc = cat(bcc, checkaddrs(lextract(optarg,GBCC|GFULL)));
210 sendflag = 1;
211 break;
212 case 'c':
213 /* Get Carbon Copy Recipient list */
214 cc = cat(cc, checkaddrs(lextract(optarg, GCC|GFULL)));
215 sendflag = 1;
216 break;
217 case 'D':
218 assign("disconnected", "");
219 break;
220 case 'd':
221 ++debug;
222 break;
223 case 'E':
224 Eflag = 1;
225 break;
226 case 'e':
227 existonly = 1;
228 break;
229 case 'F':
230 Fflag = 1;
231 sendflag = 1;
232 break;
233 case 'f':
235 * User is specifying file to "edit" with Mail,
236 * as opposed to reading system mailbox.
237 * If no argument is given, we read his mbox file.
238 * Check for remaining arguments later.
240 ef = "&";
241 break;
242 case 'H':
243 headersonly = 1;
244 break;
245 jIflag: case 'I':
246 /* Show Newsgroups: field in header summary */
247 Iflag = 1;
248 break;
249 case 'i':
250 /* Ignore interrupts */
251 assign("ignore", "");
252 break;
253 case 'N':
254 /* Avoid initial header printing */
255 Nflag = 1;
256 unset_internal("header");
257 break;
258 case 'n':
259 /* Don't source "unspecified system start-up file" */
260 nosrc = 1;
261 break;
262 case 'O':
263 /* Additional options to pass-through to MTA */
264 smopts = cat(smopts, nalloc(optarg, 0));
265 sendflag = 1;
266 break;
267 case 'q':
268 /* Quote file TODO drop? -Q with real quote?? what ? */
269 if (*optarg != '-')
270 qf = optarg;
271 sendflag = 1;
272 break;
273 case 'R':
274 /* Open folders read-only */
275 Rflag = 1;
276 break;
277 case 'r':
278 /* Set From address. */
279 i = count(smopts);
280 fromaddr = optarg;
281 smopts = cat(smopts, nalloc("-r", 0));
282 smopts = cat(smopts, lextract(optarg, GFULL));
283 if (count(smopts) != i + 2) {
284 fprintf(stderr, tr(271,
285 "More than one address "
286 "given with -r\n"));
287 goto usage;
289 tildeflag = -1;
290 sendflag = 1;
291 break;
292 case 'S':
293 /* Set variable (do so later, after RC loading..) */
294 argv[scnt++] = optarg;
295 break;
296 case 's':
297 /* Subject: */
298 subject = optarg;
299 sendflag = 1;
300 break;
301 case 'T':
303 * Next argument is temp file to write which
304 * articles have been read/deleted for netnews.
306 Tflag = optarg;
307 if ((i = creat(Tflag, 0600)) < 0) {
308 perror(Tflag);
309 exit(1);
311 close(i);
312 goto jIflag;
313 case 't':
314 /* Read defined set of headers from mail to be send */
315 sendflag = 1;
316 tflag = 1;
317 break;
318 case 'u':
319 /* Set user name to pretend to be */
320 uflag = myname = optarg;
321 break;
322 case 'V':
323 puts(version);
324 exit(0);
325 /*NOTREACHED*/
326 case 'v':
327 /* Be verbose */
328 assign("verbose", "");
329 break;
330 case '~':
331 /* Enable tilde escapes even in non-interactive mode */
332 if (tildeflag == 0)
333 tildeflag = 1;
334 break;
335 case '?':
336 usage: fprintf(stderr, tr(135, usagestr),
337 progname, progname, progname);
338 exit(2);
342 if (ef != NULL) {
343 if (optind < argc) {
344 if (optind + 1 < argc) {
345 fprintf(stderr, tr(205,
346 "More than one file given with -f\n"));
347 goto usage;
349 ef = argv[optind];
351 } else {
352 for (i = optind; argv[i]; i++)
353 to = cat(to, checkaddrs(lextract(argv[i], GTO|GFULL)));
356 /* Check for inconsistent arguments */
357 if (ef != NULL && to != NULL) {
358 fprintf(stderr, tr(137,
359 "Cannot give -f and people to send to.\n"));
360 goto usage;
362 if (sendflag && !tflag && to == NULL) {
363 fprintf(stderr, tr(138,
364 "Send options without primary recipient specified.\n"));
365 goto usage;
367 if (Rflag && to != NULL) {
368 fprintf(stderr, "The -R option is meaningless in send mode.\n");
369 goto usage;
371 if (Iflag && ef == NULL) {
372 fprintf(stderr, tr(204, "Need -f with -I.\n"));
373 goto usage;
376 tinit();
377 setscreensize(0);
378 #ifdef SIGWINCH
379 if (value("interactive"))
380 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
381 safe_signal(SIGWINCH, setscreensize);
382 #endif
383 input = stdin;
384 rcvmode = !to && !tflag;
385 spreserve();
387 if (! nosrc)
388 load(SYSCONFRC);
390 * Expand returns a savestr, but load only uses the file name
391 * for fopen, so it's safe to do this.
393 if ((cp = getenv("MAILRC")) != NULL)
394 load(file_expand(cp));
395 else if ((cp = getenv("NAILRC")) != NULL)
396 load(file_expand(cp));
397 else
398 load(file_expand("~/.mailrc"));
399 if (getenv("NAIL_EXTRA_RC") == NULL &&
400 (cp = value("NAIL_EXTRA_RC")) != NULL)
401 load(file_expand(cp));
404 * Now we can set the account.
406 if (Aflag) {
407 char *a[2];
408 a[0] = Aflag;
409 a[1] = NULL;
410 account(a);
413 * Override 'skipemptybody' if '-E' flag was given.
415 if (Eflag)
416 assign("skipemptybody", "");
418 * -S arguments override rc files.
420 for (i = 0; i < scnt; ++i) {
421 char *a[2];
422 a[0] = argv[i];
423 a[1] = NULL;
424 set(a);
427 starting = 0;
430 * From address from command line overrides rc files.
432 if (fromaddr)
433 assign("from", fromaddr);
434 if (!rcvmode) {
435 mail(to, cc, bcc, smopts, subject, attach, qf, Fflag, tflag,
436 Eflag);
438 * why wait?
440 exit(senderr ? 1 : 0);
443 * Ok, we are reading mail.
444 * Decide whether we are editing a mailbox or reading
445 * the system mailbox, and open up the right stuff.
447 if (ef == NULL)
448 ef = "%";
449 else if (*ef == '@') {
451 * This must be treated specially to make invocation like
452 * -A imap -f @mailbox work.
454 if ((cp = value("folder")) != NULL &&
455 which_protocol(cp) == PROTO_IMAP)
456 strncpy(mailname, cp, PATHSIZE)[PATHSIZE-1] = '\0';
458 i = setfile(ef, 0);
459 if (i < 0)
460 exit(1); /* error already reported */
461 if (existonly)
462 exit(i);
463 if (headersonly) {
464 if (mb.mb_type == MB_IMAP)
465 imap_getheaders(1, msgCount);
466 for (i = 1; i <= msgCount; i++)
467 printhead(i, stdout, 0);
468 exit(exit_status);
470 callhook(mailname, 0);
471 if (i > 0 && value("emptystart") == NULL)
472 exit(1);
473 if (sigsetjmp(hdrjmp, 1) == 0) {
474 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
475 safe_signal(SIGINT, hdrstop);
476 if (Nflag == 0) {
477 if (value("quiet") == NULL)
478 printf(tr(140,
479 "%s version %s. Type ? for help.\n"),
480 value("bsdcompat") ? "Mail" : uagent,
481 version);
482 announce(1);
483 fflush(stdout);
485 safe_signal(SIGINT, prevint);
487 commands();
488 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
489 safe_signal(SIGHUP, SIG_IGN);
490 safe_signal(SIGINT, SIG_IGN);
491 safe_signal(SIGQUIT, SIG_IGN);
493 strncpy(mboxname, expand("&"), sizeof mboxname)[sizeof mboxname-1]='\0';
494 quit();
495 return exit_status;
499 * Interrupt printing of the headers.
501 /*ARGSUSED*/
502 static void
503 hdrstop(int signo)
505 (void)signo;
507 fflush(stdout);
508 fprintf(stderr, tr(141, "\nInterrupt\n"));
509 siglongjmp(hdrjmp, 1);
513 * Compute what the screen size for printing headers should be.
514 * We use the following algorithm for the height:
515 * If baud rate < 1200, use 9
516 * If baud rate = 1200, use 14
517 * If baud rate > 1200, use 24 or ws_row
518 * Width is either 80 or ws_col;
520 /*ARGSUSED*/
521 static void
522 setscreensize(int dummy)
524 struct termios tbuf;
525 #ifdef TIOCGWINSZ
526 struct winsize ws;
527 #endif
528 speed_t ospeed;
529 (void)dummy;
531 #ifdef TIOCGWINSZ
532 if (ioctl(1, TIOCGWINSZ, &ws) < 0)
533 ws.ws_col = ws.ws_row = 0;
534 #endif
535 if (tcgetattr(1, &tbuf) < 0)
536 ospeed = B9600;
537 else
538 ospeed = cfgetospeed(&tbuf);
539 if (ospeed < B1200)
540 scrnheight = 9;
541 else if (ospeed == B1200)
542 scrnheight = 14;
543 #ifdef TIOCGWINSZ
544 else if (ws.ws_row != 0)
545 scrnheight = ws.ws_row;
546 #endif
547 else
548 scrnheight = 24;
549 #ifdef TIOCGWINSZ
550 if ((realscreenheight = ws.ws_row) == 0)
551 realscreenheight = 24;
552 #endif
553 #ifdef TIOCGWINSZ
554 if ((scrnwidth = ws.ws_col) == 0)
555 #endif
556 scrnwidth = 80;