3 * mhmail.c -- simple mail program
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #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 = r1bindex (argv[0], '/');
62 /* foil search of user profile/context */
63 if (context_foil (NULL) == -1)
66 /* If no arguments, just incorporate new mail */
68 execlp (incproc, r1bindex (incproc, '/'), NULL);
69 adios (incproc, "unable to exec");
72 arguments = getarguments (invo_name, argc, argv, 0);
75 while ((cp = *argp++)) {
77 switch (smatch (++cp, switches)) {
79 ambigsw (cp, switches);
82 adios (NULL, "-%s unknown", cp);
85 snprintf (buf, sizeof(buf), "%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", argp[-2]);
99 if (!(body = *argp++) || *body == '-')
100 adios (NULL, "missing argument to %s", argp[-2]);
108 if (!(subject = *argp++) || *subject == '-')
109 adios (NULL, "missing argument to %s", argp[-2]);
122 cclist = cclist ? add (cp, add (", ", cclist)) : getcpy (cp);
124 tolist = tolist ? add (cp, add (", ", tolist)) : getcpy (cp);
128 adios (NULL, "usage: %s addrs ... [switches]", invo_name);
130 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
131 if (tfile == NULL) adios("mhmail", "unable to create temporary file");
133 strncpy (tmpfil, tfile, sizeof(tmpfil));
135 SIGNAL2 (SIGINT, intrser);
137 fprintf (out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
139 fprintf (out, "%scc: %s\n", resent ? "Resent-" : "", cclist);
141 fprintf (out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
143 fprintf (out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
145 fprintf (out, "From: %s\n", getlocalmbox ());
150 fprintf (out, "%s", body);
151 if (*body && *(body + strlen (body) - 1) != '\n')
155 (i = fread (buf, sizeof(*buf), sizeof(buf), stdin)) > 0;
157 if (fwrite (buf, sizeof(*buf), i, out) != i)
158 adios (tmpfil, "error writing");
167 vec[nvec++] = r1bindex (postproc, '/');
168 vec[nvec++] = tmpfil;
170 vec[nvec++] = "-dist";
172 vec[nvec++] = "-queued";
175 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
178 if (child_id == NOTOK) {
179 /* report failure and then send it */
180 adios (NULL, "unable to fork");
181 } else if (child_id) {
183 if ((status = pidXwait(child_id, postproc))) {
184 fprintf (stderr, "Letter saved in dead.letter\n");
185 execl ("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
186 execl ("/usr/bin/mv", "mv", tmpfil, "dead.letter", NULL);
191 done (status ? 1 : 0);
194 execvp (postproc, vec);
195 fprintf (stderr, "unable to exec ");
200 return 0; /* dead code to satisfy the compiler */
208 done (i != 0 ? 1 : 0);