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 static gchar *pop3_get_filename_for_partial_mail(Pop3Session *session,
616 gchar *result = NULL;
618 gchar buf[POPBUFSIZE];
619 gchar uidl[POPBUFSIZE];
624 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
625 "uidl", G_DIR_SEPARATOR_S,
626 session->ac_prefs->recv_server,
627 "-", session->ac_prefs->userid, NULL);
628 if ((fp = fopen(path, "rb")) == NULL) {
629 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
631 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
632 "uidl-", session->ac_prefs->recv_server,
633 "-", session->ac_prefs->userid, NULL);
634 if ((fp = fopen(path, "rb")) == NULL) {
635 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
644 while (fgets(buf, sizeof(buf), fp) != NULL) {
645 gchar tmp[POPBUFSIZE];
647 recv_time = RECV_TIME_NONE;
650 if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, &tmp) < 2) {
651 if (sscanf(buf, "%s", uidl) != 1)
657 if (!strcmp(muidl, uidl)) {
658 result = strdup(tmp);
668 int pop3_mark_for_download(const gchar *server, const gchar *login,
669 const gchar *muidl, const gchar *filename)
675 gchar buf[POPBUFSIZE];
676 gchar uidl[POPBUFSIZE];
680 gchar partial_recv[POPBUFSIZE];
683 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
684 "uidl", G_DIR_SEPARATOR_S, server,
686 if ((fp = fopen(path, "rb")) == NULL) {
688 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
690 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
693 if ((fp = fopen(path, "rb")) == NULL) {
694 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
700 pathnew = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
701 "uidl", G_DIR_SEPARATOR_S, server,
702 "-", login, ".new", NULL);
703 if ((fpnew = fopen(pathnew, "wb")) == NULL) {
712 while (fgets(buf, sizeof(buf), fp) != NULL) {
714 recv_time = RECV_TIME_NONE;
715 sprintf(partial_recv,"0");
717 if (sscanf(buf, "%s\t%ld\t%s",
718 uidl, &recv_time, &partial_recv) < 2) {
719 if (sscanf(buf, "%s", uidl) != 1)
725 if (strcmp(muidl, uidl)) {
726 fprintf(fpnew, "%s\t%ld\t%s\n",
727 uidl, recv_time, partial_recv);
729 fprintf(fpnew, "%s\t%ld\t%s\n",
730 uidl, recv_time, filename);
736 move_file(pathnew, path, TRUE);
741 if ((fp = fopen(filename,"rb")) == NULL) {
745 pathnew = g_strdup_printf("%s.new", filename);
746 if ((fpnew = fopen(pathnew, "wb")) == NULL) {
753 fprintf(fpnew, "SC-Marked-For-Download: 1\n");
754 while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
755 fprintf(fpnew, "%s", buf);
760 rename(pathnew, filename);
766 gint pop3_write_uidl_list(Pop3Session *session)
773 if (!session->uidl_is_valid) return 0;
775 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
776 "uidl", G_DIR_SEPARATOR_S,
777 session->ac_prefs->recv_server,
778 "-", session->ac_prefs->userid, NULL);
779 if ((fp = fopen(path, "wb")) == NULL) {
780 FILE_OP_ERROR(path, "fopen");
785 for (n = 1; n <= session->count; n++) {
786 msg = &session->msg[n];
787 if (msg->uidl && msg->received && !msg->deleted) {
788 fprintf(fp, "%s\t%ld\t%d\n",
789 msg->uidl, msg->recv_time, msg->partial_recv);
793 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
799 static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
800 guint len, const gchar *prefix)
803 const gchar *prev, *cur;
805 g_return_val_if_fail(file != NULL, -1);
807 if ((fp = fopen(file, "wb")) == NULL) {
808 FILE_OP_ERROR(file, "fopen");
812 if (change_file_mode_rw(fp, file) < 0)
813 FILE_OP_ERROR(file, "chmod");
815 if (prefix != NULL) {
820 /* +------------------+----------------+--------------------------+ *
821 * ^data ^prev ^cur data+len-1^ */
824 while ((cur = memchr(prev, '\r', len - (prev - data))) != NULL) {
825 if ((cur > prev && fwrite(prev, cur - prev, 1, fp) < 1) ||
826 fputc('\n', fp) == EOF) {
827 FILE_OP_ERROR(file, "fwrite");
828 g_warning("can't write to file: %s\n", file);
834 if (cur == data + len - 1) {
839 if (*(cur + 1) == '\n')
844 if (prev - data < len - 1 && *prev == '.' && *(prev + 1) == '.')
847 if (prev - data >= len)
851 if (prev - data < len &&
852 fwrite(prev, len - (prev - data), 1, fp) < 1) {
853 FILE_OP_ERROR(file, "fwrite");
854 g_warning("can't write to file: %s\n", file);
859 if (data[len - 1] != '\r' && data[len - 1] != '\n') {
860 if (fputc('\n', fp) == EOF) {
861 FILE_OP_ERROR(file, "fputc");
862 g_warning("can't write to file: %s\n", file);
869 if (fclose(fp) == EOF) {
870 FILE_OP_ERROR(file, "fclose");
878 static Pop3State pop3_lookup_next(Pop3Session *session)
881 PrefsAccount *ac = session->ac_prefs;
883 gboolean size_limit_over;
886 msg = &session->msg[session->cur_msg];
889 (ac->enable_size_limit &&
890 ac->size_limit > 0 &&
891 size > ac->size_limit * 1024);
894 msg->recv_time != RECV_TIME_NONE &&
895 msg->recv_time != RECV_TIME_KEEP &&
896 session->current_time - msg->recv_time >=
897 ac->msg_leave_time * 24 * 60 * 60) {
898 if (msg->partial_recv == 0) {
900 (_("POP3: Deleting expired message "
901 "%d\n"), session->cur_msg);
902 pop3_delete_send(session);
904 } else if (session->current_time - msg->recv_time >=
907 (_("POP3: Deleting too big expired "
908 "message %d\n"), session->cur_msg);
909 pop3_delete_send(session);
914 if (size_limit_over) {
916 (_("POP3: Skipping message %d (%d bytes)\n"),
917 session->cur_msg, size);
919 if (!msg->received && msg->partial_recv != 2) {
920 pop3_top_send(session, ac->size_limit);
922 } else if (msg->partial_recv == 2) {
927 if (size == 0 || msg->received || size_limit_over) {
928 session->cur_total_bytes += size;
929 if (session->cur_msg == session->count) {
930 pop3_logout_send(session);
938 pop3_retr_send(session);
942 static Pop3ErrorValue pop3_ok(Pop3Session *session, const gchar *msg)
946 log_print("POP3< %s\n", msg);
948 if (!strncmp(msg, "+OK", 3))
950 else if (!strncmp(msg, "-ERR", 4)) {
951 if (strstr(msg + 4, "lock") ||
952 strstr(msg + 4, "Lock") ||
953 strstr(msg + 4, "LOCK") ||
954 strstr(msg + 4, "wait")) {
955 log_warning(_("mailbox is locked\n"));
957 } else if (strcasestr(msg + 4, "timeout")) {
958 log_warning(_("session timeout\n"));
961 switch (session->state) {
964 log_warning(_("can't start TLS session\n"));
968 case POP3_GETAUTH_USER:
969 case POP3_GETAUTH_PASS:
970 case POP3_GETAUTH_APOP:
971 log_warning(_("error occurred on authentication\n"));
974 case POP3_GETRANGE_LAST:
975 case POP3_GETRANGE_UIDL:
977 log_warning(_("command not supported\n"));
978 ok = PS_NOTSUPPORTED;
982 log_warning(_("error occurred on POP3 session\n"));
987 g_free(session->error_msg);
988 session->error_msg = g_strdup(msg);
989 fprintf(stderr, "POP3: %s\n", msg);
993 session->error_val = ok;
997 static gint pop3_session_recv_msg(Session *session, const gchar *msg)
999 Pop3Session *pop3_session = POP3_SESSION(session);
1000 Pop3ErrorValue val = PS_SUCCESS;
1004 if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
1005 pop3_session->state != POP3_GETSIZE_LIST_RECV) {
1006 val = pop3_ok(pop3_session, msg);
1007 if (val != PS_SUCCESS) {
1008 if (val != PS_NOTSUPPORTED) {
1009 pop3_session->state = POP3_ERROR;
1014 if (*body == '+' || *body == '-')
1016 while (isalpha(*body))
1018 while (isspace(*body))
1022 switch (pop3_session->state) {
1025 pop3_greeting_recv(pop3_session, body);
1027 if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
1028 pop3_stls_send(pop3_session);
1031 if (pop3_session->ac_prefs->protocol == A_APOP)
1032 pop3_getauth_apop_send(pop3_session);
1034 pop3_getauth_user_send(pop3_session);
1038 if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
1040 if (pop3_session->ac_prefs->protocol == A_APOP)
1041 pop3_getauth_apop_send(pop3_session);
1043 pop3_getauth_user_send(pop3_session);
1046 case POP3_GETAUTH_USER:
1047 pop3_getauth_pass_send(pop3_session);
1049 case POP3_GETAUTH_PASS:
1050 case POP3_GETAUTH_APOP:
1051 if (!pop3_session->pop_before_smtp)
1052 pop3_getrange_stat_send(pop3_session);
1054 pop3_logout_send(pop3_session);
1056 case POP3_GETRANGE_STAT:
1057 if (pop3_getrange_stat_recv(pop3_session, body) < 0)
1059 if (pop3_session->count > 0)
1060 pop3_getrange_uidl_send(pop3_session);
1062 pop3_logout_send(pop3_session);
1064 case POP3_GETRANGE_LAST:
1065 if (val == PS_NOTSUPPORTED)
1066 pop3_session->error_val = PS_SUCCESS;
1067 else if (pop3_getrange_last_recv(pop3_session, body) < 0)
1069 if (pop3_session->cur_msg > 0)
1070 pop3_getsize_list_send(pop3_session);
1072 pop3_logout_send(pop3_session);
1074 case POP3_GETRANGE_UIDL:
1075 if (val == PS_NOTSUPPORTED) {
1076 pop3_session->error_val = PS_SUCCESS;
1077 pop3_getrange_last_send(pop3_session);
1079 pop3_session->state = POP3_GETRANGE_UIDL_RECV;
1080 session_recv_data(session, 0, ".\r\n");
1083 case POP3_GETSIZE_LIST:
1084 pop3_session->state = POP3_GETSIZE_LIST_RECV;
1085 session_recv_data(session, 0, ".\r\n");
1088 pop3_session->state = POP3_RETR_RECV;
1089 session_recv_data(session, 0, ".\r\n");
1092 if (val == PS_NOTSUPPORTED) {
1093 pop3_session->error_val = PS_SUCCESS;
1095 pop3_session->state = POP3_TOP_RECV;
1096 session_recv_data(session, 0, ".\r\n");
1100 pop3_delete_recv(pop3_session);
1101 if (pop3_session->cur_msg == pop3_session->count)
1102 pop3_logout_send(pop3_session);
1104 pop3_session->cur_msg++;
1105 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1110 session_disconnect(session);
1120 static gint pop3_session_recv_data_finished(Session *session, guchar *data,
1123 Pop3Session *pop3_session = POP3_SESSION(session);
1124 Pop3ErrorValue val = PS_SUCCESS;
1126 switch (pop3_session->state) {
1127 case POP3_GETRANGE_UIDL_RECV:
1128 val = pop3_getrange_uidl_recv(pop3_session, data, len);
1129 if (val == PS_SUCCESS) {
1130 if (pop3_session->new_msg_exist)
1131 pop3_getsize_list_send(pop3_session);
1133 pop3_logout_send(pop3_session);
1137 case POP3_GETSIZE_LIST_RECV:
1138 val = pop3_getsize_list_recv(pop3_session, data, len);
1139 if (val == PS_SUCCESS) {
1140 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1145 case POP3_RETR_RECV:
1146 if (pop3_retr_recv(pop3_session, data, len) < 0)
1149 if (pop3_session->ac_prefs->rmmail &&
1150 pop3_session->ac_prefs->msg_leave_time == 0 &&
1151 pop3_session->msg[pop3_session->cur_msg].recv_time
1153 pop3_delete_send(pop3_session);
1154 else if (pop3_session->cur_msg == pop3_session->count)
1155 pop3_logout_send(pop3_session);
1157 pop3_session->cur_msg++;
1158 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1163 if (pop3_top_recv(pop3_session, data, len) < 0)
1166 if (pop3_session->cur_msg == pop3_session->count)
1167 pop3_logout_send(pop3_session);
1169 pop3_session->cur_msg++;
1170 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1175 log_warning(_("TOP command unsupported\n"));
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)