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