+int el_job_connect2(job_t *w, const ACCOUNT *act)
+{
+ int rc;
+ char *host = NULL;
+ struct addrinfo *res;
+ struct addrinfo hints = {
+ .ai_family = AF_UNSPEC,
+ .ai_socktype = SOCK_STREAM,
+ };
+
+# ifdef HAVE_LIBIDN
+ if (idna_to_ascii_lz(act->host, &host, 1) != IDNA_SUCCESS) {
+ mutt_error(_("Bad IDN \"%s\"."), act->host);
+ return -1;
+ }
+# else
+ host = act->host;
+# endif
+ mutt_message(_("Looking up %s..."), act->host);
+ rc = getaddrinfo(host, NULL, &hints, &res);
+# ifdef HAVE_LIBIDN
+ p_delete(&host);
+# endif
+
+ if (rc) {
+ mutt_error(_("Could not find the host \"%s\""), act->host);
+ mutt_sleep(2);
+ return -1;
+ }
+ mutt_message(_("Connecting to %s..."), act->host);
+ rc = el_job_connect(w, res->ai_addr, res->ai_addrlen, res->ai_socktype,
+ res->ai_protocol, act->has_ssl);
+ freeaddrinfo (res);
+ if (rc) {
+ mutt_error(_("Could not connect to %s (%m)."), act->host);
+ mutt_sleep(2);
+ return -1;
+ }
+ return 0;
+}
+
+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;
+
+ buffer_ensure(buf, BUFSIZ);
+
+ if (w->session) {
+ nr = gnutls_record_recv(w->session, buf->data + buf->len, BUFSIZ);
+ if (nr < 0 && !gnutls_error_is_fatal(nr)) {
+ int wr = gnutls_record_get_direction(w->session);
+ return el_job_setemode(w, wr ? EL_WRITING : EL_READING);
+ }
+ EL_JOB_CHECK(el_job_setemode(w, w->mode));
+ } else {
+ nr = read(w->fd, buf->data + buf->len, BUFSIZ);
+ if (nr < 0 && (errno == EINTR || errno == EAGAIN))
+ return 0;
+ }
+ if (nr <= 0)
+ return el_job_release(w, EL_RDHUP);
+ buffer_extend(buf, nr);
+ return nr;
+}
+
+ssize_t el_job_write(job_t *w, buffer_t *buf)
+{
+ ssize_t nr;
+
+ if (buf->len == 0)
+ return 0;
+
+ if (w->session) {
+ nr = gnutls_record_send(w->session, buf->data, buf->len);
+ if (nr < 0 && !gnutls_error_is_fatal(nr)) {
+ int wr = gnutls_record_get_direction(w->session);
+ return el_job_setemode(w, wr ? EL_WRITING : EL_READING);
+ }
+ EL_JOB_CHECK(el_job_setemode(w, w->mode));
+ } else {
+ nr = write(w->fd, buf->data, buf->len);
+ if (nr < 0 && (errno == EINTR || errno == EAGAIN))
+ return 0;
+ }
+ if (nr <= 0)
+ return el_job_release(w, EL_RDHUP);
+ buffer_splice(buf, 0, nr, NULL, 0);
+ return nr;
+}
+
+void el_lock(void)
+{
+ pthread_mutex_lock(&el_mx);
+}
+
+void el_unlock(void)
+{
+ pthread_mutex_unlock(&el_mx);
+}
+