2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 Hiroyuki Yamamoto
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.
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.
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.
36 static RecvUIFunc recv_ui_func;
37 static gpointer recv_ui_func_data;
39 gint recv_write_to_file(SockInfo *sock, const gchar *filename)
43 g_return_val_if_fail(filename != NULL, -1);
45 if ((fp = fopen(filename, "w")) == NULL) {
46 FILE_OP_ERROR(filename, "fopen");
47 recv_write(sock, NULL);
51 if (change_file_mode_rw(fp, filename) < 0)
52 FILE_OP_ERROR(filename, "chmod");
54 if (recv_write(sock, fp) < 0) {
60 if (fclose(fp) == EOF) {
61 FILE_OP_ERROR(filename, "fclose");
69 gint recv_bytes_write_to_file(SockInfo *sock, glong size, const gchar *filename)
73 g_return_val_if_fail(filename != NULL, -1);
75 if ((fp = fopen(filename, "w")) == NULL) {
76 FILE_OP_ERROR(filename, "fopen");
77 recv_write(sock, NULL);
81 if (change_file_mode_rw(fp, filename) < 0)
82 FILE_OP_ERROR(filename, "chmod");
84 if (recv_bytes_write(sock, size, fp) < 0) {
90 if (fclose(fp) == EOF) {
91 FILE_OP_ERROR(filename, "fclose");
99 gint recv_write(SockInfo *sock, FILE *fp)
105 nb = sock_is_nonblocking_mode(sock);
106 if (nb) sock_set_nonblocking_mode(sock, FALSE);
109 if (sock_gets(sock, buf, sizeof(buf)) < 0) {
110 g_warning(_("error occurred while retrieving data.\n"));
111 if (nb) sock_set_nonblocking_mode(sock, TRUE);
116 if (len > 1 && buf[0] == '.' && buf[1] == '\r') break;
119 recv_ui_func(sock, len, recv_ui_func_data);
121 if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
127 if (buf[0] == '.' && buf[1] == '.')
128 memmove(buf, buf + 1, len--);
130 if (!strncmp(buf, ">From ", 6))
131 memmove(buf, buf + 1, len--);
133 if (fp && fputs(buf, fp) == EOF) {
135 g_warning(_("Can't write to file.\n"));
140 if (nb) sock_set_nonblocking_mode(sock, TRUE);
147 gint recv_bytes_write(SockInfo *sock, glong size, FILE *fp)
154 nb = sock_is_nonblocking_mode(sock);
155 if (nb) sock_set_nonblocking_mode(sock, FALSE);
157 Xalloca(buf, size, return -1);
162 read_count = sock_read(sock, buf + count, size - count);
163 if (read_count < 0) {
164 if (nb) sock_set_nonblocking_mode(sock, TRUE);
168 } while (count < size);
170 /* +------------------+----------------+--------------------------+ *
171 * ^buf ^prev ^cur buf+size-1^ */
174 while ((cur = memchr(prev, '\r', size - (prev - buf))) != NULL) {
175 if (cur == buf + size - 1) break;
177 if (fwrite(prev, sizeof(gchar), cur - prev, fp) == EOF ||
178 fwrite("\n", sizeof(gchar), 1, fp) == EOF) {
180 g_warning(_("Can't write to file.\n"));
181 if (nb) sock_set_nonblocking_mode(sock, TRUE);
185 if (*(cur + 1) == '\n')
190 if (prev - buf >= size) break;
193 if (prev - buf < size && fwrite(buf, sizeof(gchar),
194 size - (prev - buf), fp) == EOF) {
196 g_warning(_("Can't write to file.\n"));
197 if (nb) sock_set_nonblocking_mode(sock, TRUE);
201 if (nb) sock_set_nonblocking_mode(sock, TRUE);
205 void recv_set_ui_func(RecvUIFunc func, gpointer data)
208 recv_ui_func_data = data;