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);
70 adios(NULL, "-%s unknown", cp);
73 snprintf(buf, sizeof(buf),
74 "%s addrs... [switches]",
76 print_help(buf, switches, 0);
79 print_version(invo_name);
83 if (!(from = *argp++) || *from == '-')
84 adios(NULL, "missing argument to %s",
89 if (!(body = *argp++) || *body == '-')
90 adios(NULL, "missing argument to %s",
99 if (!(subject = *argp++) || *subject == '-')
100 adios(NULL, "missing argument to %s",
106 cclist = cclist ? add(cp, add(", ", cclist)) :
109 tolist = tolist ? add(cp, add(", ", tolist)) :
114 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
116 tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
118 adios("mhmail", "unable to create temporary file");
120 strncpy(tmpfil, tfile, sizeof(tmpfil));
122 SIGNAL2(SIGINT, intrser);
124 fprintf(out, "To: %s\n", tolist);
126 fprintf(out, "Cc: %s\n", cclist);
128 fprintf(out, "Subject: %s\n", subject);
130 fprintf(out, "From: %s\n", from);
134 fprintf(out, "%s", body);
135 if (*body && body[strlen(body) - 1] != '\n')
140 while (fgets(buf, sizeof buf, stdin)) {
141 if (buf[0]=='.' && buf[1]=='\n') {
142 /* A period alone on a line means EOF. */
146 if (fputs(buf, out) == EOF) {
147 adios(tmpfil, "error writing");
152 adios(NULL, "not sending message with empty body");
158 vec[nvec++] = "spost";
159 vec[nvec++] = tmpfil;
162 if ((status = execprog(*vec, vec))) {
163 /* spost failed, save draft as dead.letter */
166 in = open(tmpfil, O_RDONLY);
167 out = creat("dead.letter", 0600);
168 if (in == -1 || out == -1) {
169 fprintf(stderr, "Letter left at %s.\n",
171 done(status ? 1 : 0);
173 cpydata(in, out, tmpfil, "dead.letter");
176 fprintf(stderr, "Letter saved in dead.letter\n");
179 done(status ? 1 : 0);
180 return 0; /* dead code to satisfy the compiler */
188 done(i != 0 ? 1 : 0);