2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
25 #include <glib/gi18n.h>
36 #include "prefs_account.h"
39 #include "partial_download.h"
43 static gint
pop3_greeting_recv (Pop3Session
*session
,
45 static gint
pop3_getauth_user_send (Pop3Session
*session
);
46 static gint
pop3_getauth_pass_send (Pop3Session
*session
);
47 static gint
pop3_getauth_apop_send (Pop3Session
*session
);
49 static gint
pop3_stls_send (Pop3Session
*session
);
50 static gint
pop3_stls_recv (Pop3Session
*session
);
52 static gint
pop3_getrange_stat_send (Pop3Session
*session
);
53 static gint
pop3_getrange_stat_recv (Pop3Session
*session
,
55 static gint
pop3_getrange_last_send (Pop3Session
*session
);
56 static gint
pop3_getrange_last_recv (Pop3Session
*session
,
58 static gint
pop3_getrange_uidl_send (Pop3Session
*session
);
59 static gint
pop3_getrange_uidl_recv (Pop3Session
*session
,
62 static gint
pop3_getsize_list_send (Pop3Session
*session
);
63 static gint
pop3_getsize_list_recv (Pop3Session
*session
,
66 static gint
pop3_retr_send (Pop3Session
*session
);
67 static gint
pop3_retr_recv (Pop3Session
*session
,
70 static gint
pop3_delete_send (Pop3Session
*session
);
71 static gint
pop3_delete_recv (Pop3Session
*session
);
72 static gint
pop3_logout_send (Pop3Session
*session
);
74 static void pop3_gen_send (Pop3Session
*session
,
75 const gchar
*format
, ...);
77 static void pop3_session_destroy (Session
*session
);
79 static gint
pop3_write_msg_to_file (const gchar
*file
,
84 static Pop3State
pop3_lookup_next (Pop3Session
*session
);
85 static Pop3ErrorValue
pop3_ok (Pop3Session
*session
,
88 static gint
pop3_session_recv_msg (Session
*session
,
90 static gint
pop3_session_recv_data_finished (Session
*session
,
93 static void pop3_get_uidl_table(PrefsAccount
*ac_prefs
, Pop3Session
*session
);
95 static gint
pop3_greeting_recv(Pop3Session
*session
, const gchar
*msg
)
97 session
->state
= POP3_GREETING
;
99 session
->greeting
= g_strdup(msg
);
104 static gint
pop3_stls_send(Pop3Session
*session
)
106 session
->state
= POP3_STLS
;
107 pop3_gen_send(session
, "STLS");
111 static gint
pop3_stls_recv(Pop3Session
*session
)
113 if (session_start_tls(SESSION(session
)) < 0) {
114 session
->error_val
= PS_SOCKET
;
119 #endif /* USE_GNUTLS */
121 static gint
pop3_getauth_user_send(Pop3Session
*session
)
123 cm_return_val_if_fail(session
->user
!= NULL
, -1);
125 session
->state
= POP3_GETAUTH_USER
;
126 pop3_gen_send(session
, "USER %s", session
->user
);
130 static gint
pop3_getauth_pass_send(Pop3Session
*session
)
132 cm_return_val_if_fail(session
->pass
!= NULL
, -1);
134 session
->state
= POP3_GETAUTH_PASS
;
135 pop3_gen_send(session
, "PASS %s", session
->pass
);
139 static gint
pop3_getauth_apop_send(Pop3Session
*session
)
145 cm_return_val_if_fail(session
->user
!= NULL
, -1);
146 cm_return_val_if_fail(session
->pass
!= NULL
, -1);
148 session
->state
= POP3_GETAUTH_APOP
;
150 if ((start
= strchr(session
->greeting
, '<')) == NULL
) {
151 log_error(LOG_PROTOCOL
, _("Required APOP timestamp not found "
153 session
->error_val
= PS_PROTOCOL
;
157 if ((end
= strchr(start
, '>')) == NULL
|| end
== start
+ 1) {
158 log_error(LOG_PROTOCOL
, _("Timestamp syntax error in greeting\n"));
159 session
->error_val
= PS_PROTOCOL
;
164 if (!is_ascii_str(start
)) {
165 log_error(LOG_PROTOCOL
, _("Timestamp syntax error in greeting (not ASCII)\n"));
166 session
->error_val
= PS_PROTOCOL
;
170 apop_str
= g_strconcat(start
, session
->pass
, NULL
);
171 md5_hex_digest(md5sum
, apop_str
);
174 pop3_gen_send(session
, "APOP %s %s", session
->user
, md5sum
);
179 static gint
pop3_getrange_stat_send(Pop3Session
*session
)
181 session
->state
= POP3_GETRANGE_STAT
;
182 pop3_gen_send(session
, "STAT");
186 static gint
pop3_getrange_stat_recv(Pop3Session
*session
, const gchar
*msg
)
188 if (sscanf(msg
, "%d %d", &session
->count
, &session
->total_bytes
) != 2) {
189 log_error(LOG_PROTOCOL
, _("POP3 protocol error\n"));
190 session
->error_val
= PS_PROTOCOL
;
193 if (session
->count
== 0) {
194 session
->uidl_is_valid
= TRUE
;
196 session
->msg
= g_new0(Pop3MsgInfo
, session
->count
+ 1);
197 session
->cur_msg
= 1;
204 static gint
pop3_getrange_last_send(Pop3Session
*session
)
206 session
->state
= POP3_GETRANGE_LAST
;
207 pop3_gen_send(session
, "LAST");
211 static gint
pop3_getrange_last_recv(Pop3Session
*session
, const gchar
*msg
)
215 if (sscanf(msg
, "%d", &last
) == 0) {
216 log_warning(LOG_PROTOCOL
, _("POP3 protocol error\n"));
217 session
->error_val
= PS_PROTOCOL
;
220 if (session
->count
> last
) {
221 session
->new_msg_exist
= TRUE
;
222 session
->cur_msg
= last
+ 1;
224 session
->cur_msg
= 0;
230 static gint
pop3_getrange_uidl_send(Pop3Session
*session
)
232 session
->state
= POP3_GETRANGE_UIDL
;
233 pop3_gen_send(session
, "UIDL");
237 static gint
pop3_getrange_uidl_recv(Pop3Session
*session
, const gchar
*data
,
241 gchar buf
[POPBUFSIZE
];
246 const gchar
*p
= data
;
247 const gchar
*lastp
= data
+ len
;
248 const gchar
*newline
;
251 if ((newline
= memchr(p
, '\r', lastp
- p
)) == NULL
)
253 buf_len
= MIN(newline
- p
, sizeof(buf
) - 1);
254 memcpy(buf
, p
, buf_len
);
258 if (p
< lastp
&& *p
== '\n') p
++;
260 if (sscanf(buf
, "%d %" Xstr(IDLEN
) "s", &num
, id
) != 2 ||
261 num
<= 0 || num
> session
->count
) {
262 log_warning(LOG_PROTOCOL
, _("invalid UIDL response: %s\n"), buf
);
266 session
->msg
[num
].uidl
= g_strdup(id
);
268 recv_time
= (time_t)(GPOINTER_TO_INT(g_hash_table_lookup(
269 session
->uidl_table
, id
)));
270 session
->msg
[num
].recv_time
= recv_time
;
272 if (recv_time
!= RECV_TIME_NONE
) {
273 debug_print("num %d uidl %s: already got it\n", num
, id
);
275 debug_print("num %d uidl %s: unknown\n", num
, id
);
278 partial_recv
= (gint
)(GPOINTER_TO_INT(g_hash_table_lookup(
279 session
->partial_recv_table
, id
)));
281 if (recv_time
!= RECV_TIME_NONE
282 || partial_recv
!= POP3_TOTALLY_RECEIVED
) {
283 session
->msg
[num
].received
=
284 (partial_recv
!= POP3_MUST_COMPLETE_RECV
);
285 session
->msg
[num
].partial_recv
= partial_recv
;
286 if (partial_recv
== POP3_MUST_COMPLETE_RECV
)
287 session
->new_msg_exist
= TRUE
;
289 if (!session
->new_msg_exist
&&
290 (recv_time
== RECV_TIME_NONE
||
291 session
->ac_prefs
->rmmail
)) {
292 session
->cur_msg
= num
;
293 session
->new_msg_exist
= TRUE
;
297 session
->uidl_is_valid
= TRUE
;
301 static gint
pop3_getsize_list_send(Pop3Session
*session
)
303 session
->state
= POP3_GETSIZE_LIST
;
304 pop3_gen_send(session
, "LIST");
308 static gint
pop3_getsize_list_recv(Pop3Session
*session
, const gchar
*data
,
311 gchar buf
[POPBUFSIZE
];
314 const gchar
*p
= data
;
315 const gchar
*lastp
= data
+ len
;
316 const gchar
*newline
;
319 if ((newline
= memchr(p
, '\r', lastp
- p
)) == NULL
)
321 buf_len
= MIN(newline
- p
, sizeof(buf
) - 1);
322 memcpy(buf
, p
, buf_len
);
326 if (p
< lastp
&& *p
== '\n') p
++;
328 if (sscanf(buf
, "%u %u", &num
, &size
) != 2) {
329 session
->error_val
= PS_PROTOCOL
;
333 if (num
> 0 && num
<= session
->count
)
334 session
->msg
[num
].size
= size
;
335 if (num
> 0 && num
< session
->cur_msg
)
336 session
->cur_total_bytes
+= size
;
342 static gint
pop3_retr_send(Pop3Session
*session
)
344 session
->state
= POP3_RETR
;
345 debug_print("retrieving %d [%s]\n", session
->cur_msg
,
346 session
->msg
[session
->cur_msg
].uidl
?
347 session
->msg
[session
->cur_msg
].uidl
:" ");
348 pop3_gen_send(session
, "RETR %d", session
->cur_msg
);
352 static gint
pop3_retr_recv(Pop3Session
*session
, const gchar
*data
, guint len
)
356 MailReceiveData mail_receive_data
;
358 /* NOTE: we allocate a slightly larger buffer with a zero terminator
359 * because some plugins may think that it has a C string. */
360 mail_receive_data
.session
= session
;
361 mail_receive_data
.data
= g_new0(gchar
, len
+ 1);
362 mail_receive_data
.data_len
= len
;
363 memcpy(mail_receive_data
.data
, data
, len
);
365 hooks_invoke(MAIL_RECEIVE_HOOKLIST
, &mail_receive_data
);
367 file
= get_tmp_file();
368 if (pop3_write_msg_to_file(file
, mail_receive_data
.data
,
369 mail_receive_data
.data_len
, NULL
) < 0) {
371 g_free(mail_receive_data
.data
);
372 session
->error_val
= PS_IOERR
;
375 g_free(mail_receive_data
.data
);
377 if (session
->msg
[session
->cur_msg
].partial_recv
378 == POP3_MUST_COMPLETE_RECV
) {
379 gchar
*old_file
= partial_get_filename(
380 session
->ac_prefs
->recv_server
,
381 session
->ac_prefs
->userid
,
382 session
->msg
[session
->cur_msg
].uidl
);
385 partial_delete_old(old_file
);
390 /* drop_ok: 0: success 1: don't receive -1: error */
391 drop_ok
= session
->drop_message(session
, file
);
395 session
->error_val
= PS_IOERR
;
399 session
->cur_total_bytes
+= session
->msg
[session
->cur_msg
].size
;
400 session
->cur_total_recv_bytes
+= session
->msg
[session
->cur_msg
].size
;
401 session
->cur_total_num
++;
403 session
->msg
[session
->cur_msg
].received
= TRUE
;
404 session
->msg
[session
->cur_msg
].partial_recv
= POP3_TOTALLY_RECEIVED
;
406 session
->msg
[session
->cur_msg
].recv_time
=
407 drop_ok
== 1 ? RECV_TIME_KEEP
: session
->current_time
;
412 static gint
pop3_top_send(Pop3Session
*session
, gint max_size
)
414 gint num_lines
= (max_size
*1024)/82; /* consider lines to be 80 chars */
415 session
->state
= POP3_TOP
;
416 pop3_gen_send(session
, "TOP %d %d", session
->cur_msg
, num_lines
);
420 static gint
pop3_top_recv(Pop3Session
*session
, const gchar
*data
, guint len
)
424 MailReceiveData mail_receive_data
;
425 gchar
*partial_notice
= NULL
;
427 /* NOTE: we allocate a slightly larger buffer with a zero terminator
428 * because some plugins may think that it has a C string. */
429 mail_receive_data
.session
= session
;
430 mail_receive_data
.data
= g_new0(gchar
, len
+ 1);
431 mail_receive_data
.data_len
= len
;
432 memcpy(mail_receive_data
.data
, data
, len
);
434 hooks_invoke(MAIL_RECEIVE_HOOKLIST
, &mail_receive_data
);
436 partial_notice
= g_strdup_printf("SC-Marked-For-Download: 0\n"
437 "SC-Partially-Retrieved: %s\n"
438 "SC-Account-Server: %s\n"
439 "SC-Account-Login: %s\n"
440 "SC-Message-Size: %d",
441 session
->msg
[session
->cur_msg
].uidl
,
442 session
->ac_prefs
->recv_server
,
443 session
->ac_prefs
->userid
,
444 session
->msg
[session
->cur_msg
].size
);
445 file
= get_tmp_file();
446 if (pop3_write_msg_to_file(file
, mail_receive_data
.data
,
447 mail_receive_data
.data_len
,
448 partial_notice
) < 0) {
450 g_free(mail_receive_data
.data
);
451 session
->error_val
= PS_IOERR
;
452 g_free(partial_notice
);
455 g_free(mail_receive_data
.data
);
456 g_free(partial_notice
);
458 /* drop_ok: 0: success 1: don't receive -1: error */
459 drop_ok
= session
->drop_message(session
, file
);
462 session
->error_val
= PS_IOERR
;
466 session
->cur_total_bytes
+= session
->msg
[session
->cur_msg
].size
;
467 session
->cur_total_recv_bytes
+= session
->msg
[session
->cur_msg
].size
;
468 session
->cur_total_num
++;
470 session
->msg
[session
->cur_msg
].received
= TRUE
;
471 session
->msg
[session
->cur_msg
].partial_recv
= POP3_PARTIALLY_RECEIVED
;
472 session
->msg
[session
->cur_msg
].recv_time
=
473 drop_ok
== 1 ? RECV_TIME_KEEP
: session
->current_time
;
478 static gint
pop3_delete_send(Pop3Session
*session
)
480 session
->state
= POP3_DELETE
;
481 pop3_gen_send(session
, "DELE %d", session
->cur_msg
);
485 static gint
pop3_delete_recv(Pop3Session
*session
)
487 session
->msg
[session
->cur_msg
].deleted
= TRUE
;
491 static gint
pop3_logout_send(Pop3Session
*session
)
493 session
->state
= POP3_LOGOUT
;
494 pop3_gen_send(session
, "QUIT");
498 static void pop3_gen_send(Pop3Session
*session
, const gchar
*format
, ...)
500 gchar buf
[POPBUFSIZE
+ 1];
503 va_start(args
, format
);
504 g_vsnprintf(buf
, sizeof(buf
) - 2, format
, args
);
507 if (!g_ascii_strncasecmp(buf
, "PASS ", 5))
508 log_print(LOG_PROTOCOL
, "POP3> PASS ********\n");
510 log_print(LOG_PROTOCOL
, "POP3> %s\n", buf
);
512 session_send_msg(SESSION(session
), SESSION_MSG_NORMAL
, buf
);
515 Session
*pop3_session_new(PrefsAccount
*account
)
517 Pop3Session
*session
;
519 cm_return_val_if_fail(account
!= NULL
, NULL
);
521 session
= g_new0(Pop3Session
, 1);
523 session_init(SESSION(session
), account
, FALSE
);
525 SESSION(session
)->type
= SESSION_POP3
;
527 SESSION(session
)->recv_msg
= pop3_session_recv_msg
;
528 SESSION(session
)->recv_data_finished
= pop3_session_recv_data_finished
;
529 SESSION(session
)->send_data_finished
= NULL
;
531 SESSION(session
)->destroy
= pop3_session_destroy
;
533 session
->state
= POP3_READY
;
534 session
->ac_prefs
= account
;
535 session
->pop_before_smtp
= FALSE
;
536 pop3_get_uidl_table(account
, session
);
537 session
->current_time
= time(NULL
);
538 session
->error_val
= PS_SUCCESS
;
539 session
->error_msg
= NULL
;
541 return SESSION(session
);
544 static void pop3_session_destroy(Session
*session
)
546 Pop3Session
*pop3_session
= POP3_SESSION(session
);
549 cm_return_if_fail(session
!= NULL
);
551 for (n
= 1; n
<= pop3_session
->count
; n
++)
552 g_free(pop3_session
->msg
[n
].uidl
);
553 g_free(pop3_session
->msg
);
555 if (pop3_session
->uidl_table
) {
556 hash_free_strings(pop3_session
->uidl_table
);
557 g_hash_table_destroy(pop3_session
->uidl_table
);
560 if (pop3_session
->partial_recv_table
) {
561 hash_free_strings(pop3_session
->partial_recv_table
);
562 g_hash_table_destroy(pop3_session
->partial_recv_table
);
565 g_free(pop3_session
->greeting
);
566 g_free(pop3_session
->user
);
567 g_free(pop3_session
->pass
);
568 g_free(pop3_session
->error_msg
);
571 static void pop3_get_uidl_table(PrefsAccount
*ac_prefs
, Pop3Session
*session
)
574 GHashTable
*partial_recv_table
;
577 gchar buf
[POPBUFSIZE
];
578 gchar uidl
[POPBUFSIZE
];
582 gchar
*sanitized_uid
= g_strdup(ac_prefs
->userid
);
584 subst_for_filename(sanitized_uid
);
586 table
= g_hash_table_new(g_str_hash
, g_str_equal
);
587 partial_recv_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
589 path
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
590 "uidl", G_DIR_SEPARATOR_S
, ac_prefs
->recv_server
,
591 "-", sanitized_uid
, NULL
);
593 g_free(sanitized_uid
);
594 if ((fp
= g_fopen(path
, "rb")) == NULL
) {
595 if (ENOENT
!= errno
) FILE_OP_ERROR(path
, "fopen");
597 path
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
598 "uidl-", ac_prefs
->recv_server
,
599 "-", ac_prefs
->userid
, NULL
);
600 if ((fp
= g_fopen(path
, "rb")) == NULL
) {
601 if (ENOENT
!= errno
) FILE_OP_ERROR(path
, "fopen");
603 session
->uidl_table
= table
;
604 session
->partial_recv_table
= partial_recv_table
;
612 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
613 gchar tmp
[POPBUFSIZE
];
615 recv_time
= RECV_TIME_NONE
;
616 partial_recv
= POP3_TOTALLY_RECEIVED
;
618 if (sscanf(buf
, "%s\t%ld\t%s", uidl
, (long int *) &recv_time
, tmp
) < 3) {
619 if (sscanf(buf
, "%s\t%ld", uidl
, (long int *) &recv_time
) != 2) {
620 if (sscanf(buf
, "%s", uidl
) != 1)
631 if (recv_time
== RECV_TIME_NONE
)
632 recv_time
= RECV_TIME_RECEIVED
;
633 g_hash_table_insert(table
, g_strdup(uidl
),
634 GINT_TO_POINTER(recv_time
));
635 if (strlen(tmp
) == 1)
636 partial_recv
= atoi(tmp
); /* totally received ?*/
638 partial_recv
= POP3_MUST_COMPLETE_RECV
;
640 g_hash_table_insert(partial_recv_table
, g_strdup(uidl
),
641 GINT_TO_POINTER(partial_recv
));
645 session
->uidl_table
= table
;
646 session
->partial_recv_table
= partial_recv_table
;
654 g_warning("failed to write\n"); \
658 gint pop3_write_uidl_list(Pop3Session *session)
660 gchar
*path
, *tmp_path
;
664 gchar
*sanitized_uid
= g_strdup(session
->ac_prefs
->userid
);
666 subst_for_filename(sanitized_uid
);
669 if (!session
->uidl_is_valid
) return 0;
671 path
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
672 "uidl", G_DIR_SEPARATOR_S
,
673 session
->ac_prefs
->recv_server
,
674 "-", sanitized_uid
, NULL
);
675 tmp_path
= g_strconcat(path
, ".tmp", NULL
);
677 g_free(sanitized_uid
);
679 if ((fp
= g_fopen(tmp_path
, "wb")) == NULL
) {
680 FILE_OP_ERROR(tmp_path
, "fopen");
684 for (n
= 1; n
<= session
->count
; n
++) {
685 msg
= &session
->msg
[n
];
686 if (msg
->uidl
&& msg
->received
&&
687 (!msg
->deleted
|| session
->state
!= POP3_DONE
))
688 TRY(fprintf(fp
, "%s\t%ld\t%d\n",
689 msg
->uidl
, (long int)
695 if (fclose(fp
) == EOF
) {
696 FILE_OP_ERROR(tmp_path
, "fclose");
704 if (g_rename(tmp_path
, path
) < 0) {
705 FILE_OP_ERROR(path
, "rename");
721 static gint
pop3_write_msg_to_file(const gchar
*file
, const gchar
*data
,
722 guint len
, const gchar
*prefix
)
725 const gchar
*prev
, *cur
;
727 cm_return_val_if_fail(file
!= NULL
, -1);
729 if ((fp
= g_fopen(file
, "wb")) == NULL
) {
730 FILE_OP_ERROR(file
, "fopen");
734 if (change_file_mode_rw(fp
, file
) < 0)
735 FILE_OP_ERROR(file
, "chmod");
737 if (prefix
!= NULL
) {
738 if (fprintf(fp
, "%s\n", prefix
) < 0) {
739 FILE_OP_ERROR(file
, "fprintf");
746 /* +------------------+----------------+--------------------------+ *
747 * ^data ^prev ^cur data+len-1^ */
750 while ((cur
= (gchar
*)my_memmem(prev
, len
- (prev
- data
), "\r\n", 2))
752 if ((cur
> prev
&& fwrite(prev
, 1, cur
- prev
, fp
) < 1) ||
753 fputc('\n', fp
) == EOF
) {
754 FILE_OP_ERROR(file
, "fwrite");
755 g_warning("can't write to file: %s\n", file
);
761 if (cur
== data
+ len
- 1) {
766 if (*(cur
+ 1) == '\n')
771 if (prev
- data
< len
- 1 && *prev
== '.' && *(prev
+ 1) == '.')
774 if (prev
- data
>= len
)
778 if (prev
- data
< len
&&
779 fwrite(prev
, 1, len
- (prev
- data
), fp
) < 1) {
780 FILE_OP_ERROR(file
, "fwrite");
781 g_warning("can't write to file: %s\n", file
);
786 if (data
[len
- 1] != '\r' && data
[len
- 1] != '\n') {
787 if (fputc('\n', fp
) == EOF
) {
788 FILE_OP_ERROR(file
, "fputc");
789 g_warning("can't write to file: %s\n", file
);
796 if (fclose(fp
) == EOF
) {
797 FILE_OP_ERROR(file
, "fclose");
805 static Pop3State
pop3_lookup_next(Pop3Session
*session
)
808 PrefsAccount
*ac
= session
->ac_prefs
;
810 gboolean size_limit_over
;
813 msg
= &session
->msg
[session
->cur_msg
];
816 (ac
->enable_size_limit
&&
817 ac
->size_limit
> 0 &&
818 size
> ac
->size_limit
* 1024);
821 msg
->recv_time
!= RECV_TIME_NONE
&&
822 msg
->recv_time
!= RECV_TIME_KEEP
&&
823 msg
->partial_recv
== POP3_TOTALLY_RECEIVED
&&
824 session
->current_time
- msg
->recv_time
>=
825 ((ac
->msg_leave_time
* 24 * 60 * 60) +
826 (ac
->msg_leave_hour
* 60 * 60))) {
827 log_message(LOG_PROTOCOL
,
828 _("POP3: Deleting expired message %d [%s]\n"),
829 session
->cur_msg
, msg
->uidl
?msg
->uidl
:" ");
830 session
->cur_total_bytes
+= size
;
831 pop3_delete_send(session
);
835 if (size_limit_over
) {
836 if (!msg
->received
&& msg
->partial_recv
!=
837 POP3_MUST_COMPLETE_RECV
) {
838 pop3_top_send(session
, ac
->size_limit
);
840 } else if (msg
->partial_recv
== POP3_MUST_COMPLETE_RECV
)
843 log_message(LOG_PROTOCOL
,
844 _("POP3: Skipping message %d [%s] (%d bytes)\n"),
845 session
->cur_msg
, msg
->uidl
?msg
->uidl
:" ", size
);
848 if (size
== 0 || msg
->received
|| size_limit_over
) {
849 session
->cur_total_bytes
+= size
;
850 if (session
->cur_msg
== session
->count
) {
851 pop3_logout_send(session
);
859 pop3_retr_send(session
);
863 static Pop3ErrorValue
pop3_ok(Pop3Session
*session
, const gchar
*msg
)
867 log_print(LOG_PROTOCOL
, "POP3< %s\n", msg
);
869 if (!strncmp(msg
, "+OK", 3))
871 else if (!strncmp(msg
, "-ERR", 4)) {
872 if (strstr(msg
+ 4, "lock") ||
873 strstr(msg
+ 4, "Lock") ||
874 strstr(msg
+ 4, "LOCK") ||
875 strstr(msg
+ 4, "wait")) {
876 log_error(LOG_PROTOCOL
, _("mailbox is locked\n"));
878 } else if (strcasestr(msg
+ 4, "timeout")) {
879 log_error(LOG_PROTOCOL
, _("Session timeout\n"));
882 switch (session
->state
) {
885 log_error(LOG_PROTOCOL
, _("couldn't start TLS session\n"));
889 case POP3_GETAUTH_USER
:
890 case POP3_GETAUTH_PASS
:
891 case POP3_GETAUTH_APOP
:
892 log_error(LOG_PROTOCOL
, _("error occurred on authentication\n"));
895 case POP3_GETRANGE_LAST
:
896 case POP3_GETRANGE_UIDL
:
898 log_warning(LOG_PROTOCOL
, _("command not supported\n"));
899 ok
= PS_NOTSUPPORTED
;
903 log_error(LOG_PROTOCOL
, _("error occurred on POP3 session\n"));
908 g_free(session
->error_msg
);
909 session
->error_msg
= g_strdup(msg
);
910 g_printerr("POP3: %s\n", msg
);
914 session
->error_val
= ok
;
918 static gint
pop3_session_recv_msg(Session
*session
, const gchar
*msg
)
920 Pop3Session
*pop3_session
= POP3_SESSION(session
);
921 Pop3ErrorValue val
= PS_SUCCESS
;
925 if (pop3_session
->state
!= POP3_GETRANGE_UIDL_RECV
&&
926 pop3_session
->state
!= POP3_GETSIZE_LIST_RECV
) {
927 val
= pop3_ok(pop3_session
, msg
);
928 if (val
!= PS_SUCCESS
) {
929 if (val
!= PS_NOTSUPPORTED
) {
930 pop3_session
->state
= POP3_ERROR
;
935 if (*body
== '+' || *body
== '-')
937 while (g_ascii_isalpha(*body
))
939 while (g_ascii_isspace(*body
))
943 switch (pop3_session
->state
) {
946 pop3_greeting_recv(pop3_session
, body
);
948 if (pop3_session
->ac_prefs
->ssl_pop
== SSL_STARTTLS
)
949 val
= pop3_stls_send(pop3_session
);
952 if (pop3_session
->ac_prefs
->use_apop_auth
)
953 val
= pop3_getauth_apop_send(pop3_session
);
955 val
= pop3_getauth_user_send(pop3_session
);
959 if (pop3_stls_recv(pop3_session
) != PS_SUCCESS
)
961 if (pop3_session
->ac_prefs
->use_apop_auth
)
962 val
= pop3_getauth_apop_send(pop3_session
);
964 val
= pop3_getauth_user_send(pop3_session
);
967 case POP3_GETAUTH_USER
:
968 val
= pop3_getauth_pass_send(pop3_session
);
970 case POP3_GETAUTH_PASS
:
971 case POP3_GETAUTH_APOP
:
972 if (!pop3_session
->pop_before_smtp
)
973 val
= pop3_getrange_stat_send(pop3_session
);
975 val
= pop3_logout_send(pop3_session
);
977 case POP3_GETRANGE_STAT
:
978 if (pop3_getrange_stat_recv(pop3_session
, body
) < 0)
980 if (pop3_session
->count
> 0)
981 val
= pop3_getrange_uidl_send(pop3_session
);
983 val
= pop3_logout_send(pop3_session
);
985 case POP3_GETRANGE_LAST
:
986 if (val
== PS_NOTSUPPORTED
)
987 pop3_session
->error_val
= PS_SUCCESS
;
988 else if (pop3_getrange_last_recv(pop3_session
, body
) < 0)
990 if (pop3_session
->cur_msg
> 0)
991 val
= pop3_getsize_list_send(pop3_session
);
993 val
= pop3_logout_send(pop3_session
);
995 case POP3_GETRANGE_UIDL
:
996 if (val
== PS_NOTSUPPORTED
) {
997 pop3_session
->error_val
= PS_SUCCESS
;
998 val
= pop3_getrange_last_send(pop3_session
);
1000 pop3_session
->state
= POP3_GETRANGE_UIDL_RECV
;
1001 session_recv_data(session
, 0, ".\r\n");
1004 case POP3_GETSIZE_LIST
:
1005 pop3_session
->state
= POP3_GETSIZE_LIST_RECV
;
1006 session_recv_data(session
, 0, ".\r\n");
1009 pop3_session
->state
= POP3_RETR_RECV
;
1010 session_recv_data(session
, 0, ".\r\n");
1013 if (val
== PS_NOTSUPPORTED
) {
1014 pop3_session
->error_val
= PS_SUCCESS
;
1016 pop3_session
->state
= POP3_TOP_RECV
;
1017 session_recv_data(session
, 0, ".\r\n");
1021 pop3_delete_recv(pop3_session
);
1022 if (pop3_session
->cur_msg
== pop3_session
->count
)
1023 val
= pop3_logout_send(pop3_session
);
1025 pop3_session
->cur_msg
++;
1026 if (pop3_lookup_next(pop3_session
) == POP3_ERROR
)
1031 pop3_session
->state
= POP3_DONE
;
1032 session_disconnect(session
);
1039 return val
== PS_SUCCESS
?0:-1;
1042 static gint
pop3_session_recv_data_finished(Session
*session
, guchar
*data
,
1045 Pop3Session
*pop3_session
= POP3_SESSION(session
);
1046 Pop3ErrorValue val
= PS_SUCCESS
;
1048 switch (pop3_session
->state
) {
1049 case POP3_GETRANGE_UIDL_RECV
:
1050 val
= pop3_getrange_uidl_recv(pop3_session
, data
, len
);
1051 if (val
== PS_SUCCESS
) {
1052 if (pop3_session
->new_msg_exist
)
1053 pop3_getsize_list_send(pop3_session
);
1055 pop3_logout_send(pop3_session
);
1059 case POP3_GETSIZE_LIST_RECV
:
1060 val
= pop3_getsize_list_recv(pop3_session
, data
, len
);
1061 if (val
== PS_SUCCESS
) {
1062 if (pop3_lookup_next(pop3_session
) == POP3_ERROR
)
1067 case POP3_RETR_RECV
:
1068 if (pop3_retr_recv(pop3_session
, data
, len
) < 0)
1071 if (pop3_session
->ac_prefs
->rmmail
&&
1072 pop3_session
->ac_prefs
->msg_leave_time
== 0 &&
1073 pop3_session
->ac_prefs
->msg_leave_hour
== 0 &&
1074 pop3_session
->msg
[pop3_session
->cur_msg
].recv_time
1076 pop3_delete_send(pop3_session
);
1077 else if (pop3_session
->cur_msg
== pop3_session
->count
)
1078 pop3_logout_send(pop3_session
);
1080 pop3_session
->cur_msg
++;
1081 if (pop3_lookup_next(pop3_session
) == POP3_ERROR
)
1086 if (pop3_top_recv(pop3_session
, data
, len
) < 0)
1089 if (pop3_session
->cur_msg
== pop3_session
->count
)
1090 pop3_logout_send(pop3_session
);
1092 pop3_session
->cur_msg
++;
1093 if (pop3_lookup_next(pop3_session
) == POP3_ERROR
)
1098 log_warning(LOG_PROTOCOL
, _("TOP command unsupported\n"));
1099 if (pop3_session
->cur_msg
== pop3_session
->count
)
1100 pop3_logout_send(pop3_session
);
1102 pop3_session
->cur_msg
++;
1103 if (pop3_lookup_next(pop3_session
) == POP3_ERROR
)