uagent,version are "char const" not "const char"
[s-mailx.git] / main.c
bloba5e9f707977a1a59c9f1860ded9a8f342d22c46e
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.
41 * Most strcpy/sprintf functions have been changed to strncpy/snprintf to
42 * correct several buffer overruns (at least one ot them was exploitable).
43 * Sat Jun 20 04:58:09 CEST 1998 Alvaro Martinez Echevarria <alvaro@lander.es>
44 * ---
45 * Note: We set egid to realgid ... and only if we need the egid we will
46 * switch back temporary. Nevertheless, I do not like seg faults.
47 * Werner Fink, <werner@suse.de>
51 #include "config.h"
52 #ifdef HAVE_NL_LANGINFO
53 # include <langinfo.h>
54 #endif
55 #define _MAIL_GLOBS_
56 #include "rcv.h"
57 #include "extern.h"
58 #include <sys/stat.h>
59 #include <sys/ioctl.h>
60 #include <termios.h>
61 #include <unistd.h>
62 #include <fcntl.h>
63 #ifdef HAVE_SETLOCALE
64 # include <locale.h>
65 #endif
68 * Mail -- a mail program
70 * Startup -- interface with user.
73 static sigjmp_buf hdrjmp;
75 char *progname;
76 sighandler_type dflpipe = SIG_DFL;
78 static void hdrstop(int signo);
79 static void setscreensize(int dummy);
81 int
82 main(int argc, char *argv[])
84 const char optstr[] = "A:a:Bb:c:DdEeFfHIiNnO:q:Rr:S:s:T:tu:Vv~",
85 usagestr[] =
86 "Usage:\t%s [-BDdEFintv~] [-A acc] [-a attachment]\n"
87 "\t\t[-b bcc-addr] [-c cc-addr] [-O mtaopt [-O mtaopt-arg]]\n"
88 "\t\t[-q file] [-r from-addr] [-S var[=value]]\n"
89 "\t\t[-s subject] to-addr...\n"
90 "\t%s [-BDdEeHIiNnRv~] [-A acct]\n"
91 "\t\t[-S var[=value]] [-T name] -f [file]\n"
92 "\t%s [-BDdEeiNnRv~] [-A acc] [-S var[=value]] [-u user]\n";
94 int scnt, i;
95 struct name *to, *cc, *bcc, *smopts;
96 struct attachment *attach;
97 char *cp, *subject, *ef, *qf, *fromaddr, *Aflag;
98 char existonly, headersonly, sendflag, nosrc, Eflag, Fflag, tflag;
99 char volatile Nflag;
100 sighandler_type prevint;
103 * Absolutely the first thing we do is save our egid
104 * and set it to the rgid, so that we can safely run
105 * setgid. We use the sgid (saved set-gid) to allow ourselves
106 * to revert to the egid if we want (temporarily) to become
107 * priveliged.
109 effectivegid = getegid();
110 realgid = getgid();
111 if (setgid(realgid) < 0) {
112 perror("setgid");
113 exit(1);
116 starting = 1;
117 progname = strrchr(argv[0], '/');
118 if (progname != NULL)
119 progname++;
120 else
121 progname = argv[0];
123 * Set up a reasonable environment.
124 * Figure out whether we are being run interactively,
125 * start the SIGCHLD catcher, and so forth.
127 safe_signal(SIGCHLD, sigchild);
128 is_a_tty[0] = isatty(0);
129 is_a_tty[1] = isatty(1);
130 if (is_a_tty[0]) {
131 assign("interactive", "");
132 if (is_a_tty[1])
133 safe_signal(SIGPIPE, dflpipe = SIG_IGN);
135 assign("header", "");
136 assign("save", "");
137 #ifdef HAVE_SETLOCALE
138 setlocale(LC_ALL, "");
139 mb_cur_max = MB_CUR_MAX;
140 # if defined HAVE_NL_LANGINFO && defined CODESET
141 if (value("ttycharset") == NULL && (cp = nl_langinfo(CODESET)) != NULL)
142 assign("ttycharset", cp);
143 # endif
144 # if defined HAVE_MBTOWC && defined HAVE_WCTYPE_H
145 if (mb_cur_max > 1) {
146 wchar_t wc;
147 if (mbtowc(&wc, "\303\266", 2) == 2 && wc == 0xF6 &&
148 mbtowc(&wc, "\342\202\254", 3) == 3 &&
149 wc == 0x20AC)
150 utf8 = 1;
152 # endif
153 #else
154 mb_cur_max = 1;
155 #endif
156 #ifdef HAVE_CATGETS
157 # ifdef NL_CAT_LOCALE
158 i = NL_CAT_LOCALE;
159 # else
160 i = 0;
161 # endif
162 catd = catopen(CATNAME, i);
163 #endif
164 #ifdef HAVE_ICONV
165 iconvd = (iconv_t) -1;
166 #endif
167 image = -1;
169 * Now, determine how we are being used.
170 * We successively pick off - flags.
171 * If there is anything left, it is the base of the list
172 * of users to mail to. Argp will be set to point to the
173 * first of these users.
175 scnt = 0;
176 to = cc = bcc = smopts = NULL;
177 attach = NULL;
178 subject = ef = qf = fromaddr = Aflag = NULL;
179 existonly = headersonly = sendflag = nosrc =
180 Eflag = Fflag = tflag = 0;
181 Nflag = 0;
183 while ((i = getopt(argc, argv, optstr)) != EOF) {
184 switch (i) {
185 case 'A':
186 /* Execute an account command later on */
187 Aflag = optarg;
188 break;
189 case 'a':
190 /* Get attachment filenames */
191 attach = add_attachment(attach, optarg, 0);
192 if (attach == NULL) {
193 perror(optarg);
194 exit(1);
196 sendflag = 1;
197 break;
198 case 'B':
199 /* Make 0/1 line buffered */
200 setvbuf(stdin, NULL, _IOLBF, 0);
201 setvbuf(stdout, NULL, _IOLBF, 0);
202 break;
203 case 'b':
204 /* Get Blind Carbon Copy Recipient list */
205 bcc = cat(bcc, checkaddrs(lextract(optarg,GBCC|GFULL)));
206 sendflag = 1;
207 break;
208 case 'c':
209 /* Get Carbon Copy Recipient list */
210 cc = cat(cc, checkaddrs(lextract(optarg, GCC|GFULL)));
211 sendflag = 1;
212 break;
213 case 'D':
214 assign("disconnected", "");
215 break;
216 case 'd':
217 ++debug;
218 break;
219 case 'E':
220 Eflag = 1;
221 break;
222 case 'e':
223 existonly = 1;
224 break;
225 case 'F':
226 Fflag = 1;
227 sendflag = 1;
228 break;
229 case 'f':
231 * User is specifying file to "edit" with Mail,
232 * as opposed to reading system mailbox.
233 * If no argument is given, we read his mbox file.
234 * Check for remaining arguments later.
236 ef = "&";
237 break;
238 case 'H':
239 headersonly = 1;
240 break;
241 jIflag: case 'I':
242 /* Show Newsgroups: field in header summary */
243 Iflag = 1;
244 break;
245 case 'i':
246 /* Ignore interrupts */
247 assign("ignore", "");
248 break;
249 case 'N':
250 /* Avoid initial header printing */
251 Nflag = 1;
252 unset_internal("header");
253 break;
254 case 'n':
255 /* Don't source "unspecified system start-up file" */
256 nosrc = 1;
257 break;
258 case 'O':
259 /* Additional options to pass-through to MTA */
260 smopts = cat(smopts, nalloc(optarg, 0));
261 sendflag = 1;
262 break;
263 case 'q':
264 /* Quote file TODO drop? -Q with real quote?? what ? */
265 if (*optarg != '-')
266 qf = optarg;
267 sendflag = 1;
268 break;
269 case 'R':
270 /* Open folders read-only */
271 Rflag = 1;
272 break;
273 case 'r':
274 /* Set From address. */
275 i = count(smopts);
276 fromaddr = optarg;
277 smopts = cat(smopts, nalloc("-r", 0));
278 smopts = cat(smopts, lextract(optarg, GFULL));
279 if (count(smopts) != i + 2) {
280 fprintf(stderr, tr(271,
281 "More than one address "
282 "given with -r\n"));
283 goto usage;
285 tildeflag = -1;
286 sendflag = 1;
287 break;
288 case 'S':
289 /* Set variable (do so later, after RC loading..) */
290 argv[scnt++] = optarg;
291 /* TODO Also set -S directly (see TODO) */
292 { char *a[2];
293 a[0] = optarg;
294 a[1] = NULL;
295 set(a);
297 break;
298 case 's':
299 /* Subject: */
300 subject = optarg;
301 sendflag = 1;
302 break;
303 case 'T':
305 * Next argument is temp file to write which
306 * articles have been read/deleted for netnews.
308 Tflag = optarg;
309 if ((i = creat(Tflag, 0600)) < 0) {
310 perror(Tflag);
311 exit(1);
313 close(i);
314 goto jIflag;
315 case 't':
316 /* Read defined set of headers from mail to be send */
317 sendflag = 1;
318 tflag = 1;
319 break;
320 case 'u':
321 /* Set user name to pretend to be */
322 uflag = myname = optarg;
323 break;
324 case 'V':
325 puts(version);
326 exit(0);
327 /*NOTREACHED*/
328 case 'v':
329 /* Be verbose */
330 assign("verbose", "");
331 break;
332 case '~':
333 /* Enable tilde escapes even in non-interactive mode */
334 if (tildeflag == 0)
335 tildeflag = 1;
336 break;
337 case '?':
338 usage: fprintf(stderr, tr(135, usagestr),
339 progname, progname, progname);
340 exit(2);
344 if (ef != NULL) {
345 if (optind < argc) {
346 if (optind + 1 < argc) {
347 fprintf(stderr, tr(205,
348 "More than one file given with -f\n"));
349 goto usage;
351 ef = argv[optind];
353 } else {
354 for (i = optind; argv[i]; i++)
355 to = cat(to, checkaddrs(lextract(argv[i], GTO|GFULL)));
358 /* Check for inconsistent arguments */
359 if (ef != NULL && to != NULL) {
360 fprintf(stderr, tr(137,
361 "Cannot give -f and people to send to.\n"));
362 goto usage;
364 if (sendflag && !tflag && to == NULL) {
365 fprintf(stderr, tr(138,
366 "Send options without primary recipient specified.\n"));
367 goto usage;
369 if (Rflag && to != NULL) {
370 fprintf(stderr, "The -R option is meaningless in send mode.\n");
371 goto usage;
373 if (Iflag && ef == NULL) {
374 fprintf(stderr, tr(204, "Need -f with -I.\n"));
375 goto usage;
378 tinit();
379 setscreensize(0);
380 #ifdef SIGWINCH
381 if (value("interactive"))
382 if (safe_signal(SIGWINCH, SIG_IGN) != SIG_IGN)
383 safe_signal(SIGWINCH, setscreensize);
384 #endif
385 input = stdin;
386 rcvmode = !to && !tflag;
387 spreserve();
389 if (! nosrc)
390 load(SYSCONFRC);
392 * Expand returns a savestr, but load only uses the file name
393 * for fopen, so it's safe to do this.
395 if ((cp = getenv("MAILRC")) != NULL)
396 load(file_expand(cp));
397 else if ((cp = getenv("NAILRC")) != NULL)
398 load(file_expand(cp));
399 else
400 load(file_expand("~/.mailrc"));
401 if (getenv("NAIL_EXTRA_RC") == NULL &&
402 (cp = value("NAIL_EXTRA_RC")) != NULL)
403 load(file_expand(cp));
406 * Now we can set the account.
408 if (Aflag) {
409 char *a[2];
410 a[0] = Aflag;
411 a[1] = NULL;
412 account(a);
415 * Override 'skipemptybody' if '-E' flag was given.
417 if (Eflag)
418 assign("skipemptybody", "");
420 * -S arguments override rc files.
422 for (i = 0; i < scnt; ++i) {
423 char *a[2];
424 a[0] = argv[i];
425 a[1] = NULL;
426 set(a);
429 starting = 0;
432 * From address from command line overrides rc files.
434 if (fromaddr)
435 assign("from", fromaddr);
436 if (!rcvmode) {
437 mail(to, cc, bcc, smopts, subject, attach, qf, Fflag, tflag,
438 Eflag);
440 * why wait?
442 exit(senderr ? 1 : 0);
445 * Ok, we are reading mail.
446 * Decide whether we are editing a mailbox or reading
447 * the system mailbox, and open up the right stuff.
449 if (ef == NULL)
450 ef = "%";
451 else if (*ef == '@') {
453 * This must be treated specially to make invocation like
454 * -A imap -f @mailbox work.
456 if ((cp = value("folder")) != NULL &&
457 which_protocol(cp) == PROTO_IMAP)
458 strncpy(mailname, cp, PATHSIZE)[PATHSIZE-1] = '\0';
460 i = setfile(ef, 0);
461 if (i < 0)
462 exit(1); /* error already reported */
463 if (existonly)
464 exit(i);
465 if (headersonly) {
466 if (mb.mb_type == MB_IMAP)
467 imap_getheaders(1, msgCount);
468 for (i = 1; i <= msgCount; i++)
469 printhead(i, stdout, 0);
470 exit(exit_status);
472 callhook(mailname, 0);
473 if (i > 0 && value("emptystart") == NULL)
474 exit(1);
475 if (sigsetjmp(hdrjmp, 1) == 0) {
476 if ((prevint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
477 safe_signal(SIGINT, hdrstop);
478 if (Nflag == 0) {
479 if (value("quiet") == NULL)
480 printf(tr(140,
481 "%s version %s. Type ? for help.\n"),
482 value("bsdcompat") ? "Mail" : uagent,
483 version);
484 announce(1);
485 fflush(stdout);
487 safe_signal(SIGINT, prevint);
489 commands();
490 if (mb.mb_type == MB_FILE || mb.mb_type == MB_MAILDIR) {
491 safe_signal(SIGHUP, SIG_IGN);
492 safe_signal(SIGINT, SIG_IGN);
493 safe_signal(SIGQUIT, SIG_IGN);
495 strncpy(mboxname, expand("&"), sizeof mboxname)[sizeof mboxname-1]='\0';
496 quit();
497 return exit_status;
501 * Interrupt printing of the headers.
503 /*ARGSUSED*/
504 static void
505 hdrstop(int signo)
507 (void)signo;
509 fflush(stdout);
510 fprintf(stderr, tr(141, "\nInterrupt\n"));
511 siglongjmp(hdrjmp, 1);
515 * Compute what the screen size for printing headers should be.
516 * We use the following algorithm for the height:
517 * If baud rate < 1200, use 9
518 * If baud rate = 1200, use 14
519 * If baud rate > 1200, use 24 or ws_row
520 * Width is either 80 or ws_col;
522 /*ARGSUSED*/
523 static void
524 setscreensize(int dummy)
526 struct termios tbuf;
527 #ifdef TIOCGWINSZ
528 struct winsize ws;
529 #endif
530 speed_t ospeed;
531 (void)dummy;
533 #ifdef TIOCGWINSZ
534 if (ioctl(1, TIOCGWINSZ, &ws) < 0)
535 ws.ws_col = ws.ws_row = 0;
536 #endif
537 if (tcgetattr(1, &tbuf) < 0)
538 ospeed = B9600;
539 else
540 ospeed = cfgetospeed(&tbuf);
541 if (ospeed < B1200)
542 scrnheight = 9;
543 else if (ospeed == B1200)
544 scrnheight = 14;
545 #ifdef TIOCGWINSZ
546 else if (ws.ws_row != 0)
547 scrnheight = ws.ws_row;
548 #endif
549 else
550 scrnheight = 24;
551 #ifdef TIOCGWINSZ
552 if ((realscreenheight = ws.ws_row) == 0)
553 realscreenheight = 24;
554 #endif
555 #ifdef TIOCGWINSZ
556 if ((scrnwidth = ws.ws_col) == 0)
557 #endif
558 scrnwidth = 80;