add ability to leave downloaded mail on server for n days
[claws.git] / src / pop.c
index 052c8a38f7b0a88c4d35ac6fb918dc1b7fe022c3..c74573f1376ea9d13fcd476db04ee1e05e21b80e 100644 (file)
--- a/src/pop.c
+++ b/src/pop.c
@@ -288,7 +288,7 @@ gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
        if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
 
        if (!state->uidl_table) new = TRUE;
        if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
 
        if (!state->uidl_table) new = TRUE;
-       if (state->ac_prefs->rmmail || state->ac_prefs->getall)
+       if (state->ac_prefs->getall)
                get_all = TRUE;
 
        while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
                get_all = TRUE;
 
        while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
@@ -312,6 +312,11 @@ gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
                                }
                        }
                }
                                }
                        }
                }
+
+               if(should_delete(buf, (Pop3State *)state))
+                       state->uidl_todelete_list = g_slist_append
+                                       (state->uidl_todelete_list, g_strdup(buf));             
+               
        }
 
        state->uidl_is_valid = TRUE;
        }
 
        state->uidl_is_valid = TRUE;
@@ -321,6 +326,32 @@ gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
        else
                return POP3_LOGOUT_SEND;
 }
        else
                return POP3_LOGOUT_SEND;
 }
+gboolean should_delete(char * uidl, gpointer data) {
+       Pop3State *state = (Pop3State *)data;
+       /* answer[0] will contain id
+          answer[0] will contain uidl */
+       gchar **answer;
+       GDate *curdate = g_date_new();
+       int id = 0;
+       g_date_set_time(curdate, time(NULL));
+       if( state->ac_prefs->rmmail && strchr(uidl,' ') ) {
+               /* remove \r\n */
+               uidl = g_strndup(uidl, strlen(uidl)-2);
+               answer = g_strsplit(uidl, " ", 2);
+               id = atoi(answer[0]);
+               if( g_hash_table_lookup(state->uidl_table, answer[1]) != NULL ) {
+                       gchar *sdate = g_hash_table_lookup(state->uidl_table, answer[1]);
+                       int tdate = atoi(sdate);
+                       int keep_for = atoi(state->ac_prefs->leave_time);
+                       int today = g_date_day_of_year(curdate);
+                       int nb_days = 365;
+                       if ( g_date_is_leap_year (g_date_year(curdate)) ) 
+                               nb_days = 366;
+                       return ( (tdate + keep_for)%nb_days <= today );
+               }
+       }
+       return FALSE;
+}
 
 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
 {
 
 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
 {
@@ -434,7 +465,8 @@ gint pop3_retr_recv(SockInfo *sock, gpointer data)
        Pop3State *state = (Pop3State *)data;
        const gchar *file;
        gint ok, drop_ok;
        Pop3State *state = (Pop3State *)data;
        const gchar *file;
        gint ok, drop_ok;
-
+       int keep_for=atoi(g_strdup(state->ac_prefs->leave_time));
+       
        if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
                if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
                        if (state->inc_state == INC_SUCCESS)
        if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
                if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
                        if (state->inc_state == INC_SUCCESS)
@@ -446,12 +478,13 @@ gint pop3_retr_recv(SockInfo *sock, gpointer data)
                        state->inc_state = INC_ERROR;
                        return -1;
                }
                        state->inc_state = INC_ERROR;
                        return -1;
                }
-
+       
                state->cur_total_bytes += state->msg[state->cur_msg].size;
                state->cur_total_num++;
 
                state->cur_total_bytes += state->msg[state->cur_msg].size;
                state->cur_total_num++;
 
-               if (drop_ok == 0 && state->ac_prefs->rmmail)
+               if (drop_ok == 0 && state->ac_prefs->rmmail && keep_for == 0) {
                        return POP3_DELETE_SEND;
                        return POP3_DELETE_SEND;
+               }
 
                state->msg[state->cur_msg].received = TRUE;
 
 
                state->msg[state->cur_msg].received = TRUE;
 
@@ -505,7 +538,17 @@ gint pop3_delete_recv(SockInfo *sock, gpointer data)
 gint pop3_logout_send(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
 gint pop3_logout_send(SockInfo *sock, gpointer data)
 {
        Pop3State *state = (Pop3State *)data;
-
+       while (state->uidl_todelete_list != NULL) {
+               gchar **parts;
+               gint ok;
+               parts=g_strsplit((gchar *)state->uidl_todelete_list->data," ",2);
+               state->uidl_todelete_list = g_slist_remove(
+                                       state->uidl_todelete_list, state->uidl_todelete_list->data);
+               pop3_gen_send(sock, "DELE %s",parts[0]);
+               if ((ok = pop3_ok(sock, NULL)) != PS_SUCCESS)
+                       log_warning(_("error occurred on DELE\n"));
+       }
+       
        inc_progress_update(state, POP3_LOGOUT_SEND);
 
        pop3_gen_send(sock, "QUIT");
        inc_progress_update(state, POP3_LOGOUT_SEND);
 
        pop3_gen_send(sock, "QUIT");