Use gnutls_priority override for STARTTLS as well.
[claws.git] / src / common / session.c
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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <time.h>
35 #include <errno.h>
36
37 #include "session.h"
38 #include "utils.h"
39 #include "log.h"
40
41 static gint session_connect_cb          (SockInfo       *sock,
42                                          gpointer        data);
43 static gint session_close               (Session        *session);
44
45 static gboolean session_timeout_cb      (gpointer        data);
46
47 static gboolean session_recv_msg_idle_cb        (gpointer        data);
48 static gboolean session_recv_data_idle_cb       (gpointer        data);
49
50 static gboolean session_read_msg_cb     (SockInfo       *source,
51                                          GIOCondition    condition,
52                                          gpointer        data);
53 static gboolean session_read_data_cb    (SockInfo       *source,
54                                          GIOCondition    condition,
55                                          gpointer        data);
56 static gboolean session_write_msg_cb    (SockInfo       *source,
57                                          GIOCondition    condition,
58                                          gpointer        data);
59 static gboolean session_write_data_cb   (SockInfo       *source,
60                                          GIOCondition    condition,
61                                          gpointer        data);
62
63
64 void session_init(Session *session, const void *prefs_account, gboolean is_smtp)
65 {
66         session->type = SESSION_UNKNOWN;
67         session->sock = NULL;
68         session->server = NULL;
69         session->port = 0;
70 #ifdef USE_GNUTLS
71         session->ssl_type = SSL_NONE;
72 #endif
73         session->nonblocking = TRUE;
74         session->state = SESSION_READY;
75         session->last_access_time = time(NULL);
76
77         g_get_current_time(&session->tv_prev);
78
79         session->conn_id = 0;
80
81         session->io_tag = 0;
82
83         session->read_buf_p = session->read_buf;
84         session->read_buf_len = 0;
85
86         session->read_msg_buf = g_string_sized_new(1024);
87         session->read_data_buf = g_byte_array_new();
88
89         session->write_buf = NULL;
90         session->write_buf_p = NULL;
91         session->write_buf_len = 0;
92
93         session->write_data = NULL;
94         session->write_data_p = NULL;
95         session->write_data_len = 0;
96
97         session->timeout_tag = 0;
98         session->timeout_interval = 0;
99
100         session->data = NULL;
101         session->account = prefs_account;
102         session->is_smtp = is_smtp;
103
104         session->ping_tag = -1;
105 }
106
107 /*!
108  *\brief        Set up parent and child process
109  *              Childloop: Read commands from parent,
110  *              send to server, get answer, pass to parent
111  *
112  *\param        session Contains session information
113  *              server to connect to
114  *              port to connect to
115  *
116  *\return        0 : success
117  *              -1 : pipe / fork errors (parent)
118  *               1 : connection error (child)
119  */
120 gint session_connect(Session *session, const gchar *server, gushort port)
121 {
122 #ifdef G_OS_UNIX
123         session->server = g_strdup(server);
124         session->port = port;
125
126         session->conn_id = sock_connect_async(server, port, session_connect_cb,
127                                               session);
128         if (session->conn_id < 0) {
129                 g_warning("can't connect to server.");
130                 session_close(session);
131                 return -1;
132         }
133
134         return 0;
135 #else
136         SockInfo *sock;
137
138         session->server = g_strdup(server);
139         session->port = port;
140
141         sock = sock_connect(server, port);
142         if (sock == NULL) {
143                 g_warning("can't connect to server.");
144                 session_close(session);
145                 return -1;
146         }
147         sock->is_smtp = session->is_smtp;
148
149         return session_connect_cb(sock, session);
150 #endif
151 }
152
153 static gint session_connect_cb(SockInfo *sock, gpointer data)
154 {
155         Session *session = SESSION(data);
156
157         session->conn_id = 0;
158
159         if (!sock) {
160                 g_warning("can't connect to server.");
161                 session->state = SESSION_ERROR;
162                 return -1;
163         }
164
165         session->sock = sock;
166         sock->account = session->account;
167         sock->is_smtp = session->is_smtp;
168         sock->ssl_cert_auto_accept = session->ssl_cert_auto_accept;
169
170 #ifdef USE_GNUTLS
171         sock->gnutls_priority = session->gnutls_priority;
172
173         if (session->ssl_type == SSL_TUNNEL) {
174                 sock_set_nonblocking_mode(sock, FALSE);
175                 if (!ssl_init_socket(sock)) {
176                         g_warning("can't initialize SSL.");
177                         log_error(LOG_PROTOCOL, _("SSL handshake failed\n"));
178                         session->state = SESSION_ERROR;
179                         return -1;
180                 }
181         }
182 #endif
183
184         /* we could have gotten a timeout while waiting for user input in 
185          * an SSL certificate dialog */
186         if (session->state == SESSION_TIMEOUT)
187                 return -1;
188
189         sock_set_nonblocking_mode(sock, session->nonblocking);
190
191         debug_print("session (%p): connected\n", session);
192
193         session->state = SESSION_RECV;
194         session->io_tag = sock_add_watch(session->sock, G_IO_IN,
195                                          session_read_msg_cb,
196                                          session);
197
198         return 0;
199 }
200
201 /*!
202  *\brief        child and parent: send DISCONNECT message to other process
203  *
204  *\param        session Contains session information
205  *
206  *\return        0 : success
207  */
208 gint session_disconnect(Session *session)
209 {
210         session_close(session);
211         return 0;
212 }
213
214 /*!
215  *\brief        parent ?
216  *
217  *\param        session Contains session information
218  */
219 void session_destroy(Session *session)
220 {
221         cm_return_if_fail(session != NULL);
222         cm_return_if_fail(session->destroy != NULL);
223
224         session_register_ping(session, NULL);
225
226         session_close(session);
227         session->destroy(session);
228         g_free(session->server);
229         g_string_free(session->read_msg_buf, TRUE);
230         g_byte_array_free(session->read_data_buf, TRUE);
231         g_free(session->read_data_terminator);
232         g_free(session->write_buf);
233 #ifdef USE_GNUTLS
234         g_free(session->gnutls_priority);
235 #endif
236
237         debug_print("session (%p): destroyed\n", session);
238
239         g_free(session);
240 }
241
242 gboolean session_is_running(Session *session)
243 {
244         return (session->state == SESSION_READY ||
245                 session->state == SESSION_SEND ||
246                 session->state == SESSION_RECV);
247 }
248
249 gboolean session_is_connected(Session *session)
250 {
251         return (session->state == SESSION_SEND ||
252                 session->state == SESSION_RECV);
253 }
254
255 void session_set_access_time(Session *session)
256 {
257         session->last_access_time = time(NULL);
258 }
259
260 void session_set_timeout(Session *session, guint interval)
261 {
262         if (session->timeout_tag > 0)
263                 g_source_remove(session->timeout_tag);
264
265         session->timeout_interval = interval;
266         if (interval > 0) {
267                 if (interval % 1000 == 0)
268                         session->timeout_tag =
269                                 g_timeout_add_seconds(interval/1000, session_timeout_cb, session);
270                 else
271                         session->timeout_tag =
272                                 g_timeout_add(interval, session_timeout_cb, session);
273         } else
274                 session->timeout_tag = 0;
275 }
276
277 static gboolean session_timeout_cb(gpointer data)
278 {
279         Session *session = SESSION(data);
280
281         g_warning("session timeout.\n");
282
283         if (session->io_tag > 0) {
284                 g_source_remove(session->io_tag);
285                 session->io_tag = 0;
286         }
287
288         session->timeout_tag = 0;
289         session->state = SESSION_TIMEOUT;
290
291         return FALSE;
292 }
293
294 void session_set_recv_message_notify(Session *session,
295                                      RecvMsgNotify notify_func, gpointer data)
296 {
297         session->recv_msg_notify = notify_func;
298         session->recv_msg_notify_data = data;
299 }
300
301 void session_set_recv_data_progressive_notify
302                                         (Session *session,
303                                          RecvDataProgressiveNotify notify_func,
304                                          gpointer data)
305 {
306         session->recv_data_progressive_notify = notify_func,
307         session->recv_data_progressive_notify_data = data;
308 }
309
310 void session_set_recv_data_notify(Session *session, RecvDataNotify notify_func,
311                                   gpointer data)
312 {
313         session->recv_data_notify = notify_func;
314         session->recv_data_notify_data = data;
315 }
316
317 void session_set_send_data_progressive_notify
318                                         (Session *session,
319                                          SendDataProgressiveNotify notify_func,
320                                          gpointer data)
321 {
322         session->send_data_progressive_notify = notify_func;
323         session->send_data_progressive_notify_data = data;
324 }
325
326 void session_set_send_data_notify(Session *session, SendDataNotify notify_func,
327                                   gpointer data)
328 {
329         session->send_data_notify = notify_func;
330         session->send_data_notify_data = data;
331 }
332
333 /*!
334  *\brief        child and parent cleanup (child closes first)
335  *
336  *\param        session Contains session information
337  *
338  *\return        0 : success
339  */
340 static gint session_close(Session *session)
341 {
342         cm_return_val_if_fail(session != NULL, -1);
343
344 #ifdef G_OS_UNIX
345         if (session->conn_id > 0) {
346                 sock_connect_async_cancel(session->conn_id);
347                 session->conn_id = 0;
348                 debug_print("session (%p): connection cancelled\n", session);
349         }
350 #endif
351
352         session_set_timeout(session, 0);
353
354         if (session->io_tag > 0) {
355                 g_source_remove(session->io_tag);
356                 session->io_tag = 0;
357         }
358
359         if (session->sock) {
360                 sock_close(session->sock);
361                 session->sock = NULL;
362                 session->state = SESSION_DISCONNECTED;
363                 debug_print("session (%p): closed\n", session);
364         }
365
366         return 0;
367 }
368
369 #ifdef USE_GNUTLS
370 gint session_start_tls(Session *session)
371 {
372         gboolean nb_mode;
373
374         nb_mode = sock_is_nonblocking_mode(session->sock);
375
376         session->sock->ssl_cert_auto_accept = session->ssl_cert_auto_accept;
377         session->sock->gnutls_priority = session->gnutls_priority;
378
379         if (nb_mode)
380                 sock_set_nonblocking_mode(session->sock, FALSE);
381
382         if (!ssl_init_socket(session->sock)) {
383                 g_warning("couldn't start TLS session.\n");
384                 if (nb_mode)
385                         sock_set_nonblocking_mode(session->sock, session->nonblocking);
386                 return -1;
387         }
388
389         if (nb_mode)
390                 sock_set_nonblocking_mode(session->sock, session->nonblocking);
391
392         return 0;
393 }
394 #endif
395
396 gint session_send_msg(Session *session, SessionMsgType type, const gchar *msg)
397 {
398         gboolean ret;
399
400         cm_return_val_if_fail(session->write_buf == NULL, -1);
401         cm_return_val_if_fail(msg != NULL, -1);
402         cm_return_val_if_fail(msg[0] != '\0', -1);
403
404         session->state = SESSION_SEND;
405         session->write_buf = g_strconcat(msg, "\r\n", NULL);
406         session->write_buf_p = session->write_buf;
407         session->write_buf_len = strlen(msg) + 2;
408
409         ret = session_write_msg_cb(session->sock, G_IO_OUT, session);
410
411         if (ret == TRUE)
412                 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
413                                                  session_write_msg_cb, session);
414         else if (session->state == SESSION_ERROR)
415                 return -1;
416
417         return 0;
418 }
419
420 gint session_recv_msg(Session *session)
421 {
422         cm_return_val_if_fail(session->read_msg_buf->len == 0, -1);
423
424         session->state = SESSION_RECV;
425
426         if (session->read_buf_len > 0)
427                 g_idle_add(session_recv_msg_idle_cb, session);
428         else
429                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
430                                                  session_read_msg_cb, session);
431
432         return 0;
433 }
434
435 static gboolean session_recv_msg_idle_cb(gpointer data)
436 {
437         Session *session = SESSION(data);
438         gboolean ret;
439
440         ret = session_read_msg_cb(session->sock, G_IO_IN, session);
441
442         if (ret == TRUE)
443                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
444                                                  session_read_msg_cb, session);
445
446         return FALSE;
447 }
448
449 /*!
450  *\brief        parent (child?): send data to other process
451  *
452  *\param        session Contains session information
453  *              data Data to send
454  *              size Bytes to send
455  *
456  *\return        0 : success
457  *              -1 : error
458  */
459 gint session_send_data(Session *session, const guchar *data, guint size)
460 {
461         gboolean ret;
462
463         cm_return_val_if_fail(session->write_data == NULL, -1);
464         cm_return_val_if_fail(data != NULL, -1);
465         cm_return_val_if_fail(size != 0, -1);
466
467         session->state = SESSION_SEND;
468
469         session->write_data = data;
470         session->write_data_p = session->write_data;
471         session->write_data_len = size;
472         g_get_current_time(&session->tv_prev);
473
474         ret = session_write_data_cb(session->sock, G_IO_OUT, session);
475
476         if (ret == TRUE)
477                 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
478                                                  session_write_data_cb,
479                                                  session);
480         else if (session->state == SESSION_ERROR)
481                 return -1;
482
483         return 0;
484 }
485
486 gint session_recv_data(Session *session, guint size, const gchar *terminator)
487 {
488         cm_return_val_if_fail(session->read_data_buf->len == 0, -1);
489
490         session->state = SESSION_RECV;
491
492         g_free(session->read_data_terminator);
493         session->read_data_terminator = g_strdup(terminator);
494         g_get_current_time(&session->tv_prev);
495
496         if (session->read_buf_len > 0)
497                 g_idle_add(session_recv_data_idle_cb, session);
498         else
499                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
500                                                  session_read_data_cb, session);
501
502         return 0;
503 }
504
505 static gboolean session_recv_data_idle_cb(gpointer data)
506 {
507         Session *session = SESSION(data);
508         gboolean ret;
509
510         ret = session_read_data_cb(session->sock, G_IO_IN, session);
511
512         if (ret == TRUE)
513                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
514                                                  session_read_data_cb, session);
515
516         return FALSE;
517 }
518
519 static gboolean session_read_msg_cb(SockInfo *source, GIOCondition condition,
520                                     gpointer data)
521 {
522         Session *session = SESSION(data);
523         gchar buf[SESSION_BUFFSIZE];
524         gint line_len;
525         gchar *newline;
526         gchar *msg;
527         gint ret;
528
529         cm_return_val_if_fail(condition == G_IO_IN, FALSE);
530
531         session_set_timeout(session, session->timeout_interval);
532
533         if (session->read_buf_len == 0) {
534                 gint read_len = -1;
535
536                 if (session->sock)
537                         read_len = sock_read(session->sock, session->read_buf,
538                                      SESSION_BUFFSIZE - 1);
539
540                 if (read_len == -1 && session->state == SESSION_DISCONNECTED) {
541                         g_warning ("sock_read: session disconnected\n");
542                         if (session->io_tag > 0) {
543                                 g_source_remove(session->io_tag);
544                                 session->io_tag = 0;
545                         }
546                         return FALSE;
547                 }
548                 
549                 if (read_len == 0) {
550                         g_warning("sock_read: received EOF\n");
551                         session->state = SESSION_EOF;
552                         return FALSE;
553                 }
554
555                 if (read_len < 0) {
556                         switch (errno) {
557                         case EAGAIN:
558                                 return TRUE;
559                         default:
560                                 g_warning("sock_read: %s\n", g_strerror(errno));
561                                 session->state = SESSION_ERROR;
562                                 return FALSE;
563                         }
564                 }
565
566                 session->read_buf_len = read_len;
567         }
568
569         if ((newline = memchr(session->read_buf_p, '\n', session->read_buf_len))
570                 != NULL)
571                 line_len = newline - session->read_buf_p + 1;
572         else
573                 line_len = session->read_buf_len;
574
575         if (line_len == 0)
576                 return TRUE;
577
578         memcpy(buf, session->read_buf_p, line_len);
579         buf[line_len] = '\0';
580
581         g_string_append(session->read_msg_buf, buf);
582
583         session->read_buf_len -= line_len;
584         if (session->read_buf_len == 0)
585                 session->read_buf_p = session->read_buf;
586         else
587                 session->read_buf_p += line_len;
588
589         /* incomplete read */
590         if (buf[line_len - 1] != '\n')
591                 return TRUE;
592
593         /* complete */
594         if (session->io_tag > 0) {
595                 g_source_remove(session->io_tag);
596                 session->io_tag = 0;
597         }
598
599         /* callback */
600         msg = g_strdup(session->read_msg_buf->str);
601         strretchomp(msg);
602         g_string_truncate(session->read_msg_buf, 0);
603
604         ret = session->recv_msg(session, msg);
605         session->recv_msg_notify(session, msg, session->recv_msg_notify_data);
606
607         g_free(msg);
608
609         if (ret < 0)
610                 session->state = SESSION_ERROR;
611
612         return FALSE;
613 }
614
615 static gboolean session_read_data_cb(SockInfo *source, GIOCondition condition,
616                                      gpointer data)
617 {
618         Session *session = SESSION(data);
619         GByteArray *data_buf;
620         gint terminator_len;
621         gboolean complete = FALSE;
622         guint data_len;
623         gint ret;
624
625         cm_return_val_if_fail(condition == G_IO_IN, FALSE);
626
627         session_set_timeout(session, session->timeout_interval);
628
629         if (session->read_buf_len == 0) {
630                 gint read_len;
631
632                 read_len = sock_read(session->sock, session->read_buf,
633                                      SESSION_BUFFSIZE);
634
635                 if (read_len == 0) {
636                         g_warning("sock_read: received EOF\n");
637                         session->state = SESSION_EOF;
638                         return FALSE;
639                 }
640
641                 if (read_len < 0) {
642                         switch (errno) {
643                         case EAGAIN:
644                                 return TRUE;
645                         default:
646                                 g_warning("sock_read: %s\n", g_strerror(errno));
647                                 session->state = SESSION_ERROR;
648                                 return FALSE;
649                         }
650                 }
651
652                 session->read_buf_len = read_len;
653         }
654
655         data_buf = session->read_data_buf;
656         terminator_len = strlen(session->read_data_terminator);
657
658         if (session->read_buf_len == 0)
659                 return TRUE;
660
661         g_byte_array_append(data_buf, session->read_buf_p,
662                             session->read_buf_len);
663
664         session->read_buf_len = 0;
665         session->read_buf_p = session->read_buf;
666
667         /* check if data is terminated */
668         if (data_buf->len >= terminator_len) {
669                 if (memcmp(data_buf->data, session->read_data_terminator,
670                            terminator_len) == 0)
671                         complete = TRUE;
672                 else if (data_buf->len >= terminator_len + 2 &&
673                          memcmp(data_buf->data + data_buf->len -
674                                 (terminator_len + 2), "\r\n", 2) == 0 &&
675                          memcmp(data_buf->data + data_buf->len -
676                                 terminator_len, session->read_data_terminator,
677                                 terminator_len) == 0)
678                         complete = TRUE;
679         }
680
681         /* incomplete read */
682         if (!complete) {
683                 GTimeVal tv_cur;
684
685                 g_get_current_time(&tv_cur);
686                 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
687                     tv_cur.tv_usec - session->tv_prev.tv_usec >
688                     UI_REFRESH_INTERVAL) {
689                         session->recv_data_progressive_notify
690                                 (session, data_buf->len, 0,
691                                  session->recv_data_progressive_notify_data);
692                         g_get_current_time(&session->tv_prev);
693                 }
694                 return TRUE;
695         }
696
697         /* complete */
698         if (session->io_tag > 0) {
699                 g_source_remove(session->io_tag);
700                 session->io_tag = 0;
701         }
702
703         data_len = data_buf->len - terminator_len;
704
705         /* callback */
706         ret = session->recv_data_finished(session, (gchar *)data_buf->data,
707                                           data_len);
708
709         g_byte_array_set_size(data_buf, 0);
710
711         session->recv_data_notify(session, data_len,
712                                   session->recv_data_notify_data);
713
714         if (ret < 0)
715                 session->state = SESSION_ERROR;
716
717         return FALSE;
718 }
719
720 static gint session_write_buf(Session *session)
721 {
722         gint write_len;
723         gint to_write_len;
724
725         cm_return_val_if_fail(session->write_buf != NULL, -1);
726         cm_return_val_if_fail(session->write_buf_p != NULL, -1);
727         cm_return_val_if_fail(session->write_buf_len > 0, -1);
728
729         to_write_len = session->write_buf_len -
730                 (session->write_buf_p - session->write_buf);
731         to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
732
733         write_len = sock_write(session->sock, session->write_buf_p,
734                                to_write_len);
735
736         if (write_len < 0) {
737                 switch (errno) {
738                 case EAGAIN:
739                         write_len = 0;
740                         break;
741                 default:
742                         g_warning("sock_write: %s\n", g_strerror(errno));
743                         session->state = SESSION_ERROR;
744                         return -1;
745                 }
746         }
747
748         /* incomplete write */
749         if (session->write_buf_p - session->write_buf + write_len <
750             session->write_buf_len) {
751                 session->write_buf_p += write_len;
752                 return 1;
753         }
754
755         g_free(session->write_buf);
756         session->write_buf = NULL;
757         session->write_buf_p = NULL;
758         session->write_buf_len = 0;
759
760         return 0;
761 }
762
763 static gint session_write_data(Session *session)
764 {
765         gint write_len;
766         gint to_write_len;
767
768         cm_return_val_if_fail(session->write_data != NULL, -1);
769         cm_return_val_if_fail(session->write_data_p != NULL, -1);
770         cm_return_val_if_fail(session->write_data_len > 0, -1);
771
772         to_write_len = session->write_data_len -
773                 (session->write_data_p - session->write_data);
774         to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
775
776         write_len = sock_write(session->sock, session->write_data_p,
777                                to_write_len);
778
779         if (write_len < 0) {
780                 switch (errno) {
781                 case EAGAIN:
782                         write_len = 0;
783                         break;
784                 default:
785                         g_warning("sock_write: %s\n", g_strerror(errno));
786                         session->state = SESSION_ERROR;
787                         return -1;
788                 }
789         }
790
791         /* incomplete write */
792         if (session->write_data_p - session->write_data + write_len <
793             session->write_data_len) {
794                 session->write_data_p += write_len;
795                 return 1;
796         }
797
798         session->write_data = NULL;
799         session->write_data_p = NULL;
800         session->write_data_len = 0;
801
802         return 0;
803 }
804
805 static gboolean session_write_msg_cb(SockInfo *source, GIOCondition condition,
806                                      gpointer data)
807 {
808         Session *session = SESSION(data);
809         gint ret;
810
811         cm_return_val_if_fail(condition == G_IO_OUT, FALSE);
812         cm_return_val_if_fail(session->write_buf != NULL, FALSE);
813         cm_return_val_if_fail(session->write_buf_p != NULL, FALSE);
814         cm_return_val_if_fail(session->write_buf_len > 0, FALSE);
815
816         ret = session_write_buf(session);
817
818         if (ret < 0) {
819                 session->state = SESSION_ERROR;
820                 return FALSE;
821         } else if (ret > 0)
822                 return TRUE;
823
824         if (session->io_tag > 0) {
825                 g_source_remove(session->io_tag);
826                 session->io_tag = 0;
827         }
828
829         session_recv_msg(session);
830
831         return FALSE;
832 }
833
834 static gboolean session_write_data_cb(SockInfo *source,
835                                       GIOCondition condition, gpointer data)
836 {
837         Session *session = SESSION(data);
838         guint write_data_len;
839         gint ret;
840
841         cm_return_val_if_fail(condition == G_IO_OUT, FALSE);
842         cm_return_val_if_fail(session->write_data != NULL, FALSE);
843         cm_return_val_if_fail(session->write_data_p != NULL, FALSE);
844         cm_return_val_if_fail(session->write_data_len > 0, FALSE);
845
846         write_data_len = session->write_data_len;
847
848         ret = session_write_data(session);
849
850         if (ret < 0) {
851                 session->state = SESSION_ERROR;
852                 return FALSE;
853         } else if (ret > 0) {
854                 GTimeVal tv_cur;
855
856                 g_get_current_time(&tv_cur);
857                 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
858                     tv_cur.tv_usec - session->tv_prev.tv_usec >
859                     UI_REFRESH_INTERVAL) {
860                         session_set_timeout(session, session->timeout_interval);
861                         session->send_data_progressive_notify
862                                 (session,
863                                  session->write_data_p - session->write_data,
864                                  write_data_len,
865                                  session->send_data_progressive_notify_data);
866                         g_get_current_time(&session->tv_prev);
867                 }
868                 return TRUE;
869         }
870
871         if (session->io_tag > 0) {
872                 g_source_remove(session->io_tag);
873                 session->io_tag = 0;
874         }
875
876         /* callback */
877         ret = session->send_data_finished(session, write_data_len);
878         session->send_data_notify(session, write_data_len,
879                                   session->send_data_notify_data);
880
881         return FALSE;
882 }
883
884 void session_register_ping(Session *session, gboolean (*ping_cb)(gpointer data))
885 {
886         if (!session)
887                 return;
888         if (session->ping_tag > -1)
889                 g_source_remove(session->ping_tag);
890
891         session->ping_tag = -1;
892
893         if (ping_cb != NULL)
894                 session->ping_tag = g_timeout_add_seconds(60, ping_cb, session);
895 }