#include <sys/stat.h>
#include <protocols/talkd.h>
#include <limits.h>
#include <paths.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
#include "talkd.h"
static void print_mesg(FILE *,CTL_MSG *,char *);
int
announce(CTL_MSG *request, char *remote_machine)
{
char full_tty[PATH_MAX];
FILE *tf;
struct stat stbuf;
(void)snprintf(full_tty, sizeof(full_tty), "%s/%s", _PATH_DEV,
request->r_tty);
if (access(full_tty, 0) != 0)
return (FAILED);
if ((tf = fopen(full_tty, "w")) == NULL)
return (PERMISSION_DENIED);
if (fstat(fileno(tf), &stbuf) == -1) {
fclose(tf);
return (PERMISSION_DENIED);
}
if ((stbuf.st_mode & S_IWGRP) == 0) {
fclose(tf);
return (PERMISSION_DENIED);
}
print_mesg(tf, request, remote_machine);
fclose(tf);
return (SUCCESS);
}
#define max(a,b) ( (a) > (b) ? (a) : (b) )
#define N_LINES 5
#define N_CHARS 120
static void
print_mesg(FILE *tf, CTL_MSG *request, char *remote_machine)
{
time_t clocktime;
struct tm *localclock;
char line_buf[N_LINES][N_CHARS];
int sizes[N_LINES];
char big_buf[(N_LINES + 1) * N_CHARS];
char *bptr, *lptr, vis_user[sizeof(request->l_name) * 4];
int i, j, max_size;
i = 0;
max_size = 0;
time(&clocktime);
localclock = localtime(&clocktime);
(void)snprintf(line_buf[i], N_CHARS, " ");
sizes[i] = strlen(line_buf[i]);
max_size = max(max_size, sizes[i]);
i++;
if (localclock) {
(void)snprintf(line_buf[i], N_CHARS,
"Message from Talk_Daemon@%s at %d:%02d ...",
hostname, localclock->tm_hour , localclock->tm_min );
} else {
(void)snprintf(line_buf[i], N_CHARS,
"Message from Talk_Daemon@%s ...", hostname);
}
sizes[i] = strlen(line_buf[i]);
max_size = max(max_size, sizes[i]);
i++;
strvis(vis_user, request->l_name, VIS_CSTYLE);
(void)snprintf(line_buf[i], N_CHARS,
"talk: connection requested by %s@%s.",
vis_user, remote_machine);
sizes[i] = strlen(line_buf[i]);
max_size = max(max_size, sizes[i]);
i++;
(void)snprintf(line_buf[i], N_CHARS, "talk: respond with: talk %s@%s",
vis_user, remote_machine);
sizes[i] = strlen(line_buf[i]);
max_size = max(max_size, sizes[i]);
i++;
(void)snprintf(line_buf[i], N_CHARS, " ");
sizes[i] = strlen(line_buf[i]);
max_size = max(max_size, sizes[i]);
i++;
bptr = big_buf;
*bptr++ = '\007';
*bptr++ = '\r';
*bptr++ = '\n';
for (i = 0; i < N_LINES; i++) {
lptr = line_buf[i];
while (*lptr != '\0')
*(bptr++) = *(lptr++);
for (j = sizes[i]; j < max_size + 2; j++)
*(bptr++) = ' ';
*(bptr++) = '\r';
*(bptr++) = '\n';
}
*bptr = '\0';
fprintf(tf, "%s", big_buf);
fflush(tf);
}