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