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