Some nail.1 tweaks
[s-mailx.git] / main.c
blobb6d08b05941e15b602e1d3ed9ffb179ac28a659f
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:Ss: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, existonly = 0, headersonly = 0, sendflag = 0;
99 struct name *to, *cc, *bcc, *smopts;
100 struct attachment *attach;
101 char *subject, *cp, *ef, *qf = NULL, *fromaddr = NULL, *Aflag = NULL;
102 char nosrc = 0;
103 int Eflag = 0, Fflag = 0, Nflag = 0, tflag = 0;
104 sighandler_type prevint;
106 (void)&Nflag;
108 * Absolutely the first thing we do is save our egid
109 * and set it to the rgid, so that we can safely run
110 * setgid. We use the sgid (saved set-gid) to allow ourselves
111 * to revert to the egid if we want (temporarily) to become
112 * priveliged.
114 effectivegid = getegid();
115 realgid = getgid();
116 if (setgid(realgid) < 0) {
117 perror("setgid");
118 exit(1);
121 starting = 1;
122 progname = strrchr(argv[0], '/');
123 if (progname != NULL)
124 progname++;
125 else
126 progname = argv[0];
128 * Set up a reasonable environment.
129 * Figure out whether we are being run interactively,
130 * start the SIGCHLD catcher, and so forth.
132 safe_signal(SIGCHLD, sigchild);
133 is_a_tty[0] = isatty(0);
134 is_a_tty[1] = isatty(1);
135 if (is_a_tty[0]) {
136 assign("interactive", "");
137 if (is_a_tty[1])
138 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
140 assign("header", "");
141 assign("save", "");
142 #ifdef HAVE_SETLOCALE
143 setlocale(LC_ALL, "");
144 mb_cur_max = MB_CUR_MAX;
145 # if defined HAVE_NL_LANGINFO && defined CODESET
146 if (value("ttycharset") == NULL && (cp = nl_langinfo(CODESET)) != NULL)
147 assign("ttycharset", cp);
148 # endif
149 # if defined HAVE_MBTOWC && defined HAVE_WCTYPE_H
150 if (mb_cur_max > 1) {
151 wchar_t wc;
152 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
153 mbtowc(&wc, "\342\202\254", 3) == 3 &&
154 wc == 0x20AC)
155 utf8 = 1;
157 # endif
158 #else
159 mb_cur_max = 1;
160 #endif
161 #ifdef HAVE_CATGETS
162 # ifdef NL_CAT_LOCALE
163 i = NL_CAT_LOCALE;
164 # else
165 i = 0;
166 # endif
167 catd = catopen(CATNAME, i);
168 #endif
169 #ifdef HAVE_ICONV
170 iconvd = (iconv_t) -1;
171 #endif
172 image = -1;
174 * Now, determine how we are being used.
175 * We successively pick off - flags.
176 * If there is anything left, it is the base of the list
177 * of users to mail to. Argp will be set to point to the
178 * first of these users.
180 ef = NULL;
181 to = NULL;
182 cc = NULL;
183 bcc = NULL;
184 attach = NULL;
185 smopts = NULL;
186 subject = NULL;
187 scnt = 0;
188 while ((i = getopt(argc, argv, optstr)) != EOF) {
189 switch (i) {
190 case 'A':
191 /* Execute an account command later on */
192 Aflag = optarg;
193 break;
194 case 'a':
195 /* Get attachment filenames */
196 attach = add_attachment(attach, optarg, 0);
197 if (attach == NULL) {
198 perror(optarg);
199 exit(1);
201 sendflag++;
202 break;
203 case 'B':
204 /* Make 0/1 line buffered */
205 setvbuf(stdin, NULL, _IOLBF, 0);
206 setvbuf(stdout, NULL, _IOLBF, 0);
207 break;
208 case 'b':
209 /* Get Blind Carbon Copy Recipient list */
210 bcc = checkaddrs(cat(bcc,
211 userarg_extract(optarg, GBCC|GFULL)));
212 sendflag++;
213 break;
214 case 'c':
215 /* Get Carbon Copy Recipient list */
216 cc = checkaddrs(cat(cc,
217 userarg_extract(optarg, GCC|GFULL)));
218 sendflag++;
219 break;
220 case 'D':
221 assign("disconnected", "");
222 break;
223 case 'd':
224 debug++;
225 break;
226 case 'E':
227 Eflag = 1;
228 break;
229 case 'e':
230 existonly++;
231 break;
232 case 'F':
233 Fflag = 1;
234 sendflag++;
235 break;
236 case 'f':
238 * User is specifying file to "edit" with Mail,
239 * as opposed to reading system mailbox.
240 * If no argument is given, we read his mbox file.
241 * Check for remaining arguments later.
243 ef = "&";
244 break;
245 case 'H':
246 headersonly = 1;
247 break;
248 jIflag: case 'I':
249 /* Show Newsgroups: field in header summary */
250 Iflag = 1;
251 break;
253 case 'i':
254 /* Ignore interrupts */
255 assign("ignore", "");
256 break;
257 case 'N':
258 /* Avoid initial header printing */
259 Nflag = 1;
260 unset_internal("header");
261 break;
262 case 'n':
263 /* Don't source "unspecified system start-up file" */
264 ++nosrc;
265 break;
266 case 'O':
267 /* Additional options to pass-through to MTA */
268 smopts = cat(smopts, nalloc(optarg, 0));
269 sendflag++;
270 break;
271 case 'q':
272 /* Quote file TODO drop? -Q with real quote?? what ? */
273 if (*optarg != '-')
274 qf = optarg;
275 sendflag++;
276 break;
277 case 'R':
278 /* Open folders read-only */
279 Rflag = 1;
280 break;
281 case 'r':
282 /* Set From address. */
283 i = count(smopts);
284 fromaddr = optarg;
285 smopts = cat(smopts, nalloc("-r", 0));
286 smopts = cat(smopts, userarg_extract(optarg, GFULL));
287 if (count(smopts) != i + 2) {
288 fprintf(stderr, tr(271,
289 "More than one address "
290 "given with -r\n"));
291 goto usage;
293 tildeflag = -1;
294 sendflag++;
295 break;
296 case 'S':
297 /* Set variable (do so later, after RC loading..) */
298 argv[scnt++] = optarg;
299 break;
300 case 's':
301 /* Subject: */
302 subject = optarg;
303 sendflag++;
304 break;
305 case 'T':
307 * Next argument is temp file to write which
308 * articles have been read/deleted for netnews.
310 Tflag = optarg;
311 if ((i = creat(Tflag, 0600)) < 0) {
312 perror(Tflag);
313 exit(1);
315 close(i);
316 goto jIflag;
317 case 't':
318 /* Read defined set of headers from mail to be send */
319 sendflag = 1;
320 tflag = 1;
321 break;
322 case 'u':
323 /* Set user name to pretend to be */
324 uflag = myname = optarg;
325 break;
326 case 'V':
327 puts(version);
328 exit(0);
329 /*NOTREACHED*/
330 case 'v':
331 /* Be verbose */
332 assign("verbose", "");
333 break;
334 case '~':
335 /* Enable tilde escapes even in non-interactive mode */
336 if (tildeflag == 0)
337 tildeflag = 1;
338 break;
339 case '?':
340 usage: fprintf(stderr, tr(135, usagestr),
341 progname, progname, progname);
342 exit(2);
346 if (ef != NULL) {
347 if (optind < argc) {
348 if (optind + 1 < argc) {
349 fprintf(stderr, tr(205,
350 "More than one file given with -f\n"));
351 goto usage;
353 ef = argv[optind];
355 } else {
356 for (i = optind; argv[i]; i++)
357 to = checkaddrs(cat(to,
358 userarg_extract(argv[i], GTO|GFULL)));
361 /* Check for inconsistent arguments */
362 if (ef != NULL && to != NULL) {
363 fprintf(stderr, tr(137,
364 "Cannot give -f and people to send to.\n"));
365 goto usage;
367 if (sendflag && !tflag && to == NULL) {
368 fprintf(stderr, tr(138,
369 "Send options without primary recipient specified.\n"));
370 goto usage;
372 if (Rflag && to != NULL) {
373 fprintf(stderr, "The -R option is meaningless in send mode.\n");
374 goto usage;
376 if (Iflag && ef == NULL) {
377 fprintf(stderr, tr(204, "Need -f with -I.\n"));
378 goto usage;
381 tinit();
382 setscreensize(0);
383 #ifdef SIGWINCH
384 if (value("interactive"))
385 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
386 safe_signal(SIGWINCH, setscreensize);
387 #endif
388 input = stdin;
389 rcvmode = !to && !tflag;
390 spreserve();
392 if (! nosrc)
393 load(SYSCONFRC);
395 * Expand returns a savestr, but load only uses the file name
396 * for fopen, so it's safe to do this.
398 if ((cp = getenv("MAILRC")) != NULL)
399 load(expand(cp));
400 else if ((cp = getenv("NAILRC")) != NULL)
401 load(expand(cp));
402 else
403 load(expand("~/.mailrc"));
404 if (getenv("NAIL_EXTRA_RC") == NULL &&
405 (cp = value("NAIL_EXTRA_RC")) != NULL)
406 load(expand(cp));
409 * Now we can set the account.
411 if (Aflag) {
412 char *a[2];
413 a[0] = Aflag;
414 a[1] = NULL;
415 account(a);
418 * Override 'skipemptybody' if '-E' flag was given.
420 if (Eflag)
421 assign("skipemptybody", "");
423 * -S arguments override rc files.
425 for (i = 0; i < scnt; ++i) {
426 char *a[2];
427 a[0] = argv[i];
428 a[1] = NULL;
429 set(a);
432 starting = 0;
435 * From address from command line overrides rc files.
437 if (fromaddr)
438 assign("from", fromaddr);
439 if (!rcvmode) {
440 mail(to, cc, bcc, smopts, subject, attach, qf, Fflag, tflag,
441 Eflag);
443 * why wait?
445 exit(senderr ? 1 : 0);
448 * Ok, we are reading mail.
449 * Decide whether we are editing a mailbox or reading
450 * the system mailbox, and open up the right stuff.
452 if (ef == NULL)
453 ef = "%";
454 else if (*ef == '@') {
456 * This must be treated specially to make invocation like
457 * -A imap -f @mailbox work.
459 if ((cp = value("folder")) != NULL &&
460 which_protocol(cp) == PROTO_IMAP)
461 strncpy(mailname, cp, PATHSIZE)[PATHSIZE-1] = '\0';
463 i = setfile(ef, 0);
464 if (i < 0)
465 exit(1); /* error already reported */
466 if (existonly)
467 exit(i);
468 if (headersonly) {
469 if (mb.mb_type == MB_IMAP)
470 imap_getheaders(1, msgCount);
471 for (i = 1; i <= msgCount; i++)
472 printhead(i, stdout, 0);
473 exit(exit_status);
475 callhook(mailname, 0);
476 if (i > 0 && value("emptystart") == NULL)
477 exit(1);
478 if (sigsetjmp(hdrjmp, 1) == 0) {
479 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
480 safe_signal(SIGINT, hdrstop);
481 if (Nflag == 0) {
482 if (value("quiet") == NULL)
483 printf(tr(140,
484 "%s version %s. Type ? for help.\n"),
485 value("bsdcompat") ? "Mail" : uagent,
486 version);
487 announce(1);
488 fflush(stdout);
490 safe_signal(SIGINT, prevint);
492 commands();
493 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
494 safe_signal(SIGHUP, SIG_IGN);
495 safe_signal(SIGINT, SIG_IGN);
496 safe_signal(SIGQUIT, SIG_IGN);
498 strncpy(mboxname, expand("&"), sizeof mboxname)[sizeof mboxname-1]='\0';
499 quit();
500 return exit_status;
504 * Interrupt printing of the headers.
506 /*ARGSUSED*/
507 static void
508 hdrstop(int signo)
510 (void)signo;
512 fflush(stdout);
513 fprintf(stderr, tr(141, "\nInterrupt\n"));
514 siglongjmp(hdrjmp, 1);
518 * Compute what the screen size for printing headers should be.
519 * We use the following algorithm for the height:
520 * If baud rate < 1200, use 9
521 * If baud rate = 1200, use 14
522 * If baud rate > 1200, use 24 or ws_row
523 * Width is either 80 or ws_col;
525 /*ARGSUSED*/
526 static void
527 setscreensize(int dummy)
529 struct termios tbuf;
530 #ifdef TIOCGWINSZ
531 struct winsize ws;
532 #endif
533 speed_t ospeed;
534 (void)dummy;
536 #ifdef TIOCGWINSZ
537 if (ioctl(1, TIOCGWINSZ, &ws) < 0)
538 ws.ws_col = ws.ws_row = 0;
539 #endif
540 if (tcgetattr(1, &tbuf) < 0)
541 ospeed = B9600;
542 else
543 ospeed = cfgetospeed(&tbuf);
544 if (ospeed < B1200)
545 scrnheight = 9;
546 else if (ospeed == B1200)
547 scrnheight = 14;
548 #ifdef TIOCGWINSZ
549 else if (ws.ws_row != 0)
550 scrnheight = ws.ws_row;
551 #endif
552 else
553 scrnheight = 24;
554 #ifdef TIOCGWINSZ
555 if ((realscreenheight = ws.ws_row) == 0)
556 realscreenheight = 24;
557 #endif
558 #ifdef TIOCGWINSZ
559 if ((scrnwidth = ws.ws_col) == 0)
560 #endif
561 scrnwidth = 80;