90fa283d4b0a7145c51322d6c02d3121206cfe3e
[claws.git] / src / pop.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 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 "recv.h"
39
40 #include "log.h"
41 #include "hooks.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 *data,
61                                          guint        len);
62 static gint pop3_getsize_list_send      (Pop3Session *session);
63 static gint pop3_getsize_list_recv      (Pop3Session *session,
64                                          const gchar *data,
65                                          guint        len);
66 static gint pop3_retr_send              (Pop3Session *session);
67 static gint pop3_retr_recv              (Pop3Session *session,
68                                          const gchar *data,
69                                          guint        len);
70 static gint pop3_delete_send            (Pop3Session *session);
71 static gint pop3_delete_recv            (Pop3Session *session);
72 static gint pop3_logout_send            (Pop3Session *session);
73
74 static void pop3_gen_send               (Pop3Session    *session,
75                                          const gchar    *format, ...);
76
77 static void pop3_session_destroy        (Session        *session);
78
79 static gint pop3_write_msg_to_file      (const gchar    *file,
80                                          const gchar    *data,
81                                          guint           len);
82
83 static Pop3State pop3_lookup_next       (Pop3Session    *session);
84 static Pop3ErrorValue pop3_ok           (Pop3Session    *session,
85                                          const gchar    *msg);
86
87 static gint pop3_session_recv_msg               (Session        *session,
88                                                  const gchar    *msg);
89 static gint pop3_session_recv_data_finished     (Session        *session,
90                                                  guchar         *data,
91                                                  guint           len);
92
93
94 static gint pop3_greeting_recv(Pop3Session *session, const gchar *msg)
95 {
96         session->state = POP3_GREETING;
97
98         session->greeting = g_strdup(msg);
99         return PS_SUCCESS;
100 }
101
102 #if USE_OPENSSL
103 static gint pop3_stls_send(Pop3Session *session)
104 {
105         session->state = POP3_STLS;
106         pop3_gen_send(session, "STLS");
107         return PS_SUCCESS;
108 }
109
110 static gint pop3_stls_recv(Pop3Session *session)
111 {
112         if (session_start_tls(SESSION(session)) < 0) {
113                 session->error_val = PS_SOCKET;
114                 return -1;
115         }
116         return PS_SUCCESS;
117 }
118 #endif /* USE_OPENSSL */
119
120 static gint pop3_getauth_user_send(Pop3Session *session)
121 {
122         g_return_val_if_fail(session->user != NULL, -1);
123
124         session->state = POP3_GETAUTH_USER;
125         pop3_gen_send(session, "USER %s", session->user);
126         return PS_SUCCESS;
127 }
128
129 static gint pop3_getauth_pass_send(Pop3Session *session)
130 {
131         g_return_val_if_fail(session->pass != NULL, -1);
132
133         session->state = POP3_GETAUTH_PASS;
134         pop3_gen_send(session, "PASS %s", session->pass);
135         return PS_SUCCESS;
136 }
137
138 static gint pop3_getauth_apop_send(Pop3Session *session)
139 {
140         gchar *start, *end;
141         gchar *apop_str;
142         gchar md5sum[33];
143
144         g_return_val_if_fail(session->user != NULL, -1);
145         g_return_val_if_fail(session->pass != NULL, -1);
146
147         session->state = POP3_GETAUTH_APOP;
148
149         if ((start = strchr(session->greeting, '<')) == NULL) {
150                 log_warning(_("Required APOP timestamp not found "
151                               "in greeting\n"));
152                 session->error_val = PS_PROTOCOL;
153                 return -1;
154         }
155
156         if ((end = strchr(start, '>')) == NULL || end == start + 1) {
157                 log_warning(_("Timestamp syntax error in greeting\n"));
158                 session->error_val = PS_PROTOCOL;
159                 return -1;
160         }
161
162         *(end + 1) = '\0';
163
164         apop_str = g_strconcat(start, session->pass, NULL);
165         md5_hex_digest(md5sum, apop_str);
166         g_free(apop_str);
167
168         pop3_gen_send(session, "APOP %s %s", session->user, md5sum);
169
170         return PS_SUCCESS;
171 }
172
173 static gint pop3_getrange_stat_send(Pop3Session *session)
174 {
175         session->state = POP3_GETRANGE_STAT;
176         pop3_gen_send(session, "STAT");
177         return PS_SUCCESS;
178 }
179
180 static gint pop3_getrange_stat_recv(Pop3Session *session, const gchar *msg)
181 {
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;
185                 return -1;
186         } else {
187                 if (session->count == 0) {
188                         session->uidl_is_valid = TRUE;
189                 } else {
190                         session->msg = g_new0(Pop3MsgInfo, session->count + 1);
191                         session->cur_msg = 1;
192                 }
193         }
194
195         return PS_SUCCESS;
196 }
197
198 static gint pop3_getrange_last_send(Pop3Session *session)
199 {
200         session->state = POP3_GETRANGE_LAST;
201         pop3_gen_send(session, "LAST");
202         return PS_SUCCESS;
203 }
204
205 static gint pop3_getrange_last_recv(Pop3Session *session, const gchar *msg)
206 {
207         gint last;
208
209         if (sscanf(msg, "%d", &last) == 0) {
210                 log_warning(_("POP3 protocol error\n"));
211                 session->error_val = PS_PROTOCOL;
212                 return -1;
213         } else {
214                 if (session->count > last) {
215                         session->new_msg_exist = TRUE;
216                         session->cur_msg = last + 1;
217                 } else
218                         session->cur_msg = 0;
219         }
220
221         return PS_SUCCESS;
222 }
223
224 static gint pop3_getrange_uidl_send(Pop3Session *session)
225 {
226         session->state = POP3_GETRANGE_UIDL;
227         pop3_gen_send(session, "UIDL");
228         return PS_SUCCESS;
229 }
230
231 static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *data,
232                                     guint len)
233 {
234         gchar id[IDLEN + 1];
235         gchar buf[POPBUFSIZE];
236         gint buf_len;
237         gint num;
238         time_t recv_time;
239         const gchar *p = data;
240         const gchar *lastp = data + len;
241         const gchar *newline;
242
243         while (p < lastp) {
244                 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
245                         return -1;
246                 buf_len = MIN(newline - p, sizeof(buf) - 1);
247                 memcpy(buf, p, buf_len);
248                 buf[buf_len] = '\0';
249
250                 p = newline + 1;
251                 if (p < lastp && *p == '\n') p++;
252
253                 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
254                         return -1;
255                 if (num <= 0 || num > session->count)
256                         return -1;
257
258                 session->msg[num].uidl = g_strdup(id);
259
260                 recv_time = (time_t)g_hash_table_lookup(session->uidl_table, id);
261                 session->msg[num].recv_time = recv_time;
262
263                 if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE)
264                         session->msg[num].received = TRUE;
265
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;
271                 }
272         }
273
274         session->uidl_is_valid = TRUE;
275         return PS_SUCCESS;
276 }
277
278 static gint pop3_getsize_list_send(Pop3Session *session)
279 {
280         session->state = POP3_GETSIZE_LIST;
281         pop3_gen_send(session, "LIST");
282         return PS_SUCCESS;
283 }
284
285 static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *data,
286                                    guint len)
287 {
288         gchar buf[POPBUFSIZE];
289         gint buf_len;
290         guint num, size;
291         const gchar *p = data;
292         const gchar *lastp = data + len;
293         const gchar *newline;
294
295         while (p < lastp) {
296                 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
297                         return -1;
298                 buf_len = MIN(newline - p, sizeof(buf) - 1);
299                 memcpy(buf, p, buf_len);
300                 buf[buf_len] = '\0';
301
302                 p = newline + 1;
303                 if (p < lastp && *p == '\n') p++;
304
305                 if (sscanf(buf, "%u %u", &num, &size) != 2) {
306                         session->error_val = PS_PROTOCOL;
307                         return -1;
308                 }
309
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;
314         }
315
316         return PS_SUCCESS;
317 }
318
319 static gint pop3_retr_send(Pop3Session *session)
320 {
321         session->state = POP3_RETR;
322         pop3_gen_send(session, "RETR %d", session->cur_msg);
323         return PS_SUCCESS;
324 }
325
326 static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
327 {
328         gchar *file;
329         gint drop_ok;
330         MailReceiveData mail_receive_data;
331
332         mail_receive_data.session = session;
333         mail_receive_data.data = g_strndup(data, len);
334         hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
335
336         file = get_tmp_file();
337         if (pop3_write_msg_to_file(file, mail_receive_data.data,
338                 strlen(mail_receive_data.data)) < 0) {
339                 g_free(file);
340                 g_free(mail_receive_data.data);
341                 session->error_val = PS_IOERR;
342                 return -1;
343         }
344         g_free(mail_receive_data.data);
345
346         /* drop_ok: 0: success 1: don't receive -1: error */
347         drop_ok = session->drop_message(session, file);
348         g_free(file);
349         if (drop_ok < 0) {
350                 session->error_val = PS_IOERR;
351                 return -1;
352         }
353
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++;
357
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;
361
362         return PS_SUCCESS;
363 }
364
365 static gint pop3_delete_send(Pop3Session *session)
366 {
367         session->state = POP3_DELETE;
368         pop3_gen_send(session, "DELE %d", session->cur_msg);
369         return PS_SUCCESS;
370 }
371
372 static gint pop3_delete_recv(Pop3Session *session)
373 {
374         session->msg[session->cur_msg].deleted = TRUE;
375         return PS_SUCCESS;
376 }
377
378 static gint pop3_logout_send(Pop3Session *session)
379 {
380         session->state = POP3_LOGOUT;
381         pop3_gen_send(session, "QUIT");
382         return PS_SUCCESS;
383 }
384
385 static void pop3_gen_send(Pop3Session *session, const gchar *format, ...)
386 {
387         gchar buf[POPBUFSIZE + 1];
388         va_list args;
389
390         va_start(args, format);
391         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
392         va_end(args);
393
394         if (!strncasecmp(buf, "PASS ", 5))
395                 log_print("POP3> PASS ********\n");
396         else
397                 log_print("POP3> %s\n", buf);
398
399         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
400 }
401
402 Session *pop3_session_new(PrefsAccount *account)
403 {
404         Pop3Session *session;
405
406         g_return_val_if_fail(account != NULL, NULL);
407
408         session = g_new0(Pop3Session, 1);
409
410         session_init(SESSION(session));
411
412         SESSION(session)->type = SESSION_POP3;
413
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;
417
418         SESSION(session)->destroy = pop3_session_destroy;
419
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;
426
427         return SESSION(session);
428 }
429
430 static void pop3_session_destroy(Session *session)
431 {
432         Pop3Session *pop3_session = POP3_SESSION(session);
433         gint n;
434
435         g_return_if_fail(session != NULL);
436
437         for (n = 1; n <= pop3_session->count; n++)
438                 g_free(pop3_session->msg[n].uidl);
439         g_free(pop3_session->msg);
440
441         if (pop3_session->uidl_table) {
442                 hash_free_strings(pop3_session->uidl_table);
443                 g_hash_table_destroy(pop3_session->uidl_table);
444         }
445
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);
450 }
451
452 GHashTable *pop3_get_uidl_table(PrefsAccount *ac_prefs)
453 {
454         GHashTable *table;
455         gchar *path;
456         FILE *fp;
457         gchar buf[POPBUFSIZE];
458         gchar uidl[POPBUFSIZE];
459         time_t recv_time;
460         time_t now;
461
462         table = g_hash_table_new(g_str_hash, g_str_equal);
463
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");
469                 g_free(path);
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");
475                         g_free(path);
476                         return table;
477                 }
478         }
479         g_free(path);
480
481         now = time(NULL);
482
483         while (fgets(buf, sizeof(buf), fp) != NULL) {
484                 strretchomp(buf);
485                 recv_time = RECV_TIME_NONE;
486                 if (sscanf(buf, "%s\t%ld", uidl, &recv_time) != 2) {
487                         if (sscanf(buf, "%s", uidl) != 1)
488                                 continue;
489                         else
490                                 recv_time = now;
491                 }
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));
496         }
497
498         fclose(fp);
499         return table;
500 }
501
502 gint pop3_write_uidl_list(Pop3Session *session)
503 {
504         gchar *path;
505         FILE *fp;
506         Pop3MsgInfo *msg;
507         gint n;
508
509         if (!session->uidl_is_valid) return 0;
510
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");
517                 g_free(path);
518                 return -1;
519         }
520
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);
525         }
526
527         if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
528         g_free(path);
529
530         return 0;
531 }
532
533 static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
534                                    guint len)
535 {
536         FILE *fp;
537         const gchar *prev, *cur;
538
539         g_return_val_if_fail(file != NULL, -1);
540
541         if ((fp = fopen(file, "wb")) == NULL) {
542                 FILE_OP_ERROR(file, "fopen");
543                 return -1;
544         }
545
546         if (change_file_mode_rw(fp, file) < 0)
547                 FILE_OP_ERROR(file, "chmod");
548
549         /* +------------------+----------------+--------------------------+ *
550          * ^data              ^prev            ^cur             data+len-1^ */
551
552         prev = data;
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);
558                         fclose(fp);
559                         unlink(file);
560                         return -1;
561                 }
562
563                 if (cur == data + len - 1) {
564                         prev = cur + 1;
565                         break;
566                 }
567
568                 if (*(cur + 1) == '\n')
569                         prev = cur + 2;
570                 else
571                         prev = cur + 1;
572
573                 if (prev - data < len - 1 && *prev == '.' && *(prev + 1) == '.')
574                         prev++;
575
576                 if (prev - data >= len)
577                         break;
578         }
579
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);
584                 fclose(fp);
585                 unlink(file);
586                 return -1;
587         }
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);
592                         fclose(fp);
593                         unlink(file);
594                         return -1;
595                 }
596         }
597
598         if (fclose(fp) == EOF) {
599                 FILE_OP_ERROR(file, "fclose");
600                 unlink(file);
601                 return -1;
602         }
603
604         return 0;
605 }
606
607 static Pop3State pop3_lookup_next(Pop3Session *session)
608 {
609         Pop3MsgInfo *msg;
610         PrefsAccount *ac = session->ac_prefs;
611         gint size;
612         gboolean size_limit_over;
613
614         for (;;) {
615                 msg = &session->msg[session->cur_msg];
616                 size = msg->size;
617                 size_limit_over =
618                     (ac->enable_size_limit &&
619                      ac->size_limit > 0 &&
620                      size > ac->size_limit * 1024);
621
622                 if (ac->rmmail &&
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) {
627                         log_print(_("POP3: Deleting expired message %d\n"),
628                                   session->cur_msg);
629                         pop3_delete_send(session);
630                         return POP3_DELETE;
631                 }
632
633                 if (size_limit_over)
634                         log_print
635                                 (_("POP3: Skipping message %d (%d bytes)\n"),
636                                   session->cur_msg, size);
637
638                 if (size == 0 || msg->received || size_limit_over) {
639                         session->cur_total_bytes += size;
640                         if (session->cur_msg == session->count) {
641                                 pop3_logout_send(session);
642                                 return POP3_LOGOUT;
643                         } else
644                                 session->cur_msg++;
645                 } else
646                         break;
647         }
648
649         pop3_retr_send(session);
650         return POP3_RETR;
651 }
652
653 static Pop3ErrorValue pop3_ok(Pop3Session *session, const gchar *msg)
654 {
655         Pop3ErrorValue ok;
656
657         log_print("POP3< %s\n", msg);
658
659         if (!strncmp(msg, "+OK", 3))
660                 ok = PS_SUCCESS;
661         else if (!strncmp(msg, "-ERR", 4)) {
662                 if (strstr(msg + 4, "lock") ||
663                     strstr(msg + 4, "Lock") ||
664                     strstr(msg + 4, "LOCK") ||
665                     strstr(msg + 4, "wait")) {
666                         log_warning(_("mailbox is locked\n"));
667                         ok = PS_LOCKBUSY;
668                 } else if (strcasestr(msg + 4, "timeout")) {
669                         log_warning(_("session timeout\n"));
670                         ok = PS_ERROR;
671                 } else {
672                         switch (session->state) {
673 #if USE_OPENSSL
674                         case POP3_STLS:
675                                 log_warning(_("can't start TLS session\n"));
676                                 ok = PS_ERROR;
677                                 break;
678 #endif
679                         case POP3_GETAUTH_USER:
680                         case POP3_GETAUTH_PASS:
681                         case POP3_GETAUTH_APOP:
682                                 log_warning(_("error occurred on authentication\n"));
683                                 ok = PS_AUTHFAIL;
684                                 break;
685                         case POP3_GETRANGE_LAST:
686                         case POP3_GETRANGE_UIDL:
687                                 log_warning(_("command not supported\n"));
688                                 ok = PS_NOTSUPPORTED;
689                                 break;
690                         default:
691                                 log_warning(_("error occurred on POP3 session\n"));
692                                 ok = PS_ERROR;
693                         }
694                 }
695
696                 g_free(session->error_msg);
697                 session->error_msg = g_strdup(msg);
698                 fprintf(stderr, "POP3: %s\n", msg);
699         } else
700                 ok = PS_PROTOCOL;
701
702         session->error_val = ok;
703         return ok;
704 }
705
706 static gint pop3_session_recv_msg(Session *session, const gchar *msg)
707 {
708         Pop3Session *pop3_session = POP3_SESSION(session);
709         Pop3ErrorValue val = PS_SUCCESS;
710         const guchar *body;
711
712         body = msg;
713         if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
714             pop3_session->state != POP3_GETSIZE_LIST_RECV) {
715                 val = pop3_ok(pop3_session, msg);
716                 if (val != PS_SUCCESS) {
717                         if (val != PS_NOTSUPPORTED) {
718                                 pop3_session->state = POP3_ERROR;
719                                 return -1;
720                         }
721                 }
722
723                 if (*body == '+' || *body == '-')
724                         body++;
725                 while (isalpha(*body))
726                         body++;
727                 while (isspace(*body))
728                         body++;
729         }
730
731         switch (pop3_session->state) {
732         case POP3_READY:
733         case POP3_GREETING:
734                 pop3_greeting_recv(pop3_session, body);
735 #if USE_OPENSSL
736                 if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
737                         pop3_stls_send(pop3_session);
738                 else
739 #endif
740                 if (pop3_session->ac_prefs->protocol == A_APOP)
741                         pop3_getauth_apop_send(pop3_session);
742                 else
743                         pop3_getauth_user_send(pop3_session);
744                 break;
745 #if USE_OPENSSL
746         case POP3_STLS:
747                 if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
748                         return -1;
749                 if (pop3_session->ac_prefs->protocol == A_APOP)
750                         pop3_getauth_apop_send(pop3_session);
751                 else
752                         pop3_getauth_user_send(pop3_session);
753                 break;
754 #endif
755         case POP3_GETAUTH_USER:
756                 pop3_getauth_pass_send(pop3_session);
757                 break;
758         case POP3_GETAUTH_PASS:
759         case POP3_GETAUTH_APOP:
760                 pop3_getrange_stat_send(pop3_session);
761                 break;
762         case POP3_GETRANGE_STAT:
763                 if (pop3_getrange_stat_recv(pop3_session, body) < 0)
764                         return -1;
765                 if (pop3_session->count > 0)
766                         pop3_getrange_uidl_send(pop3_session);
767                 else
768                         pop3_logout_send(pop3_session);
769                 break;
770         case POP3_GETRANGE_LAST:
771                 if (val == PS_NOTSUPPORTED)
772                         pop3_session->error_val = PS_SUCCESS;
773                 else if (pop3_getrange_last_recv(pop3_session, body) < 0)
774                         return -1;
775                 if (pop3_session->cur_msg > 0)
776                         pop3_getsize_list_send(pop3_session);
777                 else
778                         pop3_logout_send(pop3_session);
779                 break;
780         case POP3_GETRANGE_UIDL:
781                 if (val == PS_NOTSUPPORTED) {
782                         pop3_session->error_val = PS_SUCCESS;
783                         pop3_getrange_last_send(pop3_session);
784                 } else {
785                         pop3_session->state = POP3_GETRANGE_UIDL_RECV;
786                         session_recv_data(session, 0, ".\r\n");
787                 }
788                 break;
789         case POP3_GETSIZE_LIST:
790                 pop3_session->state = POP3_GETSIZE_LIST_RECV;
791                 session_recv_data(session, 0, ".\r\n");
792                 break;
793         case POP3_RETR:
794                 pop3_session->state = POP3_RETR_RECV;
795                 session_recv_data(session, 0, ".\r\n");
796                 break;
797         case POP3_DELETE:
798                 pop3_delete_recv(pop3_session);
799                 if (pop3_session->cur_msg == pop3_session->count)
800                         pop3_logout_send(pop3_session);
801                 else {
802                         pop3_session->cur_msg++;
803                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
804                                 return -1;
805                 }
806                 break;
807         case POP3_LOGOUT:
808                 session_disconnect(session);
809                 break;
810         case POP3_ERROR:
811         default:
812                 return -1;
813         }
814
815         return 0;
816 }
817
818 static gint pop3_session_recv_data_finished(Session *session, guchar *data,
819                                             guint len)
820 {
821         Pop3Session *pop3_session = POP3_SESSION(session);
822         Pop3ErrorValue val = PS_SUCCESS;
823
824         switch (pop3_session->state) {
825         case POP3_GETRANGE_UIDL_RECV:
826                 val = pop3_getrange_uidl_recv(pop3_session, data, len);
827                 if (val == PS_SUCCESS) {
828                         if (pop3_session->new_msg_exist)
829                                 pop3_getsize_list_send(pop3_session);
830                         else
831                                 pop3_logout_send(pop3_session);
832                 } else
833                         return -1;
834                 break;
835         case POP3_GETSIZE_LIST_RECV:
836                 val = pop3_getsize_list_recv(pop3_session, data, len);
837                 if (val == PS_SUCCESS) {
838                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
839                                 return -1;
840                 } else
841                         return -1;
842                 break;
843         case POP3_RETR_RECV:
844                 if (pop3_retr_recv(pop3_session, data, len) < 0)
845                         return -1;
846
847                 if (pop3_session->ac_prefs->rmmail &&
848                     pop3_session->ac_prefs->msg_leave_time == 0 &&
849                     pop3_session->msg[pop3_session->cur_msg].recv_time
850                     != RECV_TIME_KEEP)
851                         pop3_delete_send(pop3_session);
852                 else if (pop3_session->cur_msg == pop3_session->count)
853                         pop3_logout_send(pop3_session);
854                 else {
855                         pop3_session->cur_msg++;
856                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
857                                 return -1;
858                 }
859                 break;
860         case POP3_ERROR:
861         default:
862                 return -1;
863         }
864
865         return 0;
866 }