2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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"
44 static gint pop3_greeting_recv (Pop3Session *session,
46 static gint pop3_getauth_user_send (Pop3Session *session);
47 static gint pop3_getauth_pass_send (Pop3Session *session);
48 static gint pop3_getauth_apop_send (Pop3Session *session);
50 static gint pop3_stls_send (Pop3Session *session);
51 static gint pop3_stls_recv (Pop3Session *session);
53 static gint pop3_getrange_stat_send (Pop3Session *session);
54 static gint pop3_getrange_stat_recv (Pop3Session *session,
56 static gint pop3_getrange_last_send (Pop3Session *session);
57 static gint pop3_getrange_last_recv (Pop3Session *session,
59 static gint pop3_getrange_uidl_send (Pop3Session *session);
60 static gint pop3_getrange_uidl_recv (Pop3Session *session,
63 static gint pop3_getsize_list_send (Pop3Session *session);
64 static gint pop3_getsize_list_recv (Pop3Session *session,
67 static gint pop3_retr_send (Pop3Session *session);
68 static gint pop3_retr_recv (Pop3Session *session,
71 static gint pop3_delete_send (Pop3Session *session);
72 static gint pop3_delete_recv (Pop3Session *session);
73 static gint pop3_logout_send (Pop3Session *session);
75 static void pop3_gen_send (Pop3Session *session,
76 const gchar *format, ...);
78 static void pop3_session_destroy (Session *session);
80 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,
95 static gint pop3_greeting_recv(Pop3Session *session, const gchar *msg)
97 session->state = POP3_GREETING;
99 session->greeting = g_strdup(msg);
104 static gint pop3_stls_send(Pop3Session *session)
106 session->state = POP3_STLS;
107 pop3_gen_send(session, "STLS");
111 static gint pop3_stls_recv(Pop3Session *session)
113 if (session_start_tls(SESSION(session)) < 0) {
114 session->error_val = PS_SOCKET;
119 #endif /* USE_OPENSSL */
121 static gint pop3_getauth_user_send(Pop3Session *session)
123 g_return_val_if_fail(session->user != NULL, -1);
125 session->state = POP3_GETAUTH_USER;
126 pop3_gen_send(session, "USER %s", session->user);
130 static gint pop3_getauth_pass_send(Pop3Session *session)
132 g_return_val_if_fail(session->pass != NULL, -1);
134 session->state = POP3_GETAUTH_PASS;
135 pop3_gen_send(session, "PASS %s", session->pass);
139 static gint pop3_getauth_apop_send(Pop3Session *session)
145 g_return_val_if_fail(session->user != NULL, -1);
146 g_return_val_if_fail(session->pass != NULL, -1);
148 session->state = POP3_GETAUTH_APOP;
150 if ((start = strchr(session->greeting, '<')) == NULL) {
151 log_warning(_("Required APOP timestamp not found "
153 session->error_val = PS_PROTOCOL;
157 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
158 log_warning(_("Timestamp syntax error in greeting\n"));
159 session->error_val = PS_PROTOCOL;
165 apop_str = g_strconcat(start, session->pass, NULL);
166 md5_hex_digest(md5sum, apop_str);
169 pop3_gen_send(session, "APOP %s %s", session->user, md5sum);
174 static gint pop3_getrange_stat_send(Pop3Session *session)
176 session->state = POP3_GETRANGE_STAT;
177 pop3_gen_send(session, "STAT");
181 static gint pop3_getrange_stat_recv(Pop3Session *session, const gchar *msg)
183 if (sscanf(msg, "%d %d", &session->count, &session->total_bytes) != 2) {
184 log_warning(_("POP3 protocol error\n"));
185 session->error_val = PS_PROTOCOL;
188 if (session->count == 0) {
189 session->uidl_is_valid = TRUE;
191 session->msg = g_new0(Pop3MsgInfo, session->count + 1);
192 session->cur_msg = 1;
199 static gint pop3_getrange_last_send(Pop3Session *session)
201 session->state = POP3_GETRANGE_LAST;
202 pop3_gen_send(session, "LAST");
206 static gint pop3_getrange_last_recv(Pop3Session *session, const gchar *msg)
210 if (sscanf(msg, "%d", &last) == 0) {
211 log_warning(_("POP3 protocol error\n"));
212 session->error_val = PS_PROTOCOL;
215 if (session->count > last) {
216 session->new_msg_exist = TRUE;
217 session->cur_msg = last + 1;
219 session->cur_msg = 0;
225 static gint pop3_getrange_uidl_send(Pop3Session *session)
227 session->state = POP3_GETRANGE_UIDL;
228 pop3_gen_send(session, "UIDL");
232 static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *data,
236 gchar buf[POPBUFSIZE];
240 const gchar *p = data;
241 const gchar *lastp = data + len;
242 const gchar *newline;
245 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
247 buf_len = MIN(newline - p, sizeof(buf) - 1);
248 memcpy(buf, p, buf_len);
252 if (p < lastp && *p == '\n') p++;
254 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
256 if (num <= 0 || num > session->count)
259 session->msg[num].uidl = g_strdup(id);
261 recv_time = (time_t)g_hash_table_lookup(session->uidl_table, id);
262 session->msg[num].recv_time = recv_time;
264 if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE)
265 session->msg[num].received = TRUE;
267 if (!session->new_msg_exist &&
268 (session->ac_prefs->getall || recv_time == RECV_TIME_NONE ||
269 session->ac_prefs->rmmail)) {
270 session->cur_msg = num;
271 session->new_msg_exist = TRUE;
275 session->uidl_is_valid = TRUE;
279 static gint pop3_getsize_list_send(Pop3Session *session)
281 session->state = POP3_GETSIZE_LIST;
282 pop3_gen_send(session, "LIST");
286 static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *data,
289 gchar buf[POPBUFSIZE];
292 const gchar *p = data;
293 const gchar *lastp = data + len;
294 const gchar *newline;
297 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
299 buf_len = MIN(newline - p, sizeof(buf) - 1);
300 memcpy(buf, p, buf_len);
304 if (p < lastp && *p == '\n') p++;
306 if (sscanf(buf, "%u %u", &num, &size) != 2) {
307 session->error_val = PS_PROTOCOL;
311 if (num > 0 && num <= session->count)
312 session->msg[num].size = size;
313 if (num > 0 && num < session->cur_msg)
314 session->cur_total_bytes += size;
320 static gint pop3_retr_send(Pop3Session *session)
322 session->state = POP3_RETR;
323 pop3_gen_send(session, "RETR %d", session->cur_msg);
327 static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
331 MailReceiveData mail_receive_data;
333 mail_receive_data.session = session;
334 mail_receive_data.data = g_strndup(data, len);
335 hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
337 file = get_tmp_file();
338 if (pop3_write_msg_to_file(file, mail_receive_data.data,
339 strlen(mail_receive_data.data)) < 0) {
341 g_free(mail_receive_data.data);
342 session->error_val = PS_IOERR;
345 g_free(mail_receive_data.data);
347 /* drop_ok: 0: success 1: don't receive -1: error */
348 drop_ok = inc_drop_message(file, session);
351 session->error_val = PS_IOERR;
355 session->cur_total_bytes += session->msg[session->cur_msg].size;
356 session->cur_total_recv_bytes += session->msg[session->cur_msg].size;
357 session->cur_total_num++;
359 session->msg[session->cur_msg].received = TRUE;
360 session->msg[session->cur_msg].recv_time =
361 drop_ok == 1 ? RECV_TIME_KEEP : session->current_time;
366 static gint pop3_delete_send(Pop3Session *session)
368 session->state = POP3_DELETE;
369 pop3_gen_send(session, "DELE %d", session->cur_msg);
373 static gint pop3_delete_recv(Pop3Session *session)
375 session->msg[session->cur_msg].deleted = TRUE;
379 static gint pop3_logout_send(Pop3Session *session)
381 session->state = POP3_LOGOUT;
382 pop3_gen_send(session, "QUIT");
386 static void pop3_gen_send(Pop3Session *session, const gchar *format, ...)
388 gchar buf[POPBUFSIZE + 1];
391 va_start(args, format);
392 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
395 if (!strncasecmp(buf, "PASS ", 5))
396 log_print("POP3> PASS ********\n");
398 log_print("POP3> %s\n", buf);
400 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
403 Session *pop3_session_new(PrefsAccount *account)
405 Pop3Session *session;
407 g_return_val_if_fail(account != NULL, NULL);
409 session = g_new0(Pop3Session, 1);
411 session_init(SESSION(session));
413 SESSION(session)->type = SESSION_POP3;
415 SESSION(session)->recv_msg = pop3_session_recv_msg;
416 SESSION(session)->recv_data_finished = pop3_session_recv_data_finished;
417 SESSION(session)->send_data_finished = NULL;
419 SESSION(session)->destroy = pop3_session_destroy;
421 session->state = POP3_READY;
422 session->ac_prefs = account;
423 session->uidl_table = pop3_get_uidl_table(account);
424 session->current_time = time(NULL);
425 session->error_val = PS_SUCCESS;
426 session->error_msg = NULL;
428 return SESSION(session);
431 static void pop3_session_destroy(Session *session)
433 Pop3Session *pop3_session = POP3_SESSION(session);
436 g_return_if_fail(session != NULL);
438 for (n = 1; n <= pop3_session->count; n++)
439 g_free(pop3_session->msg[n].uidl);
440 g_free(pop3_session->msg);
442 if (pop3_session->uidl_table) {
443 hash_free_strings(pop3_session->uidl_table);
444 g_hash_table_destroy(pop3_session->uidl_table);
447 g_free(pop3_session->greeting);
448 g_free(pop3_session->user);
449 g_free(pop3_session->pass);
450 g_free(pop3_session->error_msg);
453 GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
458 gchar buf[POPBUFSIZE];
459 gchar uidl[POPBUFSIZE];
463 table = g_hash_table_new(g_str_hash, g_str_equal);
465 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
466 "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
467 "-", ac_prefs->userid, NULL);
468 if ((fp = fopen(path, "rb")) == NULL) {
469 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
471 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
472 "uidl-", ac_prefs->recv_server,
473 "-", ac_prefs->userid, NULL);
474 if ((fp = fopen(path, "rb")) == NULL) {
475 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
484 while (fgets(buf, sizeof(buf), fp) != NULL) {
486 recv_time = RECV_TIME_NONE;
487 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
488 if (sscanf(buf, "%s", uidl) != 1)
493 if (recv_time == RECV_TIME_NONE)
494 recv_time = RECV_TIME_RECEIVED;
495 g_hash_table_insert(table, g_strdup(uidl),
496 GINT_TO_POINTER(recv_time));
503 gint pop3_write_uidl_list(Pop3Session *session)
510 if (!session->uidl_is_valid) return 0;
512 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
513 "uidl", G_DIR_SEPARATOR_S,
514 session->ac_prefs->recv_server,
515 "-", session->ac_prefs->userid, NULL);
516 if ((fp = fopen(path, "wb")) == NULL) {
517 FILE_OP_ERROR(path, "fopen");
522 for (n = 1; n <= session->count; n++) {
523 msg = &session->msg[n];
524 if (msg->uidl && msg->received && !msg->deleted)
525 fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
528 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
534 static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
538 const gchar *prev, *cur;
540 g_return_val_if_fail(file != NULL, -1);
542 if ((fp = fopen(file, "wb")) == NULL) {
543 FILE_OP_ERROR(file, "fopen");
547 if (change_file_mode_rw(fp, file) < 0)
548 FILE_OP_ERROR(file, "chmod");
550 /* +------------------+----------------+--------------------------+ *
551 * ^data ^prev ^cur data+len-1^ */
554 while ((cur = memchr(prev, '\r', len - (prev - data))) != NULL) {
555 if ((cur > prev && fwrite(prev, cur - prev, 1, fp) < 1) ||
556 fputc('\n', fp) == EOF) {
557 FILE_OP_ERROR(file, "fwrite");
558 g_warning("can't write to file: %s\n", file);
564 if (cur == data + len - 1) {
569 if (*(cur + 1) == '\n')
574 if (prev - data < len - 1 && *prev == '.' && *(prev + 1) == '.')
577 if (prev - data >= len)
581 if (prev - data < len &&
582 fwrite(prev, len - (prev - data), 1, fp) < 1) {
583 FILE_OP_ERROR(file, "fwrite");
584 g_warning("can't write to file: %s\n", file);
589 if (data[len - 1] != '\r' && data[len - 1] != '\n') {
590 if (fputc('\n', fp) == EOF) {
591 FILE_OP_ERROR(file, "fputc");
592 g_warning("can't write to file: %s\n", file);
599 if (fclose(fp) == EOF) {
600 FILE_OP_ERROR(file, "fclose");
608 static Pop3State pop3_lookup_next(Pop3Session *session)
611 PrefsAccount *ac = session->ac_prefs;
613 gboolean size_limit_over;
616 msg = &session->msg[session->cur_msg];
619 (ac->enable_size_limit &&
620 ac->size_limit > 0 &&
621 size > ac->size_limit * 1024);
624 msg->recv_time != RECV_TIME_NONE &&
625 msg->recv_time != RECV_TIME_KEEP &&
626 session->current_time - msg->recv_time >=
627 ac->msg_leave_time * 24 * 60 * 60) {
628 log_print(_("POP3: Deleting expired message %d\n"),
630 pop3_delete_send(session);
636 (_("POP3: Skipping message %d (%d bytes)\n"),
637 session->cur_msg, size);
639 if (size == 0 || msg->received || size_limit_over) {
640 session->cur_total_bytes += size;
641 if (session->cur_msg == session->count) {
642 pop3_logout_send(session);
650 pop3_retr_send(session);
654 static Pop3ErrorValue pop3_ok(Pop3Session *session, const gchar *msg)
658 log_print("POP3< %s\n", msg);
660 if (!strncmp(msg, "+OK", 3))
662 else if (!strncmp(msg, "-ERR", 4)) {
663 if (strstr(msg + 4, "lock") ||
664 strstr(msg + 4, "Lock") ||
665 strstr(msg + 4, "LOCK") ||
666 strstr(msg + 4, "wait")) {
667 log_warning(_("mailbox is locked\n"));
669 } else if (strcasestr(msg + 4, "timeout")) {
670 log_warning(_("session timeout\n"));
673 switch (session->state) {
676 log_warning(_("can't start TLS session\n"));
680 case POP3_GETAUTH_USER:
681 case POP3_GETAUTH_PASS:
682 case POP3_GETAUTH_APOP:
683 log_warning(_("error occurred on authentication\n"));
686 case POP3_GETRANGE_LAST:
687 case POP3_GETRANGE_UIDL:
688 log_warning(_("command not supported\n"));
689 ok = PS_NOTSUPPORTED;
692 log_warning(_("error occurred on POP3 session\n"));
697 g_free(session->error_msg);
698 session->error_msg = g_strdup(msg);
699 fprintf(stderr, "POP3: %s\n", msg);
703 session->error_val = ok;
707 static gint pop3_session_recv_msg(Session *session, const gchar *msg)
709 Pop3Session *pop3_session = POP3_SESSION(session);
710 Pop3ErrorValue val = PS_SUCCESS;
714 if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
715 pop3_session->state != POP3_GETSIZE_LIST_RECV) {
716 val = pop3_ok(pop3_session, msg);
717 if (val != PS_SUCCESS) {
718 if (val != PS_NOTSUPPORTED) {
719 pop3_session->state = POP3_ERROR;
724 if (*body == '+' || *body == '-')
726 while (isalpha(*body))
728 while (isspace(*body))
732 switch (pop3_session->state) {
735 pop3_greeting_recv(pop3_session, body);
737 if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
738 pop3_stls_send(pop3_session);
741 if (pop3_session->ac_prefs->protocol == A_APOP)
742 pop3_getauth_apop_send(pop3_session);
744 pop3_getauth_user_send(pop3_session);
748 if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
750 if (pop3_session->ac_prefs->protocol == A_APOP)
751 pop3_getauth_apop_send(pop3_session);
753 pop3_getauth_user_send(pop3_session);
756 case POP3_GETAUTH_USER:
757 pop3_getauth_pass_send(pop3_session);
759 case POP3_GETAUTH_PASS:
760 case POP3_GETAUTH_APOP:
761 pop3_getrange_stat_send(pop3_session);
763 case POP3_GETRANGE_STAT:
764 if (pop3_getrange_stat_recv(pop3_session, body) < 0)
766 if (pop3_session->count > 0)
767 pop3_getrange_uidl_send(pop3_session);
769 pop3_logout_send(pop3_session);
771 case POP3_GETRANGE_LAST:
772 if (val == PS_NOTSUPPORTED)
773 pop3_session->error_val = PS_SUCCESS;
774 else if (pop3_getrange_last_recv(pop3_session, body) < 0)
776 if (pop3_session->cur_msg > 0)
777 pop3_getsize_list_send(pop3_session);
779 pop3_logout_send(pop3_session);
781 case POP3_GETRANGE_UIDL:
782 if (val == PS_NOTSUPPORTED) {
783 pop3_session->error_val = PS_SUCCESS;
784 pop3_getrange_last_send(pop3_session);
786 pop3_session->state = POP3_GETRANGE_UIDL_RECV;
787 session_recv_data(session, 0, ".\r\n");
790 case POP3_GETSIZE_LIST:
791 pop3_session->state = POP3_GETSIZE_LIST_RECV;
792 session_recv_data(session, 0, ".\r\n");
795 pop3_session->state = POP3_RETR_RECV;
796 session_recv_data(session, 0, ".\r\n");
799 pop3_delete_recv(pop3_session);
800 if (pop3_session->cur_msg == pop3_session->count)
801 pop3_logout_send(pop3_session);
803 pop3_session->cur_msg++;
804 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
809 session_disconnect(session);
819 static gint pop3_session_recv_data_finished(Session *session, guchar *data,
822 Pop3Session *pop3_session = POP3_SESSION(session);
823 Pop3ErrorValue val = PS_SUCCESS;
825 switch (pop3_session->state) {
826 case POP3_GETRANGE_UIDL_RECV:
827 val = pop3_getrange_uidl_recv(pop3_session, data, len);
828 if (val == PS_SUCCESS) {
829 if (pop3_session->new_msg_exist)
830 pop3_getsize_list_send(pop3_session);
832 pop3_logout_send(pop3_session);
836 case POP3_GETSIZE_LIST_RECV:
837 val = pop3_getsize_list_recv(pop3_session, data, len);
838 if (val == PS_SUCCESS) {
839 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
845 if (pop3_retr_recv(pop3_session, data, len) < 0)
848 if (pop3_session->ac_prefs->rmmail &&
849 pop3_session->ac_prefs->msg_leave_time == 0 &&
850 pop3_session->msg[pop3_session->cur_msg].recv_time
852 pop3_delete_send(pop3_session);
853 else if (pop3_session->cur_msg == pop3_session->count)
854 pop3_logout_send(pop3_session);
856 pop3_session->cur_msg++;
857 if (pop3_lookup_next(pop3_session) == POP3_ERROR)