1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef __SESSION_H__
21 #define __SESSION_H__
22 
23 #ifdef HAVE_CONFIG_H
24 #include "claws-features.h"
25 #endif
26 
27 #include <glib.h>
28 
29 #include <time.h>
30 #include <unistd.h>
31 
32 #include "socket.h"
33 #include "proxy.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_TIMEOUT,
55 	SESSION_ERROR,
56 	SESSION_DISCONNECTED
57 } SessionState;
58 
59 typedef gint (*RecvMsgNotify)			(Session	*session,
60 						 const gchar	*msg,
61 						 gpointer	 user_data);
62 typedef gint (*RecvDataProgressiveNotify)	(Session	*session,
63 						 guint		 cur_len,
64 						 guint		 total_len,
65 						 gpointer	 user_data);
66 typedef gint (*RecvDataNotify)			(Session	*session,
67 						 guint		 len,
68 						 gpointer	 user_data);
69 typedef gint (*SendDataProgressiveNotify)	(Session	*session,
70 						 guint		 cur_len,
71 						 guint		 total_len,
72 						 gpointer	 user_data);
73 typedef gint (*SendDataNotify)			(Session	*session,
74 						 guint		 len,
75 						 gpointer	 user_data);
76 
77 struct _Session
78 {
79 	SessionType type;
80 
81 	SockInfo *sock;
82 
83 	gchar *server;
84 	gushort port;
85 
86 	gboolean nonblocking;
87 
88 	SessionState state;
89 
90 	time_t last_access_time;
91 	GDateTime *tv_prev;
92 	gint conn_id;
93 
94 	gint io_tag;
95 
96 	gchar read_buf[SESSION_BUFFSIZE];
97 	gchar *read_buf_p;
98 	gint read_buf_len;
99 
100 	GString *read_msg_buf;
101 	GByteArray *read_data_buf;
102 	gchar *read_data_terminator;
103 
104 	/* buffer for short messages */
105 	gchar *write_buf;
106 	gchar *write_buf_p;
107 	gint write_buf_len;
108 
109 	/* buffer for large data */
110 	const guchar *write_data;
111 	const guchar *write_data_p;
112 	gint write_data_len;
113 
114 	guint timeout_tag;
115 	guint timeout_interval;
116 
117 	gpointer data;
118 
119 	/* virtual methods to parse server responses */
120 	gint (*recv_msg)		(Session	*session,
121 					 const gchar	*msg);
122 
123 	void (*connect_finished)	(Session	*session,
124 					 gboolean	success);
125 	gint (*send_data_finished)	(Session	*session,
126 					 guint		 len);
127 	gint (*recv_data_finished)	(Session	*session,
128 					 guchar		*data,
129 					 guint		 len);
130 
131 	void (*destroy)			(Session	*session);
132 
133 	/* notification functions */
134 	RecvMsgNotify			recv_msg_notify;
135 	RecvDataProgressiveNotify	recv_data_progressive_notify;
136 	RecvDataNotify			recv_data_notify;
137 	SendDataProgressiveNotify	send_data_progressive_notify;
138 	SendDataNotify			send_data_notify;
139 
140 	gpointer recv_msg_notify_data;
141 	gpointer recv_data_progressive_notify_data;
142 	gpointer recv_data_notify_data;
143 	gpointer send_data_progressive_notify_data;
144 	gpointer send_data_notify_data;
145 
146 	const void *account;
147 	gboolean is_smtp;
148 	gboolean ssl_cert_auto_accept;
149 	gint ping_tag;
150 
151 	/* Pointer to ProxyInfo struct holding the info about proxy
152 	 * to be used. Set to NULL if no proxy is used.
153 	 * If non-NULL, the memory this pointer is pointing at does
154 	 * not belong to this Session, and shouldn't be modified
155 	 * or freed by Session. It is usually a pointer to the
156 	 * SockInfo in common prefs, or in account prefs. */
157 	ProxyInfo *proxy_info;
158 
159 #ifdef USE_GNUTLS
160 	SSLType ssl_type;
161 	gchar *gnutls_priority;
162 	gboolean use_tls_sni;
163 #endif
164 };
165 
166 void session_init		(Session	*session,
167 				 const void 	*prefs_account,
168 				 gboolean	 is_smtp);
169 gint session_connect		(Session	*session,
170 				 const gchar	*server,
171 				 gushort	 port);
172 gint session_disconnect		(Session	*session);
173 void session_destroy		(Session	*session);
174 gboolean session_is_running	(Session	*session);
175 gboolean session_is_connected	(Session	*session);
176 
177 void session_set_access_time	(Session	*session);
178 
179 void session_set_timeout	(Session	*session,
180 				 guint		 interval);
181 
182 void session_set_recv_message_notify	(Session	*session,
183 					 RecvMsgNotify	 notify_func,
184 					 gpointer	 data);
185 void session_set_recv_data_progressive_notify
186 					(Session	*session,
187 					 RecvDataProgressiveNotify notify_func,
188 					 gpointer	 data);
189 void session_set_recv_data_notify	(Session	*session,
190 					 RecvDataNotify	 notify_func,
191 					 gpointer	 data);
192 void session_set_send_data_progressive_notify
193 					(Session	*session,
194 					 SendDataProgressiveNotify notify_func,
195 					 gpointer	 data);
196 void session_set_send_data_notify	(Session	*session,
197 					 SendDataNotify	 notify_func,
198 					 gpointer	 data);
199 
200 #ifdef USE_GNUTLS
201 gint session_start_tls	(Session	*session);
202 #endif
203 
204 gint session_send_msg	(Session	*session,
205 			 const gchar	*msg);
206 gint session_recv_msg	(Session	*session);
207 gint session_send_data	(Session	*session,
208 			 const guchar	*data,
209 			 guint		 size);
210 gint session_recv_data	(Session	*session,
211 			 guint		 size,
212 			 const gchar	*terminator);
213 void session_register_ping(Session *session, gboolean (*ping_cb)(gpointer data));
214 
215 #endif /* __SESSION_H__ */
216