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