4a6517ceec7ea7c7d1a7cb3df6988bd97922419e
[claws.git] / src / common / session.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2005 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 <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 #if USE_OPENSSL
96         SSLType ssl_type;
97 #endif
98
99         gboolean nonblocking;
100
101         SessionState state;
102
103         time_t last_access_time;
104         GTimeVal tv_prev;
105
106         gint conn_id;
107
108         gint io_tag;
109
110         gchar read_buf[SESSION_BUFFSIZE];
111         gchar *read_buf_p;
112         gint read_buf_len;
113
114         GString *read_msg_buf;
115         GByteArray *read_data_buf;
116         gchar *read_data_terminator;
117
118         /* buffer for short messages */
119         gchar *write_buf;
120         gchar *write_buf_p;
121         gint write_buf_len;
122
123         /* buffer for large data */
124         const guchar *write_data;
125         const guchar *write_data_p;
126         gint write_data_len;
127
128         guint timeout_tag;
129         guint timeout_interval;
130
131         gpointer data;
132
133         /* virtual methods to parse server responses */
134         gint (*recv_msg)                (Session        *session,
135                                          const gchar    *msg);
136
137         gint (*send_data_finished)      (Session        *session,
138                                          guint           len);
139         gint (*recv_data_finished)      (Session        *session,
140                                          guchar         *data,
141                                          guint           len);
142
143         void (*destroy)                 (Session        *session);
144
145         /* notification functions */
146         RecvMsgNotify                   recv_msg_notify;
147         RecvDataProgressiveNotify       recv_data_progressive_notify;
148         RecvDataNotify                  recv_data_notify;
149         SendDataProgressiveNotify       send_data_progressive_notify;
150         SendDataNotify                  send_data_notify;
151
152         gpointer recv_msg_notify_data;
153         gpointer recv_data_progressive_notify_data;
154         gpointer recv_data_notify_data;
155         gpointer send_data_progressive_notify_data;
156         gpointer send_data_notify_data;
157 };
158
159 void session_init               (Session        *session);
160 gint session_connect            (Session        *session,
161                                  const gchar    *server,
162                                  gushort         port);
163 gint session_disconnect         (Session        *session);
164 void session_destroy            (Session        *session);
165 gboolean session_is_connected   (Session        *session);
166
167 void session_set_access_time    (Session        *session);
168
169 void session_set_timeout        (Session        *session,
170                                  guint           interval);
171
172 void session_set_recv_message_notify    (Session        *session,
173                                          RecvMsgNotify   notify_func,
174                                          gpointer        data);
175 void session_set_recv_data_progressive_notify
176                                         (Session        *session,
177                                          RecvDataProgressiveNotify notify_func,
178                                          gpointer        data);
179 void session_set_recv_data_notify       (Session        *session,
180                                          RecvDataNotify  notify_func,
181                                          gpointer        data);
182 void session_set_send_data_progressive_notify
183                                         (Session        *session,
184                                          SendDataProgressiveNotify notify_func,
185                                          gpointer        data);
186 void session_set_send_data_notify       (Session        *session,
187                                          SendDataNotify  notify_func,
188                                          gpointer        data);
189
190 #if USE_OPENSSL
191 gint session_start_tls  (Session        *session);
192 #endif
193
194 gint session_send_msg   (Session        *session,
195                          SessionMsgType  type,
196                          const gchar    *msg);
197 gint session_recv_msg   (Session        *session);
198 gint session_send_data  (Session        *session,
199                          const guchar   *data,
200                          guint           size);
201 gint session_recv_data  (Session        *session,
202                          guint           size,
203                          const gchar    *terminator);
204
205 #endif /* __SESSION_H__ */