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 Pop3State *state = (Pop3State *)data;
196 inc_progress_update(state, POP3_GETRANGE_LAST_SEND);
198 pop3_gen_send(sock, "LAST");
200 return POP3_GETRANGE_LAST_RECV;
203 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
205 Pop3State *state = (Pop3State *)data;
206 gchar buf[POPBUFSIZE + 1];
208 if (pop3_ok(sock, buf) == PS_SUCCESS) {
211 if (sscanf(buf, "%d", &last) == 0) {
212 log_warning(_("POP3 protocol error\n"));
215 if (state->count == last)
216 return POP3_LOGOUT_SEND;
218 state->new = state->count - last;
219 state->cur_msg = last + 1;
220 return POP3_GETSIZE_LIST_SEND;
224 return POP3_GETSIZE_LIST_SEND;
227 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
229 Pop3State *state = (Pop3State *)data;
231 inc_progress_update(state, POP3_GETRANGE_UIDL_SEND);
233 pop3_gen_send(sock, "UIDL");
235 return POP3_GETRANGE_UIDL_RECV;
238 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
240 Pop3State *state = (Pop3State *)data;
241 gboolean new = FALSE;
242 gchar buf[POPBUFSIZE];
245 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
247 if (!state->id_table) new = TRUE;
249 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
252 if (buf[0] == '.') break;
253 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
257 g_hash_table_lookup(state->id_table, id) == NULL) {
258 state->new = state->count - num + 1;
259 state->cur_msg = num;
264 state->new_id_list = g_slist_append
265 (state->new_id_list, g_strdup(id));
267 state->id_list = g_slist_append
268 (state->id_list, g_strdup(id));
272 return POP3_GETSIZE_LIST_SEND;
274 return POP3_LOGOUT_SEND;
277 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
279 Pop3State *state = (Pop3State *)data;
281 inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
283 pop3_gen_send(sock, "LIST");
285 return POP3_GETSIZE_LIST_RECV;
288 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
290 Pop3State *state = (Pop3State *)data;
291 gchar buf[POPBUFSIZE];
293 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
295 state->sizes = g_new0(gint, state->count + 1);
296 state->cur_total_bytes = 0;
298 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
301 if (buf[0] == '.') break;
302 if (sscanf(buf, "%d %d", &num, &size) != 2)
305 if (num <= state->count)
306 state->sizes[num] = size;
307 if (num < state->cur_msg)
308 state->cur_total_bytes += size;
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 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
333 state->inc_state = INC_NOSPACE;
337 state->cur_total_bytes += state->sizes[state->cur_msg];
339 if ((drop_ok = inc_drop_message(file, state)) < 0) {
340 state->inc_state = INC_ERROR;
343 if (drop_ok == 0 && state->ac_prefs->rmmail)
344 return POP3_DELETE_SEND;
346 if (state->new_id_list) {
347 state->id_list = g_slist_append
348 (state->id_list, state->new_id_list->data);
350 g_slist_remove(state->new_id_list,
351 state->new_id_list->data);
354 if (state->cur_msg < state->count) {
356 return POP3_RETR_SEND;
358 return POP3_LOGOUT_SEND;
359 } else if (ok == PS_PROTOCOL)
360 return POP3_LOGOUT_SEND;
365 gint pop3_delete_send(SockInfo *sock, gpointer data)
367 Pop3State *state = (Pop3State *)data;
369 //inc_progress_update(state, POP3_DELETE_SEND);
371 pop3_gen_send(sock, "DELE %d", state->cur_msg);
373 return POP3_DELETE_RECV;
376 gint pop3_delete_recv(SockInfo *sock, gpointer data)
378 Pop3State *state = (Pop3State *)data;
381 if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
382 if (state->cur_msg < state->count) {
384 return POP3_RETR_SEND;
386 return POP3_LOGOUT_SEND;
387 } else if (ok == PS_PROTOCOL)
388 return POP3_LOGOUT_SEND;
393 gint pop3_logout_send(SockInfo *sock, gpointer data)
395 Pop3State *state = (Pop3State *)data;
397 inc_progress_update(state, POP3_LOGOUT_SEND);
399 pop3_gen_send(sock, "QUIT");
401 return POP3_LOGOUT_RECV;
404 gint pop3_logout_recv(SockInfo *sock, gpointer data)
406 if (pop3_ok(sock, NULL) == PS_SUCCESS)
412 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
415 gchar buf[POPBUFSIZE + 1];
418 if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
420 if (*bufp == '+' || *bufp == '-')
425 while (isalpha(*bufp))
431 if (!strcmp(buf, "+OK"))
433 else if (!strncmp(buf, "-ERR", 4)) {
434 if (strstr(bufp, "lock") ||
435 strstr(bufp, "Lock") ||
436 strstr(bufp, "LOCK") ||
437 strstr(bufp, "wait"))
443 fprintf(stderr, "POP3: %s\n", bufp);
448 strcpy(argbuf, bufp);
454 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
456 gchar buf[POPBUFSIZE + 1];
459 va_start(args, format);
460 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
463 if (!strncasecmp(buf, "PASS ", 5))
464 log_print("POP3> PASS ********\n");
466 log_print("POP3> %s\n", buf);
469 sock_write(sock, buf, strlen(buf));
472 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
474 if (sock_gets(sock, buf, size) < 0) {
478 log_print("POP3< %s\n", buf);