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[] = {
20 { "cc addrs ...", 0 },
24 { "subject text", 0 },
36 static char tmpfil[BUFSIZ];
41 static void intrser(int);
45 main(int argc, char **argv)
48 int status, iscc = 0, nvec;
50 int queued = 0, resent = 0, somebody;
51 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
52 char *from = NULL, *body = NULL, **argp, **arguments;
53 char *vec[5], buf[BUFSIZ];
58 setlocale(LC_ALL, "");
60 invo_name = mhbasename(argv[0]);
62 /* foil search of user profile/context */
63 if (context_foil(NULL) == -1)
66 /* Without arguments, exit. */
68 adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
71 arguments = getarguments(invo_name, argc, argv, 0);
74 while ((cp = *argp++)) {
76 switch (smatch(++cp, switches)) {
78 ambigsw(cp, switches);
81 adios(NULL, "-%s unknown", cp);
84 snprintf(buf, sizeof(buf),
85 "%s addrs... [switches]",
87 print_help(buf, switches, 0);
90 print_version(invo_name);
94 if (!(from = *argp++) || *from == '-')
95 adios(NULL, "missing argument to %s",
100 if (!(body = *argp++) || *body == '-')
101 adios(NULL, "missing argument to %s",
110 if (!(subject = *argp++) || *subject == '-')
111 adios(NULL, "missing argument to %s",
125 cclist = cclist ? add(cp, add(", ", cclist)) :
128 tolist = tolist ? add(cp, add(", ", tolist)) :
133 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
135 tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
137 adios("mhmail", "unable to create temporary file");
139 strncpy(tmpfil, tfile, sizeof(tmpfil));
141 SIGNAL2(SIGINT, intrser);
143 fprintf(out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
145 fprintf(out, "%sCc: %s\n", resent ? "Resent-" : "", cclist);
147 fprintf(out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
149 fprintf(out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
154 fprintf(out, "%s", body);
155 if (*body && *(body + strlen(body) - 1) != '\n')
158 for (somebody = 0; (i = fread(buf, sizeof(*buf), sizeof(buf),
159 stdin)) > 0; somebody++)
160 if (fwrite(buf, sizeof(*buf), i, out) != i)
161 adios(tmpfil, "error writing");
170 vec[nvec++] = "spost";
171 vec[nvec++] = tmpfil;
173 vec[nvec++] = "-dist";
175 vec[nvec++] = "-queued";
178 if ((child_id = fork()) == NOTOK) {
179 /* report failure and then send it */
180 adios(NULL, "unable to fork");
182 } else if (child_id == 0) {
185 fprintf(stderr, "unable to exec ");
191 if ((status = pidXwait(child_id, *vec))) {
192 /* spost failed, save draft as dead.letter */
195 in = open(tmpfil, O_RDONLY);
196 out = creat("dead.letter", 0600);
197 if (in == -1 || out == -1) {
198 fprintf(stderr, "Letter left at %s.\n",
200 done(status ? 1 : 0);
202 cpydata(in, out, tmpfil, "dead.letter");
205 fprintf(stderr, "Letter saved in dead.letter\n");
208 done(status ? 1 : 0);
211 return 0; /* dead code to satisfy the compiler */
219 done(i != 0 ? 1 : 0);