sync with 0.8.11cvs22
[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         session->error_msg = NULL;
394
395         return SESSION(session);
396 }
397
398 static void pop3_session_destroy(Session *session)
399 {
400         Pop3Session *pop3_session = POP3_SESSION(session);
401         gint n;
402
403         g_return_if_fail(session != NULL);
404
405         for (n = 1; n <= pop3_session->count; n++)
406                 g_free(pop3_session->msg[n].uidl);
407         g_free(pop3_session->msg);
408
409         if (pop3_session->uidl_table) {
410                 hash_free_strings(pop3_session->uidl_table);
411                 g_hash_table_destroy(pop3_session->uidl_table);
412         }
413
414         g_free(pop3_session->greeting);
415         g_free(pop3_session->user);
416         g_free(pop3_session->pass);
417         g_free(pop3_session->error_msg);
418 }
419
420 GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
421 {
422         GHashTable *table;
423         gchar *path;
424         FILE *fp;
425         gchar buf[POPBUFSIZE];
426         gchar uidl[POPBUFSIZE];
427         time_t recv_time;
428         time_t now;
429
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");
435                 g_free(path);
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");
441                         g_free(path);
442                         return NULL;
443                 }
444         }
445         g_free(path);
446
447         table = g_hash_table_new(g_str_hash, g_str_equal);
448
449         now = time(NULL);
450
451         while (fgets(buf, sizeof(buf), fp) != NULL) {
452                 strretchomp(buf);
453                 recv_time = RECV_TIME_NONE;
454                 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
455                         if (sscanf(buf, "%s", uidl) != 1)
456                                 continue;
457                         else
458                                 recv_time = now;
459                 }
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));
464         }
465
466         fclose(fp);
467         return table;
468 }
469
470 gint pop3_write_uidl_list(Pop3Session *session)
471 {
472         gchar *path;
473         FILE *fp;
474         Pop3MsgInfo *msg;
475         gint n;
476
477         if (!session->uidl_is_valid) return 0;
478
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");
485                 g_free(path);
486                 return -1;
487         }
488
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);
493         }
494
495         if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
496         g_free(path);
497
498         return 0;
499 }
500
501 static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
502                                    guint len)
503 {
504         FILE *fp;
505         const gchar *prev, *cur;
506
507         g_return_val_if_fail(file != NULL, -1);
508
509         if ((fp = fopen(file, "wb")) == NULL) {
510                 FILE_OP_ERROR(file, "fopen");
511                 return -1;
512         }
513
514         if (change_file_mode_rw(fp, file) < 0)
515                 FILE_OP_ERROR(file, "chmod");
516
517         /* +------------------+----------------+--------------------------+ *
518          * ^data              ^prev            ^cur             data+len-1^ */
519
520         prev = data;
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);
526                         fclose(fp);
527                         unlink(file);
528                         return -1;
529                 }
530
531                 if (cur == data + len - 1) {
532                         prev = cur + 1;
533                         break;
534                 }
535
536                 if (*(cur + 1) == '\n')
537                         prev = cur + 2;
538                 else
539                         prev = cur + 1;
540
541                 if (prev - data >= len)
542                         break;
543         }
544
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);
549                 fclose(fp);
550                 unlink(file);
551                 return -1;
552         }
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);
557                         fclose(fp);
558                         unlink(file);
559                         return -1;
560                 }
561         }
562
563         if (fclose(fp) == EOF) {
564                 FILE_OP_ERROR(file, "fclose");
565                 unlink(file);
566                 return -1;
567         }
568
569         return 0;
570 }
571
572 static Pop3State pop3_lookup_next(Pop3Session *session)
573 {
574         Pop3MsgInfo *msg;
575         PrefsAccount *ac = session->ac_prefs;
576         gint size;
577         gboolean size_limit_over;
578
579         for (;;) {
580                 msg = &session->msg[session->cur_msg];
581                 size = msg->size;
582                 size_limit_over =
583                     (ac->enable_size_limit &&
584                      ac->size_limit > 0 &&
585                      size > ac->size_limit * 1024);
586
587                 if (ac->rmmail &&
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"),
593                                   session->cur_msg);
594                         pop3_delete_send(session);
595                         return POP3_DELETE;
596                 }
597
598                 if (size_limit_over)
599                         log_print
600                                 (_("POP3: Skipping message %d (%d bytes)\n"),
601                                   session->cur_msg, size);
602
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);
607                                 return POP3_LOGOUT;
608                         } else
609                                 session->cur_msg++;
610                 } else
611                         break;
612         }
613
614         pop3_retr_send(session);
615         return POP3_RETR;
616 }
617
618 static Pop3ErrorValue pop3_ok(Pop3Session *session, const gchar *msg)
619 {
620         Pop3ErrorValue ok;
621
622         log_print("POP3< %s\n", msg);
623
624         if (!strncmp(msg, "+OK", 3))
625                 ok = PS_SUCCESS;
626         else if (!strncmp(msg, "-ERR", 4)) {
627                 if (strstr(msg + 4, "lock") ||
628                     strstr(msg + 4, "Lock") ||
629                     strstr(msg + 4, "LOCK") ||
630                     strstr(msg + 4, "wait")) {
631                         log_warning(_("mailbox is locked\n"));
632                         ok = PS_LOCKBUSY;
633                 } else {
634                         switch (session->state) {
635 #if USE_OPENSSL
636                         case POP3_STLS:
637                                 log_warning(_("can't start TLS session\n"));
638                                 ok = PS_ERROR;
639                                 break;
640 #endif
641                         case POP3_GETAUTH_USER:
642                         case POP3_GETAUTH_PASS:
643                         case POP3_GETAUTH_APOP:
644                                 log_warning(_("error occurred on authentication\n"));
645                                 ok = PS_AUTHFAIL;
646                                 break;
647                         default:
648                                 log_warning(_("error occurred on POP3 session\n"));
649                                 ok = PS_ERROR;
650                         }
651                 }
652
653                 g_free(session->error_msg);
654                 session->error_msg = g_strdup(msg);
655                 fprintf(stderr, "POP3: %s\n", msg);
656         } else
657                 ok = PS_PROTOCOL;
658
659         session->error_val = ok;
660         return ok;
661 }
662
663 static gint pop3_session_recv_msg(Session *session, const gchar *msg)
664 {
665         Pop3Session *pop3_session = POP3_SESSION(session);
666         Pop3ErrorValue val;
667         const gchar *body;
668
669         body = msg;
670         if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
671             pop3_session->state != POP3_GETSIZE_LIST_RECV) {
672                 val = pop3_ok(pop3_session, msg);
673                 if (val != PS_SUCCESS) {
674                         pop3_session->state = POP3_ERROR;
675                         return -1;
676                 }
677
678                 if (*body == '+' || *body == '-')
679                         body++;
680                 while (isalpha(*body))
681                         body++;
682                 while (isspace(*body))
683                         body++;
684         }
685
686         switch (pop3_session->state) {
687         case POP3_READY:
688         case POP3_GREETING:
689                 pop3_greeting_recv(pop3_session, body);
690 #if USE_OPENSSL
691                 if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
692                         pop3_stls_send(pop3_session);
693                 else
694 #endif
695                 if (pop3_session->ac_prefs->protocol == A_APOP)
696                         pop3_getauth_apop_send(pop3_session);
697                 else
698                         pop3_getauth_user_send(pop3_session);
699                 break;
700 #if USE_OPENSSL
701         case POP3_STLS:
702                 if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
703                         return -1;
704                 if (pop3_session->ac_prefs->protocol == A_APOP)
705                         pop3_getauth_apop_send(pop3_session);
706                 else
707                         pop3_getauth_user_send(pop3_session);
708                 break;
709 #endif
710         case POP3_GETAUTH_USER:
711                 pop3_getauth_pass_send(pop3_session);
712                 break;
713         case POP3_GETAUTH_PASS:
714         case POP3_GETAUTH_APOP:
715                 pop3_getrange_stat_send(pop3_session);
716                 break;
717         case POP3_GETRANGE_STAT:
718                 if (pop3_getrange_stat_recv(pop3_session, body) < 0)
719                         return -1;
720                 if (pop3_session->count > 0)
721                         pop3_getrange_uidl_send(pop3_session);
722                 else
723                         pop3_logout_send(pop3_session);
724                 break;
725         case POP3_GETRANGE_LAST:
726                 if (pop3_getrange_last_recv(pop3_session, body) < 0)
727                         return -1;
728                 if (pop3_session->cur_msg > 0)
729                         pop3_getsize_list_send(pop3_session);
730                 else
731                         pop3_logout_send(pop3_session);
732                 break;
733         case POP3_GETRANGE_UIDL:
734                 pop3_session->state = POP3_GETRANGE_UIDL_RECV;
735                 return 1;
736         case POP3_GETRANGE_UIDL_RECV:
737                 val = pop3_getrange_uidl_recv(pop3_session, body);
738                 if (val == PS_CONTINUE)
739                         return 1;
740                 else if (val == PS_SUCCESS) {
741                         if (pop3_session->new_msg_exist)
742                                 pop3_getsize_list_send(pop3_session);
743                         else
744                                 pop3_logout_send(pop3_session);
745                 } else
746                         return -1;
747                 break;
748         case POP3_GETSIZE_LIST:
749                 pop3_session->state = POP3_GETSIZE_LIST_RECV;
750                 return 1;
751         case POP3_GETSIZE_LIST_RECV:
752                 val = pop3_getsize_list_recv(pop3_session, body);
753                 if (val == PS_CONTINUE)
754                         return 1;
755                 else if (val == PS_SUCCESS) {
756                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
757                                 return -1;
758                 } else
759                         return -1;
760                 break;
761         case POP3_RETR:
762                 pop3_session->state = POP3_RETR_RECV;
763                 session_recv_data
764                         (session,
765                          pop3_session->msg[pop3_session->cur_msg].size, TRUE);
766                 break;
767         case POP3_DELETE:
768                 pop3_delete_recv(pop3_session);
769                 if (pop3_session->cur_msg == pop3_session->count)
770                         pop3_logout_send(pop3_session);
771                 else {
772                         pop3_session->cur_msg++;
773                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
774                                 return -1;
775                 }
776                 break;
777         case POP3_LOGOUT:
778                 session_disconnect(session);
779                 break;
780         case POP3_ERROR:
781         default:
782                 return -1;
783         }
784
785         return 0;
786 }
787
788 static gint pop3_session_recv_data_finished(Session *session, guchar *data,
789                                             guint len)
790 {
791         Pop3Session *pop3_session = POP3_SESSION(session);
792
793         if (len == 0)
794                 return -1;
795
796         if (pop3_retr_recv(pop3_session, data, len) < 0)
797                 return -1;
798
799         if (pop3_session->ac_prefs->rmmail &&
800             pop3_session->ac_prefs->msg_leave_time == 0)
801                 pop3_delete_send(pop3_session);
802         else if (pop3_session->cur_msg == pop3_session->count)
803                 pop3_logout_send(pop3_session);
804         else {
805                 pop3_session->cur_msg++;
806                 if (pop3_lookup_next(pop3_session) == POP3_ERROR)
807                         return -1;
808         }
809
810         return 0;
811 }