announce.c revision 1592
11592Srgrimes/*
21592Srgrimes * Copyright (c) 1983, 1993
31592Srgrimes *	The Regents of the University of California.  All rights reserved.
41592Srgrimes *
51592Srgrimes * Redistribution and use in source and binary forms, with or without
61592Srgrimes * modification, are permitted provided that the following conditions
71592Srgrimes * are met:
81592Srgrimes * 1. Redistributions of source code must retain the above copyright
91592Srgrimes *    notice, this list of conditions and the following disclaimer.
101592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111592Srgrimes *    notice, this list of conditions and the following disclaimer in the
121592Srgrimes *    documentation and/or other materials provided with the distribution.
131592Srgrimes * 3. All advertising materials mentioning features or use of this software
141592Srgrimes *    must display the following acknowledgement:
151592Srgrimes *	This product includes software developed by the University of
161592Srgrimes *	California, Berkeley and its contributors.
171592Srgrimes * 4. Neither the name of the University nor the names of its contributors
181592Srgrimes *    may be used to endorse or promote products derived from this software
191592Srgrimes *    without specific prior written permission.
201592Srgrimes *
211592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311592Srgrimes * SUCH DAMAGE.
321592Srgrimes */
331592Srgrimes
341592Srgrimes#ifndef lint
351592Srgrimesstatic char sccsid[] = "@(#)announce.c	8.2 (Berkeley) 1/7/94";
361592Srgrimes#endif /* not lint */
371592Srgrimes
381592Srgrimes#include <sys/types.h>
391592Srgrimes#include <sys/uio.h>
401592Srgrimes#include <sys/stat.h>
411592Srgrimes#include <sys/time.h>
421592Srgrimes#include <sys/wait.h>
431592Srgrimes#include <sys/socket.h>
441592Srgrimes#include <protocols/talkd.h>
451592Srgrimes#include <sgtty.h>
461592Srgrimes#include <errno.h>
471592Srgrimes#include <syslog.h>
481592Srgrimes#include <unistd.h>
491592Srgrimes#include <stdio.h>
501592Srgrimes#include <string.h>
511592Srgrimes#include <paths.h>
521592Srgrimes
531592Srgrimesextern char hostname[];
541592Srgrimes
551592Srgrimes/*
561592Srgrimes * Announce an invitation to talk.
571592Srgrimes */
581592Srgrimes
591592Srgrimes/*
601592Srgrimes * See if the user is accepting messages. If so, announce that
611592Srgrimes * a talk is requested.
621592Srgrimes */
631592Srgrimesannounce(request, remote_machine)
641592Srgrimes	CTL_MSG *request;
651592Srgrimes	char *remote_machine;
661592Srgrimes{
671592Srgrimes	char full_tty[32];
681592Srgrimes	FILE *tf;
691592Srgrimes	struct stat stbuf;
701592Srgrimes
711592Srgrimes	(void)snprintf(full_tty, sizeof(full_tty),
721592Srgrimes	    "%s%s", _PATH_DEV, request->r_tty);
731592Srgrimes	if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0)
741592Srgrimes		return (PERMISSION_DENIED);
751592Srgrimes	return (print_mesg(request->r_tty, tf, request, remote_machine));
761592Srgrimes}
771592Srgrimes
781592Srgrimes#define max(a,b) ( (a) > (b) ? (a) : (b) )
791592Srgrimes#define N_LINES 5
801592Srgrimes#define N_CHARS 120
811592Srgrimes
821592Srgrimes/*
831592Srgrimes * Build a block of characters containing the message.
841592Srgrimes * It is sent blank filled and in a single block to
851592Srgrimes * try to keep the message in one piece if the recipient
861592Srgrimes * in in vi at the time
871592Srgrimes */
881592Srgrimesprint_mesg(tty, tf, request, remote_machine)
891592Srgrimes	char *tty;
901592Srgrimes	FILE *tf;
911592Srgrimes	CTL_MSG *request;
921592Srgrimes	char *remote_machine;
931592Srgrimes{
941592Srgrimes	struct timeval clock;
951592Srgrimes	struct timezone zone;
961592Srgrimes	struct tm *localtime();
971592Srgrimes	struct tm *localclock;
981592Srgrimes	struct iovec iovec;
991592Srgrimes	char line_buf[N_LINES][N_CHARS];
1001592Srgrimes	int sizes[N_LINES];
1011592Srgrimes	char big_buf[N_LINES*N_CHARS];
1021592Srgrimes	char *bptr, *lptr, *ttymsg();
1031592Srgrimes	int i, j, max_size;
1041592Srgrimes
1051592Srgrimes	i = 0;
1061592Srgrimes	max_size = 0;
1071592Srgrimes	gettimeofday(&clock, &zone);
1081592Srgrimes	localclock = localtime( &clock.tv_sec );
1091592Srgrimes	(void)sprintf(line_buf[i], " ");
1101592Srgrimes	sizes[i] = strlen(line_buf[i]);
1111592Srgrimes	max_size = max(max_size, sizes[i]);
1121592Srgrimes	i++;
1131592Srgrimes	(void)sprintf(line_buf[i], "Message from Talk_Daemon@%s at %d:%02d ...",
1141592Srgrimes	hostname, localclock->tm_hour , localclock->tm_min );
1151592Srgrimes	sizes[i] = strlen(line_buf[i]);
1161592Srgrimes	max_size = max(max_size, sizes[i]);
1171592Srgrimes	i++;
1181592Srgrimes	(void)sprintf(line_buf[i], "talk: connection requested by %s@%s",
1191592Srgrimes		request->l_name, remote_machine);
1201592Srgrimes	sizes[i] = strlen(line_buf[i]);
1211592Srgrimes	max_size = max(max_size, sizes[i]);
1221592Srgrimes	i++;
1231592Srgrimes	(void)sprintf(line_buf[i], "talk: respond with:  talk %s@%s",
1241592Srgrimes		request->l_name, remote_machine);
1251592Srgrimes	sizes[i] = strlen(line_buf[i]);
1261592Srgrimes	max_size = max(max_size, sizes[i]);
1271592Srgrimes	i++;
1281592Srgrimes	(void)sprintf(line_buf[i], " ");
1291592Srgrimes	sizes[i] = strlen(line_buf[i]);
1301592Srgrimes	max_size = max(max_size, sizes[i]);
1311592Srgrimes	i++;
1321592Srgrimes	bptr = big_buf;
1331592Srgrimes	*bptr++ = ''; /* send something to wake them up */
1341592Srgrimes	*bptr++ = '\r';	/* add a \r in case of raw mode */
1351592Srgrimes	*bptr++ = '\n';
1361592Srgrimes	for (i = 0; i < N_LINES; i++) {
1371592Srgrimes		/* copy the line into the big buffer */
1381592Srgrimes		lptr = line_buf[i];
1391592Srgrimes		while (*lptr != '\0')
1401592Srgrimes			*(bptr++) = *(lptr++);
1411592Srgrimes		/* pad out the rest of the lines with blanks */
1421592Srgrimes		for (j = sizes[i]; j < max_size + 2; j++)
1431592Srgrimes			*(bptr++) = ' ';
1441592Srgrimes		*(bptr++) = '\r';	/* add a \r in case of raw mode */
1451592Srgrimes		*(bptr++) = '\n';
1461592Srgrimes	}
1471592Srgrimes	*bptr = '\0';
1481592Srgrimes	iovec.iov_base = big_buf;
1491592Srgrimes	iovec.iov_len = bptr - big_buf;
1501592Srgrimes	/*
1511592Srgrimes	 * we choose a timeout of RING_WAIT-5 seconds so that we don't
1521592Srgrimes	 * stack up processes trying to write messages to a tty
1531592Srgrimes	 * that is permanently blocked.
1541592Srgrimes	 */
1551592Srgrimes	if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
1561592Srgrimes		return (FAILED);
1571592Srgrimes
1581592Srgrimes	return (SUCCESS);
1591592Srgrimes}
160