2 * send.c -- send a composed message
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
15 static struct swit switches[] = {
17 { "alias aliasfile", 0 },
23 { "draftfolder +folder", 6 },
25 { "draftmessage msg", 6 },
27 { "nodraftfolder", 0 },
29 { "filter filterfile", 0 },
53 { "split seconds", 0 },
67 { "width columns", 0 },
73 { "dashstuffing", -12 },
74 #define NBITSTUFFSW 29
75 { "nodashstuffing", -14 },
85 { "client host", -6 },
92 #define ATTACHFORMATSW 41
93 { "attachformat", 7 },
95 { "port server-port-name/number" , 4 },
99 static struct swit anyl[] = {
109 extern int debugsw; /* from sendsbr.c */
117 extern char *altmsg; /* .. */
118 extern char *annotext;
119 extern char *distfile;
123 main (int argc, char **argv)
125 int msgp = 0, distsw = 0, vecp = 1;
126 int isdf = 0, mime = 0;
128 char *cp, *dfolder = NULL, *maildir = NULL;
129 char buf[BUFSIZ], **ap, **argp, **arguments;
130 char *msgs[MAXARGS], *vec[MAXARGS];
133 char *attach = (char *)0; /* header field name for attachments */
134 int attachformat = 0; /* mhbuild format specifier for attachments */
140 setlocale(LC_ALL, "");
142 invo_name = r1bindex (argv[0], '/');
144 /* read user profile/context */
147 arguments = getarguments (invo_name, argc, argv, 1);
150 vec[vecp++] = "-library";
151 vec[vecp++] = getcpy (m_maildir (""));
153 while ((cp = *argp++)) {
155 switch (smatch (++cp, switches)) {
157 ambigsw (cp, switches);
160 adios (NULL, "-%s unknown\n", cp);
163 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
164 print_help (buf, switches, 1);
167 print_version(invo_name);
171 msgs[msgp++] = draft;
176 adios (NULL, "only one draft folder at a time!");
177 if (!(cp = *argp++) || *cp == '-')
178 adios (NULL, "missing argument to %s", argp[-2]);
179 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
180 *cp != '@' ? TFOLDER : TSUBCWF);
183 if (!(cp = *argp++) || *cp == '-')
184 adios (NULL, "missing argument to %s", argp[-2]);
200 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
201 adios (NULL, "missing argument to %s", argp[-2]);
237 debugsw++; /* fall */
262 if (!(cp = *argp++) || *cp == '-')
263 adios (NULL, "missing argument to %s", argp[-2]);
268 if (!(attach = *argp++) || *attach == '-')
269 adios (NULL, "missing argument to %s", argp[-2]);
273 if (! *argp || **argp == '-')
274 adios (NULL, "missing argument to %s", argp[-1]);
276 attachformat = atoi (*argp);
277 if (attachformat < 0 ||
278 attachformat > ATTACHFORMATS - 1) {
279 advise (NULL, "unsupported attachformat %d",
293 * check for "Aliasfile:" profile entry
295 if ((cp = context_find ("Aliasfile"))) {
298 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
299 vec[vecp++] = "-alias";
304 if (dfolder == NULL) {
307 if ((cp = getenv ("mhdraft")) && *cp) {
312 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
313 if (stat (msgs[0], &st) == NOTOK)
314 adios (msgs[0], "unable to stat draft file");
315 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
316 for (status = LISTDSW; status != YESW;) {
317 if (!(argp = getans (cp, anyl)))
319 switch (status = smatch (*argp, anyl)) {
325 showfile (++argp, msgs[0]);
328 advise (NULL, "say what?");
333 for (msgnum = 0; msgnum < msgp; msgnum++)
334 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
337 if (!context_find ("path"))
338 free (path ("./", TFOLDER));
341 msgs[msgp++] = "cur";
342 maildir = m_maildir (dfolder);
344 if (chdir (maildir) == NOTOK)
345 adios (maildir, "unable to change directory to");
347 /* read folder and create message structure */
348 if (!(mp = folder_read (dfolder)))
349 adios (NULL, "unable to read folder %s", dfolder);
351 /* check for empty folder */
353 adios (NULL, "no messages in %s", dfolder);
355 /* parse all the message ranges/sequences and set SELECTED */
356 for (msgnum = 0; msgnum < msgp; msgnum++)
357 if (!m_convert (mp, msgs[msgnum]))
359 seq_setprev (mp); /* set the previous-sequence */
361 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
362 if (is_selected (mp, msgnum)) {
363 msgs[msgp++] = getcpy (m_name (msgnum));
364 unset_exists (mp, msgnum);
368 mp->msgflags |= SEQMOD;
376 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
377 if ((cp = context_find ("signature")) && *cp)
378 m_putenv ("SIGNATURE", cp);
381 snprintf (buf, sizeof(buf), "%s/.signature", mypath);
382 if ((fp = fopen (buf, "r")) != NULL
383 && fgets (buf, sizeof buf, fp) != NULL) {
385 if (cp = strchr (buf, '\n'))
387 m_putenv ("SIGNATURE", buf);
392 for (msgnum = 0; msgnum < msgp; msgnum++)
393 if (stat (msgs[msgnum], &st) == NOTOK)
394 adios (msgs[msgnum], "unable to stat draft file");
396 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
398 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
400 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
401 altmsg = NULL; /* used by dist interface - see below */
403 if ((cp = getenv ("mhdist"))
405 && (distsw = atoi (cp))
407 vec[vecp++] = "-dist";
408 distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL));
409 if (link (altmsg, distfile) == NOTOK) {
412 && errno != EISREMOTE
413 #endif /* EISREMOTE */
415 adios (distfile, "unable to link %s to", altmsg);
417 distfile = getcpy (m_mktemp2(NULL, invo_name, NULL, NULL));
422 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
423 adios (altmsg, "unable to open");
425 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
426 adios (distfile, "unable to write");
427 cpydata (in, out, altmsg, distfile);
436 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
445 vec[0] = r1bindex (postproc, '/');
448 for (msgnum = 0; msgnum < msgp; msgnum++) {
449 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
460 context_save (); /* save the context file */