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;
412 while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
415 if (buf[0] == '.') break;
416 if (sscanf(buf, "%u %u", &num, &size) != 2) {
417 state->error_val = PS_PROTOCOL;
418 state->inc_state = INC_ERROR;
422 if (num > 0 && num <= state->count)
423 state->msg[num].size = size;
424 if (num > 0 && num < state->cur_msg)
425 state->cur_total_bytes += size;
429 log_error(_("Socket error\n"));
430 state->error_val = PS_SOCKET;
431 state->inc_state = INC_ERROR;
435 if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
439 return POP3_RETR_SEND;
442 gint pop3_top_send(SockInfo *sock, gpointer data)
444 Pop3State *state = (Pop3State *)data;
446 inc_progress_update(state, POP3_TOP_SEND);
448 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
450 return POP3_TOP_RECV;
453 gint pop3_top_recv(SockInfo *sock, gpointer data)
455 Pop3State *state = (Pop3State *)data;
456 gchar *filename, *path;
459 if (pop3_ok(sock, NULL) != PS_SUCCESS)
460 return POP3_LOGOUT_SEND;
462 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
464 if ( !is_dir_exist(path) )
467 filename = g_strdup_printf("%s%i", path, state->cur_msg);
469 if (recv_write_to_file(sock, filename) < 0) {
470 state->inc_state = INC_NOSPACE;
474 pop3_sd_state(state, POP3_TOP_RECV, &next_state);
476 if (state->cur_msg < state->count) {
478 return POP3_TOP_SEND;
480 return POP3_LOGOUT_SEND;
483 gint pop3_retr_send(SockInfo *sock, gpointer data)
485 Pop3State *state = (Pop3State *)data;
487 pop3_gen_send(sock, "RETR %d", state->cur_msg);
489 return POP3_RETR_RECV;
492 gint pop3_retr_recv(SockInfo *sock, gpointer data)
494 Pop3State *state = (Pop3State *)data;
498 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
499 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
500 if (state->inc_state == INC_SUCCESS)
501 state->inc_state = INC_NOSPACE;
505 if ((drop_ok = inc_drop_message(file, state)) < 0) {
506 state->inc_state = INC_ERROR;
510 if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
513 state->cur_total_bytes += state->msg[state->cur_msg].size;
514 state->cur_total_num++;
516 state->msg[state->cur_msg].received = TRUE;
517 state->msg[state->cur_msg].recv_time = state->current_time;
519 if (state->ac_prefs->rmmail &&
520 state->ac_prefs->msg_leave_time == 0)
521 return POP3_DELETE_SEND;
523 if (state->cur_msg < state->count) {
526 return POP3_RETR_SEND;
528 return POP3_LOGOUT_SEND;
529 } else if (ok == PS_PROTOCOL) {
530 return POP3_LOGOUT_SEND;
532 state->inc_state = INC_ERROR;
537 gint pop3_delete_send(SockInfo *sock, gpointer data)
539 Pop3State *state = (Pop3State *)data;
541 pop3_gen_send(sock, "DELE %d", state->cur_msg);
543 return POP3_DELETE_RECV;
546 gint pop3_delete_recv(SockInfo *sock, gpointer data)
548 Pop3State *state = (Pop3State *)data;
552 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
553 state->msg[state->cur_msg].deleted = TRUE;
555 if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
558 if (state->cur_msg < state->count) {
561 return POP3_RETR_SEND;
563 return POP3_LOGOUT_SEND;
564 } else if (ok == PS_PROTOCOL) {
565 return POP3_LOGOUT_SEND;
567 state->inc_state = INC_ERROR;
572 gint pop3_logout_send(SockInfo *sock, gpointer data)
574 pop3_gen_send(sock, "QUIT");
576 return POP3_LOGOUT_RECV;
579 gint pop3_logout_recv(SockInfo *sock, gpointer data)
581 Pop3State *state = (Pop3State *)data;
583 if (pop3_ok(sock, NULL) != PS_SUCCESS)
584 state->inc_state = INC_ERROR;
589 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
592 gchar buf[POPBUFSIZE + 1];
595 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
597 if (*bufp == '+' || *bufp == '-')
602 while (isalpha(*bufp))
608 if (!strcmp(buf, "+OK"))
610 else if (!strncmp(buf, "-ERR", 4)) {
611 if (strstr(bufp, "lock") ||
612 strstr(bufp, "Lock") ||
613 strstr(bufp, "LOCK") ||
614 strstr(bufp, "wait"))
620 fprintf(stderr, "POP3: %s\n", bufp);
625 strcpy(argbuf, bufp);
631 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
633 gchar buf[POPBUFSIZE + 1];
636 va_start(args, format);
637 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
640 if (!strncasecmp(buf, "PASS ", 5))
641 log_print("POP3> PASS ********\n");
643 log_print("POP3> %s\n", buf);
646 sock_write(sock, buf, strlen(buf));
649 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
651 if (sock_gets(sock, buf, size) < 0) {
655 log_print("POP3< %s\n", buf);
661 static void pop3_sd_new_header(Pop3State *state)
663 HeaderItems *new_msg;
664 if (state->cur_msg <= state->count) {
665 new_msg = g_new0(HeaderItems, 1);
667 new_msg->index = state->cur_msg;
668 new_msg->state = SD_UNCHECKED;
669 new_msg->size = state->msg[state->cur_msg].size;
670 new_msg->received = state->msg[state->cur_msg].received;
671 new_msg->del_by_old_session = FALSE;
673 state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list,
678 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state)
680 gint session = state->ac_prefs->session;
681 guint goto_state = -1;
684 case POP3_GETRANGE_UIDL_RECV:
688 case STYPE_PREVIEW_ALL:
689 goto_state = POP3_GETSIZE_LIST_SEND;
694 case POP3_GETSIZE_LIST_RECV:
696 case STYPE_PREVIEW_ALL:
698 case STYPE_PREVIEW_NEW:
699 goto_state = POP3_TOP_SEND;
702 if (pop3_sd_get_next(state))
703 goto_state = POP3_DELETE_SEND;
705 goto_state = POP3_LOGOUT_SEND;
708 if (pop3_sd_get_next(state))
709 goto_state = POP3_RETR_SEND;
711 goto_state = POP3_LOGOUT_SEND;
718 case STYPE_PREVIEW_ALL:
719 case STYPE_PREVIEW_NEW:
720 pop3_sd_new_header(state);
728 if (state->ac_prefs->sd_rmmail_on_download)
729 goto_state = POP3_DELETE_SEND;
731 if (pop3_sd_get_next(state))
732 goto_state = POP3_RETR_SEND;
734 goto_state = POP3_LOGOUT_SEND;
740 case POP3_DELETE_RECV:
743 if (pop3_sd_get_next(state))
744 goto_state = POP3_DELETE_SEND;
746 goto_state = POP3_LOGOUT_SEND;
749 if (pop3_sd_get_next(state))
750 goto_state = POP3_RETR_SEND;
752 goto_state = POP3_LOGOUT_SEND;
761 *next_state = goto_state;
762 if (goto_state != -1)
768 gboolean pop3_sd_get_next(Pop3State *state)
771 gint deleted_msgs = 0;
773 switch (state->ac_prefs->session) {
776 for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
777 HeaderItems *items = (HeaderItems*)cur->data;
779 if (items->del_by_old_session)
782 switch (items->state) {
784 items->state = SD_REMOVED;
787 items->state = SD_DOWNLOADED;
790 state->cur_msg = items->index - deleted_msgs;
791 if (state->ac_prefs->session == STYPE_DELETE)
792 items->state = SD_REMOVE;
794 items->state = SD_DOWNLOAD;