2 ** mhmail.c -- simple mail program
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/signals.h>
19 static struct swit switches[] = {
21 { "bodytext text", 0 },
23 { "cc addrs ...", 0 },
27 { "subject text", 0 },
35 char *version=VERSION;
37 static char tmpfil[BUFSIZ];
42 static void intrser(int);
46 main(int argc, char **argv)
48 int status, iscc = 0, nvec;
49 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
50 char *from = NULL, *body = NULL, **argp, **arguments;
51 char *vec[5], buf[BUFSIZ];
55 setlocale(LC_ALL, "");
56 invo_name = mhbasename(argv[0]);
58 /* Without arguments, exit. */
60 adios(EX_USAGE, NULL, "no interactive mail shell. Use inc/scan/show instead.");
65 arguments = getarguments(invo_name, argc, argv, 0);
68 while ((cp = *argp++)) {
70 switch (smatch(++cp, switches)) {
72 ambigsw(cp, switches);
75 adios(EX_USAGE, NULL, "-%s unknown", cp);
78 snprintf(buf, sizeof(buf),
79 "%s addrs... [switches]",
81 print_help(buf, switches, 0);
82 exit(argc == 2 ? EX_OK : EX_USAGE);
84 print_version(invo_name);
85 exit(argc == 2 ? EX_OK : EX_USAGE);
88 if (!(from = *argp++) || *from == '-')
89 adios(EX_USAGE, NULL, "missing argument to %s",
94 if (!(body = *argp++) || *body == '-')
95 adios(EX_USAGE, NULL, "missing argument to %s",
104 if (!(subject = *argp++) || *subject == '-')
105 adios(EX_USAGE, NULL, "missing argument to %s",
111 cclist = cclist ? add(cp, add(", ", cclist)) :
114 tolist = tolist ? add(cp, add(", ", tolist)) :
119 adios(EX_USAGE, NULL, "usage: %s addrs ... [switches]", invo_name);
121 tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
123 adios(EX_CANTCREAT, "mhmail", "unable to create temporary file");
125 strncpy(tmpfil, tfile, sizeof(tmpfil));
127 SIGNAL2(SIGINT, intrser);
129 fprintf(out, "To: %s\n", tolist);
131 fprintf(out, "Cc: %s\n", cclist);
133 fprintf(out, "Subject: %s\n", subject);
135 fprintf(out, "From: %s\n", from);
139 fprintf(out, "%s", body);
140 if (*body && body[strlen(body) - 1] != '\n')
145 while (fgets(buf, sizeof buf, stdin)) {
146 if (buf[0]=='.' && buf[1]=='\n') {
147 /* A period alone on a line means EOF. */
151 if (fputs(buf, out) == EOF) {
152 adios(EX_IOERR, tmpfil, "error writing");
157 adios(EX_DATAERR, NULL, "not sending message with empty body");
163 vec[nvec++] = "spost";
164 vec[nvec++] = tmpfil;
167 if ((status = execprog(*vec, vec))) {
168 /* spost failed, save draft as dead.letter */
171 in = open(tmpfil, O_RDONLY);
172 out = creat("dead.letter", 0600);
173 if (in == -1 || out == -1) {
174 fprintf(stderr, "Letter left at %s.\n",
178 cpydata(in, out, tmpfil, "dead.letter");
181 fprintf(stderr, "Letter saved in dead.letter\n");