Home | History | Annotate | Line # | Download | only in talkd
announce.c revision 1.11.2.1
      1  1.11.2.1        he /*	$NetBSD: announce.c,v 1.11.2.1 2000/10/11 18:45:16 he 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.11.2.1        he __RCSID("$NetBSD: announce.c,v 1.11.2.1 2000/10/11 18:45:16 he 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.10       mrg 	if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&S_IWGRP) == 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.11.2.1        he 	char big_buf[(N_LINES + 1) *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.10       mrg 	(void)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.10       mrg 
    126       1.6       rat 	(void)snprintf(line_buf[i], N_CHARS,
    127       1.6       rat 	    "Message from Talk_Daemon@%s at %d:%02d ...",
    128      1.10       mrg 	    hostname, localclock->tm_hour, localclock->tm_min );
    129       1.1       cgd 	sizes[i] = strlen(line_buf[i]);
    130       1.1       cgd 	max_size = max(max_size, sizes[i]);
    131       1.1       cgd 	i++;
    132      1.10       mrg 	vis_user = (char *)malloc(strlen(request->l_name) * 4 + 1);
    133       1.3     glass 	strvis(vis_user, request->l_name, VIS_CSTYLE);
    134       1.6       rat 	(void)snprintf(line_buf[i], N_CHARS,
    135       1.6       rat 	    "talk: connection requested by %s@%s.", vis_user, remote_machine);
    136       1.1       cgd 	sizes[i] = strlen(line_buf[i]);
    137       1.1       cgd 	max_size = max(max_size, sizes[i]);
    138       1.1       cgd 	i++;
    139       1.6       rat 	(void)snprintf(line_buf[i], N_CHARS,
    140       1.6       rat 	    "talk: respond with:  talk %s@%s", vis_user, remote_machine);
    141       1.1       cgd 	sizes[i] = strlen(line_buf[i]);
    142       1.1       cgd 	max_size = max(max_size, sizes[i]);
    143       1.1       cgd 	i++;
    144       1.6       rat 	(void)snprintf(line_buf[i], N_CHARS, " ");
    145       1.1       cgd 	sizes[i] = strlen(line_buf[i]);
    146       1.1       cgd 	max_size = max(max_size, sizes[i]);
    147       1.1       cgd 	i++;
    148       1.1       cgd 	bptr = big_buf;
    149       1.1       cgd 	*bptr++ = ''; /* send something to wake them up */
    150       1.1       cgd 	*bptr++ = '\r';	/* add a \r in case of raw mode */
    151       1.1       cgd 	*bptr++ = '\n';
    152       1.1       cgd 	for (i = 0; i < N_LINES; i++) {
    153       1.1       cgd 		/* copy the line into the big buffer */
    154       1.1       cgd 		lptr = line_buf[i];
    155      1.11       mrg 		while (*lptr != '\0' && lptr < (line_buf[i] + N_CHARS))
    156       1.1       cgd 			*(bptr++) = *(lptr++);
    157       1.1       cgd 		/* pad out the rest of the lines with blanks */
    158       1.1       cgd 		for (j = sizes[i]; j < max_size + 2; j++)
    159       1.1       cgd 			*(bptr++) = ' ';
    160       1.1       cgd 		*(bptr++) = '\r';	/* add a \r in case of raw mode */
    161       1.1       cgd 		*(bptr++) = '\n';
    162       1.1       cgd 	}
    163       1.1       cgd 	*bptr = '\0';
    164       1.7  christos 	iovec.iov_base = big_buf;
    165       1.7  christos 	iovec.iov_len = bptr - big_buf;
    166       1.7  christos 	/*
    167       1.7  christos 	 * we choose a timeout of RING_WAIT-5 seconds so that we don't
    168       1.7  christos 	 * stack up processes trying to write messages to a tty
    169       1.7  christos 	 * that is permanently blocked.
    170       1.7  christos 	 */
    171       1.7  christos 	if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
    172       1.7  christos 		return (FAILED);
    173       1.7  christos 
    174       1.7  christos 	return (SUCCESS);
    175       1.1       cgd }
    176