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