]> diplodocus.org Git - nmh/blob - uip/mhmail.c
Feed fileproc and mhlproc from rcvdist, send, and whatnow to post.
[nmh] / uip / mhmail.c
1
2 /*
3 * mhmail.c -- simple mail program
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/signals.h>
12 #include <h/utils.h>
13 #include <signal.h>
14
15 static struct swit switches[] = {
16 #define BODYSW 0
17 { "body text", 0 },
18 #define CCSW 1
19 { "cc addrs ...", 0 },
20 #define FROMSW 2
21 { "from addr", 0 },
22 #define SUBJSW 3
23 { "subject text", 0 },
24 #define VERSIONSW 4
25 { "version", 0 },
26 #define HELPSW 5
27 { "help", 0 },
28 #define RESNDSW 6
29 { "resent", -6 },
30 #define QUEUESW 7
31 { "queued", -6 },
32 { NULL, 0 }
33 };
34
35 static char tmpfil[BUFSIZ];
36
37 /*
38 * static prototypes
39 */
40 static RETSIGTYPE intrser (int);
41
42
43 int
44 main (int argc, char **argv)
45 {
46 pid_t child_id;
47 int status, i, iscc = 0, nvec;
48 int queued = 0, resent = 0, somebody;
49 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
50 char *from = NULL, *body = NULL, **argp, **arguments;
51 char *vec[5], buf[BUFSIZ];
52 FILE *out;
53 char *tfile = NULL;
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
128 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
129 if (tfile == NULL) adios("mhmail", "unable to create temporary file");
130 chmod(tfile, 0600);
131 strncpy (tmpfil, tfile, sizeof(tmpfil));
132
133 SIGNAL2 (SIGINT, intrser);
134
135 fprintf (out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
136 if (cclist)
137 fprintf (out, "%scc: %s\n", resent ? "Resent-" : "", cclist);
138 if (subject)
139 fprintf (out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
140 if (from)
141 fprintf (out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
142 if (!resent)
143 fputs ("\n", out);
144
145 if (body) {
146 fprintf (out, "%s", body);
147 if (*body && *(body + strlen (body) - 1) != '\n')
148 fputs ("\n", out);
149 } else {
150 for (somebody = 0;
151 (i = fread (buf, sizeof(*buf), sizeof(buf), stdin)) > 0;
152 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++] = r1bindex (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", NULL);
183 perror ("mv");
184 _exit (-1);
185 }
186 unlink (tmpfil);
187 done (status ? 1 : 0);
188 } else {
189 /* child process */
190 execvp (postproc, vec);
191 fprintf (stderr, "unable to exec ");
192 perror (postproc);
193 _exit (-1);
194 }
195
196 return 0; /* dead code to satisfy the compiler */
197 }
198
199
200 static RETSIGTYPE
201 intrser (int i)
202 {
203 #ifndef RELIABLE_SIGNALS
204 if (i)
205 SIGNAL (i, SIG_IGN);
206 #endif
207
208 unlink (tmpfil);
209 done (i != 0 ? 1 : 0);
210 }
211