cd4b774dbd6b5f705acca0bc364f1344541d3306
[claws.git] / src / common / session.h
1 /*
2  * Sylpheed -- 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 #ifdef HAVE_CONFIG_H
24 #include "claws-features.h"
25 #endif
26
27 #include <glib.h>
28
29 #include <time.h>
30 #include <unistd.h>
31
32 #include "socket.h"
33
34 #define SESSION_BUFFSIZE        4096
35
36 typedef struct _Session Session;
37
38 #define SESSION(obj)    ((Session *)obj)
39
40 typedef enum {
41         SESSION_UNKNOWN,
42         SESSION_IMAP,
43         SESSION_NEWS,
44         SESSION_SMTP,
45         SESSION_POP3
46 } SessionType;
47
48 typedef enum {
49         SESSION_READY,
50         SESSION_SEND,
51         SESSION_RECV,
52         SESSION_EOF,
53         SESSION_TIMEOUT,
54         SESSION_ERROR,
55         SESSION_DISCONNECTED
56 } SessionState;
57
58 typedef enum
59 {
60         SESSION_MSG_NORMAL,
61         SESSION_MSG_SEND_DATA,
62         SESSION_MSG_RECV_DATA,
63         SESSION_MSG_CONTROL,
64         SESSION_MSG_ERROR,
65         SESSION_MSG_UNKNOWN
66 } SessionMsgType;
67
68 typedef gint (*RecvMsgNotify)                   (Session        *session,
69                                                  const gchar    *msg,
70                                                  gpointer        user_data);
71 typedef gint (*RecvDataProgressiveNotify)       (Session        *session,
72                                                  guint           cur_len,
73                                                  guint           total_len,
74                                                  gpointer        user_data);
75 typedef gint (*RecvDataNotify)                  (Session        *session,
76                                                  guint           len,
77                                                  gpointer        user_data);
78 typedef gint (*SendDataProgressiveNotify)       (Session        *session,
79                                                  guint           cur_len,
80                                                  guint           total_len,
81                                                  gpointer        user_data);
82 typedef gint (*SendDataNotify)                  (Session        *session,
83                                                  guint           len,
84                                                  gpointer        user_data);
85
86 struct _Session
87 {
88         SessionType type;
89
90         SockInfo *sock;
91
92         gchar *server;
93         gushort port;
94
95         gboolean nonblocking;
96
97         SessionState state;
98
99         time_t last_access_time;
100         GTimeVal tv_prev;
101
102         gint conn_id;
103
104         gint io_tag;
105
106         gchar read_buf[SESSION_BUFFSIZE];
107         gchar *read_buf_p;
108         gint read_buf_len;
109
110         GString *read_msg_buf;
111         GByteArray *read_data_buf;
112         gchar *read_data_terminator;
113
114         /* buffer for short messages */
115         gchar *write_buf;
116         gchar *write_buf_p;
117         gint write_buf_len;
118
119         /* buffer for large data */
120         const guchar *write_data;
121         const guchar *write_data_p;
122         gint write_data_len;
123
124         guint timeout_tag;
125         guint timeout_interval;
126
127         gpointer data;
128
129         /* virtual methods to parse server responses */
130         gint (*recv_msg)                (Session        *session,
131                                          const gchar    *msg);
132
133         void (*connect_finished)        (Session        *session,
134                                          gboolean       success);
135         gint (*send_data_finished)      (Session        *session,
136                                          guint           len);
137         gint (*recv_data_finished)      (Session        *session,
138                                          guchar         *data,
139                                          guint           len);
140
141         void (*destroy)                 (Session        *session);
142
143         /* notification functions */
144         RecvMsgNotify                   recv_msg_notify;
145         RecvDataProgressiveNotify       recv_data_progressive_notify;
146         RecvDataNotify                  recv_data_notify;
147         SendDataProgressiveNotify       send_data_progressive_notify;
148         SendDataNotify                  send_data_notify;
149
150         gpointer recv_msg_notify_data;
151         gpointer recv_data_progressive_notify_data;
152         gpointer recv_data_notify_data;
153         gpointer send_data_progressive_notify_data;
154         gpointer send_data_notify_data;
155
156         const void *account;
157         gboolean is_smtp;
158         gboolean ssl_cert_auto_accept;
159         gint ping_tag;
160
161 #ifdef USE_GNUTLS
162         SSLType ssl_type;
163         gchar *gnutls_priority;
164 #endif
165 };
166
167 void session_init               (Session        *session, 
168                                  const void     *prefs_account,
169                                  gboolean        is_smtp);
170 gint session_connect            (Session        *session,
171                                  const gchar    *server,
172                                  gushort         port);
173 gint session_disconnect         (Session        *session);
174 void session_destroy            (Session        *session);
175 gboolean session_is_running     (Session        *session);
176 gboolean session_is_connected   (Session        *session);
177
178 void session_set_access_time    (Session        *session);
179
180 void session_set_timeout        (Session        *session,
181                                  guint           interval);
182
183 void session_set_recv_message_notify    (Session        *session,
184                                          RecvMsgNotify   notify_func,
185                                          gpointer        data);
186 void session_set_recv_data_progressive_notify
187                                         (Session        *session,
188                                          RecvDataProgressiveNotify notify_func,
189                                          gpointer        data);
190 void session_set_recv_data_notify       (Session        *session,
191                                          RecvDataNotify  notify_func,
192                                          gpointer        data);
193 void session_set_send_data_progressive_notify
194                                         (Session        *session,
195                                          SendDataProgressiveNotify notify_func,
196                                          gpointer        data);
197 void session_set_send_data_notify       (Session        *session,
198                                          SendDataNotify  notify_func,
199                                          gpointer        data);
200
201 #ifdef USE_GNUTLS
202 gint session_start_tls  (Session        *session);
203 #endif
204
205 gint session_send_msg   (Session        *session,
206                          SessionMsgType  type,
207                          const gchar    *msg);
208 gint session_recv_msg   (Session        *session);
209 gint session_send_data  (Session        *session,
210                          const guchar   *data,
211                          guint           size);
212 gint session_recv_data  (Session        *session,
213                          guint           size,
214                          const gchar    *terminator);
215 void session_register_ping(Session *session, gboolean (*ping_cb)(gpointer data));
216
217 #endif /* __SESSION_H__ */