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),
143 "%s [file] [switches]",
145 print_help(buf, switches, 1);
148 print_version(invo_name);
191 debugsw++; /* fall */
216 if (!(cp = *argp++) || *cp == '-')
217 adios(NULL, "missing argument to %s", argp[-2]);
222 if (!(attach = *argp++) ||
224 adios(NULL, "missing argument to %s", argp[-2]);
228 if (! *argp || **argp == '-')
229 adios(NULL, "missing argument to %s", argp[-1]);
231 attachformat = atoi(*argp);
232 if (attachformat < 0 ||
233 attachformat > ATTACHFORMATS - 1) {
234 advise(NULL, "unsupported attachformat %d",
248 ** check for "Aliasfile:" profile entry
250 if ((cp = context_find("Aliasfile"))) {
253 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap;
255 vec[vecp++] = "-alias";
260 if (!context_find("path"))
261 free(path("./", TFOLDER));
264 msgs[msgp++] = "cur";
265 maildir = m_maildir(draftfolder);
267 if (chdir(maildir) == NOTOK)
268 adios(maildir, "unable to change directory to");
270 /* read folder and create message structure */
271 if (!(mp = folder_read(draftfolder)))
272 adios(NULL, "unable to read draft folder %s", draftfolder);
274 /* check for empty folder */
276 adios(NULL, "no messages in draft folder %s", draftfolder);
278 /* parse all the message ranges/sequences and set SELECTED */
279 for (msgnum = 0; msgnum < msgp; msgnum++)
280 if (!m_convert(mp, msgs[msgnum]))
282 seq_setprev(mp); /* set the previous-sequence */
284 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
285 if (is_selected(mp, msgnum)) {
286 msgs[msgp++] = getcpy(m_name(msgnum));
287 unset_exists(mp, msgnum);
291 mp->msgflags |= SEQMOD;
294 if ((cp = getenv("SIGNATURE")) == NULL || *cp == 0)
295 if ((cp = context_find("signature")) && *cp)
296 m_putenv("SIGNATURE", cp);
299 snprintf(buf, sizeof(buf), "%s/.signature", mypath);
300 if ((fp = fopen(buf, "r")) != NULL &&
301 fgets(buf, sizeof buf, fp) != NULL) {
303 if (cp = strchr(buf, '\n'))
305 m_putenv("SIGNATURE", buf);
310 for (msgnum = 0; msgnum < msgp; msgnum++)
311 if (stat(msgs[msgnum], &st) == NOTOK)
312 adios(msgs[msgnum], "unable to stat draft file");
314 if ((annotext = getenv("mhannotate")) == NULL || *annotext == 0)
316 if (annotext && ((cp = getenv("mhinplace")) != NULL && *cp != 0))
318 if ((altmsg = getenv("mhaltmsg")) == NULL || *altmsg == 0)
319 altmsg = NULL; /* used by dist interface - see below */
321 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi (cp)) && altmsg) {
322 vec[vecp++] = "-dist";
323 distfile = getcpy(m_mktemp2(altmsg, invo_name, NULL, NULL));
324 if (link(altmsg, distfile) == NOTOK) {
327 && errno != EISREMOTE
328 #endif /* EISREMOTE */
330 adios(distfile, "unable to link %s to",
333 distfile = getcpy(m_mktemp2(NULL, invo_name,
339 if ((in = open(altmsg, O_RDONLY)) == NOTOK)
340 adios(altmsg, "unable to open");
342 if ((out = creat(distfile,
343 (int) st.st_mode & 0777))
345 adios(distfile, "unable to write");
346 cpydata(in, out, altmsg, distfile);
355 if (altmsg == NULL || stat(altmsg, &st) == NOTOK) {
364 vec[0] = r1bindex(postproc, '/');
367 for (msgnum = 0; msgnum < msgp; msgnum++) {
368 switch (sendsbr(vec, vecp, msgs[msgnum], &st, 1, attach,
379 context_save(); /* save the context file */