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 },
21 { "filter filterfile", 0 },
57 { "width columns", 0 },
63 { "dashstuffing", -12 },
64 #define NBITSTUFFSW 24
65 { "nodashstuffing", -14 },
75 { "client host", -6 },
82 #define ATTACHFORMATSW 33
83 { "attachformat", 7 },
85 { "port server-port-name/number" , 4 },
89 extern int debugsw; /* from sendsbr.c */
96 extern char *altmsg; /* .. */
97 extern char *annotext;
98 extern char *distfile;
102 main (int argc, char **argv)
104 int msgp = 0, distsw = 0, vecp = 1;
107 char *cp, *maildir = NULL;
108 char buf[BUFSIZ], **ap, **argp, **arguments;
109 char *msgs[MAXARGS], *vec[MAXARGS];
112 char *attach = (char *)0; /* header field name for attachments */
113 int attachformat = 0; /* mhbuild format specifier for attachments */
119 setlocale(LC_ALL, "");
121 invo_name = r1bindex (argv[0], '/');
123 /* read user profile/context */
126 arguments = getarguments (invo_name, argc, argv, 1);
129 vec[vecp++] = "-library";
130 vec[vecp++] = getcpy (m_maildir (""));
132 while ((cp = *argp++)) {
134 switch (smatch (++cp, switches)) {
136 ambigsw (cp, switches);
139 adios (NULL, "-%s unknown\n", cp);
142 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
143 print_help (buf, switches, 1);
146 print_version(invo_name);
189 debugsw++; /* fall */
214 if (!(cp = *argp++) || *cp == '-')
215 adios (NULL, "missing argument to %s", argp[-2]);
220 if (!(attach = *argp++) || *attach == '-')
221 adios (NULL, "missing argument to %s", argp[-2]);
225 if (! *argp || **argp == '-')
226 adios (NULL, "missing argument to %s", argp[-1]);
228 attachformat = atoi (*argp);
229 if (attachformat < 0 ||
230 attachformat > ATTACHFORMATS - 1) {
231 advise (NULL, "unsupported attachformat %d",
245 * check for "Aliasfile:" profile entry
247 if ((cp = context_find ("Aliasfile"))) {
250 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
251 vec[vecp++] = "-alias";
256 if (!context_find ("path"))
257 free (path ("./", TFOLDER));
260 msgs[msgp++] = "cur";
261 maildir = m_maildir (draftfolder);
263 if (chdir (maildir) == NOTOK)
264 adios (maildir, "unable to change directory to");
266 /* read folder and create message structure */
267 if (!(mp = folder_read (draftfolder)))
268 adios (NULL, "unable to read draft folder %s", draftfolder);
270 /* check for empty folder */
272 adios (NULL, "no messages in draft folder %s", draftfolder);
274 /* parse all the message ranges/sequences and set SELECTED */
275 for (msgnum = 0; msgnum < msgp; msgnum++)
276 if (!m_convert (mp, msgs[msgnum]))
278 seq_setprev (mp); /* set the previous-sequence */
280 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
281 if (is_selected (mp, msgnum)) {
282 msgs[msgp++] = getcpy (m_name (msgnum));
283 unset_exists (mp, msgnum);
287 mp->msgflags |= SEQMOD;
290 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
291 if ((cp = context_find ("signature")) && *cp)
292 m_putenv ("SIGNATURE", cp);
295 snprintf (buf, sizeof(buf), "%s/.signature", mypath);
296 if ((fp = fopen (buf, "r")) != NULL
297 && fgets (buf, sizeof buf, fp) != NULL) {
299 if (cp = strchr (buf, '\n'))
301 m_putenv ("SIGNATURE", buf);
306 for (msgnum = 0; msgnum < msgp; msgnum++)
307 if (stat (msgs[msgnum], &st) == NOTOK)
308 adios (msgs[msgnum], "unable to stat draft file");
310 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
312 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
314 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
315 altmsg = NULL; /* used by dist interface - see below */
317 if ((cp = getenv ("mhdist"))
319 && (distsw = atoi (cp))
321 vec[vecp++] = "-dist";
322 distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL));
323 if (link (altmsg, distfile) == NOTOK) {
326 && errno != EISREMOTE
327 #endif /* EISREMOTE */
329 adios (distfile, "unable to link %s to", altmsg);
331 distfile = getcpy (m_mktemp2(NULL, invo_name, NULL, NULL));
336 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
337 adios (altmsg, "unable to open");
339 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
340 adios (distfile, "unable to write");
341 cpydata (in, out, altmsg, distfile);
350 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
359 vec[0] = r1bindex (postproc, '/');
362 for (msgnum = 0; msgnum < msgp; msgnum++) {
363 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
374 context_save (); /* save the context file */