xref: /freebsd/libexec/talkd/announce.c (revision 1130b656)
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  *	$FreeBSD$
34  */
35 
36 #ifndef lint
37 static char sccsid[] = "@(#)announce.c	8.2 (Berkeley) 1/7/94";
38 #endif /* not lint */
39 
40 #include <sys/types.h>
41 #include <sys/uio.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/wait.h>
45 #include <sys/socket.h>
46 #include <protocols/talkd.h>
47 #include <errno.h>
48 #include <syslog.h>
49 #include <unistd.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <paths.h>
53 
54 extern char hostname[];
55 
56 /*
57  * Announce an invitation to talk.
58  */
59 
60 /*
61  * See if the user is accepting messages. If so, announce that
62  * a talk is requested.
63  */
64 announce(request, remote_machine)
65 	CTL_MSG *request;
66 	char *remote_machine;
67 {
68 	char full_tty[32];
69 	FILE *tf;
70 	struct stat stbuf;
71 
72 	(void)snprintf(full_tty, sizeof(full_tty),
73 	    "%s%s", _PATH_DEV, request->r_tty);
74 	if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0)
75 		return (PERMISSION_DENIED);
76 	return (print_mesg(request->r_tty, tf, request, remote_machine));
77 }
78 
79 #define max(a,b) ( (a) > (b) ? (a) : (b) )
80 #define N_LINES 5
81 #define N_CHARS 120
82 
83 /*
84  * Build a block of characters containing the message.
85  * It is sent blank filled and in a single block to
86  * try to keep the message in one piece if the recipient
87  * in in vi at the time
88  */
89 print_mesg(tty, tf, request, remote_machine)
90 	char *tty;
91 	FILE *tf;
92 	CTL_MSG *request;
93 	char *remote_machine;
94 {
95 	struct timeval clock;
96 	struct timezone zone;
97 	struct tm *localtime();
98 	struct tm *localclock;
99 	struct iovec iovec;
100 	char line_buf[N_LINES][N_CHARS];
101 	int sizes[N_LINES];
102 	char big_buf[N_LINES*N_CHARS];
103 	char *bptr, *lptr, *ttymsg();
104 	int i, j, max_size;
105 
106 	i = 0;
107 	max_size = 0;
108 	gettimeofday(&clock, &zone);
109 	localclock = localtime( &clock.tv_sec );
110 	(void)sprintf(line_buf[i], " ");
111 	sizes[i] = strlen(line_buf[i]);
112 	max_size = max(max_size, sizes[i]);
113 	i++;
114 	(void)sprintf(line_buf[i], "Message from Talk_Daemon@%s at %d:%02d ...",
115 	hostname, localclock->tm_hour , localclock->tm_min );
116 	sizes[i] = strlen(line_buf[i]);
117 	max_size = max(max_size, sizes[i]);
118 	i++;
119 	(void)sprintf(line_buf[i], "talk: connection requested by %s@%s",
120 		request->l_name, remote_machine);
121 	sizes[i] = strlen(line_buf[i]);
122 	max_size = max(max_size, sizes[i]);
123 	i++;
124 	(void)sprintf(line_buf[i], "talk: respond with:  talk %s@%s",
125 		request->l_name, remote_machine);
126 	sizes[i] = strlen(line_buf[i]);
127 	max_size = max(max_size, sizes[i]);
128 	i++;
129 	(void)sprintf(line_buf[i], " ");
130 	sizes[i] = strlen(line_buf[i]);
131 	max_size = max(max_size, sizes[i]);
132 	i++;
133 	bptr = big_buf;
134 	*bptr++ = '\007'; /* send something to wake them up */
135 	*bptr++ = '\r';	/* add a \r in case of raw mode */
136 	*bptr++ = '\n';
137 	for (i = 0; i < N_LINES; i++) {
138 		/* copy the line into the big buffer */
139 		lptr = line_buf[i];
140 		while (*lptr != '\0')
141 			*(bptr++) = *(lptr++);
142 		/* pad out the rest of the lines with blanks */
143 		for (j = sizes[i]; j < max_size + 2; j++)
144 			*(bptr++) = ' ';
145 		*(bptr++) = '\r';	/* add a \r in case of raw mode */
146 		*(bptr++) = '\n';
147 	}
148 	*bptr = '\0';
149 	iovec.iov_base = big_buf;
150 	iovec.iov_len = bptr - big_buf;
151 	/*
152 	 * we choose a timeout of RING_WAIT-5 seconds so that we don't
153 	 * stack up processes trying to write messages to a tty
154 	 * that is permanently blocked.
155 	 */
156 	if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
157 		return (FAILED);
158 
159 	return (SUCCESS);
160 }
161