2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #include "prefs_account.h"
40 #include "selective_download.h"
45 #define LOOKUP_NEXT_MSG() \
48 PrefsAccount *ac = state->ac_prefs; \
50 gboolean size_limit_over; \
53 msg = &state->msg[state->cur_msg]; \
56 (ac->enable_size_limit && \
57 ac->size_limit > 0 && \
58 size > ac->size_limit * 1024); \
61 msg->recv_time != 0 && \
62 state->current_time - msg->recv_time >= \
63 ac->msg_leave_time * 24 * 60 * 60) { \
64 log_print(_("POP3: Deleting expired message %d\n"), \
66 return POP3_DELETE_SEND; \
69 if (size_limit_over) \
70 log_print(_("POP3: Skipping message %d (%d bytes)\n"), \
71 state->cur_msg, size); \
73 if (size == 0 || msg->received || size_limit_over) { \
74 state->cur_total_bytes += size; \
75 if (state->cur_msg == state->count) \
76 return POP3_LOGOUT_SEND; \
84 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
85 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
86 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
87 static gboolean pop3_sd_get_next (Pop3State *state);
88 static void pop3_sd_new_header(Pop3State *state);
89 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state);
91 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
93 Pop3State *state = (Pop3State *)data;
94 gchar buf[POPBUFSIZE];
96 if (pop3_ok(sock, buf) == PS_SUCCESS) {
97 state->greeting = g_strdup(buf);
99 if (state->ac_prefs->ssl_pop == SSL_STARTTLS)
100 return POP3_STLS_SEND;
102 if (state->ac_prefs->protocol == A_APOP)
103 return POP3_GETAUTH_APOP_SEND;
105 return POP3_GETAUTH_USER_SEND;
107 state->inc_state = INC_ERROR;
113 gint pop3_stls_send(SockInfo *sock, gpointer data)
115 pop3_gen_send(sock, "STLS");
117 return POP3_STLS_RECV;
120 gint pop3_stls_recv(SockInfo *sock, gpointer data)
122 Pop3State *state = (Pop3State *)data;
125 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
126 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1)) {
127 state->error_val = PS_SOCKET;
128 state->inc_state = INC_ERROR;
131 if (state->ac_prefs->protocol == A_APOP)
132 return POP3_GETAUTH_APOP_SEND;
134 return POP3_GETAUTH_USER_SEND;
135 } else if (ok == PS_PROTOCOL) {
136 log_warning(_("can't start TLS session\n"));
137 state->error_val = PS_PROTOCOL;
138 state->inc_state = INC_ERROR;
139 return POP3_LOGOUT_SEND;
141 state->inc_state = INC_ERROR;
147 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
149 Pop3State *state = (Pop3State *)data;
151 g_return_val_if_fail(state->user != NULL, -1);
153 pop3_gen_send(sock, "USER %s", state->user);
155 return POP3_GETAUTH_USER_RECV;
158 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
160 Pop3State *state = (Pop3State *)data;
162 if (pop3_ok(sock, NULL) == PS_SUCCESS)
163 return POP3_GETAUTH_PASS_SEND;
165 log_warning(_("error occurred on authentication\n"));
166 state->error_val = PS_AUTHFAIL;
167 state->inc_state = INC_AUTH_FAILED;
172 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
174 Pop3State *state = (Pop3State *)data;
176 g_return_val_if_fail(state->pass != NULL, -1);
178 pop3_gen_send(sock, "PASS %s", state->pass);
180 return POP3_GETAUTH_PASS_RECV;
183 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
185 Pop3State *state = (Pop3State *)data;
187 if (pop3_ok(sock, NULL) == PS_SUCCESS)
188 return POP3_GETRANGE_STAT_SEND;
190 log_warning(_("error occurred on authentication\n"));
191 state->error_val = PS_AUTHFAIL;
192 state->inc_state = INC_AUTH_FAILED;
197 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
199 Pop3State *state = (Pop3State *)data;
204 g_return_val_if_fail(state->user != NULL, -1);
205 g_return_val_if_fail(state->pass != NULL, -1);
207 if ((start = strchr(state->greeting, '<')) == NULL) {
208 log_warning(_("Required APOP timestamp not found "
210 state->error_val = PS_PROTOCOL;
211 state->inc_state = INC_ERROR;
215 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
216 log_warning(_("Timestamp syntax error in greeting\n"));
217 state->error_val = PS_PROTOCOL;
218 state->inc_state = INC_ERROR;
224 apop_str = g_strconcat(start, state->pass, NULL);
225 md5_hex_digest(md5sum, apop_str);
228 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
230 return POP3_GETAUTH_APOP_RECV;
233 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
235 Pop3State *state = (Pop3State *)data;
237 if (pop3_ok(sock, NULL) == PS_SUCCESS)
238 return POP3_GETRANGE_STAT_SEND;
240 log_warning(_("error occurred on authentication\n"));
241 state->error_val = PS_AUTHFAIL;
242 state->inc_state = INC_AUTH_FAILED;
247 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
249 pop3_gen_send(sock, "STAT");
251 return POP3_GETRANGE_STAT_RECV;
254 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
256 Pop3State *state = (Pop3State *)data;
257 gchar buf[POPBUFSIZE + 1];
260 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
261 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
263 log_warning(_("POP3 protocol error\n"));
264 state->error_val = PS_PROTOCOL;
265 state->inc_state = INC_ERROR;
268 if (state->count == 0) {
269 state->uidl_is_valid = TRUE;
270 return POP3_LOGOUT_SEND;
273 (Pop3MsgInfo, state->count + 1);
275 return POP3_GETRANGE_UIDL_SEND;
278 } else if (ok == PS_PROTOCOL) {
279 return POP3_LOGOUT_SEND;
281 state->inc_state = INC_ERROR;
286 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
288 pop3_gen_send(sock, "LAST");
290 return POP3_GETRANGE_LAST_RECV;
293 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
295 Pop3State *state = (Pop3State *)data;
296 gchar buf[POPBUFSIZE + 1];
298 if (pop3_ok(sock, buf) == PS_SUCCESS) {
301 if (sscanf(buf, "%d", &last) == 0) {
302 log_warning(_("POP3 protocol error\n"));
303 state->error_val = PS_PROTOCOL;
304 state->inc_state = INC_ERROR;
307 if (state->count == last)
308 return POP3_LOGOUT_SEND;
310 state->cur_msg = last + 1;
311 return POP3_GETSIZE_LIST_SEND;
315 return POP3_GETSIZE_LIST_SEND;
318 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
320 pop3_gen_send(sock, "UIDL");
322 return POP3_GETRANGE_UIDL_RECV;
325 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
327 Pop3State *state = (Pop3State *)data;
328 gboolean new = FALSE;
329 gboolean get_all = FALSE;
330 gchar buf[POPBUFSIZE];
335 if (!state->uidl_table) new = TRUE;
336 if (state->ac_prefs->getall ||
337 (state->ac_prefs->rmmail && state->ac_prefs->msg_leave_time == 0))
340 if (pop3_ok(sock, NULL) != PS_SUCCESS) {
341 /* UIDL is not supported */
342 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
346 return POP3_GETRANGE_LAST_SEND;
348 return POP3_GETSIZE_LIST_SEND;
351 while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
355 if (buf[0] == '.') break;
356 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
358 if (num <= 0 || num > state->count) continue;
360 state->msg[num].uidl = g_strdup(id);
362 if (!state->uidl_table) continue;
364 recv_time = (time_t)g_hash_table_lookup(state->uidl_table, id);
365 state->msg[num].recv_time = recv_time;
367 if (!get_all && recv_time != 0)
368 state->msg[num].received = TRUE;
371 (get_all || recv_time == 0 || state->ac_prefs->rmmail)) {
372 state->cur_msg = num;
378 log_error(_("Socket error\n"));
379 state->error_val = PS_SOCKET;
380 state->inc_state = INC_ERROR;
384 state->uidl_is_valid = TRUE;
385 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
389 return POP3_GETSIZE_LIST_SEND;
391 return POP3_LOGOUT_SEND;
394 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
396 pop3_gen_send(sock, "LIST");
398 return POP3_GETSIZE_LIST_RECV;
401 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
403 Pop3State *state = (Pop3State *)data;
404 gchar buf[POPBUFSIZE];
408 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
410 state->cur_total_bytes = 0;
411 state->cur_total_recv_bytes = 0;
413 while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
416 if (buf[0] == '.') break;
417 if (sscanf(buf, "%u %u", &num, &size) != 2) {
418 state->error_val = PS_PROTOCOL;
419 state->inc_state = INC_ERROR;
423 if (num > 0 && num <= state->count)
424 state->msg[num].size = size;
425 if (num > 0 && num < state->cur_msg)
426 state->cur_total_bytes += size;
430 log_error(_("Socket error\n"));
431 state->error_val = PS_SOCKET;
432 state->inc_state = INC_ERROR;
436 if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
440 return POP3_RETR_SEND;
443 gint pop3_top_send(SockInfo *sock, gpointer data)
445 Pop3State *state = (Pop3State *)data;
447 inc_progress_update(state, POP3_TOP_SEND);
449 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
451 return POP3_TOP_RECV;
454 gint pop3_top_recv(SockInfo *sock, gpointer data)
456 Pop3State *state = (Pop3State *)data;
457 gchar *filename, *path;
460 if (pop3_ok(sock, NULL) != PS_SUCCESS)
461 return POP3_LOGOUT_SEND;
463 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
465 if ( !is_dir_exist(path) )
468 filename = g_strdup_printf("%s%i", path, state->cur_msg);
470 if (recv_write_to_file(sock, filename) < 0) {
471 state->inc_state = INC_NOSPACE;
475 pop3_sd_state(state, POP3_TOP_RECV, &next_state);
477 if (state->cur_msg < state->count) {
479 return POP3_TOP_SEND;
481 return POP3_LOGOUT_SEND;
484 gint pop3_retr_send(SockInfo *sock, gpointer data)
486 Pop3State *state = (Pop3State *)data;
488 pop3_gen_send(sock, "RETR %d", state->cur_msg);
490 return POP3_RETR_RECV;
493 gint pop3_retr_recv(SockInfo *sock, gpointer data)
495 Pop3State *state = (Pop3State *)data;
499 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
500 file = get_tmp_file();
501 if (recv_write_to_file(sock, file) < 0) {
503 if (state->inc_state == INC_SUCCESS)
504 state->inc_state = INC_NOSPACE;
508 drop_ok = inc_drop_message(file, state);
511 state->inc_state = INC_ERROR;
515 if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
518 state->cur_total_bytes += state->msg[state->cur_msg].size;
519 state->cur_total_recv_bytes += state->msg[state->cur_msg].size;
520 state->cur_total_num++;
522 state->msg[state->cur_msg].received = TRUE;
523 state->msg[state->cur_msg].recv_time = state->current_time;
525 if (state->ac_prefs->rmmail &&
526 state->ac_prefs->msg_leave_time == 0)
527 return POP3_DELETE_SEND;
529 if (state->cur_msg < state->count) {
532 return POP3_RETR_SEND;
534 return POP3_LOGOUT_SEND;
535 } else if (ok == PS_PROTOCOL) {
536 return POP3_LOGOUT_SEND;
538 state->inc_state = INC_ERROR;
543 gint pop3_delete_send(SockInfo *sock, gpointer data)
545 Pop3State *state = (Pop3State *)data;
547 pop3_gen_send(sock, "DELE %d", state->cur_msg);
549 return POP3_DELETE_RECV;
552 gint pop3_delete_recv(SockInfo *sock, gpointer data)
554 Pop3State *state = (Pop3State *)data;
558 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
559 state->msg[state->cur_msg].deleted = TRUE;
561 if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
564 if (state->cur_msg < state->count) {
567 return POP3_RETR_SEND;
569 return POP3_LOGOUT_SEND;
570 } else if (ok == PS_PROTOCOL) {
571 return POP3_LOGOUT_SEND;
573 state->inc_state = INC_ERROR;
578 gint pop3_logout_send(SockInfo *sock, gpointer data)
580 pop3_gen_send(sock, "QUIT");
582 return POP3_LOGOUT_RECV;
585 gint pop3_logout_recv(SockInfo *sock, gpointer data)
587 Pop3State *state = (Pop3State *)data;
589 if (pop3_ok(sock, NULL) != PS_SUCCESS)
590 state->inc_state = INC_ERROR;
595 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
598 gchar buf[POPBUFSIZE + 1];
601 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
603 if (*bufp == '+' || *bufp == '-')
608 while (isalpha(*bufp))
614 if (!strcmp(buf, "+OK"))
616 else if (!strncmp(buf, "-ERR", 4)) {
617 if (strstr(bufp, "lock") ||
618 strstr(bufp, "Lock") ||
619 strstr(bufp, "LOCK") ||
620 strstr(bufp, "wait"))
626 fprintf(stderr, "POP3: %s\n", bufp);
631 strcpy(argbuf, bufp);
637 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
639 gchar buf[POPBUFSIZE + 1];
642 va_start(args, format);
643 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
646 if (!strncasecmp(buf, "PASS ", 5))
647 log_print("POP3> PASS ********\n");
649 log_print("POP3> %s\n", buf);
652 sock_write(sock, buf, strlen(buf));
655 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
657 if (sock_gets(sock, buf, size) < 0) {
661 log_print("POP3< %s\n", buf);
667 static void pop3_sd_new_header(Pop3State *state)
669 HeaderItems *new_msg;
670 if (state->cur_msg <= state->count) {
671 new_msg = g_new0(HeaderItems, 1);
673 new_msg->index = state->cur_msg;
674 new_msg->state = SD_UNCHECKED;
675 new_msg->size = state->msg[state->cur_msg].size;
676 new_msg->received = state->msg[state->cur_msg].received;
677 new_msg->del_by_old_session = FALSE;
679 state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list,
684 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state)
686 gint session = state->ac_prefs->session;
687 guint goto_state = -1;
690 case POP3_GETRANGE_UIDL_RECV:
694 case STYPE_PREVIEW_ALL:
695 goto_state = POP3_GETSIZE_LIST_SEND;
700 case POP3_GETSIZE_LIST_RECV:
702 case STYPE_PREVIEW_ALL:
704 case STYPE_PREVIEW_NEW:
705 goto_state = POP3_TOP_SEND;
708 if (pop3_sd_get_next(state))
709 goto_state = POP3_DELETE_SEND;
711 goto_state = POP3_LOGOUT_SEND;
714 if (pop3_sd_get_next(state))
715 goto_state = POP3_RETR_SEND;
717 goto_state = POP3_LOGOUT_SEND;
724 case STYPE_PREVIEW_ALL:
725 case STYPE_PREVIEW_NEW:
726 pop3_sd_new_header(state);
734 if (state->ac_prefs->sd_rmmail_on_download)
735 goto_state = POP3_DELETE_SEND;
737 if (pop3_sd_get_next(state))
738 goto_state = POP3_RETR_SEND;
740 goto_state = POP3_LOGOUT_SEND;
746 case POP3_DELETE_RECV:
749 if (pop3_sd_get_next(state))
750 goto_state = POP3_DELETE_SEND;
752 goto_state = POP3_LOGOUT_SEND;
755 if (pop3_sd_get_next(state))
756 goto_state = POP3_RETR_SEND;
758 goto_state = POP3_LOGOUT_SEND;
767 *next_state = goto_state;
768 if (goto_state != -1)
774 gboolean pop3_sd_get_next(Pop3State *state)
777 gint deleted_msgs = 0;
779 switch (state->ac_prefs->session) {
782 for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
783 HeaderItems *items = (HeaderItems*)cur->data;
785 if (items->del_by_old_session)
788 switch (items->state) {
790 items->state = SD_REMOVED;
793 items->state = SD_DOWNLOADED;
796 state->cur_msg = items->index - deleted_msgs;
797 if (state->ac_prefs->session == STYPE_DELETE)
798 items->state = SD_REMOVE;
800 items->state = SD_DOWNLOAD;