home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / RDQWKSRC.ZIP / QLIB.C < prev    next >
C/C++ Source or Header  |  1991-01-15  |  14KB  |  423 lines

  1. /*
  2. ╔══════════════════════════════════════════════════════════════════════════╗
  3. ║                                                                          ║
  4. ║         Test routines to read, in Qmail Format - R.Cougnenc 90 -         ║
  5. ║                                                                          ║
  6. ║                            Public Domain                                 ║
  7. ║                                                                          ║
  8. ╟──────────────────────────────────────────────────────────────────────────╢
  9. ║                                                                          ║
  10. ║       These modules correspond to different tests at reading in          ║
  11. ║       Qmail Format. They were never planned to be released as a          ║
  12. ║       package.                                                           ║
  13. ║                                                                          ║
  14. ║       Source code is not beautified or optimised as they are work        ║
  15. ║       files and are distributed as is.                                   ║
  16. ║                                                                          ║
  17. ║       The program, even at it's rustic stage is fully fonctional,        ║
  18. ║       as I use it regularly...                                           ║
  19. ║                                                                          ║
  20. ║       Feel free to use any or all of these modules, if you can sort      ║
  21. ║       it out! :-)                                                        ║
  22. ║                                                                          ║
  23. ╚══════════════════════════════════════════════════════════════════════════╝
  24. */
  25.  
  26. #include <stdio.h>
  27. #include <time.h>
  28. #include <fcntl.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #ifdef __TURBOC__
  32. #include <alloc.h>
  33. #else
  34. #include <malloc.h>
  35. #endif
  36. #include "reader.h"
  37. #include "readlib.h"
  38. #include "qmail.h"
  39. #include "ansi.h"
  40.  
  41. struct MsgHeaderType Qmail ;
  42.  
  43. #define  call   goto        /* :-))                                  */
  44.  
  45.  
  46.  
  47.                         /* Variables used to read Control.dat         */
  48.  
  49. int LastConf  =  0 ;
  50. int Security  =  0 ;
  51. char BoardName[50],
  52.      UserName [50],
  53.      First    [50],
  54.      City     [50],
  55.      HomePhone[50],
  56.      DataPhone[50];
  57.  
  58.  
  59. char ConfName[MAXCONF][NAMCONF];
  60.  
  61. int  dd,
  62.      mm,
  63.      year,
  64.      hh,
  65.      mn,
  66.      sec;
  67.  
  68. int  exp_dd,
  69.      exp_mm,
  70.      exp_yy;
  71.  
  72. long PrevOffset ;
  73. int conf ;
  74. int count;
  75. int fd, fs ;
  76.  
  77.  
  78.  
  79.  
  80. /*
  81.  
  82.           Reads the file CONTROL.DAT pointed by Pathname.
  83.           This function is not finished but works well enought.
  84.  
  85. */
  86.  
  87. int ReadControl(Path)
  88. char *Path;
  89. {
  90.     FILE *fp;
  91.     int i,numconf;
  92.     char tmp[MAXPATHS];
  93.  
  94.     sprintf(tmp,"%s\\%s",Path,CNTRL_FILE);
  95.  
  96.     if( (fp = fopen( tmp,"r") ) == NULL )   call bad_end ;
  97.  
  98.                                         /*--- Get Board Name -----*/
  99.     if( fget(BoardName,50,fp) == NULL  )              call bad_end ;
  100.                                         /*--- Skip 5 lines...-----*/
  101.     for (i = 0 ; i < 5 ; i++ )
  102.        if(  fget(tmp, 126,fp) == NULL )               call bad_end ;
  103.  
  104.                                         /* Get time & date        */
  105.    sscanf(tmp,"%d-%d-%d,%d:%d:%d",&mm,&dd,&year,&hh,&mn,&sec);
  106.  
  107.                                         /*--- Get User Name  -----*/
  108.    if( fget(UserName,50,fp) == NULL )                 call bad_end ;
  109.  
  110.                                         /*--- Skip 3 lines...-----*/
  111.     for (i = 0 ; i < 3 ; i++ )
  112.        if(  fget(tmp, 126,fp) == NULL )               call bad_end ;
  113.  
  114.                                         /*--- Get Last conf #-----*/
  115.     if (( fget(tmp,126,fp ) == NULL) )                call bad_end ;
  116.     LastConf = atoi(tmp);
  117.     if(! LastConf || LastConf > MAXCONF )             call bad_end ;
  118.  
  119.                                         /*--- Get Conf Names -----*/
  120.     for ( i = 0 ; i < MAXCONF ; i ++ )
  121.     {
  122.        if ( fget(tmp,126,fp) == NULL  )               call bad_end ;
  123.        numconf = atoi(tmp);
  124.        if( numconf < 0 || numconf > LastConf )        call bad_end ;
  125.        if ( fget(ConfName[numconf],15,fp) == NULL  )  call bad_end ;
  126.        if (numconf == LastConf ) break;
  127.     }
  128.  
  129. #ifdef MARKMAIL
  130.  
  131.                                /* Look for the 2 occurence of user Name */
  132.     while( 1 )
  133.     {
  134.          if( fget (tmp, 126, fp ) == NULL )            call bad_end ;
  135.          if(! strncmp(tmp,UserName ) ) break;
  136.     }
  137.                                /* And get First Name. */
  138.     if( fget(First,50,fp)      == NULL  )                call bad_end ;
  139.     if( fget(City,50,fp)       == NULL  )                call bad_end ;
  140.     if( fget(HomePhone,50,fp)  == NULL  )                call bad_end ;
  141.     if( fget(DataPhone,50,fp)  == NULL  )                call bad_end ;
  142.  
  143.     if( fget(tmp,50,fp)        == NULL  )                call bad_end ;
  144.     Security = atoi(tmp);
  145.  
  146.      if( fget(tmp,50,fp)        == NULL  )                call bad_end ;
  147.      sscanf(tmp,"%d-%d-%d",&exp_mm,&exp_dd,&exp_yy);
  148.      exp_yy +=1900;
  149.  
  150. #endif
  151.  
  152.   fclose(fp);
  153.   return ( OK );
  154. bad_end:
  155.   fclose(fp);
  156.   return( ERROR );
  157. }
  158.  
  159.  
  160.  
  161.  
  162.  
  163. /*
  164.      Scans the file MESSAGE.DAT, and calls WriteIndex for writing
  165.      index files.
  166. */
  167. int MkIndex(SrcDir,DestDir)
  168. char *SrcDir,*DestDir ;
  169. {
  170.   FILE *fs,*fd,*fx;
  171.   unsigned char *ptr ;
  172.   int conf = 0 ;
  173.   int count = 0 ;
  174.   int oldcount = 0 ;
  175.   register int i ;
  176.   int Head  = sizeof( struct MsgHeaderType );
  177.   unsigned MaxChars,
  178.            NbBlocs ;
  179.   char Src[MAXPATHS];              /* Source file Message.DAT */
  180.   char Dst[MAXPATHS];              /* Conference data  file   */
  181.   char Idx[MAXPATHS];              /* Conference index file   */
  182.   long last ;
  183.  
  184.   if( ReadControl(SrcDir) )     /* Load new CONTROL.DAT file. */
  185.   {
  186.     printf("%s !\n",txt[14]);   /* "error in control.dat" */
  187.     return ( ERROR );
  188.   }
  189.                                 /* Update Control.dat in bbs dir */
  190.   sprintf(Src,"%s/%s",SrcDir ,CNTRL_FILE);
  191.   sprintf(Dst,"%s/%s",DestDir,CNTRL_FILE);
  192.   if( fcopy(Src,Dst ))
  193.      return( ERROR );
  194.  
  195.                                 /* Update Newfiles.dat in bbs dir */
  196.   sprintf(Src,"%s/%s",SrcDir,NEWFILES);
  197.   if(! access(Src,0) )
  198.   {
  199.       sprintf(Dst,"%s/%s",DestDir,NEWFILES);
  200.       if( fcopy(Src,Dst ))
  201.           return( ERROR );
  202.   }
  203.  
  204.                                 /* Update Welcome file in bbs dir */
  205.  
  206.   sprintf(Src,"%s/%s",SrcDir,WELCOME);  /* try ascii mode */
  207.   if(! access(Src,0) )
  208.   {
  209.       sprintf(Dst,"%s/%s",DestDir,WELCOME);
  210.       if( fcopy(Src,Dst ))
  211.           return( ERROR );
  212.   }
  213.   else
  214.     sprintf(Src,"%s/%s",SrcDir,WELCOMEG);  /* or ansi mode */
  215.   if(! access(Src,0) )
  216.   {
  217.       sprintf(Dst,"%s/%s",DestDir,WELCOMEG);
  218.       if( fcopy(Src,Dst ))
  219.           return( ERROR );
  220.   }
  221.  
  222.  
  223.                                 /* Update News file in bbs dir */
  224.  
  225.   sprintf(Src,"%s/%s",SrcDir,NEWS);  /* try ascii mode */
  226.   if(! access(Src,0) )
  227.   {
  228.       sprintf(Dst,"%s/%s",DestDir,NEWS);
  229.       if( fcopy(Src,Dst ))
  230.           return( ERROR );
  231.   }
  232.   else
  233.     sprintf(Src,"%s/%s",SrcDir,NEWSG);  /* or ansi mode */
  234.   if(! access(Src,0) )
  235.   {
  236.       sprintf(Dst,"%s/%s",DestDir,NEWSG);
  237.       if( fcopy(Src,Dst ))
  238.           return( ERROR );
  239.   }
  240.  
  241.  
  242.  
  243.   yellow();
  244.   printf("%s...\n",txt[71]);  /* "Adding msgs and creating indexes" */
  245.   cyan();
  246.  
  247.   sprintf(Src,"%s/%s",SrcDir,MSG_FILE );     /* Open MESSAGES.DAT */
  248.   if(( fs = fopen(Src,"rb")) == NULL )
  249.   {
  250.      printf("%s %s...\n",txt[51],Src);       /* "unable to open file" */
  251.      return (ERROR);
  252.   }
  253.   if( setvbuf(fs,NULL,_IOFBF,VBUF ) )
  254.     printf("%sMkIndex() [%s]\n",txt[ 94 ],Src);  /* "Setvbuf failed..." */
  255.  
  256.                                            /* Skip First Header      */
  257.  if(! fread((char *) &Qmail.Status,1, Head ,fs) )
  258.   {
  259.     printf("%s %s\n",txt[58],Src );     /* "error reading file" */
  260.     fclose(fs);
  261.     return (ERROR );
  262.   }
  263.  
  264.   sprintf(Dst,"%s/%d.cnf",DestDir,conf );     /*--< Open Conference file >--*/
  265.   if(( fd = fopen(Dst,"a+b")) == NULL )
  266.   {
  267.      printf("%s %s...\n",txt[51],Dst);        /* "unable to open file" */
  268.      fclose(fs);
  269.      return (ERROR);
  270.   }
  271.   if( setvbuf(fd,NULL,_IOFBF,VBUF ) )
  272.    printf("%sMkIndex() [%s]\n",txt[ 94 ],Dst);  /* "Setvbuf failed..." */
  273.  
  274.    fseek(fd,0L,SEEK_END );     /* Go to end of file... */
  275.    last = (long) ftell(fd) ;   /* Take size of existing messages */
  276.   /* printf("Last = %ld\n",last );*/
  277.  
  278.   sprintf(Idx,"%s/%d.idx",DestDir,conf );     /*--< Open Conference Index >--*/
  279.   if( access(Idx,0) ) /* Create if not exist */
  280.   {
  281.     fx = fopen(Idx,"w");
  282.     fclose( fx );
  283.   }
  284.   if(( fx = fopen(Idx,"r+b")) == NULL )
  285.   {
  286.      printf("%s %s...\n",txt[ 51 ],Idx); /* "unable to open file" */
  287.      fclose(fs);
  288.      fclose(fd);
  289.      return (ERROR);
  290.   }
  291.   if( setvbuf(fx,NULL,_IOFBF,VBUF ) )
  292.    printf("%sMkIndex() [%s]\n",txt[ 94 ],Idx);  /* "Setvbuf failed..." */
  293.    fseek(fx,0L,SEEK_END );
  294. /*   printf("Offset Index = %ld\n",ftell(fx) ); */
  295.   count = (int) ( ftell(fx) / sizeof( struct MyIndex ));
  296.   oldcount = count ;
  297.                                                /*----------------------------*/
  298.  
  299.   while(1)
  300.   {
  301.     if( ! fread (&Qmail.Status,1, Head,fs) )
  302.       break;  /* End of file         */
  303.     if(Qmail.MsgTime[2] != 58 )
  304.       break;  /* Dernier entete vide cree par les doors qmail probablement.*/
  305.               /* Faudrait tester un peu mieux que ça... enfin...           */
  306.       memcpy((char *) rbuf, (char *) &Qmail.Status, Head ) ;
  307.  
  308.       printf("%d : %d\r",conf,count - oldcount);
  309.         if( (int) Qmail.Conference != conf )     /* Another conference ? */
  310.         {
  311.           fclose(fx);                            /* Close index file      */
  312.           fclose(fd);                            /* Close Conference file */
  313.           printf("Conference %d \t[ %10s ] :  %2d New %s\n",
  314.           conf,ConfName[conf],count - oldcount ,
  315.           (count - oldcount) > 1 ? "Messages" : "Message");
  316.           count = 0 ;
  317.           oldcount = 0 ;
  318.           conf = Qmail.Conference ;
  319.                                                   /* Open new files  */
  320.           sprintf(Dst,"%s/%d.cnf",DestDir,conf );     /*--< Open Conference file >--*/
  321.           if(( fd = fopen(Dst,"a+b")) == NULL )
  322.           {
  323.              printf("%s %s...\n",txt[ 51 ],Dst);  /* "unable to open file" */
  324.              fclose(fs);
  325.              return (ERROR);
  326.           }
  327.           if( setvbuf(fd,NULL,_IOFBF,VBUF ) )
  328.              printf("%sMkIndex() [%s]\n",txt[ 94 ],Dst);  /* "Setvbuf failed..." */
  329.  
  330.            fseek(fd,0L,SEEK_END );     /* Go to end of file... */
  331.            last = (long) ftell(fd) ;   /* Take size of existing messages */
  332.            /* printf("Last = %ld\n",last ); */
  333.  
  334.            sprintf(Idx,"%s/%d.idx",DestDir,conf );     /*--< Open Conference Index >--*/
  335.            if( access(Idx,0) ) /* Create if not exist */
  336.            {
  337.                 fx = fopen(Idx,"w");
  338.                 fclose( fx );
  339.            }
  340.            if(( fx = fopen(Idx,"r+b")) == NULL )
  341.            {
  342.                 printf("%s %s...\n",txt[ 51 ],Idx); /* "unable to open file" */
  343.                 fclose(fs);
  344.                 fclose(fd);
  345.                 return (ERROR);
  346.            }
  347.            if( setvbuf(fx,NULL,_IOFBF,VBUF ) )
  348.               printf("%sMkIndex() [%s]\n",txt[ 94 ],Idx);  /* "Setvbuf failed..." */
  349.            fseek(fx,0L,SEEK_END );
  350.            /*  printf("Offset Index = %ld\n",ftell(fx));*/
  351.            count = (int) ( ftell(fx) / sizeof( struct MyIndex ));
  352.            oldcount = count ;
  353.         }
  354.  
  355.         NbBlocs = atoi(Qmail.SizeMsg) - 1;
  356.         MaxChars =   NbBlocs * 128 ;
  357.         if( MaxChars > MYBUF )
  358.         {
  359.            printf("%s [ > %u bytes ]\n",txt[72],MYBUF); /* "msg too big" */
  360.            return (ERROR );
  361.         }
  362.  
  363.     WriteIndex(fx,count,MaxChars,last);    /* Write Index Structure */
  364.     count ++ ;
  365.  
  366.     if(!   fread((char *)( rbuf + Head) ,1,MaxChars,fs) ) /* Read the message..... */
  367.     {
  368.       printf("%s %s\n",txt[ 58 ],Src);  /* "error reading file " */
  369.       fclose(fs);
  370.       fclose(fd);
  371.       fclose(fx);
  372.       return( ERROR );
  373.     }
  374.  
  375.     ptr = (unsigned char * ) (rbuf + Head );
  376.     for( i = 0 ; i < MaxChars ; i ++ )   /* Translate to normal Line Feed */
  377.     {
  378.      if ( *ptr == 227 )
  379.        *ptr = '\n';
  380.      ptr++;
  381.     }
  382.     while( *(--ptr) == 0x20 ) *ptr = 0 ; /* delete padding spaces */
  383.     if(!   fwrite(rbuf,1,MaxChars + Head,fd) )           /* And copy it.         */
  384.     {
  385.       printf("%s %s\n",txt[ 73 ],Dst);  /* "error writing file " */
  386.       fclose(fs);
  387.       fclose(fd);
  388.       fclose(fx);
  389.       return( ERROR );
  390.     }
  391.  
  392.     last += (long ) MaxChars + Head ;      /* INC counter... */
  393.  
  394.   }
  395.   printf("Conference %d \t[ %10s ] :  %2d New %s\n",
  396.   conf,ConfName[conf],count - oldcount,
  397.   (count - oldcount) > 1 ? "Messages" : "Message");
  398.   printf("\n");
  399.   fclose(fs);
  400.   fclose(fd);
  401.   fclose(fx);
  402. }
  403.  
  404. /*
  405.               Add an index struct to the index file.
  406. */
  407. void WriteIndex(fx,count,Size,Offset)
  408. FILE *fx;
  409. int count ;
  410. int Size  ;
  411. long Offset ;
  412. {
  413.     Index.MsgNum = count  ;
  414.     Index.Size   = Size   ;
  415.     Index.Offset = Offset ;
  416.     Index.LastRead = 0 ;
  417.  
  418.     if(! fwrite(&Index.LastRead,1,sizeof(struct MyIndex),fx ) )
  419.      {
  420.         printf("%s !\n",txt[74]); /* "error writing index file "*/
  421.      }
  422. }
  423.