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,
83 static Pop3State pop3_lookup_next (Pop3Session *session);
84 static Pop3ErrorValue pop3_ok (Pop3Session *session,
87 static gint pop3_session_recv_msg (Session *session,
89 static gint pop3_session_recv_data_finished (Session *session,
94 static gint pop3_greeting_recv(Pop3Session *session, const gchar *msg)
96 session->state = POP3_GREETING;
98 session->greeting = g_strdup(msg);
103 static gint pop3_stls_send(Pop3Session *session)
105 session->state = POP3_STLS;
106 pop3_gen_send(session, "STLS");
110 static gint pop3_stls_recv(Pop3Session *session)
112 if (session_start_tls(SESSION(session)) < 0) {
113 session->error_val = PS_SOCKET;
118 #endif /* USE_OPENSSL */
120 static gint pop3_getauth_user_send(Pop3Session *session)
122 g_return_val_if_fail(session->user != NULL, -1);
124 session->state = POP3_GETAUTH_USER;
125 pop3_gen_send(session, "USER %s", session->user);
129 static gint pop3_getauth_pass_send(Pop3Session *session)
131 g_return_val_if_fail(session->pass != NULL, -1);
133 session->state = POP3_GETAUTH_PASS;
134 pop3_gen_send(session, "PASS %s", session->pass);
138 static gint pop3_getauth_apop_send(Pop3Session *session)
144 g_return_val_if_fail(session->user != NULL, -1);
145 g_return_val_if_fail(session->pass != NULL, -1);
147 session->state = POP3_GETAUTH_APOP;
149 if ((start = strchr(session->greeting, '<')) == NULL) {
150 log_warning(_("Required APOP timestamp not found "
152 session->error_val = PS_PROTOCOL;
156 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
157 log_warning(_("Timestamp syntax error in greeting\n"));
158 session->error_val = PS_PROTOCOL;
164 apop_str = g_strconcat(start, session->pass, NULL);
165 md5_hex_digest(md5sum, apop_str);
168 pop3_gen_send(session, "APOP %s %s", session->user, md5sum);
173 static gint pop3_getrange_stat_send(Pop3Session *session)
175 session->state = POP3_GETRANGE_STAT;
176 pop3_gen_send(session, "STAT");
180 static gint pop3_getrange_stat_recv(Pop3Session *session, const gchar *msg)
182 if (sscanf(msg, "%d %d", &session->count, &session->total_bytes) != 2) {
183 log_warning(_("POP3 protocol error\n"));
184 session->error_val = PS_PROTOCOL;
187 if (session->count == 0) {
188 session->uidl_is_valid = TRUE;
190 session->msg = g_new0(Pop3MsgInfo, session->count + 1);
191 session->cur_msg = 1;
198 static gint pop3_getrange_last_send(Pop3Session *session)
200 session->state = POP3_GETRANGE_LAST;
201 pop3_gen_send(session, "LAST");
205 static gint pop3_getrange_last_recv(Pop3Session *session, const gchar *msg)
209 if (sscanf(msg, "%d", &last) == 0) {
210 log_warning(_("POP3 protocol error\n"));
211 session->error_val = PS_PROTOCOL;
214 if (session->count > last) {
215 session->new_msg_exist = TRUE;
216 session->cur_msg = last + 1;
218 session->cur_msg = 0;
224 static gint pop3_getrange_uidl_send(Pop3Session *session)
226 session->state = POP3_GETRANGE_UIDL;
227 pop3_gen_send(session, "UIDL");
231 static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *data,
235 gchar buf[POPBUFSIZE];
239 const gchar *p = data;
240 const gchar *lastp = data + len;
241 const gchar *newline;
244 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
246 buf_len = MIN(newline - p, sizeof(buf) - 1);
247 memcpy(buf, p, buf_len);
251 if (p < lastp && *p == '\n') p++;
253 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
255 if (num <= 0 || num > session->count)
258 session->msg[num].uidl = g_strdup(id);
260 recv_time = (time_t)g_hash_table_lookup(session->uidl_table, id);
261 session->msg[num].recv_time = recv_time;
263 if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE)
264 session->msg[num].received = TRUE;
266 if (!session->new_msg_exist &&
267 (session->ac_prefs->getall || recv_time == RECV_TIME_NONE ||
268 session->ac_prefs->rmmail)) {
269 session->cur_msg = num;
270 session->new_msg_exist = TRUE;
274 session->uidl_is_valid = TRUE;
278 static gint pop3_getsize_list_send(Pop3Session *session)
280 session->state = POP3_GETSIZE_LIST;
281 pop3_gen_send(session, "LIST");
285 static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *data,
288 gchar buf[POPBUFSIZE];
291 const gchar *p = data;
292 const gchar *lastp = data + len;
293 const gchar *newline;
296 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
298 buf_len = MIN(newline - p, sizeof(buf) - 1);
299 memcpy(buf, p, buf_len);
303 if (p < lastp && *p == '\n') p++;
305 if (sscanf(buf, "%u %u", &num, &size) != 2) {
306 session->error_val = PS_PROTOCOL;
310 if (num > 0 && num <= session->count)
311 session->msg[num].size = size;
312 if (num > 0 && num < session->cur_msg)
313 session->cur_total_bytes += size;
319 static gint pop3_retr_send(Pop3Session *session)
321 session->state = POP3_RETR;
322 pop3_gen_send(session, "RETR %d", session->cur_msg);
326 static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
330 MailReceiveData mail_receive_data;
332 mail_receive_data.session = session;
333 mail_receive_data.data = g_strndup(data, len);
334 hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
336 file = get_tmp_file();
337 if (pop3_write_msg_to_file(file, mail_receive_data.data,
338 strlen(mail_receive_data.data)) < 0) {
340 g_free(mail_receive_data.data);
341 session->error_val = PS_IOERR;
344 g_free(mail_receive_data.data);
346 /* drop_ok: 0: success 1: don't receive -1: error */
347 drop_ok = session->drop_message(session, file);
350 session->error_val = PS_IOERR;
354 session->cur_total_bytes += session->msg[session->cur_msg].size;
355 session->cur_total_recv_bytes += session->msg[session->cur_msg].size;
356 session->cur_total_num++;
358 session->msg[session->cur_msg].received = TRUE;
359 session->msg[session->cur_msg].recv_time =
360 drop_ok == 1 ? RECV_TIME_KEEP : session->current_time;
365 static gint pop3_delete_send(Pop3Session *session)
367 session->state = POP3_DELETE;
368 pop3_gen_send(session, "DELE %d", session->cur_msg);
372 static gint pop3_delete_recv(Pop3Session *session)
374 session->msg[session->cur_msg].deleted = TRUE;
378 static gint pop3_logout_send(Pop3Session *session)
380 session->state = POP3_LOGOUT;
381 pop3_gen_send(session, "QUIT");
385 static void pop3_gen_send(Pop3Session *session, const gchar *format, ...)
387 gchar buf[POPBUFSIZE + 1];
390 va_start(args, format);
391 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
394 if (!strncasecmp(buf, "PASS ", 5))
395 log_print("POP3> PASS ********\n");
397 log_print("POP3> %s\n", buf);
399 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
402 Session *pop3_session_new(PrefsAccount *account)
404 Pop3Session *session;
406 g_return_val_if_fail(account != NULL, NULL);
408 session = g_new0(Pop3Session, 1);
410 session_init(SESSION(session));
412 SESSION(session)->type = SESSION_POP3;
414 SESSION(session)->recv_msg = pop3_session_recv_msg;
415 SESSION(session)->recv_data_finished = pop3_session_recv_data_finished;
416 SESSION(session)->send_data_finished = NULL;
418 SESSION(session)->destroy = pop3_session_destroy;
420 session->state = POP3_READY;
421 session->ac_prefs = account;
422 session->uidl_table = pop3_get_uidl_table(account);
423 session->current_time = time(NULL);
424 session->error_val = PS_SUCCESS;
425 session->error_msg = NULL;
427 return SESSION(session);
430 static void pop3_session_destroy(Session *session)
432 Pop3Session *pop3_session = POP3_SESSION(session);
435 g_return_if_fail(session != NULL);
437 for (n = 1; n <= pop3_session->count; n++)
438 g_free(pop3_session->msg[n].uidl);
439 g_free(pop3_session->msg);
441 if (pop3_session->uidl_table) {
442 hash_free_strings(pop3_session->uidl_table);
443 g_hash_table_destroy(pop3_session->uidl_table);
446 g_free(pop3_session->greeting);
447 g_free(pop3_session->user);
448 g_free(pop3_session->pass);
449 g_free(pop3_session->error_msg);
452 GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
457 gchar buf[POPBUFSIZE];
458 gchar uidl[POPBUFSIZE];
462 table = g_hash_table_new(g_str_hash, g_str_equal);
464 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
465 "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
466 "-", ac_prefs->userid, NULL);
467 if ((fp = fopen(path, "rb")) == NULL) {
468 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
470 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
471 "uidl-", ac_prefs->recv_server,
472 "-", ac_prefs->userid, NULL);
473 if ((fp = fopen(path, "rb")) == NULL) {
474 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
483 while (fgets(buf, sizeof(buf), fp) != NULL) {
485 recv_time = RECV_TIME_NONE;
486 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
487 if (sscanf(buf, "%s", uidl) != 1)
492 if (recv_time == RECV_TIME_NONE)
493 recv_time = RECV_TIME_RECEIVED;
494 g_hash_table_insert(table, g_strdup(uidl),
495 GINT_TO_POINTER(recv_time));
502 gint pop3_write_uidl_list(Pop3Session *session)
509 if (!session->uidl_is_valid) return 0;
511 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
512 "uidl", G_DIR_SEPARATOR_S,
513 session->ac_prefs->recv_server,
514 "-", session->ac_prefs->userid, NULL);
515 if ((fp = fopen(path, "wb")) == NULL) {
516 FILE_OP_ERROR(path, "fopen");
521 for (n = 1; n <= session->count; n++) {
522 msg = &session->msg[n];
523 if (msg->uidl && msg->received && !msg->deleted)
524 fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
527 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
533 static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
537 const gchar *prev, *cur;
539 g_return_val_if_fail(file != NULL, -1);
541 if ((fp = fopen(file, "wb")) == NULL) {
542 FILE_OP_ERROR(file, "fopen");
546 if (change_file_mode_rw(fp, file) < 0)
547 FILE_OP_ERROR(file, "chmod");
549 /* +------------------+----------------+--------------------------+ *
550 * ^data ^prev ^cur data+len-1^ */
553 while ((cur = memchr(prev, '\r', len - (prev - data))) != NULL) {
554 if ((cur > prev && fwrite(prev, cur - prev, 1, fp) < 1) ||
555 fputc('\n', fp) == EOF) {
556 FILE_OP_ERROR(file, "fwrite");
557 g_warning("can't write to file: %s\n", file);
563 if (cur == data + len - 1) {
568 if (*(cur + 1) == '\n')
573 if (prev - data < len - 1 && *prev == '.' && *(prev + 1) == '.')
576 if (prev - data >= len)
580 if (prev - data < len &&
581 fwrite(prev, len - (prev - data), 1, fp) < 1) {
582 FILE_OP_ERROR(file, "fwrite");
583 g_warning("can't write to file: %s\n", file);
588 if (data[len - 1] != '\r' && data[len - 1] != '\n') {
589 if (fputc('\n', fp) == EOF) {
590 FILE_OP_ERROR(file, "fputc");
591 g_warning("can't write to file: %s\n", file);
598 if (fclose(fp) == EOF) {
599 FILE_OP_ERROR(file, "fclose");
607 static Pop3State pop3_lookup_next(Pop3Session *session)
610 PrefsAccount *ac = session->ac_prefs;
612 gboolean size_limit_over;
615 msg = &session->msg[session->cur_msg];
618 (ac->enable_size_limit &&
619 ac->size_limit > 0 &&
620 size > ac->size_limit * 1024);
623 msg->recv_time != RECV_TIME_NONE &&
624 msg->recv_time != RECV_TIME_KEEP &&
625 session->current_time - msg->recv_time >=
626 ac->msg_leave_time * 24 * 60 * 60) {
628 (_("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)