home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / WWIV2.ZIP / SR.C < prev    next >
C/C++ Source or Header  |  1992-12-03  |  14KB  |  696 lines

  1. /*****************************************************************************
  2.  
  3.                 WWIV Version 4
  4.                     Copyright (C) 1988-1993 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 <time.h>
  22. #include <math.h>
  23.  
  24.  
  25.  
  26. char *stripfn(char *fn)
  27. {
  28.   static char ofn[15];
  29.   int i,i1;
  30.   char s[81];
  31.  
  32.   i1=-1;
  33.   for (i=0; i<strlen(fn); i++)
  34.     if ((fn[i]=='\\') || (fn[i]==':') || (fn[i]=='/'))
  35.       i1=i;
  36.   if (i1!=-1)
  37.     strcpy(s,&(fn[i1+1]));
  38.   else
  39.     strcpy(s,fn);
  40.   for (i=0; i<strlen(s); i++)
  41.     if ((s[i]>='A') && (s[i]<='Z'))
  42.       s[i]=s[i]-'A'+'a';
  43.   i=0;
  44.   while (s[i]!=0) {
  45.     if (s[i]==32)
  46.       strcpy(&s[i],&s[i+1]);
  47.     else
  48.       ++i;
  49.   }
  50.   strcpy(ofn,s);
  51.   return(ofn);
  52. }
  53.  
  54.  
  55. void stripfn1(char *fn)
  56. {
  57.   int i,i1;
  58.   char s[81],s1[81];
  59.  
  60.   i1=0;
  61.   for (i=0; i<strlen(fn); i++)
  62.     if ((fn[i]=='\\') || (fn[i]==':') || (fn[i]=='/'))
  63.       i1=i;
  64.   strcpy(s1,fn);
  65.   if (i1) {
  66.     strcpy(s,&(fn[i1+1]));
  67.     s1[i1+1]=0;
  68.   } else {
  69.     strcpy(s,fn);
  70.     s1[0]=0;
  71.   }
  72.  
  73.   for (i=0; i<strlen(s); i++)
  74.     if ((s[i]>='A') && (s[i]<='Z'))
  75.       s[i]=s[i]-'A'+'a';
  76.   i=0;
  77.   while (s[i]!=0) {
  78.     if (s[i]==32)
  79.       strcpy(&s[i],&s[i+1]);
  80.     else
  81.       ++i;
  82.   }
  83.   strcat(s1,s);
  84.   strcpy(fn,s1);
  85. }
  86.  
  87.  
  88. void calc_CRC(unsigned char b)
  89. {
  90.   int i;
  91.  
  92.   checksum += b;
  93.  
  94.   crc ^= (((unsigned short) (b)) << 8);
  95.   for (i=0; i<8; i++)
  96.     if (crc & 0x8000) {
  97.       crc=(crc << 1);
  98.       crc ^= 0x1021;
  99.     } else
  100.       crc=(crc << 1);
  101. }
  102.  
  103.  
  104. char gettimeout(double d, int *abort)
  105. {
  106.   double d1;
  107.   char ch;
  108.  
  109.   if (comhit())
  110.     return(get1c());
  111.   d1=timer();
  112.   while ((fabs(timer()-d1)<d) && (!comhit()) && (!hangup) && (!*abort)) {
  113.     if (kbhitb()) {
  114.       ch=getchd();
  115.       if (ch==0)
  116.         getchd();
  117.       else
  118.         if (ch==27)
  119.           *abort=1;
  120.     }
  121.     checkhangup();
  122.   }
  123.   if (comhit())
  124.     return(get1c());
  125.   else
  126.     return(0);
  127. }
  128.  
  129.  
  130. int extern_prot(int pn, char *fn1, int sending)
  131. {
  132.   char s[255],s1[81],s2[81],fn[81],sx1[21],sx2[21],sx3[21];
  133.   int i,i1;
  134.  
  135.   if (sending) {
  136.     nl();
  137.     pl(get_string(886));
  138.     if (pn<0)
  139.       strcpy(s1,over_intern[(-pn)-1].sendfn);
  140.     else
  141.       strcpy(s1,(externs[pn].sendfn));
  142.   } else {
  143.     nl();
  144.     pl(get_string(887));
  145.     if (pn<0)
  146.       strcpy(s1,over_intern[(-pn)-1].receivefn);
  147.     else
  148.       strcpy(s1,(externs[pn].receivefn));
  149.   }
  150.   strcpy(fn,fn1);
  151.   stripfn1(fn);
  152.   ultoa(com_speed,sx1,10);
  153.   ultoa(modem_speed,sx3,10);
  154.   sx2[0]='0'+syscfg.primaryport;
  155.   sx2[1]=0;
  156.   stuff_in(s,s1,sx1,sx2,fn,sx3,"");
  157.   if (s[0]) {
  158.     set_protect(0);
  159.     outs(get_stringx(1,64));
  160.     outs(nam(&thisuser,usernum));
  161.     outs("\r\n\r\n");
  162.     outs(s);
  163.     outs("\r\n");
  164.     if (incom) {
  165.       create_chain_file("CHAIN.TXT");
  166.       i=run_external1(s);
  167.       topscreen();
  168.       return(i);
  169.     } else {
  170.       topscreen();
  171.       return(-5);
  172.     }
  173.   }
  174.   return(-5);
  175. }
  176.  
  177.  
  178. int ok_prot(int pn, xfertype xt)
  179. {
  180.   int ok=0;
  181.   int i;
  182.  
  183.   if (xt==xf_none)
  184.     return(0);
  185.  
  186.   if ((pn>0) && (pn<(numextrn+6))) {
  187.  
  188.     switch(pn) {
  189.       case 1:
  190.         if ((xt==xf_down) || (xt==xf_down_temp))
  191.           ok=1;
  192.         break;
  193.       case 2:
  194.       case 3:
  195.       case 4:
  196.         if ((xt!=xf_up_batch) && (xt!=xf_down_batch) && (xt!=xf_bi))
  197.           ok=1;
  198.         if ((pn==4) && (xt==xf_down_batch))
  199.           ok=1;
  200.         break;
  201.       case 5:
  202.         if (xt==xf_up) {
  203.           for (i=0; i<numextrn; i++)
  204.             if (externs[i].receivebatchfn[0] || externs[i].bibatchfn)
  205.               ok=1;
  206.         } else if (xt==xf_down) {
  207.           for (i=0; i<numextrn; i++)
  208.             if (externs[i].sendbatchfn[0] || externs[i].bibatchfn)
  209.               ok=1;
  210.         }
  211.         if ((xt==xf_up) || (xt==xf_down))
  212.           ok=1;
  213.         break;
  214.       default:
  215.         switch(xt) {
  216.           case xf_up:
  217.           case xf_up_temp:
  218.             if (externs[pn-6].receivefn[0])
  219.               ok=1;
  220.             break;
  221.           case xf_down:
  222.           case xf_down_temp:
  223.             if (externs[pn-6].sendfn[0])
  224.               ok=1;
  225.             break;
  226.           case xf_up_batch:
  227.             if (externs[pn-6].receivebatchfn[0])
  228.               ok=1;
  229.             break;
  230.           case xf_down_batch:
  231.             if (externs[pn-6].sendbatchfn[0])
  232.               ok=1;
  233.             break;
  234.           case xf_bi:
  235.             if (externs[pn-6].bibatchfn[0])
  236.               ok=1;
  237.             break;
  238.         }
  239.         if (externs[pn-6].othr & othr_error_correct)
  240.           if (!(modem_flag & flag_ec))
  241.             ok=0;
  242.         break;
  243.     }
  244.   }
  245.  
  246.   return(ok);
  247. }
  248.  
  249.  
  250. char *prot_name(int pn)
  251. {
  252.   char *ss=get_string(888);
  253.  
  254.   switch(pn) {
  255.     case 1:
  256.       ss=get_string(889);
  257.       break;
  258.     case 2:
  259.       ss=get_string(890);
  260.       break;
  261.     case 3:
  262.       ss=get_string(891);
  263.       break;
  264.     case 4:
  265.       ss=get_string(892);
  266.       break;
  267.     case 5:
  268.       ss=get_string(893);
  269.       break;
  270.     default:
  271.       if ((pn>5) || (pn<(numextrn+6)))
  272.         ss=externs[pn-6].description;
  273.       break;
  274.   }
  275.  
  276.   return(ss);
  277. }
  278.  
  279. #define BASE_CHAR '!'
  280.  
  281. int get_protocol(xfertype xt)
  282. {
  283.   char s[81],s1[81],oks[81],s2[81],ch,oks1[81],*ss,ch1,fl[80];
  284.   int i,i1,i2,prot,maxprot,done,only,oks1p;
  285.  
  286.   if (ok_prot(thisuser.defprot, xt))
  287.     prot=thisuser.defprot;
  288.   else
  289.     prot=0;
  290.  
  291.   oks1p=0;
  292.   oks1[0]=0;
  293.   strcpy(oks,"Q?0");
  294.   i1=strlen(oks);
  295.   only=0;
  296.   maxprot=5+numextrn;
  297.   for (i=1; i<=maxprot; i++) {
  298.     fl[i]=0;
  299.     if (ok_prot(i,xt)) {
  300.       if (i<10)
  301.         oks[i1++]='0'+i;
  302.       else
  303.         oks[i1++]=BASE_CHAR+i-10;
  304.       if (only==0)
  305.         only=i;
  306.       else
  307.         only=-1;
  308.       if (i>=3) {
  309.         ch1=upcase(*prot_name(i));
  310.         if (strchr(oks1,ch1)==0) {
  311.           oks1[oks1p++]=ch1;
  312.           oks1[oks1p]=0;
  313.           fl[i]=1;
  314.         }
  315.       }
  316.     }
  317.   }
  318.   oks[i1]=0;
  319.   strcat(oks,oks1);
  320.   if (only>0)
  321.     prot=only;
  322.  
  323.   if ((only==0) && (xt != xf_none)) {
  324.     nl();
  325.     pl(get_string(894));
  326.     nl();
  327.     return(-1);
  328.   }
  329.  
  330.   done=0;
  331.   if (prot) {
  332.     ss=prot_name(prot);
  333.     sprintf(s,"%s%s) : ", get_string(895),ss);
  334.     strcpy(s1,oks);
  335.     strcat(s1,"\r");
  336.   } else {
  337.     strcpy(s,get_string(896));
  338.     strcpy(s1,oks);
  339.   }
  340.   do {
  341.     nl();
  342.     prt(2,s);
  343.     ch=onek(s1);
  344.     if (ch=='?') {
  345.       nl();
  346.       pl(get_string(897));
  347.       pl(get_string(898));
  348.       for (i=1; i<=maxprot; i++) {
  349.         if (ok_prot(i,xt)) {
  350.           ch1=upcase(*prot_name(i));
  351.           if (fl[i]==0)
  352.             npr("%c. %s\r\n",(i<10)?(i+'0'):(i+BASE_CHAR-10),prot_name(i));
  353.           else
  354.             npr("%c. %c)%s\r\n",
  355.                 (i<10)?(i+'0'):(i+BASE_CHAR-10),*prot_name(i), prot_name(i)+1);
  356.         }
  357.       }
  358.       nl();
  359.     } else
  360.       done=1;
  361.   } while ((!done) && (!hangup));
  362.   if (ch==13)
  363.     return(prot);
  364.   if ((ch>='0') && (ch<='9'))
  365.     return(ch-'0');
  366.   else {
  367.     if (ch=='Q')
  368.       return(-1);
  369.     else {
  370.       i1=ch-BASE_CHAR+10;
  371.       if (i1<numextrn+6)
  372.         return(ch-BASE_CHAR+10);
  373.       for (i=3; i<numextrn+6; i++) {
  374.         if (upcase(*prot_name(i))==ch)
  375.           return(i);
  376.       }
  377.     }
  378.   }
  379.   return(-1);
  380. }
  381.  
  382.  
  383. void ascii_send(char *fn, int *sent, double *percent)
  384. {
  385.   char b[2048];
  386.   int i,i1,done,abort,i2,next;
  387.   long pos,max;
  388.  
  389.   i=open(fn,O_RDONLY | O_BINARY);
  390.   if (i>0) {
  391.     max=filelength(i);
  392.     if (!max)
  393.       max=1;
  394.     i1=read(i,(void *)b,1024);
  395.     pos=0L;
  396.     abort=0;
  397.     while ((i1) && (!hangup) && (!abort)) {
  398.       i2=0;
  399.       while ((!hangup) && (!abort) && (i2<i1)) {
  400.     checkhangup();
  401.     outchr(b[i2++]);
  402.     checka(&abort,&next);
  403.       }
  404.       pos += (long) i2;
  405.       checka(&abort,&next);
  406.       i1=read(i,(void *)b,1024);
  407.     }
  408.     close(i);
  409.     if (!abort)
  410.       *sent=1;
  411.     else {
  412.       *sent=0;
  413.       thisuser.dk += ((pos+1023L)/1024L);
  414.     }
  415.     *percent=((double) pos)/((double)max);
  416.   } else {
  417.     nl();
  418.     pl(get_string(89));
  419.     nl();
  420.     *sent=0;
  421.     *percent=0.0;
  422.   }
  423. }
  424.  
  425.  
  426. void maybe_internal(char *fn, int *xferred, double *percent, char ft, char *ftp, int send, int prot)
  427. {
  428.   if (over_intern && (over_intern[prot-2].othr & othr_override_internal) &&
  429.       ((send && over_intern[prot-2].sendfn[0]) ||
  430.        (!send && over_intern[prot-2].receivefn[0]))) {
  431.     if (extern_prot(-(prot-1), fn, send)==over_intern[prot-2].ok1)
  432.       *xferred=1;
  433.   } else {
  434.     if (incom) {
  435.       if (send) {
  436.         switch(prot) {
  437.           case 2:
  438.             xymodem_send(fn, xferred, percent, ft, 0, 0, 0);
  439.             break;
  440.           case 3:
  441.             xymodem_send(fn, xferred, percent, ft, 1, 0, 0);
  442.             break;
  443.           case 4:
  444.             xymodem_send(fn, xferred, percent, ft, 1, 1, 0);
  445.             break;
  446.         }
  447.       } else {
  448.         switch(prot) {
  449.           case 2:
  450.             xymodem_receive(fn, ftp, xferred, 0);
  451.             break;
  452.           case 3:
  453.           case 4:
  454.             xymodem_receive(fn, ftp, xferred, 1);
  455.             break;
  456.         }
  457.       }
  458.     } else {
  459.       outstr(get_string(899));
  460.       pl(prot_name(prot));
  461.     }
  462.   }
  463. }
  464.  
  465.  
  466. void send_file(char *fn, int *sent, int *abort, char ft, char *sfn, int dn, long fs)
  467. {
  468.   int i,i1,ok;
  469.   double percent,t;
  470.   char s[81];
  471.  
  472.   *sent=0;
  473.   *abort=0;
  474.   if (fs<0) {
  475.     i=get_protocol(xf_none);
  476.   } else {
  477.     if (dn==-1)
  478.       i=get_protocol(xf_down_temp);
  479.     else
  480.       i=get_protocol(xf_down);
  481.   }
  482.   ok=0;
  483.   percent=0.0;
  484.   if (check_batch_queue(sfn)) {
  485.     *sent=0;
  486.     if (i>0) {
  487.       nl();
  488.       pl(get_string(762));
  489.       nl();
  490.     } else if (i==-1)
  491.       *abort=1;
  492.   } else {
  493.     switch(i) {
  494.       case -1:
  495.         *abort=1;
  496.         ok=1;
  497.         break;
  498.       case 0:
  499.         ok=1;
  500.         break;
  501.       case 1:
  502.         ascii_send(fn,sent,&percent);
  503.         break;
  504.       case 2:
  505.       case 3:
  506.       case 4:
  507.         maybe_internal(fn, sent, &percent, ft, NULL, 1, i);
  508.         break;
  509.       case 5:
  510.         ok=1;
  511.         if (numbatch>=MAX_BATCH) {
  512.           nl();
  513.           pl(get_string(900));
  514.           nl();
  515.           *sent=0;
  516.           *abort=0;
  517.         } else {
  518.           if (modem_speed)
  519.             t=(12.656) / ((double) (modem_speed)) * ((double)(fs));
  520.           else
  521.             t=0.0;
  522.           if (nsl()<=(batchtime + t)) {
  523.             nl();
  524.             pl(get_string(901));
  525.             nl();
  526.             *sent=0;
  527.             *abort=0;
  528.           } else {
  529.             if (dn==-1) {
  530.               nl();
  531.               pl(get_string(902));
  532.               nl();
  533.               *sent=0;
  534.               *abort=0;
  535.             } else {
  536.               batchtime += t;
  537.               strcpy(batch[numbatch].filename,sfn);
  538.               batch[numbatch].dir=dn;
  539.               batch[numbatch].time=t;
  540.               batch[numbatch].sending=1;
  541.               batch[numbatch].len=fs;
  542.  
  543.               numbatch++;
  544.               ++numbatchdl;
  545.               nl();
  546.               pl(get_string(903));
  547.               sprintf(s,"%s - %d  %s - %s",get_string(904),
  548.                       numbatch, get_string(905),ctim(batchtime));
  549.               nl();
  550.               pl(s);
  551.               nl();
  552.               *sent=0;
  553.               *abort=0;
  554.             }
  555.           }
  556.         }
  557.         break;
  558.       default:
  559.         i1=extern_prot(i-6,fn,1);
  560.         *abort=0;
  561.         if (i1==externs[i-6].ok1)
  562.           *sent=1;
  563.         break;
  564.     }
  565.   }
  566.   if ((*sent==0) && (ok==0))
  567.     if (percent==1.0) {
  568.       *sent=1;
  569.       add_ass(10,get_stringx(1,65));
  570.     } else {
  571.       sprintf(s,get_stringx(1,66),stripfn(fn),percent*100.0);
  572.       sysoplog(s);
  573.     }
  574. }
  575.  
  576.  
  577. void receive_file(char *fn, int *received, char *ft, char *sfn, int dn)
  578. {
  579.   int i;
  580.   char s[81];
  581.  
  582.   if (dn==-1)
  583.     i=get_protocol(xf_up_temp);
  584.   else
  585.     i=get_protocol(xf_up);
  586.  
  587.   switch(i) {
  588.     case -1:
  589.       *received=0;
  590.       break;
  591.     case 0:
  592.       *received=0;
  593.       break;
  594.     case 2:
  595.     case 3:
  596.     case 4:
  597.       maybe_internal(fn, received, NULL, 0, ft, 0, i);
  598.       break;
  599.     case 5:
  600.       if (dn!=-1) {
  601.         if (numbatch>=MAX_BATCH) {
  602.           nl();
  603.           pl(get_string(900));
  604.           nl();
  605.           *received=0;
  606.         } else {
  607.           *received=2;
  608.           strcpy(batch[numbatch].filename,sfn);
  609.           batch[numbatch].dir=dn;
  610.           batch[numbatch].time=0;
  611.           batch[numbatch].sending=0;
  612.           batch[numbatch].len=0;
  613.  
  614.           numbatch++;
  615.           nl();
  616.           pl(get_string(903));
  617.           nl();
  618.           outstr(get_string(906));
  619.           pln(numbatch-numbatchdl);
  620.           nl();
  621.         }
  622.       } else {
  623.         nl();
  624.         pl(get_string(907));
  625.         nl();
  626.       }
  627.       break;
  628.     default:
  629.       if ((i>5) && (incom)) {
  630.         extern_prot(i-6,fn,0);
  631.         *received=exist(fn);
  632.       }
  633.       break;
  634.   }
  635. }
  636.  
  637.  
  638.  
  639. char end_batch1(void)
  640. {
  641.   char b[128],ch;
  642.   int i,i1,done,nerr;
  643.  
  644.   for (i1=0; i1<128; i1++)
  645.     b[i1]=0;
  646.   done=0;
  647.   nerr=0;
  648.   i=0;
  649.   do {
  650.     send_block(b,5,1,0);
  651.     ch=gettimeout(5.0,&i);
  652.     if ((ch==6) || (ch==24))
  653.       done=1;
  654.     else {
  655.       ++nerr;
  656.       if (nerr>=9)
  657.         done=1;
  658.     }
  659.   } while ((!done) && (!hangup) && (!i));
  660.   if (ch==6)
  661.     return(6);
  662.   if (ch==24)
  663.     return(24);
  664.   return(21);
  665. }
  666.  
  667.  
  668. void endbatch(void)
  669. {
  670.   char ch;
  671.   int abort,ucrc,terr,xx1,yy1;
  672.  
  673.   abort=0;
  674.   terr=0;
  675.   xx1=wherex();
  676.   yy1=wherey();
  677.   if (!okstart(&ucrc,&abort))
  678.     abort=1;
  679.   if ((!abort) && (!hangup)) {
  680.     ch=end_batch1();
  681.     if (ch==24)
  682.       abort=1;
  683.     if (ch==21) {
  684.       send_b(0,0L,3,0,&ucrc,"",&terr,&abort);
  685.       abort=1;
  686.     }
  687. /*
  688.     if ((!hangup) && (!abort))
  689.       send_b(0,0L,2,0,&ucrc,"",&terr,&abort);
  690. */
  691.   }
  692.   movecsr(xx1,yy1);
  693. }
  694.  
  695.  
  696.