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) },
105 #define SASLMECHSW 38
106 { "saslmech mechanism", SASLminc(-5) },
108 { "user username", SASLminc(-4) },
111 #define ATTACHFORMATSW 41
112 { "attachformat", 7 },
114 { "port server-port-name/number" , 4 },
116 { "tls", TLSminc(-3) },
120 static struct swit anyl[] = {
130 extern int debugsw; /* from sendsbr.c */
138 extern char *altmsg; /* .. */
139 extern char *annotext;
140 extern char *distfile;
144 main (int argc, char **argv)
146 int msgp = 0, distsw = 0, vecp = 1;
147 int isdf = 0, mime = 0;
149 char *cp, *dfolder = NULL, *maildir = NULL;
150 char buf[BUFSIZ], **ap, **argp, **arguments;
151 char *msgs[MAXARGS], *vec[MAXARGS];
154 char *attach = (char *)0; /* header field name for attachments */
155 int attachformat = 0; /* mhbuild format specifier for attachments */
161 setlocale(LC_ALL, "");
163 invo_name = r1bindex (argv[0], '/');
165 /* read user profile/context */
168 arguments = getarguments (invo_name, argc, argv, 1);
171 vec[vecp++] = "-library";
172 vec[vecp++] = getcpy (m_maildir (""));
174 if ((cp = context_find ("fileproc"))) {
175 vec[vecp++] = "-fileproc";
179 if ((cp = context_find ("mhlproc"))) {
180 vec[vecp++] = "-mhlproc";
184 while ((cp = *argp++)) {
186 switch (smatch (++cp, switches)) {
188 ambigsw (cp, switches);
191 adios (NULL, "-%s unknown\n", cp);
194 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
195 print_help (buf, switches, 1);
198 print_version(invo_name);
202 msgs[msgp++] = draft;
207 adios (NULL, "only one draft folder at a time!");
208 if (!(cp = *argp++) || *cp == '-')
209 adios (NULL, "missing argument to %s", argp[-2]);
210 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
211 *cp != '@' ? TFOLDER : TSUBCWF);
214 if (!(cp = *argp++) || *cp == '-')
215 adios (NULL, "missing argument to %s", argp[-2]);
231 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
232 adios (NULL, "missing argument to %s", argp[-2]);
268 debugsw++; /* fall */
297 if (!(cp = *argp++) || *cp == '-')
298 adios (NULL, "missing argument to %s", argp[-2]);
303 if (!(attach = *argp++) || *attach == '-')
304 adios (NULL, "missing argument to %s", argp[-2]);
308 if (! *argp || **argp == '-')
309 adios (NULL, "missing argument to %s", argp[-1]);
311 attachformat = atoi (*argp);
312 if (attachformat < 0 ||
313 attachformat > ATTACHFORMATS - 1) {
314 advise (NULL, "unsupported attachformat %d",
328 * check for "Aliasfile:" profile entry
330 if ((cp = context_find ("Aliasfile"))) {
333 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
334 vec[vecp++] = "-alias";
339 if (dfolder == NULL) {
342 if ((cp = getenv ("mhdraft")) && *cp) {
347 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
348 if (stat (msgs[0], &st) == NOTOK)
349 adios (msgs[0], "unable to stat draft file");
350 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
351 for (status = LISTDSW; status != YESW;) {
352 if (!(argp = getans (cp, anyl)))
354 switch (status = smatch (*argp, anyl)) {
360 showfile (++argp, msgs[0]);
363 advise (NULL, "say what?");
368 for (msgnum = 0; msgnum < msgp; msgnum++)
369 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
372 if (!context_find ("path"))
373 free (path ("./", TFOLDER));
376 msgs[msgp++] = "cur";
377 maildir = m_maildir (dfolder);
379 if (chdir (maildir) == NOTOK)
380 adios (maildir, "unable to change directory to");
382 /* read folder and create message structure */
383 if (!(mp = folder_read (dfolder)))
384 adios (NULL, "unable to read folder %s", dfolder);
386 /* check for empty folder */
388 adios (NULL, "no messages in %s", dfolder);
390 /* parse all the message ranges/sequences and set SELECTED */
391 for (msgnum = 0; msgnum < msgp; msgnum++)
392 if (!m_convert (mp, msgs[msgnum]))
394 seq_setprev (mp); /* set the previous-sequence */
396 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
397 if (is_selected (mp, msgnum)) {
398 msgs[msgp++] = getcpy (m_name (msgnum));
399 unset_exists (mp, msgnum);
403 mp->msgflags |= SEQMOD;
411 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
412 if ((cp = context_find ("signature")) && *cp)
413 m_putenv ("SIGNATURE", cp);
416 snprintf (buf, sizeof(buf), "%s/.signature", mypath);
417 if ((fp = fopen (buf, "r")) != NULL
418 && fgets (buf, sizeof buf, fp) != NULL) {
420 if (cp = strchr (buf, '\n'))
422 m_putenv ("SIGNATURE", buf);
427 for (msgnum = 0; msgnum < msgp; msgnum++)
428 if (stat (msgs[msgnum], &st) == NOTOK)
429 adios (msgs[msgnum], "unable to stat draft file");
431 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
433 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
435 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
436 altmsg = NULL; /* used by dist interface - see below */
438 if ((cp = getenv ("mhdist"))
440 && (distsw = atoi (cp))
442 vec[vecp++] = "-dist";
443 distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL));
444 if (link (altmsg, distfile) == NOTOK) {
447 && errno != EISREMOTE
448 #endif /* EISREMOTE */
450 adios (distfile, "unable to link %s to", altmsg);
452 distfile = getcpy (m_mktemp2(NULL, invo_name, NULL, NULL));
457 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
458 adios (altmsg, "unable to open");
460 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
461 adios (distfile, "unable to write");
462 cpydata (in, out, altmsg, distfile);
471 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
480 vec[0] = r1bindex (postproc, '/');
483 for (msgnum = 0; msgnum < msgp; msgnum++) {
484 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
495 context_save (); /* save the context file */