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