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