]>
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 "sbr/fmt_new.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"
24 #include "h/fmt_scan.h"
29 #include "sbr/m_mktemp.h"
34 #endif /* HAVE_GETUTXENT */
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}>>%>"
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) \
49 X("nobell", 0, NBELSW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(RCVTTY
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(RCVTTY
, switches
);
62 static bool bell
= true;
63 static bool newline
= true;
65 static int width
= -1;
66 static char *form
= NULL
;
67 static char *format
= NULL
;
72 static void alrmser (int);
73 static int message_fd (char **);
74 static int header_fd (void);
76 static void alert (char *, int);
77 #endif /* HAVE_GETUTXENT */
81 main (int argc
, char **argv
)
84 char *cp
, *user
, buf
[BUFSIZ
];
85 char **argp
, **arguments
, *vec
[MAXARGS
];
88 if (nmh_init(argv
[0], true, false)) { return 1; }
91 arguments
= getarguments (invo_name
, argc
, argv
, 1);
94 while ((cp
= *argp
++)) {
96 switch (smatch (++cp
, switches
)) {
98 ambigsw (cp
, switches
);
105 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
106 print_help (buf
, switches
, 1);
109 print_version(invo_name
);
117 if (!(form
= *argp
++) || *form
== '-')
118 die("missing argument to %s", argp
[-2]);
122 if (!(format
= *argp
++) || *format
== '-')
123 die("missing argument to %s", argp
[-2]);
128 if (!(cp
= *argp
++) || *cp
== '-')
129 die("missing argument to %s", argp
[-2]);
151 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
154 user
= getusername();
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
);
168 #endif /* HAVE_GETUTXENT */
184 message_fd (char **vec
)
192 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
193 inform("unable to create temporary file in %s", get_temp_dir());
196 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
198 if ((child_id
= fork()) == NOTOK
) {
205 if (!setjmp (myctx
)) {
206 SIGNAL (SIGALRM
, alrmser
);
207 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
209 /* amount of time to wait depends on message size */
211 /* give at least 5 minutes */
213 } else if (bytes
>= 90000) {
214 /* but 30 minutes should be long enough */
217 seconds
= (bytes
/ 60) + 300;
219 alarm ((unsigned int) seconds
);
220 pidwait(child_id
, OK
);
223 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> 0)
227 * Ruthlessly kill the child and anything
228 * else in its process group.
230 killpg(child_id
, SIGKILL
);
238 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
240 setpgid(0, getpid()); /* put in own process group */
241 if (execvp (vec
[0], vec
) == NOTOK
) {
255 charstring_t scanl
= NULL
;
257 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
258 inform("unable to create temporary file in %s", get_temp_dir());
261 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
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
);
270 if (write (fd
, "\n\r", 2) < 0) {
271 advise (tfile
, "write LF/CR");
274 if (write (fd
, charstring_buffer (scanl
), charstring_bytes (scanl
)) < 0) {
275 advise (tfile
, "write");
277 charstring_free (scanl
);
279 if (write (fd
, "\007", 1) < 0) {
280 advise (tfile
, "write BEL");
290 alert (char *tty
, int md
)
293 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
296 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
299 * The mask depends on whether we are checking for
300 * write permission based on `biff' or `mesg'.
302 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
303 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
306 if (setjmp (myctx
)) {
310 SIGNAL (SIGALRM
, alrmser
);
312 td
= open (ttyspec
, O_WRONLY
);
317 lseek(md
, 0, SEEK_SET
);
319 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
320 if (write (td
, buffer
, i
) != i
)
325 #endif /* HAVE_GETUTXENT */