]> diplodocus.org Git - nmh/blob - sbr/ext_hook.c
817ab1facd9f8feebb76cce0caa89b21cc35375d
[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 "pidstatus.h"
11 #include "arglist.h"
12 #include "error.h"
13
14 int
15 ext_hook(char *hook_name, char *message_file_name_1, char *message_file_name_2)
16 {
17 char *hook; /* hook program from context */
18 pid_t pid; /* ID of child process */
19 int status; /* exit or other child process status */
20 char **vec; /* argument vector for child process */
21 int vecp; /* Vector index */
22 char *program; /* Name of program to execute */
23
24 static bool did_message; /* set if we've already output a message */
25
26 if ((hook = context_find(hook_name)) == NULL)
27 return OK;
28
29 switch (pid = fork()) {
30 case -1:
31 status = NOTOK;
32 inform("external database may be out-of-date.");
33 break;
34
35 case 0:
36 vec = argsplit(hook, &program, &vecp);
37 vec[vecp++] = message_file_name_1;
38 vec[vecp++] = message_file_name_2;
39 vec[vecp++] = NULL;
40 execvp(program, vec);
41 advise(program, "Unable to execute");
42 _exit(1);
43 /* NOTREACHED */
44
45 default:
46 status = pidwait(pid, -1);
47 break;
48 }
49
50 if (status == OK)
51 return OK;
52
53 if (!did_message) {
54 char *msghook;
55 if ((msghook = context_find("msg-hook")) != NULL)
56 inform("%s", msghook);
57 else {
58 char errbuf[BUFSIZ];
59 snprintf(errbuf, sizeof(errbuf), "external hook \"%s\"", hook);
60 pidstatus(status, stderr, errbuf);
61 }
62 did_message = true;
63 }
64
65 return NOTOK;
66 }