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 != 0 && \
63 state->current_time - msg->recv_time >= \
64 ac->msg_leave_time * 24 * 60 * 60) { \
65 log_print(_("POP3: Deleting expired message %d\n"), \
67 return POP3_DELETE_SEND; \
70 if (size_limit_over) \
71 log_print(_("POP3: Skipping message %d (%d bytes)\n"), \
72 state->cur_msg, size); \
74 if (size == 0 || msg->received || size_limit_over) { \
75 state->cur_total_bytes += size; \
76 if (state->cur_msg == state->count) \
77 return POP3_LOGOUT_SEND; \
85 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
86 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
87 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
88 static gboolean pop3_sd_get_next (Pop3State *state);
89 static void pop3_sd_new_header(Pop3State *state);
90 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state);
92 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
94 Pop3State *state = (Pop3State *)data;
95 gchar buf[POPBUFSIZE];
98 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
99 state->greeting = g_strdup(buf);
101 if (state->ac_prefs->ssl_pop == SSL_STARTTLS)
102 return POP3_STLS_SEND;
104 if (state->ac_prefs->protocol == A_APOP)
105 return POP3_GETAUTH_APOP_SEND;
107 return POP3_GETAUTH_USER_SEND;
109 state->error_val = ok;
115 gint pop3_stls_send(SockInfo *sock, gpointer data)
117 pop3_gen_send(sock, "STLS");
119 return POP3_STLS_RECV;
122 gint pop3_stls_recv(SockInfo *sock, gpointer data)
124 Pop3State *state = (Pop3State *)data;
127 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
128 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1)) {
129 state->error_val = PS_SOCKET;
132 if (state->ac_prefs->protocol == A_APOP)
133 return POP3_GETAUTH_APOP_SEND;
135 return POP3_GETAUTH_USER_SEND;
136 } else if (ok == PS_PROTOCOL) {
137 log_warning(_("can't start TLS session\n"));
138 state->error_val = ok;
139 return POP3_LOGOUT_SEND;
141 state->error_val = ok;
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;
171 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
173 Pop3State *state = (Pop3State *)data;
175 g_return_val_if_fail(state->pass != NULL, -1);
177 pop3_gen_send(sock, "PASS %s", state->pass);
179 return POP3_GETAUTH_PASS_RECV;
182 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
184 Pop3State *state = (Pop3State *)data;
187 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS)
188 return POP3_GETRANGE_STAT_SEND;
189 else if (ok == PS_LOCKBUSY) {
190 log_warning(_("mailbox is locked\n"));
191 state->error_val = ok;
194 log_warning(_("error occurred on authentication\n"));
195 state->error_val = PS_AUTHFAIL;
200 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
202 Pop3State *state = (Pop3State *)data;
207 g_return_val_if_fail(state->user != NULL, -1);
208 g_return_val_if_fail(state->pass != NULL, -1);
210 if ((start = strchr(state->greeting, '<')) == NULL) {
211 log_warning(_("Required APOP timestamp not found "
213 state->error_val = PS_PROTOCOL;
217 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
218 log_warning(_("Timestamp syntax error in greeting\n"));
219 state->error_val = PS_PROTOCOL;
225 apop_str = g_strconcat(start, state->pass, NULL);
226 md5_hex_digest(md5sum, apop_str);
229 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
231 return POP3_GETAUTH_APOP_RECV;
234 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
236 Pop3State *state = (Pop3State *)data;
239 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS)
240 return POP3_GETRANGE_STAT_SEND;
241 else if (ok == PS_LOCKBUSY) {
242 log_warning(_("mailbox is locked\n"));
243 state->error_val = ok;
246 log_warning(_("error occurred on authentication\n"));
247 state->error_val = PS_AUTHFAIL;
252 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
254 pop3_gen_send(sock, "STAT");
256 return POP3_GETRANGE_STAT_RECV;
259 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
261 Pop3State *state = (Pop3State *)data;
262 gchar buf[POPBUFSIZE + 1];
265 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
266 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
268 log_warning(_("POP3 protocol error\n"));
269 state->error_val = PS_PROTOCOL;
272 if (state->count == 0) {
273 state->uidl_is_valid = TRUE;
274 return POP3_LOGOUT_SEND;
277 (Pop3MsgInfo, state->count + 1);
279 return POP3_GETRANGE_UIDL_SEND;
282 } else if (ok == PS_PROTOCOL) {
283 state->error_val = ok;
284 return POP3_LOGOUT_SEND;
286 state->error_val = ok;
291 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
293 pop3_gen_send(sock, "LAST");
295 return POP3_GETRANGE_LAST_RECV;
298 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
300 Pop3State *state = (Pop3State *)data;
301 gchar buf[POPBUFSIZE + 1];
303 if (pop3_ok(sock, buf) == PS_SUCCESS) {
306 if (sscanf(buf, "%d", &last) == 0) {
307 log_warning(_("POP3 protocol error\n"));
308 state->error_val = PS_PROTOCOL;
311 if (state->count == last)
312 return POP3_LOGOUT_SEND;
314 state->cur_msg = last + 1;
315 return POP3_GETSIZE_LIST_SEND;
319 return POP3_GETSIZE_LIST_SEND;
322 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
324 pop3_gen_send(sock, "UIDL");
326 return POP3_GETRANGE_UIDL_RECV;
329 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
331 Pop3State *state = (Pop3State *)data;
332 gboolean new = FALSE;
333 gboolean get_all = FALSE;
334 gchar buf[POPBUFSIZE];
339 if (!state->uidl_table) new = TRUE;
340 if (state->ac_prefs->getall ||
341 (state->ac_prefs->rmmail && state->ac_prefs->msg_leave_time == 0))
344 if (pop3_ok(sock, NULL) != PS_SUCCESS) {
345 /* UIDL is not supported */
346 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
350 return POP3_GETRANGE_LAST_SEND;
352 return POP3_GETSIZE_LIST_SEND;
355 while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
359 if (buf[0] == '.') break;
360 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
362 if (num <= 0 || num > state->count) continue;
364 state->msg[num].uidl = g_strdup(id);
366 if (!state->uidl_table) continue;
368 recv_time = (time_t)g_hash_table_lookup(state->uidl_table, id);
369 state->msg[num].recv_time = recv_time;
371 if (!get_all && recv_time != 0)
372 state->msg[num].received = TRUE;
375 (get_all || recv_time == 0 || state->ac_prefs->rmmail)) {
376 state->cur_msg = num;
382 log_error(_("Socket error\n"));
383 state->error_val = PS_SOCKET;
387 state->uidl_is_valid = TRUE;
388 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
392 return POP3_GETSIZE_LIST_SEND;
394 return POP3_LOGOUT_SEND;
397 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
399 pop3_gen_send(sock, "LIST");
401 return POP3_GETSIZE_LIST_RECV;
404 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
406 Pop3State *state = (Pop3State *)data;
407 gchar buf[POPBUFSIZE];
411 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
413 state->cur_total_bytes = 0;
414 state->cur_total_recv_bytes = 0;
416 while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
419 if (buf[0] == '.') break;
420 if (sscanf(buf, "%u %u", &num, &size) != 2) {
421 state->error_val = PS_PROTOCOL;
425 if (num > 0 && num <= state->count)
426 state->msg[num].size = size;
427 if (num > 0 && num < state->cur_msg)
428 state->cur_total_bytes += size;
432 log_error(_("Socket error\n"));
433 state->error_val = PS_SOCKET;
437 if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
441 return POP3_RETR_SEND;
444 gint pop3_top_send(SockInfo *sock, gpointer data)
446 Pop3State *state = (Pop3State *)data;
448 inc_progress_update(state, POP3_TOP_SEND);
450 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
452 return POP3_TOP_RECV;
455 gint pop3_top_recv(SockInfo *sock, gpointer data)
457 Pop3State *state = (Pop3State *)data;
458 gchar *filename, *path;
461 if (pop3_ok(sock, NULL) != PS_SUCCESS)
462 return POP3_LOGOUT_SEND;
464 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
466 if ( !is_dir_exist(path) )
469 filename = g_strdup_printf("%s%i", path, state->cur_msg);
471 if (recv_write_to_file(sock, filename) < 0) {
472 state->error_val = PS_IOERR;
476 pop3_sd_state(state, POP3_TOP_RECV, &next_state);
478 if (state->cur_msg < state->count) {
480 return POP3_TOP_SEND;
482 return POP3_LOGOUT_SEND;
485 gint pop3_retr_send(SockInfo *sock, gpointer data)
487 Pop3State *state = (Pop3State *)data;
489 pop3_gen_send(sock, "RETR %d", state->cur_msg);
491 return POP3_RETR_RECV;
494 gint pop3_retr_recv(SockInfo *sock, gpointer data)
496 Pop3State *state = (Pop3State *)data;
500 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
501 file = get_tmp_file();
502 if (recv_write_to_file(sock, file) < 0) {
504 if (!state->cancelled)
505 state->error_val = PS_IOERR;
509 drop_ok = inc_drop_message(file, state);
512 state->error_val = PS_ERROR;
516 if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
519 state->cur_total_bytes += state->msg[state->cur_msg].size;
520 state->cur_total_recv_bytes += state->msg[state->cur_msg].size;
521 state->cur_total_num++;
523 state->msg[state->cur_msg].received = TRUE;
524 state->msg[state->cur_msg].recv_time = state->current_time;
526 if (state->ac_prefs->rmmail &&
527 state->ac_prefs->msg_leave_time == 0)
528 return POP3_DELETE_SEND;
530 if (state->cur_msg < state->count) {
533 return POP3_RETR_SEND;
535 return POP3_LOGOUT_SEND;
536 } else if (ok == PS_PROTOCOL) {
537 state->error_val = ok;
538 return POP3_LOGOUT_SEND;
540 state->error_val = ok;
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) {
561 state->msg[state->cur_msg].deleted = TRUE;
563 if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
566 if (state->cur_msg < state->count) {
569 return POP3_RETR_SEND;
571 return POP3_LOGOUT_SEND;
572 } else if (ok == PS_PROTOCOL) {
573 state->error_val = ok;
574 return POP3_LOGOUT_SEND;
576 state->error_val = ok;
581 gint pop3_logout_send(SockInfo *sock, gpointer data)
583 pop3_gen_send(sock, "QUIT");
585 return POP3_LOGOUT_RECV;
588 gint pop3_logout_recv(SockInfo *sock, gpointer data)
590 Pop3State *state = (Pop3State *)data;
593 if ((ok = pop3_ok(sock, NULL)) != PS_SUCCESS)
594 state->error_val = ok;
599 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
602 gchar buf[POPBUFSIZE + 1];
605 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
607 if (*bufp == '+' || *bufp == '-')
612 while (isalpha(*bufp))
618 if (!strcmp(buf, "+OK"))
620 else if (!strncmp(buf, "-ERR", 4)) {
621 if (strstr(bufp, "lock") ||
622 strstr(bufp, "Lock") ||
623 strstr(bufp, "LOCK") ||
624 strstr(bufp, "wait"))
630 fprintf(stderr, "POP3: %s\n", bufp);
635 strcpy(argbuf, bufp);
641 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
643 gchar buf[POPBUFSIZE + 1];
646 va_start(args, format);
647 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
650 if (!strncasecmp(buf, "PASS ", 5))
651 log_print("POP3> PASS ********\n");
653 log_print("POP3> %s\n", buf);
656 sock_write(sock, buf, strlen(buf));
659 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
661 if (sock_gets(sock, buf, size) < 0) {
665 log_print("POP3< %s\n", buf);
671 static void pop3_sd_new_header(Pop3State *state)
673 HeaderItems *new_msg;
674 if (state->cur_msg <= state->count) {
675 new_msg = g_new0(HeaderItems, 1);
677 new_msg->index = state->cur_msg;
678 new_msg->state = SD_UNCHECKED;
679 new_msg->size = state->msg[state->cur_msg].size;
680 new_msg->received = state->msg[state->cur_msg].received;
681 new_msg->del_by_old_session = FALSE;
683 state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list,
688 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state)
690 gint session = state->ac_prefs->session;
691 guint goto_state = -1;
694 case POP3_GETRANGE_UIDL_RECV:
698 case STYPE_PREVIEW_ALL:
699 goto_state = POP3_GETSIZE_LIST_SEND;
704 case POP3_GETSIZE_LIST_RECV:
706 case STYPE_PREVIEW_ALL:
708 case STYPE_PREVIEW_NEW:
709 goto_state = POP3_TOP_SEND;
712 if (pop3_sd_get_next(state))
713 goto_state = POP3_DELETE_SEND;
715 goto_state = POP3_LOGOUT_SEND;
718 if (pop3_sd_get_next(state))
719 goto_state = POP3_RETR_SEND;
721 goto_state = POP3_LOGOUT_SEND;
728 case STYPE_PREVIEW_ALL:
729 case STYPE_PREVIEW_NEW:
730 pop3_sd_new_header(state);
738 if (state->ac_prefs->sd_rmmail_on_download)
739 goto_state = POP3_DELETE_SEND;
741 if (pop3_sd_get_next(state))
742 goto_state = POP3_RETR_SEND;
744 goto_state = POP3_LOGOUT_SEND;
750 case POP3_DELETE_RECV:
753 if (pop3_sd_get_next(state))
754 goto_state = POP3_DELETE_SEND;
756 goto_state = POP3_LOGOUT_SEND;
759 if (pop3_sd_get_next(state))
760 goto_state = POP3_RETR_SEND;
762 goto_state = POP3_LOGOUT_SEND;
771 *next_state = goto_state;
772 if (goto_state != -1)
778 gboolean pop3_sd_get_next(Pop3State *state)
781 gint deleted_msgs = 0;
783 switch (state->ac_prefs->session) {
786 for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
787 HeaderItems *items = (HeaderItems*)cur->data;
789 if (items->del_by_old_session)
792 switch (items->state) {
794 items->state = SD_REMOVED;
797 items->state = SD_DOWNLOADED;
800 state->cur_msg = items->index - deleted_msgs;
801 if (state->ac_prefs->session == STYPE_DELETE)
802 items->state = SD_REMOVE;
804 items->state = SD_DOWNLOAD;
816 Pop3State *pop3_state_new(PrefsAccount *account)
820 g_return_val_if_fail(account != NULL, NULL);
822 state = g_new0(Pop3State, 1);
824 state->ac_prefs = account;
825 state->uidl_table = pop3_get_uidl_table(account);
826 state->current_time = time(NULL);
827 state->error_val = PS_SUCCESS;
832 void pop3_state_destroy(Pop3State *state)
836 g_return_if_fail(state != NULL);
838 for (n = 1; n <= state->count; n++)
839 g_free(state->msg[n].uidl);
842 if (state->uidl_table) {
843 hash_free_strings(state->uidl_table);
844 g_hash_table_destroy(state->uidl_table);
847 g_free(state->greeting);
850 g_free(state->prev_folder);
855 GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
860 gchar buf[IDLEN + 3];
861 gchar uidl[IDLEN + 3];
865 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
866 "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
867 "-", ac_prefs->userid, NULL);
868 if ((fp = fopen(path, "rb")) == NULL) {
869 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
871 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
872 "uidl-", ac_prefs->recv_server,
873 "-", ac_prefs->userid, NULL);
874 if ((fp = fopen(path, "rb")) == NULL) {
875 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
882 table = g_hash_table_new(g_str_hash, g_str_equal);
886 while (fgets(buf, sizeof(buf), fp) != NULL) {
889 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
890 if (sscanf(buf, "%s", uidl) != 1)
897 g_hash_table_insert(table, g_strdup(uidl),
898 GINT_TO_POINTER(recv_time));
905 gint pop3_write_uidl_list(Pop3State *state)
912 if (!state->uidl_is_valid) return 0;
914 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
915 "uidl", G_DIR_SEPARATOR_S,
916 state->ac_prefs->recv_server,
917 "-", state->ac_prefs->userid, NULL);
918 if ((fp = fopen(path, "wb")) == NULL) {
919 FILE_OP_ERROR(path, "fopen");
924 for (n = 1; n <= state->count; n++) {
925 msg = &state->msg[n];
926 if (msg->uidl && msg->received && !msg->deleted)
927 fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
930 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");