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[] = {
70 main(int argc, char **argv)
72 int msgp = 0, nfiles = 0, distsw = 0, vecp = 1;
76 char *cp, *maildir = NULL;
77 char buf[BUFSIZ], **argp, **arguments;
78 char *msgs[MAXARGS], *vec[MAXARGS];
84 setlocale(LC_ALL, "");
85 invo_name = mhbasename(argv[0]);
87 /* read user profile/context */
90 arguments = getarguments(invo_name, argc, argv, 1);
93 while ((cp = *argp++)) {
95 switch (smatch(++cp, switches)) {
97 ambigsw(cp, switches);
100 adios(NULL, "-%s unknown\n", cp);
103 snprintf(buf, sizeof(buf),
104 "%s [file] [switches]",
106 print_help(buf, switches, 1);
109 print_version(invo_name);
122 files[nfiles++] = cp;
129 if (!msgp && !nfiles) {
130 msgs[msgp++] = seq_cur;
132 maildir = toabsdir(draftfolder);
134 if (chdir(maildir) == NOTOK) {
135 adios(maildir, "unable to change directory to");
138 if (!(mp = folder_read(draftfolder))) {
139 adios(NULL, "unable to read draft folder %s", draftfolder);
141 if (mp->nummsg == 0) {
142 adios(NULL, "no messages in draft folder %s", draftfolder);
144 /* parse all the message ranges/sequences and set SELECTED */
145 for (msgnum = 0; msgnum < msgp; msgnum++) {
146 if (!m_convert(mp, msgs[msgnum])) {
152 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
153 if (is_selected(mp, msgnum)) {
154 files[nfiles++] = getcpy(m_name(msgnum));
155 unset_exists(mp, msgnum);
159 mp->msgflags |= SEQMOD;
162 if (!(cp = getenv("SIGNATURE")) || !*cp) {
163 if ((cp = context_find("signature")) && *cp) {
164 m_putenv("SIGNATURE", cp);
168 for (msgnum = 0; msgnum < nfiles; msgnum++) {
169 if (stat(files[msgnum], &st) == NOTOK) {
170 adios(files[msgnum], "unable to stat draft file");
174 if (!(annotext = getenv("mhannotate")) || !*annotext) {
177 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
178 altmsg = NULL; /* used by dist interface - see below */
181 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
182 vec[vecp++] = "-dist";
183 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
184 adios(altmsg, "unable to open for reading");
187 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
188 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
189 adios(distfile, "unable to open for writing");
191 cpydata(in, out, altmsg, distfile);
198 if (!altmsg || stat(altmsg, &st) == NOTOK) {
205 for (n=3; n<OPEN_MAX; n++) {
209 for (msgnum = 0; msgnum < nfiles; msgnum++) {
210 switch (sendsbr(vec, vecp, files[msgnum], &st)) {
227 ** message sending back-end
230 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
234 char *original_draft;
235 char *p; /* string pointer for building file name */
238 ** Save the original name of the draft file. The name of the
239 ** draft file is changed to a temporary file containing the built
240 ** MIME message if there are attachments. We need the original
241 ** name so that it can be renamed after the message is sent.
243 original_draft = drft;
246 ** Convert the draft to a MIME message.
247 ** Use the mhbuild composition file for the draft if there was
248 ** a successful conversion because that now contains the MIME
249 ** message. A nice side effect of this is that it leaves the
250 ** original draft file untouched so that it can be retrieved
251 ** and modified if desired.
253 switch (attach(drft)) {
255 drft = composition_file_name;
266 switch (setjmp(env)) {
268 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
269 /* rename the original draft */
271 strncpy(buffer, m_backup(original_draft),
273 if (rename(original_draft, buffer) == NOTOK) {
274 advise(buffer, "unable to rename %s to", drft);
290 ** Get rid of any temporary files that we created for attachments.
291 ** Also get rid of the renamed composition file that mhbuild
292 ** leaves as a turd. It looks confusing, but we use the body
293 ** file name to help build the renamed composition file name.
295 if (drft == composition_file_name) {
296 clean_up_temporary_files();
298 if (strlen(composition_file_name) >=
299 sizeof (composition_file_name) - 6) {
300 advise(NULL, "unable to remove original composition file.");
302 if (!(p = strrchr(composition_file_name, '/'))) {
303 p = composition_file_name;
307 strcpy(body_file_name, p);
309 strcpy(p, body_file_name);
312 unlink(composition_file_name);
320 attach(char *draft_file_name)
322 char buf[MAXPATHLEN + 6];
326 int non_ascii; /* msg body contains non-ASCII chars */
327 int length; /* length of attachment header field name */
330 if (!(draft_file = fopen(draft_file_name, "r"))) {
331 adios(NULL, "can't open draft file `%s'.", draft_file_name);
334 /* We'll grow the buffer as needed. */
335 field = (char *)mh_xmalloc(field_size = 256);
338 ** Scan the draft file for an attachment header field name.
340 length = strlen(attach_hdr);
342 while (get_line() != EOF && *field != '\0' && *field != '-') {
343 if (strncasecmp(field, attach_hdr, length)==0 &&
344 field[length] == ':') {
350 ** Look for at least one non-blank line in the body of the
351 ** message which indicates content in the body.
352 ** Check if body contains at least one non-blank (= not empty)
353 ** and if it contains any non-ASCII chars (= need MIME).
357 while (get_line() != EOF) {
358 for (p = field; *p != '\0'; p++) {
359 if (*p != ' ' && *p != '\t') {
362 if (*p > 127 || *p < 0) {
366 if (has_body && non_ascii) {
367 break; /* that's been already enough information */
371 if (!has_attachment && non_ascii==0) {
372 /* We don't need to convert it to MIME. */
380 /* Make names for the temporary files. */
381 strncpy(body_file_name,
382 m_mktemp(toabsdir(invo_name), NULL, NULL),
383 sizeof (body_file_name));
384 strncpy(composition_file_name,
385 m_mktemp(toabsdir(invo_name), NULL, NULL),
386 sizeof (composition_file_name));
389 body_file = fopen(body_file_name, "w");
391 composition_file = fopen(composition_file_name, "w");
393 if ((has_body && !body_file) || !composition_file) {
394 clean_up_temporary_files();
395 adios(NULL, "unable to open all of the temporary files.");
398 /* Copy non-attachment header fields to the temp composition file. */
400 while (get_line() != EOF && *field && *field != '-') {
401 if (strncasecmp(field, attach_hdr, length) != 0 ||
402 field[length] != ':') {
403 fprintf(composition_file, "%s\n", field);
406 fputs("--------\n", composition_file);
409 /* Copy the message body to the temporary file. */
410 while ((c = getc(draft_file)) != EOF) {
415 /* Add a mhbuild MIME composition file line for the body */
416 /* charset will be discovered/guessed by mhbuild */
417 fprintf(composition_file, "#text/plain %s\n", body_file_name);
421 ** Now, go back to the beginning of the draft file and look for
422 ** header fields that specify attachments. Add a mhbuild MIME
423 ** composition file for each.
426 while (get_line() != EOF && *field && *field != '-') {
427 if (strncasecmp(field, attach_hdr, length) == 0 &&
428 field[length] == ':') {
429 for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
433 /* forwarded message */
434 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
436 /* regular attachment */
437 make_mime_composition_file_entry(p);
441 fclose(composition_file);
443 /* We're ready to roll! */
444 sprintf(buf, "mhbuild %s", composition_file_name);
445 if (system(buf) != 0) {
447 clean_up_temporary_files();
455 clean_up_temporary_files(void)
457 unlink(body_file_name);
458 unlink(composition_file_name);
466 int c; /* current character */
467 int n; /* number of bytes in buffer */
471 ** Get a line from the input file, growing the field buffer as
472 ** needed. We do this so that we can fit an entire line in the
473 ** buffer making it easy to do a string comparison on both the
474 ** field name and the field body which might be a long path name.
476 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
477 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
478 ungetc(c, draft_file);
482 if (++n >= field_size - 1) {
483 field = (char *)mh_xrealloc(field, field_size += 256);
493 make_mime_composition_file_entry(char *file_name)
498 char content_type[BUFSIZ];
500 char *cmd = mimetypequeryproc;
502 for (np = m_defs; np; np = np->n_next) {
503 if (strcasecmp(np->n_name, mimetypequery)==0) {
508 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
510 if (!(fp = popen(cmdbuf, "r"))) {
511 clean_up_temporary_files();
512 adios(NULL, "unable to determine content type with `%s'",
515 if (fgets(content_type, sizeof content_type, fp) &&
516 (cp = strrchr(content_type, '\n'))) {
519 strcpy(content_type, "application/octet-stream");
520 admonish(NULL, "problems with `%s', using fall back type `%s'",
521 cmdbuf, content_type);
525 /* TODO: don't use access(2) because it checks for ruid, not euid */
526 if (access(file_name, R_OK) != 0) {
527 clean_up_temporary_files();
528 adios(NULL, "unable to access file `%s'", file_name);
531 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
532 fprintf(composition_file, "#%s; name=\"%s\" <> [%s] {attachment}",
533 content_type, cp, cp);
535 fprintf(composition_file, " %s\n", file_name);
541 ** The back-end of the message sending back-end.
542 ** Annotate original message, and call `spost' to send message.
545 sendaux(char **vec, int vecp, char *drft, struct stat *st)
552 if (distfile && distout(drft, distfile, backup) == NOTOK) {
557 switch (child_id = fork()) {
559 /* oops -- fork error */
560 adios("fork", "unable to");
561 break; /* NOT REACHED */
564 /* child process -- send it */
566 fprintf(stderr, "unable to exec ");
569 break; /* NOT REACHED */
572 /* parent process -- wait for it */
573 if ((status = pidwait(child_id, NOTOK)) == OK) {
579 advise(NULL, "message not delivered to anyone");
582 if (rename(backup, drft) == NOTOK) {
583 advise(drft, "unable to rename %s to",
596 anno(struct stat *st)
602 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
603 st->st_mtime != st2.st_mtime ||
604 st->st_dev != st2.st_dev ||
605 st->st_ino != st2.st_ino)) {
607 admonish(NULL, "$mhaltmsg mismatch");
612 if (!(folder = getenv("mhfolder")) || !*folder) {
614 admonish(NULL, "$mhfolder not set");
618 if (!(msgs = getenv("mhmessages")) || !*msgs) {
620 admonish(NULL, "$mhmessages not set");
625 advise(NULL, "annotate as `%s': %s %s", annotext,
628 snprintf(buf, sizeof buf, "anno -comp '%s' '+%s' %s",
629 annotext, folder, msgs);
630 if (system(buf) != 0) {
631 advise(NULL, "unable to annotate");
637 armed_done(int status)
639 longjmp(env, status ? status : NOTOK);