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