2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail team
4 * This file (C) 2004 Colin Leroy
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * A mail which has been completely downloaded will have no special headers,
23 * and its entry in the uidl file will end by 0 (POP3_TOTALLY_RECEIVED);
25 * A mail which has been partially downloaded will have some special headers,
26 * and its entry in the uidl file will first be 1 (POP3_PARTIALLY_RECEIVED);
27 * the special headers will be including "SC-Marked-For-Download" which can
29 * 0 (POP3_PARTIAL_DLOAD_UNKN) meaning that the user has not yet chosen to
30 * download the mail or let it be deleted - this header is absent until the
31 * user first chooses an action
32 * 1 (POP3_PARTIAL_DLOAD_DLOAD) meaning that the user wants to finish
33 * downloading the mail
34 * 2 (POP3_PARTIAL_DLOAD_DELE) meaning that the user does not want to finish
35 * downloading the mail
36 * When updating this header to POP3_PARTIAL_DLOAD_DLOAD, the uidl line of
37 * this mail will end with the mail's physical path, which Sylpheed will remove
38 * after having downloaded the complete mail. msg->partial_recv will equal
39 * 2 (POP3_MUST_COMPLETE_RECV).
40 * When updating this header to POP3_PARTIAL_DLOAD_DELE, the uidl line of
41 * this mail will be 0 (POP3_TOTALLY_RECEIVED), which will let Sylpheed delete
42 * this mail from the server as soon as the leave_time preference specifies.
50 #include <glib/gi18n.h>
59 #include "partial_download.h"
63 #include "procheader.h"
66 int partial_msg_in_uidl_list(MsgInfo *msginfo)
70 gchar buf[POPBUFSIZE];
71 gchar uidl[POPBUFSIZE];
75 gchar *sanitized_uid = NULL;
77 if (!msginfo->extradata)
80 sanitized_uid = g_strdup(msginfo->extradata->account_login);
82 subst_for_filename(sanitized_uid);
84 if (!msginfo->extradata->account_server
85 || !msginfo->extradata->account_login
86 || !msginfo->extradata->partial_recv)
89 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
90 "uidl", G_DIR_SEPARATOR_S, msginfo->extradata->account_server,
91 "-", msginfo->extradata->account_login, NULL);
92 if ((fp = g_fopen(path, "rb")) == NULL) {
93 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
95 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
96 "uidl-", msginfo->extradata->account_server,
97 "-", sanitized_uid, NULL);
98 if ((fp = g_fopen(path, "rb")) == NULL) {
99 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
100 g_free(sanitized_uid);
105 g_free(sanitized_uid);
110 while (fgets(buf, sizeof(buf), fp) != NULL) {
111 gchar tmp[POPBUFSIZE];
113 recv_time = RECV_TIME_NONE;
114 partial_recv = POP3_TOTALLY_RECEIVED;
116 if (sscanf(buf, "%s\t%ld\t%s", uidl, (long int *) &recv_time,
118 if (sscanf(buf, "%s", uidl) != 1)
124 if (!strcmp(uidl, msginfo->extradata->partial_recv)) {
134 static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
140 gchar buf[POPBUFSIZE];
141 gchar uidl[POPBUFSIZE];
146 gchar partial_recv[POPBUFSIZE];
150 gchar *sanitized_uid = NULL;
152 filename = procmsg_get_message_file_path(msginfo);
154 g_warning("can't get message file path.\n");
157 tinfo = procheader_parse_file(filename, msginfo->flags, TRUE, TRUE);
159 if (!tinfo->extradata) {
164 sanitized_uid = g_strdup(tinfo->extradata->account_login);
165 subst_for_filename(sanitized_uid);
167 if (!tinfo->extradata->account_server
168 || !tinfo->extradata->account_login
169 || !tinfo->extradata->partial_recv) {
172 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
173 "uidl", G_DIR_SEPARATOR_S, tinfo->extradata->account_server,
174 "-", sanitized_uid, NULL);
176 if ((fp = g_fopen(path, "rb")) == NULL) {
178 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
180 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
181 "uidl-", tinfo->extradata->account_server,
182 "-", tinfo->extradata->account_login, NULL);
183 if ((fp = g_fopen(path, "rb")) == NULL) {
184 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
190 pathnew = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
191 "uidl", G_DIR_SEPARATOR_S, tinfo->extradata->account_server,
192 "-", sanitized_uid, ".new", NULL);
194 g_free(sanitized_uid);
196 if ((fpnew = g_fopen(pathnew, "wb")) == NULL) {
205 while (fgets(buf, sizeof(buf), fp) != NULL) {
207 recv_time = RECV_TIME_NONE;
208 sprintf(partial_recv,"0");
210 if (sscanf(buf, "%s\t%ld\t%s",
211 uidl, (long int *) &recv_time, partial_recv) < 2) {
212 if (sscanf(buf, "%s", uidl) != 1)
218 if (strcmp(tinfo->extradata->partial_recv, uidl)) {
219 fprintf(fpnew, "%s\t%ld\t%s\n",
220 uidl, (long int) recv_time, partial_recv);
223 if (download == POP3_PARTIAL_DLOAD_DLOAD) {
224 gchar *folder_id = folder_item_get_identifier(
226 stat = g_strdup_printf("%s:%d",
227 folder_id, msginfo->msgnum);
230 else if (download == POP3_PARTIAL_DLOAD_UNKN)
231 stat = g_strdup("1");
232 else if (download == POP3_PARTIAL_DLOAD_DELE)
233 stat = g_strdup("0");
235 fprintf(fpnew, "%s\t%ld\t%s\n",
236 uidl, (long int) recv_time, stat);
243 move_file(pathnew, path, TRUE);
248 if ((fp = g_fopen(filename,"rb")) == NULL) {
252 pathnew = g_strdup_printf("%s.new", filename);
253 if ((fpnew = g_fopen(pathnew, "wb")) == NULL) {
260 fprintf(fpnew, "SC-Marked-For-Download: %d\n",
262 while (fgets(buf, sizeof(buf)-1, fp) != NULL) {
263 if(strlen(buf) > strlen("SC-Marked-For-Download: x\n")
264 && !strncmp(buf, "SC-Marked-For-Download:",
265 strlen("SC-Marked-For-Download:"))) {
267 buf+strlen("SC-Marked-For-Download: x\n"));
269 } else if (strlen(buf) == strlen("SC-Marked-For-Download: x\n")
270 && !strncmp(buf, "SC-Marked-For-Download:",
271 strlen("SC-Marked-For-Download:"))) {
274 fprintf(fpnew, "%s", buf);
279 rename(pathnew, filename);
281 msginfo->planned_download = download;
282 msgcache_update_msg(msginfo->folder->cache, msginfo);
287 procmsg_msginfo_free(tinfo);
292 int partial_mark_for_delete(MsgInfo *msginfo)
294 return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DELE);
297 int partial_mark_for_download(MsgInfo *msginfo)
299 return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DLOAD);
302 int partial_unmark(MsgInfo *msginfo)
304 return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_UNKN);
307 void partial_delete_old(const gchar *file)
309 gchar *id = g_strdup(file);
310 gchar *snum = strrchr(file, ':');
312 FolderItem *item = NULL;
314 debug_print("too big message updated,should remove %s\n", file);
320 return; /* not a real problem */
325 if (strrchr(id, ':'))
326 *(strrchr(id, ':'))='\0';
328 item = folder_find_item_from_identifier(id);
330 debug_print("removing %d in %s\n", num, id);
331 folder_item_remove_msg(item, num);
336 gchar *partial_get_filename(const gchar *server, const gchar *login,
340 gchar *result = NULL;
342 gchar buf[POPBUFSIZE];
343 gchar uidl[POPBUFSIZE];
347 gchar *sanitized_uid = g_strdup(login);
349 subst_for_filename(sanitized_uid);
351 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
352 "uidl", G_DIR_SEPARATOR_S,
353 server, "-", sanitized_uid, NULL);
354 if ((fp = g_fopen(path, "rb")) == NULL) {
355 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
357 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
359 "-", sanitized_uid, NULL);
360 if ((fp = g_fopen(path, "rb")) == NULL) {
361 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
362 g_free(sanitized_uid);
367 g_free(sanitized_uid);
372 while (fgets(buf, sizeof(buf), fp) != NULL) {
373 gchar tmp[POPBUFSIZE];
375 recv_time = RECV_TIME_NONE;
376 partial_recv = POP3_TOTALLY_RECEIVED;
378 if (sscanf(buf, "%s\t%ld\t%s", uidl, (long int *) &recv_time,
380 if (sscanf(buf, "%s", uidl) != 1)
386 if (!strcmp(muidl, uidl)) {
387 result = g_strdup(tmp);