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