]>
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>
22 #include "sbr/m_mktemp.h"
27 #endif /* HAVE_GETUTXENT */
30 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
31 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
32 %{subject}%<{body}<<%{body}>>%>"
34 #define RCVTTY_SWITCHES \
35 X("biff", 0, BIFFSW) \
36 X("form formatfile", 0, FORMSW) \
37 X("format string", 5, FMTSW) \
38 X("width columns", 0, WIDTHSW) \
39 X("newline", 0, NLSW) \
40 X("nonewline", 0, NNLSW) \
42 X("nobell", 0, NBELSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
46 #define X(sw, minchars, id) id,
47 DEFINE_SWITCH_ENUM(RCVTTY
);
50 #define X(sw, minchars, id) { sw, minchars, id },
51 DEFINE_SWITCH_ARRAY(RCVTTY
, switches
);
56 static int newline
= 1;
58 static int width
= -1;
59 static char *form
= NULL
;
60 static char *format
= NULL
;
65 static void alrmser (int);
66 static int message_fd (char **);
67 static int header_fd (void);
69 static void alert (char *, int);
70 #endif /* HAVE_GETUTXENT */
74 main (int argc
, char **argv
)
77 char *cp
, *user
, buf
[BUFSIZ
], tty
[BUFSIZ
];
78 char **argp
, **arguments
, *vec
[MAXARGS
];
81 if (nmh_init(argv
[0], 2)) { return 1; }
84 arguments
= getarguments (invo_name
, argc
, argv
, 1);
87 while ((cp
= *argp
++)) {
89 switch (smatch (++cp
, switches
)) {
91 ambigsw (cp
, switches
);
98 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
99 print_help (buf
, switches
, 1);
102 print_version(invo_name
);
110 if (!(form
= *argp
++) || *form
== '-')
111 adios (NULL
, "missing argument to %s", argp
[-2]);
115 if (!(format
= *argp
++) || *format
== '-')
116 adios (NULL
, "missing argument to %s", argp
[-2]);
121 if (!(cp
= *argp
++) || *cp
== '-')
122 adios(NULL
, "missing argument to %s", argp
[-2]);
144 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
147 user
= getusername();
151 while ((utp
= getutxent()) != NULL
) {
152 if (utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
153 && utp
->ut_line
[0] != 0
154 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
155 strncpy (tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
163 #endif /* HAVE_GETUTXENT */
179 message_fd (char **vec
)
187 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
188 inform("unable to create temporary file in %s", get_temp_dir());
191 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
193 if ((child_id
= fork()) == NOTOK
) {
200 if (!setjmp (myctx
)) {
201 SIGNAL (SIGALRM
, alrmser
);
202 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
204 /* amount of time to wait depends on message size */
206 /* give at least 5 minutes */
208 } else if (bytes
>= 90000) {
209 /* but 30 minutes should be long enough */
212 seconds
= (bytes
/ 60) + 300;
214 alarm ((unsigned int) seconds
);
215 pidwait(child_id
, OK
);
218 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> 0)
222 * Ruthlessly kill the child and anything
223 * else in its process group.
225 killpg(child_id
, SIGKILL
);
233 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
236 setpgid(0, getpid()); /* put in own process group */
237 if (execvp (vec
[0], vec
) == NOTOK
) {
251 charstring_t scanl
= NULL
;
253 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
254 inform("unable to create temporary file in %s", get_temp_dir());
257 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
261 /* get new format string */
262 nfs
= new_fs (form
, format
, SCANFMT
);
263 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0, &scanl
);
266 if (write (fd
, "\n\r", 2) < 0) {
267 advise (tfile
, "write LF/CR");
270 if (write (fd
, charstring_buffer (scanl
), charstring_bytes (scanl
)) < 0) {
271 advise (tfile
, "write");
273 charstring_free (scanl
);
275 if (write (fd
, "\007", 1) < 0) {
276 advise (tfile
, "write BEL");
286 alert (char *tty
, int md
)
289 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
292 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
295 * The mask depends on whether we are checking for
296 * write permission based on `biff' or `mesg'.
298 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
299 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
302 if (setjmp (myctx
)) {
306 SIGNAL (SIGALRM
, alrmser
);
308 td
= open (ttyspec
, O_WRONLY
);
313 lseek(md
, 0, SEEK_SET
);
315 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
316 if (write (td
, buffer
, i
) != i
)
321 #endif /* HAVE_GETUTXENT */