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