]>
diplodocus.org Git - nmh/blob - uip/rcvtty.c
3 * rcvtty.c -- a rcvmail program (a lot like rcvalert) handling IPC ttys
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
10 /* Changed to use getutent() and friends. Assumes that when getutent() exists,
11 * a number of other things also exist. Please check.
12 * Ruud de Rooij <ruud@ruud.org> Sun, 28 May 2000 17:28:55 +0200
16 #include <h/signals.h>
18 #include <h/rcvmail.h>
19 #include <h/scansbr.h>
26 #endif /* HAVE_GETUTXENT */
29 "%2(hour{dtimenow}):%02(min{dtimenow}): %<(size)%5(size) %>%<{encrypted}E%>\
30 %<(mymbox{from})%<{to}To:%14(friendly{to})%>%>%<(zero)%17(friendly{from})%> \
31 %{subject}%<{body}<<%{body}>>%>"
33 #define RCVTTY_SWITCHES \
34 X("biff", 0, BIFFSW) \
35 X("form formatfile", 0, FORMSW) \
36 X("format string", 5, FMTSW) \
37 X("width columns", 0, WIDTHSW) \
38 X("newline", 0, NLSW) \
39 X("nonewline", 0, NNLSW) \
41 X("nobell", 0, NBELSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(RCVTTY
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(RCVTTY
, switches
);
55 static int newline
= 1;
57 static int width
= -1;
58 static char *form
= NULL
;
59 static char *format
= NULL
;
64 char *getusername(void);
69 static void alrmser (int);
70 static int message_fd (char **);
71 static int header_fd (void);
73 static void alert (char *, int);
74 #endif /* HAVE_GETUTXENT */
78 main (int argc
, char **argv
)
81 char *cp
, *user
, buf
[BUFSIZ
], tty
[BUFSIZ
];
82 char **argp
, **arguments
, *vec
[MAXARGS
];
85 if (nmh_init(argv
[0], 1)) { return 1; }
88 arguments
= getarguments (invo_name
, argc
, argv
, 1);
91 while ((cp
= *argp
++)) {
93 switch (smatch (++cp
, switches
)) {
95 ambigsw (cp
, switches
);
102 snprintf (buf
, sizeof(buf
), "%s [command ...]", invo_name
);
103 print_help (buf
, switches
, 1);
106 print_version(invo_name
);
114 if (!(form
= *argp
++) || *form
== '-')
115 adios (NULL
, "missing argument to %s", argp
[-2]);
119 if (!(format
= *argp
++) || *format
== '-')
120 adios (NULL
, "missing argument to %s", argp
[-2]);
125 if (!(cp
= *argp
++) || *cp
== '-')
126 adios(NULL
, "missing argument to %s", argp
[-2]);
148 if ((md
= vecp
? message_fd (vec
) : header_fd ()) == NOTOK
)
151 user
= getusername();
155 while ((utp
= getutxent()) != NULL
) {
156 if (utp
->ut_type
== USER_PROCESS
&& utp
->ut_user
[0] != 0
157 && utp
->ut_line
[0] != 0
158 && strncmp (user
, utp
->ut_user
, sizeof(utp
->ut_user
)) == 0) {
159 strncpy (tty
, utp
->ut_line
, sizeof(utp
->ut_line
));
167 #endif /* HAVE_GETUTXENT */
183 message_fd (char **vec
)
191 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
192 advise(NULL
, "unable to create temporary file in %s", get_temp_dir());
195 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
197 if ((child_id
= fork()) == NOTOK
) {
201 } else if (child_id
) {
203 if (!setjmp (myctx
)) {
204 SIGNAL (SIGALRM
, alrmser
);
205 bytes
= fstat(fileno (stdin
), &st
) != NOTOK
? (int) st
.st_size
: 100;
207 /* amount of time to wait depends on message size */
209 /* give at least 5 minutes */
211 } else if (bytes
>= 90000) {
212 /* but 30 minutes should be long enough */
215 seconds
= (bytes
/ 60) + 300;
217 alarm ((unsigned int) seconds
);
218 pidwait(child_id
, OK
);
221 if (fstat (fd
, &st
) != NOTOK
&& st
.st_size
> (off_t
) 0)
225 * Ruthlessly kill the child and anything
226 * else in its process group.
228 killpg(child_id
, SIGKILL
);
236 if (dup2 (fd
, 1) == NOTOK
|| dup2 (fd
, 2) == NOTOK
)
239 setpgid ((pid_t
) 0, getpid ()); /* put in own process group */
240 if (execvp (vec
[0], vec
) == NOTOK
) {
254 charstring_t scanl
= NULL
;
256 if ((tfile
= m_mktemp2(NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
257 advise(NULL
, "unable to create temporary file in %s", get_temp_dir());
260 (void) m_unlink(tfile
); /* Use fd, no longer need the file name. */
264 /* get new format string */
265 nfs
= new_fs (form
, format
, SCANFMT
);
266 scan (stdin
, 0, 0, nfs
, width
, 0, 0, NULL
, 0L, 0, &scanl
);
269 if (write (fd
, "\n\r", 2) < 0) {
270 advise (tfile
, "write LF/CR");
273 if (write (fd
, charstring_buffer (scanl
), charstring_bytes (scanl
)) < 0) {
274 advise (tfile
, "write");
276 charstring_free (scanl
);
278 if (write (fd
, "\007", 1) < 0) {
279 advise (tfile
, "write BEL");
289 alert (char *tty
, int md
)
292 char buffer
[BUFSIZ
], ttyspec
[BUFSIZ
];
295 snprintf (ttyspec
, sizeof(ttyspec
), "/dev/%s", tty
);
298 * The mask depends on whether we are checking for
299 * write permission based on `biff' or `mesg'.
301 mask
= biff
? S_IEXEC
: (S_IWRITE
>> 3);
302 if (stat (ttyspec
, &st
) == NOTOK
|| (st
.st_mode
& mask
) == 0)
305 if (!setjmp (myctx
)) {
306 SIGNAL (SIGALRM
, alrmser
);
308 td
= open (ttyspec
, O_WRONLY
);
317 lseek (md
, (off_t
) 0, SEEK_SET
);
319 while ((i
= read (md
, buffer
, sizeof(buffer
))) > 0)
320 if (write (td
, buffer
, i
) != i
)
325 #endif /* HAVE_GETUTXENT */