session.h (5019B)
1 /* 2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client 3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 #ifndef __SESSION_H__ 21 #define __SESSION_H__ 22 23 #include <glib.h> 24 25 #include <time.h> 26 #include <unistd.h> 27 28 #include "socket.h" 29 30 #define SESSION_BUFFSIZE 4096 31 32 typedef struct _Session Session; 33 34 #define SESSION(obj) ((Session *)obj) 35 36 typedef enum { 37 SESSION_UNKNOWN, 38 SESSION_IMAP, 39 SESSION_NEWS, 40 SESSION_SMTP, 41 SESSION_POP3 42 } SessionType; 43 44 typedef enum { 45 SESSION_READY, 46 SESSION_SEND, 47 SESSION_RECV, 48 SESSION_EOF, 49 SESSION_TIMEOUT, 50 SESSION_ERROR, 51 SESSION_DISCONNECTED 52 } SessionState; 53 54 typedef gint (*RecvMsgNotify) (Session *session, 55 const gchar *msg, 56 gpointer user_data); 57 typedef gint (*RecvDataProgressiveNotify) (Session *session, 58 guint cur_len, 59 guint total_len, 60 gpointer user_data); 61 typedef gint (*RecvDataNotify) (Session *session, 62 guint len, 63 gpointer user_data); 64 typedef gint (*SendDataProgressiveNotify) (Session *session, 65 guint cur_len, 66 guint total_len, 67 gpointer user_data); 68 typedef gint (*SendDataNotify) (Session *session, 69 guint len, 70 gpointer user_data); 71 72 struct _Session 73 { 74 SessionType type; 75 76 SockInfo *sock; 77 78 gchar *server; 79 gushort port; 80 81 gboolean nonblocking; 82 83 SessionState state; 84 85 time_t last_access_time; 86 GDateTime *tv_prev; 87 gint conn_id; 88 89 gint io_tag; 90 91 gchar read_buf[SESSION_BUFFSIZE]; 92 gchar *read_buf_p; 93 gint read_buf_len; 94 95 GString *read_msg_buf; 96 GByteArray *read_data_buf; 97 gchar *read_data_terminator; 98 99 /* buffer for short messages */ 100 gchar *write_buf; 101 gchar *write_buf_p; 102 gint write_buf_len; 103 104 /* buffer for large data */ 105 const guchar *write_data; 106 const guchar *write_data_p; 107 gint write_data_len; 108 109 guint timeout_tag; 110 guint timeout_interval; 111 112 gpointer data; 113 114 /* virtual methods to parse server responses */ 115 gint (*recv_msg) (Session *session, 116 const gchar *msg); 117 118 void (*connect_finished) (Session *session, 119 gboolean success); 120 gint (*send_data_finished) (Session *session, 121 guint len); 122 gint (*recv_data_finished) (Session *session, 123 guchar *data, 124 guint len); 125 126 void (*destroy) (Session *session); 127 128 /* notification functions */ 129 RecvMsgNotify recv_msg_notify; 130 RecvDataProgressiveNotify recv_data_progressive_notify; 131 RecvDataNotify recv_data_notify; 132 SendDataProgressiveNotify send_data_progressive_notify; 133 SendDataNotify send_data_notify; 134 135 gpointer recv_msg_notify_data; 136 gpointer recv_data_progressive_notify_data; 137 gpointer recv_data_notify_data; 138 gpointer send_data_progressive_notify_data; 139 gpointer send_data_notify_data; 140 141 const void *account; 142 gboolean is_smtp; 143 gboolean ssl_cert_auto_accept; 144 gint ping_tag; 145 146 SSLType ssl_type; 147 gchar *gnutls_priority; 148 gboolean use_tls_sni; 149 }; 150 151 void session_init (Session *session, 152 const void *prefs_account, 153 gboolean is_smtp); 154 gint session_connect (Session *session, 155 const gchar *server, 156 gushort port); 157 gint session_disconnect (Session *session); 158 void session_destroy (Session *session); 159 gboolean session_is_running (Session *session); 160 gboolean session_is_connected (Session *session); 161 162 void session_set_access_time (Session *session); 163 164 void session_set_timeout (Session *session, 165 guint interval); 166 167 void session_set_recv_message_notify (Session *session, 168 RecvMsgNotify notify_func, 169 gpointer data); 170 void session_set_recv_data_progressive_notify 171 (Session *session, 172 RecvDataProgressiveNotify notify_func, 173 gpointer data); 174 void session_set_recv_data_notify (Session *session, 175 RecvDataNotify notify_func, 176 gpointer data); 177 void session_set_send_data_progressive_notify 178 (Session *session, 179 SendDataProgressiveNotify notify_func, 180 gpointer data); 181 void session_set_send_data_notify (Session *session, 182 SendDataNotify notify_func, 183 gpointer data); 184 185 gint session_start_tls (Session *session); 186 187 gint session_send_msg (Session *session, 188 const gchar *msg); 189 gint session_recv_msg (Session *session); 190 gint session_send_data (Session *session, 191 const guchar *data, 192 guint size); 193 gint session_recv_data (Session *session, 194 guint size, 195 const gchar *terminator); 196 void session_register_ping(Session *session, gboolean (*ping_cb)(gpointer data)); 197 198 #endif /* __SESSION_H__ */