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>
16 static struct swit switches[] = {
18 { "bodytext text", 0 },
20 { "cc addrs ...", 0 },
24 { "subject text", 0 },
32 static char tmpfil[BUFSIZ];
37 static void intrser(int);
41 main(int argc, char **argv)
43 int status, iscc = 0, nvec;
44 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
45 char *from = NULL, *body = NULL, **argp, **arguments;
46 char *vec[5], buf[BUFSIZ];
50 setlocale(LC_ALL, "");
51 invo_name = mhbasename(argv[0]);
53 /* Without arguments, exit. */
55 adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
60 arguments = getarguments(invo_name, argc, argv, 0);
63 while ((cp = *argp++)) {
65 switch (smatch(++cp, switches)) {
67 ambigsw(cp, switches);
68 /* sysexits.h EX_USAGE */
71 adios(NULL, "-%s unknown", cp);
74 snprintf(buf, sizeof(buf),
75 "%s addrs... [switches]",
77 print_help(buf, switches, 0);
80 print_version(invo_name);
84 if (!(from = *argp++) || *from == '-')
85 adios(NULL, "missing argument to %s",
90 if (!(body = *argp++) || *body == '-')
91 adios(NULL, "missing argument to %s",
100 if (!(subject = *argp++) || *subject == '-')
101 adios(NULL, "missing argument to %s",
107 cclist = cclist ? add(cp, add(", ", cclist)) :
110 tolist = tolist ? add(cp, add(", ", tolist)) :
115 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
117 tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
119 adios("mhmail", "unable to create temporary file");
121 strncpy(tmpfil, tfile, sizeof(tmpfil));
123 SIGNAL2(SIGINT, intrser);
125 fprintf(out, "To: %s\n", tolist);
127 fprintf(out, "Cc: %s\n", cclist);
129 fprintf(out, "Subject: %s\n", subject);
131 fprintf(out, "From: %s\n", from);
135 fprintf(out, "%s", body);
136 if (*body && body[strlen(body) - 1] != '\n')
141 while (fgets(buf, sizeof buf, stdin)) {
142 if (buf[0]=='.' && buf[1]=='\n') {
143 /* A period alone on a line means EOF. */
147 if (fputs(buf, out) == EOF) {
148 adios(tmpfil, "error writing");
153 adios(NULL, "not sending message with empty body");
159 vec[nvec++] = "spost";
160 vec[nvec++] = tmpfil;
163 if ((status = execprog(*vec, vec))) {
164 /* spost failed, save draft as dead.letter */
167 in = open(tmpfil, O_RDONLY);
168 out = creat("dead.letter", 0600);
169 if (in == -1 || out == -1) {
170 fprintf(stderr, "Letter left at %s.\n",
172 /* sysexits.h exit-status from spost */
173 exit(status ? 1 : 0);
175 cpydata(in, out, tmpfil, "dead.letter");
178 fprintf(stderr, "Letter saved in dead.letter\n");
181 /* sysexits.h exit status from spost */
182 exit(status ? 1 : 0);
190 exit(i != 0 ? 1 : 0);