2009-01-17 [colin] 3.7.0cvs37
[claws.git] / src / common / session.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2009 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 "config.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 #ifdef USE_GNUTLS
158         SSLType ssl_type;
159 #endif
160 };
161
162 void session_init               (Session        *session, 
163                                  const void     *prefs_account,
164                                  gboolean        is_smtp);
165 gint session_connect            (Session        *session,
166                                  const gchar    *server,
167                                  gushort         port);
168 gint session_disconnect         (Session        *session);
169 void session_destroy            (Session        *session);
170 gboolean session_is_connected   (Session        *session);
171
172 void session_set_access_time    (Session        *session);
173
174 void session_set_timeout        (Session        *session,
175                                  guint           interval);
176
177 void session_set_recv_message_notify    (Session        *session,
178                                          RecvMsgNotify   notify_func,
179                                          gpointer        data);
180 void session_set_recv_data_progressive_notify
181                                         (Session        *session,
182                                          RecvDataProgressiveNotify notify_func,
183                                          gpointer        data);
184 void session_set_recv_data_notify       (Session        *session,
185                                          RecvDataNotify  notify_func,
186                                          gpointer        data);
187 void session_set_send_data_progressive_notify
188                                         (Session        *session,
189                                          SendDataProgressiveNotify notify_func,
190                                          gpointer        data);
191 void session_set_send_data_notify       (Session        *session,
192                                          SendDataNotify  notify_func,
193                                          gpointer        data);
194
195 #ifdef USE_GNUTLS
196 gint session_start_tls  (Session        *session);
197 #endif
198
199 gint session_send_msg   (Session        *session,
200                          SessionMsgType  type,
201                          const gchar    *msg);
202 gint session_recv_msg   (Session        *session);
203 gint session_send_data  (Session        *session,
204                          const guchar   *data,
205                          guint           size);
206 gint session_recv_data  (Session        *session,
207                          guint           size,
208                          const gchar    *terminator);
209
210 #endif /* __SESSION_H__ */