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 inc_progress_update(state, POP3_GETAUTH_USER_SEND);
94 pop3_gen_send(sock, "USER %s", state->user);
96 return POP3_GETAUTH_USER_RECV;
99 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
101 Pop3State *state = (Pop3State *)data;
103 if (pop3_ok(sock, NULL) == PS_SUCCESS)
104 return POP3_GETAUTH_PASS_SEND;
106 log_warning(_("error occurred on authentication\n"));
107 state->error_val = PS_AUTHFAIL;
108 state->inc_state = INC_AUTH_FAILED;
113 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
115 Pop3State *state = (Pop3State *)data;
117 g_return_val_if_fail(state->pass != NULL, -1);
119 pop3_gen_send(sock, "PASS %s", state->pass);
121 return POP3_GETAUTH_PASS_RECV;
124 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
126 Pop3State *state = (Pop3State *)data;
128 if (pop3_ok(sock, NULL) == PS_SUCCESS) {
130 if (pop3_delete_header(state) == TRUE)
131 return POP3_DELETE_SEND;
133 return POP3_GETRANGE_STAT_SEND;
136 log_warning(_("error occurred on authentication\n"));
137 state->error_val = PS_AUTHFAIL;
138 state->inc_state = INC_AUTH_FAILED;
143 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
145 Pop3State *state = (Pop3State *)data;
150 g_return_val_if_fail(state->user != NULL, -1);
151 g_return_val_if_fail(state->pass != NULL, -1);
153 inc_progress_update(state, POP3_GETAUTH_APOP_SEND);
155 if ((start = strchr(state->greeting, '<')) == NULL) {
156 log_warning(_("Required APOP timestamp not found "
161 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
162 log_warning(_("Timestamp syntax error in greeting\n"));
168 apop_str = g_strconcat(start, state->pass, NULL);
169 md5_hex_digest(md5sum, apop_str);
172 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
174 return POP3_GETAUTH_APOP_RECV;
177 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
179 Pop3State *state = (Pop3State *)data;
181 if (pop3_ok(sock, NULL) == PS_SUCCESS) {
183 if (pop3_delete_header(state) == TRUE)
184 return POP3_DELETE_SEND;
186 return POP3_GETRANGE_STAT_SEND;
190 log_warning(_("error occurred on authentication\n"));
191 state->error_val = PS_AUTHFAIL;
192 state->inc_state = INC_AUTH_FAILED;
197 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
199 Pop3State *state = (Pop3State *)data;
201 inc_progress_update(state, POP3_GETRANGE_STAT_SEND);
203 pop3_gen_send(sock, "STAT");
205 return POP3_GETRANGE_STAT_RECV;
208 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
210 Pop3State *state = (Pop3State *)data;
211 gchar buf[POPBUFSIZE + 1];
214 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
215 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
217 log_warning(_("POP3 protocol error\n"));
220 if (state->count == 0) {
221 state->uidl_is_valid = TRUE;
222 return POP3_LOGOUT_SEND;
225 (Pop3MsgInfo, state->count + 1);
227 return POP3_GETRANGE_UIDL_SEND;
230 } else if (ok == PS_PROTOCOL)
231 return POP3_LOGOUT_SEND;
236 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
238 Pop3State *state = (Pop3State *)data;
240 inc_progress_update(state, POP3_GETRANGE_LAST_SEND);
242 pop3_gen_send(sock, "LAST");
244 return POP3_GETRANGE_LAST_RECV;
247 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
249 Pop3State *state = (Pop3State *)data;
250 gchar buf[POPBUFSIZE + 1];
252 if (pop3_ok(sock, buf) == PS_SUCCESS) {
255 if (sscanf(buf, "%d", &last) == 0) {
256 log_warning(_("POP3 protocol error\n"));
259 if (state->count == last)
260 return POP3_LOGOUT_SEND;
262 state->cur_msg = last + 1;
263 return POP3_GETSIZE_LIST_SEND;
267 return POP3_GETSIZE_LIST_SEND;
270 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
272 Pop3State *state = (Pop3State *)data;
274 inc_progress_update(state, POP3_GETRANGE_UIDL_SEND);
276 pop3_gen_send(sock, "UIDL");
278 return POP3_GETRANGE_UIDL_RECV;
281 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
283 Pop3State *state = (Pop3State *)data;
284 gboolean new = FALSE;
285 gboolean get_all = FALSE;
286 gchar buf[POPBUFSIZE];
289 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
291 if (!state->uidl_table) new = TRUE;
292 if (state->ac_prefs->getall)
295 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
298 if (buf[0] == '.') break;
299 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
301 if (num <= 0 || num > state->count) continue;
303 state->msg[num].uidl = g_strdup(id);
305 if (state->uidl_table) {
307 g_hash_table_lookup(state->uidl_table, id) != NULL)
308 state->msg[num].received = TRUE;
311 state->cur_msg = num;
317 if (should_delete(buf, (Pop3State *) state))
318 state->uidl_todelete_list = g_slist_append
319 (state->uidl_todelete_list, g_strdup(buf));
323 state->uidl_is_valid = TRUE;
326 return POP3_GETSIZE_LIST_SEND;
328 return POP3_LOGOUT_SEND;
331 static gboolean should_delete(const char *uidl, gpointer data)
333 /* answer[0] will contain id
334 * answer[0] will contain uidl */
335 Pop3State *state = (Pop3State *) data;
339 int tdate, keep_for, today, nb_days;
344 if (!state->ac_prefs->rmmail || !strchr(uidl, ' '))
348 tuidl = g_strndup(uidl, strlen(uidl) - 2);
349 answer = g_strsplit(tuidl, " ", 2);
350 id = atoi(answer[0]);
352 if (NULL != (sdate = g_hash_table_lookup(state->uidl_table, answer[1]))) {
354 keep_for = atoi(state->ac_prefs->leave_time); /* FIXME: leave time should be an int */
356 g_date_clear(&curdate, 1);
357 g_date_set_time(&curdate, time(NULL));
358 today = g_date_day_of_year(&curdate);
360 nb_days = g_date_is_leap_year(g_date_year(&curdate)) ? 366 : 365;
361 result = ((tdate + keep_for) % nb_days <= today);
371 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
373 Pop3State *state = (Pop3State *)data;
375 inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
377 pop3_gen_send(sock, "LIST");
379 return POP3_GETSIZE_LIST_RECV;
382 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
384 Pop3State *state = (Pop3State *)data;
385 gchar buf[POPBUFSIZE];
387 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
389 state->cur_total_bytes = 0;
391 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
394 if (buf[0] == '.') break;
395 if (sscanf(buf, "%u %u", &num, &size) != 2)
398 if (num > 0 && num <= state->count)
399 state->msg[num].size = size;
400 if (num > 0 && num < state->cur_msg)
401 state->cur_total_bytes += size;
405 if (state->ac_prefs->session_type == RETR_HEADER)
406 return POP3_TOP_SEND;
408 return POP3_RETR_SEND;
411 gint pop3_top_send(SockInfo *sock, gpointer data)
413 Pop3State *state = (Pop3State *)data;
415 inc_progress_update(state, POP3_TOP_SEND);
417 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
419 return POP3_TOP_RECV;
422 gint pop3_top_recv(SockInfo *sock, gpointer data)
424 Pop3State *state = (Pop3State *)data;
426 gchar buf[POPBUFSIZE];
428 gchar *filename, *path;
430 inc_progress_update(state, POP3_TOP_RECV);
432 if (pop3_ok(sock, NULL) != PS_SUCCESS)
433 return POP3_LOGOUT_SEND;
435 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
437 if ( !is_dir_exist(path) )
440 filename = g_strdup_printf("%s%i", path, state->cur_msg);
442 if (recv_write_to_file(sock, filename) < 0) {
443 state->inc_state = INC_NOSPACE;
446 /* we add a Complete-Size Header Item ...
447 note: overwrites first line --> this is dirty */
448 if ( (fp = fopen(filename, "rb+")) != NULL ) {
449 gchar *buf = g_strdup_printf("%s%i", SIZE_HEADER,
450 state->msg[state->cur_msg].size);
452 if (change_file_mode_rw(fp, filename) == 0)
453 fprintf(fp, "%s\n", buf);
461 if (state->cur_msg < state->count) {
463 return POP3_TOP_SEND;
465 return POP3_LOGOUT_SEND;
468 gint pop3_retr_send(SockInfo *sock, gpointer data)
470 Pop3State *state = (Pop3State *)data;
472 inc_progress_update(state, POP3_RETR_SEND);
474 pop3_gen_send(sock, "RETR %d", state->cur_msg);
476 return POP3_RETR_RECV;
479 gint pop3_retr_recv(SockInfo *sock, gpointer data)
481 Pop3State *state = (Pop3State *)data;
486 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
487 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
488 if (state->inc_state == INC_SUCCESS)
489 state->inc_state = INC_NOSPACE;
493 if ((drop_ok = inc_drop_message(file, state)) < 0) {
494 state->inc_state = INC_ERROR;
498 state->cur_total_bytes += state->msg[state->cur_msg].size;
499 state->cur_total_num++;
501 keep_for = (state->ac_prefs && state->ac_prefs->leave_time) ?
502 atoi(state->ac_prefs->leave_time) : 0;
504 if (drop_ok == 0 && state->ac_prefs->rmmail && keep_for == 0)
505 return POP3_DELETE_SEND;
507 state->msg[state->cur_msg].received = TRUE;
509 if (state->cur_msg < state->count) {
512 return POP3_RETR_SEND;
514 return POP3_LOGOUT_SEND;
515 } else if (ok == PS_PROTOCOL)
516 return POP3_LOGOUT_SEND;
521 gint pop3_delete_send(SockInfo *sock, gpointer data)
523 Pop3State *state = (Pop3State *)data;
525 /* inc_progress_update(state, POP3_DELETE_SEND); */
527 pop3_gen_send(sock, "DELE %d", state->cur_msg);
529 return POP3_DELETE_RECV;
532 gint pop3_delete_recv(SockInfo *sock, gpointer data)
534 Pop3State *state = (Pop3State *)data;
537 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
538 if (state->ac_prefs->session_type == RETR_NORMAL)
539 state->msg[state->cur_msg].deleted = TRUE;
541 if (pop3_delete_header(state) == TRUE)
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 return POP3_LOGOUT_SEND;
556 gint pop3_logout_send(SockInfo *sock, gpointer data)
558 Pop3State *state = (Pop3State *)data;
561 while (state->uidl_todelete_list != NULL) {
563 * FIXME: doesn't feel right - no checks for parts
565 parts = g_strsplit((gchar *) state->uidl_todelete_list->data, " ", 2);
566 state->uidl_todelete_list = g_slist_remove
567 (state->uidl_todelete_list, state->uidl_todelete_list->data);
568 pop3_gen_send(sock, "DELE %s", parts[0]);
569 if (pop3_ok(sock, NULL) != PS_SUCCESS)
570 log_warning(_("error occurred on DELE\n"));
574 inc_progress_update(state, POP3_LOGOUT_SEND);
576 pop3_gen_send(sock, "QUIT");
578 return POP3_LOGOUT_RECV;
581 gint pop3_logout_recv(SockInfo *sock, gpointer data)
583 if (pop3_ok(sock, NULL) == PS_SUCCESS)
589 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
592 gchar buf[POPBUFSIZE + 1];
595 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
597 if (*bufp == '+' || *bufp == '-')
602 while (isalpha(*bufp))
608 if (!strcmp(buf, "+OK"))
610 else if (!strncmp(buf, "-ERR", 4)) {
611 if (strstr(bufp, "lock") ||
612 strstr(bufp, "Lock") ||
613 strstr(bufp, "LOCK") ||
614 strstr(bufp, "wait"))
620 fprintf(stderr, "POP3: %s\n", bufp);
625 strcpy(argbuf, bufp);
631 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
633 gchar buf[POPBUFSIZE + 1];
636 va_start(args, format);
637 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
640 if (!strncasecmp(buf, "PASS ", 5))
641 log_print("POP3> PASS ********\n");
643 log_print("POP3> %s\n", buf);
646 sock_write(sock, buf, strlen(buf));
649 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
651 if (sock_gets(sock, buf, size) < 0) {
655 log_print("POP3< %s\n", buf);
661 gboolean pop3_delete_header (Pop3State *state)
664 if ( (state->ac_prefs->session_type == DELE_HEADER) &&
665 (g_slist_length(state->ac_prefs->to_delete) > 0) ) {
667 state->cur_msg = (gint) state->ac_prefs->to_delete->data;
668 debug_print(_("next to delete %i\n"), state->cur_msg);
669 state->ac_prefs->to_delete = g_slist_remove
670 (state->ac_prefs->to_delete, state->ac_prefs->to_delete->data);