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;
111 gint pop3_stls_send(SockInfo *sock, gpointer data)
113 pop3_gen_send(sock, "STLS");
115 return POP3_STLS_RECV;
118 gint pop3_stls_recv(SockInfo *sock, gpointer data)
120 Pop3State *state = (Pop3State *)data;
123 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
124 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1))
126 if (state->ac_prefs->protocol == A_APOP)
127 return POP3_GETAUTH_APOP_SEND;
129 return POP3_GETAUTH_USER_SEND;
130 } else if (ok == PS_PROTOCOL) {
131 log_warning(_("can't start TLS session\n"));
132 state->error_val = PS_PROTOCOL;
133 state->inc_state = INC_ERROR;
134 return POP3_LOGOUT_SEND;
140 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
142 Pop3State *state = (Pop3State *)data;
144 g_return_val_if_fail(state->user != NULL, -1);
146 pop3_gen_send(sock, "USER %s", state->user);
148 return POP3_GETAUTH_USER_RECV;
151 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
153 Pop3State *state = (Pop3State *)data;
155 if (pop3_ok(sock, NULL) == PS_SUCCESS)
156 return POP3_GETAUTH_PASS_SEND;
158 log_warning(_("error occurred on authentication\n"));
159 state->error_val = PS_AUTHFAIL;
160 state->inc_state = INC_AUTH_FAILED;
165 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
167 Pop3State *state = (Pop3State *)data;
169 g_return_val_if_fail(state->pass != NULL, -1);
171 pop3_gen_send(sock, "PASS %s", state->pass);
173 return POP3_GETAUTH_PASS_RECV;
176 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
178 Pop3State *state = (Pop3State *)data;
180 if (pop3_ok(sock, NULL) == PS_SUCCESS)
181 return POP3_GETRANGE_STAT_SEND;
183 log_warning(_("error occurred on authentication\n"));
184 state->error_val = PS_AUTHFAIL;
185 state->inc_state = INC_AUTH_FAILED;
190 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
192 Pop3State *state = (Pop3State *)data;
197 g_return_val_if_fail(state->user != NULL, -1);
198 g_return_val_if_fail(state->pass != NULL, -1);
200 if ((start = strchr(state->greeting, '<')) == NULL) {
201 log_warning(_("Required APOP timestamp not found "
206 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
207 log_warning(_("Timestamp syntax error in greeting\n"));
213 apop_str = g_strconcat(start, state->pass, NULL);
214 md5_hex_digest(md5sum, apop_str);
217 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
219 return POP3_GETAUTH_APOP_RECV;
222 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
224 Pop3State *state = (Pop3State *)data;
226 if (pop3_ok(sock, NULL) == PS_SUCCESS)
227 return POP3_GETRANGE_STAT_SEND;
229 log_warning(_("error occurred on authentication\n"));
230 state->error_val = PS_AUTHFAIL;
231 state->inc_state = INC_AUTH_FAILED;
236 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
238 pop3_gen_send(sock, "STAT");
240 return POP3_GETRANGE_STAT_RECV;
243 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
245 Pop3State *state = (Pop3State *)data;
246 gchar buf[POPBUFSIZE + 1];
249 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
250 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
252 log_warning(_("POP3 protocol error\n"));
255 if (state->count == 0) {
256 state->uidl_is_valid = TRUE;
257 return POP3_LOGOUT_SEND;
260 (Pop3MsgInfo, state->count + 1);
262 return POP3_GETRANGE_UIDL_SEND;
265 } else if (ok == PS_PROTOCOL)
266 return POP3_LOGOUT_SEND;
271 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
273 pop3_gen_send(sock, "LAST");
275 return POP3_GETRANGE_LAST_RECV;
278 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
280 Pop3State *state = (Pop3State *)data;
281 gchar buf[POPBUFSIZE + 1];
283 if (pop3_ok(sock, buf) == PS_SUCCESS) {
286 if (sscanf(buf, "%d", &last) == 0) {
287 log_warning(_("POP3 protocol error\n"));
290 if (state->count == last)
291 return POP3_LOGOUT_SEND;
293 state->cur_msg = last + 1;
294 return POP3_GETSIZE_LIST_SEND;
298 return POP3_GETSIZE_LIST_SEND;
301 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
303 pop3_gen_send(sock, "UIDL");
305 return POP3_GETRANGE_UIDL_RECV;
308 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
310 Pop3State *state = (Pop3State *)data;
311 gboolean new = FALSE;
312 gboolean get_all = FALSE;
313 gchar buf[POPBUFSIZE];
317 if (!state->uidl_table) new = TRUE;
318 if (state->ac_prefs->getall ||
319 (state->ac_prefs->rmmail && state->ac_prefs->msg_leave_time == 0))
322 if (pop3_ok(sock, NULL) != PS_SUCCESS) {
323 /* UIDL is not supported */
324 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
328 return POP3_GETRANGE_LAST_SEND;
330 return POP3_GETSIZE_LIST_SEND;
333 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
337 if (buf[0] == '.') break;
338 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
340 if (num <= 0 || num > state->count) continue;
342 state->msg[num].uidl = g_strdup(id);
344 if (!state->uidl_table) continue;
346 recv_time = (time_t)g_hash_table_lookup(state->uidl_table, id);
347 state->msg[num].recv_time = recv_time;
349 if (!get_all && recv_time != 0)
350 state->msg[num].received = TRUE;
353 (get_all || recv_time == 0 || state->ac_prefs->rmmail)) {
354 state->cur_msg = num;
359 state->uidl_is_valid = TRUE;
361 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
365 return POP3_GETSIZE_LIST_SEND;
367 return POP3_LOGOUT_SEND;
370 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
372 pop3_gen_send(sock, "LIST");
374 return POP3_GETSIZE_LIST_RECV;
377 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
379 Pop3State *state = (Pop3State *)data;
380 gchar buf[POPBUFSIZE];
383 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
385 state->cur_total_bytes = 0;
387 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
390 if (buf[0] == '.') break;
391 if (sscanf(buf, "%u %u", &num, &size) != 2)
394 if (num > 0 && num <= state->count)
395 state->msg[num].size = size;
396 if (num > 0 && num < state->cur_msg)
397 state->cur_total_bytes += size;
400 if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
404 return POP3_RETR_SEND;
407 gint pop3_top_send(SockInfo *sock, gpointer data)
409 Pop3State *state = (Pop3State *)data;
411 inc_progress_update(state, POP3_TOP_SEND);
413 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
415 return POP3_TOP_RECV;
418 gint pop3_top_recv(SockInfo *sock, gpointer data)
420 Pop3State *state = (Pop3State *)data;
421 gchar *filename, *path;
424 if (pop3_ok(sock, NULL) != PS_SUCCESS)
425 return POP3_LOGOUT_SEND;
427 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
429 if ( !is_dir_exist(path) )
432 filename = g_strdup_printf("%s%i", path, state->cur_msg);
434 if (recv_write_to_file(sock, filename) < 0) {
435 state->inc_state = INC_NOSPACE;
439 pop3_sd_state(state, POP3_TOP_RECV, &next_state);
441 if (state->cur_msg < state->count) {
443 return POP3_TOP_SEND;
445 return POP3_LOGOUT_SEND;
448 gint pop3_retr_send(SockInfo *sock, gpointer data)
450 Pop3State *state = (Pop3State *)data;
452 pop3_gen_send(sock, "RETR %d", state->cur_msg);
454 return POP3_RETR_RECV;
457 gint pop3_retr_recv(SockInfo *sock, gpointer data)
459 Pop3State *state = (Pop3State *)data;
463 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
464 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
465 if (state->inc_state == INC_SUCCESS)
466 state->inc_state = INC_NOSPACE;
470 if ((drop_ok = inc_drop_message(file, state)) < 0) {
471 state->inc_state = INC_ERROR;
475 if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
478 state->cur_total_bytes += state->msg[state->cur_msg].size;
479 state->cur_total_num++;
481 state->msg[state->cur_msg].received = TRUE;
482 state->msg[state->cur_msg].recv_time = state->current_time;
484 if (state->ac_prefs->rmmail &&
485 state->ac_prefs->msg_leave_time == 0)
486 return POP3_DELETE_SEND;
488 if (state->cur_msg < state->count) {
491 return POP3_RETR_SEND;
493 return POP3_LOGOUT_SEND;
494 } else if (ok == PS_PROTOCOL)
495 return POP3_LOGOUT_SEND;
500 gint pop3_delete_send(SockInfo *sock, gpointer data)
502 Pop3State *state = (Pop3State *)data;
504 pop3_gen_send(sock, "DELE %d", state->cur_msg);
506 return POP3_DELETE_RECV;
509 gint pop3_delete_recv(SockInfo *sock, gpointer data)
511 Pop3State *state = (Pop3State *)data;
515 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
517 state->msg[state->cur_msg].deleted = TRUE;
519 if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
522 if (state->cur_msg < state->count) {
525 return POP3_RETR_SEND;
527 return POP3_LOGOUT_SEND;
528 } else if (ok == PS_PROTOCOL)
529 return POP3_LOGOUT_SEND;
534 gint pop3_logout_send(SockInfo *sock, gpointer data)
536 pop3_gen_send(sock, "QUIT");
538 return POP3_LOGOUT_RECV;
541 gint pop3_logout_recv(SockInfo *sock, gpointer data)
543 if (pop3_ok(sock, NULL) == PS_SUCCESS)
549 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
552 gchar buf[POPBUFSIZE + 1];
555 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
557 if (*bufp == '+' || *bufp == '-')
562 while (isalpha(*bufp))
568 if (!strcmp(buf, "+OK"))
570 else if (!strncmp(buf, "-ERR", 4)) {
571 if (strstr(bufp, "lock") ||
572 strstr(bufp, "Lock") ||
573 strstr(bufp, "LOCK") ||
574 strstr(bufp, "wait"))
580 fprintf(stderr, "POP3: %s\n", bufp);
585 strcpy(argbuf, bufp);
591 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
593 gchar buf[POPBUFSIZE + 1];
596 va_start(args, format);
597 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
600 if (!strncasecmp(buf, "PASS ", 5))
601 log_print("POP3> PASS ********\n");
603 log_print("POP3> %s\n", buf);
606 sock_write(sock, buf, strlen(buf));
609 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
611 if (sock_gets(sock, buf, size) < 0) {
615 log_print("POP3< %s\n", buf);
621 static void pop3_sd_new_header(Pop3State *state)
623 HeaderItems *new_msg;
624 if (state->cur_msg <= state->count) {
625 new_msg = g_new0(HeaderItems, 1);
627 new_msg->index = state->cur_msg;
628 new_msg->state = SD_UNCHECKED;
629 new_msg->size = state->msg[state->cur_msg].size;
630 new_msg->received = state->msg[state->cur_msg].received;
631 new_msg->del_by_old_session = FALSE;
633 state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list,
638 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state)
640 gint session = state->ac_prefs->session;
641 guint goto_state = -1;
644 case POP3_GETRANGE_UIDL_RECV:
648 case STYPE_PREVIEW_ALL:
649 goto_state = POP3_GETSIZE_LIST_SEND;
654 case POP3_GETSIZE_LIST_RECV:
656 case STYPE_PREVIEW_ALL:
658 case STYPE_PREVIEW_NEW:
659 goto_state = POP3_TOP_SEND;
662 if (pop3_sd_get_next(state))
663 goto_state = POP3_DELETE_SEND;
665 goto_state = POP3_LOGOUT_SEND;
668 if (pop3_sd_get_next(state))
669 goto_state = POP3_RETR_SEND;
671 goto_state = POP3_LOGOUT_SEND;
678 case STYPE_PREVIEW_ALL:
679 case STYPE_PREVIEW_NEW:
680 pop3_sd_new_header(state);
688 if (state->ac_prefs->sd_rmmail_on_download)
689 goto_state = POP3_DELETE_SEND;
691 if (pop3_sd_get_next(state))
692 goto_state = POP3_RETR_SEND;
694 goto_state = POP3_LOGOUT_SEND;
700 case POP3_DELETE_RECV:
703 if (pop3_sd_get_next(state))
704 goto_state = POP3_DELETE_SEND;
706 goto_state = POP3_LOGOUT_SEND;
709 if (pop3_sd_get_next(state))
710 goto_state = POP3_RETR_SEND;
712 goto_state = POP3_LOGOUT_SEND;
721 *next_state = goto_state;
722 if (goto_state != -1)
728 gboolean pop3_sd_get_next(Pop3State *state)
731 gint deleted_msgs = 0;
733 switch (state->ac_prefs->session) {
736 for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
737 HeaderItems *items = (HeaderItems*)cur->data;
739 if (items->del_by_old_session)
742 switch (items->state) {
744 items->state = SD_REMOVED;
747 items->state = SD_DOWNLOADED;
750 state->cur_msg = items->index - deleted_msgs;
751 if (state->ac_prefs->session == STYPE_DELETE)
752 items->state = SD_REMOVE;
754 items->state = SD_DOWNLOAD;