Tweak for (More on names and package friendliness.., 2012-09-28)
[s-mailx.git] / main.c
blobff50a81ed0bdfc6a9b543e3aaf6973350ea23280
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 /* HAVE_NL_LANGINFO */
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 /* HAVE_SETLOCALE */
72 * Mail -- a mail program
74 * Startup -- interface with user.
77 static sigjmp_buf hdrjmp;
78 char *progname;
79 sighandler_type dflpipe = SIG_DFL;
81 static void hdrstop(int signo);
82 static void setscreensize(int dummy);
84 int
85 main(int argc, char *argv[])
87 const char optstr[] = "A:BHEFINVT:RS:a:b:c:dDefinqr:s:tu:v~O:";
88 int scnt, i, existonly = 0, headersonly = 0, sendflag = 0;
89 struct name *to, *cc, *bcc, *smopts;
90 struct attachment *attach;
91 char *subject, *cp, *ef, *qf = NULL, *fromaddr = NULL, *Aflag = NULL;
92 char nosrc = 0;
93 int Eflag = 0, Fflag = 0, Nflag = 0, tflag = 0;
94 sighandler_type prevint;
96 (void)&Nflag;
98 * Absolutely the first thing we do is save our egid
99 * and set it to the rgid, so that we can safely run
100 * setgid. We use the sgid (saved set-gid) to allow ourselves
101 * to revert to the egid if we want (temporarily) to become
102 * priveliged.
104 effectivegid = getegid();
105 realgid = getgid();
106 if (setgid(realgid) < 0) {
107 perror("setgid");
108 exit(1);
111 starting = 1;
112 progname = strrchr(argv[0], '/');
113 if (progname != NULL)
114 progname++;
115 else
116 progname = argv[0];
118 * Set up a reasonable environment.
119 * Figure out whether we are being run interactively,
120 * start the SIGCHLD catcher, and so forth.
122 safe_signal(SIGCHLD, sigchild);
123 is_a_tty[0] = isatty(0);
124 is_a_tty[1] = isatty(1);
125 if (is_a_tty[0]) {
126 assign("interactive", "");
127 if (is_a_tty[1])
128 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
130 assign("header", "");
131 assign("save", "");
132 #ifdef HAVE_SETLOCALE
133 setlocale(LC_CTYPE, "");
134 setlocale(LC_COLLATE, "");
135 setlocale(LC_MESSAGES, "");
136 mb_cur_max = MB_CUR_MAX;
137 #if defined (HAVE_NL_LANGINFO) && defined (CODESET)
138 if (value("ttycharset") == NULL && (cp = nl_langinfo(CODESET)) != NULL)
139 assign("ttycharset", cp);
140 #endif /* HAVE_NL_LANGINFO && CODESET */
141 #if defined (HAVE_MBTOWC) && defined (HAVE_WCTYPE_H)
142 if (mb_cur_max > 1) {
143 wchar_t wc;
144 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
145 mbtowc(&wc, "\342\202\254", 3) == 3 &&
146 wc == 0x20AC)
147 utf8 = 1;
149 #endif /* HAVE_MBTOWC && HAVE_WCTYPE_H */
150 #else /* !HAVE_SETLOCALE */
151 mb_cur_max = 1;
152 #endif /* !HAVE_SETLOCALE */
153 #ifdef HAVE_CATGETS
154 #ifdef NL_CAT_LOCALE
155 i = NL_CAT_LOCALE;
156 #else
157 i = 0;
158 #endif
159 catd = catopen(CATNAME, i);
160 #endif /* HAVE_CATGETS */
161 #ifdef HAVE_ICONV
162 iconvd = (iconv_t) -1;
163 #endif
164 image = -1;
166 * Now, determine how we are being used.
167 * We successively pick off - flags.
168 * If there is anything left, it is the base of the list
169 * of users to mail to. Argp will be set to point to the
170 * first of these users.
172 ef = NULL;
173 to = NULL;
174 cc = NULL;
175 bcc = NULL;
176 attach = NULL;
177 smopts = NULL;
178 subject = NULL;
179 scnt = 0;
180 while ((i = getopt(argc, argv, optstr)) != EOF) {
181 switch (i) {
182 case 'V':
183 puts(version);
184 exit(0);
185 /*NOTREACHED*/
186 case 'B':
187 setvbuf(stdin, NULL, _IOLBF, 0);
188 setvbuf(stdout, NULL, _IOLBF, 0);
189 break;
190 case 'H':
191 headersonly = 1;
192 break;
193 case 'E':
194 Eflag = 1;
195 break;
196 case 'F':
197 Fflag = 1;
198 sendflag++;
199 break;
200 case 'S':
201 argv[scnt++] = optarg;
202 break;
203 case 'T':
205 * Next argument is temp file to write which
206 * articles have been read/deleted for netnews.
208 Tflag = optarg;
209 if ((i = creat(Tflag, 0600)) < 0) {
210 perror(Tflag);
211 exit(1);
213 close(i);
214 /*FALLTHRU*/
215 case 'I':
217 * Show Newsgroups: field in header summary
219 Iflag = 1;
220 break;
221 case 'u':
223 * Next argument is person to pretend to be.
225 uflag = myname = optarg;
226 break;
227 case 'i':
229 * User wants to ignore interrupts.
230 * Set the variable "ignore"
232 assign("ignore", "");
233 break;
234 case 'd':
235 debug++;
236 break;
237 case 'D':
238 assign("disconnected", "");
239 break;
240 case 'e':
241 existonly++;
242 break;
243 case 's':
245 * Give a subject field for sending from
246 * non terminal
248 subject = optarg;
249 sendflag++;
250 break;
251 case 'f':
253 * User is specifying file to "edit" with Mail,
254 * as opposed to reading system mailbox.
255 * If no argument is given, we read his
256 * mbox file.
258 * Check for remaining arguments later.
260 ef = "&";
261 break;
262 case 'q':
264 * User is specifying file to quote in front of
265 * the mail to be collected.
267 if ((argv[optind]) && (argv[optind][0] != '-'))
268 qf = argv[optind++];
269 else
270 qf = NULL;
271 sendflag++;
272 break;
273 case 'n':
275 * User doesn't want to source /usr/lib/Mail.rc
277 nosrc++;
278 break;
279 case 'N':
281 * Avoid initial header printing.
283 Nflag = 1;
284 unset_internal("header");
285 break;
286 case 'v':
288 * Send mailer verbose flag
290 assign("verbose", "");
291 break;
292 case 'r':
294 * Set From address.
295 * MTA is only interested in plain address so strip
296 * anything else (but still give user the option to
297 * simply pass a fully fledged email address)
299 i = count(smopts);
300 fromaddr = optarg;
301 smopts = cat(smopts, nalloc("-r", 0));
302 smopts = cat(smopts, sextract(optarg, GFULL));
303 if (count(smopts) != i + 2) {
304 fprintf(stderr, tr(271,
305 "More than one address "
306 "given with -r\n"));
307 goto usage;
309 tildeflag = -1;
310 sendflag++;
311 break;
312 case 'a':
314 * Get attachment filenames
316 attach = add_attachment(attach, optarg, 0);
317 if (attach == NULL) {
318 perror(optarg);
319 exit(1);
321 sendflag++;
322 break;
323 case 'c':
325 * Get Carbon Copy Recipient list
327 cc = checkaddrs(cat(cc, sextract(optarg, GCC|GFULL)));
328 sendflag++;
329 break;
330 case 'b':
332 * Get Blind Carbon Copy Recipient list
334 bcc = checkaddrs(cat(bcc,
335 sextract(optarg, GBCC|GFULL)));
336 sendflag++;
337 break;
338 case 'O':
340 * Additional options to pass-through to MTA
342 smopts = cat(smopts, nalloc(optarg, 0));
343 sendflag++;
344 break;
345 case '~':
346 if (tildeflag == 0)
347 tildeflag = 1;
348 break;
349 case 't':
350 sendflag = 1;
351 tflag = 1;
352 break;
353 case 'A':
354 Aflag = optarg;
355 break;
356 case 'R':
357 Rflag = 1;
358 break;
359 case '?':
360 usage:
361 fprintf(stderr, tr(135,
362 "Usage: %s -eiIUdEFntBDNHRV~ -T FILE "
363 "-u USER -O MTAARG -r address "
364 "-s SUBJECT -a FILE -q FILE -f FILE "
365 "-A ACCOUNT -b USERS -c USERS "
366 "-S OPTION USERS\n"),
367 progname);
368 exit(2);
371 if (ef != NULL) {
372 if (optind < argc) {
373 if (optind + 1 < argc) {
374 fprintf(stderr, tr(205,
375 "More than one file given with -f\n"));
376 goto usage;
378 ef = argv[optind];
380 } else {
381 for (i = optind; argv[i]; i++)
382 to = checkaddrs(cat(to, sextract(argv[i], GTO|GFULL)));
385 * Check for inconsistent arguments.
387 if (ef != NULL && to != NULL) {
388 fprintf(stderr, tr(137,
389 "Cannot give -f and people to send to.\n"));
390 goto usage;
392 if (sendflag && !tflag && to == NULL) {
393 fprintf(stderr, tr(138,
394 "Send options without primary recipient specified.\n"));
395 goto usage;
397 if (Rflag && to != NULL) {
398 fprintf(stderr, "The -R option is meaningless in send mode.\n");
399 goto usage;
401 if (Iflag && ef == NULL) {
402 fprintf(stderr, tr(204, "Need -f with -I.\n"));
403 goto usage;
405 tinit();
406 setscreensize(0);
407 #ifdef SIGWINCH
408 if (value("interactive"))
409 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
410 safe_signal(SIGWINCH, setscreensize);
411 #endif /* SIGWINCH */
412 input = stdin;
413 rcvmode = !to && !tflag;
414 spreserve();
415 if (!nosrc)
416 load(MAILRC);
418 * Expand returns a savestr, but load only uses the file name
419 * for fopen, so it's safe to do this.
421 if ((cp = getenv("MAILRC")) != NULL)
422 load(expand(cp));
423 else if ((cp = getenv("NAILRC")) != NULL)
424 load(expand(cp));
425 else
426 load(expand("~/.mailrc"));
427 if (getenv("NAIL_EXTRA_RC") == NULL &&
428 (cp = value("NAIL_EXTRA_RC")) != NULL)
429 load(expand(cp));
431 * Now we can set the account.
433 if (Aflag) {
434 char *a[2];
435 a[0] = Aflag;
436 a[1] = NULL;
437 account(a);
440 * Override 'skipemptybody' if '-E' flag was given.
442 if (Eflag)
443 assign("skipemptybody", "");
445 * -S arguments override rc files.
447 for (i = 0; i < scnt; ++i) {
448 char *a[2];
449 a[0] = argv[i];
450 a[1] = NULL;
451 set(a);
454 starting = 0;
457 * From address from command line overrides rc files.
459 if (fromaddr)
460 assign("from", fromaddr);
461 if (!rcvmode) {
462 mail(to, cc, bcc, smopts, subject, attach, qf, Fflag, tflag,
463 Eflag);
465 * why wait?
467 exit(senderr ? 1 : 0);
470 * Ok, we are reading mail.
471 * Decide whether we are editing a mailbox or reading
472 * the system mailbox, and open up the right stuff.
474 if (ef == NULL)
475 ef = "%";
476 else if (*ef == '@') {
478 * This must be treated specially to make invocation like
479 * -A imap -f @mailbox work.
481 if ((cp = value("folder")) != NULL &&
482 which_protocol(cp) == PROTO_IMAP)
483 strncpy(mailname, cp, PATHSIZE)[PATHSIZE-1] = '\0';
485 i = setfile(ef, 0);
486 if (i < 0)
487 exit(1); /* error already reported */
488 if (existonly)
489 exit(i);
490 if (headersonly) {
491 if (mb.mb_type == MB_IMAP)
492 imap_getheaders(1, msgCount);
493 for (i = 1; i <= msgCount; i++)
494 printhead(i, stdout, 0);
495 exit(exit_status);
497 callhook(mailname, 0);
498 if (i > 0 && value("emptystart") == NULL)
499 exit(1);
500 if (sigsetjmp(hdrjmp, 1) == 0) {
501 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
502 safe_signal(SIGINT, hdrstop);
503 if (Nflag == 0) {
504 if (value("quiet") == NULL)
505 printf(tr(140,
506 "%s version %s. Type ? for help.\n"),
507 value("bsdcompat") ? "Mail" : uagent,
508 version);
509 announce(1);
510 fflush(stdout);
512 safe_signal(SIGINT, prevint);
514 commands();
515 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
516 safe_signal(SIGHUP, SIG_IGN);
517 safe_signal(SIGINT, SIG_IGN);
518 safe_signal(SIGQUIT, SIG_IGN);
520 strncpy(mboxname, expand("&"), sizeof mboxname)[sizeof mboxname-1]='\0';
521 quit();
522 return exit_status;
526 * Interrupt printing of the headers.
528 /*ARGSUSED*/
529 static void
530 hdrstop(int signo)
532 (void)signo;
534 fflush(stdout);
535 fprintf(stderr, tr(141, "\nInterrupt\n"));
536 siglongjmp(hdrjmp, 1);
540 * Compute what the screen size for printing headers should be.
541 * We use the following algorithm for the height:
542 * If baud rate < 1200, use 9
543 * If baud rate = 1200, use 14
544 * If baud rate > 1200, use 24 or ws_row
545 * Width is either 80 or ws_col;
547 /*ARGSUSED*/
548 static void
549 setscreensize(int dummy)
551 struct termios tbuf;
552 #ifdef TIOCGWINSZ
553 struct winsize ws;
554 #endif
555 speed_t ospeed;
556 (void)dummy;
558 #ifdef TIOCGWINSZ
559 if (ioctl(1, TIOCGWINSZ, &ws) < 0)
560 ws.ws_col = ws.ws_row = 0;
561 #endif
562 if (tcgetattr(1, &tbuf) < 0)
563 ospeed = B9600;
564 else
565 ospeed = cfgetospeed(&tbuf);
566 if (ospeed < B1200)
567 scrnheight = 9;
568 else if (ospeed == B1200)
569 scrnheight = 14;
570 #ifdef TIOCGWINSZ
571 else if (ws.ws_row != 0)
572 scrnheight = ws.ws_row;
573 #endif
574 else
575 scrnheight = 24;
576 #ifdef TIOCGWINSZ
577 if ((realscreenheight = ws.ws_row) == 0)
578 realscreenheight = 24;
579 #endif
580 #ifdef TIOCGWINSZ
581 if ((scrnwidth = ws.ws_col) == 0)
582 #endif
583 scrnwidth = 80;