now min version of glib is 2.20, remove all irrelevent conditionals
[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
378         if (nb_mode)
379                 sock_set_nonblocking_mode(session->sock, FALSE);
380
381         if (!ssl_init_socket_with_method(session->sock, SSL_METHOD_TLSv1)) {
382                 g_warning("couldn't start TLS session.\n");
383                 if (nb_mode)
384                         sock_set_nonblocking_mode(session->sock, session->nonblocking);
385                 return -1;
386         }
387
388         if (nb_mode)
389                 sock_set_nonblocking_mode(session->sock, session->nonblocking);
390
391         return 0;
392 }
393 #endif
394
395 gint session_send_msg(Session *session, SessionMsgType type, const gchar *msg)
396 {
397         gboolean ret;
398
399         cm_return_val_if_fail(session->write_buf == NULL, -1);
400         cm_return_val_if_fail(msg != NULL, -1);
401         cm_return_val_if_fail(msg[0] != '\0', -1);
402
403         session->state = SESSION_SEND;
404         session->write_buf = g_strconcat(msg, "\r\n", NULL);
405         session->write_buf_p = session->write_buf;
406         session->write_buf_len = strlen(msg) + 2;
407
408         ret = session_write_msg_cb(session->sock, G_IO_OUT, session);
409
410         if (ret == TRUE)
411                 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
412                                                  session_write_msg_cb, session);
413         else if (session->state == SESSION_ERROR)
414                 return -1;
415
416         return 0;
417 }
418
419 gint session_recv_msg(Session *session)
420 {
421         cm_return_val_if_fail(session->read_msg_buf->len == 0, -1);
422
423         session->state = SESSION_RECV;
424
425         if (session->read_buf_len > 0)
426                 g_idle_add(session_recv_msg_idle_cb, session);
427         else
428                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
429                                                  session_read_msg_cb, session);
430
431         return 0;
432 }
433
434 static gboolean session_recv_msg_idle_cb(gpointer data)
435 {
436         Session *session = SESSION(data);
437         gboolean ret;
438
439         ret = session_read_msg_cb(session->sock, G_IO_IN, session);
440
441         if (ret == TRUE)
442                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
443                                                  session_read_msg_cb, session);
444
445         return FALSE;
446 }
447
448 /*!
449  *\brief        parent (child?): send data to other process
450  *
451  *\param        session Contains session information
452  *              data Data to send
453  *              size Bytes to send
454  *
455  *\return        0 : success
456  *              -1 : error
457  */
458 gint session_send_data(Session *session, const guchar *data, guint size)
459 {
460         gboolean ret;
461
462         cm_return_val_if_fail(session->write_data == NULL, -1);
463         cm_return_val_if_fail(data != NULL, -1);
464         cm_return_val_if_fail(size != 0, -1);
465
466         session->state = SESSION_SEND;
467
468         session->write_data = data;
469         session->write_data_p = session->write_data;
470         session->write_data_len = size;
471         g_get_current_time(&session->tv_prev);
472
473         ret = session_write_data_cb(session->sock, G_IO_OUT, session);
474
475         if (ret == TRUE)
476                 session->io_tag = sock_add_watch(session->sock, G_IO_OUT,
477                                                  session_write_data_cb,
478                                                  session);
479         else if (session->state == SESSION_ERROR)
480                 return -1;
481
482         return 0;
483 }
484
485 gint session_recv_data(Session *session, guint size, const gchar *terminator)
486 {
487         cm_return_val_if_fail(session->read_data_buf->len == 0, -1);
488
489         session->state = SESSION_RECV;
490
491         g_free(session->read_data_terminator);
492         session->read_data_terminator = g_strdup(terminator);
493         g_get_current_time(&session->tv_prev);
494
495         if (session->read_buf_len > 0)
496                 g_idle_add(session_recv_data_idle_cb, session);
497         else
498                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
499                                                  session_read_data_cb, session);
500
501         return 0;
502 }
503
504 static gboolean session_recv_data_idle_cb(gpointer data)
505 {
506         Session *session = SESSION(data);
507         gboolean ret;
508
509         ret = session_read_data_cb(session->sock, G_IO_IN, session);
510
511         if (ret == TRUE)
512                 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
513                                                  session_read_data_cb, session);
514
515         return FALSE;
516 }
517
518 static gboolean session_read_msg_cb(SockInfo *source, GIOCondition condition,
519                                     gpointer data)
520 {
521         Session *session = SESSION(data);
522         gchar buf[SESSION_BUFFSIZE];
523         gint line_len;
524         gchar *newline;
525         gchar *msg;
526         gint ret;
527
528         cm_return_val_if_fail(condition == G_IO_IN, FALSE);
529
530         session_set_timeout(session, session->timeout_interval);
531
532         if (session->read_buf_len == 0) {
533                 gint read_len = -1;
534
535                 if (session->sock)
536                         read_len = sock_read(session->sock, session->read_buf,
537                                      SESSION_BUFFSIZE - 1);
538
539                 if (read_len == -1 && session->state == SESSION_DISCONNECTED) {
540                         g_warning ("sock_read: session disconnected\n");
541                         if (session->io_tag > 0) {
542                                 g_source_remove(session->io_tag);
543                                 session->io_tag = 0;
544                         }
545                         return FALSE;
546                 }
547                 
548                 if (read_len == 0) {
549                         g_warning("sock_read: received EOF\n");
550                         session->state = SESSION_EOF;
551                         return FALSE;
552                 }
553
554                 if (read_len < 0) {
555                         switch (errno) {
556                         case EAGAIN:
557                                 return TRUE;
558                         default:
559                                 g_warning("sock_read: %s\n", g_strerror(errno));
560                                 session->state = SESSION_ERROR;
561                                 return FALSE;
562                         }
563                 }
564
565                 session->read_buf_len = read_len;
566         }
567
568         if ((newline = memchr(session->read_buf_p, '\n', session->read_buf_len))
569                 != NULL)
570                 line_len = newline - session->read_buf_p + 1;
571         else
572                 line_len = session->read_buf_len;
573
574         if (line_len == 0)
575                 return TRUE;
576
577         memcpy(buf, session->read_buf_p, line_len);
578         buf[line_len] = '\0';
579
580         g_string_append(session->read_msg_buf, buf);
581
582         session->read_buf_len -= line_len;
583         if (session->read_buf_len == 0)
584                 session->read_buf_p = session->read_buf;
585         else
586                 session->read_buf_p += line_len;
587
588         /* incomplete read */
589         if (buf[line_len - 1] != '\n')
590                 return TRUE;
591
592         /* complete */
593         if (session->io_tag > 0) {
594                 g_source_remove(session->io_tag);
595                 session->io_tag = 0;
596         }
597
598         /* callback */
599         msg = g_strdup(session->read_msg_buf->str);
600         strretchomp(msg);
601         g_string_truncate(session->read_msg_buf, 0);
602
603         ret = session->recv_msg(session, msg);
604         session->recv_msg_notify(session, msg, session->recv_msg_notify_data);
605
606         g_free(msg);
607
608         if (ret < 0)
609                 session->state = SESSION_ERROR;
610
611         return FALSE;
612 }
613
614 static gboolean session_read_data_cb(SockInfo *source, GIOCondition condition,
615                                      gpointer data)
616 {
617         Session *session = SESSION(data);
618         GByteArray *data_buf;
619         gint terminator_len;
620         gboolean complete = FALSE;
621         guint data_len;
622         gint ret;
623
624         cm_return_val_if_fail(condition == G_IO_IN, FALSE);
625
626         session_set_timeout(session, session->timeout_interval);
627
628         if (session->read_buf_len == 0) {
629                 gint read_len;
630
631                 read_len = sock_read(session->sock, session->read_buf,
632                                      SESSION_BUFFSIZE);
633
634                 if (read_len == 0) {
635                         g_warning("sock_read: received EOF\n");
636                         session->state = SESSION_EOF;
637                         return FALSE;
638                 }
639
640                 if (read_len < 0) {
641                         switch (errno) {
642                         case EAGAIN:
643                                 return TRUE;
644                         default:
645                                 g_warning("sock_read: %s\n", g_strerror(errno));
646                                 session->state = SESSION_ERROR;
647                                 return FALSE;
648                         }
649                 }
650
651                 session->read_buf_len = read_len;
652         }
653
654         data_buf = session->read_data_buf;
655         terminator_len = strlen(session->read_data_terminator);
656
657         if (session->read_buf_len == 0)
658                 return TRUE;
659
660         g_byte_array_append(data_buf, session->read_buf_p,
661                             session->read_buf_len);
662
663         session->read_buf_len = 0;
664         session->read_buf_p = session->read_buf;
665
666         /* check if data is terminated */
667         if (data_buf->len >= terminator_len) {
668                 if (memcmp(data_buf->data, session->read_data_terminator,
669                            terminator_len) == 0)
670                         complete = TRUE;
671                 else if (data_buf->len >= terminator_len + 2 &&
672                          memcmp(data_buf->data + data_buf->len -
673                                 (terminator_len + 2), "\r\n", 2) == 0 &&
674                          memcmp(data_buf->data + data_buf->len -
675                                 terminator_len, session->read_data_terminator,
676                                 terminator_len) == 0)
677                         complete = TRUE;
678         }
679
680         /* incomplete read */
681         if (!complete) {
682                 GTimeVal tv_cur;
683
684                 g_get_current_time(&tv_cur);
685                 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
686                     tv_cur.tv_usec - session->tv_prev.tv_usec >
687                     UI_REFRESH_INTERVAL) {
688                         session->recv_data_progressive_notify
689                                 (session, data_buf->len, 0,
690                                  session->recv_data_progressive_notify_data);
691                         g_get_current_time(&session->tv_prev);
692                 }
693                 return TRUE;
694         }
695
696         /* complete */
697         if (session->io_tag > 0) {
698                 g_source_remove(session->io_tag);
699                 session->io_tag = 0;
700         }
701
702         data_len = data_buf->len - terminator_len;
703
704         /* callback */
705         ret = session->recv_data_finished(session, (gchar *)data_buf->data,
706                                           data_len);
707
708         g_byte_array_set_size(data_buf, 0);
709
710         session->recv_data_notify(session, data_len,
711                                   session->recv_data_notify_data);
712
713         if (ret < 0)
714                 session->state = SESSION_ERROR;
715
716         return FALSE;
717 }
718
719 static gint session_write_buf(Session *session)
720 {
721         gint write_len;
722         gint to_write_len;
723
724         cm_return_val_if_fail(session->write_buf != NULL, -1);
725         cm_return_val_if_fail(session->write_buf_p != NULL, -1);
726         cm_return_val_if_fail(session->write_buf_len > 0, -1);
727
728         to_write_len = session->write_buf_len -
729                 (session->write_buf_p - session->write_buf);
730         to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
731
732         write_len = sock_write(session->sock, session->write_buf_p,
733                                to_write_len);
734
735         if (write_len < 0) {
736                 switch (errno) {
737                 case EAGAIN:
738                         write_len = 0;
739                         break;
740                 default:
741                         g_warning("sock_write: %s\n", g_strerror(errno));
742                         session->state = SESSION_ERROR;
743                         return -1;
744                 }
745         }
746
747         /* incomplete write */
748         if (session->write_buf_p - session->write_buf + write_len <
749             session->write_buf_len) {
750                 session->write_buf_p += write_len;
751                 return 1;
752         }
753
754         g_free(session->write_buf);
755         session->write_buf = NULL;
756         session->write_buf_p = NULL;
757         session->write_buf_len = 0;
758
759         return 0;
760 }
761
762 static gint session_write_data(Session *session)
763 {
764         gint write_len;
765         gint to_write_len;
766
767         cm_return_val_if_fail(session->write_data != NULL, -1);
768         cm_return_val_if_fail(session->write_data_p != NULL, -1);
769         cm_return_val_if_fail(session->write_data_len > 0, -1);
770
771         to_write_len = session->write_data_len -
772                 (session->write_data_p - session->write_data);
773         to_write_len = MIN(to_write_len, SESSION_BUFFSIZE);
774
775         write_len = sock_write(session->sock, session->write_data_p,
776                                to_write_len);
777
778         if (write_len < 0) {
779                 switch (errno) {
780                 case EAGAIN:
781                         write_len = 0;
782                         break;
783                 default:
784                         g_warning("sock_write: %s\n", g_strerror(errno));
785                         session->state = SESSION_ERROR;
786                         return -1;
787                 }
788         }
789
790         /* incomplete write */
791         if (session->write_data_p - session->write_data + write_len <
792             session->write_data_len) {
793                 session->write_data_p += write_len;
794                 return 1;
795         }
796
797         session->write_data = NULL;
798         session->write_data_p = NULL;
799         session->write_data_len = 0;
800
801         return 0;
802 }
803
804 static gboolean session_write_msg_cb(SockInfo *source, GIOCondition condition,
805                                      gpointer data)
806 {
807         Session *session = SESSION(data);
808         gint ret;
809
810         cm_return_val_if_fail(condition == G_IO_OUT, FALSE);
811         cm_return_val_if_fail(session->write_buf != NULL, FALSE);
812         cm_return_val_if_fail(session->write_buf_p != NULL, FALSE);
813         cm_return_val_if_fail(session->write_buf_len > 0, FALSE);
814
815         ret = session_write_buf(session);
816
817         if (ret < 0) {
818                 session->state = SESSION_ERROR;
819                 return FALSE;
820         } else if (ret > 0)
821                 return TRUE;
822
823         if (session->io_tag > 0) {
824                 g_source_remove(session->io_tag);
825                 session->io_tag = 0;
826         }
827
828         session_recv_msg(session);
829
830         return FALSE;
831 }
832
833 static gboolean session_write_data_cb(SockInfo *source,
834                                       GIOCondition condition, gpointer data)
835 {
836         Session *session = SESSION(data);
837         guint write_data_len;
838         gint ret;
839
840         cm_return_val_if_fail(condition == G_IO_OUT, FALSE);
841         cm_return_val_if_fail(session->write_data != NULL, FALSE);
842         cm_return_val_if_fail(session->write_data_p != NULL, FALSE);
843         cm_return_val_if_fail(session->write_data_len > 0, FALSE);
844
845         write_data_len = session->write_data_len;
846
847         ret = session_write_data(session);
848
849         if (ret < 0) {
850                 session->state = SESSION_ERROR;
851                 return FALSE;
852         } else if (ret > 0) {
853                 GTimeVal tv_cur;
854
855                 g_get_current_time(&tv_cur);
856                 if (tv_cur.tv_sec - session->tv_prev.tv_sec > 0 ||
857                     tv_cur.tv_usec - session->tv_prev.tv_usec >
858                     UI_REFRESH_INTERVAL) {
859                         session_set_timeout(session, session->timeout_interval);
860                         session->send_data_progressive_notify
861                                 (session,
862                                  session->write_data_p - session->write_data,
863                                  write_data_len,
864                                  session->send_data_progressive_notify_data);
865                         g_get_current_time(&session->tv_prev);
866                 }
867                 return TRUE;
868         }
869
870         if (session->io_tag > 0) {
871                 g_source_remove(session->io_tag);
872                 session->io_tag = 0;
873         }
874
875         /* callback */
876         ret = session->send_data_finished(session, write_data_len);
877         session->send_data_notify(session, write_data_len,
878                                   session->send_data_notify_data);
879
880         return FALSE;
881 }
882
883 void session_register_ping(Session *session, gboolean (*ping_cb)(gpointer data))
884 {
885         if (!session)
886                 return;
887         if (session->ping_tag > -1)
888                 g_source_remove(session->ping_tag);
889
890         session->ping_tag = -1;
891
892         if (ping_cb != NULL)
893         session->ping_tag = g_timeout_add_seconds(60, ping_cb, session);
894 }