#define MAIL			"bbs\\mailhdr"
#define HDR				"bbs\\hdr"
#define CONFIG_FIL		"CONFIG.BBS"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

char fname[51],fbuff[255],work[6],guestid[9];
unsigned char td[8];
FILE *fp1;
FILE *fp2;
FILE *fp3;


main()
     {
      char fdate[9],nowdate[9],fnoz[5];
      int fnoi,i,mail_cnt = 0,file_cnt = 0;
      
	  printf("ＲＢＢＳファイル期限処理  Ver_1.1\n\n");
      config_read();
      nwdate();
      td[1]--;
      if ( td[1] == 0 ) { td[0]--; td[1] = 12; }
      sprintf(nowdate,"%02d/%02d/%02d",td[0],td[1],td[2]);
      
	  strcpy(fname,work); strcat(fname,"mailhdr.bak");
	  filcpy(fname,MAIL);

      fp1 = fopen(fname,"r");
      fp2 = fopen(MAIL,"w");

      while( fgets(fbuff,255,fp1 ) )
        {  if ( ! isdigit(*(fbuff)) )  continue;
           
		   substr(fbuff,47,8,fdate);
		   if ( ! strcmp(fdate,guestid) )
			  { if ( fbuff[4] == 'X' )
					{ substr(fbuff,0,4,fnoz);
					  fnoi = atoi(fnoz);
					  mail_cnt++;
					  fildlt(fnoi);
					  continue;
					}
			  }

           substr(fbuff,76,8,fdate);
           if ( strcmp(fdate,nowdate) < 0 )
              { substr(fbuff,0,4,fnoz);
                fnoi = atoi(fnoz);
				mail_cnt++;
                fildlt(fnoi);
                continue;
              }

           substr(fbuff,55,8,fdate);
           if ( strcmp(fdate,nowdate) < 0 && fbuff[4] == ' ' )
              { substr(fbuff,0,4,fnoz);
                fnoi = atoi(fnoz);
				mail_cnt++;
                fildlt(fnoi);
                continue;
              }

           substr(fbuff,4,1,fdate); substr(fbuff,75,1,fnoz);
           if ( fdate[0] == 'X' && fnoz[0] == 'F' )
              { substr(fbuff,0,4,fnoz);
                fnoi = atoi(fnoz);
				mail_cnt++;
                fildlt(fnoi);
                continue;
              }
              
           fputs(fbuff,fp2);
        }
     
     fclose(fp1); fclose(fp2);
	 remove(fname);

     nwdate();                              /* ｹﾞﾝｻﾞｲ ﾋｽﾞｹ ﾊｱｸ */
     for ( i = 0 ; i < 10 ; i++)             /* 10ﾆﾁ ﾏｴ ｻﾝｼｭﾂ */
         {  td[2]--;
            if (td[2] == 0) { td[1]--; td[2] = 30;}
            if (td[1] == 0) { td[0]--; td[1] = 12;}  }
     sprintf(nowdate,"%02d/%02d/%02d",td[0],td[1],td[2]);
     
     strcpy(fname,work);  strcat(fname,"hdr.bak");
	 filcpy(fname,HDR);

     fp1 = fopen(fname,"r");
     fp2 = fopen(HDR,"w");

     while( fgets(fbuff,255,fp1 ) )
        {  if ( ! isdigit(*(fbuff)) )  continue;

           substr(fbuff,5,8,fdate);
           if ( ! strcmp(fdate,"PLAZA   ") )
              {  substr(fbuff,55,8,fdate);
                 if ( strcmp(fdate,nowdate) < 0 )
                    { substr(fbuff,0,4,fnoz);
                      fnoi = atoi(fnoz);
					  file_cnt++;
                      fildlt(fnoi);
                      continue;
                    }
              }
           fputs(fbuff,fp2);
        }

    fclose(fp1); fclose(fp2);
	remove(fname);

	printf("ファイル削除＝ %d件  メール削除＝ %d件\n",file_cnt,mail_cnt);
    exit(0);
    }

      
fildlt(fno)
     int fno;
     {
      int ch;
      long pnf;

    /* ｶﾝﾘ ﾌｧｲﾙ ｺｳｼﾝ */

      fp3 = fopen("bbs\\kanri","r+");
                
      pnf = fno;
      fseek(fp3,pnf,0);
      
      ch = '0';
      fputc(ch,fp3);
      
      fclose(fp3);        
	  return(0);
     }  

substr(buff1,n1,n2,buff2)      /* ﾓｼﾞﾚﾂ ﾌﾞﾝｶﾂ */
     char buff1[];
     int n1;
     int n2;
     char buff2[];
    { int i;
      int j = 0;
      for ( i = n1; j < n2; i++ )
          {  *(buff2 + j) = *(buff1 + i);
             j++;
          }
      *(buff2 + j) = NULL;
      return(0);
    }

#include <dos.h>

nwdate()
  {  
	int i;
    struct TIM_CALEN {
    int year;
    char  month;
	char day;
	char what_day;
	char hour;
	char minute;
	char sec;
	char msec;
	char ext;
	}  calender;

	TIM_rdcalender(&calender);

	td[0] = calender.year - 1900; td[1] = calender.month;
	td[2] = calender.day; td[3] = calender.hour; td[4] = calender.minute;
	td[5] = calender.sec; td[6] = NULL;

	return(0);
  }


filcpy(fno1,fno2)
    char fno1[],fno2[];
	{
		fp1 = fopen(fno1,"w");
		fp2 = fopen(fno2,"r");
		
		while(1)
		{   if ( fgets(fbuff,255,fp2) == NULL )  break;
			if ( fputs(fbuff,fp1) == -1 )
				{  printf("ファイル作成エラー %s\n",fno1);
				   exit(1);
				}
		}

		fclose(fp1); fclose(fp2);
		return(0);
	}


config_read()
     {  int i;
		*(fname) = NULL;
		strcpy(fname,getenv("BBS"));
		if ( getenv("BBS") )  strcat(fname,"\\");
		strcat(fname,CONFIG_FIL);
        fp1 = fopen(fname,"r");

        if ( fp1 == NULL ) { printf("CONFIG.BBSがありません\n");
                             exit(1); }

        for ( i = 1 ; i <= 11 ; i++ )
        { if ( fgets( fbuff,255,fp1) == NULL )
                           { fclose(fp1);
                             conf_err(i);
                           }
        
          switch(i)
	     {
                case 4: strcpy(fname,"cd "); strcat(fname,fbuff);
						system(fname);
						strncpy(fname,fbuff,2); *(fname + 2) = NULL;
						system(fname);  break;
                case 8: strncpy(work,fbuff,strlen(fbuff) - 1);
						*(work + strlen(fname) ) = NULL;
						break;
                case 10: idcpy(guestid,fbuff);
						break;
                default: break;
             }
        }
        fclose(fp1);
        
	return(0);                             
     }

conf_err(errno)
     int errno;
     {
        printf("CONFIG_FILE ERROR   ERR No.=%d\n",errno);
        return(0);
     }

idcpy(id2,id1)      /* ﾓｼﾞﾚﾂ ﾌﾞﾝｶﾂ */
     char id2[];
     char id1[];
    { int i,j;
	  j = strlen(id1); if ( j > 8 ) j = 8;
      for ( i = 0 ; i < j ; i++ )
          { if ( *(id1 + i ) == '\n' ) break; 
			*(id2 + i) = *(id1 + i);
          }
	  for ( ; i < 8 ; i++ )
      	  { *(id2 + i) = ' '; }
	  *(id2 + 8) = NULL;
    return(0);
    }
