]> diplodocus.org Git - nmh/blob - uip/viamail.c
Fixed three LC_ALL settings:
[nmh] / uip / viamail.c
1
2 /*
3 * viamail.c -- send multiple files in a MIME message
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 <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <h/mts.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18
19 #define VIAMAIL_SWITCHES \
20 X("to mailpath", 0, TOSW) \
21 X("from mailpath", 0, FROMSW) \
22 X("subject subject", 0, SUBJECTSW) \
23 X("parameters arguments", 0, PARAMSW) \
24 X("description text", 0, DESCRIPTSW) \
25 X("comment text", 0, COMMENTSW) \
26 X("delay seconds", 0, DELAYSW) \
27 X("verbose", 0, VERBSW) \
28 X("noverbose", 0, NVERBSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
31 X("debug", -5, DEBUGSW) \
32
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(VIAMAIL);
35 #undef X
36
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(VIAMAIL, switches);
39 #undef X
40
41 extern int debugsw;
42 extern int splitsw;
43 extern int verbsw;
44
45 /*
46 * static prototypes
47 */
48 static int via_mail (char *, char *, char *, char *, char *, int, char *);
49
50
51 int
52 main (int argc, char **argv)
53 {
54 int delay = 0;
55 char *f1 = NULL, *f2 = NULL, *f3 = NULL;
56 char *f4 = NULL, *f5 = NULL, *f7 = NULL;
57 static char postpath[PATH_MAX];
58 char *cp, buf[BUFSIZ];
59 char **argp, **arguments;
60
61 setlocale(LC_ALL, "");
62 invo_name = r1bindex (argv[0], '/');
63
64 /* foil search of user profile/context */
65 if (context_foil (NULL) == -1)
66 done (1);
67
68 arguments = getarguments (invo_name, argc, argv, 0);
69 argp = arguments;
70
71 while ((cp = *argp++)) {
72 if (*cp == '-') {
73 switch (smatch (++cp, switches)) {
74 case AMBIGSW:
75 ambigsw (cp, switches);
76 done (1);
77 case UNKWNSW:
78 adios (NULL, "-%s unknown", cp);
79
80 case HELPSW:
81 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
82 print_help (buf, switches, 1);
83 done (0);
84 case VERSIONSW:
85 print_version(invo_name);
86 done (0);
87
88 case TOSW:
89 if (!(f1 = *argp++))
90 adios (NULL, "missing argument to %s", argp[-2]);
91 continue;
92 case SUBJECTSW:
93 if (!(f2 = *argp++))
94 adios (NULL, "missing argument to %s", argp[-2]);
95 continue;
96 case PARAMSW:
97 if (!(f3 = *argp++))
98 adios (NULL, "missing argument to %s", argp[-2]);
99 continue;
100 case DESCRIPTSW:
101 if (!(f4 = *argp++))
102 adios (NULL, "missing argument to %s", argp[-2]);
103 continue;
104 case COMMENTSW:
105 if (!(f5 = *argp++))
106 adios (NULL, "missing argument to %s", argp[-2]);
107 continue;
108 case DELAYSW:
109 if (!(cp = *argp++) || *cp == '-')
110 adios (NULL, "missing argument to %s", argp[-2]);
111
112 /*
113 * If there is an error, just reset the delay parameter
114 * to -1. We will set a default delay later.
115 */
116 if (sscanf (cp, "%d", &delay) != 1)
117 delay = -1;
118 continue;
119 case FROMSW:
120 if (!(f7 = *argp++))
121 adios (NULL, "missing argument to %s", argp[-2]);
122 continue;
123
124 case VERBSW:
125 verbsw = 1;
126 continue;
127 case NVERBSW:
128 verbsw = 0;
129 continue;
130
131 case DEBUGSW:
132 debugsw = 1;
133 continue;
134 }
135 }
136 }
137
138 if (!f1)
139 adios (NULL, "missing -viamail \"mailpath\" switch");
140
141 /* viamail doesn't read the context and postproc isn't always what
142 we want, such as when running make distcheck. If we have the
143 absolute path, set postproc to point to post in the same
144 directory as this executable.
145 This could be generalized to handle relative paths (by
146 converting to absolute), to find the full path from PATH given
147 just the basename, and to squash out ../ but it's only needed
148 here. viamail is typically called from sendfiles, which
149 provides the absolute path.
150 */
151 if (argv[0] && argv[0][0] == '/' &&
152 strlen(argv[0]) - 3 < sizeof postpath) {
153 strncpy (postpath, argv[0], sizeof postpath - 1);
154 postpath[sizeof postpath - 1] = '\0';
155 if ((cp = strrchr (postpath, '/'))) {
156 struct stat st;
157
158 *(cp + 1) = '\0';
159 /* strlen ("post") <= sizeof postpath - (cp - postpath) - 2
160 but use strncat just in case the code above changes. */
161 strncat (postpath, "post", sizeof postpath - (cp - postpath) - 2);
162
163 if (stat (postpath, &st) == OK) {
164 postproc = postpath;
165 }
166 }
167 }
168
169 via_mail (f1, f2, f3, f4, f5, delay, f7);
170 return 0; /* dead code to satisfy the compiler */
171 }
172
173
174 /*
175 * VIAMAIL
176 */
177
178 static int
179 via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw,
180 char *cmntsw, int delay, char *fromsw)
181 {
182 int status, vecp;
183 char tmpfil[BUFSIZ], *program;
184 char **vec;
185 struct stat st;
186 FILE *fp;
187 char *tfile = NULL;
188 char *cp;
189
190 umask (~m_gmprot ());
191
192 tfile = m_mktemp2(NULL, invo_name, NULL, &fp);
193 if (tfile == NULL) adios("viamail", "unable to create temporary file");
194 chmod(tfile, 0600);
195 strncpy (tmpfil, tfile, sizeof(tmpfil));
196
197 if (!strchr(mailsw, '@'))
198 mailsw = concat (mailsw, "@", LocalName (0), NULL);
199 fprintf (fp, "To: %s\n", mailsw);
200
201 if (subjsw)
202 fprintf (fp, "Subject: %s\n", subjsw);
203
204 if (fromsw) {
205 if (!strchr(fromsw, '@'))
206 fromsw = concat (fromsw, "@", LocalName (0), NULL);
207 fprintf (fp, "From: %s\n", fromsw);
208 }
209
210 fprintf (fp, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
211 fprintf (fp, "%s: application/octet-stream", TYPE_FIELD);
212
213 if (parmsw)
214 fprintf (fp, "; %s", parmsw);
215
216 if (cmntsw)
217 fprintf (fp, "\n\t(%s)", cmntsw);
218
219 if (descsw)
220 fprintf (fp, "\n%s: %s", DESCR_FIELD, descsw);
221
222 fprintf (fp, "\n%s: %s\n\n", ENCODING_FIELD, "base64");
223
224 if (fflush (fp))
225 adios (tmpfil, "error writing to");
226
227 writeBase64aux (stdin, fp);
228 if (fflush (fp))
229 adios (tmpfil, "error writing to");
230
231 if (fstat (fileno (fp), &st) == NOTOK)
232 adios ("failed", "fstat of %s", tmpfil);
233
234 if (delay < 0)
235 splitsw = 10;
236 else
237 splitsw = delay;
238
239 status = 0;
240
241 vec = argsplit(postproc, &program, &vecp);
242 if (verbsw)
243 vec[vecp++] = "-verbose";
244
245 if ((cp = context_find ("credentials"))) {
246 /* post doesn't read context so need to pass credentials. */
247 vec[vecp++] = "-credentials";
248 vec[vecp++] = cp;
249 }
250
251 switch (sendsbr (vec, vecp, program, tmpfil, &st, 0, (char *)0, 0)) {
252 case DONE:
253 case NOTOK:
254 status++;
255 break;
256 case OK:
257 break;
258 }
259
260 fclose (fp);
261 if (unlink (tmpfil) == -1)
262 advise (NULL, "unable to remove temp file %s", tmpfil);
263 done (status);
264 return 1;
265 }