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