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.
16 static struct swit switches[] = {
18 { "alias aliasfile", 0 },
24 { "draftfolder +folder", 6 },
26 { "draftmessage msg", 6 },
28 { "nodraftfolder", 0 },
30 { "filter filterfile", 0 },
54 { "split seconds", 0 },
68 { "width columns", 0 },
74 { "dashstuffing", -12 },
75 #define NBITSTUFFSW 29
76 { "nodashstuffing", -14 },
86 { "client host", -6 },
93 #define ATTACHFORMATSW 41
94 { "attachformat", 7 },
96 { "port server-port-name/number" , 4 },
100 static struct swit anyl[] = {
110 extern int debugsw; /* from sendsbr.c */
118 extern char *altmsg; /* .. */
119 extern char *annotext;
120 extern char *distfile;
124 main (int argc, char **argv)
126 int msgp = 0, distsw = 0, vecp = 1;
127 int isdf = 0, mime = 0;
129 char *cp, *dfolder = NULL, *maildir = NULL;
130 char buf[BUFSIZ], **ap, **argp, **arguments;
131 char *msgs[MAXARGS], *vec[MAXARGS];
134 char *attach = (char *)0; /* header field name for attachments */
135 int attachformat = 0; /* mhbuild format specifier for attachments */
141 setlocale(LC_ALL, "");
143 invo_name = r1bindex (argv[0], '/');
145 /* read user profile/context */
148 arguments = getarguments (invo_name, argc, argv, 1);
151 vec[vecp++] = "-library";
152 vec[vecp++] = getcpy (m_maildir (""));
154 while ((cp = *argp++)) {
156 switch (smatch (++cp, switches)) {
158 ambigsw (cp, switches);
161 adios (NULL, "-%s unknown\n", cp);
164 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
165 print_help (buf, switches, 1);
168 print_version(invo_name);
172 msgs[msgp++] = draft;
177 adios (NULL, "only one draft folder at a time!");
178 if (!(cp = *argp++) || *cp == '-')
179 adios (NULL, "missing argument to %s", argp[-2]);
180 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
181 *cp != '@' ? TFOLDER : TSUBCWF);
184 if (!(cp = *argp++) || *cp == '-')
185 adios (NULL, "missing argument to %s", argp[-2]);
201 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
202 adios (NULL, "missing argument to %s", argp[-2]);
238 debugsw++; /* fall */
263 if (!(cp = *argp++) || *cp == '-')
264 adios (NULL, "missing argument to %s", argp[-2]);
269 if (!(attach = *argp++) || *attach == '-')
270 adios (NULL, "missing argument to %s", argp[-2]);
274 if (! *argp || **argp == '-')
275 adios (NULL, "missing argument to %s", argp[-1]);
277 attachformat = atoi (*argp);
278 if (attachformat < 0 ||
279 attachformat > ATTACHFORMATS - 1) {
280 advise (NULL, "unsupported attachformat %d",
294 * check for "Aliasfile:" profile entry
296 if ((cp = context_find ("Aliasfile"))) {
299 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
300 vec[vecp++] = "-alias";
305 if (dfolder == NULL) {
308 if ((cp = getenv ("mhdraft")) && *cp) {
313 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
314 if (stat (msgs[0], &st) == NOTOK)
315 adios (msgs[0], "unable to stat draft file");
316 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
317 for (status = LISTDSW; status != YESW;) {
318 if (!(argp = getans (cp, anyl)))
320 switch (status = smatch (*argp, anyl)) {
326 showfile (++argp, msgs[0]);
329 advise (NULL, "say what?");
334 for (msgnum = 0; msgnum < msgp; msgnum++)
335 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
338 if (!context_find ("path"))
339 free (path ("./", TFOLDER));
342 msgs[msgp++] = "cur";
343 maildir = m_maildir (dfolder);
345 if (chdir (maildir) == NOTOK)
346 adios (maildir, "unable to change directory to");
348 /* read folder and create message structure */
349 if (!(mp = folder_read (dfolder)))
350 adios (NULL, "unable to read folder %s", dfolder);
352 /* check for empty folder */
354 adios (NULL, "no messages in %s", dfolder);
356 /* parse all the message ranges/sequences and set SELECTED */
357 for (msgnum = 0; msgnum < msgp; msgnum++)
358 if (!m_convert (mp, msgs[msgnum]))
360 seq_setprev (mp); /* set the previous-sequence */
362 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
363 if (is_selected (mp, msgnum)) {
364 msgs[msgp++] = getcpy (m_name (msgnum));
365 unset_exists (mp, msgnum);
369 mp->msgflags |= SEQMOD;
377 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
378 if ((cp = context_find ("signature")) && *cp)
379 m_putenv ("SIGNATURE", cp);
382 snprintf (buf, sizeof(buf), "%s/.signature", mypath);
383 if ((fp = fopen (buf, "r")) != NULL
384 && fgets (buf, sizeof buf, fp) != NULL) {
386 if (cp = strchr (buf, '\n'))
388 m_putenv ("SIGNATURE", buf);
393 for (msgnum = 0; msgnum < msgp; msgnum++)
394 if (stat (msgs[msgnum], &st) == NOTOK)
395 adios (msgs[msgnum], "unable to stat draft file");
397 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
399 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
401 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
402 altmsg = NULL; /* used by dist interface - see below */
404 if ((cp = getenv ("mhdist"))
406 && (distsw = atoi (cp))
408 vec[vecp++] = "-dist";
409 distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL));
410 if (link (altmsg, distfile) == NOTOK) {
413 && errno != EISREMOTE
414 #endif /* EISREMOTE */
416 adios (distfile, "unable to link %s to", altmsg);
418 distfile = getcpy (m_mktemp2(NULL, invo_name, NULL, NULL));
423 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
424 adios (altmsg, "unable to open");
426 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
427 adios (distfile, "unable to write");
428 cpydata (in, out, altmsg, distfile);
437 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
446 vec[0] = r1bindex (postproc, '/');
449 for (msgnum = 0; msgnum < msgp; msgnum++) {
450 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
461 context_save (); /* save the context file */