ef738a70a372c2eda547c71dd5c74388efdcd897
[claws.git] / src / pop.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <errno.h>
32
33 #include "intl.h"
34 #include "pop.h"
35 #include "md5.h"
36 #include "prefs_account.h"
37 #include "utils.h"
38 #include "inc.h"
39 #include "recv.h"
40
41 #include "log.h"
42
43 static gint pop3_greeting_recv          (Pop3Session *session,
44                                          const gchar *msg);
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);
48 #if USE_OPENSSL
49 static gint pop3_stls_send              (Pop3Session *session);
50 static gint pop3_stls_recv              (Pop3Session *session);
51 #endif
52 static gint pop3_getrange_stat_send     (Pop3Session *session);
53 static gint pop3_getrange_stat_recv     (Pop3Session *session,
54                                          const gchar *msg);
55 static gint pop3_getrange_last_send     (Pop3Session *session);
56 static gint pop3_getrange_last_recv     (Pop3Session *session,
57                                          const gchar *msg);
58 static gint pop3_getrange_uidl_send     (Pop3Session *session);
59 static gint pop3_getrange_uidl_recv     (Pop3Session *session,
60                                          const gchar *msg);
61 static gint pop3_getsize_list_send      (Pop3Session *session);
62 static gint pop3_getsize_list_recv      (Pop3Session *session,
63                                          const gchar *msg);
64 static gint pop3_retr_send              (Pop3Session *session);
65 static gint pop3_retr_recv              (Pop3Session *session,
66                                          const gchar *data,
67                                          guint        len);
68 static gint pop3_delete_send            (Pop3Session *session);
69 static gint pop3_delete_recv            (Pop3Session *session);
70 static gint pop3_logout_send            (Pop3Session *session);
71
72 static void pop3_gen_send               (Pop3Session    *session,
73                                          const gchar    *format, ...);
74
75 static void pop3_session_destroy        (Session        *session);
76
77 static gint pop3_write_msg_to_file      (const gchar    *file,
78                                          const gchar    *data,
79                                          guint           len);
80
81 static Pop3State pop3_lookup_next       (Pop3Session    *session);
82 static Pop3ErrorValue pop3_ok           (Pop3Session    *session,
83                                          const gchar    *msg);
84
85 static gint pop3_session_recv_msg               (Session        *session,
86                                                  const gchar    *msg);
87 static gint pop3_session_recv_data_finished     (Session        *session,
88                                                  guchar         *data,
89                                                  guint           len);
90
91 static gint pop3_greeting_recv(Pop3Session *session, const gchar *msg)
92 {
93         session->state = POP3_GREETING;
94
95         session->greeting = g_strdup(msg);
96         return PS_SUCCESS;
97 }
98
99 #if USE_OPENSSL
100 static gint pop3_stls_send(Pop3Session *session)
101 {
102         session->state = POP3_STLS;
103         pop3_gen_send(session, "STLS");
104         return PS_SUCCESS;
105 }
106
107 static gint pop3_stls_recv(Pop3Session *session)
108 {
109         if (session_start_tls(SESSION(session)) < 0) {
110                 session->error_val = PS_SOCKET;
111                 return -1;
112         }
113         return PS_SUCCESS;
114 }
115 #endif /* USE_OPENSSL */
116
117 static gint pop3_getauth_user_send(Pop3Session *session)
118 {
119         g_return_val_if_fail(session->user != NULL, -1);
120
121         session->state = POP3_GETAUTH_USER;
122         pop3_gen_send(session, "USER %s", session->user);
123         return PS_SUCCESS;
124 }
125
126 static gint pop3_getauth_pass_send(Pop3Session *session)
127 {
128         g_return_val_if_fail(session->pass != NULL, -1);
129
130         session->state = POP3_GETAUTH_PASS;
131         pop3_gen_send(session, "PASS %s", session->pass);
132         return PS_SUCCESS;
133 }
134
135 static gint pop3_getauth_apop_send(Pop3Session *session)
136 {
137         gchar *start, *end;
138         gchar *apop_str;
139         gchar md5sum[33];
140
141         g_return_val_if_fail(session->user != NULL, -1);
142         g_return_val_if_fail(session->pass != NULL, -1);
143
144         session->state = POP3_GETAUTH_APOP;
145
146         if ((start = strchr(session->greeting, '<')) == NULL) {
147                 log_warning(_("Required APOP timestamp not found "
148                               "in greeting\n"));
149                 session->error_val = PS_PROTOCOL;
150                 return -1;
151         }
152
153         if ((end = strchr(start, '>')) == NULL || end == start + 1) {
154                 log_warning(_("Timestamp syntax error in greeting\n"));
155                 session->error_val = PS_PROTOCOL;
156                 return -1;
157         }
158
159         *(end + 1) = '\0';
160
161         apop_str = g_strconcat(start, session->pass, NULL);
162         md5_hex_digest(md5sum, apop_str);
163         g_free(apop_str);
164
165         pop3_gen_send(session, "APOP %s %s", session->user, md5sum);
166
167         return PS_SUCCESS;
168 }
169
170 static gint pop3_getrange_stat_send(Pop3Session *session)
171 {
172         session->state = POP3_GETRANGE_STAT;
173         pop3_gen_send(session, "STAT");
174         return PS_SUCCESS;
175 }
176
177 static gint pop3_getrange_stat_recv(Pop3Session *session, const gchar *msg)
178 {
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;
182                 return -1;
183         } else {
184                 if (session->count == 0) {
185                         session->uidl_is_valid = TRUE;
186                 } else {
187                         session->msg = g_new0(Pop3MsgInfo, session->count + 1);
188                         session->cur_msg = 1;
189                 }
190         }
191
192         return PS_SUCCESS;
193 }
194
195 static gint pop3_getrange_last_send(Pop3Session *session)
196 {
197         session->state = POP3_GETRANGE_LAST;
198         pop3_gen_send(session, "LAST");
199         return PS_SUCCESS;
200 }
201
202 static gint pop3_getrange_last_recv(Pop3Session *session, const gchar *msg)
203 {
204         gint last;
205
206         if (sscanf(msg, "%d", &last) == 0) {
207                 log_warning(_("POP3 protocol error\n"));
208                 session->error_val = PS_PROTOCOL;
209                 return -1;
210         } else {
211                 if (session->count > last)
212                         session->cur_msg = last + 1;
213                 else
214                         session->cur_msg = 0;
215         }
216
217         return PS_SUCCESS;
218 }
219
220 static gint pop3_getrange_uidl_send(Pop3Session *session)
221 {
222         session->state = POP3_GETRANGE_UIDL;
223         pop3_gen_send(session, "UIDL");
224         return PS_SUCCESS;
225 }
226
227 static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *msg)
228 {
229         gchar id[IDLEN + 1];
230         gint num;
231         time_t recv_time;
232
233         if (msg[0] == '.') {
234                 session->uidl_is_valid = TRUE;
235                 return PS_SUCCESS;
236         }
237
238         if (sscanf(msg, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
239                 return -1;
240         if (num <= 0 || num > session->count)
241                 return -1;
242
243         session->msg[num].uidl = g_strdup(id);
244
245         if (!session->uidl_table)
246                 return PS_CONTINUE;
247
248         recv_time = (time_t)g_hash_table_lookup(session->uidl_table, id);
249         session->msg[num].recv_time = recv_time;
250
251         if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE)
252                 session->msg[num].received = TRUE;
253
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;
259         }
260
261         return PS_CONTINUE;
262 }
263
264 static gint pop3_getsize_list_send(Pop3Session *session)
265 {
266         session->state = POP3_GETSIZE_LIST;
267         pop3_gen_send(session, "LIST");
268         return PS_SUCCESS;
269 }
270
271 static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *msg)
272 {
273         guint num, size;
274         gint next_state;
275
276         if (msg[0] == '.')
277                 return PS_SUCCESS;
278
279         if (sscanf(msg, "%u %u", &num, &size) != 2) {
280                 session->error_val = PS_PROTOCOL;
281                 return -1;
282         }
283
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;
288
289         return PS_CONTINUE;
290 }
291
292 static gint pop3_retr_send(Pop3Session *session)
293 {
294         session->state = POP3_RETR;
295         pop3_gen_send(session, "RETR %d", session->cur_msg);
296         return PS_SUCCESS;
297 }
298
299 static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
300 {
301         gchar *file;
302         gint drop_ok;
303
304         file = get_tmp_file();
305         if (pop3_write_msg_to_file(file, data, len) < 0) {
306                 g_free(file);
307                 session->error_val = PS_IOERR;
308                 return -1;
309         }
310
311         /* drop_ok: 0: success 1: don't receive -1: error */
312         drop_ok = inc_drop_message(file, session);
313         g_free(file);
314         if (drop_ok < 0) {
315                 session->error_val = PS_IOERR;
316                 return -1;
317         }
318
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++;
322
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;
326
327         return PS_SUCCESS;
328 }
329
330 static gint pop3_delete_send(Pop3Session *session)
331 {
332         session->state = POP3_DELETE;
333         pop3_gen_send(session, "DELE %d", session->cur_msg);
334         return PS_SUCCESS;
335 }
336
337 static gint pop3_delete_recv(Pop3Session *session)
338 {
339         session->msg[session->cur_msg].deleted = TRUE;
340         return PS_SUCCESS;
341 }
342
343 static gint pop3_logout_send(Pop3Session *session)
344 {
345         session->state = POP3_LOGOUT;
346         pop3_gen_send(session, "QUIT");
347         return PS_SUCCESS;
348 }
349
350 static void pop3_gen_send(Pop3Session *session, const gchar *format, ...)
351 {
352         gchar buf[POPBUFSIZE + 1];
353         va_list args;
354
355         va_start(args, format);
356         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
357         va_end(args);
358
359         if (!strncasecmp(buf, "PASS ", 5))
360                 log_print("POP3> PASS ********\n");
361         else
362                 log_print("POP3> %s\n", buf);
363
364         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
365 }
366
367 Session *pop3_session_new(PrefsAccount *account)
368 {
369         Pop3Session *session;
370
371         g_return_val_if_fail(account != NULL, NULL);
372
373         session = g_new0(Pop3Session, 1);
374
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;
381
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;
385
386         SESSION(session)->destroy = pop3_session_destroy;
387
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;
393
394         return SESSION(session);
395 }
396
397 static void pop3_session_destroy(Session *session)
398 {
399         Pop3Session *pop3_session = POP3_SESSION(session);
400         gint n;
401
402         g_return_if_fail(session != NULL);
403
404         for (n = 1; n <= pop3_session->count; n++)
405                 g_free(pop3_session->msg[n].uidl);
406         g_free(pop3_session->msg);
407
408         if (pop3_session->uidl_table) {
409                 hash_free_strings(pop3_session->uidl_table);
410                 g_hash_table_destroy(pop3_session->uidl_table);
411         }
412
413         g_free(pop3_session->greeting);
414         g_free(pop3_session->user);
415         g_free(pop3_session->pass);
416 }
417
418 GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
419 {
420         GHashTable *table;
421         gchar *path;
422         FILE *fp;
423         gchar buf[POPBUFSIZE];
424         gchar uidl[POPBUFSIZE];
425         time_t recv_time;
426         time_t now;
427
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");
433                 g_free(path);
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");
439                         g_free(path);
440                         return NULL;
441                 }
442         }
443         g_free(path);
444
445         table = g_hash_table_new(g_str_hash, g_str_equal);
446
447         now = time(NULL);
448
449         while (fgets(buf, sizeof(buf), fp) != NULL) {
450                 strretchomp(buf);
451                 recv_time = RECV_TIME_NONE;
452                 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
453                         if (sscanf(buf, "%s", uidl) != 1)
454                                 continue;
455                         else
456                                 recv_time = now;
457                 }
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));
462         }
463
464         fclose(fp);
465         return table;
466 }
467
468 gint pop3_write_uidl_list(Pop3Session *session)
469 {
470         gchar *path;
471         FILE *fp;
472         Pop3MsgInfo *msg;
473         gint n;
474
475         if (!session->uidl_is_valid) return 0;
476
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");
483                 g_free(path);
484                 return -1;
485         }
486
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);
491         }
492
493         if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
494         g_free(path);
495
496         return 0;
497 }
498
499 static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
500                                    guint len)
501 {
502         FILE *fp;
503         const gchar *prev, *cur;
504
505         g_return_val_if_fail(file != NULL, -1);
506
507         if ((fp = fopen(file, "wb")) == NULL) {
508                 FILE_OP_ERROR(file, "fopen");
509                 return -1;
510         }
511
512         if (change_file_mode_rw(fp, file) < 0)
513                 FILE_OP_ERROR(file, "chmod");
514
515         /* +------------------+----------------+--------------------------+ *
516          * ^data              ^prev            ^cur             data+len-1^ */
517
518         prev = data;
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);
524                         fclose(fp);
525                         unlink(file);
526                         return -1;
527                 }
528
529                 if (cur == data + len - 1) {
530                         prev = cur + 1;
531                         break;
532                 }
533
534                 if (*(cur + 1) == '\n')
535                         prev = cur + 2;
536                 else
537                         prev = cur + 1;
538
539                 if (prev - data >= len)
540                         break;
541         }
542
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);
547                 fclose(fp);
548                 unlink(file);
549                 return -1;
550         }
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);
555                         fclose(fp);
556                         unlink(file);
557                         return -1;
558                 }
559         }
560
561         if (fclose(fp) == EOF) {
562                 FILE_OP_ERROR(file, "fclose");
563                 unlink(file);
564                 return -1;
565         }
566
567         return 0;
568 }
569
570 static Pop3State pop3_lookup_next(Pop3Session *session)
571 {
572         Pop3MsgInfo *msg;
573         PrefsAccount *ac = session->ac_prefs;
574         gint size;
575         gboolean size_limit_over;
576
577         for (;;) {
578                 msg = &session->msg[session->cur_msg];
579                 size = msg->size;
580                 size_limit_over =
581                     (ac->enable_size_limit &&
582                      ac->size_limit > 0 &&
583                      size > ac->size_limit * 1024);
584
585                 if (ac->rmmail &&
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"),
591                                   session->cur_msg);
592                         pop3_delete_send(session);
593                         return POP3_DELETE;
594                 }
595
596                 if (size_limit_over)
597                         log_print
598                                 (_("POP3: Skipping message %d (%d bytes)\n"),
599                                   session->cur_msg, size);
600
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);
605                                 return POP3_LOGOUT;
606                         } else
607                                 session->cur_msg++;
608                 } else
609                         break;
610         }
611
612         pop3_retr_send(session);
613         return POP3_RETR;
614 }
615
616 static Pop3ErrorValue pop3_ok(Pop3Session *session, const gchar *msg)
617 {
618         Pop3ErrorValue ok;
619
620         log_print("POP3< %s\n", msg);
621
622         if (!strncmp(msg, "+OK", 3))
623                 ok = PS_SUCCESS;
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"));
630                         ok = PS_LOCKBUSY;
631                 } else {
632                         switch (session->state) {
633 #if USE_OPENSSL
634                         case POP3_STLS:
635                                 log_warning(_("can't start TLS session\n"));
636                                 ok = PS_ERROR;
637                                 break;
638 #endif
639                         case POP3_GETAUTH_USER:
640                         case POP3_GETAUTH_PASS:
641                         case POP3_GETAUTH_APOP:
642                                 log_warning(_("error occurred on authentication\n"));
643                                 ok = PS_AUTHFAIL;
644                                 break;
645                         default:
646                                 log_warning(_("error occured on POP3 session\n"));
647                                 ok = PS_ERROR;
648                         }
649                 }
650
651                 fprintf(stderr, "POP3: %s\n", msg);
652         } else
653                 ok = PS_PROTOCOL;
654
655         session->error_val = ok;
656         return ok;
657 }
658
659 static gint pop3_session_recv_msg(Session *session, const gchar *msg)
660 {
661         Pop3Session *pop3_session = POP3_SESSION(session);
662         Pop3ErrorValue val;
663         const gchar *body;
664
665         body = msg;
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;
671                         return -1;
672                 }
673
674                 if (*body == '+' || *body == '-')
675                         body++;
676                 while (isalpha(*body))
677                         body++;
678                 while (isspace(*body))
679                         body++;
680         }
681
682         switch (pop3_session->state) {
683         case POP3_READY:
684         case POP3_GREETING:
685                 pop3_greeting_recv(pop3_session, body);
686 #if USE_OPENSSL
687                 if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
688                         pop3_stls_send(pop3_session);
689                 else
690 #endif
691                 if (pop3_session->ac_prefs->protocol == A_APOP)
692                         pop3_getauth_apop_send(pop3_session);
693                 else
694                         pop3_getauth_user_send(pop3_session);
695                 break;
696 #if USE_OPENSSL
697         case POP3_STLS:
698                 if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
699                         return -1;
700                 if (pop3_session->ac_prefs->protocol == A_APOP)
701                         pop3_getauth_apop_send(pop3_session);
702                 else
703                         pop3_getauth_user_send(pop3_session);
704                 break;
705 #endif
706         case POP3_GETAUTH_USER:
707                 pop3_getauth_pass_send(pop3_session);
708                 break;
709         case POP3_GETAUTH_PASS:
710         case POP3_GETAUTH_APOP:
711                 pop3_getrange_stat_send(pop3_session);
712                 break;
713         case POP3_GETRANGE_STAT:
714                 if (pop3_getrange_stat_recv(pop3_session, body) < 0)
715                         return -1;
716                 if (pop3_session->count > 0)
717                         pop3_getrange_uidl_send(pop3_session);
718                 else
719                         pop3_logout_send(pop3_session);
720                 break;
721         case POP3_GETRANGE_LAST:
722                 if (pop3_getrange_last_recv(pop3_session, body) < 0)
723                         return -1;
724                 if (pop3_session->cur_msg > 0)
725                         pop3_getsize_list_send(pop3_session);
726                 else
727                         pop3_logout_send(pop3_session);
728                 break;
729         case POP3_GETRANGE_UIDL:
730                 pop3_session->state = POP3_GETRANGE_UIDL_RECV;
731                 return 1;
732         case POP3_GETRANGE_UIDL_RECV:
733                 val = pop3_getrange_uidl_recv(pop3_session, body);
734                 if (val == PS_CONTINUE)
735                         return 1;
736                 else if (val == PS_SUCCESS) {
737                         if (pop3_session->new_msg_exist)
738                                 pop3_getsize_list_send(pop3_session);
739                         else
740                                 pop3_logout_send(pop3_session);
741                 } else
742                         return -1;
743                 break;
744         case POP3_GETSIZE_LIST:
745                 pop3_session->state = POP3_GETSIZE_LIST_RECV;
746                 return 1;
747         case POP3_GETSIZE_LIST_RECV:
748                 val = pop3_getsize_list_recv(pop3_session, body);
749                 if (val == PS_CONTINUE)
750                         return 1;
751                 else if (val == PS_SUCCESS) {
752                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
753                                 return -1;
754                 } else
755                         return -1;
756                 break;
757         case POP3_RETR:
758                 pop3_session->state = POP3_RETR_RECV;
759                 session_recv_data
760                         (session,
761                          pop3_session->msg[pop3_session->cur_msg].size, TRUE);
762                 break;
763         case POP3_DELETE:
764                 pop3_delete_recv(pop3_session);
765                 if (pop3_session->cur_msg == pop3_session->count)
766                         pop3_logout_send(pop3_session);
767                 else {
768                         pop3_session->cur_msg++;
769                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
770                                 return -1;
771                 }
772                 break;
773         case POP3_LOGOUT:
774                 session_disconnect(session);
775                 break;
776         case POP3_ERROR:
777         default:
778                 return -1;
779         }
780
781         return 0;
782 }
783
784 static gint pop3_session_recv_data_finished(Session *session, guchar *data,
785                                             guint len)
786 {
787         Pop3Session *pop3_session = POP3_SESSION(session);
788
789         if (len == 0)
790                 return -1;
791
792         if (pop3_retr_recv(pop3_session, data, len) < 0)
793                 return -1;
794
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);
800         else {
801                 pop3_session->cur_msg++;
802                 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
803                         return -1;
804         }
805
806         return 0;
807 }