2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2022 the Claws Mail team and 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 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/>.
21 #include "claws-features.h"
25 #include <glib/gi18n.h>
36 #include "prefs_account.h"
39 #include "partial_download.h"
42 #include "file-utils.h"
45 static gint
pop3_greeting_recv (Pop3Session
*session
,
47 static gint
pop3_getauth_user_send (Pop3Session
*session
);
48 static gint
pop3_getauth_pass_send (Pop3Session
*session
);
49 static gint
pop3_getauth_apop_send (Pop3Session
*session
);
51 static gint
pop3_stls_send (Pop3Session
*session
);
52 static gint
pop3_stls_recv (Pop3Session
*session
);
54 static gint
pop3_getrange_stat_send (Pop3Session
*session
);
55 static gint
pop3_getrange_stat_recv (Pop3Session
*session
,
57 static gint
pop3_getrange_last_send (Pop3Session
*session
);
58 static gint
pop3_getrange_last_recv (Pop3Session
*session
,
60 static gint
pop3_getrange_uidl_send (Pop3Session
*session
);
61 static gint
pop3_getrange_uidl_recv (Pop3Session
*session
,
64 static gint
pop3_getsize_list_send (Pop3Session
*session
);
65 static gint
pop3_getsize_list_recv (Pop3Session
*session
,
68 static gint
pop3_retr_send (Pop3Session
*session
);
69 static gint
pop3_retr_recv (Pop3Session
*session
,
72 static gint
pop3_delete_send (Pop3Session
*session
);
73 static gint
pop3_delete_recv (Pop3Session
*session
);
74 static gint
pop3_logout_send (Pop3Session
*session
);
76 static void pop3_gen_send (Pop3Session
*session
,
77 const gchar
*format
, ...);
79 static void pop3_session_destroy (Session
*session
);
81 static gint
pop3_write_msg_to_file (const gchar
*file
,
86 static Pop3State
pop3_lookup_next (Pop3Session
*session
);
87 static Pop3ErrorValue
pop3_ok (Pop3Session
*session
,
90 static gint
pop3_session_recv_msg (Session
*session
,
92 static gint
pop3_session_recv_data_finished (Session
*session
,
95 static void pop3_get_uidl_table(PrefsAccount
*ac_prefs
, Pop3Session
*session
);
97 static gint
pop3_greeting_recv(Pop3Session
*session
, const gchar
*msg
)
99 session
->state
= POP3_GREETING
;
101 session
->greeting
= g_strdup(msg
);
106 static gint
pop3_stls_send(Pop3Session
*session
)
108 session
->state
= POP3_STLS
;
109 pop3_gen_send(session
, "STLS");
113 static gint
pop3_stls_recv(Pop3Session
*session
)
115 if (session_start_tls(SESSION(session
)) < 0) {
116 session
->error_val
= PS_SOCKET
;
121 #endif /* USE_GNUTLS */
123 static gint
pop3_getauth_user_send(Pop3Session
*session
)
125 cm_return_val_if_fail(session
->user
!= NULL
, -1);
127 session
->state
= POP3_GETAUTH_USER
;
128 pop3_gen_send(session
, "USER %s", session
->user
);
132 static gint
pop3_getauth_pass_send(Pop3Session
*session
)
134 cm_return_val_if_fail(session
->pass
!= NULL
, -1);
136 session
->state
= POP3_GETAUTH_PASS
;
137 pop3_gen_send(session
, "PASS %s", session
->pass
);
141 static gint
pop3_getauth_apop_send(Pop3Session
*session
)
147 cm_return_val_if_fail(session
->user
!= NULL
, -1);
148 cm_return_val_if_fail(session
->pass
!= NULL
, -1);
150 session
->state
= POP3_GETAUTH_APOP
;
152 if ((start
= strchr(session
->greeting
, '<')) == NULL
) {
153 log_error(LOG_PROTOCOL
, _("Required APOP timestamp not found "
155 session
->error_val
= PS_PROTOCOL
;
159 if ((end
= strchr(start
, '>')) == NULL
|| end
== start
+ 1) {
160 log_error(LOG_PROTOCOL
, _("Timestamp syntax error in greeting\n"));
161 session
->error_val
= PS_PROTOCOL
;
166 if (!is_ascii_str(start
)) {
167 log_error(LOG_PROTOCOL
, _("Timestamp syntax error in greeting (not ASCII)\n"));
168 session
->error_val
= PS_PROTOCOL
;
172 apop_str
= g_strconcat(start
, session
->pass
, NULL
);
173 md5_hex_digest(md5sum
, apop_str
);
176 pop3_gen_send(session
, "APOP %s %s", session
->user
, md5sum
);
182 static gint
pop3_getauth_oauth2_send_generic(Pop3Session
*session
)
184 gchar buf
[MESSAGEBUFSIZE
], *b64buf
, *out
;
187 cm_return_val_if_fail(session
->user
!= NULL
, -1);
188 cm_return_val_if_fail(session
->pass
!= NULL
, -1);
190 session
->state
= POP3_GETAUTH_OAUTH2
;
191 memset(buf
, 0, sizeof buf
);
193 /* "user=" {User} "^Aauth=Bearer " {Access Token} "^A^A" */
194 /* session->pass contains the OAUTH2 Access Token */
195 len
= sprintf(buf
, "user=%s\1auth=Bearer %s\1\1", session
->user
, session
->pass
);
196 b64buf
= g_base64_encode(buf
, len
);
197 out
= g_strconcat("AUTH XOAUTH2 ", b64buf
, NULL
);
200 pop3_gen_send(session
, "%s", out
);
201 /* Any error response contains base64({JSON-Body}) containing three values: status, schemes, and scope */
202 /* This could be dealt with but is currently written to the log in a fairly graceful fail - not crucial */
207 /* Microsoft requires authentication to be split in two lines */
208 static gint
pop3_getauth_oauth2_send_microsoft_1(Pop3Session
*session
)
210 cm_return_val_if_fail(session
->user
!= NULL
, -1);
211 cm_return_val_if_fail(session
->pass
!= NULL
, -1);
213 session
->state
= POP3_GETAUTH_USER_PHASE2
;
215 pop3_gen_send(session
, "AUTH XOAUTH2");
220 static gint
pop3_getauth_oauth2_send_microsoft_2(Pop3Session
*session
)
222 gchar buf
[MESSAGEBUFSIZE
], *b64buf
;
225 cm_return_val_if_fail(session
->user
!= NULL
, -1);
226 cm_return_val_if_fail(session
->pass
!= NULL
, -1);
228 session
->state
= POP3_GETAUTH_OAUTH2
;
229 memset(buf
, 0, sizeof buf
);
231 /* "user=" {User} "^Aauth=Bearer " {Access Token} "^A^A"*/
232 /* session->pass contains the OAUTH2 Access Token*/
233 len
= sprintf(buf
, "user=%s\1auth=Bearer %s\1\1", session
->user
, session
->pass
);
234 b64buf
= g_base64_encode(buf
, len
);
236 pop3_gen_send(session
, "%s", b64buf
);
239 /* Any error response contains base64({JSON-Body}) containing three values: status, schemes, and scope */
240 /* This could be dealt with but is currently written to the log in a fairly graceful fail - not crucial */
244 static gint
pop3_getauth_oauth2_send(Pop3Session
*session
)
246 gint oauth2_provider
= session
->ac_prefs
->oauth2_provider
;
247 return ( oauth2_provider
== OAUTH2AUTH_OUTLOOK
||
248 oauth2_provider
== OAUTH2AUTH_EXCHANGE
249 ? pop3_getauth_oauth2_send_microsoft_1(session
)
250 : pop3_getauth_oauth2_send_generic(session
)
255 static gint
pop3_getrange_stat_send(Pop3Session
*session
)
257 session
->state
= POP3_GETRANGE_STAT
;
258 pop3_gen_send(session
, "STAT");
262 static gint
pop3_getrange_stat_recv(Pop3Session
*session
, const gchar
*msg
)
264 if (sscanf(msg
, "%d %d", &session
->count
, &session
->total_bytes
) != 2) {
265 log_error(LOG_PROTOCOL
, _("POP protocol error\n"));
266 session
->error_val
= PS_PROTOCOL
;
269 if (session
->count
== 0) {
270 session
->uidl_is_valid
= TRUE
;
272 session
->msg
= g_new0(Pop3MsgInfo
, session
->count
+ 1);
273 session
->cur_msg
= 1;
280 static gint
pop3_getrange_last_send(Pop3Session
*session
)
282 session
->state
= POP3_GETRANGE_LAST
;
283 pop3_gen_send(session
, "LAST");
287 static gint
pop3_getrange_last_recv(Pop3Session
*session
, const gchar
*msg
)
291 if (sscanf(msg
, "%d", &last
) == 0) {
292 log_warning(LOG_PROTOCOL
, _("POP protocol error\n"));
293 session
->error_val
= PS_PROTOCOL
;
296 if (session
->count
> last
) {
297 session
->new_msg_exist
= TRUE
;
298 session
->cur_msg
= last
+ 1;
300 session
->cur_msg
= 0;
306 static gint
pop3_getrange_uidl_send(Pop3Session
*session
)
308 session
->state
= POP3_GETRANGE_UIDL
;
309 pop3_gen_send(session
, "UIDL");
313 static gint
pop3_getrange_uidl_recv(Pop3Session
*session
, const gchar
*data
,
317 gchar buf
[POPBUFSIZE
];
322 const gchar
*p
= data
;
323 const gchar
*lastp
= data
+ len
;
324 const gchar
*newline
;
327 if ((newline
= memchr(p
, '\r', lastp
- p
)) == NULL
)
329 buf_len
= MIN(newline
- p
, sizeof(buf
) - 1);
330 memcpy(buf
, p
, buf_len
);
334 if (p
< lastp
&& *p
== '\n') p
++;
336 if (sscanf(buf
, "%d %" Xstr(IDLEN
) "s", &num
, id
) != 2 ||
337 num
<= 0 || num
> session
->count
) {
338 log_warning(LOG_PROTOCOL
, _("invalid UIDL response: %s\n"), buf
);
342 session
->msg
[num
].uidl
= g_strdup(id
);
344 recv_time
= (time_t)(GPOINTER_TO_INT(g_hash_table_lookup(
345 session
->uidl_table
, id
)));
346 session
->msg
[num
].recv_time
= recv_time
;
348 if (recv_time
!= RECV_TIME_NONE
) {
349 debug_print("num %d uidl %s: already got it\n", num
, id
);
351 debug_print("num %d uidl %s: unknown\n", num
, id
);
354 partial_recv
= (gint
)(GPOINTER_TO_INT(g_hash_table_lookup(
355 session
->partial_recv_table
, id
)));
357 if (recv_time
!= RECV_TIME_NONE
358 || partial_recv
!= POP3_TOTALLY_RECEIVED
) {
359 session
->msg
[num
].received
=
360 (partial_recv
!= POP3_MUST_COMPLETE_RECV
);
361 session
->msg
[num
].partial_recv
= partial_recv
;
362 if (partial_recv
== POP3_MUST_COMPLETE_RECV
)
363 session
->new_msg_exist
= TRUE
;
365 if (!session
->new_msg_exist
&&
366 (recv_time
== RECV_TIME_NONE
||
367 session
->ac_prefs
->rmmail
)) {
368 session
->cur_msg
= num
;
369 session
->new_msg_exist
= TRUE
;
373 session
->uidl_is_valid
= TRUE
;
377 static gint
pop3_getsize_list_send(Pop3Session
*session
)
379 session
->state
= POP3_GETSIZE_LIST
;
380 pop3_gen_send(session
, "LIST");
384 static gint
pop3_getsize_list_recv(Pop3Session
*session
, const gchar
*data
,
387 gchar buf
[POPBUFSIZE
];
390 const gchar
*p
= data
;
391 const gchar
*lastp
= data
+ len
;
392 const gchar
*newline
;
395 if ((newline
= memchr(p
, '\r', lastp
- p
)) == NULL
)
397 buf_len
= MIN(newline
- p
, sizeof(buf
) - 1);
398 memcpy(buf
, p
, buf_len
);
402 if (p
< lastp
&& *p
== '\n') p
++;
404 if (sscanf(buf
, "%u %u", &num
, &size
) != 2) {
405 session
->error_val
= PS_PROTOCOL
;
409 if (num
> 0 && num
<= session
->count
)
410 session
->msg
[num
].size
= size
;
411 if (num
> 0 && num
< session
->cur_msg
)
412 session
->cur_total_bytes
+= size
;
418 static gint
pop3_retr_send(Pop3Session
*session
)
420 session
->state
= POP3_RETR
;
421 debug_print("retrieving %d [%s]\n", session
->cur_msg
,
422 session
->msg
[session
->cur_msg
].uidl
?
423 session
->msg
[session
->cur_msg
].uidl
:" ");
424 pop3_gen_send(session
, "RETR %d", session
->cur_msg
);
428 static gint
pop3_retr_recv(Pop3Session
*session
, const gchar
*data
, guint len
)
432 MailReceiveData mail_receive_data
;
434 /* NOTE: we allocate a slightly larger buffer with a zero terminator
435 * because some plugins may think that it has a C string. */
436 mail_receive_data
.session
= session
;
437 mail_receive_data
.data
= g_new0(gchar
, len
+ 1);
438 mail_receive_data
.data_len
= len
;
439 memcpy(mail_receive_data
.data
, data
, len
);
441 hooks_invoke(MAIL_RECEIVE_HOOKLIST
, &mail_receive_data
);
443 file
= get_tmp_file();
444 if (pop3_write_msg_to_file(file
, mail_receive_data
.data
,
445 mail_receive_data
.data_len
, NULL
) < 0) {
447 g_free(mail_receive_data
.data
);
448 session
->error_val
= PS_IOERR
;
451 g_free(mail_receive_data
.data
);
453 if (session
->msg
[session
->cur_msg
].partial_recv
454 == POP3_MUST_COMPLETE_RECV
) {
455 gchar
*old_file
= partial_get_filename(
456 session
->ac_prefs
->recv_server
,
457 session
->ac_prefs
->userid
,
458 session
->msg
[session
->cur_msg
].uidl
);
461 partial_delete_old(old_file
);
466 /* drop_ok: 0: success 1: don't receive -1: error */
467 drop_ok
= session
->drop_message(session
, file
);
471 session
->error_val
= PS_IOERR
;
475 session
->cur_total_bytes
+= session
->msg
[session
->cur_msg
].size
;
476 session
->cur_total_recv_bytes
+= session
->msg
[session
->cur_msg
].size
;
477 session
->cur_total_num
++;
479 session
->msg
[session
->cur_msg
].received
= TRUE
;
480 session
->msg
[session
->cur_msg
].partial_recv
= POP3_TOTALLY_RECEIVED
;
482 session
->msg
[session
->cur_msg
].recv_time
=
483 drop_ok
== 1 ? RECV_TIME_KEEP
: session
->current_time
;
488 static gint
pop3_top_send(Pop3Session
*session
, gint max_size
)
490 gint num_lines
= (max_size
*1024)/82; /* consider lines to be 80 chars */
491 session
->state
= POP3_TOP
;
492 pop3_gen_send(session
, "TOP %d %d", session
->cur_msg
, num_lines
);
496 static gint
pop3_top_recv(Pop3Session
*session
, const gchar
*data
, guint len
)
500 MailReceiveData mail_receive_data
;
501 gchar
*partial_notice
= NULL
;
503 /* NOTE: we allocate a slightly larger buffer with a zero terminator
504 * because some plugins may think that it has a C string. */
505 mail_receive_data
.session
= session
;
506 mail_receive_data
.data
= g_new0(gchar
, len
+ 1);
507 mail_receive_data
.data_len
= len
;
508 memcpy(mail_receive_data
.data
, data
, len
);
510 hooks_invoke(MAIL_RECEIVE_HOOKLIST
, &mail_receive_data
);
512 partial_notice
= g_strdup_printf("SC-Marked-For-Download: 0\n"
513 "SC-Partially-Retrieved: %s\n"
514 "SC-Account-Server: %s\n"
515 "SC-Account-Login: %s\n"
516 "SC-Message-Size: %d",
517 session
->msg
[session
->cur_msg
].uidl
,
518 session
->ac_prefs
->recv_server
,
519 session
->ac_prefs
->userid
,
520 session
->msg
[session
->cur_msg
].size
);
521 file
= get_tmp_file();
522 if (pop3_write_msg_to_file(file
, mail_receive_data
.data
,
523 mail_receive_data
.data_len
,
524 partial_notice
) < 0) {
526 g_free(mail_receive_data
.data
);
527 session
->error_val
= PS_IOERR
;
528 g_free(partial_notice
);
531 g_free(mail_receive_data
.data
);
532 g_free(partial_notice
);
534 /* drop_ok: 0: success 1: don't receive -1: error */
535 drop_ok
= session
->drop_message(session
, file
);
538 session
->error_val
= PS_IOERR
;
542 session
->cur_total_bytes
+= session
->msg
[session
->cur_msg
].size
;
543 session
->cur_total_recv_bytes
+= session
->msg
[session
->cur_msg
].size
;
544 session
->cur_total_num
++;
546 session
->msg
[session
->cur_msg
].received
= TRUE
;
547 session
->msg
[session
->cur_msg
].partial_recv
= POP3_PARTIALLY_RECEIVED
;
548 session
->msg
[session
->cur_msg
].recv_time
=
549 drop_ok
== 1 ? RECV_TIME_KEEP
: session
->current_time
;
554 static gint
pop3_delete_send(Pop3Session
*session
)
556 session
->state
= POP3_DELETE
;
557 pop3_gen_send(session
, "DELE %d", session
->cur_msg
);
561 static gint
pop3_delete_recv(Pop3Session
*session
)
563 session
->msg
[session
->cur_msg
].deleted
= TRUE
;
567 static gint
pop3_logout_send(Pop3Session
*session
)
569 session
->state
= POP3_LOGOUT
;
570 pop3_gen_send(session
, "QUIT");
574 static void pop3_gen_send(Pop3Session
*session
, const gchar
*format
, ...)
576 gchar buf
[POPBUFSIZE
+ 1];
580 va_start(args
, format
);
581 length
= g_vsnprintf(buf
, sizeof(buf
) - 2, format
, args
);
583 if (length
> POPBUFSIZE
)
584 g_warning("POP buffer length overflow! (wanted to write = %d > %d = buf size)\n", length
, POPBUFSIZE
);
586 if (!g_ascii_strncasecmp(buf
, "PASS ", 5))
587 log_print(LOG_PROTOCOL
, "POP> PASS ********\n");
589 else if (!g_ascii_strncasecmp(buf
, "AUTH XOAUTH2", 12))
590 log_print(LOG_PROTOCOL
, "POP> AUTH XOAUTH2 ********\n");
592 else if (length
> 128)
593 log_print(LOG_PROTOCOL
, "POP> %.128s... (truncated from %d)\n", buf
, length
);
595 log_print(LOG_PROTOCOL
, "POP> %s\n", buf
);
597 session_send_msg(SESSION(session
), buf
);
600 Session
*pop3_session_new(PrefsAccount
*account
)
602 Pop3Session
*session
;
604 cm_return_val_if_fail(account
!= NULL
, NULL
);
606 account
->receive_in_progress
= TRUE
;
608 session
= g_new0(Pop3Session
, 1);
610 session_init(SESSION(session
), account
, FALSE
);
612 SESSION(session
)->type
= SESSION_POP3
;
614 SESSION(session
)->recv_msg
= pop3_session_recv_msg
;
615 SESSION(session
)->recv_data_finished
= pop3_session_recv_data_finished
;
616 SESSION(session
)->send_data_finished
= NULL
;
617 SESSION(session
)->ssl_cert_auto_accept
= account
->ssl_certs_auto_accept
;
618 SESSION(session
)->destroy
= pop3_session_destroy
;
621 if (account
->set_gnutls_priority
&& account
->gnutls_priority
&&
622 strlen(account
->gnutls_priority
) != 0)
623 SESSION(session
)->gnutls_priority
= g_strdup(account
->gnutls_priority
);
624 SESSION(session
)->use_tls_sni
= account
->use_tls_sni
;
627 session
->state
= POP3_READY
;
628 session
->ac_prefs
= account
;
629 session
->pop_before_smtp
= FALSE
;
630 pop3_get_uidl_table(account
, session
);
631 session
->current_time
= time(NULL
);
632 session
->error_val
= PS_SUCCESS
;
633 session
->error_msg
= NULL
;
635 return SESSION(session
);
638 static void pop3_session_destroy(Session
*session
)
640 Pop3Session
*pop3_session
= POP3_SESSION(session
);
643 cm_return_if_fail(session
!= NULL
);
645 for (n
= 1; n
<= pop3_session
->count
; n
++)
646 g_free(pop3_session
->msg
[n
].uidl
);
647 g_free(pop3_session
->msg
);
649 if (pop3_session
->uidl_table
) {
650 hash_free_strings(pop3_session
->uidl_table
);
651 g_hash_table_destroy(pop3_session
->uidl_table
);
654 if (pop3_session
->partial_recv_table
) {
655 hash_free_strings(pop3_session
->partial_recv_table
);
656 g_hash_table_destroy(pop3_session
->partial_recv_table
);
659 g_free(pop3_session
->greeting
);
660 g_free(pop3_session
->user
);
661 g_free(pop3_session
->pass
);
662 g_free(pop3_session
->error_msg
);
664 pop3_session
->ac_prefs
->receive_in_progress
= FALSE
;
667 static void pop3_get_uidl_table(PrefsAccount
*ac_prefs
, Pop3Session
*session
)
670 GHashTable
*partial_recv_table
;
673 gchar buf
[POPBUFSIZE
];
674 gchar uidl
[POPBUFSIZE
];
678 gchar
*sanitized_uid
= g_strdup(ac_prefs
->userid
);
680 subst_for_filename(sanitized_uid
);
682 table
= g_hash_table_new(g_str_hash
, g_str_equal
);
683 partial_recv_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
685 path
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
686 "uidl", G_DIR_SEPARATOR_S
, ac_prefs
->recv_server
,
687 "-", sanitized_uid
, NULL
);
689 g_free(sanitized_uid
);
690 if ((fp
= claws_fopen(path
, "rb")) == NULL
) {
691 if (ENOENT
!= errno
) FILE_OP_ERROR(path
, "claws_fopen");
693 path
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
694 "uidl-", ac_prefs
->recv_server
,
695 "-", ac_prefs
->userid
, NULL
);
696 if ((fp
= claws_fopen(path
, "rb")) == NULL
) {
697 if (ENOENT
!= errno
) FILE_OP_ERROR(path
, "claws_fopen");
699 session
->uidl_table
= table
;
700 session
->partial_recv_table
= partial_recv_table
;
708 while (claws_fgets(buf
, sizeof(buf
), fp
) != NULL
) {
709 gchar tmp
[POPBUFSIZE
];
711 recv_time
= RECV_TIME_NONE
;
712 partial_recv
= POP3_TOTALLY_RECEIVED
;
714 if (sscanf(buf
, "%s\t%ld\t%s", uidl
, (long int *) &recv_time
, tmp
) < 3) {
715 if (sscanf(buf
, "%s\t%ld", uidl
, (long int *) &recv_time
) != 2) {
716 if (sscanf(buf
, "%s", uidl
) != 1)
727 if (recv_time
== RECV_TIME_NONE
)
728 recv_time
= RECV_TIME_RECEIVED
;
729 g_hash_table_insert(table
, g_strdup(uidl
),
730 GINT_TO_POINTER(recv_time
));
731 if (strlen(tmp
) == 1)
732 partial_recv
= atoi(tmp
); /* totally received ?*/
734 partial_recv
= POP3_MUST_COMPLETE_RECV
;
736 g_hash_table_insert(partial_recv_table
, g_strdup(uidl
),
737 GINT_TO_POINTER(partial_recv
));
741 session
->uidl_table
= table
;
742 session
->partial_recv_table
= partial_recv_table
;
750 g_warning("failed to write"); \
754 gint pop3_write_uidl_list(Pop3Session *session)
756 gchar
*path
, *tmp_path
;
760 gchar
*sanitized_uid
= g_strdup(session
->ac_prefs
->userid
);
762 subst_for_filename(sanitized_uid
);
764 if (!session
->uidl_is_valid
) {
765 g_free(sanitized_uid
);
769 path
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
,
770 "uidl", G_DIR_SEPARATOR_S
,
771 session
->ac_prefs
->recv_server
,
772 "-", sanitized_uid
, NULL
);
773 tmp_path
= g_strconcat(path
, ".tmp", NULL
);
775 g_free(sanitized_uid
);
777 if ((fp
= claws_fopen(tmp_path
, "wb")) == NULL
) {
778 FILE_OP_ERROR(tmp_path
, "claws_fopen");
782 for (n
= 1; n
<= session
->count
; n
++) {
783 msg
= &session
->msg
[n
];
784 if (msg
->uidl
&& msg
->received
&&
785 (!msg
->deleted
|| session
->state
!= POP3_DONE
))
786 TRY(fprintf(fp
, "%s\t%ld\t%d\n",
787 msg
->uidl
, (long int)
793 if (claws_safe_fclose(fp
) == EOF
) {
794 FILE_OP_ERROR(tmp_path
, "claws_fclose");
802 if (g_rename(tmp_path
, path
) < 0) {
803 FILE_OP_ERROR(path
, "rename");
819 static gint
pop3_write_msg_to_file(const gchar
*file
, const gchar
*data
,
820 guint len
, const gchar
*prefix
)
823 const gchar
*prev
, *cur
;
825 cm_return_val_if_fail(file
!= NULL
, -1);
827 if ((fp
= claws_fopen(file
, "wb")) == NULL
) {
828 FILE_OP_ERROR(file
, "claws_fopen");
832 if (change_file_mode_rw(fp
, file
) < 0)
833 FILE_OP_ERROR(file
, "chmod");
835 if (prefix
!= NULL
) {
836 if (fprintf(fp
, "%s\n", prefix
) < 0) {
837 FILE_OP_ERROR(file
, "fprintf");
844 /* +------------------+----------------+--------------------------+ *
845 * ^data ^prev ^cur data+len-1^ */
848 while ((cur
= (gchar
*)my_memmem(prev
, len
- (prev
- data
), "\r\n", 2))
850 if ((cur
> prev
&& claws_fwrite(prev
, 1, cur
- prev
, fp
) < 1) ||
851 claws_fputc('\n', fp
) == EOF
) {
852 FILE_OP_ERROR(file
, "claws_fwrite");
853 g_warning("can't write to file: %s", file
);
859 if (cur
== data
+ len
- 1) {
864 if (*(cur
+ 1) == '\n')
869 if (prev
- data
< len
- 1 && *prev
== '.' && *(prev
+ 1) == '.')
872 if (prev
- data
>= len
)
876 if (prev
- data
< len
&&
877 claws_fwrite(prev
, 1, len
- (prev
- data
), fp
) < 1) {
878 FILE_OP_ERROR(file
, "claws_fwrite");
879 g_warning("can't write to file: %s", file
);
884 if (data
[len
- 1] != '\r' && data
[len
- 1] != '\n') {
885 if (claws_fputc('\n', fp
) == EOF
) {
886 FILE_OP_ERROR(file
, "claws_fputc");
887 g_warning("can't write to file: %s", file
);
894 if (claws_safe_fclose(fp
) == EOF
) {
895 FILE_OP_ERROR(file
, "claws_fclose");
903 static Pop3State
pop3_lookup_next(Pop3Session
*session
)
906 PrefsAccount
*ac
= session
->ac_prefs
;
908 gboolean size_limit_over
;
911 msg
= &session
->msg
[session
->cur_msg
];
914 (ac
->enable_size_limit
&&
915 ac
->size_limit
> 0 &&
916 size
> ac
->size_limit
* 1024);
919 msg
->recv_time
!= RECV_TIME_NONE
&&
920 msg
->recv_time
!= RECV_TIME_KEEP
&&
921 msg
->partial_recv
== POP3_TOTALLY_RECEIVED
&&
922 session
->current_time
- msg
->recv_time
>=
923 ((ac
->msg_leave_time
* 24 * 60 * 60) +
924 (ac
->msg_leave_hour
* 60 * 60))) {
925 log_message(LOG_PROTOCOL
,
926 _("POP: Deleting expired message %d [%s]\n"),
927 session
->cur_msg
, msg
->uidl
?msg
->uidl
:" ");
928 session
->cur_total_bytes
+= size
;
929 pop3_delete_send(session
);
933 if (size_limit_over
) {
934 if (!msg
->received
&& msg
->partial_recv
!=
935 POP3_MUST_COMPLETE_RECV
) {
936 pop3_top_send(session
, ac
->size_limit
);
938 } else if (msg
->partial_recv
== POP3_MUST_COMPLETE_RECV
)
941 log_message(LOG_PROTOCOL
,
942 _("POP: Skipping message %d [%s] (%d bytes)\n"),
943 session
->cur_msg
, msg
->uidl
?msg
->uidl
:" ", size
);
946 if (size
== 0 || msg
->received
|| size_limit_over
) {
947 session
->cur_total_bytes
+= size
;
948 if (session
->cur_msg
== session
->count
) {
949 pop3_logout_send(session
);
957 pop3_retr_send(session
);
961 static Pop3ErrorValue
pop3_ok(Pop3Session
*session
, const gchar
*msg
)
965 log_print(LOG_PROTOCOL
, "POP< %s\n", msg
);
967 /* exchange replies '+' in response to first line of auth xoauth */
968 if (!strncmp(msg
, "+OK", 3) || !strncmp(msg
, "+", 1))
970 else if (!strncmp(msg
, "-ERR", 4)) {
971 if (strstr(msg
+ 4, "lock") ||
972 strstr(msg
+ 4, "Lock") ||
973 strstr(msg
+ 4, "LOCK") ||
974 strstr(msg
+ 4, "wait")) {
975 log_error(LOG_PROTOCOL
, _("mailbox is locked\n"));
977 } else if (strcasestr(msg
+ 4, "timeout")) {
978 log_error(LOG_PROTOCOL
, _("Session timeout\n"));
981 switch (session
->state
) {
984 log_error(LOG_PROTOCOL
, _("couldn't start STARTTLS session\n"));
988 case POP3_GETAUTH_USER
:
989 case POP3_GETAUTH_PASS
:
990 case POP3_GETAUTH_APOP
:
991 log_error(LOG_PROTOCOL
, _("error occurred on authentication\n"));
994 case POP3_GETRANGE_LAST
:
995 case POP3_GETRANGE_UIDL
:
997 log_warning(LOG_PROTOCOL
, _("command not supported\n"));
998 ok
= PS_NOTSUPPORTED
;
1002 log_error(LOG_PROTOCOL
, _("error occurred on POP session\n"));
1007 g_free(session
->error_msg
);
1008 session
->error_msg
= g_strdup(msg
);
1009 g_printerr("POP: %s\n", msg
);
1013 session
->error_val
= ok
;
1017 static gint
pop3_session_recv_msg(Session
*session
, const gchar
*msg
)
1019 Pop3Session
*pop3_session
= POP3_SESSION(session
);
1020 Pop3ErrorValue val
= PS_SUCCESS
;
1024 if (pop3_session
->state
!= POP3_GETRANGE_UIDL_RECV
&&
1025 pop3_session
->state
!= POP3_GETSIZE_LIST_RECV
) {
1026 val
= pop3_ok(pop3_session
, msg
);
1027 if (val
!= PS_SUCCESS
) {
1028 if (val
!= PS_NOTSUPPORTED
) {
1029 pop3_session
->state
= POP3_ERROR
;
1034 if (*body
== '+' || *body
== '-')
1036 while (g_ascii_isalpha(*body
))
1038 while (g_ascii_isspace(*body
))
1042 switch (pop3_session
->state
) {
1045 pop3_greeting_recv(pop3_session
, body
);
1047 if (pop3_session
->ac_prefs
->ssl_pop
== SSL_STARTTLS
)
1048 val
= pop3_stls_send(pop3_session
);
1052 if (pop3_session
->ac_prefs
->use_pop_auth
&& pop3_session
->ac_prefs
->pop_auth_type
== POPAUTH_OAUTH2
)
1053 val
= pop3_getauth_oauth2_send(pop3_session
);
1056 if (pop3_session
->ac_prefs
->use_pop_auth
&& pop3_session
->ac_prefs
->pop_auth_type
== POPAUTH_APOP
)
1057 val
= pop3_getauth_apop_send(pop3_session
);
1059 val
= pop3_getauth_user_send(pop3_session
);
1063 if (pop3_stls_recv(pop3_session
) != PS_SUCCESS
)
1065 if (pop3_session
->ac_prefs
->use_pop_auth
&& pop3_session
->ac_prefs
->pop_auth_type
== POPAUTH_APOP
)
1066 val
= pop3_getauth_apop_send(pop3_session
);
1068 else if (pop3_session
->ac_prefs
->use_pop_auth
&& pop3_session
->ac_prefs
->pop_auth_type
== POPAUTH_OAUTH2
)
1069 val
= pop3_getauth_oauth2_send(pop3_session
);
1072 val
= pop3_getauth_user_send(pop3_session
);
1075 case POP3_GETAUTH_USER
:
1076 val
= pop3_getauth_pass_send(pop3_session
);
1079 case POP3_GETAUTH_USER_PHASE2
:
1080 val
= pop3_getauth_oauth2_send_microsoft_2(pop3_session
);
1083 case POP3_GETAUTH_PASS
:
1084 case POP3_GETAUTH_APOP
:
1086 case POP3_GETAUTH_OAUTH2
:
1088 if (!pop3_session
->pop_before_smtp
)
1089 val
= pop3_getrange_stat_send(pop3_session
);
1091 val
= pop3_logout_send(pop3_session
);
1093 case POP3_GETRANGE_STAT
:
1094 if (pop3_getrange_stat_recv(pop3_session
, body
) < 0)
1096 if (pop3_session
->count
> 0)
1097 val
= pop3_getrange_uidl_send(pop3_session
);
1099 val
= pop3_logout_send(pop3_session
);
1101 case POP3_GETRANGE_LAST
:
1102 if (val
== PS_NOTSUPPORTED
)
1103 pop3_session
->error_val
= PS_SUCCESS
;
1104 else if (pop3_getrange_last_recv(pop3_session
, body
) < 0)
1106 if (pop3_session
->cur_msg
> 0)
1107 val
= pop3_getsize_list_send(pop3_session
);
1109 val
= pop3_logout_send(pop3_session
);
1111 case POP3_GETRANGE_UIDL
:
1112 if (val
== PS_NOTSUPPORTED
) {
1113 pop3_session
->error_val
= PS_SUCCESS
;
1114 val
= pop3_getrange_last_send(pop3_session
);
1116 pop3_session
->state
= POP3_GETRANGE_UIDL_RECV
;
1117 session_recv_data(session
, 0, ".\r\n");
1120 case POP3_GETSIZE_LIST
:
1121 pop3_session
->state
= POP3_GETSIZE_LIST_RECV
;
1122 session_recv_data(session
, 0, ".\r\n");
1125 pop3_session
->state
= POP3_RETR_RECV
;
1126 session_recv_data(session
, 0, ".\r\n");
1129 if (val
== PS_NOTSUPPORTED
) {
1130 pop3_session
->error_val
= PS_SUCCESS
;
1132 pop3_session
->state
= POP3_TOP_RECV
;
1133 session_recv_data(session
, 0, ".\r\n");
1137 pop3_delete_recv(pop3_session
);
1138 if (pop3_session
->cur_msg
== pop3_session
->count
)
1139 val
= pop3_logout_send(pop3_session
);
1141 pop3_session
->cur_msg
++;
1142 if (pop3_lookup_next(pop3_session
) == POP3_ERROR
)
1147 pop3_session
->state
= POP3_DONE
;
1148 session_disconnect(session
);
1155 return val
== PS_SUCCESS
?0:-1;
1158 static gint
pop3_session_recv_data_finished(Session
*session
, guchar
*data
,
1161 Pop3Session
*pop3_session
= POP3_SESSION(session
);
1162 Pop3ErrorValue val
= PS_SUCCESS
;
1164 switch (pop3_session
->state
) {
1165 case POP3_GETRANGE_UIDL_RECV
:
1166 val
= pop3_getrange_uidl_recv(pop3_session
, data
, len
);
1167 if (val
== PS_SUCCESS
) {
1168 if (pop3_session
->new_msg_exist
)
1169 pop3_getsize_list_send(pop3_session
);
1171 pop3_logout_send(pop3_session
);
1175 case POP3_GETSIZE_LIST_RECV
:
1176 val
= pop3_getsize_list_recv(pop3_session
, data
, len
);
1177 if (val
== PS_SUCCESS
) {
1178 if (pop3_lookup_next(pop3_session
) == POP3_ERROR
)
1183 case POP3_RETR_RECV
:
1184 if (pop3_retr_recv(pop3_session
, data
, len
) < 0)
1187 if (pop3_session
->ac_prefs
->rmmail
&&
1188 pop3_session
->ac_prefs
->msg_leave_time
== 0 &&
1189 pop3_session
->ac_prefs
->msg_leave_hour
== 0 &&
1190 pop3_session
->msg
[pop3_session
->cur_msg
].recv_time
1192 pop3_delete_send(pop3_session
);
1193 else if (pop3_session
->cur_msg
== pop3_session
->count
)
1194 pop3_logout_send(pop3_session
);
1196 pop3_session
->cur_msg
++;
1197 if (pop3_lookup_next(pop3_session
) == POP3_ERROR
)
1202 if (pop3_top_recv(pop3_session
, data
, len
) < 0)
1205 if (pop3_session
->cur_msg
== pop3_session
->count
)
1206 pop3_logout_send(pop3_session
);
1208 pop3_session
->cur_msg
++;
1209 if (pop3_lookup_next(pop3_session
) == POP3_ERROR
)
1214 log_warning(LOG_PROTOCOL
, _("TOP command unsupported\n"));
1215 if (pop3_session
->cur_msg
== pop3_session
->count
)
1216 pop3_logout_send(pop3_session
);
1218 pop3_session
->cur_msg
++;
1219 if (pop3_lookup_next(pop3_session
) == POP3_ERROR
)