[C] Separate Large Mailbox(IMAP format) into two files based on date

[C] Separate Large Mailbox(IMAP format) into two files based on date
0 votes, 0.00 avg. rating (0% score)

You can generate a copy of your IMAP mailbox based on particular date and hour.

Actually this code generate a copy of IMAP mailbox based on the date & optionally hour argument.

Arguments: mailbox_filename date(mm/dd/yyyy) [hour]
hour is optional arg.

For example, if your inbox size is larger than 50 Meg and you are having hard time to read the box with your shell mail client.

Let’s say you want to save mails from ‘usermail’ file upto 09/01/2005.

You should run ( let’s say the compiled program’s name is divide)

$> divide usermail 09/01/2005

Then all mails received after 09/01/2005 will be saved to ‘usermail.bak2’.
You can backup the original mail file and rename the ‘usermail.bak2’ to ‘usermail’.

Here is the actual code.


#include
#include
char MONTH[12][10]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug"
, "Sep", "Oct", "Nov", "Dec"};

void getword(char *word, char *line, char stop) {

int x = 0,y;
for(x=0;((line[x]) && (line[x] != stop));x++)
word[x] = line[x];

word[x] = '\0';
if(line[x]) ++x;
y=0;

while(line[y++] = line[x++]);
}
main(int argc, char **argv){
FILE *tfd, *ifd ,*ifd1, *ifd2;
char c, oldc ;
unsigned long count=0 , limit;
char *filename;
char *file1;
char *file2;
char buf[1024],obuf[1024]="";
char datestr[100];
char tmp[100];
char mmstr[100];
char ddstr[100];
char yystr[100];
int rmm,rdd,ryy,rhh,rmi;
struct tm dt;
int mm,dd,yy;
time_t otime,ctime;
int i,head;
int time;
int ahour=0;

if(!(argc==3 || argc==4)) {
printf("args needed: file date(mm/dd/yyyy) [hour]\n");
printf("results: file is divided into file1 and file2\n");

exit(0);
}

filename=strdup(argv[1]);
sscanf(argv[2],"%d/%d/%d",&mm,&dd,&yy);
if(mm<1 || mm >12 || dd<1 || dd >31 || yy < 1970 ){ printf("Date format incorrent\n"); exit(0); } if(argc==4){ //time limit defined ahour= atoi(argv[3]); if(ahour < 0 || ahour >23) {
printf("Invalid time (0-23)\n");
exit(0);
}
}

// set original otime
dt.tm_sec =0;
dt.tm_min =0;
dt.tm_hour=ahour;
dt.tm_mday=dd;
dt.tm_mon =mm-1;
dt.tm_year=yy-1900;
otime=mktime(&dt);

printf("%d/%d/%d\n",mm,dd,yy);
file2=malloc(sizeof(char) * strlen(filename)+10);

if((tfd=fopen(filename,"r"))==NULL){
printf("could not find %s\n",filename);
exit(1);
}

sprintf(file2,"%s.bak2",filename);

if((ifd2=fopen(file2,"w"))==NULL){
printf("Could not create file2\n");
exit(1);
}

printf("Searching Date\n");

head=0;
// copying imap header part of original mail skip "\n" line twice
while(head<2 && fgets(buf,1024,tfd)!=NULL){ if(strncmp(buf,"\n",1)==0){ head++; } fprintf(ifd2,"%s",buf); } obuf[0]='\0'; while(fgets(buf,1024,tfd)!=NULL){ if(strncmp(obuf,"\n",1)==0 && strncmp(buf,"From ",5) ==0){ sscanf(buf,"From %s %3s %3s %2d %2d:%2d:%s %4d",tmp,tmp,mmstr,&rdd,&rhh,&rmi,&tmp,&ryy); printf("%s, %d, %d h:%2d:%2d\n",mmstr,rdd,ryy,rhh,rmi); for(i=0;i<12;i++) if(strcmp(MONTH[i],mmstr)==0){ rmm=i+1; break; } dt.tm_hour=rhh; dt.tm_min =rmi; dt.tm_mday=rdd; dt.tm_mon =rmm-1; dt.tm_year=ryy-1900; ctime= mktime(&dt); if(ctime>=otime)
break;

// printf("%d/%d/%d\n",rmm,rdd,ryy);

}
strcpy(obuf,buf);
}

printf("Generating file2\n");

fprintf(ifd2,"%s",buf);
while(fgets(buf,1024,tfd)!=NULL){
fprintf(ifd2,"%s",buf);
}
fclose(tfd);
fclose(ifd2);

}

Leave a Reply

Your email address will not be published. Required fields are marked *