]>
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/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
18 #include "h/signals.h"
20 #include "h/rcvmail.h"
21 #include "h/scansbr.h"
22 #include "h/fmt_scan.h"
27 #include "sbr/m_mktemp.h"
32 #endif /* HAVE_GETUTXENT */
35 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
36 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
37 %{subject}%<{body}<<%{body}>>%>"
39 #define RCVTTY_SWITCHES \
40 X("biff", 0, BIFFSW) \
41 X("form formatfile", 0, FORMSW) \
42 X("format string", 5, FMTSW) \
43 X("width columns", 0, WIDTHSW) \
44 X("newline", 0, NLSW) \
45 X("nonewline", 0, NNLSW) \
47 X("nobell", 0, NBELSW) \
48 X("version", 0, VERSIONSW) \
49 X("help", 0, HELPSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(RCVTTY
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(RCVTTY
, switches
);
60 static bool bell
= true;
61 static bool newline
= true;
63 static int width
= -1;
64 static char *form
= NULL
;
65 static char *format
= NULL
;
70 static void alrmser (int);
71 static int message_fd (char **);
72 static int header_fd (void);
74 static void alert (char *, int);
75 #endif /* HAVE_GETUTXENT */
79 main (int argc
, char **argv
)
82 char *cp
, *user
, buf
[BUFSIZ
], tty
[BUFSIZ
];
83 char **argp
, **arguments
, *vec
[MAXARGS
];
86 if (nmh_init(argv
[0], true, false)) { return 1; }
89 arguments
= getarguments (invo_name
, argc
, argv
, 1);
92 while ((cp
= *argp
++)) {
94 switch (smatch (++cp
, switches
)) {
96 ambigsw (cp
, switches
);
103 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
104 print_help (buf
, switches
, 1);
107 print_version(invo_name
);
115 if (!(form
= *argp
++) || *form
== '-')
116 die("missing argument to %s", argp
[-2]);
120 if (!(format
= *argp
++) || *format
== '-')
121 die("missing argument to %s", argp
[-2]);
126 if (!(cp
= *argp
++) || *cp
== '-')
127 die("missing argument to %s", argp
[-2]);
149 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
152 user
= getusername();
156 while ((utp
= getutxent()) != NULL
) {
157 if (utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
158 && utp
->ut_line
[0] != 0
159 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
160 strncpy (tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
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
)
241 setpgid(0, getpid()); /* put in own process group */
242 if (execvp (vec
[0], vec
) == NOTOK
) {
256 charstring_t scanl
= NULL
;
258 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
259 inform("unable to create temporary file in %s", get_temp_dir());
262 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
266 /* get new format string */
267 nfs
= new_fs (form
, format
, SCANFMT
);
268 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0, &scanl
);
271 if (write (fd
, "\n\r", 2) < 0) {
272 advise (tfile
, "write LF/CR");
275 if (write (fd
, charstring_buffer (scanl
), charstring_bytes (scanl
)) < 0) {
276 advise (tfile
, "write");
278 charstring_free (scanl
);
280 if (write (fd
, "\007", 1) < 0) {
281 advise (tfile
, "write BEL");
291 alert (char *tty
, int md
)
294 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
297 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
300 * The mask depends on whether we are checking for
301 * write permission based on `biff' or `mesg'.
303 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
304 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
307 if (setjmp (myctx
)) {
311 SIGNAL (SIGALRM
, alrmser
);
313 td
= open (ttyspec
, O_WRONLY
);
318 lseek(md
, 0, SEEK_SET
);
320 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
321 if (write (td
, buffer
, i
) != i
)
326 #endif /* HAVE_GETUTXENT */