b648cfce38dc4d73f73ea439c6ea0c6af7e4ebfe
[claws.git] / src / pop.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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
32 #include "intl.h"
33 #include "pop.h"
34 #include "socket.h"
35 #include "md5.h"
36 #include "prefs_account.h"
37 #include "utils.h"
38 #include "inc.h"
39 #include "recv.h"
40 #include "selective_download.h"
41 #if USE_SSL
42 #  include "ssl.h"
43 #endif
44
45 #define LOOKUP_NEXT_MSG()                                                       \
46 {                                                                               \
47         Pop3MsgInfo *msg;                                                       \
48         PrefsAccount *ac = state->ac_prefs;                                     \
49         gint size;                                                              \
50         gboolean size_limit_over;                                               \
51                                                                                 \
52         for (;;) {                                                              \
53                 msg = &state->msg[state->cur_msg];                              \
54                 size = msg->size;                                               \
55                 size_limit_over =                                               \
56                     (ac->enable_size_limit &&                                   \
57                      ac->size_limit > 0 &&                                      \
58                      size > ac->size_limit * 1024);                             \
59                                                                                 \
60                 if (ac->rmmail &&                                               \
61                     msg->recv_time != 0 &&                                      \
62                     state->current_time - msg->recv_time >=                     \
63                     ac->msg_leave_time * 24 * 60 * 60) {                        \
64                         log_print(_("POP3: Deleting expired message %d\n"),     \
65                                   state->cur_msg);                              \
66                         return POP3_DELETE_SEND;                                \
67                 }                                                               \
68                                                                                 \
69                 if (size_limit_over)                                            \
70                         log_print(_("POP3: Skipping message %d (%d bytes)\n"),  \
71                                   state->cur_msg, size);                        \
72                                                                                 \
73                 if (size == 0 || msg->received || size_limit_over) {            \
74                         state->cur_total_bytes += size;                         \
75                         if (state->cur_msg == state->count)                     \
76                                 return POP3_LOGOUT_SEND;                        \
77                         else                                                    \
78                                 state->cur_msg++;                               \
79                 } else                                                          \
80                         break;                                                  \
81         }                                                                       \
82 }
83
84 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
85 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
86 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
87 static gboolean pop3_sd_get_next (Pop3State *state);
88 static void pop3_sd_new_header(Pop3State *state);
89 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state);
90
91 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
92 {
93         Pop3State *state = (Pop3State *)data;
94         gchar buf[POPBUFSIZE];
95
96         if (pop3_ok(sock, buf) == PS_SUCCESS) {
97                 state->greeting = g_strdup(buf);
98 #if USE_SSL
99                 if (state->ac_prefs->ssl_pop == SSL_STARTTLS)
100                         return POP3_STLS_SEND;
101 #endif
102                 if (state->ac_prefs->protocol == A_APOP)
103                         return POP3_GETAUTH_APOP_SEND;
104                 else
105                         return POP3_GETAUTH_USER_SEND;
106         } else {
107                 state->inc_state = INC_ERROR;
108                 return -1;
109         }
110 }
111
112 #if USE_SSL
113 gint pop3_stls_send(SockInfo *sock, gpointer data)
114 {
115         pop3_gen_send(sock, "STLS");
116
117         return POP3_STLS_RECV;
118 }
119
120 gint pop3_stls_recv(SockInfo *sock, gpointer data)
121 {
122         Pop3State *state = (Pop3State *)data;
123         gint ok;
124
125         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
126                 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1)) {
127                         state->error_val = PS_SOCKET;
128                         state->inc_state = INC_ERROR;
129                         return -1;
130                 }
131                 if (state->ac_prefs->protocol == A_APOP)
132                         return POP3_GETAUTH_APOP_SEND;
133                 else
134                         return POP3_GETAUTH_USER_SEND;
135         } else if (ok == PS_PROTOCOL) {
136                 log_warning(_("can't start TLS session\n"));
137                 state->error_val = PS_PROTOCOL;
138                 state->inc_state = INC_ERROR;
139                 return POP3_LOGOUT_SEND;
140         } else {
141                 state->inc_state = INC_ERROR;
142                 return -1;
143         }
144 }
145 #endif /* USE_SSL */
146
147 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
148 {
149         Pop3State *state = (Pop3State *)data;
150
151         g_return_val_if_fail(state->user != NULL, -1);
152
153         pop3_gen_send(sock, "USER %s", state->user);
154
155         return POP3_GETAUTH_USER_RECV;
156 }
157
158 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
159 {
160         Pop3State *state = (Pop3State *)data;
161
162         if (pop3_ok(sock, NULL) == PS_SUCCESS)
163                 return POP3_GETAUTH_PASS_SEND;
164         else {
165                 log_warning(_("error occurred on authentication\n"));
166                 state->error_val = PS_AUTHFAIL;
167                 state->inc_state = INC_AUTH_FAILED;
168                 return -1;
169         }
170 }
171
172 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
173 {
174         Pop3State *state = (Pop3State *)data;
175
176         g_return_val_if_fail(state->pass != NULL, -1);
177
178         pop3_gen_send(sock, "PASS %s", state->pass);
179
180         return POP3_GETAUTH_PASS_RECV;
181 }
182
183 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
184 {
185         Pop3State *state = (Pop3State *)data;
186
187         if (pop3_ok(sock, NULL) == PS_SUCCESS)
188                 return POP3_GETRANGE_STAT_SEND;
189         else {
190                 log_warning(_("error occurred on authentication\n"));
191                 state->error_val = PS_AUTHFAIL;
192                 state->inc_state = INC_AUTH_FAILED;
193                 return -1;
194         }
195 }
196
197 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
198 {
199         Pop3State *state = (Pop3State *)data;
200         gchar *start, *end;
201         gchar *apop_str;
202         gchar md5sum[33];
203
204         g_return_val_if_fail(state->user != NULL, -1);
205         g_return_val_if_fail(state->pass != NULL, -1);
206
207         if ((start = strchr(state->greeting, '<')) == NULL) {
208                 log_warning(_("Required APOP timestamp not found "
209                               "in greeting\n"));
210                 state->error_val = PS_PROTOCOL;
211                 state->inc_state = INC_ERROR;
212                 return -1;
213         }
214
215         if ((end = strchr(start, '>')) == NULL || end == start + 1) {
216                 log_warning(_("Timestamp syntax error in greeting\n"));
217                 state->error_val = PS_PROTOCOL;
218                 state->inc_state = INC_ERROR;
219                 return -1;
220         }
221
222         *(end + 1) = '\0';
223
224         apop_str = g_strconcat(start, state->pass, NULL);
225         md5_hex_digest(md5sum, apop_str);
226         g_free(apop_str);
227
228         pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
229
230         return POP3_GETAUTH_APOP_RECV;
231 }
232
233 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
234 {
235         Pop3State *state = (Pop3State *)data;
236
237         if (pop3_ok(sock, NULL) == PS_SUCCESS)
238                 return POP3_GETRANGE_STAT_SEND;
239         else {
240                 log_warning(_("error occurred on authentication\n"));
241                 state->error_val = PS_AUTHFAIL;
242                 state->inc_state = INC_AUTH_FAILED;
243                 return -1;
244         }
245 }
246
247 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
248 {
249         pop3_gen_send(sock, "STAT");
250
251         return POP3_GETRANGE_STAT_RECV;
252 }
253
254 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
255 {
256         Pop3State *state = (Pop3State *)data;
257         gchar buf[POPBUFSIZE + 1];
258         gint ok;
259
260         if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
261                 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
262                     != 2) {
263                         log_warning(_("POP3 protocol error\n"));
264                         state->error_val = PS_PROTOCOL;
265                         state->inc_state = INC_ERROR;
266                         return -1;
267                 } else {
268                         if (state->count == 0) {
269                                 state->uidl_is_valid = TRUE;
270                                 return POP3_LOGOUT_SEND;
271                         } else {
272                                 state->msg = g_new0
273                                         (Pop3MsgInfo, state->count + 1);
274                                 state->cur_msg = 1;
275                                 return POP3_GETRANGE_UIDL_SEND;
276                         }
277                 }
278         } else if (ok == PS_PROTOCOL) {
279                 return POP3_LOGOUT_SEND;
280         } else {
281                 state->inc_state = INC_ERROR;
282                 return -1;
283         }
284 }
285
286 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
287 {
288         pop3_gen_send(sock, "LAST");
289
290         return POP3_GETRANGE_LAST_RECV;
291 }
292
293 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
294 {
295         Pop3State *state = (Pop3State *)data;
296         gchar buf[POPBUFSIZE + 1];
297
298         if (pop3_ok(sock, buf) == PS_SUCCESS) {
299                 gint last;
300
301                 if (sscanf(buf, "%d", &last) == 0) {
302                         log_warning(_("POP3 protocol error\n"));
303                         state->error_val = PS_PROTOCOL;
304                         state->inc_state = INC_ERROR;
305                         return -1;
306                 } else {
307                         if (state->count == last)
308                                 return POP3_LOGOUT_SEND;
309                         else {
310                                 state->cur_msg = last + 1;
311                                 return POP3_GETSIZE_LIST_SEND;
312                         }
313                 }
314         } else
315                 return POP3_GETSIZE_LIST_SEND;
316 }
317
318 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
319 {
320         pop3_gen_send(sock, "UIDL");
321
322         return POP3_GETRANGE_UIDL_RECV;
323 }
324
325 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
326 {
327         Pop3State *state = (Pop3State *)data;
328         gboolean new = FALSE;
329         gboolean get_all = FALSE;
330         gchar buf[POPBUFSIZE];
331         gchar id[IDLEN + 1];
332         gint len;
333         gint next_state;
334
335         if (!state->uidl_table) new = TRUE;
336         if (state->ac_prefs->getall ||
337             (state->ac_prefs->rmmail && state->ac_prefs->msg_leave_time == 0))
338                 get_all = TRUE;
339
340         if (pop3_ok(sock, NULL) != PS_SUCCESS) {
341                 /* UIDL is not supported */
342                 if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
343                         return next_state;
344
345                 if (!get_all)
346                         return POP3_GETRANGE_LAST_SEND;
347                 else
348                         return POP3_GETSIZE_LIST_SEND;
349         }
350
351         while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
352                 gint num;
353                 time_t recv_time;
354
355                 if (buf[0] == '.') break;
356                 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
357                         continue;
358                 if (num <= 0 || num > state->count) continue;
359
360                 state->msg[num].uidl = g_strdup(id);
361
362                 if (!state->uidl_table) continue;
363
364                 recv_time = (time_t)g_hash_table_lookup(state->uidl_table, id);
365                 state->msg[num].recv_time = recv_time;
366
367                 if (!get_all && recv_time != 0)
368                         state->msg[num].received = TRUE;
369
370                 if (new == FALSE &&
371                     (get_all || recv_time == 0 || state->ac_prefs->rmmail)) {
372                         state->cur_msg = num;
373                         new = TRUE;
374                 }
375         }
376
377         if (len < 0) {
378                 log_error(_("Socket error\n"));
379                 state->error_val = PS_SOCKET;
380                 state->inc_state = INC_ERROR;
381                 return -1;
382         }
383
384         state->uidl_is_valid = TRUE;
385         if (pop3_sd_state(state, POP3_GETRANGE_UIDL_RECV, &next_state))
386                 return next_state;
387
388         if (new == TRUE)
389                 return POP3_GETSIZE_LIST_SEND;
390         else
391                 return POP3_LOGOUT_SEND;
392 }
393
394 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
395 {
396         pop3_gen_send(sock, "LIST");
397
398         return POP3_GETSIZE_LIST_RECV;
399 }
400
401 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
402 {
403         Pop3State *state = (Pop3State *)data;
404         gchar buf[POPBUFSIZE];
405         gint len;
406         gint next_state;
407
408         if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
409
410         state->cur_total_bytes = 0;
411
412         while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
413                 guint num, size;
414
415                 if (buf[0] == '.') break;
416                 if (sscanf(buf, "%u %u", &num, &size) != 2) {
417                         state->error_val = PS_PROTOCOL;
418                         state->inc_state = INC_ERROR;
419                         return -1;
420                 }
421
422                 if (num > 0 && num <= state->count)
423                         state->msg[num].size = size;
424                 if (num > 0 && num < state->cur_msg)
425                         state->cur_total_bytes += size;
426         }
427
428         if (len < 0) {
429                 log_error(_("Socket error\n"));
430                 state->error_val = PS_SOCKET;
431                 state->inc_state = INC_ERROR;
432                 return -1;
433         }
434
435         if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
436                 return next_state;
437
438         LOOKUP_NEXT_MSG();
439         return POP3_RETR_SEND;
440 }
441  
442 gint pop3_top_send(SockInfo *sock, gpointer data)
443 {
444         Pop3State *state = (Pop3State *)data;
445
446         inc_progress_update(state, POP3_TOP_SEND); 
447
448         pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
449
450         return POP3_TOP_RECV;
451 }
452
453 gint pop3_top_recv(SockInfo *sock, gpointer data)
454 {
455         Pop3State *state = (Pop3State *)data;
456         gchar *filename, *path;
457         gint next_state;
458         
459         if (pop3_ok(sock, NULL) != PS_SUCCESS) 
460                 return POP3_LOGOUT_SEND;
461
462         path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
463
464         if ( !is_dir_exist(path) )
465                 make_dir_hier(path);
466         
467         filename = g_strdup_printf("%s%i", path, state->cur_msg);
468                                    
469         if (recv_write_to_file(sock, filename) < 0) {
470                 state->inc_state = INC_NOSPACE;
471                 return -1;
472         }
473
474         pop3_sd_state(state, POP3_TOP_RECV, &next_state);
475         
476         if (state->cur_msg < state->count) {
477                 state->cur_msg++;
478                 return POP3_TOP_SEND;
479         } else
480                 return POP3_LOGOUT_SEND;
481 }
482
483 gint pop3_retr_send(SockInfo *sock, gpointer data)
484 {
485         Pop3State *state = (Pop3State *)data;
486
487         pop3_gen_send(sock, "RETR %d", state->cur_msg);
488
489         return POP3_RETR_RECV;
490 }
491
492 gint pop3_retr_recv(SockInfo *sock, gpointer data)
493 {
494         Pop3State *state = (Pop3State *)data;
495         gchar *file;
496         gint ok, drop_ok;
497         gint next_state;
498         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
499                 file = get_tmp_file();
500                 if (recv_write_to_file(sock, file) < 0) {
501                         g_free(file);
502                         if (state->inc_state == INC_SUCCESS)
503                                 state->inc_state = INC_NOSPACE;
504                         return -1;
505                 }
506
507                 drop_ok = inc_drop_message(file, state);
508                 g_free(file);
509                 if (drop_ok < 0) {
510                         state->inc_state = INC_ERROR;
511                         return -1;
512                 }
513
514                 if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
515                         return next_state;
516         
517                 state->cur_total_bytes += state->msg[state->cur_msg].size;
518                 state->cur_total_num++;
519
520                 state->msg[state->cur_msg].received = TRUE;
521                 state->msg[state->cur_msg].recv_time = state->current_time;
522
523                 if (state->ac_prefs->rmmail &&
524                     state->ac_prefs->msg_leave_time == 0)
525                         return POP3_DELETE_SEND;
526
527                 if (state->cur_msg < state->count) {
528                         state->cur_msg++;
529                         LOOKUP_NEXT_MSG();
530                         return POP3_RETR_SEND;
531                 } else
532                         return POP3_LOGOUT_SEND;
533         } else if (ok == PS_PROTOCOL) {
534                 return POP3_LOGOUT_SEND;
535         } else {
536                 state->inc_state = INC_ERROR;
537                 return -1;
538         }
539 }
540
541 gint pop3_delete_send(SockInfo *sock, gpointer data)
542 {
543         Pop3State *state = (Pop3State *)data;
544
545         pop3_gen_send(sock, "DELE %d", state->cur_msg);
546
547         return POP3_DELETE_RECV;
548 }
549
550 gint pop3_delete_recv(SockInfo *sock, gpointer data)
551 {
552         Pop3State *state = (Pop3State *)data;
553         gint next_state;
554         gint ok;
555
556         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
557                 state->msg[state->cur_msg].deleted = TRUE;
558                 
559                 if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
560                         return next_state;      
561
562                 if (state->cur_msg < state->count) {
563                         state->cur_msg++;
564                         LOOKUP_NEXT_MSG();
565                         return POP3_RETR_SEND;
566                 } else
567                         return POP3_LOGOUT_SEND;
568         } else if (ok == PS_PROTOCOL) {
569                 return POP3_LOGOUT_SEND;
570         } else {
571                 state->inc_state = INC_ERROR;
572                 return -1;
573         }
574 }
575
576 gint pop3_logout_send(SockInfo *sock, gpointer data)
577 {
578         pop3_gen_send(sock, "QUIT");
579
580         return POP3_LOGOUT_RECV;
581 }
582
583 gint pop3_logout_recv(SockInfo *sock, gpointer data)
584 {
585         Pop3State *state = (Pop3State *)data;
586
587         if (pop3_ok(sock, NULL) != PS_SUCCESS)
588                 state->inc_state = INC_ERROR;
589
590         return -1;
591 }
592
593 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
594 {
595         gint ok;
596         gchar buf[POPBUFSIZE + 1];
597         gchar *bufp;
598
599         if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
600                 bufp = buf;
601                 if (*bufp == '+' || *bufp == '-')
602                         bufp++;
603                 else
604                         return PS_PROTOCOL;
605
606                 while (isalpha(*bufp))
607                         bufp++;
608
609                 if (*bufp)
610                         *(bufp++) = '\0';
611
612                 if (!strcmp(buf, "+OK"))
613                         ok = PS_SUCCESS;
614                 else if (!strncmp(buf, "-ERR", 4)) {
615                         if (strstr(bufp, "lock") ||
616                                  strstr(bufp, "Lock") ||
617                                  strstr(bufp, "LOCK") ||
618                                  strstr(bufp, "wait"))
619                                 ok = PS_LOCKBUSY;
620                         else
621                                 ok = PS_PROTOCOL;
622
623                         if (*bufp)
624                                 fprintf(stderr, "POP3: %s\n", bufp);
625                 } else
626                         ok = PS_PROTOCOL;
627
628                 if (argbuf)
629                         strcpy(argbuf, bufp);
630         }
631
632         return ok;
633 }
634
635 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
636 {
637         gchar buf[POPBUFSIZE + 1];
638         va_list args;
639
640         va_start(args, format);
641         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
642         va_end(args);
643
644         if (!strncasecmp(buf, "PASS ", 5))
645                 log_print("POP3> PASS ********\n");
646         else
647                 log_print("POP3> %s\n", buf);
648
649         strcat(buf, "\r\n");
650         sock_write(sock, buf, strlen(buf));
651 }
652
653 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
654 {
655         if (sock_gets(sock, buf, size) < 0) {
656                 return PS_SOCKET;
657         } else {
658                 strretchomp(buf);
659                 log_print("POP3< %s\n", buf);
660
661                 return PS_SUCCESS;
662         }
663 }
664
665 static void pop3_sd_new_header(Pop3State *state)
666 {
667         HeaderItems *new_msg;
668         if (state->cur_msg <= state->count) {
669                 new_msg = g_new0(HeaderItems, 1); 
670                 
671                 new_msg->index              = state->cur_msg;
672                 new_msg->state              = SD_UNCHECKED;
673                 new_msg->size               = state->msg[state->cur_msg].size; 
674                 new_msg->received           = state->msg[state->cur_msg].received;
675                 new_msg->del_by_old_session = FALSE;
676                 
677                 state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list, 
678                                                            new_msg);
679         }
680 }
681
682 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state) 
683 {
684         gint session = state->ac_prefs->session;
685         guint goto_state = -1;
686
687         switch (cur_state) { 
688         case POP3_GETRANGE_UIDL_RECV:
689                 switch (session) {
690                 case STYPE_DOWNLOAD:
691                 case STYPE_DELETE:
692                 case STYPE_PREVIEW_ALL:
693                         goto_state = POP3_GETSIZE_LIST_SEND;
694                 default:
695                         break;
696                 }
697                 break;
698         case POP3_GETSIZE_LIST_RECV:
699                 switch (session) {
700                 case STYPE_PREVIEW_ALL:
701                         state->cur_msg = 1;
702                 case STYPE_PREVIEW_NEW:
703                         goto_state = POP3_TOP_SEND;
704                         break;
705                 case STYPE_DELETE:
706                         if (pop3_sd_get_next(state))
707                                 goto_state = POP3_DELETE_SEND;          
708                         else
709                                 goto_state = POP3_LOGOUT_SEND;
710                         break;
711                 case STYPE_DOWNLOAD:
712                         if (pop3_sd_get_next(state))
713                                 goto_state = POP3_RETR_SEND;
714                         else
715                                 goto_state = POP3_LOGOUT_SEND;
716                 default:
717                         break;
718                 }
719                 break;
720         case POP3_TOP_RECV: 
721                 switch (session) { 
722                 case STYPE_PREVIEW_ALL:
723                 case STYPE_PREVIEW_NEW:
724                         pop3_sd_new_header(state);
725                 default:
726                         break;
727                 }
728                 break;
729         case POP3_RETR_RECV:
730                 switch (session) {
731                 case STYPE_DOWNLOAD:
732                         if (state->ac_prefs->sd_rmmail_on_download) 
733                                 goto_state = POP3_DELETE_SEND;
734                         else {
735                                 if (pop3_sd_get_next(state)) 
736                                         goto_state = POP3_RETR_SEND;
737                                 else
738                                         goto_state = POP3_LOGOUT_SEND;
739                         }
740                 default:        
741                         break;
742                 }
743                 break;
744         case POP3_DELETE_RECV:
745                 switch (session) {
746                 case STYPE_DELETE:
747                         if (pop3_sd_get_next(state)) 
748                                 goto_state = POP3_DELETE_SEND;
749                         else
750                                 goto_state =  POP3_LOGOUT_SEND;
751                         break;
752                 case STYPE_DOWNLOAD:
753                         if (pop3_sd_get_next(state)) 
754                                 goto_state = POP3_RETR_SEND;
755                         else
756                                 goto_state = POP3_LOGOUT_SEND;
757                 default:
758                         break;
759                 }
760         default:
761                 break;
762                 
763         }                 
764
765         *next_state = goto_state;
766         if (goto_state != -1)
767                 return TRUE;
768         else 
769                 return FALSE;
770 }
771
772 gboolean pop3_sd_get_next(Pop3State *state)
773 {
774         GSList *cur;
775         gint deleted_msgs = 0;
776         
777         switch (state->ac_prefs->session) {
778         case STYPE_DOWNLOAD:
779         case STYPE_DELETE:      
780                 for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
781                         HeaderItems *items = (HeaderItems*)cur->data;
782
783                         if (items->del_by_old_session)
784                                 deleted_msgs++;
785
786                         switch (items->state) {
787                         case SD_REMOVE:
788                                 items->state = SD_REMOVED;
789                                 break;
790                         case SD_DOWNLOAD:
791                                 items->state = SD_DOWNLOADED;
792                                 break;
793                         case SD_CHECKED:
794                                 state->cur_msg = items->index - deleted_msgs;
795                                 if (state->ac_prefs->session == STYPE_DELETE)
796                                         items->state = SD_REMOVE;
797                                 else
798                                         items->state = SD_DOWNLOAD;
799                                 return TRUE;
800                         default:
801                                 break;
802                         }
803                 }
804                 return FALSE;
805         default:
806                 return FALSE;
807         }
808 }