home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2002 / chatter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  4.0 KB  |  199 lines

  1. /*
  2.  
  3. chatter
  4. server process for multi-user chat
  5. Written by D'Arcy J.M. Cain
  6. West Hill, Ontario
  7. darcy@druid.UUCP
  8. All rights reserved
  9.  
  10. This is the server for the chat program.  It handles various
  11. requests and sends messages to the users of chat.
  12.  
  13. Permission is hereby granted to freely copy and redistribute this
  14. software, provided that the author is clearly credited in all
  15. copies and derivations.  This software is provided ``As Is'' and
  16. without any express or implied warranties.
  17.  
  18. */
  19.  
  20. #include    <stdio.h>
  21. #include    <signal.h>
  22. #include    <string.h>
  23. #include    <stdlib.h>
  24. #include    <sys/types.h>
  25. #include    <ctype.h>
  26. #include    <errno.h>
  27. #include    "chat.h"
  28.  
  29. char chatter_rcsid[] = "$Id: chatter.c,v 1.2 90/10/26 19:55:20 darcy Exp Locker: darcy $\b ";
  30.  
  31. extern char    chs_rcsid[];
  32.  
  33. #define        MAX_CHATTERS        32
  34. CHATTER        u[MAX_CHATTERS];
  35.  
  36. char    progname[64];
  37.  
  38. void    cleanup(int sig)    /* exit through here always */
  39. {
  40.     ch_close();
  41.     fprintf(stderr, "\nchatter exiting\n");
  42.     exit(sig);
  43. }
  44.  
  45. /* open message queue */
  46. void    init(void)
  47. {
  48.     char    *p;
  49.  
  50.     if ((p = ch_init()) != NULL)
  51.     {
  52.         fprintf(stderr, "%s: %s\n", progname, p);
  53.         exit(1);
  54.     }
  55. }
  56.  
  57. void    main(int argc, char **argv)
  58. {
  59.     int        k, u_id;
  60.     char    ch, msgbuf[512], message[256];
  61.  
  62.     strcpy(progname, argv[0]);
  63.  
  64.     /* all trap everything to go through cleanup */
  65.     signal(SIGINT, cleanup);
  66.     signal(SIGABRT, cleanup);
  67.     signal(SIGKILL, cleanup);
  68.     signal(SIGTERM, cleanup);
  69.  
  70.     init();
  71.  
  72.     /* reset all channels */
  73.     for (k = 0; k < MAX_CHATTERS; k++)
  74.         u[k].channel = 0;
  75.  
  76.     for (;;)        /* till the stars fall from the sky */
  77.     {
  78.         while ((u_id = ch_get(msgbuf)) == -1)
  79.             ;
  80.  
  81.         if (u[u_id].channel == 0)
  82.             u[u_id].channel = '0';
  83.  
  84.         ch = u[u_id].channel;
  85.  
  86.         switch (*msgbuf)
  87.         {
  88.             case 'o':    /* open a chat channel */
  89.                 strcpy(u[u_id].logname, msgbuf + 1);
  90.                 sprintf(msgbuf, "[%-12.12s] Joining Chat", u[u_id].logname);
  91.  
  92.                 /* advise the chat world of newcomer */
  93.                 for (k = 0; k < MAX_CHATTERS; k++)
  94.                     if (u[k].channel)
  95.                         ch_send(msgbuf, k);
  96.  
  97.                 break;
  98.  
  99.  
  100.             case 'x':        /* user exiting from chat */
  101.                 sprintf(msgbuf, "[%-12.12s] Leaving Chat", u[u_id].logname);
  102.  
  103.                 /* check each user */
  104.                 for (k = 0; k < MAX_CHATTERS; k++)
  105.                 {
  106.                     if (k == u_id)                /* this user */
  107.                         u[k].channel = 0;
  108.                     else if (u[k].channel)        /* other user */
  109.                         if (u[k].channel == ch)    /* same channel */
  110.                             ch_send(msgbuf, k);
  111.                 }
  112.  
  113.                 ch_send(NULL, u_id);            /* remove user from list */
  114.                 break;
  115.  
  116.             case 'c':            /* change channel */
  117.                 if (isascii(msgbuf[1]))            /* only allow ASCII */
  118.                 {
  119.                     if (msgbuf[1] == '*')
  120.                     {
  121.                         int c = '0';
  122.  
  123.                         while (isascii(c))
  124.                         {
  125.                             if (c == '*')
  126.                                 continue;
  127.  
  128.                             for (k = 0; k < MAX_CHATTERS; k++)
  129.                                 if (u[k].channel == c)
  130.                                     k = MAX_CHATTERS + 1;
  131.  
  132.                             if (k == MAX_CHATTERS)
  133.                             {
  134.                                 u[u_id].channel = c;
  135.                                 sprintf(msgbuf,
  136.                                         "[%-12.12s] switched to channel %c",
  137.                                         u[u_id].logname, c);
  138.  
  139.                                 for (k = 0; k < MAX_CHATTERS; k++)
  140.                                     if (u[k].channel)
  141.                                         ch_send(msgbuf, k);
  142.  
  143.                                 break;
  144.                             }
  145.  
  146.                             c++;
  147.                         }
  148.  
  149.                         break;
  150.                     }
  151.                             
  152.                     u[u_id].channel = msgbuf[1];
  153.                     sprintf(msgbuf, "[%-12.12s] switched to channel %c",
  154.                         u[u_id].logname, u[u_id].channel);
  155.  
  156.                     for (k = 0; k < MAX_CHATTERS; k++)
  157.                         if (u[k].channel)
  158.                             ch_send(msgbuf, k);
  159.                 }
  160.  
  161.                 break;
  162.  
  163.             case 's':            /* show users */
  164.                 for (k = 0; k < MAX_CHATTERS; k++)
  165.                 {
  166.                     if (u[k].channel)
  167.                     {
  168.                         sprintf(msgbuf, "[%-12.12s] is on channel %c",
  169.                                 u[k].logname, u[k].channel);
  170.                         ch_send(msgbuf, u_id);
  171.                     }
  172.                 }
  173.  
  174.                 break;
  175.  
  176.             case 'n':            /* change user's display name */
  177.                 if (msgbuf[1])
  178.                     strcpy(u[u_id].logname, msgbuf + 1);
  179.                 break;
  180.                 
  181.             case 'm':            /* send a message */
  182.                 msgbuf[64] = 0;
  183.                 sprintf(message, "[%-12.12s] %s",
  184.                         u[u_id].logname, msgbuf + 1);
  185.  
  186.                 for (k = 0; k < MAX_CHATTERS; k++)
  187.                     if (u[k].channel == ch)
  188.                         ch_send(message, k);
  189.  
  190.                 break;
  191.  
  192.             case 'v':            /* send a message */
  193.                 ch_send(chatter_rcsid + 1, u_id);
  194.                 ch_send(chs_rcsid + 1, u_id);
  195.                 break;
  196.         }
  197.     }
  198. }
  199.