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