1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 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. 20 * 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. 32 */ 33 34 #ifndef lint 35 static char sccsid[] = "@(#)announce.c 8.2 (Berkeley) 1/7/94"; 36 #endif /* not lint */ 37 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> 44 #include <protocols/talkd.h> 45 #include <sgtty.h> 46 #include <errno.h> 47 #include <syslog.h> 48 #include <unistd.h> 49 #include <stdio.h> 50 #include <string.h> 51 #include <paths.h> 52 53 extern char hostname[]; 54 55 /* 56 * Announce an invitation to talk. 57 */ 58 59 /* 60 * See if the user is accepting messages. If so, announce that 61 * a talk is requested. 62 */ 63 announce(request, remote_machine) 64 CTL_MSG *request; 65 char *remote_machine; 66 { 67 char full_tty[32]; 68 FILE *tf; 69 struct stat stbuf; 70 71 (void)snprintf(full_tty, sizeof(full_tty), 72 "%s%s", _PATH_DEV, request->r_tty); 73 if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0) 74 return (PERMISSION_DENIED); 75 return (print_mesg(request->r_tty, tf, request, remote_machine)); 76 } 77 78 #define max(a,b) ( (a) > (b) ? (a) : (b) ) 79 #define N_LINES 5 80 #define N_CHARS 120 81 82 /* 83 * Build a block of characters containing the message. 84 * It is sent blank filled and in a single block to 85 * try to keep the message in one piece if the recipient 86 * in in vi at the time 87 */ 88 print_mesg(tty, tf, request, remote_machine) 89 char *tty; 90 FILE *tf; 91 CTL_MSG *request; 92 char *remote_machine; 93 { 94 struct timeval clock; 95 struct timezone zone; 96 struct tm *localtime(); 97 struct tm *localclock; 98 struct iovec iovec; 99 char line_buf[N_LINES][N_CHARS]; 100 int sizes[N_LINES]; 101 char big_buf[N_LINES*N_CHARS]; 102 char *bptr, *lptr, *ttymsg(); 103 int i, j, max_size; 104 105 i = 0; 106 max_size = 0; 107 gettimeofday(&clock, &zone); 108 localclock = localtime( &clock.tv_sec ); 109 (void)sprintf(line_buf[i], " "); 110 sizes[i] = strlen(line_buf[i]); 111 max_size = max(max_size, sizes[i]); 112 i++; 113 (void)sprintf(line_buf[i], "Message from Talk_Daemon@%s at %d:%02d ...", 114 hostname, localclock->tm_hour , localclock->tm_min ); 115 sizes[i] = strlen(line_buf[i]); 116 max_size = max(max_size, sizes[i]); 117 i++; 118 (void)sprintf(line_buf[i], "talk: connection requested by %s@%s", 119 request->l_name, remote_machine); 120 sizes[i] = strlen(line_buf[i]); 121 max_size = max(max_size, sizes[i]); 122 i++; 123 (void)sprintf(line_buf[i], "talk: respond with: talk %s@%s", 124 request->l_name, remote_machine); 125 sizes[i] = strlen(line_buf[i]); 126 max_size = max(max_size, sizes[i]); 127 i++; 128 (void)sprintf(line_buf[i], " "); 129 sizes[i] = strlen(line_buf[i]); 130 max_size = max(max_size, sizes[i]); 131 i++; 132 bptr = big_buf; 133 *bptr++ = '\007'; /* send something to wake them up */ 134 *bptr++ = '\r'; /* add a \r in case of raw mode */ 135 *bptr++ = '\n'; 136 for (i = 0; i < N_LINES; i++) { 137 /* copy the line into the big buffer */ 138 lptr = line_buf[i]; 139 while (*lptr != '\0') 140 *(bptr++) = *(lptr++); 141 /* pad out the rest of the lines with blanks */ 142 for (j = sizes[i]; j < max_size + 2; j++) 143 *(bptr++) = ' '; 144 *(bptr++) = '\r'; /* add a \r in case of raw mode */ 145 *(bptr++) = '\n'; 146 } 147 *bptr = '\0'; 148 iovec.iov_base = big_buf; 149 iovec.iov_len = bptr - big_buf; 150 /* 151 * we choose a timeout of RING_WAIT-5 seconds so that we don't 152 * stack up processes trying to write messages to a tty 153 * that is permanently blocked. 154 */ 155 if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL) 156 return (FAILED); 157 158 return (SUCCESS); 159 } 160