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