1 | |
---|
2 | /* |
---|
3 | * whom.c -- report to whom a message would be sent |
---|
4 | * |
---|
5 | * $Id: whom.c,v 1.1.1.1 1999-02-07 18:14:17 danw Exp $ |
---|
6 | */ |
---|
7 | |
---|
8 | #include <h/mh.h> |
---|
9 | #include <h/signals.h> |
---|
10 | #include <signal.h> |
---|
11 | |
---|
12 | static struct swit switches[] = { |
---|
13 | #define ALIASW 0 |
---|
14 | { "alias aliasfile", 0 }, |
---|
15 | #define CHKSW 1 |
---|
16 | { "check", 0 }, |
---|
17 | #define NOCHKSW 2 |
---|
18 | { "nocheck", 0 }, |
---|
19 | #define DRAFTSW 3 |
---|
20 | { "draft", 0 }, |
---|
21 | #define DFOLDSW 4 |
---|
22 | { "draftfolder +folder", 6 }, |
---|
23 | #define DMSGSW 5 |
---|
24 | { "draftmessage msg", 6 }, |
---|
25 | #define NDFLDSW 6 |
---|
26 | { "nodraftfolder", 0 }, |
---|
27 | #define VERSIONSW 7 |
---|
28 | { "version", 0 }, |
---|
29 | #define HELPSW 8 |
---|
30 | { "help", 4 }, |
---|
31 | #define CLIESW 9 |
---|
32 | { "client host", -6 }, |
---|
33 | #define SERVSW 10 |
---|
34 | { "server host", -6 }, |
---|
35 | #define SNOOPSW 11 |
---|
36 | { "snoop", -5 }, |
---|
37 | { NULL, 0 } |
---|
38 | }; |
---|
39 | |
---|
40 | |
---|
41 | int |
---|
42 | main (int argc, char **argv) |
---|
43 | { |
---|
44 | pid_t child_id; |
---|
45 | int i, status, isdf = 0; |
---|
46 | int distsw = 0, vecp = 0; |
---|
47 | char *cp, *dfolder = NULL, *dmsg = NULL; |
---|
48 | char *msg = NULL, **ap, **argp, backup[BUFSIZ]; |
---|
49 | char buf[BUFSIZ], **arguments, *vec[MAXARGS]; |
---|
50 | |
---|
51 | #ifdef LOCALE |
---|
52 | setlocale(LC_ALL, ""); |
---|
53 | #endif |
---|
54 | invo_name = r1bindex (argv[0], '/'); |
---|
55 | |
---|
56 | /* read user profile/context */ |
---|
57 | context_read(); |
---|
58 | |
---|
59 | arguments = getarguments (invo_name, argc, argv, 1); |
---|
60 | argp = arguments; |
---|
61 | |
---|
62 | vec[vecp++] = invo_name; |
---|
63 | vec[vecp++] = "-whom"; |
---|
64 | vec[vecp++] = "-library"; |
---|
65 | vec[vecp++] = getcpy (m_maildir ("")); |
---|
66 | |
---|
67 | while ((cp = *argp++)) { |
---|
68 | if (*cp == '-') { |
---|
69 | switch (smatch (++cp, switches)) { |
---|
70 | case AMBIGSW: |
---|
71 | ambigsw (cp, switches); |
---|
72 | done (1); |
---|
73 | case UNKWNSW: |
---|
74 | adios (NULL, "-%s unknown", cp); |
---|
75 | |
---|
76 | case HELPSW: |
---|
77 | snprintf (buf, sizeof(buf), "%s [switches] [file]", invo_name); |
---|
78 | print_help (buf, switches, 1); |
---|
79 | done (1); |
---|
80 | case VERSIONSW: |
---|
81 | print_version(invo_name); |
---|
82 | done (1); |
---|
83 | |
---|
84 | case CHKSW: |
---|
85 | case NOCHKSW: |
---|
86 | case SNOOPSW: |
---|
87 | vec[vecp++] = --cp; |
---|
88 | continue; |
---|
89 | |
---|
90 | case DRAFTSW: |
---|
91 | msg = draft; |
---|
92 | continue; |
---|
93 | |
---|
94 | case DFOLDSW: |
---|
95 | if (dfolder) |
---|
96 | adios (NULL, "only one draft folder at a time!"); |
---|
97 | if (!(cp = *argp++) || *cp == '-') |
---|
98 | adios (NULL, "missing argument to %s", argp[-2]); |
---|
99 | dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp, |
---|
100 | *cp != '@' ? TFOLDER : TSUBCWF); |
---|
101 | continue; |
---|
102 | case DMSGSW: |
---|
103 | if (dmsg) |
---|
104 | adios (NULL, "only one draft message at a time!"); |
---|
105 | if (!(dmsg = *argp++) || *dmsg == '-') |
---|
106 | adios (NULL, "missing argument to %s", argp[-2]); |
---|
107 | continue; |
---|
108 | case NDFLDSW: |
---|
109 | dfolder = NULL; |
---|
110 | isdf = NOTOK; |
---|
111 | continue; |
---|
112 | |
---|
113 | case ALIASW: |
---|
114 | case CLIESW: |
---|
115 | case SERVSW: |
---|
116 | vec[vecp++] = --cp; |
---|
117 | if (!(cp = *argp++) || *cp == '-') |
---|
118 | adios (NULL, "missing argument to %s", argp[-2]); |
---|
119 | vec[vecp++] = cp; |
---|
120 | continue; |
---|
121 | } |
---|
122 | } |
---|
123 | if (msg) |
---|
124 | adios (NULL, "only one draft at a time!"); |
---|
125 | else |
---|
126 | vec[vecp++] = msg = cp; |
---|
127 | } |
---|
128 | |
---|
129 | /* allow Aliasfile: profile entry */ |
---|
130 | if ((cp = context_find ("Aliasfile"))) { |
---|
131 | char *dp = NULL; |
---|
132 | |
---|
133 | for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) { |
---|
134 | vec[vecp++] = "-alias"; |
---|
135 | vec[vecp++] = *ap; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | if (msg == NULL) { |
---|
140 | #ifdef WHATNOW |
---|
141 | if (dfolder || (cp = getenv ("mhdraft")) == NULL || *cp == '\0') |
---|
142 | #endif /* WHATNOW */ |
---|
143 | cp = getcpy (m_draft (dfolder, dmsg, 1, &isdf)); |
---|
144 | msg = vec[vecp++] = cp; |
---|
145 | } |
---|
146 | if ((cp = getenv ("mhdist")) |
---|
147 | && *cp |
---|
148 | && (distsw = atoi (cp)) |
---|
149 | && (cp = getenv ("mhaltmsg")) |
---|
150 | && *cp) { |
---|
151 | if (distout (msg, cp, backup) == NOTOK) |
---|
152 | done (1); |
---|
153 | vec[vecp++] = "-dist"; |
---|
154 | distsw++; |
---|
155 | } |
---|
156 | vec[vecp] = NULL; |
---|
157 | |
---|
158 | closefds (3); |
---|
159 | |
---|
160 | if (distsw) { |
---|
161 | for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++) |
---|
162 | sleep (5); |
---|
163 | } |
---|
164 | |
---|
165 | switch (distsw ? child_id : OK) { |
---|
166 | case NOTOK: |
---|
167 | advise (NULL, "unable to fork, so checking directly..."); |
---|
168 | case OK: |
---|
169 | execvp (postproc, vec); |
---|
170 | fprintf (stderr, "unable to exec "); |
---|
171 | perror (postproc); |
---|
172 | _exit (-1); |
---|
173 | |
---|
174 | default: |
---|
175 | SIGNAL (SIGHUP, SIG_IGN); |
---|
176 | SIGNAL (SIGINT, SIG_IGN); |
---|
177 | SIGNAL (SIGQUIT, SIG_IGN); |
---|
178 | SIGNAL (SIGTERM, SIG_IGN); |
---|
179 | |
---|
180 | status = pidwait(child_id, OK); |
---|
181 | |
---|
182 | unlink (msg); |
---|
183 | if (rename (backup, msg) == NOTOK) |
---|
184 | adios (msg, "unable to rename %s to", backup); |
---|
185 | done (status); |
---|
186 | } |
---|
187 | |
---|
188 | exit (-1); /* NOT REACHED */ |
---|
189 | } |
---|