1 /* $NetBSD: announce.c,v 1.24 2009/03/16 01:04:32 lukem Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)announce.c 8.3 (Berkeley) 4/28/95";
37 __RCSID("$NetBSD: announce.c,v 1.24 2009/03/16 01:04:32 lukem Exp $");
41 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <protocols/talkd.h>
60 extern char hostname
[];
63 * Announce an invitation to talk.
67 * See if the user is accepting messages. If so, announce that
68 * a talk is requested.
71 announce(CTL_MSG
*request
, const char *remote_machine
)
76 (void)snprintf(full_tty
, sizeof(full_tty
),
77 "%s%s", _PATH_DEV
, request
->r_tty
);
78 if (stat(full_tty
, &stbuf
) < 0 || (stbuf
.st_mode
&S_IWGRP
) == 0)
79 return (PERMISSION_DENIED
);
80 return (print_mesg(request
->r_tty
, request
, remote_machine
));
83 #define max(a, b) ((a) > (b) ? (a) : (b))
88 * Build a block of characters containing the message.
89 * It is sent blank filled and in a single block to
90 * try to keep the message in one piece if the recipient
91 * is in vi at the time.
94 print_mesg(const char *tty
, CTL_MSG
*request
, const char *remote_machine
)
96 struct timeval clocktv
;
98 struct tm
*localclock
;
100 char line_buf
[N_LINES
][N_CHARS
];
102 char big_buf
[(N_LINES
+ 1) * N_CHARS
];
103 char *bptr
, *lptr
, vis_user
[sizeof(request
->l_name
) * 4];
108 (void)gettimeofday(&clocktv
, NULL
);
109 clocktime
= clocktv
.tv_sec
;
110 localclock
= localtime(&clocktime
);
111 (void)snprintf(line_buf
[i
], N_CHARS
, " ");
112 sizes
[i
] = strlen(line_buf
[i
]);
113 max_size
= max(max_size
, sizes
[i
]);
116 (void)snprintf(line_buf
[i
], N_CHARS
,
117 "Message from Talk_Daemon@%s at %d:%02d ...",
118 hostname
, localclock
->tm_hour
, localclock
->tm_min
);
119 sizes
[i
] = strlen(line_buf
[i
]);
120 max_size
= max(max_size
, sizes
[i
]);
122 if (strlen(request
->l_name
) + 1 > sizeof(vis_user
) / 4)
124 strvis(vis_user
, request
->l_name
, VIS_CSTYLE
);
125 (void)snprintf(line_buf
[i
], N_CHARS
,
126 "talk: connection requested by %s@%s.", vis_user
, remote_machine
);
127 sizes
[i
] = strlen(line_buf
[i
]);
128 max_size
= max(max_size
, sizes
[i
]);
130 (void)snprintf(line_buf
[i
], N_CHARS
,
131 "talk: respond with: talk %s@%s", vis_user
, remote_machine
);
132 sizes
[i
] = strlen(line_buf
[i
]);
133 max_size
= max(max_size
, sizes
[i
]);
135 (void)snprintf(line_buf
[i
], N_CHARS
, " ");
136 sizes
[i
] = strlen(line_buf
[i
]);
137 max_size
= max(max_size
, sizes
[i
]);
140 *bptr
++ = '\a'; /* send something to wake them up */
141 *bptr
++ = '\r'; /* add a \r in case of raw mode */
143 for (i
= 0; i
< N_LINES
; i
++) {
144 /* copy the line into the big buffer */
146 while (*lptr
!= '\0' && lptr
< (line_buf
[i
] + N_CHARS
))
147 *(bptr
++) = *(lptr
++);
148 /* pad out the rest of the lines with blanks */
149 for (j
= sizes
[i
]; j
< max_size
+ 2; j
++)
151 *(bptr
++) = '\r'; /* add a \r in case of raw mode */
155 iovec
.iov_base
= big_buf
;
156 iovec
.iov_len
= bptr
- big_buf
;
158 * we choose a timeout of RING_WAIT-5 seconds so that we don't
159 * stack up processes trying to write messages to a tty
160 * that is permanently blocked.
162 if (ttymsg(&iovec
, 1, tty
, RING_WAIT
- 5) != NULL
)