d28bc8d235818e3b8103f46affc696dca8d51734
[claws.git] / src / common / session.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 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_ERROR,
54         SESSION_DISCONNECTED
55 } SessionState;
56
57 typedef enum
58 {
59         SESSION_MSG_NORMAL,
60         SESSION_MSG_SEND_DATA,
61         SESSION_MSG_RECV_DATA,
62         SESSION_MSG_CONTROL,
63         SESSION_MSG_ERROR,
64         SESSION_MSG_UNKNOWN
65 } SessionMsgType;
66
67 typedef gint (*RecvMsgNotify)                   (Session        *session,
68                                                  const gchar    *msg,
69                                                  gpointer        user_data);
70 typedef gint (*RecvDataProgressiveNotify)       (Session        *session,
71                                                  guint           cur_len,
72                                                  guint           total_len,
73                                                  gpointer        user_data);
74 typedef gint (*RecvDataNotify)                  (Session        *session,
75                                                  guint           len,
76                                                  gpointer        user_data);
77 typedef gint (*SendDataProgressiveNotify)       (Session        *session,
78                                                  guint           cur_len,
79                                                  guint           total_len,
80                                                  gpointer        user_data);
81 typedef gint (*SendDataNotify)                  (Session        *session,
82                                                  guint           len,
83                                                  gpointer        user_data);
84
85 struct _Session
86 {
87         SessionType type;
88
89         SockInfo *sock;
90
91         gchar *server;
92         gushort port;
93
94 #if USE_OPENSSL
95         SSLType ssl_type;
96 #endif
97
98         SessionState state;
99
100         time_t last_access_time;
101         struct timeval tv_prev;
102
103         gint conn_id;
104
105         gint io_tag;
106
107         GString *read_buf;
108         GByteArray *read_data_buf;
109         gchar *read_data_terminator;
110
111         gchar *write_buf;
112         gchar *write_buf_p;
113         gint write_buf_len;
114
115         gpointer data;
116
117         /* virtual methods to parse server responses */
118         gint (*recv_msg)                (Session        *session,
119                                          const gchar    *msg);
120
121         gint (*send_data_finished)      (Session        *session,
122                                          guint           len);
123         gint (*recv_data_finished)      (Session        *session,
124                                          guchar         *data,
125                                          guint           len);
126
127         void (*destroy)                 (Session        *session);
128
129         /* notification functions */
130         RecvMsgNotify                   recv_msg_notify;
131         RecvDataProgressiveNotify       recv_data_progressive_notify;
132         RecvDataNotify                  recv_data_notify;
133         SendDataProgressiveNotify       send_data_progressive_notify;
134         SendDataNotify                  send_data_notify;
135
136         gpointer recv_msg_notify_data;
137         gpointer recv_data_progressive_notify_data;
138         gpointer recv_data_notify_data;
139         gpointer send_data_progressive_notify_data;
140         gpointer send_data_notify_data;
141 };
142
143 void session_init       (Session        *session);
144 gint session_connect    (Session        *session,
145                          const gchar    *server,
146                          gushort         port);
147 gint session_disconnect (Session        *session);
148 void session_destroy    (Session        *session);
149
150 void session_set_recv_message_notify    (Session        *session,
151                                          RecvMsgNotify   notify_func,
152                                          gpointer        data);
153 void session_set_recv_data_progressive_notify
154                                         (Session        *session,
155                                          RecvDataProgressiveNotify notify_func,
156                                          gpointer        data);
157 void session_set_recv_data_notify       (Session        *session,
158                                          RecvDataNotify  notify_func,
159                                          gpointer        data);
160 void session_set_send_data_progressive_notify
161                                         (Session        *session,
162                                          SendDataProgressiveNotify notify_func,
163                                          gpointer        data);
164 void session_set_send_data_notify       (Session        *session,
165                                          SendDataNotify  notify_func,
166                                          gpointer        data);
167
168 #if USE_OPENSSL
169 gint session_start_tls  (Session        *session);
170 #endif
171
172 gint session_send_msg   (Session        *session,
173                          SessionMsgType  type,
174                          const gchar    *msg);
175 gint session_recv_msg   (Session        *session);
176 gint session_send_data  (Session        *session,
177                          const guchar   *data,
178                          guint           size);
179 gint session_recv_data  (Session        *session,
180                          guint           size,
181                          const gchar    *terminator);
182
183 #endif /* __SESSION_H__ */