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.
13 #include <h/signals.h>
19 #ifdef HAVE_SYS_TIME_H
20 # include <sys/time.h>
24 int debugsw = 0; /* global */
27 char *annotext = NULL;
28 char *distfile = NULL;
32 /* name of temp file for body content */
33 static char body_file_name[MAXPATHLEN + 1];
34 /* name of mhbuild composition temporary file */
35 static char composition_file_name[MAXPATHLEN + 1];
36 static int field_size; /* size of header field buffer */
37 static char *field; /* header field buffer */
38 static FILE *draft_file; /* draft file pointer */
39 static FILE *body_file; /* body file pointer */
40 static FILE *composition_file; /* composition file pointer */
45 static int sendsbr(char **, int, char *, struct stat *, int);
46 static void armed_done(int) NORETURN;
47 static void anno(struct stat *);
48 static int sendaux(char **, int, char *, struct stat *);
49 static int attach(char *);
50 static void clean_up_temporary_files(void);
51 static int get_line(void);
52 static void make_mime_composition_file_entry(char *);
55 static struct swit switches[] = {
57 { "alias aliasfile", 0 },
77 main(int argc, char **argv)
79 int msgp = 0, nfiles = 0, distsw = 0, vecp = 1;
83 char *cp, *maildir = NULL;
84 char buf[BUFSIZ], **ap, **argp, **arguments;
85 char *msgs[MAXARGS], *vec[MAXARGS];
93 setlocale(LC_ALL, "");
95 invo_name = mhbasename(argv[0]);
97 /* read user profile/context */
100 arguments = getarguments(invo_name, argc, argv, 1);
103 vec[vecp++] = "-library";
104 vec[vecp++] = getcpy(toabsdir("+"));
106 while ((cp = *argp++)) {
108 switch (smatch(++cp, switches)) {
110 ambigsw(cp, switches);
113 adios(NULL, "-%s unknown\n", cp);
116 snprintf(buf, sizeof(buf),
117 "%s [file] [switches]",
119 print_help(buf, switches, 1);
122 print_version(invo_name);
135 debugsw++; /* fall */
143 if (!(cp = *argp++) || *cp == '-') {
144 adios(NULL, "missing argument to %s",
153 files[nfiles++] = cp;
160 /* check for "Aliasfile:" profile entry */
161 if ((cp = context_find("Aliasfile"))) {
164 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap; ap++) {
165 vec[vecp++] = "-alias";
166 vec[vecp++] = getcpy(etcpath(*ap));
170 if (!msgp && !nfiles) {
171 msgs[msgp++] = seq_cur;
173 maildir = toabsdir(draftfolder);
175 if (chdir(maildir) == NOTOK) {
176 adios(maildir, "unable to change directory to");
179 if (!(mp = folder_read(draftfolder))) {
180 adios(NULL, "unable to read draft folder %s", draftfolder);
182 if (mp->nummsg == 0) {
183 adios(NULL, "no messages in draft folder %s", draftfolder);
185 /* parse all the message ranges/sequences and set SELECTED */
186 for (msgnum = 0; msgnum < msgp; msgnum++) {
187 if (!m_convert(mp, msgs[msgnum])) {
193 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
194 if (is_selected(mp, msgnum)) {
195 files[nfiles++] = getcpy(m_name(msgnum));
196 unset_exists(mp, msgnum);
200 mp->msgflags |= SEQMOD;
203 if (!(cp = getenv("SIGNATURE")) || !*cp) {
204 if ((cp = context_find("signature")) && *cp) {
205 m_putenv("SIGNATURE", cp);
209 for (msgnum = 0; msgnum < nfiles; msgnum++) {
210 if (stat(files[msgnum], &st) == NOTOK) {
211 adios(files[msgnum], "unable to stat draft file");
215 if (!(annotext = getenv("mhannotate")) || !*annotext) {
218 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
219 altmsg = NULL; /* used by dist interface - see below */
222 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
223 vec[vecp++] = "-dist";
224 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
225 adios(altmsg, "unable to open for reading");
228 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
229 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
230 adios(distfile, "unable to open for writing");
232 cpydata(in, out, altmsg, distfile);
239 if (!altmsg || stat(altmsg, &st) == NOTOK) {
246 for (n=3; n<OPEN_MAX; n++) {
250 for (msgnum = 0; msgnum < nfiles; msgnum++) {
251 switch (sendsbr(vec, vecp, files[msgnum], &st, 1)) {
268 ** message sending back-end
271 sendsbr(char **vec, int vecp, char *drft, struct stat *st, int rename_drft)
275 char *original_draft;
276 char *p; /* string pointer for building file name */
279 ** Save the original name of the draft file. The name of the
280 ** draft file is changed to a temporary file containing the built
281 ** MIME message if there are attachments. We need the original
282 ** name so that it can be renamed after the message is sent.
284 original_draft = drft;
287 ** Convert the draft to a MIME message.
288 ** Use the mhbuild composition file for the draft if there was
289 ** a successful conversion because that now contains the MIME
290 ** message. A nice side effect of this is that it leaves the
291 ** original draft file untouched so that it can be retrieved
292 ** and modified if desired.
294 switch (attach(drft)) {
296 drft = composition_file_name;
307 switch (setjmp(env)) {
309 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
310 /* rename the original draft */
311 if (rename_drft && status == OK &&
312 rename(original_draft, strncpy(buffer,
313 m_backup(original_draft), sizeof(buffer)))
315 advise(buffer, "unable to rename %s to", drft);
330 ** Get rid of any temporary files that we created for attachments.
331 ** Also get rid of the renamed composition file that mhbuild
332 ** leaves as a turd. It looks confusing, but we use the body
333 ** file name to help build the renamed composition file name.
335 if (drft == composition_file_name) {
336 clean_up_temporary_files();
338 if (strlen(composition_file_name) >=
339 sizeof (composition_file_name) - 6) {
340 advise(NULL, "unable to remove original composition file.");
342 if (!(p = strrchr(composition_file_name, '/'))) {
343 p = composition_file_name;
347 strcpy(body_file_name, p);
349 strcpy(p, body_file_name);
352 unlink(composition_file_name);
360 attach(char *draft_file_name)
362 char buf[MAXPATHLEN + 6];
366 int non_ascii; /* msg body contains non-ASCII chars */
367 int length; /* length of attachment header field name */
370 if (!(draft_file = fopen(draft_file_name, "r"))) {
371 adios(NULL, "can't open draft file `%s'.", draft_file_name);
374 /* We'll grow the buffer as needed. */
375 field = (char *)mh_xmalloc(field_size = 256);
378 ** Scan the draft file for an attachment header field name.
380 length = strlen(attach_hdr);
382 while (get_line() != EOF && *field != '\0' && *field != '-') {
383 if (strncasecmp(field, attach_hdr, length)==0 &&
384 field[length] == ':') {
390 ** Look for at least one non-blank line in the body of the
391 ** message which indicates content in the body.
392 ** Check if body contains at least one non-blank (= not empty)
393 ** and if it contains any non-ASCII chars (= need MIME).
397 while (get_line() != EOF) {
398 for (p = field; *p != '\0'; p++) {
399 if (*p != ' ' && *p != '\t') {
402 if (*p > 127 || *p < 0) {
406 if (has_body && non_ascii) {
407 break; /* that's been already enough information */
411 if (!has_attachment && non_ascii==0) {
412 /* We don't need to convert it to MIME. */
420 /* Make names for the temporary files. */
421 strncpy(body_file_name,
422 m_mktemp(toabsdir(invo_name), NULL, NULL),
423 sizeof (body_file_name));
424 strncpy(composition_file_name,
425 m_mktemp(toabsdir(invo_name), NULL, NULL),
426 sizeof (composition_file_name));
429 body_file = fopen(body_file_name, "w");
431 composition_file = fopen(composition_file_name, "w");
433 if ((has_body && !body_file) || !composition_file) {
434 clean_up_temporary_files();
435 adios(NULL, "unable to open all of the temporary files.");
438 /* Copy non-attachment header fields to the temp composition file. */
440 while (get_line() != EOF && *field && *field != '-') {
441 if (strncasecmp(field, attach_hdr, length) != 0 ||
442 field[length] != ':') {
443 fprintf(composition_file, "%s\n", field);
446 fputs("--------\n", composition_file);
449 /* Copy the message body to the temporary file. */
450 while ((c = getc(draft_file)) != EOF) {
455 /* Add a mhbuild MIME composition file line for the body */
456 /* charset will be discovered/guessed by mhbuild */
457 fprintf(composition_file, "#text/plain %s\n", body_file_name);
461 ** Now, go back to the beginning of the draft file and look for
462 ** header fields that specify attachments. Add a mhbuild MIME
463 ** composition file for each.
466 while (get_line() != EOF && *field && *field != '-') {
467 if (strncasecmp(field, attach_hdr, length) == 0 &&
468 field[length] == ':') {
469 for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
473 /* forwarded message */
474 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
476 /* regular attachment */
477 make_mime_composition_file_entry(p);
481 fclose(composition_file);
483 /* We're ready to roll! */
484 sprintf(buf, "mhbuild %s", composition_file_name);
485 if (system(buf) != 0) {
487 clean_up_temporary_files();
495 clean_up_temporary_files(void)
497 unlink(body_file_name);
498 unlink(composition_file_name);
506 int c; /* current character */
507 int n; /* number of bytes in buffer */
511 ** Get a line from the input file, growing the field buffer as
512 ** needed. We do this so that we can fit an entire line in the
513 ** buffer making it easy to do a string comparison on both the
514 ** field name and the field body which might be a long path name.
516 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
517 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
518 ungetc(c, draft_file);
522 if (++n >= field_size - 1) {
523 field = (char *)mh_xrealloc(field, field_size += 256);
533 make_mime_composition_file_entry(char *file_name)
538 char content_type[BUFSIZ];
540 char *cmd = mimetypequeryproc;
542 for (np = m_defs; np; np = np->n_next) {
543 if (strcasecmp(np->n_name, mimetypequery)==0) {
548 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
550 if (!(fp = popen(cmdbuf, "r"))) {
551 clean_up_temporary_files();
552 adios(NULL, "unable to determine content type with `%s'",
555 if (fgets(content_type, sizeof content_type, fp) &&
556 (cp = strrchr(content_type, '\n'))) {
559 strcpy(content_type, "application/octet-stream");
560 admonish(NULL, "problems with `%s', using fall back type `%s'",
561 cmdbuf, content_type);
565 /* TODO: don't use access(2) because it checks for ruid, not euid */
566 if (access(file_name, R_OK) != 0) {
567 clean_up_temporary_files();
568 adios(NULL, "unable to access file `%s'", file_name);
571 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
572 fprintf(composition_file, "#%s; name=\"%s\" <> [%s] {attachment}",
573 content_type, cp, cp);
575 fprintf(composition_file, " %s\n", file_name);
581 ** The back-end of the message sending back-end.
582 ** Annotate original message, and call `spost' to send message.
585 sendaux(char **vec, int vecp, char *drft, struct stat *st)
592 if (distfile && distout(drft, distfile, backup) == NOTOK) {
597 switch (child_id = fork()) {
599 /* oops -- fork error */
600 adios("fork", "unable to");
601 break; /* NOT REACHED */
604 /* child process -- send it */
606 fprintf(stderr, "unable to exec ");
609 break; /* NOT REACHED */
612 /* parent process -- wait for it */
613 if ((status = pidwait(child_id, NOTOK)) == OK) {
619 advise(NULL, "message not delivered to anyone");
622 if (rename(backup, drft) == NOTOK) {
623 advise(drft, "unable to rename %s to",
636 anno(struct stat *st)
642 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
643 st->st_mtime != st2.st_mtime ||
644 st->st_dev != st2.st_dev ||
645 st->st_ino != st2.st_ino)) {
647 admonish(NULL, "$mhaltmsg mismatch");
652 if (!(folder = getenv("mhfolder")) || !*folder) {
654 admonish(NULL, "$mhfolder not set");
658 if (!(msgs = getenv("mhmessages")) || !*msgs) {
660 admonish(NULL, "$mhmessages not set");
665 advise(NULL, "annotate as `%s': %s %s", annotext,
668 snprintf(buf, sizeof buf, "anno -comp '%s' '+%s' %s",
669 annotext, folder, msgs);
670 if (system(buf) != 0) {
671 advise(NULL, "unable to annotate");
677 armed_done(int status)
679 longjmp(env, status ? status : NOTOK);