sync with main 0.9.11cvs2
[claws.git] / src / common / session.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 <sys/time.h>
31 #include <unistd.h>
32
33 #include "socket.h"
34
35 #define SESSION_BUFFSIZE        4096
36
37 typedef struct _Session Session;
38
39 #define SESSION(obj)    ((Session *)obj)
40
41 typedef enum {
42         SESSION_UNKNOWN,
43         SESSION_IMAP,
44         SESSION_NEWS,
45         SESSION_SMTP,
46         SESSION_POP3
47 } SessionType;
48
49 typedef enum {
50         SESSION_READY,
51         SESSION_SEND,
52         SESSION_RECV,
53         SESSION_EOF,
54         SESSION_TIMEOUT,
55         SESSION_ERROR,
56         SESSION_DISCONNECTED
57 } SessionState;
58
59 typedef enum
60 {
61         SESSION_MSG_NORMAL,
62         SESSION_MSG_SEND_DATA,
63         SESSION_MSG_RECV_DATA,
64         SESSION_MSG_CONTROL,
65         SESSION_MSG_ERROR,
66         SESSION_MSG_UNKNOWN
67 } SessionMsgType;
68
69 typedef gint (*RecvMsgNotify)                   (Session        *session,
70                                                  const gchar    *msg,
71                                                  gpointer        user_data);
72 typedef gint (*RecvDataProgressiveNotify)       (Session        *session,
73                                                  guint           cur_len,
74                                                  guint           total_len,
75                                                  gpointer        user_data);
76 typedef gint (*RecvDataNotify)                  (Session        *session,
77                                                  guint           len,
78                                                  gpointer        user_data);
79 typedef gint (*SendDataProgressiveNotify)       (Session        *session,
80                                                  guint           cur_len,
81                                                  guint           total_len,
82                                                  gpointer        user_data);
83 typedef gint (*SendDataNotify)                  (Session        *session,
84                                                  guint           len,
85                                                  gpointer        user_data);
86
87 struct _Session
88 {
89         SessionType type;
90
91         SockInfo *sock;
92
93         gchar *server;
94         gushort port;
95
96 #if USE_OPENSSL
97         SSLType ssl_type;
98 #endif
99
100         gboolean nonblocking;
101
102         SessionState state;
103
104         time_t last_access_time;
105         struct timeval tv_prev;
106
107         gint conn_id;
108
109         gint io_tag;
110
111         gchar read_buf[SESSION_BUFFSIZE];
112         gchar *read_buf_p;
113         gint read_buf_len;
114
115         GString *read_msg_buf;
116         GByteArray *read_data_buf;
117         gchar *read_data_terminator;
118
119         gchar *write_buf;
120         gchar *write_buf_p;
121         gint write_buf_len;
122
123         guint timeout_tag;
124         guint timeout_interval;
125
126         gpointer data;
127
128         /* virtual methods to parse server responses */
129         gint (*recv_msg)                (Session        *session,
130                                          const gchar    *msg);
131
132         gint (*send_data_finished)      (Session        *session,
133                                          guint           len);
134         gint (*recv_data_finished)      (Session        *session,
135                                          guchar         *data,
136                                          guint           len);
137
138         void (*destroy)                 (Session        *session);
139
140         /* notification functions */
141         RecvMsgNotify                   recv_msg_notify;
142         RecvDataProgressiveNotify       recv_data_progressive_notify;
143         RecvDataNotify                  recv_data_notify;
144         SendDataProgressiveNotify       send_data_progressive_notify;
145         SendDataNotify                  send_data_notify;
146
147         gpointer recv_msg_notify_data;
148         gpointer recv_data_progressive_notify_data;
149         gpointer recv_data_notify_data;
150         gpointer send_data_progressive_notify_data;
151         gpointer send_data_notify_data;
152 };
153
154 void session_init               (Session        *session);
155 gint session_connect            (Session        *session,
156                                  const gchar    *server,
157                                  gushort         port);
158 gint session_disconnect         (Session        *session);
159 void session_destroy            (Session        *session);
160 gboolean session_is_connected   (Session        *session);
161
162 void session_set_timeout        (Session        *session,
163                                  guint           interval);
164
165 void session_set_recv_message_notify    (Session        *session,
166                                          RecvMsgNotify   notify_func,
167                                          gpointer        data);
168 void session_set_recv_data_progressive_notify
169                                         (Session        *session,
170                                          RecvDataProgressiveNotify notify_func,
171                                          gpointer        data);
172 void session_set_recv_data_notify       (Session        *session,
173                                          RecvDataNotify  notify_func,
174                                          gpointer        data);
175 void session_set_send_data_progressive_notify
176                                         (Session        *session,
177                                          SendDataProgressiveNotify notify_func,
178                                          gpointer        data);
179 void session_set_send_data_notify       (Session        *session,
180                                          SendDataNotify  notify_func,
181                                          gpointer        data);
182
183 #if USE_OPENSSL
184 gint session_start_tls  (Session        *session);
185 #endif
186
187 gint session_send_msg   (Session        *session,
188                          SessionMsgType  type,
189                          const gchar    *msg);
190 gint session_recv_msg   (Session        *session);
191 gint session_send_data  (Session        *session,
192                          const guchar   *data,
193                          guint           size);
194 gint session_recv_data  (Session        *session,
195                          guint           size,
196                          const gchar    *terminator);
197
198 #endif /* __SESSION_H__ */