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 */
26 char *annotext = NULL;
27 char *distfile = NULL;
31 /* name of temp file for body content */
32 static char body_file_name[MAXPATHLEN + 1];
33 /* name of mhbuild composition temporary file */
34 static char composition_file_name[MAXPATHLEN + 1];
35 static int field_size; /* size of header field buffer */
36 static char *field; /* header field buffer */
37 static FILE *draft_file; /* draft file pointer */
38 static FILE *body_file; /* body file pointer */
39 static FILE *composition_file; /* composition file pointer */
44 static int sendsbr(char **, int, char *, struct stat *);
45 static void armed_done(int) NORETURN;
46 static void anno(struct stat *);
47 static int sendaux(char **, int, char *, struct stat *);
48 static int attach(char *);
49 static void clean_up_temporary_files(void);
50 static int get_line(void);
51 static void make_mime_composition_file_entry(char *);
54 static struct swit switches[] = {
56 { "alias aliasfile", 0 },
72 main(int argc, char **argv)
74 int msgp = 0, nfiles = 0, distsw = 0, vecp = 1;
78 char *cp, *maildir = NULL;
79 char buf[BUFSIZ], **ap, **argp, **arguments;
80 char *msgs[MAXARGS], *vec[MAXARGS];
86 setlocale(LC_ALL, "");
87 invo_name = mhbasename(argv[0]);
89 /* read user profile/context */
92 arguments = getarguments(invo_name, argc, argv, 1);
95 while ((cp = *argp++)) {
97 switch (smatch(++cp, switches)) {
99 ambigsw(cp, switches);
102 adios(NULL, "-%s unknown\n", cp);
105 snprintf(buf, sizeof(buf),
106 "%s [file] [switches]",
108 print_help(buf, switches, 1);
111 print_version(invo_name);
124 if (!(cp = *argp++) || *cp == '-') {
125 adios(NULL, "missing argument to %s",
134 files[nfiles++] = cp;
141 /* check for "Aliasfile:" profile entry */
142 if ((cp = context_find("Aliasfile"))) {
145 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap; ap++) {
146 vec[vecp++] = "-alias";
147 vec[vecp++] = getcpy(etcpath(*ap));
151 if (!msgp && !nfiles) {
152 msgs[msgp++] = seq_cur;
154 maildir = toabsdir(draftfolder);
156 if (chdir(maildir) == NOTOK) {
157 adios(maildir, "unable to change directory to");
160 if (!(mp = folder_read(draftfolder))) {
161 adios(NULL, "unable to read draft folder %s", draftfolder);
163 if (mp->nummsg == 0) {
164 adios(NULL, "no messages in draft folder %s", draftfolder);
166 /* parse all the message ranges/sequences and set SELECTED */
167 for (msgnum = 0; msgnum < msgp; msgnum++) {
168 if (!m_convert(mp, msgs[msgnum])) {
174 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
175 if (is_selected(mp, msgnum)) {
176 files[nfiles++] = getcpy(m_name(msgnum));
177 unset_exists(mp, msgnum);
181 mp->msgflags |= SEQMOD;
184 if (!(cp = getenv("SIGNATURE")) || !*cp) {
185 if ((cp = context_find("signature")) && *cp) {
186 m_putenv("SIGNATURE", cp);
190 for (msgnum = 0; msgnum < nfiles; msgnum++) {
191 if (stat(files[msgnum], &st) == NOTOK) {
192 adios(files[msgnum], "unable to stat draft file");
196 if (!(annotext = getenv("mhannotate")) || !*annotext) {
199 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
200 altmsg = NULL; /* used by dist interface - see below */
203 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
204 vec[vecp++] = "-dist";
205 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
206 adios(altmsg, "unable to open for reading");
209 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
210 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
211 adios(distfile, "unable to open for writing");
213 cpydata(in, out, altmsg, distfile);
220 if (!altmsg || stat(altmsg, &st) == NOTOK) {
227 for (n=3; n<OPEN_MAX; n++) {
231 for (msgnum = 0; msgnum < nfiles; msgnum++) {
232 switch (sendsbr(vec, vecp, files[msgnum], &st)) {
249 ** message sending back-end
252 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
256 char *original_draft;
257 char *p; /* string pointer for building file name */
260 ** Save the original name of the draft file. The name of the
261 ** draft file is changed to a temporary file containing the built
262 ** MIME message if there are attachments. We need the original
263 ** name so that it can be renamed after the message is sent.
265 original_draft = drft;
268 ** Convert the draft to a MIME message.
269 ** Use the mhbuild composition file for the draft if there was
270 ** a successful conversion because that now contains the MIME
271 ** message. A nice side effect of this is that it leaves the
272 ** original draft file untouched so that it can be retrieved
273 ** and modified if desired.
275 switch (attach(drft)) {
277 drft = composition_file_name;
288 switch (setjmp(env)) {
290 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
291 /* rename the original draft */
293 strncpy(buffer, m_backup(original_draft),
295 if (rename(original_draft, buffer) == NOTOK) {
296 advise(buffer, "unable to rename %s to", drft);
312 ** Get rid of any temporary files that we created for attachments.
313 ** Also get rid of the renamed composition file that mhbuild
314 ** leaves as a turd. It looks confusing, but we use the body
315 ** file name to help build the renamed composition file name.
317 if (drft == composition_file_name) {
318 clean_up_temporary_files();
320 if (strlen(composition_file_name) >=
321 sizeof (composition_file_name) - 6) {
322 advise(NULL, "unable to remove original composition file.");
324 if (!(p = strrchr(composition_file_name, '/'))) {
325 p = composition_file_name;
329 strcpy(body_file_name, p);
331 strcpy(p, body_file_name);
334 unlink(composition_file_name);
342 attach(char *draft_file_name)
344 char buf[MAXPATHLEN + 6];
348 int non_ascii; /* msg body contains non-ASCII chars */
349 int length; /* length of attachment header field name */
352 if (!(draft_file = fopen(draft_file_name, "r"))) {
353 adios(NULL, "can't open draft file `%s'.", draft_file_name);
356 /* We'll grow the buffer as needed. */
357 field = (char *)mh_xmalloc(field_size = 256);
360 ** Scan the draft file for an attachment header field name.
362 length = strlen(attach_hdr);
364 while (get_line() != EOF && *field != '\0' && *field != '-') {
365 if (strncasecmp(field, attach_hdr, length)==0 &&
366 field[length] == ':') {
372 ** Look for at least one non-blank line in the body of the
373 ** message which indicates content in the body.
374 ** Check if body contains at least one non-blank (= not empty)
375 ** and if it contains any non-ASCII chars (= need MIME).
379 while (get_line() != EOF) {
380 for (p = field; *p != '\0'; p++) {
381 if (*p != ' ' && *p != '\t') {
384 if (*p > 127 || *p < 0) {
388 if (has_body && non_ascii) {
389 break; /* that's been already enough information */
393 if (!has_attachment && non_ascii==0) {
394 /* We don't need to convert it to MIME. */
402 /* Make names for the temporary files. */
403 strncpy(body_file_name,
404 m_mktemp(toabsdir(invo_name), NULL, NULL),
405 sizeof (body_file_name));
406 strncpy(composition_file_name,
407 m_mktemp(toabsdir(invo_name), NULL, NULL),
408 sizeof (composition_file_name));
411 body_file = fopen(body_file_name, "w");
413 composition_file = fopen(composition_file_name, "w");
415 if ((has_body && !body_file) || !composition_file) {
416 clean_up_temporary_files();
417 adios(NULL, "unable to open all of the temporary files.");
420 /* Copy non-attachment header fields to the temp composition file. */
422 while (get_line() != EOF && *field && *field != '-') {
423 if (strncasecmp(field, attach_hdr, length) != 0 ||
424 field[length] != ':') {
425 fprintf(composition_file, "%s\n", field);
428 fputs("--------\n", composition_file);
431 /* Copy the message body to the temporary file. */
432 while ((c = getc(draft_file)) != EOF) {
437 /* Add a mhbuild MIME composition file line for the body */
438 /* charset will be discovered/guessed by mhbuild */
439 fprintf(composition_file, "#text/plain %s\n", body_file_name);
443 ** Now, go back to the beginning of the draft file and look for
444 ** header fields that specify attachments. Add a mhbuild MIME
445 ** composition file for each.
448 while (get_line() != EOF && *field && *field != '-') {
449 if (strncasecmp(field, attach_hdr, length) == 0 &&
450 field[length] == ':') {
451 for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
455 /* forwarded message */
456 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
458 /* regular attachment */
459 make_mime_composition_file_entry(p);
463 fclose(composition_file);
465 /* We're ready to roll! */
466 sprintf(buf, "mhbuild %s", composition_file_name);
467 if (system(buf) != 0) {
469 clean_up_temporary_files();
477 clean_up_temporary_files(void)
479 unlink(body_file_name);
480 unlink(composition_file_name);
488 int c; /* current character */
489 int n; /* number of bytes in buffer */
493 ** Get a line from the input file, growing the field buffer as
494 ** needed. We do this so that we can fit an entire line in the
495 ** buffer making it easy to do a string comparison on both the
496 ** field name and the field body which might be a long path name.
498 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
499 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
500 ungetc(c, draft_file);
504 if (++n >= field_size - 1) {
505 field = (char *)mh_xrealloc(field, field_size += 256);
515 make_mime_composition_file_entry(char *file_name)
520 char content_type[BUFSIZ];
522 char *cmd = mimetypequeryproc;
524 for (np = m_defs; np; np = np->n_next) {
525 if (strcasecmp(np->n_name, mimetypequery)==0) {
530 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
532 if (!(fp = popen(cmdbuf, "r"))) {
533 clean_up_temporary_files();
534 adios(NULL, "unable to determine content type with `%s'",
537 if (fgets(content_type, sizeof content_type, fp) &&
538 (cp = strrchr(content_type, '\n'))) {
541 strcpy(content_type, "application/octet-stream");
542 admonish(NULL, "problems with `%s', using fall back type `%s'",
543 cmdbuf, content_type);
547 /* TODO: don't use access(2) because it checks for ruid, not euid */
548 if (access(file_name, R_OK) != 0) {
549 clean_up_temporary_files();
550 adios(NULL, "unable to access file `%s'", file_name);
553 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
554 fprintf(composition_file, "#%s; name=\"%s\" <> [%s] {attachment}",
555 content_type, cp, cp);
557 fprintf(composition_file, " %s\n", file_name);
563 ** The back-end of the message sending back-end.
564 ** Annotate original message, and call `spost' to send message.
567 sendaux(char **vec, int vecp, char *drft, struct stat *st)
574 if (distfile && distout(drft, distfile, backup) == NOTOK) {
579 switch (child_id = fork()) {
581 /* oops -- fork error */
582 adios("fork", "unable to");
583 break; /* NOT REACHED */
586 /* child process -- send it */
588 fprintf(stderr, "unable to exec ");
591 break; /* NOT REACHED */
594 /* parent process -- wait for it */
595 if ((status = pidwait(child_id, NOTOK)) == OK) {
601 advise(NULL, "message not delivered to anyone");
604 if (rename(backup, drft) == NOTOK) {
605 advise(drft, "unable to rename %s to",
618 anno(struct stat *st)
624 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
625 st->st_mtime != st2.st_mtime ||
626 st->st_dev != st2.st_dev ||
627 st->st_ino != st2.st_ino)) {
629 admonish(NULL, "$mhaltmsg mismatch");
634 if (!(folder = getenv("mhfolder")) || !*folder) {
636 admonish(NULL, "$mhfolder not set");
640 if (!(msgs = getenv("mhmessages")) || !*msgs) {
642 admonish(NULL, "$mhmessages not set");
647 advise(NULL, "annotate as `%s': %s %s", annotext,
650 snprintf(buf, sizeof buf, "anno -comp '%s' '+%s' %s",
651 annotext, folder, msgs);
652 if (system(buf) != 0) {
653 advise(NULL, "unable to annotate");
659 armed_done(int status)
661 longjmp(env, status ? status : NOTOK);