]> diplodocus.org Git - nmh/blob - uip/mhmail.c
We're not using the .Bu macro anymore.
[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 <h/utils.h>
15 #include <signal.h>
16
17 static struct swit switches[] = {
18 #define BODYSW 0
19 { "body text", 0 },
20 #define CCSW 1
21 { "cc addrs ...", 0 },
22 #define FROMSW 2
23 { "from addr", 0 },
24 #define SUBJSW 3
25 { "subject text", 0 },
26 #define VERSIONSW 4
27 { "version", 0 },
28 #define HELPSW 5
29 { "help", 0 },
30 #define RESNDSW 6
31 { "resent", -6 },
32 #define QUEUESW 7
33 { "queued", -6 },
34 { NULL, 0 }
35 };
36
37 static char tmpfil[BUFSIZ];
38
39 /*
40 * static prototypes
41 */
42 static RETSIGTYPE intrser (int);
43
44
45 int
46 main (int argc, char **argv)
47 {
48 pid_t child_id;
49 int status, i, iscc = 0, nvec;
50 int queued = 0, resent = 0, somebody;
51 char *cp, *tolist = NULL, *cclist = NULL, *subject = NULL;
52 char *from = NULL, *body = NULL, **argp, **arguments;
53 char *vec[5], buf[BUFSIZ];
54 FILE *out;
55
56 #ifdef LOCALE
57 setlocale(LC_ALL, "");
58 #endif
59 invo_name = r1bindex (argv[0], '/');
60
61 /* foil search of user profile/context */
62 if (context_foil (NULL) == -1)
63 done (1);
64
65 /* If no arguments, just incorporate new mail */
66 if (argc == 1) {
67 execlp (incproc, r1bindex (incproc, '/'), NULL);
68 adios (incproc, "unable to exec");
69 }
70
71 arguments = getarguments (invo_name, argc, argv, 0);
72 argp = arguments;
73
74 while ((cp = *argp++)) {
75 if (*cp == '-') {
76 switch (smatch (++cp, switches)) {
77 case AMBIGSW:
78 ambigsw (cp, switches);
79 done (1);
80 case UNKWNSW:
81 adios (NULL, "-%s unknown", cp);
82
83 case HELPSW:
84 snprintf (buf, sizeof(buf), "%s [addrs ... [switches]]",
85 invo_name);
86 print_help (buf, switches, 0);
87 done (1);
88 case VERSIONSW:
89 print_version(invo_name);
90 done (1);
91
92 case FROMSW:
93 if (!(from = *argp++) || *from == '-')
94 adios (NULL, "missing argument to %s", argp[-2]);
95 continue;
96
97 case BODYSW:
98 if (!(body = *argp++) || *body == '-')
99 adios (NULL, "missing argument to %s", argp[-2]);
100 continue;
101
102 case CCSW:
103 iscc++;
104 continue;
105
106 case SUBJSW:
107 if (!(subject = *argp++) || *subject == '-')
108 adios (NULL, "missing argument to %s", argp[-2]);
109 continue;
110
111 case RESNDSW:
112 resent++;
113 continue;
114
115 case QUEUESW:
116 queued++;
117 continue;
118 }
119 }
120 if (iscc)
121 cclist = cclist ? add (cp, add (", ", cclist)) : getcpy (cp);
122 else
123 tolist = tolist ? add (cp, add (", ", tolist)) : getcpy (cp);
124 }
125
126 if (tolist == NULL)
127 adios (NULL, "usage: %s addrs ... [switches]", invo_name);
128 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
129 if ((out = fopen (tmpfil, "w")) == NULL)
130 adios (tmpfil, "unable to write");
131 chmod (tmpfil, 0600);
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 admonish (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