2012-08-27 [colin] 3.8.1cvs37
[claws.git] / src / partial_download.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> 
4  * and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
18  * 
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 #include "claws-features.h"
48 #endif
49
50 #include <glib.h>
51 #include <glib/gi18n.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <stdarg.h>
55 #include <ctype.h>
56 #include <unistd.h>
57 #include <time.h>
58 #include <errno.h>
59
60 #include "partial_download.h"
61 #include "utils.h"
62 #include "pop.h"
63 #include "folder.h"
64 #include "procheader.h"
65 #include "msgcache.h"
66
67 int partial_msg_in_uidl_list(MsgInfo *msginfo)
68 {
69         gchar *path;
70         FILE *fp;
71         gchar buf[POPBUFSIZE];
72         gchar uidl[POPBUFSIZE];
73         time_t recv_time;
74         time_t now;
75         gint partial_recv;
76         gchar *sanitized_uid = NULL;
77         
78         if (!msginfo->extradata)
79                 return FALSE;
80
81         sanitized_uid = g_strdup(msginfo->extradata->account_login);
82         
83         subst_for_filename(sanitized_uid);
84
85         if (!msginfo->extradata->account_server
86         ||  !msginfo->extradata->account_login
87         ||  !msginfo->extradata->partial_recv)
88                 return FALSE;
89         
90         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
91                            "uidl", G_DIR_SEPARATOR_S, msginfo->extradata->account_server,
92                            "-", msginfo->extradata->account_login, NULL);
93         if ((fp = g_fopen(path, "rb")) == NULL) {
94                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
95                 g_free(path);
96                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
97                                    "uidl-", msginfo->extradata->account_server,
98                                    "-", sanitized_uid, NULL);
99                 if ((fp = g_fopen(path, "rb")) == NULL) {
100                         if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
101                         g_free(sanitized_uid);
102                         g_free(path);
103                         return FALSE;
104                 }
105         }
106         g_free(sanitized_uid);
107         g_free(path);
108
109         now = time(NULL);
110
111         while (fgets(buf, sizeof(buf), fp) != NULL) {
112                 gchar tmp[POPBUFSIZE];
113                 strretchomp(buf);
114                 recv_time = RECV_TIME_NONE;
115                 partial_recv = POP3_TOTALLY_RECEIVED;
116                 
117                 if (sscanf(buf, "%s\t%ld\t%s", uidl, (long int *) &recv_time, 
118                            tmp) < 2) {
119                         if (sscanf(buf, "%s", uidl) != 1)
120                                 continue;
121                         else {
122                                 recv_time = now;
123                         }
124                 }
125                 if (!strcmp(uidl, msginfo->extradata->partial_recv)) {
126                         fclose(fp);
127                         return TRUE;
128                 }
129         }
130
131         fclose(fp);     
132         return FALSE;
133 }
134
135 static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
136 {
137         gchar *path;
138         gchar *pathnew;
139         FILE *fp;
140         FILE *fpnew;
141         gchar buf[POPBUFSIZE];
142         gchar uidl[POPBUFSIZE];
143         time_t recv_time;
144         time_t now;
145         gchar partial_recv[POPBUFSIZE];
146         int err = -1;
147         gchar *filename;
148         MsgInfo *tinfo;
149         gchar *sanitized_uid = NULL;    
150
151         filename = procmsg_get_message_file_path(msginfo);
152         if (!filename) {
153                 g_warning("can't get message file path.\n");
154                 return err;
155         }
156         tinfo = procheader_parse_file(filename, msginfo->flags, TRUE, TRUE);
157         
158         if (!tinfo->extradata) {
159                 g_free(filename);
160                 return err;
161         }
162
163         sanitized_uid = g_strdup(tinfo->extradata->account_login);
164         subst_for_filename(sanitized_uid);
165
166         if (!tinfo->extradata->account_server
167         ||  !tinfo->extradata->account_login
168         ||  !tinfo->extradata->partial_recv) {
169                 goto bail;
170         }
171         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
172                            "uidl", G_DIR_SEPARATOR_S, tinfo->extradata->account_server,
173                            "-", sanitized_uid, NULL);
174
175         if ((fp = g_fopen(path, "rb")) == NULL) {
176                 perror("fopen1");
177                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
178                 g_free(path);
179                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
180                                    "uidl-", tinfo->extradata->account_server,
181                                    "-", tinfo->extradata->account_login, NULL);
182                 if ((fp = g_fopen(path, "rb")) == NULL) {
183                         if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
184                         g_free(path);
185                         goto bail;
186                 }
187         }
188
189         pathnew = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
190                            "uidl", G_DIR_SEPARATOR_S, tinfo->extradata->account_server,
191                            "-", sanitized_uid, ".new", NULL);
192         
193         g_free(sanitized_uid);
194
195         if ((fpnew = g_fopen(pathnew, "wb")) == NULL) {
196                 perror("fopen2");
197                 fclose(fp);
198                 g_free(pathnew);
199                 goto bail;
200         }
201         
202         now = time(NULL);
203
204         while (fgets(buf, sizeof(buf), fp) != NULL) {
205                 strretchomp(buf);
206                 recv_time = RECV_TIME_NONE;
207                 sprintf(partial_recv,"0");
208                 
209                 if (sscanf(buf, "%s\t%ld\t%s", 
210                            uidl, (long int *) &recv_time, partial_recv) < 2) {
211                         if (sscanf(buf, "%s", uidl) != 1)
212                                 continue;
213                         else {
214                                 recv_time = now;
215                         }
216                 }
217                 if (strcmp(tinfo->extradata->partial_recv, uidl)) {
218                         if (fprintf(fpnew, "%s\t%ld\t%s\n", 
219                                 uidl, (long int) recv_time, partial_recv) < 0) {
220                                 FILE_OP_ERROR(pathnew, "fprintf");
221                                 fclose(fpnew);
222                                 fclose(fp);
223                                 g_free(path);
224                                 g_free(pathnew);
225                                 goto bail;
226                         }
227                 } else {
228                         gchar *stat = NULL;
229                         if (download == POP3_PARTIAL_DLOAD_DLOAD) {
230                                 gchar *folder_id = folder_item_get_identifier(
231                                                         msginfo->folder);
232                                 stat = g_strdup_printf("%s:%d",
233                                         folder_id, msginfo->msgnum);
234                                 g_free(folder_id);
235                         }
236                         else if (download == POP3_PARTIAL_DLOAD_UNKN)
237                                 stat = g_strdup("1");
238                         else if (download == POP3_PARTIAL_DLOAD_DELE)
239                                 stat = g_strdup("0");
240                         
241                         if (fprintf(fpnew, "%s\t%ld\t%s\n", 
242                                 uidl, (long int) recv_time, stat) < 0) {
243                                 FILE_OP_ERROR(pathnew, "fprintf");
244                                 fclose(fpnew);
245                                 fclose(fp);
246                                 g_free(path);
247                                 g_free(pathnew);
248                                 goto bail;
249                         }
250                         g_free(stat);
251                 }
252         }
253         if (fclose(fpnew) == EOF) {
254                 FILE_OP_ERROR(pathnew, "fclose");
255                 fclose(fp);
256                 g_free(path);
257                 g_free(pathnew);
258                 goto bail;
259         }
260         fclose(fp);
261
262         move_file(pathnew, path, TRUE);
263
264         g_free(path);
265         g_free(pathnew);
266         
267         if ((fp = g_fopen(filename,"rb")) == NULL) {
268                 perror("fopen3");
269                 goto bail;
270         }
271         pathnew = g_strdup_printf("%s.new", filename);
272         if ((fpnew = g_fopen(pathnew, "wb")) == NULL) {
273                 perror("fopen4");
274                 fclose(fp);
275                 g_free(pathnew);
276                 goto bail;
277         }
278         
279         if (fprintf(fpnew, "SC-Marked-For-Download: %d\n", 
280                         download) < 0) {
281                 FILE_OP_ERROR(pathnew, "fprintf");
282                 fclose(fpnew);
283                 fclose(fp);
284                 g_free(pathnew);
285                 goto bail;
286         }
287         while (fgets(buf, sizeof(buf)-1, fp) != NULL) {
288                 if(strlen(buf) > strlen("SC-Marked-For-Download: x\n")
289                 && !strncmp(buf, "SC-Marked-For-Download:", 
290                             strlen("SC-Marked-For-Download:"))) {
291                         if (fprintf(fpnew, "%s", 
292                          buf+strlen("SC-Marked-For-Download: x\n")) < 0) {
293                                 FILE_OP_ERROR(pathnew, "fprintf");
294                                 fclose(fpnew);
295                                 fclose(fp);
296                                 g_free(pathnew);
297                                 goto bail;
298                         }
299                         continue;
300                 } else if (strlen(buf) == strlen("SC-Marked-For-Download: x\n")
301                 && !strncmp(buf, "SC-Marked-For-Download:", 
302                             strlen("SC-Marked-For-Download:"))) {
303                         continue;
304                 }
305                 if (fprintf(fpnew, "%s", buf) < 0) {
306                         FILE_OP_ERROR(pathnew, "fprintf");
307                         fclose(fpnew);
308                         fclose(fp);
309                         g_free(pathnew);
310                         goto bail;
311                 }
312         }
313         if (fclose(fpnew) == EOF) {
314                 FILE_OP_ERROR(pathnew, "fclose");
315                 fclose(fp);
316                 g_free(pathnew);
317                 goto bail;
318         }
319
320         fclose(fp);
321         claws_unlink(filename);
322         g_rename(pathnew, filename);
323         g_free(pathnew);
324         msginfo->planned_download = download;
325         msgcache_update_msg(msginfo->folder->cache, msginfo);
326
327         err = 0;
328 bail:
329         g_free(filename);
330         procmsg_msginfo_free(tinfo);
331         
332         return err;
333 }
334  
335 int partial_mark_for_delete(MsgInfo *msginfo)
336 {
337         return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DELE);
338 }
339
340 int partial_mark_for_download(MsgInfo *msginfo)
341 {
342         return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DLOAD);
343 }
344
345 int partial_unmark(MsgInfo *msginfo)
346 {
347         return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_UNKN);
348 }
349
350 void partial_delete_old(const gchar *file) 
351 {
352         gchar *id = g_strdup(file);
353         gchar *snum = strrchr(file, ':');
354         int num = 0;
355         FolderItem *item = NULL;
356
357         debug_print("too big message updated, should remove %s\n", file?file:"(null)");
358
359         if (snum) {
360                 snum++;
361         } else {
362                 g_free(id);
363                 return; /* not a real problem */
364         }
365
366         num = atoi(snum);
367
368         if (strrchr(id, ':'))
369                 *(strrchr(id, ':'))='\0';
370
371         item = folder_find_item_from_identifier(id);
372         if (item) {
373                 debug_print("removing %d in %s\n", num, id);
374                 folder_item_remove_msg(item, num);
375         } 
376         g_free(id);
377 }
378
379 gchar *partial_get_filename(const gchar *server, const gchar *login,
380                                    const gchar *muidl)
381 {
382         gchar *path;
383         gchar *result = NULL;
384         FILE *fp;
385         gchar buf[POPBUFSIZE];
386         gchar uidl[POPBUFSIZE];
387         time_t recv_time;
388         time_t now;
389         gint partial_recv;
390         gchar *sanitized_uid = g_strdup(login); 
391
392         subst_for_filename(sanitized_uid);
393
394         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
395                            "uidl", G_DIR_SEPARATOR_S, 
396                            server, "-", sanitized_uid, NULL);
397         if ((fp = g_fopen(path, "rb")) == NULL) {
398                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
399                 g_free(path);
400                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
401                                    "uidl-", server,
402                                    "-", sanitized_uid, NULL);
403                 if ((fp = g_fopen(path, "rb")) == NULL) {
404                         if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
405                         g_free(sanitized_uid);
406                         g_free(path);
407                         return result;
408                 }
409         }
410         g_free(sanitized_uid);
411         g_free(path);
412
413         now = time(NULL);
414
415         while (fgets(buf, sizeof(buf), fp) != NULL) {
416                 gchar tmp[POPBUFSIZE];
417                 strretchomp(buf);
418                 recv_time = RECV_TIME_NONE;
419                 partial_recv = POP3_TOTALLY_RECEIVED;
420                 
421                 if (sscanf(buf, "%s\t%ld\t%s", uidl, (long int *) &recv_time, 
422                            tmp) < 2) {
423                         if (sscanf(buf, "%s", uidl) != 1)
424                                 continue;
425                         else {
426                                 recv_time = now;
427                         }
428                 }
429                 if (!strcmp(muidl, uidl)) {
430                         result = g_strdup(tmp);
431                         break;
432                 }
433         }
434
435         fclose(fp);
436         
437         return result;
438 }
439