home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / conf / part03 / confrw.c < prev   
Encoding:
C/C++ Source or Header  |  1988-03-13  |  8.3 KB  |  349 lines

  1. /*
  2.  *    conf - An interactive multi-user chat program.
  3.  *
  4.  *    conf Copyright (c) 1986, 1987 by Keith Gabryelski
  5.  *
  6.  *    Conf is quasi-public domain software; it may be used and copied
  7.  *    freely and may be modified to suit the indivuals needs as long
  8.  *    as:
  9.  *
  10.  *    [1] It is not sold.
  11.  *    [2] It is not made part of any licensed product.
  12.  *    [3] This header and others like it are not modified or removed.
  13.  *    [4] You allow others to use and copy without charge.
  14.  *    
  15.  *    without expressed written permission from the original
  16.  *    author (Keith Gabryelski).
  17.  *
  18.  *
  19.  */
  20.  
  21. #include "conf.h"
  22.  
  23. read_log()
  24. {
  25.     char *ptr, *p;
  26.     char rduser[MAXNAMELEN], rdtty[MAXTTYLEN];
  27.     int tous, toline;
  28.     unsigned meslen;
  29.  
  30.     while (read(log_rfd, (char *)&tlog, (unsigned)sizeof(struct clogfil)) ==
  31.     sizeof(struct clogfil))
  32.     {
  33.     meslen = (tlog.messlen + 7)&~7;   /* divisable by 8 */
  34.  
  35.     switch(tlog.type)
  36.     {
  37.         case NORMAL:
  38.         if (tlog.f_line == cuser.cu_line)
  39.         {
  40.             if (meslen > wdlen)
  41.             wrdata = myrealloc(wrdata, wdlen = meslen);
  42.  
  43.             read(log_rfd, rduser, tlog.f_usrlen);
  44.             read(log_rfd, rdtty, tlog.f_ttylen);
  45.             read(log_rfd, wrdata, meslen);
  46.  
  47.             if (!seeme && !strcmp(rdtty, cuser.cu_tty))
  48.             break;
  49.  
  50.             setkey(curblock);
  51.             enccpy(wrdata, wrdata, meslen/MAXPASSWORDLEN, 1);
  52.  
  53.             if (!cmpdat(tlog.password, curpass, MAXPASSWORDLEN))
  54.             {
  55.             if (warncrypt)
  56.             /* some one could fake this, BFD, but, maybe
  57.                 take care of (someday) */
  58.                 (void)strcpy(wrdata, "[Crypted Message]");
  59.             else
  60.                 break;
  61.             }
  62.  
  63.             if (beep)
  64.             (void)putchar(BELL);
  65.  
  66.             printmess(stdout, normform, rduser, rdtty,
  67.                 wrdata, tlog.messlen);
  68.  
  69.             if (cuser.cu_flags&USER_RECORD)
  70.             printmess(rec_fp, normform, rduser, rdtty,
  71.                 wrdata, tlog.messlen);
  72.         }
  73.         else
  74.             (void)lseek(log_rfd, (long)(tlog.f_usrlen + tlog.f_ttylen +
  75.             meslen), 1);
  76.         break;
  77.  
  78.         case SEND:
  79.         read(log_rfd, rduser, tlog.f_usrlen);
  80.         read(log_rfd, rdtty, tlog.f_ttylen);
  81.  
  82.         ptr = mymalloc(tlog.t_utlen);
  83.         read(log_rfd, ptr, tlog.t_utlen);
  84.         read(log_rfd, wrdata, meslen);
  85.  
  86.         p = ptr;
  87.         toline = tous = FALSE;
  88.         do
  89.         {
  90.             switch(*p)
  91.             {
  92.             case '/':
  93.                 if (atoi(p+1) == cuser.cu_line)
  94.                 toline = TRUE;
  95.                 break;
  96.  
  97.             case ':':
  98.                 if (!strcmp(p+1, cuser.cu_tty))
  99.                 tous = TRUE;
  100.                 break;
  101.  
  102.             default:
  103.                 if (!strcmp(p, cuser.cu_cname))
  104.                 tous = TRUE;
  105.                 break;
  106.             }
  107.             
  108.             while (*p++) ;  /* to next name */
  109.         } while (*p);       /* while another name exists */
  110.  
  111.         free(ptr);
  112.  
  113.         if (tous)
  114.         {
  115.             setkey(pubblock);
  116.             enccpy(wrdata, wrdata, meslen/MAXPASSWORDLEN, 1);
  117.  
  118.             if (beep)
  119.             (void)putchar(BELL);
  120.  
  121.             printmess(stdout, sendform, rduser, rdtty,
  122.             wrdata, tlog.messlen);
  123.  
  124.             if (cuser.cu_flags&USER_RECORD)
  125.             printmess(rec_fp, sendform, rduser, rdtty,
  126.                 wrdata, tlog.messlen);
  127.  
  128.             (void)strcpy(replytty, rdtty);
  129.             replytty[strlen(replytty)] = '\0'; /* extra nul 4 send*/
  130.             (void)strcpy(replyname, rduser);
  131.         }
  132.  
  133.         if (toline)
  134.         {
  135.             setkey(pubblock);
  136.             enccpy(wrdata, wrdata, meslen/MAXPASSWORDLEN, 1);
  137.  
  138.             if (beep)
  139.             (void)putchar(BELL);
  140.  
  141.             printmess(stdout, lineform, rduser, rdtty,
  142.                 wrdata, tlog.messlen);
  143.  
  144.             if (cuser.cu_flags&USER_RECORD)
  145.             printmess(rec_fp, lineform, rduser, rdtty,
  146.                 wrdata, tlog.messlen);
  147.         }
  148.         break;
  149.  
  150.         case SHOUT:
  151.         read(log_rfd, rduser, tlog.f_usrlen);
  152.         read(log_rfd, rdtty, tlog.f_ttylen);
  153.         read(log_rfd, wrdata, tlog.messlen);
  154.  
  155.         if (beep)
  156.             (void)putchar(BELL);
  157.  
  158.         printmess(stdout, shoutform, rduser, rdtty, wrdata,
  159.             tlog.messlen);
  160.  
  161.         if (cuser.cu_flags&USER_RECORD)
  162.             printmess(rec_fp, shoutform, rduser, rdtty,
  163.                 wrdata, tlog.messlen);
  164.         break;
  165.  
  166.         case INFORM:
  167.         if (tlog.f_line == cuser.cu_line)
  168.         {
  169.             if (tlog.messlen > wdlen)
  170.             wrdata = myrealloc(wrdata, wdlen = tlog.messlen);
  171.  
  172.             read(log_rfd, rduser, tlog.f_usrlen);
  173.             read(log_rfd, rdtty, tlog.f_ttylen);
  174.             read(log_rfd, wrdata, tlog.messlen);
  175.  
  176.             if (!informe && !strcmp(rdtty, cuser.cu_tty))
  177.             break;
  178.  
  179.             if (beep)
  180.             (void)putchar(BELL);
  181.  
  182.             printmess(stdout, informform, rduser, rdtty,
  183.                 wrdata, tlog.messlen);
  184.  
  185.             if (cuser.cu_flags&USER_RECORD)
  186.             printmess(rec_fp, informform, rduser,
  187.                 rdtty,wrdata, tlog.messlen);
  188.         }
  189.         else
  190.         {
  191.             (void)lseek(log_rfd, (long)(tlog.f_usrlen + tlog.f_ttylen +
  192.             tlog.messlen), 1);
  193.  
  194. #ifdef DEBUG1
  195.             (void)puts("flushin' this inform...");
  196.             (void)printf("f_line = %d, cu_line = %d.\n", 
  197.             tlog.f_line, cuser.cu_line);
  198. #endif DEBUG1
  199.         }
  200.         break;
  201.  
  202.         default:
  203.         /* ignore invalid type lseek to end */
  204. #ifdef    DEBUG0
  205.         (void)printf("invalid type (0x%x)\n",tlog.type);
  206. #endif    DEBUG0
  207.         (void)lseek(log_rfd, 0L, 2);
  208.         break;
  209.     }
  210.     }
  211. }
  212.  
  213. write_log(type, message, user, userlen, siz)
  214. int type;
  215. unsigned int userlen, siz;
  216. char *message, *user;
  217. {
  218.     static char *wrbuf;
  219.     static unsigned wblen=0;
  220.     unsigned wrlen, meslen;
  221.  
  222.     clog.type = type;
  223.  
  224.     clog.messlen = siz;
  225.     meslen = (siz + 7)&~7;   /* divisable by 8 */
  226.  
  227.     clog.t_utlen = userlen;
  228.  
  229.     switch(type)
  230.     {
  231.     case NORMAL:
  232.         wrlen = sizeof(struct clogfil) + clog.f_usrlen + clog.f_ttylen +
  233.             meslen;
  234.  
  235.         if (wrlen > wblen)
  236.         wrbuf = myrealloc(wrbuf, wblen = wrlen);
  237.  
  238.         /* move data into wrbuf */
  239.         setkey(curblock);
  240.         cpystr(clog.password, curpass, MAXPASSWORDLEN);
  241.  
  242.         cpystr(wrbuf, (char *)&clog, (unsigned)sizeof(struct clogfil));
  243.         (void)strcpy(wrbuf+sizeof(struct clogfil), cuser.cu_cname);
  244.         (void)strcpy(wrbuf+sizeof(struct clogfil)+clog.f_usrlen, cuser.cu_tty);
  245.         enccpy(wrbuf+sizeof(struct clogfil)+clog.f_usrlen+clog.f_ttylen,
  246.         message, meslen/MAXPASSWORDLEN, 0);
  247.  
  248.         write(log_wfd, wrbuf, wrlen);
  249.         break;
  250.  
  251.     case SEND:
  252.         /* must encrypt the buffer or zero out the last 8 bytes before
  253.         it copies the data into it. --kmg */
  254.  
  255.         wrlen = sizeof(struct clogfil) + clog.f_usrlen + clog.f_ttylen +
  256.         clog.t_utlen + meslen;
  257.  
  258.         if (wrlen > wblen)
  259.         wrbuf = myrealloc(wrbuf, wblen = wrlen);
  260.  
  261.         /* move data into wrbuf */
  262.         setkey(pubblock);
  263.         cpystr(clog.password, pubpass, (unsigned)MAXPASSWORDLEN);
  264.  
  265.         cpystr(wrbuf, (char *)&clog, sizeof(struct clogfil));
  266.         (void)strcpy(wrbuf+sizeof(struct clogfil), cuser.cu_cname);
  267.         (void)strcpy(wrbuf+sizeof(struct clogfil)+clog.f_usrlen, cuser.cu_tty);
  268.         cpystr(wrbuf+sizeof(struct clogfil)+clog.f_usrlen+clog.f_ttylen,
  269.          user, clog.t_utlen);
  270.         enccpy(wrbuf + sizeof(struct clogfil) + clog.f_usrlen +
  271.         clog.f_ttylen + clog.t_utlen, message, meslen/MAXPASSWORDLEN, 0);
  272.  
  273.         write(log_wfd, wrbuf, wrlen);
  274.         break;
  275.  
  276.     case SHOUT:
  277.         /* must encrypt the buffer or zero out the last 8 bytes before
  278.         it copies the data into it. --kmg */
  279.  
  280.         wrlen = sizeof(struct clogfil) + clog.f_usrlen + clog.f_ttylen +
  281.             clog.messlen;
  282.  
  283.         if (wrlen > wblen)
  284.         wrbuf = myrealloc(wrbuf, wblen = wrlen);
  285.  
  286.         /* move data into wrbuf */
  287.  
  288.         cpystr(wrbuf, (char *)&clog, sizeof(struct clogfil));
  289.         (void)strcpy(wrbuf+sizeof(struct clogfil), cuser.cu_cname);
  290.         (void)strcpy(wrbuf+sizeof(struct clogfil)+clog.f_usrlen, cuser.cu_tty);
  291.         cpystr(wrbuf+sizeof(struct clogfil)+clog.f_usrlen+clog.f_ttylen,
  292.         message, clog.messlen);
  293.  
  294.         write(log_wfd, wrbuf, wrlen);
  295.         break;
  296.  
  297.     case INFORM:
  298.         /* must encrypt the buffer or zero out the last 8 bytes before
  299.         it copies the data into it. --kmg */
  300.  
  301.         wrlen = sizeof(struct clogfil) + clog.f_usrlen + clog.f_ttylen +
  302.             clog.messlen;
  303.  
  304.         if (wrlen > wblen)
  305.         wrbuf = myrealloc(wrbuf, wblen = wrlen);
  306.  
  307.         /* move data into wrbuf */
  308.  
  309.         cpystr(wrbuf, (char *)&clog, (unsigned)sizeof(struct clogfil));
  310.         (void)strcpy(wrbuf+sizeof(struct clogfil), cuser.cu_cname);
  311.         (void)strcpy(wrbuf+sizeof(struct clogfil)+clog.f_usrlen, cuser.cu_tty);
  312.         cpystr(wrbuf+sizeof(struct clogfil)+clog.f_usrlen+clog.f_ttylen,
  313.         message, clog.messlen);
  314.  
  315.         write(log_wfd, wrbuf, wrlen);
  316.         break;
  317.  
  318. #ifdef    DEBUG0
  319.     default:
  320.         (void)printf("warning .. Bogus type, no write (type = %d)\n", type);
  321.         break;
  322. #endif    DEBUG0
  323.     }
  324. }
  325.  
  326. write_usr()
  327. {
  328.     (void)lseek(usr_fd, ourplace, 0);
  329.  
  330. #ifdef    SYSV
  331.     lockf(usr_fd, F_LOCK, (long)sizeof(struct cusrfil));
  332. #endif    SYSV
  333.  
  334. #ifdef    BSD
  335.     flock(usr_fd, LOCK_EX);
  336. #endif    BSD
  337.  
  338.     write(usr_fd, (char *)&cuser, sizeof(struct cusrfil));
  339.  
  340. #ifdef    SYSV
  341.     (void)lseek(usr_fd, ourplace, 0);
  342.     lockf(usr_fd, F_ULOCK, (long)sizeof(struct cusrfil));
  343. #endif    SYSV
  344.  
  345. #ifdef    BSD
  346.     flock(usr_fd, LOCK_UN);
  347. #endif    BSD
  348. }
  349.