2005-05-09 [paul] 1.0.4cvs11
[claws.git] / src / partial_download.c
1 /*
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
5  *
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.
10  *
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.
15  *
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 /* Partial download:
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);
24  *
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 
28  * have three values:
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.
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #  include "config.h"
47 #endif
48
49 #include <glib.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdarg.h>
53 #include <ctype.h>
54 #include <unistd.h>
55 #include <time.h>
56 #include <errno.h>
57
58 #include "intl.h"
59 #include "partial_download.h"
60 #include "utils.h"
61 #include "pop.h"
62 #include "folder.h"
63 #include "procheader.h"
64 #include "msgcache.h"
65
66 int partial_msg_in_uidl_list(MsgInfo *msginfo)
67 {
68         gchar *path;
69         FILE *fp;
70         gchar buf[POPBUFSIZE];
71         gchar uidl[POPBUFSIZE];
72         time_t recv_time;
73         time_t now;
74         gint partial_recv;
75         gchar *sanitized_uid = g_strdup(msginfo->account_login);
76         
77         subst_for_filename(sanitized_uid);
78
79         if (!msginfo->account_server
80         ||  !msginfo->account_login
81         ||  !msginfo->partial_recv)
82                 return FALSE;
83         
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 = fopen(path, "rb")) == NULL) {
88                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
89                 g_free(path);
90                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
91                                    "uidl-", msginfo->account_server,
92                                    "-", sanitized_uid, NULL);
93                 if ((fp = fopen(path, "rb")) == NULL) {
94                         if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
95                         g_free(sanitized_uid);
96                         g_free(path);
97                         return FALSE;
98                 }
99         }
100         g_free(sanitized_uid);
101         g_free(path);
102
103         now = time(NULL);
104
105         while (fgets(buf, sizeof(buf), fp) != NULL) {
106                 gchar tmp[POPBUFSIZE];
107                 strretchomp(buf);
108                 recv_time = RECV_TIME_NONE;
109                 partial_recv = POP3_TOTALLY_RECEIVED;
110                 
111                 if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, tmp) < 2) {
112                         if (sscanf(buf, "%s", uidl) != 1)
113                                 continue;
114                         else {
115                                 recv_time = now;
116                         }
117                 }
118                 if (!strcmp(uidl, msginfo->partial_recv)) {
119                         fclose(fp);
120                         return TRUE;
121                 }
122         }
123
124         fclose(fp);     
125         return FALSE;
126 }
127
128 static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
129 {
130         gchar *path;
131         gchar *pathnew;
132         FILE *fp;
133         FILE *fpnew;
134         gchar buf[POPBUFSIZE];
135         gchar uidl[POPBUFSIZE];
136         time_t recv_time;
137         time_t now;
138         int len;
139         int start = TRUE;
140         gchar partial_recv[POPBUFSIZE];
141         int err = -1;
142         gchar *filename;
143         MsgInfo *tinfo;
144         gchar *sanitized_uid = NULL;    
145
146         filename = procmsg_get_message_file_path(msginfo);
147         if (!filename) {
148                 g_warning("can't get message file path.\n");
149                 return err;
150         }
151         tinfo = procheader_parse_file(filename, msginfo->flags, TRUE, TRUE);
152
153         sanitized_uid = g_strdup(tinfo->account_login);
154         subst_for_filename(sanitized_uid);
155
156         if (!tinfo->account_server
157         ||  !tinfo->account_login
158         ||  !tinfo->partial_recv) {
159                 goto bail;
160         }
161         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
162                            "uidl", G_DIR_SEPARATOR_S, tinfo->account_server,
163                            "-", sanitized_uid, NULL);
164
165         if ((fp = fopen(path, "rb")) == NULL) {
166                 perror("fopen1");
167                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
168                 g_free(path);
169                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
170                                    "uidl-", tinfo->account_server,
171                                    "-", tinfo->account_login, NULL);
172                 if ((fp = fopen(path, "rb")) == NULL) {
173                         if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
174                         g_free(path);
175                 }
176                 goto bail;
177         }
178
179         pathnew = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
180                            "uidl", G_DIR_SEPARATOR_S, tinfo->account_server,
181                            "-", sanitized_uid, ".new", NULL);
182         
183         g_free(sanitized_uid);
184
185         if ((fpnew = fopen(pathnew, "wb")) == NULL) {
186                 perror("fopen2");
187                 fclose(fp);
188                 g_free(pathnew);
189                 goto bail;
190         }
191         
192         now = time(NULL);
193
194         while (fgets(buf, sizeof(buf), fp) != NULL) {
195                 strretchomp(buf);
196                 recv_time = RECV_TIME_NONE;
197                 sprintf(partial_recv,"0");
198                 
199                 if (sscanf(buf, "%s\t%ld\t%s", 
200                            uidl, &recv_time, partial_recv) < 2) {
201                         if (sscanf(buf, "%s", uidl) != 1)
202                                 continue;
203                         else {
204                                 recv_time = now;
205                         }
206                 }
207                 if (strcmp(tinfo->partial_recv, uidl)) {
208                         fprintf(fpnew, "%s\t%ld\t%s\n", 
209                                 uidl, recv_time, partial_recv);
210                 } else {
211                         gchar *stat = NULL;
212                         if (download == POP3_PARTIAL_DLOAD_DLOAD) {
213                                 gchar *folder_id = folder_item_get_identifier(
214                                                         msginfo->folder);
215                                 stat = g_strdup_printf("%s:%d",
216                                         folder_id, msginfo->msgnum);
217                                 g_free(folder_id);
218                         }
219                         else if (download == POP3_PARTIAL_DLOAD_UNKN)
220                                 stat = g_strdup("1");
221                         else if (download == POP3_PARTIAL_DLOAD_DELE)
222                                 stat = g_strdup("0");
223                         
224                         fprintf(fpnew, "%s\t%ld\t%s\n", 
225                                 uidl, recv_time, stat);
226                         g_free(stat);
227                 }
228         }
229         fclose(fpnew);
230         fclose(fp);
231
232         move_file(pathnew, path, TRUE);
233
234         g_free(path);
235         g_free(pathnew);
236         
237         if ((fp = fopen(filename,"rb")) == NULL) {
238                 perror("fopen3");
239                 goto bail;
240         }
241         pathnew = g_strdup_printf("%s.new", filename);
242         if ((fpnew = fopen(pathnew, "wb")) == NULL) {
243                 perror("fopen4");
244                 fclose(fp);
245                 g_free(pathnew);
246                 goto bail;
247         }
248         
249         while ((len = fread(buf, sizeof(gchar), sizeof(buf)-1, fp)) > 0) {
250                 buf[len]='\0';
251                 if (start) {
252                         start = FALSE;
253                         fprintf(fpnew, "SC-Marked-For-Download: %d\n", 
254                                         download);
255                         
256                         if(strlen(buf) > strlen("SC-Marked-For-Download: x\n")
257                         && !strncmp(buf, "SC-Marked-For-Download:", 
258                                     strlen("SC-Marked-For-Download:"))) {
259                                 fprintf(fpnew, "%s", 
260                                  buf+strlen("SC-Marked-For-Download: x\n"));
261                                 continue;
262                         }
263                 }
264                 fprintf(fpnew, "%s", buf);
265         }
266         fclose(fpnew);
267         fclose(fp);
268         unlink(filename);
269         rename(pathnew, filename);
270         g_free(pathnew);
271         msginfo->planned_download = download;
272         msgcache_update_msg(msginfo->folder->cache, msginfo);
273
274         err = 0;
275 bail:
276         g_free(filename);
277         procmsg_msginfo_free(tinfo);
278         
279         return err;
280 }
281  
282 int partial_mark_for_delete(MsgInfo *msginfo)
283 {
284         return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DELE);
285 }
286
287 int partial_mark_for_download(MsgInfo *msginfo)
288 {
289         return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DLOAD);
290 }
291
292 int partial_unmark(MsgInfo *msginfo)
293 {
294         return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_UNKN);
295 }
296
297 void partial_delete_old(const gchar *file) 
298 {
299         gchar *id = g_strdup(file);
300         gchar *snum = strrchr(file, ':');
301         int num = 0;
302         FolderItem *item = NULL;
303
304         debug_print("too big message updated,should remove %s\n", file);
305
306         if (snum) {
307                 snum++;
308         } else {
309                 g_free(id);
310                 return; /* not a real problem */
311         }
312
313         num = atoi(snum);
314
315         if (strrchr(id, ':'))
316                 *(strrchr(id, ':'))='\0';
317
318         item = folder_find_item_from_identifier(id);
319         if (item) {
320                 debug_print("removing %d in %s\n", num, id);
321                 folder_item_remove_msg(item, num);
322         } 
323         g_free(id);
324 }
325
326 gchar *partial_get_filename(const gchar *server, const gchar *login,
327                                    const gchar *muidl)
328 {
329         gchar *path;
330         gchar *result = NULL;
331         FILE *fp;
332         gchar buf[POPBUFSIZE];
333         gchar uidl[POPBUFSIZE];
334         time_t recv_time;
335         time_t now;
336         gint partial_recv;
337         gchar *sanitized_uid = g_strdup(login); 
338
339         subst_for_filename(sanitized_uid);
340
341         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
342                            "uidl", G_DIR_SEPARATOR_S, 
343                            server, "-", sanitized_uid, NULL);
344         if ((fp = fopen(path, "rb")) == NULL) {
345                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
346                 g_free(path);
347                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
348                                    "uidl-", server,
349                                    "-", sanitized_uid, NULL);
350                 if ((fp = fopen(path, "rb")) == NULL) {
351                         if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
352                         g_free(sanitized_uid);
353                         g_free(path);
354                         return result;
355                 }
356         }
357         g_free(sanitized_uid);
358         g_free(path);
359
360         now = time(NULL);
361
362         while (fgets(buf, sizeof(buf), fp) != NULL) {
363                 gchar tmp[POPBUFSIZE];
364                 strretchomp(buf);
365                 recv_time = RECV_TIME_NONE;
366                 partial_recv = POP3_TOTALLY_RECEIVED;
367                 
368                 if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, tmp) < 2) {
369                         if (sscanf(buf, "%s", uidl) != 1)
370                                 continue;
371                         else {
372                                 recv_time = now;
373                         }
374                 }
375                 if (!strcmp(muidl, uidl)) {
376                         result = g_strdup(tmp);
377                         break;
378                 }
379         }
380
381         fclose(fp);
382         
383         return result;
384 }
385