better email compilance

This commit is contained in:
SJ 2013-09-25 22:08:38 +02:00
parent e5dd2f06a2
commit 8a9b963fbf
3 changed files with 40 additions and 22 deletions

View File

@ -14,7 +14,7 @@
#define VERSION "0.1.25-master-branch" #define VERSION "0.1.25-master-branch"
#define BUILD 844 #define BUILD 845
#define HOSTID "mailarchiver" #define HOSTID "mailarchiver"

View File

@ -76,21 +76,36 @@ int is_last_complete_packet(char *s, int len, char *tagok, char *tagbad, int *po
} }
int read_response(int sd, char *buf, int buflen, char *tagok, struct __data *data, int use_ssl){ int read_response(int sd, char *buf, int buflen, int *seq, struct __data *data, int use_ssl){
int n, len=0; int i=0, n, len=0, rc=0;
char puf[MAXBUFSIZE]; char puf[MAXBUFSIZE], tagok[SMALLBUFSIZE], tagno[SMALLBUFSIZE];
snprintf(tagok, sizeof(tagok)-1, "\r\nA%d OK", *seq);
snprintf(tagno, sizeof(tagno)-1, "A%d NO", *seq);
memset(buf, 0, buflen); memset(buf, 0, buflen);
while(!strstr(buf, tagok)){ while(!strstr(buf, tagok)){
n = recvtimeoutssl(sd, puf, sizeof(puf), 10, use_ssl, data->ssl); n = recvtimeoutssl(sd, puf, sizeof(puf), 10, use_ssl, data->ssl);
printf("rcvd: %s", puf);
if(n + len < buflen) strncat(buf, puf, n); if(n + len < buflen) strncat(buf, puf, n);
else return 0; else goto END;
if(i == 0 && strstr(puf, tagno)) goto END;
len += n; len += n;
i++;
} }
return 1; rc = 1;
END:
(*seq)++;
return rc;
} }
@ -101,13 +116,10 @@ int process_imap_folder(int sd, int *seq, char *folder, struct session_data *sda
/* imap cmd: SELECT */ /* imap cmd: SELECT */
snprintf(tag, sizeof(tag)-1, "A%d", *seq); snprintf(tagok, sizeof(tagok)-1, "\r\nA%d OK", (*seq)++); snprintf(buf, sizeof(buf)-1, "A%d SELECT \"%s\"\r\n", *seq, folder);
snprintf(buf, sizeof(buf)-1, "%s SELECT \"%s\"\r\n", tag, folder);
n = write1(sd, buf, strlen(buf), use_ssl, data->ssl); n = write1(sd, buf, strlen(buf), use_ssl, data->ssl);
read_response(sd, buf, sizeof(buf), tagok, data, use_ssl); if(read_response(sd, buf, sizeof(buf), seq, data, use_ssl) == 0){
if(!strstr(buf, tagok)){
trimBuffer(buf); trimBuffer(buf);
printf("error: %s\n", buf); printf("error: %s\n", buf);
return rc; return rc;
@ -226,7 +238,7 @@ int process_imap_folder(int sd, int *seq, char *folder, struct session_data *sda
int connect_to_imap_server(int sd, int *seq, char *username, char *password, int port, struct __data *data, int use_ssl){ int connect_to_imap_server(int sd, int *seq, char *username, char *password, int port, struct __data *data, int use_ssl){
int n; int n;
char tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], buf[MAXBUFSIZE]; char buf[MAXBUFSIZE];
X509* server_cert; X509* server_cert;
char *str; char *str;
@ -271,22 +283,18 @@ int connect_to_imap_server(int sd, int *seq, char *username, char *password, int
/* imap cmd: CAPABILITY */ /* imap cmd: CAPABILITY */
snprintf(tag, sizeof(tag)-1, "A%d", *seq); snprintf(tagok, sizeof(tagok)-1, "A%d OK", (*seq)++); /*snprintf(buf, sizeof(buf)-1, "A%d CAPABILITY\r\n", *seq);
snprintf(buf, sizeof(buf)-1, "%s CAPABILITY\r\n", tag);
write1(sd, buf, strlen(buf), use_ssl, data->ssl); write1(sd, buf, strlen(buf), use_ssl, data->ssl);
read_response(sd, buf, sizeof(buf), tagok, data, use_ssl); read_response(sd, buf, sizeof(buf), seq, data, use_ssl);*/
/* imap cmd: LOGIN */ /* imap cmd: LOGIN */
snprintf(tag, sizeof(tag)-1, "A%d", *seq); snprintf(tagok, sizeof(tagok)-1, "A%d OK", (*seq)++); snprintf(buf, sizeof(buf)-1, "A%d LOGIN %s \"%s\"\r\n", *seq, username, password);
snprintf(buf, sizeof(buf)-1, "%s LOGIN %s \"%s\"\r\n", tag, username, password);
write1(sd, buf, strlen(buf), use_ssl, data->ssl); write1(sd, buf, strlen(buf), use_ssl, data->ssl);
read_response(sd, buf, sizeof(buf), tagok, data, use_ssl); if(read_response(sd, buf, sizeof(buf), seq, data, use_ssl) == 0){
if(!strstr(buf, tagok)){
printf("login failed, server reponse: %s\n", buf); printf("login failed, server reponse: %s\n", buf);
return ERR; return ERR;
} }
@ -295,9 +303,17 @@ int connect_to_imap_server(int sd, int *seq, char *username, char *password, int
} }
void send_imap_close(int sd, int *seq, struct __data *data, int use_ssl){
char puf[SMALLBUFSIZE];
snprintf(puf, sizeof(puf)-1, "A%d CLOSE\r\n", *seq);
write1(sd, puf, strlen(puf), use_ssl, data->ssl);
}
void close_connection(int sd, struct __data *data, int use_ssl){ void close_connection(int sd, struct __data *data, int use_ssl){
close(sd); close(sd);
if(use_ssl == 1){ if(use_ssl == 1){
SSL_shutdown(data->ssl); SSL_shutdown(data->ssl);
SSL_free(data->ssl); SSL_free(data->ssl);

View File

@ -41,6 +41,7 @@ int list_folders(int sd, int *seq, int use_ssl, struct __data *data);
int process_imap_folder(int sd, int *seq, char *folder, struct session_data *sdata, struct __data *data, int use_ssl, int dryrun, struct __config *cfg); int process_imap_folder(int sd, int *seq, char *folder, struct session_data *sdata, struct __data *data, int use_ssl, int dryrun, struct __config *cfg);
int connect_to_pop3_server(int sd, char *username, char *password, int port, struct __data *data, int use_ssl); int connect_to_pop3_server(int sd, char *username, char *password, int port, struct __data *data, int use_ssl);
int process_pop3_emails(int sd, struct session_data *sdata, struct __data *data, int use_ssl, int dryrun, struct __config *cfg); int process_pop3_emails(int sd, struct session_data *sdata, struct __data *data, int use_ssl, int dryrun, struct __config *cfg);
void send_imap_close(int sd, int *seq, struct __data *data, int use_ssl);
void close_connection(int sd, struct __data *data, int use_ssl); void close_connection(int sd, struct __data *data, int use_ssl);
void update_import_job_stat(struct session_data *sdata, struct __data *data); void update_import_job_stat(struct session_data *sdata, struct __data *data);
@ -318,7 +319,7 @@ int import_from_imap_server(char *server, char *username, char *password, int po
if(quiet == 0) printf("SKIPPING FOLDER: %s\n", (char *)q->str); if(quiet == 0) printf("SKIPPING FOLDER: %s\n", (char *)q->str);
} }
else { else {
if(quiet == 0) printf("processing folder: %s...\n", (char *)q->str); if(quiet == 0) printf("processing folder: %s... ", (char *)q->str);
if(process_imap_folder(sd, &seq, q->str, sdata, data, use_ssl, dryrun, cfg) == ERR) ret = ERR; if(process_imap_folder(sd, &seq, q->str, sdata, data, use_ssl, dryrun, cfg) == ERR) ret = ERR;
} }
@ -330,6 +331,7 @@ int import_from_imap_server(char *server, char *username, char *password, int po
} }
} }
send_imap_close(sd, &seq, data, use_ssl);
close_connection(sd, data, use_ssl); close_connection(sd, data, use_ssl);