sbr/brkstring.c: replaced static brkany() with call to strchr(); plus cleanups
[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),
83                                                 "%s [addrs ... [switches]]",
84                                                 invo_name);
85                                 print_help(buf, switches, 0);
86                                 done(1);
87                         case VERSIONSW:
88                                 print_version(invo_name);
89                                 done(1);
90
91                         case FROMSW:
92                                 if (!(from = *argp++) || *from == '-')
93                                         adios(NULL, "missing argument to %s",
94                                                         argp[-2]);
95                                 continue;
96
97                         case BODYSW:
98                                 if (!(body = *argp++) || *body == '-')
99                                         adios(NULL, "missing argument to %s",
100                                                         argp[-2]);
101                                 continue;
102
103                         case CCSW:
104                                 iscc++;
105                                 continue;
106
107                         case SUBJSW:
108                                 if (!(subject = *argp++) || *subject == '-')
109                                         adios(NULL, "missing argument to %s",
110                                                         argp[-2]);
111                                 continue;
112
113                         case RESNDSW:
114                                 resent++;
115                                 continue;
116
117                         case QUEUESW:
118                                 queued++;
119                                 continue;
120                         }
121                 }
122                 if (iscc)
123                         cclist = cclist ? add(cp, add(", ", cclist)) :
124                                         getcpy(cp);
125                 else
126                         tolist = tolist ? add(cp, add(", ", tolist)) :
127                                         getcpy(cp);
128         }
129
130         if (tolist == NULL)
131                 adios(NULL, "usage: %s addrs ... [switches]", invo_name);
132
133         tfile = m_mktemp2(NULL, invo_name, NULL, &out);
134         if (tfile == NULL)
135                 adios("mhmail", "unable to create temporary file");
136         chmod(tfile, 0600);
137         strncpy(tmpfil, tfile, sizeof(tmpfil));
138
139         SIGNAL2(SIGINT, intrser);
140
141         fprintf(out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
142         if (cclist)
143                 fprintf(out, "%sCc: %s\n", resent ? "Resent-" : "", cclist);
144         if (subject)
145                 fprintf(out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
146         if (from)
147                 fprintf(out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
148         if (!resent)
149                 fputs("\n", out);
150
151         if (body) {
152                 fprintf(out, "%s", body);
153                 if (*body && *(body + strlen(body) - 1) != '\n')
154                         fputs("\n", out);
155         } else {
156                 for (somebody = 0; (i = fread(buf, sizeof(*buf), sizeof(buf),
157                                 stdin)) > 0; somebody++)
158                         if (fwrite(buf, sizeof(*buf), i, out) != i)
159                                 adios(tmpfil, "error writing");
160                 if (!somebody) {
161                         unlink(tmpfil);
162                         done(1);
163                 }
164         }
165         fclose(out);
166
167         nvec = 0;
168         vec[nvec++] = mhbasename(postproc);
169         vec[nvec++] = tmpfil;
170         if (resent)
171                 vec[nvec++] = "-dist";
172         if (queued)
173                 vec[nvec++] = "-queued";
174         vec[nvec] = NULL;
175
176         for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
177                 sleep(5);
178
179         if (child_id == NOTOK) {
180                 /* report failure and then send it */
181                 adios(NULL, "unable to fork");
182         } else if (child_id) {
183                 /* parent process */
184                 if ((status = pidXwait(child_id, postproc))) {
185                         fprintf(stderr, "Letter saved in dead.letter\n");
186                         execl("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
187                         execl("/usr/bin/mv", "mv", tmpfil, "dead.letter",
188                                         NULL);
189                         perror("mv");
190                         _exit(-1);
191                 }
192                 unlink(tmpfil);
193                 done(status ? 1 : 0);
194         } else {
195                 /* child process */
196                 execvp(postproc, vec);
197                 fprintf(stderr, "unable to exec ");
198                 perror(postproc);
199                 _exit(-1);
200         }
201
202         return 0;  /* dead code to satisfy the compiler */
203 }
204
205
206 static RETSIGTYPE
207 intrser(int i)
208 {
209 #ifndef RELIABLE_SIGNALS
210         if (i)
211                 SIGNAL(i, SIG_IGN);
212 #endif
213
214         unlink(tmpfil);
215         done(i != 0 ? 1 : 0);
216 }
217