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