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"
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,
61 static gint pop3_getsize_list_send (Pop3Session *session);
62 static gint pop3_getsize_list_recv (Pop3Session *session,
64 static gint pop3_retr_send (Pop3Session *session);
65 static gint pop3_retr_recv (Pop3Session *session,
68 static gint pop3_delete_send (Pop3Session *session);
69 static gint pop3_delete_recv (Pop3Session *session);
70 static gint pop3_logout_send (Pop3Session *session);
72 static void pop3_gen_send (Pop3Session *session,
73 const gchar *format, ...);
75 static void pop3_session_destroy (Session *session);
77 static gint pop3_write_msg_to_file (const gchar *file,
81 static Pop3State pop3_lookup_next (Pop3Session *session);
82 static Pop3ErrorValue pop3_ok (Pop3Session *session,
85 static gint pop3_session_recv_msg (Session *session,
87 static gint pop3_session_recv_data_finished (Session *session,
91 static gint pop3_greeting_recv(Pop3Session *session, const gchar *msg)
93 session->state = POP3_GREETING;
95 session->greeting = g_strdup(msg);
100 static gint pop3_stls_send(Pop3Session *session)
102 session->state = POP3_STLS;
103 pop3_gen_send(session, "STLS");
107 static gint pop3_stls_recv(Pop3Session *session)
109 if (session_start_tls(SESSION(session)) < 0) {
110 session->error_val = PS_SOCKET;
115 #endif /* USE_OPENSSL */
117 static gint pop3_getauth_user_send(Pop3Session *session)
119 g_return_val_if_fail(session->user != NULL, -1);
121 session->state = POP3_GETAUTH_USER;
122 pop3_gen_send(session, "USER %s", session->user);
126 static gint pop3_getauth_pass_send(Pop3Session *session)
128 g_return_val_if_fail(session->pass != NULL, -1);
130 session->state = POP3_GETAUTH_PASS;
131 pop3_gen_send(session, "PASS %s", session->pass);
135 static gint pop3_getauth_apop_send(Pop3Session *session)
141 g_return_val_if_fail(session->user != NULL, -1);
142 g_return_val_if_fail(session->pass != NULL, -1);
144 session->state = POP3_GETAUTH_APOP;
146 if ((start = strchr(session->greeting, '<')) == NULL) {
147 log_warning(_("Required APOP timestamp not found "
149 session->error_val = PS_PROTOCOL;
153 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
154 log_warning(_("Timestamp syntax error in greeting\n"));
155 session->error_val = PS_PROTOCOL;
161 apop_str = g_strconcat(start, session->pass, NULL);
162 md5_hex_digest(md5sum, apop_str);
165 pop3_gen_send(session, "APOP %s %s", session->user, md5sum);
170 static gint pop3_getrange_stat_send(Pop3Session *session)
172 session->state = POP3_GETRANGE_STAT;
173 pop3_gen_send(session, "STAT");
177 static gint pop3_getrange_stat_recv(Pop3Session *session, const gchar *msg)
179 if (sscanf(msg, "%d %d", &session->count, &session->total_bytes) != 2) {
180 log_warning(_("POP3 protocol error\n"));
181 session->error_val = PS_PROTOCOL;
184 if (session->count == 0) {
185 session->uidl_is_valid = TRUE;
187 session->msg = g_new0(Pop3MsgInfo, session->count + 1);
188 session->cur_msg = 1;
195 static gint pop3_getrange_last_send(Pop3Session *session)
197 session->state = POP3_GETRANGE_LAST;
198 pop3_gen_send(session, "LAST");
202 static gint pop3_getrange_last_recv(Pop3Session *session, const gchar *msg)
206 if (sscanf(msg, "%d", &last) == 0) {
207 log_warning(_("POP3 protocol error\n"));
208 session->error_val = PS_PROTOCOL;
211 if (session->count > last)
212 session->cur_msg = last + 1;
214 session->cur_msg = 0;
220 static gint pop3_getrange_uidl_send(Pop3Session *session)
222 session->state = POP3_GETRANGE_UIDL;
223 pop3_gen_send(session, "UIDL");
227 static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *msg)
234 session->uidl_is_valid = TRUE;
238 if (sscanf(msg, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
240 if (num <= 0 || num > session->count)
243 session->msg[num].uidl = g_strdup(id);
245 if (!session->uidl_table)
248 recv_time = (time_t)g_hash_table_lookup(session->uidl_table, id);
249 session->msg[num].recv_time = recv_time;
251 if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE)
252 session->msg[num].received = TRUE;
254 if (!session->new_msg_exist &&
255 (session->ac_prefs->getall || recv_time == RECV_TIME_NONE ||
256 session->ac_prefs->rmmail)) {
257 session->cur_msg = num;
258 session->new_msg_exist = TRUE;
264 static gint pop3_getsize_list_send(Pop3Session *session)
266 session->state = POP3_GETSIZE_LIST;
267 pop3_gen_send(session, "LIST");
271 static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *msg)
279 if (sscanf(msg, "%u %u", &num, &size) != 2) {
280 session->error_val = PS_PROTOCOL;
284 if (num > 0 && num <= session->count)
285 session->msg[num].size = size;
286 if (num > 0 && num < session->cur_msg)
287 session->cur_total_bytes += size;
292 static gint pop3_retr_send(Pop3Session *session)
294 session->state = POP3_RETR;
295 pop3_gen_send(session, "RETR %d", session->cur_msg);
299 static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
304 file = get_tmp_file();
305 if (pop3_write_msg_to_file(file, data, len) < 0) {
307 session->error_val = PS_IOERR;
311 /* drop_ok: 0: success 1: don't receive -1: error */
312 drop_ok = inc_drop_message(file, session);
315 session->error_val = PS_IOERR;
319 session->cur_total_bytes += session->msg[session->cur_msg].size;
320 session->cur_total_recv_bytes += session->msg[session->cur_msg].size;
321 session->cur_total_num++;
323 session->msg[session->cur_msg].received = TRUE;
324 session->msg[session->cur_msg].recv_time =
325 drop_ok == 1 ? RECV_TIME_KEEP : session->current_time;
330 static gint pop3_delete_send(Pop3Session *session)
332 session->state = POP3_DELETE;
333 pop3_gen_send(session, "DELE %d", session->cur_msg);
337 static gint pop3_delete_recv(Pop3Session *session)
339 session->msg[session->cur_msg].deleted = TRUE;
343 static gint pop3_logout_send(Pop3Session *session)
345 session->state = POP3_LOGOUT;
346 pop3_gen_send(session, "QUIT");
350 static void pop3_gen_send(Pop3Session *session, const gchar *format, ...)
352 gchar buf[POPBUFSIZE + 1];
355 va_start(args, format);
356 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
359 if (!strncasecmp(buf, "PASS ", 5))
360 log_print("POP3> PASS ********\n");
362 log_print("POP3> %s\n", buf);
364 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
367 Session *pop3_session_new(PrefsAccount *account)
369 Pop3Session *session;
371 g_return_val_if_fail(account != NULL, NULL);
373 session = g_new0(Pop3Session, 1);
375 SESSION(session)->type = SESSION_POP3;
376 SESSION(session)->server = NULL;
377 SESSION(session)->port = 0;
378 SESSION(session)->sock = NULL;
379 SESSION(session)->state = SESSION_READY;
380 SESSION(session)->data = NULL;
382 SESSION(session)->recv_msg = pop3_session_recv_msg;
383 SESSION(session)->recv_data_finished = pop3_session_recv_data_finished;
384 SESSION(session)->send_data_finished = NULL;
386 SESSION(session)->destroy = pop3_session_destroy;
388 session->state = POP3_READY;
389 session->ac_prefs = account;
390 session->uidl_table = pop3_get_uidl_table(account);
391 session->current_time = time(NULL);
392 session->error_val = PS_SUCCESS;
394 return SESSION(session);
397 static void pop3_session_destroy(Session *session)
399 Pop3Session *pop3_session = POP3_SESSION(session);
402 g_return_if_fail(session != NULL);
404 for (n = 1; n <= pop3_session->count; n++)
405 g_free(pop3_session->msg[n].uidl);
406 g_free(pop3_session->msg);
408 if (pop3_session->uidl_table) {
409 hash_free_strings(pop3_session->uidl_table);
410 g_hash_table_destroy(pop3_session->uidl_table);
413 g_free(pop3_session->greeting);
414 g_free(pop3_session->user);
415 g_free(pop3_session->pass);
418 GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
423 gchar buf[POPBUFSIZE];
424 gchar uidl[POPBUFSIZE];
428 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
429 "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
430 "-", ac_prefs->userid, NULL);
431 if ((fp = fopen(path, "rb")) == NULL) {
432 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
434 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
435 "uidl-", ac_prefs->recv_server,
436 "-", ac_prefs->userid, NULL);
437 if ((fp = fopen(path, "rb")) == NULL) {
438 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
445 table = g_hash_table_new(g_str_hash, g_str_equal);
449 while (fgets(buf, sizeof(buf), fp) != NULL) {
451 recv_time = RECV_TIME_NONE;
452 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
453 if (sscanf(buf, "%s", uidl) != 1)
458 if (recv_time == RECV_TIME_NONE)
459 recv_time = RECV_TIME_RECEIVED;
460 g_hash_table_insert(table, g_strdup(uidl),
461 GINT_TO_POINTER(recv_time));
468 gint pop3_write_uidl_list(Pop3Session *session)
475 if (!session->uidl_is_valid) return 0;
477 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
478 "uidl", G_DIR_SEPARATOR_S,
479 session->ac_prefs->recv_server,
480 "-", session->ac_prefs->userid, NULL);
481 if ((fp = fopen(path, "wb")) == NULL) {
482 FILE_OP_ERROR(path, "fopen");
487 for (n = 1; n <= session->count; n++) {
488 msg = &session->msg[n];
489 if (msg->uidl && msg->received && !msg->deleted)
490 fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
493 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
499 static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
503 const gchar *prev, *cur;
505 g_return_val_if_fail(file != NULL, -1);
507 if ((fp = fopen(file, "wb")) == NULL) {
508 FILE_OP_ERROR(file, "fopen");
512 if (change_file_mode_rw(fp, file) < 0)
513 FILE_OP_ERROR(file, "chmod");
515 /* +------------------+----------------+--------------------------+ *
516 * ^data ^prev ^cur data+len-1^ */
519 while ((cur = memchr(prev, '\r', len - (prev - data))) != NULL) {
520 if ((cur > prev && fwrite(prev, cur - prev, 1, fp) < 1) ||
521 fputc('\n', fp) == EOF) {
522 FILE_OP_ERROR(file, "fwrite");
523 g_warning("can't write to file: %s\n", file);
529 if (cur == data + len - 1) {
534 if (*(cur + 1) == '\n')
539 if (prev - data >= len)
543 if (prev - data < len &&
544 fwrite(prev, len - (prev - data), 1, fp) < 1) {
545 FILE_OP_ERROR(file, "fwrite");
546 g_warning("can't write to file: %s\n", file);
551 if (data[len - 1] != '\r' && data[len - 1] != '\n') {
552 if (fputc('\n', fp) == EOF) {
553 FILE_OP_ERROR(file, "fputc");
554 g_warning("can't write to file: %s\n", file);
561 if (fclose(fp) == EOF) {
562 FILE_OP_ERROR(file, "fclose");
570 static Pop3State pop3_lookup_next(Pop3Session *session)
573 PrefsAccount *ac = session->ac_prefs;
575 gboolean size_limit_over;
578 msg = &session->msg[session->cur_msg];
581 (ac->enable_size_limit &&
582 ac->size_limit > 0 &&
583 size > ac->size_limit * 1024);
586 msg->recv_time != RECV_TIME_NONE &&
587 msg->recv_time != RECV_TIME_KEEP &&
588 session->current_time - msg->recv_time >=
589 ac->msg_leave_time * 24 * 60 * 60) {
590 log_print(_("POP3: Deleting expired message %d\n"),
592 pop3_delete_send(session);
598 (_("POP3: Skipping message %d (%d bytes)\n"),
599 session->cur_msg, size);
601 if (size == 0 || msg->received || size_limit_over) {
602 session->cur_total_bytes += size;
603 if (session->cur_msg == session->count) {
604 pop3_logout_send(session);
612 pop3_retr_send(session);
616 static Pop3ErrorValue pop3_ok(Pop3Session *session, const gchar *msg)
620 log_print("POP3< %s\n", msg);
622 if (!strncmp(msg, "+OK", 3))
624 else if (!strncmp(msg, "-ERR", 4)) {
625 if (strstr(msg + 4, "lock") ||
626 strstr(msg + 4, "Lock") ||
627 strstr(msg + 4, "LOCK") ||
628 strstr(msg + 4, "wait")) {
629 log_warning(_("mailbox is locked\n"));
632 switch (session->state) {
635 log_warning(_("can't start TLS session\n"));
639 case POP3_GETAUTH_USER:
640 case POP3_GETAUTH_PASS:
641 case POP3_GETAUTH_APOP:
642 log_warning(_("error occurred on authentication\n"));
646 log_warning(_("error occured on POP3 session\n"));
651 fprintf(stderr, "POP3: %s\n", msg);
655 session->error_val = ok;
659 static gint pop3_session_recv_msg(Session *session, const gchar *msg)
661 Pop3Session *pop3_session = POP3_SESSION(session);
666 if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
667 pop3_session->state != POP3_GETSIZE_LIST_RECV) {
668 val = pop3_ok(pop3_session, msg);
669 if (val != PS_SUCCESS) {
670 pop3_session->state = POP3_ERROR;
674 if (*body == '+' || *body == '-')
676 while (isalpha(*body))
678 while (isspace(*body))
682 switch (pop3_session->state) {
685 pop3_greeting_recv(pop3_session, body);
687 if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
688 pop3_stls_send(pop3_session);
691 if (pop3_session->ac_prefs->protocol == A_APOP)
692 pop3_getauth_apop_send(pop3_session);
694 pop3_getauth_user_send(pop3_session);
698 if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
700 if (pop3_session->ac_prefs->protocol == A_APOP)
701 pop3_getauth_apop_send(pop3_session);
703 pop3_getauth_user_send(pop3_session);
706 case POP3_GETAUTH_USER:
707 pop3_getauth_pass_send(pop3_session);
709 case POP3_GETAUTH_PASS:
710 case POP3_GETAUTH_APOP:
711 pop3_getrange_stat_send(pop3_session);
713 case POP3_GETRANGE_STAT:
714 if (pop3_getrange_stat_recv(pop3_session, body) < 0)
716 if (pop3_session->count > 0)
717 pop3_getrange_uidl_send(pop3_session);
719 pop3_logout_send(pop3_session);
721 case POP3_GETRANGE_LAST:
722 if (pop3_getrange_last_recv(pop3_session, body) < 0)
724 if (pop3_session->cur_msg > 0)
725 pop3_getsize_list_send(pop3_session);
727 pop3_logout_send(pop3_session);
729 case POP3_GETRANGE_UIDL:
730 pop3_session->state = POP3_GETRANGE_UIDL_RECV;
732 case POP3_GETRANGE_UIDL_RECV:
733 val = pop3_getrange_uidl_recv(pop3_session, body);
734 if (val == PS_CONTINUE)
736 else if (val == PS_SUCCESS) {
737 if (pop3_session->new_msg_exist)
738 pop3_getsize_list_send(pop3_session);
740 pop3_logout_send(pop3_session);
744 case POP3_GETSIZE_LIST:
745 pop3_session->state = POP3_GETSIZE_LIST_RECV;
747 case POP3_GETSIZE_LIST_RECV:
748 val = pop3_getsize_list_recv(pop3_session, body);
749 if (val == PS_CONTINUE)
751 else if (val == PS_SUCCESS) {
752 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
758 pop3_session->state = POP3_RETR_RECV;
761 pop3_session->msg[pop3_session->cur_msg].size, TRUE);
764 pop3_delete_recv(pop3_session);
765 if (pop3_session->cur_msg == pop3_session->count)
766 pop3_logout_send(pop3_session);
768 pop3_session->cur_msg++;
769 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
774 session_disconnect(session);
784 static gint pop3_session_recv_data_finished(Session *session, guchar *data,
787 Pop3Session *pop3_session = POP3_SESSION(session);
792 if (pop3_retr_recv(pop3_session, data, len) < 0)
795 if (pop3_session->ac_prefs->rmmail &&
796 pop3_session->ac_prefs->msg_leave_time == 0)
797 pop3_delete_send(pop3_session);
798 else if (pop3_session->cur_msg == pop3_session->count)
799 pop3_logout_send(pop3_session);
801 pop3_session->cur_msg++;
802 if (pop3_lookup_next(pop3_session) == POP3_ERROR)