home *** CD-ROM | disk | FTP | other *** search
- /*
-
- chatter
- server process for multi-user chat
- Written by D'Arcy J.M. Cain
- West Hill, Ontario
- darcy@druid.UUCP
- All rights reserved
-
- This is the server for the chat program. It handles various
- requests and sends messages to the users of chat.
-
- Permission is hereby granted to freely copy and redistribute this
- software, provided that the author is clearly credited in all
- copies and derivations. This software is provided ``As Is'' and
- without any express or implied warranties.
-
- */
-
- #include <stdio.h>
- #include <signal.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <ctype.h>
- #include <errno.h>
- #include "chat.h"
-
- char chatter_rcsid[] = "$Id: chatter.c,v 1.2 90/10/26 19:55:20 darcy Exp Locker: darcy $\b ";
-
- extern char chs_rcsid[];
-
- #define MAX_CHATTERS 32
- CHATTER u[MAX_CHATTERS];
-
- char progname[64];
-
- void cleanup(int sig) /* exit through here always */
- {
- ch_close();
- fprintf(stderr, "\nchatter exiting\n");
- exit(sig);
- }
-
- /* open message queue */
- void init(void)
- {
- char *p;
-
- if ((p = ch_init()) != NULL)
- {
- fprintf(stderr, "%s: %s\n", progname, p);
- exit(1);
- }
- }
-
- void main(int argc, char **argv)
- {
- int k, u_id;
- char ch, msgbuf[512], message[256];
-
- strcpy(progname, argv[0]);
-
- /* all trap everything to go through cleanup */
- signal(SIGINT, cleanup);
- signal(SIGABRT, cleanup);
- signal(SIGKILL, cleanup);
- signal(SIGTERM, cleanup);
-
- init();
-
- /* reset all channels */
- for (k = 0; k < MAX_CHATTERS; k++)
- u[k].channel = 0;
-
- for (;;) /* till the stars fall from the sky */
- {
- while ((u_id = ch_get(msgbuf)) == -1)
- ;
-
- if (u[u_id].channel == 0)
- u[u_id].channel = '0';
-
- ch = u[u_id].channel;
-
- switch (*msgbuf)
- {
- case 'o': /* open a chat channel */
- strcpy(u[u_id].logname, msgbuf + 1);
- sprintf(msgbuf, "[%-12.12s] Joining Chat", u[u_id].logname);
-
- /* advise the chat world of newcomer */
- for (k = 0; k < MAX_CHATTERS; k++)
- if (u[k].channel)
- ch_send(msgbuf, k);
-
- break;
-
-
- case 'x': /* user exiting from chat */
- sprintf(msgbuf, "[%-12.12s] Leaving Chat", u[u_id].logname);
-
- /* check each user */
- for (k = 0; k < MAX_CHATTERS; k++)
- {
- if (k == u_id) /* this user */
- u[k].channel = 0;
- else if (u[k].channel) /* other user */
- if (u[k].channel == ch) /* same channel */
- ch_send(msgbuf, k);
- }
-
- ch_send(NULL, u_id); /* remove user from list */
- break;
-
- case 'c': /* change channel */
- if (isascii(msgbuf[1])) /* only allow ASCII */
- {
- if (msgbuf[1] == '*')
- {
- int c = '0';
-
- while (isascii(c))
- {
- if (c == '*')
- continue;
-
- for (k = 0; k < MAX_CHATTERS; k++)
- if (u[k].channel == c)
- k = MAX_CHATTERS + 1;
-
- if (k == MAX_CHATTERS)
- {
- u[u_id].channel = c;
- sprintf(msgbuf,
- "[%-12.12s] switched to channel %c",
- u[u_id].logname, c);
-
- for (k = 0; k < MAX_CHATTERS; k++)
- if (u[k].channel)
- ch_send(msgbuf, k);
-
- break;
- }
-
- c++;
- }
-
- break;
- }
-
- u[u_id].channel = msgbuf[1];
- sprintf(msgbuf, "[%-12.12s] switched to channel %c",
- u[u_id].logname, u[u_id].channel);
-
- for (k = 0; k < MAX_CHATTERS; k++)
- if (u[k].channel)
- ch_send(msgbuf, k);
- }
-
- break;
-
- case 's': /* show users */
- for (k = 0; k < MAX_CHATTERS; k++)
- {
- if (u[k].channel)
- {
- sprintf(msgbuf, "[%-12.12s] is on channel %c",
- u[k].logname, u[k].channel);
- ch_send(msgbuf, u_id);
- }
- }
-
- break;
-
- case 'n': /* change user's display name */
- if (msgbuf[1])
- strcpy(u[u_id].logname, msgbuf + 1);
- break;
-
- case 'm': /* send a message */
- msgbuf[64] = 0;
- sprintf(message, "[%-12.12s] %s",
- u[u_id].logname, msgbuf + 1);
-
- for (k = 0; k < MAX_CHATTERS; k++)
- if (u[k].channel == ch)
- ch_send(message, k);
-
- break;
-
- case 'v': /* send a message */
- ch_send(chatter_rcsid + 1, u_id);
- ch_send(chs_rcsid + 1, u_id);
- break;
- }
- }
- }
-