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>
18 static struct swit switches[] = {
20 { "bodytext text", 0 },
22 { "cc addrs ...", 0 },
26 { "subject text", 0 },
34 static char tmpfil[BUFSIZ];
39 static void intrser(int);
43 main(int argc, char **argv)
45 int status, iscc = 0, nvec;
46 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
47 char *from = NULL, *body = NULL, **argp, **arguments;
48 char *vec[5], buf[BUFSIZ];
52 setlocale(LC_ALL, "");
53 invo_name = mhbasename(argv[0]);
55 /* Without arguments, exit. */
57 adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
62 arguments = getarguments(invo_name, argc, argv, 0);
65 while ((cp = *argp++)) {
67 switch (smatch(++cp, switches)) {
69 ambigsw(cp, switches);
70 /* sysexits.h EX_USAGE */
73 adios(NULL, "-%s unknown", cp);
76 snprintf(buf, sizeof(buf),
77 "%s addrs... [switches]",
79 print_help(buf, switches, 0);
82 print_version(invo_name);
86 if (!(from = *argp++) || *from == '-')
87 adios(NULL, "missing argument to %s",
92 if (!(body = *argp++) || *body == '-')
93 adios(NULL, "missing argument to %s",
102 if (!(subject = *argp++) || *subject == '-')
103 adios(NULL, "missing argument to %s",
109 cclist = cclist ? add(cp, add(", ", cclist)) :
112 tolist = tolist ? add(cp, add(", ", tolist)) :
117 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
119 tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
121 adios("mhmail", "unable to create temporary file");
123 strncpy(tmpfil, tfile, sizeof(tmpfil));
125 SIGNAL2(SIGINT, intrser);
127 fprintf(out, "To: %s\n", tolist);
129 fprintf(out, "Cc: %s\n", cclist);
131 fprintf(out, "Subject: %s\n", subject);
133 fprintf(out, "From: %s\n", from);
137 fprintf(out, "%s", body);
138 if (*body && body[strlen(body) - 1] != '\n')
143 while (fgets(buf, sizeof buf, stdin)) {
144 if (buf[0]=='.' && buf[1]=='\n') {
145 /* A period alone on a line means EOF. */
149 if (fputs(buf, out) == EOF) {
150 adios(tmpfil, "error writing");
155 adios(NULL, "not sending message with empty body");
161 vec[nvec++] = "spost";
162 vec[nvec++] = tmpfil;
165 if ((status = execprog(*vec, vec))) {
166 /* spost failed, save draft as dead.letter */
169 in = open(tmpfil, O_RDONLY);
170 out = creat("dead.letter", 0600);
171 if (in == -1 || out == -1) {
172 fprintf(stderr, "Letter left at %s.\n",
174 /* sysexits.h exit-status from spost */
175 exit(status ? 1 : 0);
177 cpydata(in, out, tmpfil, "dead.letter");
180 fprintf(stderr, "Letter saved in dead.letter\n");
183 /* sysexits.h exit status from spost */
184 exit(status ? 1 : 0);
192 exit(i != 0 ? 1 : 0);