3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
10 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
11 * a number of other things also exist. Please check.
12 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
16 #include <h/signals.h>
17 #include <h/m_setjmp.h>
18 #include <h/rcvmail.h>
19 #include <h/scansbr.h>
28 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
29 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
30 %{subject}%<{body}<<%{body}>>%>"
32 static struct swit switches[] = {
36 { "form formatfile", 0 },
38 { "format string", 5 },
40 { "width columns", 0 },
58 static int newline = 1;
61 static char *form = NULL;
62 static char *format = NULL;
67 char *getusername(void);
72 static void alrmser (int);
73 static int message_fd (char **);
74 static int header_fd (void);
75 static void alert (char *, int);
79 main (int argc, char **argv)
82 char *cp, *user, buf[BUFSIZ], tty[BUFSIZ];
83 char **argp, **arguments, *vec[MAXARGS];
86 setlocale(LC_ALL, "");
88 invo_name = r1bindex (argv[0], '/');
90 /* read user profile/context */
94 arguments = getarguments (invo_name, argc, argv, 1);
97 while ((cp = *argp++)) {
99 switch (smatch (++cp, switches)) {
101 ambigsw (cp, switches);
108 snprintf (buf, sizeof(buf), "%s [command ...]", invo_name);
109 print_help (buf, switches, 1);
112 print_version(invo_name);
120 if (!(form = *argp++) || *form == '-')
121 adios (NULL, "missing argument to %s", argp[-2]);
125 if (!(format = *argp++) || *format == '-')
126 adios (NULL, "missing argument to %s", argp[-2]);
131 if (!(cp = *argp++) || *cp == '-')
132 adios(NULL, "missing argument to %s", argp[-2]);
154 if ((md = vecp ? message_fd (vec) : header_fd ()) == NOTOK)
157 user = getusername();
160 while ((utp = getutxent()) != NULL) {
161 if (utp->ut_type == USER_PROCESS && utp->ut_user[0] != 0
162 && utp->ut_line[0] != 0
163 && strncmp (user, utp->ut_user, sizeof(utp->ut_user)) == 0) {
164 strncpy (tty, utp->ut_line, sizeof(utp->ut_line));
171 return 0; /* dead code to satisfy the compiler */
185 message_fd (char **vec)
188 int bytes, fd, seconds;
192 fd = mkstemp (strncpy (tmpfil, "/tmp/rcvttyXXXXX", sizeof(tmpfil)));
195 if ((child_id = vfork()) == NOTOK) {
199 } else if (child_id) {
201 if (!m_setjmp (myctx)) {
202 SIGNAL (SIGALRM, alrmser);
203 bytes = fstat(fileno (stdin), &st) != NOTOK ? (int) st.st_size : 100;
205 /* amount of time to wait depends on message size */
207 /* give at least 5 minutes */
209 } else if (bytes >= 90000) {
210 /* but 30 minutes should be long enough */
213 seconds = (bytes / 60) + 300;
215 alarm ((unsigned int) seconds);
216 pidwait(child_id, OK);
219 if (fstat (fd, &st) != NOTOK && st.st_size > (off_t) 0)
223 * Ruthlessly kill the child and anything
224 * else in its process group.
226 killpg(child_id, SIGKILL);
234 if (dup2 (fd, 1) == NOTOK || dup2 (fd, 2) == NOTOK)
237 setpgid ((pid_t) 0, getpid ()); /* put in own process group */
238 execvp (vec[0], vec);
240 return 1; /* dead code to satisfy compiler */
251 tfile = m_mktemp2(NULL, invo_name, &fd, NULL);
252 if (tfile == NULL) return NOTOK;
257 /* get new format string */
258 nfs = new_fs (form, format, SCANFMT);
259 scan (stdin, 0, 0, nfs, width, 0, 0, NULL, 0L, 0);
261 write (fd, "\n\r", 2);
262 write (fd, scanl, strlen (scanl));
264 write (fd, "\007", 1);
271 alert (char *tty, int md)
274 char buffer[BUFSIZ], ttyspec[BUFSIZ];
277 snprintf (ttyspec, sizeof(ttyspec), "/dev/%s", tty);
280 * The mask depends on whether we are checking for
281 * write permission based on `biff' or `mesg'.
283 mask = biff ? S_IEXEC : (S_IWRITE >> 3);
284 if (stat (ttyspec, &st) == NOTOK || (st.st_mode & mask) == 0)
287 if (!m_setjmp (myctx)) {
288 SIGNAL (SIGALRM, alrmser);
290 td = open (ttyspec, O_WRONLY);
299 lseek (md, (off_t) 0, SEEK_SET);
301 while ((i = read (md, buffer, sizeof(buffer))) > 0)
302 if (write (td, buffer, i) != i)