]>
git.cameronkatri.com Git - apple_cmds.git/blob - remote_cmds/talkd.tproj/announce.c
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid
[] = "@(#)announce.c 8.3 (Berkeley) 4/28/95";
38 __attribute__((__used__
))
39 static const char rcsid
[] =
40 "$FreeBSD: src/libexec/talkd/announce.c,v 1.16 2003/04/03 05:13:27 jmallett Exp $";
43 #include <sys/cdefs.h>
45 #include <sys/types.h>
50 #include <sys/socket.h>
52 #include <protocols/talkd.h>
70 extern char hostname
[];
73 * Announce an invitation to talk.
77 * See if the user is accepting messages. If so, announce that
78 * a talk is requested.
81 announce(CTL_MSG
*request
, const char *remote_machine
)
86 (void)snprintf(full_tty
, sizeof(full_tty
),
87 "%s%s", _PATH_DEV
, request
->r_tty
);
88 if (stat(full_tty
, &stbuf
) < 0 || (stbuf
.st_mode
&020) == 0)
89 return (PERMISSION_DENIED
);
90 return (print_mesg(request
->r_tty
, request
, remote_machine
));
93 #define max(a,b) ( (a) > (b) ? (a) : (b) )
98 * Build a block of characters containing the message.
99 * It is sent blank filled and in a single block to
100 * try to keep the message in one piece if the recipient
101 * in in vi at the time
104 print_mesg(const char *tty
, CTL_MSG
*request
,
105 const char *remote_machine
)
109 struct timezone zone
;
110 struct tm
*localclock
;
112 char line_buf
[N_LINES
][N_CHARS
];
114 char big_buf
[N_LINES
*N_CHARS
];
115 char *bptr
, *lptr
, *vis_user
;
120 gettimeofday(&now
, &zone
);
121 clock_sec
= now
.tv_sec
;
122 localclock
= localtime(&clock_sec
);
123 (void)snprintf(line_buf
[i
], N_CHARS
, " ");
124 sizes
[i
] = strlen(line_buf
[i
]);
125 max_size
= max(max_size
, sizes
[i
]);
127 (void)snprintf(line_buf
[i
], N_CHARS
,
128 "Message from Talk_Daemon@%s at %d:%02d on %d/%.2d/%.2d ...",
129 hostname
, localclock
->tm_hour
, localclock
->tm_min
,
130 localclock
->tm_year
+ 1900, localclock
->tm_mon
+ 1,
131 localclock
->tm_mday
);
132 sizes
[i
] = strlen(line_buf
[i
]);
133 max_size
= max(max_size
, sizes
[i
]);
136 vis_user
= malloc(strlen(request
->l_name
) * 4 + 1);
137 strvis(vis_user
, request
->l_name
, VIS_CSTYLE
);
138 (void)snprintf(line_buf
[i
], N_CHARS
,
139 "talk: connection requested by %s@%s", vis_user
, remote_machine
);
140 sizes
[i
] = strlen(line_buf
[i
]);
141 max_size
= max(max_size
, sizes
[i
]);
143 (void)snprintf(line_buf
[i
], N_CHARS
, "talk: respond with: talk %s@%s",
144 vis_user
, remote_machine
);
145 sizes
[i
] = strlen(line_buf
[i
]);
146 max_size
= max(max_size
, sizes
[i
]);
148 (void)snprintf(line_buf
[i
], N_CHARS
, " ");
149 sizes
[i
] = strlen(line_buf
[i
]);
150 max_size
= max(max_size
, sizes
[i
]);
153 *bptr
++ = '\007'; /* send something to wake them up */
154 *bptr
++ = '\r'; /* add a \r in case of raw mode */
156 for (i
= 0; i
< N_LINES
; i
++) {
157 /* copy the line into the big buffer */
159 while (*lptr
!= '\0')
160 *(bptr
++) = *(lptr
++);
161 /* pad out the rest of the lines with blanks */
162 for (j
= sizes
[i
]; j
< max_size
+ 2; j
++)
164 *(bptr
++) = '\r'; /* add a \r in case of raw mode */
168 iovec
.iov_base
= big_buf
;
169 iovec
.iov_len
= bptr
- big_buf
;
171 * we choose a timeout of RING_WAIT-5 seconds so that we don't
172 * stack up processes trying to write messages to a tty
173 * that is permanently blocked.
175 if (ttymsg(&iovec
, 1, tty
, RING_WAIT
- 5) != NULL
)