/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
*
* 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
+ * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* 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.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
+#include "claws-features.h"
#endif
#include <glib.h>
+#include <glib/gi18n.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <errno.h>
-#include "intl.h"
#include "pop.h"
#include "md5.h"
#include "prefs_account.h"
#include "utils.h"
#include "recv.h"
-#include "folder.h"
-#include "procmsg.h"
-#include "procheader.h"
-#include "msgcache.h"
-
+#include "partial_download.h"
#include "log.h"
#include "hooks.h"
static gint pop3_getauth_user_send (Pop3Session *session);
static gint pop3_getauth_pass_send (Pop3Session *session);
static gint pop3_getauth_apop_send (Pop3Session *session);
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
static gint pop3_stls_send (Pop3Session *session);
static gint pop3_stls_recv (Pop3Session *session);
#endif
static gint pop3_session_recv_data_finished (Session *session,
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 void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session);
static gint pop3_greeting_recv(Pop3Session *session, const gchar *msg)
{
return PS_SUCCESS;
}
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
static gint pop3_stls_send(Pop3Session *session)
{
session->state = POP3_STLS;
}
return PS_SUCCESS;
}
-#endif /* USE_OPENSSL */
+#endif /* USE_GNUTLS */
static gint pop3_getauth_user_send(Pop3Session *session)
{
- g_return_val_if_fail(session->user != NULL, -1);
+ cm_return_val_if_fail(session->user != NULL, -1);
session->state = POP3_GETAUTH_USER;
pop3_gen_send(session, "USER %s", session->user);
static gint pop3_getauth_pass_send(Pop3Session *session)
{
- g_return_val_if_fail(session->pass != NULL, -1);
+ cm_return_val_if_fail(session->pass != NULL, -1);
session->state = POP3_GETAUTH_PASS;
pop3_gen_send(session, "PASS %s", session->pass);
gchar *apop_str;
gchar md5sum[33];
- g_return_val_if_fail(session->user != NULL, -1);
- g_return_val_if_fail(session->pass != NULL, -1);
+ cm_return_val_if_fail(session->user != NULL, -1);
+ cm_return_val_if_fail(session->pass != NULL, -1);
session->state = POP3_GETAUTH_APOP;
if ((start = strchr(session->greeting, '<')) == NULL) {
- log_warning(_("Required APOP timestamp not found "
+ log_error(LOG_PROTOCOL, _("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(LOG_PROTOCOL, _("Timestamp syntax error in greeting\n"));
session->error_val = PS_PROTOCOL;
return -1;
}
-
*(end + 1) = '\0';
+ if (!is_ascii_str(start)) {
+ log_error(LOG_PROTOCOL, _("Timestamp syntax error in greeting (not ASCII)\n"));
+ session->error_val = PS_PROTOCOL;
+ return -1;
+ }
+
apop_str = g_strconcat(start, session->pass, NULL);
md5_hex_digest(md5sum, apop_str);
g_free(apop_str);
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(LOG_PROTOCOL, _("POP protocol error\n"));
session->error_val = PS_PROTOCOL;
return -1;
} else {
gint last;
if (sscanf(msg, "%d", &last) == 0) {
- log_warning(_("POP3 protocol error\n"));
+ log_warning(LOG_PROTOCOL, _("POP protocol error\n"));
session->error_val = PS_PROTOCOL;
return -1;
} else {
gchar id[IDLEN + 1];
gchar buf[POPBUFSIZE];
gint buf_len;
- gint num;
+ guint32 num;
time_t recv_time;
gint partial_recv;
const gchar *p = data;
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(LOG_PROTOCOL, _("invalid UIDL response: %s\n"), buf);
+ continue;
+ }
session->msg[num].uidl = g_strdup(id);
- recv_time = (time_t)g_hash_table_lookup(
- session->uidl_table, id);
+ recv_time = (time_t)(GPOINTER_TO_INT(g_hash_table_lookup(
+ session->uidl_table, id)));
session->msg[num].recv_time = recv_time;
- partial_recv = (gint)g_hash_table_lookup(
- session->partial_recv_table, id);
+ if (recv_time != RECV_TIME_NONE) {
+ debug_print("num %d uidl %s: already got it\n", num, id);
+ } else {
+ debug_print("num %d uidl %s: unknown\n", num, id);
+ }
+
+ partial_recv = (gint)(GPOINTER_TO_INT(g_hash_table_lookup(
+ session->partial_recv_table, id)));
- if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE) {
+ if (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 (partial_recv == POP3_MUST_COMPLETE_RECV)
+ session->new_msg_exist = TRUE;
}
if (!session->new_msg_exist &&
- (session->ac_prefs->getall || recv_time == RECV_TIME_NONE ||
+ (recv_time == RECV_TIME_NONE ||
session->ac_prefs->rmmail)) {
session->cur_msg = num;
session->new_msg_exist = TRUE;
static gint pop3_retr_send(Pop3Session *session)
{
session->state = POP3_RETR;
+ debug_print("retrieving %d [%s]\n", session->cur_msg,
+ session->msg[session->cur_msg].uidl ?
+ session->msg[session->cur_msg].uidl:" ");
pop3_gen_send(session, "RETR %d", session->cur_msg);
return PS_SUCCESS;
}
gint drop_ok;
MailReceiveData mail_receive_data;
- mail_receive_data.session = session;
- mail_receive_data.data = g_strndup(data, len);
+ /* NOTE: we allocate a slightly larger buffer with a zero terminator
+ * because some plugins may think that it has a C string. */
+ mail_receive_data.session = session;
+ mail_receive_data.data = g_new0(gchar, len + 1);
+ mail_receive_data.data_len = len;
+ memcpy(mail_receive_data.data, data, len);
+
hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
file = get_tmp_file();
- if (pop3_write_msg_to_file(file, mail_receive_data.data,
- strlen(mail_receive_data.data), NULL) < 0) {
+ if (pop3_write_msg_to_file(file, mail_receive_data.data,
+ mail_receive_data.data_len, NULL) < 0) {
g_free(file);
g_free(mail_receive_data.data);
session->error_val = PS_IOERR;
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);
}
}
MailReceiveData mail_receive_data;
gchar *partial_notice = NULL;
- mail_receive_data.session = session;
- mail_receive_data.data = g_strndup(data, len);
+ /* NOTE: we allocate a slightly larger buffer with a zero terminator
+ * because some plugins may think that it has a C string. */
+ mail_receive_data.session = session;
+ mail_receive_data.data = g_new0(gchar, len + 1);
+ mail_receive_data.data_len = len;
+ memcpy(mail_receive_data.data, data, len);
+
hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
partial_notice = g_strdup_printf("SC-Marked-For-Download: 0\n"
session->msg[session->cur_msg].size);
file = get_tmp_file();
if (pop3_write_msg_to_file(file, mail_receive_data.data,
- strlen(mail_receive_data.data), partial_notice) < 0) {
+ mail_receive_data.data_len,
+ partial_notice) < 0) {
g_free(file);
g_free(mail_receive_data.data);
session->error_val = PS_IOERR;
g_vsnprintf(buf, sizeof(buf) - 2, format, args);
va_end(args);
- if (!strncasecmp(buf, "PASS ", 5))
- log_print("POP3> PASS ********\n");
+ if (!g_ascii_strncasecmp(buf, "PASS ", 5))
+ log_print(LOG_PROTOCOL, "POP> PASS ********\n");
else
- log_print("POP3> %s\n", buf);
+ log_print(LOG_PROTOCOL, "POP> %s\n", buf);
- session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
+ session_send_msg(SESSION(session), buf);
}
Session *pop3_session_new(PrefsAccount *account)
{
Pop3Session *session;
- g_return_val_if_fail(account != NULL, NULL);
+ cm_return_val_if_fail(account != NULL, NULL);
+
+ account->receive_in_progress = TRUE;
session = g_new0(Pop3Session, 1);
- session_init(SESSION(session));
+ session_init(SESSION(session), account, FALSE);
SESSION(session)->type = SESSION_POP3;
SESSION(session)->recv_msg = pop3_session_recv_msg;
SESSION(session)->recv_data_finished = pop3_session_recv_data_finished;
SESSION(session)->send_data_finished = NULL;
-
+ SESSION(session)->ssl_cert_auto_accept = account->ssl_certs_auto_accept;
SESSION(session)->destroy = pop3_session_destroy;
+#ifdef USE_GNUTLS
+ if (account->set_gnutls_priority && account->gnutls_priority &&
+ strlen(account->gnutls_priority) != 0)
+ SESSION(session)->gnutls_priority = g_strdup(account->gnutls_priority);
+#endif
+
session->state = POP3_READY;
session->ac_prefs = account;
session->pop_before_smtp = FALSE;
Pop3Session *pop3_session = POP3_SESSION(session);
gint n;
- g_return_if_fail(session != NULL);
+ cm_return_if_fail(session != NULL);
for (n = 1; n <= pop3_session->count; n++)
g_free(pop3_session->msg[n].uidl);
g_free(pop3_session->user);
g_free(pop3_session->pass);
g_free(pop3_session->error_msg);
+
+ pop3_session->ac_prefs->receive_in_progress = FALSE;
}
-void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
+static void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
{
GHashTable *table;
GHashTable *partial_recv_table;
time_t recv_time;
time_t now;
gint partial_recv;
+ gchar *sanitized_uid = g_strdup(ac_prefs->userid);
+
+ subst_for_filename(sanitized_uid);
table = g_hash_table_new(g_str_hash, g_str_equal);
partial_recv_table = g_hash_table_new(g_str_hash, g_str_equal);
path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
"uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
- "-", ac_prefs->userid, NULL);
- if ((fp = fopen(path, "rb")) == NULL) {
+ "-", sanitized_uid, NULL);
+
+ g_free(sanitized_uid);
+ if ((fp = g_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-", ac_prefs->recv_server,
"-", ac_prefs->userid, NULL);
- if ((fp = fopen(path, "rb")) == NULL) {
+ if ((fp = g_fopen(path, "rb")) == NULL) {
if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
g_free(path);
session->uidl_table = table;
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, (long int *) &recv_time, tmp) < 3) {
+ if (sscanf(buf, "%s\t%ld", uidl, (long int *) &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(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);
-
- 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 = 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");
- 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), 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;
-}
-
-/* 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(MsgInfo *msginfo)
-{
- return pop3_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DELE);
-}
-
-int pop3_mark_for_download(MsgInfo *msginfo)
-{
- return pop3_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DLOAD);
-}
-
-int pop3_unmark(MsgInfo *msginfo)
-{
- return pop3_uidl_mark_mail(msginfo, 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);
-}
+#define TRY(func) \
+if (!(func)) \
+{ \
+ g_warning("failed to write"); \
+ goto err_write; \
+} \
gint pop3_write_uidl_list(Pop3Session *session)
{
- gchar *path;
+ gchar *path, *tmp_path;
FILE *fp;
Pop3MsgInfo *msg;
gint n;
+ gchar *sanitized_uid = g_strdup(session->ac_prefs->userid);
+
+ subst_for_filename(sanitized_uid);
- if (!session->uidl_is_valid) return 0;
+ if (!session->uidl_is_valid) {
+ g_free(sanitized_uid);
+ return 0;
+ }
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, "wb")) == NULL) {
- FILE_OP_ERROR(path, "fopen");
- g_free(path);
- return -1;
+ "-", sanitized_uid, NULL);
+ tmp_path = g_strconcat(path, ".tmp", NULL);
+
+ g_free(sanitized_uid);
+
+ if ((fp = g_fopen(tmp_path, "wb")) == NULL) {
+ FILE_OP_ERROR(tmp_path, "fopen");
+ goto err_write;
}
for (n = 1; n <= session->count; n++) {
msg = &session->msg[n];
- if (msg->uidl && msg->received && !msg->deleted) {
- fprintf(fp, "%s\t%ld\t%d\n",
- msg->uidl, msg->recv_time, msg->partial_recv);
- }
+ if (msg->uidl && msg->received &&
+ (!msg->deleted || session->state != POP3_DONE))
+ TRY(fprintf(fp, "%s\t%ld\t%d\n",
+ msg->uidl, (long int)
+ msg->recv_time,
+ msg->partial_recv)
+ > 0);
}
- if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
+ if (fclose(fp) == EOF) {
+ FILE_OP_ERROR(tmp_path, "fclose");
+ fp = NULL;
+ goto err_write;
+ }
+ fp = NULL;
+#ifdef G_OS_WIN32
+ claws_unlink(path);
+#endif
+ if (g_rename(tmp_path, path) < 0) {
+ FILE_OP_ERROR(path, "rename");
+ goto err_write;
+ }
g_free(path);
-
+ g_free(tmp_path);
return 0;
+err_write:
+ if (fp)
+ fclose(fp);
+ g_free(path);
+ g_free(tmp_path);
+ return -1;
}
+#undef TRY
+
static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
guint len, const gchar *prefix)
{
FILE *fp;
const gchar *prev, *cur;
- g_return_val_if_fail(file != NULL, -1);
+ cm_return_val_if_fail(file != NULL, -1);
- if ((fp = fopen(file, "wb")) == NULL) {
+ if ((fp = g_fopen(file, "wb")) == NULL) {
FILE_OP_ERROR(file, "fopen");
return -1;
}
FILE_OP_ERROR(file, "chmod");
if (prefix != NULL) {
- fprintf(fp, prefix);
- fprintf(fp, "\n");
+ if (fprintf(fp, "%s\n", prefix) < 0) {
+ FILE_OP_ERROR(file, "fprintf");
+ fclose(fp);
+ claws_unlink(file);
+ return -1;
+ }
}
/* +------------------+----------------+--------------------------+ *
* ^data ^prev ^cur data+len-1^ */
prev = data;
- while ((cur = memchr(prev, '\r', len - (prev - data))) != NULL) {
- if ((cur > prev && fwrite(prev, cur - prev, 1, fp) < 1) ||
+ while ((cur = (gchar *)my_memmem(prev, len - (prev - data), "\r\n", 2))
+ != NULL) {
+ if ((cur > prev && fwrite(prev, 1, cur - prev, fp) < 1) ||
fputc('\n', fp) == EOF) {
FILE_OP_ERROR(file, "fwrite");
- g_warning("can't write to file: %s\n", file);
+ g_warning("can't write to file: %s", file);
fclose(fp);
- unlink(file);
+ claws_unlink(file);
return -1;
}
}
if (prev - data < len &&
- fwrite(prev, len - (prev - data), 1, fp) < 1) {
+ fwrite(prev, 1, len - (prev - data), fp) < 1) {
FILE_OP_ERROR(file, "fwrite");
- g_warning("can't write to file: %s\n", file);
+ g_warning("can't write to file: %s", file);
fclose(fp);
- unlink(file);
+ claws_unlink(file);
return -1;
}
if (data[len - 1] != '\r' && data[len - 1] != '\n') {
if (fputc('\n', fp) == EOF) {
FILE_OP_ERROR(file, "fputc");
- g_warning("can't write to file: %s\n", file);
+ g_warning("can't write to file: %s", file);
fclose(fp);
- unlink(file);
+ claws_unlink(file);
return -1;
}
}
if (fclose(fp) == EOF) {
FILE_OP_ERROR(file, "fclose");
- unlink(file);
+ claws_unlink(file);
return -1;
}
msg->recv_time != RECV_TIME_KEEP &&
msg->partial_recv == POP3_TOTALLY_RECEIVED &&
session->current_time - msg->recv_time >=
- ac->msg_leave_time * 24 * 60 * 60) {
- log_message
- (_("POP3: Deleting expired message "
- "%d\n"), session->cur_msg);
+ ((ac->msg_leave_time * 24 * 60 * 60) +
+ (ac->msg_leave_hour * 60 * 60))) {
+ log_message(LOG_PROTOCOL,
+ _("POP: Deleting expired message %d [%s]\n"),
+ session->cur_msg, msg->uidl?msg->uidl:" ");
+ session->cur_total_bytes += size;
pop3_delete_send(session);
return POP3_DELETE;
}
} else if (msg->partial_recv == POP3_MUST_COMPLETE_RECV)
break;
- log_message
- (_("POP3: Skipping message %d (%d bytes)\n"),
- session->cur_msg, size);
+ log_message(LOG_PROTOCOL,
+ _("POP: Skipping message %d [%s] (%d bytes)\n"),
+ session->cur_msg, msg->uidl?msg->uidl:" ", size);
}
if (size == 0 || msg->received || size_limit_over) {
{
Pop3ErrorValue ok;
- log_print("POP3< %s\n", msg);
+ log_print(LOG_PROTOCOL, "POP< %s\n", msg);
if (!strncmp(msg, "+OK", 3))
ok = PS_SUCCESS;
strstr(msg + 4, "Lock") ||
strstr(msg + 4, "LOCK") ||
strstr(msg + 4, "wait")) {
- log_warning(_("mailbox is locked\n"));
+ log_error(LOG_PROTOCOL, _("mailbox is locked\n"));
ok = PS_LOCKBUSY;
} else if (strcasestr(msg + 4, "timeout")) {
- log_warning(_("session timeout\n"));
+ log_error(LOG_PROTOCOL, _("Session timeout\n"));
ok = PS_ERROR;
} else {
switch (session->state) {
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
case POP3_STLS:
- log_warning(_("can't start TLS session\n"));
+ log_error(LOG_PROTOCOL, _("couldn't start STARTTLS 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(LOG_PROTOCOL, _("error occurred on authentication\n"));
ok = PS_AUTHFAIL;
break;
case POP3_GETRANGE_LAST:
case POP3_GETRANGE_UIDL:
case POP3_TOP:
- log_warning(_("command not supported\n"));
+ log_warning(LOG_PROTOCOL, _("command not supported\n"));
ok = PS_NOTSUPPORTED;
break;
default:
- log_warning(_("error occurred on POP3 session\n"));
+ log_error(LOG_PROTOCOL, _("error occurred on POP session\n"));
ok = PS_ERROR;
}
}
g_free(session->error_msg);
session->error_msg = g_strdup(msg);
- fprintf(stderr, "POP3: %s\n", msg);
+ g_printerr("POP: %s\n", msg);
} else
ok = PS_PROTOCOL;
{
Pop3Session *pop3_session = POP3_SESSION(session);
Pop3ErrorValue val = PS_SUCCESS;
- const guchar *body;
+ const gchar *body;
body = msg;
if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
if (*body == '+' || *body == '-')
body++;
- while (isalpha(*body))
+ while (g_ascii_isalpha(*body))
body++;
- while (isspace(*body))
+ while (g_ascii_isspace(*body))
body++;
}
case POP3_READY:
case POP3_GREETING:
pop3_greeting_recv(pop3_session, body);
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
- pop3_stls_send(pop3_session);
+ val = pop3_stls_send(pop3_session);
else
#endif
- if (pop3_session->ac_prefs->protocol == A_APOP)
- pop3_getauth_apop_send(pop3_session);
+ if (pop3_session->ac_prefs->use_apop_auth)
+ val = pop3_getauth_apop_send(pop3_session);
else
- pop3_getauth_user_send(pop3_session);
+ val = pop3_getauth_user_send(pop3_session);
break;
-#if USE_OPENSSL
+#ifdef USE_GNUTLS
case POP3_STLS:
if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
return -1;
- if (pop3_session->ac_prefs->protocol == A_APOP)
- pop3_getauth_apop_send(pop3_session);
+ if (pop3_session->ac_prefs->use_apop_auth)
+ val = pop3_getauth_apop_send(pop3_session);
else
- pop3_getauth_user_send(pop3_session);
+ val = pop3_getauth_user_send(pop3_session);
break;
#endif
case POP3_GETAUTH_USER:
- pop3_getauth_pass_send(pop3_session);
+ val = pop3_getauth_pass_send(pop3_session);
break;
case POP3_GETAUTH_PASS:
case POP3_GETAUTH_APOP:
if (!pop3_session->pop_before_smtp)
- pop3_getrange_stat_send(pop3_session);
+ val = pop3_getrange_stat_send(pop3_session);
else
- pop3_logout_send(pop3_session);
+ val = pop3_logout_send(pop3_session);
break;
case POP3_GETRANGE_STAT:
if (pop3_getrange_stat_recv(pop3_session, body) < 0)
return -1;
if (pop3_session->count > 0)
- pop3_getrange_uidl_send(pop3_session);
+ val = pop3_getrange_uidl_send(pop3_session);
else
- pop3_logout_send(pop3_session);
+ val = pop3_logout_send(pop3_session);
break;
case POP3_GETRANGE_LAST:
if (val == PS_NOTSUPPORTED)
else if (pop3_getrange_last_recv(pop3_session, body) < 0)
return -1;
if (pop3_session->cur_msg > 0)
- pop3_getsize_list_send(pop3_session);
+ val = pop3_getsize_list_send(pop3_session);
else
- pop3_logout_send(pop3_session);
+ val = pop3_logout_send(pop3_session);
break;
case POP3_GETRANGE_UIDL:
if (val == PS_NOTSUPPORTED) {
pop3_session->error_val = PS_SUCCESS;
- pop3_getrange_last_send(pop3_session);
+ val = pop3_getrange_last_send(pop3_session);
} else {
pop3_session->state = POP3_GETRANGE_UIDL_RECV;
session_recv_data(session, 0, ".\r\n");
case POP3_DELETE:
pop3_delete_recv(pop3_session);
if (pop3_session->cur_msg == pop3_session->count)
- pop3_logout_send(pop3_session);
+ val = pop3_logout_send(pop3_session);
else {
pop3_session->cur_msg++;
if (pop3_lookup_next(pop3_session) == POP3_ERROR)
}
break;
case POP3_LOGOUT:
+ pop3_session->state = POP3_DONE;
session_disconnect(session);
break;
case POP3_ERROR:
return -1;
}
- return 0;
+ return val == PS_SUCCESS?0:-1;
}
static gint pop3_session_recv_data_finished(Session *session, guchar *data,
if (pop3_session->ac_prefs->rmmail &&
pop3_session->ac_prefs->msg_leave_time == 0 &&
+ pop3_session->ac_prefs->msg_leave_hour == 0 &&
pop3_session->msg[pop3_session->cur_msg].recv_time
!= RECV_TIME_KEEP)
pop3_delete_send(pop3_session);
}
break;
case POP3_TOP:
- log_warning(_("TOP command unsupported\n"));
+ log_warning(LOG_PROTOCOL, _("TOP command unsupported\n"));
if (pop3_session->cur_msg == pop3_session->count)
pop3_logout_send(pop3_session);
else {