2007-04-13 [colin] 2.8.1cvs95
[claws.git] / src / pop.c
index 5c15b477f054fb7315e56e8cf85440dd15dc1d85..5ce354defa7a386c9f5e9ecad738643e53aa5d11 100644 (file)
--- a/src/pop.c
+++ b/src/pop.c
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2007 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
@@ -14,7 +14,7 @@
  *
  * 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -22,6 +22,7 @@
 #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 "inc.h"
 #include "recv.h"
-
+#include "partial_download.h"
 #include "log.h"
 #include "hooks.h"
 
@@ -58,10 +57,12 @@ static gint pop3_getrange_last_recv (Pop3Session *session,
                                         const gchar *msg);
 static gint pop3_getrange_uidl_send    (Pop3Session *session);
 static gint pop3_getrange_uidl_recv    (Pop3Session *session,
-                                        const gchar *msg);
+                                        const gchar *data,
+                                        guint        len);
 static gint pop3_getsize_list_send     (Pop3Session *session);
 static gint pop3_getsize_list_recv     (Pop3Session *session,
-                                        const gchar *msg);
+                                        const gchar *data,
+                                        guint        len);
 static gint pop3_retr_send             (Pop3Session *session);
 static gint pop3_retr_recv             (Pop3Session *session,
                                         const gchar *data,
@@ -77,7 +78,8 @@ static void pop3_session_destroy      (Session        *session);
 
 static gint pop3_write_msg_to_file     (const gchar    *file,
                                         const gchar    *data,
-                                        guint           len);
+                                        guint           len,
+                                        const gchar    *prefix);
 
 static Pop3State pop3_lookup_next      (Pop3Session    *session);
 static Pop3ErrorValue pop3_ok          (Pop3Session    *session,
@@ -89,7 +91,6 @@ static gint pop3_session_recv_data_finished   (Session        *session,
                                                 guchar         *data,
                                                 guint           len);
 
-
 static gint pop3_greeting_recv(Pop3Session *session, const gchar *msg)
 {
        session->state = POP3_GREETING;
@@ -146,14 +147,14 @@ static gint pop3_getauth_apop_send(Pop3Session *session)
        session->state = POP3_GETAUTH_APOP;
 
        if ((start = strchr(session->greeting, '<')) == NULL) {
-               log_warning(_("Required APOP timestamp not found "
-                             "in greeting\n"));
+               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;
        }
@@ -179,7 +180,7 @@ static gint pop3_getrange_stat_send(Pop3Session *session)
 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, _("POP3 protocol error\n"));
                session->error_val = PS_PROTOCOL;
                return -1;
        } else {
@@ -206,13 +207,14 @@ static gint pop3_getrange_last_recv(Pop3Session *session, const gchar *msg)
        gint last;
 
        if (sscanf(msg, "%d", &last) == 0) {
-               log_warning(_("POP3 protocol error\n"));
+               log_warning(LOG_PROTOCOL, _("POP3 protocol error\n"));
                session->error_val = PS_PROTOCOL;
                return -1;
        } else {
-               if (session->count > last)
+               if (session->count > last) {
+                       session->new_msg_exist = TRUE;
                        session->cur_msg = last + 1;
-               else
+               else
                        session->cur_msg = 0;
        }
 
@@ -226,41 +228,68 @@ static gint pop3_getrange_uidl_send(Pop3Session *session)
        return PS_SUCCESS;
 }
 
-static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *msg)
+static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *data,
+                                   guint len)
 {
        gchar id[IDLEN + 1];
-       gint num;
+       gchar buf[POPBUFSIZE];
+       gint buf_len;
+       guint32 num;
        time_t recv_time;
+       gint partial_recv;
+       const gchar *p = data;
+       const gchar *lastp = data + len;
+       const gchar *newline;
 
-       if (msg[0] == '.') {
-               session->uidl_is_valid = TRUE;
-               return PS_SUCCESS;
-       }
+       while (p < lastp) {
+               if ((newline = memchr(p, '\r', lastp - p)) == NULL)
+                       return -1;
+               buf_len = MIN(newline - p, sizeof(buf) - 1);
+               memcpy(buf, p, buf_len);
+               buf[buf_len] = '\0';
 
-       if (sscanf(msg, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
-               return -1;
-       if (num <= 0 || num > session->count)
-               return -1;
+               p = newline + 1;
+               if (p < lastp && *p == '\n') p++;
 
-       session->msg[num].uidl = g_strdup(id);
+               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;
+               }
 
-       if (!session->uidl_table)
-               return PS_CONTINUE;
+               session->msg[num].uidl = g_strdup(id);
 
-       recv_time = (time_t)g_hash_table_lookup(session->uidl_table, id);
-       session->msg[num].recv_time = recv_time;
+               recv_time = (time_t)g_hash_table_lookup(
+                                       session->uidl_table, id);
+               session->msg[num].recv_time = recv_time;
 
-       if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE)
-               session->msg[num].received = TRUE;
+               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)g_hash_table_lookup(
+                                       session->partial_recv_table, id);
 
-       if (!session->new_msg_exist &&
-           (session->ac_prefs->getall || recv_time == RECV_TIME_NONE ||
-            session->ac_prefs->rmmail)) {
-               session->cur_msg = num;
-               session->new_msg_exist = TRUE;
+               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 (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 ||
+                    session->ac_prefs->rmmail)) {
+                       session->cur_msg = num;
+                       session->new_msg_exist = TRUE;
+               }
        }
 
-       return PS_CONTINUE;
+       session->uidl_is_valid = TRUE;
+       return PS_SUCCESS;
 }
 
 static gint pop3_getsize_list_send(Pop3Session *session)
@@ -270,29 +299,46 @@ static gint pop3_getsize_list_send(Pop3Session *session)
        return PS_SUCCESS;
 }
 
-static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *msg)
+static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *data,
+                                  guint len)
 {
+       gchar buf[POPBUFSIZE];
+       gint buf_len;
        guint num, size;
+       const gchar *p = data;
+       const gchar *lastp = data + len;
+       const gchar *newline;
 
-       if (msg[0] == '.')
-               return PS_SUCCESS;
+       while (p < lastp) {
+               if ((newline = memchr(p, '\r', lastp - p)) == NULL)
+                       return -1;
+               buf_len = MIN(newline - p, sizeof(buf) - 1);
+               memcpy(buf, p, buf_len);
+               buf[buf_len] = '\0';
 
-       if (sscanf(msg, "%u %u", &num, &size) != 2) {
-               session->error_val = PS_PROTOCOL;
-               return -1;
-       }
+               p = newline + 1;
+               if (p < lastp && *p == '\n') p++;
 
-       if (num > 0 && num <= session->count)
-               session->msg[num].size = size;
-       if (num > 0 && num < session->cur_msg)
-               session->cur_total_bytes += size;
+               if (sscanf(buf, "%u %u", &num, &size) != 2) {
+                       session->error_val = PS_PROTOCOL;
+                       return -1;
+               }
+
+               if (num > 0 && num <= session->count)
+                       session->msg[num].size = size;
+               if (num > 0 && num < session->cur_msg)
+                       session->cur_total_bytes += size;
+       }
 
-       return PS_CONTINUE;
+       return PS_SUCCESS;
 }
 
 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;
 }
@@ -303,22 +349,108 @@ static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
        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, 
+                                  mail_receive_data.data_len, NULL) < 0) {
+               g_free(file);
+               g_free(mail_receive_data.data);
+               session->error_val = PS_IOERR;
+               return -1;
+       }
+       g_free(mail_receive_data.data);
+
+       if (session->msg[session->cur_msg].partial_recv 
+           == POP3_MUST_COMPLETE_RECV) {
+               gchar *old_file = partial_get_filename(
+                               session->ac_prefs->recv_server,
+                               session->ac_prefs->userid,
+                               session->msg[session->cur_msg].uidl);
+               
+               if (old_file) {
+                       partial_delete_old(old_file);
+                       g_free(old_file);
+               }
+       } 
+
+       /* drop_ok: 0: success 1: don't receive -1: error */
+       drop_ok = session->drop_message(session, file);
+
+       g_free(file);
+       if (drop_ok < 0) {
+               session->error_val = PS_IOERR;
+               return -1;
+       }
+       
+       session->cur_total_bytes += session->msg[session->cur_msg].size;
+       session->cur_total_recv_bytes += session->msg[session->cur_msg].size;
+       session->cur_total_num++;
+
+       session->msg[session->cur_msg].received = TRUE;
+       session->msg[session->cur_msg].partial_recv = POP3_TOTALLY_RECEIVED;
+
+       session->msg[session->cur_msg].recv_time =
+               drop_ok == 1 ? RECV_TIME_KEEP : session->current_time;
+
+       return PS_SUCCESS;
+}
+
+static gint pop3_top_send(Pop3Session *session, gint max_size)
+{
+       gint num_lines = (max_size*1024)/82; /* consider lines to be 80 chars */
+       session->state = POP3_TOP;
+       pop3_gen_send(session, "TOP %d %d", session->cur_msg, num_lines);
+       return PS_SUCCESS;
+}
+
+static gint pop3_top_recv(Pop3Session *session, const gchar *data, guint len)
+{
+       gchar *file;
+       gint drop_ok;
+       MailReceiveData mail_receive_data;
+       gchar *partial_notice = NULL;
+       
+       /* 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"
+                                        "SC-Partially-Retrieved: %s\n"
+                                        "SC-Account-Server: %s\n"
+                                        "SC-Account-Login: %s\n"
+                                        "SC-Message-Size: %d",
+                                        session->msg[session->cur_msg].uidl,
+                                        session->ac_prefs->recv_server,
+                                        session->ac_prefs->userid,
+                                        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)) < 0) {
+                                  mail_receive_data.data_len,  
+                                  partial_notice) < 0) {
                g_free(file);
                g_free(mail_receive_data.data);
                session->error_val = PS_IOERR;
+               g_free(partial_notice);
                return -1;
        }
        g_free(mail_receive_data.data);
+       g_free(partial_notice);
 
        /* drop_ok: 0: success 1: don't receive -1: error */
-       drop_ok = inc_drop_message(file, session);
+       drop_ok = session->drop_message(session, file);
        g_free(file);
        if (drop_ok < 0) {
                session->error_val = PS_IOERR;
@@ -330,6 +462,7 @@ static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
        session->cur_total_num++;
 
        session->msg[session->cur_msg].received = TRUE;
+       session->msg[session->cur_msg].partial_recv = POP3_PARTIALLY_RECEIVED;
        session->msg[session->cur_msg].recv_time =
                drop_ok == 1 ? RECV_TIME_KEEP : session->current_time;
 
@@ -365,10 +498,10 @@ static void pop3_gen_send(Pop3Session *session, const gchar *format, ...)
        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, "POP3> PASS ********\n");
        else
-               log_print("POP3> %s\n", buf);
+               log_print(LOG_PROTOCOL, "POP3> %s\n", buf);
 
        session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
 }
@@ -381,12 +514,9 @@ Session *pop3_session_new(PrefsAccount *account)
 
        session = g_new0(Pop3Session, 1);
 
+       session_init(SESSION(session));
+
        SESSION(session)->type = SESSION_POP3;
-       SESSION(session)->server = NULL;
-       SESSION(session)->port = 0;
-       SESSION(session)->sock = NULL;
-       SESSION(session)->state = SESSION_READY;
-       SESSION(session)->data = NULL;
 
        SESSION(session)->recv_msg = pop3_session_recv_msg;
        SESSION(session)->recv_data_finished = pop3_session_recv_data_finished;
@@ -396,7 +526,8 @@ Session *pop3_session_new(PrefsAccount *account)
 
        session->state = POP3_READY;
        session->ac_prefs = account;
-       session->uidl_table = pop3_get_uidl_table(account);
+       session->pop_before_smtp = FALSE;
+       pop3_get_uidl_table(account, session);
        session->current_time = time(NULL);
        session->error_val = PS_SUCCESS;
        session->error_msg = NULL;
@@ -420,37 +551,52 @@ static void pop3_session_destroy(Session *session)
                g_hash_table_destroy(pop3_session->uidl_table);
        }
 
+       if (pop3_session->partial_recv_table) {
+               hash_free_strings(pop3_session->partial_recv_table);
+               g_hash_table_destroy(pop3_session->partial_recv_table);
+       }
+
        g_free(pop3_session->greeting);
        g_free(pop3_session->user);
        g_free(pop3_session->pass);
        g_free(pop3_session->error_msg);
 }
 
-GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
+void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
 {
        GHashTable *table;
+       GHashTable *partial_recv_table;
        gchar *path;
        FILE *fp;
        gchar buf[POPBUFSIZE];
        gchar uidl[POPBUFSIZE];
        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);
-                       return table;
+                       session->uidl_table = table;
+                       session->partial_recv_table = partial_recv_table;
+                       return;
                }
        }
        g_free(path);
@@ -458,64 +604,123 @@ GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
        now = time(NULL);
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
+               gchar tmp[POPBUFSIZE];
                strretchomp(buf);
                recv_time = RECV_TIME_NONE;
-               if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
-                       if (sscanf(buf, "%s", uidl) != 1)
-                               continue;
-                       else
-                               recv_time = now;
+               partial_recv = POP3_TOTALLY_RECEIVED;
+               
+               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),
                                    GINT_TO_POINTER(recv_time));
+               if (strlen(tmp) == 1)
+                       partial_recv = atoi(tmp); /* totally received ?*/
+               else
+                       partial_recv = POP3_MUST_COMPLETE_RECV;
+
+               g_hash_table_insert(partial_recv_table, g_strdup(uidl),
+                                   GINT_TO_POINTER(partial_recv));
        }
 
        fclose(fp);
-       return table;
+       session->uidl_table = table;
+       session->partial_recv_table = partial_recv_table;
+       
+       return;
 }
 
+#define TRY(func) \
+if (!(func)) \
+{ \
+       g_warning("failed to write\n"); \
+       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;
 
        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\n", msg->uidl, msg->recv_time);
+               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
+       g_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)
+                                  guint len, const gchar *prefix)
 {
        FILE *fp;
        const gchar *prev, *cur;
 
        g_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;
        }
@@ -523,17 +728,23 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
        if (change_file_mode_rw(fp, file) < 0)
                FILE_OP_ERROR(file, "chmod");
 
+       if (prefix != NULL) {
+               fprintf(fp, "%s", prefix);
+               fprintf(fp, "\n");
+       }
+       
        /* +------------------+----------------+--------------------------+ *
         * ^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);
                        fclose(fp);
-                       unlink(file);
+                       g_unlink(file);
                        return -1;
                }
 
@@ -547,16 +758,19 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
                else
                        prev = cur + 1;
 
+               if (prev - data < len - 1 && *prev == '.' && *(prev + 1) == '.')
+                       prev++;
+
                if (prev - data >= len)
                        break;
        }
 
        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);
                fclose(fp);
-               unlink(file);
+               g_unlink(file);
                return -1;
        }
        if (data[len - 1] != '\r' && data[len - 1] != '\n') {
@@ -564,14 +778,14 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
                        FILE_OP_ERROR(file, "fputc");
                        g_warning("can't write to file: %s\n", file);
                        fclose(fp);
-                       unlink(file);
+                       g_unlink(file);
                        return -1;
                }
        }
 
        if (fclose(fp) == EOF) {
                FILE_OP_ERROR(file, "fclose");
-               unlink(file);
+               g_unlink(file);
                return -1;
        }
 
@@ -596,19 +810,30 @@ static Pop3State pop3_lookup_next(Pop3Session *session)
                if (ac->rmmail &&
                    msg->recv_time != RECV_TIME_NONE &&
                    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_print(_("POP3: Deleting expired message %d\n"),
-                                 session->cur_msg);
+                       log_message(LOG_PROTOCOL, 
+                                       _("POP3: 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;
                }
 
-               if (size_limit_over)
-                       log_print
-                               (_("POP3: Skipping message %d (%d bytes)\n"),
-                                 session->cur_msg, size);
+               if (size_limit_over) {
+                       if (!msg->received && msg->partial_recv != 
+                           POP3_MUST_COMPLETE_RECV) {
+                               pop3_top_send(session, ac->size_limit);
+                               return POP3_TOP;
+                       } else if (msg->partial_recv == POP3_MUST_COMPLETE_RECV)
+                               break;
 
+                       log_message(LOG_PROTOCOL, 
+                                       _("POP3: Skipping message %d [%s] (%d bytes)\n"),
+                                       session->cur_msg, msg->uidl?msg->uidl:" ", size);
+               }
+               
                if (size == 0 || msg->received || size_limit_over) {
                        session->cur_total_bytes += size;
                        if (session->cur_msg == session->count) {
@@ -628,7 +853,7 @@ static Pop3ErrorValue pop3_ok(Pop3Session *session, const gchar *msg)
 {
        Pop3ErrorValue ok;
 
-       log_print("POP3< %s\n", msg);
+       log_print(LOG_PROTOCOL, "POP3< %s\n", msg);
 
        if (!strncmp(msg, "+OK", 3))
                ok = PS_SUCCESS;
@@ -637,32 +862,34 @@ static Pop3ErrorValue pop3_ok(Pop3Session *session, const gchar *msg)
                    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
                        case POP3_STLS:
-                               log_warning(_("can't start TLS session\n"));
+                               log_error(LOG_PROTOCOL, _("couldn'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(LOG_PROTOCOL, _("error occurred on authentication\n"));
                                ok = PS_AUTHFAIL;
                                break;
                        case POP3_GETRANGE_LAST:
                        case POP3_GETRANGE_UIDL:
-                               log_warning(_("command not supported\n"));
+                       case POP3_TOP:
+                               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 POP3 session\n"));
                                ok = PS_ERROR;
                        }
                }
@@ -696,9 +923,9 @@ static gint pop3_session_recv_msg(Session *session, const gchar *msg)
 
                if (*body == '+' || *body == '-')
                        body++;
-               while (isalpha(*body))
+               while (g_ascii_isalpha(*body))
                        body++;
-               while (isspace(*body))
+               while (g_ascii_isspace(*body))
                        body++;
        }
 
@@ -711,7 +938,7 @@ static gint pop3_session_recv_msg(Session *session, const gchar *msg)
                        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);
@@ -720,7 +947,7 @@ static gint pop3_session_recv_msg(Session *session, const gchar *msg)
        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);
@@ -731,7 +958,10 @@ static gint pop3_session_recv_msg(Session *session, const gchar *msg)
                break;
        case POP3_GETAUTH_PASS:
        case POP3_GETAUTH_APOP:
-               pop3_getrange_stat_send(pop3_session);
+               if (!pop3_session->pop_before_smtp)
+                       pop3_getrange_stat_send(pop3_session);
+               else
+                       pop3_logout_send(pop3_session);
                break;
        case POP3_GETRANGE_STAT:
                if (pop3_getrange_stat_recv(pop3_session, body) < 0)
@@ -757,39 +987,24 @@ static gint pop3_session_recv_msg(Session *session, const gchar *msg)
                        pop3_getrange_last_send(pop3_session);
                } else {
                        pop3_session->state = POP3_GETRANGE_UIDL_RECV;
-                       return 1;
+                       session_recv_data(session, 0, ".\r\n");
                }
                break;
-       case POP3_GETRANGE_UIDL_RECV:
-               val = pop3_getrange_uidl_recv(pop3_session, body);
-               if (val == PS_CONTINUE)
-                       return 1;
-               else if (val == PS_SUCCESS) {
-                       if (pop3_session->new_msg_exist)
-                               pop3_getsize_list_send(pop3_session);
-                       else
-                               pop3_logout_send(pop3_session);
-               } else
-                       return -1;
-               break;
        case POP3_GETSIZE_LIST:
                pop3_session->state = POP3_GETSIZE_LIST_RECV;
-               return 1;
-       case POP3_GETSIZE_LIST_RECV:
-               val = pop3_getsize_list_recv(pop3_session, body);
-               if (val == PS_CONTINUE)
-                       return 1;
-               else if (val == PS_SUCCESS) {
-                       if (pop3_lookup_next(pop3_session) == POP3_ERROR)
-                               return -1;
-               } else
-                       return -1;
+               session_recv_data(session, 0, ".\r\n");
                break;
        case POP3_RETR:
                pop3_session->state = POP3_RETR_RECV;
-               session_recv_data
-                       (session,
-                        pop3_session->msg[pop3_session->cur_msg].size, TRUE);
+               session_recv_data(session, 0, ".\r\n");
+               break;
+       case POP3_TOP:
+               if (val == PS_NOTSUPPORTED) {
+                       pop3_session->error_val = PS_SUCCESS;
+               } else {
+                       pop3_session->state = POP3_TOP_RECV;
+                       session_recv_data(session, 0, ".\r\n");
+               }
                break;
        case POP3_DELETE:
                pop3_delete_recv(pop3_session);
@@ -802,6 +1017,7 @@ static gint pop3_session_recv_msg(Session *session, const gchar *msg)
                }
                break;
        case POP3_LOGOUT:
+               pop3_session->state = POP3_DONE;
                session_disconnect(session);
                break;
        case POP3_ERROR:
@@ -816,24 +1032,69 @@ static gint pop3_session_recv_data_finished(Session *session, guchar *data,
                                            guint len)
 {
        Pop3Session *pop3_session = POP3_SESSION(session);
+       Pop3ErrorValue val = PS_SUCCESS;
 
-       if (len == 0)
-               return -1;
-
-       if (pop3_retr_recv(pop3_session, data, len) < 0)
-               return -1;
+       switch (pop3_session->state) {
+       case POP3_GETRANGE_UIDL_RECV:
+               val = pop3_getrange_uidl_recv(pop3_session, data, len);
+               if (val == PS_SUCCESS) {
+                       if (pop3_session->new_msg_exist)
+                               pop3_getsize_list_send(pop3_session);
+                       else
+                               pop3_logout_send(pop3_session);
+               } else
+                       return -1;
+               break;
+       case POP3_GETSIZE_LIST_RECV:
+               val = pop3_getsize_list_recv(pop3_session, data, len);
+               if (val == PS_SUCCESS) {
+                       if (pop3_lookup_next(pop3_session) == POP3_ERROR)
+                               return -1;
+               } else
+                       return -1;
+               break;
+       case POP3_RETR_RECV:
+               if (pop3_retr_recv(pop3_session, data, len) < 0)
+                       return -1;
 
-       if (pop3_session->ac_prefs->rmmail &&
-           pop3_session->ac_prefs->msg_leave_time == 0 &&
-           pop3_session->msg[pop3_session->cur_msg].recv_time
-           != RECV_TIME_KEEP)
-               pop3_delete_send(pop3_session);
-       else if (pop3_session->cur_msg == pop3_session->count)
-               pop3_logout_send(pop3_session);
-       else {
-               pop3_session->cur_msg++;
-               if (pop3_lookup_next(pop3_session) == POP3_ERROR)
+               if (pop3_session->ac_prefs->rmmail &&
+                   pop3_session->ac_prefs->msg_leave_time == 0 &&
+                   pop3_session->msg[pop3_session->cur_msg].recv_time
+                   != RECV_TIME_KEEP)
+                       pop3_delete_send(pop3_session);
+               else if (pop3_session->cur_msg == pop3_session->count)
+                       pop3_logout_send(pop3_session);
+               else {
+                       pop3_session->cur_msg++;
+                       if (pop3_lookup_next(pop3_session) == POP3_ERROR)
+                               return -1;
+               }
+               break;
+       case POP3_TOP_RECV:
+               if (pop3_top_recv(pop3_session, data, len) < 0)
                        return -1;
+
+               if (pop3_session->cur_msg == pop3_session->count)
+                       pop3_logout_send(pop3_session);
+               else {
+                       pop3_session->cur_msg++;
+                       if (pop3_lookup_next(pop3_session) == POP3_ERROR)
+                               return -1;
+               }
+               break;
+       case POP3_TOP:
+               log_warning(LOG_PROTOCOL, _("TOP command unsupported\n"));
+               if (pop3_session->cur_msg == pop3_session->count)
+                       pop3_logout_send(pop3_session);
+               else {
+                       pop3_session->cur_msg++;
+                       if (pop3_lookup_next(pop3_session) == POP3_ERROR)
+                               return -1;
+               }
+               break;
+       case POP3_ERROR:
+       default:
+               return -1;
        }
 
        return 0;