summaryrefslogtreecommitdiff
path: root/libsylph/session.h
diff options
context:
space:
mode:
Diffstat (limited to 'libsylph/session.h')
-rw-r--r--libsylph/session.h248
1 files changed, 248 insertions, 0 deletions
diff --git a/libsylph/session.h b/libsylph/session.h
new file mode 100644
index 0000000..41c10e2
--- /dev/null
+++ b/libsylph/session.h
@@ -0,0 +1,248 @@
1/*
2 * LibSylph -- E-Mail client library
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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#include <stdio.h>
29#include <time.h>
30#include <unistd.h>
31
32#include "socket.h"
33#include "socks.h"
34#include "utils.h"
35
36#define SESSION_BUFFSIZE 8192
37
38typedef struct _Session Session;
39
40#define SESSION(obj) ((Session *)obj)
41
42typedef enum {
43 SESSION_UNKNOWN,
44 SESSION_IMAP,
45 SESSION_NEWS,
46 SESSION_SMTP,
47 SESSION_POP3
48} SessionType;
49
50typedef enum {
51 SESSION_READY,
52 SESSION_SEND,
53 SESSION_RECV,
54 SESSION_EOF,
55 SESSION_TIMEOUT,
56 SESSION_ERROR,
57 SESSION_DISCONNECTED
58} SessionState;
59
60typedef enum
61{
62 SESSION_MSG_NORMAL,
63 SESSION_MSG_SEND_DATA,
64 SESSION_MSG_RECV_DATA,
65 SESSION_MSG_CONTROL,
66 SESSION_MSG_ERROR,
67 SESSION_MSG_UNKNOWN
68} SessionMsgType;
69
70#ifndef USE_SSL
71typedef enum
72{
73 SSL_NONE
74} SSLType;
75#endif
76
77typedef enum {
78 SESSION_ERROR_OK,
79 SESSION_ERROR_LOOKUP,
80 SESSION_ERROR_CONNFAIL,
81 SESSION_ERROR_IO,
82 SESSION_ERROR_SOCKET,
83 SESSION_ERROR_TIMEOUT,
84 SESSION_ERROR_ERROR
85} SessionErrorValue;
86
87typedef gint (*RecvMsgNotify) (Session *session,
88 const gchar *msg,
89 gpointer user_data);
90typedef gint (*RecvDataProgressiveNotify) (Session *session,
91 guint cur_len,
92 guint total_len,
93 gpointer user_data);
94typedef gint (*RecvDataNotify) (Session *session,
95 guint len,
96 gpointer user_data);
97typedef gint (*SendDataProgressiveNotify) (Session *session,
98 guint cur_len,
99 guint total_len,
100 gpointer user_data);
101typedef gint (*SendDataNotify) (Session *session,
102 guint len,
103 gpointer user_data);
104
105struct _Session
106{
107 SessionType type;
108
109 SockInfo *sock;
110
111 gchar *server;
112 gushort port;
113
114 SSLType ssl_type;
115
116 gboolean nonblocking;
117
118 SessionState state;
119
120 stime_t last_access_time;
121 GTimeVal tv_prev;
122
123 gint conn_id;
124
125 gint io_tag;
126
127 gchar read_buf[SESSION_BUFFSIZE];
128 gchar *read_buf_p;
129 gint read_buf_len;
130
131 /* buffer for short messages */
132 GString *read_msg_buf;
133
134 /* buffer for relatively short multiple lines data */
135 GByteArray *read_data_buf;
136 gchar *read_data_terminator;
137
138 /* buffer for large data */
139 FILE *read_data_fp;
140 gint read_data_pos;
141
142 gint preread_len;
143
144 /* buffer for short messages */
145 gchar *write_buf;
146 gchar *write_buf_p;
147 gint write_buf_len;
148
149 /* buffer for large data */
150 FILE *write_data_fp;
151 gint write_data_pos;
152 gint write_data_len;
153
154 guint timeout_tag;
155 guint timeout_interval;
156
157 guint idle_tag;
158 guint ping_tag;
159
160 gpointer data;
161
162 /* virtual methods to parse server responses */
163 gint (*recv_msg) (Session *session,
164 const gchar *msg);
165
166 gint (*send_data_finished) (Session *session,
167 guint len);
168 gint (*recv_data_finished) (Session *session,
169 guchar *data,
170 guint len);
171
172 gint (*recv_data_as_file_finished) (Session *session,
173 FILE *fp,
174 guint len);
175
176 void (*destroy) (Session *session);
177
178 /* notification functions */
179 RecvMsgNotify recv_msg_notify;
180 RecvDataProgressiveNotify recv_data_progressive_notify;
181 RecvDataNotify recv_data_notify;
182 SendDataProgressiveNotify send_data_progressive_notify;
183 SendDataNotify send_data_notify;
184
185 gpointer recv_msg_notify_data;
186 gpointer recv_data_progressive_notify_data;
187 gpointer recv_data_notify_data;
188 gpointer send_data_progressive_notify_data;
189 gpointer send_data_notify_data;
190};
191
192void session_init (Session *session);
193gint session_connect (Session *session,
194 const gchar *server,
195 gushort port);
196gint session_connect_full (Session *session,
197 const gchar *server,
198 gushort port,
199 SocksInfo *socks_info);
200gint session_disconnect (Session *session);
201void session_destroy (Session *session);
202gboolean session_is_connected (Session *session);
203
204SessionErrorValue session_get_error (Session *session);
205
206void session_set_access_time (Session *session);
207
208void session_set_timeout (Session *session,
209 guint interval);
210
211void session_set_recv_message_notify (Session *session,
212 RecvMsgNotify notify_func,
213 gpointer data);
214void session_set_recv_data_progressive_notify
215 (Session *session,
216 RecvDataProgressiveNotify notify_func,
217 gpointer data);
218void session_set_recv_data_notify (Session *session,
219 RecvDataNotify notify_func,
220 gpointer data);
221void session_set_send_data_progressive_notify
222 (Session *session,
223 SendDataProgressiveNotify notify_func,
224 gpointer data);
225void session_set_send_data_notify (Session *session,
226 SendDataNotify notify_func,
227 gpointer data);
228
229#if USE_SSL
230gint session_start_tls (Session *session);
231#endif
232
233gint session_send_msg (Session *session,
234 SessionMsgType type,
235 const gchar *msg);
236gint session_recv_msg (Session *session);
237gint session_send_data (Session *session,
238 FILE *data_fp,
239 guint size);
240gint session_recv_data (Session *session,
241 guint size,
242 const gchar *terminator);
243
244gint session_recv_data_as_file (Session *session,
245 guint size,
246 const gchar *terminator);
247
248#endif /* __SESSION_H__ */