imap_private.h is … private
[apps/madmutt.git] / lib-sys / evtloop.c
index 3e1719e..a28c9b8 100644 (file)
@@ -17,6 +17,7 @@
  *  Copyright © 2006 Pierre Habouzit
  */
 
+#include <pthread.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
 #ifndef EPOLLRDHUP
 #include "mutt.h"
 #include "mutt_ssl.li"
 
+DO_ARRAY_TYPE(job_t, job);
+
 static int epollfd = -1;
+static job_array jobs;
 
 static int el_job_setemode(job_t *w, el_mode emode)
 {
@@ -78,6 +82,32 @@ void job_wipe(job_t *w)
         gnutls_deinit(w->session);
 }
 
+static void job_arrau_dtor(job_t **j)
+{
+    if (*j)
+        IGNORE(el_job_release(*j, EL_KILLED));
+}
+
+DO_ARRAY_FUNCS(job_t, job, job_arrau_dtor);
+
+static void job_array_remove(job_array *arr, job_t *j)
+{
+    for (int i = 0; i < arr->len; i++) {
+        if (arr->arr[i] == j) {
+            job_array_take(arr, i);
+            break;
+        }
+    }
+}
+
+job_t *el_job_start(const machine_t *m, void *cfg)
+{
+    job_t *w = job_new();
+    w->m = m;
+    job_array_append(&jobs, w);
+    return m->setup(w, cfg) < 0 ? NULL : w;
+}
+
 int el_job_release(job_t *w, el_status reason)
 {
     w->state = EL_LLP_FINI;
@@ -89,14 +119,14 @@ int el_job_release(job_t *w, el_status reason)
             gnutls_bye(w->session, GNUTLS_SHUT_RDWR);
         close(w->fd);
     }
+    job_array_remove(&jobs, w);
     job_delete(&w);
     return -1;
 }
 
-static int el_job_connecting_ssl(job_t *w)
+static int el_job_tlsing(job_t *w, int starttls)
 {
     int err = gnutls_handshake(w->session);
-
     if (err < 0 && !gnutls_error_is_fatal(err)) {
         int wr = gnutls_record_get_direction(w->session);
         return el_job_setemode(w, wr ? EL_WRITING : EL_READING);
@@ -113,9 +143,21 @@ static int el_job_connecting_ssl(job_t *w)
     /* NB: gnutls_cipher_get_key_size() returns key length in bytes */
     w->ssf   = gnutls_cipher_get_key_size(gnutls_cipher_get(w->session)) * 8;
     w->state = EL_LLP_READY;
+    if (starttls)
+        return el_job_setemode(w, w->mode);
     return w->m->on_event(w, EL_EVT_RUNNING);
 }
 
+static int el_job_starttlsing(job_t *w)
+{
+    return el_job_tlsing(w, true);
+}
+
+static int el_job_connecting_ssl(job_t *w)
+{
+    return el_job_tlsing(w, false);
+}
+
 static int el_job_connecting(job_t *w)
 {
     int err = 0;
@@ -132,7 +174,7 @@ static int el_job_connecting(job_t *w)
     return w->m->on_event(w, EL_EVT_RUNNING);
 }
 
-static int tls_negociate(job_t *w)
+static int tls_negotiate(job_t *w)
 {
     static int protocol_priority[] = { GNUTLS_TLS1, GNUTLS_SSL3, 0 };
 
@@ -182,7 +224,7 @@ int el_job_connect(job_t *w, struct sockaddr *addr, socklen_t len,
         goto error;
 
     w->fd  = sock;
-    if (ssl && tls_negociate(w) < 0)
+    if (ssl && tls_negotiate(w) < 0)
         goto error;
 
     w->llp = &el_job_connecting;
@@ -193,6 +235,15 @@ int el_job_connect(job_t *w, struct sockaddr *addr, socklen_t len,
     return el_job_release(w, EL_ERROR);
 }
 
+int el_job_starttls(job_t *w)
+{
+    if (tls_negotiate(w) < 0)
+        return el_job_release(w, EL_RDHUP);
+    w->state = EL_LLP_INIT;
+    w->llp   = &el_job_starttlsing;
+    return w->llp(w);
+}
+
 ssize_t el_job_read(job_t *w, buffer_t *buf)
 {
     ssize_t nr;
@@ -259,6 +310,7 @@ int el_dispatch(int timeout)
         int event = events[count].events;
         int evt   = 0;
 
+        gettimeofday(&w->mru, NULL);
         switch (w->state) {
           case EL_LLP_INIT:
             w->llp(w);
@@ -286,3 +338,44 @@ int el_dispatch(int timeout)
 
     return 0;
 }
+
+void *el_loop(void *data)
+{
+    time_t sec = time(NULL);
+
+    for (;;) {
+        struct timeval now;
+
+        el_dispatch(100);
+        pthread_testcancel();
+
+        gettimeofday(&now, NULL);
+        if (sec >= now.tv_sec)
+            continue;
+        sec = now.tv_sec;
+        now.tv_sec -= 10;
+        for (int i = jobs.len - 1; i >= 0; --i) {
+            job_t *w = jobs.arr[i];
+            if (timercmp(&now, &w->mru, >))
+                IGNORE(w->m->on_event(w, EL_EVT_WAKEUP));
+        }
+    }
+}
+
+void el_initialize(void)
+{
+    gnutls_global_init();
+    epollfd = epoll_create(1024);
+    if (epollfd < 0) {
+        mutt_error("epoll_create");
+        mutt_exit(EXIT_FAILURE);
+    }
+    job_array_init(&jobs);
+}
+
+void el_shutdown(void)
+{
+    job_array_wipe(&jobs);
+    close(epollfd);
+    gnutls_global_deinit();
+}