From da65977b3df340d3cc56f9cecb11beb521477446 Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 27 Jan 2024 05:01:09 +0000 Subject: [PATCH] pop3 git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@107 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/pop3-list/pop3-list.c | 5 +-- Library/Core.c | 8 ++-- Library/DNS.c | 2 +- Library/POP3.c | 72 ++++++++++++++++++++--------------- Library/Util.c | 4 +- Library/W3POP3.h | 1 + 6 files changed, 51 insertions(+), 41 deletions(-) diff --git a/Example/pop3-list/pop3-list.c b/Example/pop3-list/pop3-list.c index 636c5b2..c44bc95 100644 --- a/Example/pop3-list/pop3-list.c +++ b/Example/pop3-list/pop3-list.c @@ -17,11 +17,10 @@ #include #include -void list_handler(struct W3* w3, char* data, size_t size) { -} +void list_handler(struct W3* w3, char* data, size_t size) {} int main(int argc, char** argv) { - if(argc < 4){ + if(argc < 4) { return 1; } W3_Library_Init(); diff --git a/Library/Core.c b/Library/Core.c index 21f2d40..ae94ffc 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -71,7 +71,7 @@ struct W3* W3_Create(const char* protocol, const char* hostname, int port) { if(strcmp(protocol, "http") == 0) { #ifdef SSL_SUPPORT } else if(strcmp(protocol, "https") == 0) { - } else if(strcmp(protocol, "pop3s") == 0){ + } else if(strcmp(protocol, "pop3s") == 0) { #endif } else if(strcmp(protocol, "gopher") == 0) { } else if(strcmp(protocol, "pop3") == 0) { @@ -112,7 +112,7 @@ void W3_Set_Path(struct W3* w3, const char* path) { void W3_Send_Request(struct W3* w3) { if(strcmp(w3->protocol, "http") == 0 #ifdef SSL_SUPPORT - || strcmp(w3->protocol, "https") == 0 + || strcmp(w3->protocol, "https") == 0 #endif ) { __W3_HTTP_Request(w3); @@ -120,9 +120,9 @@ void W3_Send_Request(struct W3* w3) { __W3_Gopher_Request(w3); } else if(strcmp(w3->protocol, "pop3") == 0 #ifdef SSL_SUPPORT - || strcmp(w3->protocol, "pop3s") == 0 + || strcmp(w3->protocol, "pop3s") == 0 #endif - ){ + ) { __W3_POP3_Request(w3); } else if(strcmp(w3->protocol, "file") == 0) { __W3_File_Request(w3); diff --git a/Library/DNS.c b/Library/DNS.c index c04a5e2..4efabfb 100644 --- a/Library/DNS.c +++ b/Library/DNS.c @@ -7,8 +7,8 @@ #include #ifdef __MINGW32__ -#include #include +#include #include #else #include diff --git a/Library/POP3.c b/Library/POP3.c index 4595355..cb521bc 100644 --- a/Library/POP3.c +++ b/Library/POP3.c @@ -4,16 +4,18 @@ #include "W3Core.h" #include "W3Util.h" +#include #include #include -#include + +extern int strcasecmp(const char* s1, const char* s2); void __W3_POP3_Request(struct W3* w3) { - if(__W3_Get_Prop(w3, "POP3_USERNAME") == NULL || __W3_Get_Prop(w3, "POP3_PASSWORD") == NULL){ + if(__W3_Get_Prop(w3, "POP3_USERNAME") == NULL || __W3_Get_Prop(w3, "POP3_PASSWORD") == NULL) { __W3_Debug("LibW3-POP3", "Set the username/password"); void* funcptr = __W3_Get_Event(w3, "error"); - if(funcptr != NULL){ - void(*func)(struct W3*, const char*) = (void(*)(struct W3*, const char*))funcptr; + if(funcptr != NULL) { + void (*func)(struct W3*, const char*) = (void (*)(struct W3*, const char*))funcptr; func(w3, "did-not-auth"); } return; @@ -21,76 +23,86 @@ void __W3_POP3_Request(struct W3* w3) { char* buf = malloc(w3->readsize); int phase = 0; int login = 0; + char* message = malloc(1); + message[0] = 0; + char* cbuf = malloc(2); + cbuf[1] = 0; while(true) { int len = __W3_Auto_Read(w3, buf, w3->readsize); if(len <= 0) break; int i; - for(i = 0; i < len; i++){ - write(1, buf + i, 1); + for(i = 0; i < len; i++) { char c = buf[i]; if(c == '\r') continue; - if(c == '\n'){ - if(login == 0){ + if(c == '\n') { + if(login == 0) { char* str = __W3_Concat3("USER ", __W3_Get_Prop(w3, "POP3_USERNAME"), "\r\n"); __W3_Auto_Write(w3, str, strlen(str)); free(str); login = 1; - }else if(login == 1){ + } else if(login == 1) { char* str = __W3_Concat3("PASS ", __W3_Get_Prop(w3, "POP3_PASSWORD"), "\r\n"); __W3_Auto_Write(w3, str, strlen(str)); free(str); login = 2; - }else{ - if(phase == 3){ + } else { + if(phase == 3) { /* OK */ - if(login == 2){ + if(login == 2) { /* Login success */ login = 3; void* funcptr = __W3_Get_Event(w3, "login"); - if(funcptr != NULL){ - void(*func)(struct W3*, int) = (void(*)(struct W3*, int))funcptr; + if(funcptr != NULL) { + void (*func)(struct W3*, int) = (void (*)(struct W3*, int))funcptr; func(w3, 512); } __W3_Debug("LibW3-POP3", "Login successful"); - }else{ + } else { } - }else if(phase == 4){ + } else if(phase == 4) { /* ERR */ - if(login == 2){ + if(login == 2) { /* Login failed */ void* funcptr = __W3_Get_Event(w3, "error"); - if(funcptr != NULL){ - void(*func)(struct W3*, const char*) = (void(*)(struct W3*, const char*))funcptr; + if(funcptr != NULL) { + void (*func)(struct W3*, const char*) = (void (*)(struct W3*, const char*))funcptr; func(w3, "login-fail"); } login = 0; return; - }else{ + } else { } } } + free(message); + message = malloc(1); + message[0] = 0; phase = 0; continue; + } else if(phase == 3 || phase == 4) { + cbuf[0] = buf[i]; + char* tmp = message; + message = __W3_Concat(tmp, cbuf); + free(tmp); } - if(phase == 0){ - if(c == '+'){ + if(phase == 0) { + if(c == '+') { phase = 1; - }else if(c == '-'){ + } else if(c == '-') { phase = 2; } - }else if(phase == 1){ + } else if(phase == 1) { if(c == ' ') phase += 2; } } } + free(message); + free(cbuf); free(buf); } -void W3_POP3_Set_Username(struct W3* w3, const char* username){ - __W3_Add_Prop(w3, "POP3_USERNAME", username); -} +void W3_POP3_Set_Username(struct W3* w3, const char* username) { __W3_Add_Prop(w3, "POP3_USERNAME", username); } +void W3_POP3_Set_Password(struct W3* w3, const char* password) { __W3_Add_Prop(w3, "POP3_PASSWORD", password); } -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) {} diff --git a/Library/Util.c b/Library/Util.c index 59ecea8..9c461a4 100644 --- a/Library/Util.c +++ b/Library/Util.c @@ -119,15 +119,13 @@ bool __W3_Have_Prop(struct W3* w3, const char* name) { return false; } - -char* __W3_Get_Prop(struct W3* w3, const char* name){ +char* __W3_Get_Prop(struct W3* w3, const char* name) { if(w3->props == NULL) return NULL; int i; for(i = 0; w3->props[i] != NULL; i += 2) { if(strcmp(w3->props[i], name) == 0) return w3->props[i + 1]; } return NULL; - } void __W3_Add_Prop(struct W3* w3, const char* name, const char* value) { diff --git a/Library/W3POP3.h b/Library/W3POP3.h index 1682123..464d524 100644 --- a/Library/W3POP3.h +++ b/Library/W3POP3.h @@ -11,6 +11,7 @@ extern "C" { 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); #ifdef __cplusplus } -- 2.43.0