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