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 /* foil search of user profile/context */
55 if (context_foil(NULL) == -1)
58 /* Without arguments, exit. */
60 adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
63 arguments = getarguments(invo_name, argc, argv, 0);
66 while ((cp = *argp++)) {
68 switch (smatch(++cp, switches)) {
70 ambigsw(cp, switches);
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 ((child_id = fork()) == NOTOK) {
166 /* report failure and then send it */
167 adios(NULL, "unable to fork");
169 } else if (child_id == 0) {
172 fprintf(stderr, "unable to exec ");
178 if ((status = pidXwait(child_id, *vec))) {
179 /* spost failed, save draft as dead.letter */
182 in = open(tmpfil, O_RDONLY);
183 out = creat("dead.letter", 0600);
184 if (in == -1 || out == -1) {
185 fprintf(stderr, "Letter left at %s.\n",
187 done(status ? 1 : 0);
189 cpydata(in, out, tmpfil, "dead.letter");
192 fprintf(stderr, "Letter saved in dead.letter\n");
195 done(status ? 1 : 0);
198 return 0; /* dead code to satisfy the compiler */
206 done(i != 0 ? 1 : 0);