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"
41 #define LOOKUP_NEXT_MSG() \
43 gint size = state->msg[state->cur_msg].size; \
44 gboolean size_limit_over = \
45 (state->ac_prefs->enable_size_limit && \
46 state->ac_prefs->size_limit > 0 && \
47 size > state->ac_prefs->size_limit * 1024); \
49 if (size_limit_over) \
50 log_print(_("POP3: Skipping message %d (%d bytes)\n"), \
51 state->cur_msg, size); \
53 if (size == 0 || state->msg[state->cur_msg].received || \
56 state->cur_total_bytes += size; \
57 if (state->cur_msg == state->count) \
58 return POP3_LOGOUT_SEND; \
65 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
66 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
67 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
68 static gboolean pop3_delete_header (Pop3State *state);
69 static gboolean should_delete (const char *uidl, gpointer data);
71 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
73 Pop3State *state = (Pop3State *)data;
74 gchar buf[POPBUFSIZE];
76 if (pop3_ok(sock, buf) == PS_SUCCESS) {
77 if (state->ac_prefs->protocol == A_APOP) {
78 state->greeting = g_strdup(buf);
79 return POP3_GETAUTH_APOP_SEND;
81 return POP3_GETAUTH_USER_SEND;
86 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
88 Pop3State *state = (Pop3State *)data;
90 g_return_val_if_fail(state->user != NULL, -1);
92 pop3_gen_send(sock, "USER %s", state->user);
94 return POP3_GETAUTH_USER_RECV;
97 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
99 Pop3State *state = (Pop3State *)data;
101 if (pop3_ok(sock, NULL) == PS_SUCCESS)
102 return POP3_GETAUTH_PASS_SEND;
104 log_warning(_("error occurred on authentication\n"));
105 state->error_val = PS_AUTHFAIL;
106 state->inc_state = INC_AUTH_FAILED;
111 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
113 Pop3State *state = (Pop3State *)data;
115 g_return_val_if_fail(state->pass != NULL, -1);
117 pop3_gen_send(sock, "PASS %s", state->pass);
119 return POP3_GETAUTH_PASS_RECV;
122 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
124 Pop3State *state = (Pop3State *)data;
126 if (pop3_ok(sock, NULL) == PS_SUCCESS) {
128 if (pop3_delete_header(state) == TRUE)
129 return POP3_DELETE_SEND;
131 return POP3_GETRANGE_STAT_SEND;
134 log_warning(_("error occurred on authentication\n"));
135 state->error_val = PS_AUTHFAIL;
136 state->inc_state = INC_AUTH_FAILED;
141 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
143 Pop3State *state = (Pop3State *)data;
148 g_return_val_if_fail(state->user != NULL, -1);
149 g_return_val_if_fail(state->pass != NULL, -1);
151 if ((start = strchr(state->greeting, '<')) == NULL) {
152 log_warning(_("Required APOP timestamp not found "
157 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
158 log_warning(_("Timestamp syntax error in greeting\n"));
164 apop_str = g_strconcat(start, state->pass, NULL);
165 md5_hex_digest(md5sum, apop_str);
168 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
170 return POP3_GETAUTH_APOP_RECV;
173 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
175 Pop3State *state = (Pop3State *)data;
177 if (pop3_ok(sock, NULL) == PS_SUCCESS) {
179 if (pop3_delete_header(state) == TRUE)
180 return POP3_DELETE_SEND;
182 return POP3_GETRANGE_STAT_SEND;
186 log_warning(_("error occurred on authentication\n"));
187 state->error_val = PS_AUTHFAIL;
188 state->inc_state = INC_AUTH_FAILED;
193 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
195 pop3_gen_send(sock, "STAT");
197 return POP3_GETRANGE_STAT_RECV;
200 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
202 Pop3State *state = (Pop3State *)data;
203 gchar buf[POPBUFSIZE + 1];
206 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
207 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
209 log_warning(_("POP3 protocol error\n"));
212 if (state->count == 0) {
213 state->uidl_is_valid = TRUE;
214 return POP3_LOGOUT_SEND;
217 (Pop3MsgInfo, state->count + 1);
219 return POP3_GETRANGE_UIDL_SEND;
222 } else if (ok == PS_PROTOCOL)
223 return POP3_LOGOUT_SEND;
228 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
230 pop3_gen_send(sock, "LAST");
232 return POP3_GETRANGE_LAST_RECV;
235 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
237 Pop3State *state = (Pop3State *)data;
238 gchar buf[POPBUFSIZE + 1];
240 if (pop3_ok(sock, buf) == PS_SUCCESS) {
243 if (sscanf(buf, "%d", &last) == 0) {
244 log_warning(_("POP3 protocol error\n"));
247 if (state->count == last)
248 return POP3_LOGOUT_SEND;
250 state->cur_msg = last + 1;
251 return POP3_GETSIZE_LIST_SEND;
255 return POP3_GETSIZE_LIST_SEND;
258 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
260 pop3_gen_send(sock, "UIDL");
262 return POP3_GETRANGE_UIDL_RECV;
265 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
267 Pop3State *state = (Pop3State *)data;
268 gboolean new = FALSE;
269 gboolean get_all = FALSE;
270 gchar buf[POPBUFSIZE];
273 if (!state->uidl_table) new = TRUE;
274 if (state->ac_prefs->getall)
277 if (pop3_ok(sock, NULL) != PS_SUCCESS) {
278 /* UIDL is not supported */
280 return POP3_GETRANGE_LAST_SEND;
282 return POP3_GETSIZE_LIST_SEND;
285 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
288 if (buf[0] == '.') break;
289 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
291 if (num <= 0 || num > state->count) continue;
293 state->msg[num].uidl = g_strdup(id);
295 if (state->uidl_table) {
297 g_hash_table_lookup(state->uidl_table, id) != NULL)
298 state->msg[num].received = TRUE;
301 state->cur_msg = num;
307 if (should_delete(buf, (Pop3State *) state))
308 state->uidl_todelete_list = g_slist_append
309 (state->uidl_todelete_list, g_strdup(buf));
313 state->uidl_is_valid = TRUE;
316 return POP3_GETSIZE_LIST_SEND;
318 return POP3_LOGOUT_SEND;
321 static gboolean should_delete(const char *uidl, gpointer data)
323 /* answer[0] will contain id
324 * answer[0] will contain uidl */
325 Pop3State *state = (Pop3State *) data;
329 int tdate, keep_for, today, nb_days;
334 if (!state->ac_prefs->rmmail || !strchr(uidl, ' '))
338 tuidl = g_strndup(uidl, strlen(uidl) - 2);
339 answer = g_strsplit(tuidl, " ", 2);
340 id = atoi(answer[0]);
342 if (NULL != (sdate = g_hash_table_lookup(state->uidl_table, answer[1]))) {
344 keep_for = atoi(state->ac_prefs->leave_time); /* FIXME: leave time should be an int */
346 g_date_clear(&curdate, 1);
347 g_date_set_time(&curdate, time(NULL));
348 today = g_date_day_of_year(&curdate);
350 nb_days = g_date_is_leap_year(g_date_year(&curdate)) ? 366 : 365;
351 result = ((tdate + keep_for) % nb_days <= today);
361 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
363 pop3_gen_send(sock, "LIST");
365 return POP3_GETSIZE_LIST_RECV;
368 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
370 Pop3State *state = (Pop3State *)data;
371 gchar buf[POPBUFSIZE];
373 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
375 state->cur_total_bytes = 0;
377 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
380 if (buf[0] == '.') break;
381 if (sscanf(buf, "%u %u", &num, &size) != 2)
384 if (num > 0 && num <= state->count)
385 state->msg[num].size = size;
386 if (num > 0 && num < state->cur_msg)
387 state->cur_total_bytes += size;
391 if (state->ac_prefs->session_type == RETR_HEADER)
392 return POP3_TOP_SEND;
394 return POP3_RETR_SEND;
397 gint pop3_top_send(SockInfo *sock, gpointer data)
399 Pop3State *state = (Pop3State *)data;
401 inc_progress_update(state, POP3_TOP_SEND);
403 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
405 return POP3_TOP_RECV;
408 gint pop3_top_recv(SockInfo *sock, gpointer data)
410 Pop3State *state = (Pop3State *)data;
412 gchar buf[POPBUFSIZE];
414 gchar *filename, *path;
416 inc_progress_update(state, POP3_TOP_RECV);
418 if (pop3_ok(sock, NULL) != PS_SUCCESS)
419 return POP3_LOGOUT_SEND;
421 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
423 if ( !is_dir_exist(path) )
426 filename = g_strdup_printf("%s%i", path, state->cur_msg);
428 if (recv_write_to_file(sock, filename) < 0) {
429 state->inc_state = INC_NOSPACE;
432 /* we add a Complete-Size Header Item ...
433 note: overwrites first line --> this is dirty */
434 if ( (fp = fopen(filename, "rb+")) != NULL ) {
435 gchar *buf = g_strdup_printf("%s%i", SIZE_HEADER,
436 state->msg[state->cur_msg].size);
438 if (change_file_mode_rw(fp, filename) == 0)
439 fprintf(fp, "%s\n", buf);
447 if (state->cur_msg < state->count) {
449 return POP3_TOP_SEND;
451 return POP3_LOGOUT_SEND;
454 gint pop3_retr_send(SockInfo *sock, gpointer data)
456 Pop3State *state = (Pop3State *)data;
458 pop3_gen_send(sock, "RETR %d", state->cur_msg);
460 return POP3_RETR_RECV;
463 gint pop3_retr_recv(SockInfo *sock, gpointer data)
465 Pop3State *state = (Pop3State *)data;
470 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
471 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
472 if (state->inc_state == INC_SUCCESS)
473 state->inc_state = INC_NOSPACE;
477 if ((drop_ok = inc_drop_message(file, state)) < 0) {
478 state->inc_state = INC_ERROR;
482 state->cur_total_bytes += state->msg[state->cur_msg].size;
483 state->cur_total_num++;
485 keep_for = (state->ac_prefs && state->ac_prefs->leave_time) ?
486 atoi(state->ac_prefs->leave_time) : 0;
488 if (drop_ok == 0 && state->ac_prefs->rmmail && keep_for == 0)
489 return POP3_DELETE_SEND;
491 state->msg[state->cur_msg].received = TRUE;
493 if (state->cur_msg < state->count) {
496 return POP3_RETR_SEND;
498 return POP3_LOGOUT_SEND;
499 } else if (ok == PS_PROTOCOL)
500 return POP3_LOGOUT_SEND;
505 gint pop3_delete_send(SockInfo *sock, gpointer data)
507 Pop3State *state = (Pop3State *)data;
509 pop3_gen_send(sock, "DELE %d", state->cur_msg);
511 return POP3_DELETE_RECV;
514 gint pop3_delete_recv(SockInfo *sock, gpointer data)
516 Pop3State *state = (Pop3State *)data;
519 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
520 if (state->ac_prefs->session_type == RETR_NORMAL)
521 state->msg[state->cur_msg].deleted = TRUE;
523 if (pop3_delete_header(state) == TRUE)
524 return POP3_DELETE_SEND;
526 if (state->cur_msg < state->count) {
529 return POP3_RETR_SEND;
531 return POP3_LOGOUT_SEND;
532 } else if (ok == PS_PROTOCOL)
533 return POP3_LOGOUT_SEND;
538 gint pop3_logout_send(SockInfo *sock, gpointer data)
540 Pop3State *state = (Pop3State *)data;
543 while (state->uidl_todelete_list != NULL) {
545 * FIXME: doesn't feel right - no checks for parts
547 parts = g_strsplit((gchar *) state->uidl_todelete_list->data, " ", 2);
548 state->uidl_todelete_list = g_slist_remove
549 (state->uidl_todelete_list, state->uidl_todelete_list->data);
550 pop3_gen_send(sock, "DELE %s", parts[0]);
551 if (pop3_ok(sock, NULL) != PS_SUCCESS)
552 log_warning(_("error occurred on DELE\n"));
556 pop3_gen_send(sock, "QUIT");
558 return POP3_LOGOUT_RECV;
561 gint pop3_logout_recv(SockInfo *sock, gpointer data)
563 if (pop3_ok(sock, NULL) == PS_SUCCESS)
569 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
572 gchar buf[POPBUFSIZE + 1];
575 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
577 if (*bufp == '+' || *bufp == '-')
582 while (isalpha(*bufp))
588 if (!strcmp(buf, "+OK"))
590 else if (!strncmp(buf, "-ERR", 4)) {
591 if (strstr(bufp, "lock") ||
592 strstr(bufp, "Lock") ||
593 strstr(bufp, "LOCK") ||
594 strstr(bufp, "wait"))
600 fprintf(stderr, "POP3: %s\n", bufp);
605 strcpy(argbuf, bufp);
611 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
613 gchar buf[POPBUFSIZE + 1];
616 va_start(args, format);
617 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
620 if (!strncasecmp(buf, "PASS ", 5))
621 log_print("POP3> PASS ********\n");
623 log_print("POP3> %s\n", buf);
626 sock_write(sock, buf, strlen(buf));
629 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
631 if (sock_gets(sock, buf, size) < 0) {
635 log_print("POP3< %s\n", buf);
641 gboolean pop3_delete_header (Pop3State *state)
644 if ( (state->ac_prefs->session_type == DELE_HEADER) &&
645 (g_slist_length(state->ac_prefs->to_delete) > 0) ) {
647 state->cur_msg = (gint) state->ac_prefs->to_delete->data;
648 debug_print(_("next to delete %i\n"), state->cur_msg);
649 state->ac_prefs->to_delete = g_slist_remove
650 (state->ac_prefs->to_delete, state->ac_prefs->to_delete->data);