announce.c revision 50476
1/*
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)announce.c	8.3 (Berkeley) 4/28/95";
37#endif
38static const char rcsid[] =
39  "$FreeBSD: head/libexec/talkd/announce.c 50476 1999-08-28 00:22:10Z peter $";
40#endif /* not lint */
41
42#include <sys/types.h>
43#include <sys/uio.h>
44#include <sys/stat.h>
45#include <sys/time.h>
46#include <sys/wait.h>
47#include <sys/socket.h>
48
49#include <protocols/talkd.h>
50
51#include <errno.h>
52#include <paths.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <syslog.h>
57#include <unistd.h>
58#include <vis.h>
59
60extern char hostname[];
61
62int print_mesg __P((char *, FILE *, CTL_MSG *, char *));
63char *ttymsg __P((struct iovec *, int, char *, int));
64
65/*
66 * Announce an invitation to talk.
67 */
68
69/*
70 * See if the user is accepting messages. If so, announce that
71 * a talk is requested.
72 */
73int
74announce(request, remote_machine)
75	CTL_MSG *request;
76	char *remote_machine;
77{
78	char full_tty[32];
79	FILE *tf;
80	struct stat stbuf;
81
82	(void)snprintf(full_tty, sizeof(full_tty),
83	    "%s%s", _PATH_DEV, request->r_tty);
84	if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0)
85		return (PERMISSION_DENIED);
86	return (print_mesg(request->r_tty, tf, request, remote_machine));
87}
88
89#define max(a,b) ( (a) > (b) ? (a) : (b) )
90#define N_LINES 5
91#define N_CHARS 256
92
93/*
94 * Build a block of characters containing the message.
95 * It is sent blank filled and in a single block to
96 * try to keep the message in one piece if the recipient
97 * in in vi at the time
98 */
99int
100print_mesg(tty, tf, request, remote_machine)
101	char *tty;
102	FILE *tf;
103	CTL_MSG *request;
104	char *remote_machine;
105{
106	struct timeval clock;
107	time_t clock_sec;
108	struct timezone zone;
109	struct tm *localtime();
110	struct tm *localclock;
111	struct iovec iovec;
112	char line_buf[N_LINES][N_CHARS];
113	int sizes[N_LINES];
114	char big_buf[N_LINES*N_CHARS];
115	char *bptr, *lptr, *vis_user;
116	int i, j, max_size;
117
118	i = 0;
119	max_size = 0;
120	gettimeofday(&clock, &zone);
121	clock_sec = clock.tv_sec;
122	localclock = localtime(&clock_sec);
123	(void)snprintf(line_buf[i], N_CHARS, " ");
124	sizes[i] = strlen(line_buf[i]);
125	max_size = max(max_size, sizes[i]);
126	i++;
127	(void)snprintf(line_buf[i], N_CHARS,
128		"Message from Talk_Daemon@%s at %d:%02d ...",
129		hostname, localclock->tm_hour , localclock->tm_min );
130	sizes[i] = strlen(line_buf[i]);
131	max_size = max(max_size, sizes[i]);
132	i++;
133
134	vis_user = malloc(strlen(request->l_name) * 4 + 1);
135	strvis(vis_user, request->l_name, VIS_CSTYLE);
136	(void)snprintf(line_buf[i], N_CHARS,
137	    "talk: connection requested by %s@%s", vis_user, remote_machine);
138	sizes[i] = strlen(line_buf[i]);
139	max_size = max(max_size, sizes[i]);
140	i++;
141	(void)snprintf(line_buf[i], N_CHARS, "talk: respond with:  talk %s@%s",
142	    vis_user, remote_machine);
143	sizes[i] = strlen(line_buf[i]);
144	max_size = max(max_size, sizes[i]);
145	i++;
146	(void)snprintf(line_buf[i], N_CHARS, " ");
147	sizes[i] = strlen(line_buf[i]);
148	max_size = max(max_size, sizes[i]);
149	i++;
150	bptr = big_buf;
151	*bptr++ = '\007'; /* send something to wake them up */
152	*bptr++ = '\r';	/* add a \r in case of raw mode */
153	*bptr++ = '\n';
154	for (i = 0; i < N_LINES; i++) {
155		/* copy the line into the big buffer */
156		lptr = line_buf[i];
157		while (*lptr != '\0')
158			*(bptr++) = *(lptr++);
159		/* pad out the rest of the lines with blanks */
160		for (j = sizes[i]; j < max_size + 2; j++)
161			*(bptr++) = ' ';
162		*(bptr++) = '\r';	/* add a \r in case of raw mode */
163		*(bptr++) = '\n';
164	}
165	*bptr = '\0';
166	iovec.iov_base = big_buf;
167	iovec.iov_len = bptr - big_buf;
168	/*
169	 * we choose a timeout of RING_WAIT-5 seconds so that we don't
170	 * stack up processes trying to write messages to a tty
171	 * that is permanently blocked.
172	 */
173	if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
174		return (FAILED);
175
176	return (SUCCESS);
177}
178