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 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 authentication\n"));
99 state
->error_val
= PS_AUTHFAIL
;
100 state
->inc_state
= INC_AUTH_FAILED
;
105 gint
pop3_getauth_apop_send(SockInfo
*sock
, gpointer data
)
107 Pop3State
*state
= (Pop3State
*)data
;
112 g_return_val_if_fail(state
->user
!= NULL
, -1);
113 g_return_val_if_fail(state
->pass
!= NULL
, -1);
115 inc_progress_update(state
, POP3_GETAUTH_APOP_SEND
);
117 if ((start
= strchr(state
->greeting
, '<')) == NULL
) {
118 log_warning(_("Required APOP timestamp not found "
123 if ((end
= strchr(start
, '>')) == NULL
|| end
== start
+ 1) {
124 log_warning(_("Timestamp syntax error in greeting\n"));
130 apop_str
= g_strconcat(start
, state
->pass
, NULL
);
131 md5_hex_digest(md5sum
, apop_str
);
134 pop3_gen_send(sock
, "APOP %s %s", state
->user
, md5sum
);
136 return POP3_GETAUTH_APOP_RECV
;
139 gint
pop3_getauth_apop_recv(SockInfo
*sock
, gpointer data
)
141 Pop3State
*state
= (Pop3State
*)data
;
143 if (pop3_ok(sock
, NULL
) == PS_SUCCESS
)
144 return POP3_GETRANGE_STAT_SEND
;
146 log_warning(_("error occurred on authentication\n"));
147 state
->error_val
= PS_AUTHFAIL
;
148 state
->inc_state
= INC_AUTH_FAILED
;
153 gint
pop3_getrange_stat_send(SockInfo
*sock
, gpointer data
)
155 Pop3State
*state
= (Pop3State
*)data
;
157 inc_progress_update(state
, POP3_GETRANGE_STAT_SEND
);
159 pop3_gen_send(sock
, "STAT");
161 return POP3_GETRANGE_STAT_RECV
;
164 gint
pop3_getrange_stat_recv(SockInfo
*sock
, gpointer data
)
166 Pop3State
*state
= (Pop3State
*)data
;
167 gchar buf
[POPBUFSIZE
+ 1];
170 if ((ok
= pop3_ok(sock
, buf
)) == PS_SUCCESS
) {
171 if (sscanf(buf
, "%d %d", &state
->count
, &state
->total_bytes
)
173 log_warning(_("POP3 protocol error\n"));
176 if (state
->count
== 0)
177 return POP3_LOGOUT_SEND
;
180 state
->new = state
->count
;
181 if (state
->ac_prefs
->rmmail
||
182 state
->ac_prefs
->getall
)
183 return POP3_GETSIZE_LIST_SEND
;
185 return POP3_GETRANGE_UIDL_SEND
;
188 } else if (ok
== PS_PROTOCOL
)
189 return POP3_LOGOUT_SEND
;
194 gint
pop3_getrange_last_send(SockInfo
*sock
, gpointer data
)
196 Pop3State
*state
= (Pop3State
*)data
;
198 inc_progress_update(state
, POP3_GETRANGE_LAST_SEND
);
200 pop3_gen_send(sock
, "LAST");
202 return POP3_GETRANGE_LAST_RECV
;
205 gint
pop3_getrange_last_recv(SockInfo
*sock
, gpointer data
)
207 Pop3State
*state
= (Pop3State
*)data
;
208 gchar buf
[POPBUFSIZE
+ 1];
210 if (pop3_ok(sock
, buf
) == PS_SUCCESS
) {
213 if (sscanf(buf
, "%d", &last
) == 0) {
214 log_warning(_("POP3 protocol error\n"));
217 if (state
->count
== last
)
218 return POP3_LOGOUT_SEND
;
220 state
->new = state
->count
- last
;
221 state
->cur_msg
= last
+ 1;
222 return POP3_GETSIZE_LIST_SEND
;
226 return POP3_GETSIZE_LIST_SEND
;
229 gint
pop3_getrange_uidl_send(SockInfo
*sock
, gpointer data
)
231 Pop3State
*state
= (Pop3State
*)data
;
233 inc_progress_update(state
, POP3_GETRANGE_UIDL_SEND
);
235 pop3_gen_send(sock
, "UIDL");
237 return POP3_GETRANGE_UIDL_RECV
;
240 gint
pop3_getrange_uidl_recv(SockInfo
*sock
, gpointer data
)
242 Pop3State
*state
= (Pop3State
*)data
;
243 gboolean
new = FALSE
;
244 gchar buf
[POPBUFSIZE
];
247 if (pop3_ok(sock
, NULL
) != PS_SUCCESS
) return POP3_GETRANGE_LAST_SEND
;
249 if (!state
->id_table
) new = TRUE
;
251 while (sock_gets(sock
, buf
, sizeof(buf
)) >= 0) {
254 if (buf
[0] == '.') break;
255 if (sscanf(buf
, "%d %" Xstr(IDLEN
) "s", &num
, id
) != 2)
259 g_hash_table_lookup(state
->id_table
, id
) == NULL
) {
260 state
->new = state
->count
- num
+ 1;
261 state
->cur_msg
= num
;
266 state
->new_id_list
= g_slist_append
267 (state
->new_id_list
, g_strdup(id
));
269 state
->id_list
= g_slist_append
270 (state
->id_list
, g_strdup(id
));
274 return POP3_GETSIZE_LIST_SEND
;
276 return POP3_LOGOUT_SEND
;
279 gint
pop3_getsize_list_send(SockInfo
*sock
, gpointer data
)
281 Pop3State
*state
= (Pop3State
*)data
;
283 inc_progress_update(state
, POP3_GETSIZE_LIST_SEND
);
285 pop3_gen_send(sock
, "LIST");
287 return POP3_GETSIZE_LIST_RECV
;
290 gint
pop3_getsize_list_recv(SockInfo
*sock
, gpointer data
)
292 Pop3State
*state
= (Pop3State
*)data
;
293 gchar buf
[POPBUFSIZE
];
295 if (pop3_ok(sock
, NULL
) != PS_SUCCESS
) return POP3_LOGOUT_SEND
;
297 state
->sizes
= g_new0(gint
, state
->count
+ 1);
298 state
->cur_total_bytes
= 0;
300 while (sock_gets(sock
, buf
, sizeof(buf
)) >= 0) {
303 if (buf
[0] == '.') break;
304 if (sscanf(buf
, "%u %u", &num
, &size
) != 2)
307 if (num
> 0 && num
<= state
->count
)
308 state
->sizes
[num
] = size
;
309 if (num
> 0 && num
< state
->cur_msg
)
310 state
->cur_total_bytes
+= size
;
313 while (state
->sizes
[state
->cur_msg
] == 0) {
314 if (state
->cur_msg
== state
->count
)
315 return POP3_LOGOUT_SEND
;
320 return POP3_RETR_SEND
;
323 gint
pop3_retr_send(SockInfo
*sock
, gpointer data
)
325 Pop3State
*state
= (Pop3State
*)data
;
327 inc_progress_update(state
, POP3_RETR_SEND
);
329 pop3_gen_send(sock
, "RETR %d", state
->cur_msg
);
331 return POP3_RETR_RECV
;
334 gint
pop3_retr_recv(SockInfo
*sock
, gpointer data
)
336 Pop3State
*state
= (Pop3State
*)data
;
340 if ((ok
= pop3_ok(sock
, NULL
)) == PS_SUCCESS
) {
341 if (recv_write_to_file(sock
, (file
= get_tmp_file())) < 0) {
342 state
->inc_state
= INC_NOSPACE
;
346 if ((drop_ok
= inc_drop_message(file
, state
)) < 0) {
347 state
->inc_state
= INC_ERROR
;
351 state
->cur_total_bytes
+= state
->sizes
[state
->cur_msg
];
352 state
->cur_total_num
++;
354 if (drop_ok
== 0 && state
->ac_prefs
->rmmail
)
355 return POP3_DELETE_SEND
;
357 if (state
->new_id_list
) {
358 state
->id_list
= g_slist_append
359 (state
->id_list
, state
->new_id_list
->data
);
361 g_slist_remove(state
->new_id_list
,
362 state
->new_id_list
->data
);
365 if (state
->cur_msg
< state
->count
) {
367 while (state
->sizes
[state
->cur_msg
] == 0) {
368 if (state
->cur_msg
== state
->count
)
369 return POP3_LOGOUT_SEND
;
373 return POP3_RETR_SEND
;
375 return POP3_LOGOUT_SEND
;
376 } else if (ok
== PS_PROTOCOL
)
377 return POP3_LOGOUT_SEND
;
382 gint
pop3_delete_send(SockInfo
*sock
, gpointer data
)
384 Pop3State
*state
= (Pop3State
*)data
;
386 /*inc_progress_update(state, POP3_DELETE_SEND);*/
388 pop3_gen_send(sock
, "DELE %d", state
->cur_msg
);
390 return POP3_DELETE_RECV
;
393 gint
pop3_delete_recv(SockInfo
*sock
, gpointer data
)
395 Pop3State
*state
= (Pop3State
*)data
;
398 if ((ok
= pop3_ok(sock
, NULL
)) == PS_SUCCESS
) {
399 if (state
->cur_msg
< state
->count
) {
401 while (state
->sizes
[state
->cur_msg
] == 0) {
402 if (state
->cur_msg
== state
->count
)
403 return POP3_LOGOUT_SEND
;
407 return POP3_RETR_SEND
;
409 return POP3_LOGOUT_SEND
;
410 } else if (ok
== PS_PROTOCOL
)
411 return POP3_LOGOUT_SEND
;
416 gint
pop3_logout_send(SockInfo
*sock
, gpointer data
)
418 Pop3State
*state
= (Pop3State
*)data
;
420 inc_progress_update(state
, POP3_LOGOUT_SEND
);
422 pop3_gen_send(sock
, "QUIT");
424 return POP3_LOGOUT_RECV
;
427 gint
pop3_logout_recv(SockInfo
*sock
, gpointer data
)
429 if (pop3_ok(sock
, NULL
) == PS_SUCCESS
)
435 static gint
pop3_ok(SockInfo
*sock
, gchar
*argbuf
)
438 gchar buf
[POPBUFSIZE
+ 1];
441 if ((ok
= pop3_gen_recv(sock
, buf
, sizeof(buf
))) == PS_SUCCESS
) {
443 if (*bufp
== '+' || *bufp
== '-')
448 while (isalpha(*bufp
))
454 if (!strcmp(buf
, "+OK"))
456 else if (!strncmp(buf
, "-ERR", 4)) {
457 if (strstr(bufp
, "lock") ||
458 strstr(bufp
, "Lock") ||
459 strstr(bufp
, "LOCK") ||
460 strstr(bufp
, "wait"))
466 fprintf(stderr
, "POP3: %s\n", bufp
);
471 strcpy(argbuf
, bufp
);
477 static void pop3_gen_send(SockInfo
*sock
, const gchar
*format
, ...)
479 gchar buf
[POPBUFSIZE
+ 1];
482 va_start(args
, format
);
483 g_vsnprintf(buf
, sizeof(buf
) - 2, format
, args
);
486 if (!strncasecmp(buf
, "PASS ", 5))
487 log_print("POP3> PASS ********\n");
489 log_print("POP3> %s\n", buf
);
492 sock_write(sock
, buf
, strlen(buf
));
495 static gint
pop3_gen_recv(SockInfo
*sock
, gchar
*buf
, gint size
)
497 if (sock_gets(sock
, buf
, size
) < 0) {
501 log_print("POP3< %s\n", buf
);