From 4fdd083fa829a9cab4c9bdc48b01546e07b4df22 Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 27 Jan 2024 01:24:52 +0000 Subject: [PATCH] pop3 git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@102 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/pop3-list/pop3-list.c | 8 ++++ Library/HTTP.c | 2 +- Library/POP3.c | 71 ++++++++++++++++++++++++++++++++++- Library/Util.c | 22 ++++++++--- Library/W3POP3.h | 2 + Library/W3Util.h | 3 +- 6 files changed, 100 insertions(+), 8 deletions(-) diff --git a/Example/pop3-list/pop3-list.c b/Example/pop3-list/pop3-list.c index d42d82d..636c5b2 100644 --- a/Example/pop3-list/pop3-list.c +++ b/Example/pop3-list/pop3-list.c @@ -21,5 +21,13 @@ void list_handler(struct W3* w3, char* data, size_t size) { } int main(int argc, char** argv) { + if(argc < 4){ + return 1; + } W3_Library_Init(); + 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_Send_Request(w3); } diff --git a/Library/HTTP.c b/Library/HTTP.c index efc1eb3..6849c16 100644 --- a/Library/HTTP.c +++ b/Library/HTTP.c @@ -287,4 +287,4 @@ void __W3_HTTP_Request(struct W3* w3) { if(redir != NULL) free(redir); } -void W3_Enable_Redirect(struct W3* w3) { __W3_Add_Prop(w3, "HTTP_REDIRECT"); } +void W3_Enable_Redirect(struct W3* w3) { __W3_Add_Prop(w3, "HTTP_REDIRECT", "YES"); } diff --git a/Library/POP3.c b/Library/POP3.c index 8891e11..ee691ff 100644 --- a/Library/POP3.c +++ b/Library/POP3.c @@ -6,5 +6,74 @@ #include #include +#include -void __W3_POP3_Request(struct W3* w3) {} +void __W3_POP3_Request(struct W3* w3) { + if(__W3_Get_Prop(w3, "POP3_USERNAME") == NULL || __W3_Get_Prop(w3, "POP3_PASSWORD") == NULL){ + __W3_Debug("LibW3-POP3", "Set the username/password"); + return; + } + char* buf = malloc(w3->readsize); + int phase = 0; + int login = 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); + char c = buf[i]; + if(c == '\r') continue; + 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){ + 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){ + /* OK */ + if(login == 2){ + /* Login success */ + login = 3; + }else{ + } + }else if(phase == 4){ + /* ERR */ + if(login == 2){ + /* Login failed */ + login = 0; + }else{ + } + } + } + phase = 0; + continue; + } + if(phase == 0){ + if(c == '+'){ + phase = 1; + }else if(c == '-'){ + phase = 2; + } + }else if(phase == 1){ + if(c == ' ') phase += 2; + } + } + } + free(buf); +} + +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); +} diff --git a/Library/Util.c b/Library/Util.c index d0297c2..59ecea8 100644 --- a/Library/Util.c +++ b/Library/Util.c @@ -113,29 +113,41 @@ char* __W3_Get_Platform(void) { bool __W3_Have_Prop(struct W3* w3, const char* name) { if(w3->props == NULL) return false; int i; - for(i = 0; w3->props[i] != NULL; i++) { + for(i = 0; w3->props[i] != NULL; i += 2) { if(strcmp(w3->props[i], name) == 0) return true; } return false; } -void __W3_Add_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) { char* str = __W3_Concat3("Adding a prop `", name, "`"); __W3_Debug("Prop", str); free(str); int len = 0; if(w3->props == NULL) { - w3->props = malloc(sizeof(*w3->props) * 2); + w3->props = malloc(sizeof(*w3->props) * 3); } else { char** oldprops = w3->props; for(len = 0; oldprops[len] != NULL; len++) ; - w3->props = malloc(sizeof(*w3->props) * (len + 2)); + w3->props = malloc(sizeof(*w3->props) * (len + 3)); for(len = 0; oldprops[len] != NULL; len++) { w3->props[len] = oldprops[len]; } free(oldprops); } w3->props[len] = __W3_Strdup(name); - w3->props[len + 1] = NULL; + w3->props[len + 1] = __W3_Strdup(value); + w3->props[len + 2] = NULL; } diff --git a/Library/W3POP3.h b/Library/W3POP3.h index 56acb79..1682123 100644 --- a/Library/W3POP3.h +++ b/Library/W3POP3.h @@ -9,6 +9,8 @@ extern "C" { #include "W3Core.h" 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); #ifdef __cplusplus } diff --git a/Library/W3Util.h b/Library/W3Util.h index 4bcadde..47ae5b9 100644 --- a/Library/W3Util.h +++ b/Library/W3Util.h @@ -18,7 +18,8 @@ unsigned long __W3_Auto_Write(struct W3* w3, char* data, unsigned long length); unsigned long __W3_Auto_Read(struct W3* w3, char* data, unsigned long length); bool __W3_Have_Header(struct W3* w3, const char* name); bool __W3_Have_Prop(struct W3* w3, const char* name); -void __W3_Add_Prop(struct W3* w3, const char* name); +void __W3_Add_Prop(struct W3* w3, const char* name, const char* value); +char* __W3_Get_Prop(struct W3* w3, const char* name); void* __W3_Get_Event(struct W3* w3, const char* eventname); char* __W3_Get_Platform(void); -- 2.43.0