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