]> diplodocus.org Git - nmh/blob - uip/mhmail.c
Bring these changes over from the branch.
[nmh] / uip / mhmail.c
1
2 /*
3 * mhmail.c -- simple mail program
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/signals.h>
14 #include <signal.h>
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 #define RESNDSW 6
30 { "resent", -6 },
31 #define QUEUESW 7
32 { "queued", -6 },
33 { NULL, 0 }
34 };
35
36 static char tmpfil[BUFSIZ];
37
38 /*
39 * static prototypes
40 */
41 static RETSIGTYPE intrser (int);
42
43
44 int
45 main (int argc, char **argv)
46 {
47 pid_t child_id;
48 int status, i, iscc = 0, nvec;
49 int queued = 0, resent = 0, somebody;
50 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
51 char *from = NULL, *body = NULL, **argp, **arguments;
52 char *vec[5], buf[BUFSIZ];
53 FILE *out;
54
55 #ifdef LOCALE
56 setlocale(LC_ALL, "");
57 #endif
58 invo_name = r1bindex (argv[0], '/');
59
60 /* foil search of user profile/context */
61 if (context_foil (NULL) == -1)
62 done (1);
63
64 /* If no arguments, just incorporate new mail */
65 if (argc == 1) {
66 execlp (incproc, r1bindex (incproc, '/'), NULL);
67 adios (incproc, "unable to exec");
68 }
69
70 arguments = getarguments (invo_name, argc, argv, 0);
71 argp = arguments;
72
73 while ((cp = *argp++)) {
74 if (*cp == '-') {
75 switch (smatch (++cp, switches)) {
76 case AMBIGSW:
77 ambigsw (cp, switches);
78 done (1);
79 case UNKWNSW:
80 adios (NULL, "-%s unknown", cp);
81
82 case HELPSW:
83 snprintf (buf, sizeof(buf), "%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", argp[-2]);
94 continue;
95
96 case BODYSW:
97 if (!(body = *argp++) || *body == '-')
98 adios (NULL, "missing argument to %s", 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", argp[-2]);
108 continue;
109
110 case RESNDSW:
111 resent++;
112 continue;
113
114 case QUEUESW:
115 queued++;
116 continue;
117 }
118 }
119 if (iscc)
120 cclist = cclist ? add (cp, add (", ", cclist)) : getcpy (cp);
121 else
122 tolist = tolist ? add (cp, add (", ", tolist)) : getcpy (cp);
123 }
124
125 if (tolist == NULL)
126 adios (NULL, "usage: %s addrs ... [switches]", invo_name);
127 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
128 if ((out = fopen (tmpfil, "w")) == NULL)
129 adios (tmpfil, "unable to write");
130 chmod (tmpfil, 0600);
131
132 SIGNAL2 (SIGINT, intrser);
133
134 fprintf (out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
135 if (cclist)
136 fprintf (out, "%scc: %s\n", resent ? "Resent-" : "", cclist);
137 if (subject)
138 fprintf (out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
139 if (from)
140 fprintf (out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
141 if (!resent)
142 fputs ("\n", out);
143
144 if (body) {
145 fprintf (out, "%s", body);
146 if (*body && *(body + strlen (body) - 1) != '\n')
147 fputs ("\n", out);
148 } else {
149 for (somebody = 0;
150 (i = fread (buf, sizeof(*buf), sizeof(buf), stdin)) > 0;
151 somebody++)
152 if (fwrite (buf, sizeof(*buf), i, out) != i)
153 adios (tmpfil, "error writing");
154 if (!somebody) {
155 unlink (tmpfil);
156 done (1);
157 }
158 }
159 fclose (out);
160
161 nvec = 0;
162 vec[nvec++] = r1bindex (postproc, '/');
163 vec[nvec++] = tmpfil;
164 if (resent)
165 vec[nvec++] = "-dist";
166 if (queued)
167 vec[nvec++] = "-queued";
168 vec[nvec] = NULL;
169
170 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
171 sleep (5);
172
173 if (child_id == NOTOK) {
174 /* report failure and then send it */
175 admonish (NULL, "unable to fork");
176 } else if (child_id) {
177 /* parent process */
178 if ((status = pidXwait(child_id, postproc))) {
179 fprintf (stderr, "Letter saved in dead.letter\n");
180 execl ("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
181 execl ("/usr/bin/mv", "mv", tmpfil, "dead.letter", NULL);
182 perror ("mv");
183 _exit (-1);
184 }
185 unlink (tmpfil);
186 done (status ? 1 : 0);
187 } else {
188 /* child process */
189 execvp (postproc, vec);
190 fprintf (stderr, "unable to exec ");
191 perror (postproc);
192 _exit (-1);
193 }
194
195 return 0; /* dead code to satisfy the compiler */
196 }
197
198
199 static RETSIGTYPE
200 intrser (int i)
201 {
202 #ifndef RELIABLE_SIGNALS
203 if (i)
204 SIGNAL (i, SIG_IGN);
205 #endif
206
207 unlink (tmpfil);
208 done (i != 0 ? 1 : 0);
209 }
210