bug sync with sylpheed
[claws.git] / src / recv.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "intl.h"
30 #include "recv.h"
31 #include "socket.h"
32 #include "utils.h"
33
34 #define BUFFSIZE        8192
35
36 static RecvUIFunc       recv_ui_func;
37 static gpointer         recv_ui_func_data;
38
39 gint recv_write_to_file(SockInfo *sock, const gchar *filename)
40 {
41         FILE *fp;
42
43         g_return_val_if_fail(filename != NULL, -1);
44
45         if ((fp = fopen(filename, "w")) == NULL) {
46                 FILE_OP_ERROR(filename, "fopen");
47                 recv_write(sock, NULL);
48                 return -1;
49         }
50
51         if (change_file_mode_rw(fp, filename) < 0)
52                 FILE_OP_ERROR(filename, "chmod");
53
54         if (recv_write(sock, fp) < 0) {
55                 fclose(fp);
56                 unlink(filename);
57                 return -1;
58         }
59
60         if (fclose(fp) == EOF) {
61                 FILE_OP_ERROR(filename, "fclose");
62                 unlink(filename);
63                 return -1;
64         }
65
66         return 0;
67 }
68
69 gint recv_bytes_write_to_file(SockInfo *sock, glong size, const gchar *filename)
70 {
71         FILE *fp;
72
73         g_return_val_if_fail(filename != NULL, -1);
74
75         if ((fp = fopen(filename, "w")) == NULL) {
76                 FILE_OP_ERROR(filename, "fopen");
77                 recv_write(sock, NULL);
78                 return -1;
79         }
80
81         if (change_file_mode_rw(fp, filename) < 0)
82                 FILE_OP_ERROR(filename, "chmod");
83
84         if (recv_bytes_write(sock, size, fp) < 0) {
85                 fclose(fp);
86                 unlink(filename);
87                 return -1;
88         }
89
90         if (fclose(fp) == EOF) {
91                 FILE_OP_ERROR(filename, "fclose");
92                 unlink(filename);
93                 return -1;
94         }
95
96         return 0;
97 }
98
99 gint recv_write(SockInfo *sock, FILE *fp)
100 {
101         gchar buf[BUFFSIZE];
102         gint len;
103         gboolean nb;
104
105         nb = sock_is_nonblocking_mode(sock);
106         if (nb) sock_set_nonblocking_mode(sock, FALSE);
107
108         for (;;) {
109                 if (sock_read(sock, buf, sizeof(buf)) < 0) {
110                         g_warning(_("error occurred while retrieving data.\n"));
111                         if (nb) sock_set_nonblocking_mode(sock, TRUE);
112                         return -1;
113                 }
114
115                 len = strlen(buf);
116                 if (len > 1 && buf[0] == '.' && buf[1] == '\r') break;
117
118                 if (recv_ui_func)
119                         recv_ui_func(sock, len, recv_ui_func_data);
120
121                 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
122                         buf[len - 2] = '\n';
123                         buf[len - 1] = '\0';
124                         len--;
125                 }
126
127                 if (buf[0] == '.' && buf[1] == '.')
128                         memmove(buf, buf + 1, len--);
129
130                 if (!strncmp(buf, ">From ", 6))
131                         memmove(buf, buf + 1, len--);
132
133                 if (fp && fputs(buf, fp) == EOF) {
134                         perror("fputs");
135                         g_warning(_("Can't write to file.\n"));
136                         fp = NULL;
137                 }
138         }
139
140         if (nb) sock_set_nonblocking_mode(sock, TRUE);
141
142         if (!fp) return -1;
143
144         return 0;
145 }
146
147 gint recv_bytes_write(SockInfo *sock, glong size, FILE *fp)
148 {
149         gchar *buf;
150         gboolean nb;
151         glong count = 0;
152         gchar *prev, *cur;
153
154         nb = sock_is_nonblocking_mode(sock);
155         if (nb) sock_set_nonblocking_mode(sock, FALSE);
156
157         Xalloca(buf, size, return -1);
158
159         do {
160                 size_t read_count;
161
162                 /* FIXME: put this into socket.c :WK: */
163                 read_count = fd_read(sock->sock, buf + count, size - count);
164                 if (read_count < 0) {
165                         if (nb) sock_set_nonblocking_mode(sock, TRUE);
166                         return -1;
167                 }
168                 count += read_count;
169         } while (count < size);
170
171         prev = buf;
172         while ((cur = memchr(prev, '\r', size)) != NULL) {
173                 if (cur - buf + 1 < size && *(cur + 1) == '\n') {
174                         if (fwrite(prev, sizeof(gchar), cur - prev, fp) == EOF ||
175                             fwrite("\n", sizeof(gchar), 1, fp) == EOF) {
176                                 perror("fwrite");
177                                 g_warning(_("Can't write to file.\n"));
178                                 if (nb) sock_set_nonblocking_mode(sock, TRUE);
179                                 return -1;
180                         }
181                         prev = cur + 2;
182                         if (prev - buf >= size) break;
183                 }
184         }
185
186         if (prev - buf < size && fwrite(buf, sizeof(gchar),
187                                         size - (prev - buf), fp) == EOF) {
188                 perror("fwrite");
189                 g_warning(_("Can't write to file.\n"));
190                 if (nb) sock_set_nonblocking_mode(sock, TRUE);
191                 return -1;
192         }
193
194         if (nb) sock_set_nonblocking_mode(sock, TRUE);
195         return 0;
196 }
197
198 void recv_set_ui_func(RecvUIFunc func, gpointer data)
199 {
200         recv_ui_func = func;
201         recv_ui_func_data = data;
202 }