From d7792ee9f3e8f2175eb65e67ce9f303b7999a158 Mon Sep 17 00:00:00 2001 From: SJ Date: Thu, 10 Jan 2013 22:30:30 +0100 Subject: [PATCH] fixed a bug in imap parser --- src/config.h | 2 +- src/imap.c | 31 ++++++++++++++++++++++--------- src/pilerimport.c | 15 ++++----------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/config.h b/src/config.h index 650e1f1d..e46aad52 100644 --- a/src/config.h +++ b/src/config.h @@ -13,7 +13,7 @@ #define VERSION "0.1.23-master-branch" -#define BUILD 749 +#define BUILD 751 #define HOSTID "mailarchiver" diff --git a/src/imap.c b/src/imap.c index c6ab4ca4..eb1c02fc 100644 --- a/src/imap.c +++ b/src/imap.c @@ -93,7 +93,7 @@ int process_imap_folder(int sd, int *seq, char *folder, struct session_data *sda p = strstr(buf, " EXISTS"); if(p){ *p = '\0'; - p = strrchr(buf, '\n'); + p = strrchr(buf, ' '); if(p){ while(!isdigit(*p)){ p++; } messages = atoi(p); @@ -286,16 +286,28 @@ void close_connection(int sd, struct __data *data, int use_ssl){ int list_folders(int sd, int *seq, char *folders, int foldersize, int use_ssl, struct __data *data){ - char *p, *q, tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], buf[MAXBUFSIZE], puf[MAXBUFSIZE]; + char *p, *q, tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], buf[3*MAXBUFSIZE+3], puf[MAXBUFSIZE]; + int len=0, n; + + memset(buf, 0, sizeof(buf)); snprintf(tag, sizeof(tag)-1, "A%d", *seq); snprintf(tagok, sizeof(tagok)-1, "A%d OK", (*seq)++); - //snprintf(buf, sizeof(buf)-1, "%s LIST \"\" %%\r\n", tag); - snprintf(buf, sizeof(buf)-1, "%s LIST \"\" \"*\"\r\n", tag); + //snprintf(puf, sizeof(puf)-1, "%s LIST \"\" %%\r\n", tag); + snprintf(puf, sizeof(puf)-1, "%s LIST \"\" \"*\"\r\n", tag); - write1(sd, buf, use_ssl, data->ssl); + write1(sd, puf, use_ssl, data->ssl); - recvtimeoutssl(sd, buf, sizeof(buf), 10, use_ssl, data->ssl); + while(1){ + n = recvtimeoutssl(sd, puf, sizeof(puf), 10, use_ssl, data->ssl); + if(len + n < sizeof(buf)){ + memcpy(&buf[len], puf, n); + len += n; + } + else break; + if(strstr(buf, tagok)) break; + } + p = &buf[0]; do { memset(puf, 0, sizeof(puf)); @@ -303,10 +315,11 @@ int list_folders(int sd, int *seq, char *folders, int foldersize, int use_ssl, s trimBuffer(puf); if(strncmp(puf, "* LIST ", 7) == 0){ - q = strstr(puf, "\".\""); + + q = strstr(puf, ") \""); if(q){ - q += 3; - + q += 5; + if(*q == ' ') q++; if(*q == '"') q++; diff --git a/src/pilerimport.c b/src/pilerimport.c index 58e9baa7..1381b3bb 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -21,7 +21,7 @@ #include -#define SKIPLIST "junk,trash,spam,draft" +#define SKIPLIST "junk,trash,spam,draft," #define MBOX_ARGS 1024 extern char *optarg; @@ -243,7 +243,7 @@ int import_from_maildir(char *directory, struct session_data *sdata, struct __da int import_from_imap_server(char *server, char *username, char *password, int port, struct session_data *sdata, struct __data *data, char *skiplist, struct __config *cfg){ int rc=ERR, ret=OK, sd, seq=1, skipmatch, use_ssl=0; char *p, puf[SMALLBUFSIZE]; - char *q, muf[SMALLBUFSIZE]; + char muf[SMALLBUFSIZE]; char folders[MAXBUFSIZE]; @@ -274,15 +274,8 @@ int import_from_imap_server(char *server, char *username, char *password, int po skipmatch = 0; if(skiplist && strlen(skiplist) > 0){ - q = skiplist; - do { - memset(muf, 0, sizeof(muf)); - q = split(q, ',', muf, sizeof(muf)-1); - if(strncasecmp(puf, muf, strlen(muf)) == 0){ - skipmatch = 1; - break; - } - } while(q); + snprintf(muf, sizeof(muf)-1, "%s,", puf); + if(strstr(skiplist, muf)) skipmatch = 1; } if(skipmatch == 1){