From 7651043741b3c9ece5730f412e9a67d60a105c4c Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 27 Jan 2024 12:19:00 +0000 Subject: [PATCH] pop3 patch git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@117 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/pop3-list/pop3-list.c | 3 +- Library/POP3.c | 76 +++++++++++++++++++++++++++++++---- W3Version.h.p | 2 +- 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/Example/pop3-list/pop3-list.c b/Example/pop3-list/pop3-list.c index 2ccf98d..2f4f480 100644 --- a/Example/pop3-list/pop3-list.c +++ b/Example/pop3-list/pop3-list.c @@ -24,7 +24,8 @@ void list_handler(struct W3* w3, bool ok, char* data) { void login_handler(struct W3* w3) { printf("Logged in\n"); - W3_Set_Method(w3, "LIST"); + W3_Set_Method(w3, "RETR"); + W3_Set_Path(w3, "18"); W3_POP3_Send_Request(w3); } diff --git a/Library/POP3.c b/Library/POP3.c index 34bf677..ff7d508 100644 --- a/Library/POP3.c +++ b/Library/POP3.c @@ -27,23 +27,78 @@ void __W3_POP3_Request(struct W3* w3) { message[0] = 0; char* cbuf = malloc(2); cbuf[1] = 0; - bool newl_cond = true; + int newl_cond = 1; w3->generic = &newl_cond; + int octets = 0; + char* octets_n = malloc(1); + octets_n[0] = 0; while(true) { int len = __W3_Auto_Read(w3, buf, w3->readsize); if(len <= 0) break; int i; for(i = 0; i < len; i++) { char c = buf[i]; - if(c == '\r') continue; bool newl = false; - if(c == '\n' && newl_cond) { + if(newl_cond & (1 << 1)) { + if(c == '\n' && !(newl_cond & (1 << 4))){ + if(!(newl_cond & (1 << 2))){ + newl_cond |= (1 << 2); + } + }else if(c == '\n' && newl_cond & (1 << 4) && !(newl_cond & (1 << 5))){ + newl_cond |= (1 << 5); + char* oct = __W3_Concat(octets_n, " octets"); + __W3_Debug("LibW3-POP3", oct); + free(oct); + octets = atoi(octets_n) + 3; + free(octets_n); + octets_n = malloc(1); + octets_n[0] = 0; + }else if(!(newl_cond & (1 << 2))){ + if(c == ' ' & !(newl_cond & (1 << 4))){ + if(newl_cond & (1 << 3)){ + newl_cond |= (1 << 4); + }else{ + newl_cond |= (1 << 3); + } + }else if(newl_cond & (1 << 3)){ + if(newl_cond & (1 << 5)){ + int readlen = (len - i) > octets ? octets : (len - i); + octets -= readlen; + void* funcptr = __W3_Get_Event(w3, "pop3data"); + if(funcptr != NULL) { + void (*func)(struct W3*, bool, char*) = (void (*)(struct W3*, bool, char*))funcptr; + char* buffer = malloc(readlen + 1); + memcpy(buffer, buf + i, readlen); + buffer[readlen] = 0; + func(w3, true, buffer); + free(buffer); + } + if(octets == 0){ + __W3_Debug("LibW3-POP3", "Received all"); + newl_cond &= ~(1 << 1); + } + i += readlen; + }else if(newl_cond & (1 << 4)){ + }else{ + char* tmp = octets_n; + cbuf[0] = c; + octets_n = __W3_Concat(tmp, cbuf); + free(tmp); + } + } + } newl = true; - } else if(c == '.' && !newl_cond) { - newl_cond = true; + continue; + }else if(newl_cond & 1) { + if(c == '\n'){ + newl = true; + } + } else if(c == '.' && !(newl_cond & 1)) { + newl_cond |= 1; } + if(c == '\r') continue; if(newl) { - newl_cond = true; + newl_cond |= 1; if(login == 0) { char* str = __W3_Concat3("USER ", __W3_Get_Prop(w3, "POP3_USERNAME"), "\r\n"); __W3_Auto_Write(w3, str, strlen(str)); @@ -116,6 +171,7 @@ void __W3_POP3_Request(struct W3* w3) { } } } + free(octets_n); free(message); free(cbuf); free(buf); @@ -131,14 +187,18 @@ void W3_POP3_Send_Request(struct W3* w3) { if(w3->path != NULL && strlen(w3->path) != 0) { __W3_Auto_Write(w3, w3->path, strlen(w3->path)); } else { - *((bool*)w3->generic) = false; + *((int*)w3->generic) &= ~1; } __W3_Auto_Write(w3, "\r\n", 2); } else if(strcasecmp(w3->method, "RETR") == 0) { __W3_Auto_Write(w3, "RETR ", 5); if(w3->path != NULL && strlen(w3->path) != 0) { __W3_Auto_Write(w3, w3->path, strlen(w3->path)); - *((bool*)w3->generic) = false; + *((int*)w3->generic) |= (1 << 1); + *((int*)w3->generic) &= ~(1 << 2); + *((int*)w3->generic) &= ~(1 << 3); + *((int*)w3->generic) &= ~(1 << 4); + *((int*)w3->generic) &= ~(1 << 5); } __W3_Auto_Write(w3, "\r\n", 2); } diff --git a/W3Version.h.p b/W3Version.h.p index 434ab9f..9f4f893 100644 --- a/W3Version.h.p +++ b/W3Version.h.p @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "2.0A" \ +#define LIBW3_VERSION "2.0B" \ SUFFIX #ifdef __cplusplus -- 2.43.0