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 vec[vecp++] = "-library";
96 vec[vecp++] = getcpy(toabsdir("+"));
98 while ((cp = *argp++)) {
100 switch (smatch(++cp, switches)) {
102 ambigsw(cp, switches);
105 adios(NULL, "-%s unknown\n", cp);
108 snprintf(buf, sizeof(buf),
109 "%s [file] [switches]",
111 print_help(buf, switches, 1);
114 print_version(invo_name);
127 if (!(cp = *argp++) || *cp == '-') {
128 adios(NULL, "missing argument to %s",
137 files[nfiles++] = cp;
144 /* check for "Aliasfile:" profile entry */
145 if ((cp = context_find("Aliasfile"))) {
148 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap; ap++) {
149 vec[vecp++] = "-alias";
150 vec[vecp++] = getcpy(etcpath(*ap));
154 if (!msgp && !nfiles) {
155 msgs[msgp++] = seq_cur;
157 maildir = toabsdir(draftfolder);
159 if (chdir(maildir) == NOTOK) {
160 adios(maildir, "unable to change directory to");
163 if (!(mp = folder_read(draftfolder))) {
164 adios(NULL, "unable to read draft folder %s", draftfolder);
166 if (mp->nummsg == 0) {
167 adios(NULL, "no messages in draft folder %s", draftfolder);
169 /* parse all the message ranges/sequences and set SELECTED */
170 for (msgnum = 0; msgnum < msgp; msgnum++) {
171 if (!m_convert(mp, msgs[msgnum])) {
177 for (msgp = 0, msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
178 if (is_selected(mp, msgnum)) {
179 files[nfiles++] = getcpy(m_name(msgnum));
180 unset_exists(mp, msgnum);
184 mp->msgflags |= SEQMOD;
187 if (!(cp = getenv("SIGNATURE")) || !*cp) {
188 if ((cp = context_find("signature")) && *cp) {
189 m_putenv("SIGNATURE", cp);
193 for (msgnum = 0; msgnum < nfiles; msgnum++) {
194 if (stat(files[msgnum], &st) == NOTOK) {
195 adios(files[msgnum], "unable to stat draft file");
199 if (!(annotext = getenv("mhannotate")) || !*annotext) {
202 if (!(altmsg = getenv("mhaltmsg")) || !*altmsg) {
203 altmsg = NULL; /* used by dist interface - see below */
206 if ((cp = getenv("mhdist")) && *cp && (distsw = atoi(cp)) && altmsg) {
207 vec[vecp++] = "-dist";
208 if ((in = open(altmsg, O_RDONLY)) == NOTOK) {
209 adios(altmsg, "unable to open for reading");
212 distfile = getcpy(m_mktemp2(NULL, invo_name, NULL, NULL));
213 if ((out = creat(distfile, (int)st2.st_mode & 0777))==NOTOK) {
214 adios(distfile, "unable to open for writing");
216 cpydata(in, out, altmsg, distfile);
223 if (!altmsg || stat(altmsg, &st) == NOTOK) {
230 for (n=3; n<OPEN_MAX; n++) {
234 for (msgnum = 0; msgnum < nfiles; msgnum++) {
235 switch (sendsbr(vec, vecp, files[msgnum], &st)) {
252 ** message sending back-end
255 sendsbr(char **vec, int vecp, char *drft, struct stat *st)
259 char *original_draft;
260 char *p; /* string pointer for building file name */
263 ** Save the original name of the draft file. The name of the
264 ** draft file is changed to a temporary file containing the built
265 ** MIME message if there are attachments. We need the original
266 ** name so that it can be renamed after the message is sent.
268 original_draft = drft;
271 ** Convert the draft to a MIME message.
272 ** Use the mhbuild composition file for the draft if there was
273 ** a successful conversion because that now contains the MIME
274 ** message. A nice side effect of this is that it leaves the
275 ** original draft file untouched so that it can be retrieved
276 ** and modified if desired.
278 switch (attach(drft)) {
280 drft = composition_file_name;
291 switch (setjmp(env)) {
293 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
294 /* rename the original draft */
296 strncpy(buffer, m_backup(original_draft),
298 if (rename(original_draft, buffer) == NOTOK) {
299 advise(buffer, "unable to rename %s to", drft);
315 ** Get rid of any temporary files that we created for attachments.
316 ** Also get rid of the renamed composition file that mhbuild
317 ** leaves as a turd. It looks confusing, but we use the body
318 ** file name to help build the renamed composition file name.
320 if (drft == composition_file_name) {
321 clean_up_temporary_files();
323 if (strlen(composition_file_name) >=
324 sizeof (composition_file_name) - 6) {
325 advise(NULL, "unable to remove original composition file.");
327 if (!(p = strrchr(composition_file_name, '/'))) {
328 p = composition_file_name;
332 strcpy(body_file_name, p);
334 strcpy(p, body_file_name);
337 unlink(composition_file_name);
345 attach(char *draft_file_name)
347 char buf[MAXPATHLEN + 6];
351 int non_ascii; /* msg body contains non-ASCII chars */
352 int length; /* length of attachment header field name */
355 if (!(draft_file = fopen(draft_file_name, "r"))) {
356 adios(NULL, "can't open draft file `%s'.", draft_file_name);
359 /* We'll grow the buffer as needed. */
360 field = (char *)mh_xmalloc(field_size = 256);
363 ** Scan the draft file for an attachment header field name.
365 length = strlen(attach_hdr);
367 while (get_line() != EOF && *field != '\0' && *field != '-') {
368 if (strncasecmp(field, attach_hdr, length)==0 &&
369 field[length] == ':') {
375 ** Look for at least one non-blank line in the body of the
376 ** message which indicates content in the body.
377 ** Check if body contains at least one non-blank (= not empty)
378 ** and if it contains any non-ASCII chars (= need MIME).
382 while (get_line() != EOF) {
383 for (p = field; *p != '\0'; p++) {
384 if (*p != ' ' && *p != '\t') {
387 if (*p > 127 || *p < 0) {
391 if (has_body && non_ascii) {
392 break; /* that's been already enough information */
396 if (!has_attachment && non_ascii==0) {
397 /* We don't need to convert it to MIME. */
405 /* Make names for the temporary files. */
406 strncpy(body_file_name,
407 m_mktemp(toabsdir(invo_name), NULL, NULL),
408 sizeof (body_file_name));
409 strncpy(composition_file_name,
410 m_mktemp(toabsdir(invo_name), NULL, NULL),
411 sizeof (composition_file_name));
414 body_file = fopen(body_file_name, "w");
416 composition_file = fopen(composition_file_name, "w");
418 if ((has_body && !body_file) || !composition_file) {
419 clean_up_temporary_files();
420 adios(NULL, "unable to open all of the temporary files.");
423 /* Copy non-attachment header fields to the temp composition file. */
425 while (get_line() != EOF && *field && *field != '-') {
426 if (strncasecmp(field, attach_hdr, length) != 0 ||
427 field[length] != ':') {
428 fprintf(composition_file, "%s\n", field);
431 fputs("--------\n", composition_file);
434 /* Copy the message body to the temporary file. */
435 while ((c = getc(draft_file)) != EOF) {
440 /* Add a mhbuild MIME composition file line for the body */
441 /* charset will be discovered/guessed by mhbuild */
442 fprintf(composition_file, "#text/plain %s\n", body_file_name);
446 ** Now, go back to the beginning of the draft file and look for
447 ** header fields that specify attachments. Add a mhbuild MIME
448 ** composition file for each.
451 while (get_line() != EOF && *field && *field != '-') {
452 if (strncasecmp(field, attach_hdr, length) == 0 &&
453 field[length] == ':') {
454 for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
458 /* forwarded message */
459 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
461 /* regular attachment */
462 make_mime_composition_file_entry(p);
466 fclose(composition_file);
468 /* We're ready to roll! */
469 sprintf(buf, "mhbuild %s", composition_file_name);
470 if (system(buf) != 0) {
472 clean_up_temporary_files();
480 clean_up_temporary_files(void)
482 unlink(body_file_name);
483 unlink(composition_file_name);
491 int c; /* current character */
492 int n; /* number of bytes in buffer */
496 ** Get a line from the input file, growing the field buffer as
497 ** needed. We do this so that we can fit an entire line in the
498 ** buffer making it easy to do a string comparison on both the
499 ** field name and the field body which might be a long path name.
501 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
502 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
503 ungetc(c, draft_file);
507 if (++n >= field_size - 1) {
508 field = (char *)mh_xrealloc(field, field_size += 256);
518 make_mime_composition_file_entry(char *file_name)
523 char content_type[BUFSIZ];
525 char *cmd = mimetypequeryproc;
527 for (np = m_defs; np; np = np->n_next) {
528 if (strcasecmp(np->n_name, mimetypequery)==0) {
533 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
535 if (!(fp = popen(cmdbuf, "r"))) {
536 clean_up_temporary_files();
537 adios(NULL, "unable to determine content type with `%s'",
540 if (fgets(content_type, sizeof content_type, fp) &&
541 (cp = strrchr(content_type, '\n'))) {
544 strcpy(content_type, "application/octet-stream");
545 admonish(NULL, "problems with `%s', using fall back type `%s'",
546 cmdbuf, content_type);
550 /* TODO: don't use access(2) because it checks for ruid, not euid */
551 if (access(file_name, R_OK) != 0) {
552 clean_up_temporary_files();
553 adios(NULL, "unable to access file `%s'", file_name);
556 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
557 fprintf(composition_file, "#%s; name=\"%s\" <> [%s] {attachment}",
558 content_type, cp, cp);
560 fprintf(composition_file, " %s\n", file_name);
566 ** The back-end of the message sending back-end.
567 ** Annotate original message, and call `spost' to send message.
570 sendaux(char **vec, int vecp, char *drft, struct stat *st)
577 if (distfile && distout(drft, distfile, backup) == NOTOK) {
582 switch (child_id = fork()) {
584 /* oops -- fork error */
585 adios("fork", "unable to");
586 break; /* NOT REACHED */
589 /* child process -- send it */
591 fprintf(stderr, "unable to exec ");
594 break; /* NOT REACHED */
597 /* parent process -- wait for it */
598 if ((status = pidwait(child_id, NOTOK)) == OK) {
604 advise(NULL, "message not delivered to anyone");
607 if (rename(backup, drft) == NOTOK) {
608 advise(drft, "unable to rename %s to",
621 anno(struct stat *st)
627 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
628 st->st_mtime != st2.st_mtime ||
629 st->st_dev != st2.st_dev ||
630 st->st_ino != st2.st_ino)) {
632 admonish(NULL, "$mhaltmsg mismatch");
637 if (!(folder = getenv("mhfolder")) || !*folder) {
639 admonish(NULL, "$mhfolder not set");
643 if (!(msgs = getenv("mhmessages")) || !*msgs) {
645 admonish(NULL, "$mhmessages not set");
650 advise(NULL, "annotate as `%s': %s %s", annotext,
653 snprintf(buf, sizeof buf, "anno -comp '%s' '+%s' %s",
654 annotext, folder, msgs);
655 if (system(buf) != 0) {
656 advise(NULL, "unable to annotate");
662 armed_done(int status)
664 longjmp(env, status ? status : NOTOK);