2005-11-08 [wwp] 1.9.100cvs2
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <glib/gi18n.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <stdarg.h>
54 #include <ctype.h>
55 #include <unistd.h>
56 #include <time.h>
57 #include <errno.h>
58
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 = g_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 = g_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, (long int *) &recv_time, 
112                            tmp) < 2) {
113                         if (sscanf(buf, "%s", uidl) != 1)
114                                 continue;
115                         else {
116                                 recv_time = now;
117                         }
118                 }
119                 if (!strcmp(uidl, msginfo->partial_recv)) {
120                         fclose(fp);
121                         return TRUE;
122                 }
123         }
124
125         fclose(fp);     
126         return FALSE;
127 }
128
129 static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
130 {
131         gchar *path;
132         gchar *pathnew;
133         FILE *fp;
134         FILE *fpnew;
135         gchar buf[POPBUFSIZE];
136         gchar uidl[POPBUFSIZE];
137         time_t recv_time;
138         time_t now;
139         int len;
140         int start = TRUE;
141         gchar partial_recv[POPBUFSIZE];
142         int err = -1;
143         gchar *filename;
144         MsgInfo *tinfo;
145         gchar *sanitized_uid = NULL;    
146
147         filename = procmsg_get_message_file_path(msginfo);
148         if (!filename) {
149                 g_warning("can't get message file path.\n");
150                 return err;
151         }
152         tinfo = procheader_parse_file(filename, msginfo->flags, TRUE, TRUE);
153
154         sanitized_uid = g_strdup(tinfo->account_login);
155         subst_for_filename(sanitized_uid);
156
157         if (!tinfo->account_server
158         ||  !tinfo->account_login
159         ||  !tinfo->partial_recv) {
160                 goto bail;
161         }
162         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
163                            "uidl", G_DIR_SEPARATOR_S, tinfo->account_server,
164                            "-", sanitized_uid, NULL);
165
166         if ((fp = g_fopen(path, "rb")) == NULL) {
167                 perror("fopen1");
168                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
169                 g_free(path);
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");
175                         g_free(path);
176                         goto bail;
177                 }
178         }
179
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);
183         
184         g_free(sanitized_uid);
185
186         if ((fpnew = g_fopen(pathnew, "wb")) == NULL) {
187                 perror("fopen2");
188                 fclose(fp);
189                 g_free(pathnew);
190                 goto bail;
191         }
192         
193         now = time(NULL);
194
195         while (fgets(buf, sizeof(buf), fp) != NULL) {
196                 strretchomp(buf);
197                 recv_time = RECV_TIME_NONE;
198                 sprintf(partial_recv,"0");
199                 
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)
203                                 continue;
204                         else {
205                                 recv_time = now;
206                         }
207                 }
208                 if (strcmp(tinfo->partial_recv, uidl)) {
209                         fprintf(fpnew, "%s\t%ld\t%s\n", 
210                                 uidl, (long int) recv_time, partial_recv);
211                 } else {
212                         gchar *stat = NULL;
213                         if (download == POP3_PARTIAL_DLOAD_DLOAD) {
214                                 gchar *folder_id = folder_item_get_identifier(
215                                                         msginfo->folder);
216                                 stat = g_strdup_printf("%s:%d",
217                                         folder_id, msginfo->msgnum);
218                                 g_free(folder_id);
219                         }
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");
224                         
225                         fprintf(fpnew, "%s\t%ld\t%s\n", 
226                                 uidl, (long int) recv_time, stat);
227                         g_free(stat);
228                 }
229         }
230         fclose(fpnew);
231         fclose(fp);
232
233         move_file(pathnew, path, TRUE);
234
235         g_free(path);
236         g_free(pathnew);
237         
238         if ((fp = g_fopen(filename,"rb")) == NULL) {
239                 perror("fopen3");
240                 goto bail;
241         }
242         pathnew = g_strdup_printf("%s.new", filename);
243         if ((fpnew = g_fopen(pathnew, "wb")) == NULL) {
244                 perror("fopen4");
245                 fclose(fp);
246                 g_free(pathnew);
247                 goto bail;
248         }
249         
250         while ((len = fread(buf, sizeof(gchar), sizeof(buf)-1, fp)) > 0) {
251                 buf[len]='\0';
252                 if (start) {
253                         start = FALSE;
254                         fprintf(fpnew, "SC-Marked-For-Download: %d\n", 
255                                         download);
256                         
257                         if(strlen(buf) > strlen("SC-Marked-For-Download: x\n")
258                         && !strncmp(buf, "SC-Marked-For-Download:", 
259                                     strlen("SC-Marked-For-Download:"))) {
260                                 fprintf(fpnew, "%s", 
261                                  buf+strlen("SC-Marked-For-Download: x\n"));
262                                 continue;
263                         }
264                 }
265                 fprintf(fpnew, "%s", buf);
266         }
267         fclose(fpnew);
268         fclose(fp);
269         g_unlink(filename);
270         rename(pathnew, filename);
271         g_free(pathnew);
272         msginfo->planned_download = download;
273         msgcache_update_msg(msginfo->folder->cache, msginfo);
274
275         err = 0;
276 bail:
277         g_free(filename);
278         procmsg_msginfo_free(tinfo);
279         
280         return err;
281 }
282  
283 int partial_mark_for_delete(MsgInfo *msginfo)
284 {
285         return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DELE);
286 }
287
288 int partial_mark_for_download(MsgInfo *msginfo)
289 {
290         return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DLOAD);
291 }
292
293 int partial_unmark(MsgInfo *msginfo)
294 {
295         return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_UNKN);
296 }
297
298 void partial_delete_old(const gchar *file) 
299 {
300         gchar *id = g_strdup(file);
301         gchar *snum = strrchr(file, ':');
302         int num = 0;
303         FolderItem *item = NULL;
304
305         debug_print("too big message updated,should remove %s\n", file);
306
307         if (snum) {
308                 snum++;
309         } else {
310                 g_free(id);
311                 return; /* not a real problem */
312         }
313
314         num = atoi(snum);
315
316         if (strrchr(id, ':'))
317                 *(strrchr(id, ':'))='\0';
318
319         item = folder_find_item_from_identifier(id);
320         if (item) {
321                 debug_print("removing %d in %s\n", num, id);
322                 folder_item_remove_msg(item, num);
323         } 
324         g_free(id);
325 }
326
327 gchar *partial_get_filename(const gchar *server, const gchar *login,
328                                    const gchar *muidl)
329 {
330         gchar *path;
331         gchar *result = NULL;
332         FILE *fp;
333         gchar buf[POPBUFSIZE];
334         gchar uidl[POPBUFSIZE];
335         time_t recv_time;
336         time_t now;
337         gint partial_recv;
338         gchar *sanitized_uid = g_strdup(login); 
339
340         subst_for_filename(sanitized_uid);
341
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");
347                 g_free(path);
348                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
349                                    "uidl-", server,
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);
354                         g_free(path);
355                         return result;
356                 }
357         }
358         g_free(sanitized_uid);
359         g_free(path);
360
361         now = time(NULL);
362
363         while (fgets(buf, sizeof(buf), fp) != NULL) {
364                 gchar tmp[POPBUFSIZE];
365                 strretchomp(buf);
366                 recv_time = RECV_TIME_NONE;
367                 partial_recv = POP3_TOTALLY_RECEIVED;
368                 
369                 if (sscanf(buf, "%s\t%ld\t%s", uidl, (long int *) &recv_time, 
370                            tmp) < 2) {
371                         if (sscanf(buf, "%s", uidl) != 1)
372                                 continue;
373                         else {
374                                 recv_time = now;
375                         }
376                 }
377                 if (!strcmp(muidl, uidl)) {
378                         result = g_strdup(tmp);
379                         break;
380                 }
381         }
382
383         fclose(fp);
384         
385         return result;
386 }
387