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