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