]> diplodocus.org Git - nmh/blob - uip/viamail.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / viamail.c
1 /* viamail.c -- send multiple files in a MIME message
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <fcntl.h>
10 #include <h/md5.h>
11 #include <h/mts.h>
12 #include <h/tws.h>
13 #include <h/mime.h>
14 #include <h/mhparse.h>
15 #include "h/done.h"
16 #include <h/utils.h>
17 #include "sbr/m_mktemp.h"
18 #include "sbr/base64.h"
19
20 #define VIAMAIL_SWITCHES \
21 X("to mailpath", 0, TOSW) \
22 X("from mailpath", 0, FROMSW) \
23 X("subject subject", 0, SUBJECTSW) \
24 X("parameters arguments", 0, PARAMSW) \
25 X("description text", 0, DESCRIPTSW) \
26 X("comment text", 0, COMMENTSW) \
27 X("delay seconds", 0, DELAYSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
32 X("debug", -5, DEBUGSW) \
33
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(VIAMAIL);
36 #undef X
37
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(VIAMAIL, switches);
40 #undef X
41
42 extern int debugsw;
43 extern int splitsw;
44 extern bool verbsw;
45
46 /*
47 * static prototypes
48 */
49 static int via_mail (char *, char *, char *, char *, char *, int, char *);
50
51
52 int
53 main (int argc, char **argv)
54 {
55 int delay = 0;
56 char *f1 = NULL, *f2 = NULL, *f3 = NULL;
57 char *f4 = NULL, *f5 = NULL, *f7 = NULL;
58 char *cp, buf[BUFSIZ];
59 char **argp, **arguments;
60
61 if (nmh_init(argv[0], true, false)) { return 1; }
62
63 arguments = getarguments (invo_name, argc, argv, 0);
64 argp = arguments;
65
66 while ((cp = *argp++)) {
67 if (*cp == '-') {
68 switch (smatch (++cp, switches)) {
69 case AMBIGSW:
70 ambigsw (cp, switches);
71 done (1);
72 case UNKWNSW:
73 die("-%s unknown", cp);
74
75 case HELPSW:
76 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
77 print_help (buf, switches, 1);
78 done (0);
79 case VERSIONSW:
80 print_version(invo_name);
81 done (0);
82
83 case TOSW:
84 if (!(f1 = *argp++))
85 die("missing argument to %s", argp[-2]);
86 continue;
87 case SUBJECTSW:
88 if (!(f2 = *argp++))
89 die("missing argument to %s", argp[-2]);
90 continue;
91 case PARAMSW:
92 if (!(f3 = *argp++))
93 die("missing argument to %s", argp[-2]);
94 continue;
95 case DESCRIPTSW:
96 if (!(f4 = *argp++))
97 die("missing argument to %s", argp[-2]);
98 continue;
99 case COMMENTSW:
100 if (!(f5 = *argp++))
101 die("missing argument to %s", argp[-2]);
102 continue;
103 case DELAYSW:
104 if (!(cp = *argp++) || *cp == '-')
105 die("missing argument to %s", argp[-2]);
106
107 /*
108 * If there is an error, just reset the delay parameter
109 * to -1. We will set a default delay later.
110 */
111 if (sscanf (cp, "%d", &delay) != 1)
112 delay = -1;
113 continue;
114 case FROMSW:
115 if (!(f7 = *argp++))
116 die("missing argument to %s", argp[-2]);
117 continue;
118
119 case VERBSW:
120 verbsw = true;
121 continue;
122 case NVERBSW:
123 verbsw = false;
124 continue;
125
126 case DEBUGSW:
127 debugsw = 1;
128 continue;
129 }
130 }
131 }
132
133 if (!f1)
134 die("missing -viamail \"mailpath\" switch");
135
136 via_mail (f1, f2, f3, f4, f5, delay, f7);
137 return 0; /* dead code to satisfy the compiler */
138 }
139
140
141 /*
142 * VIAMAIL
143 */
144
145 static int
146 via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw,
147 char *cmntsw, int delay, char *fromsw)
148 {
149 int status, vecp;
150 char tmpfil[BUFSIZ], *program;
151 char **vec;
152 struct stat st;
153 FILE *fp;
154 char *tfile = NULL;
155 char *cp;
156
157 umask (~m_gmprot ());
158
159 if ((tfile = m_mktemp2(NULL, invo_name, NULL, &fp)) == NULL) {
160 die("unable to create temporary file in %s", get_temp_dir());
161 }
162 strncpy (tmpfil, tfile, sizeof(tmpfil));
163
164 if (!strchr(mailsw, '@'))
165 mailsw = concat (mailsw, "@", LocalName (0), NULL);
166 fprintf (fp, "To: %s\n", mailsw);
167
168 if (subjsw)
169 fprintf (fp, "Subject: %s\n", subjsw);
170
171 if (fromsw) {
172 if (!strchr(fromsw, '@'))
173 fromsw = concat (fromsw, "@", LocalName (0), NULL);
174 fprintf (fp, "From: %s\n", fromsw);
175 }
176
177 fprintf (fp, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
178 fprintf (fp, "%s: application/octet-stream", TYPE_FIELD);
179
180 if (parmsw)
181 fprintf (fp, "; %s", parmsw);
182
183 if (cmntsw)
184 fprintf (fp, "\n\t(%s)", cmntsw);
185
186 if (descsw)
187 fprintf (fp, "\n%s: %s", DESCR_FIELD, descsw);
188
189 fprintf (fp, "\n%s: %s\n\n", ENCODING_FIELD, "base64");
190
191 if (fflush (fp))
192 adios (tmpfil, "error writing to");
193
194 writeBase64aux (stdin, fp, 0);
195 if (fflush (fp))
196 adios (tmpfil, "error writing to");
197
198 if (fstat (fileno (fp), &st) == NOTOK)
199 adios ("failed", "fstat of %s", tmpfil);
200
201 if (delay < 0)
202 splitsw = 10;
203 else
204 splitsw = delay;
205
206 status = 0;
207
208 vec = argsplit(postproc, &program, &vecp);
209 if (verbsw)
210 vec[vecp++] = "-verbose";
211
212 if ((cp = context_find ("credentials"))) {
213 /* post doesn't read context so need to pass credentials. */
214 vec[vecp++] = "-credentials";
215 vec[vecp++] = cp;
216 }
217
218 switch (sendsbr (vec, vecp, program, tmpfil, &st, 0, NULL)) {
219 case DONE:
220 case NOTOK:
221 status++;
222 break;
223 case OK:
224 break;
225 }
226
227 fclose (fp);
228 if (m_unlink (tmpfil) == -1)
229 advise (tmpfil, "unable to remove temp file %s", tmpfil);
230 done (status);
231 return 1;
232 }