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