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