2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 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(gint sock, gchar *argbuf);
41 static void pop3_gen_send(gint sock, const gchar *format, ...);
42 static gint pop3_gen_recv(gint sock, gchar *buf, gint size);
44 gint pop3_greeting_recv(gint 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(gint 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(gint sock, gpointer data)
74 if (pop3_ok(sock, NULL) == PS_SUCCESS)
75 return POP3_GETAUTH_PASS_SEND;
80 gint pop3_getauth_pass_send(gint sock, gpointer data)
82 Pop3State *state = (Pop3State *)data;
84 g_return_val_if_fail(state->pass != NULL, -1);
86 pop3_gen_send(sock, "PASS %s", state->pass);
88 return POP3_GETAUTH_PASS_RECV;
91 gint pop3_getauth_pass_recv(gint sock, gpointer data)
93 Pop3State *state = (Pop3State *)data;
95 if (pop3_ok(sock, NULL) == PS_SUCCESS)
96 return POP3_GETRANGE_STAT_SEND;
98 log_warning(_("error occurred on authorization\n"));
99 state->error_val = PS_AUTHFAIL;
104 gint pop3_getauth_apop_send(gint sock, gpointer data)
106 Pop3State *state = (Pop3State *)data;
108 gchar *apop_str, *md5sum;
110 g_return_val_if_fail(state->user != NULL, -1);
111 g_return_val_if_fail(state->pass != NULL, -1);
113 inc_progress_update(state, POP3_GETAUTH_APOP_SEND);
115 if ((start = strchr(state->greeting, '<')) == NULL) {
116 log_warning(_("Required APOP timestamp not found "
121 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
122 log_warning(_("Timestamp syntax error in greeting\n"));
128 apop_str = g_strconcat(start, state->pass, NULL);
129 md5sum = MD5Digest(apop_str);
132 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
134 return POP3_GETAUTH_APOP_RECV;
137 gint pop3_getauth_apop_recv(gint sock, gpointer data)
139 Pop3State *state = (Pop3State *)data;
141 if (pop3_ok(sock, NULL) == PS_SUCCESS)
142 return POP3_GETRANGE_STAT_SEND;
144 log_warning(_("error occurred on authorization\n"));
145 state->error_val = PS_AUTHFAIL;
150 gint pop3_getrange_stat_send(gint sock, gpointer data)
152 Pop3State *state = (Pop3State *)data;
154 inc_progress_update(state, POP3_GETRANGE_STAT_SEND);
156 pop3_gen_send(sock, "STAT");
158 return POP3_GETRANGE_STAT_RECV;
161 gint pop3_getrange_stat_recv(gint sock, gpointer data)
163 Pop3State *state = (Pop3State *)data;
164 gchar buf[POPBUFSIZE + 1];
167 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
168 if (sscanf(buf, "%d %d", &state->count, &state->bytes) != 2) {
169 log_warning(_("POP3 protocol error\n"));
172 if (state->count == 0)
173 return POP3_LOGOUT_SEND;
176 state->new = state->count;
177 if (state->ac_prefs->rmmail ||
178 state->ac_prefs->getall)
179 return POP3_RETR_SEND;
181 return POP3_GETRANGE_UIDL_SEND;
184 } else if (ok == PS_PROTOCOL)
185 return POP3_LOGOUT_SEND;
190 gint pop3_getrange_last_send(gint sock, gpointer data)
192 pop3_gen_send(sock, "LAST");
194 return POP3_GETRANGE_LAST_RECV;
197 gint pop3_getrange_last_recv(gint sock, gpointer data)
199 Pop3State *state = (Pop3State *)data;
200 gchar buf[POPBUFSIZE + 1];
202 if (pop3_ok(sock, buf) == PS_SUCCESS) {
205 if (sscanf(buf, "%d", &last) == 0) {
206 log_warning(_("POP3 protocol error\n"));
209 if (state->count == last)
210 return POP3_LOGOUT_SEND;
212 state->new = state->count - last;
213 state->cur_msg = last + 1;
214 return POP3_RETR_SEND;
218 return POP3_RETR_SEND;
221 gint pop3_getrange_uidl_send(gint sock, gpointer data)
223 pop3_gen_send(sock, "UIDL");
225 return POP3_GETRANGE_UIDL_RECV;
228 gint pop3_getrange_uidl_recv(gint sock, gpointer data)
230 Pop3State *state = (Pop3State *)data;
232 gboolean new = FALSE;
233 gchar buf[POPBUFSIZE];
236 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
238 if (!state->id_table) new = TRUE;
240 nb = sock_is_nonblocking_mode(sock);
241 if (nb && (sock_set_nonblocking_mode(sock, FALSE) < 0)) return -1;
243 while (sock_read(sock, buf, sizeof(buf)) >= 0) {
246 if (buf[0] == '.') break;
247 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
251 g_hash_table_lookup(state->id_table, id) == NULL) {
252 state->new = state->count - num + 1;
253 state->cur_msg = num;
258 state->new_id_list = g_slist_append
259 (state->new_id_list, g_strdup(id));
261 state->id_list = g_slist_append
262 (state->id_list, g_strdup(id));
265 if (nb && (sock_set_nonblocking_mode(sock, TRUE) < 0)) return -1;
268 return POP3_RETR_SEND;
270 return POP3_LOGOUT_SEND;
273 gint pop3_retr_send(gint sock, gpointer data)
275 Pop3State *state = (Pop3State *)data;
277 inc_progress_update(state, POP3_RETR_SEND);
279 pop3_gen_send(sock, "RETR %d", state->cur_msg);
281 return POP3_RETR_RECV;
284 gint pop3_retr_recv(gint sock, gpointer data)
286 Pop3State *state = (Pop3State *)data;
290 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
291 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
292 state->inc_state = INC_NOSPACE;
295 if ((drop_ok = inc_drop_message(file, state)) < 0) {
296 state->inc_state = INC_ERROR;
299 if (drop_ok == 0 && state->ac_prefs->rmmail)
300 return POP3_DELETE_SEND;
302 if (state->new_id_list) {
303 state->id_list = g_slist_append
304 (state->id_list, state->new_id_list->data);
306 g_slist_remove(state->new_id_list,
307 state->new_id_list->data);
310 if (state->cur_msg < state->count) {
312 return POP3_RETR_SEND;
314 return POP3_LOGOUT_SEND;
315 } else if (ok == PS_PROTOCOL)
316 return POP3_LOGOUT_SEND;
321 gint pop3_delete_send(gint sock, gpointer data)
323 Pop3State *state = (Pop3State *)data;
325 //inc_progress_update(state, POP3_DELETE_SEND);
327 pop3_gen_send(sock, "DELE %d", state->cur_msg);
329 return POP3_DELETE_RECV;
332 gint pop3_delete_recv(gint sock, gpointer data)
334 Pop3State *state = (Pop3State *)data;
337 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
338 if (state->cur_msg < state->count) {
340 return POP3_RETR_SEND;
342 return POP3_LOGOUT_SEND;
343 } else if (ok == PS_PROTOCOL)
344 return POP3_LOGOUT_SEND;
349 gint pop3_logout_send(gint sock, gpointer data)
351 Pop3State *state = (Pop3State *)data;
353 inc_progress_update(state, POP3_LOGOUT_SEND);
355 pop3_gen_send(sock, "QUIT");
357 return POP3_LOGOUT_RECV;
360 gint pop3_logout_recv(gint sock, gpointer data)
362 if (pop3_ok(sock, NULL) == PS_SUCCESS)
368 static gint pop3_ok(gint sock, gchar *argbuf)
371 gchar buf[POPBUFSIZE + 1];
374 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
376 if (*bufp == '+' || *bufp == '-')
381 while (isalpha(*bufp))
387 if (!strcmp(buf, "+OK"))
389 else if (!strncmp(buf, "-ERR", 4)) {
390 if (strstr(bufp, "lock") ||
391 strstr(bufp, "Lock") ||
392 strstr(bufp, "LOCK") ||
393 strstr(bufp, "wait"))
399 fprintf(stderr, "POP3: %s\n", bufp);
404 strcpy(argbuf, bufp);
410 static void pop3_gen_send(gint sock, const gchar *format, ...)
412 gchar buf[POPBUFSIZE + 1];
415 va_start(args, format);
416 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
419 if (!strncasecmp(buf, "PASS ", 5))
420 log_print("POP3> PASS ********\n");
422 log_print("POP3> %s\n", buf);
425 sock_write(sock, buf, strlen(buf));
428 static gint pop3_gen_recv(gint sock, gchar *buf, gint size)
432 nb = sock_is_nonblocking_mode(sock);
434 if (nb && (sock_set_nonblocking_mode(sock, FALSE) < 0))
436 if (sock_read(sock, buf, size) < 0) {
440 log_print("POP3< %s\n", buf);
441 if (nb && (sock_set_nonblocking_mode(sock, TRUE) < 0))