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