mhmail: Read the context!
[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                                 done(1);
70                         case UNKWNSW:
71                                 adios(NULL, "-%s unknown", cp);
72
73                         case HELPSW:
74                                 snprintf(buf, sizeof(buf),
75                                                 "%s addrs... [switches]",
76                                                 invo_name);
77                                 print_help(buf, switches, 0);
78                                 done(1);
79                         case VERSIONSW:
80                                 print_version(invo_name);
81                                 done(1);
82
83                         case FROMSW:
84                                 if (!(from = *argp++) || *from == '-')
85                                         adios(NULL, "missing argument to %s",
86                                                         argp[-2]);
87                                 continue;
88
89                         case BODYSW:
90                                 if (!(body = *argp++) || *body == '-')
91                                         adios(NULL, "missing argument to %s",
92                                                         argp[-2]);
93                                 continue;
94
95                         case CCSW:
96                                 iscc++;
97                                 continue;
98
99                         case SUBJSW:
100                                 if (!(subject = *argp++) || *subject == '-')
101                                         adios(NULL, "missing argument to %s",
102                                                         argp[-2]);
103                                 continue;
104                         }
105                 }
106                 if (iscc)
107                         cclist = cclist ? add(cp, add(", ", cclist)) :
108                                         getcpy(cp);
109                 else
110                         tolist = tolist ? add(cp, add(", ", tolist)) :
111                                         getcpy(cp);
112         }
113
114         if (tolist == NULL)
115                 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
116
117         tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
118         if (tfile == NULL)
119                 adios("mhmail", "unable to create temporary file");
120         chmod(tfile, 0600);
121         strncpy(tmpfil, tfile, sizeof(tmpfil));
122
123         SIGNAL2(SIGINT, intrser);
124
125         fprintf(out, "To: %s\n", tolist);
126         if (cclist)
127                 fprintf(out, "Cc: %s\n", cclist);
128         if (subject)
129                 fprintf(out, "Subject: %s\n", subject);
130         if (from)
131                 fprintf(out, "From: %s\n", from);
132         fputs("\n", out);
133
134         if (body) {
135                 fprintf(out, "%s", body);
136                 if (*body && body[strlen(body) - 1] != '\n')
137                         fputs("\n", out);
138         } else {
139                 int empty = 1;
140
141                 while (fgets(buf, sizeof buf, stdin)) {
142                         if (buf[0]=='.' && buf[1]=='\n') {
143                                 /* A period alone on a line means EOF. */
144                                 break;
145                         }
146                         empty = 0;
147                         if (fputs(buf, out) == EOF) {
148                                 adios(tmpfil, "error writing");
149                         }
150                 }
151                 if (empty) {
152                         unlink(tmpfil);
153                         adios(NULL, "not sending message with empty body");
154                 }
155         }
156         fclose(out);
157
158         nvec = 0;
159         vec[nvec++] = "spost";
160         vec[nvec++] = tmpfil;
161         vec[nvec] = NULL;
162
163         if ((child_id = fork()) == NOTOK) {
164                 /* report failure and then send it */
165                 adios(NULL, "unable to fork");
166
167         } else if (child_id == 0) {
168                 /* child process */
169                 execvp(*vec, vec);
170                 fprintf(stderr, "unable to exec ");
171                 perror(*vec);
172                 _exit(-1);
173
174         } else {
175                 /* parent process */
176                 if ((status = pidXwait(child_id, *vec))) {
177                         /* spost failed, save draft as dead.letter */
178                         int in, out;
179
180                         in = open(tmpfil, O_RDONLY);
181                         out = creat("dead.letter", 0600);
182                         if (in == -1 || out == -1) {
183                                 fprintf(stderr, "Letter left at %s.\n",
184                                                 tmpfil);
185                                 done(status ? 1 : 0);
186                         }
187                         cpydata(in, out, tmpfil, "dead.letter");
188                         close(in);
189                         close(out);
190                         fprintf(stderr, "Letter saved in dead.letter\n");
191                 }
192                 unlink(tmpfil);
193                 done(status ? 1 : 0);
194         }
195
196         return 0;  /* dead code to satisfy the compiler */
197 }
198
199
200 static void
201 intrser(int i)
202 {
203         unlink(tmpfil);
204         done(i != 0 ? 1 : 0);
205 }
206