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->sizes[state->cur_msg]; \
46 (state->ac_prefs->enable_size_limit && \
47 state->ac_prefs->size_limit > 0 && \
48 size > state->ac_prefs->size_limit * 1024)) { \
49 log_print(_("Skipping message %d\n"), state->cur_msg); \
51 state->cur_total_bytes += size; \
52 state->cur_total_num++; \
53 if (state->new_id_list) { \
54 state->id_list = g_slist_append(state->id_list, state->new_id_list->data); \
55 state->new_id_list = g_slist_remove(state->new_id_list, state->new_id_list->data); \
58 if (state->cur_msg == state->count) \
59 return POP3_LOGOUT_SEND; \
66 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
67 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
68 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
69 static gboolean pop3_delete_header (Pop3State *state);
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 return POP3_LOGOUT_SEND;
224 state->new = state->count;
225 if (state->ac_prefs->rmmail ||
226 state->ac_prefs->getall)
227 return POP3_GETSIZE_LIST_SEND;
229 return POP3_GETRANGE_UIDL_SEND;
232 } else if (ok == PS_PROTOCOL)
233 return POP3_LOGOUT_SEND;
238 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
240 Pop3State *state = (Pop3State *)data;
242 inc_progress_update(state, POP3_GETRANGE_LAST_SEND);
244 pop3_gen_send(sock, "LAST");
246 return POP3_GETRANGE_LAST_RECV;
249 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
251 Pop3State *state = (Pop3State *)data;
252 gchar buf[POPBUFSIZE + 1];
254 if (pop3_ok(sock, buf) == PS_SUCCESS) {
257 if (sscanf(buf, "%d", &last) == 0) {
258 log_warning(_("POP3 protocol error\n"));
261 if (state->count == last)
262 return POP3_LOGOUT_SEND;
264 state->new = state->count - last;
265 state->cur_msg = last + 1;
266 return POP3_GETSIZE_LIST_SEND;
270 return POP3_GETSIZE_LIST_SEND;
273 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
275 Pop3State *state = (Pop3State *)data;
277 inc_progress_update(state, POP3_GETRANGE_UIDL_SEND);
279 pop3_gen_send(sock, "UIDL");
281 return POP3_GETRANGE_UIDL_RECV;
284 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
286 Pop3State *state = (Pop3State *)data;
287 gboolean new = FALSE;
288 gchar buf[POPBUFSIZE];
291 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
293 if (!state->id_table) new = TRUE;
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)
303 g_hash_table_lookup(state->id_table, id) == NULL) {
304 state->new = state->count - num + 1;
305 state->cur_msg = num;
310 state->new_id_list = g_slist_append
311 (state->new_id_list, g_strdup(id));
313 state->id_list = g_slist_append
314 (state->id_list, g_strdup(id));
318 return POP3_GETSIZE_LIST_SEND;
320 return POP3_LOGOUT_SEND;
323 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
325 Pop3State *state = (Pop3State *)data;
327 inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
329 pop3_gen_send(sock, "LIST");
331 return POP3_GETSIZE_LIST_RECV;
334 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
336 Pop3State *state = (Pop3State *)data;
337 gchar buf[POPBUFSIZE];
339 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
341 state->sizes = g_new0(gint, state->count + 1);
342 state->cur_total_bytes = 0;
344 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
347 if (buf[0] == '.') break;
348 if (sscanf(buf, "%u %u", &num, &size) != 2)
351 if (num > 0 && num <= state->count)
352 state->sizes[num] = size;
353 if (num > 0 && num < state->cur_msg)
354 state->cur_total_bytes += size;
358 if (state->ac_prefs->session_type == RETR_HEADER)
359 return POP3_TOP_SEND;
361 return POP3_RETR_SEND;
364 gint pop3_top_send(SockInfo *sock, gpointer data)
366 Pop3State *state = (Pop3State *)data;
368 inc_progress_update(state, POP3_TOP_SEND);
370 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
372 return POP3_TOP_RECV;
375 gint pop3_top_recv(SockInfo *sock, gpointer data)
377 Pop3State *state = (Pop3State *)data;
379 gchar buf[POPBUFSIZE];
381 gchar *filename, *path;
383 inc_progress_update(state, POP3_TOP_RECV);
385 if (pop3_ok(sock, NULL) != PS_SUCCESS)
386 return POP3_LOGOUT_SEND;
388 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
390 if ( !is_dir_exist(path) )
393 filename = g_strdup_printf("%s%i", path, state->cur_msg);
395 if (recv_write_to_file(sock, filename) < 0) {
396 state->inc_state = INC_NOSPACE;
399 /* we add a Complete-Size Header Item ...
400 note: overwrites first line --> this is dirty */
401 if ( (fp = fopen(filename, "r+")) != NULL ) {
402 gchar *buf = g_strdup_printf("%s%i", SIZE_HEADER,
403 state->sizes[state->cur_msg]);
405 if (change_file_mode_rw(fp, filename) == 0)
406 fprintf(fp, "%s\n", buf);
413 if (state->cur_msg < state->count) {
415 return POP3_TOP_SEND;
417 return POP3_LOGOUT_SEND;
420 gint pop3_retr_send(SockInfo *sock, gpointer data)
422 Pop3State *state = (Pop3State *)data;
424 inc_progress_update(state, POP3_RETR_SEND);
426 pop3_gen_send(sock, "RETR %d", state->cur_msg);
428 return POP3_RETR_RECV;
431 gint pop3_retr_recv(SockInfo *sock, gpointer data)
433 Pop3State *state = (Pop3State *)data;
437 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
438 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
439 if (state->inc_state == INC_SUCCESS)
440 state->inc_state = INC_NOSPACE;
444 if ((drop_ok = inc_drop_message(file, state)) < 0) {
445 state->inc_state = INC_ERROR;
449 state->cur_total_bytes += state->sizes[state->cur_msg];
450 state->cur_total_num++;
452 if (drop_ok == 0 && state->ac_prefs->rmmail)
453 return POP3_DELETE_SEND;
455 if (state->new_id_list) {
456 state->id_list = g_slist_append
457 (state->id_list, state->new_id_list->data);
459 g_slist_remove(state->new_id_list,
460 state->new_id_list->data);
463 if (state->cur_msg < state->count) {
466 return POP3_RETR_SEND;
468 return POP3_LOGOUT_SEND;
469 } else if (ok == PS_PROTOCOL)
470 return POP3_LOGOUT_SEND;
475 gint pop3_delete_send(SockInfo *sock, gpointer data)
477 Pop3State *state = (Pop3State *)data;
479 /* inc_progress_update(state, POP3_DELETE_SEND); */
481 pop3_gen_send(sock, "DELE %d", state->cur_msg);
483 return POP3_DELETE_RECV;
486 gint pop3_delete_recv(SockInfo *sock, gpointer data)
488 Pop3State *state = (Pop3State *)data;
491 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
493 if (pop3_delete_header(state) == TRUE)
494 return POP3_DELETE_SEND;
496 if (state->cur_msg < state->count) {
499 return POP3_RETR_SEND;
501 return POP3_LOGOUT_SEND;
502 } else if (ok == PS_PROTOCOL)
503 return POP3_LOGOUT_SEND;
508 gint pop3_logout_send(SockInfo *sock, gpointer data)
510 Pop3State *state = (Pop3State *)data;
512 inc_progress_update(state, POP3_LOGOUT_SEND);
514 pop3_gen_send(sock, "QUIT");
516 return POP3_LOGOUT_RECV;
519 gint pop3_logout_recv(SockInfo *sock, gpointer data)
521 if (pop3_ok(sock, NULL) == PS_SUCCESS)
527 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
530 gchar buf[POPBUFSIZE + 1];
533 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
535 if (*bufp == '+' || *bufp == '-')
540 while (isalpha(*bufp))
546 if (!strcmp(buf, "+OK"))
548 else if (!strncmp(buf, "-ERR", 4)) {
549 if (strstr(bufp, "lock") ||
550 strstr(bufp, "Lock") ||
551 strstr(bufp, "LOCK") ||
552 strstr(bufp, "wait"))
558 fprintf(stderr, "POP3: %s\n", bufp);
563 strcpy(argbuf, bufp);
569 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
571 gchar buf[POPBUFSIZE + 1];
574 va_start(args, format);
575 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
578 if (!strncasecmp(buf, "PASS ", 5))
579 log_print("POP3> PASS ********\n");
581 log_print("POP3> %s\n", buf);
584 sock_write(sock, buf, strlen(buf));
587 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
589 if (sock_gets(sock, buf, size) < 0) {
593 log_print("POP3< %s\n", buf);
599 gboolean pop3_delete_header (Pop3State *state)
602 if ( (state->ac_prefs->session_type == DELE_HEADER) &&
603 (g_slist_length(state->ac_prefs->to_delete) > 0) ) {
605 state->cur_msg = (gint) state->ac_prefs->to_delete->data;
606 debug_print(_("next to delete %i\n"), state->cur_msg);
607 state->ac_prefs->to_delete = g_slist_remove
608 (state->ac_prefs->to_delete, state->ac_prefs->to_delete->data);