2004-11-29 [colin] 0.9.12cvs174.1
[claws.git] / src / pop.c
index ff0f9dd124b6ba09ce7aaf7016488f13e3ec91d2..07c5fc49729a4c289930f60928297c75e913b9b3 100644 (file)
--- a/src/pop.c
+++ b/src/pop.c
@@ -36,7 +36,7 @@
 #include "prefs_account.h"
 #include "utils.h"
 #include "recv.h"
-
+#include "partial_download.h"
 #include "log.h"
 #include "hooks.h"
 
@@ -91,8 +91,6 @@ 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 gint pop3_greeting_recv(Pop3Session *session, const gchar *msg)
 {
        session->state = POP3_GREETING;
@@ -149,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 "
+               log_error(_("Required APOP timestamp not found "
                            "in greeting\n"));
                session->error_val = PS_PROTOCOL;
                return -1;
        }
 
        if ((end = strchr(start, '>')) == NULL || end == start + 1) {
-               log_warning(_("Timestamp syntax error in greeting\n"));
+               log_error(_("Timestamp syntax error in greeting\n"));
                session->error_val = PS_PROTOCOL;
                return -1;
        }
@@ -182,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(_("POP3 protocol error\n"));
                session->error_val = PS_PROTOCOL;
                return -1;
        } else {
@@ -253,20 +251,25 @@ static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *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(_("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)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);
+               partial_recv = (gint)g_hash_table_lookup(
+                                       session->partial_recv_table, id);
 
-               if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE) {
-                       session->msg[num].received = (partial_recv != 2);
+               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;
 
                }
@@ -350,31 +353,34 @@ static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
        }
        g_free(mail_receive_data.data);
 
-       if (session->msg[session->cur_msg].partial_recv == 2) {
-               gchar *old_file = pop3_get_filename_for_partial_mail(session, session->msg[session->cur_msg].uidl);
-               unlink(old_file);
-               rename(file, old_file);
-               g_free(file);
-               file = old_file;
-               /* drop_ok: 0: success 1: don't receive -1: error */
-               drop_ok = session->drop_message(session, file, TRUE);
-       } else {
-               /* drop_ok: 0: success 1: don't receive -1: error */
-               drop_ok = session->drop_message(session, file, FALSE);
-       }
+       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;
        }
        
-stats:
        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 = FALSE;
+       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;
@@ -382,10 +388,11 @@ stats:
        return PS_SUCCESS;
 }
 
-static gint pop3_top_send(Pop3Session *session)
+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 10", session->cur_msg);
+       pop3_gen_send(session, "TOP %d %d", session->cur_msg, num_lines);
        return PS_SUCCESS;
 }
 
@@ -400,7 +407,8 @@ static gint pop3_top_recv(Pop3Session *session, const gchar *data, guint len)
        mail_receive_data.data = g_strndup(data, len);
        hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
 
-       partial_notice = g_strdup_printf("SC-Partially-Retrieved: %s\n"
+       partial_notice = g_strdup_printf("SC-Marked-For-Download: 0\n"
+                                        "SC-Partially-Retrieved: %s\n"
                                         "SC-Account-Server: %s\n"
                                         "SC-Account-Login: %s\n"
                                         "SC-Message-Size: %d",
@@ -421,7 +429,7 @@ static gint pop3_top_recv(Pop3Session *session, const gchar *data, guint len)
        g_free(partial_notice);
 
        /* drop_ok: 0: success 1: don't receive -1: error */
-       drop_ok = session->drop_message(session, file, FALSE);
+       drop_ok = session->drop_message(session, file);
        g_free(file);
        if (drop_ok < 0) {
                session->error_val = PS_IOERR;
@@ -433,7 +441,7 @@ static gint pop3_top_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 = 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;
 
@@ -469,7 +477,7 @@ 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))
+       if (!g_ascii_strncasecmp(buf, "PASS ", 5))
                log_print("POP3> PASS ********\n");
        else
                log_print("POP3> %s\n", buf);
@@ -533,7 +541,7 @@ static void pop3_session_destroy(Session *session)
        g_free(pop3_session->error_msg);
 }
 
-GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
+void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
 {
        GHashTable *table;
        GHashTable *partial_recv_table;
@@ -562,7 +570,7 @@ GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
                        g_free(path);
                        session->uidl_table = table;
                        session->partial_recv_table = partial_recv_table;
-                       return table;
+                       return;
                }
        }
        g_free(path);
@@ -573,23 +581,29 @@ GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
                gchar tmp[POPBUFSIZE];
                strretchomp(buf);
                recv_time = RECV_TIME_NONE;
-               partial_recv = 0;
+               partial_recv = POP3_TOTALLY_RECEIVED;
                
-               if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, &tmp) < 2) {
-                       if (sscanf(buf, "%s", uidl) != 1)
-                               continue;
-                       else {
-                               recv_time = now;
+               if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, tmp) < 3) {
+                       if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
+                               if (sscanf(buf, "%s", uidl) != 1)
+                                       continue;
+                               else {
+                                       recv_time = now;
+                                       strcpy(tmp, "0");
+                               }
+                       } else {
+                               strcpy(tmp, "0");
                        }
                }
+
                if (recv_time == RECV_TIME_NONE)
                        recv_time = RECV_TIME_RECEIVED;
                g_hash_table_insert(table, g_strdup(uidl),
                                    GINT_TO_POINTER(recv_time));
                if (strlen(tmp) == 1)
-                       partial_recv = atoi(tmp);
+                       partial_recv = atoi(tmp); /* totally received ?*/
                else
-                       partial_recv = 2;
+                       partial_recv = POP3_MUST_COMPLETE_RECV;
 
                g_hash_table_insert(partial_recv_table, g_strdup(uidl),
                                    GINT_TO_POINTER(partial_recv));
@@ -599,156 +613,7 @@ GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
        session->uidl_table = table;
        session->partial_recv_table = partial_recv_table;
        
-       return table;
-}
-
-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 = 0;
-               
-               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;
-}
-
-int pop3_mark_for_download(const gchar *server, const gchar *login, const gchar *muidl, const gchar *filename)
-{
-       gchar *path;
-       gchar *pathnew;
-       FILE *fp;
-       FILE *fpnew;
-       gchar buf[POPBUFSIZE];
-       gchar uidl[POPBUFSIZE];
-       time_t recv_time;
-       time_t now;
-       int len;
-       gchar partial_recv[POPBUFSIZE];
-       
-
-       path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
-                          "uidl", G_DIR_SEPARATOR_S, server,
-                          "-", login, NULL);
-       if ((fp = fopen(path, "rb")) == NULL) {
-               perror("fopen1");
-               if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
-               g_free(path);
-               path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
-                                  "uidl-", server,
-                                  "-", login, NULL);
-               if ((fp = fopen(path, "rb")) == NULL) {
-                       if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
-                       g_free(path);
-               }
-               return -1;
-       }
-
-       pathnew = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
-                          "uidl", G_DIR_SEPARATOR_S, server,
-                          "-", login, ".new", NULL);
-       if ((fpnew = fopen(pathnew, "wb")) == NULL) {
-               perror("fopen2");
-               fclose(fp);
-               g_free(pathnew);
-               return -1;      
-       }
-       
-       now = time(NULL);
-
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               strretchomp(buf);
-               recv_time = RECV_TIME_NONE;
-               sprintf(partial_recv,"0");
-               
-               if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, &partial_recv) < 2) {
-                       if (sscanf(buf, "%s", uidl) != 1)
-                               continue;
-                       else {
-                               recv_time = now;
-                       }
-               }
-               if (strcmp(muidl, uidl)) {
-                       fprintf(fpnew, "%s\t%ld\t%s\n", uidl, recv_time, partial_recv);
-               } else {
-                       fprintf(fpnew, "%s\t%ld\t%s\n", uidl, recv_time, filename);
-               }
-       }
-       fclose(fpnew);
-       fclose(fp);
-
-       unlink(path);
-       rename(pathnew, path);
-               
-       g_free(path);
-       g_free(pathnew);
-       
-       if ((fp = fopen(filename,"rb")) == NULL) {
-               perror("fopen3");
-               return -1;
-       }
-       pathnew = g_strdup_printf("%s.new", filename);
-       if ((fpnew = fopen(pathnew, "wb")) == NULL) {
-               perror("fopen4");
-               fclose(fp);
-               g_free(pathnew);
-               return -1;
-       }
-       
-       fprintf(fpnew, "SC-Marked-For-Download: 1\n");
-       while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
-               fprintf(fpnew, "%s", buf);
-       }
-       fclose(fpnew);
-       fclose(fp);
-       unlink(filename);
-       rename(pathnew, filename);
-       
-       g_free(pathnew);
-       return 0;
+       return;
 }
 
 gint pop3_write_uidl_list(Pop3Session *session)
@@ -773,7 +638,8 @@ gint pop3_write_uidl_list(Pop3Session *session)
        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);
+                       fprintf(fp, "%s\t%ld\t%d\n", 
+                               msg->uidl, msg->recv_time, msg->partial_recv);
                }
        }
 
@@ -800,7 +666,7 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
                FILE_OP_ERROR(file, "chmod");
 
        if (prefix != NULL) {
-               fprintf(fp, prefix);
+               fprintf(fp, "%s", prefix);
                fprintf(fp, "\n");
        }
        
@@ -880,26 +746,27 @@ 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_message
-                               (_("POP3: Deleting expired message %d\n"),
-                                  session->cur_msg);
+                               (_("POP3: Deleting expired message "
+                                  "%d\n"), session->cur_msg);
                        pop3_delete_send(session);
                        return POP3_DELETE;
                }
 
                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
                                (_("POP3: Skipping message %d (%d bytes)\n"),
                                   session->cur_msg, size);
-
-                       if (!msg->received && msg->partial_recv != 2) {
-                               pop3_top_send(session);
-                               return POP3_TOP;
-                       } else if (msg->partial_recv == 2) {
-                               break;
-                       }
                }
                
                if (size == 0 || msg->received || size_limit_over) {
@@ -930,32 +797,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(_("mailbox is locked\n"));
                        ok = PS_LOCKBUSY;
                } else if (strcasestr(msg + 4, "timeout")) {
-                       log_warning(_("session timeout\n"));
+                       log_error(_("Session timeout\n"));
                        ok = PS_ERROR;
                } else {
                        switch (session->state) {
 #if USE_OPENSSL
                        case POP3_STLS:
-                               log_warning(_("can't start TLS session\n"));
+                               log_error(_("can't start TLS session\n"));
                                ok = PS_ERROR;
                                break;
 #endif
                        case POP3_GETAUTH_USER:
                        case POP3_GETAUTH_PASS:
                        case POP3_GETAUTH_APOP:
-                               log_warning(_("error occurred on authentication\n"));
+                               log_error(_("error occurred on authentication\n"));
                                ok = PS_AUTHFAIL;
                                break;
                        case POP3_GETRANGE_LAST:
                        case POP3_GETRANGE_UIDL:
+                       case POP3_TOP:
                                log_warning(_("command not supported\n"));
                                ok = PS_NOTSUPPORTED;
                                break;
+                               
                        default:
-                               log_warning(_("error occurred on POP3 session\n"));
+                               log_error(_("error occurred on POP3 session\n"));
                                ok = PS_ERROR;
                        }
                }
@@ -1004,7 +873,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);
@@ -1013,7 +882,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);
@@ -1065,8 +934,12 @@ static gint pop3_session_recv_msg(Session *session, const gchar *msg)
                session_recv_data(session, 0, ".\r\n");
                break;
        case POP3_TOP:
-               pop3_session->state = POP3_TOP_RECV;
-               session_recv_data(session, 0, ".\r\n");
+               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);
@@ -1143,6 +1016,16 @@ static gint pop3_session_recv_data_finished(Session *session, guchar *data,
                                return -1;
                }
                break;
+       case POP3_TOP:
+               log_warning(_("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;