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"
47 #define LOOKUP_NEXT_MSG() \
50 PrefsAccount *ac = state->ac_prefs; \
52 gboolean size_limit_over; \
55 msg = &state->msg[state->cur_msg]; \
58 (ac->enable_size_limit && \
59 ac->size_limit > 0 && \
60 size > ac->size_limit * 1024); \
63 msg->recv_time != RECV_TIME_NONE && \
64 msg->recv_time != RECV_TIME_KEEP && \
65 state->current_time - msg->recv_time >= \
66 ac->msg_leave_time * 24 * 60 * 60) { \
67 log_print(_("POP3: Deleting expired message %d\n"), \
69 return POP3_DELETE_SEND; \
72 if (size_limit_over) \
73 log_print(_("POP3: Skipping message %d (%d bytes)\n"), \
74 state->cur_msg, size); \
76 if (size == 0 || msg->received || size_limit_over) { \
77 state->cur_total_bytes += size; \
78 if (state->cur_msg == state->count) \
79 return POP3_LOGOUT_SEND; \
87 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
88 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
89 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
90 static gboolean pop3_sd_get_next (Pop3State *state);
91 static void pop3_sd_new_header(Pop3State *state);
92 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state);
94 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
96 Pop3State *state = (Pop3State *)data;
97 gchar buf[POPBUFSIZE];
100 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
101 state->greeting = g_strdup(buf);
103 if (state->ac_prefs->ssl_pop == SSL_STARTTLS)
104 return POP3_STLS_SEND;
106 if (state->ac_prefs->protocol == A_APOP)
107 return POP3_GETAUTH_APOP_SEND;
109 return POP3_GETAUTH_USER_SEND;
111 state->error_val = ok;
117 gint pop3_stls_send(SockInfo *sock, gpointer data)
119 pop3_gen_send(sock, "STLS");
121 return POP3_STLS_RECV;
124 gint pop3_stls_recv(SockInfo *sock, gpointer data)
126 Pop3State *state = (Pop3State *)data;
129 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
130 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1)) {
131 state->error_val = PS_SOCKET;
134 if (state->ac_prefs->protocol == A_APOP)
135 return POP3_GETAUTH_APOP_SEND;
137 return POP3_GETAUTH_USER_SEND;
138 } else if (ok == PS_PROTOCOL) {
139 log_warning(_("can't start TLS session\n"));
140 state->error_val = ok;
141 return POP3_LOGOUT_SEND;
143 state->error_val = ok;
149 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
151 Pop3State *state = (Pop3State *)data;
153 g_return_val_if_fail(state->user != NULL, -1);
155 pop3_gen_send(sock, "USER %s", state->user);
157 return POP3_GETAUTH_USER_RECV;
160 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
162 Pop3State *state = (Pop3State *)data;
164 if (pop3_ok(sock, NULL) == PS_SUCCESS)
165 return POP3_GETAUTH_PASS_SEND;
167 log_warning(_("error occurred on authentication\n"));
168 state->error_val = PS_AUTHFAIL;
173 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
175 Pop3State *state = (Pop3State *)data;
177 g_return_val_if_fail(state->pass != NULL, -1);
179 pop3_gen_send(sock, "PASS %s", state->pass);
181 return POP3_GETAUTH_PASS_RECV;
184 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
186 Pop3State *state = (Pop3State *)data;
189 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS)
190 return POP3_GETRANGE_STAT_SEND;
191 else if (ok == PS_LOCKBUSY) {
192 log_warning(_("mailbox is locked\n"));
193 state->error_val = ok;
196 log_warning(_("error occurred on authentication\n"));
197 state->error_val = PS_AUTHFAIL;
202 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
204 Pop3State *state = (Pop3State *)data;
209 g_return_val_if_fail(state->user != NULL, -1);
210 g_return_val_if_fail(state->pass != NULL, -1);
212 if ((start = strchr(state->greeting, '<')) == NULL) {
213 log_warning(_("Required APOP timestamp not found "
215 state->error_val = PS_PROTOCOL;
219 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
220 log_warning(_("Timestamp syntax error in greeting\n"));
221 state->error_val = PS_PROTOCOL;
227 apop_str = g_strconcat(start, state->pass, NULL);
228 md5_hex_digest(md5sum, apop_str);
231 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
233 return POP3_GETAUTH_APOP_RECV;
236 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
238 Pop3State *state = (Pop3State *)data;
241 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS)
242 return POP3_GETRANGE_STAT_SEND;
243 else if (ok == PS_LOCKBUSY) {
244 log_warning(_("mailbox is locked\n"));
245 state->error_val = ok;
248 log_warning(_("error occurred on authentication\n"));
249 state->error_val = PS_AUTHFAIL;
254 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
256 pop3_gen_send(sock, "STAT");
258 return POP3_GETRANGE_STAT_RECV;
261 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
263 Pop3State *state = (Pop3State *)data;
264 gchar buf[POPBUFSIZE + 1];
267 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
268 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
270 log_warning(_("POP3 protocol error\n"));
271 state->error_val = PS_PROTOCOL;
274 if (state->count == 0) {
275 state->uidl_is_valid = TRUE;
276 return POP3_LOGOUT_SEND;
279 (Pop3MsgInfo, state->count + 1);
281 return POP3_GETRANGE_UIDL_SEND;
284 } else if (ok == PS_PROTOCOL) {
285 state->error_val = ok;
286 return POP3_LOGOUT_SEND;
288 state->error_val = ok;
293 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
295 pop3_gen_send(sock, "LAST");
297 return POP3_GETRANGE_LAST_RECV;
300 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
302 Pop3State *state = (Pop3State *)data;
303 gchar buf[POPBUFSIZE + 1];
305 if (pop3_ok(sock, buf) == PS_SUCCESS) {
308 if (sscanf(buf, "%d", &last) == 0) {
309 log_warning(_("POP3 protocol error\n"));
310 state->error_val = PS_PROTOCOL;
313 if (state->count == last)
314 return POP3_LOGOUT_SEND;
316 state->cur_msg = last + 1;
317 return POP3_GETSIZE_LIST_SEND;
321 return POP3_GETSIZE_LIST_SEND;
324 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
326 pop3_gen_send(sock, "UIDL");
328 return POP3_GETRANGE_UIDL_RECV;
331 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
333 Pop3State *state = (Pop3State *)data;
334 gboolean new = FALSE;
335 gboolean get_all = FALSE;
336 gchar buf[POPBUFSIZE];
341 if (!state->uidl_table) new = TRUE;
343 if (state->ac_prefs->getall ||
344 (state->ac_prefs->rmmail && state->ac_prefs->msg_leave_time == 0))
346 if (state->ac_prefs->getall)
349 if (pop3_ok(sock, NULL) != PS_SUCCESS) {
350 /* UIDL is not supported */
351 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
355 return POP3_GETRANGE_LAST_SEND;
357 return POP3_GETSIZE_LIST_SEND;
360 while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
364 if (buf[0] == '.') break;
365 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
367 if (num <= 0 || num > state->count) continue;
369 state->msg[num].uidl = g_strdup(id);
371 if (!state->uidl_table) continue;
373 recv_time = (time_t)g_hash_table_lookup(state->uidl_table, id);
374 state->msg[num].recv_time = recv_time;
376 if (!get_all && recv_time != RECV_TIME_NONE)
377 state->msg[num].received = TRUE;
380 (get_all || recv_time == RECV_TIME_NONE ||
381 state->ac_prefs->rmmail)) {
382 state->cur_msg = num;
388 log_error(_("Socket error\n"));
389 state->error_val = PS_SOCKET;
393 state->uidl_is_valid = TRUE;
394 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
398 return POP3_GETSIZE_LIST_SEND;
400 return POP3_LOGOUT_SEND;
403 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
405 pop3_gen_send(sock, "LIST");
407 return POP3_GETSIZE_LIST_RECV;
410 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
412 Pop3State *state = (Pop3State *)data;
413 gchar buf[POPBUFSIZE];
417 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
419 state->cur_total_bytes = 0;
420 state->cur_total_recv_bytes = 0;
422 while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
425 if (buf[0] == '.') break;
426 if (sscanf(buf, "%u %u", &num, &size) != 2) {
427 state->error_val = PS_PROTOCOL;
431 if (num > 0 && num <= state->count)
432 state->msg[num].size = size;
433 if (num > 0 && num < state->cur_msg)
434 state->cur_total_bytes += size;
438 log_error(_("Socket error\n"));
439 state->error_val = PS_SOCKET;
443 if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
447 return POP3_RETR_SEND;
450 gint pop3_top_send(SockInfo *sock, gpointer data)
452 Pop3State *state = (Pop3State *)data;
454 inc_progress_update(state, POP3_TOP_SEND);
456 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
458 return POP3_TOP_RECV;
461 gint pop3_top_recv(SockInfo *sock, gpointer data)
463 Pop3State *state = (Pop3State *)data;
464 gchar *filename, *path;
467 if (pop3_ok(sock, NULL) != PS_SUCCESS)
468 return POP3_LOGOUT_SEND;
470 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
472 if ( !is_dir_exist(path) )
475 filename = g_strdup_printf("%s%i", path, state->cur_msg);
477 if ( (write_val = recv_write_to_file(sock, filename)) < 0) {
478 state->error_val = (write_val == -1 ? PS_IOERR : PS_SOCKET);
487 pop3_sd_state(state, POP3_TOP_RECV, &next_state);
489 if (state->cur_msg < state->count) {
491 return POP3_TOP_SEND;
493 return POP3_LOGOUT_SEND;
496 gint pop3_retr_send(SockInfo *sock, gpointer data)
498 Pop3State *state = (Pop3State *)data;
500 pop3_gen_send(sock, "RETR %d", state->cur_msg);
502 return POP3_RETR_RECV;
505 gint pop3_retr_recv(SockInfo *sock, gpointer data)
507 Pop3State *state = (Pop3State *)data;
512 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
513 file = get_tmp_file();
514 if ((write_val = recv_write_to_file(sock, file)) < 0) {
516 if (!state->cancelled)
518 (write_val == -1 ? PS_IOERR : PS_SOCKET);
522 /* drop_ok: 0: success 1: don't receive -1: error */
523 drop_ok = inc_drop_message(file, state);
526 state->error_val = PS_ERROR;
530 if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
533 state->cur_total_bytes += state->msg[state->cur_msg].size;
534 state->cur_total_recv_bytes += state->msg[state->cur_msg].size;
535 state->cur_total_num++;
537 state->msg[state->cur_msg].received = TRUE;
538 state->msg[state->cur_msg].recv_time =
539 drop_ok == 1 ? RECV_TIME_KEEP : state->current_time;
541 if (drop_ok == 0 && state->ac_prefs->rmmail &&
542 state->ac_prefs->msg_leave_time == 0)
543 return POP3_DELETE_SEND;
545 if (state->cur_msg < state->count) {
548 return POP3_RETR_SEND;
550 return POP3_LOGOUT_SEND;
551 } else if (ok == PS_PROTOCOL) {
552 state->error_val = ok;
553 return POP3_LOGOUT_SEND;
555 state->error_val = ok;
560 gint pop3_delete_send(SockInfo *sock, gpointer data)
562 Pop3State *state = (Pop3State *)data;
564 pop3_gen_send(sock, "DELE %d", state->cur_msg);
566 return POP3_DELETE_RECV;
569 gint pop3_delete_recv(SockInfo *sock, gpointer data)
571 Pop3State *state = (Pop3State *)data;
575 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
576 state->msg[state->cur_msg].deleted = TRUE;
578 if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
581 if (state->cur_msg < state->count) {
584 return POP3_RETR_SEND;
586 return POP3_LOGOUT_SEND;
587 } else if (ok == PS_PROTOCOL) {
588 state->error_val = ok;
589 return POP3_LOGOUT_SEND;
591 state->error_val = ok;
596 gint pop3_logout_send(SockInfo *sock, gpointer data)
598 pop3_gen_send(sock, "QUIT");
600 return POP3_LOGOUT_RECV;
603 gint pop3_logout_recv(SockInfo *sock, gpointer data)
605 Pop3State *state = (Pop3State *)data;
608 if ((ok = pop3_ok(sock, NULL)) != PS_SUCCESS)
609 state->error_val = ok;
614 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
617 gchar buf[POPBUFSIZE + 1];
620 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
622 if (*bufp == '+' || *bufp == '-')
627 while (isalpha(*bufp))
633 if (!strcmp(buf, "+OK"))
635 else if (!strncmp(buf, "-ERR", 4)) {
636 if (strstr(bufp, "lock") ||
637 strstr(bufp, "Lock") ||
638 strstr(bufp, "LOCK") ||
639 strstr(bufp, "wait"))
645 fprintf(stderr, "POP3: %s\n", bufp);
650 strcpy(argbuf, bufp);
656 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
658 gchar buf[POPBUFSIZE + 1];
661 va_start(args, format);
662 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
665 if (!strncasecmp(buf, "PASS ", 5))
666 log_print("POP3> PASS ********\n");
668 log_print("POP3> %s\n", buf);
671 sock_write(sock, buf, strlen(buf));
674 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
676 if (sock_gets(sock, buf, size) < 0) {
680 log_print("POP3< %s\n", buf);
686 static void pop3_sd_new_header(Pop3State *state)
688 HeaderItems *new_msg;
689 if (state->cur_msg <= state->count) {
690 new_msg = g_new0(HeaderItems, 1);
692 new_msg->index = state->cur_msg;
693 new_msg->state = SD_UNCHECKED;
694 new_msg->size = state->msg[state->cur_msg].size;
695 new_msg->received = state->msg[state->cur_msg].received;
696 new_msg->del_by_old_session = FALSE;
698 state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list,
700 debug_print("received ?: msg %i, received: %i\n",new_msg->index, new_msg->received);
704 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state)
706 gint session = state->ac_prefs->session;
707 guint goto_state = -1;
710 case POP3_GETRANGE_UIDL_RECV:
712 case STYPE_POP_BEFORE_SMTP:
713 goto_state = POP3_LOGOUT_SEND;
717 case STYPE_PREVIEW_ALL:
718 goto_state = POP3_GETSIZE_LIST_SEND;
723 case POP3_GETSIZE_LIST_RECV:
725 case STYPE_PREVIEW_ALL:
727 case STYPE_PREVIEW_NEW:
728 goto_state = POP3_TOP_SEND;
731 if (pop3_sd_get_next(state))
732 goto_state = POP3_DELETE_SEND;
734 goto_state = POP3_LOGOUT_SEND;
737 if (pop3_sd_get_next(state))
738 goto_state = POP3_RETR_SEND;
740 goto_state = POP3_LOGOUT_SEND;
747 case STYPE_PREVIEW_ALL:
748 case STYPE_PREVIEW_NEW:
749 pop3_sd_new_header(state);
757 if (state->ac_prefs->sd_rmmail_on_download)
758 goto_state = POP3_DELETE_SEND;
760 if (pop3_sd_get_next(state))
761 goto_state = POP3_RETR_SEND;
763 goto_state = POP3_LOGOUT_SEND;
769 case POP3_DELETE_RECV:
772 if (pop3_sd_get_next(state))
773 goto_state = POP3_DELETE_SEND;
775 goto_state = POP3_LOGOUT_SEND;
778 if (pop3_sd_get_next(state))
779 goto_state = POP3_RETR_SEND;
781 goto_state = POP3_LOGOUT_SEND;
790 *next_state = goto_state;
791 if (goto_state != -1)
797 gboolean pop3_sd_get_next(Pop3State *state)
800 gint deleted_msgs = 0;
802 switch (state->ac_prefs->session) {
805 for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
806 HeaderItems *items = (HeaderItems*)cur->data;
808 if (items->del_by_old_session)
811 switch (items->state) {
813 items->state = SD_REMOVED;
816 items->state = SD_DOWNLOADED;
819 state->cur_msg = items->index - deleted_msgs;
820 if (state->ac_prefs->session == STYPE_DELETE)
821 items->state = SD_REMOVE;
823 items->state = SD_DOWNLOAD;
835 Pop3State *pop3_state_new(PrefsAccount *account)
839 g_return_val_if_fail(account != NULL, NULL);
841 state = g_new0(Pop3State, 1);
843 state->ac_prefs = account;
844 state->uidl_table = pop3_get_uidl_table(account);
845 state->current_time = time(NULL);
846 state->error_val = PS_SUCCESS;
851 void pop3_state_destroy(Pop3State *state)
855 g_return_if_fail(state != NULL);
857 for (n = 1; n <= state->count; n++)
858 g_free(state->msg[n].uidl);
861 if (state->uidl_table) {
862 hash_free_strings(state->uidl_table);
863 g_hash_table_destroy(state->uidl_table);
866 g_free(state->greeting);
869 g_free(state->prev_folder);
874 GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
879 gchar buf[POPBUFSIZE];
880 gchar uidl[POPBUFSIZE];
884 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
885 "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
886 "-", ac_prefs->userid, NULL);
887 if ((fp = fopen(path, "rb")) == NULL) {
888 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
890 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
891 "uidl-", ac_prefs->recv_server,
892 "-", ac_prefs->userid, NULL);
893 if ((fp = fopen(path, "rb")) == NULL) {
894 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
901 table = g_hash_table_new(g_str_hash, g_str_equal);
905 while (fgets(buf, sizeof(buf), fp) != NULL) {
907 recv_time = RECV_TIME_NONE;
908 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
909 if (sscanf(buf, "%s", uidl) != 1)
914 if (recv_time == RECV_TIME_NONE)
915 recv_time = RECV_TIME_RECEIVED;
916 g_hash_table_insert(table, g_strdup(uidl),
917 GINT_TO_POINTER(recv_time));
924 gint pop3_write_uidl_list(Pop3State *state)
931 if (!state->uidl_is_valid) return 0;
933 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
934 "uidl", G_DIR_SEPARATOR_S,
935 state->ac_prefs->recv_server,
936 "-", state->ac_prefs->userid, NULL);
937 if ((fp = fopen(path, "wb")) == NULL) {
938 FILE_OP_ERROR(path, "fopen");
943 for (n = 1; n <= state->count; n++) {
944 msg = &state->msg[n];
945 if (msg->uidl && msg->received && !msg->deleted)
946 fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
949 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");