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