From: nishi Date: Sat, 27 Jan 2024 05:19:48 +0000 (+0000) Subject: pop3 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=c80b6860231369a61c33d0e7b6417bb83a5d2a3b;p=libw3.git pop3 git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@108 d27a3e52-49c5-7645-884c-6793ebffc270 --- diff --git a/Example/pop3-list/pop3-list.c b/Example/pop3-list/pop3-list.c index c44bc95..5f41215 100644 --- a/Example/pop3-list/pop3-list.c +++ b/Example/pop3-list/pop3-list.c @@ -17,7 +17,16 @@ #include #include -void list_handler(struct W3* w3, char* data, size_t size) {} +void list_handler(struct W3* w3, bool ok, char* data) { + printf("[%s] %s\n", ok ? " OK " : " FAIL ", data); + W3_POP3_Disconnect(w3); +} + +void login_handler(struct W3* w3){ + printf("Logged in\n"); + W3_Set_Method(w3, "LIST"); + W3_POP3_Send_Request(w3); +} int main(int argc, char** argv) { if(argc < 4) { @@ -27,6 +36,7 @@ int main(int argc, char** argv) { struct W3* w3 = W3_Create("pop3", argv[1], 110); W3_POP3_Set_Username(w3, argv[2]); W3_POP3_Set_Password(w3, argv[3]); - W3_Set_Method(w3, "LIST"); + W3_On(w3, "login", login_handler); + W3_On(w3, "pop3data", list_handler); W3_Send_Request(w3); } diff --git a/Library/POP3.c b/Library/POP3.c index cb521bc..5a8a0ac 100644 --- a/Library/POP3.c +++ b/Library/POP3.c @@ -27,6 +27,8 @@ void __W3_POP3_Request(struct W3* w3) { message[0] = 0; char* cbuf = malloc(2); cbuf[1] = 0; + bool newl_cond = true; + w3->generic = &newl_cond; while(true) { int len = __W3_Auto_Read(w3, buf, w3->readsize); if(len <= 0) break; @@ -34,7 +36,14 @@ void __W3_POP3_Request(struct W3* w3) { for(i = 0; i < len; i++) { char c = buf[i]; if(c == '\r') continue; - if(c == '\n') { + bool newl = false; + if(c == '\n' && newl_cond){ + newl = true; + }else if(c == '.' && !newl_cond){ + newl_cond = true; + } + if(newl) { + newl_cond = true; if(login == 0) { char* str = __W3_Concat3("USER ", __W3_Get_Prop(w3, "POP3_USERNAME"), "\r\n"); __W3_Auto_Write(w3, str, strlen(str)); @@ -58,6 +67,11 @@ void __W3_POP3_Request(struct W3* w3) { } __W3_Debug("LibW3-POP3", "Login successful"); } else { + void* funcptr = __W3_Get_Event(w3, "pop3data"); + if(funcptr != NULL) { + void (*func)(struct W3*, bool, char*) = (void (*)(struct W3*, bool, char*))funcptr; + func(w3, true, message); + } } } else if(phase == 4) { /* ERR */ @@ -71,6 +85,11 @@ void __W3_POP3_Request(struct W3* w3) { login = 0; return; } else { + void* funcptr = __W3_Get_Event(w3, "pop3data"); + if(funcptr != NULL) { + void (*func)(struct W3*, bool, char*) = (void (*)(struct W3*, bool, char*))funcptr; + func(w3, false, message); + } } } } @@ -105,4 +124,13 @@ void W3_POP3_Set_Username(struct W3* w3, const char* username) { __W3_Add_Prop(w void W3_POP3_Set_Password(struct W3* w3, const char* password) { __W3_Add_Prop(w3, "POP3_PASSWORD", password); } -void W3_POP3_Send_Request(struct W3* w3) {} +void W3_POP3_Send_Request(struct W3* w3) { + if(strcasecmp(w3->method, "LIST") == 0){ + *((bool*)w3->generic) = false; + __W3_Auto_Write(w3, "LIST\r\n", 6); + } +} + +void W3_POP3_Disconnect(struct W3* w3){ + __W3_Auto_Write(w3, "QUIT\r\n", 6); +} diff --git a/Library/W3Core.h b/Library/W3Core.h index 3fb33d2..6240fbe 100644 --- a/Library/W3Core.h +++ b/Library/W3Core.h @@ -25,6 +25,7 @@ struct W3 { size_t size; /* Size of the data */ size_t readsize; /* Read buffer size, default is 512 */ char** props; /* Properties */ + void* generic; /* Depends on the protocol */ #ifdef SSL_SUPPORT void* ssl; /* Actually SSL*, NULL if no SSL */ void* ssl_ctx; /* Actually SSL_CTX* */ diff --git a/Library/W3POP3.h b/Library/W3POP3.h index 464d524..003a34b 100644 --- a/Library/W3POP3.h +++ b/Library/W3POP3.h @@ -12,6 +12,7 @@ void __W3_POP3_Request(struct W3* w3); void W3_POP3_Set_Username(struct W3* w3, const char* username); void W3_POP3_Set_Password(struct W3* w3, const char* password); void W3_POP3_Send_Request(struct W3* w3); +void W3_POP3_Disconnect(struct W3* w3); #ifdef __cplusplus } diff --git a/W3Version.h.p b/W3Version.h.p index b3e03fd..8124100 100644 --- a/W3Version.h.p +++ b/W3Version.h.p @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "1.6B" \ +#define LIBW3_VERSION "1.6C" \ SUFFIX #ifdef __cplusplus