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);
90 static gboolean should_delete (const char *uidl, gpointer data);
92 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
94 Pop3State *state = (Pop3State *)data;
95 gchar buf[POPBUFSIZE];
97 if (pop3_ok(sock, buf) == PS_SUCCESS) {
98 state->greeting = g_strdup(buf);
100 if (state->ac_prefs->ssl_pop == SSL_STARTTLS)
101 return POP3_STLS_SEND;
103 if (state->ac_prefs->protocol == A_APOP)
104 return POP3_GETAUTH_APOP_SEND;
106 return POP3_GETAUTH_USER_SEND;
112 gint pop3_stls_send(SockInfo *sock, gpointer data)
114 pop3_gen_send(sock, "STLS");
116 return POP3_STLS_RECV;
119 gint pop3_stls_recv(SockInfo *sock, gpointer data)
121 Pop3State *state = (Pop3State *)data;
124 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
125 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1))
127 if (state->ac_prefs->protocol == A_APOP)
128 return POP3_GETAUTH_APOP_SEND;
130 return POP3_GETAUTH_USER_SEND;
131 } else if (ok == PS_PROTOCOL) {
132 log_warning(_("can't start TLS session\n"));
133 state->error_val = PS_PROTOCOL;
134 state->inc_state = INC_ERROR;
135 return POP3_LOGOUT_SEND;
141 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
143 Pop3State *state = (Pop3State *)data;
145 g_return_val_if_fail(state->user != NULL, -1);
147 pop3_gen_send(sock, "USER %s", state->user);
149 return POP3_GETAUTH_USER_RECV;
152 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
154 Pop3State *state = (Pop3State *)data;
156 if (pop3_ok(sock, NULL) == PS_SUCCESS)
157 return POP3_GETAUTH_PASS_SEND;
159 log_warning(_("error occurred on authentication\n"));
160 state->error_val = PS_AUTHFAIL;
161 state->inc_state = INC_AUTH_FAILED;
166 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
168 Pop3State *state = (Pop3State *)data;
170 g_return_val_if_fail(state->pass != NULL, -1);
172 pop3_gen_send(sock, "PASS %s", state->pass);
174 return POP3_GETAUTH_PASS_RECV;
177 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
179 Pop3State *state = (Pop3State *)data;
181 if (pop3_ok(sock, NULL) == PS_SUCCESS)
182 return POP3_GETRANGE_STAT_SEND;
184 log_warning(_("error occurred on authentication\n"));
185 state->error_val = PS_AUTHFAIL;
186 state->inc_state = INC_AUTH_FAILED;
191 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
193 Pop3State *state = (Pop3State *)data;
198 g_return_val_if_fail(state->user != NULL, -1);
199 g_return_val_if_fail(state->pass != NULL, -1);
201 if ((start = strchr(state->greeting, '<')) == NULL) {
202 log_warning(_("Required APOP timestamp not found "
207 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
208 log_warning(_("Timestamp syntax error in greeting\n"));
214 apop_str = g_strconcat(start, state->pass, NULL);
215 md5_hex_digest(md5sum, apop_str);
218 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
220 return POP3_GETAUTH_APOP_RECV;
223 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
225 Pop3State *state = (Pop3State *)data;
227 if (pop3_ok(sock, NULL) == PS_SUCCESS)
228 return POP3_GETRANGE_STAT_SEND;
230 log_warning(_("error occurred on authentication\n"));
231 state->error_val = PS_AUTHFAIL;
232 state->inc_state = INC_AUTH_FAILED;
237 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
239 pop3_gen_send(sock, "STAT");
241 return POP3_GETRANGE_STAT_RECV;
244 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
246 Pop3State *state = (Pop3State *)data;
247 gchar buf[POPBUFSIZE + 1];
250 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
251 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
253 log_warning(_("POP3 protocol error\n"));
256 if (state->count == 0) {
257 state->uidl_is_valid = TRUE;
258 return POP3_LOGOUT_SEND;
261 (Pop3MsgInfo, state->count + 1);
263 return POP3_GETRANGE_UIDL_SEND;
266 } else if (ok == PS_PROTOCOL)
267 return POP3_LOGOUT_SEND;
272 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
274 pop3_gen_send(sock, "LAST");
276 return POP3_GETRANGE_LAST_RECV;
279 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
281 Pop3State *state = (Pop3State *)data;
282 gchar buf[POPBUFSIZE + 1];
284 if (pop3_ok(sock, buf) == PS_SUCCESS) {
287 if (sscanf(buf, "%d", &last) == 0) {
288 log_warning(_("POP3 protocol error\n"));
291 if (state->count == last)
292 return POP3_LOGOUT_SEND;
294 state->cur_msg = last + 1;
295 return POP3_GETSIZE_LIST_SEND;
299 return POP3_GETSIZE_LIST_SEND;
302 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
304 pop3_gen_send(sock, "UIDL");
306 return POP3_GETRANGE_UIDL_RECV;
309 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
311 Pop3State *state = (Pop3State *)data;
312 gboolean new = FALSE;
313 gboolean get_all = FALSE;
314 gchar buf[POPBUFSIZE];
318 if (!state->uidl_table) new = TRUE;
319 if (state->ac_prefs->getall ||
320 (state->ac_prefs->rmmail && state->ac_prefs->msg_leave_time == 0))
323 if (pop3_ok(sock, NULL) != PS_SUCCESS) {
324 /* UIDL is not supported */
325 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
329 return POP3_GETRANGE_LAST_SEND;
331 return POP3_GETSIZE_LIST_SEND;
334 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
338 if (buf[0] == '.') break;
339 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
341 if (num <= 0 || num > state->count) continue;
343 state->msg[num].uidl = g_strdup(id);
345 if (!state->uidl_table) continue;
347 recv_time = (time_t)g_hash_table_lookup(state->uidl_table, id);
348 state->msg[num].recv_time = recv_time;
350 if (!get_all && recv_time != 0)
351 state->msg[num].received = TRUE;
354 (get_all || recv_time == 0 || state->ac_prefs->rmmail)) {
355 state->cur_msg = num;
358 if (should_delete(buf, (Pop3State *) state))
359 state->uidl_todelete_list = g_slist_append
360 (state->uidl_todelete_list, g_strdup(buf));
364 state->uidl_is_valid = TRUE;
366 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
370 return POP3_GETSIZE_LIST_SEND;
372 return POP3_LOGOUT_SEND;
375 static gboolean should_delete(const char *uidl, gpointer data)
377 /* answer[0] will contain id
378 * answer[0] will contain uidl */
379 Pop3State *state = (Pop3State *) data;
383 int tdate, keep_for, today, nb_days;
388 if (!state->ac_prefs->rmmail || !strchr(uidl, ' '))
392 tuidl = g_strndup(uidl, strlen(uidl) - 2);
393 answer = g_strsplit(tuidl, " ", 2);
394 id = atoi(answer[0]);
396 if (NULL != (sdate = g_hash_table_lookup(state->uidl_table, answer[1]))) {
398 keep_for = atoi(state->ac_prefs->msg_leave_time); /* FIXME: leave time should be an int */
400 g_date_clear(&curdate, 1);
401 g_date_set_time(&curdate, time(NULL));
402 today = g_date_day_of_year(&curdate);
404 nb_days = g_date_is_leap_year(g_date_year(&curdate)) ? 366 : 365;
405 result = ((tdate + keep_for) % nb_days <= today);
415 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
417 pop3_gen_send(sock, "LIST");
419 return POP3_GETSIZE_LIST_RECV;
422 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
424 Pop3State *state = (Pop3State *)data;
425 gchar buf[POPBUFSIZE];
428 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
430 state->cur_total_bytes = 0;
432 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
435 if (buf[0] == '.') break;
436 if (sscanf(buf, "%u %u", &num, &size) != 2)
439 if (num > 0 && num <= state->count)
440 state->msg[num].size = size;
441 if (num > 0 && num < state->cur_msg)
442 state->cur_total_bytes += size;
445 if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
449 return POP3_RETR_SEND;
452 gint pop3_top_send(SockInfo *sock, gpointer data)
454 Pop3State *state = (Pop3State *)data;
456 inc_progress_update(state, POP3_TOP_SEND);
458 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
460 return POP3_TOP_RECV;
463 gint pop3_top_recv(SockInfo *sock, gpointer data)
465 Pop3State *state = (Pop3State *)data;
466 gchar *filename, *path;
469 if (pop3_ok(sock, NULL) != PS_SUCCESS)
470 return POP3_LOGOUT_SEND;
472 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
474 if ( !is_dir_exist(path) )
477 filename = g_strdup_printf("%s%i", path, state->cur_msg);
479 if (recv_write_to_file(sock, filename) < 0) {
480 state->inc_state = INC_NOSPACE;
484 pop3_sd_state(state, POP3_TOP_RECV, &next_state);
486 if (state->cur_msg < state->count) {
488 return POP3_TOP_SEND;
490 return POP3_LOGOUT_SEND;
493 gint pop3_retr_send(SockInfo *sock, gpointer data)
495 Pop3State *state = (Pop3State *)data;
497 pop3_gen_send(sock, "RETR %d", state->cur_msg);
499 return POP3_RETR_RECV;
502 gint pop3_retr_recv(SockInfo *sock, gpointer data)
504 Pop3State *state = (Pop3State *)data;
508 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
509 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
510 if (state->inc_state == INC_SUCCESS)
511 state->inc_state = INC_NOSPACE;
515 if ((drop_ok = inc_drop_message(file, state)) < 0) {
516 state->inc_state = INC_ERROR;
520 if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
523 state->cur_total_bytes += state->msg[state->cur_msg].size;
524 state->cur_total_num++;
526 state->msg[state->cur_msg].received = TRUE;
527 state->msg[state->cur_msg].recv_time = state->current_time;
529 if (state->ac_prefs->rmmail &&
530 state->ac_prefs->msg_leave_time == 0)
531 return POP3_DELETE_SEND;
533 if (state->cur_msg < state->count) {
536 return POP3_RETR_SEND;
538 return POP3_LOGOUT_SEND;
539 } else if (ok == PS_PROTOCOL)
540 return POP3_LOGOUT_SEND;
545 gint pop3_delete_send(SockInfo *sock, gpointer data)
547 Pop3State *state = (Pop3State *)data;
549 pop3_gen_send(sock, "DELE %d", state->cur_msg);
551 return POP3_DELETE_RECV;
554 gint pop3_delete_recv(SockInfo *sock, gpointer data)
556 Pop3State *state = (Pop3State *)data;
560 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
562 state->msg[state->cur_msg].deleted = TRUE;
564 if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
567 if (state->cur_msg < state->count) {
570 return POP3_RETR_SEND;
572 return POP3_LOGOUT_SEND;
573 } else if (ok == PS_PROTOCOL)
574 return POP3_LOGOUT_SEND;
579 gint pop3_logout_send(SockInfo *sock, gpointer data)
581 Pop3State *state = (Pop3State *)data;
584 while (state->uidl_todelete_list != NULL) {
586 * FIXME: doesn't feel right - no checks for parts
588 parts = g_strsplit((gchar *) state->uidl_todelete_list->data, " ", 2);
589 state->uidl_todelete_list = g_slist_remove
590 (state->uidl_todelete_list, state->uidl_todelete_list->data);
591 pop3_gen_send(sock, "DELE %s", parts[0]);
592 if (pop3_ok(sock, NULL) != PS_SUCCESS)
593 log_warning(_("error occurred on DELE\n"));
597 pop3_gen_send(sock, "QUIT");
599 return POP3_LOGOUT_RECV;
602 gint pop3_logout_recv(SockInfo *sock, gpointer data)
604 if (pop3_ok(sock, NULL) == PS_SUCCESS)
610 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
613 gchar buf[POPBUFSIZE + 1];
616 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
618 if (*bufp == '+' || *bufp == '-')
623 while (isalpha(*bufp))
629 if (!strcmp(buf, "+OK"))
631 else if (!strncmp(buf, "-ERR", 4)) {
632 if (strstr(bufp, "lock") ||
633 strstr(bufp, "Lock") ||
634 strstr(bufp, "LOCK") ||
635 strstr(bufp, "wait"))
641 fprintf(stderr, "POP3: %s\n", bufp);
646 strcpy(argbuf, bufp);
652 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
654 gchar buf[POPBUFSIZE + 1];
657 va_start(args, format);
658 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
661 if (!strncasecmp(buf, "PASS ", 5))
662 log_print("POP3> PASS ********\n");
664 log_print("POP3> %s\n", buf);
667 sock_write(sock, buf, strlen(buf));
670 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
672 if (sock_gets(sock, buf, size) < 0) {
676 log_print("POP3< %s\n", buf);
682 static void pop3_sd_new_header(Pop3State *state)
684 HeaderItems *new_msg;
685 if (state->cur_msg <= state->count) {
686 new_msg = g_new0(HeaderItems, 1);
688 new_msg->index = state->cur_msg;
689 new_msg->state = SD_UNCHECKED;
690 new_msg->size = state->msg[state->cur_msg].size;
691 new_msg->received = state->msg[state->cur_msg].received;
692 new_msg->del_by_old_session = FALSE;
694 state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list,
699 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state)
701 gint session = state->ac_prefs->session;
702 guint goto_state = -1;
705 case POP3_GETRANGE_UIDL_RECV:
709 case STYPE_PREVIEW_ALL:
710 goto_state = POP3_GETSIZE_LIST_SEND;
715 case POP3_GETSIZE_LIST_RECV:
717 case STYPE_PREVIEW_ALL:
719 case STYPE_PREVIEW_NEW:
720 goto_state = POP3_TOP_SEND;
723 if (pop3_sd_get_next(state))
724 goto_state = POP3_DELETE_SEND;
726 goto_state = POP3_LOGOUT_SEND;
729 if (pop3_sd_get_next(state))
730 goto_state = POP3_RETR_SEND;
732 goto_state = POP3_LOGOUT_SEND;
739 case STYPE_PREVIEW_ALL:
740 case STYPE_PREVIEW_NEW:
741 pop3_sd_new_header(state);
749 if (state->ac_prefs->sd_rmmail_on_download)
750 goto_state = POP3_DELETE_SEND;
752 if (pop3_sd_get_next(state))
753 goto_state = POP3_RETR_SEND;
755 goto_state = POP3_LOGOUT_SEND;
761 case POP3_DELETE_RECV:
764 if (pop3_sd_get_next(state))
765 goto_state = POP3_DELETE_SEND;
767 goto_state = POP3_LOGOUT_SEND;
770 if (pop3_sd_get_next(state))
771 goto_state = POP3_RETR_SEND;
773 goto_state = POP3_LOGOUT_SEND;
782 *next_state = goto_state;
783 if (goto_state != -1)
789 gboolean pop3_sd_get_next(Pop3State *state)
792 gint deleted_msgs = 0;
794 switch (state->ac_prefs->session) {
797 for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
798 HeaderItems *items = (HeaderItems*)cur->data;
800 if (items->del_by_old_session)
803 switch (items->state) {
805 items->state = SD_REMOVED;
808 items->state = SD_DOWNLOADED;
811 state->cur_msg = items->index - deleted_msgs;
812 if (state->ac_prefs->session == STYPE_DELETE)
813 items->state = SD_REMOVE;
815 items->state = SD_DOWNLOAD;