3 * send.c -- send a composed message
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
19 # define SASLminc(a) (a)
20 #else /* CYRUS_SASL */
21 # define SASLminc(a) 0
22 #endif /* CYRUS_SASL */
24 static struct swit switches[] = {
26 { "alias aliasfile", 0 },
32 { "draftfolder +folder", 6 },
34 { "draftmessage msg", 6 },
36 { "nodraftfolder", 0 },
38 { "filter filterfile", 0 },
62 { "split seconds", 0 },
76 { "width columns", 0 },
82 { "dashstuffing", -12 },
83 #define NBITSTUFFSW 29
84 { "nodashstuffing", -14 },
94 { "client host", -6 },
96 { "server host", -6 },
100 { "sasl", SASLminc(-4) },
101 #define SASLMECHSW 38
102 { "saslmech", SASLminc(-5) },
104 { "user", SASLminc(-4) },
107 #define ATTACHFORMATSW 41
108 { "attachformat", 7 },
112 static struct swit anyl[] = {
122 extern int debugsw; /* from sendsbr.c */
130 extern char *altmsg; /* .. */
131 extern char *annotext;
132 extern char *distfile;
136 main (int argc, char **argv)
138 int msgp = 0, distsw = 0, vecp = 1;
139 int isdf = 0, mime = 0;
141 char *cp, *dfolder = NULL, *maildir = NULL;
142 char buf[BUFSIZ], **ap, **argp, **arguments;
143 char *msgs[MAXARGS], *vec[MAXARGS];
146 char *attach = (char *)0; /* header field name for attachments */
147 int attachformat = 0; /* mhbuild format specifier for attachments */
153 setlocale(LC_ALL, "");
155 invo_name = r1bindex (argv[0], '/');
157 /* read user profile/context */
160 arguments = getarguments (invo_name, argc, argv, 1);
163 vec[vecp++] = "-library";
164 vec[vecp++] = getcpy (m_maildir (""));
166 while ((cp = *argp++)) {
168 switch (smatch (++cp, switches)) {
170 ambigsw (cp, switches);
173 adios (NULL, "-%s unknown\n", cp);
176 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
177 print_help (buf, switches, 1);
180 print_version(invo_name);
184 msgs[msgp++] = draft;
189 adios (NULL, "only one draft folder at a time!");
190 if (!(cp = *argp++) || *cp == '-')
191 adios (NULL, "missing argument to %s", argp[-2]);
192 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
193 *cp != '@' ? TFOLDER : TSUBCWF);
196 if (!(cp = *argp++) || *cp == '-')
197 adios (NULL, "missing argument to %s", argp[-2]);
213 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
214 adios (NULL, "missing argument to %s", argp[-2]);
250 debugsw++; /* fall */
277 if (!(cp = *argp++) || *cp == '-')
278 adios (NULL, "missing argument to %s", argp[-2]);
283 if (!(attach = *argp++) || *attach == '-')
284 adios (NULL, "missing argument to %s", argp[-2]);
288 if (! *argp || **argp == '-')
289 adios (NULL, "missing argument to %s", argp[-1]);
291 attachformat = atoi (*argp);
292 if (attachformat < 0 ||
293 attachformat > ATTACHFORMATS - 1) {
294 advise (NULL, "unsupported attachformat %d",
308 * check for "Aliasfile:" profile entry
310 if ((cp = context_find ("Aliasfile"))) {
313 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
314 vec[vecp++] = "-alias";
319 if (dfolder == NULL) {
322 if ((cp = getenv ("mhdraft")) && *cp) {
327 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
328 if (stat (msgs[0], &st) == NOTOK)
329 adios (msgs[0], "unable to stat draft file");
330 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
331 for (status = LISTDSW; status != YESW;) {
332 if (!(argp = getans (cp, anyl)))
334 switch (status = smatch (*argp, anyl)) {
340 showfile (++argp, msgs[0]);
343 advise (NULL, "say what?");
348 for (msgnum = 0; msgnum < msgp; msgnum++)
349 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
352 if (!context_find ("path"))
353 free (path ("./", TFOLDER));
356 msgs[msgp++] = "cur";
357 maildir = m_maildir (dfolder);
359 if (chdir (maildir) == NOTOK)
360 adios (maildir, "unable to change directory to");
362 /* read folder and create message structure */
363 if (!(mp = folder_read (dfolder)))
364 adios (NULL, "unable to read folder %s", dfolder);
366 /* check for empty folder */
368 adios (NULL, "no messages in %s", dfolder);
370 /* parse all the message ranges/sequences and set SELECTED */
371 for (msgnum = 0; msgnum < msgp; msgnum++)
372 if (!m_convert (mp, msgs[msgnum]))
374 seq_setprev (mp); /* set the previous-sequence */
376 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
377 if (is_selected (mp, msgnum)) {
378 msgs[msgp++] = getcpy (m_name (msgnum));
379 unset_exists (mp, msgnum);
383 mp->msgflags |= SEQMOD;
391 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
392 if ((cp = context_find ("signature")) && *cp)
393 m_putenv ("SIGNATURE", cp);
396 snprintf (buf, sizeof(buf), "%s/.signature", mypath);
397 if ((fp = fopen (buf, "r")) != NULL
398 && fgets (buf, sizeof buf, fp) != NULL) {
400 if (cp = strchr (buf, '\n'))
402 m_putenv ("SIGNATURE", buf);
407 for (msgnum = 0; msgnum < msgp; msgnum++)
408 if (stat (msgs[msgnum], &st) == NOTOK)
409 adios (msgs[msgnum], "unable to stat draft file");
411 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
413 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
415 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
416 altmsg = NULL; /* used by dist interface - see below */
418 if ((cp = getenv ("mhdist"))
420 && (distsw = atoi (cp))
422 vec[vecp++] = "-dist";
423 distfile = getcpy (m_scratch (altmsg, invo_name));
424 if (link (altmsg, distfile) == NOTOK) {
427 && errno != EISREMOTE
428 #endif /* EISREMOTE */
430 adios (distfile, "unable to link %s to", altmsg);
432 distfile = getcpy (m_tmpfil (invo_name));
437 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
438 adios (altmsg, "unable to open");
440 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
441 adios (distfile, "unable to write");
442 cpydata (in, out, altmsg, distfile);
451 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
460 vec[0] = r1bindex (postproc, '/');
463 for (msgnum = 0; msgnum < msgp; msgnum++) {
464 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
475 context_save (); /* save the context file */