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