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

Example/pop3-list/pop3-list.c
Library/POP3.c
Library/W3Core.h
Library/W3POP3.h
W3Version.h.p

index c44bc95541a118c4e5ab4120bef15e75039a2c80..5f412153b4156ea21ba479629c649587de2c6e0e 100644 (file)
 #include <string.h>
 #include <unistd.h>
 
-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);
 }
index cb521bc0b38bb25c03107a099aaa85de847c3890..5a8a0acf641d825b832ea9561d7152d1204a4182 100644 (file)
@@ -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);
+}
index 3fb33d2736330429f9f576d762de667a0136e869..6240fbebd6c0e6be606592a69a530fa66c80d695 100644 (file)
@@ -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* */
index 464d524f0a4e04f945693c044f446b9dfd408033..003a34bdaf7093af15527803c9823d55a6fc5084 100644 (file)
@@ -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
 }
index b3e03fdb107a9d762f70239aeba884d7c35ef89d..81241000e6ffb79d027e95aabe009eb36a3a18dd 100644 (file)
@@ -6,7 +6,7 @@
 extern "C" {
 #endif
 
-#define LIBW3_VERSION "1.6B" \
+#define LIBW3_VERSION "1.6C" \
 SUFFIX
 
 #ifdef __cplusplus