home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / W / WWIVSOR.ZIP / SUBXTR.C < prev    next >
C/C++ Source or Header  |  1995-04-25  |  8KB  |  294 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1995 by Wayne Bell
  5.  
  6. Distribution of the source code for WWIV, in any form, modified or unmodified,
  7. without PRIOR, WRITTEN APPROVAL by the author, is expressly prohibited.
  8. Distribution of compiled versions of WWIV is limited to copies compiled BY
  9. THE AUTHOR.  Distribution of any copies of WWIV not compiled by the author
  10. is expressly prohibited.
  11.  
  12.  
  13. *****************************************************************************/
  14.  
  15. #pragma hdrstop
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <alloc.h>
  20. #include <stdlib.h>
  21. #include <fcntl.h>
  22. #include <io.h>
  23.  
  24.  
  25. #ifdef __OS2__
  26. #define far
  27. #define huge
  28. #endif /* __OS2__ */
  29.  
  30. #include "subxtr.h"
  31. #include "vardec.h"
  32. #include "net.h"
  33. #include "share.h"
  34.  
  35. extern configrec syscfg;
  36. extern int net_num_max;
  37. extern net_networks_rec *net_networks;
  38.  
  39. /****************************************************************************/
  40.  
  41. static char *mallocin_file(char *fn, long *len)
  42. {
  43.   char *ss;
  44.   int f;
  45.  
  46.   *len=0;
  47.   ss=NULL;
  48.  
  49.   f=sh_open1(fn, O_RDONLY|O_BINARY);
  50.   if (f>0) {
  51.     *len=filelength(f);
  52.     ss=(char *)bbsmalloc(*len+20);
  53.     if (ss) {
  54.       read(f,ss,*len);
  55.       ss[*len]=0;
  56.     }
  57.     sh_close(f);
  58.     return(ss);
  59.   }
  60.   return(ss);
  61. }
  62.  
  63. /****************************************************************************/
  64.  
  65. static char *skipspace(char *ss2)
  66. {
  67.   while ((*ss2) && ((*ss2!=' ') && (*ss2!='\t')))
  68.     ++ss2;
  69.   if (*ss2)
  70.     *ss2++=0;
  71.   while ((*ss2==' ') || (*ss2=='\t'))
  72.     ++ss2;
  73.   return(ss2);
  74. }
  75.  
  76.  
  77. /****************************************************************************/
  78.  
  79. static xtrasubsnetrec *xsubsn;
  80. static int nn;
  81.  
  82. static xtrasubsnetrec *fsub(int netnum, unsigned short type)
  83. {
  84.   int i;
  85.  
  86.   if (type) {
  87.     for (i=0; i<nn; i++) {
  88.       if ((xsubsn[i].net_num==netnum) && (xsubsn[i].type==type))
  89.         return(&(xsubsn[i]));
  90.     }
  91.   }
  92.   return(NULL);
  93. }
  94.  
  95. int read_subs_xtr(int max_subs, int num_subs, subboardrec *subboards)
  96. {
  97.   char *ss,*ss1,*ss2;
  98.   char huge *xx;
  99.   long l;
  100.   int n,i,curn;
  101.   char s[81];
  102.   xtrasubsnetrec *xnp;
  103.  
  104.  
  105.   if (xsubs) {
  106.     for (i=0; i<num_subs; i++) {
  107.       if ((xsubs[i].flags&XTRA_MALLOCED) && xsubs[i].nets)
  108.         bbsfree(xsubs[i].nets);
  109.     }
  110.     bbsfree((void *)xsubs);
  111.     if (xsubsn)
  112.       bbsfree(xsubsn);
  113.     xsubsn=NULL;
  114.     xsubs=NULL;
  115.   }
  116.  
  117.   l=((long)max_subs)*sizeof(xtrasubsrec);
  118.   xsubs=(xtrasubsrec huge *)bbsmalloc(l+1);
  119.   if (!xsubs) {
  120.     printf("Insufficient memory (%ld bytes) for SUBS.XTR\n", l);
  121.     return(1);
  122.   }
  123.  
  124.   xx=(char huge *)xsubs;
  125.   while (l>0) {
  126.     if (l>32768) {
  127.       memset((void *)xx,0,32768);
  128.       l-=32768;
  129.       xx += 32768;
  130.     } else {
  131.       memset((void *)xx,0,l);
  132.       break;
  133.     }
  134.   }
  135.  
  136.   sprintf(s,"%sSUBS.XTR",syscfg.datadir);
  137.   ss=mallocin_file(s,&l);
  138.   nn=0;
  139.   if (ss) {
  140.     for (ss1=strtok(ss,"\r\n"); ss1; ss1=strtok(NULL,"\r\n")) {
  141.       if (*ss1=='$')
  142.         ++nn;
  143.     }
  144.     bbsfree(ss);
  145.   } else {
  146.     for (i=0; i<num_subs; i++) {
  147.       if (subboards[i].type)
  148.         ++nn;
  149.     }
  150.   }
  151.   if (nn) {
  152.     l=((long)nn)*sizeof(xtrasubsnetrec);
  153.     xsubsn=(xtrasubsnetrec *)bbsmalloc(l);
  154.     if (!xsubsn) {
  155.       printf("Insufficient memory (%ld bytes) for net subs info\n", l);
  156.       return(1);
  157.     }
  158.     memset(xsubsn, 0, l);
  159.  
  160.     nn=0;
  161.     ss=mallocin_file(s,&l);
  162.     if (ss) {
  163.       curn=-1;
  164.       for (ss1=strtok(ss,"\r\n"); ss1; ss1=strtok(NULL,"\r\n")) {
  165.         switch(*ss1) {
  166.           case '!': /* sub idx */
  167.             curn=atoi(ss1+1);
  168.             if ((curn<0) || (curn>=num_subs))
  169.               curn=-1;
  170.             break;
  171.           case '@': /* desc */
  172.             if (curn>=0) {
  173.               strncpy(xsubs[curn].desc,ss1+1,60);
  174.             }
  175.             break;
  176.           case '#': /* flags */
  177.             if (curn>=0) {
  178.               xsubs[curn].flags=atol(ss1+1);
  179.             }
  180.             break;
  181.           case '$': /* net info */
  182.             if (curn>=0) {
  183.               if (!xsubs[curn].num_nets)
  184.                 xsubs[curn].nets=&(xsubsn[nn]);
  185.               ss2=skipspace(++ss1);
  186.               for (i=0; i<net_num_max; i++) {
  187.                 if (stricmp(net_networks[i].name, ss1)==0)
  188.                   break;
  189.               }
  190.               if ((i<net_num_max) && (*ss2)) {
  191.                 xsubsn[nn].net_num=i;
  192.                 ss1=ss2;
  193.                 ss2=skipspace(ss2);
  194.                 strncpy(xsubsn[nn].stype,ss1,7);
  195.                 xsubsn[nn].type=atoi(xsubsn[nn].stype);
  196.                 if (xsubsn[nn].type)
  197.                   sprintf(xsubsn[nn].stype,"%u",xsubsn[nn].type);
  198.                 ss1=ss2;
  199.                 ss2=skipspace(ss2);
  200.                 xsubsn[nn].flags=atol(ss1);
  201.                 ss1=ss2;
  202.                 ss2=skipspace(ss2);
  203.                 xsubsn[nn].host=atoi(ss1);
  204.                 ss1=ss2;
  205.                 ss2=skipspace(ss2);
  206.                 xsubsn[nn].category=atoi(ss1);
  207.                 nn++;
  208.                 xsubs[curn].num_nets++;
  209.                 xsubs[curn].num_nets_max++;
  210.               } else {
  211.                 printf("Unknown network '%s' in SUBS.XTR\n",ss1);
  212.               }
  213.             }
  214.             break;
  215.           case 0:
  216.             break;
  217.           default:
  218.             break;
  219.         }
  220.       }
  221.       bbsfree(ss);
  222.     } else {
  223.       for (curn=0; curn<num_subs; curn++) {
  224.         if (subboards[curn].type) {
  225.           if (subboards[curn].age&0x80)
  226.             xsubsn[nn].net_num=subboards[curn].name[40];
  227.           else
  228.             xsubsn[nn].net_num=0;
  229.           if ((xsubsn[nn].net_num>=0) && (xsubsn[nn].net_num<net_num_max)) {
  230.             xsubs[curn].nets=&(xsubsn[nn]);
  231.             xsubsn[nn].type=subboards[curn].type;
  232.             sprintf(xsubsn[nn].stype,"%u",xsubsn[nn].type);
  233.             nn++;
  234.             xsubs[curn].num_nets=1;
  235.             xsubs[curn].num_nets_max=1;
  236.           }
  237.         }
  238.       }
  239.       for (n=0; n<net_num_max; n++) {
  240.         sprintf(s,"%sALLOW.NET",net_networks[n].dir);
  241.         ss=mallocin_file(s,&l);
  242.         if (ss) {
  243.           for (ss1=strtok(ss," \t\r\n"); ss1; ss1=strtok(NULL," \t\r\n")) {
  244.             xnp=fsub(n, atoi(ss1));
  245.             if (xnp)
  246.               xnp->flags |= XTRA_NET_AUTO_ADDDROP;
  247.           }
  248.           bbsfree(ss);
  249.         }
  250.  
  251.         sprintf(s,"%sSUBS.PUB",net_networks[n].dir);
  252.         ss=mallocin_file(s,&l);
  253.         if (ss) {
  254.           for (ss1=strtok(ss," \t\r\n"); ss1; ss1=strtok(NULL," \t\r\n")) {
  255.             xnp=fsub(n, atoi(ss1));
  256.             if (xnp)
  257.               xnp->flags |= XTRA_NET_AUTO_INFO;
  258.           }
  259.           bbsfree(ss);
  260.         }
  261.         sprintf(s,"%sNNALL.NET",net_networks[n].dir);
  262.         ss=mallocin_file(s,&l);
  263.         if (ss) {
  264.           for (ss1=strtok(ss,"\r\n"); ss1; ss1=strtok(NULL,"\r\n")) {
  265.             while ((*ss1==' ') || (*ss1=='\t'))
  266.               ++ss1;
  267.             ss2=skipspace(ss1);
  268.             xnp=fsub(n, atoi(ss1));
  269.             if (xnp)
  270.               xnp->host=atoi(ss2);
  271.           }
  272.           bbsfree(ss);
  273.         }
  274.       }
  275.       for (i=0; i<nn; i++) {
  276.         if ((xsubsn[i].type) && (!xsubsn[i].host)) {
  277.           sprintf(s,"%sNN%u.NET",
  278.                   net_networks[xsubsn[i].net_num].dir,
  279.                   xsubsn[i].type);
  280.           ss=mallocin_file(s,&l);
  281.           if (ss) {
  282.             for (ss1=ss; (*ss1) && ((*ss1<'0') || (*ss1>'9')); ss1++)
  283.               ;
  284.             xsubsn[i].host=atoi(ss1);
  285.             bbsfree(ss);
  286.           }
  287.         }
  288.       }
  289.     }
  290.   }
  291.  
  292.   return(0);
  293. }
  294.