]>
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>
18 #include <h/fmt_scan.h>
23 #include "sbr/m_mktemp.h"
28 #endif /* HAVE_GETUTXENT */
31 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
32 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
33 %{subject}%<{body}<<%{body}>>%>"
35 #define RCVTTY_SWITCHES \
36 X("biff", 0, BIFFSW) \
37 X("form formatfile", 0, FORMSW) \
38 X("format string", 5, FMTSW) \
39 X("width columns", 0, WIDTHSW) \
40 X("newline", 0, NLSW) \
41 X("nonewline", 0, NNLSW) \
43 X("nobell", 0, NBELSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(RCVTTY
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(RCVTTY
, switches
);
56 static bool bell
= true;
57 static bool newline
= true;
59 static int width
= -1;
60 static char *form
= NULL
;
61 static char *format
= NULL
;
66 static void alrmser (int);
67 static int message_fd (char **);
68 static int header_fd (void);
70 static void alert (char *, int);
71 #endif /* HAVE_GETUTXENT */
75 main (int argc
, char **argv
)
78 char *cp
, *user
, buf
[BUFSIZ
], tty
[BUFSIZ
];
79 char **argp
, **arguments
, *vec
[MAXARGS
];
82 if (nmh_init(argv
[0], true, false)) { return 1; }
85 arguments
= getarguments (invo_name
, argc
, argv
, 1);
88 while ((cp
= *argp
++)) {
90 switch (smatch (++cp
, switches
)) {
92 ambigsw (cp
, switches
);
99 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
100 print_help (buf
, switches
, 1);
103 print_version(invo_name
);
111 if (!(form
= *argp
++) || *form
== '-')
112 die("missing argument to %s", argp
[-2]);
116 if (!(format
= *argp
++) || *format
== '-')
117 die("missing argument to %s", argp
[-2]);
122 if (!(cp
= *argp
++) || *cp
== '-')
123 die("missing argument to %s", argp
[-2]);
145 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
148 user
= getusername();
152 while ((utp
= getutxent()) != NULL
) {
153 if (utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
154 && utp
->ut_line
[0] != 0
155 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
156 strncpy (tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
164 #endif /* HAVE_GETUTXENT */
180 message_fd (char **vec
)
188 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
189 inform("unable to create temporary file in %s", get_temp_dir());
192 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
194 if ((child_id
= fork()) == NOTOK
) {
201 if (!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
> 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(0, getpid()); /* put in own process group */
238 if (execvp (vec
[0], vec
) == NOTOK
) {
252 charstring_t scanl
= NULL
;
254 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
255 inform("unable to create temporary file in %s", get_temp_dir());
258 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
262 /* get new format string */
263 nfs
= new_fs (form
, format
, SCANFMT
);
264 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0, &scanl
);
267 if (write (fd
, "\n\r", 2) < 0) {
268 advise (tfile
, "write LF/CR");
271 if (write (fd
, charstring_buffer (scanl
), charstring_bytes (scanl
)) < 0) {
272 advise (tfile
, "write");
274 charstring_free (scanl
);
276 if (write (fd
, "\007", 1) < 0) {
277 advise (tfile
, "write BEL");
287 alert (char *tty
, int md
)
290 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
293 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
296 * The mask depends on whether we are checking for
297 * write permission based on `biff' or `mesg'.
299 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
300 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
303 if (setjmp (myctx
)) {
307 SIGNAL (SIGALRM
, alrmser
);
309 td
= open (ttyspec
, O_WRONLY
);
314 lseek(md
, 0, SEEK_SET
);
316 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
317 if (write (td
, buffer
, i
) != i
)
322 #endif /* HAVE_GETUTXENT */