Statistics
| Revision:

root / libsylph / session.h @ 3386

History | View | Annotate | Download (5.67 KB)

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

    
38
typedef struct _Session        Session;
39

    
40
#define SESSION(obj)        ((Session *)obj)
41

    
42
typedef enum {
43
        SESSION_UNKNOWN,
44
        SESSION_IMAP,
45
        SESSION_NEWS,
46
        SESSION_SMTP,
47
        SESSION_POP3
48
} SessionType;
49

    
50
typedef 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

    
60
typedef 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
71
typedef enum
72
{
73
        SSL_NONE
74
} SSLType;
75
#endif
76

    
77
typedef 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

    
87
typedef gint (*RecvMsgNotify)                        (Session        *session,
88
                                                 const gchar        *msg,
89
                                                 gpointer         user_data);
90
typedef gint (*RecvDataProgressiveNotify)        (Session        *session,
91
                                                 guint                 cur_len,
92
                                                 guint                 total_len,
93
                                                 gpointer         user_data);
94
typedef gint (*RecvDataNotify)                        (Session        *session,
95
                                                 guint                 len,
96
                                                 gpointer         user_data);
97
typedef gint (*SendDataProgressiveNotify)        (Session        *session,
98
                                                 guint                 cur_len,
99
                                                 guint                 total_len,
100
                                                 gpointer         user_data);
101
typedef gint (*SendDataNotify)                        (Session        *session,
102
                                                 guint                 len,
103
                                                 gpointer         user_data);
104

    
105
struct _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

    
192
void session_init                (Session        *session);
193
gint session_connect                (Session        *session,
194
                                 const gchar        *server,
195
                                 gushort         port);
196
gint session_connect_full        (Session        *session,
197
                                 const gchar        *server,
198
                                 gushort         port,
199
                                 SocksInfo        *socks_info);
200
gint session_disconnect                (Session        *session);
201
void session_destroy                (Session        *session);
202
gboolean session_is_connected        (Session        *session);
203

    
204
SessionErrorValue session_get_error        (Session        *session);
205

    
206
void session_set_access_time        (Session        *session);
207

    
208
void session_set_timeout        (Session        *session,
209
                                 guint                 interval);
210

    
211
void session_set_recv_message_notify        (Session        *session,
212
                                         RecvMsgNotify         notify_func,
213
                                         gpointer         data);
214
void session_set_recv_data_progressive_notify
215
                                        (Session        *session,
216
                                         RecvDataProgressiveNotify notify_func,
217
                                         gpointer         data);
218
void session_set_recv_data_notify        (Session        *session,
219
                                         RecvDataNotify         notify_func,
220
                                         gpointer         data);
221
void session_set_send_data_progressive_notify
222
                                        (Session        *session,
223
                                         SendDataProgressiveNotify notify_func,
224
                                         gpointer         data);
225
void session_set_send_data_notify        (Session        *session,
226
                                         SendDataNotify         notify_func,
227
                                         gpointer         data);
228

    
229
#if USE_SSL
230
gint session_start_tls        (Session        *session);
231
#endif
232

    
233
gint session_send_msg        (Session        *session,
234
                         SessionMsgType         type,
235
                         const gchar        *msg);
236
gint session_recv_msg        (Session        *session);
237
gint session_send_data        (Session        *session,
238
                         FILE                *data_fp,
239
                         guint                 size);
240
gint session_recv_data        (Session        *session,
241
                         guint                 size,
242
                         const gchar        *terminator);
243

    
244
gint session_recv_data_as_file        (Session        *session,
245
                                 guint                 size,
246
                                 const gchar        *terminator);
247

    
248
#endif /* __SESSION_H__ */