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(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 if (pop3_ok(sock, NULL) == PS_SUCCESS)
75 return POP3_GETAUTH_PASS_SEND;
80 gint pop3_getauth_pass_send(SockInfo *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(SockInfo *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(SockInfo *sock, gpointer data)
106 Pop3State *state = (Pop3State *)data;
111 g_return_val_if_fail(state->user != NULL, -1);
112 g_return_val_if_fail(state->pass != NULL, -1);
114 inc_progress_update(state, POP3_GETAUTH_APOP_SEND);
116 if ((start = strchr(state->greeting, '<')) == NULL) {
117 log_warning(_("Required APOP timestamp not found "
122 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
123 log_warning(_("Timestamp syntax error in greeting\n"));
129 apop_str = g_strconcat(start, state->pass, NULL);
130 md5_hex_digest(md5sum, apop_str);
133 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
135 return POP3_GETAUTH_APOP_RECV;
138 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
140 Pop3State *state = (Pop3State *)data;
142 if (pop3_ok(sock, NULL) == PS_SUCCESS)
143 return POP3_GETRANGE_STAT_SEND;
145 log_warning(_("error occurred on authorization\n"));
146 state->error_val = PS_AUTHFAIL;
151 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
153 Pop3State *state = (Pop3State *)data;
155 inc_progress_update(state, POP3_GETRANGE_STAT_SEND);
157 pop3_gen_send(sock, "STAT");
159 return POP3_GETRANGE_STAT_RECV;
162 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
164 Pop3State *state = (Pop3State *)data;
165 gchar buf[POPBUFSIZE + 1];
168 if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
169 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
171 log_warning(_("POP3 protocol error\n"));
174 if (state->count == 0)
175 return POP3_LOGOUT_SEND;
178 state->new = state->count;
179 if (state->ac_prefs->rmmail ||
180 state->ac_prefs->getall)
181 return POP3_GETSIZE_LIST_SEND;
183 return POP3_GETRANGE_UIDL_SEND;
186 } else if (ok == PS_PROTOCOL)
187 return POP3_LOGOUT_SEND;
192 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
194 pop3_gen_send(sock, "LAST");
196 return POP3_GETRANGE_LAST_RECV;
199 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
201 Pop3State *state = (Pop3State *)data;
202 gchar buf[POPBUFSIZE + 1];
204 if (pop3_ok(sock, buf) == PS_SUCCESS) {
207 if (sscanf(buf, "%d", &last) == 0) {
208 log_warning(_("POP3 protocol error\n"));
211 if (state->count == last)
212 return POP3_LOGOUT_SEND;
214 state->new = state->count - last;
215 state->cur_msg = last + 1;
216 return POP3_GETSIZE_LIST_SEND;
220 return POP3_GETSIZE_LIST_SEND;
223 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
225 pop3_gen_send(sock, "UIDL");
227 return POP3_GETRANGE_UIDL_RECV;
230 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
232 Pop3State *state = (Pop3State *)data;
234 gboolean new = FALSE;
235 gchar buf[POPBUFSIZE];
238 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
240 if (!state->id_table) new = TRUE;
242 nb = sock_is_nonblocking_mode(sock);
243 if (nb && (sock_set_nonblocking_mode(sock, FALSE) < 0)) return -1;
245 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
248 if (buf[0] == '.') break;
249 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
253 g_hash_table_lookup(state->id_table, id) == NULL) {
254 state->new = state->count - num + 1;
255 state->cur_msg = num;
260 state->new_id_list = g_slist_append
261 (state->new_id_list, g_strdup(id));
263 state->id_list = g_slist_append
264 (state->id_list, g_strdup(id));
267 if (nb && (sock_set_nonblocking_mode(sock, TRUE) < 0)) return -1;
270 return POP3_GETSIZE_LIST_SEND;
272 return POP3_LOGOUT_SEND;
275 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
277 pop3_gen_send(sock, "LIST");
279 return POP3_GETSIZE_LIST_RECV;
282 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
284 Pop3State *state = (Pop3State *)data;
286 gchar buf[POPBUFSIZE];
288 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
290 state->sizes = g_new0(gint, state->count + 1);
291 state->cur_total_bytes = 0;
293 nb = sock_is_nonblocking_mode(sock);
294 if (nb && (sock_set_nonblocking_mode(sock, FALSE) < 0)) return -1;
296 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
299 if (buf[0] == '.') break;
300 if (sscanf(buf, "%d %d", &num, &size) != 2)
303 if (num <= state->count)
304 state->sizes[num] = size;
305 if (num < state->cur_msg)
306 state->cur_total_bytes += size;
309 if (nb && (sock_set_nonblocking_mode(sock, TRUE) < 0)) return -1;
311 return POP3_RETR_SEND;
314 gint pop3_retr_send(SockInfo *sock, gpointer data)
316 Pop3State *state = (Pop3State *)data;
318 inc_progress_update(state, POP3_RETR_SEND);
320 pop3_gen_send(sock, "RETR %d", state->cur_msg);
322 return POP3_RETR_RECV;
325 gint pop3_retr_recv(SockInfo *sock, gpointer data)
327 Pop3State *state = (Pop3State *)data;
331 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
332 state->cur_msg_bytes = 0;
334 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
335 state->inc_state = INC_NOSPACE;
339 state->cur_total_bytes += state->sizes[state->cur_msg];
341 if ((drop_ok = inc_drop_message(file, state)) < 0) {
342 state->inc_state = INC_ERROR;
345 if (drop_ok == 0 && state->ac_prefs->rmmail)
346 return POP3_DELETE_SEND;
348 if (state->new_id_list) {
349 state->id_list = g_slist_append
350 (state->id_list, state->new_id_list->data);
352 g_slist_remove(state->new_id_list,
353 state->new_id_list->data);
356 if (state->cur_msg < state->count) {
358 return POP3_RETR_SEND;
360 return POP3_LOGOUT_SEND;
361 } else if (ok == PS_PROTOCOL)
362 return POP3_LOGOUT_SEND;
367 gint pop3_delete_send(SockInfo *sock, gpointer data)
369 Pop3State *state = (Pop3State *)data;
371 //inc_progress_update(state, POP3_DELETE_SEND);
373 pop3_gen_send(sock, "DELE %d", state->cur_msg);
375 return POP3_DELETE_RECV;
378 gint pop3_delete_recv(SockInfo *sock, gpointer data)
380 Pop3State *state = (Pop3State *)data;
383 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
384 if (state->cur_msg < state->count) {
386 return POP3_RETR_SEND;
388 return POP3_LOGOUT_SEND;
389 } else if (ok == PS_PROTOCOL)
390 return POP3_LOGOUT_SEND;
395 gint pop3_logout_send(SockInfo *sock, gpointer data)
397 Pop3State *state = (Pop3State *)data;
399 inc_progress_update(state, POP3_LOGOUT_SEND);
401 pop3_gen_send(sock, "QUIT");
403 return POP3_LOGOUT_RECV;
406 gint pop3_logout_recv(SockInfo *sock, gpointer data)
408 if (pop3_ok(sock, NULL) == PS_SUCCESS)
414 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
417 gchar buf[POPBUFSIZE + 1];
420 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
422 if (*bufp == '+' || *bufp == '-')
427 while (isalpha(*bufp))
433 if (!strcmp(buf, "+OK"))
435 else if (!strncmp(buf, "-ERR", 4)) {
436 if (strstr(bufp, "lock") ||
437 strstr(bufp, "Lock") ||
438 strstr(bufp, "LOCK") ||
439 strstr(bufp, "wait"))
445 fprintf(stderr, "POP3: %s\n", bufp);
450 strcpy(argbuf, bufp);
456 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
458 gchar buf[POPBUFSIZE + 1];
461 va_start(args, format);
462 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
465 if (!strncasecmp(buf, "PASS ", 5))
466 log_print("POP3> PASS ********\n");
468 log_print("POP3> %s\n", buf);
471 sock_write(sock, buf, strlen(buf));
474 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
478 nb = sock_is_nonblocking_mode(sock);
480 if (nb && (sock_set_nonblocking_mode(sock, FALSE) < 0))
482 if (sock_gets(sock, buf, size) < 0) {
486 log_print("POP3< %s\n", buf);
487 if (nb && (sock_set_nonblocking_mode(sock, TRUE) < 0))