0.8.6claws75
[claws.git] / src / pop.c
index b672504627a0291a9445282d4b65129e4a93b807..8738bb438e6e2984310e5d33148bb4f67dc2d7da 100644 (file)
--- a/src/pop.c
+++ b/src/pop.c
@@ -27,6 +27,8 @@
 #include <stdarg.h>
 #include <ctype.h>
 #include <unistd.h>
+#include <time.h>
+#include <errno.h>
 
 #include "intl.h"
 #include "pop.h"
 #include "inc.h"
 #include "recv.h"
 #include "selective_download.h"
+#include "log.h"
+#if USE_OPENSSL
+#  include "ssl.h"
+#endif
 
-#define LOOKUP_NEXT_MSG() \
-       for (;;) { \
-               gint size = state->sizes[state->cur_msg]; \
- \
-               if (size == 0 || \
-                   (state->ac_prefs->enable_size_limit && \
-                    state->ac_prefs->size_limit > 0 && \
-                    size > state->ac_prefs->size_limit * 1024)) { \
-                       log_print(_("Skipping message %d\n"), state->cur_msg); \
-                       if (size > 0) { \
-                               state->cur_total_bytes += size; \
-                               state->cur_total_num++; \
-                               if (state->new_id_list) { \
-                                       state->id_list = g_slist_append(state->id_list, state->new_id_list->data); \
-                                       state->new_id_list = g_slist_remove(state->new_id_list, state->new_id_list->data); \
-                               } \
-                       } \
-                       if (state->cur_msg == state->count) \
-                               return POP3_LOGOUT_SEND; \
-                       else \
-                               state->cur_msg++; \
-               } else \
-                       break; \
-       }
+#define LOOKUP_NEXT_MSG()                                                      \
+{                                                                              \
+       Pop3MsgInfo *msg;                                                       \
+       PrefsAccount *ac = state->ac_prefs;                                     \
+       gint size;                                                              \
+       gboolean size_limit_over;                                               \
+                                                                               \
+       for (;;) {                                                              \
+               msg = &state->msg[state->cur_msg];                              \
+               size = msg->size;                                               \
+               size_limit_over =                                               \
+                   (ac->enable_size_limit &&                                   \
+                    ac->size_limit > 0 &&                                      \
+                    size > ac->size_limit * 1024);                             \
+                                                                               \
+               if (ac->rmmail &&                                               \
+                   msg->recv_time != RECV_TIME_NONE &&                         \
+                   msg->recv_time != RECV_TIME_KEEP &&                         \
+                   state->current_time - msg->recv_time >=                     \
+                   ac->msg_leave_time * 24 * 60 * 60) {                        \
+                       log_print(_("POP3: Deleting expired message %d\n"),     \
+                                 state->cur_msg);                              \
+                       return POP3_DELETE_SEND;                                \
+               }                                                               \
+                                                                               \
+               if (size_limit_over)                                            \
+                       log_print(_("POP3: Skipping message %d (%d bytes)\n"),  \
+                                 state->cur_msg, size);                        \
+                                                                               \
+               if (size == 0 || msg->received || size_limit_over) {            \
+                       state->cur_total_bytes += size;                         \
+                       if (state->cur_msg == state->count)                     \
+                               return POP3_LOGOUT_SEND;                        \
+                       else                                                    \
+                               state->cur_msg++;                               \
+               } else                                                          \
+                       break;                                                  \
+       }                                                                       \
+}
 
 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
-static gboolean pop3_delete_header (Pop3State *state);
+static gboolean pop3_sd_get_next (Pop3State *state);
+static void pop3_sd_new_header(Pop3State *state);
+gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state);
 
 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
        gchar buf[POPBUFSIZE];
+       gint ok;
 
-       if (pop3_ok(sock, buf) == PS_SUCCESS) {
-               if (state->ac_prefs->protocol == A_APOP) {
-                       state->greeting = g_strdup(buf);
+       if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
+               state->greeting = g_strdup(buf);
+#if USE_OPENSSL
+               if (state->ac_prefs->ssl_pop == SSL_STARTTLS)
+                       return POP3_STLS_SEND;
+#endif
+               if (state->ac_prefs->protocol == A_APOP)
                        return POP3_GETAUTH_APOP_SEND;
-               else
+               else
                        return POP3_GETAUTH_USER_SEND;
-       } else
+       } else {
+               state->error_val = ok;
+               return -1;
+       }
+}
+
+#if USE_OPENSSL
+gint pop3_stls_send(SockInfo *sock, gpointer data)
+{
+       pop3_gen_send(sock, "STLS");
+
+       return POP3_STLS_RECV;
+}
+
+gint pop3_stls_recv(SockInfo *sock, gpointer data)
+{
+       Pop3State *state = (Pop3State *)data;
+       gint ok;
+
+       if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
+               if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1)) {
+                       state->error_val = PS_SOCKET;
+                       return -1;
+               }
+               if (state->ac_prefs->protocol == A_APOP)
+                       return POP3_GETAUTH_APOP_SEND;
+               else
+                       return POP3_GETAUTH_USER_SEND;
+       } else if (ok == PS_PROTOCOL) {
+               log_warning(_("can't start TLS session\n"));
+               state->error_val = ok;
+               return POP3_LOGOUT_SEND;
+       } else {
+               state->error_val = ok;
                return -1;
+       }
 }
+#endif /* USE_OPENSSL */
 
 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
 {
@@ -89,8 +152,6 @@ gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
 
        g_return_val_if_fail(state->user != NULL, -1);
 
-       inc_progress_update(state, POP3_GETAUTH_USER_SEND);
-
        pop3_gen_send(sock, "USER %s", state->user);
 
        return POP3_GETAUTH_USER_RECV;
@@ -105,7 +166,6 @@ gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
        else {
                log_warning(_("error occurred on authentication\n"));
                state->error_val = PS_AUTHFAIL;
-               state->inc_state = INC_AUTH_FAILED;
                return -1;
        }
 }
@@ -124,18 +184,17 @@ gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
+       gint ok;
 
-       if (pop3_ok(sock, NULL) == PS_SUCCESS) {
-               
-               if (pop3_delete_header(state) == TRUE)
-                       return POP3_DELETE_SEND;
-               else
-                       return POP3_GETRANGE_STAT_SEND;
-       }
-       else {
+       if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS)
+               return POP3_GETRANGE_STAT_SEND;
+       else if (ok == PS_LOCKBUSY) {
+               log_warning(_("mailbox is locked\n"));
+               state->error_val = ok;
+               return -1;
+       } else {
                log_warning(_("error occurred on authentication\n"));
                state->error_val = PS_AUTHFAIL;
-               state->inc_state = INC_AUTH_FAILED;
                return -1;
        }
 }
@@ -150,16 +209,16 @@ gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
        g_return_val_if_fail(state->user != NULL, -1);
        g_return_val_if_fail(state->pass != NULL, -1);
 
-       inc_progress_update(state, POP3_GETAUTH_APOP_SEND);
-
        if ((start = strchr(state->greeting, '<')) == NULL) {
                log_warning(_("Required APOP timestamp not found "
                              "in greeting\n"));
+               state->error_val = PS_PROTOCOL;
                return -1;
        }
 
        if ((end = strchr(start, '>')) == NULL || end == start + 1) {
                log_warning(_("Timestamp syntax error in greeting\n"));
+               state->error_val = PS_PROTOCOL;
                return -1;
        }
 
@@ -177,29 +236,23 @@ gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
+       gint ok;
 
-       if (pop3_ok(sock, NULL) == PS_SUCCESS) {
-               
-               if (pop3_delete_header(state) == TRUE)
-                       return POP3_DELETE_SEND;
-               else
+       if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS)
                return POP3_GETRANGE_STAT_SEND;
-       }
-
-       else {
+       else if (ok == PS_LOCKBUSY) {
+               log_warning(_("mailbox is locked\n"));
+               state->error_val = ok;
+               return -1;
+       } else {
                log_warning(_("error occurred on authentication\n"));
                state->error_val = PS_AUTHFAIL;
-               state->inc_state = INC_AUTH_FAILED;
                return -1;
        }
 }
 
 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
 {
-       Pop3State *state = (Pop3State *)data;
-
-       inc_progress_update(state, POP3_GETRANGE_STAT_SEND);
-
        pop3_gen_send(sock, "STAT");
 
        return POP3_GETRANGE_STAT_RECV;
@@ -215,32 +268,30 @@ gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
                if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
                    != 2) {
                        log_warning(_("POP3 protocol error\n"));
+                       state->error_val = PS_PROTOCOL;
                        return -1;
                } else {
-                       if (state->count == 0)
+                       if (state->count == 0) {
+                               state->uidl_is_valid = TRUE;
                                return POP3_LOGOUT_SEND;
-                       else {
+                       } else {
+                               state->msg = g_new0
+                                       (Pop3MsgInfo, state->count + 1);
                                state->cur_msg = 1;
-                               state->new = state->count;
-                               if (state->ac_prefs->rmmail ||
-                                   state->ac_prefs->getall)
-                                       return POP3_GETSIZE_LIST_SEND;
-                               else
-                                       return POP3_GETRANGE_UIDL_SEND;
+                               return POP3_GETRANGE_UIDL_SEND;
                        }
                }
-       } else if (ok == PS_PROTOCOL)
+       } else if (ok == PS_PROTOCOL) {
+               state->error_val = ok;
                return POP3_LOGOUT_SEND;
-       else
+       } else {
+               state->error_val = ok;
                return -1;
+       }
 }
 
 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
 {
-       Pop3State *state = (Pop3State *)data;
-
-       inc_progress_update(state, POP3_GETRANGE_LAST_SEND);
-
        pop3_gen_send(sock, "LAST");
 
        return POP3_GETRANGE_LAST_RECV;
@@ -256,12 +307,12 @@ gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
 
                if (sscanf(buf, "%d", &last) == 0) {
                        log_warning(_("POP3 protocol error\n"));
+                       state->error_val = PS_PROTOCOL;
                        return -1;
                } else {
                        if (state->count == last)
                                return POP3_LOGOUT_SEND;
                        else {
-                               state->new = state->count - last;
                                state->cur_msg = last + 1;
                                return POP3_GETSIZE_LIST_SEND;
                        }
@@ -272,10 +323,6 @@ gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
 
 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
 {
-       Pop3State *state = (Pop3State *)data;
-
-       inc_progress_update(state, POP3_GETRANGE_UIDL_SEND);
-
        pop3_gen_send(sock, "UIDL");
 
        return POP3_GETRANGE_UIDL_RECV;
@@ -285,35 +332,68 @@ gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
        gboolean new = FALSE;
+       gboolean get_all = FALSE;
        gchar buf[POPBUFSIZE];
        gchar id[IDLEN + 1];
+       gint len;
+       gint next_state;
 
-       if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
+       if (!state->uidl_table) new = TRUE;
+#if 0
+       if (state->ac_prefs->getall ||
+           (state->ac_prefs->rmmail && state->ac_prefs->msg_leave_time == 0))
+#endif
+       if (state->ac_prefs->getall)
+               get_all = TRUE;
+
+       if (pop3_ok(sock, NULL) != PS_SUCCESS) {
+               /* UIDL is not supported */
+               if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
+                       return next_state;
 
-       if (!state->id_table) new = TRUE;
+               if (!get_all)
+                       return POP3_GETRANGE_LAST_SEND;
+               else
+                       return POP3_GETSIZE_LIST_SEND;
+       }
 
-       while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
+       while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
                gint num;
+               time_t recv_time;
 
                if (buf[0] == '.') break;
                if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
                        continue;
+               if (num <= 0 || num > state->count) continue;
+
+               state->msg[num].uidl = g_strdup(id);
+
+               if (!state->uidl_table) continue;
+
+               recv_time = (time_t)g_hash_table_lookup(state->uidl_table, id);
+               state->msg[num].recv_time = recv_time;
+
+               if (!get_all && recv_time != RECV_TIME_NONE)
+                       state->msg[num].received = TRUE;
 
                if (new == FALSE &&
-                   g_hash_table_lookup(state->id_table, id) == NULL) {
-                       state->new = state->count - num + 1;
+                   (get_all || recv_time == RECV_TIME_NONE ||
+                    state->ac_prefs->rmmail)) {
                        state->cur_msg = num;
                        new = TRUE;
                }
+       }
 
-               if (new == TRUE)
-                       state->new_id_list = g_slist_append
-                               (state->new_id_list, g_strdup(id));
-               else
-                       state->id_list = g_slist_append
-                               (state->id_list, g_strdup(id));
+       if (len < 0) {
+               log_error(_("Socket error\n"));
+               state->error_val = PS_SOCKET;
+               return -1;
        }
 
+       state->uidl_is_valid = TRUE;
+       if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
+               return next_state;
+
        if (new == TRUE)
                return POP3_GETSIZE_LIST_SEND;
        else
@@ -322,10 +402,6 @@ gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
 
 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
 {
-       Pop3State *state = (Pop3State *)data;
-
-       inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
-
        pop3_gen_send(sock, "LIST");
 
        return POP3_GETSIZE_LIST_RECV;
@@ -335,31 +411,41 @@ gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
        gchar buf[POPBUFSIZE];
+       gint len;
+       gint next_state;
 
        if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
 
-       state->sizes = g_new0(gint, state->count + 1);
        state->cur_total_bytes = 0;
+       state->cur_total_recv_bytes = 0;
 
-       while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
+       while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
                guint num, size;
 
                if (buf[0] == '.') break;
-               if (sscanf(buf, "%u %u", &num, &size) != 2)
+               if (sscanf(buf, "%u %u", &num, &size) != 2) {
+                       state->error_val = PS_PROTOCOL;
                        return -1;
+               }
 
                if (num > 0 && num <= state->count)
-                       state->sizes[num] = size;
+                       state->msg[num].size = size;
                if (num > 0 && num < state->cur_msg)
                        state->cur_total_bytes += size;
        }
 
+       if (len < 0) {
+               log_error(_("Socket error\n"));
+               state->error_val = PS_SOCKET;
+               return -1;
+       }
+
+       if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
+               return next_state;
+
        LOOKUP_NEXT_MSG();
-       if (state->ac_prefs->session_type == RETR_HEADER) 
-               return POP3_TOP_SEND;
-       else
-               return POP3_RETR_SEND;
- }
+       return POP3_RETR_SEND;
+}
  
 gint pop3_top_send(SockInfo *sock, gpointer data)
 {
@@ -375,13 +461,9 @@ gint pop3_top_send(SockInfo *sock, gpointer data)
 gint pop3_top_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
-       FILE  *fp;
-       gchar buf[POPBUFSIZE];
-       gchar *header;
        gchar *filename, *path;
-       
-       inc_progress_update(state, POP3_TOP_RECV); 
-
+       gint next_state;
+       gint write_val;
        if (pop3_ok(sock, NULL) != PS_SUCCESS) 
                return POP3_LOGOUT_SEND;
 
@@ -392,24 +474,18 @@ gint pop3_top_recv(SockInfo *sock, gpointer data)
        
        filename = g_strdup_printf("%s%i", path, state->cur_msg);
                                   
-       if (recv_write_to_file(sock, filename) < 0) {
-               state->inc_state = INC_NOSPACE;
+       if ( (write_val = recv_write_to_file(sock, filename)) < 0) {
+               state->error_val = (write_val == -1 ? PS_IOERR : PS_SOCKET);
+               g_free(path);
+               g_free(filename);
                return -1;
        }
-       /* we add a Complete-Size Header Item ...
-          note: overwrites first line  --> this is dirty */
-       if ( (fp = fopen(filename, "r+")) != NULL ) {
-               gchar *buf = g_strdup_printf("%s%i", SIZE_HEADER, 
-                                            state->sizes[state->cur_msg]);
-       
-               if (change_file_mode_rw(fp, filename) == 0) 
-                       fprintf(fp, "%s\n", buf);
-               fclose(fp);
-       }
        
        g_free(path);
        g_free(filename);
-
+       
+       pop3_sd_state(state, POP3_TOP_RECV, &next_state);
+       
        if (state->cur_msg < state->count) {
                state->cur_msg++;
                return POP3_TOP_SEND;
@@ -421,8 +497,6 @@ gint pop3_retr_send(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
 
-       inc_progress_update(state, POP3_RETR_SEND);
-
        pop3_gen_send(sock, "RETR %d", state->cur_msg);
 
        return POP3_RETR_RECV;
@@ -431,33 +505,42 @@ gint pop3_retr_send(SockInfo *sock, gpointer data)
 gint pop3_retr_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
-       const gchar *file;
+       gchar *file;
        gint ok, drop_ok;
-
+       gint next_state;
+       gint write_val;
        if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
-               if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
-                       state->inc_state = INC_NOSPACE;
+               file = get_tmp_file();
+               if ((write_val = recv_write_to_file(sock, file)) < 0) {
+                       g_free(file);
+                       if (!state->cancelled)
+                               state->error_val = 
+                                       (write_val == -1 ? PS_IOERR : PS_SOCKET);
                        return -1;
                }
 
-               if ((drop_ok = inc_drop_message(file, state)) < 0) {
-                       state->inc_state = INC_ERROR;
+               /* drop_ok: 0: success 1: don't receive -1: error */
+               drop_ok = inc_drop_message(file, state);
+               g_free(file);
+               if (drop_ok < 0) {
+                       state->error_val = PS_ERROR;
                        return -1;
                }
 
-               state->cur_total_bytes += state->sizes[state->cur_msg];
+               if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
+                       return next_state;
+       
+               state->cur_total_bytes += state->msg[state->cur_msg].size;
+               state->cur_total_recv_bytes += state->msg[state->cur_msg].size;
                state->cur_total_num++;
 
-               if (drop_ok == 0 && state->ac_prefs->rmmail)
-                       return POP3_DELETE_SEND;
+               state->msg[state->cur_msg].received = TRUE;
+               state->msg[state->cur_msg].recv_time =
+                       drop_ok == 1 ? RECV_TIME_KEEP : state->current_time;
 
-               if (state->new_id_list) {
-                       state->id_list = g_slist_append
-                               (state->id_list, state->new_id_list->data);
-                       state->new_id_list =
-                               g_slist_remove(state->new_id_list,
-                                              state->new_id_list->data);
-               }
+               if (drop_ok == 0 && state->ac_prefs->rmmail &&
+                   state->ac_prefs->msg_leave_time == 0)
+                       return POP3_DELETE_SEND;
 
                if (state->cur_msg < state->count) {
                        state->cur_msg++;
@@ -465,18 +548,19 @@ gint pop3_retr_recv(SockInfo *sock, gpointer data)
                        return POP3_RETR_SEND;
                } else
                        return POP3_LOGOUT_SEND;
-       } else if (ok == PS_PROTOCOL)
+       } else if (ok == PS_PROTOCOL) {
+               state->error_val = ok;
                return POP3_LOGOUT_SEND;
-       else
+       } else {
+               state->error_val = ok;
                return -1;
+       }
 }
 
 gint pop3_delete_send(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
 
-       /* inc_progress_update(state, POP3_DELETE_SEND); */
-
        pop3_gen_send(sock, "DELE %d", state->cur_msg);
 
        return POP3_DELETE_RECV;
@@ -485,12 +569,14 @@ gint pop3_delete_send(SockInfo *sock, gpointer data)
 gint pop3_delete_recv(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
+       gint next_state;
        gint ok;
 
        if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
-
-               if (pop3_delete_header(state) == TRUE) 
-                       return POP3_DELETE_SEND;
+               state->msg[state->cur_msg].deleted = TRUE;
+               
+               if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
+                       return next_state;      
 
                if (state->cur_msg < state->count) {
                        state->cur_msg++;
@@ -498,18 +584,17 @@ gint pop3_delete_recv(SockInfo *sock, gpointer data)
                        return POP3_RETR_SEND;
                } else
                        return POP3_LOGOUT_SEND;
-       } else if (ok == PS_PROTOCOL)
+       } else if (ok == PS_PROTOCOL) {
+               state->error_val = ok;
                return POP3_LOGOUT_SEND;
-       else
+       } else {
+               state->error_val = ok;
                return -1;
+       }
 }
 
 gint pop3_logout_send(SockInfo *sock, gpointer data)
 {
-       Pop3State *state = (Pop3State *)data;
-
-       inc_progress_update(state, POP3_LOGOUT_SEND);
-
        pop3_gen_send(sock, "QUIT");
 
        return POP3_LOGOUT_RECV;
@@ -517,10 +602,13 @@ gint pop3_logout_send(SockInfo *sock, gpointer data)
 
 gint pop3_logout_recv(SockInfo *sock, gpointer data)
 {
-       if (pop3_ok(sock, NULL) == PS_SUCCESS)
-               return -1;
-       else
-               return -1;
+       Pop3State *state = (Pop3State *)data;
+       gint ok;
+
+       if ((ok = pop3_ok(sock, NULL)) != PS_SUCCESS)
+               state->error_val = ok;
+
+       return -1;
 }
 
 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
@@ -595,17 +683,271 @@ static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
        }
 }
 
-gboolean pop3_delete_header (Pop3State *state)
+static void pop3_sd_new_header(Pop3State *state)
 {
-       
-       if ( (state->ac_prefs->session_type == DELE_HEADER) &&
-            (g_slist_length(state->ac_prefs->to_delete) > 0) ) {
+       HeaderItems *new_msg;
+       if (state->cur_msg <= state->count) {
+               new_msg = g_new0(HeaderItems, 1); 
+               
+               new_msg->index              = state->cur_msg;
+               new_msg->state              = SD_UNCHECKED;
+               new_msg->size               = state->msg[state->cur_msg].size; 
+               new_msg->received           = state->msg[state->cur_msg].received;
+               new_msg->del_by_old_session = FALSE;
+               
+               state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list, 
+                                                          new_msg);
+               debug_print("received ?: msg %i, received: %i\n",new_msg->index, new_msg->received); 
+       }
+}
+
+gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state) 
+{
+       gint session = state->ac_prefs->session;
+       guint goto_state = -1;
+
+       switch (cur_state) { 
+       case POP3_GETRANGE_UIDL_RECV:
+               switch (session) {
+               case STYPE_POP_BEFORE_SMTP:
+                       goto_state = POP3_LOGOUT_SEND;
+                       break;
+               case STYPE_DOWNLOAD:
+               case STYPE_DELETE:
+               case STYPE_PREVIEW_ALL:
+                       goto_state = POP3_GETSIZE_LIST_SEND;
+               default:
+                       break;
+               }
+               break;
+       case POP3_GETSIZE_LIST_RECV:
+               switch (session) {
+               case STYPE_PREVIEW_ALL:
+                       state->cur_msg = 1;
+               case STYPE_PREVIEW_NEW:
+                       goto_state = POP3_TOP_SEND;
+                       break;
+               case STYPE_DELETE:
+                       if (pop3_sd_get_next(state))
+                               goto_state = POP3_DELETE_SEND;          
+                       else
+                               goto_state = POP3_LOGOUT_SEND;
+                       break;
+               case STYPE_DOWNLOAD:
+                       if (pop3_sd_get_next(state))
+                               goto_state = POP3_RETR_SEND;
+                       else
+                               goto_state = POP3_LOGOUT_SEND;
+               default:
+                       break;
+               }
+               break;
+       case POP3_TOP_RECV: 
+               switch (session) { 
+               case STYPE_PREVIEW_ALL:
+               case STYPE_PREVIEW_NEW:
+                       pop3_sd_new_header(state);
+               default:
+                       break;
+               }
+               break;
+       case POP3_RETR_RECV:
+               switch (session) {
+               case STYPE_DOWNLOAD:
+                       if (state->ac_prefs->sd_rmmail_on_download) 
+                               goto_state = POP3_DELETE_SEND;
+                       else {
+                               if (pop3_sd_get_next(state)) 
+                                       goto_state = POP3_RETR_SEND;
+                               else
+                                       goto_state = POP3_LOGOUT_SEND;
+                       }
+               default:        
+                       break;
+               }
+               break;
+       case POP3_DELETE_RECV:
+               switch (session) {
+               case STYPE_DELETE:
+                       if (pop3_sd_get_next(state)) 
+                               goto_state = POP3_DELETE_SEND;
+                       else
+                               goto_state =  POP3_LOGOUT_SEND;
+                       break;
+               case STYPE_DOWNLOAD:
+                       if (pop3_sd_get_next(state)) 
+                               goto_state = POP3_RETR_SEND;
+                       else
+                               goto_state = POP3_LOGOUT_SEND;
+               default:
+                       break;
+               }
+       default:
+               break;
+               
+       }                 
 
-               state->cur_msg = (gint) state->ac_prefs->to_delete->data;
-               debug_print(_("next to delete %i\n"), state->cur_msg);
-               state->ac_prefs->to_delete = g_slist_remove 
-                       (state->ac_prefs->to_delete, state->ac_prefs->to_delete->data);
+       *next_state = goto_state;
+       if (goto_state != -1)
                return TRUE;
+       else 
+               return FALSE;
+}
+
+gboolean pop3_sd_get_next(Pop3State *state)
+{
+       GSList *cur;
+       gint deleted_msgs = 0;
+       
+       switch (state->ac_prefs->session) {
+       case STYPE_DOWNLOAD:
+       case STYPE_DELETE:      
+               for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
+                       HeaderItems *items = (HeaderItems*)cur->data;
+
+                       if (items->del_by_old_session)
+                               deleted_msgs++;
+
+                       switch (items->state) {
+                       case SD_REMOVE:
+                               items->state = SD_REMOVED;
+                               break;
+                       case SD_DOWNLOAD:
+                               items->state = SD_DOWNLOADED;
+                               break;
+                       case SD_CHECKED:
+                               state->cur_msg = items->index - deleted_msgs;
+                               if (state->ac_prefs->session == STYPE_DELETE)
+                                       items->state = SD_REMOVE;
+                               else
+                                       items->state = SD_DOWNLOAD;
+                               return TRUE;
+                       default:
+                               break;
+                       }
+               }
+               return FALSE;
+       default:
+               return FALSE;
+       }
+}
+
+Pop3State *pop3_state_new(PrefsAccount *account)
+{
+       Pop3State *state;
+
+       g_return_val_if_fail(account != NULL, NULL);
+
+       state = g_new0(Pop3State, 1);
+
+       state->ac_prefs = account;
+       state->uidl_table = pop3_get_uidl_table(account);
+       state->current_time = time(NULL);
+       state->error_val = PS_SUCCESS;
+
+       return state;
+}
+
+void pop3_state_destroy(Pop3State *state)
+{
+       gint n;
+
+       g_return_if_fail(state != NULL);
+
+       for (n = 1; n <= state->count; n++)
+               g_free(state->msg[n].uidl);
+       g_free(state->msg);
+
+       if (state->uidl_table) {
+               hash_free_strings(state->uidl_table);
+               g_hash_table_destroy(state->uidl_table);
+       }
+
+       g_free(state->greeting);
+       g_free(state->user);
+       g_free(state->pass);
+       g_free(state->prev_folder);
+
+       g_free(state);
+}
+
+GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
+{
+       GHashTable *table;
+       gchar *path;
+       FILE *fp;
+       gchar buf[POPBUFSIZE];
+       gchar uidl[POPBUFSIZE];
+       time_t recv_time;
+       time_t now;
+
+       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) {
+               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 (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+                       g_free(path);
+                       return NULL;
+               }
+       }
+       g_free(path);
+
+       table = g_hash_table_new(g_str_hash, g_str_equal);
+
+       now = time(NULL);
+
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               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;
+               }
+               if (recv_time == RECV_TIME_NONE)
+                       recv_time = RECV_TIME_RECEIVED;
+               g_hash_table_insert(table, g_strdup(uidl),
+                                   GINT_TO_POINTER(recv_time));
+       }
+
+       fclose(fp);
+       return table;
+}
+
+gint pop3_write_uidl_list(Pop3State *state)
+{
+       gchar *path;
+       FILE *fp;
+       Pop3MsgInfo *msg;
+       gint n;
+
+       if (!state->uidl_is_valid) return 0;
+
+       path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
+                          "uidl", G_DIR_SEPARATOR_S,
+                          state->ac_prefs->recv_server,
+                          "-", state->ac_prefs->userid, NULL);
+       if ((fp = fopen(path, "wb")) == NULL) {
+               FILE_OP_ERROR(path, "fopen");
+               g_free(path);
+               return -1;
+       }
+
+       for (n = 1; n <= state->count; n++) {
+               msg = &state->msg[n];
+               if (msg->uidl && msg->received && !msg->deleted)
+                       fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
        }
-       return FALSE;
+
+       if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
+       g_free(path);
+
+       return 0;
 }