for release 4.2.0
[claws.git] / src / pop.c
blob695f5a072c210f52278403ce8f1f52530f3010df
1 /*
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/>.
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #include "claws-features.h"
22 #endif
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <ctype.h>
30 #include <unistd.h>
31 #include <time.h>
32 #include <errno.h>
34 #include "pop.h"
35 #include "md5.h"
36 #include "prefs_account.h"
37 #include "utils.h"
38 #include "recv.h"
39 #include "partial_download.h"
40 #include "log.h"
41 #include "hooks.h"
42 #include "file-utils.h"
43 #include "oauth2.h"
45 static gint pop3_greeting_recv (Pop3Session *session,
46 const gchar *msg);
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);
50 #ifdef USE_GNUTLS
51 static gint pop3_stls_send (Pop3Session *session);
52 static gint pop3_stls_recv (Pop3Session *session);
53 #endif
54 static gint pop3_getrange_stat_send (Pop3Session *session);
55 static gint pop3_getrange_stat_recv (Pop3Session *session,
56 const gchar *msg);
57 static gint pop3_getrange_last_send (Pop3Session *session);
58 static gint pop3_getrange_last_recv (Pop3Session *session,
59 const gchar *msg);
60 static gint pop3_getrange_uidl_send (Pop3Session *session);
61 static gint pop3_getrange_uidl_recv (Pop3Session *session,
62 const gchar *data,
63 guint len);
64 static gint pop3_getsize_list_send (Pop3Session *session);
65 static gint pop3_getsize_list_recv (Pop3Session *session,
66 const gchar *data,
67 guint len);
68 static gint pop3_retr_send (Pop3Session *session);
69 static gint pop3_retr_recv (Pop3Session *session,
70 const gchar *data,
71 guint len);
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,
82 const gchar *data,
83 guint len,
84 const gchar *prefix);
86 static Pop3State pop3_lookup_next (Pop3Session *session);
87 static Pop3ErrorValue pop3_ok (Pop3Session *session,
88 const gchar *msg);
90 static gint pop3_session_recv_msg (Session *session,
91 const gchar *msg);
92 static gint pop3_session_recv_data_finished (Session *session,
93 guchar *data,
94 guint len);
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);
102 return PS_SUCCESS;
105 #ifdef USE_GNUTLS
106 static gint pop3_stls_send(Pop3Session *session)
108 session->state = POP3_STLS;
109 pop3_gen_send(session, "STLS");
110 return PS_SUCCESS;
113 static gint pop3_stls_recv(Pop3Session *session)
115 if (session_start_tls(SESSION(session)) < 0) {
116 session->error_val = PS_SOCKET;
117 return -1;
119 return PS_SUCCESS;
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);
129 return PS_SUCCESS;
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);
138 return PS_SUCCESS;
141 static gint pop3_getauth_apop_send(Pop3Session *session)
143 gchar *start, *end;
144 gchar *apop_str;
145 gchar md5sum[33];
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 "
154 "in greeting\n"));
155 session->error_val = PS_PROTOCOL;
156 return -1;
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;
162 return -1;
164 *(end + 1) = '\0';
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;
169 return -1;
172 apop_str = g_strconcat(start, session->pass, NULL);
173 md5_hex_digest(md5sum, apop_str);
174 g_free(apop_str);
176 pop3_gen_send(session, "APOP %s %s", session->user, md5sum);
178 return PS_SUCCESS;
181 #ifdef USE_OAUTH2
182 static gint pop3_getauth_oauth2_send_generic(Pop3Session *session)
184 gchar buf[MESSAGEBUFSIZE], *b64buf, *out;
185 gint len;
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);
198 g_free(b64buf);
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 */
203 g_free(out);
204 return PS_SUCCESS;
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");
217 return PS_SUCCESS;
220 static gint pop3_getauth_oauth2_send_microsoft_2(Pop3Session *session)
222 gchar buf[MESSAGEBUFSIZE], *b64buf;
223 gint len;
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);
238 g_free(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 */
241 return PS_SUCCESS;
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)
253 #endif
255 static gint pop3_getrange_stat_send(Pop3Session *session)
257 session->state = POP3_GETRANGE_STAT;
258 pop3_gen_send(session, "STAT");
259 return PS_SUCCESS;
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;
267 return -1;
268 } else {
269 if (session->count == 0) {
270 session->uidl_is_valid = TRUE;
271 } else {
272 session->msg = g_new0(Pop3MsgInfo, session->count + 1);
273 session->cur_msg = 1;
277 return PS_SUCCESS;
280 static gint pop3_getrange_last_send(Pop3Session *session)
282 session->state = POP3_GETRANGE_LAST;
283 pop3_gen_send(session, "LAST");
284 return PS_SUCCESS;
287 static gint pop3_getrange_last_recv(Pop3Session *session, const gchar *msg)
289 gint last;
291 if (sscanf(msg, "%d", &last) == 0) {
292 log_warning(LOG_PROTOCOL, _("POP protocol error\n"));
293 session->error_val = PS_PROTOCOL;
294 return -1;
295 } else {
296 if (session->count > last) {
297 session->new_msg_exist = TRUE;
298 session->cur_msg = last + 1;
299 } else
300 session->cur_msg = 0;
303 return PS_SUCCESS;
306 static gint pop3_getrange_uidl_send(Pop3Session *session)
308 session->state = POP3_GETRANGE_UIDL;
309 pop3_gen_send(session, "UIDL");
310 return PS_SUCCESS;
313 static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *data,
314 guint len)
316 gchar id[IDLEN + 1];
317 gchar buf[POPBUFSIZE];
318 gint buf_len;
319 guint32 num;
320 time_t recv_time;
321 gint partial_recv;
322 const gchar *p = data;
323 const gchar *lastp = data + len;
324 const gchar *newline;
326 while (p < lastp) {
327 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
328 return -1;
329 buf_len = MIN(newline - p, sizeof(buf) - 1);
330 memcpy(buf, p, buf_len);
331 buf[buf_len] = '\0';
333 p = newline + 1;
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);
339 continue;
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);
350 } else {
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;
374 return PS_SUCCESS;
377 static gint pop3_getsize_list_send(Pop3Session *session)
379 session->state = POP3_GETSIZE_LIST;
380 pop3_gen_send(session, "LIST");
381 return PS_SUCCESS;
384 static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *data,
385 guint len)
387 gchar buf[POPBUFSIZE];
388 gint buf_len;
389 guint num, size;
390 const gchar *p = data;
391 const gchar *lastp = data + len;
392 const gchar *newline;
394 while (p < lastp) {
395 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
396 return -1;
397 buf_len = MIN(newline - p, sizeof(buf) - 1);
398 memcpy(buf, p, buf_len);
399 buf[buf_len] = '\0';
401 p = newline + 1;
402 if (p < lastp && *p == '\n') p++;
404 if (sscanf(buf, "%u %u", &num, &size) != 2) {
405 session->error_val = PS_PROTOCOL;
406 return -1;
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;
415 return PS_SUCCESS;
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);
425 return PS_SUCCESS;
428 static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
430 gchar *file;
431 gint drop_ok;
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) {
446 g_free(file);
447 g_free(mail_receive_data.data);
448 session->error_val = PS_IOERR;
449 return -1;
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);
460 if (old_file) {
461 partial_delete_old(old_file);
462 g_free(old_file);
466 /* drop_ok: 0: success 1: don't receive -1: error */
467 drop_ok = session->drop_message(session, file);
469 g_free(file);
470 if (drop_ok < 0) {
471 session->error_val = PS_IOERR;
472 return -1;
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;
485 return PS_SUCCESS;
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);
493 return PS_SUCCESS;
496 static gint pop3_top_recv(Pop3Session *session, const gchar *data, guint len)
498 gchar *file;
499 gint drop_ok;
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) {
525 g_free(file);
526 g_free(mail_receive_data.data);
527 session->error_val = PS_IOERR;
528 g_free(partial_notice);
529 return -1;
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);
536 g_free(file);
537 if (drop_ok < 0) {
538 session->error_val = PS_IOERR;
539 return -1;
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;
551 return PS_SUCCESS;
554 static gint pop3_delete_send(Pop3Session *session)
556 session->state = POP3_DELETE;
557 pop3_gen_send(session, "DELE %d", session->cur_msg);
558 return PS_SUCCESS;
561 static gint pop3_delete_recv(Pop3Session *session)
563 session->msg[session->cur_msg].deleted = TRUE;
564 return PS_SUCCESS;
567 static gint pop3_logout_send(Pop3Session *session)
569 session->state = POP3_LOGOUT;
570 pop3_gen_send(session, "QUIT");
571 return PS_SUCCESS;
574 static void pop3_gen_send(Pop3Session *session, const gchar *format, ...)
576 gchar buf[POPBUFSIZE + 1];
577 int length;
578 va_list args;
580 va_start(args, format);
581 length = g_vsnprintf(buf, sizeof(buf) - 2, format, args);
582 va_end(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");
588 #ifdef USE_OAUTH2
589 else if (!g_ascii_strncasecmp(buf, "AUTH XOAUTH2", 12))
590 log_print(LOG_PROTOCOL, "POP> AUTH XOAUTH2 ********\n");
591 #endif
592 else if (length > 128)
593 log_print(LOG_PROTOCOL, "POP> %.128s... (truncated from %d)\n", buf, length);
594 else
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;
620 #ifdef USE_GNUTLS
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;
625 #endif
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);
641 gint n;
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)
669 GHashTable *table;
670 GHashTable *partial_recv_table;
671 gchar *path;
672 FILE *fp;
673 gchar buf[POPBUFSIZE];
674 gchar uidl[POPBUFSIZE];
675 time_t recv_time;
676 time_t now;
677 gint partial_recv;
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");
692 g_free(path);
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");
698 g_free(path);
699 session->uidl_table = table;
700 session->partial_recv_table = partial_recv_table;
701 return;
704 g_free(path);
706 now = time(NULL);
708 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
709 gchar tmp[POPBUFSIZE];
710 strretchomp(buf);
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)
717 continue;
718 else {
719 recv_time = now;
720 strcpy(tmp, "0");
722 } else {
723 strcpy(tmp, "0");
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 ?*/
733 else
734 partial_recv = POP3_MUST_COMPLETE_RECV;
736 g_hash_table_insert(partial_recv_table, g_strdup(uidl),
737 GINT_TO_POINTER(partial_recv));
740 claws_fclose(fp);
741 session->uidl_table = table;
742 session->partial_recv_table = partial_recv_table;
744 return;
747 #define TRY(func) \
748 if (!(func)) \
750 g_warning("failed to write"); \
751 goto err_write; \
754 gint pop3_write_uidl_list(Pop3Session *session)
756 gchar *path, *tmp_path;
757 FILE *fp;
758 Pop3MsgInfo *msg;
759 gint n;
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);
766 return 0;
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");
779 goto err_write;
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)
788 msg->recv_time,
789 msg->partial_recv)
790 > 0);
793 if (claws_safe_fclose(fp) == EOF) {
794 FILE_OP_ERROR(tmp_path, "claws_fclose");
795 fp = NULL;
796 goto err_write;
798 fp = NULL;
799 #ifdef G_OS_WIN32
800 claws_unlink(path);
801 #endif
802 if (g_rename(tmp_path, path) < 0) {
803 FILE_OP_ERROR(path, "rename");
804 goto err_write;
806 g_free(path);
807 g_free(tmp_path);
808 return 0;
809 err_write:
810 if (fp)
811 claws_fclose(fp);
812 g_free(path);
813 g_free(tmp_path);
814 return -1;
817 #undef TRY
819 static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
820 guint len, const gchar *prefix)
822 FILE *fp;
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");
829 return -1;
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");
838 claws_fclose(fp);
839 claws_unlink(file);
840 return -1;
844 /* +------------------+----------------+--------------------------+ *
845 * ^data ^prev ^cur data+len-1^ */
847 prev = data;
848 while ((cur = (gchar *)my_memmem(prev, len - (prev - data), "\r\n", 2))
849 != NULL) {
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);
854 claws_fclose(fp);
855 claws_unlink(file);
856 return -1;
859 if (cur == data + len - 1) {
860 prev = cur + 1;
861 break;
864 if (*(cur + 1) == '\n')
865 prev = cur + 2;
866 else
867 prev = cur + 1;
869 if (prev - data < len - 1 && *prev == '.' && *(prev + 1) == '.')
870 prev++;
872 if (prev - data >= len)
873 break;
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);
880 claws_fclose(fp);
881 claws_unlink(file);
882 return -1;
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);
888 claws_fclose(fp);
889 claws_unlink(file);
890 return -1;
894 if (claws_safe_fclose(fp) == EOF) {
895 FILE_OP_ERROR(file, "claws_fclose");
896 claws_unlink(file);
897 return -1;
900 return 0;
903 static Pop3State pop3_lookup_next(Pop3Session *session)
905 Pop3MsgInfo *msg;
906 PrefsAccount *ac = session->ac_prefs;
907 gint size;
908 gboolean size_limit_over;
910 for (;;) {
911 msg = &session->msg[session->cur_msg];
912 size = msg->size;
913 size_limit_over =
914 (ac->enable_size_limit &&
915 ac->size_limit > 0 &&
916 size > ac->size_limit * 1024);
918 if (ac->rmmail &&
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);
930 return POP3_DELETE;
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);
937 return POP3_TOP;
938 } else if (msg->partial_recv == POP3_MUST_COMPLETE_RECV)
939 break;
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);
950 return POP3_LOGOUT;
951 } else
952 session->cur_msg++;
953 } else
954 break;
957 pop3_retr_send(session);
958 return POP3_RETR;
961 static Pop3ErrorValue pop3_ok(Pop3Session *session, const gchar *msg)
963 Pop3ErrorValue ok;
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))
969 ok = PS_SUCCESS;
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"));
976 ok = PS_LOCKBUSY;
977 } else if (strcasestr(msg + 4, "timeout")) {
978 log_error(LOG_PROTOCOL, _("Session timeout\n"));
979 ok = PS_ERROR;
980 } else {
981 switch (session->state) {
982 #ifdef USE_GNUTLS
983 case POP3_STLS:
984 log_error(LOG_PROTOCOL, _("couldn't start STARTTLS session\n"));
985 ok = PS_ERROR;
986 break;
987 #endif
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"));
992 ok = PS_AUTHFAIL;
993 break;
994 case POP3_GETRANGE_LAST:
995 case POP3_GETRANGE_UIDL:
996 case POP3_TOP:
997 log_warning(LOG_PROTOCOL, _("command not supported\n"));
998 ok = PS_NOTSUPPORTED;
999 break;
1001 default:
1002 log_error(LOG_PROTOCOL, _("error occurred on POP session\n"));
1003 ok = PS_ERROR;
1007 g_free(session->error_msg);
1008 session->error_msg = g_strdup(msg);
1009 g_printerr("POP: %s\n", msg);
1010 } else
1011 ok = PS_PROTOCOL;
1013 session->error_val = ok;
1014 return 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;
1021 const gchar *body;
1023 body = msg;
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;
1030 return -1;
1034 if (*body == '+' || *body == '-')
1035 body++;
1036 while (g_ascii_isalpha(*body))
1037 body++;
1038 while (g_ascii_isspace(*body))
1039 body++;
1042 switch (pop3_session->state) {
1043 case POP3_READY:
1044 case POP3_GREETING:
1045 pop3_greeting_recv(pop3_session, body);
1046 #ifdef USE_GNUTLS
1047 if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
1048 val = pop3_stls_send(pop3_session);
1049 else
1050 #endif
1051 #ifdef USE_OAUTH2
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);
1054 else
1055 #endif
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);
1058 else
1059 val = pop3_getauth_user_send(pop3_session);
1060 break;
1061 #ifdef USE_GNUTLS
1062 case POP3_STLS:
1063 if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
1064 return -1;
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);
1067 #ifdef USE_OAUTH2
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);
1070 #endif
1071 else
1072 val = pop3_getauth_user_send(pop3_session);
1073 break;
1074 #endif
1075 case POP3_GETAUTH_USER:
1076 val = pop3_getauth_pass_send(pop3_session);
1077 break;
1078 #ifdef USE_OAUTH2
1079 case POP3_GETAUTH_USER_PHASE2:
1080 val = pop3_getauth_oauth2_send_microsoft_2(pop3_session);
1081 break;
1082 #endif
1083 case POP3_GETAUTH_PASS:
1084 case POP3_GETAUTH_APOP:
1085 #ifdef USE_OAUTH2
1086 case POP3_GETAUTH_OAUTH2:
1087 #endif
1088 if (!pop3_session->pop_before_smtp)
1089 val = pop3_getrange_stat_send(pop3_session);
1090 else
1091 val = pop3_logout_send(pop3_session);
1092 break;
1093 case POP3_GETRANGE_STAT:
1094 if (pop3_getrange_stat_recv(pop3_session, body) < 0)
1095 return -1;
1096 if (pop3_session->count > 0)
1097 val = pop3_getrange_uidl_send(pop3_session);
1098 else
1099 val = pop3_logout_send(pop3_session);
1100 break;
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)
1105 return -1;
1106 if (pop3_session->cur_msg > 0)
1107 val = pop3_getsize_list_send(pop3_session);
1108 else
1109 val = pop3_logout_send(pop3_session);
1110 break;
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);
1115 } else {
1116 pop3_session->state = POP3_GETRANGE_UIDL_RECV;
1117 session_recv_data(session, 0, ".\r\n");
1119 break;
1120 case POP3_GETSIZE_LIST:
1121 pop3_session->state = POP3_GETSIZE_LIST_RECV;
1122 session_recv_data(session, 0, ".\r\n");
1123 break;
1124 case POP3_RETR:
1125 pop3_session->state = POP3_RETR_RECV;
1126 session_recv_data(session, 0, ".\r\n");
1127 break;
1128 case POP3_TOP:
1129 if (val == PS_NOTSUPPORTED) {
1130 pop3_session->error_val = PS_SUCCESS;
1131 } else {
1132 pop3_session->state = POP3_TOP_RECV;
1133 session_recv_data(session, 0, ".\r\n");
1135 break;
1136 case POP3_DELETE:
1137 pop3_delete_recv(pop3_session);
1138 if (pop3_session->cur_msg == pop3_session->count)
1139 val = pop3_logout_send(pop3_session);
1140 else {
1141 pop3_session->cur_msg++;
1142 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1143 return -1;
1145 break;
1146 case POP3_LOGOUT:
1147 pop3_session->state = POP3_DONE;
1148 session_disconnect(session);
1149 break;
1150 case POP3_ERROR:
1151 default:
1152 return -1;
1155 return val == PS_SUCCESS?0:-1;
1158 static gint pop3_session_recv_data_finished(Session *session, guchar *data,
1159 guint len)
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);
1170 else
1171 pop3_logout_send(pop3_session);
1172 } else
1173 return -1;
1174 break;
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)
1179 return -1;
1180 } else
1181 return -1;
1182 break;
1183 case POP3_RETR_RECV:
1184 if (pop3_retr_recv(pop3_session, data, len) < 0)
1185 return -1;
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
1191 != RECV_TIME_KEEP)
1192 pop3_delete_send(pop3_session);
1193 else if (pop3_session->cur_msg == pop3_session->count)
1194 pop3_logout_send(pop3_session);
1195 else {
1196 pop3_session->cur_msg++;
1197 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1198 return -1;
1200 break;
1201 case POP3_TOP_RECV:
1202 if (pop3_top_recv(pop3_session, data, len) < 0)
1203 return -1;
1205 if (pop3_session->cur_msg == pop3_session->count)
1206 pop3_logout_send(pop3_session);
1207 else {
1208 pop3_session->cur_msg++;
1209 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1210 return -1;
1212 break;
1213 case POP3_TOP:
1214 log_warning(LOG_PROTOCOL, _("TOP command unsupported\n"));
1215 if (pop3_session->cur_msg == pop3_session->count)
1216 pop3_logout_send(pop3_session);
1217 else {
1218 pop3_session->cur_msg++;
1219 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1220 return -1;
1222 break;
1223 case POP3_ERROR:
1224 default:
1225 return -1;
1228 return 0;