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