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) },
110 static struct swit anyl[] = {
120 extern int debugsw; /* from sendsbr.c */
128 extern char *altmsg; /* .. */
129 extern char *annotext;
130 extern char *distfile;
134 main (int argc, char **argv)
136 int msgp = 0, distsw = 0, vecp = 1;
137 int isdf = 0, mime = 0;
139 char *cp, *dfolder = NULL, *maildir = NULL;
140 char buf[BUFSIZ], **ap, **argp, **arguments;
141 char *msgs[MAXARGS], *vec[MAXARGS];
144 char *attach = (char *)0; /* header field name for attachments */
150 setlocale(LC_ALL, "");
152 invo_name = r1bindex (argv[0], '/');
154 /* read user profile/context */
157 arguments = getarguments (invo_name, argc, argv, 1);
160 vec[vecp++] = "-library";
161 vec[vecp++] = getcpy (m_maildir (""));
163 while ((cp = *argp++)) {
165 switch (smatch (++cp, switches)) {
167 ambigsw (cp, switches);
170 adios (NULL, "-%s unknown\n", cp);
173 snprintf (buf, sizeof(buf), "%s [file] [switches]", invo_name);
174 print_help (buf, switches, 1);
177 print_version(invo_name);
181 msgs[msgp++] = draft;
186 adios (NULL, "only one draft folder at a time!");
187 if (!(cp = *argp++) || *cp == '-')
188 adios (NULL, "missing argument to %s", argp[-2]);
189 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
190 *cp != '@' ? TFOLDER : TSUBCWF);
193 if (!(cp = *argp++) || *cp == '-')
194 adios (NULL, "missing argument to %s", argp[-2]);
210 if (!(cp = *argp++) || sscanf (cp, "%d", &splitsw) != 1)
211 adios (NULL, "missing argument to %s", argp[-2]);
247 debugsw++; /* fall */
274 if (!(cp = *argp++) || *cp == '-')
275 adios (NULL, "missing argument to %s", argp[-2]);
280 if (!(attach = *argp++) || *attach == '-')
281 adios (NULL, "missing argument to %s", argp[-2]);
290 * check for "Aliasfile:" profile entry
292 if ((cp = context_find ("Aliasfile"))) {
295 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) {
296 vec[vecp++] = "-alias";
301 if (dfolder == NULL) {
304 if ((cp = getenv ("mhdraft")) && *cp) {
309 msgs[msgp++] = getcpy (m_draft (NULL, NULL, 1, &isdf));
310 if (stat (msgs[0], &st) == NOTOK)
311 adios (msgs[0], "unable to stat draft file");
312 cp = concat ("Use \"", msgs[0], "\"? ", NULL);
313 for (status = LISTDSW; status != YESW;) {
314 if (!(argp = getans (cp, anyl)))
316 switch (status = smatch (*argp, anyl)) {
322 showfile (++argp, msgs[0]);
325 advise (NULL, "say what?");
330 for (msgnum = 0; msgnum < msgp; msgnum++)
331 msgs[msgnum] = getcpy (m_maildir (msgs[msgnum]));
334 if (!context_find ("path"))
335 free (path ("./", TFOLDER));
338 msgs[msgp++] = "cur";
339 maildir = m_maildir (dfolder);
341 if (chdir (maildir) == NOTOK)
342 adios (maildir, "unable to change directory to");
344 /* read folder and create message structure */
345 if (!(mp = folder_read (dfolder)))
346 adios (NULL, "unable to read folder %s", dfolder);
348 /* check for empty folder */
350 adios (NULL, "no messages in %s", dfolder);
352 /* parse all the message ranges/sequences and set SELECTED */
353 for (msgnum = 0; msgnum < msgp; msgnum++)
354 if (!m_convert (mp, msgs[msgnum]))
356 seq_setprev (mp); /* set the previous-sequence */
358 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
359 if (is_selected (mp, msgnum)) {
360 msgs[msgp++] = getcpy (m_name (msgnum));
361 unset_exists (mp, msgnum);
365 mp->msgflags |= SEQMOD;
373 if ((cp = getenv ("SIGNATURE")) == NULL || *cp == 0)
374 if ((cp = context_find ("signature")) && *cp)
375 m_putenv ("SIGNATURE", cp);
378 snprintf (buf, sizeof(buf), "%s/.signature", mypath);
379 if ((fp = fopen (buf, "r")) != NULL
380 && fgets (buf, sizeof buf, fp) != NULL) {
382 if (cp = strchr (buf, '\n'))
384 m_putenv ("SIGNATURE", buf);
389 for (msgnum = 0; msgnum < msgp; msgnum++)
390 if (stat (msgs[msgnum], &st) == NOTOK)
391 adios (msgs[msgnum], "unable to stat draft file");
393 if ((annotext = getenv ("mhannotate")) == NULL || *annotext == 0)
395 if (annotext && ((cp = getenv ("mhinplace")) != NULL && *cp != 0))
397 if ((altmsg = getenv ("mhaltmsg")) == NULL || *altmsg == 0)
398 altmsg = NULL; /* used by dist interface - see below */
400 if ((cp = getenv ("mhdist"))
402 && (distsw = atoi (cp))
404 vec[vecp++] = "-dist";
405 distfile = getcpy (m_scratch (altmsg, invo_name));
406 if (link (altmsg, distfile) == NOTOK) {
409 && errno != EISREMOTE
410 #endif /* EISREMOTE */
412 adios (distfile, "unable to link %s to", altmsg);
414 distfile = getcpy (m_tmpfil (invo_name));
419 if ((in = open (altmsg, O_RDONLY)) == NOTOK)
420 adios (altmsg, "unable to open");
422 if ((out = creat (distfile, (int) st.st_mode & 0777)) == NOTOK)
423 adios (distfile, "unable to write");
424 cpydata (in, out, altmsg, distfile);
433 if (altmsg == NULL || stat (altmsg, &st) == NOTOK) {
442 vec[0] = r1bindex (postproc, '/');
445 for (msgnum = 0; msgnum < msgp; msgnum++) {
446 switch (sendsbr (vec, vecp, msgs[msgnum], &st, 1, attach)) {
456 context_save (); /* save the context file */
457 return done (status);