2007-08-23 [wwp] 2.10.0cvs158
[claws.git] / src / recv.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 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #ifdef G_OS_WIN32
33 #include "w32lib.h"
34 #else
35 #include <sys/time.h>
36 #endif
37
38 #include "recv.h"
39 #include "socket.h"
40 #include "utils.h"
41
42 static RecvUIFunc       recv_ui_func;
43 static gpointer         recv_ui_func_data;
44
45 gint recv_write_to_file(SockInfo *sock, const gchar *filename)
46 {
47         FILE *fp;
48         gint ret;
49
50         g_return_val_if_fail(filename != NULL, -1);
51
52         if ((fp = g_fopen(filename, "wb")) == NULL) {
53                 FILE_OP_ERROR(filename, "fopen");
54                 recv_write(sock, NULL);
55                 return -1;
56         }
57
58         if (change_file_mode_rw(fp, filename) < 0)
59                 FILE_OP_ERROR(filename, "chmod");
60
61         if ((ret = recv_write(sock, fp)) < 0) {
62                 fclose(fp);
63                 g_unlink(filename);
64                 return ret;
65         }
66
67         if (fclose(fp) == EOF) {
68                 FILE_OP_ERROR(filename, "fclose");
69                 g_unlink(filename);
70                 return -1;
71         }
72
73         return 0;
74 }
75
76 gint recv_bytes_write_to_file(SockInfo *sock, glong size, const gchar *filename)
77 {
78         FILE *fp;
79         gint ret;
80
81         g_return_val_if_fail(filename != NULL, -1);
82
83         if ((fp = g_fopen(filename, "wb")) == NULL) {
84                 FILE_OP_ERROR(filename, "fopen");
85                 recv_write(sock, NULL);
86                 return -1;
87         }
88
89         if (change_file_mode_rw(fp, filename) < 0)
90                 FILE_OP_ERROR(filename, "chmod");
91
92         if ((ret = recv_bytes_write(sock, size, fp)) < 0) {
93                 fclose(fp);
94                 g_unlink(filename);
95                 return ret;
96         }
97
98         if (fclose(fp) == EOF) {
99                 FILE_OP_ERROR(filename, "fclose");
100                 g_unlink(filename);
101                 return -1;
102         }
103
104         return 0;
105 }
106
107 gint recv_write(SockInfo *sock, FILE *fp)
108 {
109         gchar buf[BUFFSIZE];
110         gint len;
111         gint count = 0;
112         gint bytes = 0;
113         struct timeval tv_prev, tv_cur;
114
115         gettimeofday(&tv_prev, NULL);
116
117         for (;;) {
118                 if (sock_gets(sock, buf, sizeof(buf)) < 0) {
119                         g_warning("error occurred while retrieving data.\n");
120                         return -2;
121                 }
122
123                 len = strlen(buf);
124                 if (len > 1 && buf[0] == '.' && buf[1] == '\r') {
125                         if (recv_ui_func)
126                                 recv_ui_func(sock, count, bytes,
127                                              recv_ui_func_data);
128                         break;
129                 }
130                 count++;
131                 bytes += len;
132
133                 if (recv_ui_func) {
134                         gettimeofday(&tv_cur, NULL);
135                         /* if elapsed time from previous update is greater
136                            than 50msec, update UI */
137                         if (tv_cur.tv_sec - tv_prev.tv_sec > 0 ||
138                             tv_cur.tv_usec - tv_prev.tv_usec > UI_REFRESH_INTERVAL) {
139                                 gboolean ret;
140                                 ret = recv_ui_func(sock, count, bytes,
141                                                    recv_ui_func_data);
142                                 if (ret == FALSE) return -1;
143                                 gettimeofday(&tv_prev, NULL);
144                         }
145                 }
146
147                 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
148                         buf[len - 2] = '\n';
149                         buf[len - 1] = '\0';
150                         len--;
151                 }
152
153                 if (buf[0] == '.' && buf[1] == '.')
154                         memmove(buf, buf + 1, len--);
155
156                 if (!strncmp(buf, ">From ", 6))
157                         memmove(buf, buf + 1, len--);
158
159                 if (fp && fputs(buf, fp) == EOF) {
160                         perror("fputs");
161                         g_warning("Can't write to file.\n");
162                         fp = NULL;
163                 }
164         }
165
166         if (!fp) return -1;
167
168         return 0;
169 }
170
171 gint recv_bytes_write(SockInfo *sock, glong size, FILE *fp)
172 {
173         gchar *buf;
174         glong count = 0;
175         gchar *prev, *cur;
176
177         if (size == 0)
178                 return 0;
179
180         buf = g_malloc(size);
181
182         do {
183                 gint read_count;
184
185                 read_count = sock_read(sock, buf + count, size - count);
186                 if (read_count < 0) {
187                         g_free(buf);
188                         return -2;
189                 }
190                 count += read_count;
191         } while (count < size);
192
193         /* +------------------+----------------+--------------------------+ *
194          * ^buf               ^prev            ^cur             buf+size-1^ */
195
196         prev = buf;
197         while ((cur = memchr(prev, '\r', size - (prev - buf))) != NULL) {
198                 if (cur == buf + size - 1) break;
199
200                 if (fwrite(prev, sizeof(gchar), cur - prev, fp) == EOF ||
201                     fwrite("\n", sizeof(gchar), 1, fp) == EOF) {
202                         perror("fwrite");
203                         g_warning("Can't write to file.\n");
204                         g_free(buf);
205                         return -1;
206                 }
207
208                 if (*(cur + 1) == '\n')
209                         prev = cur + 2;
210                 else
211                         prev = cur + 1;
212
213                 if (prev - buf >= size) break;
214         }
215
216         if (prev - buf < size && fwrite(buf, sizeof(gchar),
217                                         size - (prev - buf), fp) == EOF) {
218                 perror("fwrite");
219                 g_warning("Can't write to file.\n");
220                 g_free(buf);
221                 return -1;
222         }
223
224         g_free(buf);
225         return 0;
226 }
227
228 void recv_set_ui_func(RecvUIFunc func, gpointer data)
229 {
230         recv_ui_func = func;
231         recv_ui_func_data = data;
232 }