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