]> diplodocus.org Git - nmh/blob - uip/rcvtty.c
Mention inc(1)/msgchk(1) changes
[nmh] / uip / rcvtty.c
1 /* rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
9 * a number of other things also exist. Please check.
10 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
11 */
12
13 #include "h/mh.h"
14 #include "sbr/fmt_new.h"
15 #include "scansbr.h"
16 #include "sbr/getarguments.h"
17 #include "sbr/smatch.h"
18 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/error.h"
22 #include "h/signals.h"
23 #include <setjmp.h>
24 #include "h/fmt_scan.h"
25 #include "h/tws.h"
26 #include "h/mts.h"
27 #include "h/done.h"
28 #include "h/utils.h"
29 #include "sbr/m_mktemp.h"
30 #include <fcntl.h>
31
32 #ifdef HAVE_GETUTXENT
33 #include <utmpx.h>
34 #endif /* HAVE_GETUTXENT */
35
36 #define SCANFMT \
37 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
38 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
39 %{subject}%<{body}<<%{body}>>%>"
40
41 #define RCVTTY_SWITCHES \
42 X("biff", 0, BIFFSW) \
43 X("form formatfile", 0, FORMSW) \
44 X("format string", 5, FMTSW) \
45 X("width columns", 0, WIDTHSW) \
46 X("newline", 0, NLSW) \
47 X("nonewline", 0, NNLSW) \
48 X("bell", 0, BELSW) \
49 X("nobell", 0, NBELSW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
52
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(RCVTTY);
55 #undef X
56
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(RCVTTY, switches);
59 #undef X
60
61 static jmp_buf myctx;
62 static bool bell = true;
63 static bool newline = true;
64 static bool biff;
65 static int width = -1;
66 static char *form = NULL;
67 static char *format = NULL;
68
69 /*
70 * static prototypes
71 */
72 static void alrmser (int);
73 static int message_fd (char **);
74 static int header_fd (void);
75 #if HAVE_GETUTXENT
76 static void alert (char *, int);
77 #endif /* HAVE_GETUTXENT */
78
79
80 int
81 main (int argc, char **argv)
82 {
83 int md, vecp = 0;
84 char *cp, *user, buf[BUFSIZ];
85 char **argp, **arguments, *vec[MAXARGS];
86 struct utmpx *utp;
87
88 if (nmh_init(argv[0], true, false)) { return 1; }
89
90 mts_init ();
91 arguments = getarguments (invo_name, argc, argv, 1);
92 argp = arguments;
93
94 while ((cp = *argp++)) {
95 if (*cp == '-') {
96 switch (smatch (++cp, switches)) {
97 case AMBIGSW:
98 ambigsw (cp, switches);
99 done (1);
100 case UNKWNSW:
101 vec[vecp++] = --cp;
102 continue;
103
104 case HELPSW:
105 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
106 print_help (buf, switches, 1);
107 done (0);
108 case VERSIONSW:
109 print_version(invo_name);
110 done (0);
111
112 case BIFFSW:
113 biff = true;
114 continue;
115
116 case FORMSW:
117 if (!(form = *argp++) || *form == '-')
118 die("missing argument to %s", argp[-2]);
119 format = NULL;
120 continue;
121 case FMTSW:
122 if (!(format = *argp++) || *format == '-')
123 die("missing argument to %s", argp[-2]);
124 form = NULL;
125 continue;
126
127 case WIDTHSW:
128 if (!(cp = *argp++) || *cp == '-')
129 die("missing argument to %s", argp[-2]);
130 width = atoi(cp);
131 continue;
132 case NLSW:
133 newline = true;
134 continue;
135 case NNLSW:
136 newline = false;
137 continue;
138 case BELSW:
139 bell = true;
140 continue;
141 case NBELSW:
142 bell = false;
143 continue;
144
145 }
146 }
147 vec[vecp++] = cp;
148 }
149 vec[vecp] = 0;
150
151 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
152 exit(1);
153
154 user = getusername();
155
156 #if HAVE_GETUTXENT
157 setutxent();
158 while ((utp = getutxent()) != NULL) {
159 if (utp->ut_type == USER_PROCESS && utp->ut_user[0] != 0
160 && utp->ut_line[0] != 0
161 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
162 alert(utp->ut_line, md);
163 }
164 }
165 endutxent();
166 #else
167 NMH_UNUSED (utp);
168 #endif /* HAVE_GETUTXENT */
169
170 exit(0);
171 }
172
173
174 static void
175 alrmser (int i)
176 {
177 NMH_UNUSED (i);
178
179 longjmp (myctx, 1);
180 }
181
182
183 static int
184 message_fd (char **vec)
185 {
186 pid_t child_id;
187 int bytes, seconds;
188 int fd;
189 char *tfile;
190 struct stat st;
191
192 if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) {
193 inform("unable to create temporary file in %s", get_temp_dir());
194 return NOTOK;
195 }
196 (void) m_unlink(tfile); /* Use fd, no longer need the file name. */
197
198 if ((child_id = fork()) == NOTOK) {
199 /* fork error */
200 close (fd);
201 return header_fd ();
202 }
203 if (child_id) {
204 /* parent process */
205 if (!setjmp (myctx)) {
206 SIGNAL (SIGALRM, alrmser);
207 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
208
209 /* amount of time to wait depends on message size */
210 if (bytes <= 100) {
211 /* give at least 5 minutes */
212 seconds = 300;
213 } else if (bytes >= 90000) {
214 /* but 30 minutes should be long enough */
215 seconds = 1800;
216 } else {
217 seconds = (bytes / 60) + 300;
218 }
219 alarm ((unsigned int) seconds);
220 pidwait(child_id, OK);
221 alarm (0);
222
223 if (fstat (fd, &st) != NOTOK && st.st_size > 0)
224 return fd;
225 } else {
226 /*
227 * Ruthlessly kill the child and anything
228 * else in its process group.
229 */
230 killpg(child_id, SIGKILL);
231 }
232 close (fd);
233 return header_fd ();
234 }
235
236 /* child process */
237 rewind (stdin);
238 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
239 _exit(1);
240 setpgid(0, getpid()); /* put in own process group */
241 if (execvp (vec[0], vec) == NOTOK) {
242 _exit(1);
243 }
244
245 return NOTOK;
246 }
247
248
249 static int
250 header_fd (void)
251 {
252 int fd;
253 char *nfs;
254 char *tfile = NULL;
255 charstring_t scanl = NULL;
256
257 if ((tfile = m_mktemp2(NULL, invo_name, &fd, NULL)) == NULL) {
258 inform("unable to create temporary file in %s", get_temp_dir());
259 return NOTOK;
260 }
261 (void) m_unlink(tfile); /* Use fd, no longer need the file name. */
262
263 rewind (stdin);
264
265 /* get new format string */
266 nfs = new_fs (form, format, SCANFMT);
267 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0, &scanl);
268 scan_finished ();
269 if (newline) {
270 if (write (fd, "\n\r", 2) < 0) {
271 advise (tfile, "write LF/CR");
272 }
273 }
274 if (write (fd, charstring_buffer (scanl), charstring_bytes (scanl)) < 0) {
275 advise (tfile, "write");
276 }
277 charstring_free (scanl);
278 if (bell) {
279 if (write (fd, "\007", 1) < 0) {
280 advise (tfile, "write BEL");
281 }
282 }
283
284 return fd;
285 }
286
287
288 #if HAVE_GETUTXENT
289 static void
290 alert (char *tty, int md)
291 {
292 int i, td, mask;
293 char buffer[BUFSIZ], ttyspec[BUFSIZ];
294 struct stat st;
295
296 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
297
298 /*
299 * The mask depends on whether we are checking for
300 * write permission based on `biff' or `mesg'.
301 */
302 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
303 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
304 return;
305
306 if (setjmp (myctx)) {
307 alarm (0);
308 return;
309 }
310 SIGNAL (SIGALRM, alrmser);
311 alarm (2);
312 td = open (ttyspec, O_WRONLY);
313 alarm (0);
314 if (td == NOTOK)
315 return;
316
317 lseek(md, 0, SEEK_SET);
318
319 while ((i = read (md, buffer, sizeof(buffer))) > 0)
320 if (write (td, buffer, i) != i)
321 break;
322
323 close (td);
324 }
325 #endif /* HAVE_GETUTXENT */