home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / mrl1.zip / mrl.c < prev    next >
C/C++ Source or Header  |  1997-06-20  |  7KB  |  202 lines

  1. #include "mrl.h"
  2. #include <malloc.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8.  
  9. unsigned int mmin = 65535;    /* Low-end  of .MSG */
  10. unsigned int mmax = 0;        /* High-end of .MSG */
  11. unsigned int mlast = 0;        /* Lastread Pointer */
  12. unsigned int mmsgs = 0;
  13. char *logname="mrl.log";
  14. int loglevel=3; /* Log level */
  15.  
  16. void helptext(void)
  17. {
  18.  printf("Ussage: MRL -[option1..N]  <Directory>\n");
  19.  printf("\nMRL is utility for helping FIDONET Sysops to manage their messagebases.\nI use it for some actions in my T-MAIL's NETMAIL area.");
  20.  printf("\nWARNING: This programs works ONLY with FTS-0001 *.MSG messabases.");
  21.  printf("\nOptions:\nL - Link messages by MSGID\nR - Renumber messages\nP <nn> - Purge messages to max nn.\n");
  22.  printf("F - turn off lastread pointer fix\nV <n> - Set logfile level to n <1-5> def. 3\nN <name> - set logfile name, def. mrl.log\n");
  23.  printf("\n\nExample:\nmrl -n d:\\t-mail\\log\\mrl.log -l -r \\msg\\elkin\n");
  24.  
  25. /* WriteLog(logname,"BAd ArGuMenTS",'!'); */
  26.  return;
  27. }
  28.  
  29. #define FALSE 0
  30. #define TRUE  1
  31.  
  32. int
  33. main (int narg,char *arg[])
  34. {
  35.   char purge=FALSE,renumber=FALSE,link=FALSE,fix=TRUE;
  36.   char nextnum=FALSE;
  37.   unsigned int nechat=0;
  38.   char *dir=malloc(260);
  39.   char *tmp=malloc(260);
  40.   int i;
  41.   time_t zacatek;
  42.   zacatek=time(NULL);
  43.   if ((!dir)||(!tmp)) return 255;
  44.   strcpy(dir,"");
  45.   printf(APP_VERSION". Copyright by Radim Kolar (2:423/66.111,hsn@cybermail.net)\n");
  46.   printf("This program is public domain and may be freely modified.\n");
  47.   printf("Compiled date "__DATE__", using GCC "__VERSION__" with emx09c package.\n\n");
  48.   if(narg<2) {
  49.     helptext();
  50.     return 2;
  51.               };
  52.   for(i=1;i<narg;i++)
  53.   {
  54.    if (nextnum==TRUE) { nextnum=FALSE; nechat=atoi(arg[i]);
  55.             if (nechat<1) { purge=FALSE;
  56.                    printf("%s in NOT number, setting purge=No.\n",arg[i]);
  57.                       }else
  58.                          continue;
  59.                          }     
  60.    if (nextnum==TRUE+1) { nextnum=FALSE; loglevel=atoi(arg[i]);
  61.             if (loglevel<1) { loglevel=3;
  62.                    printf("%s in NOT number, setting loglevel=3.\n",arg[i]);
  63.                       }else
  64.                          continue;
  65.                          }     
  66.    if (nextnum==TRUE+2) { nextnum=FALSE; 
  67.                           /* free(logname); */
  68.                           logname=malloc(strlen(arg[i])+1);
  69.                           if (logname) strcpy(logname,arg[i]);
  70.                          continue;
  71.                          }     
  72.  
  73.    if((*(arg[i])=='-')&&(strlen(arg[i])==2))
  74.      /* naber parametry */
  75.     {switch(toupper(*(arg[i]+1)))
  76.      {
  77.       case 'P':nextnum=TRUE;purge=TRUE;break;
  78.       case 'L':link=TRUE;break;
  79.       case 'R':renumber=TRUE;break;
  80.       case 'F':fix=FALSE;break;
  81.       case 'V':nextnum=TRUE+1;break;
  82.       case 'N':nextnum=TRUE+2;break;
  83.       default: printf("Bad parameter : %c\n",*(arg[i]+1));
  84.       }      
  85.      continue; 
  86.      } 
  87.    if((*(arg[i])=='-')&&(strlen(arg[i])!=2))
  88.      {
  89.       printf("Parameter %s is too long or too short-ignored\n",arg[i]);
  90.       continue;
  91.      }
  92.    strcpy(dir,arg[i]); 
  93.   }
  94.   if(nextnum==TRUE) {
  95.       printf("No number after -P, setting purge=No.\n");
  96.       purge=FALSE;
  97.       };
  98.   if(strlen(dir)==0) {printf("Working directory is NOT set, aborting.\n");
  99.                   return 2;
  100.              };
  101.   WriteLog(logname,NULL,'!');                   
  102.   WriteLog(logname,"Begin, "APP_VERSION,'+'); 
  103.   printf("MRL started with this option(s):\n");
  104.   printf("\tWorking directory: %s\n",dir);
  105.   sprintf(tmp,"Working directory: %s",dir);
  106.   WriteLog(logname,tmp,'-');
  107.   printf("\tFix lastread pointer: %s\n",(fix==TRUE)?"YES":"NO");
  108.   sprintf(tmp,"Fix lastread pointer: %s",(fix==TRUE)?"YES":"NO");
  109.   WriteLog(logname,tmp,'-');
  110.   printf("\tPurge messages: %s\n",(purge==TRUE)?"YES":"NO");
  111.   sprintf(tmp,"Purge messages: %s",(purge==TRUE)?"YES":"NO");
  112.   WriteLog(logname,tmp,'-');
  113.   if(purge) { printf("\t\tLeave max. %d messages\n",nechat);
  114.               sprintf(tmp,"...Leave max. %d messages\n",nechat);
  115.               WriteLog(logname,tmp,'-');
  116.             }
  117.   printf("\tRenumber messages: %s\n",(renumber==TRUE)?"YES":"NO");
  118.   sprintf(tmp,"Renumber messages: %s",(renumber==TRUE)?"YES":"NO");
  119.   WriteLog(logname,tmp,'-');
  120.   printf("\tLink messages: %s\n",(link==TRUE)?"YES":"NO");
  121.   sprintf(tmp,"Link messages: %s",(link==TRUE)?"YES":"NO");
  122.   WriteLog(logname,tmp,'-');
  123.   printf("\tLog level: %d\n",loglevel);
  124.   sprintf(tmp,"Log level: %d",loglevel);
  125.   WriteLog(logname,tmp,'-');
  126.   printf("\tLog file name: %s\n",logname);
  127.   sprintf(tmp,"Log file name: %s",logname);
  128.   WriteLog(logname,tmp,'-');
  129.   /* konec Uvodnich Hlasek */
  130.   printf("\nMRL: Scaning directory %s - ",dir);
  131.   sprintf(tmp,"Scaning directory %s",dir);
  132.   WriteLog(logname,tmp,'#');
  133.   i=Scan(dir);
  134.   printf("%s",(i==0)?"OK\n":"Error while scanning\n");
  135.   if(i) return 1;
  136.   printf("\tThere are %d messages\n",mmsgs);
  137.   sprintf(tmp,"There are %d messages",mmsgs);
  138.   WriteLog(logname,tmp,':');
  139.   if(mmsgs)
  140.     {
  141.      printf("\tMessages are in range from %d to %d\n",mmin,mmax);
  142.      sprintf(tmp,"Messages are in range from %d to %d",mmin,mmax);
  143.      WriteLog(logname,tmp,':');
  144.      printf("\tLastread points to %d\n",mlast);
  145.      sprintf(tmp,"Lastread points to %d",mlast);
  146.      WriteLog(logname,tmp,':');
  147.      if ((mlast>mmax)&&(fix))
  148.                      { printf("MRL: Fixing last read pointer.\n");
  149.                        WriteLog(logname,"Fixing last read pointer.",'#');
  150.                        FixLRP(dir);
  151.                      }
  152.  
  153.     }
  154.   if( ((!purge)&&(!renumber)&&(!link)&&(!fix))||(!mmsgs))
  155.    {
  156.     printf("MRL: Nothing to do.");
  157.     WriteLog(logname,"End (nothing to do), "APP_VERSION,'+');      
  158.     return 0;
  159.    }
  160.  
  161.   if(purge)
  162.   {
  163.     
  164.     printf("MRL: Purging messages in %s - ",dir);
  165.     sprintf(tmp,"Purging messages (leave %d)",nechat);
  166.     WriteLog(logname,tmp,'#');
  167.     i=Kill(dir,nechat);  
  168.     printf("%s",(i==0)?"OK\n":"Error while purging\n");
  169.     if(i) return 1;
  170.     printf("MRL: ReScaning directory %s - ",dir);
  171.     i=Scan(dir);
  172.     printf("%s",(i==0)?"OK\n":"Error while rescanning\n");
  173.     if(i) return 1;
  174.    } 
  175.   if(renumber)
  176.   {
  177.     WriteLog(logname,"Renumbering messages",'#');
  178.     printf("MRL: Renumbering messages in %s - ",dir);
  179.     i=Renum(dir);
  180.     printf("%s",(i==0)?"OK\n":"Error while renumbering\n");
  181.     if(i) return 1;
  182.     printf("MRL: ReScaning directory %s - ",dir);
  183.     i=Scan(dir);
  184.     printf("%s",(i==0)?"OK\n":"Error while rescanning\n");
  185.     if(i) return 1;
  186.    } 
  187.   if(link)
  188.   {
  189.     WriteLog(logname,"Linking messages",'#');
  190.     printf("MRL: Linking messages in %s - ",dir);
  191.     i=Link(dir);
  192.     printf("%s",(i==0)?"OK\n":"Error while linking\n");
  193.     if(i) return 1;
  194.    } 
  195.   WriteLog(logname,"End, "APP_VERSION,'+');  
  196.   printf("MRL: Successfuly ended. Time elapsed: %ld sec.",time(NULL)-zacatek);
  197.  
  198.  free(dir); 
  199.  free(tmp);
  200.  return 0;
  201. }
  202.