#include "prefs_account.h"
#include "utils.h"
#include "recv.h"
-#include "folder.h"
-
+#include "partial_download.h"
#include "log.h"
#include "hooks.h"
guchar *data,
guint len);
-static gchar *pop3_get_filename_for_partial_mail(Pop3Session *session,
- gchar *muidl);
-static void pop3_delete_old_partial (const gchar *file);
-
static gint pop3_greeting_recv(Pop3Session *session, const gchar *msg)
{
session->state = POP3_GREETING;
session->state = POP3_GETAUTH_APOP;
if ((start = strchr(session->greeting, '<')) == NULL) {
- log_warning(_("Required APOP timestamp not found "
+ log_error(_("Required APOP timestamp not found "
"in greeting\n"));
session->error_val = PS_PROTOCOL;
return -1;
}
if ((end = strchr(start, '>')) == NULL || end == start + 1) {
- log_warning(_("Timestamp syntax error in greeting\n"));
+ log_error(_("Timestamp syntax error in greeting\n"));
session->error_val = PS_PROTOCOL;
return -1;
}
static gint pop3_getrange_stat_recv(Pop3Session *session, const gchar *msg)
{
if (sscanf(msg, "%d %d", &session->count, &session->total_bytes) != 2) {
- log_warning(_("POP3 protocol error\n"));
+ log_error(_("POP3 protocol error\n"));
session->error_val = PS_PROTOCOL;
return -1;
} else {
p = newline + 1;
if (p < lastp && *p == '\n') p++;
- if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
- return -1;
- if (num <= 0 || num > session->count)
- return -1;
+ if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2 ||
+ num <= 0 || num > session->count) {
+ log_warning(_("invalid UIDL response: %s\n"), buf);
+ continue;
+ }
session->msg[num].uidl = g_strdup(id);
partial_recv = (gint)g_hash_table_lookup(
session->partial_recv_table, id);
- if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE) {
+ if ((!session->ac_prefs->getall && recv_time != RECV_TIME_NONE)
+ || partial_recv != POP3_TOTALLY_RECEIVED) {
session->msg[num].received =
(partial_recv != POP3_MUST_COMPLETE_RECV);
session->msg[num].partial_recv = partial_recv;
if (session->msg[session->cur_msg].partial_recv
== POP3_MUST_COMPLETE_RECV) {
- gchar *old_file = pop3_get_filename_for_partial_mail(
- session, session->msg[session->cur_msg].uidl);
+ gchar *old_file = partial_get_filename(
+ session->ac_prefs->recv_server,
+ session->ac_prefs->userid,
+ session->msg[session->cur_msg].uidl);
if (old_file) {
- pop3_delete_old_partial (old_file);
+ partial_delete_old(old_file);
g_free(old_file);
}
}
mail_receive_data.data = g_strndup(data, len);
hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
- partial_notice = g_strdup_printf("SC-Partially-Retrieved: %s\n"
+ partial_notice = g_strdup_printf("SC-Marked-For-Download: 0\n"
+ "SC-Partially-Retrieved: %s\n"
"SC-Account-Server: %s\n"
"SC-Account-Login: %s\n"
"SC-Message-Size: %d",
g_vsnprintf(buf, sizeof(buf) - 2, format, args);
va_end(args);
- if (!strncasecmp(buf, "PASS ", 5))
+ if (!g_ascii_strncasecmp(buf, "PASS ", 5))
log_print("POP3> PASS ********\n");
else
log_print("POP3> %s\n", 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 (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, tmp) < 3) {
+ if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
+ if (sscanf(buf, "%s", uidl) != 1)
+ continue;
+ else {
+ recv_time = now;
+ strcpy(tmp, "0");
+ }
+ } else {
+ strcpy(tmp, "0");
}
}
+
if (recv_time == RECV_TIME_NONE)
recv_time = RECV_TIME_RECEIVED;
g_hash_table_insert(table, g_strdup(uidl),
return;
}
-int pop3_msg_in_uidl_list(const gchar *server, const gchar *login,
- const gchar *muidl)
-{
- gchar *path;
- 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 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, muidl)) {
- fclose(fp);
- return TRUE;
- }
- }
-
- fclose(fp);
- return FALSE;
-}
-
-static gchar *pop3_get_filename_for_partial_mail(Pop3Session *session,
- 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,
- session->ac_prefs->recv_server,
- "-", session->ac_prefs->userid, 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-", session->ac_prefs->recv_server,
- "-", session->ac_prefs->userid, 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 = strdup(tmp);
- break;
- }
- }
-
- fclose(fp);
-
- return result;
-}
-
-static int pop3_uidl_mark_mail(const gchar *server, const gchar *login,
- const gchar *muidl, const gchar *filename,
- const gchar *folder_item_id, guint msgnum,
- 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];
-
- path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- "uidl", G_DIR_SEPARATOR_S, server,
- "-", 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-", server,
- "-", login, NULL);
- if ((fp = fopen(path, "rb")) == NULL) {
- if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
- g_free(path);
- }
- return -1;
- }
-
- pathnew = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
- "uidl", G_DIR_SEPARATOR_S, server,
- "-", login, ".new", NULL);
- if ((fpnew = fopen(pathnew, "wb")) == NULL) {
- perror("fopen2");
- fclose(fp);
- g_free(pathnew);
- return -1;
- }
-
- 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(muidl, uidl)) {
- fprintf(fpnew, "%s\t%ld\t%s\n",
- uidl, recv_time, partial_recv);
- } else {
- gchar *stat = NULL;
- if (download == POP3_PARTIAL_DLOAD_DLOAD) {
- stat = g_strdup_printf("%s:%d",
- folder_item_id, msgnum);
- }
- else if (download == POP3_PARTIAL_DLOAD_UNKN)
- stat = strdup("1");
- else if (download == POP3_PARTIAL_DLOAD_DELE)
- stat = 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");
- return -1;
- }
- pathnew = g_strdup_printf("%s.new", filename);
- if ((fpnew = fopen(pathnew, "wb")) == NULL) {
- perror("fopen4");
- fclose(fp);
- g_free(pathnew);
- return -1;
- }
-
- while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
- buf[len]='\0';
- if (start) {
- start = FALSE;
- if (download != POP3_PARTIAL_DLOAD_UNKN)
- 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);
- return 0;
-}
-
-/* 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.
- */
-
-int pop3_mark_for_delete(const gchar *server, const gchar *login,
- const gchar *muidl, const gchar *file,
- const gchar *folder_item_id, guint msgnum)
-{
- return pop3_uidl_mark_mail(server, login, muidl, file, folder_item_id,
- msgnum, POP3_PARTIAL_DLOAD_DELE);
-}
-
-int pop3_mark_for_download(const gchar *server, const gchar *login,
- const gchar *muidl, const gchar *file,
- const gchar *folder_item_id, guint msgnum)
-{
- return pop3_uidl_mark_mail(server, login, muidl, file, folder_item_id,
- msgnum, POP3_PARTIAL_DLOAD_DLOAD);
-}
-
-int pop3_unmark(const gchar *server, const gchar *login,
- const gchar *muidl, const gchar *file,
- const gchar *folder_item_id, guint msgnum)
-{
- return pop3_uidl_mark_mail(server, login, muidl, file, folder_item_id,
- msgnum, POP3_PARTIAL_DLOAD_UNKN);
-}
-
-static void pop3_delete_old_partial(const gchar *file)
-{
- gchar *id = 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);
-}
-
gint pop3_write_uidl_list(Pop3Session *session)
{
gchar *path;
FILE_OP_ERROR(file, "chmod");
if (prefix != NULL) {
- fprintf(fp, prefix);
+ fprintf(fp, "%s", prefix);
fprintf(fp, "\n");
}
strstr(msg + 4, "Lock") ||
strstr(msg + 4, "LOCK") ||
strstr(msg + 4, "wait")) {
- log_warning(_("mailbox is locked\n"));
+ log_error(_("mailbox is locked\n"));
ok = PS_LOCKBUSY;
} else if (strcasestr(msg + 4, "timeout")) {
- log_warning(_("session timeout\n"));
+ log_error(_("Session timeout\n"));
ok = PS_ERROR;
} else {
switch (session->state) {
#if USE_OPENSSL
case POP3_STLS:
- log_warning(_("can't start TLS session\n"));
+ log_error(_("can't start TLS session\n"));
ok = PS_ERROR;
break;
#endif
case POP3_GETAUTH_USER:
case POP3_GETAUTH_PASS:
case POP3_GETAUTH_APOP:
- log_warning(_("error occurred on authentication\n"));
+ log_error(_("error occurred on authentication\n"));
ok = PS_AUTHFAIL;
break;
case POP3_GETRANGE_LAST:
break;
default:
- log_warning(_("error occurred on POP3 session\n"));
+ log_error(_("error occurred on POP3 session\n"));
ok = PS_ERROR;
}
}
pop3_stls_send(pop3_session);
else
#endif
- if (pop3_session->ac_prefs->protocol == A_APOP)
+ if (pop3_session->ac_prefs->use_apop_auth)
pop3_getauth_apop_send(pop3_session);
else
pop3_getauth_user_send(pop3_session);
case POP3_STLS:
if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
return -1;
- if (pop3_session->ac_prefs->protocol == A_APOP)
+ if (pop3_session->ac_prefs->use_apop_auth)
pop3_getauth_apop_send(pop3_session);
else
pop3_getauth_user_send(pop3_session);