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.
37 #include "prefs_account.h"
41 #include "selective_download.h"
46 #define LOOKUP_NEXT_MSG() \
49 PrefsAccount *ac = state->ac_prefs; \
51 gboolean size_limit_over; \
54 msg = &state->msg[state->cur_msg]; \
57 (ac->enable_size_limit && \
58 ac->size_limit > 0 && \
59 size > ac->size_limit * 1024); \
62 msg->recv_time != RECV_TIME_NONE && \
63 msg->recv_time != RECV_TIME_KEEP && \
64 state->current_time - msg->recv_time >= \
65 ac->msg_leave_time * 24 * 60 * 60) { \
66 log_print(_("POP3: Deleting expired message %d\n"), \
68 return POP3_DELETE_SEND; \
71 if (size_limit_over) \
72 log_print(_("POP3: Skipping message %d (%d bytes)\n"), \
73 state->cur_msg, size); \
75 if (size == 0 || msg->received || size_limit_over) { \
76 state->cur_total_bytes += size; \
77 if (state->cur_msg == state->count) \
78 return POP3_LOGOUT_SEND; \
86 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
87 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
88 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
89 static gboolean pop3_sd_get_next (Pop3State *state);
90 static void pop3_sd_new_header(Pop3State *state);
91 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state);
93 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
95 Pop3State *state = (Pop3State *)data;
96 gchar buf[POPBUFSIZE];
99 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
100 state->greeting = g_strdup(buf);
102 if (state->ac_prefs->ssl_pop == SSL_STARTTLS)
103 return POP3_STLS_SEND;
105 if (state->ac_prefs->protocol == A_APOP)
106 return POP3_GETAUTH_APOP_SEND;
108 return POP3_GETAUTH_USER_SEND;
110 state->error_val = ok;
116 gint pop3_stls_send(SockInfo *sock, gpointer data)
118 pop3_gen_send(sock, "STLS");
120 return POP3_STLS_RECV;
123 gint pop3_stls_recv(SockInfo *sock, gpointer data)
125 Pop3State *state = (Pop3State *)data;
128 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
129 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1)) {
130 state->error_val = PS_SOCKET;
133 if (state->ac_prefs->protocol == A_APOP)
134 return POP3_GETAUTH_APOP_SEND;
136 return POP3_GETAUTH_USER_SEND;
137 } else if (ok == PS_PROTOCOL) {
138 log_warning(_("can't start TLS session\n"));
139 state->error_val = ok;
140 return POP3_LOGOUT_SEND;
142 state->error_val = ok;
148 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
150 Pop3State *state = (Pop3State *)data;
152 g_return_val_if_fail(state->user != NULL, -1);
154 pop3_gen_send(sock, "USER %s", state->user);
156 return POP3_GETAUTH_USER_RECV;
159 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
161 Pop3State *state = (Pop3State *)data;
163 if (pop3_ok(sock, NULL) == PS_SUCCESS)
164 return POP3_GETAUTH_PASS_SEND;
166 log_warning(_("error occurred on authentication\n"));
167 state->error_val = PS_AUTHFAIL;
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;
188 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS)
189 return POP3_GETRANGE_STAT_SEND;
190 else if (ok == PS_LOCKBUSY) {
191 log_warning(_("mailbox is locked\n"));
192 state->error_val = ok;
195 log_warning(_("error occurred on authentication\n"));
196 state->error_val = PS_AUTHFAIL;
201 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
203 Pop3State *state = (Pop3State *)data;
208 g_return_val_if_fail(state->user != NULL, -1);
209 g_return_val_if_fail(state->pass != NULL, -1);
211 if ((start = strchr(state->greeting, '<')) == NULL) {
212 log_warning(_("Required APOP timestamp not found "
214 state->error_val = PS_PROTOCOL;
218 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
219 log_warning(_("Timestamp syntax error in greeting\n"));
220 state->error_val = PS_PROTOCOL;
226 apop_str = g_strconcat(start, state->pass, NULL);
227 md5_hex_digest(md5sum, apop_str);
230 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
232 return POP3_GETAUTH_APOP_RECV;
235 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
237 Pop3State *state = (Pop3State *)data;
240 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS)
241 return POP3_GETRANGE_STAT_SEND;
242 else if (ok == PS_LOCKBUSY) {
243 log_warning(_("mailbox is locked\n"));
244 state->error_val = ok;
247 log_warning(_("error occurred on authentication\n"));
248 state->error_val = PS_AUTHFAIL;
253 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
255 pop3_gen_send(sock, "STAT");
257 return POP3_GETRANGE_STAT_RECV;
260 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
262 Pop3State *state = (Pop3State *)data;
263 gchar buf[POPBUFSIZE + 1];
266 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
267 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
269 log_warning(_("POP3 protocol error\n"));
270 state->error_val = PS_PROTOCOL;
273 if (state->count == 0) {
274 state->uidl_is_valid = TRUE;
275 return POP3_LOGOUT_SEND;
278 (Pop3MsgInfo, state->count + 1);
280 return POP3_GETRANGE_UIDL_SEND;
283 } else if (ok == PS_PROTOCOL) {
284 state->error_val = ok;
285 return POP3_LOGOUT_SEND;
287 state->error_val = ok;
292 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
294 pop3_gen_send(sock, "LAST");
296 return POP3_GETRANGE_LAST_RECV;
299 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
301 Pop3State *state = (Pop3State *)data;
302 gchar buf[POPBUFSIZE + 1];
304 if (pop3_ok(sock, buf) == PS_SUCCESS) {
307 if (sscanf(buf, "%d", &last) == 0) {
308 log_warning(_("POP3 protocol error\n"));
309 state->error_val = PS_PROTOCOL;
312 if (state->count == last)
313 return POP3_LOGOUT_SEND;
315 state->cur_msg = last + 1;
316 return POP3_GETSIZE_LIST_SEND;
320 return POP3_GETSIZE_LIST_SEND;
323 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
325 pop3_gen_send(sock, "UIDL");
327 return POP3_GETRANGE_UIDL_RECV;
330 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
332 Pop3State *state = (Pop3State *)data;
333 gboolean new = FALSE;
334 gboolean get_all = FALSE;
335 gchar buf[POPBUFSIZE];
340 if (!state->uidl_table) new = TRUE;
342 if (state->ac_prefs->getall ||
343 (state->ac_prefs->rmmail && state->ac_prefs->msg_leave_time == 0))
345 if (state->ac_prefs->getall)
348 if (pop3_ok(sock, NULL) != PS_SUCCESS) {
349 /* UIDL is not supported */
350 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
354 return POP3_GETRANGE_LAST_SEND;
356 return POP3_GETSIZE_LIST_SEND;
359 while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
363 if (buf[0] == '.') break;
364 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
366 if (num <= 0 || num > state->count) continue;
368 state->msg[num].uidl = g_strdup(id);
370 if (!state->uidl_table) continue;
372 recv_time = (time_t)g_hash_table_lookup(state->uidl_table, id);
373 state->msg[num].recv_time = recv_time;
375 if (!get_all && recv_time != RECV_TIME_NONE)
376 state->msg[num].received = TRUE;
379 (get_all || recv_time == RECV_TIME_NONE ||
380 state->ac_prefs->rmmail)) {
381 state->cur_msg = num;
387 log_error(_("Socket error\n"));
388 state->error_val = PS_SOCKET;
392 state->uidl_is_valid = TRUE;
393 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
397 return POP3_GETSIZE_LIST_SEND;
399 return POP3_LOGOUT_SEND;
402 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
404 pop3_gen_send(sock, "LIST");
406 return POP3_GETSIZE_LIST_RECV;
409 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
411 Pop3State *state = (Pop3State *)data;
412 gchar buf[POPBUFSIZE];
416 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
418 state->cur_total_bytes = 0;
419 state->cur_total_recv_bytes = 0;
421 while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
424 if (buf[0] == '.') break;
425 if (sscanf(buf, "%u %u", &num, &size) != 2) {
426 state->error_val = PS_PROTOCOL;
430 if (num > 0 && num <= state->count)
431 state->msg[num].size = size;
432 if (num > 0 && num < state->cur_msg)
433 state->cur_total_bytes += size;
437 log_error(_("Socket error\n"));
438 state->error_val = PS_SOCKET;
442 if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
446 return POP3_RETR_SEND;
449 gint pop3_top_send(SockInfo *sock, gpointer data)
451 Pop3State *state = (Pop3State *)data;
453 inc_progress_update(state, POP3_TOP_SEND);
455 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
457 return POP3_TOP_RECV;
460 gint pop3_top_recv(SockInfo *sock, gpointer data)
462 Pop3State *state = (Pop3State *)data;
463 gchar *filename, *path;
466 if (pop3_ok(sock, NULL) != PS_SUCCESS)
467 return POP3_LOGOUT_SEND;
469 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
471 if ( !is_dir_exist(path) )
474 filename = g_strdup_printf("%s%i", path, state->cur_msg);
476 if ( (write_val = recv_write_to_file(sock, filename)) < 0) {
477 state->error_val = (write_val == -1 ? PS_IOERR : PS_SOCKET);
481 pop3_sd_state(state, POP3_TOP_RECV, &next_state);
483 if (state->cur_msg < state->count) {
485 return POP3_TOP_SEND;
487 return POP3_LOGOUT_SEND;
490 gint pop3_retr_send(SockInfo *sock, gpointer data)
492 Pop3State *state = (Pop3State *)data;
494 pop3_gen_send(sock, "RETR %d", state->cur_msg);
496 return POP3_RETR_RECV;
499 gint pop3_retr_recv(SockInfo *sock, gpointer data)
501 Pop3State *state = (Pop3State *)data;
506 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
507 file = get_tmp_file();
508 if ((write_val = recv_write_to_file(sock, file)) < 0) {
510 if (!state->cancelled)
512 (write_val == -1 ? PS_IOERR : PS_SOCKET);
516 /* drop_ok: 0: success 1: don't receive -1: error */
517 drop_ok = inc_drop_message(file, state);
520 state->error_val = PS_ERROR;
524 if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
527 state->cur_total_bytes += state->msg[state->cur_msg].size;
528 state->cur_total_recv_bytes += state->msg[state->cur_msg].size;
529 state->cur_total_num++;
531 state->msg[state->cur_msg].received = TRUE;
532 state->msg[state->cur_msg].recv_time =
533 drop_ok == 1 ? RECV_TIME_KEEP : state->current_time;
535 if (drop_ok == 0 && state->ac_prefs->rmmail &&
536 state->ac_prefs->msg_leave_time == 0)
537 return POP3_DELETE_SEND;
539 if (state->cur_msg < state->count) {
542 return POP3_RETR_SEND;
544 return POP3_LOGOUT_SEND;
545 } else if (ok == PS_PROTOCOL) {
546 state->error_val = ok;
547 return POP3_LOGOUT_SEND;
549 state->error_val = ok;
554 gint pop3_delete_send(SockInfo *sock, gpointer data)
556 Pop3State *state = (Pop3State *)data;
558 pop3_gen_send(sock, "DELE %d", state->cur_msg);
560 return POP3_DELETE_RECV;
563 gint pop3_delete_recv(SockInfo *sock, gpointer data)
565 Pop3State *state = (Pop3State *)data;
569 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
570 state->msg[state->cur_msg].deleted = TRUE;
572 if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
575 if (state->cur_msg < state->count) {
578 return POP3_RETR_SEND;
580 return POP3_LOGOUT_SEND;
581 } else if (ok == PS_PROTOCOL) {
582 state->error_val = ok;
583 return POP3_LOGOUT_SEND;
585 state->error_val = ok;
590 gint pop3_logout_send(SockInfo *sock, gpointer data)
592 pop3_gen_send(sock, "QUIT");
594 return POP3_LOGOUT_RECV;
597 gint pop3_logout_recv(SockInfo *sock, gpointer data)
599 Pop3State *state = (Pop3State *)data;
602 if ((ok = pop3_ok(sock, NULL)) != PS_SUCCESS)
603 state->error_val = ok;
608 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
611 gchar buf[POPBUFSIZE + 1];
614 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
616 if (*bufp == '+' || *bufp == '-')
621 while (isalpha(*bufp))
627 if (!strcmp(buf, "+OK"))
629 else if (!strncmp(buf, "-ERR", 4)) {
630 if (strstr(bufp, "lock") ||
631 strstr(bufp, "Lock") ||
632 strstr(bufp, "LOCK") ||
633 strstr(bufp, "wait"))
639 fprintf(stderr, "POP3: %s\n", bufp);
644 strcpy(argbuf, bufp);
650 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
652 gchar buf[POPBUFSIZE + 1];
655 va_start(args, format);
656 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
659 if (!strncasecmp(buf, "PASS ", 5))
660 log_print("POP3> PASS ********\n");
662 log_print("POP3> %s\n", buf);
665 sock_write(sock, buf, strlen(buf));
668 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
670 if (sock_gets(sock, buf, size) < 0) {
674 log_print("POP3< %s\n", buf);
680 static void pop3_sd_new_header(Pop3State *state)
682 HeaderItems *new_msg;
683 if (state->cur_msg <= state->count) {
684 new_msg = g_new0(HeaderItems, 1);
686 new_msg->index = state->cur_msg;
687 new_msg->state = SD_UNCHECKED;
688 new_msg->size = state->msg[state->cur_msg].size;
689 new_msg->received = state->msg[state->cur_msg].received;
690 new_msg->del_by_old_session = FALSE;
692 state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list,
697 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state)
699 gint session = state->ac_prefs->session;
700 guint goto_state = -1;
703 case POP3_GETRANGE_UIDL_RECV:
705 case STYPE_POP_BEFORE_SMTP:
706 goto_state = POP3_LOGOUT_SEND;
710 case STYPE_PREVIEW_ALL:
711 goto_state = POP3_GETSIZE_LIST_SEND;
716 case POP3_GETSIZE_LIST_RECV:
718 case STYPE_PREVIEW_ALL:
720 case STYPE_PREVIEW_NEW:
721 goto_state = POP3_TOP_SEND;
724 if (pop3_sd_get_next(state))
725 goto_state = POP3_DELETE_SEND;
727 goto_state = POP3_LOGOUT_SEND;
730 if (pop3_sd_get_next(state))
731 goto_state = POP3_RETR_SEND;
733 goto_state = POP3_LOGOUT_SEND;
740 case STYPE_PREVIEW_ALL:
741 case STYPE_PREVIEW_NEW:
742 pop3_sd_new_header(state);
750 if (state->ac_prefs->sd_rmmail_on_download)
751 goto_state = POP3_DELETE_SEND;
753 if (pop3_sd_get_next(state))
754 goto_state = POP3_RETR_SEND;
756 goto_state = POP3_LOGOUT_SEND;
762 case POP3_DELETE_RECV:
765 if (pop3_sd_get_next(state))
766 goto_state = POP3_DELETE_SEND;
768 goto_state = POP3_LOGOUT_SEND;
771 if (pop3_sd_get_next(state))
772 goto_state = POP3_RETR_SEND;
774 goto_state = POP3_LOGOUT_SEND;
783 *next_state = goto_state;
784 if (goto_state != -1)
790 gboolean pop3_sd_get_next(Pop3State *state)
793 gint deleted_msgs = 0;
795 switch (state->ac_prefs->session) {
798 for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
799 HeaderItems *items = (HeaderItems*)cur->data;
801 if (items->del_by_old_session)
804 switch (items->state) {
806 items->state = SD_REMOVED;
809 items->state = SD_DOWNLOADED;
812 state->cur_msg = items->index - deleted_msgs;
813 if (state->ac_prefs->session == STYPE_DELETE)
814 items->state = SD_REMOVE;
816 items->state = SD_DOWNLOAD;
828 Pop3State *pop3_state_new(PrefsAccount *account)
832 g_return_val_if_fail(account != NULL, NULL);
834 state = g_new0(Pop3State, 1);
836 state->ac_prefs = account;
837 state->uidl_table = pop3_get_uidl_table(account);
838 state->current_time = time(NULL);
839 state->error_val = PS_SUCCESS;
844 void pop3_state_destroy(Pop3State *state)
848 g_return_if_fail(state != NULL);
850 for (n = 1; n <= state->count; n++)
851 g_free(state->msg[n].uidl);
854 if (state->uidl_table) {
855 hash_free_strings(state->uidl_table);
856 g_hash_table_destroy(state->uidl_table);
859 g_free(state->greeting);
862 g_free(state->prev_folder);
867 GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
872 gchar buf[POPBUFSIZE];
873 gchar uidl[POPBUFSIZE];
877 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
878 "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
879 "-", ac_prefs->userid, NULL);
880 if ((fp = fopen(path, "rb")) == NULL) {
881 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
883 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
884 "uidl-", ac_prefs->recv_server,
885 "-", ac_prefs->userid, NULL);
886 if ((fp = fopen(path, "rb")) == NULL) {
887 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
894 table = g_hash_table_new(g_str_hash, g_str_equal);
898 while (fgets(buf, sizeof(buf), fp) != NULL) {
900 recv_time = RECV_TIME_NONE;
901 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
902 if (sscanf(buf, "%s", uidl) != 1)
907 if (recv_time == RECV_TIME_NONE)
908 recv_time = RECV_TIME_RECEIVED;
909 g_hash_table_insert(table, g_strdup(uidl),
910 GINT_TO_POINTER(recv_time));
917 gint pop3_write_uidl_list(Pop3State *state)
924 if (!state->uidl_is_valid) return 0;
926 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
927 "uidl", G_DIR_SEPARATOR_S,
928 state->ac_prefs->recv_server,
929 "-", state->ac_prefs->userid, NULL);
930 if ((fp = fopen(path, "wb")) == NULL) {
931 FILE_OP_ERROR(path, "fopen");
936 for (n = 1; n <= state->count; n++) {
937 msg = &state->msg[n];
938 if (msg->uidl && msg->received && !msg->deleted)
939 fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
942 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");