]> diplodocus.org Git - nmh/blob - uip/mhmail.c
Formatting cleanup.
[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 void intrser (int);
41
42
43 int
44 main (int argc, char **argv)
45 {
46 pid_t child_id;
47 int status, iscc = 0, nvec;
48 size_t i;
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 char *tfile = NULL;
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
129 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
130 if (tfile == NULL) adios("mhmail", "unable to create temporary file");
131 chmod(tfile, 0600);
132 strncpy (tmpfil, tfile, sizeof(tmpfil));
133
134 SIGNAL2 (SIGINT, intrser);
135
136 fprintf (out, "%sTo: %s\n", resent ? "Resent-" : "", tolist);
137 if (cclist)
138 fprintf (out, "%scc: %s\n", resent ? "Resent-" : "", cclist);
139 if (subject)
140 fprintf (out, "%sSubject: %s\n", resent ? "Resent-" : "", subject);
141 if (from)
142 fprintf (out, "%sFrom: %s\n", resent ? "Resent-" : "", from);
143 if (!resent)
144 fputs ("\n", out);
145
146 if (body) {
147 fprintf (out, "%s", body);
148 if (*body && *(body + strlen (body) - 1) != '\n')
149 fputs ("\n", out);
150 } else {
151 for (somebody = 0;
152 (i = fread (buf, sizeof(*buf), sizeof(buf), stdin)) > 0;
153 somebody++)
154 if (fwrite (buf, sizeof(*buf), i, out) != i)
155 adios (tmpfil, "error writing");
156 if (!somebody) {
157 unlink (tmpfil);
158 done (1);
159 }
160 }
161 fclose (out);
162
163 nvec = 0;
164 vec[nvec++] = r1bindex (postproc, '/');
165 vec[nvec++] = tmpfil;
166 if (resent)
167 vec[nvec++] = "-dist";
168 if (queued)
169 vec[nvec++] = "-queued";
170 vec[nvec] = NULL;
171
172 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
173 sleep (5);
174
175 if (child_id == NOTOK) {
176 /* report failure and then send it */
177 adios (NULL, "unable to fork");
178 } else if (child_id) {
179 /* parent process */
180 if ((status = pidXwait(child_id, postproc))) {
181 fprintf (stderr, "Letter saved in dead.letter\n");
182 execl ("/bin/mv", "mv", tmpfil, "dead.letter", NULL);
183 execl ("/usr/bin/mv", "mv", tmpfil, "dead.letter", NULL);
184 perror ("mv");
185 _exit (-1);
186 }
187 unlink (tmpfil);
188 done (status ? 1 : 0);
189 } else {
190 /* child process */
191 execvp (postproc, vec);
192 fprintf (stderr, "unable to exec ");
193 perror (postproc);
194 _exit (-1);
195 }
196
197 return 0; /* dead code to satisfy the compiler */
198 }
199
200
201 static void
202 intrser (int i)
203 {
204 unlink (tmpfil);
205 done (i != 0 ? 1 : 0);
206 }
207