2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 Hiroyuki Yamamoto
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 = g_strdup(msginfo->account_login);
77 subst_for_filename(sanitized_uid);
79 if (!msginfo->account_server
80 || !msginfo->account_login
81 || !msginfo->partial_recv)
84 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
85 "uidl", G_DIR_SEPARATOR_S, msginfo->account_server,
86 "-", msginfo->account_login, NULL);
87 if ((fp = g_fopen(path, "rb")) == NULL) {
88 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
90 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
91 "uidl-", msginfo->account_server,
92 "-", sanitized_uid, NULL);
93 if ((fp = g_fopen(path, "rb")) == NULL) {
94 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
95 g_free(sanitized_uid);
100 g_free(sanitized_uid);
105 while (fgets(buf, sizeof(buf), fp) != NULL) {
106 gchar tmp[POPBUFSIZE];
108 recv_time = RECV_TIME_NONE;
109 partial_recv = POP3_TOTALLY_RECEIVED;
111 if (sscanf(buf, "%s\t%ld\t%s", uidl, (long int *) &recv_time,
113 if (sscanf(buf, "%s", uidl) != 1)
119 if (!strcmp(uidl, msginfo->partial_recv)) {
129 static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
135 gchar buf[POPBUFSIZE];
136 gchar uidl[POPBUFSIZE];
141 gchar partial_recv[POPBUFSIZE];
145 gchar *sanitized_uid = NULL;
147 filename = procmsg_get_message_file_path(msginfo);
149 g_warning("can't get message file path.\n");
152 tinfo = procheader_parse_file(filename, msginfo->flags, TRUE, TRUE);
154 sanitized_uid = g_strdup(tinfo->account_login);
155 subst_for_filename(sanitized_uid);
157 if (!tinfo->account_server
158 || !tinfo->account_login
159 || !tinfo->partial_recv) {
162 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
163 "uidl", G_DIR_SEPARATOR_S, tinfo->account_server,
164 "-", sanitized_uid, NULL);
166 if ((fp = g_fopen(path, "rb")) == NULL) {
168 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
170 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
171 "uidl-", tinfo->account_server,
172 "-", tinfo->account_login, NULL);
173 if ((fp = g_fopen(path, "rb")) == NULL) {
174 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
180 pathnew = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
181 "uidl", G_DIR_SEPARATOR_S, tinfo->account_server,
182 "-", sanitized_uid, ".new", NULL);
184 g_free(sanitized_uid);
186 if ((fpnew = g_fopen(pathnew, "wb")) == NULL) {
195 while (fgets(buf, sizeof(buf), fp) != NULL) {
197 recv_time = RECV_TIME_NONE;
198 sprintf(partial_recv,"0");
200 if (sscanf(buf, "%s\t%ld\t%s",
201 uidl, (long int *) &recv_time, partial_recv) < 2) {
202 if (sscanf(buf, "%s", uidl) != 1)
208 if (strcmp(tinfo->partial_recv, uidl)) {
209 fprintf(fpnew, "%s\t%ld\t%s\n",
210 uidl, (long int) recv_time, partial_recv);
213 if (download == POP3_PARTIAL_DLOAD_DLOAD) {
214 gchar *folder_id = folder_item_get_identifier(
216 stat = g_strdup_printf("%s:%d",
217 folder_id, msginfo->msgnum);
220 else if (download == POP3_PARTIAL_DLOAD_UNKN)
221 stat = g_strdup("1");
222 else if (download == POP3_PARTIAL_DLOAD_DELE)
223 stat = g_strdup("0");
225 fprintf(fpnew, "%s\t%ld\t%s\n",
226 uidl, (long int) recv_time, stat);
233 move_file(pathnew, path, TRUE);
238 if ((fp = g_fopen(filename,"rb")) == NULL) {
242 pathnew = g_strdup_printf("%s.new", filename);
243 if ((fpnew = g_fopen(pathnew, "wb")) == NULL) {
250 while ((len = fread(buf, sizeof(gchar), sizeof(buf)-1, fp)) > 0) {
254 fprintf(fpnew, "SC-Marked-For-Download: %d\n",
257 if(strlen(buf) > strlen("SC-Marked-For-Download: x\n")
258 && !strncmp(buf, "SC-Marked-For-Download:",
259 strlen("SC-Marked-For-Download:"))) {
261 buf+strlen("SC-Marked-For-Download: x\n"));
265 fprintf(fpnew, "%s", buf);
270 rename(pathnew, filename);
272 msginfo->planned_download = download;
273 msgcache_update_msg(msginfo->folder->cache, msginfo);
278 procmsg_msginfo_free(tinfo);
283 int partial_mark_for_delete(MsgInfo *msginfo)
285 return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DELE);
288 int partial_mark_for_download(MsgInfo *msginfo)
290 return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DLOAD);
293 int partial_unmark(MsgInfo *msginfo)
295 return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_UNKN);
298 void partial_delete_old(const gchar *file)
300 gchar *id = g_strdup(file);
301 gchar *snum = strrchr(file, ':');
303 FolderItem *item = NULL;
305 debug_print("too big message updated,should remove %s\n", file);
311 return; /* not a real problem */
316 if (strrchr(id, ':'))
317 *(strrchr(id, ':'))='\0';
319 item = folder_find_item_from_identifier(id);
321 debug_print("removing %d in %s\n", num, id);
322 folder_item_remove_msg(item, num);
327 gchar *partial_get_filename(const gchar *server, const gchar *login,
331 gchar *result = NULL;
333 gchar buf[POPBUFSIZE];
334 gchar uidl[POPBUFSIZE];
338 gchar *sanitized_uid = g_strdup(login);
340 subst_for_filename(sanitized_uid);
342 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
343 "uidl", G_DIR_SEPARATOR_S,
344 server, "-", sanitized_uid, NULL);
345 if ((fp = g_fopen(path, "rb")) == NULL) {
346 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
348 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
350 "-", sanitized_uid, NULL);
351 if ((fp = g_fopen(path, "rb")) == NULL) {
352 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
353 g_free(sanitized_uid);
358 g_free(sanitized_uid);
363 while (fgets(buf, sizeof(buf), fp) != NULL) {
364 gchar tmp[POPBUFSIZE];
366 recv_time = RECV_TIME_NONE;
367 partial_recv = POP3_TOTALLY_RECEIVED;
369 if (sscanf(buf, "%s\t%ld\t%s", uidl, (long int *) &recv_time,
371 if (sscanf(buf, "%s", uidl) != 1)
377 if (!strcmp(muidl, uidl)) {
378 result = g_strdup(tmp);