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