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