#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) {
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);
}
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;
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));
}
__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 */
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);
+ }
}
}
}
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);
+}
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* */