Further pop_mx_ng work
[apps/madmutt.git] / lib-mx / pop-new.c
index 3d772b4..4978f8f 100644 (file)
 #define POP3_PORT         110
 #define POP3S_PORT        995
 
+enum pop_state {
+    POP_NEW,
+    POP_READY,
+};
+
 typedef struct pop_data_t {
     int refcnt;
-    job_t *w;
+    enum pop_state state;
+
+    volatile job_t *w;
 
     ACCOUNT act;
 } pop_data_t;
@@ -57,7 +64,7 @@ static inline pop_data_t *pop_data_dup(pop_data_t *t)
 static void pop_data_wipe(pop_data_t *pd)
 {
     if (pd->w)
-        IGNORE(el_job_release(pd->w, EL_KILLED));
+        IGNORE(el_job_release((job_t *)pd->w, EL_KILLED));
 }
 static void pop_data_delete(pop_data_t **tp)
 {
@@ -141,6 +148,7 @@ static pop_data_t *pop_find_conn(ACCOUNT *act)
 {
     pop_data_t *pd = NULL;
 
+    el_lock();
     for (int i = 0; i < conns.len; i++) {
         if (mutt_account_match(act, &conns.arr[i]->act)) {
             pd = pop_data_dup(conns.arr[i]);
@@ -151,7 +159,16 @@ static pop_data_t *pop_find_conn(ACCOUNT *act)
         pd = pop_data_new();
         pd->act = *act;
     }
+    if (!pd->w) {
+        pd->w = el_job_start(&pop_machine, pd);
+    }
+    while (pd->w && pd->state != POP_READY) {
+        el_wait(pd->w);
+    }
+    if (!pd->w)
+        pop_data_delete(&pd);
 
+    el_unlock();
     return pd;
 }
 
@@ -171,18 +188,13 @@ static int pop_open_mailbox(CONTEXT *ctx)
     p_clear(&url, 1);
     mutt_account_tourl(&act, &url);
 
-    el_lock();
     pd = pop_find_conn(&act);
-    if (false) {
+    if (pd) {
         ctx->data = pd;
         url_ciss_tostring(&url, buf, sizeof (buf), 0);
         m_strreplace(&ctx->path, buf);
-    } else {
-        pop_data_delete(&pd);
     }
-    el_unlock();
-
-    return -1;
+    return 0;
 }
 
 mx_t const pop_mx_ng = {