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)
44 int status, iscc = 0, nvec;
45 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
46 char *from = NULL, *body = NULL, **argp, **arguments;
47 char *vec[5], buf[BUFSIZ];
51 setlocale(LC_ALL, "");
52 invo_name = mhbasename(argv[0]);
54 /* Without arguments, exit. */
56 adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
61 arguments = getarguments(invo_name, argc, argv, 0);
64 while ((cp = *argp++)) {
66 switch (smatch(++cp, switches)) {
68 ambigsw(cp, switches);
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 ((child_id = fork()) == NOTOK) {
164 /* report failure and then send it */
165 adios(NULL, "unable to fork");
167 } else if (child_id == 0) {
170 fprintf(stderr, "unable to exec ");
176 if ((status = pidXwait(child_id, *vec))) {
177 /* spost failed, save draft as dead.letter */
180 in = open(tmpfil, O_RDONLY);
181 out = creat("dead.letter", 0600);
182 if (in == -1 || out == -1) {
183 fprintf(stderr, "Letter left at %s.\n",
185 done(status ? 1 : 0);
187 cpydata(in, out, tmpfil, "dead.letter");
190 fprintf(stderr, "Letter saved in dead.letter\n");
193 done(status ? 1 : 0);
196 return 0; /* dead code to satisfy the compiler */
204 done(i != 0 ? 1 : 0);