Renames: s/+outbox/+sent/ and s/cc:/Cc:/
[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
14 static struct swit switches[] = {
15 #define BODYSW  0
16         { "body text", 0 },
17 #define CCSW  1
18         { "cc addrs ...", 0 },
19 #define FROMSW  2
20         { "from addr", 0 },
21 #define SUBJSW  3
22         { "subject text", 0 },
23 #define VERSIONSW  4
24         { "version", 0 },
25 #define HELPSW  5
26         { "help", 0 },
27 #define RESNDSW  6
28         { "resent", -6 },
29 #define QUEUESW  7
30         { "queued", -6 },
31         { NULL, 0 }
32 };
33
34 static char tmpfil[BUFSIZ];
35
36 /*
37 ** static prototypes
38 */
39 static RETSIGTYPE intrser(int);
40
41
42 int
43 main(int argc, char **argv)
44 {
45         pid_t child_id;
46         int status, i, iscc = 0, nvec;
47         int queued = 0, resent = 0, somebody;
48         char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
49         char *from = NULL, *body = NULL, **argp, **arguments;
50         char *vec[5], buf[BUFSIZ];
51         FILE *out;
52         char *tfile = NULL;
53
54 #ifdef LOCALE
55         setlocale(LC_ALL, "");
56 #endif
57         invo_name = mhbasename(argv[0]);
58
59         /* foil search of user profile/context */
60         if (context_foil(NULL) == -1)
61                 done(1);
62
63         /* If no arguments, just incorporate new mail */
64         if (argc == 1) {
65                 execlp(incproc, mhbasename(incproc), NULL);
66                 adios(incproc, "unable to exec");
67         }
68
69         arguments = getarguments(invo_name, argc, argv, 0);
70         argp = arguments;
71
72         while ((cp = *argp++)) {
73                 if (*cp == '-') {
74                         switch (smatch(++cp, switches)) {
75                                 case AMBIGSW:
76                                         ambigsw(cp, switches);
77                                         done(1);
78                                 case UNKWNSW:
79                                         adios(NULL, "-%s unknown", cp);
80
81                                 case HELPSW:
82                                         snprintf(buf, sizeof(buf), "%s [addrs ... [switches]]", 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", argp[-2]);
92                                         continue;
93
94                                 case BODYSW:
95                                         if (!(body = *argp++) || *body == '-')
96                                                 adios(NULL, "missing argument to %s", 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", argp[-2]);
106                                         continue;
107
108                                 case RESNDSW:
109                                         resent++;
110                                         continue;
111
112                                 case QUEUESW:
113                                         queued++;
114                                         continue;
115                         }
116                 }
117                 if (iscc)
118                         cclist = cclist ? add(cp, add(", ", cclist)) :
119                                         getcpy(cp);
120                 else
121                         tolist = tolist ? add(cp, add(", ", tolist)) :
122                                         getcpy(cp);
123         }
124
125         if (tolist == NULL)
126                 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
127
128         tfile = m_mktemp2(NULL, invo_name, NULL, &out);
129         if (tfile == NULL)
130                 adios("mhmail", "unable to create temporary file");
131         chmod(tfile, 0600);
132         strncpy(tmpfil, tfile, sizeof(tmpfil));
133
134         SIGNAL2(SIGINT, intrser);
135
136         fprintf(out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
137         if (cclist)
138                 fprintf(out, "%sCc: %s\n", resent ? "Resent-" : "", cclist);
139         if (subject)
140                 fprintf(out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
141         if (from)
142                 fprintf(out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
143         if (!resent)
144                 fputs("\n", out);
145
146         if (body) {
147                 fprintf(out, "%s", body);
148                 if (*body && *(body + strlen(body) - 1) != '\n')
149                         fputs("\n", out);
150         } else {
151                 for (somebody = 0; (i = fread(buf, sizeof(*buf), sizeof(buf),
152                                 stdin)) > 0; somebody++)
153                         if (fwrite(buf, sizeof(*buf), i, out) != i)
154                                 adios(tmpfil, "error writing");
155                 if (!somebody) {
156                         unlink(tmpfil);
157                         done(1);
158                 }
159         }
160         fclose(out);
161
162         nvec = 0;
163         vec[nvec++] = mhbasename(postproc);
164         vec[nvec++] = tmpfil;
165         if (resent)
166                 vec[nvec++] = "-dist";
167         if (queued)
168                 vec[nvec++] = "-queued";
169         vec[nvec] = NULL;
170
171         for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
172                 sleep(5);
173
174         if (child_id == NOTOK) {
175                 /* report failure and then send it */
176                 adios(NULL, "unable to fork");
177         } else if (child_id) {
178                 /* parent process */
179                 if ((status = pidXwait(child_id, postproc))) {
180                         fprintf(stderr, "Letter saved in dead.letter\n");
181                         execl("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
182                         execl("/usr/bin/mv", "mv", tmpfil, "dead.letter",
183                                         NULL);
184                         perror("mv");
185                         _exit(-1);
186                 }
187                 unlink(tmpfil);
188                 done(status ? 1 : 0);
189         } else {
190                 /* child process */
191                 execvp(postproc, vec);
192                 fprintf(stderr, "unable to exec ");
193                 perror(postproc);
194                 _exit(-1);
195         }
196
197         return 0;  /* dead code to satisfy the compiler */
198 }
199
200
201 static RETSIGTYPE
202 intrser(int i)
203 {
204 #ifndef RELIABLE_SIGNALS
205         if (i)
206                 SIGNAL(i, SIG_IGN);
207 #endif
208
209         unlink(tmpfil);
210         done(i != 0 ? 1 : 0);
211 }
212