3 * send.c -- send a composed message
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.
17 # define SASLminc(a) (a)
18 #else /* CYRUS_SASL */
19 # define SASLminc(a) 0
20 #endif /* CYRUS_SASL */
23 # define TLSminc(a) (a)
24 #else /* TLS_SUPPORT */
26 #endif /* TLS_SUPPORT */
28 static struct swit switches[] = {
30 { "alias aliasfile", 0 },
36 { "draftfolder +folder", 6 },
38 { "draftmessage msg", 6 },
40 { "nodraftfolder", 0 },
42 { "filter filterfile", 0 },
66 { "split seconds", 0 },
80 { "width columns", 0 },
86 { "dashstuffing", -12 },
87 #define NBITSTUFFSW 29
88 { "nodashstuffing", -14 },
98 { "client host", -6 },
100 { "server host", 6 },
104 { "sasl", SASLminc(4) },
106 { "nosasl", SASLminc(-6) },
107 #define SASLMXSSFSW 39
108 { "saslmaxssf", SASLminc(-10) },
109 #define SASLMECHSW 40
110 { "saslmech mechanism", SASLminc(-5) },
112 { "user username", SASLminc(-4) },
115 #define NOATTACHSW 43
117 #define ATTACHFORMATSW 44
118 { "attachformat", 7 },
120 { "port server-port-name/number" , 4 },
122 { "tls", TLSminc(-3) },
124 { "notls", TLSminc(-5) },
126 { "mts smtp|sendmail/smtp|sendmail/pipe", 2 },
130 static struct swit anyl[] = {
140 extern int debugsw; /* from sendsbr.c */
148 extern char *altmsg; /* .. */
149 extern char *annotext;
150 extern char *distfile;
154 main (int argc, char **argv)
156 int msgp = 0, distsw = 0, vecp = 1;
157 int isdf = 0, mime = 0;
159 char *cp, *dfolder = NULL, *maildir = NULL;
160 char buf[BUFSIZ], **ap, **argp, **arguments;
161 char *msgs[MAXARGS], *vec[MAXARGS];
164 char *attach = NMH_ATTACH_HEADER; /* header field name for attachments */
165 int attachformat = 1; /* mhbuild format specifier for attachments */
168 setlocale(LC_ALL, "");
170 invo_name = r1bindex (argv[0], '/');
172 /* read user profile/context */
175 arguments = getarguments (invo_name, argc, argv, 1);
178 vec[vecp++] = "-library";
179 vec[vecp++] = getcpy (m_maildir (""));
181 if ((cp = context_find ("fileproc"))) {
182 vec[vecp++] = "-fileproc";
186 if ((cp = context_find ("mhlproc"))) {
187 vec[vecp++] = "-mhlproc";
191 while ((cp = *argp++)) {
193 switch (smatch (++cp, switches)) {
195 ambigsw (cp, switches);
198 adios (NULL, "-%s unknown\n", cp);
201 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
202 print_help (buf, switches, 1);
205 print_version(invo_name);
209 msgs[msgp++] = draft;
214 adios (NULL, "only one draft folder at a time!");
215 if (!(cp = *argp++) || *cp == '-')
216 adios (NULL, "missing argument to %s", argp[-2]);
217 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
218 *cp != '@' ? TFOLDER : TSUBCWF);
221 if (!(cp = *argp++) || *cp == '-')
222 adios (NULL, "missing argument to %s", argp[-2]);
238 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
239 adios (NULL, "missing argument to %s", argp[-2]);
275 debugsw++; /* fall */
308 if (!(cp = *argp++) || *cp == '-')
309 adios (NULL, "missing argument to %s", argp[-2]);
314 if (!(attach = *argp++) || *attach == '-')
315 adios (NULL, "missing argument to %s", argp[-2]);
322 if (! *argp || **argp == '-')
323 adios (NULL, "missing argument to %s", argp[-1]);
325 attachformat = atoi (*argp);
326 if (attachformat < 0 ||
327 attachformat > ATTACHFORMATS - 1) {
328 advise (NULL, "unsupported attachformat %d",
342 * check for "Aliasfile:" profile entry
344 if ((cp = context_find ("Aliasfile"))) {
347 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
348 vec[vecp++] = "-alias";
353 if (dfolder == NULL) {
356 if ((cp = getenv ("mhdraft")) && *cp) {
361 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
362 if (stat (msgs[0], &st) == NOTOK)
363 adios (msgs[0], "unable to stat draft file");
364 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
365 for (status = LISTDSW; status != YESW;) {
366 if (!(argp = getans (cp, anyl)))
368 switch (status = smatch (*argp, anyl)) {
374 showfile (++argp, msgs[0]);
377 advise (NULL, "say what?");
382 for (msgnum = 0; msgnum < msgp; msgnum++)
383 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
386 if (!context_find ("path"))
387 free (path ("./", TFOLDER));
390 msgs[msgp++] = "cur";
391 maildir = m_maildir (dfolder);
393 if (chdir (maildir) == NOTOK)
394 adios (maildir, "unable to change directory to");
396 /* read folder and create message structure */
397 if (!(mp = folder_read (dfolder)))
398 adios (NULL, "unable to read folder %s", dfolder);
400 /* check for empty folder */
402 adios (NULL, "no messages in %s", dfolder);
404 /* parse all the message ranges/sequences and set SELECTED */
405 for (msgnum = 0; msgnum < msgp; msgnum++)
406 if (!m_convert (mp, msgs[msgnum]))
408 seq_setprev (mp); /* set the previous-sequence */
410 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
411 if (is_selected (mp, msgnum)) {
412 msgs[msgp++] = getcpy (m_name (msgnum));
413 unset_exists (mp, msgnum);
417 mp->msgflags |= SEQMOD;
425 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
426 if ((cp = context_find ("signature")) && *cp)
427 m_putenv ("SIGNATURE", cp);
429 for (msgnum = 0; msgnum < msgp; msgnum++)
430 if (stat (msgs[msgnum], &st) == NOTOK)
431 adios (msgs[msgnum], "unable to stat draft file");
433 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
435 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
437 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
438 altmsg = NULL; /* used by dist interface - see below */
440 if ((cp = getenv ("mhdist"))
442 && (distsw = atoi (cp))
444 vec[vecp++] = "-dist";
445 distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL));
447 if (link (altmsg, distfile) == NOTOK) {
448 /* Cygwin with FAT32 filesystem produces EPERM. */
449 if (errno != EXDEV && errno != EPERM
451 && errno != EISREMOTE
452 #endif /* EISREMOTE */
454 adios (distfile, "unable to link %s to", altmsg);
456 distfile = getcpy (m_mktemp2(NULL, invo_name, NULL, NULL));
461 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
462 adios (altmsg, "unable to open");
464 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
465 adios (distfile, "unable to write");
466 cpydata (in, out, altmsg, distfile);
475 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
484 vec[0] = r1bindex (postproc, '/');
487 for (msgnum = 0; msgnum < msgp; msgnum++) {
488 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
499 context_save (); /* save the context file */