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);
486 pop3_sd_state(state, POP3_TOP_RECV, &next_state);
488 if (state->cur_msg < state->count) {
490 return POP3_TOP_SEND;
492 return POP3_LOGOUT_SEND;
495 gint pop3_retr_send(SockInfo *sock, gpointer data)
497 Pop3State *state = (Pop3State *)data;
499 pop3_gen_send(sock, "RETR %d", state->cur_msg);
501 return POP3_RETR_RECV;
504 gint pop3_retr_recv(SockInfo *sock, gpointer data)
506 Pop3State *state = (Pop3State *)data;
511 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
512 file = get_tmp_file();
513 if ((write_val = recv_write_to_file(sock, file)) < 0) {
515 if (!state->cancelled)
517 (write_val == -1 ? PS_IOERR : PS_SOCKET);
521 /* drop_ok: 0: success 1: don't receive -1: error */
522 drop_ok = inc_drop_message(file, state);
525 state->error_val = PS_ERROR;
529 if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
532 state->cur_total_bytes += state->msg[state->cur_msg].size;
533 state->cur_total_recv_bytes += state->msg[state->cur_msg].size;
534 state->cur_total_num++;
536 state->msg[state->cur_msg].received = TRUE;
537 state->msg[state->cur_msg].recv_time =
538 drop_ok == 1 ? RECV_TIME_KEEP : state->current_time;
540 if (drop_ok == 0 && state->ac_prefs->rmmail &&
541 state->ac_prefs->msg_leave_time == 0)
542 return POP3_DELETE_SEND;
544 if (state->cur_msg < state->count) {
547 return POP3_RETR_SEND;
549 return POP3_LOGOUT_SEND;
550 } else if (ok == PS_PROTOCOL) {
551 state->error_val = ok;
552 return POP3_LOGOUT_SEND;
554 state->error_val = ok;
559 gint pop3_delete_send(SockInfo *sock, gpointer data)
561 Pop3State *state = (Pop3State *)data;
563 pop3_gen_send(sock, "DELE %d", state->cur_msg);
565 return POP3_DELETE_RECV;
568 gint pop3_delete_recv(SockInfo *sock, gpointer data)
570 Pop3State *state = (Pop3State *)data;
574 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
575 state->msg[state->cur_msg].deleted = TRUE;
577 if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
580 if (state->cur_msg < state->count) {
583 return POP3_RETR_SEND;
585 return POP3_LOGOUT_SEND;
586 } else if (ok == PS_PROTOCOL) {
587 state->error_val = ok;
588 return POP3_LOGOUT_SEND;
590 state->error_val = ok;
595 gint pop3_logout_send(SockInfo *sock, gpointer data)
597 pop3_gen_send(sock, "QUIT");
599 return POP3_LOGOUT_RECV;
602 gint pop3_logout_recv(SockInfo *sock, gpointer data)
604 Pop3State *state = (Pop3State *)data;
607 if ((ok = pop3_ok(sock, NULL)) != PS_SUCCESS)
608 state->error_val = ok;
613 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
616 gchar buf[POPBUFSIZE + 1];
619 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
621 if (*bufp == '+' || *bufp == '-')
626 while (isalpha(*bufp))
632 if (!strcmp(buf, "+OK"))
634 else if (!strncmp(buf, "-ERR", 4)) {
635 if (strstr(bufp, "lock") ||
636 strstr(bufp, "Lock") ||
637 strstr(bufp, "LOCK") ||
638 strstr(bufp, "wait"))
644 fprintf(stderr, "POP3: %s\n", bufp);
649 strcpy(argbuf, bufp);
655 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
657 gchar buf[POPBUFSIZE + 1];
660 va_start(args, format);
661 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
664 if (!strncasecmp(buf, "PASS ", 5))
665 log_print("POP3> PASS ********\n");
667 log_print("POP3> %s\n", buf);
670 sock_write(sock, buf, strlen(buf));
673 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
675 if (sock_gets(sock, buf, size) < 0) {
679 log_print("POP3< %s\n", buf);
685 static void pop3_sd_new_header(Pop3State *state)
687 HeaderItems *new_msg;
688 if (state->cur_msg <= state->count) {
689 new_msg = g_new0(HeaderItems, 1);
691 new_msg->index = state->cur_msg;
692 new_msg->state = SD_UNCHECKED;
693 new_msg->size = state->msg[state->cur_msg].size;
694 new_msg->received = state->msg[state->cur_msg].received;
695 new_msg->del_by_old_session = FALSE;
697 state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list,
699 debug_print("received ?: msg %i, received: %i\n",new_msg->index, new_msg->received);
703 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state)
705 gint session = state->ac_prefs->session;
706 guint goto_state = -1;
709 case POP3_GETRANGE_UIDL_RECV:
711 case STYPE_POP_BEFORE_SMTP:
712 goto_state = POP3_LOGOUT_SEND;
716 case STYPE_PREVIEW_ALL:
717 goto_state = POP3_GETSIZE_LIST_SEND;
722 case POP3_GETSIZE_LIST_RECV:
724 case STYPE_PREVIEW_ALL:
726 case STYPE_PREVIEW_NEW:
727 goto_state = POP3_TOP_SEND;
730 if (pop3_sd_get_next(state))
731 goto_state = POP3_DELETE_SEND;
733 goto_state = POP3_LOGOUT_SEND;
736 if (pop3_sd_get_next(state))
737 goto_state = POP3_RETR_SEND;
739 goto_state = POP3_LOGOUT_SEND;
746 case STYPE_PREVIEW_ALL:
747 case STYPE_PREVIEW_NEW:
748 pop3_sd_new_header(state);
756 if (state->ac_prefs->sd_rmmail_on_download)
757 goto_state = POP3_DELETE_SEND;
759 if (pop3_sd_get_next(state))
760 goto_state = POP3_RETR_SEND;
762 goto_state = POP3_LOGOUT_SEND;
768 case POP3_DELETE_RECV:
771 if (pop3_sd_get_next(state))
772 goto_state = POP3_DELETE_SEND;
774 goto_state = POP3_LOGOUT_SEND;
777 if (pop3_sd_get_next(state))
778 goto_state = POP3_RETR_SEND;
780 goto_state = POP3_LOGOUT_SEND;
789 *next_state = goto_state;
790 if (goto_state != -1)
796 gboolean pop3_sd_get_next(Pop3State *state)
799 gint deleted_msgs = 0;
801 switch (state->ac_prefs->session) {
804 for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
805 HeaderItems *items = (HeaderItems*)cur->data;
807 if (items->del_by_old_session)
810 switch (items->state) {
812 items->state = SD_REMOVED;
815 items->state = SD_DOWNLOADED;
818 state->cur_msg = items->index - deleted_msgs;
819 if (state->ac_prefs->session == STYPE_DELETE)
820 items->state = SD_REMOVE;
822 items->state = SD_DOWNLOAD;
834 Pop3State *pop3_state_new(PrefsAccount *account)
838 g_return_val_if_fail(account != NULL, NULL);
840 state = g_new0(Pop3State, 1);
842 state->ac_prefs = account;
843 state->uidl_table = pop3_get_uidl_table(account);
844 state->current_time = time(NULL);
845 state->error_val = PS_SUCCESS;
850 void pop3_state_destroy(Pop3State *state)
854 g_return_if_fail(state != NULL);
856 for (n = 1; n <= state->count; n++)
857 g_free(state->msg[n].uidl);
860 if (state->uidl_table) {
861 hash_free_strings(state->uidl_table);
862 g_hash_table_destroy(state->uidl_table);
865 g_free(state->greeting);
868 g_free(state->prev_folder);
873 GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
878 gchar buf[POPBUFSIZE];
879 gchar uidl[POPBUFSIZE];
883 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
884 "uidl", G_DIR_SEPARATOR_S, 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");
889 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
890 "uidl-", ac_prefs->recv_server,
891 "-", ac_prefs->userid, NULL);
892 if ((fp = fopen(path, "rb")) == NULL) {
893 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
900 table = g_hash_table_new(g_str_hash, g_str_equal);
904 while (fgets(buf, sizeof(buf), fp) != NULL) {
906 recv_time = RECV_TIME_NONE;
907 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
908 if (sscanf(buf, "%s", uidl) != 1)
913 if (recv_time == RECV_TIME_NONE)
914 recv_time = RECV_TIME_RECEIVED;
915 g_hash_table_insert(table, g_strdup(uidl),
916 GINT_TO_POINTER(recv_time));
923 gint pop3_write_uidl_list(Pop3State *state)
930 if (!state->uidl_is_valid) return 0;
932 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
933 "uidl", G_DIR_SEPARATOR_S,
934 state->ac_prefs->recv_server,
935 "-", state->ac_prefs->userid, NULL);
936 if ((fp = fopen(path, "wb")) == NULL) {
937 FILE_OP_ERROR(path, "fopen");
942 for (n = 1; n <= state->count; n++) {
943 msg = &state->msg[n];
944 if (msg->uidl && msg->received && !msg->deleted)
945 fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
948 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");