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 },
65 { "width columns", 0 },
71 { "dashstuffing", -12 },
72 #define NBITSTUFFSW 29
73 { "nodashstuffing", -14 },
83 { "client host", -6 },
90 #define ATTACHFORMATSW 41
91 { "attachformat", 7 },
93 { "port server-port-name/number" , 4 },
97 static struct swit anyl[] = {
107 extern int debugsw; /* from sendsbr.c */
114 extern char *altmsg; /* .. */
115 extern char *annotext;
116 extern char *distfile;
120 main (int argc, char **argv)
122 int msgp = 0, distsw = 0, vecp = 1;
123 int isdf = 0, mime = 0;
125 char *cp, *dfolder = NULL, *maildir = NULL;
126 char buf[BUFSIZ], **ap, **argp, **arguments;
127 char *msgs[MAXARGS], *vec[MAXARGS];
130 char *attach = (char *)0; /* header field name for attachments */
131 int attachformat = 0; /* mhbuild format specifier for attachments */
137 setlocale(LC_ALL, "");
139 invo_name = r1bindex (argv[0], '/');
141 /* read user profile/context */
144 arguments = getarguments (invo_name, argc, argv, 1);
147 vec[vecp++] = "-library";
148 vec[vecp++] = getcpy (m_maildir (""));
150 while ((cp = *argp++)) {
152 switch (smatch (++cp, switches)) {
154 ambigsw (cp, switches);
157 adios (NULL, "-%s unknown\n", cp);
160 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
161 print_help (buf, switches, 1);
164 print_version(invo_name);
168 msgs[msgp++] = draft;
173 adios (NULL, "only one draft folder at a time!");
174 if (!(cp = *argp++) || *cp == '-')
175 adios (NULL, "missing argument to %s", argp[-2]);
176 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
177 *cp != '@' ? TFOLDER : TSUBCWF);
180 if (!(cp = *argp++) || *cp == '-')
181 adios (NULL, "missing argument to %s", argp[-2]);
229 debugsw++; /* fall */
254 if (!(cp = *argp++) || *cp == '-')
255 adios (NULL, "missing argument to %s", argp[-2]);
260 if (!(attach = *argp++) || *attach == '-')
261 adios (NULL, "missing argument to %s", argp[-2]);
265 if (! *argp || **argp == '-')
266 adios (NULL, "missing argument to %s", argp[-1]);
268 attachformat = atoi (*argp);
269 if (attachformat < 0 ||
270 attachformat > ATTACHFORMATS - 1) {
271 advise (NULL, "unsupported attachformat %d",
285 * check for "Aliasfile:" profile entry
287 if ((cp = context_find ("Aliasfile"))) {
290 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
291 vec[vecp++] = "-alias";
296 if (dfolder == NULL) {
299 if ((cp = getenv ("mhdraft")) && *cp) {
304 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
305 if (stat (msgs[0], &st) == NOTOK)
306 adios (msgs[0], "unable to stat draft file");
307 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
308 for (status = LISTDSW; status != YESW;) {
309 if (!(argp = getans (cp, anyl)))
311 switch (status = smatch (*argp, anyl)) {
317 showfile (++argp, msgs[0]);
320 advise (NULL, "say what?");
325 for (msgnum = 0; msgnum < msgp; msgnum++)
326 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
329 if (!context_find ("path"))
330 free (path ("./", TFOLDER));
333 msgs[msgp++] = "cur";
334 maildir = m_maildir (dfolder);
336 if (chdir (maildir) == NOTOK)
337 adios (maildir, "unable to change directory to");
339 /* read folder and create message structure */
340 if (!(mp = folder_read (dfolder)))
341 adios (NULL, "unable to read folder %s", dfolder);
343 /* check for empty folder */
345 adios (NULL, "no messages in %s", dfolder);
347 /* parse all the message ranges/sequences and set SELECTED */
348 for (msgnum = 0; msgnum < msgp; msgnum++)
349 if (!m_convert (mp, msgs[msgnum]))
351 seq_setprev (mp); /* set the previous-sequence */
353 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
354 if (is_selected (mp, msgnum)) {
355 msgs[msgp++] = getcpy (m_name (msgnum));
356 unset_exists (mp, msgnum);
360 mp->msgflags |= SEQMOD;
368 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
369 if ((cp = context_find ("signature")) && *cp)
370 m_putenv ("SIGNATURE", cp);
373 snprintf (buf, sizeof(buf), "%s/.signature", mypath);
374 if ((fp = fopen (buf, "r")) != NULL
375 && fgets (buf, sizeof buf, fp) != NULL) {
377 if (cp = strchr (buf, '\n'))
379 m_putenv ("SIGNATURE", buf);
384 for (msgnum = 0; msgnum < msgp; msgnum++)
385 if (stat (msgs[msgnum], &st) == NOTOK)
386 adios (msgs[msgnum], "unable to stat draft file");
388 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
390 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
392 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
393 altmsg = NULL; /* used by dist interface - see below */
395 if ((cp = getenv ("mhdist"))
397 && (distsw = atoi (cp))
399 vec[vecp++] = "-dist";
400 distfile = getcpy (m_mktemp2 (altmsg, invo_name, NULL, NULL));
401 if (link (altmsg, distfile) == NOTOK) {
404 && errno != EISREMOTE
405 #endif /* EISREMOTE */
407 adios (distfile, "unable to link %s to", altmsg);
409 distfile = getcpy (m_mktemp2(NULL, invo_name, NULL, NULL));
414 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
415 adios (altmsg, "unable to open");
417 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
418 adios (distfile, "unable to write");
419 cpydata (in, out, altmsg, distfile);
428 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
437 vec[0] = r1bindex (postproc, '/');
440 for (msgnum = 0; msgnum < msgp; msgnum++) {
441 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach,
452 context_save (); /* save the context file */