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;
237 ** Save the original name of the draft file. The name of the
238 ** draft file is changed to a temporary file containing the built
239 ** MIME message if there are attachments. We need the original
240 ** name so that it can be renamed after the message is sent.
242 original_draft = drft;
245 ** Convert the draft to a MIME message.
246 ** Use the mhbuild composition file for the draft if there was
247 ** a successful conversion because that now contains the MIME
248 ** message. A nice side effect of this is that it leaves the
249 ** original draft file untouched so that it can be retrieved
250 ** and modified if desired.
252 switch (attach(drft)) {
254 drft = composition_file_name;
265 switch (setjmp(env)) {
267 status = sendaux(vec, vecp, drft, st) ? NOTOK : OK;
269 /* move original draft to +trash folder */
270 snprintf(buffer, sizeof buffer,
271 "</dev/null refile -file %s +trash",
273 if (system(buffer) != 0) {
274 advise(NULL, "unable to trash the draft");
289 /* Get rid of temporary files that we created for attachments. */
290 if (drft == composition_file_name) {
291 clean_up_temporary_files();
298 attach(char *draft_file_name)
300 char buf[MAXPATHLEN + 6];
304 int non_ascii; /* msg body contains non-ASCII chars */
305 int length; /* length of attachment header field name */
308 if (!(draft_file = fopen(draft_file_name, "r"))) {
309 adios(NULL, "can't open draft file `%s'.", draft_file_name);
312 /* We'll grow the buffer as needed. */
313 field = (char *)mh_xmalloc(field_size = 256);
316 ** Scan the draft file for an attachment header field name.
318 length = strlen(attach_hdr);
320 while (get_line() != EOF && *field != '\0' && *field != '-') {
321 if (strncasecmp(field, attach_hdr, length)==0 &&
322 field[length] == ':') {
328 ** Look for at least one non-blank line in the body of the
329 ** message which indicates content in the body.
330 ** Check if body contains at least one non-blank (= not empty)
331 ** and if it contains any non-ASCII chars (= need MIME).
335 while (get_line() != EOF) {
336 for (p = field; *p != '\0'; p++) {
337 if (*p != ' ' && *p != '\t') {
340 if (*p > 127 || *p < 0) {
344 if (has_body && non_ascii) {
345 break; /* that's been already enough information */
349 if (!has_attachment && non_ascii==0) {
350 /* We don't need to convert it to MIME. */
358 /* Make names for the temporary files. */
359 strncpy(body_file_name,
360 m_mktemp(toabsdir(invo_name), NULL, NULL),
361 sizeof (body_file_name));
362 strncpy(composition_file_name,
363 m_mktemp(toabsdir(invo_name), NULL, NULL),
364 sizeof (composition_file_name));
367 body_file = fopen(body_file_name, "w");
369 composition_file = fopen(composition_file_name, "w");
371 if ((has_body && !body_file) || !composition_file) {
372 clean_up_temporary_files();
373 adios(NULL, "unable to open all of the temporary files.");
376 /* Copy non-attachment header fields to the temp composition file. */
378 while (get_line() != EOF && *field && *field != '-') {
379 if (strncasecmp(field, attach_hdr, length) != 0 ||
380 field[length] != ':') {
381 fprintf(composition_file, "%s\n", field);
384 fputs("--------\n", composition_file);
387 /* Copy the message body to the temporary file. */
388 while ((c = getc(draft_file)) != EOF) {
393 /* Add a mhbuild MIME composition file line for the body */
394 /* charset will be discovered/guessed by mhbuild */
395 fprintf(composition_file, "#text/plain %s\n", body_file_name);
399 ** Now, go back to the beginning of the draft file and look for
400 ** header fields that specify attachments. Add a mhbuild MIME
401 ** composition file for each.
404 while (get_line() != EOF && *field && *field != '-') {
405 if (strncasecmp(field, attach_hdr, length) == 0 &&
406 field[length] == ':') {
407 for (p = field+length+1; *p==' ' || *p=='\t'; p++) {
411 /* forwarded message */
412 fprintf(composition_file, "#forw [forwarded message(s)] %s\n", p);
414 /* regular attachment */
415 make_mime_composition_file_entry(p);
419 fclose(composition_file);
421 /* We're ready to roll! */
422 sprintf(buf, "mhbuild %s", composition_file_name);
423 if (system(buf) != 0) {
425 clean_up_temporary_files();
428 /* Remove the automatically created backup of mhbuild. */
429 snprintf(buf, sizeof buf, "%s.orig", composition_file_name);
430 if (unlink(buf) == -1) {
431 advise(NULL, "unable to remove original composition file.");
438 clean_up_temporary_files(void)
440 unlink(body_file_name);
441 unlink(composition_file_name);
449 int c; /* current character */
450 int n; /* number of bytes in buffer */
454 ** Get a line from the input file, growing the field buffer as
455 ** needed. We do this so that we can fit an entire line in the
456 ** buffer making it easy to do a string comparison on both the
457 ** field name and the field body which might be a long path name.
459 for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
460 if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
461 ungetc(c, draft_file);
465 if (++n >= field_size - 1) {
466 field = (char *)mh_xrealloc(field, field_size += 256);
476 make_mime_composition_file_entry(char *file_name)
481 char content_type[BUFSIZ];
483 char *cmd = mimetypequeryproc;
485 for (np = m_defs; np; np = np->n_next) {
486 if (strcasecmp(np->n_name, mimetypequery)==0) {
491 snprintf(cmdbuf, sizeof cmdbuf, "%s %s", cmd, file_name);
493 if (!(fp = popen(cmdbuf, "r"))) {
494 clean_up_temporary_files();
495 adios(NULL, "unable to determine content type with `%s'",
498 if (fgets(content_type, sizeof content_type, fp) &&
499 (cp = strrchr(content_type, '\n'))) {
502 strcpy(content_type, "application/octet-stream");
503 admonish(NULL, "problems with `%s', using fall back type `%s'",
504 cmdbuf, content_type);
508 /* TODO: don't use access(2) because it checks for ruid, not euid */
509 if (access(file_name, R_OK) != 0) {
510 clean_up_temporary_files();
511 adios(NULL, "unable to access file `%s'", file_name);
514 cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
515 fprintf(composition_file, "#%s; name=\"%s\" <> [%s] {attachment}",
516 content_type, cp, cp);
518 fprintf(composition_file, " %s\n", file_name);
524 ** The back-end of the message sending back-end.
525 ** Annotate original message, and call `spost' to send message.
528 sendaux(char **vec, int vecp, char *drft, struct stat *st)
535 if (distfile && distout(drft, distfile, backup) == NOTOK) {
540 switch (child_id = fork()) {
542 /* oops -- fork error */
543 adios("fork", "unable to");
544 break; /* NOT REACHED */
547 /* child process -- send it */
549 fprintf(stderr, "unable to exec ");
552 break; /* NOT REACHED */
555 /* parent process -- wait for it */
556 if ((status = pidwait(child_id, NOTOK)) == OK) {
562 advise(NULL, "message not delivered to anyone");
565 if (rename(backup, drft) == NOTOK) {
566 advise(drft, "unable to rename %s to",
579 anno(struct stat *st)
585 if (altmsg && (stat(altmsg, &st2) == NOTOK ||
586 st->st_mtime != st2.st_mtime ||
587 st->st_dev != st2.st_dev ||
588 st->st_ino != st2.st_ino)) {
590 admonish(NULL, "$mhaltmsg mismatch");
595 if (!(folder = getenv("mhfolder")) || !*folder) {
597 admonish(NULL, "$mhfolder not set");
601 if (!(msgs = getenv("mhmessages")) || !*msgs) {
603 admonish(NULL, "$mhmessages not set");
608 advise(NULL, "annotate as `%s': %s %s", annotext,
611 snprintf(buf, sizeof buf, "anno -comp '%s' '+%s' %s",
612 annotext, folder, msgs);
613 if (system(buf) != 0) {
614 advise(NULL, "unable to annotate");
620 armed_done(int status)
622 longjmp(env, status ? status : NOTOK);