home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / WWIVMODS / 421-421A.ZIP / NETSUP.C < prev    next >
C/C++ Source or Header  |  1992-07-18  |  16KB  |  722 lines

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