2ab9cfd96b0c14fae712f8d495730c474e0ebee8
[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         { "body 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 #define RESNDSW  6
30         { "resent", -6 },
31 #define QUEUESW  7
32         { "queued", -6 },
33         { NULL, 0 }
34 };
35
36 static char tmpfil[BUFSIZ];
37
38 /*
39 ** static prototypes
40 */
41 static void intrser(int);
42
43
44 int
45 main(int argc, char **argv)
46 {
47         pid_t child_id;
48         int status, iscc = 0, nvec;
49         unsigned int i;
50         int queued = 0, resent = 0, somebody;
51         char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
52         char *from = NULL, *body = NULL, **argp, **arguments;
53         char *vec[5], buf[BUFSIZ];
54         FILE *out;
55         char *tfile = NULL;
56
57 #ifdef LOCALE
58         setlocale(LC_ALL, "");
59 #endif
60         invo_name = mhbasename(argv[0]);
61
62         /* foil search of user profile/context */
63         if (context_foil(NULL) == -1)
64                 done(1);
65
66         /* Without arguments, exit. */
67         if (argc == 1) {
68                 adios(NULL, "no interactive mail shell. Use inc/scan/show instead.");
69         }
70
71         arguments = getarguments(invo_name, argc, argv, 0);
72         argp = arguments;
73
74         while ((cp = *argp++)) {
75                 if (*cp == '-') {
76                         switch (smatch(++cp, switches)) {
77                         case AMBIGSW:
78                                 ambigsw(cp, switches);
79                                 done(1);
80                         case UNKWNSW:
81                                 adios(NULL, "-%s unknown", cp);
82
83                         case HELPSW:
84                                 snprintf(buf, sizeof(buf),
85                                                 "%s addrs... [switches]",
86                                                 invo_name);
87                                 print_help(buf, switches, 0);
88                                 done(1);
89                         case VERSIONSW:
90                                 print_version(invo_name);
91                                 done(1);
92
93                         case FROMSW:
94                                 if (!(from = *argp++) || *from == '-')
95                                         adios(NULL, "missing argument to %s",
96                                                         argp[-2]);
97                                 continue;
98
99                         case BODYSW:
100                                 if (!(body = *argp++) || *body == '-')
101                                         adios(NULL, "missing argument to %s",
102                                                         argp[-2]);
103                                 continue;
104
105                         case CCSW:
106                                 iscc++;
107                                 continue;
108
109                         case SUBJSW:
110                                 if (!(subject = *argp++) || *subject == '-')
111                                         adios(NULL, "missing argument to %s",
112                                                         argp[-2]);
113                                 continue;
114
115                         case RESNDSW:
116                                 resent++;
117                                 continue;
118
119                         case QUEUESW:
120                                 queued++;
121                                 continue;
122                         }
123                 }
124                 if (iscc)
125                         cclist = cclist ? add(cp, add(", ", cclist)) :
126                                         getcpy(cp);
127                 else
128                         tolist = tolist ? add(cp, add(", ", tolist)) :
129                                         getcpy(cp);
130         }
131
132         if (tolist == NULL)
133                 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
134
135         tfile = m_mktemp2("/tmp/", invo_name, NULL, &out);
136         if (tfile == NULL)
137                 adios("mhmail", "unable to create temporary file");
138         chmod(tfile, 0600);
139         strncpy(tmpfil, tfile, sizeof(tmpfil));
140
141         SIGNAL2(SIGINT, intrser);
142
143         fprintf(out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
144         if (cclist)
145                 fprintf(out, "%sCc: %s\n", resent ? "Resent-" : "", cclist);
146         if (subject)
147                 fprintf(out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
148         if (from)
149                 fprintf(out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
150         if (!resent)
151                 fputs("\n", out);
152
153         if (body) {
154                 fprintf(out, "%s", body);
155                 if (*body && *(body + strlen(body) - 1) != '\n')
156                         fputs("\n", out);
157         } else {
158                 for (somebody = 0; (i = fread(buf, sizeof(*buf), sizeof(buf),
159                                 stdin)) > 0; somebody++)
160                         if (fwrite(buf, sizeof(*buf), i, out) != i)
161                                 adios(tmpfil, "error writing");
162                 if (!somebody) {
163                         unlink(tmpfil);
164                         done(1);
165                 }
166         }
167         fclose(out);
168
169         nvec = 0;
170         vec[nvec++] = "spost";
171         vec[nvec++] = tmpfil;
172         if (resent)
173                 vec[nvec++] = "-dist";
174         if (queued)
175                 vec[nvec++] = "-queued";
176         vec[nvec] = NULL;
177
178         if ((child_id = fork()) == NOTOK) {
179                 /* report failure and then send it */
180                 adios(NULL, "unable to fork");
181
182         } else if (child_id == 0) {
183                 /* child process */
184                 execvp(*vec, vec);
185                 fprintf(stderr, "unable to exec ");
186                 perror(*vec);
187                 _exit(-1);
188
189         } else {
190                 /* parent process */
191                 if ((status = pidXwait(child_id, *vec))) {
192                         /* spost failed, save draft as dead.letter */
193                         int in, out;
194
195                         in = open(tmpfil, O_RDONLY);
196                         out = creat("dead.letter", 0600);
197                         if (in == -1 || out == -1) {
198                                 fprintf(stderr, "Letter left at %s.\n",
199                                                 tmpfil);
200                                 done(status ? 1 : 0);
201                         }
202                         cpydata(in, out, tmpfil, "dead.letter");
203                         close(in);
204                         close(out);
205                         fprintf(stderr, "Letter saved in dead.letter\n");
206                 }
207                 unlink(tmpfil);
208                 done(status ? 1 : 0);
209         }
210
211         return 0;  /* dead code to satisfy the compiler */
212 }
213
214
215 static void
216 intrser(int i)
217 {
218         unlink(tmpfil);
219         done(i != 0 ? 1 : 0);
220 }
221