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);
70 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
72 Pop3State *state = (Pop3State *)data;
73 gchar buf[POPBUFSIZE];
75 if (pop3_ok(sock, buf) == PS_SUCCESS) {
76 if (state->ac_prefs->protocol == A_APOP) {
77 state->greeting = g_strdup(buf);
78 return POP3_GETAUTH_APOP_SEND;
80 return POP3_GETAUTH_USER_SEND;
85 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
87 Pop3State *state = (Pop3State *)data;
89 g_return_val_if_fail(state->user != NULL, -1);
91 inc_progress_update(state, POP3_GETAUTH_USER_SEND);
93 pop3_gen_send(sock, "USER %s", state->user);
95 return POP3_GETAUTH_USER_RECV;
98 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
100 Pop3State *state = (Pop3State *)data;
102 if (pop3_ok(sock, NULL) == PS_SUCCESS)
103 return POP3_GETAUTH_PASS_SEND;
105 log_warning(_("error occurred on authentication\n"));
106 state->error_val = PS_AUTHFAIL;
107 state->inc_state = INC_AUTH_FAILED;
112 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
114 Pop3State *state = (Pop3State *)data;
116 g_return_val_if_fail(state->pass != NULL, -1);
118 pop3_gen_send(sock, "PASS %s", state->pass);
120 return POP3_GETAUTH_PASS_RECV;
123 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
125 Pop3State *state = (Pop3State *)data;
127 if (pop3_ok(sock, NULL) == PS_SUCCESS) {
129 if (pop3_delete_header(state) == TRUE)
130 return POP3_DELETE_SEND;
132 return POP3_GETRANGE_STAT_SEND;
135 log_warning(_("error occurred on authentication\n"));
136 state->error_val = PS_AUTHFAIL;
137 state->inc_state = INC_AUTH_FAILED;
142 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
144 Pop3State *state = (Pop3State *)data;
149 g_return_val_if_fail(state->user != NULL, -1);
150 g_return_val_if_fail(state->pass != NULL, -1);
152 inc_progress_update(state, POP3_GETAUTH_APOP_SEND);
154 if ((start = strchr(state->greeting, '<')) == NULL) {
155 log_warning(_("Required APOP timestamp not found "
160 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
161 log_warning(_("Timestamp syntax error in greeting\n"));
167 apop_str = g_strconcat(start, state->pass, NULL);
168 md5_hex_digest(md5sum, apop_str);
171 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
173 return POP3_GETAUTH_APOP_RECV;
176 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
178 Pop3State *state = (Pop3State *)data;
180 if (pop3_ok(sock, NULL) == PS_SUCCESS) {
182 if (pop3_delete_header(state) == TRUE)
183 return POP3_DELETE_SEND;
185 return POP3_GETRANGE_STAT_SEND;
189 log_warning(_("error occurred on authentication\n"));
190 state->error_val = PS_AUTHFAIL;
191 state->inc_state = INC_AUTH_FAILED;
196 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
198 Pop3State *state = (Pop3State *)data;
200 inc_progress_update(state, POP3_GETRANGE_STAT_SEND);
202 pop3_gen_send(sock, "STAT");
204 return POP3_GETRANGE_STAT_RECV;
207 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
209 Pop3State *state = (Pop3State *)data;
210 gchar buf[POPBUFSIZE + 1];
213 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
214 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
216 log_warning(_("POP3 protocol error\n"));
219 if (state->count == 0) {
220 state->uidl_is_valid = TRUE;
221 return POP3_LOGOUT_SEND;
224 (Pop3MsgInfo, state->count + 1);
226 return POP3_GETRANGE_UIDL_SEND;
229 } else if (ok == PS_PROTOCOL)
230 return POP3_LOGOUT_SEND;
235 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
237 Pop3State *state = (Pop3State *)data;
239 inc_progress_update(state, POP3_GETRANGE_LAST_SEND);
241 pop3_gen_send(sock, "LAST");
243 return POP3_GETRANGE_LAST_RECV;
246 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
248 Pop3State *state = (Pop3State *)data;
249 gchar buf[POPBUFSIZE + 1];
251 if (pop3_ok(sock, buf) == PS_SUCCESS) {
254 if (sscanf(buf, "%d", &last) == 0) {
255 log_warning(_("POP3 protocol error\n"));
258 if (state->count == last)
259 return POP3_LOGOUT_SEND;
261 state->cur_msg = last + 1;
262 return POP3_GETSIZE_LIST_SEND;
266 return POP3_GETSIZE_LIST_SEND;
269 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
271 Pop3State *state = (Pop3State *)data;
273 inc_progress_update(state, POP3_GETRANGE_UIDL_SEND);
275 pop3_gen_send(sock, "UIDL");
277 return POP3_GETRANGE_UIDL_RECV;
280 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
282 Pop3State *state = (Pop3State *)data;
283 gboolean new = FALSE;
284 gboolean get_all = FALSE;
285 gchar buf[POPBUFSIZE];
288 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
290 if (!state->uidl_table) new = TRUE;
291 if (state->ac_prefs->rmmail || state->ac_prefs->getall)
294 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
297 if (buf[0] == '.') break;
298 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
300 if (num <= 0 || num > state->count) continue;
302 state->msg[num].uidl = g_strdup(id);
304 if (state->uidl_table) {
306 g_hash_table_lookup(state->uidl_table, id) != NULL)
307 state->msg[num].received = TRUE;
310 state->cur_msg = num;
317 state->uidl_is_valid = TRUE;
320 return POP3_GETSIZE_LIST_SEND;
322 return POP3_LOGOUT_SEND;
325 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
327 Pop3State *state = (Pop3State *)data;
329 inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
331 pop3_gen_send(sock, "LIST");
333 return POP3_GETSIZE_LIST_RECV;
336 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
338 Pop3State *state = (Pop3State *)data;
339 gchar buf[POPBUFSIZE];
341 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
343 state->cur_total_bytes = 0;
345 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
348 if (buf[0] == '.') break;
349 if (sscanf(buf, "%u %u", &num, &size) != 2)
352 if (num > 0 && num <= state->count)
353 state->msg[num].size = size;
354 if (num > 0 && num < state->cur_msg)
355 state->cur_total_bytes += size;
359 if (state->ac_prefs->session_type == RETR_HEADER)
360 return POP3_TOP_SEND;
362 return POP3_RETR_SEND;
365 gint pop3_top_send(SockInfo *sock, gpointer data)
367 Pop3State *state = (Pop3State *)data;
369 inc_progress_update(state, POP3_TOP_SEND);
371 pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
373 return POP3_TOP_RECV;
376 gint pop3_top_recv(SockInfo *sock, gpointer data)
378 Pop3State *state = (Pop3State *)data;
380 gchar buf[POPBUFSIZE];
382 gchar *filename, *path;
384 inc_progress_update(state, POP3_TOP_RECV);
386 if (pop3_ok(sock, NULL) != PS_SUCCESS)
387 return POP3_LOGOUT_SEND;
389 path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
391 if ( !is_dir_exist(path) )
394 filename = g_strdup_printf("%s%i", path, state->cur_msg);
396 if (recv_write_to_file(sock, filename) < 0) {
397 state->inc_state = INC_NOSPACE;
400 /* we add a Complete-Size Header Item ...
401 note: overwrites first line --> this is dirty */
402 if ( (fp = fopen(filename, "r+")) != NULL ) {
403 gchar *buf = g_strdup_printf("%s%i", SIZE_HEADER,
404 state->msg[state->cur_msg].size);
406 if (change_file_mode_rw(fp, filename) == 0)
407 fprintf(fp, "%s\n", buf);
414 if (state->cur_msg < state->count) {
416 return POP3_TOP_SEND;
418 return POP3_LOGOUT_SEND;
421 gint pop3_retr_send(SockInfo *sock, gpointer data)
423 Pop3State *state = (Pop3State *)data;
425 inc_progress_update(state, POP3_RETR_SEND);
427 pop3_gen_send(sock, "RETR %d", state->cur_msg);
429 return POP3_RETR_RECV;
432 gint pop3_retr_recv(SockInfo *sock, gpointer data)
434 Pop3State *state = (Pop3State *)data;
438 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
439 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
440 if (state->inc_state == INC_SUCCESS)
441 state->inc_state = INC_NOSPACE;
445 if ((drop_ok = inc_drop_message(file, state)) < 0) {
446 state->inc_state = INC_ERROR;
450 state->cur_total_bytes += state->msg[state->cur_msg].size;
451 state->cur_total_num++;
453 if (drop_ok == 0 && state->ac_prefs->rmmail)
454 return POP3_DELETE_SEND;
456 state->msg[state->cur_msg].received = TRUE;
458 if (state->cur_msg < state->count) {
461 return POP3_RETR_SEND;
463 return POP3_LOGOUT_SEND;
464 } else if (ok == PS_PROTOCOL)
465 return POP3_LOGOUT_SEND;
470 gint pop3_delete_send(SockInfo *sock, gpointer data)
472 Pop3State *state = (Pop3State *)data;
474 /* inc_progress_update(state, POP3_DELETE_SEND); */
476 pop3_gen_send(sock, "DELE %d", state->cur_msg);
478 return POP3_DELETE_RECV;
481 gint pop3_delete_recv(SockInfo *sock, gpointer data)
483 Pop3State *state = (Pop3State *)data;
486 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
487 if (state->ac_prefs->session_type == RETR_NORMAL)
488 state->msg[state->cur_msg].deleted = TRUE;
490 if (pop3_delete_header(state) == TRUE)
491 return POP3_DELETE_SEND;
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_logout_send(SockInfo *sock, gpointer data)
507 Pop3State *state = (Pop3State *)data;
509 inc_progress_update(state, POP3_LOGOUT_SEND);
511 pop3_gen_send(sock, "QUIT");
513 return POP3_LOGOUT_RECV;
516 gint pop3_logout_recv(SockInfo *sock, gpointer data)
518 if (pop3_ok(sock, NULL) == PS_SUCCESS)
524 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
527 gchar buf[POPBUFSIZE + 1];
530 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
532 if (*bufp == '+' || *bufp == '-')
537 while (isalpha(*bufp))
543 if (!strcmp(buf, "+OK"))
545 else if (!strncmp(buf, "-ERR", 4)) {
546 if (strstr(bufp, "lock") ||
547 strstr(bufp, "Lock") ||
548 strstr(bufp, "LOCK") ||
549 strstr(bufp, "wait"))
555 fprintf(stderr, "POP3: %s\n", bufp);
560 strcpy(argbuf, bufp);
566 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
568 gchar buf[POPBUFSIZE + 1];
571 va_start(args, format);
572 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
575 if (!strncasecmp(buf, "PASS ", 5))
576 log_print("POP3> PASS ********\n");
578 log_print("POP3> %s\n", buf);
581 sock_write(sock, buf, strlen(buf));
584 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
586 if (sock_gets(sock, buf, size) < 0) {
590 log_print("POP3< %s\n", buf);
596 gboolean pop3_delete_header (Pop3State *state)
599 if ( (state->ac_prefs->session_type == DELE_HEADER) &&
600 (g_slist_length(state->ac_prefs->to_delete) > 0) ) {
602 state->cur_msg = (gint) state->ac_prefs->to_delete->data;
603 debug_print(_("next to delete %i\n"), state->cur_msg);
604 state->ac_prefs->to_delete = g_slist_remove
605 (state->ac_prefs->to_delete, state->ac_prefs->to_delete->data);