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 TLSminc(a) (a)
18 #else /* TLS_SUPPORT */
20 #endif /* TLS_SUPPORT */
22 static struct swit switches[] = {
24 { "alias aliasfile", 0 },
30 { "draftfolder +folder", 6 },
32 { "draftmessage msg", 6 },
34 { "nodraftfolder", 0 },
36 { "filter filterfile", 0 },
60 { "split seconds", 0 },
74 { "width columns", 0 },
80 { "dashstuffing", -12 },
81 #define NBITSTUFFSW 29
82 { "nodashstuffing", -14 },
92 { "client host", -6 },
99 #define ATTACHFORMATSW 41
100 { "attachformat", 7 },
102 { "port server-port-name/number" , 4 },
104 { "tls", TLSminc(-3) },
108 static struct swit anyl[] = {
118 extern int debugsw; /* from sendsbr.c */
126 extern char *altmsg; /* .. */
127 extern char *annotext;
128 extern char *distfile;
132 main (int argc, char **argv)
134 int msgp = 0, distsw = 0, vecp = 1;
135 int isdf = 0, mime = 0;
137 char *cp, *dfolder = NULL, *maildir = NULL;
138 char buf[BUFSIZ], **ap, **argp, **arguments;
139 char *msgs[MAXARGS], *vec[MAXARGS];
142 char *attach = (char *)0; /* header field name for attachments */
143 int attachformat = 0; /* mhbuild format specifier for attachments */
149 setlocale(LC_ALL, "");
151 invo_name = r1bindex (argv[0], '/');
153 /* read user profile/context */
156 arguments = getarguments (invo_name, argc, argv, 1);
159 vec[vecp++] = "-library";
160 vec[vecp++] = getcpy (m_maildir (""));
162 while ((cp = *argp++)) {
164 switch (smatch (++cp, switches)) {
166 ambigsw (cp, switches);
169 adios (NULL, "-%s unknown\n", cp);
172 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
173 print_help (buf, switches, 1);
176 print_version(invo_name);
180 msgs[msgp++] = draft;
185 adios (NULL, "only one draft folder at a time!");
186 if (!(cp = *argp++) || *cp == '-')
187 adios (NULL, "missing argument to %s", argp[-2]);
188 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
189 *cp != '@' ? TFOLDER : TSUBCWF);
192 if (!(cp = *argp++) || *cp == '-')
193 adios (NULL, "missing argument to %s", argp[-2]);
209 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
210 adios (NULL, "missing argument to %s", argp[-2]);
246 debugsw++; /* fall */
272 if (!(cp = *argp++) || *cp == '-')
273 adios (NULL, "missing argument to %s", argp[-2]);
278 if (!(attach = *argp++) || *attach == '-')
279 adios (NULL, "missing argument to %s", argp[-2]);
283 if (! *argp || **argp == '-')
284 adios (NULL, "missing argument to %s", argp[-1]);
286 attachformat = atoi (*argp);
287 if (attachformat < 0 ||
288 attachformat > ATTACHFORMATS - 1) {
289 advise (NULL, "unsupported attachformat %d",
303 * check for "Aliasfile:" profile entry
305 if ((cp = context_find ("Aliasfile"))) {
308 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
309 vec[vecp++] = "-alias";
314 if (dfolder == NULL) {
317 if ((cp = getenv ("mhdraft")) && *cp) {
322 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
323 if (stat (msgs[0], &st) == NOTOK)
324 adios (msgs[0], "unable to stat draft file");
325 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
326 for (status = LISTDSW; status != YESW;) {
327 if (!(argp = getans (cp, anyl)))
329 switch (status = smatch (*argp, anyl)) {
335 showfile (++argp, msgs[0]);
338 advise (NULL, "say what?");
343 for (msgnum = 0; msgnum < msgp; msgnum++)
344 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
347 if (!context_find ("path"))
348 free (path ("./", TFOLDER));
351 msgs[msgp++] = "cur";
352 maildir = m_maildir (dfolder);
354 if (chdir (maildir) == NOTOK)
355 adios (maildir, "unable to change directory to");
357 /* read folder and create message structure */
358 if (!(mp = folder_read (dfolder)))
359 adios (NULL, "unable to read folder %s", dfolder);
361 /* check for empty folder */
363 adios (NULL, "no messages in %s", dfolder);
365 /* parse all the message ranges/sequences and set SELECTED */
366 for (msgnum = 0; msgnum < msgp; msgnum++)
367 if (!m_convert (mp, msgs[msgnum]))
369 seq_setprev (mp); /* set the previous-sequence */
371 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
372 if (is_selected (mp, msgnum)) {
373 msgs[msgp++] = getcpy (m_name (msgnum));
374 unset_exists (mp, msgnum);
378 mp->msgflags |= SEQMOD;
386 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
387 if ((cp = context_find ("signature")) && *cp)
388 m_putenv ("SIGNATURE", cp);
391 snprintf (buf, sizeof(buf), "%s/.signature", mypath);
392 if ((fp = fopen (buf, "r")) != NULL
393 && fgets (buf, sizeof buf, fp) != NULL) {
395 if (cp = strchr (buf, '\n'))
397 m_putenv ("SIGNATURE", buf);
402 for (msgnum = 0; msgnum < msgp; msgnum++)
403 if (stat (msgs[msgnum], &st) == NOTOK)
404 adios (msgs[msgnum], "unable to stat draft file");
406 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
408 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
410 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
411 altmsg = NULL; /* used by dist interface - see below */
413 if ((cp = getenv ("mhdist"))
415 && (distsw = atoi (cp))
417 vec[vecp++] = "-dist";
418 distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL));
419 if (link (altmsg, distfile) == NOTOK) {
422 && errno != EISREMOTE
423 #endif /* EISREMOTE */
425 adios (distfile, "unable to link %s to", altmsg);
427 distfile = getcpy (m_mktemp2(NULL, invo_name, NULL, NULL));
432 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
433 adios (altmsg, "unable to open");
435 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
436 adios (distfile, "unable to write");
437 cpydata (in, out, altmsg, distfile);
446 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
455 vec[0] = r1bindex (postproc, '/');
458 for (msgnum = 0; msgnum < msgp; msgnum++) {
459 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
470 context_save (); /* save the context file */