]>
diplodocus.org Git - nmh/blob - uip/sendsbr.c
3 * sendsbr.c -- routines to help WhatNow/Send along
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/fmt_scan.h>
12 #include <h/fmt_compile.h>
13 #include <h/signals.h>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
29 static int setup_oauth_params(char *[], int *, int, const char **);
30 #endif /* OAUTH_SUPPORT */
32 int debugsw
= 0; /* global */
40 char *altmsg
= NULL
; /* .. */
41 char *annotext
= NULL
;
42 char *distfile
= NULL
;
49 static void alert (char *, int);
50 static int tmp_fd (void);
51 static void anno (int, struct stat
*);
52 static void annoaux (int);
53 static int splitmsg (char **, int, char *, char *, struct stat
*, int);
54 static int sendaux (char **, int, char *, char *, struct stat
*);
55 static void handle_sendfrom(char **, int *, char *);
56 static int get_from_header_info(const char *, const char **, const char **, const char **);
57 static const char *get_message_header_info(FILE *, char *);
58 static void merge_profile_entry(const char *, const char *, char *[], int *);
59 static void armed_done (int) NORETURN
;
62 * Entry point into (back-end) routines to send message.
66 sendsbr (char **vec
, int vecp
, char *program
, char *draft
, struct stat
*st
,
71 char buffer
[BUFSIZ
], file
[BUFSIZ
];
73 char **buildvec
, *buildprogram
;
74 char *volatile drft
= draft
;
75 /* nvecs is volatile to prevent warning from gcc about possible clobbering
77 volatile int nvecs
= vecp
;
78 int *nvecsp
= (int *) &nvecs
;
81 * Run the mimebuildproc (which is by default mhbuild) on the message
82 * with the addition of the "-auto" flag
85 switch (child
= fork()) {
87 adios("fork", "unable to");
91 buildvec
= argsplit(buildmimeproc
, &buildprogram
, &i
);
92 buildvec
[i
++] = "-auto";
94 buildvec
[i
++] = "-dist";
95 buildvec
[i
++] = (char *) drft
;
97 execvp(buildprogram
, buildvec
);
98 fprintf(stderr
, "unable to exec ");
99 perror(buildmimeproc
);
104 if (pidXwait(child
, buildmimeproc
))
110 switch (setjmp (env
)) {
113 * If given -push and -unique (which is undocumented), then
114 * rename the draft file. I'm not quite sure why.
116 if (pushsw
&& unique
) {
117 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, NULL
);
119 adios(NULL
, "unable to create temporary file in %s",
122 if (rename (drft
, strncpy(file
, cp
, sizeof(file
))) == NOTOK
)
123 adios (file
, "unable to rename %s to", drft
);
128 * Rework the vec based on From: header in draft, as specified
129 * by sendfrom-address entries in profile.
131 if (context_find_prefix("sendfrom-")) {
132 handle_sendfrom(vec
, nvecsp
, draft
);
136 * Check if we need to split the message into
137 * multiple messages of type "message/partial".
139 if (splitsw
>= 0 && !distfile
&& stat ((char *) drft
, &sts
) != NOTOK
140 && sts
.st_size
>= CPERMSG
) {
141 status
= splitmsg (vec
, nvecs
, program
, drft
,
142 st
, splitsw
) ? NOTOK
: OK
;
144 status
= sendaux (vec
, nvecs
, program
, drft
, st
) ? NOTOK
: OK
;
147 /* rename the original draft */
148 if (rename_drft
&& status
== OK
&&
149 rename (drft
, strncpy (buffer
, m_backup (drft
),
150 sizeof(buffer
))) == NOTOK
)
151 advise (buffer
, "unable to rename %s to", drft
);
161 (void) m_unlink (distfile
);
167 * Split large message into several messages of
168 * type "message/partial" and send them.
172 splitmsg (char **vec
, int vecp
, char *program
, char *drft
,
173 struct stat
*st
, int delay
)
175 int compnum
, nparts
, partno
, state
, status
;
178 char *cp
, *dp
, buffer
[BUFSIZ
], msgid
[BUFSIZ
];
179 char subject
[BUFSIZ
];
180 char name
[NAMESZ
], partnum
[BUFSIZ
];
182 m_getfld_state_t gstate
= 0;
184 if ((in
= fopen (drft
, "r")) == NULL
)
185 adios (drft
, "unable to open for reading");
191 * Scan through the message and examine the various header fields,
192 * as well as locate the beginning of the message body.
194 m_getfld_track_filepos (&gstate
, in
);
195 for (compnum
= 1;;) {
196 int bufsz
= sizeof buffer
;
197 switch (state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
)) {
203 * This header field is discarded.
205 if (!strcasecmp (name
, "Message-ID")) {
206 while (state
== FLDPLUS
) {
207 bufsz
= sizeof buffer
;
208 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
210 } else if (uprf (name
, XXX_FIELD_PRF
)
211 || !strcasecmp (name
, VRSN_FIELD
)
212 || !strcasecmp (name
, "Subject")
213 || !strcasecmp (name
, "Encrypted")) {
215 * These header fields are copied to the enclosed
216 * header of the first message in the collection
217 * of message/partials. For the "Subject" header
218 * field, we also record it, so that a modified
219 * version of it, can be copied to the header
220 * of each message/partial in the collection.
222 if (!strcasecmp (name
, "Subject")) {
225 strncpy (subject
, buffer
, BUFSIZ
);
226 sublen
= strlen (subject
);
227 if (sublen
> 0 && subject
[sublen
- 1] == '\n')
228 subject
[sublen
- 1] = '\0';
231 dp
= add (concat (name
, ":", buffer
, NULL
), dp
);
232 while (state
== FLDPLUS
) {
233 bufsz
= sizeof buffer
;
234 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
235 dp
= add (buffer
, dp
);
239 * These header fields are copied to the header of
240 * each message/partial in the collection.
242 cp
= add (concat (name
, ":", buffer
, NULL
), cp
);
243 while (state
== FLDPLUS
) {
244 bufsz
= sizeof buffer
;
245 state
= m_getfld (&gstate
, name
, buffer
, &bufsz
, in
);
246 cp
= add (buffer
, cp
);
250 start
= ftell (in
) + 1;
259 adios (NULL
, "message format error in component #%d", compnum
);
262 adios (NULL
, "getfld () returned %d", state
);
267 m_getfld_state_destroy (&gstate
);
269 adios (NULL
, "headers missing from draft");
273 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
276 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
282 /* Only one part, nothing to split */
289 return sendaux (vec
, vecp
, program
, drft
, st
);
293 printf ("Sending as %d Partial Messages\n", nparts
);
298 vec
[vecp
++] = "-partno";
299 vec
[vecp
++] = partnum
;
301 vec
[vecp
++] = "-queued";
304 snprintf (msgid
, sizeof(msgid
), "%s", message_id (clock
, 0));
306 fseek (in
, start
, SEEK_SET
);
307 for (partno
= 1; partno
<= nparts
; partno
++) {
311 char *cp
= m_mktemp2(drft
, invo_name
, NULL
, &out
);
313 adios(NULL
, "unable to create temporary file in %s",
316 strncpy(tmpdrf
, cp
, sizeof(tmpdrf
));
319 * Output the header fields
322 fprintf (out
, "Subject: %s (part %d of %d)\n", subject
, partno
, nparts
);
323 fprintf (out
, "%s: %s\n", VRSN_FIELD
, VRSN_VALUE
);
324 fprintf (out
, "%s: message/partial; id=\"%s\";\n", TYPE_FIELD
, msgid
);
325 fprintf (out
, "\tnumber=%d; total=%d\n", partno
, nparts
);
326 fprintf (out
, "%s: part %d of %d\n\n", DESCR_FIELD
, partno
, nparts
);
329 * If this is the first in the collection, output the
330 * header fields we are encapsulating at the beginning
331 * of the body of the first message.
336 fprintf (out
, "Message-ID: %s\n", msgid
);
344 if (!fgets (buffer
, sizeof(buffer
) - 1, in
)) {
345 if (partno
== nparts
)
347 adios (NULL
, "premature eof");
350 if ((pos
+= (len
= strlen (buffer
))) > CPERMSG
) {
351 fseek (in
, -len
, SEEK_CUR
);
359 adios (tmpdrf
, "error writing to");
363 if (!pushsw
&& verbsw
) {
368 /* Pause here, if a delay is specified */
369 if (delay
> 0 && 1 < partno
&& partno
<= nparts
) {
371 printf ("pausing %d seconds before sending part %d...\n",
375 sleep ((unsigned int) delay
);
378 snprintf (partnum
, sizeof(partnum
), "%d", partno
);
379 status
= sendaux (vec
, vecp
, program
, tmpdrf
, st
);
380 (void) m_unlink (tmpdrf
);
385 * This is so sendaux will only annotate
386 * the altmsg the first time it is called.
395 fclose (in
); /* close the draft */
401 * Annotate original message, and
402 * call `postproc' (which is passed down in "program") to send message.
406 sendaux (char **vec
, int vecp
, char *program
, char *drft
, struct stat
*st
)
409 int i
, status
, fd
, fd2
;
410 char backup
[BUFSIZ
], buf
[BUFSIZ
];
412 fd
= pushsw
? tmp_fd () : NOTOK
;
416 if ((fd2
= tmp_fd ()) != NOTOK
) {
417 vec
[vecp
++] = "-idanno";
418 snprintf (buf
, sizeof(buf
), "%d", fd2
);
421 admonish (NULL
, "unable to create temporary file in %s "
422 "for annotation list", get_temp_dir());
426 if (distfile
&& distout (drft
, distfile
, backup
) == NOTOK
)
430 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
435 /* oops -- fork error */
436 adios ("fork", "unable to");
437 break; /* NOT REACHED */
441 * child process -- send it
443 * If fd is ok, then we are pushing and fd points to temp
444 * file, so capture anything on stdout and stderr there.
447 dup2 (fd
, fileno (stdout
));
448 dup2 (fd
, fileno (stderr
));
451 execvp (program
, vec
);
452 fprintf (stderr
, "unable to exec ");
458 * parent process -- wait for it
460 if ((status
= pidwait(child_id
, NOTOK
)) == OK
) {
461 if (annotext
&& fd2
!= NOTOK
)
465 * If postproc failed, and we have good fd (which means
466 * we pushed), then mail error message (and possibly the
467 * draft) back to the user.
473 advise (NULL
, "message not delivered to anyone");
475 if (annotext
&& fd2
!= NOTOK
)
478 (void) m_unlink (drft
);
479 if (rename (backup
, drft
) == NOTOK
)
480 advise (drft
, "unable to rename %s to", backup
);
491 * Mail error notification (and possibly a copy of the
492 * message) back to the user, using the mailproc
496 alert (char *file
, int out
)
504 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
509 /* oops -- fork error */
510 advise ("fork", "unable to");
513 /* child process -- send it */
514 SIGNAL (SIGHUP
, SIG_IGN
);
515 SIGNAL (SIGINT
, SIG_IGN
);
516 SIGNAL (SIGQUIT
, SIG_IGN
);
517 SIGNAL (SIGTERM
, SIG_IGN
);
519 if ((in
= open (file
, O_RDONLY
)) == NOTOK
) {
520 admonish (file
, "unable to re-open");
522 lseek (out
, (off_t
) 0, SEEK_END
);
523 strncpy (buf
, "\nMessage not delivered to anyone.\n", sizeof(buf
));
524 if (write (out
, buf
, strlen (buf
)) < 0) {
525 advise (file
, "write");
527 strncpy (buf
, "\n------- Unsent Draft\n\n", sizeof(buf
));
528 if (write (out
, buf
, strlen (buf
)) < 0) {
529 advise (file
, "write");
531 cpydgst (in
, out
, file
, "temporary file");
533 strncpy (buf
, "\n------- End of Unsent Draft\n", sizeof(buf
));
534 if (write (out
, buf
, strlen (buf
)) < 0) {
535 advise (file
, "write");
537 if (rename (file
, strncpy (buf
, m_backup (file
), sizeof(buf
))) == NOTOK
)
538 admonish (buf
, "unable to rename %s to", file
);
541 lseek (out
, (off_t
) 0, SEEK_SET
);
542 dup2 (out
, fileno (stdin
));
544 /* create subject for error notification */
545 snprintf (buf
, sizeof(buf
), "send failed on %s",
546 forwsw
? "enclosed draft" : file
);
548 arglist
= argsplit(mailproc
, &program
, &argp
);
550 arglist
[argp
++] = getusername();
551 arglist
[argp
++] = "-subject";
552 arglist
[argp
++] = buf
;
553 arglist
[argp
] = NULL
;
555 execvp (program
, arglist
);
556 fprintf (stderr
, "unable to exec ");
560 default: /* no waiting... */
572 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) return NOTOK
;
575 advise (NULL
, "temporary file %s selected", tfile
);
577 if (m_unlink (tfile
) == NOTOK
)
578 advise (tfile
, "unable to remove");
585 anno (int fd
, struct stat
*st
)
589 static char *cwd
= NULL
;
593 (stat (altmsg
, &st2
) == NOTOK
594 || st
->st_mtime
!= st2
.st_mtime
595 || st
->st_dev
!= st2
.st_dev
596 || st
->st_ino
!= st2
.st_ino
)) {
598 admonish (NULL
, "$mhaltmsg mismatch");
602 child_id
= debugsw
? NOTOK
: fork ();
604 case NOTOK
: /* oops */
607 "unable to fork, so doing annotations by hand...");
609 cwd
= getcpy (pwd ());
612 /* block a few signals */
614 sigaddset (&set
, SIGHUP
);
615 sigaddset (&set
, SIGINT
);
616 sigaddset (&set
, SIGQUIT
);
617 sigaddset (&set
, SIGTERM
);
618 sigprocmask (SIG_BLOCK
, &set
, &oset
);
620 unregister_for_removal(0);
626 /* reset the signal mask */
627 sigprocmask (SIG_SETMASK
, &oset
, &set
);
629 if (chdir (cwd
) < 0) {
630 advise (cwd
, "chdir");
634 default: /* no waiting... */
644 int fd2
, fd3
, msgnum
;
645 char *cp
, *folder
, *maildir
;
646 char buffer
[BUFSIZ
], **ap
;
650 if ((folder
= getenv ("mhfolder")) == NULL
|| *folder
== 0) {
652 admonish (NULL
, "$mhfolder not set");
655 maildir
= m_maildir (folder
);
656 if (chdir (maildir
) == NOTOK
) {
658 admonish (maildir
, "unable to change directory to");
661 if (!(mp
= folder_read (folder
, 0))) {
663 admonish (NULL
, "unable to read folder %s", folder
);
667 /* check for empty folder */
668 if (mp
->nummsg
== 0) {
670 admonish (NULL
, "no messages in %s", folder
);
674 if ((cp
= getenv ("mhmessages")) == NULL
|| *cp
== 0) {
676 admonish (NULL
, "$mhmessages not set");
679 if (!debugsw
/* MOBY HACK... */
681 && (fd3
= open ("/dev/null", O_RDWR
)) != NOTOK
682 && (fd2
= dup (fileno (stderr
))) != NOTOK
) {
683 dup2 (fd3
, fileno (stderr
));
688 for (ap
= brkstring (cp
= getcpy (cp
), " ", NULL
); *ap
; ap
++)
692 dup2 (fd2
, fileno (stderr
));
693 if (mp
->numsel
== 0) {
695 admonish (NULL
, "no messages to annotate");
699 lseek (fd
, (off_t
) 0, SEEK_SET
);
700 if ((fp
= fdopen (fd
, "r")) == NULL
) {
702 admonish (NULL
, "unable to fdopen annotation list");
706 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
707 cp
= add (buffer
, cp
);
711 advise (NULL
, "annotate%s with %s: \"%s\"",
712 inplace
? " inplace" : "", annotext
, cp
);
713 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
714 if (is_selected(mp
, msgnum
)) {
716 advise (NULL
, "annotate message %d", msgnum
);
717 annotate (m_name (msgnum
), annotext
, cp
, inplace
, 1, -2, 0);
724 folder_free (mp
); /* free folder/message structure */
730 handle_sendfrom(char **vec
, int *vecp
, char *draft
) {
731 const char *addr
, *host
;
734 /* Extract address and host from From: header line in draft. */
735 if (get_from_header_info(draft
, &addr
, &host
, &message
) != OK
) {
736 adios(draft
, message
);
739 /* Merge in any address or host specific switches to post(1) from profile. */
740 merge_profile_entry(addr
, host
, vec
, vecp
);
749 for (vp
= vec
; *vp
; ++vp
) {
750 if (strcmp(*vp
, "xoauth2") == 0) {
754 /* -snoop will be in vec if it was enabled. */
755 for (vp
= vec
; vp
&& *vp
; ++vp
) {
756 if (strcmp(*vp
, "-snoop") == 0) {
762 if (setup_oauth_params(vec
, vecp
, snoop
, &message
) != OK
) {
763 adios(NULL
, message
);
767 adios(NULL
, "sendfrom built without OAUTH_SUPPORT, "
768 "so -saslmech xoauth2 is not supported");
769 #endif /* OAUTH_SUPPORT */
778 * For XOAUTH2, append access token, from mh_oauth_do_xoauth(), for the user to vec.
782 setup_oauth_params(char *vec
[], int *vecp
, int snoop
, const char **message
) {
783 const char *saslmech
= NULL
, *user
= NULL
, *auth_svc
= NULL
;
786 /* Make sure we have all the information we need. */
787 for (i
= 1; i
< *vecp
; ++i
) {
788 /* Don't support abbreviated switches, to avoid collisions in the
789 future if new ones are added. */
790 if (! strcmp(vec
[i
-1], "-saslmech")) {
792 } else if (! strcmp(vec
[i
-1], "-user")) {
794 } else if (! strcmp(vec
[i
-1], "-authservice")) {
799 if (auth_svc
== NULL
) {
800 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
801 *message
= "must specify -authservice with -saslmech xoauth2";
806 *message
= "must specify -user with -saslmech xoauth2";
810 vec
[(*vecp
)++] = getcpy("-authservice");
811 if (saslmech
&& ! strcasecmp(saslmech
, "xoauth2")) {
812 vec
[(*vecp
)++] = mh_oauth_do_xoauth(user
, auth_svc
, snoop
? stderr
: NULL
);
814 vec
[(*vecp
)++] = getcpy(auth_svc
);
820 #endif /* OAUTH_SUPPORT */
824 * Extract user and domain from From: header line in draft.
828 get_from_header_info(const char *filename
, const char **addr
, const char **host
, const char **message
) {
832 if (stat (filename
, &st
) == NOTOK
) {
833 *message
= "unable to stat draft file";
837 if ((in
= fopen (filename
, "r")) != NULL
) {
838 /* There must be a non-blank Envelope-From or {Resent-}Sender or
839 {Resent-}From header. */
840 char *addrformat
= "%(addr{Envelope-From})";
841 char *hostformat
= "%(host{Envelope-From})";
843 if ((*addr
= get_message_header_info (in
, addrformat
)) == NULL
||
844 strlen (*addr
) == 0) {
845 addrformat
= distfile
== NULL
? "%(addr{Sender})" : "%(addr{Resent-Sender})";
846 hostformat
= distfile
== NULL
? "%(host{Sender})" : "%(host{Resent-Sender})";
848 if ((*addr
= get_message_header_info (in
, addrformat
)) == NULL
) {
849 addrformat
= distfile
== NULL
? "%(addr{From})" : "%(addr{Resent-From})";
850 hostformat
= distfile
== NULL
? "%(host{From})" : "%(host{Resent-From})";
852 if ((*addr
= get_message_header_info (in
, addrformat
)) == NULL
) {
853 *message
= "unable to find sender address in";
860 /* Use the hostformat that corresponds to the successful addrformat. */
861 if ((*host
= get_message_header_info(in
, hostformat
)) == NULL
) {
863 *message
= "unable to find sender host in";
871 *message
= "unable to open";
878 * Get formatted information from header of a message.
879 * Adapted from process_single_file() in uip/fmttest.c.
883 get_message_header_info(FILE *in
, char *format
) {
888 m_getfld_state_t gstate
= 0;
889 charstring_t buffer
= charstring_create(0);
892 dat
[0] = dat
[1] = dat
[4] = 0;
893 dat
[2] = fstat(fileno(in
), &st
) == 0 ? st
.st_size
: 0;
896 (void) fmt_compile(new_fs(NULL
, NULL
, format
), &fmt
, 1);
900 * Read in the message and process the header.
905 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
906 int bufsz
= sizeof rbuf
;
907 int state
= m_getfld(&gstate
, name
, rbuf
, &bufsz
, in
);
912 int bucket
= fmt_addcomptext(name
, rbuf
);
915 while (state
== FLDPLUS
) {
917 state
= m_getfld(&gstate
, name
, rbuf
, &bufsz
, in
);
918 fmt_appendcomp(bucket
, name
, rbuf
);
922 while (state
== FLDPLUS
) {
924 state
= m_getfld(&gstate
, name
, rbuf
, &bufsz
, in
);
931 } while (parsing_header
);
932 m_getfld_state_destroy(&gstate
);
934 fmt_scan(fmt
, buffer
, INT_MAX
, dat
, NULL
);
937 /* Trim trailing newline, if any. */
938 retval
= rtrim(charstring_buffer_copy((buffer
)));
939 charstring_free(buffer
);
940 if (strlen (retval
) > 0) {
950 * Look in profile for entry corresponding to addr or host, and add its contents to vec.
952 * Could do some of this automatically, by looking for:
953 * 1) access-$(mbox{from}) in oauth-svc file using mh_oauth_cred_load(), which isn't
954 * static and doesn't have side effects; free the result with mh_oauth_cred_free())
955 * 2) machine $(mbox{from}) in creds
956 * If no -server passed in from profile or commandline, could use smtp.<svc>.com for gmail,
957 * but that might not generalize for other svcs.
961 merge_profile_entry(const char *addr
, const char *host
, char *vec
[], int *vecp
) {
962 char *addr_entry
= concat("sendfrom-", addr
, NULL
);
963 char *profile_entry
= context_find(addr_entry
);
966 if (profile_entry
== NULL
) {
967 /* No entry for the user. Look for one for the host. */
968 char *host_entry
= concat("sendfrom-", host
, NULL
);
970 profile_entry
= context_find(host_entry
);
974 /* Use argsplit() to do the real work of splitting the args in the profile entry. */
975 if (profile_entry
&& strlen(profile_entry
) > 0) {
978 char **profile_vec
= argsplit(profile_entry
, &file
, &profile_vecp
);
981 for (i
= 0; i
< profile_vecp
; ++i
) {
982 vec
[(*vecp
)++] = getcpy(profile_vec
[i
]);
985 arglist_free(file
, profile_vec
);
991 armed_done (int status
)
993 longjmp (env
, status
? status
: NOTOK
);