home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / WWIV2.ZIP / XFERTMP.C < prev    next >
C/C++ Source or Header  |  1992-12-03  |  16KB  |  738 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 <dir.h>
  22.  
  23.  
  24. #define SETREC(i)  lseek(dlf,((long) (i))*((long)sizeof(uploadsrec)),SEEK_SET);
  25.  
  26. /* the archive type to use */
  27. #define ARC_NUMBER 0
  28.  
  29.  
  30. /* strings not to allow in a .zip file to extract from */
  31. char *bad_words[] = {
  32.   "COMMAND",
  33.   "..",
  34.   NULL,
  35. };
  36.  
  37. long bad_filename(char *fn)
  38. {
  39.   int i;
  40.  
  41.   strupr(fn);
  42.   for (i=0; bad_words[i]; i++) {
  43.     if (strstr(fn,bad_words[i])) {
  44.       outstr(get_string(840));
  45.       pl(fn);
  46.       return(1);
  47.     }
  48.   }
  49.   if (!okfn(fn)) {
  50.     outstr(get_string(840));
  51.     pl(fn);
  52.     return(1);
  53.   }
  54.   return(0);
  55. }
  56.  
  57. /****************************************************************************/
  58.  
  59. typedef struct {
  60.   unsigned char type;
  61.   char name[13];
  62.   long len;
  63.   int date,time,crc;
  64.   long size;
  65. } arch;
  66.  
  67. int check_for_files_arc(char *fn)
  68. {
  69.   int f;
  70.   char s[20];
  71.   arch a;
  72.   long l1,l;
  73.  
  74.   f=open(fn,O_RDONLY | O_BINARY);
  75.   if (f>0) {
  76.     l1=filelength(f);
  77.     l=1;
  78.     lseek(f,0L,SEEK_SET);
  79.     read(f,&a,1);
  80.     if (a.type!=26) {
  81.       close(f);
  82.       outstr(stripfn(fn));
  83.       pl(get_string(841));
  84.       return(1);
  85.     }
  86.     while (l<l1) {
  87.       lseek(f,l,SEEK_SET);
  88.       if ((read(f,&a,sizeof(arch)))==sizeof(arch)) {
  89.     l += sizeof(arch);
  90.     if (a.type==1) {
  91.       l-=4;
  92.       a.size=a.len;
  93.     }
  94.     if (a.type) {
  95.       l += a.len;
  96.       ++l;
  97.       strncpy(s,a.name,13);
  98.       s[13]=0;
  99.       if (bad_filename(s)) {
  100.         close(f);
  101.         return(1);
  102.       }
  103.     } else
  104.       l=l1;
  105.       } else {
  106.     close(f);
  107.         if (a.type!=0) {
  108.           outstr(stripfn(fn));
  109.           outstr(get_string(841));
  110.           return(1);
  111.         } else
  112.           l=l1;
  113.       }
  114.     }
  115.     
  116.     close(f);
  117.     return(0);
  118.   }
  119.   outstr(get_string(842));
  120.   pl(stripfn(fn));
  121.   return(1);
  122. }
  123.  
  124.  
  125. /****************************************************************************/
  126.  
  127. /* .ZIP structures and defines */
  128. #define ZIP_LOCAL_SIG 0x04034b50
  129. #define ZIP_CENT_START_SIG 0x02014b50
  130. #define ZIP_CENT_END_SIG 0x06054b50
  131.  
  132. typedef struct {
  133.   unsigned long         signature;    /* 0x04034b50 */
  134.   unsigned short        extract_ver;
  135.   unsigned short        flags;
  136.   unsigned short        comp_meth;
  137.   unsigned short        mod_time;
  138.   unsigned short        mod_date;
  139.   unsigned long         crc_32;
  140.   unsigned long         comp_size;
  141.   unsigned long         uncomp_size;
  142.   unsigned short        filename_len;
  143.   unsigned short        extra_length;
  144. } zip_local_header;
  145.  
  146. typedef struct {
  147.   unsigned long         signature;    /* 0x02014b50 */
  148.   unsigned short        made_ver;
  149.   unsigned short        extract_ver;
  150.   unsigned short        flags;
  151.   unsigned short        comp_meth;
  152.   unsigned short        mod_time;
  153.   unsigned short        mod_date;
  154.   unsigned long         crc_32;
  155.   unsigned long         comp_size;
  156.   unsigned long         uncomp_size;
  157.   unsigned short        filename_len;
  158.   unsigned short        extra_len;
  159.   unsigned short        comment_len;
  160.   unsigned short        disk_start;
  161.   unsigned short        int_attr;
  162.   unsigned long         ext_attr;
  163.   unsigned long         rel_ofs_header;
  164. } zip_central_dir;
  165.  
  166.  
  167. typedef struct {
  168.   unsigned long         signature;    /* 0x06054b50 */
  169.   unsigned short        disk_num;
  170.   unsigned short        cent_dir_disk_num;
  171.   unsigned short        total_entries_this_disk;
  172.   unsigned short        total_entries_total;
  173.   unsigned long         central_dir_size;
  174.   unsigned long         ofs_cent_dir;
  175.   unsigned short        comment_len;
  176. } zip_end_dir;
  177.  
  178.  
  179. int check_for_files_zip(char *fn)
  180. {
  181.   int f;
  182.   long l,sig,len;
  183.   zip_local_header zl;
  184.   zip_central_dir zc;
  185.   zip_end_dir ze;
  186.   char s[161];
  187.  
  188. #define READ_FN(ln) {read(f,s,ln); s[ln]=0;}
  189.  
  190.   f=open(fn,O_RDONLY | O_BINARY);
  191.   if (f>0) {
  192.     l=0;
  193.     len=filelength(f);
  194.     while (l<len) {
  195.       lseek(f,l,SEEK_SET);
  196.       read(f,&sig, 4);
  197.       lseek(f,l,SEEK_SET);
  198.       switch(sig) {
  199.         case ZIP_LOCAL_SIG:
  200.           read(f,&zl, sizeof(zl));
  201.           READ_FN(zl.filename_len);
  202.           if (bad_filename(s)) {
  203.             close(f);
  204.             return(1);
  205.           }
  206.           l += sizeof(zl);
  207.           l += zl.comp_size + zl.filename_len + zl.extra_length;
  208.           break;
  209.         case ZIP_CENT_START_SIG:
  210.           read(f,&zc, sizeof(zc));
  211.           READ_FN(zc.filename_len);
  212.           if (bad_filename(s)) {
  213.             close(f);
  214.             return(1);
  215.           }
  216.           l += sizeof(zc);
  217.           l += zc.filename_len + zc.extra_len;
  218.           break;
  219.         case ZIP_CENT_END_SIG:
  220.           read(f,&ze, sizeof(ze));
  221.           close(f);
  222.           return(0);
  223.         default:
  224.           close(f);
  225.           pl(get_string(843));
  226.           return(1);
  227.       }
  228.     }
  229.     close(f);
  230.     return(0);
  231.   }
  232.  
  233.   outstr(get_string(842));
  234.   pl(stripfn(fn));
  235.   return(1);
  236. }
  237.  
  238. /****************************************************************************/
  239.  
  240. typedef struct {
  241.   unsigned char   checksum;
  242.   char            ctype[5];
  243.   long            comp_size;
  244.   long            uncomp_size;
  245.   unsigned short  time;
  246.   unsigned short  date;
  247.   unsigned short  attr;
  248.   unsigned char   fn_len;
  249. } lharc_header;
  250.  
  251.  
  252.  
  253. int check_for_files_lzh(char *fn)
  254. {
  255.   long l1,l;
  256.   int f,err=0;
  257.   unsigned short crc;
  258.   char s[256];
  259.   char flag;
  260.   lharc_header a;
  261.  
  262.   if ((f=open(fn,O_RDONLY|O_BINARY))<0) {
  263.     outstr(get_string(842));
  264.     pl(stripfn(fn));
  265.     return(1);
  266.   }
  267.   l1=filelength(f);
  268.   for(l=0;l<l1;l+=a.fn_len+a.comp_size+sizeof(lharc_header)+read(f,&crc,sizeof(crc))+1) {
  269.     lseek(f,l,SEEK_SET);
  270.     read(f,&flag,1);
  271.     if (!flag) {
  272.       l=l1;
  273.       break;
  274.     }
  275.     if ((read(f,&a,sizeof(lharc_header)))!=sizeof(lharc_header)) {
  276.       outstr(stripfn(fn));
  277.       pl(get_string(844));
  278.       err=1;
  279.       break;
  280.     }
  281.     if (read(f,s,a.fn_len)!=a.fn_len) {
  282.       outstr(stripfn(fn));
  283.       pl(get_string(844));
  284.       err=1;
  285.       break;
  286.     }
  287.     s[a.fn_len]=0;
  288.     if (bad_filename(s)) {
  289.       err=1;
  290.       break;
  291.     }
  292.   }
  293.   close(f);
  294.   return(err);
  295. }
  296. /****************************************************************************/
  297.  
  298. int check_for_files_arj(char *fn)
  299. {
  300.   int f,i;
  301.   char s[256];
  302.   long l1,l,l2;
  303.   unsigned short sh;
  304.   unsigned char s1;
  305.  
  306.  
  307.   f=open(fn,O_RDONLY | O_BINARY);
  308.   if (f>0) {
  309.     l1=filelength(f);
  310.     l=0;
  311.     lseek(f,0L,SEEK_SET);
  312.     while (l<l1) {
  313.       lseek(f,l,SEEK_SET);
  314.       i=read(f,&sh,2);
  315.       if ((i!=2) || (sh!=0xea60)) {
  316.         close(f);
  317.         outstr(stripfn(fn));
  318.         outstr(get_string(845));
  319.         return(1);
  320.       }
  321.       l+=i+2;
  322.       read(f,&sh,2);
  323.       read(f,&s1,1);
  324.       lseek(f,l+12,SEEK_SET);
  325.       read(f,&l2,4);
  326.       lseek(f,l+(long)s1,SEEK_SET);
  327.       read(f,s,250);
  328.       s[250]=0;
  329.       if (strlen(s)>240) {
  330.         close(f);
  331.         outstr(stripfn(fn));
  332.         outstr(get_string(845));
  333.         return(1);
  334.       }
  335.       l += 4+(long) sh;
  336.       lseek(f,l,SEEK_SET);
  337.       read(f,&sh,2);
  338.       l += 2;
  339.       while ((l<l1) && (sh)) {
  340.         l += 6+(long) sh;
  341.         lseek(f,l-2,SEEK_SET);
  342.         read(f,&sh,2);
  343.       }
  344.       l += l2;
  345.       if (bad_filename(s)) {
  346.         close(f);
  347.         return(1);
  348.       }
  349.     }
  350.     
  351.     close(f);
  352.     return(0);
  353.   }
  354.   outstr(get_string(842));
  355.   pl(stripfn(fn));
  356.   return(1);
  357. }
  358.  
  359.  
  360.  
  361. /****************************************************************************/
  362.  
  363.  
  364. typedef struct {
  365.   char *arc_name;
  366.   int (*func)(char *);
  367. } arc_testers;
  368.  
  369. arc_testers arc_t[] = {
  370.   {"ZIP", check_for_files_zip},
  371.   {"ARC", check_for_files_arc},
  372.   {"LZH", check_for_files_lzh},
  373.   {"ARJ", check_for_files_arj},
  374.   {NULL, NULL},
  375. };
  376.  
  377.  
  378.  
  379. int check_for_files(char *fn)
  380. {
  381.   char *ss;
  382.   int i;
  383.  
  384.   ss=strrchr(fn,'.');
  385.   if (ss) {
  386.     ss++;
  387.     for (i=0; arc_t[i].arc_name; i++) {
  388.       if (stricmp(ss,arc_t[i].arc_name)==NULL) {
  389.         return(arc_t[i].func(fn));
  390.       }
  391.     }
  392.   } else {
  393.     /* no extension? */
  394.     pl(get_string(846));
  395.     return(1);
  396.   }
  397.   return(0);
  398. }
  399.  
  400.  
  401. void download_temp_arc(char *fn, int xfer)
  402. {
  403.   char s[81],s1[81];
  404.   long numbytes;
  405.   double d;
  406.   int i,f,sent,abort;
  407.  
  408.   outstr(get_string(847));
  409.   npr("%s.%s:\r\n\r\n", fn, syscfg.arcs[ARC_NUMBER].extension);
  410.   if (xfer && !ratio_ok()) {
  411.     pl(get_string(848));
  412.     return;
  413.   }
  414.   sprintf(s1,"%s%s.%s",syscfg.tempdir,fn, syscfg.arcs[ARC_NUMBER].extension);
  415.   f=open(s1,O_RDONLY | O_BINARY);
  416.   if (f<0) {
  417.     pl(get_string(849));
  418.     nl();
  419.     return;
  420.   }
  421.   numbytes=filelength(f);
  422.   close(f);
  423.   if (numbytes==0L) {
  424.     pl(get_string(850));
  425.     nl();
  426.     return;
  427.   }
  428.   d=((double) (((numbytes)+127)/128)) *
  429.     (1620.0) /
  430.     ((double) (modem_speed));
  431.   if (d<=nsl()) {
  432.     outstr(get_string(851));
  433.     pl(ctim(d));
  434.     sent=0;
  435.     abort=0;
  436.     sprintf(s,"%s.%s",fn,syscfg.arcs[ARC_NUMBER].extension);
  437.     send_file(s1,&sent,&abort,0,s,-1,numbytes);
  438.     if (sent) {
  439.       if (xfer) {
  440.         ++thisuser.downloaded;
  441.         thisuser.dk += ((numbytes+1023)/1024);
  442.         nl();
  443.         nl();
  444.         outstr(get_string(782)); npr("%-6.3f\r\n",ratio());
  445.       }
  446.       sprintf(s1,get_stringx(1,45),(numbytes+1023)/1024, s);
  447.       sysoplog(s1);
  448.       if (useron)
  449.         topscreen();
  450.     }
  451.   } else {
  452.     nl();
  453.     nl();
  454.     pl(get_string(787));
  455.     nl();
  456.   }
  457. }
  458.  
  459. void add_arc(char *arc, char *fn, int dos)
  460. {
  461.   char s[255], s1[81];
  462.  
  463.   sprintf(s1,"%s.%s",arc, syscfg.arcs[ARC_NUMBER].extension);
  464.   get_arc_cmd(s,s1,2,fn);
  465.   if (s[0]) {
  466.     cd_to(syscfg.tempdir);
  467.     outs(s);
  468.     outs("\r\n");
  469.     if (dos) {
  470.       do_external(s,0);
  471.     } else {
  472.       set_protect(0);
  473.       do_remote(s, 1);
  474.       topscreen();
  475.     }
  476.     cd_to(cdir);
  477.     sprintf(s,get_stringx(1,46),fn, s1);
  478.     sysoplog(s);
  479.   } else
  480.     pl(get_string(852));
  481. }
  482.  
  483. void add_temp_arc(void)
  484. {
  485.   char s[81],s2[81];
  486.   int i;
  487.  
  488.   nl();
  489.   pl(get_string(853));
  490.   pl(get_string(854));
  491.   prt(2,": ");
  492.   input(s,12);
  493.   if (!okfn(s))
  494.     return;
  495.   if (s[0]==0)
  496.     return;
  497.   if (strchr(s,'.')==NULL)
  498.     strcat(s,".*");
  499.   strcpy(s2,stripfn(s));
  500.   for (i=0; i<strlen(s2); i++)
  501.     if ((s2[i]=='|') || (s2[i]=='>') || (s2[i]=='<') || (s2[i]==';') || (s2[i]==' ') ||
  502.         (s2[i]==':') || (s2[i]=='/') || (s2[i]=='\\'))
  503.       return;
  504.   add_arc("TEMP", s2, 1);
  505. }
  506.  
  507. void del_temp(void)
  508. {
  509.   char s[81],s1[81];
  510.   nl();
  511.   prt(2,get_string(855));
  512.   input(s1,12);
  513.   if (!okfn(s1))
  514.     return;
  515.   if (s1[0]) {
  516.     if (strchr(s1,'.')==NULL)
  517.       strcat(s1,".*");
  518.     remove_from_temp(s1,syscfg.tempdir, 1);
  519.   }
  520. }
  521.  
  522. void list_temp_dir(void)
  523. {
  524.   int i,i1,f1,abort;
  525.   char s[81],s1[81];
  526.   struct ffblk ff;
  527.   uploadsrec u;
  528.  
  529.   sprintf(s1,"%s*.*",syscfg.tempdir);
  530.   f1=findfirst(s1,&ff,0);
  531.   nl();
  532.   pl(get_string(856));
  533.   nl();
  534.   i1=0;
  535.   abort=0;
  536.   while ((f1==0) && (!hangup) && (!abort)) {
  537.     strcpy(s,(ff.ff_name));
  538.     align(s);
  539.     sprintf(s1,"%12s  %-8ld",s,ff.ff_fsize);
  540.     pla(s1, &abort);
  541.     f1=findnext(&ff);
  542.     i1=1;
  543.   }
  544.   if (!i1)
  545.     pl(get_string(5));
  546.   nl();
  547.   if ((!abort) && (!hangup)) {
  548.     npr("%s: %ldk.\r\n", get_string(857),(long) freek1(syscfg.tempdir));
  549.     nl();
  550.   }
  551. }
  552.  
  553.  
  554. void temp_extract(void)
  555. {
  556.   int i,i1,i2,i3,ok,abort,ok1;
  557.   char s[255],s1[255],s2[81],s3[255],s4[129];
  558.   uploadsrec u,u1;
  559.  
  560.   dliscan();
  561.   nl();
  562.   pl(get_string(858));
  563.   nl();
  564.   prt(2,get_string(44));
  565.   input(s,12);
  566.   if ((!okfn(s)) || (s[0]==0)) {
  567.     closedl();
  568.     return;
  569.   }
  570.   if (strchr(s,'.')==NULL)
  571.     strcat(s,".*");
  572.   align(s);
  573.   i=recno(s);
  574.   ok=1;
  575.   while ((i>0) && (ok) && (!hangup)) {
  576.     SETREC(i);
  577.     read(dlf,(void *)&u,sizeof(uploadsrec));
  578.     sprintf(s2,"%s%s",directories[udir[curdir].subnum].path,u.filename);
  579.     get_arc_cmd(s1,s2,1,"");
  580.     if ((s1[0]) && (exist(s2))) {
  581.       nl();
  582.       nl();
  583.       abort=0;
  584.       printinfo(&u,&abort);
  585.       nl();
  586.       cd_to(directories[udir[curdir].subnum].path);
  587.       get_dir(s4,1);
  588.       strcat(s4,stripfn(u.filename));
  589.       cd_to(cdir);
  590.       if (!check_for_files(s4)) {
  591.         do {
  592.           prt(2,get_string(859));
  593.           input(s1,12);
  594.           if (s1[0]==0)
  595.             ok1=0;
  596.           else
  597.             ok1=1;
  598.           if (!okfn(s1))
  599.             ok1=0;
  600.           if (strcmp(s1,"?")==0) {
  601.             list_arc_out(stripfn(u.filename),directories[udir[curdir].subnum].path);
  602.             s1[0]=0;
  603.           }
  604.           if (strcmp(s1,"Q")==0) {
  605.             ok=0;
  606.             s1[0]=0;
  607.           }
  608.           i2=0;
  609.           for (i1=0; i1<strlen(s1); i1++)
  610.             if ((s1[i1]=='|') || (s1[i1]=='>') || (s1[i1]=='<') || (s1[i1]==';') || (s1[i1]==' '))
  611.               i2=1;
  612.           if (i2)
  613.             s1[0]=0;
  614.           if (s1[0]) {
  615.             if (strchr(s1,'.')==NULL)
  616.               strcat(s1,".*");
  617.             get_arc_cmd(s3,s4,1,stripfn(s1));
  618.             cd_to(syscfg.tempdir);
  619.             if (!okfn(s1))
  620.               s3[0]=0;
  621.             if (s3[0]) {
  622.               do_external(s3,0);
  623.               sprintf(s2,get_stringx(1,47),s1,u.filename);
  624.             } else
  625.               s2[0]=0;
  626.             cd_to(cdir);
  627.             if (s2[0])
  628.               sysoplog(s2);
  629.           }
  630.         } while ((!hangup) && (ok) && (ok1));
  631.       }
  632.     } else
  633.       if (s1[0]) {
  634.         nl();
  635.         pl(get_string(860));
  636.         nl();
  637.       }
  638.     if (ok)
  639.       i=nrecno(s,i);
  640.   }
  641.   closedl();
  642. }
  643.  
  644. void list_temp_text(void)
  645. {
  646.   int i,i1,f1,ok,sent;
  647.   char s[81],s1[81];
  648.   struct ffblk ff;
  649.   uploadsrec u;
  650.   double percent;
  651.  
  652.   nl();
  653.   prt(2,get_string(861));
  654.   input(s,12);
  655.   if (!okfn(s))
  656.     return;
  657.   if (s[0]) {
  658.     if (strchr(s,'.')==NULL)
  659.       strcat(s,".*");
  660.     sprintf(s1,"%s%s",syscfg.tempdir,stripfn(s));
  661.     f1=findfirst(s1,&ff,0);
  662.     ok=1;
  663.     nl();
  664.     while ((f1==0) && (ok)) {
  665.       sprintf(s,"%s%s",syscfg.tempdir,ff.ff_name);
  666.       nl();
  667.       outstr(get_string(862));
  668.       pl(ff.ff_name);
  669.       nl();
  670.       ascii_send(s,&sent,&percent);
  671.       if (sent) {
  672.         sprintf(s,get_stringx(1,48),ff.ff_name);
  673.         sysoplog(s);
  674.       } else {
  675.         sprintf(s,get_stringx(1,49),ff.ff_name,percent*100.0);
  676.         sysoplog(s);
  677.         ok=0;
  678.       }
  679.       f1=findnext(&ff);
  680.     }
  681.   }
  682. }
  683.  
  684.  
  685. void list_temp_arc(void)
  686. {
  687.   char s1[81],s2[81];
  688.  
  689.   sprintf(s1,"TEMP.%s",syscfg.arcs[ARC_NUMBER].extension);
  690.   list_arc_out(s1,syscfg.tempdir);
  691.   nl();
  692. }
  693.  
  694.  
  695.  
  696.  
  697. void temporary_stuff(void)
  698. {
  699.   char s[81],s1[81],ch;
  700.   int done;
  701.  
  702.   done=0;
  703.   do {
  704.     nl();
  705.     prt(2,get_string(863));
  706.     ch=onek("Q?DRAVLT");
  707.     switch(ch) {
  708.       case 'Q':
  709.     done=1;
  710.     break;
  711.       case 'D':
  712.         download_temp_arc("TEMP", 1);
  713.     break;
  714.       case 'V':
  715.     list_temp_arc();
  716.     break;
  717.       case 'A':
  718.         add_temp_arc();
  719.     break;
  720.       case 'R':
  721.     del_temp();
  722.     break;
  723.       case 'L':
  724.     list_temp_dir();
  725.     break;
  726.       case 'T':
  727.     list_temp_text();
  728.     break;
  729.       case '?':
  730.     printmenu(14);
  731.     break;
  732.     }
  733.   } while ((!hangup) && (!done));
  734. }
  735.  
  736.  
  737.  
  738.