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