]> diplodocus.org Git - nmh/blob - sbr/ext_hook.c
8a4a91b3b421b9b1c5b20e9c2f99a9d875c33ac2
[nmh] / sbr / ext_hook.c
1 /* Run a program that hooks into some other system. The first argument is
2 * name of the hook to use, the second is the full path name of a mail message.
3 * The third argument is also the full path name of a mail message, or a NULL
4 * pointer if it isn't needed. Look in the context for an error message if
5 * something goes wrong; there is a built-in message in case one isn't specified.
6 * Only produce the error message once.
7 */
8
9 #include "h/mh.h"
10 #include "error.h"
11
12 int
13 ext_hook(char *hook_name, char *message_file_name_1, char *message_file_name_2)
14 {
15 char *hook; /* hook program from context */
16 pid_t pid; /* ID of child process */
17 int status; /* exit or other child process status */
18 char **vec; /* argument vector for child process */
19 int vecp; /* Vector index */
20 char *program; /* Name of program to execute */
21
22 static bool did_message; /* set if we've already output a message */
23
24 if ((hook = context_find(hook_name)) == NULL)
25 return OK;
26
27 switch (pid = fork()) {
28 case -1:
29 status = NOTOK;
30 inform("external database may be out-of-date.");
31 break;
32
33 case 0:
34 vec = argsplit(hook, &program, &vecp);
35 vec[vecp++] = message_file_name_1;
36 vec[vecp++] = message_file_name_2;
37 vec[vecp++] = NULL;
38 execvp(program, vec);
39 advise(program, "Unable to execute");
40 _exit(1);
41 /* NOTREACHED */
42
43 default:
44 status = pidwait(pid, -1);
45 break;
46 }
47
48 if (status == OK)
49 return OK;
50
51 if (!did_message) {
52 char *msghook;
53 if ((msghook = context_find("msg-hook")) != NULL)
54 inform("%s", msghook);
55 else {
56 char errbuf[BUFSIZ];
57 snprintf(errbuf, sizeof(errbuf), "external hook \"%s\"", hook);
58 pidstatus(status, stderr, errbuf);
59 }
60 did_message = true;
61 }
62
63 return NOTOK;
64 }