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