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