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