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