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.
35 #include "prefs_account.h"
39 #include "selective_download.h"
44 #define LOOKUP_NEXT_MSG() \
46 gint size = state->msg[state->cur_msg].size; \
47 gboolean size_limit_over = \
48 (state->ac_prefs->enable_size_limit && \
49 state->ac_prefs->size_limit > 0 && \
50 size > state->ac_prefs->size_limit * 1024); \
52 if (size_limit_over) \
53 log_print(_("POP3: Skipping message %d (%d bytes)\n"), \
54 state->cur_msg, size); \
56 if (size == 0 || state->msg[state->cur_msg].received || \
59 state->cur_total_bytes += size; \
60 if (state->cur_msg == state->count) \
61 return POP3_LOGOUT_SEND; \
68 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
69 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
70 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
71 static gboolean pop3_sd_get_next (Pop3State *state);
72 static void pop3_sd_new_header(Pop3State *state);
73 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state);
74 static gboolean should_delete (const char *uidl, gpointer data);
76 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
78 Pop3State *state = (Pop3State *)data;
79 gchar buf[POPBUFSIZE];
81 if (pop3_ok(sock, buf) == PS_SUCCESS) {
83 if (state->ac_prefs->ssl_pop == SSL_STARTTLS) {
84 state->greeting = g_strdup(buf);
85 return POP3_STLS_SEND;
88 if (state->ac_prefs->protocol == A_APOP) {
89 state->greeting = g_strdup(buf);
90 return POP3_GETAUTH_APOP_SEND;
92 return POP3_GETAUTH_USER_SEND;
98 gint pop3_stls_send(SockInfo *sock, gpointer data)
100 pop3_gen_send(sock, "STLS");
102 return POP3_STLS_RECV;
105 gint pop3_stls_recv(SockInfo *sock, gpointer data)
107 Pop3State *state = (Pop3State *)data;
110 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
111 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1))
113 if (state->ac_prefs->protocol == A_APOP) {
114 return POP3_GETAUTH_APOP_SEND;
116 return POP3_GETAUTH_USER_SEND;
117 } else if (ok == PS_PROTOCOL) {
118 log_warning(_("can't start TLS session\n"));
119 state->error_val = PS_PROTOCOL;
120 state->inc_state = INC_ERROR;
121 return POP3_LOGOUT_SEND;
127 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
129 Pop3State *state = (Pop3State *)data;
131 g_return_val_if_fail(state->user != NULL, -1);
133 pop3_gen_send(sock, "USER %s", state->user);
135 return POP3_GETAUTH_USER_RECV;
138 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
140 Pop3State *state = (Pop3State *)data;
142 if (pop3_ok(sock, NULL) == PS_SUCCESS)
143 return POP3_GETAUTH_PASS_SEND;
145 log_warning(_("error occurred on authentication\n"));
146 state->error_val = PS_AUTHFAIL;
147 state->inc_state = INC_AUTH_FAILED;
152 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
154 Pop3State *state = (Pop3State *)data;
156 g_return_val_if_fail(state->pass != NULL, -1);
158 pop3_gen_send(sock, "PASS %s", state->pass);
160 return POP3_GETAUTH_PASS_RECV;
163 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
165 Pop3State *state = (Pop3State *)data;
167 if (pop3_ok(sock, NULL) == PS_SUCCESS)
168 return POP3_GETRANGE_STAT_SEND;
171 log_warning(_("error occurred on authentication\n"));
172 state->error_val = PS_AUTHFAIL;
173 state->inc_state = INC_AUTH_FAILED;
178 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
180 Pop3State *state = (Pop3State *)data;
185 g_return_val_if_fail(state->user != NULL, -1);
186 g_return_val_if_fail(state->pass != NULL, -1);
188 if ((start = strchr(state->greeting, '<')) == NULL) {
189 log_warning(_("Required APOP timestamp not found "
194 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
195 log_warning(_("Timestamp syntax error in greeting\n"));
201 apop_str = g_strconcat(start, state->pass, NULL);
202 md5_hex_digest(md5sum, apop_str);
205 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
207 return POP3_GETAUTH_APOP_RECV;
210 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
212 Pop3State *state = (Pop3State *)data;
214 if (pop3_ok(sock, NULL) == PS_SUCCESS)
215 return POP3_GETRANGE_STAT_SEND;
218 log_warning(_("error occurred on authentication\n"));
219 state->error_val = PS_AUTHFAIL;
220 state->inc_state = INC_AUTH_FAILED;
225 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
227 pop3_gen_send(sock, "STAT");
229 return POP3_GETRANGE_STAT_RECV;
232 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
234 Pop3State *state = (Pop3State *)data;
235 gchar buf[POPBUFSIZE + 1];
238 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
239 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
241 log_warning(_("POP3 protocol error\n"));
244 if (state->count == 0) {
245 state->uidl_is_valid = TRUE;
246 return POP3_LOGOUT_SEND;
249 (Pop3MsgInfo, state->count + 1);
251 return POP3_GETRANGE_UIDL_SEND;
254 } else if (ok == PS_PROTOCOL)
255 return POP3_LOGOUT_SEND;
260 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
262 pop3_gen_send(sock, "LAST");
264 return POP3_GETRANGE_LAST_RECV;
267 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
269 Pop3State *state = (Pop3State *)data;
270 gchar buf[POPBUFSIZE + 1];
272 if (pop3_ok(sock, buf) == PS_SUCCESS) {
275 if (sscanf(buf, "%d", &last) == 0) {
276 log_warning(_("POP3 protocol error\n"));
279 if (state->count == last)
280 return POP3_LOGOUT_SEND;
282 state->cur_msg = last + 1;
283 return POP3_GETSIZE_LIST_SEND;
287 return POP3_GETSIZE_LIST_SEND;
290 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
292 pop3_gen_send(sock, "UIDL");
294 return POP3_GETRANGE_UIDL_RECV;
297 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
299 Pop3State *state = (Pop3State *)data;
300 gboolean new = FALSE;
301 gboolean get_all = FALSE;
302 gchar buf[POPBUFSIZE];
306 if (!state->uidl_table) new = TRUE;
307 if (state->ac_prefs->getall)
310 if (pop3_ok(sock, NULL) != PS_SUCCESS) {
311 /* UIDL is not supported */
312 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
316 return POP3_GETRANGE_LAST_SEND;
318 return POP3_GETSIZE_LIST_SEND;
321 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
324 if (buf[0] == '.') break;
325 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
327 if (num <= 0 || num > state->count) continue;
329 state->msg[num].uidl = g_strdup(id);
331 if (state->uidl_table) {
333 g_hash_table_lookup(state->uidl_table, id) != NULL)
334 state->msg[num].received = TRUE;
337 state->cur_msg = num;
343 if (should_delete(buf, (Pop3State *) state))
344 state->uidl_todelete_list = g_slist_append
345 (state->uidl_todelete_list, g_strdup(buf));
349 state->uidl_is_valid = TRUE;
351 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
355 return POP3_GETSIZE_LIST_SEND;
357 return POP3_LOGOUT_SEND;
360 static gboolean should_delete(const char *uidl, gpointer data)
362 /* answer[0] will contain id
363 * answer[0] will contain uidl */
364 Pop3State *state = (Pop3State *) data;
368 int tdate, keep_for, today, nb_days;
373 if (!state->ac_prefs->rmmail || !strchr(uidl, ' '))
377 tuidl = g_strndup(uidl, strlen(uidl) - 2);
378 answer = g_strsplit(tuidl, " ", 2);
379 id = atoi(answer[0]);
381 if (NULL != (sdate = g_hash_table_lookup(state->uidl_table, answer[1]))) {
383 keep_for = atoi(state->ac_prefs->leave_time); /* FIXME: leave time should be an int */
385 g_date_clear(&curdate, 1);
386 g_date_set_time(&curdate, time(NULL));
387 today = g_date_day_of_year(&curdate);
389 nb_days = g_date_is_leap_year(g_date_year(&curdate)) ? 366 : 365;
390 result = ((tdate + keep_for) % nb_days <= today);
400 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
402 pop3_gen_send(sock, "LIST");
404 return POP3_GETSIZE_LIST_RECV;
407 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
409 Pop3State *state = (Pop3State *)data;
410 gchar buf[POPBUFSIZE];
413 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
415 state->cur_total_bytes = 0;
417 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
420 if (buf[0] == '.') break;
421 if (sscanf(buf, "%u %u", &num, &size) != 2)
424 if (num > 0 && num <= state->count)
425 state->msg[num].size = size;
426 if (num > 0 && num < state->cur_msg)
427 state->cur_total_bytes += size;
430 if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
434 return POP3_RETR_SEND;
437 gint pop3_top_send(SockInfo *sock, gpointer data)
439 Pop3State *state = (Pop3State *)data;
441 inc_progress_update(state, POP3_TOP_SEND);
443 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
445 return POP3_TOP_RECV;
448 gint pop3_top_recv(SockInfo *sock, gpointer data)
450 Pop3State *state = (Pop3State *)data;
451 gchar *filename, *path;
454 if (pop3_ok(sock, NULL) != PS_SUCCESS)
455 return POP3_LOGOUT_SEND;
457 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
459 if ( !is_dir_exist(path) )
462 filename = g_strdup_printf("%s%i", path, state->cur_msg);
464 if (recv_write_to_file(sock, filename) < 0) {
465 state->inc_state = INC_NOSPACE;
469 pop3_sd_state(state, POP3_TOP_RECV, &next_state);
471 if (state->cur_msg < state->count) {
473 return POP3_TOP_SEND;
475 return POP3_LOGOUT_SEND;
478 gint pop3_retr_send(SockInfo *sock, gpointer data)
480 Pop3State *state = (Pop3State *)data;
482 pop3_gen_send(sock, "RETR %d", state->cur_msg);
484 return POP3_RETR_RECV;
487 gint pop3_retr_recv(SockInfo *sock, gpointer data)
489 Pop3State *state = (Pop3State *)data;
495 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
496 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
497 if (state->inc_state == INC_SUCCESS)
498 state->inc_state = INC_NOSPACE;
502 if ((drop_ok = inc_drop_message(file, state)) < 0) {
503 state->inc_state = INC_ERROR;
507 if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
510 state->cur_total_bytes += state->msg[state->cur_msg].size;
511 state->cur_total_num++;
513 keep_for = (state->ac_prefs && state->ac_prefs->leave_time) ?
514 atoi(state->ac_prefs->leave_time) : 0;
516 if (drop_ok == 0 && state->ac_prefs->rmmail && keep_for == 0)
517 return POP3_DELETE_SEND;
519 state->msg[state->cur_msg].received = TRUE;
521 if (state->cur_msg < state->count) {
524 return POP3_RETR_SEND;
526 return POP3_LOGOUT_SEND;
527 } else if (ok == PS_PROTOCOL)
528 return POP3_LOGOUT_SEND;
533 gint pop3_delete_send(SockInfo *sock, gpointer data)
535 Pop3State *state = (Pop3State *)data;
537 pop3_gen_send(sock, "DELE %d", state->cur_msg);
539 return POP3_DELETE_RECV;
542 gint pop3_delete_recv(SockInfo *sock, gpointer data)
544 Pop3State *state = (Pop3State *)data;
548 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
550 state->msg[state->cur_msg].deleted = TRUE;
552 if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
555 if (state->cur_msg < state->count) {
558 return POP3_RETR_SEND;
560 return POP3_LOGOUT_SEND;
561 } else if (ok == PS_PROTOCOL)
562 return POP3_LOGOUT_SEND;
567 gint pop3_logout_send(SockInfo *sock, gpointer data)
569 Pop3State *state = (Pop3State *)data;
572 while (state->uidl_todelete_list != NULL) {
574 * FIXME: doesn't feel right - no checks for parts
576 parts = g_strsplit((gchar *) state->uidl_todelete_list->data, " ", 2);
577 state->uidl_todelete_list = g_slist_remove
578 (state->uidl_todelete_list, state->uidl_todelete_list->data);
579 pop3_gen_send(sock, "DELE %s", parts[0]);
580 if (pop3_ok(sock, NULL) != PS_SUCCESS)
581 log_warning(_("error occurred on DELE\n"));
585 pop3_gen_send(sock, "QUIT");
587 return POP3_LOGOUT_RECV;
590 gint pop3_logout_recv(SockInfo *sock, gpointer data)
592 if (pop3_ok(sock, NULL) == PS_SUCCESS)
598 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
601 gchar buf[POPBUFSIZE + 1];
604 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
606 if (*bufp == '+' || *bufp == '-')
611 while (isalpha(*bufp))
617 if (!strcmp(buf, "+OK"))
619 else if (!strncmp(buf, "-ERR", 4)) {
620 if (strstr(bufp, "lock") ||
621 strstr(bufp, "Lock") ||
622 strstr(bufp, "LOCK") ||
623 strstr(bufp, "wait"))
629 fprintf(stderr, "POP3: %s\n", bufp);
634 strcpy(argbuf, bufp);
640 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
642 gchar buf[POPBUFSIZE + 1];
645 va_start(args, format);
646 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
649 if (!strncasecmp(buf, "PASS ", 5))
650 log_print("POP3> PASS ********\n");
652 log_print("POP3> %s\n", buf);
655 sock_write(sock, buf, strlen(buf));
658 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
660 if (sock_gets(sock, buf, size) < 0) {
664 log_print("POP3< %s\n", buf);
670 static void pop3_sd_new_header(Pop3State *state)
672 HeaderItems *new_msg;
673 if (state->cur_msg <= state->count) {
674 new_msg = g_new0(HeaderItems, 1);
676 new_msg->index = state->cur_msg;
677 new_msg->state = SD_UNCHECKED;
678 new_msg->size = state->msg[state->cur_msg].size;
679 new_msg->received = state->msg[state->cur_msg].received;
680 new_msg->del_by_old_session = FALSE;
682 state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list,
687 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state)
689 gint session = state->ac_prefs->session;
690 guint goto_state = -1;
693 case POP3_GETRANGE_UIDL_RECV:
697 case STYPE_PREVIEW_ALL:
698 goto_state = POP3_GETSIZE_LIST_SEND;
703 case POP3_GETSIZE_LIST_RECV:
705 case STYPE_PREVIEW_ALL:
707 case STYPE_PREVIEW_NEW:
708 goto_state = POP3_TOP_SEND;
711 if (pop3_sd_get_next(state))
712 goto_state = POP3_DELETE_SEND;
714 goto_state = POP3_LOGOUT_SEND;
717 if (pop3_sd_get_next(state))
718 goto_state = POP3_RETR_SEND;
720 goto_state = POP3_LOGOUT_SEND;
727 case STYPE_PREVIEW_ALL:
728 case STYPE_PREVIEW_NEW:
729 pop3_sd_new_header(state);
737 if (state->ac_prefs->sd_rmmail_on_download)
738 goto_state = POP3_DELETE_SEND;
740 if (pop3_sd_get_next(state))
741 goto_state = POP3_RETR_SEND;
743 goto_state = POP3_LOGOUT_SEND;
749 case POP3_DELETE_RECV:
752 if (pop3_sd_get_next(state))
753 goto_state = POP3_DELETE_SEND;
755 goto_state = POP3_LOGOUT_SEND;
758 if (pop3_sd_get_next(state))
759 goto_state = POP3_RETR_SEND;
761 goto_state = POP3_LOGOUT_SEND;
770 *next_state = goto_state;
771 if (goto_state != -1)
777 gboolean pop3_sd_get_next(Pop3State *state)
780 gint deleted_msgs = 0;
782 switch (state->ac_prefs->session) {
785 for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
786 HeaderItems *items = (HeaderItems*)cur->data;
788 if (items->del_by_old_session)
791 switch (items->state) {
793 items->state = SD_REMOVED;
796 items->state = SD_DOWNLOADED;
799 state->cur_msg = items->index - deleted_msgs;
800 if (state->ac_prefs->session == STYPE_DELETE)
801 items->state = SD_REMOVE;
803 items->state = SD_DOWNLOAD;