5aec6ad996ee22debef049597c12bd81d68ca239
[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 gint recv_write_to_file(SockInfo *sock, const gchar *filename)
37 {
38         FILE *fp;
39
40         g_return_val_if_fail(filename != NULL, -1);
41
42         if ((fp = fopen(filename, "w")) == NULL) {
43                 FILE_OP_ERROR(filename, "fopen");
44                 recv_write(sock, NULL);
45                 return -1;
46         }
47
48         if (change_file_mode_rw(fp, filename) < 0)
49                 FILE_OP_ERROR(filename, "chmod");
50
51         if (recv_write(sock, fp) < 0) {
52                 fclose(fp);
53                 unlink(filename);
54                 return -1;
55         }
56
57         if (fclose(fp) == EOF) {
58                 FILE_OP_ERROR(filename, "fclose");
59                 unlink(filename);
60                 return -1;
61         }
62
63         return 0;
64 }
65
66 gint recv_bytes_write_to_file(SockInfo *sock, glong size, const gchar *filename)
67 {
68         FILE *fp;
69
70         g_return_val_if_fail(filename != NULL, -1);
71
72         if ((fp = fopen(filename, "w")) == NULL) {
73                 FILE_OP_ERROR(filename, "fopen");
74                 recv_write(sock, NULL);
75                 return -1;
76         }
77
78         if (change_file_mode_rw(fp, filename) < 0)
79                 FILE_OP_ERROR(filename, "chmod");
80
81         if (recv_bytes_write(sock, size, fp) < 0) {
82                 fclose(fp);
83                 unlink(filename);
84                 return -1;
85         }
86
87         if (fclose(fp) == EOF) {
88                 FILE_OP_ERROR(filename, "fclose");
89                 unlink(filename);
90                 return -1;
91         }
92
93         return 0;
94 }
95
96 gint recv_write(SockInfo *sock, FILE *fp)
97 {
98         gchar buf[BUFFSIZE];
99         gint len;
100         gboolean nb;
101
102         nb = sock_is_nonblocking_mode(sock);
103         if (nb) sock_set_nonblocking_mode(sock, FALSE);
104
105         for (;;) {
106                 if (sock_read(sock, buf, sizeof(buf)) < 0) {
107                         g_warning(_("error occurred while retrieving data.\n"));
108                         if (nb) sock_set_nonblocking_mode(sock, TRUE);
109                         return -1;
110                 }
111
112                 len = strlen(buf);
113                 if (len > 1 && buf[0] == '.' && buf[1] == '\r') break;
114
115                 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
116                         buf[len - 2] = '\n';
117                         buf[len - 1] = '\0';
118                 }
119
120                 if (buf[0] == '.' && buf[1] == '.')
121                         memmove(buf, buf + 1, strlen(buf));
122
123                 if (!strncmp(buf, ">From ", 6))
124                         memmove(buf, buf + 1, strlen(buf));
125
126                 if (fp && fputs(buf, fp) == EOF) {
127                         perror("fputs");
128                         g_warning(_("Can't write to file.\n"));
129                         fp = NULL;
130                 }
131         }
132
133         if (nb) sock_set_nonblocking_mode(sock, TRUE);
134
135         if (!fp) return -1;
136
137         return 0;
138 }
139
140 gint recv_bytes_write(SockInfo *sock, glong size, FILE *fp)
141 {
142         gchar *buf;
143         gboolean nb;
144         glong count = 0;
145         gchar *prev, *cur;
146
147         nb = sock_is_nonblocking_mode(sock);
148         if (nb) sock_set_nonblocking_mode(sock, FALSE);
149
150         Xalloca(buf, size, return -1);
151
152         do {
153                 size_t read_count;
154
155                 /* FIXME: put this into socket.c :WK: */
156                 read_count = fd_read(sock->sock, buf + count, size - count);
157                 if (read_count < 0) {
158                         if (nb) sock_set_nonblocking_mode(sock, TRUE);
159                         return -1;
160                 }
161                 count += read_count;
162         } while (count < size);
163
164         prev = buf;
165         while ((cur = memchr(prev, '\r', size)) != NULL) {
166                 if (cur - buf + 1 < size && *(cur + 1) == '\n') {
167                         if (fwrite(prev, sizeof(gchar), cur - prev, fp) == EOF ||
168                             fwrite("\n", sizeof(gchar), 1, fp) == EOF) {
169                                 perror("fwrite");
170                                 g_warning(_("Can't write to file.\n"));
171                                 if (nb) sock_set_nonblocking_mode(sock, TRUE);
172                                 return -1;
173                         }
174                         prev = cur + 2;
175                         if (prev - buf >= size) break;
176                 }
177         }
178
179         if (prev - buf < size && fwrite(buf, sizeof(gchar),
180                                         size - (prev - buf), fp) == EOF) {
181                 perror("fwrite");
182                 g_warning(_("Can't write to file.\n"));
183                 if (nb) sock_set_nonblocking_mode(sock, TRUE);
184                 return -1;
185         }
186
187         if (nb) sock_set_nonblocking_mode(sock, TRUE);
188         return 0;
189 }