Home | History | Annotate | Line # | Download | only in talkd
announce.c revision 1.9
      1 /*	$NetBSD: announce.c,v 1.9 1998/04/01 14:51:54 kleink Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)announce.c	8.3 (Berkeley) 4/28/95";
     40 #else
     41 __RCSID("$NetBSD: announce.c,v 1.9 1998/04/01 14:51:54 kleink Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/uio.h>
     47 #include <sys/stat.h>
     48 #include <sys/time.h>
     49 #include <sys/wait.h>
     50 #include <sys/socket.h>
     51 #include <protocols/talkd.h>
     52 #include <errno.h>
     53 #include <syslog.h>
     54 #include <unistd.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <time.h>
     59 #include <vis.h>
     60 #include <paths.h>
     61 #include <util.h>
     62 #include "extern.h"
     63 
     64 extern char hostname[];
     65 
     66 /*
     67  * Announce an invitation to talk.
     68  */
     69 
     70 /*
     71  * See if the user is accepting messages. If so, announce that
     72  * a talk is requested.
     73  */
     74 int
     75 announce(request, remote_machine)
     76 	CTL_MSG *request;
     77 	char *remote_machine;
     78 {
     79 	char full_tty[32];
     80 	struct stat stbuf;
     81 
     82 	(void)snprintf(full_tty, sizeof(full_tty),
     83 	    "%s%s", _PATH_DEV, request->r_tty);
     84 	if (stat(full_tty, &stbuf) < 0 || (stbuf.st_mode&020) == 0)
     85 		return (PERMISSION_DENIED);
     86 	return (print_mesg(request->r_tty, request, remote_machine));
     87 }
     88 
     89 #define max(a,b) ( (a) > (b) ? (a) : (b) )
     90 #define N_LINES 5
     91 #define N_CHARS 256
     92 
     93 /*
     94  * Build a block of characters containing the message.
     95  * It is sent blank filled and in a single block to
     96  * try to keep the message in one piece if the recipient
     97  * in in vi at the time
     98  */
     99 int
    100 print_mesg(tty, request, remote_machine)
    101 	char *tty;
    102 	CTL_MSG *request;
    103 	char *remote_machine;
    104 {
    105 	struct timeval clock;
    106 	time_t clocktime;
    107 	struct timezone zone;
    108 	struct tm *localclock;
    109 	struct iovec iovec;
    110 	char line_buf[N_LINES][N_CHARS];
    111 	int sizes[N_LINES];
    112 	char big_buf[N_LINES*N_CHARS];
    113 	char *bptr, *lptr, *vis_user;
    114 	int i, j, max_size;
    115 
    116 	i = 0;
    117 	max_size = 0;
    118 	gettimeofday(&clock, &zone);
    119 	clocktime = clock.tv_sec;
    120 	localclock = localtime(&clocktime);
    121 	(void)snprintf(line_buf[i], N_CHARS, " ");
    122 	sizes[i] = strlen(line_buf[i]);
    123 	max_size = max(max_size, sizes[i]);
    124 	i++;
    125 	(void)snprintf(line_buf[i], N_CHARS,
    126 	    "Message from Talk_Daemon@%s at %d:%02d ...",
    127 	    hostname, localclock->tm_hour , localclock->tm_min );
    128 	sizes[i] = strlen(line_buf[i]);
    129 	max_size = max(max_size, sizes[i]);
    130 	i++;
    131 	vis_user = (char *) malloc(strlen(request->l_name) * 4 + 1);
    132 	strvis(vis_user, request->l_name, VIS_CSTYLE);
    133 	(void)snprintf(line_buf[i], N_CHARS,
    134 	    "talk: connection requested by %s@%s.", vis_user, remote_machine);
    135 	sizes[i] = strlen(line_buf[i]);
    136 	max_size = max(max_size, sizes[i]);
    137 	i++;
    138 	(void)snprintf(line_buf[i], N_CHARS,
    139 	    "talk: respond with:  talk %s@%s", vis_user, remote_machine);
    140 	sizes[i] = strlen(line_buf[i]);
    141 	max_size = max(max_size, sizes[i]);
    142 	i++;
    143 	(void)snprintf(line_buf[i], N_CHARS, " ");
    144 	sizes[i] = strlen(line_buf[i]);
    145 	max_size = max(max_size, sizes[i]);
    146 	i++;
    147 	bptr = big_buf;
    148 	*bptr++ = ''; /* send something to wake them up */
    149 	*bptr++ = '\r';	/* add a \r in case of raw mode */
    150 	*bptr++ = '\n';
    151 	for (i = 0; i < N_LINES; i++) {
    152 		/* copy the line into the big buffer */
    153 		lptr = line_buf[i];
    154 		while (*lptr != '\0')
    155 			*(bptr++) = *(lptr++);
    156 		/* pad out the rest of the lines with blanks */
    157 		for (j = sizes[i]; j < max_size + 2; j++)
    158 			*(bptr++) = ' ';
    159 		*(bptr++) = '\r';	/* add a \r in case of raw mode */
    160 		*(bptr++) = '\n';
    161 	}
    162 	*bptr = '\0';
    163 	iovec.iov_base = big_buf;
    164 	iovec.iov_len = bptr - big_buf;
    165 	/*
    166 	 * we choose a timeout of RING_WAIT-5 seconds so that we don't
    167 	 * stack up processes trying to write messages to a tty
    168 	 * that is permanently blocked.
    169 	 */
    170 	if (ttymsg(&iovec, 1, tty, RING_WAIT - 5) != NULL)
    171 		return (FAILED);
    172 
    173 	return (SUCCESS);
    174 }
    175