Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading email using IMAP #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions email.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ int email(){
int exit =0;
while(exit==0){
system("say what do you want to do with your emails ?");
printf("\n\nWhat do you want to do with your emails ? (send/configure)\n> ");
printf("\n\nWhat do you want to do with your emails ? (send/read/configure)\n> ");
scanf("%s", action);
if(strcmp(action,"send")==0 || strcmp(action,"sned")==0 || strcmp(action,"1")==0 ){
system("say You can write your email.");
ssl_connect("send");
}else if(strcmp(action,"read")==0 || strcmp(action,"raed")==0 || strcmp(action,"2")==0){
printf("NOT IMPLEMENTED YET.");
ssl_connect("read");
} else if (strcmp(action,"configure")==0 || strcmp(action,"3")==0){
config_email_function();
} else if (strcmp(action,"exit")==0 || strcmp(action,"quit")==0 || strcmp(action,"cancel")==0 || strcmp(action,"close")==0){
Expand Down
224 changes: 221 additions & 3 deletions ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <openssl/err.h>

#ifndef BUFFER_SIZE
#define BUFFER_SIZE 4096
#define BUFFER_SIZE 104096
#endif

int SMTP_request(SSL *ssl, char* to, char* title, char* body){
Expand Down Expand Up @@ -108,6 +108,224 @@ int SMTP_request(SSL *ssl, char* to, char* title, char* body){
return 0;
}

void display_header(SSL* ssl, int n){
char resp[BUFFER_SIZE]="";
char resp_buff[BUFFER_SIZE]="";
char buff[BUFFER_SIZE]="";
char from[BUFFER_SIZE]="";
char subject[BUFFER_SIZE]="";
char date[BUFFER_SIZE]="";
char temp_num[BUFFER_SIZE] = "";
char* temp = calloc(BUFFER_SIZE, sizeof(char));

sprintf(temp_num, "%d", n);
printf(" N°: %s ", temp_num);

strcpy(buff, "from fetch ");
strcat(buff, temp_num);
strcat(buff, " (body[header.fields (from date subject)])\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
strcpy(resp, resp_buff);

temp = strtok(resp_buff, "\r\n");
while(strcmp(temp,"from OK Success") != 0){
SSL_read(ssl,resp_buff, sizeof (resp_buff));
strcat(resp, resp_buff);
temp = strtok(resp_buff, "\r\n");
}
temp =strtok(resp, "\r\n");
for(int i = 0; i < 3; i++)
{
temp =strtok(NULL, "\r\n");
if(temp[0]=='F') strcpy(from, temp);
else if(temp[0]=='S') strcpy(subject, temp);
else if(temp[0]=='D'){
strncpy(date, temp, 31);
}
}

//temp = calloc(BUFFER_SIZE, sizeof(char));
printf("%s\n",from);
printf(" %s\n",subject);
printf(" %s \n",date);
bzero(resp_buff,sizeof(resp_buff));

strcpy(buff, "str store ");
strcat(buff, temp_num);
strcat(buff, " flags unseen\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff)); //fetch
//DEBUG printf("[RECEIVED] %s",resp_buff);
temp = strtok(resp_buff, "\r\n");
int success = 0;
while(success!=1){
while (temp != NULL) {
if(strcmp(temp,"str OK Success") == 0) success = 1;
temp = strtok(NULL, "\r\n");
}
if(success==0){
SSL_read(ssl,resp_buff, sizeof (resp_buff));
temp = strtok(resp_buff, "\r\n");
}
}
bzero(resp_buff,sizeof(resp_buff));
}


int display_list(SSL *ssl){
char resp_buff[BUFFER_SIZE]="";
char buff[BUFFER_SIZE]="";
int new_messages[100] = {0};
int total_unseen = 0;

strcpy(buff, "sel select inbox\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
bzero(resp_buff,sizeof(resp_buff));

strcpy(buff, "sea search (unseen)\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
int i = 0;
char* temp = malloc(BUFFER_SIZE*sizeof(char));
temp = strtok(resp_buff, " "); //*
temp = strtok(NULL, " "); //SEARCH
while(strcmp(temp = strtok(NULL, " "), "OK") != 0 ) {
new_messages[i++] = atoi(temp);
total_unseen++;
}
//temp = malloc(BUFFER_SIZE*sizeof(char));
bzero(resp_buff,sizeof(resp_buff));

printf("You have %d new email(s)\n", total_unseen);
printf(" _____________________________________________________________________________________________________________________________________________\n");
printf("| New emails |\n");
printf("|_____________________________________________________________________________________________________________________________________________|\n");
int num=0;
while(new_messages[num] != 0){
display_header(ssl, new_messages[num++]);
printf("|____________________________________________________________________________________________________________________________________________|\n");
}

return total_unseen;
}



void display_email(SSL *ssl, int n){
char resp[BUFFER_SIZE]="";
char resp_buff[BUFFER_SIZE]="";
char buff[BUFFER_SIZE]="";
char num[10]="";
char* temp = calloc(BUFFER_SIZE, sizeof(char));

sprintf(num, "%d", n);
printf("\n\n");
printf("_____________________________________________________________________________________________________________________________________________\n");

display_header(ssl, n);

strcpy(buff, "display fetch ");
strcat(buff, num);
strcat(buff, " body[text]\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff)); //fetch
//DEBUG printf("[RECEIVED] %s",resp_buff);
strcpy(resp, resp_buff);
temp = strtok(resp_buff, "\r\n");
while(strcmp(temp,"display OK Success") != 0){
SSL_read(ssl,resp_buff, sizeof (resp_buff));
strcat(resp, resp_buff);
temp = strtok(resp_buff, "\r\n");
}
temp = strtok(resp, "\r\n");
temp = strtok(NULL,"\r\n");
while(strcmp(temp, ")") != 0 ){
printf("%s\n",temp);
temp = strtok(NULL, "\r\n");
}
bzero(resp_buff,sizeof(resp_buff));
printf("_____________________________________________________________________________________________________________________\n");

}

void IMAP_request(SSL *ssl){
char resp_buff[BUFFER_SIZE]="";
char buff[BUFFER_SIZE]="";
char from[BUFFER_SIZE]="";
char authlog[BUFFER_SIZE]="";
char authpass[BUFFER_SIZE]="";
char decodepass[BUFFER_SIZE]="";
char temp[BUFFER_SIZE]="";


FILE* config_email = NULL;
config_email = fopen("config_email", "r+");
fgets(from,BUFFER_SIZE,config_email);
fgets(authlog,BUFFER_SIZE,config_email);
fgets(authpass,BUFFER_SIZE,config_email);
from[strlen(from) - 1] = '\0';
authlog[strlen(authlog) - 1] = '\0';
authpass[strlen(authpass) - 1] = '\0';

SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);

strcpy(buff,"log login ");
strcat(buff,from);
strcat(buff," ");
strcat(decodepass, (char*)b64_decode(authpass, strlen(authpass)));
strcat(buff,decodepass);
strcat(buff,"\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
bzero(resp_buff,sizeof(resp_buff));

char line[BUFFER_SIZE] = "";
display_list(ssl);
printf("Please enter a command.\nEnter 'help' to display command list.(Enter 'quit' to close your connection)\n");

while(strcmp(temp,"quit")!=0){
fgets(line, BUFFER_SIZE, stdin);
strncpy(temp, line, 4);
if(strcmp(temp, "show")==0){
strncpy(temp, line+5, strlen(line)-1);
int n = atoi(temp);
display_email(ssl, n);
printf("Please enter a command.\nEnter 'help' to display command list.(Enter 'quit' to close your connection)\n");
}
else if(strcmp(temp, "help")==0){
printf("___________________________________________________\n");
printf("User commands : |\n");
printf("show (n°email) display a specific email |\n");
printf("quit close the connection |\n");
printf("help diplay commands list |\n");
printf("___________________________________________________|\n");
printf("Please enter a command.\nEnter 'help' to display command list.(Enter 'quit' to close your connection)\n");

}

}

strcpy(buff, "LOGOUT\r\n");
SSL_write(ssl, buff, strlen(buff));
//DEBUG printf("\n[Send] %s\n", buff);
SSL_read(ssl,resp_buff, sizeof (resp_buff));
//DEBUG printf("[RECEIVED] %s",resp_buff);
}


const char* get_ip_adress(const char* target_domain){
const char* target_ip;
struct in_addr *host_address;
Expand Down Expand Up @@ -158,7 +376,7 @@ int ssl_connect(char *arg) {
if(strcmp(arg,"send")==0){
connceted_fd = connect_to_server("smtp.gmail.com",465);
}else if (strcmp(arg,"read")==0){
connceted_fd = connect_to_server("pop.gmail.com",995);
connceted_fd = connect_to_server("imap.gmail.com",993);
}
if (connceted_fd != 0) {
SSL_set_fd(ssl, connceted_fd);
Expand Down Expand Up @@ -193,7 +411,7 @@ int ssl_connect(char *arg) {
printf("Error \xe2\x9C\x97\nPlease verify your email adress and password.\n");
}
}else if (strcmp(arg,"read")==0){
//TODO
IMAP_request(ssl);
}
}
}
Expand Down