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>
19 static struct swit switches[] = {
21 { "bodytext text", 0 },
23 { "cc addrs ...", 0 },
27 { "subject text", 0 },
35 static char tmpfil[BUFSIZ];
40 static void intrser(int);
44 main(int argc, char **argv)
46 int status, iscc = 0, nvec;
47 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
48 char *from = NULL, *body = NULL, **argp, **arguments;
49 char *vec[5], buf[BUFSIZ];
53 setlocale(LC_ALL, "");
54 invo_name = mhbasename(argv[0]);
56 /* Without arguments, exit. */
58 adios(EX_USAGE, 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(EX_USAGE, NULL, "-%s unknown", cp);
76 snprintf(buf, sizeof(buf),
77 "%s addrs... [switches]",
79 print_help(buf, switches, 0);
80 exit(argc == 2 ? EX_OK : EX_USAGE);
82 print_version(invo_name);
83 exit(argc == 2 ? EX_OK : EX_USAGE);
86 if (!(from = *argp++) || *from == '-')
87 adios(EX_USAGE, NULL, "missing argument to %s",
92 if (!(body = *argp++) || *body == '-')
93 adios(EX_USAGE, NULL, "missing argument to %s",
102 if (!(subject = *argp++) || *subject == '-')
103 adios(EX_USAGE, NULL, "missing argument to %s",
109 cclist = cclist ? add(cp, add(", ", cclist)) :
112 tolist = tolist ? add(cp, add(", ", tolist)) :
117 adios(EX_USAGE, NULL, "usage: %s addrs ... [switches]", invo_name);
119 tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
121 adios(EX_CANTCREAT, "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(EX_IOERR, tmpfil, "error writing");
155 adios(EX_DATAERR, 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",
176 cpydata(in, out, tmpfil, "dead.letter");
179 fprintf(stderr, "Letter saved in dead.letter\n");