claws-mail/src/partial_download.c
Paul Mangan 3e234d4879 2005-02-10 [paul] 1.0.1cvs3.2
* AUTHORS
	* Makefile.am
	* autogen.sh
	* configure.ac
	* config/mkinstalldirs
	* intl/.cvsignore
	* intl/ChangeLog
	* intl/VERSION
	* intl/bindtextdom.c
	* intl/config.charset
	* intl/dcgettext.c
	* intl/dcigettext.c
	* intl/dcngettext.c
	* intl/dgettext.c
	* intl/dngettext.c
	* intl/eval-plural.h
	* intl/explodename.c
	* intl/finddomain.c
	* intl/gettext.c
	* intl/gettextP.h
	* intl/gmo.h
	* intl/hash-string.h
	* intl/intl-compat.c
	* intl/l10nflist.c
	* intl/libgnuintl.h.in
	* intl/loadinfo.h
	* intl/loadmsgcat.c
	* intl/localcharset.c
	* intl/localcharset.h
	* intl/locale.alias
	* intl/localealias.c
	* intl/localename.c
	* intl/log.c
	* intl/ngettext.c
	* intl/os2compat.c
	* intl/os2compat.h
	* intl/osdep.c
	* intl/plural-exp.c
	* intl/plural-exp.h
	* intl/plural.c
	* intl/plural.y
	* intl/ref-add.sin
	* intl/ref-del.sin
	* intl/relocatable.c
	* intl/relocatable.h
	* intl/textdomain.c
	* m4/Makefile.am
	* po/Makefile.in.in
	* src/Makefile.am
	* src/account.c
	* src/action.c
	* src/addr_compl.c
	* src/addressadd.c
	* src/addressbook.c
	* src/addrgather.c
	* src/addrindex.c
	* src/alertpanel.c
	* src/browseldap.c
	* src/codeconv.c
	* src/codeconv.h
	* src/compose.c
	* src/compose.h
	* src/crash.c
	* src/editaddress.c
	* src/editbook.c
	* src/editgroup.c
	* src/editjpilot.c
	* src/editldap.c
	* src/editldap_basedn.c
	* src/editvcard.c
	* src/exphtmldlg.c
	* src/expldifdlg.c
	* src/export.c
	* src/exporthtml.c
	* src/filtering.c
	* src/folder.c
	* src/folder_item_prefs.c
	* src/foldersel.c
	* src/folderview.c
	* src/grouplistdialog.c
	* src/headerview.c
	* src/imap.c
	* src/imap_gtk.c
	* src/import.c
	* src/importldif.c
	* src/importmutt.c
	* src/importpine.c
	* src/inc.c
	* src/ldif.c
	* src/main.c
	* src/mainwindow.c
	* src/matcher.c
	* src/mbox.c
	* src/message_search.c
	* src/messageview.c
	* src/mh.c
	* src/mh_gtk.c
	* src/mimeview.c
	* src/msgcache.c
	* src/news.c
	* src/news_gtk.c
	* src/noticeview.c
	* src/partial_download.c
	* src/pop.c
	* src/prefs_account.c
	* src/prefs_actions.c
	* src/prefs_common.c
	* src/prefs_customheader.c
	* src/prefs_display_header.c
	* src/prefs_ext_prog.c
	* src/prefs_filtering.c
	* src/prefs_filtering_action.c
	* src/prefs_folder_item.c
	* src/prefs_fonts.c
	* src/prefs_gtk.c
	* src/prefs_matcher.c
	* src/prefs_msg_colors.c
	* src/prefs_spelling.c
	* src/prefs_summary_column.c
	* src/prefs_template.c
	* src/prefs_themes.c
	* src/prefs_toolbar.c
	* src/prefs_wrapping.c
	* src/privacy.c
	* src/procheader.c
	* src/procmime.c
	* src/procmsg.c
	* src/quote_fmt.c
	* src/recv.c
	* src/send_message.c
	* src/setup.c
	* src/sourcewindow.c
	* src/ssl_manager.c
	* src/statusbar.c
	* src/summary_search.c
	* src/summaryview.c
	* src/textview.c
	* src/toolbar.c
	* src/wizard.c
	* src/common/nntp.c
	* src/common/plugin.c
	* src/common/smtp.c
	* src/common/ssl.c
	* src/common/ssl_certificate.c
	* src/common/string_match.c
	* src/common/sylpheed.c
	* src/common/template.c
	* src/common/utils.c
	* src/gtk/about.c
	* src/gtk/colorlabel.c
	* src/gtk/description_window.c
	* src/gtk/filesel.c
	* src/gtk/foldersort.c
	* src/gtk/gtkaspell.c
	* src/gtk/gtkutils.c
	* src/gtk/inputdialog.c
	* src/gtk/logwindow.c
	* src/gtk/menu.c
	* src/gtk/pluginwindow.c
	* src/gtk/prefswindow.c
	* src/gtk/progressdialog.c
	* src/gtk/quicksearch.c
	* src/gtk/sslcertwindow.c
	* src/plugins/clamav/clamav_plugin.c
	* src/plugins/clamav/clamav_plugin_gtk.c
	* src/plugins/demo/demo.c
	* src/plugins/dillo_viewer/dillo_prefs.c
	* src/plugins/dillo_viewer/dillo_viewer.c
	* src/plugins/image_viewer/plugin.c
	* src/plugins/image_viewer/viewer.c
	* src/plugins/image_viewer/viewerprefs.c
	* src/plugins/mathml_viewer/mathml_viewer.c
	* src/plugins/pgpmime/passphrase.c
	* src/plugins/pgpmime/plugin.c
	* src/plugins/pgpmime/prefs_gpg.c
	* src/plugins/pgpmime/select-keys.c
	* src/plugins/pgpmime/sgpgme.c
	* src/plugins/spamassassin/spamassassin.c
	* src/plugins/spamassassin/spamassassin_gtk.c
	* src/plugins/trayicon/trayicon.c
		sync with main (revision 104)
2005-02-10 12:06:07 +00:00

367 lines
9.2 KiB
C

/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
* Copyright (C) 1999-2004 Hiroyuki Yamamoto
* This file (C) 2004 Colin Leroy
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* Partial download:
* A mail which has been completely downloaded will have no special headers,
* and its entry in the uidl file will end by 0 (POP3_TOTALLY_RECEIVED);
*
* A mail which has been partially downloaded will have some special headers,
* and its entry in the uidl file will first be 1 (POP3_PARTIALLY_RECEIVED);
* the special headers will be including "SC-Marked-For-Download" which can
* have three values:
* 0 (POP3_PARTIAL_DLOAD_UNKN) meaning that the user has not yet chosen to
* download the mail or let it be deleted - this header is absent until the
* user first chooses an action
* 1 (POP3_PARTIAL_DLOAD_DLOAD) meaning that the user wants to finish
* downloading the mail
* 2 (POP3_PARTIAL_DLOAD_DELE) meaning that the user does not want to finish
* downloading the mail
* When updating this header to POP3_PARTIAL_DLOAD_DLOAD, the uidl line of
* this mail will end with the mail's physical path, which Sylpheed will remove
* after having downloaded the complete mail. msg->partial_recv will equal
* 2 (POP3_MUST_COMPLETE_RECV).
* When updating this header to POP3_PARTIAL_DLOAD_DELE, the uidl line of
* this mail will be 0 (POP3_TOTALLY_RECEIVED), which will let Sylpheed delete
* this mail from the server as soon as the leave_time preference specifies.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <glib.h>
#include <glib/gi18n.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include "partial_download.h"
#include "utils.h"
#include "pop.h"
#include "folder.h"
#include "procheader.h"
#include "msgcache.h"
int partial_msg_in_uidl_list(MsgInfo *msginfo)
{
gchar *path;
FILE *fp;
gchar buf[POPBUFSIZE];
gchar uidl[POPBUFSIZE];
time_t recv_time;
time_t now;
gint partial_recv;
if (!msginfo->account_server
|| !msginfo->account_login
|| !msginfo->partial_recv)
return FALSE;
path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
"uidl", G_DIR_SEPARATOR_S, msginfo->account_server,
"-", msginfo->account_login, NULL);
if ((fp = fopen(path, "rb")) == NULL) {
if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
g_free(path);
path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
"uidl-", msginfo->account_server,
"-", msginfo->account_login, NULL);
if ((fp = fopen(path, "rb")) == NULL) {
if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
g_free(path);
return FALSE;
}
}
g_free(path);
now = time(NULL);
while (fgets(buf, sizeof(buf), fp) != NULL) {
gchar tmp[POPBUFSIZE];
strretchomp(buf);
recv_time = RECV_TIME_NONE;
partial_recv = POP3_TOTALLY_RECEIVED;
if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, tmp) < 2) {
if (sscanf(buf, "%s", uidl) != 1)
continue;
else {
recv_time = now;
}
}
if (!strcmp(uidl, msginfo->partial_recv)) {
fclose(fp);
return TRUE;
}
}
fclose(fp);
return FALSE;
}
static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
{
gchar *path;
gchar *pathnew;
FILE *fp;
FILE *fpnew;
gchar buf[POPBUFSIZE];
gchar uidl[POPBUFSIZE];
time_t recv_time;
time_t now;
int len;
int start = TRUE;
gchar partial_recv[POPBUFSIZE];
int err = -1;
gchar *filename;
MsgInfo *tinfo;
filename = procmsg_get_message_file_path(msginfo);
if (!filename) {
g_warning("can't get message file path.\n");
return err;
}
tinfo = procheader_parse_file(filename, msginfo->flags, TRUE, TRUE);
if (!tinfo->account_server
|| !tinfo->account_login
|| !tinfo->partial_recv) {
goto bail;
}
path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
"uidl", G_DIR_SEPARATOR_S, tinfo->account_server,
"-", tinfo->account_login, NULL);
if ((fp = fopen(path, "rb")) == NULL) {
perror("fopen1");
if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
g_free(path);
path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
"uidl-", tinfo->account_server,
"-", tinfo->account_login, NULL);
if ((fp = fopen(path, "rb")) == NULL) {
if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
g_free(path);
}
goto bail;
}
pathnew = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
"uidl", G_DIR_SEPARATOR_S, tinfo->account_server,
"-", tinfo->account_login, ".new", NULL);
if ((fpnew = fopen(pathnew, "wb")) == NULL) {
perror("fopen2");
fclose(fp);
g_free(pathnew);
goto bail;
}
now = time(NULL);
while (fgets(buf, sizeof(buf), fp) != NULL) {
strretchomp(buf);
recv_time = RECV_TIME_NONE;
sprintf(partial_recv,"0");
if (sscanf(buf, "%s\t%ld\t%s",
uidl, &recv_time, partial_recv) < 2) {
if (sscanf(buf, "%s", uidl) != 1)
continue;
else {
recv_time = now;
}
}
if (strcmp(tinfo->partial_recv, uidl)) {
fprintf(fpnew, "%s\t%ld\t%s\n",
uidl, recv_time, partial_recv);
} else {
gchar *stat = NULL;
if (download == POP3_PARTIAL_DLOAD_DLOAD) {
gchar *folder_id = folder_item_get_identifier(
msginfo->folder);
stat = g_strdup_printf("%s:%d",
folder_id, msginfo->msgnum);
g_free(folder_id);
}
else if (download == POP3_PARTIAL_DLOAD_UNKN)
stat = g_strdup("1");
else if (download == POP3_PARTIAL_DLOAD_DELE)
stat = g_strdup("0");
fprintf(fpnew, "%s\t%ld\t%s\n",
uidl, recv_time, stat);
g_free(stat);
}
}
fclose(fpnew);
fclose(fp);
move_file(pathnew, path, TRUE);
g_free(path);
g_free(pathnew);
if ((fp = fopen(filename,"rb")) == NULL) {
perror("fopen3");
goto bail;
}
pathnew = g_strdup_printf("%s.new", filename);
if ((fpnew = fopen(pathnew, "wb")) == NULL) {
perror("fopen4");
fclose(fp);
g_free(pathnew);
goto bail;
}
while ((len = fread(buf, sizeof(gchar), sizeof(buf)-1, fp)) > 0) {
buf[len]='\0';
if (start) {
start = FALSE;
fprintf(fpnew, "SC-Marked-For-Download: %d\n",
download);
if(strlen(buf) > strlen("SC-Marked-For-Download: x\n")
&& !strncmp(buf, "SC-Marked-For-Download:",
strlen("SC-Marked-For-Download:"))) {
fprintf(fpnew, "%s",
buf+strlen("SC-Marked-For-Download: x\n"));
continue;
}
}
fprintf(fpnew, "%s", buf);
}
fclose(fpnew);
fclose(fp);
unlink(filename);
rename(pathnew, filename);
g_free(pathnew);
msginfo->planned_download = download;
msgcache_update_msg(msginfo->folder->cache, msginfo);
err = 0;
bail:
g_free(filename);
procmsg_msginfo_free(tinfo);
return err;
}
int partial_mark_for_delete(MsgInfo *msginfo)
{
return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DELE);
}
int partial_mark_for_download(MsgInfo *msginfo)
{
return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DLOAD);
}
int partial_unmark(MsgInfo *msginfo)
{
return partial_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_UNKN);
}
void partial_delete_old(const gchar *file)
{
gchar *id = g_strdup(file);
gchar *snum = strrchr(file, ':');
int num = 0;
FolderItem *item = NULL;
debug_print("too big message updated,should remove %s\n", file);
if (snum) {
snum++;
} else {
g_free(id);
return; /* not a real problem */
}
num = atoi(snum);
if (strrchr(id, ':'))
*(strrchr(id, ':'))='\0';
item = folder_find_item_from_identifier(id);
if (item) {
debug_print("removing %d in %s\n", num, id);
folder_item_remove_msg(item, num);
}
g_free(id);
}
gchar *partial_get_filename(const gchar *server, const gchar *login,
const gchar *muidl)
{
gchar *path;
gchar *result = NULL;
FILE *fp;
gchar buf[POPBUFSIZE];
gchar uidl[POPBUFSIZE];
time_t recv_time;
time_t now;
gint partial_recv;
path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
"uidl", G_DIR_SEPARATOR_S,
server, "-", login, NULL);
if ((fp = fopen(path, "rb")) == NULL) {
if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
g_free(path);
path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
"uidl-", server,
"-", login, NULL);
if ((fp = fopen(path, "rb")) == NULL) {
if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
g_free(path);
return result;
}
}
g_free(path);
now = time(NULL);
while (fgets(buf, sizeof(buf), fp) != NULL) {
gchar tmp[POPBUFSIZE];
strretchomp(buf);
recv_time = RECV_TIME_NONE;
partial_recv = POP3_TOTALLY_RECEIVED;
if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, tmp) < 2) {
if (sscanf(buf, "%s", uidl) != 1)
continue;
else {
recv_time = now;
}
}
if (!strcmp(muidl, uidl)) {
result = g_strdup(tmp);
break;
}
}
fclose(fp);
return result;
}