]> diplodocus.org Git - nmh/blob - uip/send.c
d4cfd2e9bbd4d3acac753c6bae1d70314a98cdf1
[nmh] / uip / send.c
1 /* send.c -- send a composed 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/done.h"
14 #include "h/utils.h"
15 #ifdef OAUTH_SUPPORT
16 #include "h/oauth.h"
17 #endif
18 #include "sbr/m_maildir.h"
19 #include "sbr/m_mktemp.h"
20
21 #ifndef CYRUS_SASL
22 # define SASLminc(a) (a)
23 #else /* CYRUS_SASL */
24 # define SASLminc(a) 0
25 #endif /* CYRUS_SASL */
26
27 #ifndef TLS_SUPPORT
28 # define TLSminc(a) (a)
29 #else /* TLS_SUPPORT */
30 # define TLSminc(a) 0
31 #endif /* TLS_SUPPORT */
32
33 #define SEND_SWITCHES \
34 X("alias aliasfile", 0, ALIASW) \
35 X("debug", -5, DEBUGSW) \
36 X("draft", 0, DRAFTSW) \
37 X("draftfolder +folder", 6, DFOLDSW) \
38 X("draftmessage msg", 6, DMSGSW) \
39 X("nodraftfolder", 0, NDFLDSW) \
40 X("filter filterfile", 0, FILTSW) \
41 X("nofilter", 0, NFILTSW) \
42 X("format", 0, FRMTSW) \
43 X("noformat", 0, NFRMTSW) \
44 X("forward", 0, FORWSW) \
45 X("noforward", 0, NFORWSW) \
46 X("mime", 0, MIMESW) \
47 X("nomime", 0, NMIMESW) \
48 X("msgid", 0, MSGDSW) \
49 X("nomsgid", 0, NMSGDSW) \
50 X("push", 0, PUSHSW) \
51 X("nopush", 0, NPUSHSW) \
52 X("split seconds", 0, SPLITSW) \
53 X("unique", -6, UNIQSW) \
54 X("nounique", -8, NUNIQSW) \
55 X("verbose", 0, VERBSW) \
56 X("noverbose", 0, NVERBSW) \
57 X("watch", 0, WATCSW) \
58 X("nowatch", 0, NWATCSW) \
59 X("width columns", 0, WIDTHSW) \
60 X("version", 0, VERSIONSW) \
61 X("help", 0, HELPSW) \
62 X("dashstuffing", -12, BITSTUFFSW) \
63 X("nodashstuffing", -14, NBITSTUFFSW) \
64 X("client host", -6, CLIESW) \
65 X("server host", 6, SERVSW) \
66 X("snoop", 5, SNOOPSW) \
67 X("sasl", SASLminc(4), SASLSW) \
68 X("nosasl", SASLminc(6), NOSASLSW) \
69 X("saslmech mechanism", SASLminc(6), SASLMECHSW) \
70 X("authservice", SASLminc(0), AUTHSERVICESW) \
71 X("user username", SASLminc(-4), USERSW) \
72 X("port server-port-name/number", 4, PORTSW) \
73 X("tls", TLSminc(-3), TLSSW) \
74 X("initialtls", TLSminc(-10), INITTLSSW) \
75 X("notls", TLSminc(-5), NTLSSW) \
76 X("certverify", TLSminc(-10), CERTVERSW) \
77 X("nocertverify", TLSminc(-12), NOCERTVERSW) \
78 X("sendmail program", 0, MTSSM) \
79 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
80 X("messageid localname|random", 2, MESSAGEIDSW) \
81
82 #define X(sw, minchars, id) id,
83 DEFINE_SWITCH_ENUM(SEND);
84 #undef X
85
86 #define X(sw, minchars, id) { sw, minchars, id },
87 DEFINE_SWITCH_ARRAY(SEND, switches);
88 #undef X
89
90 #define USE_SWITCHES \
91 X("no", 0, NOSW) \
92 X("yes", 0, YESW) \
93 X("list", 0, LISTDSW) \
94
95 #define X(sw, minchars, id) id,
96 DEFINE_SWITCH_ENUM(USE);
97 #undef X
98
99 #define X(sw, minchars, id) { sw, minchars, id },
100 DEFINE_SWITCH_ARRAY(USE, anyl);
101 #undef X
102
103 extern int debugsw; /* from sendsbr.c */
104 extern bool forwsw;
105 extern int inplace;
106 extern bool pushsw;
107 extern int splitsw;
108 extern bool unique;
109 extern bool verbsw;
110
111 extern char *altmsg; /* .. */
112 extern char *annotext;
113 extern char *distfile;
114
115
116 int
117 main (int argc, char **argv)
118 {
119 int msgp = 0, vecp;
120 int isdf = 0;
121 int msgnum, status;
122 char *cp, *dfolder = NULL, *maildir = NULL;
123 char buf[BUFSIZ], **ap, **argp, **arguments, *program;
124 char *msgs[MAXARGS], **vec;
125 const char *user = NULL, *saslmech = NULL;
126 struct msgs *mp;
127 struct stat st;
128 char *auth_svc = NULL;
129
130 if (nmh_init(argv[0], true, true)) { return 1; }
131
132 arguments = getarguments (invo_name, argc, argv, 1);
133 argp = arguments;
134
135 vec = argsplit(postproc, &program, &vecp);
136
137 vec[vecp++] = "-library";
138 vec[vecp++] = mh_xstrdup(m_maildir(""));
139
140 if ((cp = context_find ("fileproc"))) {
141 vec[vecp++] = "-fileproc";
142 vec[vecp++] = cp;
143 }
144
145 if ((cp = context_find ("mhlproc"))) {
146 vec[vecp++] = "-mhlproc";
147 vec[vecp++] = cp;
148 }
149
150 if ((cp = context_find ("credentials"))) {
151 /* post doesn't read context so need to pass credentials. */
152 vec[vecp++] = "-credentials";
153 vec[vecp++] = cp;
154 }
155
156 while ((cp = *argp++)) {
157 if (*cp == '-') {
158 switch (smatch (++cp, switches)) {
159 case AMBIGSW:
160 ambigsw (cp, switches);
161 done (1);
162 case UNKWNSW:
163 die("-%s unknown\n", cp);
164
165 case HELPSW:
166 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
167 print_help (buf, switches, 1);
168 done (0);
169 case VERSIONSW:
170 print_version(invo_name);
171 done (0);
172
173 case DRAFTSW:
174 msgs[msgp++] = draft;
175 continue;
176
177 case DFOLDSW:
178 if (dfolder)
179 die("only one draft folder at a time!");
180 if (!(cp = *argp++) || *cp == '-')
181 die("missing argument to %s", argp[-2]);
182 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
183 *cp != '@' ? TFOLDER : TSUBCWF);
184 continue;
185 case DMSGSW:
186 if (!(cp = *argp++) || *cp == '-')
187 die("missing argument to %s", argp[-2]);
188 msgs[msgp++] = cp;
189 continue;
190 case NDFLDSW:
191 dfolder = NULL;
192 isdf = NOTOK;
193 continue;
194
195 case PUSHSW:
196 pushsw = true;
197 continue;
198 case NPUSHSW:
199 pushsw = false;
200 continue;
201
202 case SPLITSW:
203 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
204 die("missing argument to %s", argp[-2]);
205 continue;
206
207 case UNIQSW:
208 unique = true;
209 continue;
210 case NUNIQSW:
211 unique = false;
212 continue;
213
214 case FORWSW:
215 forwsw = true;
216 continue;
217 case NFORWSW:
218 forwsw = false;
219 continue;
220
221 case VERBSW:
222 verbsw = true;
223 vec[vecp++] = --cp;
224 continue;
225 case NVERBSW:
226 verbsw = false;
227 vec[vecp++] = --cp;
228 continue;
229
230 case MIMESW:
231 vec[vecp++] = --cp;
232 continue;
233 case NMIMESW:
234 vec[vecp++] = --cp;
235 continue;
236
237 case SNOOPSW:
238 vec[vecp++] = --cp;
239 continue;
240
241 case DEBUGSW:
242 debugsw++;
243 /* FALLTHRU */
244 case NFILTSW:
245 case FRMTSW:
246 case NFRMTSW:
247 case BITSTUFFSW:
248 case NBITSTUFFSW:
249 case MSGDSW:
250 case NMSGDSW:
251 case WATCSW:
252 case NWATCSW:
253 case SASLSW:
254 case NOSASLSW:
255 case TLSSW:
256 case INITTLSSW:
257 case NTLSSW:
258 case CERTVERSW:
259 case NOCERTVERSW:
260 vec[vecp++] = --cp;
261 continue;
262
263 case USERSW:
264 vec[vecp++] = --cp;
265 if (!(cp = *argp++) || *cp == '-')
266 die("missing argument to %s", argp[-2]);
267 vec[vecp++] = cp;
268 user = cp;
269 continue;
270
271 case AUTHSERVICESW:
272 #ifdef OAUTH_SUPPORT
273 if (!(auth_svc = *argp++) || *auth_svc == '-')
274 die("missing argument to %s", argp[-2]);
275 #else
276 die("not built with OAuth support");
277 #endif
278 continue;
279
280 case SASLMECHSW:
281 if (!(saslmech = *argp) || *saslmech == '-')
282 die("missing argument to %s", argp[-1]);
283 /* FALLTHRU */
284
285 case ALIASW:
286 case FILTSW:
287 case WIDTHSW:
288 case CLIESW:
289 case SERVSW:
290 case PORTSW:
291 case MTSSM:
292 case MTSSW:
293 case MESSAGEIDSW:
294 vec[vecp++] = --cp;
295 if (!(cp = *argp++) || *cp == '-')
296 die("missing argument to %s", argp[-2]);
297 vec[vecp++] = cp;
298 continue;
299 }
300 } else {
301 msgs[msgp++] = cp;
302 }
303 }
304
305 /*
306 * check for "Aliasfile:" profile entry
307 */
308 if ((cp = context_find ("Aliasfile"))) {
309 char *dp = NULL;
310
311 for (ap = brkstring(dp = mh_xstrdup(cp), " ", "\n"); ap && *ap; ap++) {
312 vec[vecp++] = "-alias";
313 vec[vecp++] = *ap;
314 }
315 }
316
317 if (dfolder == NULL) {
318 if (msgp == 0) {
319 msgs[msgp++] = mh_xstrdup(m_draft(NULL, NULL, 1, &isdf));
320 if (stat (msgs[0], &st) == NOTOK)
321 adios (msgs[0], "unable to stat draft file");
322 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
323 for (status = LISTDSW; status != YESW;) {
324 if (!(argp = read_switch_multiword (cp, anyl)))
325 done (1);
326 switch (status = smatch (*argp, anyl)) {
327 case NOSW:
328 done (0);
329 case YESW:
330 break;
331 case LISTDSW:
332 showfile (++argp, msgs[0]);
333 break;
334 default:
335 inform("say what?");
336 break;
337 }
338 }
339 } else {
340 for (msgnum = 0; msgnum < msgp; msgnum++)
341 msgs[msgnum] = mh_xstrdup(m_maildir(msgs[msgnum]));
342 }
343 } else {
344 if (!context_find ("path"))
345 free (path ("./", TFOLDER));
346
347 if (!msgp)
348 msgs[msgp++] = "cur";
349 maildir = m_maildir (dfolder);
350
351 if (chdir (maildir) == NOTOK)
352 adios (maildir, "unable to change directory to");
353
354 /* read folder and create message structure */
355 if (!(mp = folder_read (dfolder, 1)))
356 die("unable to read folder %s", dfolder);
357
358 /* check for empty folder */
359 if (mp->nummsg == 0)
360 die("no messages in %s", dfolder);
361
362 /* parse all the message ranges/sequences and set SELECTED */
363 for (msgnum = 0; msgnum < msgp; msgnum++)
364 if (!m_convert (mp, msgs[msgnum]))
365 done (1);
366 seq_setprev (mp); /* set the previous-sequence */
367
368 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
369 if (is_selected (mp, msgnum)) {
370 msgs[msgp++] = mh_xstrdup(m_name (msgnum));
371 unset_exists (mp, msgnum);
372 }
373 }
374
375 mp->msgflags |= SEQMOD;
376 seq_save (mp);
377 }
378
379 #ifdef WHATNOW
380 go_to_it:
381 #endif /* WHATNOW */
382
383 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
384 if ((cp = context_find ("signature")) && *cp)
385 setenv("SIGNATURE", cp, 1);
386
387 for (msgnum = 0; msgnum < msgp; msgnum++)
388 if (stat (msgs[msgnum], &st) == NOTOK)
389 adios (msgs[msgnum], "unable to stat draft file");
390
391 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
392 annotext = NULL;
393 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
394 inplace = atoi (cp);
395 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
396 altmsg = NULL; /* used by dist interface - see below */
397
398 if ((cp = getenv ("mhdist"))
399 && *cp
400 && atoi(cp)
401 && altmsg) {
402 vec[vecp++] = "-dist";
403 if ((cp = m_mktemp2(altmsg, invo_name, NULL, NULL)) == NULL) {
404 die("unable to create temporary file");
405 }
406 distfile = mh_xstrdup(cp);
407 (void) m_unlink(distfile);
408 if (link (altmsg, distfile) == NOTOK) {
409 /* Cygwin with FAT32 filesystem produces EPERM. */
410 if (errno != EXDEV && errno != EPERM
411 #ifdef EISREMOTE
412 && errno != EISREMOTE
413 #endif /* EISREMOTE */
414 )
415 adios (distfile, "unable to link %s to", altmsg);
416 free (distfile);
417 if ((cp = m_mktemp2(NULL, invo_name, NULL, NULL)) == NULL) {
418 die("unable to create temporary file in %s",
419 get_temp_dir());
420 }
421 distfile = mh_xstrdup(cp);
422 {
423 int in, out;
424 struct stat st;
425
426 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
427 adios (altmsg, "unable to open");
428 fstat(in, &st);
429 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
430 adios (distfile, "unable to write");
431 cpydata (in, out, altmsg, distfile);
432 close (in);
433 close (out);
434 }
435 }
436 } else {
437 distfile = NULL;
438 }
439
440 #ifdef OAUTH_SUPPORT
441 if (auth_svc == NULL) {
442 if (saslmech && ! strcasecmp(saslmech, "xoauth2")) {
443 die("must specify -authservice with -saslmech xoauth2");
444 }
445 } else {
446 if (user == NULL) {
447 die("must specify -user with -saslmech xoauth2");
448 }
449 }
450 #else
451 NMH_UNUSED(auth_svc);
452 NMH_UNUSED(user);
453 NMH_UNUSED(saslmech);
454 #endif /* OAUTH_SUPPORT */
455
456 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
457 st.st_mtime = 0;
458 st.st_dev = 0;
459 st.st_ino = 0;
460 }
461 if (pushsw)
462 push ();
463
464 status = 0;
465 closefds (3);
466
467 for (msgnum = 0; msgnum < msgp; msgnum++) {
468 switch (sendsbr (vec, vecp, program, msgs[msgnum], &st, 1, auth_svc)) {
469 case DONE:
470 done (++status);
471 /* FALLTHRU */
472 case NOTOK:
473 status++;
474 /* FALLTHRU */
475 case OK:
476 break;
477 }
478 }
479
480 context_save (); /* save the context file */
481 done (status);
482 return 1;
483 }