]>
diplodocus.org Git - nmh/blob - uip/rcvtty.c
1 /* rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
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.
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
14 #include <h/signals.h>
16 #include <h/rcvmail.h>
17 #include <h/scansbr.h>
21 #include "sbr/m_mktemp.h"
26 #endif /* HAVE_GETUTXENT */
29 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
30 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
31 %{subject}%<{body}<<%{body}>>%>"
33 #define RCVTTY_SWITCHES \
34 X("biff", 0, BIFFSW) \
35 X("form formatfile", 0, FORMSW) \
36 X("format string", 5, FMTSW) \
37 X("width columns", 0, WIDTHSW) \
38 X("newline", 0, NLSW) \
39 X("nonewline", 0, NNLSW) \
41 X("nobell", 0, NBELSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(RCVTTY
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(RCVTTY
, switches
);
55 static int newline
= 1;
57 static int width
= -1;
58 static char *form
= NULL
;
59 static char *format
= NULL
;
64 static void alrmser (int);
65 static int message_fd (char **);
66 static int header_fd (void);
68 static void alert (char *, int);
69 #endif /* HAVE_GETUTXENT */
73 main (int argc
, char **argv
)
76 char *cp
, *user
, buf
[BUFSIZ
], tty
[BUFSIZ
];
77 char **argp
, **arguments
, *vec
[MAXARGS
];
80 if (nmh_init(argv
[0], 2)) { return 1; }
83 arguments
= getarguments (invo_name
, argc
, argv
, 1);
86 while ((cp
= *argp
++)) {
88 switch (smatch (++cp
, switches
)) {
90 ambigsw (cp
, switches
);
97 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
98 print_help (buf
, switches
, 1);
101 print_version(invo_name
);
109 if (!(form
= *argp
++) || *form
== '-')
110 adios (NULL
, "missing argument to %s", argp
[-2]);
114 if (!(format
= *argp
++) || *format
== '-')
115 adios (NULL
, "missing argument to %s", argp
[-2]);
120 if (!(cp
= *argp
++) || *cp
== '-')
121 adios(NULL
, "missing argument to %s", argp
[-2]);
143 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
146 user
= getusername();
150 while ((utp
= getutxent()) != NULL
) {
151 if (utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
152 && utp
->ut_line
[0] != 0
153 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
154 strncpy (tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
162 #endif /* HAVE_GETUTXENT */
178 message_fd (char **vec
)
186 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
187 inform("unable to create temporary file in %s", get_temp_dir());
190 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
192 if ((child_id
= fork()) == NOTOK
) {
199 if (!setjmp (myctx
)) {
200 SIGNAL (SIGALRM
, alrmser
);
201 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
203 /* amount of time to wait depends on message size */
205 /* give at least 5 minutes */
207 } else if (bytes
>= 90000) {
208 /* but 30 minutes should be long enough */
211 seconds
= (bytes
/ 60) + 300;
213 alarm ((unsigned int) seconds
);
214 pidwait(child_id
, OK
);
217 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> 0)
221 * Ruthlessly kill the child and anything
222 * else in its process group.
224 killpg(child_id
, SIGKILL
);
232 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
235 setpgid(0, getpid()); /* put in own process group */
236 if (execvp (vec
[0], vec
) == NOTOK
) {
250 charstring_t scanl
= NULL
;
252 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
253 inform("unable to create temporary file in %s", get_temp_dir());
256 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
260 /* get new format string */
261 nfs
= new_fs (form
, format
, SCANFMT
);
262 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0, &scanl
);
265 if (write (fd
, "\n\r", 2) < 0) {
266 advise (tfile
, "write LF/CR");
269 if (write (fd
, charstring_buffer (scanl
), charstring_bytes (scanl
)) < 0) {
270 advise (tfile
, "write");
272 charstring_free (scanl
);
274 if (write (fd
, "\007", 1) < 0) {
275 advise (tfile
, "write BEL");
285 alert (char *tty
, int md
)
288 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
291 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
294 * The mask depends on whether we are checking for
295 * write permission based on `biff' or `mesg'.
297 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
298 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
301 if (setjmp (myctx
)) {
305 SIGNAL (SIGALRM
, alrmser
);
307 td
= open (ttyspec
, O_WRONLY
);
312 lseek(md
, 0, SEEK_SET
);
314 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
315 if (write (td
, buffer
, i
) != i
)
320 #endif /* HAVE_GETUTXENT */