3f4a0e11a0a8893aa01815f0f4fc400c296445d5
[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         /* I/O channel for socket */
106         GIOChannel *sock_ch;
107         gint io_tag;
108
109         GString *read_buf;
110         GByteArray *read_data_buf;
111         gchar *read_data_terminator;
112
113         gchar *write_buf;
114         gchar *write_buf_p;
115         gint write_buf_len;
116
117         gpointer data;
118
119         /* virtual methods to parse server responses */
120         gint (*recv_msg)                (Session        *session,
121                                          const gchar    *msg);
122
123         gint (*send_data_finished)      (Session        *session,
124                                          guint           len);
125         gint (*recv_data_finished)      (Session        *session,
126                                          guchar         *data,
127                                          guint           len);
128
129         void (*destroy)                 (Session        *session);
130
131         /* notification functions */
132         RecvMsgNotify                   recv_msg_notify;
133         RecvDataProgressiveNotify       recv_data_progressive_notify;
134         RecvDataNotify                  recv_data_notify;
135         SendDataProgressiveNotify       send_data_progressive_notify;
136         SendDataNotify                  send_data_notify;
137
138         gpointer recv_msg_notify_data;
139         gpointer recv_data_progressive_notify_data;
140         gpointer recv_data_notify_data;
141         gpointer send_data_progressive_notify_data;
142         gpointer send_data_notify_data;
143 };
144
145 void session_init       (Session        *session);
146 gint session_connect    (Session        *session,
147                          const gchar    *server,
148                          gushort         port);
149 gint session_disconnect (Session        *session);
150 void session_destroy    (Session        *session);
151
152 void session_set_recv_message_notify    (Session        *session,
153                                          RecvMsgNotify   notify_func,
154                                          gpointer        data);
155 void session_set_recv_data_progressive_notify
156                                         (Session        *session,
157                                          RecvDataProgressiveNotify notify_func,
158                                          gpointer        data);
159 void session_set_recv_data_notify       (Session        *session,
160                                          RecvDataNotify  notify_func,
161                                          gpointer        data);
162 void session_set_send_data_progressive_notify
163                                         (Session        *session,
164                                          SendDataProgressiveNotify notify_func,
165                                          gpointer        data);
166 void session_set_send_data_notify       (Session        *session,
167                                          SendDataNotify  notify_func,
168                                          gpointer        data);
169
170 #if USE_OPENSSL
171 gint session_start_tls  (Session        *session);
172 #endif
173
174 gint session_send_msg   (Session        *session,
175                          SessionMsgType  type,
176                          const gchar    *msg);
177 gint session_recv_msg   (Session        *session);
178 gint session_send_data  (Session        *session,
179                          const guchar   *data,
180                          guint           size);
181 gint session_recv_data  (Session        *session,
182                          guint           size,
183                          const gchar    *terminator);
184
185 #endif /* __SESSION_H__ */