/* ********************************************************************
* chkmail.c *
* Functions: *
* Connect to the pop server and get the mail number *
* Open the specified file FILEPATH and obtain the old number *
* Call comp(),to compare the two numbers *
* Author:zuii *
**********************************************************************/
#include "chkmail.h"
int
main(int argc,char **argv)
{
int sockfd;
int i,n_size;
time_t tt;
struct hostent *host=NULL;
struct sockaddr_in serv_addr;
char n_num[MAXSIZE],rubbish[MAXSIZE];
FILE *fp;
int f_size;
struct stat f_stat;
char o_num[10];
char *POPMsg[]={
"USER YOURID\r\n",
"PASS YOURPASSWORD\r\n",
"QUIT\r\n",
NULL
};
while((host=gethostbyname("pop.163.com"))==NULL)
{
time(&tt);
printf("%s",ctime(&tt));
perror("gethostbyname error");
sleep(10);
}
while(1)
{
/* Connect to the pop server */
while((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
time(&tt);
printf("%s",ctime(&tt));
perror("socket error");
sleep(10);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(POP3SERVPORT);
serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero),8);
while (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1)
{
time(&tt);
printf("%s",ctime(&tt));
perror("connect error");
sleep(10);
}
bzero(rubbish,MAXSIZE);
recv(sockfd,rubbish,sizeof(rubbish),0);
if(rubbish[1]!='O')
continue;
/* send username to server */
bzero(rubbish,MAXSIZE);
send(sockfd,POPMsg[0],strlen(POPMsg[0]),0);
recv(sockfd,rubbish,sizeof(rubbish),0);
if(rubbish[1]!='O')
continue;
/* send password,and get the new number */
bzero(n_num,MAXSIZE);
send(sockfd,POPMsg[1],strlen(POPMsg[1]),0);
recv(sockfd,n_num,sizeof(n_num),0);
for(i=0;n_num[i+4]!=' ';i++)
{
n_num[i]=n_num[i+4];
}
n_num[i]='\0';
send(sockfd,POPMsg[2],strlen(POPMsg[2]),0);
close(sockfd);
n_size=strlen(n_num);
/* open the file which contains the old mail number */
if(access(FILEPATH,F_OK)==-1)
{
perror("Access error");
exit(1);
}
else
{
if(stat(FILEPATH,&f_stat)==-1)
{
perror("Stat Error");
exit(1);
}
f_size=f_stat.st_size;
if((fp=fopen(FILEPATH,"r"))==NULL)
{
perror("Read error !\n");
exit(1);
}
fgets(o_num,f_size+1,fp); /* read the old mail number */
fclose(fp);
}
/* call comp() to compare the two numbers */
comp(o_num,n_num);
sleep(30);
}
return 0;
}
|