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