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 gint 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)
235 session->uidl_is_valid = TRUE;
239 if (sscanf(msg, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
241 if (num <= 0 || num > session->count)
244 session->msg[num].uidl = g_strdup(id);
246 if (!session->uidl_table)
249 recv_time = (time_t)g_hash_table_lookup(session->uidl_table, id);
250 session->msg[num].recv_time = recv_time;
252 if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE)
253 session->msg[num].received = TRUE;
255 if (!session->new_msg_exist &&
256 (session->ac_prefs->getall || recv_time == RECV_TIME_NONE ||
257 session->ac_prefs->rmmail)) {
258 session->cur_msg = num;
259 session->new_msg_exist = TRUE;
265 static gint pop3_getsize_list_send(Pop3Session *session)
267 session->state = POP3_GETSIZE_LIST;
268 pop3_gen_send(session, "LIST");
272 static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *msg)
280 if (sscanf(msg, "%u %u", &num, &size) != 2) {
281 session->error_val = PS_PROTOCOL;
285 if (num > 0 && num <= session->count)
286 session->msg[num].size = size;
287 if (num > 0 && num < session->cur_msg)
288 session->cur_total_bytes += size;
293 static gint pop3_retr_send(Pop3Session *session)
295 session->state = POP3_RETR;
296 pop3_gen_send(session, "RETR %d", session->cur_msg);
300 static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
306 file = get_tmp_file();
307 if (pop3_write_msg_to_file(file, data, len) < 0) {
309 session->error_val = PS_IOERR;
313 /* drop_ok: 0: success 1: don't receive -1: error */
314 drop_ok = inc_drop_message(file, session);
317 session->error_val = PS_ERROR;
321 session->cur_total_bytes += session->msg[session->cur_msg].size;
322 session->cur_total_recv_bytes += session->msg[session->cur_msg].size;
323 session->cur_total_num++;
325 session->msg[session->cur_msg].received = TRUE;
326 session->msg[session->cur_msg].recv_time =
327 drop_ok == 1 ? RECV_TIME_KEEP : session->current_time;
332 static gint pop3_delete_send(Pop3Session *session)
334 session->state = POP3_DELETE;
335 pop3_gen_send(session, "DELE %d", session->cur_msg);
339 static gint pop3_delete_recv(Pop3Session *session)
341 session->msg[session->cur_msg].deleted = TRUE;
345 static gint pop3_logout_send(Pop3Session *session)
347 session->state = POP3_LOGOUT;
348 pop3_gen_send(session, "QUIT");
352 static void pop3_gen_send(Pop3Session *session, const gchar *format, ...)
354 gchar buf[POPBUFSIZE + 1];
357 va_start(args, format);
358 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
361 if (!strncasecmp(buf, "PASS ", 5))
362 log_print("POP3> PASS ********\n");
364 log_print("POP3> %s\n", buf);
366 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
369 Session *pop3_session_new(PrefsAccount *account)
371 Pop3Session *session;
373 g_return_val_if_fail(account != NULL, NULL);
375 session = g_new0(Pop3Session, 1);
377 SESSION(session)->type = SESSION_POP3;
378 SESSION(session)->server = NULL;
379 SESSION(session)->port = 0;
380 SESSION(session)->sock = NULL;
381 SESSION(session)->state = SESSION_READY;
382 SESSION(session)->data = NULL;
384 SESSION(session)->recv_msg = pop3_session_recv_msg;
385 SESSION(session)->recv_data_finished = pop3_session_recv_data_finished;
386 SESSION(session)->send_data_finished = NULL;
388 SESSION(session)->destroy = pop3_session_destroy;
390 session->state = POP3_READY;
391 session->ac_prefs = account;
392 session->uidl_table = pop3_get_uidl_table(account);
393 session->current_time = time(NULL);
394 session->error_val = PS_SUCCESS;
396 return SESSION(session);
399 static void pop3_session_destroy(Session *session)
401 Pop3Session *pop3_session = POP3_SESSION(session);
404 g_return_if_fail(session != NULL);
406 for (n = 1; n <= pop3_session->count; n++)
407 g_free(pop3_session->msg[n].uidl);
408 g_free(pop3_session->msg);
410 if (pop3_session->uidl_table) {
411 hash_free_strings(pop3_session->uidl_table);
412 g_hash_table_destroy(pop3_session->uidl_table);
415 g_free(pop3_session->greeting);
416 g_free(pop3_session->user);
417 g_free(pop3_session->pass);
420 GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
425 gchar buf[POPBUFSIZE];
426 gchar uidl[POPBUFSIZE];
430 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
431 "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
432 "-", ac_prefs->userid, NULL);
433 if ((fp = fopen(path, "rb")) == NULL) {
434 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
436 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
437 "uidl-", ac_prefs->recv_server,
438 "-", ac_prefs->userid, NULL);
439 if ((fp = fopen(path, "rb")) == NULL) {
440 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
447 table = g_hash_table_new(g_str_hash, g_str_equal);
451 while (fgets(buf, sizeof(buf), fp) != NULL) {
453 recv_time = RECV_TIME_NONE;
454 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
455 if (sscanf(buf, "%s", uidl) != 1)
460 if (recv_time == RECV_TIME_NONE)
461 recv_time = RECV_TIME_RECEIVED;
462 g_hash_table_insert(table, g_strdup(uidl),
463 GINT_TO_POINTER(recv_time));
470 gint pop3_write_uidl_list(Pop3Session *session)
477 if (!session->uidl_is_valid) return 0;
479 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
480 "uidl", G_DIR_SEPARATOR_S,
481 session->ac_prefs->recv_server,
482 "-", session->ac_prefs->userid, NULL);
483 if ((fp = fopen(path, "wb")) == NULL) {
484 FILE_OP_ERROR(path, "fopen");
489 for (n = 1; n <= session->count; n++) {
490 msg = &session->msg[n];
491 if (msg->uidl && msg->received && !msg->deleted)
492 fprintf(fp, "%s\t%ld\n", msg->uidl, msg->recv_time);
495 if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
501 static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
505 const gchar *prev, *cur;
507 g_return_val_if_fail(file != NULL, -1);
509 if ((fp = fopen(file, "wb")) == NULL) {
510 FILE_OP_ERROR(file, "fopen");
514 if (change_file_mode_rw(fp, file) < 0)
515 FILE_OP_ERROR(file, "chmod");
517 /* +------------------+----------------+--------------------------+ *
518 * ^data ^prev ^cur data+len-1^ */
521 while ((cur = memchr(prev, '\r', len - (prev - data))) != NULL) {
522 if ((cur > prev && fwrite(prev, cur - prev, 1, fp) < 1) ||
523 fputc('\n', fp) == EOF) {
524 FILE_OP_ERROR(file, "fwrite");
525 g_warning("can't write to file: %s\n", file);
531 if (cur == data + len - 1) {
536 if (*(cur + 1) == '\n')
541 if (prev - data >= len)
545 if (prev - data < len &&
546 fwrite(prev, len - (prev - data), 1, fp) < 1) {
547 FILE_OP_ERROR(file, "fwrite");
548 g_warning("can't write to file: %s\n", file);
553 if (data[len - 1] != '\r' && data[len - 1] != '\n') {
554 if (fputc('\n', fp) == EOF) {
555 FILE_OP_ERROR(file, "fputc");
556 g_warning("can't write to file: %s\n", file);
563 if (fclose(fp) == EOF) {
564 FILE_OP_ERROR(file, "fclose");
572 static Pop3State pop3_lookup_next(Pop3Session *session)
575 PrefsAccount *ac = session->ac_prefs;
577 gboolean size_limit_over;
580 msg = &session->msg[session->cur_msg];
583 (ac->enable_size_limit &&
584 ac->size_limit > 0 &&
585 size > ac->size_limit * 1024);
588 msg->recv_time != RECV_TIME_NONE &&
589 msg->recv_time != RECV_TIME_KEEP &&
590 session->current_time - msg->recv_time >=
591 ac->msg_leave_time * 24 * 60 * 60) {
592 log_print(_("POP3: Deleting expired message %d\n"),
594 pop3_delete_send(session);
600 (_("POP3: Skipping message %d (%d bytes)\n"),
601 session->cur_msg, size);
603 if (size == 0 || msg->received || size_limit_over) {
604 session->cur_total_bytes += size;
605 if (session->cur_msg == session->count) {
606 pop3_logout_send(session);
614 pop3_retr_send(session);
618 static gint pop3_ok(Pop3Session *session, const gchar *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"))
633 fprintf(stderr, "POP3: %s\n", msg);
640 static gint pop3_session_recv_msg(Session *session, const gchar *msg)
642 Pop3Session *pop3_session = POP3_SESSION(session);
647 if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
648 pop3_session->state != POP3_GETSIZE_LIST_RECV) {
649 log_print("POP3< %s\n", msg);
650 val = pop3_ok(pop3_session, msg);
651 if (val != PS_SUCCESS) {
652 pop3_session->state = POP3_ERROR;
653 pop3_session->error_val = val;
657 if (*body == '+' || *body == '-')
659 while (isalpha(*body))
661 while (isspace(*body))
665 switch (pop3_session->state) {
668 pop3_greeting_recv(pop3_session, body);
670 if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
671 pop3_stls_send(pop3_session);
674 if (pop3_session->ac_prefs->protocol == A_APOP)
675 pop3_getauth_apop_send(pop3_session);
677 pop3_getauth_user_send(pop3_session);
681 if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
683 if (pop3_session->ac_prefs->protocol == A_APOP)
684 pop3_getauth_apop_send(pop3_session);
686 pop3_getauth_user_send(pop3_session);
689 case POP3_GETAUTH_USER:
690 pop3_getauth_pass_send(pop3_session);
692 case POP3_GETAUTH_PASS:
693 case POP3_GETAUTH_APOP:
694 pop3_getrange_stat_send(pop3_session);
696 case POP3_GETRANGE_STAT:
697 if (pop3_getrange_stat_recv(pop3_session, body) < 0)
699 if (pop3_session->count > 0)
700 pop3_getrange_uidl_send(pop3_session);
702 pop3_logout_send(pop3_session);
704 case POP3_GETRANGE_LAST:
705 if (pop3_getrange_last_recv(pop3_session, body) < 0)
707 if (pop3_session->cur_msg > 0)
708 pop3_getsize_list_send(pop3_session);
710 pop3_logout_send(pop3_session);
712 case POP3_GETRANGE_UIDL:
713 pop3_session->state = POP3_GETRANGE_UIDL_RECV;
715 case POP3_GETRANGE_UIDL_RECV:
716 val = pop3_getrange_uidl_recv(pop3_session, body);
717 if (val == PS_CONTINUE)
719 else if (val == PS_SUCCESS) {
720 if (pop3_session->new_msg_exist)
721 pop3_getsize_list_send(pop3_session);
723 pop3_logout_send(pop3_session);
727 case POP3_GETSIZE_LIST:
728 pop3_session->state = POP3_GETSIZE_LIST_RECV;
730 case POP3_GETSIZE_LIST_RECV:
731 val = pop3_getsize_list_recv(pop3_session, body);
732 if (val == PS_CONTINUE)
734 else if (val == PS_SUCCESS) {
735 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
741 pop3_session->state = POP3_RETR_RECV;
744 pop3_session->msg[pop3_session->cur_msg].size, TRUE);
747 pop3_delete_recv(pop3_session);
748 if (pop3_session->cur_msg == pop3_session->count)
749 pop3_logout_send(pop3_session);
751 pop3_session->cur_msg++;
752 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
757 session_disconnect(session);
767 static gint pop3_session_recv_data_finished(Session *session, guchar *data,
770 Pop3Session *pop3_session = POP3_SESSION(session);
775 if (pop3_retr_recv(pop3_session, data, len) < 0)
778 if (pop3_session->ac_prefs->rmmail &&
779 pop3_session->ac_prefs->msg_leave_time == 0)
780 pop3_delete_send(pop3_session);
781 else if (pop3_session->cur_msg == pop3_session->count)
782 pop3_logout_send(pop3_session);
784 pop3_session->cur_msg++;
785 if (pop3_lookup_next(pop3_session) == POP3_ERROR)