2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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"
40 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
41 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
42 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
44 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
46 Pop3State *state = (Pop3State *)data;
47 gchar buf[POPBUFSIZE];
49 if (pop3_ok(sock, buf) == PS_SUCCESS) {
50 if (state->ac_prefs->protocol == A_APOP) {
51 state->greeting = g_strdup(buf);
52 return POP3_GETAUTH_APOP_SEND;
54 return POP3_GETAUTH_USER_SEND;
59 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
61 Pop3State *state = (Pop3State *)data;
63 g_return_val_if_fail(state->user != NULL, -1);
65 inc_progress_update(state, POP3_GETAUTH_USER_SEND);
67 pop3_gen_send(sock, "USER %s", state->user);
69 return POP3_GETAUTH_USER_RECV;
72 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
74 Pop3State *state = (Pop3State *)data;
76 if (pop3_ok(sock, NULL) == PS_SUCCESS)
77 return POP3_GETAUTH_PASS_SEND;
79 log_warning(_("error occurred on authentication\n"));
80 state->error_val = PS_AUTHFAIL;
81 state->inc_state = INC_AUTH_FAILED;
86 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
88 Pop3State *state = (Pop3State *)data;
90 g_return_val_if_fail(state->pass != NULL, -1);
92 pop3_gen_send(sock, "PASS %s", state->pass);
94 return POP3_GETAUTH_PASS_RECV;
97 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
99 Pop3State *state = (Pop3State *)data;
101 if (pop3_ok(sock, NULL) == PS_SUCCESS)
102 return POP3_GETRANGE_STAT_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_apop_send(SockInfo *sock, gpointer data)
113 Pop3State *state = (Pop3State *)data;
118 g_return_val_if_fail(state->user != NULL, -1);
119 g_return_val_if_fail(state->pass != NULL, -1);
121 inc_progress_update(state, POP3_GETAUTH_APOP_SEND);
123 if ((start = strchr(state->greeting, '<')) == NULL) {
124 log_warning(_("Required APOP timestamp not found "
129 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
130 log_warning(_("Timestamp syntax error in greeting\n"));
136 apop_str = g_strconcat(start, state->pass, NULL);
137 md5_hex_digest(md5sum, apop_str);
140 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
142 return POP3_GETAUTH_APOP_RECV;
145 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
147 Pop3State *state = (Pop3State *)data;
149 if (pop3_ok(sock, NULL) == PS_SUCCESS)
150 return POP3_GETRANGE_STAT_SEND;
152 log_warning(_("error occurred on authentication\n"));
153 state->error_val = PS_AUTHFAIL;
154 state->inc_state = INC_AUTH_FAILED;
159 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
161 Pop3State *state = (Pop3State *)data;
163 inc_progress_update(state, POP3_GETRANGE_STAT_SEND);
165 pop3_gen_send(sock, "STAT");
167 return POP3_GETRANGE_STAT_RECV;
170 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
172 Pop3State *state = (Pop3State *)data;
173 gchar buf[POPBUFSIZE + 1];
176 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
177 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
179 log_warning(_("POP3 protocol error\n"));
182 if (state->count == 0)
183 return POP3_LOGOUT_SEND;
186 state->new = state->count;
187 if (state->ac_prefs->rmmail ||
188 state->ac_prefs->getall)
189 return POP3_GETSIZE_LIST_SEND;
191 return POP3_GETRANGE_UIDL_SEND;
194 } else if (ok == PS_PROTOCOL)
195 return POP3_LOGOUT_SEND;
200 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
202 Pop3State *state = (Pop3State *)data;
204 inc_progress_update(state, POP3_GETRANGE_LAST_SEND);
206 pop3_gen_send(sock, "LAST");
208 return POP3_GETRANGE_LAST_RECV;
211 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
213 Pop3State *state = (Pop3State *)data;
214 gchar buf[POPBUFSIZE + 1];
216 if (pop3_ok(sock, buf) == PS_SUCCESS) {
219 if (sscanf(buf, "%d", &last) == 0) {
220 log_warning(_("POP3 protocol error\n"));
223 if (state->count == last)
224 return POP3_LOGOUT_SEND;
226 state->new = state->count - last;
227 state->cur_msg = last + 1;
228 return POP3_GETSIZE_LIST_SEND;
232 return POP3_GETSIZE_LIST_SEND;
235 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
237 Pop3State *state = (Pop3State *)data;
239 inc_progress_update(state, POP3_GETRANGE_UIDL_SEND);
241 pop3_gen_send(sock, "UIDL");
243 return POP3_GETRANGE_UIDL_RECV;
246 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
248 Pop3State *state = (Pop3State *)data;
249 gboolean new = FALSE;
250 gchar buf[POPBUFSIZE];
253 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
255 if (!state->id_table) new = TRUE;
257 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
260 if (buf[0] == '.') break;
261 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
265 g_hash_table_lookup(state->id_table, id) == NULL) {
266 state->new = state->count - num + 1;
267 state->cur_msg = num;
272 state->new_id_list = g_slist_append
273 (state->new_id_list, g_strdup(id));
275 state->id_list = g_slist_append
276 (state->id_list, g_strdup(id));
280 return POP3_GETSIZE_LIST_SEND;
282 return POP3_LOGOUT_SEND;
285 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
287 Pop3State *state = (Pop3State *)data;
289 inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
291 pop3_gen_send(sock, "LIST");
293 return POP3_GETSIZE_LIST_RECV;
296 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
298 Pop3State *state = (Pop3State *)data;
299 gchar buf[POPBUFSIZE];
301 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
303 state->sizes = g_new0(gint, state->count + 1);
304 state->cur_total_bytes = 0;
306 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
309 if (buf[0] == '.') break;
310 if (sscanf(buf, "%u %u", &num, &size) != 2)
313 if (num > 0 && num <= state->count)
314 state->sizes[num] = size;
315 if (num > 0 && num < state->cur_msg)
316 state->cur_total_bytes += size;
319 while (state->sizes[state->cur_msg] == 0) {
320 if (state->cur_msg == state->count)
321 return POP3_LOGOUT_SEND;
326 return POP3_RETR_SEND;
329 gint pop3_retr_send(SockInfo *sock, gpointer data)
331 Pop3State *state = (Pop3State *)data;
333 inc_progress_update(state, POP3_RETR_SEND);
335 pop3_gen_send(sock, "RETR %d", state->cur_msg);
337 return POP3_RETR_RECV;
340 gint pop3_retr_recv(SockInfo *sock, gpointer data)
342 Pop3State *state = (Pop3State *)data;
346 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
347 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
348 state->inc_state = INC_NOSPACE;
352 if ((drop_ok = inc_drop_message(file, state)) < 0) {
353 state->inc_state = INC_ERROR;
357 state->cur_total_bytes += state->sizes[state->cur_msg];
358 state->cur_total_num++;
360 if (drop_ok == 0 && state->ac_prefs->rmmail)
361 return POP3_DELETE_SEND;
363 if (state->new_id_list) {
364 state->id_list = g_slist_append
365 (state->id_list, state->new_id_list->data);
367 g_slist_remove(state->new_id_list,
368 state->new_id_list->data);
371 if (state->cur_msg < state->count) {
373 while (state->sizes[state->cur_msg] == 0) {
374 if (state->cur_msg == state->count)
375 return POP3_LOGOUT_SEND;
379 return POP3_RETR_SEND;
381 return POP3_LOGOUT_SEND;
382 } else if (ok == PS_PROTOCOL)
383 return POP3_LOGOUT_SEND;
388 gint pop3_delete_send(SockInfo *sock, gpointer data)
390 Pop3State *state = (Pop3State *)data;
392 /* inc_progress_update(state, POP3_DELETE_SEND); */
394 pop3_gen_send(sock, "DELE %d", state->cur_msg);
396 return POP3_DELETE_RECV;
399 gint pop3_delete_recv(SockInfo *sock, gpointer data)
401 Pop3State *state = (Pop3State *)data;
404 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
405 if (state->cur_msg < state->count) {
407 while (state->sizes[state->cur_msg] == 0) {
408 if (state->cur_msg == state->count)
409 return POP3_LOGOUT_SEND;
413 return POP3_RETR_SEND;
415 return POP3_LOGOUT_SEND;
416 } else if (ok == PS_PROTOCOL)
417 return POP3_LOGOUT_SEND;
422 gint pop3_logout_send(SockInfo *sock, gpointer data)
424 Pop3State *state = (Pop3State *)data;
426 inc_progress_update(state, POP3_LOGOUT_SEND);
428 pop3_gen_send(sock, "QUIT");
430 return POP3_LOGOUT_RECV;
433 gint pop3_logout_recv(SockInfo *sock, gpointer data)
435 if (pop3_ok(sock, NULL) == PS_SUCCESS)
441 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
444 gchar buf[POPBUFSIZE + 1];
447 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
449 if (*bufp == '+' || *bufp == '-')
454 while (isalpha(*bufp))
460 if (!strcmp(buf, "+OK"))
462 else if (!strncmp(buf, "-ERR", 4)) {
463 if (strstr(bufp, "lock") ||
464 strstr(bufp, "Lock") ||
465 strstr(bufp, "LOCK") ||
466 strstr(bufp, "wait"))
472 fprintf(stderr, "POP3: %s\n", bufp);
477 strcpy(argbuf, bufp);
483 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
485 gchar buf[POPBUFSIZE + 1];
488 va_start(args, format);
489 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
492 if (!strncasecmp(buf, "PASS ", 5))
493 log_print("POP3> PASS ********\n");
495 log_print("POP3> %s\n", buf);
498 sock_write(sock, buf, strlen(buf));
501 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
503 if (sock_gets(sock, buf, size) < 0) {
507 log_print("POP3< %s\n", buf);