#include "INET1.h" #include #include //#include #include #include struct character { char ch; struct character *next; }; struct message { int length; struct message * down; struct character *next; }; struct userpass { char name[20]; char pass[20]; }up[4]; //struct userpass abc[4]; strcpy(up[0].name,"sirish"); /*up[0].pass="sjce"; up[1].name="varun"; up[1].pass="sjce"; up[2].name="satish"; up[2].pass="sjce"; up[3].name="abhi"; up[3].pass="sjce";*/ typedef struct message MESS; typedef struct character CHAR; MESS *Head,*lastmess; CHAR *lastchar; /* this part is for the handling of the database management*/ void createdatabase() { FILE * f; char ch,word[5]; int i,comp=0; int k,z; void addcharacter(char); void addmessage(); void display(); int countmessage(); int sizeofeachmessage(); int sizeofdata(); Head=(MESS *)malloc(sizeof(MESS)); Head->next=NULL; Head->down=NULL; f=fopen("./root1","r"); lastmess=Head; if(f==NULL) printf("EROOR WHILE OPENING THE FILE"); else { while((ch=fgetc(f))!=EOF) { i=0; printf("%c",ch); word[i++]=ch; while(ch!='\n'&&ch!=EOF) { ch=fgetc(f); printf("%c",ch); if(i<4) { word[i++]=ch; } else if (i<5) { word[i]='\0'; if(strcmp(word,"From")==0) { if(comp==0) { printf("new mail starting"); lastchar=NULL; addmessage(); } if(comp==1) comp=0; else if(comp==0) comp=1; }//end of word comparison i=10; addcharacter(word[0]); addcharacter(word[1]); addcharacter(word[2]); addcharacter(word[3]); }//END OF ELSE IS IT IS NOT WORD else { addcharacter(ch); } }//END OF WHILE FOR END OF NEW LINE } } fclose(f); } void addcharacter(char ch) { CHAR *newchar; newchar=(CHAR *) malloc(sizeof(CHAR)); if(newchar==NULL) printf("error in creating character\n"); newchar->ch=ch; newchar->next=NULL; if((lastmess->next)==NULL) lastmess->next=newchar; else lastchar->next=newchar; lastchar=newchar; } void addmessage() { MESS *newmess; newmess=(MESS *) malloc(sizeof(MESS)); newmess->next=NULL; newmess->down=NULL; lastmess->down=newmess; lastmess=newmess; } /* void display(int num) { MESS *Mtemp; CHAR *Ctemp; Mtemp=Head; int count=1; while(countdown; } Ctemp=Mtemp->next; while(Ctemp!=NULL) { strcat(buffer,Ctemp->ch); //printf("%c",Ctemp->ch); Ctemp=Ctemp->next; } } */ int countmessage() { MESS * temp; int count=0; temp=Head; while(temp!=NULL) { if(temp->next!=NULL) count++; temp=temp->down; } return(count); } int sizeofeachmessage(int messageno) { MESS * temp; CHAR *temp1; int count=0,i; temp=Head; for(i=1;i<=messageno;i++) { if(temp==NULL) return(0); else temp=temp->down; } temp1=temp->next; while(temp1!=NULL) { count++; temp1=temp1->next; } return(count); } int sizeofdata() { int nomess,size,total=0,i; int countmessage(); int sizeofeachmessage(int); nomess=countmessage(); for(i=1;i<=nomess;i++) { size=sizeofeachmessage(i); total+=size; } return(total); } /*end of the database management */ /*this section is to findout wheater there is list number in message*/ int findlistnumber(char buffer[]) { int num=0; char val[5]; int i,j; i=5; j=0; if(buffer[i]=='\0') return(0); while(1) { if(buffer[i]=='\0') break; else val[j++]=buffer[i++]; } i=1; j--; while(j>=0) { num=(val[j--]-48)*i; i=i*10; } return(num); } /*end of this section to findout wherther there is a list number*/ /*this section is for to handle the list command*/ void handleLIST(int sockfd, char buffer[]) { int i,j,msgno,total; char data[512]; msgno=findlistnumber(buffer); printf("The list no is %d",msgno); if(msgno!=0) { if(msgno>countmessage()) { writedata(sockfd,"-ERR SUCH MESSAGE NO DOES NOT EXIST"); } else { memset((void *)data,'\0',512); strcpy(data,"+OK "); addinteger(msgno,data); strcat(data," "); addinteger(sizeofeachmessage(msgno),data); writedata(sockfd,data); } } else { total=countmessage(); memset((void *)data,'\0',512); strcpy(data,"+OK "); addinteger(total,data); strcat(data," messages ("); addinteger(sizeofdata(),data); strcat(data," octets)" ); writedata(sockfd,data); for(i=1;i<=total;i++) { memset((void *)data,'\0',512); addinteger(i,data); strcat(data," "); addinteger(sizeofeachmessage(i),data); writedata(sockfd,data); } writedata(sockfd,"046."); } } /*end of this section which handles the list command*/ void handleRETR(int sockfd, char buffer[]) { int i,j,msgno,count,k; char data[512]; MESS *Mtemp; CHAR *Ctemp; msgno=findlistnumber(buffer); printf("The msg no is %d",msgno); if((msgno<=0)||(msgno>countmessage())) { writedata(sockfd,"-ERR SUCH MESSAGE NO DOES NOT EXIST"); } else { memset((void *)data,'\0',512); strcpy(data,"+OK "); addinteger(msgno,data); strcat(data,"\n"); //addinteger(sizeofeachmessage(msgno),data); //MESS *Mtemp; //CHAR *Ctemp; Mtemp=Head; count=1; while(count<=msgno) { count++; Mtemp=Mtemp->down; } Ctemp=Mtemp->next; while(Ctemp!=NULL) { // strcat(data,Ctemp->ch); k=strlen(data); data[k]=Ctemp->ch; printf("%c",Ctemp->ch); Ctemp=Ctemp->next; } writedata(sockfd,data); } } void handleDELE(int sockfd,char buffer[]) { int i,j,msgno,count; char data[512]; MESS *Mtemp,*Ptemp; CHAR *Ctemp,*Dtemp; msgno=findlistnumber(buffer); printf("The msg no is %d",msgno); if((msgno<=0)||(msgno>countmessage())) { writedata(sockfd,"-ERR SUCH MESSAGE NO DOES NOT EXIST"); } else { memset((void *)data,'\0',512); strcpy(data,"+OK"); strcat(data,"\n"); //addinteger(sizeofeachmessage(msgno),data); //MESS *Mtemp; //CHAR *Ctemp; Ptemp=NULL; Mtemp=Head; count=1; while(count<=msgno) { count++; Ptemp=Mtemp; Mtemp=Mtemp->down; } Ctemp=Mtemp->next; while(Ctemp!=NULL) { //strcat(data,Ctemp->ch); //printf("%c",Ctemp->ch); Dtemp=Ctemp; Ctemp=Ctemp->next; free(Dtemp); } Ptemp->down=(Ptemp->down)->down; free(Mtemp); writedata(sockfd,data); } } void handleupdate(int sockfd) { FILE *f; //int i,j,msgno,count; char data[512],ch; MESS *Mtemp; CHAR *Ctemp; f=fopen("./root2","w"); //msgno=findlistnumber(buffer); //printf("The msg no is %d",msgno); /*if((msgno<=0)||(msgno>countmessage())) { writedata(sockfd,"-ERR SUCH MESSAGE NO DOES NOT EXIST"); } else { */memset((void *)data,'\0',512); strcpy(data,"+OK"); //addinteger(msgno,data); // strcat(data,"\n"); //addinteger(sizeofeachmessage(msgno),data); //MESS *Mtemp; //CHAR *Ctemp; Mtemp=Head; //count=1; while((Mtemp)!=NULL) { Ctemp=Mtemp->next; while(Ctemp!=NULL) { //strcat(data,Ctemp->ch); //ch=Ctemp->ch; fprintf(f,"%c",Ctemp->ch); Ctemp=Ctemp->next; } Mtemp=Mtemp->down; } fclose(f); writedata(sockfd,data); } /* this section is for to handle the STAT command */ void handleSTAT(int newsockfd) { char buffer[512]; memset((void *)buffer,'\0',512); strcpy(buffer,"+OK"); strcat(buffer," "); addinteger(countmessage(),buffer); strcat(buffer," "); addinteger(sizeofdata(),buffer); writedata(newsockfd,buffer); } /* end of section for to handle the list command*/ /*THIS SECTION IS FOR TO HANDLE THE NOOP COMMAND*/ void handleNOOP(int newsockfd) { char buffer[512]; memset((void *)buffer,'\0',512); strcpy(buffer,"+OK"); writedata(newsockfd,buffer); } /*END OF SESSION TO HANDLE THE NOOP COMMAND*/ main(int argc,char * argv) { int i,j,k; int sockfd,newsockfd=0,clilen,childpid; struct sockaddr_in cli_addr,serv_addr; if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) printf("error which creating socket"); bzero((char *) &serv_addr,sizeof(serv_addr)); serv_addr.sin_family =AF_INET; serv_addr.sin_addr.s_addr=htonl(INADDR_ANY); serv_addr.sin_port=htons(SERV_TCP_PORT); if(bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr))<0) printf("error while binding"); listen(sockfd,5); for(;;) { clilen=sizeof(cli_addr); printf("before accept%d",newsockfd); // waddstr(stdscr,"before accept"); newsockfd=accept(sockfd,(struct sockaddr *) &cli_addr,&clilen); printf("after accept%d",newsockfd); if(newsockfd<0) printf("error while accepting"); if((childpid=fork())<0) printf("error while forking"); else if(childpid==0) { int n=-1,i; char line[512],buffer[512],ptr[512],data[512],temp[512]; close(sockfd); /*this is the initial process of making the database*/ createdatabase(); /*end of initial phase*/ /*this is the the first stage 1 indicating that connection has been established properly*/ /*STAGE 1*/ memset((void *)buffer,'\0',512); strcpy(buffer,"+OK POP3 server ready"); if(!(write(newsockfd,buffer,sizeof(buffer)))) printf("eror while writing\n"); /* end of stage 1 for sending the greeting to the client that he has successfully connected to the server*/ /* beging of stage 2*/ /* This the authorization stage of the process*/ k=0; //this is to hang on in the loop till a valid thing //does not happen while(k==0) //this for username { readdata(newsockfd,buffer); memset((void *)data,'\0',512); strncpy(data,buffer,4); /* writedata(1,"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); puts(data); writedata(1,"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); */ i=0; for(j=4;j