1331722Seadler/*
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.
13262136Sbrueffer * 3. Neither the name of the University nor the names of its contributors
141592Srgrimes *    may be used to endorse or promote products derived from this software
151592Srgrimes *    without specific prior written permission.
161592Srgrimes *
171592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271592Srgrimes * SUCH DAMAGE.
281592Srgrimes */
291592Srgrimes
301592Srgrimes#ifndef lint
3131491Scharnier#if 0
3221838Spststatic char sccsid[] = "@(#)announce.c	8.3 (Berkeley) 4/28/95";
3331491Scharnier#endif
3431491Scharnierstatic const char rcsid[] =
3550476Speter  "$FreeBSD$";
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>
4421838Spst
451592Srgrimes#include <protocols/talkd.h>
4621838Spst
471592Srgrimes#include <errno.h>
4821838Spst#include <paths.h>
4921838Spst#include <stdio.h>
5021838Spst#include <stdlib.h>
5121838Spst#include <string.h>
521592Srgrimes#include <syslog.h>
531592Srgrimes#include <unistd.h>
5421838Spst#include <vis.h>
551592Srgrimes
5683244Sdd#include "ttymsg.h"
5790261Simp#include "extern.h"
5883244Sdd
591592Srgrimes/*
601592Srgrimes * Announce an invitation to talk.
611592Srgrimes */
628870Srgrimes
631592Srgrimes/*
648870Srgrimes * See if the user is accepting messages. If so, announce that
651592Srgrimes * a talk is requested.
661592Srgrimes */
6731491Scharnierint
6890261Simpannounce(CTL_MSG *request, const char *remote_machine)
691592Srgrimes{
701592Srgrimes	char full_tty[32];
711592Srgrimes	struct stat stbuf;
721592Srgrimes
731592Srgrimes	(void)snprintf(full_tty, sizeof(full_tty),
741592Srgrimes	    "%s%s", _PATH_DEV, request->r_tty);
751592Srgrimes	if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0)
761592Srgrimes		return (PERMISSION_DENIED);
77112998Sjmallett	return (print_mesg(request->r_tty, request, remote_machine));
781592Srgrimes}
791592Srgrimes
801592Srgrimes#define max(a,b) ( (a) > (b) ? (a) : (b) )
811592Srgrimes#define N_LINES 5
8221838Spst#define N_CHARS 256
831592Srgrimes
841592Srgrimes/*
858870Srgrimes * Build a block of characters containing the message.
861592Srgrimes * It is sent blank filled and in a single block to
871592Srgrimes * try to keep the message in one piece if the recipient
88177626Sbrueffer * in vi at the time
891592Srgrimes */
9031491Scharnierint
91112998Sjmallettprint_mesg(const char *tty, CTL_MSG *request,
9290261Simp    const char *remote_machine)
931592Srgrimes{
94112998Sjmallett	struct timeval now;
9537262Sbde	time_t clock_sec;
961592Srgrimes	struct tm *localclock;
971592Srgrimes	struct iovec iovec;
981592Srgrimes	char line_buf[N_LINES][N_CHARS];
991592Srgrimes	int sizes[N_LINES];
1001592Srgrimes	char big_buf[N_LINES*N_CHARS];
10121838Spst	char *bptr, *lptr, *vis_user;
1021592Srgrimes	int i, j, max_size;
1031592Srgrimes
1041592Srgrimes	i = 0;
1051592Srgrimes	max_size = 0;
106211056Sed	gettimeofday(&now, NULL);
107112998Sjmallett	clock_sec = now.tv_sec;
10837262Sbde	localclock = localtime(&clock_sec);
10921838Spst	(void)snprintf(line_buf[i], N_CHARS, " ");
1101592Srgrimes	sizes[i] = strlen(line_buf[i]);
1111592Srgrimes	max_size = max(max_size, sizes[i]);
1121592Srgrimes	i++;
11321838Spst	(void)snprintf(line_buf[i], N_CHARS,
11477862Sdd		"Message from Talk_Daemon@%s at %d:%02d on %d/%.2d/%.2d ...",
11577862Sdd		hostname, localclock->tm_hour , localclock->tm_min,
11679674Sbrian		localclock->tm_year + 1900, localclock->tm_mon + 1,
11777862Sdd		localclock->tm_mday);
1181592Srgrimes	sizes[i] = strlen(line_buf[i]);
1191592Srgrimes	max_size = max(max_size, sizes[i]);
1201592Srgrimes	i++;
12121838Spst
12221838Spst	vis_user = malloc(strlen(request->l_name) * 4 + 1);
12321838Spst	strvis(vis_user, request->l_name, VIS_CSTYLE);
12421838Spst	(void)snprintf(line_buf[i], N_CHARS,
12521838Spst	    "talk: connection requested by %s@%s", vis_user, remote_machine);
1261592Srgrimes	sizes[i] = strlen(line_buf[i]);
1271592Srgrimes	max_size = max(max_size, sizes[i]);
1281592Srgrimes	i++;
12921838Spst	(void)snprintf(line_buf[i], N_CHARS, "talk: respond with:  talk %s@%s",
13021838Spst	    vis_user, remote_machine);
1311592Srgrimes	sizes[i] = strlen(line_buf[i]);
1321592Srgrimes	max_size = max(max_size, sizes[i]);
1331592Srgrimes	i++;
13421838Spst	(void)snprintf(line_buf[i], N_CHARS, " ");
1351592Srgrimes	sizes[i] = strlen(line_buf[i]);
1361592Srgrimes	max_size = max(max_size, sizes[i]);
1371592Srgrimes	i++;
1381592Srgrimes	bptr = big_buf;
1391801Sphk	*bptr++ = '\007'; /* send something to wake them up */
1401592Srgrimes	*bptr++ = '\r';	/* add a \r in case of raw mode */
1411592Srgrimes	*bptr++ = '\n';
1421592Srgrimes	for (i = 0; i < N_LINES; i++) {
1431592Srgrimes		/* copy the line into the big buffer */
1441592Srgrimes		lptr = line_buf[i];
1451592Srgrimes		while (*lptr != '\0')
1461592Srgrimes			*(bptr++) = *(lptr++);
1471592Srgrimes		/* pad out the rest of the lines with blanks */
1481592Srgrimes		for (j = sizes[i]; j < max_size + 2; j++)
1491592Srgrimes			*(bptr++) = ' ';
1501592Srgrimes		*(bptr++) = '\r';	/* add a \r in case of raw mode */
1511592Srgrimes		*(bptr++) = '\n';
1521592Srgrimes	}
1531592Srgrimes	*bptr = '\0';
1541592Srgrimes	iovec.iov_base = big_buf;
1551592Srgrimes	iovec.iov_len = bptr - big_buf;
1561592Srgrimes	/*
1571592Srgrimes	 * we choose a timeout of RING_WAIT-5 seconds so that we don't
1581592Srgrimes	 * stack up processes trying to write messages to a tty
1591592Srgrimes	 * that is permanently blocked.
1601592Srgrimes	 */
1611592Srgrimes	if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
1621592Srgrimes		return (FAILED);
1631592Srgrimes
1641592Srgrimes	return (SUCCESS);
1651592Srgrimes}
166