fix indent (broken in last commit)
[claws.git] / src / common / session.c
index 19695ce19e347f70040d709c05a4d9d2ed353987..959c7a28e6785e6956dd765c893df4476fe371ad 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
 #include <unistd.h>
-#include <sys/signal.h>
-#include <sys/wait.h>
-#include <sys/time.h>
+#include <time.h>
 #include <errno.h>
 
 #include "session.h"
 #include "utils.h"
+#include "log.h"
 
 static gint session_connect_cb         (SockInfo       *sock,
                                         gpointer        data);
 static gint session_close              (Session        *session);
 
+static gboolean session_timeout_cb     (gpointer        data);
+
 static gboolean session_recv_msg_idle_cb       (gpointer        data);
 static gboolean session_recv_data_idle_cb      (gpointer        data);
 
@@ -59,20 +61,20 @@ static gboolean session_write_data_cb       (SockInfo       *source,
                                         gpointer        data);
 
 
-void session_init(Session *session)
+void session_init(Session *session, const void *prefs_account, gboolean is_smtp)
 {
        session->type = SESSION_UNKNOWN;
        session->sock = NULL;
        session->server = NULL;
        session->port = 0;
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
        session->ssl_type = SSL_NONE;
 #endif
        session->nonblocking = TRUE;
        session->state = SESSION_READY;
        session->last_access_time = time(NULL);
 
-       gettimeofday(&session->tv_prev, NULL);
+       g_get_current_time(&session->tv_prev);
 
        session->conn_id = 0;
 
@@ -88,7 +90,18 @@ void session_init(Session *session)
        session->write_buf_p = NULL;
        session->write_buf_len = 0;
 
+       session->write_data = NULL;
+       session->write_data_p = NULL;
+       session->write_data_len = 0;
+
+       session->timeout_tag = 0;
+       session->timeout_interval = 0;
+
        session->data = NULL;
+       session->account = prefs_account;
+       session->is_smtp = is_smtp;
+
+       session->ping_tag = -1;
 }
 
 /*!
@@ -106,6 +119,7 @@ void session_init(Session *session)
  */
 gint session_connect(Session *session, const gchar *server, gushort port)
 {
+#ifdef G_OS_UNIX
        session->server = g_strdup(server);
        session->port = port;
 
@@ -118,6 +132,22 @@ gint session_connect(Session *session, const gchar *server, gushort port)
        }
 
        return 0;
+#else
+       SockInfo *sock;
+
+       session->server = g_strdup(server);
+       session->port = port;
+
+       sock = sock_connect(server, port);
+       if (sock == NULL) {
+               g_warning("can't connect to server.");
+               session_close(session);
+               return -1;
+       }
+       sock->is_smtp = session->is_smtp;
+
+       return session_connect_cb(sock, session);
+#endif
 }
 
 static gint session_connect_cb(SockInfo *sock, gpointer data)
@@ -133,18 +163,29 @@ static gint session_connect_cb(SockInfo *sock, gpointer data)
        }
 
        session->sock = sock;
+       sock->account = session->account;
+       sock->is_smtp = session->is_smtp;
+       sock->ssl_cert_auto_accept = session->ssl_cert_auto_accept;
+
+#ifdef USE_GNUTLS
+       sock->gnutls_priority = session->gnutls_priority;
 
-#if USE_OPENSSL
        if (session->ssl_type == SSL_TUNNEL) {
                sock_set_nonblocking_mode(sock, FALSE);
                if (!ssl_init_socket(sock)) {
                        g_warning("can't initialize SSL.");
+                       log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
                        session->state = SESSION_ERROR;
                        return -1;
                }
        }
 #endif
 
+       /* we could have gotten a timeout while waiting for user input in 
+        * an SSL certificate dialog */
+       if (session->state == SESSION_TIMEOUT)
+               return -1;
+
        sock_set_nonblocking_mode(sock, session->nonblocking);
 
        debug_print("session (%p): connected\n", session);
@@ -177,8 +218,10 @@ gint session_disconnect(Session *session)
  */
 void session_destroy(Session *session)
 {
-       g_return_if_fail(session != NULL);
-       g_return_if_fail(session->destroy != NULL);
+       cm_return_if_fail(session != NULL);
+       cm_return_if_fail(session->destroy != NULL);
+
+       session_register_ping(session, NULL);
 
        session_close(session);
        session->destroy(session);
@@ -187,19 +230,67 @@ void session_destroy(Session *session)
        g_byte_array_free(session->read_data_buf, TRUE);
        g_free(session->read_data_terminator);
        g_free(session->write_buf);
+#ifdef USE_GNUTLS
+       g_free(session->gnutls_priority);
+#endif
 
        debug_print("session (%p): destroyed\n", session);
 
        g_free(session);
 }
 
-gboolean session_is_connected(Session *session)
+gboolean session_is_running(Session *session)
 {
        return (session->state == SESSION_READY ||
                session->state == SESSION_SEND ||
                session->state == SESSION_RECV);
 }
 
+gboolean session_is_connected(Session *session)
+{
+       return (session->state == SESSION_SEND ||
+               session->state == SESSION_RECV);
+}
+
+void session_set_access_time(Session *session)
+{
+       session->last_access_time = time(NULL);
+}
+
+void session_set_timeout(Session *session, guint interval)
+{
+       if (session->timeout_tag > 0)
+               g_source_remove(session->timeout_tag);
+
+       session->timeout_interval = interval;
+       if (interval > 0) {
+               if (interval % 1000 == 0)
+                       session->timeout_tag =
+                               g_timeout_add_seconds(interval/1000, session_timeout_cb, session);
+               else
+                       session->timeout_tag =
+                               g_timeout_add(interval, session_timeout_cb, session);
+       } else
+               session->timeout_tag = 0;
+}
+
+static gboolean session_timeout_cb(gpointer data)
+{
+       Session *session = SESSION(data);
+
+       g_warning("session timeout.\n");
+
+       if (session->io_tag > 0) {
+               g_source_remove(session->io_tag);
+               session->io_tag = 0;
+       }
+
+       session->timeout_tag = 0;
+       session->state = SESSION_TIMEOUT;
+
+       return FALSE;
+}
+
 void session_set_recv_message_notify(Session *session,
                                     RecvMsgNotify notify_func, gpointer data)
 {
@@ -248,12 +339,17 @@ void session_set_send_data_notify(Session *session, SendDataNotify notify_func,
  */
 static gint session_close(Session *session)
 {
-       g_return_val_if_fail(session != NULL, -1);
+       cm_return_val_if_fail(session != NULL, -1);
 
+#ifdef G_OS_UNIX
        if (session->conn_id > 0) {
                sock_connect_async_cancel(session->conn_id);
                session->conn_id = 0;
+               debug_print("session (%p): connection cancelled\n", session);
        }
+#endif
+
+       session_set_timeout(session, 0);
 
        if (session->io_tag > 0) {
                g_source_remove(session->io_tag);
@@ -264,27 +360,28 @@ static gint session_close(Session *session)
                sock_close(session->sock);
                session->sock = NULL;
                session->state = SESSION_DISCONNECTED;
+               debug_print("session (%p): closed\n", session);
        }
 
-       debug_print("session (%p): closed\n", session);
-
        return 0;
 }
 
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
 gint session_start_tls(Session *session)
 {
        gboolean nb_mode;
 
        nb_mode = sock_is_nonblocking_mode(session->sock);
 
+       session->sock->ssl_cert_auto_accept = session->ssl_cert_auto_accept;
+
        if (nb_mode)
                sock_set_nonblocking_mode(session->sock, FALSE);
 
        if (!ssl_init_socket_with_method(session->sock, SSL_METHOD_TLSv1)) {
-               g_warning("can't start TLS session.\n");
+               g_warning("couldn't start TLS session.\n");
                if (nb_mode)
-                       sock_set_nonblocking_mode(session->sock, TRUE);
+                       sock_set_nonblocking_mode(session->sock, session->nonblocking);
                return -1;
        }
 
@@ -299,9 +396,9 @@ gint session_send_msg(Session *session, SessionMsgType type, const gchar *msg)
 {
        gboolean ret;
 
-       g_return_val_if_fail(session->write_buf == NULL, -1);
-       g_return_val_if_fail(msg != NULL, -1);
-       g_return_val_if_fail(msg[0] != '\0', -1);
+       cm_return_val_if_fail(session->write_buf == NULL, -1);
+       cm_return_val_if_fail(msg != NULL, -1);
+       cm_return_val_if_fail(msg[0] != '\0', -1);
 
        session->state = SESSION_SEND;
        session->write_buf = g_strconcat(msg, "\r\n", NULL);
@@ -321,7 +418,7 @@ gint session_send_msg(Session *session, SessionMsgType type, const gchar *msg)
 
 gint session_recv_msg(Session *session)
 {
-       g_return_val_if_fail(session->read_msg_buf->len == 0, -1);
+       cm_return_val_if_fail(session->read_msg_buf->len == 0, -1);
 
        session->state = SESSION_RECV;
 
@@ -362,17 +459,16 @@ gint session_send_data(Session *session, const guchar *data, guint size)
 {
        gboolean ret;
 
-       g_return_val_if_fail(session->write_buf == NULL, -1);
-       g_return_val_if_fail(data != NULL, -1);
-       g_return_val_if_fail(size != 0, -1);
+       cm_return_val_if_fail(session->write_data == NULL, -1);
+       cm_return_val_if_fail(data != NULL, -1);
+       cm_return_val_if_fail(size != 0, -1);
 
        session->state = SESSION_SEND;
 
-       session->write_buf = g_malloc(size);
-       session->write_buf_p = session->write_buf;
-       memcpy(session->write_buf, data, size);
-       session->write_buf_len = size;
-       gettimeofday(&session->tv_prev, NULL);
+       session->write_data = data;
+       session->write_data_p = session->write_data;
+       session->write_data_len = size;
+       g_get_current_time(&session->tv_prev);
 
        ret = session_write_data_cb(session->sock, G_IO_OUT, session);
 
@@ -388,13 +484,13 @@ gint session_send_data(Session *session, const guchar *data, guint size)
 
 gint session_recv_data(Session *session, guint size, const gchar *terminator)
 {
-       g_return_val_if_fail(session->read_data_buf->len == 0, -1);
+       cm_return_val_if_fail(session->read_data_buf->len == 0, -1);
 
        session->state = SESSION_RECV;
 
        g_free(session->read_data_terminator);
        session->read_data_terminator = g_strdup(terminator);
-       gettimeofday(&session->tv_prev, NULL);
+       g_get_current_time(&session->tv_prev);
 
        if (session->read_buf_len > 0)
                g_idle_add(session_recv_data_idle_cb, session);
@@ -429,16 +525,23 @@ static gboolean session_read_msg_cb(SockInfo *source, GIOCondition condition,
        gchar *msg;
        gint ret;
 
-       g_return_val_if_fail(condition == G_IO_IN, FALSE);
+       cm_return_val_if_fail(condition == G_IO_IN, FALSE);
+
+       session_set_timeout(session, session->timeout_interval);
 
        if (session->read_buf_len == 0) {
-               gint read_len;
+               gint read_len = -1;
 
-               read_len = sock_read(session->sock, session->read_buf,
+               if (session->sock)
+                       read_len = sock_read(session->sock, session->read_buf,
                                     SESSION_BUFFSIZE - 1);
 
                if (read_len == -1 && session->state == SESSION_DISCONNECTED) {
                        g_warning ("sock_read: session disconnected\n");
+                       if (session->io_tag > 0) {
+                               g_source_remove(session->io_tag);
+                               session->io_tag = 0;
+                       }
                        return FALSE;
                }
                
@@ -518,7 +621,9 @@ static gboolean session_read_data_cb(SockInfo *source, GIOCondition condition,
        guint data_len;
        gint ret;
 
-       g_return_val_if_fail(condition == G_IO_IN, FALSE);
+       cm_return_val_if_fail(condition == G_IO_IN, FALSE);
+
+       session_set_timeout(session, session->timeout_interval);
 
        if (session->read_buf_len == 0) {
                gint read_len;
@@ -574,16 +679,16 @@ static gboolean session_read_data_cb(SockInfo *source, GIOCondition condition,
 
        /* incomplete read */
        if (!complete) {
-               struct timeval tv_cur;
+               GTimeVal tv_cur;
 
-               gettimeofday(&tv_cur, NULL);
+               g_get_current_time(&tv_cur);
                if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
                    tv_cur.tv_usec - session->tv_prev.tv_usec >
                    UI_REFRESH_INTERVAL) {
                        session->recv_data_progressive_notify
                                (session, data_buf->len, 0,
                                 session->recv_data_progressive_notify_data);
-                       gettimeofday(&session->tv_prev, NULL);
+                       g_get_current_time(&session->tv_prev);
                }
                return TRUE;
        }
@@ -616,9 +721,9 @@ static gint session_write_buf(Session *session)
        gint write_len;
        gint to_write_len;
 
-       g_return_val_if_fail(session->write_buf != NULL, -1);
-       g_return_val_if_fail(session->write_buf_p != NULL, -1);
-       g_return_val_if_fail(session->write_buf_len > 0, -1);
+       cm_return_val_if_fail(session->write_buf != NULL, -1);
+       cm_return_val_if_fail(session->write_buf_p != NULL, -1);
+       cm_return_val_if_fail(session->write_buf_len > 0, -1);
 
        to_write_len = session->write_buf_len -
                (session->write_buf_p - session->write_buf);
@@ -654,16 +759,58 @@ static gint session_write_buf(Session *session)
        return 0;
 }
 
+static gint session_write_data(Session *session)
+{
+       gint write_len;
+       gint to_write_len;
+
+       cm_return_val_if_fail(session->write_data != NULL, -1);
+       cm_return_val_if_fail(session->write_data_p != NULL, -1);
+       cm_return_val_if_fail(session->write_data_len > 0, -1);
+
+       to_write_len = session->write_data_len -
+               (session->write_data_p - session->write_data);
+       to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
+
+       write_len = sock_write(session->sock, session->write_data_p,
+                              to_write_len);
+
+       if (write_len < 0) {
+               switch (errno) {
+               case EAGAIN:
+                       write_len = 0;
+                       break;
+               default:
+                       g_warning("sock_write: %s\n", g_strerror(errno));
+                       session->state = SESSION_ERROR;
+                       return -1;
+               }
+       }
+
+       /* incomplete write */
+       if (session->write_data_p - session->write_data + write_len <
+           session->write_data_len) {
+               session->write_data_p += write_len;
+               return 1;
+       }
+
+       session->write_data = NULL;
+       session->write_data_p = NULL;
+       session->write_data_len = 0;
+
+       return 0;
+}
+
 static gboolean session_write_msg_cb(SockInfo *source, GIOCondition condition,
                                     gpointer data)
 {
        Session *session = SESSION(data);
        gint ret;
 
-       g_return_val_if_fail(condition == G_IO_OUT, FALSE);
-       g_return_val_if_fail(session->write_buf != NULL, FALSE);
-       g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
-       g_return_val_if_fail(session->write_buf_len > 0, FALSE);
+       cm_return_val_if_fail(condition == G_IO_OUT, FALSE);
+       cm_return_val_if_fail(session->write_buf != NULL, FALSE);
+       cm_return_val_if_fail(session->write_buf_p != NULL, FALSE);
+       cm_return_val_if_fail(session->write_buf_len > 0, FALSE);
 
        ret = session_write_buf(session);
 
@@ -687,34 +834,35 @@ static gboolean session_write_data_cb(SockInfo *source,
                                      GIOCondition condition, gpointer data)
 {
        Session *session = SESSION(data);
-       guint write_buf_len;
+       guint write_data_len;
        gint ret;
 
-       g_return_val_if_fail(condition == G_IO_OUT, FALSE);
-       g_return_val_if_fail(session->write_buf != NULL, FALSE);
-       g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
-       g_return_val_if_fail(session->write_buf_len > 0, FALSE);
+       cm_return_val_if_fail(condition == G_IO_OUT, FALSE);
+       cm_return_val_if_fail(session->write_data != NULL, FALSE);
+       cm_return_val_if_fail(session->write_data_p != NULL, FALSE);
+       cm_return_val_if_fail(session->write_data_len > 0, FALSE);
 
-       write_buf_len = session->write_buf_len;
+       write_data_len = session->write_data_len;
 
-       ret = session_write_buf(session);
+       ret = session_write_data(session);
 
        if (ret < 0) {
                session->state = SESSION_ERROR;
                return FALSE;
        } else if (ret > 0) {
-               struct timeval tv_cur;
+               GTimeVal tv_cur;
 
-               gettimeofday(&tv_cur, NULL);
+               g_get_current_time(&tv_cur);
                if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
                    tv_cur.tv_usec - session->tv_prev.tv_usec >
                    UI_REFRESH_INTERVAL) {
+                       session_set_timeout(session, session->timeout_interval);
                        session->send_data_progressive_notify
                                (session,
-                                session->write_buf_p - session->write_buf,
-                                write_buf_len,
+                                session->write_data_p - session->write_data,
+                                write_data_len,
                                 session->send_data_progressive_notify_data);
-                       gettimeofday(&session->tv_prev, NULL);
+                       g_get_current_time(&session->tv_prev);
                }
                return TRUE;
        }
@@ -725,9 +873,22 @@ static gboolean session_write_data_cb(SockInfo *source,
        }
 
        /* callback */
-       ret = session->send_data_finished(session, write_buf_len);
-       session->send_data_notify(session, write_buf_len,
+       ret = session->send_data_finished(session, write_data_len);
+       session->send_data_notify(session, write_data_len,
                                  session->send_data_notify_data);
 
        return FALSE;
 }
+
+void session_register_ping(Session *session, gboolean (*ping_cb)(gpointer data))
+{
+       if (!session)
+               return;
+       if (session->ping_tag > -1)
+               g_source_remove(session->ping_tag);
+
+       session->ping_tag = -1;
+
+       if (ping_cb != NULL)
+               session->ping_tag = g_timeout_add_seconds(60, ping_cb, session);
+}