2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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.
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.
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.
31 #include <sys/types.h>
33 #include <sys/signal.h>
41 static gint session_connect_cb (SockInfo *sock,
43 static gint session_close (Session *session);
45 static gboolean session_recv_msg_idle_cb (gpointer data);
46 static gboolean session_recv_data_idle_cb (gpointer data);
48 static gboolean session_read_msg_cb (SockInfo *source,
49 GIOCondition condition,
51 static gboolean session_read_data_cb (SockInfo *source,
52 GIOCondition condition,
54 static gboolean session_write_msg_cb (SockInfo *source,
55 GIOCondition condition,
57 static gboolean session_write_data_cb (SockInfo *source,
58 GIOCondition condition,
62 void session_init(Session *session)
64 session->type = SESSION_UNKNOWN;
66 session->server = NULL;
69 session->ssl_type = SSL_NONE;
71 session->nonblocking = TRUE;
72 session->state = SESSION_READY;
73 session->last_access_time = time(NULL);
75 gettimeofday(&session->tv_prev, NULL);
81 session->read_buf_p = session->read_buf;
82 session->read_buf_len = 0;
84 session->read_msg_buf = g_string_sized_new(1024);
85 session->read_data_buf = g_byte_array_new();
87 session->write_buf = NULL;
88 session->write_buf_p = NULL;
89 session->write_buf_len = 0;
95 *\brief Set up parent and child process
96 * Childloop: Read commands from parent,
97 * send to server, get answer, pass to parent
99 *\param session Contains session information
100 * server to connect to
104 * -1 : pipe / fork errors (parent)
105 * 1 : connection error (child)
107 gint session_connect(Session *session, const gchar *server, gushort port)
109 session->server = g_strdup(server);
110 session->port = port;
112 session->conn_id = sock_connect_async(server, port, session_connect_cb,
114 if (session->conn_id < 0) {
115 g_warning("can't connect to server.");
116 session_close(session);
123 static gint session_connect_cb(SockInfo *sock, gpointer data)
125 Session *session = SESSION(data);
127 session->conn_id = 0;
130 g_warning("can't connect to server.");
131 session->state = SESSION_ERROR;
135 session->sock = sock;
138 if (session->ssl_type == SSL_TUNNEL) {
139 sock_set_nonblocking_mode(sock, FALSE);
140 if (!ssl_init_socket(sock)) {
141 g_warning("can't initialize SSL.");
142 session->state = SESSION_ERROR;
148 sock_set_nonblocking_mode(sock, session->nonblocking);
150 debug_print("session: connected\n");
152 session->state = SESSION_RECV;
153 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
161 *\brief child and parent: send DISCONNECT message to other process
163 *\param session Contains session information
167 gint session_disconnect(Session *session)
169 session_close(session);
176 *\param session Contains session information
178 void session_destroy(Session *session)
180 g_return_if_fail(session != NULL);
181 g_return_if_fail(session->destroy != NULL);
183 debug_print("session: session_destroy()\n");
185 session_close(session);
186 session->destroy(session);
187 g_free(session->server);
188 g_string_free(session->read_msg_buf, TRUE);
189 g_byte_array_free(session->read_data_buf, TRUE);
190 g_free(session->read_data_terminator);
191 g_free(session->write_buf);
195 void session_set_recv_message_notify(Session *session,
196 RecvMsgNotify notify_func, gpointer data)
198 session->recv_msg_notify = notify_func;
199 session->recv_msg_notify_data = data;
202 void session_set_recv_data_progressive_notify
204 RecvDataProgressiveNotify notify_func,
207 session->recv_data_progressive_notify = notify_func,
208 session->recv_data_progressive_notify_data = data;
211 void session_set_recv_data_notify(Session *session, RecvDataNotify notify_func,
214 session->recv_data_notify = notify_func;
215 session->recv_data_notify_data = data;
218 void session_set_send_data_progressive_notify
220 SendDataProgressiveNotify notify_func,
223 session->send_data_progressive_notify = notify_func;
224 session->send_data_progressive_notify_data = data;
227 void session_set_send_data_notify(Session *session, SendDataNotify notify_func,
230 session->send_data_notify = notify_func;
231 session->send_data_notify_data = data;
235 *\brief child and parent cleanup (child closes first)
237 *\param session Contains session information
241 static gint session_close(Session *session)
243 g_return_val_if_fail(session != NULL, -1);
245 debug_print("session_close\n");
247 if (session->conn_id > 0) {
248 sock_connect_async_cancel(session->conn_id);
249 session->conn_id = 0;
252 if (session->io_tag > 0) {
253 g_source_remove(session->io_tag);
258 sock_close(session->sock);
259 session->sock = NULL;
260 session->state = SESSION_DISCONNECTED;
267 gint session_start_tls(Session *session)
271 nb_mode = sock_is_nonblocking_mode(session->sock);
274 sock_set_nonblocking_mode(session->sock, FALSE);
276 if (!ssl_init_socket_with_method(session->sock, SSL_METHOD_TLSv1)) {
277 g_warning("can't start TLS session.\n");
279 sock_set_nonblocking_mode(session->sock, TRUE);
284 sock_set_nonblocking_mode(session->sock, session->nonblocking);
290 gint session_send_msg(Session *session, SessionMsgType type, const gchar *msg)
294 g_return_val_if_fail(session->write_buf == NULL, -1);
295 g_return_val_if_fail(msg != NULL, -1);
296 g_return_val_if_fail(msg[0] != '\0', -1);
298 session->state = SESSION_SEND;
299 session->write_buf = g_strconcat(msg, "\r\n", NULL);
300 session->write_buf_p = session->write_buf;
301 session->write_buf_len = strlen(msg) + 2;
303 ret = session_write_msg_cb(session->sock, G_IO_OUT, session);
306 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
307 session_write_msg_cb, session);
308 else if (session->state == SESSION_ERROR)
314 gint session_recv_msg(Session *session)
316 g_return_val_if_fail(session->read_msg_buf->len == 0, -1);
318 session->state = SESSION_RECV;
320 if (session->read_buf_len > 0)
321 g_idle_add(session_recv_msg_idle_cb, session);
323 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
324 session_read_msg_cb, session);
329 static gboolean session_recv_msg_idle_cb(gpointer data)
331 Session *session = SESSION(data);
334 ret = session_read_msg_cb(session->sock, G_IO_IN, session);
337 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
338 session_read_msg_cb, session);
344 *\brief parent (child?): send data to other process
346 *\param session Contains session information
353 gint session_send_data(Session *session, const guchar *data, guint size)
357 g_return_val_if_fail(session->write_buf == NULL, -1);
358 g_return_val_if_fail(data != NULL, -1);
359 g_return_val_if_fail(size != 0, -1);
361 session->state = SESSION_SEND;
363 session->write_buf = g_malloc(size);
364 session->write_buf_p = session->write_buf;
365 memcpy(session->write_buf, data, size);
366 session->write_buf_len = size;
367 gettimeofday(&session->tv_prev, NULL);
369 ret = session_write_data_cb(session->sock, G_IO_OUT, session);
372 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
373 session_write_data_cb,
375 else if (session->state == SESSION_ERROR)
381 gint session_recv_data(Session *session, guint size, const gchar *terminator)
383 g_return_val_if_fail(session->read_data_buf->len == 0, -1);
385 session->state = SESSION_RECV;
387 g_free(session->read_data_terminator);
388 session->read_data_terminator = g_strdup(terminator);
389 gettimeofday(&session->tv_prev, NULL);
391 if (session->read_buf_len > 0)
392 g_idle_add(session_recv_data_idle_cb, session);
394 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
395 session_read_data_cb, session);
400 static gboolean session_recv_data_idle_cb(gpointer data)
402 Session *session = SESSION(data);
405 ret = session_read_data_cb(session->sock, G_IO_IN, session);
408 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
409 session_read_data_cb, session);
414 static gboolean session_read_msg_cb(SockInfo *source, GIOCondition condition,
417 Session *session = SESSION(data);
418 gchar buf[SESSION_BUFFSIZE];
424 g_return_val_if_fail(condition == G_IO_IN, FALSE);
426 if (session->read_buf_len == 0) {
429 read_len = sock_read(session->sock, session->read_buf,
430 SESSION_BUFFSIZE - 1);
437 g_warning("sock_read: %s\n", g_strerror(errno));
438 session->state = SESSION_ERROR;
443 session->read_buf_len = read_len;
446 if ((newline = memchr(session->read_buf_p, '\n', session->read_buf_len))
448 line_len = newline - session->read_buf_p + 1;
450 line_len = session->read_buf_len;
455 memcpy(buf, session->read_buf_p, line_len);
456 buf[line_len] = '\0';
458 g_string_append(session->read_msg_buf, buf);
460 session->read_buf_len -= line_len;
461 if (session->read_buf_len == 0)
462 session->read_buf_p = session->read_buf;
464 session->read_buf_p += line_len;
466 /* incomplete read */
467 if (buf[line_len - 1] != '\n')
471 if (session->io_tag > 0) {
472 g_source_remove(session->io_tag);
477 msg = g_strdup(session->read_msg_buf->str);
479 g_string_truncate(session->read_msg_buf, 0);
481 ret = session->recv_msg(session, msg);
482 session->recv_msg_notify(session, msg, session->recv_msg_notify_data);
487 session->state = SESSION_ERROR;
492 static gboolean session_read_data_cb(SockInfo *source, GIOCondition condition,
495 Session *session = SESSION(data);
496 GByteArray *data_buf;
498 gboolean complete = FALSE;
502 g_return_val_if_fail(condition == G_IO_IN, FALSE);
504 if (session->read_buf_len == 0) {
507 read_len = sock_read(session->sock, session->read_buf,
515 g_warning("sock_read: %s\n", g_strerror(errno));
516 session->state = SESSION_ERROR;
521 session->read_buf_len = read_len;
524 data_buf = session->read_data_buf;
525 terminator_len = strlen(session->read_data_terminator);
527 if (session->read_buf_len == 0)
530 g_byte_array_append(data_buf, session->read_buf_p,
531 session->read_buf_len);
533 session->read_buf_len = 0;
534 session->read_buf_p = session->read_buf;
536 /* check if data is terminated */
537 if (data_buf->len >= terminator_len) {
538 if (memcmp(data_buf->data, session->read_data_terminator,
539 terminator_len) == 0)
541 else if (data_buf->len >= terminator_len + 2 &&
542 memcmp(data_buf->data + data_buf->len -
543 (terminator_len + 2), "\r\n", 2) == 0 &&
544 memcmp(data_buf->data + data_buf->len -
545 terminator_len, session->read_data_terminator,
546 terminator_len) == 0)
550 /* incomplete read */
552 struct timeval tv_cur;
554 gettimeofday(&tv_cur, NULL);
555 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
556 tv_cur.tv_usec - session->tv_prev.tv_usec >
557 UI_REFRESH_INTERVAL) {
558 session->recv_data_progressive_notify
559 (session, data_buf->len, 0,
560 session->recv_data_progressive_notify_data);
561 gettimeofday(&session->tv_prev, NULL);
567 if (session->io_tag > 0) {
568 g_source_remove(session->io_tag);
572 data_len = data_buf->len - terminator_len;
575 ret = session->recv_data_finished(session, (gchar *)data_buf->data,
578 g_byte_array_set_size(data_buf, 0);
580 session->recv_data_notify(session, data_len,
581 session->recv_data_notify_data);
584 session->state = SESSION_ERROR;
589 static gint session_write_buf(Session *session)
594 g_return_val_if_fail(session->write_buf != NULL, -1);
595 g_return_val_if_fail(session->write_buf_p != NULL, -1);
596 g_return_val_if_fail(session->write_buf_len > 0, -1);
598 to_write_len = session->write_buf_len -
599 (session->write_buf_p - session->write_buf);
600 to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
602 write_len = sock_write(session->sock, session->write_buf_p,
611 g_warning("sock_write: %s\n", g_strerror(errno));
612 session->state = SESSION_ERROR;
617 /* incomplete write */
618 if (session->write_buf_p - session->write_buf + write_len <
619 session->write_buf_len) {
620 session->write_buf_p += write_len;
624 g_free(session->write_buf);
625 session->write_buf = NULL;
626 session->write_buf_p = NULL;
627 session->write_buf_len = 0;
632 static gboolean session_write_msg_cb(SockInfo *source, GIOCondition condition,
635 Session *session = SESSION(data);
638 g_return_val_if_fail(condition == G_IO_OUT, FALSE);
639 g_return_val_if_fail(session->write_buf != NULL, FALSE);
640 g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
641 g_return_val_if_fail(session->write_buf_len > 0, FALSE);
643 ret = session_write_buf(session);
646 session->state = SESSION_ERROR;
651 if (session->io_tag > 0) {
652 g_source_remove(session->io_tag);
656 session_recv_msg(session);
661 static gboolean session_write_data_cb(SockInfo *source,
662 GIOCondition condition, gpointer data)
664 Session *session = SESSION(data);
668 g_return_val_if_fail(condition == G_IO_OUT, FALSE);
669 g_return_val_if_fail(session->write_buf != NULL, FALSE);
670 g_return_val_if_fail(session->write_buf_p != NULL, FALSE);
671 g_return_val_if_fail(session->write_buf_len > 0, FALSE);
673 write_buf_len = session->write_buf_len;
675 ret = session_write_buf(session);
678 session->state = SESSION_ERROR;
680 } else if (ret > 0) {
681 struct timeval tv_cur;
683 gettimeofday(&tv_cur, NULL);
684 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
685 tv_cur.tv_usec - session->tv_prev.tv_usec >
686 UI_REFRESH_INTERVAL) {
687 session->send_data_progressive_notify
689 session->write_buf_p - session->write_buf,
691 session->send_data_progressive_notify_data);
692 gettimeofday(&session->tv_prev, NULL);
697 if (session->io_tag > 0) {
698 g_source_remove(session->io_tag);
703 ret = session->send_data_finished(session, write_buf_len);
704 session->send_data_notify(session, write_buf_len,
705 session->send_data_notify_data);