3 * send.c -- send a composed message
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
19 # define SASLminc(a) (a)
20 #else /* CYRUS_SASL */
21 # define SASLminc(a) 0
22 #endif /* CYRUS_SASL */
24 static struct swit switches[] = {
26 { "alias aliasfile", 0 },
32 { "draftfolder +folder", 6 },
34 { "draftmessage msg", 6 },
36 { "nodraftfolder", 0 },
38 { "filter filterfile", 0 },
62 { "split seconds", 0 },
76 { "width columns", 0 },
82 { "dashstuffing", -12 },
83 #define NBITSTUFFSW 29
84 { "nodashstuffing", -14 },
94 { "client host", -6 },
96 { "server host", -6 },
100 { "sasl", SASLminc(-4) },
101 #define SASLMECHSW 38
102 { "saslmech", SASLminc(-5) },
104 { "user", SASLminc(-4) },
108 static struct swit anyl[] = {
118 extern int debugsw; /* from sendsbr.c */
126 extern char *altmsg; /* .. */
127 extern char *annotext;
128 extern char *distfile;
133 main (int argc, char **argv)
135 int msgp = 0, distsw = 0, vecp = 1;
136 int isdf = 0, mime = 0;
138 char *cp, *dfolder = NULL, *maildir = NULL;
139 char buf[BUFSIZ], **ap, **argp, **arguments;
140 char *msgs[MAXARGS], *vec[MAXARGS];
148 setlocale(LC_ALL, "");
150 invo_name = r1bindex (argv[0], '/');
152 /* read user profile/context */
155 arguments = getarguments (invo_name, argc, argv, 1);
158 vec[vecp++] = "-library";
159 vec[vecp++] = getcpy (m_maildir (""));
161 while ((cp = *argp++)) {
163 switch (smatch (++cp, switches)) {
165 ambigsw (cp, switches);
168 adios (NULL, "-%s unknown\n", cp);
171 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
172 print_help (buf, switches, 1);
175 print_version(invo_name);
179 msgs[msgp++] = draft;
184 adios (NULL, "only one draft folder at a time!");
185 if (!(cp = *argp++) || *cp == '-')
186 adios (NULL, "missing argument to %s", argp[-2]);
187 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
188 *cp != '@' ? TFOLDER : TSUBCWF);
191 if (!(cp = *argp++) || *cp == '-')
192 adios (NULL, "missing argument to %s", argp[-2]);
208 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
209 adios (NULL, "missing argument to %s", argp[-2]);
245 debugsw++; /* fall */
272 if (!(cp = *argp++) || *cp == '-')
273 adios (NULL, "missing argument to %s", argp[-2]);
283 * check for "Aliasfile:" profile entry
285 if ((cp = context_find ("Aliasfile"))) {
288 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
289 vec[vecp++] = "-alias";
294 if (dfolder == NULL) {
297 if ((cp = getenv ("mhdraft")) && *cp) {
302 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
303 if (stat (msgs[0], &st) == NOTOK)
304 adios (msgs[0], "unable to stat draft file");
305 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
306 for (status = LISTDSW; status != YESW;) {
307 if (!(argp = getans (cp, anyl)))
309 switch (status = smatch (*argp, anyl)) {
315 showfile (++argp, msgs[0]);
318 advise (NULL, "say what?");
323 for (msgnum = 0; msgnum < msgp; msgnum++)
324 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
327 if (!context_find ("path"))
328 free (path ("./", TFOLDER));
331 msgs[msgp++] = "cur";
332 maildir = m_maildir (dfolder);
334 if (chdir (maildir) == NOTOK)
335 adios (maildir, "unable to change directory to");
337 /* read folder and create message structure */
338 if (!(mp = folder_read (dfolder)))
339 adios (NULL, "unable to read folder %s", dfolder);
341 /* check for empty folder */
343 adios (NULL, "no messages in %s", dfolder);
345 /* parse all the message ranges/sequences and set SELECTED */
346 for (msgnum = 0; msgnum < msgp; msgnum++)
347 if (!m_convert (mp, msgs[msgnum]))
349 seq_setprev (mp); /* set the previous-sequence */
351 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
352 if (is_selected (mp, msgnum)) {
353 msgs[msgp++] = getcpy (m_name (msgnum));
354 unset_exists (mp, msgnum);
358 mp->msgflags |= SEQMOD;
366 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
367 if ((cp = context_find ("signature")) && *cp)
368 m_putenv ("SIGNATURE", cp);
371 snprintf (buf, sizeof(buf), "%s/.signature", mypath);
372 if ((fp = fopen (buf, "r")) != NULL
373 && fgets (buf, sizeof buf, fp) != NULL) {
375 if (cp = strchr (buf, '\n'))
377 m_putenv ("SIGNATURE", buf);
382 for (msgnum = 0; msgnum < msgp; msgnum++)
383 if (stat (msgs[msgnum], &st) == NOTOK)
384 adios (msgs[msgnum], "unable to stat draft file");
386 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
388 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
390 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
391 altmsg = NULL; /* used by dist interface - see below */
393 if ((cp = getenv ("mhdist"))
395 && (distsw = atoi (cp))
397 vec[vecp++] = "-dist";
398 distfile = getcpy (m_scratch (altmsg, invo_name));
399 if (link (altmsg, distfile) == NOTOK) {
402 && errno != EISREMOTE
403 #endif /* EISREMOTE */
405 adios (distfile, "unable to link %s to", altmsg);
407 distfile = getcpy (m_tmpfil (invo_name));
412 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
413 adios (altmsg, "unable to open");
415 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
416 adios (distfile, "unable to write");
417 cpydata (in, out, altmsg, distfile);
426 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
435 vec[0] = r1bindex (postproc, '/');
438 for (msgnum = 0; msgnum < msgp; msgnum++) {
439 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1)) {
449 context_save (); /* save the context file */
450 return done (status);