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 ATTACHFORMATSW 43
116 { "attachformat", 7 },
118 { "port server-port-name/number" , 4 },
120 { "tls", TLSminc(-3) },
124 static struct swit anyl[] = {
134 extern int debugsw; /* from sendsbr.c */
142 extern char *altmsg; /* .. */
143 extern char *annotext;
144 extern char *distfile;
148 main (int argc, char **argv)
150 int msgp = 0, distsw = 0, vecp = 1;
151 int isdf = 0, mime = 0;
153 char *cp, *dfolder = NULL, *maildir = NULL;
154 char buf[BUFSIZ], **ap, **argp, **arguments;
155 char *msgs[MAXARGS], *vec[MAXARGS];
158 char *attach = (char *)0; /* header field name for attachments */
159 int attachformat = 0; /* mhbuild format specifier for attachments */
162 setlocale(LC_ALL, "");
164 invo_name = r1bindex (argv[0], '/');
166 /* read user profile/context */
169 arguments = getarguments (invo_name, argc, argv, 1);
172 vec[vecp++] = "-library";
173 vec[vecp++] = getcpy (m_maildir (""));
175 if ((cp = context_find ("fileproc"))) {
176 vec[vecp++] = "-fileproc";
180 if ((cp = context_find ("mhlproc"))) {
181 vec[vecp++] = "-mhlproc";
185 while ((cp = *argp++)) {
187 switch (smatch (++cp, switches)) {
189 ambigsw (cp, switches);
192 adios (NULL, "-%s unknown\n", cp);
195 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
196 print_help (buf, switches, 1);
199 print_version(invo_name);
203 msgs[msgp++] = draft;
208 adios (NULL, "only one draft folder at a time!");
209 if (!(cp = *argp++) || *cp == '-')
210 adios (NULL, "missing argument to %s", argp[-2]);
211 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
212 *cp != '@' ? TFOLDER : TSUBCWF);
215 if (!(cp = *argp++) || *cp == '-')
216 adios (NULL, "missing argument to %s", argp[-2]);
232 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
233 adios (NULL, "missing argument to %s", argp[-2]);
269 debugsw++; /* fall */
300 if (!(cp = *argp++) || *cp == '-')
301 adios (NULL, "missing argument to %s", argp[-2]);
306 if (!(attach = *argp++) || *attach == '-')
307 adios (NULL, "missing argument to %s", argp[-2]);
311 if (! *argp || **argp == '-')
312 adios (NULL, "missing argument to %s", argp[-1]);
314 attachformat = atoi (*argp);
315 if (attachformat < 0 ||
316 attachformat > ATTACHFORMATS - 1) {
317 advise (NULL, "unsupported attachformat %d",
331 * check for "Aliasfile:" profile entry
333 if ((cp = context_find ("Aliasfile"))) {
336 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
337 vec[vecp++] = "-alias";
342 if (dfolder == NULL) {
345 if ((cp = getenv ("mhdraft")) && *cp) {
350 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
351 if (stat (msgs[0], &st) == NOTOK)
352 adios (msgs[0], "unable to stat draft file");
353 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
354 for (status = LISTDSW; status != YESW;) {
355 if (!(argp = getans (cp, anyl)))
357 switch (status = smatch (*argp, anyl)) {
363 showfile (++argp, msgs[0]);
366 advise (NULL, "say what?");
371 for (msgnum = 0; msgnum < msgp; msgnum++)
372 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
375 if (!context_find ("path"))
376 free (path ("./", TFOLDER));
379 msgs[msgp++] = "cur";
380 maildir = m_maildir (dfolder);
382 if (chdir (maildir) == NOTOK)
383 adios (maildir, "unable to change directory to");
385 /* read folder and create message structure */
386 if (!(mp = folder_read (dfolder)))
387 adios (NULL, "unable to read folder %s", dfolder);
389 /* check for empty folder */
391 adios (NULL, "no messages in %s", dfolder);
393 /* parse all the message ranges/sequences and set SELECTED */
394 for (msgnum = 0; msgnum < msgp; msgnum++)
395 if (!m_convert (mp, msgs[msgnum]))
397 seq_setprev (mp); /* set the previous-sequence */
399 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
400 if (is_selected (mp, msgnum)) {
401 msgs[msgp++] = getcpy (m_name (msgnum));
402 unset_exists (mp, msgnum);
406 mp->msgflags |= SEQMOD;
414 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
415 if ((cp = context_find ("signature")) && *cp)
416 m_putenv ("SIGNATURE", cp);
418 for (msgnum = 0; msgnum < msgp; msgnum++)
419 if (stat (msgs[msgnum], &st) == NOTOK)
420 adios (msgs[msgnum], "unable to stat draft file");
422 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
424 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
426 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
427 altmsg = NULL; /* used by dist interface - see below */
429 if ((cp = getenv ("mhdist"))
431 && (distsw = atoi (cp))
433 vec[vecp++] = "-dist";
434 distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL));
436 if (link (altmsg, distfile) == NOTOK) {
439 && errno != EISREMOTE
440 #endif /* EISREMOTE */
442 adios (distfile, "unable to link %s to", altmsg);
444 distfile = getcpy (m_mktemp2(NULL, invo_name, NULL, NULL));
449 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
450 adios (altmsg, "unable to open");
452 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
453 adios (distfile, "unable to write");
454 cpydata (in, out, altmsg, distfile);
463 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
472 vec[0] = r1bindex (postproc, '/');
475 for (msgnum = 0; msgnum < msgp; msgnum++) {
476 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
487 context_save (); /* save the context file */