ktr cpu_send_ipiq
[dfdiff.git] / libexec / talkd / announce.c
blob5093e73fcf5128493be48c4bc4cea81d0647fde3
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
33 * @(#)announce.c 8.3 (Berkeley) 4/28/95
34 * $FreeBSD: src/libexec/talkd/announce.c,v 1.11.2.3 2001/10/18 12:30:42 des Exp $
35 * $DragonFly: src/libexec/talkd/announce.c,v 1.3 2003/11/14 03:54:31 dillon Exp $
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>
45 #include <protocols/talkd.h>
47 #include <errno.h>
48 #include <paths.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <syslog.h>
53 #include <unistd.h>
54 #include <vis.h>
56 #include "ttymsg.h"
58 extern char hostname[];
60 int print_mesg (char *, FILE *, CTL_MSG *, char *);
63 * Announce an invitation to talk.
67 * See if the user is accepting messages. If so, announce that
68 * a talk is requested.
70 int
71 announce(request, remote_machine)
72 CTL_MSG *request;
73 char *remote_machine;
75 char full_tty[32];
76 FILE *tf;
77 struct stat stbuf;
79 (void)snprintf(full_tty, sizeof(full_tty),
80 "%s%s", _PATH_DEV, request->r_tty);
81 if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0)
82 return (PERMISSION_DENIED);
83 return (print_mesg(request->r_tty, tf, request, remote_machine));
86 #define max(a,b) ( (a) > (b) ? (a) : (b) )
87 #define N_LINES 5
88 #define N_CHARS 256
91 * Build a block of characters containing the message.
92 * It is sent blank filled and in a single block to
93 * try to keep the message in one piece if the recipient
94 * in in vi at the time
96 int
97 print_mesg(tty, tf, request, remote_machine)
98 char *tty;
99 FILE *tf;
100 CTL_MSG *request;
101 char *remote_machine;
103 struct timeval clock;
104 time_t clock_sec;
105 struct timezone zone;
106 struct tm *localtime();
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;
115 i = 0;
116 max_size = 0;
117 gettimeofday(&clock, &zone);
118 clock_sec = clock.tv_sec;
119 localclock = localtime(&clock_sec);
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 on %d/%.2d/%.2d ...",
126 hostname, localclock->tm_hour , localclock->tm_min,
127 localclock->tm_year + 1900, localclock->tm_mon + 1,
128 localclock->tm_mday);
129 sizes[i] = strlen(line_buf[i]);
130 max_size = max(max_size, sizes[i]);
131 i++;
133 vis_user = malloc(strlen(request->l_name) * 4 + 1);
134 strvis(vis_user, request->l_name, VIS_CSTYLE);
135 (void)snprintf(line_buf[i], N_CHARS,
136 "talk: connection requested by %s@%s", vis_user, remote_machine);
137 sizes[i] = strlen(line_buf[i]);
138 max_size = max(max_size, sizes[i]);
139 i++;
140 (void)snprintf(line_buf[i], N_CHARS, "talk: respond with: talk %s@%s",
141 vis_user, remote_machine);
142 sizes[i] = strlen(line_buf[i]);
143 max_size = max(max_size, sizes[i]);
144 i++;
145 (void)snprintf(line_buf[i], N_CHARS, " ");
146 sizes[i] = strlen(line_buf[i]);
147 max_size = max(max_size, sizes[i]);
148 i++;
149 bptr = big_buf;
150 *bptr++ = '\007'; /* send something to wake them up */
151 *bptr++ = '\r'; /* add a \r in case of raw mode */
152 *bptr++ = '\n';
153 for (i = 0; i < N_LINES; i++) {
154 /* copy the line into the big buffer */
155 lptr = line_buf[i];
156 while (*lptr != '\0')
157 *(bptr++) = *(lptr++);
158 /* pad out the rest of the lines with blanks */
159 for (j = sizes[i]; j < max_size + 2; j++)
160 *(bptr++) = ' ';
161 *(bptr++) = '\r'; /* add a \r in case of raw mode */
162 *(bptr++) = '\n';
164 *bptr = '\0';
165 iovec.iov_base = big_buf;
166 iovec.iov_len = bptr - big_buf;
168 * we choose a timeout of RING_WAIT-5 seconds so that we don't
169 * stack up processes trying to write messages to a tty
170 * that is permanently blocked.
172 if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
173 return (FAILED);
175 return (SUCCESS);