2001-04-19 14:21:46 +02:00
|
|
|
/*
|
|
|
|
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
|
2003-03-25 09:29:02 +01:00
|
|
|
* Copyright (C) 1999-2003 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>
|
2002-08-31 11:41:45 +02:00
|
|
|
#include <errno.h>
|
2001-04-19 14:21:46 +02:00
|
|
|
|
|
|
|
#include "intl.h"
|
|
|
|
#include "pop.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"
|
2003-03-26 23:31:30 +01:00
|
|
|
|
2002-12-02 18:41:39 +01:00
|
|
|
#include "log.h"
|
2003-03-25 09:29:02 +01:00
|
|
|
|
|
|
|
static gint pop3_greeting_recv (Pop3Session *session,
|
|
|
|
const gchar *msg);
|
|
|
|
static gint pop3_getauth_user_send (Pop3Session *session);
|
|
|
|
static gint pop3_getauth_pass_send (Pop3Session *session);
|
|
|
|
static gint pop3_getauth_apop_send (Pop3Session *session);
|
2002-12-08 14:52:10 +01:00
|
|
|
#if USE_OPENSSL
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_stls_send (Pop3Session *session);
|
|
|
|
static gint pop3_stls_recv (Pop3Session *session);
|
2002-05-29 10:28:56 +02:00
|
|
|
#endif
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getrange_stat_send (Pop3Session *session);
|
|
|
|
static gint pop3_getrange_stat_recv (Pop3Session *session,
|
|
|
|
const gchar *msg);
|
|
|
|
static gint pop3_getrange_last_send (Pop3Session *session);
|
|
|
|
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);
|
|
|
|
static gint pop3_getsize_list_send (Pop3Session *session);
|
|
|
|
static gint pop3_getsize_list_recv (Pop3Session *session,
|
|
|
|
const gchar *msg);
|
|
|
|
static gint pop3_retr_send (Pop3Session *session);
|
|
|
|
static gint pop3_retr_recv (Pop3Session *session,
|
|
|
|
const gchar *data,
|
|
|
|
guint len);
|
|
|
|
static gint pop3_delete_send (Pop3Session *session);
|
|
|
|
static gint pop3_delete_recv (Pop3Session *session);
|
|
|
|
static gint pop3_logout_send (Pop3Session *session);
|
|
|
|
|
|
|
|
static void pop3_gen_send (Pop3Session *session,
|
|
|
|
const gchar *format, ...);
|
|
|
|
|
|
|
|
static void pop3_session_destroy (Session *session);
|
|
|
|
|
|
|
|
static gint pop3_write_msg_to_file (const gchar *file,
|
|
|
|
const gchar *data,
|
|
|
|
guint len);
|
|
|
|
|
|
|
|
static Pop3State pop3_lookup_next (Pop3Session *session);
|
|
|
|
static gint pop3_ok (Pop3Session *session,
|
|
|
|
const gchar *msg);
|
|
|
|
|
|
|
|
static gint pop3_session_recv_msg (Session *session,
|
|
|
|
const gchar *msg);
|
|
|
|
static gint pop3_session_recv_data_finished (Session *session,
|
|
|
|
guchar *data,
|
|
|
|
guint len);
|
2003-03-26 23:31:30 +01:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_greeting_recv(Pop3Session *session, const gchar *msg)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_GREETING;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
session->greeting = g_strdup(msg);
|
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2002-12-08 14:52:10 +01:00
|
|
|
#if USE_OPENSSL
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_stls_send(Pop3Session *session)
|
2002-05-29 10:28:56 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_STLS;
|
|
|
|
pop3_gen_send(session, "STLS");
|
|
|
|
return PS_SUCCESS;
|
2002-05-29 10:28:56 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_stls_recv(Pop3Session *session)
|
2002-05-29 10:28:56 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
if (session_start_tls(SESSION(session)) < 0) {
|
|
|
|
session->error_val = PS_SOCKET;
|
2002-05-29 10:28:56 +02:00
|
|
|
return -1;
|
2002-08-01 10:37:12 +02:00
|
|
|
}
|
2003-03-25 09:29:02 +01:00
|
|
|
return PS_SUCCESS;
|
2002-05-29 10:28:56 +02:00
|
|
|
}
|
2002-12-08 14:52:10 +01:00
|
|
|
#endif /* USE_OPENSSL */
|
2002-05-29 10:28:56 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getauth_user_send(Pop3Session *session)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
g_return_val_if_fail(session->user != NULL, -1);
|
2001-10-08 19:23:00 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_GETAUTH_USER;
|
|
|
|
pop3_gen_send(session, "USER %s", session->user);
|
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getauth_pass_send(Pop3Session *session)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
g_return_val_if_fail(session->pass != NULL, -1);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_GETAUTH_PASS;
|
|
|
|
pop3_gen_send(session, "PASS %s", session->pass);
|
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getauth_apop_send(Pop3Session *session)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
g_return_val_if_fail(session->user != NULL, -1);
|
|
|
|
g_return_val_if_fail(session->pass != NULL, -1);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_GETAUTH_APOP;
|
|
|
|
|
|
|
|
if ((start = strchr(session->greeting, '<')) == NULL) {
|
2001-04-19 14:21:46 +02:00
|
|
|
log_warning(_("Required APOP timestamp not found "
|
|
|
|
"in greeting\n"));
|
2003-03-25 09:29:02 +01:00
|
|
|
session->error_val = PS_PROTOCOL;
|
2001-04-19 14:21:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((end = strchr(start, '>')) == NULL || end == start + 1) {
|
|
|
|
log_warning(_("Timestamp syntax error in greeting\n"));
|
2003-03-25 09:29:02 +01:00
|
|
|
session->error_val = PS_PROTOCOL;
|
2001-04-19 14:21:46 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(end + 1) = '\0';
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
apop_str = g_strconcat(start, session->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);
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
pop3_gen_send(session, "APOP %s %s", session->user, md5sum);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getrange_stat_send(Pop3Session *session)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_GETRANGE_STAT;
|
|
|
|
pop3_gen_send(session, "STAT");
|
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getrange_stat_recv(Pop3Session *session, const gchar *msg)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
if (sscanf(msg, "%d %d", &session->count, &session->total_bytes) != 2) {
|
|
|
|
log_warning(_("POP3 protocol error\n"));
|
|
|
|
session->error_val = PS_PROTOCOL;
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (session->count == 0) {
|
|
|
|
session->uidl_is_valid = TRUE;
|
2001-04-19 14:21:46 +02:00
|
|
|
} else {
|
2003-03-25 09:29:02 +01:00
|
|
|
session->msg = g_new0(Pop3MsgInfo, session->count + 1);
|
|
|
|
session->cur_msg = 1;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
2002-08-01 10:37:12 +02:00
|
|
|
}
|
2003-03-25 09:29:02 +01:00
|
|
|
|
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getrange_last_send(Pop3Session *session)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_GETRANGE_LAST;
|
|
|
|
pop3_gen_send(session, "LAST");
|
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getrange_last_recv(Pop3Session *session, const gchar *msg)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
gint last;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
if (sscanf(msg, "%d", &last) == 0) {
|
|
|
|
log_warning(_("POP3 protocol error\n"));
|
|
|
|
session->error_val = PS_PROTOCOL;
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (session->count > last)
|
|
|
|
session->cur_msg = last + 1;
|
|
|
|
else
|
|
|
|
session->cur_msg = 0;
|
|
|
|
}
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getrange_uidl_send(Pop3Session *session)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_GETRANGE_UIDL;
|
|
|
|
pop3_gen_send(session, "UIDL");
|
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *msg)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
|
|
|
gchar id[IDLEN + 1];
|
2003-03-25 09:29:02 +01:00
|
|
|
gint num;
|
|
|
|
time_t recv_time;
|
2002-06-10 23:55:17 +02:00
|
|
|
gint next_state;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
if (msg[0] == '.') {
|
|
|
|
session->uidl_is_valid = TRUE;
|
|
|
|
return PS_SUCCESS;
|
2002-05-11 10:10:09 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
if (sscanf(msg, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
|
|
|
|
return -1;
|
|
|
|
if (num <= 0 || num > session->count)
|
|
|
|
return -1;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
session->msg[num].uidl = g_strdup(id);
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
if (!session->uidl_table)
|
|
|
|
return PS_CONTINUE;
|
2002-06-17 12:02:21 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
recv_time = (time_t)g_hash_table_lookup(session->uidl_table, id);
|
|
|
|
session->msg[num].recv_time = recv_time;
|
2002-06-17 12:02:21 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE)
|
|
|
|
session->msg[num].received = TRUE;
|
2002-03-19 18:13:22 +01:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
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;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
return PS_CONTINUE;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
2002-03-19 20:39:16 +01:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getsize_list_send(Pop3Session *session)
|
2001-04-28 00:47:56 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_GETSIZE_LIST;
|
|
|
|
pop3_gen_send(session, "LIST");
|
|
|
|
return PS_SUCCESS;
|
2001-04-28 00:47:56 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *msg)
|
2001-04-28 00:47:56 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
guint num, size;
|
2002-06-10 23:55:17 +02:00
|
|
|
gint next_state;
|
2001-04-28 00:47:56 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
if (msg[0] == '.')
|
|
|
|
return PS_SUCCESS;
|
2001-04-28 00:47:56 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
if (sscanf(msg, "%u %u", &num, &size) != 2) {
|
|
|
|
session->error_val = PS_PROTOCOL;
|
2002-08-01 10:37:12 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
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;
|
2002-06-10 23:55:17 +02:00
|
|
|
}
|
2002-02-22 10:35:36 +01:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_retr_send(Pop3Session *session)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_RETR;
|
|
|
|
pop3_gen_send(session, "RETR %d", session->cur_msg);
|
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2002-08-10 11:19:15 +02:00
|
|
|
gchar *file;
|
2003-03-25 09:29:02 +01:00
|
|
|
gint drop_ok;
|
2002-06-10 23:55:17 +02:00
|
|
|
gint next_state;
|
2001-04-28 00:47:56 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
file = get_tmp_file();
|
|
|
|
if (pop3_write_msg_to_file(file, data, len) < 0) {
|
2002-08-10 11:19:15 +02:00
|
|
|
g_free(file);
|
2003-03-25 09:29:02 +01:00
|
|
|
session->error_val = PS_IOERR;
|
2001-04-19 14:21:46 +02:00
|
|
|
return -1;
|
2002-08-01 10:37:12 +02:00
|
|
|
}
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
/* drop_ok: 0: success 1: don't receive -1: error */
|
|
|
|
drop_ok = inc_drop_message(file, session);
|
|
|
|
g_free(file);
|
|
|
|
if (drop_ok < 0) {
|
|
|
|
session->error_val = PS_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
2003-03-26 23:31:30 +01:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
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++;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
session->msg[session->cur_msg].received = TRUE;
|
|
|
|
session->msg[session->cur_msg].recv_time =
|
|
|
|
drop_ok == 1 ? RECV_TIME_KEEP : session->current_time;
|
2001-04-19 14:21:46 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_delete_send(Pop3Session *session)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_DELETE;
|
|
|
|
pop3_gen_send(session, "DELE %d", session->cur_msg);
|
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_delete_recv(Pop3Session *session)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
session->msg[session->cur_msg].deleted = TRUE;
|
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static gint pop3_logout_send(Pop3Session *session)
|
2001-04-19 14:21:46 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_LOGOUT;
|
|
|
|
pop3_gen_send(session, "QUIT");
|
|
|
|
return PS_SUCCESS;
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static void pop3_gen_send(Pop3Session *session, 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);
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
|
2001-04-19 14:21:46 +02:00
|
|
|
}
|
2002-06-10 23:55:17 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
Session *pop3_session_new(PrefsAccount *account)
|
2002-08-31 11:41:45 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
Pop3Session *session;
|
2002-08-31 11:41:45 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail(account != NULL, NULL);
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
session = g_new0(Pop3Session, 1);
|
|
|
|
|
|
|
|
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;
|
|
|
|
SESSION(session)->send_data_finished = NULL;
|
2002-08-31 11:41:45 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
SESSION(session)->destroy = pop3_session_destroy;
|
2002-08-31 11:41:45 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
session->state = POP3_READY;
|
|
|
|
session->ac_prefs = account;
|
|
|
|
session->uidl_table = pop3_get_uidl_table(account);
|
|
|
|
session->current_time = time(NULL);
|
|
|
|
session->error_val = PS_SUCCESS;
|
|
|
|
|
|
|
|
return SESSION(session);
|
2002-08-31 11:41:45 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
static void pop3_session_destroy(Session *session)
|
2002-08-31 11:41:45 +02:00
|
|
|
{
|
2003-03-25 09:29:02 +01:00
|
|
|
Pop3Session *pop3_session = POP3_SESSION(session);
|
2002-08-31 11:41:45 +02:00
|
|
|
gint n;
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
g_return_if_fail(session != NULL);
|
2002-08-31 11:41:45 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
for (n = 1; n <= pop3_session->count; n++)
|
|
|
|
g_free(pop3_session->msg[n].uidl);
|
|
|
|
g_free(pop3_session->msg);
|
2002-08-31 11:41:45 +02:00
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
if (pop3_session->uidl_table) {
|
|
|
|
hash_free_strings(pop3_session->uidl_table);
|
|
|
|
g_hash_table_destroy(pop3_session->uidl_table);
|
2002-08-31 11:41:45 +02:00
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
g_free(pop3_session->greeting);
|
|
|
|
g_free(pop3_session->user);
|
|
|
|
g_free(pop3_session->pass);
|
2002-08-31 11:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
|
|
|
|
{
|
|
|
|
GHashTable *table;
|
|
|
|
gchar *path;
|
|
|
|
FILE *fp;
|
2002-09-25 08:15:31 +02:00
|
|
|
gchar buf[POPBUFSIZE];
|
|
|
|
gchar uidl[POPBUFSIZE];
|
2002-08-31 11:41:45 +02:00
|
|
|
time_t recv_time;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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 (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
|
|
|
|
g_free(path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(path);
|
|
|
|
|
|
|
|
table = g_hash_table_new(g_str_hash, g_str_equal);
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) {
|
|
|
|
strretchomp(buf);
|
2002-09-25 08:15:31 +02:00
|
|
|
recv_time = RECV_TIME_NONE;
|
2002-08-31 11:41:45 +02:00
|
|
|
if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
|
|
|
|
if (sscanf(buf, "%s", uidl) != 1)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
recv_time = now;
|
|
|
|
}
|
2002-09-25 08:15:31 +02:00
|
|
|
if (recv_time == RECV_TIME_NONE)
|
|
|
|
recv_time = RECV_TIME_RECEIVED;
|
2002-08-31 11:41:45 +02:00
|
|
|
g_hash_table_insert(table, g_strdup(uidl),
|
|
|
|
GINT_TO_POINTER(recv_time));
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
gint pop3_write_uidl_list(Pop3Session *session)
|
2002-08-31 11:41:45 +02:00
|
|
|
{
|
|
|
|
gchar *path;
|
|
|
|
FILE *fp;
|
|
|
|
Pop3MsgInfo *msg;
|
|
|
|
gint n;
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
if (!session->uidl_is_valid) return 0;
|
2002-08-31 11:41:45 +02:00
|
|
|
|
|
|
|
path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
|
|
|
|
"uidl", G_DIR_SEPARATOR_S,
|
2003-03-25 09:29:02 +01:00
|
|
|
session->ac_prefs->recv_server,
|
|
|
|
"-", session->ac_prefs->userid, NULL);
|
2002-08-31 11:41:45 +02:00
|
|
|
if ((fp = fopen(path, "wb")) == NULL) {
|
|
|
|
FILE_OP_ERROR(path, "fopen");
|
|
|
|
g_free(path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-03-25 09:29:02 +01:00
|
|
|
for (n = 1; n <= session->count; n++) {
|
|
|
|
msg = &session->msg[n];
|
2002-08-31 11:41:45 +02:00
|
|
|
if (msg->uidl && msg->received && !msg->deleted)
|
|
|
|
fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
|
|
|
|
g_free(path);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2003-03-25 09:29:02 +01:00
|
|
|
|
|
|
|
static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
|
|
|
|
guint len)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
const gchar *prev, *cur;
|
|
|
|
|
|
|
|
g_return_val_if_fail(file != NULL, -1);
|
|
|
|
|
|
|
|
if ((fp = fopen(file, "wb")) == NULL) {
|
|
|
|
FILE_OP_ERROR(file, "fopen");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change_file_mode_rw(fp, file) < 0)
|
|
|
|
FILE_OP_ERROR(file, "chmod");
|
|
|
|
|
|
|
|
/* +------------------+----------------+--------------------------+ *
|
|
|
|
* ^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) ||
|
|
|
|
fputc('\n', fp) == EOF) {
|
|
|
|
FILE_OP_ERROR(file, "fwrite");
|
|
|
|
g_warning("can't write to file: %s\n", file);
|
|
|
|
fclose(fp);
|
|
|
|
unlink(file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cur == data + len - 1) {
|
|
|
|
prev = cur + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*(cur + 1) == '\n')
|
|
|
|
prev = cur + 2;
|
|
|
|
else
|
|
|
|
prev = cur + 1;
|
|
|
|
|
|
|
|
if (prev - data >= len)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prev - data < len &&
|
|
|
|
fwrite(prev, len - (prev - data), 1, fp) < 1) {
|
|
|
|
FILE_OP_ERROR(file, "fwrite");
|
|
|
|
g_warning("can't write to file: %s\n", file);
|
|
|
|
fclose(fp);
|
|
|
|
unlink(file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (data[len - 1] != '\r' && data[len - 1] != '\n') {
|
|
|
|
if (fputc('\n', fp) == EOF) {
|
|
|
|
FILE_OP_ERROR(file, "fputc");
|
|
|
|
g_warning("can't write to file: %s\n", file);
|
|
|
|
fclose(fp);
|
|
|
|
unlink(file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fclose(fp) == EOF) {
|
|
|
|
FILE_OP_ERROR(file, "fclose");
|
|
|
|
unlink(file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Pop3State pop3_lookup_next(Pop3Session *session)
|
|
|
|
{
|
|
|
|
Pop3MsgInfo *msg;
|
|
|
|
PrefsAccount *ac = session->ac_prefs;
|
|
|
|
gint size;
|
|
|
|
gboolean size_limit_over;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
msg = &session->msg[session->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 != RECV_TIME_NONE &&
|
|
|
|
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"),
|
|
|
|
session->cur_msg);
|
|
|
|
pop3_delete_send(session);
|
|
|
|
return POP3_DELETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size_limit_over)
|
|
|
|
log_print
|
|
|
|
(_("POP3: Skipping message %d (%d bytes)\n"),
|
|
|
|
session->cur_msg, size);
|
|
|
|
|
|
|
|
if (size == 0 || msg->received || size_limit_over) {
|
|
|
|
session->cur_total_bytes += size;
|
|
|
|
if (session->cur_msg == session->count) {
|
|
|
|
pop3_logout_send(session);
|
|
|
|
return POP3_LOGOUT;
|
|
|
|
} else
|
|
|
|
session->cur_msg++;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pop3_retr_send(session);
|
|
|
|
return POP3_RETR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint pop3_ok(Pop3Session *session, const gchar *msg)
|
|
|
|
{
|
|
|
|
gint ok;
|
|
|
|
|
|
|
|
if (!strncmp(msg, "+OK", 3))
|
|
|
|
ok = PS_SUCCESS;
|
|
|
|
else if (!strncmp(msg, "-ERR", 4)) {
|
|
|
|
if (strstr(msg + 4, "lock") ||
|
|
|
|
strstr(msg + 4, "Lock") ||
|
|
|
|
strstr(msg + 4, "LOCK") ||
|
|
|
|
strstr(msg + 4, "wait"))
|
|
|
|
ok = PS_LOCKBUSY;
|
|
|
|
else
|
|
|
|
ok = PS_PROTOCOL;
|
|
|
|
|
|
|
|
fprintf(stderr, "POP3: %s\n", msg);
|
|
|
|
} else
|
|
|
|
ok = PS_PROTOCOL;
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint pop3_session_recv_msg(Session *session, const gchar *msg)
|
|
|
|
{
|
|
|
|
Pop3Session *pop3_session = POP3_SESSION(session);
|
|
|
|
gint val;
|
|
|
|
const gchar *body;
|
|
|
|
|
|
|
|
body = msg;
|
|
|
|
if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
|
2003-03-25 18:45:32 +01:00
|
|
|
pop3_session->state != POP3_GETSIZE_LIST_RECV) {
|
2003-03-25 09:29:02 +01:00
|
|
|
log_print("POP3< %s\n", msg);
|
|
|
|
val = pop3_ok(pop3_session, msg);
|
|
|
|
if (val != PS_SUCCESS) {
|
|
|
|
pop3_session->state = POP3_ERROR;
|
|
|
|
pop3_session->error_val = val;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*body == '+' || *body == '-')
|
|
|
|
body++;
|
|
|
|
while (isalpha(*body))
|
|
|
|
body++;
|
|
|
|
while (isspace(*body))
|
|
|
|
body++;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (pop3_session->state) {
|
|
|
|
case POP3_READY:
|
|
|
|
case POP3_GREETING:
|
|
|
|
pop3_greeting_recv(pop3_session, body);
|
|
|
|
#if USE_OPENSSL
|
|
|
|
if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
|
|
|
|
pop3_stls_send(pop3_session);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (pop3_session->ac_prefs->protocol == A_APOP)
|
|
|
|
pop3_getauth_apop_send(pop3_session);
|
|
|
|
else
|
|
|
|
pop3_getauth_user_send(pop3_session);
|
|
|
|
break;
|
|
|
|
#if USE_OPENSSL
|
|
|
|
case POP3_STLS:
|
|
|
|
if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
|
|
|
|
return -1;
|
|
|
|
if (pop3_session->ac_prefs->protocol == A_APOP)
|
|
|
|
pop3_getauth_apop_send(pop3_session);
|
|
|
|
else
|
|
|
|
pop3_getauth_user_send(pop3_session);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case POP3_GETAUTH_USER:
|
|
|
|
pop3_getauth_pass_send(pop3_session);
|
|
|
|
break;
|
|
|
|
case POP3_GETAUTH_PASS:
|
|
|
|
case POP3_GETAUTH_APOP:
|
|
|
|
pop3_getrange_stat_send(pop3_session);
|
|
|
|
break;
|
|
|
|
case POP3_GETRANGE_STAT:
|
|
|
|
if (pop3_getrange_stat_recv(pop3_session, body) < 0)
|
|
|
|
return -1;
|
|
|
|
if (pop3_session->count > 0)
|
|
|
|
pop3_getrange_uidl_send(pop3_session);
|
|
|
|
else
|
|
|
|
pop3_logout_send(pop3_session);
|
|
|
|
break;
|
|
|
|
case POP3_GETRANGE_LAST:
|
|
|
|
if (pop3_getrange_last_recv(pop3_session, body) < 0)
|
|
|
|
return -1;
|
|
|
|
if (pop3_session->cur_msg > 0)
|
|
|
|
pop3_getsize_list_send(pop3_session);
|
|
|
|
else
|
|
|
|
pop3_logout_send(pop3_session);
|
|
|
|
break;
|
|
|
|
case POP3_GETRANGE_UIDL:
|
|
|
|
pop3_session->state = POP3_GETRANGE_UIDL_RECV;
|
|
|
|
return 1;
|
|
|
|
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;
|
|
|
|
break;
|
|
|
|
case POP3_RETR:
|
|
|
|
pop3_session->state = POP3_RETR_RECV;
|
|
|
|
session_recv_data
|
|
|
|
(session,
|
|
|
|
pop3_session->msg[pop3_session->cur_msg].size, TRUE);
|
|
|
|
break;
|
|
|
|
case POP3_DELETE:
|
|
|
|
pop3_delete_recv(pop3_session);
|
|
|
|
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_LOGOUT:
|
|
|
|
session_disconnect(session);
|
|
|
|
break;
|
|
|
|
case POP3_ERROR:
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint pop3_session_recv_data_finished(Session *session, guchar *data,
|
|
|
|
guint len)
|
|
|
|
{
|
|
|
|
Pop3Session *pop3_session = POP3_SESSION(session);
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (pop3_retr_recv(pop3_session, data, len) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2003-03-25 18:45:32 +01:00
|
|
|
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;
|
|
|
|
}
|
2003-03-25 09:29:02 +01:00
|
|
|
|
2003-03-25 18:45:32 +01:00
|
|
|
return 0;
|
2003-03-25 09:29:02 +01:00
|
|
|
}
|