imap folder listing fix

This commit is contained in:
SJ 2013-09-04 22:42:06 +02:00
parent 016794767c
commit 4a961e4ecc
4 changed files with 69 additions and 42 deletions

View File

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

View File

@ -262,6 +262,7 @@ struct __data {
char recursive_folder_names;
char starttls[TINYBUFSIZE];
struct node *mydomains[MAXHASH];
struct node *imapfolders[MAXHASH];
struct import *import;
#ifdef NEED_MYSQL

View File

@ -134,7 +134,7 @@ int process_imap_folder(int sd, int *seq, char *folder, struct session_data *sda
snprintf(buf, sizeof(buf)-1, "%s FETCH %d (BODY.PEEK[])\r\n", tag, i);
snprintf(filename, sizeof(filename)-1, "%s-%d.txt", folder, i);
snprintf(filename, sizeof(filename)-1, "imap-%d.txt", i);
unlink(filename);
fd = open(filename, O_CREAT|O_EXCL|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
@ -272,7 +272,7 @@ int connect_to_imap_server(int sd, int *seq, char *username, char *password, int
snprintf(buf, sizeof(buf)-1, "%s LOGIN %s \"%s\"\r\n", tag, username, password);
write1(sd, buf, strlen(buf), use_ssl, data->ssl);
n = recvtimeoutssl(sd, buf, sizeof(buf), 10, use_ssl, data->ssl);
read_response(sd, buf, sizeof(buf), tagok, data, use_ssl);
if(strncmp(buf, tagok, strlen(tagok))){
printf("login failed, server reponse: %s\n", buf);
@ -295,11 +295,14 @@ 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[3*MAXBUFSIZE+3], puf[MAXBUFSIZE];
int len=0, n;
int list_folders(int sd, int *seq, int use_ssl, struct __data *data){
char *p, *q, *buf, tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], puf[MAXBUFSIZE];
int len=MAXBUFSIZE+3, pos=0, n, rc=ERR;;
memset(buf, 0, sizeof(buf));
buf = malloc(len);
if(!buf) return rc;
memset(buf, 0, len);
snprintf(tag, sizeof(tag)-1, "A%d", *seq); snprintf(tagok, sizeof(tagok)-1, "A%d OK", (*seq)++);
//snprintf(puf, sizeof(puf)-1, "%s LIST \"\" %%\r\n", tag);
@ -309,16 +312,27 @@ int list_folders(int sd, int *seq, char *folders, int foldersize, int use_ssl, s
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;
if(pos + n >= len){
q = realloc(buf, len+MAXBUFSIZE+1);
if(!q){
printf("realloc failure: %d bytes\n", pos+MAXBUFSIZE+1);
goto ENDE_FOLDERS;
}
else break;
buf = q;
memset(buf+pos, 0, MAXBUFSIZE+1);
len += MAXBUFSIZE+1;
}
memcpy(buf + pos, puf, n);
pos += n;
if(strstr(buf, tagok)) break;
}
p = &buf[0];
p = buf;
do {
memset(puf, 0, sizeof(puf));
p = split(p, '\n', puf, sizeof(puf)-1);
@ -335,9 +349,7 @@ int list_folders(int sd, int *seq, char *folders, int foldersize, int use_ssl, s
if(q[strlen(q)-1] == '"') q[strlen(q)-1] = '\0';
strncat(folders, "\n", foldersize-1);
strncat(folders, q, foldersize-1);
addnode(data->imapfolders, q);
}
}
else {
@ -346,7 +358,14 @@ int list_folders(int sd, int *seq, char *folders, int foldersize, int use_ssl, s
} while(p);
return 0;
rc = OK;
ENDE_FOLDERS:
free(buf);
return rc;
}

View File

@ -37,7 +37,7 @@ int import_from_gui=0;
int connect_to_imap_server(int sd, int *seq, char *username, char *password, int port, struct __data *data, int use_ssl);
int list_folders(int sd, int *seq, char *folders, int foldersize, int use_ssl, struct __data *data);
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 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);
@ -257,12 +257,11 @@ 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, int dryrun, struct __config *cfg){
int rc=ERR, ret=OK, sd, seq=1, skipmatch, use_ssl=0;
char *p, puf[SMALLBUFSIZE];
char muf[SMALLBUFSIZE];
char folders[MAXBUFSIZE];
int i, rc=ERR, ret=OK, sd, seq=1, skipmatch, use_ssl=0;
char port_string[6];
struct addrinfo hints, *res;
struct node *q;
snprintf(port_string, sizeof(port_string)-1, "%d", port);
@ -297,33 +296,39 @@ int import_from_imap_server(char *server, char *username, char *password, int po
}
list_folders(sd, &seq, &folders[0], sizeof(folders), use_ssl, data);
inithash(data->imapfolders);
rc = list_folders(sd, &seq, use_ssl, data);
if(rc == ERR) goto ENDE_IMAP;
p = &folders[0];
do {
memset(puf, 0, sizeof(puf));
p = split(p, '\n', puf, sizeof(puf)-1);
for(i=0;i<MAXHASH;i++){
q = data->imapfolders[i];
while(q != NULL){
if(strlen(puf) < 1) continue;
if(q && q->str && strlen(q->str) > 1){
skipmatch = 0;
if(skiplist && strlen(skiplist) > 0){
snprintf(muf, sizeof(muf)-1, "%s,", puf);
if(strstr(skiplist, muf)) skipmatch = 1;
if(strstr(skiplist, q->str)) skipmatch = 1;
}
if(skipmatch == 1){
if(quiet == 0) printf("SKIPPING FOLDER: %s\n", puf);
continue;
if(quiet == 0) printf("SKIPPING FOLDER: %s\n", (char *)q->str);
}
else {
if(quiet == 0) printf("processing folder: %s...\n", (char *)q->str);
if(process_imap_folder(sd, &seq, q->str, sdata, data, use_ssl, dryrun, cfg) == ERR) ret = ERR;
}
if(quiet == 0) printf("processing folder: %s... ", puf);
}
if(process_imap_folder(sd, &seq, puf, sdata, data, use_ssl, dryrun, cfg) == ERR) ret = ERR;
q = q->r;
} while(p);
}
}
close_connection(sd, data, use_ssl);
@ -331,6 +336,8 @@ int import_from_imap_server(char *server, char *username, char *password, int po
ENDE_IMAP:
freeaddrinfo(res);
clearhash(data->imapfolders);
return ret;
}