]> diplodocus.org Git - nmh/blob - uip/mhmail.c
Tidied up for loop in message_id().
[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/mts.h>
12 #include <h/signals.h>
13 #include <h/utils.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 void intrser (int);
42
43
44 int
45 main (int argc, char **argv)
46 {
47 pid_t child_id;
48 int status, iscc = 0, nvec;
49 size_t i;
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 char *tfile = NULL;
56
57 #ifdef LOCALE
58 setlocale(LC_ALL, "");
59 #endif
60 invo_name = r1bindex (argv[0], '/');
61
62 /* foil search of user profile/context */
63 if (context_foil (NULL) == -1)
64 done (1);
65
66 /* If no arguments, just incorporate new mail */
67 if (argc == 1) {
68 execlp (incproc, r1bindex (incproc, '/'), NULL);
69 adios (incproc, "unable to exec");
70 }
71
72 arguments = getarguments (invo_name, argc, argv, 0);
73 argp = arguments;
74
75 while ((cp = *argp++)) {
76 if (*cp == '-') {
77 switch (smatch (++cp, switches)) {
78 case AMBIGSW:
79 ambigsw (cp, switches);
80 done (1);
81 case UNKWNSW:
82 adios (NULL, "-%s unknown", cp);
83
84 case HELPSW:
85 snprintf (buf, sizeof(buf), "%s [addrs ... [switches]]",
86 invo_name);
87 print_help (buf, switches, 0);
88 done (0);
89 case VERSIONSW:
90 print_version(invo_name);
91 done (0);
92
93 case FROMSW:
94 if (!(from = *argp++) || *from == '-')
95 adios (NULL, "missing argument to %s", argp[-2]);
96 continue;
97
98 case BODYSW:
99 if (!(body = *argp++) || *body == '-')
100 adios (NULL, "missing argument to %s", 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", argp[-2]);
110 continue;
111
112 case RESNDSW:
113 resent++;
114 continue;
115
116 case QUEUESW:
117 queued++;
118 continue;
119 }
120 }
121 if (iscc)
122 cclist = cclist ? add (cp, add (", ", cclist)) : getcpy (cp);
123 else
124 tolist = tolist ? add (cp, add (", ", tolist)) : getcpy (cp);
125 }
126
127 if (tolist == NULL)
128 adios (NULL, "usage: %s addrs ... [switches]", invo_name);
129
130 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
131 if (tfile == NULL) adios("mhmail", "unable to create temporary file");
132 chmod(tfile, 0600);
133 strncpy (tmpfil, tfile, sizeof(tmpfil));
134
135 SIGNAL2 (SIGINT, intrser);
136
137 fprintf (out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
138 if (cclist)
139 fprintf (out, "%scc: %s\n", resent ? "Resent-" : "", cclist);
140 if (subject)
141 fprintf (out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
142 if (from)
143 fprintf (out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
144 else
145 fprintf (out, "From: %s\n", getlocalmbox ());
146 if (!resent)
147 fputs ("\n", out);
148
149 if (body) {
150 fprintf (out, "%s", body);
151 if (*body && *(body + strlen (body) - 1) != '\n')
152 fputs ("\n", out);
153 } else {
154 for (somebody = 0;
155 (i = fread (buf, sizeof(*buf), sizeof(buf), stdin)) > 0;
156 somebody++)
157 if (fwrite (buf, sizeof(*buf), i, out) != i)
158 adios (tmpfil, "error writing");
159 if (!somebody) {
160 unlink (tmpfil);
161 done (1);
162 }
163 }
164 fclose (out);
165
166 nvec = 0;
167 vec[nvec++] = r1bindex (postproc, '/');
168 vec[nvec++] = tmpfil;
169 if (resent)
170 vec[nvec++] = "-dist";
171 if (queued)
172 vec[nvec++] = "-queued";
173 vec[nvec] = NULL;
174
175 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
176 sleep (5);
177
178 if (child_id == NOTOK) {
179 /* report failure and then send it */
180 adios (NULL, "unable to fork");
181 } else if (child_id) {
182 /* parent process */
183 if ((status = pidXwait(child_id, postproc))) {
184 fprintf (stderr, "Letter saved in dead.letter\n");
185 execl ("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
186 execl ("/usr/bin/mv", "mv", tmpfil, "dead.letter", NULL);
187 perror ("mv");
188 _exit (-1);
189 }
190 unlink (tmpfil);
191 done (status ? 1 : 0);
192 } else {
193 /* child process */
194 execvp (postproc, vec);
195 fprintf (stderr, "unable to exec ");
196 perror (postproc);
197 _exit (-1);
198 }
199
200 return 0; /* dead code to satisfy the compiler */
201 }
202
203
204 static void
205 intrser (int i)
206 {
207 unlink (tmpfil);
208 done (i != 0 ? 1 : 0);
209 }
210