00675c4d6fc7fdde50d3d4849410cc4c9ec10c9b
[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         gint (*send_data_finished)      (Session        *session,
134                                          guint           len);
135         gint (*recv_data_finished)      (Session        *session,
136                                          guchar         *data,
137                                          guint           len);
138
139         void (*destroy)                 (Session        *session);
140
141         /* notification functions */
142         RecvMsgNotify                   recv_msg_notify;
143         RecvDataProgressiveNotify       recv_data_progressive_notify;
144         RecvDataNotify                  recv_data_notify;
145         SendDataProgressiveNotify       send_data_progressive_notify;
146         SendDataNotify                  send_data_notify;
147
148         gpointer recv_msg_notify_data;
149         gpointer recv_data_progressive_notify_data;
150         gpointer recv_data_notify_data;
151         gpointer send_data_progressive_notify_data;
152         gpointer send_data_notify_data;
153         
154         const void *account;
155         gboolean is_smtp;
156
157         gint ping_tag;
158
159 #ifdef USE_GNUTLS
160         SSLType ssl_type;
161         gchar *gnutls_priority;
162 #endif
163 };
164
165 void session_init               (Session        *session, 
166                                  const void     *prefs_account,
167                                  gboolean        is_smtp);
168 gint session_connect            (Session        *session,
169                                  const gchar    *server,
170                                  gushort         port);
171 gint session_disconnect         (Session        *session);
172 void session_destroy            (Session        *session);
173 gboolean session_is_running     (Session        *session);
174 gboolean session_is_connected   (Session        *session);
175
176 void session_set_access_time    (Session        *session);
177
178 void session_set_timeout        (Session        *session,
179                                  guint           interval);
180
181 void session_set_recv_message_notify    (Session        *session,
182                                          RecvMsgNotify   notify_func,
183                                          gpointer        data);
184 void session_set_recv_data_progressive_notify
185                                         (Session        *session,
186                                          RecvDataProgressiveNotify notify_func,
187                                          gpointer        data);
188 void session_set_recv_data_notify       (Session        *session,
189                                          RecvDataNotify  notify_func,
190                                          gpointer        data);
191 void session_set_send_data_progressive_notify
192                                         (Session        *session,
193                                          SendDataProgressiveNotify notify_func,
194                                          gpointer        data);
195 void session_set_send_data_notify       (Session        *session,
196                                          SendDataNotify  notify_func,
197                                          gpointer        data);
198
199 #ifdef USE_GNUTLS
200 gint session_start_tls  (Session        *session);
201 #endif
202
203 gint session_send_msg   (Session        *session,
204                          SessionMsgType  type,
205                          const gchar    *msg);
206 gint session_recv_msg   (Session        *session);
207 gint session_send_data  (Session        *session,
208                          const guchar   *data,
209                          guint           size);
210 gint session_recv_data  (Session        *session,
211                          guint           size,
212                          const gchar    *terminator);
213 void session_register_ping(Session *session, gboolean (*ping_cb)(gpointer data));
214
215 #endif /* __SESSION_H__ */