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