]> Nishi Git Mirror - libw3.git/commitdiff
pop3
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Sat, 27 Jan 2024 01:24:52 +0000 (01:24 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Sat, 27 Jan 2024 01:24:52 +0000 (01:24 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@102 d27a3e52-49c5-7645-884c-6793ebffc270

Example/pop3-list/pop3-list.c
Library/HTTP.c
Library/POP3.c
Library/Util.c
Library/W3POP3.h
Library/W3Util.h

index d42d82dc0c15b5a8d000ff5c52170ee13b9d19c7..636c5b230fcc3c601ce3c8a9fab4e4ff409c55a2 100644 (file)
@@ -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);
 }
index efc1eb3176a6f55191fd9c5ea69cb899cad1ab11..6849c16e3bb365982f84316cb060ce35a0197039 100644 (file)
@@ -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"); }
index 8891e11285c9d1e19587895687c151a3b0d2a302..ee691ff2059b557a6bf0375a92197871b70d49e3 100644 (file)
@@ -6,5 +6,74 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <stdbool.h>
 
-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);
+}
index d0297c2825c10cd11af9af665cae62934fcc9259..59ecea876b5c79f3f8340278ad8e7097e5a93651 100644 (file)
@@ -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;
 }
index 56acb79aab64442641b1626867be55e7cab4b963..1682123e383467471889a22b6205eb11cb9bb898 100644 (file)
@@ -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
 }
index 4bcadde906fe72d3d36dd0dc3b8b67aba8732b99..47ae5b9ebaf641b1f8d4beafcb33ed080ccc8eb5 100644 (file)
@@ -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);