home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / UTIL / WWIVE / BATCH.C next >
Text File  |  1992-05-22  |  15KB  |  729 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 SETREC(i)  lseek(dlf,((long) (i))*((long)sizeof(uploadsrec)),SEEK_SET);
  26.  
  27.  
  28. void listbatch()
  29. {
  30.   char s[81];
  31.   int abort,i;
  32.  
  33.   abort=0;
  34.   nl();
  35.   if (numbatchdl)
  36.     npr("Files - %d  Time - %s\r\n",numbatch,ctim(batchtime));
  37.   else
  38.     npr("Files - %d\r\n",numbatch);
  39.   nl();
  40.   for (i=0; (i<numbatch) && (!abort) && (!hangup); i++) {
  41.     if (batch[i].sending)
  42.       sprintf(s,"%d. (D) %s   %s  %s",i+1, batch[i].filename,
  43.       ctim(batch[i].time),
  44.       directories[batch[i].dir].name);
  45.     else
  46.       sprintf(s,"%d. (U) %s             %s",i+1,batch[i].filename,
  47.       directories[batch[i].dir].name);
  48.  
  49.     pla(s,&abort);
  50.   }
  51. }
  52.  
  53. void delbatch(int i)
  54. {
  55.   int i1;
  56.  
  57.   if (i<numbatch) {
  58.     if (batch[i].sending) {
  59.       batchtime -= batch[i].time;
  60.       --numbatchdl;
  61.     }
  62.     --numbatch;
  63.     for (i1=i; i1<=numbatch; i1++) {
  64.       batch[i1]=batch[i1+1];
  65.     }
  66.   }
  67. }
  68.  
  69. void downloaded(char *fn)
  70. {
  71.   int i,i1;
  72.   uploadsrec u;
  73.   char s[81];
  74.  
  75.   for (i1=0; i1<numbatch; i1++) {
  76.     if ((strcmp(fn,batch[i1].filename)==0) && (batch[i1].sending)) {
  77.       dliscan1(batch[i1].dir);
  78.       i=recno((batch[i1].filename));
  79.       if (i>0) {
  80.         SETREC(i);
  81.         read(dlf,(void *)&u,sizeof(uploadsrec));
  82.         ++thisuser.downloaded;
  83.         thisuser.dk += (int) ((u.numbytes+1023)/1024);
  84.         ++u.numdloads;
  85.         SETREC(i);
  86.         write(dlf,(void *)&u,sizeof(uploadsrec));
  87.     sprintf(s,"Leeched '%s'",u.filename);
  88.     sysoplog(s);
  89.     if (syscfg.sysconfig & sysconfig_log_dl) {
  90.       sprintf(s,"%s leeched '%s' on %s",
  91.         nam(&thisuser,usernum), u.filename, date());
  92.       ssm(u.ownerusr,0,s);
  93.     }
  94.       }
  95.       closedl();
  96.       delbatch(i1);
  97.  
  98.       return;
  99.     }
  100.   }
  101.   sprintf(s,"!!! Couldn't find '%s' in DL batch queue.",fn);
  102.   sysoplog(s);
  103. }
  104.  
  105. void didnt_upload(int ind)
  106. {
  107.   int i,i1;
  108.   char s[81];
  109.   uploadsrec u;
  110.  
  111.   if (batch[ind].sending)
  112.     return;
  113.  
  114.   dliscan1(batch[ind].dir);
  115.   i=recno(batch[ind].filename);
  116.   if (i>0) {
  117.     do {
  118.       SETREC(i);
  119.       read(dlf, &u, sizeof(uploadsrec));
  120.       if (u.numbytes!=0)
  121.         i=nrecno(batch[ind].filename, i);
  122.     } while ((i!=-1) && (u.numbytes!=0));
  123.  
  124.     if ((i!=-1) && (u.numbytes==0)) {
  125.       if (u.mask & mask_extended)
  126.         delete_extended_description(u.filename);
  127.       for (i1=i; i1<numf; i1++) {
  128.         SETREC(i1+1);
  129.         read(dlf,(void *)&u,sizeof(uploadsrec));
  130.         SETREC(i1);
  131.         write(dlf,(void *)&u,sizeof(uploadsrec));
  132.       }
  133.       --i;
  134.       --numf;
  135.       SETREC(0);
  136.       read(dlf, &u, sizeof(uploadsrec));
  137.       u.numbytes=numf;
  138.       SETREC(0);
  139.       write(dlf,(void *)&u,sizeof(uploadsrec));
  140.       closedl();
  141.       return;
  142.     }
  143.   }
  144.   sprintf(s,"!!! Couldn't find '%s' in transfer area.",batch[ind].filename);
  145.   sysoplog(s);
  146.  
  147.   closedl();
  148. }
  149.  
  150.  
  151. void uploaded(char *fn)
  152. {
  153.   int i,i1, d1, d2, rn;
  154.   uploadsrec u;
  155.   char s[81], s1[81], s2[81];
  156.   char *b;
  157.  
  158.   for (i1=0; i1<numbatch; i1++) {
  159.     if ((strcmp(fn,batch[i1].filename)==0) && (!batch[i1].sending)) {
  160.       dliscan1(batch[i1].dir);
  161.       rn=recno((batch[i1].filename));
  162.       if (rn>0) {
  163.         do {
  164.           SETREC(rn);
  165.           read(dlf, &u, sizeof(uploadsrec));
  166.           if (u.numbytes!=0)
  167.             rn=nrecno(batch[i1].filename, rn);
  168.         } while ((rn!=-1) && (u.numbytes!=0));
  169.  
  170.         if ((rn!=-1) && (u.numbytes==0)) {
  171.  
  172.           sprintf(s1,"%s%s",syscfg.batchdir, fn);
  173.           sprintf(s2,"%s%s", directories[batch[i1].dir].path, fn);
  174.  
  175.           if ((strcmp(s1,s2)!=0) && (exist(s1))) {
  176.             d2=0;
  177.             if ((s1[1]!=':') && (s2[1]!=':'))
  178.               d2=1;
  179.             if ((s1[1]==':') && (s2[1]==':') && (s1[0]==s2[0]))
  180.               d2=1;
  181.             if (d2) {
  182.               rename(s1,s2);
  183.               unlink(s1);
  184.             } else {
  185.               if ((b=malloca(16400))==NULL)
  186.                 return;
  187.               d1=open(s1,O_RDONLY | O_BINARY);
  188.               d2=open(s2,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  189.               i=read(d1,(void *)b,16384);
  190.               while (i>0) {
  191.                 write(d2,(void *)b,i);
  192.                 i=read(d1,(void *)b,16384);
  193.               }
  194.               close(d1);
  195.               close(d2);
  196.               unlink(s1);
  197.               farfree(b);
  198.             }
  199.           }
  200.  
  201.           d1=open(s2,O_RDONLY | O_BINARY);
  202.           if (d1>0) {
  203.             if (syscfg.upload_c[0]) {
  204.               close(d1);
  205.               if (check_ul_event(batch[i1].dir, &u)) {
  206.                 closedl();
  207.                 didnt_upload(i1);
  208.                 delbatch(i1);
  209.                 d1=-1;
  210.               } else {
  211.                 d1=open(s2,O_RDONLY | O_BINARY);
  212.               }
  213.             }
  214.             if (d1>=0) {
  215.               u.numbytes = filelength(d1);
  216.               close(d1);
  217.  
  218.               ++thisuser.uploaded;
  219.               thisuser.uk += (int) ((u.numbytes+1023)/1024);
  220.               ++status.uptoday;
  221.               save_status();
  222.               SETREC(rn);
  223.               write(dlf,(void *)&u,sizeof(uploadsrec));
  224.               sprintf(s,"+%s uploaded on %s",
  225.                 u.filename,
  226.                 directories[batch[i1].dir].name);
  227.               sysoplog(s);
  228.               npr("Uploaded '%s' to %s\r\n",u.filename,
  229.                 directories[batch[i1].dir].name);
  230.             }
  231.           }
  232.           closedl();
  233.           delbatch(i1);
  234.           return;
  235.         }
  236.       }
  237.       sprintf("!!! Couldn't find file '%s' in directory.", fn);
  238.       sysoplog(s);
  239.       npr("Couldn't find data for file '%s'; deleting\r\n",fn);
  240.       closedl();
  241.       didnt_upload(i1);
  242.       delbatch(i1);
  243.  
  244.       return;
  245.     }
  246.   }
  247.   sprintf(s,"!!! Couldn't find '%s' in UL batch queue.",fn);
  248.   sysoplog(s);
  249.   npr("Don't know what to do with file '%s'; deleting\r\n",fn);
  250.  
  251.   sprintf(s,"%s%s",syscfg.batchdir, fn);
  252.   unlink(s);
  253. }
  254.  
  255. void ymbatchdl(int had)
  256. {
  257.   int rr,i,ok,cur=0;
  258.   char s[81];
  259.   uploadsrec u;
  260.   double percent;
  261.  
  262.   if (!incom)
  263.     return;
  264.   sprintf(s,"Ymodem BATCH DL, %d files, time=%s",numbatchdl, ctim(batchtime));
  265.   if (had)
  266.     strcat(s,", HAD");
  267.   sysoplog(s);
  268.   nl();
  269.   pl(s);
  270.   nl();
  271.  
  272.   rr=0;
  273.   do {
  274.     tleft(1);
  275.     if ((syscfg.req_ratio>0.0001) && (ratio()<syscfg.req_ratio))
  276.       rr=1;
  277.     if (thisuser.exempt & exempt_ratio)
  278.       rr=0;
  279.     if (!batch[cur].sending) {
  280.       rr=0;
  281.       ++cur;
  282.     }
  283.     if ((nsl()>=batch[cur].time) && (!rr)) {
  284.       dliscan1(batch[cur].dir);
  285.       i=recno(batch[cur].filename);
  286.       if (i<=0) {
  287.         delbatch(cur);
  288.         closedl();
  289.       } else {
  290.     sprintf(s,"%d files left, Time left = %s\r\n",numbatchdl,ctim(batchtime));
  291.     outs(s);
  292.     SETREC(i);
  293.     read(dlf,(void *)&u,sizeof(uploadsrec));
  294.     closedl();
  295.     sprintf(s,"%s%s",directories[batch[cur].dir].path,u.filename);
  296.     xymodem_send(s,&ok,&percent,u.filetype,1,1,1);
  297.     if (ok) {
  298.       downloaded(u.filename);
  299.     } else {
  300.       closedl();
  301.     }
  302.       }
  303.     } else
  304.       delbatch(cur);
  305.   } while ((ok) && (!hangup) && (numbatch>cur) && (!rr));
  306.   if ((ok) && (!hangup))
  307.     endbatch();
  308.   if (rr) {
  309.     nl();
  310.     pl("6Your ratio is too low to continue the transfer.");
  311.     nl();
  312.   }
  313.   if (had) {
  314.     dtr(0);
  315.     hangup=1;
  316.   }
  317. }
  318.  
  319.  
  320. void handle_dszline(char *l)
  321. {
  322.   char *ss;
  323.   int i;
  324.   char s[161];
  325.  
  326.   /* find the filename */
  327.   ss=strtok(l," \t");
  328.   for (i=0; (i<10) && (ss); i++)
  329.     ss=strtok(NULL," \t");
  330.  
  331.   if (ss) {
  332.     strcpy(s,stripfn(ss));
  333.     align(s);
  334.  
  335.     switch(*l) {
  336.       case 'Z':
  337.       case 'S':
  338.       case 'R':
  339.       case 'B':
  340.         /* received a file */
  341.         uploaded(s);
  342.         break;
  343.  
  344.       case 'z':
  345.       case 's':
  346.       case 'r':
  347.       case 'b':
  348.         /* sent a file */
  349.         downloaded(s);
  350.         break;
  351.  
  352.       case 'E':
  353.       case 'L':
  354.       case 'U':
  355.         /* error */
  356.         sprintf(s,"Error transferring '%s'",ss);
  357.         sysoplog(s);
  358.         break;
  359.     }
  360.   }
  361. }
  362.  
  363.  
  364.  
  365. double ratio1(long a)
  366. {
  367.   double r;
  368.  
  369.   if ((thisuser.dk==0) && (a==0))
  370.     return(99.999);
  371.   r=((float) thisuser.uk) / ((float) (thisuser.dk + a));
  372.   if (r>99.998)
  373.     r=99.998;
  374.   return(r);
  375. }
  376.  
  377. void make_ul_batch_list(char *listfn)
  378. {
  379.   int f,i;
  380.   char s[255];
  381.  
  382.   sprintf(listfn,"%s\\FILES.UL",cdir);
  383.  
  384.   _chmod(listfn,1,0);
  385.   unlink(listfn);
  386.  
  387.   f=open(listfn,O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  388.   if (f<0) {
  389.     listfn[0]=0;
  390.     return;
  391.   }
  392.  
  393.   for (i=0; i<numbatch; i++) {
  394.     if (!batch[i].sending) {
  395.       sprintf(s,"%s%s\r\n",directories[batch[i].dir].path,stripfn(batch[i].filename));
  396.       write(f,s,strlen(s));
  397.     }
  398.   }
  399.   close(f);
  400. }
  401.  
  402. void make_dl_batch_list(char *listfn)
  403. {
  404.   char s[255];
  405.   int i, f, ok;
  406.   double at=0.0;
  407.   long addk=0,thisk;
  408.  
  409.   sprintf(listfn,"%s\\FILES.DL",cdir);
  410.  
  411.   _chmod(listfn,1,0);
  412.   unlink(listfn);
  413.  
  414.   f=open(listfn,O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  415.   if (f<0) {
  416.     listfn[0]=0;
  417.     return;
  418.   }
  419.  
  420.   for (i=0; i<numbatch; i++) {
  421.     if (batch[i].sending) {
  422.       sprintf(s,"%s%s\r\n",directories[batch[i].dir].path,stripfn(batch[i].filename));
  423.       ok=1;
  424.       if (nsl() < (batch[i].time + at))
  425.         ok=0;
  426.       thisk=(batch[i].len+1023)/1024;
  427.       if ((syscfg.req_ratio>0.0001) && (ratio1(addk+thisk)<syscfg.req_ratio) &&
  428.           (!(thisuser.exempt & exempt_ratio)))
  429.         ok=0;
  430.       if (ok) {
  431.         write(f,s,strlen(s));
  432.         at += batch[i].time;
  433.         addk += thisk;
  434.       }
  435.     }
  436.   }
  437.   close(f);
  438. }
  439.  
  440.  
  441. void run_cmd(char *cmdln, char *downlist, char *uplist, char *dl, int had)
  442. {
  443.   char sx1[21], sx2[21], sx3[21], s[161];
  444.  
  445.   ultoa(com_speed,sx1,10);
  446.   ultoa(modem_speed,sx3,10);
  447.   sx2[0]='0'+syscfg.primaryport;
  448.   sx2[1]=0;
  449.   stuff_in(s,cmdln,sx1,sx2,downlist,sx3,uplist);
  450.   if (s[0]) {
  451.     make_abs_cmd(s);
  452.     clrscrb();
  453.     outs(dl);
  454.     outs("\r\n");
  455.     outs(s);
  456.     outs("\r\n");
  457.     if (incom) {
  458.       _chmod(dszlog,1,0);
  459.       unlink(dszlog);
  460.  
  461.       if (uplist[0])
  462.         cd_to(syscfg.batchdir);
  463.  
  464.       run_external1(s);
  465.  
  466.       cd_to(cdir);
  467.  
  468.       if (had) {
  469.         dtr(0);
  470.         hangup=1;
  471.         wait1(2);
  472.         if (!cdet()) {
  473.           dtr(1);
  474.           wait1(2);
  475.           holdphone(1);
  476.         }
  477.       } else {
  478.         nl();
  479.     pl("3Please wait5...");
  480.     nl();
  481.       }
  482.  
  483.       process_dszlog();
  484.       _chmod(dszlog,1,0);
  485.       unlink(dszlog);
  486.  
  487.       topscreen();
  488.     }
  489.   }
  490.  
  491.   if (downlist[0]) {
  492.     _chmod(downlist,1,0);
  493.     unlink(downlist);
  494.   }
  495.   if (uplist[0]) {
  496.     _chmod(uplist,1,0);
  497.     unlink(uplist);
  498.   }
  499. }
  500.  
  501.  
  502. void process_dszlog()
  503. {
  504.   int f,i,i1;
  505.   char *ss;
  506.   char *lines[100];
  507.  
  508.   f=open(dszlog,O_RDONLY | O_TEXT);
  509.   if (f>0) {
  510.     i1=(int)filelength(f);
  511.     ss=malloca(i1);
  512.     if (ss) {
  513.       i=read(f,ss,i1);
  514.       if (i>0) {
  515.         ss[i]=0;
  516.         lines[0]=strtok(ss,"\n");
  517.         for (i=1; (i<90) && (lines[i-1]); i++)
  518.           lines[i]=strtok(NULL,"\n");
  519.  
  520.         lines[99]=NULL;
  521.  
  522.         for (i1=0; lines[i1]; i1++) {
  523.           handle_dszline(lines[i1]);
  524.         }
  525.  
  526.       }
  527.       farfree(ss);
  528.     }
  529.     close(f);
  530.   }
  531.   _chmod(dszlog,1,0);
  532.   unlink(dszlog);
  533. }
  534.  
  535.  
  536. void dszbatchdl(int had, char *cmdln, char *desc)
  537. {
  538.   char listfn[81],dl[100];
  539.  
  540.  
  541.   sprintf(dl,"%s BATCH DL, %d files, time=%s",
  542.           desc, numbatchdl, ctim(batchtime));
  543.   if (had)
  544.     strcat(dl,", HAD");
  545.   sysoplog(dl);
  546.   nl();
  547.   pl(dl);
  548.   nl();
  549.  
  550.   make_dl_batch_list(listfn);
  551.  
  552.   run_cmd(cmdln, listfn, "", dl, had);
  553. }
  554.  
  555.  
  556. void dszbatchul(int had, char *cmdln, char *desc)
  557. {
  558.   char listfn[81],dl[100];
  559.   int i,i1,f,ok;
  560.  
  561.   sprintf(dl,"%s BATCH UL, %d files", desc, numbatch-numbatchdl);
  562.   if (had)
  563.     strcat(dl,", HAD");
  564.   sysoplog(dl);
  565.   nl();
  566.   pl(dl);
  567.   nl();
  568.  
  569.   make_ul_batch_list(listfn);
  570.  
  571.   run_cmd(cmdln, "", listfn, dl, had);
  572.  
  573. }
  574.  
  575. /****************************************************************************/
  576. /*
  577.  * Bimodem batch has not yet been implemented.
  578.  */
  579.  
  580. #pragma warn -par
  581.  
  582. void bibatch(int had, int pn)
  583. {
  584. }
  585.  
  586. #pragma warn +par
  587.  
  588.  
  589. /****************************************************************************/
  590.  
  591.  
  592. void batchdl()
  593. {
  594.   int i,abort,done,i1,i2,had,dsz;
  595.   char s[81],s1[81],ch,ch1;
  596.  
  597.   done=0;
  598.   if (numbatch==0) {
  599.     nl();
  600.     pl("6No files in queue.");
  601.     nl();
  602.     return;
  603.   }
  604.   do {
  605.     nl();
  606.     prt(7,"[1Batch7]3:7 L1,7R1,7Q1,7C1,7D1,7U1,7B1,7?3 : ");
  607.     ch=onek("Q?CLRDUB");
  608.     switch(ch) {
  609.       case '?':
  610.         printmenu(9);
  611.         break;
  612.       case 'Q':
  613.         done=1;
  614.         break;
  615.       case 'L':
  616.         listbatch();
  617.         break;
  618.       case 'R':
  619.         nl();
  620.     prt(1,"Remove which5? ");
  621.     input(s,2);
  622.     i=atoi(s);
  623.     if ((i>0) && (i<=numbatch)) {
  624.       didnt_upload(i-1);
  625.       delbatch(i-1);
  626.     }
  627.     if (numbatch==0) {
  628.       nl();
  629.       pl("1Batch queue empty5.");
  630.       nl();
  631.       done=1;
  632.     }
  633.     break;
  634.       case 'C':
  635.     prt(5,"Clear queue? ");
  636.     if (yn()) {
  637.       for (i=0; i<numbatch; i++)
  638.         didnt_upload(i);
  639.       numbatch=0;
  640.       numbatchdl=0;
  641.       batchtime=0.0;
  642.       done=1;
  643.       pl("1Queue cleared5.");
  644.     }
  645.     break;
  646.       case 'U':
  647.         if (numbatchdl==numbatch) {
  648.           nl();
  649.       pl("6Nothing in batch upload queue.");
  650.       nl();
  651.       break;
  652.     }
  653.     nl();
  654.     prt(3,"Do you wanna hang up after transfer5? ");
  655.     had=yn();
  656.     nl();
  657.  
  658.         i=get_protocol(xf_up_batch);
  659.         if (i>0) {
  660.  
  661.           dszbatchul(had, externs[i-6].receivebatchfn, externs[i-6].description);
  662.  
  663.           if (!had) {
  664.             nl();
  665.         /*npr("1Your ratio is now3:6 %-6.3f\r\n",ratio());*/
  666.       }
  667.       done=1;
  668.     }
  669.     break;
  670.       case 'B':
  671.         nl();
  672.     prt(3,"Do you wanna hang up after transfer5? ");
  673.     had=yn();
  674.     nl();
  675.  
  676.         i=get_protocol(xf_bi);
  677.         if (i>0) {
  678.  
  679.           bibatch(had, i);
  680.  
  681.           if (!had) {
  682.             nl();
  683.        /* npr("1Your ratio is now3:6 %-6.3f\r\n",ratio());*/
  684.       }
  685.       done=1;
  686.     }
  687.     break;
  688.       case 'D':
  689.         if (numbatchdl==0) {
  690.           nl();
  691.       pl("3Nothing in batch download queue5.");
  692.       nl();
  693.       break;
  694.     }
  695.  
  696.         nl();
  697.         if (!ratio_ok()) {
  698.           nl();
  699.       pl("6Sorry, your ratio is too low.");
  700.       nl();
  701.       break;
  702.     }
  703.  
  704.         nl();
  705.     prt(3,"Do you wanna hang up after transfer5? ");
  706.     had=yn();
  707.     nl();
  708.  
  709.         i=get_protocol(xf_down_batch);
  710.         if (i>0) {
  711.  
  712.           if (i==4)
  713.             ymbatchdl(had);
  714.           else
  715.             dszbatchdl(had,externs[i-6].sendbatchfn, externs[i-6].description);
  716.  
  717.           if (!had) {
  718.             nl();
  719.        /* npr("1Your ratio is now3:6 %-6.3f\r\n",ratio());*/
  720.       }
  721.       done=1;
  722.     }
  723.     break;
  724.     }
  725.   } while ((!done) && (!hangup));
  726. }
  727.  
  728.  
  729.