2005-09-14 [paul] 1.9.14cvs26
[claws.git] / src / pop.c
index f0c3cf903fc9018fa657d6a009e63c513c1211a1..a55a8ce1d7be0c336c1bccebce3904b930460244 100644 (file)
--- a/src/pop.c
+++ b/src/pop.c
@@ -22,6 +22,7 @@
 #endif
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
@@ -30,7 +31,6 @@
 #include <time.h>
 #include <errno.h>
 
-#include "intl.h"
 #include "pop.h"
 #include "md5.h"
 #include "prefs_account.h"
@@ -207,7 +207,7 @@ static gint pop3_getrange_last_recv(Pop3Session *session, const gchar *msg)
        gint last;
 
        if (sscanf(msg, "%d", &last) == 0) {
-               log_error(_("POP3 protocol error\n"));
+               log_warning(_("POP3 protocol error\n"));
                session->error_val = PS_PROTOCOL;
                return -1;
        } else {
@@ -271,6 +271,7 @@ static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *data,
                        session->msg[num].received = 
                                (partial_recv != POP3_MUST_COMPLETE_RECV);
                        session->msg[num].partial_recv = partial_recv;
+
                }
                if (!session->new_msg_exist &&
                    (session->ac_prefs->getall || recv_time == RECV_TIME_NONE ||
@@ -338,13 +339,18 @@ 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,
-               strlen(mail_receive_data.data), NULL) < 0) {
+       if (pop3_write_msg_to_file(file, mail_receive_data.data, 
+                                  mail_receive_data.data_len, NULL) < 0) {
                g_free(file);
                g_free(mail_receive_data.data);
                session->error_val = PS_IOERR;
@@ -402,8 +408,13 @@ static gint pop3_top_recv(Pop3Session *session, const gchar *data, guint len)
        MailReceiveData mail_receive_data;
        gchar *partial_notice = NULL;
        
-       mail_receive_data.session = session;
-       mail_receive_data.data = g_strndup(data, len);
+       /* NOTE: we allocate a slightly larger buffer with a zero terminator
+        * because some plugins may think that it has a C string. */ 
+       mail_receive_data.session  = session;
+       mail_receive_data.data     = g_new0(gchar, len + 1);
+       mail_receive_data.data_len = len;
+       memcpy(mail_receive_data.data, data, len);
+       
        hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
 
        partial_notice = g_strdup_printf("SC-Marked-For-Download: 0\n"
@@ -417,7 +428,8 @@ static gint pop3_top_recv(Pop3Session *session, const gchar *data, guint len)
                                         session->msg[session->cur_msg].size);
        file = get_tmp_file();
        if (pop3_write_msg_to_file(file, mail_receive_data.data,
-               strlen(mail_receive_data.data), partial_notice) < 0) {
+                                  mail_receive_data.data_len,  
+                                  partial_notice) < 0) {
                g_free(file);
                g_free(mail_receive_data.data);
                session->error_val = PS_IOERR;
@@ -476,7 +488,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);
@@ -551,20 +563,25 @@ void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
        time_t recv_time;
        time_t now;
        gint partial_recv;
+       gchar *sanitized_uid = g_strdup(ac_prefs->userid);
+       
+       subst_for_filename(sanitized_uid);
        
        table = g_hash_table_new(g_str_hash, g_str_equal);
        partial_recv_table = g_hash_table_new(g_str_hash, g_str_equal);
 
        path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                           "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
-                          "-", ac_prefs->userid, NULL);
-       if ((fp = fopen(path, "rb")) == NULL) {
+                          "-", sanitized_uid, NULL);
+                          
+       g_free(sanitized_uid);
+       if ((fp = g_fopen(path, "rb")) == NULL) {
                if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
                g_free(path);
                path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                                   "uidl-", ac_prefs->recv_server,
                                   "-", ac_prefs->userid, NULL);
-               if ((fp = fopen(path, "rb")) == NULL) {
+               if ((fp = g_fopen(path, "rb")) == NULL) {
                        if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
                        g_free(path);
                        session->uidl_table = table;
@@ -582,8 +599,8 @@ void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
                recv_time = RECV_TIME_NONE;
                partial_recv = POP3_TOTALLY_RECEIVED;
                
-               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\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 {
@@ -621,14 +638,21 @@ gint pop3_write_uidl_list(Pop3Session *session)
        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) {
+                          "-", sanitized_uid, NULL);
+       
+       g_free(sanitized_uid);
+
+       if ((fp = g_fopen(path, "wb")) == NULL) {
                FILE_OP_ERROR(path, "fopen");
                g_free(path);
                return -1;
@@ -636,10 +660,10 @@ 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) {
+               if (msg->uidl && msg->received &&
+                   (!msg->deleted || session->state != POP3_DONE))
                        fprintf(fp, "%s\t%ld\t%d\n", 
-                               msg->uidl, msg->recv_time, msg->partial_recv);
-               }
+                               msg->uidl, (long int) msg->recv_time, msg->partial_recv);
        }
 
        if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
@@ -656,7 +680,7 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
 
        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;
        }
@@ -665,7 +689,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");
        }
        
@@ -673,13 +697,14 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
         * ^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;
                }
 
@@ -701,11 +726,11 @@ static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
        }
 
        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') {
@@ -713,14 +738,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;
        }
 
@@ -842,7 +867,7 @@ static gint pop3_session_recv_msg(Session *session, const gchar *msg)
 {
        Pop3Session *pop3_session = POP3_SESSION(session);
        Pop3ErrorValue val = PS_SUCCESS;
-       const guchar *body;
+       const gchar *body;
 
        body = msg;
        if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
@@ -857,9 +882,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++;
        }
 
@@ -872,7 +897,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);
@@ -881,7 +906,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);
@@ -951,6 +976,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: