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