imap fixes

This commit is contained in:
SJ 2014-07-22 16:00:03 +02:00
parent 850445a324
commit 537641afd9
7 changed files with 67 additions and 51 deletions

View File

@ -14,7 +14,7 @@
#define VERSION "0.1.25-rc3" #define VERSION "0.1.25-rc3"
#define BUILD 880 #define BUILD 881
#define HOSTID "mailarchiver" #define HOSTID "mailarchiver"

View File

@ -87,7 +87,7 @@ END:
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 rc=ERR, i, n, messages=0, len, readlen, fd, nreads, readpos, finished, msglen, msg_written_len, tagoklen, tagbadlen; int rc=ERR, i, n, messages=0, len, readlen, fd, nreads, readpos, finished, msglen, msg_written_len, tagoklen, tagbadlen, result;
char *p, tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], tagbad[SMALLBUFSIZE], buf[MAXBUFSIZE], puf[MAXBUFSIZE], filename[SMALLBUFSIZE]; char *p, tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], tagbad[SMALLBUFSIZE], buf[MAXBUFSIZE], puf[MAXBUFSIZE], filename[SMALLBUFSIZE];
/* imap cmd: SELECT */ /* imap cmd: SELECT */
@ -159,51 +159,63 @@ int process_imap_folder(int sd, int *seq, char *folder, struct session_data *sda
do { do {
nreads++; nreads++;
memset(puf, 0, sizeof(puf)); memset(puf, 0, sizeof(puf));
p = split(p, '\n', puf, sizeof(puf)-1); p = split(p, '\n', puf, sizeof(puf)-1, &result);
len = strlen(puf); len = strlen(puf);
if(nreads == 1){ if(result == 1){
// process a complete line
if(strcasestr(puf, " FETCH ")){ if(nreads == 1){
msglen = get_message_length_from_imap_answer(puf);
if(msglen == 0){ if(strcasestr(puf, " FETCH ")){
finished = 1; msglen = get_message_length_from_imap_answer(puf);
break;
if(msglen == 0){
finished = 1;
break;
}
continue;
} }
continue; if(strcasestr(puf, " BYE")){
} printf("imap server sent BYE response: '%s'\n", puf);
close(fd);
unlink(filename);
return ERR;
}
if(strcasestr(puf, " BYE")){
printf("imap server sent BYE response: '%s'\n", puf);
close(fd);
unlink(filename);
return ERR;
} }
}
if(len > 0 && msg_written_len < msglen){
if(len > 0){
if(msg_written_len < msglen){
write(fd, puf, len); write(fd, puf, len);
write(fd, "\n", 1); write(fd, "\n", 1);
msg_written_len += len + 1; msg_written_len += len + 1;
} }
}
if(strncmp(puf, tagok, tagoklen) == 0){ if(strncmp(puf, tagok, tagoklen) == 0){
finished = 1; finished = 1;
break;
}
if(strncmp(puf, tagbad, tagbadlen) == 0){
printf("ERROR happened reading the message!\n");
finished = 1;
break;
}
}
else {
// prepend the last incomplete line back to 'buf'
snprintf(buf, sizeof(buf)-2, "%s", puf);
readpos = len;
break; break;
} }
if(strncmp(puf, tagbad, tagbadlen) == 0){
printf("ERROR happened reading the message!\n");
finished = 1;
break;
}
} while(p); } while(p);
} }
else { else {
readpos += n; readpos += n;
@ -342,7 +354,7 @@ void close_connection(int sd, struct __data *data, int use_ssl){
int list_folders(int sd, int *seq, int use_ssl, struct __data *data){ int list_folders(int sd, int *seq, int use_ssl, struct __data *data){
char *p, *q, *r, *buf, *ruf, tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], puf[MAXBUFSIZE]; char *p, *q, *r, *buf, *ruf, tag[SMALLBUFSIZE], tagok[SMALLBUFSIZE], puf[MAXBUFSIZE];
int len=MAXBUFSIZE+3, pos=0, n, rc=ERR, fldrlen=0; int len=MAXBUFSIZE+3, pos=0, n, rc=ERR, fldrlen=0, result;
printf("List of IMAP folders:\n"); printf("List of IMAP folders:\n");
@ -382,7 +394,7 @@ int list_folders(int sd, int *seq, int use_ssl, struct __data *data){
p = buf; p = buf;
do { do {
memset(puf, 0, sizeof(puf)); memset(puf, 0, sizeof(puf));
p = split(p, '\n', puf, sizeof(puf)-1); p = split(p, '\n', puf, sizeof(puf)-1, &result);
trimBuffer(puf); trimBuffer(puf);
if(strncmp(puf, "* LIST ", 7) == 0 || fldrlen){ if(strncmp(puf, "* LIST ", 7) == 0 || fldrlen){

View File

@ -146,7 +146,7 @@ int update_metadata_reference(struct session_data *sdata, struct _state *state,
int store_meta_data(struct session_data *sdata, struct _state *state, struct __data *data, struct __config *cfg){ int store_meta_data(struct session_data *sdata, struct _state *state, struct __data *data, struct __config *cfg){
int rc, ret=ERR; int rc, ret=ERR, result;
char *subj, *p, s[MAXBUFSIZE], s2[SMALLBUFSIZE], vcode[2*DIGEST_LENGTH+1], ref[2*DIGEST_LENGTH+1]; char *subj, *p, s[MAXBUFSIZE], s2[SMALLBUFSIZE], vcode[2*DIGEST_LENGTH+1], ref[2*DIGEST_LENGTH+1];
uint64 id=0; uint64 id=0;
@ -172,7 +172,7 @@ int store_meta_data(struct session_data *sdata, struct _state *state, struct __d
p = state->b_from; p = state->b_from;
do { do {
memset(s2, 0, sizeof(s2)); memset(s2, 0, sizeof(s2));
p = split(p, ' ', s2, sizeof(s2)-1); p = split(p, ' ', s2, sizeof(s2)-1, &result);
if(s2[0] == '\0') continue; if(s2[0] == '\0') continue;

View File

@ -112,23 +112,27 @@ void replaceCharacterInBuffer(char *p, char from, char to){
* split a string by a character as delimiter * split a string by a character as delimiter
*/ */
char *split(char *row, int ch, char *s, int size){ char *split(char *str, int ch, char *buf, int buflen, int *result){
char *r; char *p;
if(row == NULL || s == NULL) *result = 0;
return NULL;
r = strchr(row, ch); if(str == NULL || buf == NULL || buflen < 2) return NULL;
if(r) *r = '\0';
snprintf(s, size, "%s", row); p = strchr(str, ch);
if(p){
if(r){ *p = '\0';
*r = ch;
r++;
} }
return r; snprintf(buf, buflen, "%s", str);
if(p){
*p = ch;
*result = 1;
p++;
}
return p;
} }

View File

@ -21,7 +21,7 @@ long tvdiff(struct timeval a, struct timeval b);
int searchStringInBuffer(char *s, int len1, char *what, int len2); int searchStringInBuffer(char *s, int len1, char *what, int len2);
int countCharacterInBuffer(char *p, char c); int countCharacterInBuffer(char *p, char c);
void replaceCharacterInBuffer(char *p, char from, char to); void replaceCharacterInBuffer(char *p, char from, char to);
char *split(char *row, int ch, char *s, int size); char *split(char *str, int ch, char *buf, int buflen, int *result);
char *split_str(char *row, char *what, char *s, int size); char *split_str(char *row, char *what, char *s, int size);
int trimBuffer(char *s); int trimBuffer(char *s);
int extractEmail(char *rawmail, char *email); int extractEmail(char *rawmail, char *email);

View File

@ -173,7 +173,7 @@ void storno_attachment(struct _state *state){
int parse_line(char *buf, struct _state *state, struct session_data *sdata, int take_into_pieces, char *writebuffer, int writebuffersize, char *abuffer, int abuffersize, struct __data *data, struct __config *cfg){ int parse_line(char *buf, struct _state *state, struct session_data *sdata, int take_into_pieces, char *writebuffer, int writebuffersize, char *abuffer, int abuffersize, struct __data *data, struct __config *cfg){
char *p, *q, puf[SMALLBUFSIZE]; char *p, *q, puf[SMALLBUFSIZE];
unsigned char b64buffer[MAXBUFSIZE]; unsigned char b64buffer[MAXBUFSIZE];
int n64, len, writelen, boundary_line=0; int n64, len, writelen, boundary_line=0, result;
if(cfg->debug == 1) printf("line: %s", buf); if(cfg->debug == 1) printf("line: %s", buf);
@ -635,7 +635,7 @@ int parse_line(char *buf, struct _state *state, struct session_data *sdata, int
do { do {
memset(puf, 0, sizeof(puf)); memset(puf, 0, sizeof(puf));
p = split(p, ' ', puf, sizeof(puf)-1); p = split(p, ' ', puf, sizeof(puf)-1, &result);
if(puf[0] == '\0') continue; if(puf[0] == '\0') continue;

View File

@ -59,12 +59,12 @@ ENDE:
int remove_message_frame_files(char *s, char *update_meta_sql, struct session_data *sdata, struct __config *cfg){ int remove_message_frame_files(char *s, char *update_meta_sql, struct session_data *sdata, struct __config *cfg){
char *p, puf[SMALLBUFSIZE], filename[SMALLBUFSIZE]; char *p, puf[SMALLBUFSIZE], filename[SMALLBUFSIZE];
int n=0; int n=0, result;
struct stat st; struct stat st;
p = s; p = s;
do { do {
p = split(p, ' ', puf, sizeof(puf)-1); p = split(p, ' ', puf, sizeof(puf)-1, &result);
if(strlen(puf) == RND_STR_LEN){ if(strlen(puf) == RND_STR_LEN){
snprintf(filename, sizeof(filename)-1, "%s/%02x/%c%c%c/%c%c/%c%c/%s.m", cfg->queuedir, cfg->server_id, puf[8], puf[9], puf[10], puf[RND_STR_LEN-4], puf[RND_STR_LEN-3], puf[RND_STR_LEN-2], puf[RND_STR_LEN-1], puf); snprintf(filename, sizeof(filename)-1, "%s/%02x/%c%c%c/%c%c/%c%c/%s.m", cfg->queuedir, cfg->server_id, puf[8], puf[9], puf[10], puf[RND_STR_LEN-4], puf[RND_STR_LEN-3], puf[RND_STR_LEN-2], puf[RND_STR_LEN-1], puf);