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);
69 /* sysexits.h EX_USAGE */
72 adios(NULL, "-%s unknown", cp);
75 snprintf(buf, sizeof(buf),
76 "%s addrs... [switches]",
78 print_help(buf, switches, 0);
81 print_version(invo_name);
85 if (!(from = *argp++) || *from == '-')
86 adios(NULL, "missing argument to %s",
91 if (!(body = *argp++) || *body == '-')
92 adios(NULL, "missing argument to %s",
101 if (!(subject = *argp++) || *subject == '-')
102 adios(NULL, "missing argument to %s",
108 cclist = cclist ? add(cp, add(", ", cclist)) :
111 tolist = tolist ? add(cp, add(", ", tolist)) :
116 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
118 tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
120 adios("mhmail", "unable to create temporary file");
122 strncpy(tmpfil, tfile, sizeof(tmpfil));
124 SIGNAL2(SIGINT, intrser);
126 fprintf(out, "To: %s\n", tolist);
128 fprintf(out, "Cc: %s\n", cclist);
130 fprintf(out, "Subject: %s\n", subject);
132 fprintf(out, "From: %s\n", from);
136 fprintf(out, "%s", body);
137 if (*body && body[strlen(body) - 1] != '\n')
142 while (fgets(buf, sizeof buf, stdin)) {
143 if (buf[0]=='.' && buf[1]=='\n') {
144 /* A period alone on a line means EOF. */
148 if (fputs(buf, out) == EOF) {
149 adios(tmpfil, "error writing");
154 adios(NULL, "not sending message with empty body");
160 vec[nvec++] = "spost";
161 vec[nvec++] = tmpfil;
164 if ((child_id = fork()) == NOTOK) {
165 /* report failure and then send it */
166 adios(NULL, "unable to fork");
168 } else if (child_id == 0) {
171 fprintf(stderr, "unable to exec ");
177 if ((status = pidXwait(child_id, *vec))) {
178 /* spost failed, save draft as dead.letter */
181 in = open(tmpfil, O_RDONLY);
182 out = creat("dead.letter", 0600);
183 if (in == -1 || out == -1) {
184 fprintf(stderr, "Letter left at %s.\n",
186 /* sysexits.h exit-status from spost */
187 exit(status ? 1 : 0);
189 cpydata(in, out, tmpfil, "dead.letter");
192 fprintf(stderr, "Letter saved in dead.letter\n");
195 /* sysexits.h exit status from spost */
196 exit(status ? 1 : 0);
199 return 0; /* dead code to satisfy the compiler */
207 exit(i != 0 ? 1 : 0);