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