distout and ready_msg return NOTOK on error
[mmh] / uip / mhmail.c
1 /*
2 ** mhmail.c -- simple mail program
3 **
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.
7 */
8
9 #include <h/mh.h>
10 #include <h/signals.h>
11 #include <h/utils.h>
12 #include <signal.h>
13 #include <fcntl.h>
14
15
16 static struct swit switches[] = {
17 #define BODYSW  0
18         { "bodytext text", 0 },
19 #define CCSW  1
20         { "cc addrs ...", 0 },
21 #define FROMSW  2
22         { "from addr", 0 },
23 #define SUBJSW  3
24         { "subject text", 0 },
25 #define VERSIONSW  4
26         { "Version", 0 },
27 #define HELPSW  5
28         { "help", 0 },
29         { NULL, 0 }
30 };
31
32 static char tmpfil[BUFSIZ];
33
34 /*
35 ** static prototypes
36 */
37 static void intrser(int);
38
39
40 int
41 main(int argc, char **argv)
42 {
43         pid_t child_id;
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];
48         FILE *out;
49         char *tfile = NULL;
50
51         setlocale(LC_ALL, "");
52         invo_name = mhbasename(argv[0]);
53
54         /* Without arguments, exit. */
55         if (argc == 1) {
56                 adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
57         }
58
59         context_read();
60
61         arguments = getarguments(invo_name, argc, argv, 0);
62         argp = arguments;
63
64         while ((cp = *argp++)) {
65                 if (*cp == '-') {
66                         switch (smatch(++cp, switches)) {
67                         case AMBIGSW:
68                                 ambigsw(cp, switches);
69                                 /* sysexits.h EX_USAGE */
70                                 exit(1);
71                         case UNKWNSW:
72                                 adios(NULL, "-%s unknown", cp);
73
74                         case HELPSW:
75                                 snprintf(buf, sizeof(buf),
76                                                 "%s addrs... [switches]",
77                                                 invo_name);
78                                 print_help(buf, switches, 0);
79                                 exit(0);
80                         case VERSIONSW:
81                                 print_version(invo_name);
82                                 exit(0);
83
84                         case FROMSW:
85                                 if (!(from = *argp++) || *from == '-')
86                                         adios(NULL, "missing argument to %s",
87                                                         argp[-2]);
88                                 continue;
89
90                         case BODYSW:
91                                 if (!(body = *argp++) || *body == '-')
92                                         adios(NULL, "missing argument to %s",
93                                                         argp[-2]);
94                                 continue;
95
96                         case CCSW:
97                                 iscc++;
98                                 continue;
99
100                         case SUBJSW:
101                                 if (!(subject = *argp++) || *subject == '-')
102                                         adios(NULL, "missing argument to %s",
103                                                         argp[-2]);
104                                 continue;
105                         }
106                 }
107                 if (iscc)
108                         cclist = cclist ? add(cp, add(", ", cclist)) :
109                                         getcpy(cp);
110                 else
111                         tolist = tolist ? add(cp, add(", ", tolist)) :
112                                         getcpy(cp);
113         }
114
115         if (tolist == NULL)
116                 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
117
118         tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
119         if (tfile == NULL)
120                 adios("mhmail", "unable to create temporary file");
121         chmod(tfile, 0600);
122         strncpy(tmpfil, tfile, sizeof(tmpfil));
123
124         SIGNAL2(SIGINT, intrser);
125
126         fprintf(out, "To: %s\n", tolist);
127         if (cclist)
128                 fprintf(out, "Cc: %s\n", cclist);
129         if (subject)
130                 fprintf(out, "Subject: %s\n", subject);
131         if (from)
132                 fprintf(out, "From: %s\n", from);
133         fputs("\n", out);
134
135         if (body) {
136                 fprintf(out, "%s", body);
137                 if (*body && body[strlen(body) - 1] != '\n')
138                         fputs("\n", out);
139         } else {
140                 int empty = 1;
141
142                 while (fgets(buf, sizeof buf, stdin)) {
143                         if (buf[0]=='.' && buf[1]=='\n') {
144                                 /* A period alone on a line means EOF. */
145                                 break;
146                         }
147                         empty = 0;
148                         if (fputs(buf, out) == EOF) {
149                                 adios(tmpfil, "error writing");
150                         }
151                 }
152                 if (empty) {
153                         unlink(tmpfil);
154                         adios(NULL, "not sending message with empty body");
155                 }
156         }
157         fclose(out);
158
159         nvec = 0;
160         vec[nvec++] = "spost";
161         vec[nvec++] = tmpfil;
162         vec[nvec] = NULL;
163
164         if ((child_id = fork()) == NOTOK) {
165                 /* report failure and then send it */
166                 adios(NULL, "unable to fork");
167
168         } else if (child_id == 0) {
169                 /* child process */
170                 execvp(*vec, vec);
171                 fprintf(stderr, "unable to exec ");
172                 perror(*vec);
173                 _exit(-1);
174
175         } else {
176                 /* parent process */
177                 if ((status = pidXwait(child_id, *vec))) {
178                         /* spost failed, save draft as dead.letter */
179                         int in, out;
180
181                         in = open(tmpfil, O_RDONLY);
182                         out = creat("dead.letter", 0600);
183                         if (in == -1 || out == -1) {
184                                 fprintf(stderr, "Letter left at %s.\n",
185                                                 tmpfil);
186                                 /* sysexits.h exit-status from spost */
187                                 exit(status ? 1 : 0);
188                         }
189                         cpydata(in, out, tmpfil, "dead.letter");
190                         close(in);
191                         close(out);
192                         fprintf(stderr, "Letter saved in dead.letter\n");
193                 }
194                 unlink(tmpfil);
195                 /* sysexits.h exit status from spost */
196                 exit(status ? 1 : 0);
197         }
198
199         return 0;  /* dead code to satisfy the compiler */
200 }
201
202
203 static void
204 intrser(int i)
205 {
206         unlink(tmpfil);
207         exit(i != 0 ? 1 : 0);
208 }
209