claws-mail/src/pop.c

765 lines
17 KiB
C
Raw Normal View History

2001-04-19 14:21:46 +02:00
/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
2002-02-21 15:56:09 +01:00
* Copyright (C) 1999-2002 Hiroyuki Yamamoto
2001-04-19 14:21:46 +02:00
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <unistd.h>
2002-06-17 12:02:21 +02:00
#include <time.h>
2001-04-19 14:21:46 +02:00
#include "intl.h"
#include "pop.h"
#include "socket.h"
2001-04-27 22:27:24 +02:00
#include "md5.h"
2001-04-19 14:21:46 +02:00
#include "prefs_account.h"
#include "utils.h"
#include "inc.h"
#include "recv.h"
2002-02-22 10:35:36 +01:00
#include "selective_download.h"
2002-05-29 10:28:56 +02:00
#if USE_SSL
# include "ssl.h"
#endif
2001-04-19 14:21:46 +02:00
2002-06-17 12:02:21 +02:00
#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 != 0 && \
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; \
} \
}
2002-02-21 15:56:09 +01:00
2001-04-27 22:27:24 +02:00
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);
2002-06-10 23:55:17 +02:00
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);
2001-04-19 14:21:46 +02:00
2001-04-27 22:27:24 +02:00
gint pop3_greeting_recv(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
gchar buf[POPBUFSIZE];
if (pop3_ok(sock, buf) == PS_SUCCESS) {
2002-06-17 12:02:21 +02:00
state->greeting = g_strdup(buf);
2002-05-29 10:28:56 +02:00
#if USE_SSL
2002-06-17 12:02:21 +02:00
if (state->ac_prefs->ssl_pop == SSL_STARTTLS)
2002-05-29 10:28:56 +02:00
return POP3_STLS_SEND;
#endif
2002-06-17 12:02:21 +02:00
if (state->ac_prefs->protocol == A_APOP)
2001-04-19 14:21:46 +02:00
return POP3_GETAUTH_APOP_SEND;
2002-06-17 12:02:21 +02:00
else
2001-04-19 14:21:46 +02:00
return POP3_GETAUTH_USER_SEND;
} else
return -1;
}
2002-05-29 10:28:56 +02:00
#if USE_SSL
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))
return -1;
2002-06-17 12:02:21 +02:00
if (state->ac_prefs->protocol == A_APOP)
2002-05-29 10:28:56 +02:00
return POP3_GETAUTH_APOP_SEND;
2002-06-17 12:02:21 +02:00
else
2002-05-29 10:28:56 +02:00
return POP3_GETAUTH_USER_SEND;
} else if (ok == PS_PROTOCOL) {
log_warning(_("can't start TLS session\n"));
state->error_val = PS_PROTOCOL;
state->inc_state = INC_ERROR;
return POP3_LOGOUT_SEND;
} else
return -1;
}
#endif /* USE_SSL */
2001-04-27 22:27:24 +02:00
gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
g_return_val_if_fail(state->user != NULL, -1);
pop3_gen_send(sock, "USER %s", state->user);
return POP3_GETAUTH_USER_RECV;
}
2001-04-27 22:27:24 +02:00
gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
2001-10-08 19:23:00 +02:00
Pop3State *state = (Pop3State *)data;
2001-04-19 14:21:46 +02:00
if (pop3_ok(sock, NULL) == PS_SUCCESS)
return POP3_GETAUTH_PASS_SEND;
2001-10-08 19:23:00 +02:00
else {
log_warning(_("error occurred on authentication\n"));
state->error_val = PS_AUTHFAIL;
state->inc_state = INC_AUTH_FAILED;
2001-04-19 14:21:46 +02:00
return -1;
2001-10-08 19:23:00 +02:00
}
2001-04-19 14:21:46 +02:00
}
2001-04-27 22:27:24 +02:00
gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
g_return_val_if_fail(state->pass != NULL, -1);
pop3_gen_send(sock, "PASS %s", state->pass);
return POP3_GETAUTH_PASS_RECV;
}
2001-04-27 22:27:24 +02:00
gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
2002-06-17 12:02:21 +02:00
if (pop3_ok(sock, NULL) == PS_SUCCESS)
2002-06-10 23:55:17 +02:00
return POP3_GETRANGE_STAT_SEND;
2001-04-19 14:21:46 +02:00
else {
2001-07-25 16:49:48 +02:00
log_warning(_("error occurred on authentication\n"));
2001-04-19 14:21:46 +02:00
state->error_val = PS_AUTHFAIL;
2001-08-16 13:28:10 +02:00
state->inc_state = INC_AUTH_FAILED;
2001-04-19 14:21:46 +02:00
return -1;
}
}
2001-04-27 22:27:24 +02:00
gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
gchar *start, *end;
2001-04-27 22:27:24 +02:00
gchar *apop_str;
gchar md5sum[33];
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(state->user != NULL, -1);
g_return_val_if_fail(state->pass != NULL, -1);
if ((start = strchr(state->greeting, '<')) == NULL) {
log_warning(_("Required APOP timestamp not found "
"in greeting\n"));
return -1;
}
if ((end = strchr(start, '>')) == NULL || end == start + 1) {
log_warning(_("Timestamp syntax error in greeting\n"));
return -1;
}
*(end + 1) = '\0';
apop_str = g_strconcat(start, state->pass, NULL);
2001-04-27 22:27:24 +02:00
md5_hex_digest(md5sum, apop_str);
2001-04-19 14:21:46 +02:00
g_free(apop_str);
pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
return POP3_GETAUTH_APOP_RECV;
}
2001-04-27 22:27:24 +02:00
gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
2002-06-17 12:02:21 +02:00
if (pop3_ok(sock, NULL) == PS_SUCCESS)
2001-04-19 14:21:46 +02:00
return POP3_GETRANGE_STAT_SEND;
else {
2001-07-25 16:49:48 +02:00
log_warning(_("error occurred on authentication\n"));
2001-04-19 14:21:46 +02:00
state->error_val = PS_AUTHFAIL;
2001-08-16 13:28:10 +02:00
state->inc_state = INC_AUTH_FAILED;
2001-04-19 14:21:46 +02:00
return -1;
}
}
2001-04-27 22:27:24 +02:00
gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
pop3_gen_send(sock, "STAT");
return POP3_GETRANGE_STAT_RECV;
}
2001-04-27 22:27:24 +02:00
gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
gchar buf[POPBUFSIZE + 1];
gint ok;
if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
2001-04-28 00:47:56 +02:00
if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
!= 2) {
2001-04-19 14:21:46 +02:00
log_warning(_("POP3 protocol error\n"));
return -1;
} else {
2002-03-06 10:06:40 +01:00
if (state->count == 0) {
state->uidl_is_valid = TRUE;
2001-04-19 14:21:46 +02:00
return POP3_LOGOUT_SEND;
2002-03-06 10:06:40 +01:00
} else {
2002-02-27 09:15:53 +01:00
state->msg = g_new0
(Pop3MsgInfo, state->count + 1);
2001-04-19 14:21:46 +02:00
state->cur_msg = 1;
2002-02-27 09:15:53 +01:00
return POP3_GETRANGE_UIDL_SEND;
2001-04-19 14:21:46 +02:00
}
}
} else if (ok == PS_PROTOCOL)
return POP3_LOGOUT_SEND;
else
return -1;
}
2001-04-27 22:27:24 +02:00
gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
pop3_gen_send(sock, "LAST");
return POP3_GETRANGE_LAST_RECV;
}
2001-04-27 22:27:24 +02:00
gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
gchar buf[POPBUFSIZE + 1];
if (pop3_ok(sock, buf) == PS_SUCCESS) {
gint last;
if (sscanf(buf, "%d", &last) == 0) {
log_warning(_("POP3 protocol error\n"));
return -1;
} else {
if (state->count == last)
return POP3_LOGOUT_SEND;
else {
state->cur_msg = last + 1;
2001-04-28 00:47:56 +02:00
return POP3_GETSIZE_LIST_SEND;
2001-04-19 14:21:46 +02:00
}
}
} else
2001-04-28 00:47:56 +02:00
return POP3_GETSIZE_LIST_SEND;
2001-04-19 14:21:46 +02:00
}
2001-04-27 22:27:24 +02:00
gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
pop3_gen_send(sock, "UIDL");
return POP3_GETRANGE_UIDL_RECV;
}
2001-04-27 22:27:24 +02:00
gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
gboolean new = FALSE;
2002-02-27 09:15:53 +01:00
gboolean get_all = FALSE;
2001-04-19 14:21:46 +02:00
gchar buf[POPBUFSIZE];
gchar id[IDLEN + 1];
2002-06-10 23:55:17 +02:00
gint next_state;
2001-04-19 14:21:46 +02:00
2002-03-06 10:06:40 +01:00
if (!state->uidl_table) new = TRUE;
2002-06-17 12:02:21 +02:00
if (state->ac_prefs->getall ||
(state->ac_prefs->rmmail && state->ac_prefs->msg_leave_time == 0))
2002-02-27 09:15:53 +01:00
get_all = TRUE;
2001-04-19 14:21:46 +02:00
2002-05-11 10:10:09 +02:00
if (pop3_ok(sock, NULL) != PS_SUCCESS) {
/* UIDL is not supported */
2002-06-10 23:55:17 +02:00
if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
return next_state;
2002-05-11 10:10:09 +02:00
if (!get_all)
return POP3_GETRANGE_LAST_SEND;
else
return POP3_GETSIZE_LIST_SEND;
}
2001-04-30 23:27:59 +02:00
while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
2001-04-19 14:21:46 +02:00
gint num;
2002-06-17 12:02:21 +02:00
time_t recv_time;
2001-04-19 14:21:46 +02:00
if (buf[0] == '.') break;
if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
continue;
2002-02-27 09:15:53 +01:00
if (num <= 0 || num > state->count) continue;
2001-04-19 14:21:46 +02:00
2002-02-27 09:15:53 +01:00
state->msg[num].uidl = g_strdup(id);
2001-04-19 14:21:46 +02:00
2002-06-17 12:02:21 +02:00
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 != 0)
state->msg[num].received = TRUE;
2002-06-17 12:02:21 +02:00
if (new == FALSE &&
(get_all || recv_time == 0 || state->ac_prefs->rmmail)) {
state->cur_msg = num;
new = TRUE;
}
2001-04-19 14:21:46 +02:00
}
2002-03-06 10:06:40 +01:00
state->uidl_is_valid = TRUE;
2002-06-10 23:55:17 +02:00
if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
return next_state;
2001-04-19 14:21:46 +02:00
if (new == TRUE)
2001-04-28 00:47:56 +02:00
return POP3_GETSIZE_LIST_SEND;
2001-04-19 14:21:46 +02:00
else
return POP3_LOGOUT_SEND;
}
2002-03-19 20:39:16 +01:00
2001-04-28 00:47:56 +02:00
gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
{
pop3_gen_send(sock, "LIST");
return POP3_GETSIZE_LIST_RECV;
}
gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
{
Pop3State *state = (Pop3State *)data;
gchar buf[POPBUFSIZE];
2002-06-10 23:55:17 +02:00
gint next_state;
2001-04-28 00:47:56 +02:00
if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
state->cur_total_bytes = 0;
2001-04-30 23:27:59 +02:00
while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
2001-08-22 12:58:38 +02:00
guint num, size;
2001-04-28 00:47:56 +02:00
if (buf[0] == '.') break;
2001-08-16 13:28:10 +02:00
if (sscanf(buf, "%u %u", &num, &size) != 2)
2001-08-22 12:58:38 +02:00
return -1;
2001-04-28 00:47:56 +02:00
2001-08-16 13:28:10 +02:00
if (num > 0 && num <= state->count)
2002-02-27 09:15:53 +01:00
state->msg[num].size = size;
2001-08-16 13:28:10 +02:00
if (num > 0 && num < state->cur_msg)
2001-04-28 00:47:56 +02:00
state->cur_total_bytes += size;
}
2002-06-10 23:55:17 +02:00
if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
return next_state;
LOOKUP_NEXT_MSG();
return POP3_RETR_SEND;
}
2002-02-22 10:35:36 +01:00
gint pop3_top_send(SockInfo *sock, gpointer data)
{
Pop3State *state = (Pop3State *)data;
inc_progress_update(state, POP3_TOP_SEND);
pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
return POP3_TOP_RECV;
}
gint pop3_top_recv(SockInfo *sock, gpointer data)
{
Pop3State *state = (Pop3State *)data;
gchar *filename, *path;
2002-06-10 23:55:17 +02:00
gint next_state;
2002-02-22 10:35:36 +01:00
if (pop3_ok(sock, NULL) != PS_SUCCESS)
return POP3_LOGOUT_SEND;
path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
if ( !is_dir_exist(path) )
make_dir_hier(path);
filename = g_strdup_printf("%s%i", path, state->cur_msg);
if (recv_write_to_file(sock, filename) < 0) {
state->inc_state = INC_NOSPACE;
return -1;
}
2002-06-10 23:55:17 +02:00
pop3_sd_state(state, POP3_TOP_RECV, &next_state);
2002-02-22 10:35:36 +01:00
if (state->cur_msg < state->count) {
state->cur_msg++;
return POP3_TOP_SEND;
} else
return POP3_LOGOUT_SEND;
2001-04-28 00:47:56 +02:00
}
2001-04-27 22:27:24 +02:00
gint pop3_retr_send(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
pop3_gen_send(sock, "RETR %d", state->cur_msg);
return POP3_RETR_RECV;
}
2001-04-27 22:27:24 +02:00
gint pop3_retr_recv(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
const gchar *file;
gint ok, drop_ok;
2002-06-10 23:55:17 +02:00
gint next_state;
2001-04-19 14:21:46 +02:00
if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
2002-02-23 11:52:54 +01:00
if (state->inc_state == INC_SUCCESS)
state->inc_state = INC_NOSPACE;
2001-04-19 14:21:46 +02:00
return -1;
}
2001-04-28 00:47:56 +02:00
2001-04-19 14:21:46 +02:00
if ((drop_ok = inc_drop_message(file, state)) < 0) {
state->inc_state = INC_ERROR;
return -1;
}
2002-06-10 23:55:17 +02:00
if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
return next_state;
2002-02-27 09:15:53 +01:00
state->cur_total_bytes += state->msg[state->cur_msg].size;
2001-08-22 12:58:38 +02:00
state->cur_total_num++;
2002-06-17 12:02:21 +02:00
state->msg[state->cur_msg].received = TRUE;
state->msg[state->cur_msg].recv_time = state->current_time;
2002-06-17 12:02:21 +02:00
if (state->ac_prefs->rmmail &&
state->ac_prefs->msg_leave_time == 0)
2001-04-19 14:21:46 +02:00
return POP3_DELETE_SEND;
if (state->cur_msg < state->count) {
state->cur_msg++;
2002-02-21 15:56:09 +01:00
LOOKUP_NEXT_MSG();
2001-04-19 14:21:46 +02:00
return POP3_RETR_SEND;
} else
return POP3_LOGOUT_SEND;
} else if (ok == PS_PROTOCOL)
return POP3_LOGOUT_SEND;
else
return -1;
}
2001-04-27 22:27:24 +02:00
gint pop3_delete_send(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
pop3_gen_send(sock, "DELE %d", state->cur_msg);
return POP3_DELETE_RECV;
}
2001-04-27 22:27:24 +02:00
gint pop3_delete_recv(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
Pop3State *state = (Pop3State *)data;
2002-06-10 23:55:17 +02:00
gint next_state;
2001-04-19 14:21:46 +02:00
gint ok;
if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
2002-06-10 23:55:17 +02:00
state->msg[state->cur_msg].deleted = TRUE;
if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
return next_state;
2002-02-22 10:35:36 +01:00
2001-04-19 14:21:46 +02:00
if (state->cur_msg < state->count) {
state->cur_msg++;
2002-02-21 15:56:09 +01:00
LOOKUP_NEXT_MSG();
2001-04-19 14:21:46 +02:00
return POP3_RETR_SEND;
} else
return POP3_LOGOUT_SEND;
} else if (ok == PS_PROTOCOL)
return POP3_LOGOUT_SEND;
else
return -1;
}
2001-04-27 22:27:24 +02:00
gint pop3_logout_send(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
pop3_gen_send(sock, "QUIT");
return POP3_LOGOUT_RECV;
}
2001-04-27 22:27:24 +02:00
gint pop3_logout_recv(SockInfo *sock, gpointer data)
2001-04-19 14:21:46 +02:00
{
if (pop3_ok(sock, NULL) == PS_SUCCESS)
return -1;
else
return -1;
}
2001-04-27 22:27:24 +02:00
static gint pop3_ok(SockInfo *sock, gchar *argbuf)
2001-04-19 14:21:46 +02:00
{
gint ok;
gchar buf[POPBUFSIZE + 1];
gchar *bufp;
if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
bufp = buf;
if (*bufp == '+' || *bufp == '-')
bufp++;
else
return PS_PROTOCOL;
while (isalpha(*bufp))
bufp++;
if (*bufp)
*(bufp++) = '\0';
if (!strcmp(buf, "+OK"))
ok = PS_SUCCESS;
else if (!strncmp(buf, "-ERR", 4)) {
if (strstr(bufp, "lock") ||
strstr(bufp, "Lock") ||
strstr(bufp, "LOCK") ||
strstr(bufp, "wait"))
ok = PS_LOCKBUSY;
else
ok = PS_PROTOCOL;
if (*bufp)
fprintf(stderr, "POP3: %s\n", bufp);
} else
ok = PS_PROTOCOL;
if (argbuf)
strcpy(argbuf, bufp);
}
return ok;
}
2001-04-27 22:27:24 +02:00
static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
2001-04-19 14:21:46 +02:00
{
gchar buf[POPBUFSIZE + 1];
va_list args;
va_start(args, format);
g_vsnprintf(buf, sizeof(buf) - 2, format, args);
va_end(args);
if (!strncasecmp(buf, "PASS ", 5))
log_print("POP3> PASS ********\n");
else
log_print("POP3> %s\n", buf);
strcat(buf, "\r\n");
sock_write(sock, buf, strlen(buf));
}
2001-04-27 22:27:24 +02:00
static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
2001-04-19 14:21:46 +02:00
{
2001-04-30 23:27:59 +02:00
if (sock_gets(sock, buf, size) < 0) {
2001-04-19 14:21:46 +02:00
return PS_SOCKET;
} else {
strretchomp(buf);
log_print("POP3< %s\n", buf);
return PS_SUCCESS;
}
}
2002-02-22 10:35:36 +01:00
2002-06-10 23:55:17 +02:00
static void pop3_sd_new_header(Pop3State *state)
2002-02-22 10:35:36 +01:00
{
2002-06-10 23:55:17 +02:00
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);
}
}
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_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;
}
2002-02-22 10:35:36 +01:00
2002-06-10 23:55:17 +02:00
*next_state = goto_state;
if (goto_state != -1)
2002-02-22 10:35:36 +01:00
return TRUE;
2002-06-10 23:55:17 +02:00
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;
2002-02-22 10:35:36 +01:00
}
}