0.9.4claws42
[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_EOF,
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         struct timeval 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         gchar *write_buf;
119         gchar *write_buf_p;
120         gint write_buf_len;
121
122         gpointer data;
123
124         /* virtual methods to parse server responses */
125         gint (*recv_msg)                (Session        *session,
126                                          const gchar    *msg);
127
128         gint (*send_data_finished)      (Session        *session,
129                                          guint           len);
130         gint (*recv_data_finished)      (Session        *session,
131                                          guchar         *data,
132                                          guint           len);
133
134         void (*destroy)                 (Session        *session);
135
136         /* notification functions */
137         RecvMsgNotify                   recv_msg_notify;
138         RecvDataProgressiveNotify       recv_data_progressive_notify;
139         RecvDataNotify                  recv_data_notify;
140         SendDataProgressiveNotify       send_data_progressive_notify;
141         SendDataNotify                  send_data_notify;
142
143         gpointer recv_msg_notify_data;
144         gpointer recv_data_progressive_notify_data;
145         gpointer recv_data_notify_data;
146         gpointer send_data_progressive_notify_data;
147         gpointer send_data_notify_data;
148 };
149
150 void session_init               (Session        *session);
151 gint session_connect            (Session        *session,
152                                  const gchar    *server,
153                                  gushort         port);
154 gint session_disconnect         (Session        *session);
155 void session_destroy            (Session        *session);
156 gboolean session_is_connected   (Session        *session);
157
158 void session_set_recv_message_notify    (Session        *session,
159                                          RecvMsgNotify   notify_func,
160                                          gpointer        data);
161 void session_set_recv_data_progressive_notify
162                                         (Session        *session,
163                                          RecvDataProgressiveNotify notify_func,
164                                          gpointer        data);
165 void session_set_recv_data_notify       (Session        *session,
166                                          RecvDataNotify  notify_func,
167                                          gpointer        data);
168 void session_set_send_data_progressive_notify
169                                         (Session        *session,
170                                          SendDataProgressiveNotify notify_func,
171                                          gpointer        data);
172 void session_set_send_data_notify       (Session        *session,
173                                          SendDataNotify  notify_func,
174                                          gpointer        data);
175
176 #if USE_OPENSSL
177 gint session_start_tls  (Session        *session);
178 #endif
179
180 gint session_send_msg   (Session        *session,
181                          SessionMsgType  type,
182                          const gchar    *msg);
183 gint session_recv_msg   (Session        *session);
184 gint session_send_data  (Session        *session,
185                          const guchar   *data,
186                          guint           size);
187 gint session_recv_data  (Session        *session,
188                          guint           size,
189                          const gchar    *terminator);
190
191 #endif /* __SESSION_H__ */