home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / W / DEVBBS.ZIP / NETSUP.C < prev    next >
C/C++ Source or Header  |  1992-07-20  |  13KB  |  619 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1991 by Wayne Bell
  5.  
  6. *****************************************************************************/
  7.  
  8. #include "vars.h"
  9. #pragma hdrstop
  10. #include <dir.h>
  11. #define utoa(s,v,r) ultoa((unsigned long)(s),v,r)
  12.  
  13. void checkup(struct ftime *f1, struct ftime *f2, int *tf)
  14. {
  15.   if ((f1->ft_year)>(f2->ft_year))
  16.     *tf=1;
  17.   else
  18.     if (f1->ft_year==f2->ft_year)
  19.       if (f1->ft_month>f2->ft_month)
  20.     *tf=1;
  21.       else
  22.     if (f1->ft_month==f2->ft_month)
  23.       if (f1->ft_day>f2->ft_day)
  24.         *tf=1;
  25.       else
  26.         if (f1->ft_day==f2->ft_day)
  27.           if (f1->ft_hour>f2->ft_hour)
  28.         *tf=1;
  29.           else
  30.         if (f1->ft_hour==f2->ft_hour)
  31.           if (f1->ft_min>f2->ft_min)
  32.             *tf=1;
  33.           else
  34.             if (f1->ft_min==f2->ft_min)
  35.               if (f1->ft_tsec>f2->ft_tsec)
  36.             *tf=1;
  37. }
  38.  
  39. #ifdef OLD
  40.  
  41. int check_bbsdata()
  42. {
  43.   char s1[81],s2[81],s3[81],s4[81];
  44.   int i,e1,e2,e3,e4,tf,n,tf1;
  45.   struct ftime f1,f2,f3,f4;
  46.  
  47.   n=0;
  48.   sprintf(s1,"%sBBSLIST.NET",syscfg.datadir);
  49.   sprintf(s2,"%sCONNECT.NET",syscfg.datadir);
  50.   sprintf(s3,"%sCALLOUT.NET",syscfg.datadir);
  51.   sprintf(s4,"%sBBSDATA.NET",syscfg.datadir);
  52.   e1=exist(s1);
  53.   e2=exist(s2);
  54.   e3=exist(s3);
  55.   e4=exist(s4);
  56.   tf=0;
  57.   if ((e1) && (e2) && (e3)) {
  58.     if (e4) {
  59.       i=open(s1,O_RDONLY);
  60.       getftime(i,&f1);
  61.       close(i);
  62.       i=open(s2,O_RDONLY);
  63.       getftime(i,&f2);
  64.       close(i);
  65.       i=open(s3,O_RDONLY);
  66.       getftime(i,&f3);
  67.       close(i);
  68.       i=open(s4,O_RDONLY);
  69.       getftime(i,&f4);
  70.       close(i);
  71.       tf1=0;
  72.       checkup(&f1,&f4,&tf1);
  73.       if (tf1) {
  74.     tf=1;
  75.     n=1;
  76.       }
  77.       tf1=0;
  78.       checkup(&f2,&f4,&tf1);
  79.       if (tf1) {
  80.     tf=1;
  81.     n=1;
  82.       }
  83.       checkup(&f3,&f4,&tf);
  84.     } else {
  85.       tf=1;
  86.       n=1;
  87.     }
  88.   }
  89.   if (tf) {
  90.     holdphone(1);
  91. #ifdef OLD_WAY
  92.     if (n)
  93.       tf=run_external1("NETWORK3 Y");
  94.     else
  95.       tf=run_external1("NETWORK3");
  96. #else
  97.     if (n)
  98.       shrink_out("NETWORK3 Y",0,0,0,0);
  99.     else
  100.       shrink_out("NETWORK3",0,0,0,0);
  101. #endif
  102.     if (csn!=NULL)
  103.       farfree(csn);
  104.     if (cnn!=NULL)
  105.       farfree(cnn);
  106.     if (con!=NULL)
  107.       farfree(con);
  108.     read_bbs_list_index();
  109.     read_contacts();
  110.     if (tf<0)
  111.       return(0);
  112.     else
  113.       return(1);
  114.   }
  115.   return(0);
  116. }
  117.  
  118. #else
  119. int checkup2(struct ftime *f, char *x)
  120. {
  121.   int q;
  122.   char s[81];
  123.   struct ftime f2;
  124.  
  125.   sprintf(s,"%s%s",syscfg.datadir,x);
  126.   q=open(s,O_RDONLY);
  127.   if (q>0) {
  128.     getftime(q,&f2);
  129.     close(q);
  130.     q=0;
  131.     checkup(&f2,f,&q);
  132.   } else
  133.     q=1;
  134.   return(q);
  135. }
  136.  
  137. int check_bbsdata()
  138. {
  139.   char s[81];
  140.   int ok,ok2;
  141.   struct ftime ft;
  142.  
  143.   sprintf(s,"%sCONNECT.UPD",syscfg.datadir);
  144.   if ((ok=exist(s))==0) {
  145.     sprintf(s,"%sBBSLIST.UPD",syscfg.datadir);
  146.     ok=exist(s);
  147.   }
  148.   if (ok  && status.net_edit_stuff) {
  149.     holdphone(1);
  150.     run_external1("NETEDIT /U");
  151.   } else {
  152.     sprintf(s,"%sBBSDATA.NET",syscfg.datadir);
  153.     ok=open(s,O_RDONLY);
  154.     if (ok>0) {
  155.       getftime(ok,&ft);
  156.       close(ok);
  157.       ok=checkup2(&ft,"BBSLIST.NET")||checkup2(&ft,"CONNECT.NET");
  158.       ok2=checkup2(&ft,"CALLOUT.NET");
  159.     } else
  160.       ok=ok2=1;
  161.   }
  162.   sprintf(s,"%sBBSLIST.NET",syscfg.datadir);
  163.   if (!exist(s))
  164.     ok=ok2=0;
  165.   sprintf(s,"%sCONNECT.NET",syscfg.datadir);
  166.   if (!exist(s))
  167.     ok=ok2=0;
  168.   sprintf(s,"%sCALLOUT.NET",syscfg.datadir);
  169.   if (!exist(s))
  170.     ok=ok2=0;
  171.   if (ok||ok2) {
  172.     sprintf(s,"NETWORK3%s",(ok?" Y":""));
  173.     holdphone(1);
  174. #ifdef OLD_WAY
  175.     ok=run_external1(s);
  176. #else
  177.     shrink_out(s,0,0,0,0);
  178.     ok=0;
  179. #endif
  180.     if (csn!=NULL)
  181.       farfree((void far *)csn);
  182.     if (cnn!=NULL)
  183.       farfree(cnn);
  184.     if (con!=NULL)
  185.       farfree(con);
  186.     read_bbs_list_index();
  187.     read_contacts();
  188.     if (ok>=0)
  189.       return(1);
  190.   }
  191.   return(0);
  192. }
  193. #endif
  194.  
  195. void cleanup_net()
  196. {
  197.   char s[81],s1[81];
  198.   int ok,ok2,abort,f,i;
  199.   long l,l1;
  200.   struct ffblk ff;
  201.  
  202.   if (!syscfg.systemnumber)
  203.     return;
  204.   ok2=1;
  205.   abort=0;
  206.   set_protect(0);
  207.   clrscrb();
  208.   while ((ok2) && (!abort)) {
  209.     ok2=0;
  210.     ok=0;
  211.     sprintf(s,"%sP*.NET",syscfg.datadir);
  212.     ok=(findfirst(s,&ff,0)==0);
  213.     if (ok) {
  214.       holdphone(1);
  215.       hangup=0;
  216.       using_modem=0;
  217.       if (run_external1("NETWORK1")<0)
  218.         abort=1;
  219.       ok2=1;
  220.     }
  221.     sprintf(s,"%sLOCAL.NET",syscfg.datadir);
  222.     if (exist(s)) {
  223.       holdphone(1);
  224.       ok=1;
  225.       hangup=0;
  226.       using_modem=0;
  227.       save_status();
  228.       close_user();
  229.       sprintf(s,"%sNETUP.XXX",syscfg.datadir);
  230.       if (exist(s))
  231.         shrink_out("NETWORK2",0,0,0,0);
  232.       else
  233.         if (run_external1("NETWORK2")<0)
  234.           abort=1;
  235.       ok2=1;
  236.       l1=status.qscanptr;
  237.       get_status();
  238.       if (l1!=status.qscanptr) {
  239.         /* a sub has probably been updated; invalidate sub cache */
  240.         for (i=0; i<MAX_SUBS; i++) {
  241.           if (subboards[i].type)
  242.             sub_dates[i]=0L;
  243.         }
  244.         c_sub=0;
  245.       }
  246.       curlsub=-1;
  247.       if (wfc) {
  248.         wfc=0;
  249.         read_user(1,&thisuser);
  250.         fwaiting=thisuser.waiting;
  251.         wfc=1;
  252.       }
  253.     }
  254.     if (check_bbsdata())
  255.       ok2=1;
  256.     if (ok2) {
  257.       read_contacts();
  258.       clrscrb();
  259.     }
  260.   }
  261.   holdphone(0);
  262. }
  263.  
  264. unsigned int max_speed(int s1, int s2)
  265. {
  266.   int r1,r2,i,i1;
  267.   unsigned ms;
  268.   net_system_list_rec *csne1, *csne2,c1,c2;
  269.  
  270.   csne1=next_system(s1);
  271.   if (!csne1)
  272.     return(300);
  273.   c1=*csne1;
  274.   csne2=next_system(s2);
  275.   if (!csne2)
  276.     return(300);
  277.   c2=*csne2;
  278.   ms=c1.speed;
  279.   if (c2.speed<ms)
  280.     ms=c2.speed;
  281.   i1=(c1.other) & (c2.other);
  282.   if (i1 & other_Telebit_19200)
  283.     return(19200);
  284.   if (i1 & other_V32)
  285.     return(9600);
  286.   if (i1 & other_Hayes_9600)
  287.     return(9600);
  288.   if (i1 & other_USR_9600)
  289.     return(9600);
  290.   if (i1 & other_compucom)
  291.     return(9600);
  292.   if (ms<=2400)
  293.     return(ms);
  294.   else
  295.     return(2400);
  296. }
  297.  
  298. void do_callout(int sn)
  299. {
  300.   int i,i1,i2;
  301.   char s[81],s1[81];
  302.   float ffl;
  303.   long l;
  304.   net_system_list_rec *csne;
  305.  
  306.   time(&l);
  307.   i=-1;
  308.   for (i1=0; i1<num_call_sys; i1++)
  309.     if (con[i1].sysnum==sn)
  310.       i=i1;
  311.   if (i!=-1) {
  312.     csne=next_system(con[i].sysnum);
  313.     if (csne) {
  314.       sprintf(s,"NETWORK /N%u /A%s /P%s /S%u /T%ld",
  315.         sn,
  316.         (con[i].options & options_sendback)?"1":"0",
  317.         csne->phone,
  318.         modem_i->defl.com_speed,
  319.         l);
  320.       if (con[i].macnum) {
  321.         sprintf(s1," /M%d",(int) con[i].macnum);
  322.         strcat(s,s1);
  323.       }
  324.       if (strncmp(csne->phone,"000",3)) {
  325.         pl(s);
  326.         save_status();
  327.         holdphone(1);
  328.         wait(2.5);
  329.         run_external(s);
  330.         read_contacts();
  331.         get_status();
  332.         last_time_c=l;
  333.         global_xx=0;
  334.         cleanup_net();
  335.         holdphone(0);
  336.         imodem(0);
  337.       }
  338.     }
  339.   }
  340. }
  341.  
  342. int ok_to_call(int i)
  343. {
  344.   int ok;
  345.   struct time ti;
  346.   char ch,h,l;
  347.  
  348.   ok=((con[i].options & options_no_call)==0);
  349.   if (con[i].options & options_receive_only)
  350.     ok=0;
  351.   _AH=0x2a;
  352.   geninterrupt(0x21);
  353.   ch=_AL;
  354.   gettime(&ti);
  355.   if (con[i].options & options_ATT_night) {
  356.     if ((ch!=0) && (ch!=6)) {
  357.       if ((ti.ti_hour<23) && (ti.ti_hour>=7))
  358.         ok=0;
  359.     }
  360.     if (ch==0) {
  361.       if ((ti.ti_hour<23) && (ti.ti_hour>=16))
  362.         ok=0;
  363.     }
  364.   }
  365.   if (con[i].options & options_PCP_night) {
  366.     if ((ch!=0) && (ch!=6)) {
  367.       if ((ti.ti_hour<19) && (ti.ti_hour>=6))
  368.         ok=0;
  369.     }
  370.   }
  371.  
  372.   l=con[i].min_hr;
  373.   h=con[i].max_hr;
  374.   if ((l>-1) && (h>-1) && (h!=l)) {
  375.     if ((h==0) || (h==24)) {
  376.       if (ti.ti_hour<l)
  377.         ok=0;
  378.       else
  379.         if ((ti.ti_hour==l) && (ti.ti_min<12))
  380.           ok=0;
  381.         else
  382.           if ((ti.ti_hour==23) && (ti.ti_min>30))
  383.             ok=0;
  384.     } else
  385.       if ((l==0) || (l==24)) {
  386.         if (ti.ti_hour>=h)
  387.           ok=0;
  388.         else
  389.           if ((ti.ti_hour==h-1) && (ti.ti_min>30))
  390.             ok=0;
  391.           else
  392.             if ((ti.ti_hour==0) && (ti.ti_min<12))
  393.               ok=0;
  394.       } else
  395.         if (h>l) {
  396.           if ((ti.ti_hour<l) || (ti.ti_hour>=h))
  397.             ok=0;
  398.           else
  399.             if ((ti.ti_hour==l) && (ti.ti_min<12))
  400.               ok=0;
  401.             else
  402.               if ((ti.ti_hour==h-1) && (ti.ti_min>30))
  403.                 ok=0;
  404.         } else {
  405.           if ((ti.ti_hour>=h) && (ti.ti_hour<l))
  406.             ok=0;
  407.           else
  408.             if ((ti.ti_hour==l) && (ti.ti_min<12))
  409.               ok=0;
  410.             else
  411.               if ((ti.ti_hour==h-1) && (ti.ti_min>30))
  412.                 ok=0;
  413.         }
  414.   }
  415.   return(ok);
  416. }
  417.  
  418. #define WEIGHT 30.0
  419.  
  420. void fixup_long(long *f, long l)
  421. {
  422.   if (*f>l)
  423.     *f=l;
  424.  
  425.   if (*f+(86400L*30L)<l)
  426.     *f=l-86400L*30L;
  427. }
  428.  
  429. void attempt_callout()
  430. {
  431.   int *try,i,i1,i2,i3,index,ok,net_only;
  432.   float *weight,fl,fl1,fl2,ffl;
  433.   long l,l1;
  434.   char ch,s[81],s1[81];
  435.   struct time ti;
  436.  
  437.   net_only=1;
  438.   if (syscfg.netlowtime!= syscfg.nethightime) {
  439.     if (syscfg.nethightime>syscfg.netlowtime) {
  440.       if ((timer()<=(syscfg.netlowtime*60.0)) || (timer()>=(syscfg.nethightime*60.0)))
  441.         net_only=0;
  442.     } else {
  443.       if ((timer()<=(syscfg.netlowtime*60.0)) && (timer()>=(syscfg.nethightime*60.0)))
  444.         net_only=0;
  445.     }
  446.   } else
  447.     net_only=0;
  448.   time(&l);
  449.   if (last_time_c>l)
  450.     last_time_c=0L;
  451.   if (labs(last_time_c-l)<120)
  452.     return;
  453.   if (last_time_c==0L) {
  454.     last_time_c=l;
  455.     return;
  456.   }
  457.   if ((try=(int *)malloca(sizeof(int)*num_call_sys))==NULL)
  458.     return;
  459.   if ((weight=(float *)malloca(sizeof(float)*num_call_sys))==NULL) {
  460.     farfree(try);
  461.     return;
  462.   }
  463.   index=0;
  464.   fl2=0.0;
  465.   for (i=0; i<num_call_sys; i++) {
  466.     ok=ok_to_call(i);
  467.     i2=-1;
  468.     for (i1=0; i1<num_ncn; i1++)
  469.       if (ncn[i1].systemnumber==con[i].sysnum)
  470.     i2=i1;
  471.     if ((ok) && (i2!=-1)) {
  472.       if (ncn[i2].bytes_waiting==0L)
  473.     if (con[i].call_anyway) {
  474.           l1=(l-ncn[i2].lastcontact+60*3)/3600/24;
  475.       if (((unsigned char)l1 < con[i].call_anyway) ||
  476.          ((con[i].options & options_sendback)==0))
  477.             if (!net_only)
  478.               ok=0;
  479.     } else
  480.       ok=0;
  481.       if (con[i].options & options_once_per_day) {
  482.         if (labs(l-ncn[i2].lastcontactsent)<(20L*3600L/con[i].times_per_day))
  483.       ok=0;
  484.       }
  485.       fixup_long((long *)&(ncn[i2].lastcontactsent),l);
  486.       fixup_long((long *)&(ncn[i2].lasttry),l);
  487.       if (ok) {
  488.     if (ncn[i2].bytes_waiting==0L)
  489.       fl=5.0*WEIGHT;
  490.     else
  491.       fl=1024.0/((float)ncn[i2].bytes_waiting)*WEIGHT*60.0;
  492.         fl1=(float) (l-ncn[i2].lasttry);
  493.     if ((fl<fl1) || (net_only)) {
  494.           try[index]=i;
  495.       fl1=fl1/fl;
  496.           if (fl1<1.0)
  497.         fl1=1.0;
  498.       if (fl1>5.0)
  499.         fl1=5.0;
  500.           weight[index]=fl1;
  501.       fl2 += weight[index];
  502.       ++index;
  503.     }
  504.       }
  505.     }
  506.   }
  507.   if (index) {
  508.     fl=fl2*((float)rand())/32767.0;
  509.     fl1=0.0;
  510.     i1=-1;
  511.     for (i=0; (i<index) && (i1==-1); i++) {
  512.       fl1 += weight[i];
  513.       if (fl1>=fl)
  514.     i1=i;
  515.     }
  516.     if (i1==-1)
  517.       i1=index-1;
  518.     i=try[i1];
  519.     farfree(try);
  520.     farfree(weight);
  521.     do_callout(con[i].sysnum);
  522.     time(&l);
  523.     last_time_c=l;
  524.   } else {
  525.     farfree(try);
  526.     farfree(weight);
  527.   }
  528. }
  529.  
  530. void force_callout()
  531. {
  532.   int i,i1,i2,i3,index,ok,sn;
  533.   float fl,fl1,fl2,ffl;
  534.   long l,l1;
  535.   char ch,s[81],s1[81];
  536.   struct time ti;
  537.  
  538.   time(&l);
  539.   nl();
  540.   prt(2,"Which system? ");
  541.   input(s,5);
  542.   sn=atoi(s);
  543.   i=-1;
  544.   for (i1=0; i1<num_call_sys; i1++)
  545.     if (con[i1].sysnum==sn)
  546.       i=i1;
  547.   if (i==-1)
  548.     return;
  549.   ok=ok_to_call(i);
  550.   i2=-1;
  551.   for (i1=0; i1<num_ncn; i1++)
  552.     if (ncn[i1].systemnumber==con[i].sysnum)
  553.       i2=i1;
  554.   if (i2==-1)
  555.     ok=0;
  556.   else
  557.     if (!ok) {
  558.       nl();
  559.       prt(5,"Are you sure? ");
  560.       if (yn())
  561.         ok=1;
  562.     }
  563.   if (ok) {
  564.     if (ncn[i2].bytes_waiting==0L)
  565.       if (!(con[i].options & options_sendback))
  566.     ok=0;
  567.     if (ok) {
  568.       do_callout(sn);
  569.     }
  570.   }
  571. }
  572.  
  573. void print_pending_list()
  574. {
  575.   int i,any=0,i1,i2;
  576.   char s[81],ch,s1[81];
  577.   long l;
  578.  
  579.   if (syscfg.systemnumber) {
  580.     time(&l);
  581.     nl();
  582.     nl();
  583.     pl(" Network data pending transmission:");
  584.     nl();
  585.     pl(" System       Bytes     Hours");
  586.     pl(" ------      -------    -----");
  587.     for (i=0; i<num_ncn; i++) {
  588.       if (ncn[i].bytes_waiting) {
  589.         i2=-1;
  590.         for (i1=0; i1<num_call_sys; i1++)
  591.           if (con[i1].sysnum==ncn[i].systemnumber)
  592.             i2=i1;
  593.         if (i2!=-1) {
  594.           if (ok_to_call(i2))
  595.             ch='*';
  596.           else
  597.             ch=' ';
  598.           any=1;
  599.           if (ncn[i].lastcontactsent)
  600.             ltoa((l-ncn[i].lastcontactsent)/3600,s1,10);
  601.           else
  602.             strcpy(s1,"-NEVER-");
  603.           sprintf(s,"%c%-5d       %7ld    %s",
  604.             ch,
  605.             ncn[i].systemnumber,
  606.             ncn[i].bytes_waiting,
  607.             s1);
  608.           pl(s);
  609.         }
  610.       }
  611.     }
  612.     if (!any)
  613.       pl("--NONE--");
  614.     nl();
  615.     if (!useron)
  616.       pausescr();
  617.   }
  618. }
  619.