don't translate strings in debug_print(), log_print(), log_message(), and log_warning()
[claws.git] / src / pop.c
index 5c73b5b829ff36ca59e3a03ad0f59676bee91ff7..637421c4bea343dca6b9d4b8afc24b6cf9176d1b 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-2004 Hiroyuki Yamamoto
  *
  * 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
 #include "md5.h"
 #include "prefs_account.h"
 #include "utils.h"
-#include "inc.h"
 #include "recv.h"
 
 #include "log.h"
+#include "hooks.h"
 
 static gint pop3_greeting_recv         (Pop3Session *session,
                                         const gchar *msg);
@@ -57,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,
@@ -88,6 +90,7 @@ 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;
@@ -144,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_warning("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_warning("Timestamp syntax error in greeting\n");
                session->error_val = PS_PROTOCOL;
                return -1;
        }
@@ -177,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_warning("POP3 protocol error\n");
                session->error_val = PS_PROTOCOL;
                return -1;
        } else {
@@ -204,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("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;
        }
 
@@ -224,41 +228,51 @@ 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];
+       gchar buf[POPBUFSIZE];
+       gint buf_len;
        gint num;
        time_t recv_time;
+       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)
+                       return -1;
+               if (num <= 0 || num > session->count)
+                       return -1;
 
-       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 (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE)
+                       session->msg[num].received = 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;
+               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)
@@ -268,24 +282,38 @@ 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 (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;
+               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)
@@ -299,16 +327,24 @@ static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
 {
        gchar *file;
        gint drop_ok;
+       MailReceiveData mail_receive_data;
+
+       mail_receive_data.session = session;
+       mail_receive_data.data = g_strndup(data, len);
+       hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
 
        file = get_tmp_file();
-       if (pop3_write_msg_to_file(file, data, len) < 0) {
+       if (pop3_write_msg_to_file(file, mail_receive_data.data,
+               strlen(mail_receive_data.data)) < 0) {
                g_free(file);
+               g_free(mail_receive_data.data);
                session->error_val = PS_IOERR;
                return -1;
        }
+       g_free(mail_receive_data.data);
 
        /* 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;
@@ -371,12 +407,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;
@@ -426,6 +459,8 @@ GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
        time_t recv_time;
        time_t now;
 
+       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);
@@ -438,13 +473,11 @@ GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
                if ((fp = fopen(path, "rb")) == NULL) {
                        if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
                        g_free(path);
-                       return NULL;
+                       return table;
                }
        }
        g_free(path);
 
-       table = g_hash_table_new(g_str_hash, g_str_equal);
-
        now = time(NULL);
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
@@ -537,6 +570,9 @@ 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;
        }
@@ -588,7 +624,7 @@ static Pop3State pop3_lookup_next(Pop3Session *session)
                    msg->recv_time != RECV_TIME_KEEP &&
                    session->current_time - msg->recv_time >=
                    ac->msg_leave_time * 24 * 60 * 60) {
-                       log_print(_("POP3: Deleting expired message %d\n"),
+                       log_print("POP3: Deleting expired message %d\n",
                                  session->cur_msg);
                        pop3_delete_send(session);
                        return POP3_DELETE;
@@ -596,7 +632,7 @@ static Pop3State pop3_lookup_next(Pop3Session *session)
 
                if (size_limit_over)
                        log_print
-                               (_("POP3: Skipping message %d (%d bytes)\n"),
+                               ("POP3: Skipping message %d (%d bytes)\n",
                                  session->cur_msg, size);
 
                if (size == 0 || msg->received || size_limit_over) {
@@ -627,29 +663,32 @@ 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_warning("mailbox is locked\n");
                        ok = PS_LOCKBUSY;
+               } else if (strcasestr(msg + 4, "timeout")) {
+                       log_warning("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_warning("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_warning("error occurred on authentication\n");
                                ok = PS_AUTHFAIL;
                                break;
                        case POP3_GETRANGE_LAST:
                        case POP3_GETRANGE_UIDL:
-                               log_warning(_("command not supported\n"));
+                               log_warning("command not supported\n");
                                ok = PS_NOTSUPPORTED;
                                break;
                        default:
-                               log_warning(_("error occurred on POP3 session\n"));
+                               log_warning("error occurred on POP3 session\n");
                                ok = PS_ERROR;
                        }
                }
@@ -668,7 +707,7 @@ static gint pop3_session_recv_msg(Session *session, const gchar *msg)
 {
        Pop3Session *pop3_session = POP3_SESSION(session);
        Pop3ErrorValue val = PS_SUCCESS;
-       const gchar *body;
+       const guchar *body;
 
        body = msg;
        if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
@@ -744,39 +783,16 @@ 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_DELETE:
                pop3_delete_recv(pop3_session);
@@ -803,22 +819,47 @@ 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;
+       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_retr_recv(pop3_session, data, len) < 0)
+               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_ERROR:
+       default:
                return -1;
-
-       if (pop3_session->ac_prefs->rmmail &&
-           pop3_session->ac_prefs->msg_leave_time == 0)
-               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;
        }
 
        return 0;