]>
diplodocus.org Git - nmh/blob - uip/rcvtty.c
6f8ded3586caa928402876eff810ccf8f0a65f41
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
15 #include "sbr/getarguments.h"
16 #include "sbr/smatch.h"
17 #include "sbr/closefds.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/rcvmail.h"
25 #include "h/fmt_scan.h"
30 #include "sbr/m_mktemp.h"
35 #endif /* HAVE_GETUTXENT */
38 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
39 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
40 %{subject}%<{body}<<%{body}>>%>"
42 #define RCVTTY_SWITCHES \
43 X("biff", 0, BIFFSW) \
44 X("form formatfile", 0, FORMSW) \
45 X("format string", 5, FMTSW) \
46 X("width columns", 0, WIDTHSW) \
47 X("newline", 0, NLSW) \
48 X("nonewline", 0, NNLSW) \
50 X("nobell", 0, NBELSW) \
51 X("version", 0, VERSIONSW) \
52 X("help", 0, HELPSW) \
54 #define X(sw, minchars, id) id,
55 DEFINE_SWITCH_ENUM(RCVTTY
);
58 #define X(sw, minchars, id) { sw, minchars, id },
59 DEFINE_SWITCH_ARRAY(RCVTTY
, switches
);
63 static bool bell
= true;
64 static bool newline
= true;
66 static int width
= -1;
67 static char *form
= NULL
;
68 static char *format
= NULL
;
73 static void alrmser (int);
74 static int message_fd (char **);
75 static int header_fd (void);
77 static void alert (char *, int);
78 #endif /* HAVE_GETUTXENT */
82 main (int argc
, char **argv
)
85 char *cp
, *user
, buf
[BUFSIZ
], tty
[BUFSIZ
];
86 char **argp
, **arguments
, *vec
[MAXARGS
];
89 if (nmh_init(argv
[0], true, false)) { return 1; }
92 arguments
= getarguments (invo_name
, argc
, argv
, 1);
95 while ((cp
= *argp
++)) {
97 switch (smatch (++cp
, switches
)) {
99 ambigsw (cp
, switches
);
106 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
107 print_help (buf
, switches
, 1);
110 print_version(invo_name
);
118 if (!(form
= *argp
++) || *form
== '-')
119 die("missing argument to %s", argp
[-2]);
123 if (!(format
= *argp
++) || *format
== '-')
124 die("missing argument to %s", argp
[-2]);
129 if (!(cp
= *argp
++) || *cp
== '-')
130 die("missing argument to %s", argp
[-2]);
152 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
155 user
= getusername();
159 while ((utp
= getutxent()) != NULL
) {
160 if (utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
161 && utp
->ut_line
[0] != 0
162 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
163 strncpy (tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
171 #endif /* HAVE_GETUTXENT */
187 message_fd (char **vec
)
195 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
196 inform("unable to create temporary file in %s", get_temp_dir());
199 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
201 if ((child_id
= fork()) == NOTOK
) {
208 if (!setjmp (myctx
)) {
209 SIGNAL (SIGALRM
, alrmser
);
210 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
212 /* amount of time to wait depends on message size */
214 /* give at least 5 minutes */
216 } else if (bytes
>= 90000) {
217 /* but 30 minutes should be long enough */
220 seconds
= (bytes
/ 60) + 300;
222 alarm ((unsigned int) seconds
);
223 pidwait(child_id
, OK
);
226 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> 0)
230 * Ruthlessly kill the child and anything
231 * else in its process group.
233 killpg(child_id
, SIGKILL
);
241 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
244 setpgid(0, getpid()); /* put in own process group */
245 if (execvp (vec
[0], vec
) == NOTOK
) {
259 charstring_t scanl
= NULL
;
261 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
262 inform("unable to create temporary file in %s", get_temp_dir());
265 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
269 /* get new format string */
270 nfs
= new_fs (form
, format
, SCANFMT
);
271 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0, &scanl
);
274 if (write (fd
, "\n\r", 2) < 0) {
275 advise (tfile
, "write LF/CR");
278 if (write (fd
, charstring_buffer (scanl
), charstring_bytes (scanl
)) < 0) {
279 advise (tfile
, "write");
281 charstring_free (scanl
);
283 if (write (fd
, "\007", 1) < 0) {
284 advise (tfile
, "write BEL");
294 alert (char *tty
, int md
)
297 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
300 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
303 * The mask depends on whether we are checking for
304 * write permission based on `biff' or `mesg'.
306 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
307 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
310 if (setjmp (myctx
)) {
314 SIGNAL (SIGALRM
, alrmser
);
316 td
= open (ttyspec
, O_WRONLY
);
321 lseek(md
, 0, SEEK_SET
);
323 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
324 if (write (td
, buffer
, i
) != i
)
329 #endif /* HAVE_GETUTXENT */