2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 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.
36 #include "prefs_account.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,
94 static gchar *pop3_get_filename_for_partial_mail(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_OPENSSL */
123 static gint pop3_getauth_user_send(Pop3Session *session)
125 g_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 g_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 g_return_val_if_fail(session->user != NULL, -1);
148 g_return_val_if_fail(session->pass != NULL, -1);
150 session->state = POP3_GETAUTH_APOP;
152 if ((start = strchr(session->greeting, '<')) == NULL) {
153 log_warning(_("Required APOP timestamp not found "
155 session->error_val = PS_PROTOCOL;
159 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
160 log_warning(_("Timestamp syntax error in greeting\n"));
161 session->error_val = PS_PROTOCOL;
167 apop_str = g_strconcat(start, session->pass, NULL);
168 md5_hex_digest(md5sum, apop_str);
171 pop3_gen_send(session, "APOP %s %s", session->user, md5sum);
176 static gint pop3_getrange_stat_send(Pop3Session *session)
178 session->state = POP3_GETRANGE_STAT;
179 pop3_gen_send(session, "STAT");
183 static gint pop3_getrange_stat_recv(Pop3Session *session, const gchar *msg)
185 if (sscanf(msg, "%d %d", &session->count, &session->total_bytes) != 2) {
186 log_warning(_("POP3 protocol error\n"));
187 session->error_val = PS_PROTOCOL;
190 if (session->count == 0) {
191 session->uidl_is_valid = TRUE;
193 session->msg = g_new0(Pop3MsgInfo, session->count + 1);
194 session->cur_msg = 1;
201 static gint pop3_getrange_last_send(Pop3Session *session)
203 session->state = POP3_GETRANGE_LAST;
204 pop3_gen_send(session, "LAST");
208 static gint pop3_getrange_last_recv(Pop3Session *session, const gchar *msg)
212 if (sscanf(msg, "%d", &last) == 0) {
213 log_warning(_("POP3 protocol error\n"));
214 session->error_val = PS_PROTOCOL;
217 if (session->count > last) {
218 session->new_msg_exist = TRUE;
219 session->cur_msg = last + 1;
221 session->cur_msg = 0;
227 static gint pop3_getrange_uidl_send(Pop3Session *session)
229 session->state = POP3_GETRANGE_UIDL;
230 pop3_gen_send(session, "UIDL");
234 static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *data,
238 gchar buf[POPBUFSIZE];
243 const gchar *p = data;
244 const gchar *lastp = data + len;
245 const gchar *newline;
248 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
250 buf_len = MIN(newline - p, sizeof(buf) - 1);
251 memcpy(buf, p, buf_len);
255 if (p < lastp && *p == '\n') p++;
257 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
259 if (num <= 0 || num > session->count)
262 session->msg[num].uidl = g_strdup(id);
264 recv_time = (time_t)g_hash_table_lookup(
265 session->uidl_table, id);
266 session->msg[num].recv_time = recv_time;
268 partial_recv = (gint)g_hash_table_lookup(
269 session->partial_recv_table, id);
271 if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE) {
272 session->msg[num].received = (partial_recv != 2);
273 session->msg[num].partial_recv = partial_recv;
276 if (!session->new_msg_exist &&
277 (session->ac_prefs->getall || recv_time == RECV_TIME_NONE ||
278 session->ac_prefs->rmmail)) {
279 session->cur_msg = num;
280 session->new_msg_exist = TRUE;
284 session->uidl_is_valid = TRUE;
288 static gint pop3_getsize_list_send(Pop3Session *session)
290 session->state = POP3_GETSIZE_LIST;
291 pop3_gen_send(session, "LIST");
295 static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *data,
298 gchar buf[POPBUFSIZE];
301 const gchar *p = data;
302 const gchar *lastp = data + len;
303 const gchar *newline;
306 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
308 buf_len = MIN(newline - p, sizeof(buf) - 1);
309 memcpy(buf, p, buf_len);
313 if (p < lastp && *p == '\n') p++;
315 if (sscanf(buf, "%u %u", &num, &size) != 2) {
316 session->error_val = PS_PROTOCOL;
320 if (num > 0 && num <= session->count)
321 session->msg[num].size = size;
322 if (num > 0 && num < session->cur_msg)
323 session->cur_total_bytes += size;
329 static gint pop3_retr_send(Pop3Session *session)
331 session->state = POP3_RETR;
332 pop3_gen_send(session, "RETR %d", session->cur_msg);
336 static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
340 MailReceiveData mail_receive_data;
342 mail_receive_data.session = session;
343 mail_receive_data.data = g_strndup(data, len);
344 hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
346 file = get_tmp_file();
347 if (pop3_write_msg_to_file(file, mail_receive_data.data,
348 strlen(mail_receive_data.data), NULL) < 0) {
350 g_free(mail_receive_data.data);
351 session->error_val = PS_IOERR;
354 g_free(mail_receive_data.data);
356 if (session->msg[session->cur_msg].partial_recv == 2) {
357 gchar *old_file = pop3_get_filename_for_partial_mail(
358 session, session->msg[session->cur_msg].uidl);
359 if (old_file != NULL) {
360 move_file(file, old_file, TRUE);
364 /* drop_ok: 0: success 1: don't receive -1: error */
365 drop_ok = session->drop_message(
366 session, file, old_file != NULL);
368 /* drop_ok: 0: success 1: don't receive -1: error */
369 drop_ok = session->drop_message(
370 session, file, FALSE);
374 session->error_val = PS_IOERR;
378 session->cur_total_bytes += session->msg[session->cur_msg].size;
379 session->cur_total_recv_bytes += session->msg[session->cur_msg].size;
380 session->cur_total_num++;
382 session->msg[session->cur_msg].received = TRUE;
383 session->msg[session->cur_msg].partial_recv = FALSE;
385 session->msg[session->cur_msg].recv_time =
386 drop_ok == 1 ? RECV_TIME_KEEP : session->current_time;
391 static gint pop3_top_send(Pop3Session *session, gint max_size)
393 gint num_lines = (max_size*1024)/82; /* consider lines to be 80 chars */
394 session->state = POP3_TOP;
395 pop3_gen_send(session, "TOP %d %d", session->cur_msg, num_lines);
399 static gint pop3_top_recv(Pop3Session *session, const gchar *data, guint len)
403 MailReceiveData mail_receive_data;
404 gchar *partial_notice = NULL;
406 mail_receive_data.session = session;
407 mail_receive_data.data = g_strndup(data, len);
408 hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
410 partial_notice = g_strdup_printf("SC-Partially-Retrieved: %s\n"
411 "SC-Account-Server: %s\n"
412 "SC-Account-Login: %s\n"
413 "SC-Message-Size: %d",
414 session->msg[session->cur_msg].uidl,
415 session->ac_prefs->recv_server,
416 session->ac_prefs->userid,
417 session->msg[session->cur_msg].size);
418 file = get_tmp_file();
419 if (pop3_write_msg_to_file(file, mail_receive_data.data,
420 strlen(mail_receive_data.data), partial_notice) < 0) {
422 g_free(mail_receive_data.data);
423 session->error_val = PS_IOERR;
424 g_free(partial_notice);
427 g_free(mail_receive_data.data);
428 g_free(partial_notice);
430 /* drop_ok: 0: success 1: don't receive -1: error */
431 drop_ok = session->drop_message(session, file, FALSE);
434 session->error_val = PS_IOERR;
438 session->cur_total_bytes += session->msg[session->cur_msg].size;
439 session->cur_total_recv_bytes += session->msg[session->cur_msg].size;
440 session->cur_total_num++;
442 session->msg[session->cur_msg].received = TRUE;
443 session->msg[session->cur_msg].partial_recv = TRUE;
444 session->msg[session->cur_msg].recv_time =
445 drop_ok == 1 ? RECV_TIME_KEEP : session->current_time;
450 static gint pop3_delete_send(Pop3Session *session)
452 session->state = POP3_DELETE;
453 pop3_gen_send(session, "DELE %d", session->cur_msg);
457 static gint pop3_delete_recv(Pop3Session *session)
459 session->msg[session->cur_msg].deleted = TRUE;
463 static gint pop3_logout_send(Pop3Session *session)
465 session->state = POP3_LOGOUT;
466 pop3_gen_send(session, "QUIT");
470 static void pop3_gen_send(Pop3Session *session, const gchar *format, ...)
472 gchar buf[POPBUFSIZE + 1];
475 va_start(args, format);
476 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
479 if (!strncasecmp(buf, "PASS ", 5))
480 log_print("POP3> PASS ********\n");
482 log_print("POP3> %s\n", buf);
484 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
487 Session *pop3_session_new(PrefsAccount *account)
489 Pop3Session *session;
491 g_return_val_if_fail(account != NULL, NULL);
493 session = g_new0(Pop3Session, 1);
495 session_init(SESSION(session));
497 SESSION(session)->type = SESSION_POP3;
499 SESSION(session)->recv_msg = pop3_session_recv_msg;
500 SESSION(session)->recv_data_finished = pop3_session_recv_data_finished;
501 SESSION(session)->send_data_finished = NULL;
503 SESSION(session)->destroy = pop3_session_destroy;
505 session->state = POP3_READY;
506 session->ac_prefs = account;
507 session->pop_before_smtp = FALSE;
508 pop3_get_uidl_table(account, session);
509 session->current_time = time(NULL);
510 session->error_val = PS_SUCCESS;
511 session->error_msg = NULL;
513 return SESSION(session);
516 static void pop3_session_destroy(Session *session)
518 Pop3Session *pop3_session = POP3_SESSION(session);
521 g_return_if_fail(session != NULL);
523 for (n = 1; n <= pop3_session->count; n++)
524 g_free(pop3_session->msg[n].uidl);
525 g_free(pop3_session->msg);
527 if (pop3_session->uidl_table) {
528 hash_free_strings(pop3_session->uidl_table);
529 g_hash_table_destroy(pop3_session->uidl_table);
532 if (pop3_session->partial_recv_table) {
533 hash_free_strings(pop3_session->partial_recv_table);
534 g_hash_table_destroy(pop3_session->partial_recv_table);
537 g_free(pop3_session->greeting);
538 g_free(pop3_session->user);
539 g_free(pop3_session->pass);
540 g_free(pop3_session->error_msg);
543 void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
546 GHashTable *partial_recv_table;
549 gchar buf[POPBUFSIZE];
550 gchar uidl[POPBUFSIZE];
555 table = g_hash_table_new(g_str_hash, g_str_equal);
556 partial_recv_table = g_hash_table_new(g_str_hash, g_str_equal);
558 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
559 "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
560 "-", ac_prefs->userid, NULL);
561 if ((fp = fopen(path, "rb")) == NULL) {
562 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
564 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
565 "uidl-", ac_prefs->recv_server,
566 "-", ac_prefs->userid, NULL);
567 if ((fp = fopen(path, "rb")) == NULL) {
568 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
570 session->uidl_table = table;
571 session->partial_recv_table = partial_recv_table;
579 while (fgets(buf, sizeof(buf), fp) != NULL) {
580 gchar tmp[POPBUFSIZE];
582 recv_time = RECV_TIME_NONE;
585 if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, &tmp) < 2) {
586 if (sscanf(buf, "%s", uidl) != 1)
592 if (recv_time == RECV_TIME_NONE)
593 recv_time = RECV_TIME_RECEIVED;
594 g_hash_table_insert(table, g_strdup(uidl),
595 GINT_TO_POINTER(recv_time));
596 if (strlen(tmp) == 1)
597 partial_recv = atoi(tmp);
601 g_hash_table_insert(partial_recv_table, g_strdup(uidl),
602 GINT_TO_POINTER(partial_recv));
606 session->uidl_table = table;
607 session->partial_recv_table = partial_recv_table;
612 int pop3_msg_in_uidl_list(const gchar *server, const gchar *login,
617 gchar buf[POPBUFSIZE];
618 gchar uidl[POPBUFSIZE];
623 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
624 "uidl", G_DIR_SEPARATOR_S, server,
626 if ((fp = fopen(path, "rb")) == NULL) {
627 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
629 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
632 if ((fp = fopen(path, "rb")) == NULL) {
633 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
642 while (fgets(buf, sizeof(buf), fp) != NULL) {
643 gchar tmp[POPBUFSIZE];
645 recv_time = RECV_TIME_NONE;
648 if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, &tmp) < 2) {
649 if (sscanf(buf, "%s", uidl) != 1)
655 if (!strcmp(uidl, muidl)) {
665 static gchar *pop3_get_filename_for_partial_mail(Pop3Session *session,
669 gchar *result = NULL;
671 gchar buf[POPBUFSIZE];
672 gchar uidl[POPBUFSIZE];
677 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
678 "uidl", G_DIR_SEPARATOR_S,
679 session->ac_prefs->recv_server,
680 "-", session->ac_prefs->userid, NULL);
681 if ((fp = fopen(path, "rb")) == NULL) {
682 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
684 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
685 "uidl-", session->ac_prefs->recv_server,
686 "-", session->ac_prefs->userid, NULL);
687 if ((fp = fopen(path, "rb")) == NULL) {
688 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
697 while (fgets(buf, sizeof(buf), fp) != NULL) {
698 gchar tmp[POPBUFSIZE];
700 recv_time = RECV_TIME_NONE;
703 if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, &tmp) < 2) {
704 if (sscanf(buf, "%s", uidl) != 1)
710 if (!strcmp(muidl, uidl)) {
711 result = strdup(tmp);
721 #define DOWNLOAD_MAIL 1
722 #define DELETE_MAIL 2
723 static int pop3_uidl_mark_mail(const gchar *server, const gchar *login,
724 const gchar *muidl, const gchar *filename,
731 gchar buf[POPBUFSIZE];
732 gchar uidl[POPBUFSIZE];
737 gchar partial_recv[POPBUFSIZE];
739 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
740 "uidl", G_DIR_SEPARATOR_S, server,
742 if ((fp = fopen(path, "rb")) == NULL) {
744 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
746 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
749 if ((fp = fopen(path, "rb")) == NULL) {
750 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
756 pathnew = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
757 "uidl", G_DIR_SEPARATOR_S, server,
758 "-", login, ".new", NULL);
759 if ((fpnew = fopen(pathnew, "wb")) == NULL) {
768 while (fgets(buf, sizeof(buf), fp) != NULL) {
770 recv_time = RECV_TIME_NONE;
771 sprintf(partial_recv,"0");
773 if (sscanf(buf, "%s\t%ld\t%s",
774 uidl, &recv_time, &partial_recv) < 2) {
775 if (sscanf(buf, "%s", uidl) != 1)
781 if (strcmp(muidl, uidl)) {
782 fprintf(fpnew, "%s\t%ld\t%s\n",
783 uidl, recv_time, partial_recv);
785 fprintf(fpnew, "%s\t%ld\t%s\n",
787 download==DOWNLOAD_MAIL?filename:"0");
793 move_file(pathnew, path, TRUE);
798 if ((fp = fopen(filename,"rb")) == NULL) {
802 pathnew = g_strdup_printf("%s.new", filename);
803 if ((fpnew = fopen(pathnew, "wb")) == NULL) {
810 while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
813 fprintf(fpnew, "SC-Marked-For-Download: %d\n",
815 printf("buf '%s'\n", buf);
816 if(strlen(buf) > strlen("SC-Marked-For-Download: x\n")
817 && !strncmp(buf, "SC-Marked-For-Download:",
818 strlen("SC-Marked-For-Download:"))) {
820 buf+strlen("SC-Marked-For-Download: x\n"));
824 fprintf(fpnew, "%s", buf);
829 rename(pathnew, filename);
835 int pop3_mark_for_delete(const gchar *server, const gchar *login,
836 const gchar *muidl, const gchar *filename)
838 return pop3_uidl_mark_mail(server, login, muidl, filename,
842 int pop3_mark_for_download(const gchar *server, const gchar *login,
843 const gchar *muidl, const gchar *filename)
845 return pop3_uidl_mark_mail(server, login, muidl, filename,
849 gint pop3_write_uidl_list(Pop3Session *session)
856 if (!session->uidl_is_valid) return 0;
858 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
859 "uidl", G_DIR_SEPARATOR_S,
860 session->ac_prefs->recv_server,
861 "-", session->ac_prefs->userid, NULL);
862 if ((fp = fopen(path, "wb")) == NULL) {
863 FILE_OP_ERROR(path, "fopen");
868 for (n = 1; n <= session->count; n++) {
869 msg = &session->msg[n];
870 if (msg->uidl && msg->received && !msg->deleted) {
871 fprintf(fp, "%s\t%ld\t%d\n",
872 msg->uidl, msg->recv_time, msg->partial_recv);
876 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
882 static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
883 guint len, const gchar *prefix)
886 const gchar *prev, *cur;
888 g_return_val_if_fail(file != NULL, -1);
890 if ((fp = fopen(file, "wb")) == NULL) {
891 FILE_OP_ERROR(file, "fopen");
895 if (change_file_mode_rw(fp, file) < 0)
896 FILE_OP_ERROR(file, "chmod");
898 if (prefix != NULL) {
903 /* +------------------+----------------+--------------------------+ *
904 * ^data ^prev ^cur data+len-1^ */
907 while ((cur = memchr(prev, '\r', len - (prev - data))) != NULL) {
908 if ((cur > prev && fwrite(prev, cur - prev, 1, fp) < 1) ||
909 fputc('\n', fp) == EOF) {
910 FILE_OP_ERROR(file, "fwrite");
911 g_warning("can't write to file: %s\n", file);
917 if (cur == data + len - 1) {
922 if (*(cur + 1) == '\n')
927 if (prev - data < len - 1 && *prev == '.' && *(prev + 1) == '.')
930 if (prev - data >= len)
934 if (prev - data < len &&
935 fwrite(prev, len - (prev - data), 1, fp) < 1) {
936 FILE_OP_ERROR(file, "fwrite");
937 g_warning("can't write to file: %s\n", file);
942 if (data[len - 1] != '\r' && data[len - 1] != '\n') {
943 if (fputc('\n', fp) == EOF) {
944 FILE_OP_ERROR(file, "fputc");
945 g_warning("can't write to file: %s\n", file);
952 if (fclose(fp) == EOF) {
953 FILE_OP_ERROR(file, "fclose");
961 static Pop3State pop3_lookup_next(Pop3Session *session)
964 PrefsAccount *ac = session->ac_prefs;
966 gboolean size_limit_over;
969 msg = &session->msg[session->cur_msg];
972 (ac->enable_size_limit &&
973 ac->size_limit > 0 &&
974 size > ac->size_limit * 1024);
977 msg->recv_time != RECV_TIME_NONE &&
978 msg->recv_time != RECV_TIME_KEEP &&
979 msg->partial_recv == 0 &&
980 session->current_time - msg->recv_time >=
981 ac->msg_leave_time * 24 * 60 * 60) {
983 (_("POP3: Deleting expired message "
984 "%d\n"), session->cur_msg);
985 pop3_delete_send(session);
989 if (size_limit_over) {
991 (_("POP3: Skipping message %d (%d bytes)\n"),
992 session->cur_msg, size);
994 if (!msg->received && msg->partial_recv != 2) {
995 pop3_top_send(session, ac->size_limit);
997 } else if (msg->partial_recv == 2) {
1002 if (size == 0 || msg->received || size_limit_over) {
1003 session->cur_total_bytes += size;
1004 if (session->cur_msg == session->count) {
1005 pop3_logout_send(session);
1013 pop3_retr_send(session);
1017 static Pop3ErrorValue pop3_ok(Pop3Session *session, const gchar *msg)
1021 log_print("POP3< %s\n", msg);
1023 if (!strncmp(msg, "+OK", 3))
1025 else if (!strncmp(msg, "-ERR", 4)) {
1026 if (strstr(msg + 4, "lock") ||
1027 strstr(msg + 4, "Lock") ||
1028 strstr(msg + 4, "LOCK") ||
1029 strstr(msg + 4, "wait")) {
1030 log_warning(_("mailbox is locked\n"));
1032 } else if (strcasestr(msg + 4, "timeout")) {
1033 log_warning(_("session timeout\n"));
1036 switch (session->state) {
1039 log_warning(_("can't start TLS session\n"));
1043 case POP3_GETAUTH_USER:
1044 case POP3_GETAUTH_PASS:
1045 case POP3_GETAUTH_APOP:
1046 log_warning(_("error occurred on authentication\n"));
1049 case POP3_GETRANGE_LAST:
1050 case POP3_GETRANGE_UIDL:
1052 log_warning(_("command not supported\n"));
1053 ok = PS_NOTSUPPORTED;
1057 log_warning(_("error occurred on POP3 session\n"));
1062 g_free(session->error_msg);
1063 session->error_msg = g_strdup(msg);
1064 fprintf(stderr, "POP3: %s\n", msg);
1068 session->error_val = ok;
1072 static gint pop3_session_recv_msg(Session *session, const gchar *msg)
1074 Pop3Session *pop3_session = POP3_SESSION(session);
1075 Pop3ErrorValue val = PS_SUCCESS;
1079 if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
1080 pop3_session->state != POP3_GETSIZE_LIST_RECV) {
1081 val = pop3_ok(pop3_session, msg);
1082 if (val != PS_SUCCESS) {
1083 if (val != PS_NOTSUPPORTED) {
1084 pop3_session->state = POP3_ERROR;
1089 if (*body == '+' || *body == '-')
1091 while (isalpha(*body))
1093 while (isspace(*body))
1097 switch (pop3_session->state) {
1100 pop3_greeting_recv(pop3_session, body);
1102 if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
1103 pop3_stls_send(pop3_session);
1106 if (pop3_session->ac_prefs->protocol == A_APOP)
1107 pop3_getauth_apop_send(pop3_session);
1109 pop3_getauth_user_send(pop3_session);
1113 if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
1115 if (pop3_session->ac_prefs->protocol == A_APOP)
1116 pop3_getauth_apop_send(pop3_session);
1118 pop3_getauth_user_send(pop3_session);
1121 case POP3_GETAUTH_USER:
1122 pop3_getauth_pass_send(pop3_session);
1124 case POP3_GETAUTH_PASS:
1125 case POP3_GETAUTH_APOP:
1126 if (!pop3_session->pop_before_smtp)
1127 pop3_getrange_stat_send(pop3_session);
1129 pop3_logout_send(pop3_session);
1131 case POP3_GETRANGE_STAT:
1132 if (pop3_getrange_stat_recv(pop3_session, body) < 0)
1134 if (pop3_session->count > 0)
1135 pop3_getrange_uidl_send(pop3_session);
1137 pop3_logout_send(pop3_session);
1139 case POP3_GETRANGE_LAST:
1140 if (val == PS_NOTSUPPORTED)
1141 pop3_session->error_val = PS_SUCCESS;
1142 else if (pop3_getrange_last_recv(pop3_session, body) < 0)
1144 if (pop3_session->cur_msg > 0)
1145 pop3_getsize_list_send(pop3_session);
1147 pop3_logout_send(pop3_session);
1149 case POP3_GETRANGE_UIDL:
1150 if (val == PS_NOTSUPPORTED) {
1151 pop3_session->error_val = PS_SUCCESS;
1152 pop3_getrange_last_send(pop3_session);
1154 pop3_session->state = POP3_GETRANGE_UIDL_RECV;
1155 session_recv_data(session, 0, ".\r\n");
1158 case POP3_GETSIZE_LIST:
1159 pop3_session->state = POP3_GETSIZE_LIST_RECV;
1160 session_recv_data(session, 0, ".\r\n");
1163 pop3_session->state = POP3_RETR_RECV;
1164 session_recv_data(session, 0, ".\r\n");
1167 if (val == PS_NOTSUPPORTED) {
1168 pop3_session->error_val = PS_SUCCESS;
1170 pop3_session->state = POP3_TOP_RECV;
1171 session_recv_data(session, 0, ".\r\n");
1175 pop3_delete_recv(pop3_session);
1176 if (pop3_session->cur_msg == pop3_session->count)
1177 pop3_logout_send(pop3_session);
1179 pop3_session->cur_msg++;
1180 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1185 session_disconnect(session);
1195 static gint pop3_session_recv_data_finished(Session *session, guchar *data,
1198 Pop3Session *pop3_session = POP3_SESSION(session);
1199 Pop3ErrorValue val = PS_SUCCESS;
1201 switch (pop3_session->state) {
1202 case POP3_GETRANGE_UIDL_RECV:
1203 val = pop3_getrange_uidl_recv(pop3_session, data, len);
1204 if (val == PS_SUCCESS) {
1205 if (pop3_session->new_msg_exist)
1206 pop3_getsize_list_send(pop3_session);
1208 pop3_logout_send(pop3_session);
1212 case POP3_GETSIZE_LIST_RECV:
1213 val = pop3_getsize_list_recv(pop3_session, data, len);
1214 if (val == PS_SUCCESS) {
1215 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1220 case POP3_RETR_RECV:
1221 if (pop3_retr_recv(pop3_session, data, len) < 0)
1224 if (pop3_session->ac_prefs->rmmail &&
1225 pop3_session->ac_prefs->msg_leave_time == 0 &&
1226 pop3_session->msg[pop3_session->cur_msg].recv_time
1228 pop3_delete_send(pop3_session);
1229 else if (pop3_session->cur_msg == pop3_session->count)
1230 pop3_logout_send(pop3_session);
1232 pop3_session->cur_msg++;
1233 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1238 if (pop3_top_recv(pop3_session, data, len) < 0)
1241 if (pop3_session->cur_msg == pop3_session->count)
1242 pop3_logout_send(pop3_session);
1244 pop3_session->cur_msg++;
1245 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1250 log_warning(_("TOP command unsupported\n"));
1251 if (pop3_session->cur_msg == pop3_session->count)
1252 pop3_logout_send(pop3_session);
1254 pop3_session->cur_msg++;
1255 if (pop3_lookup_next(pop3_session) == POP3_ERROR)