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