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