Category : UNIX Files
Archive   : CHAT17.ZIP
Filename : CLIENT.C

 
Output of file : CLIENT.C contained in archive : CHAT17.ZIP
/* Client party program */

#include "chat.h"
#include "decl.h"

char filen[132];
int echo = 0;
struct termio oldbuf, tbuf;

getline(line)
char *line;

{

int ch;
int x, y;
int flags;
int index = 1;
static char line2[80] = {0};
char tmpx[80], tmpy[80];

if (strlen(line2) != 0) {
printf("> %s", line2);
fflush(stdout);
strcpy(line, line2);
index = strlen(line2);
line2[0] = 0;
goto readmore;
}
alarm(1);
if ((ch = getchar()) == EOF) {
alarm(0);
return(0);
}
if ((ch == 24) || (ch == 21)) {
puts("\n[Cancelled]");
alarm(0);
return(0);
}
if ((ch == 13) || (ch == 10)) return(0);
if ((ch < 32) || (ch > 126)) return(0);
line2[0] = 0;
line[0] = ch;
if (echo == 0) {
printf("> %c",ch);
fflush(stdout);
}

readmore:;

alarm(30); /* 30 seconds on the line */
x = 0;
while (index < 68) { /* We get up to 68 chars */
if (index != 0) x = -1; else x = 0;
ch = getchar(); /* Get one */
if (ch == EOF) { /* If EOF.... */
line[index] = 0;
puts("");
x = 0;
break;
}
if ((ch == 24) || (ch == 21)) { /* Cancel request... */
puts("\n[Cancelled]");
alarm(0);
return(0);
}
if ((ch == 13) || (ch == 10)) { /* End of line */
line[index] = 0;
if (echo == 0) puts("");
alarm(0);
if (index == 0) return(0);
return(-1);
}
if (ch == 8) { /* Backspace */
if (index > 0) { /* Do if anything here */
if (echo == 0) {
putchar(ch);
putchar(32);
putchar(ch);
fflush(stdout);
}
index--;
}
continue;
}
if ((ch < 32) || (ch > 126)) continue; /* Do nothing */
line[index++] = ch; /* Add the character */
if (echo == 0) {
putchar(ch);
fflush(stdout);
}
} /* Falling off end... */
x = -1; /* Init return code */
if (index == 68) { /* Look for break location */
index--;
y = line[index--]; /* Get last character */
while ((index > 0) && (y != ' ')) { /* Find last space */
if (echo == 0) { /* Do we echo?? */
putchar(8);
putchar(32); /* Cover thy spaces! */
putchar(8);
}
y = line[index--]; /* Check next back */
}
if (index == 0) return(-1); /* No break, send it */
index++; /* Break at next */
line[index] = 0; /* Store break */
ch = line[1];
if (ch >= 'a') ch = tolower(ch); /* Make lower case */
if ((line[0] == '/') && ((ch == 'p') || (ch == 't'))) {
sprintf(tmpx, "%s", &line[2]);
sscanf(tmpx, " %[!-~]", tmpy); /* Get name */
strncpy(line2, line, 2);
line2[2] = 0;
strcat(line2, tmpy); /* Add name */
strcat(line2, " ");
strcat(line2, &line[index + 1]);
} else
strcpy(line2, &line[index + 1]);/* Copy rest.. */
x = strlen(line2); /* Get length */
if (x == 0) x = -1; /* Return length */
if (echo == 0) puts(""); /* New line */
}
alarm(0); /* No alarms please */
return(x); /* Exit w/status */
}

/* And here we find a variety of signal catching routines.

catch() exists only to satisfy the alarm call requirements.

The rest are exits of various kinds, some forced by external
circumstances, others requested by either the daemon or the user.

*/

catch()
{
signal(SIGALRM, catch);
return;
}

servdied()
{
puts("[Communications Server disconnect]");
die1();
}

samename()
{
puts("Only one chat per user id allowed!");
die1();
}

toss()
{
puts("You have been blown out of chat by another user!");
die1();
}

full()
{
puts("Chat is full, please try later");
die1();
}

die1()
{
puts("Bye-bye");
unlink(filen);
ioctl(0,TCSETAW,&oldbuf);
exit(0);
}

/* This is it... not many comments, but then again this is
pretty simple code... Shouldn't be hard to understand..

*/

main()
{
char temp[132];
char line[132], line2[132];
char victim[20], msg[80];
int out;
int cp;
FILE *ttynq;
int fid, wr;
char key;
int x;
struct passwd *psw;
struct utmp *ptr, *user, *getutent();
char name[20];
char cmd[5];

umask(0117);
psw = getpwuid(getuid());
strcpy(name, psw->pw_name);
printf("Welcome to Chat V1.7, %s\n", name);
puts("Type /h for help");
puts("");
strcpy(cmd,"w"); /* Base command */
signal(SIGALRM,catch);
signal(SIGQUIT, toss);
signal(SIGINT, die1); /* Catch signals */
signal(SIGUSR1, full);
signal(SIGUSR2, samename);
signal(SIGPIPE, servdied);
out = 0;
strcpy(line, CHATDIR);
strcat(line, "pipe");
strcpy(temp, "mknod "); /* Make me a pipe! */
strcat(temp, line);
strcat(temp, itoa(getpid()));
strcat(temp, " p");
if (system(temp) != 0) {
puts("Pipe failure!");
exit();
}
ioctl(0,TCGETA,&tbuf);
ioctl(0,TCGETA,&oldbuf);
tbuf.c_lflag &= ~(ICANON|ECHO|ISIG);
tbuf.c_cc[4] = 1; /* CBREAK mode */
tbuf.c_cc[5] = 1;
ioctl(0,TCSETAW,&tbuf);
strcpy(temp, CHATDIR);
strcat(temp, "pipe");
strcat(temp, itoa(getpid()));
strcpy(filen, temp);
if ((wr = open(line, O_WRONLY|O_NDELAY)) == -1) {
puts("I can't find the Communications Server... Please try later.");
ioctl(0,TCSETAW,&oldbuf);
exit(1);
}
sprintf(line, "%d o %s >%s\n", getpid(), name, name);
write(wr, line, LINESIZ); /* Write one buffer. */
alarm(10);
fid = 0; /* Make pipe id impossible */
fid = open(temp, O_RDONLY); /* Open pipe for reading */
if (fid <= 0) {
puts("The server seems to have died...");
ioctl(0,TCSETAW,&oldbuf); /* Leave if no server */
exit(1);
}
setgid(getgid()); /* Turn off permissions */
setuid(getuid()); /* We have the files open */
alarm(0); /* Cancel outstanding alarm */
sprintf(temp, "%d %s %s >%s\n",getpid(), "s", name, "test");
write(wr, temp, LINESIZ);
while (out == 0) { /* While still scanning */
alarm(1); /* Interrupt in one second */
while (read(fid, temp, LINESIZ) == LINESIZ) {
stripj(temp); /* Read/print while available */
puts(temp); /* Get next line */
alarm(1); /* Reset alarm */
}
if (getline(line) != 0) { /* Did we get user input? */
if (*line == '!') { /* Shell escape */
ioctl(0,TCSETAW,&oldbuf);
alarm(0); /* Turn off alarm(s) */
system(&line[1]); /* Protected below... */
while ((wait() != -1) || (errno != ECHILD));;
ioctl(0,TCSETAW,&tbuf);
puts("!");
continue;
}
if (*line == '/') { /* Command request... */
switch(line[1]) {
case '?':
case 'h': /* Help */
case 'H':
puts("System V Chat V1.7");
puts("Copyright 1987 MCS/Karl Denninger");
puts("Not-for-profit distribution authorized\n");
puts("!cmd - Execute Unix command");
puts("/? - Same as (help)");
puts("/e - Echo (toggle)");
puts("/h - Print this help");
puts("/k - Kill a chatting user");
puts("/n - Set aliased name");
puts("/p - Send a private msg");
puts("/q - Quit chatting");
puts("/s - Show system and chat users");
puts("/t - Tell a user something");
puts("/w - Equivalent to /s");
puts("\nControl chars: CTRL-X = cancel line");
puts(" CTRL-H = backspace\n");
puts("To enter a response just type\n");
break;
case 'n':
case 'N':
if (sscanf(&line[2], " %[!-~ ]", victim) == 1) {
sprintf(temp, "%d n %s >%s\n", getpid(), name, victim);
write(wr, temp, LINESIZ);
} else puts("Requires alias");
break;
case 'p': /* Private message */
case 'P':
if (sscanf(&line[2], " %s %[!-~ ]", victim, msg) == 2) {
sprintf(temp, "%d %s %s >%s\n",getpid(), "p", victim, msg);
write(wr, temp, LINESIZ);
} else puts("Requires user and message");
break;
case 'q': /* Exit from CHAT */
case 'Q':
sprintf(temp, "%d q %s >%s\n",getpid(), name, "[Departed from chat]");
write(wr, temp, LINESIZ);
ioctl(0,TCSETAW,&oldbuf);
die1();
break;
case 'e': /* Echo toggle */
case 'E':
if (echo == 1) {
puts("Echo on");
echo = 0;
} else {
echo = 1;
puts("Echo off");
}
break;
case 't': /* Tell someone */
case 'T':
if (sscanf(&line[2], " %s %[!-~ ]",victim, msg) < 2) {
puts("Requires victim and message");
break;
}
x = 0;
setutent(); /* Find user */
while ((ptr = getutent()) != NULL) {
if (strncmp(ptr->ut_user, victim, (strlen (victim) -1)) == 0) {
user = ptr;
x = 1;
break;
}
}
if ( !x) {
printf ("User is not logged on\n");
break;
}
strcpy (temp, "/dev/");
strcat (temp, user->ut_line);
alarm(10);
if ((ttynq = fopen (temp, "w")) == NULL) {
printf ("User %s has disabled writes\n", user->ut_user);
} else {
fclose (ttynq);
printf ("Sending to %s: %s\n", victim, msg);
fflush (stdout);
strcpy (temp, "echo \"\n;;From ");
strcat (temp, name);
strcat (temp, ": ");
strcat (temp, msg);
strcat (temp, " \" >/dev/");
strcat (temp, user->ut_line);
alarm(10);
system (temp);
}
alarm(0);
break;
case 'k': /* Request kill */
case 'K':
sprintf(temp, "%d %s %s >%s\n",getpid(), "k", &line[2], "test");
write(wr, temp, LINESIZ);
break;
case 'w': /* Show who is here */
case 's':
case 'W':
case 'S':
system("who");
puts("");
sprintf(temp, "%d %s %s >%s\n",getpid(), "s", name, "test");
write(wr, temp, LINESIZ);
break;
default: /* Bad command */
puts("Unknown command, try '/h'");
}
} else { /* Otherwise send the line */
sprintf(temp, "%d %s %s >%s\n", getpid(), cmd, name, line);
write(wr, temp, LINESIZ);
}
}
}
}



  3 Responses to “Category : UNIX Files
Archive   : CHAT17.ZIP
Filename : CLIENT.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/