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