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