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