fce679ed65a49ba60f56cdc55f00e76bf666262d
[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         state->cur_total_recv_bytes = 0;
412
413         while ((len = sock_gets(sock, buf, sizeof(buf))) > 0) {
414                 guint num, size;
415
416                 if (buf[0] == '.') break;
417                 if (sscanf(buf, "%u %u", &num, &size) != 2) {
418                         state->error_val = PS_PROTOCOL;
419                         state->inc_state = INC_ERROR;
420                         return -1;
421                 }
422
423                 if (num > 0 && num <= state->count)
424                         state->msg[num].size = size;
425                 if (num > 0 && num < state->cur_msg)
426                         state->cur_total_bytes += size;
427         }
428
429         if (len < 0) {
430                 log_error(_("Socket error\n"));
431                 state->error_val = PS_SOCKET;
432                 state->inc_state = INC_ERROR;
433                 return -1;
434         }
435
436         if (pop3_sd_state(state, POP3_GETSIZE_LIST_RECV, &next_state))
437                 return next_state;
438
439         LOOKUP_NEXT_MSG();
440         return POP3_RETR_SEND;
441 }
442  
443 gint pop3_top_send(SockInfo *sock, gpointer data)
444 {
445         Pop3State *state = (Pop3State *)data;
446
447         inc_progress_update(state, POP3_TOP_SEND); 
448
449         pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
450
451         return POP3_TOP_RECV;
452 }
453
454 gint pop3_top_recv(SockInfo *sock, gpointer data)
455 {
456         Pop3State *state = (Pop3State *)data;
457         gchar *filename, *path;
458         gint next_state;
459         
460         if (pop3_ok(sock, NULL) != PS_SUCCESS) 
461                 return POP3_LOGOUT_SEND;
462
463         path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
464
465         if ( !is_dir_exist(path) )
466                 make_dir_hier(path);
467         
468         filename = g_strdup_printf("%s%i", path, state->cur_msg);
469                                    
470         if (recv_write_to_file(sock, filename) < 0) {
471                 state->inc_state = INC_NOSPACE;
472                 return -1;
473         }
474
475         pop3_sd_state(state, POP3_TOP_RECV, &next_state);
476         
477         if (state->cur_msg < state->count) {
478                 state->cur_msg++;
479                 return POP3_TOP_SEND;
480         } else
481                 return POP3_LOGOUT_SEND;
482 }
483
484 gint pop3_retr_send(SockInfo *sock, gpointer data)
485 {
486         Pop3State *state = (Pop3State *)data;
487
488         pop3_gen_send(sock, "RETR %d", state->cur_msg);
489
490         return POP3_RETR_RECV;
491 }
492
493 gint pop3_retr_recv(SockInfo *sock, gpointer data)
494 {
495         Pop3State *state = (Pop3State *)data;
496         gchar *file;
497         gint ok, drop_ok;
498         gint next_state;
499         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
500                 file = get_tmp_file();
501                 if (recv_write_to_file(sock, file) < 0) {
502                         g_free(file);
503                         if (state->inc_state == INC_SUCCESS)
504                                 state->inc_state = INC_NOSPACE;
505                         return -1;
506                 }
507
508                 drop_ok = inc_drop_message(file, state);
509                 g_free(file);
510                 if (drop_ok < 0) {
511                         state->inc_state = INC_ERROR;
512                         return -1;
513                 }
514
515                 if (pop3_sd_state(state, POP3_RETR_RECV, &next_state))
516                         return next_state;
517         
518                 state->cur_total_bytes += state->msg[state->cur_msg].size;
519                 state->cur_total_recv_bytes += state->msg[state->cur_msg].size;
520                 state->cur_total_num++;
521
522                 state->msg[state->cur_msg].received = TRUE;
523                 state->msg[state->cur_msg].recv_time = state->current_time;
524
525                 if (state->ac_prefs->rmmail &&
526                     state->ac_prefs->msg_leave_time == 0)
527                         return POP3_DELETE_SEND;
528
529                 if (state->cur_msg < state->count) {
530                         state->cur_msg++;
531                         LOOKUP_NEXT_MSG();
532                         return POP3_RETR_SEND;
533                 } else
534                         return POP3_LOGOUT_SEND;
535         } else if (ok == PS_PROTOCOL) {
536                 return POP3_LOGOUT_SEND;
537         } else {
538                 state->inc_state = INC_ERROR;
539                 return -1;
540         }
541 }
542
543 gint pop3_delete_send(SockInfo *sock, gpointer data)
544 {
545         Pop3State *state = (Pop3State *)data;
546
547         pop3_gen_send(sock, "DELE %d", state->cur_msg);
548
549         return POP3_DELETE_RECV;
550 }
551
552 gint pop3_delete_recv(SockInfo *sock, gpointer data)
553 {
554         Pop3State *state = (Pop3State *)data;
555         gint next_state;
556         gint ok;
557
558         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
559                 state->msg[state->cur_msg].deleted = TRUE;
560                 
561                 if (pop3_sd_state(state, POP3_DELETE_RECV, &next_state))
562                         return next_state;      
563
564                 if (state->cur_msg < state->count) {
565                         state->cur_msg++;
566                         LOOKUP_NEXT_MSG();
567                         return POP3_RETR_SEND;
568                 } else
569                         return POP3_LOGOUT_SEND;
570         } else if (ok == PS_PROTOCOL) {
571                 return POP3_LOGOUT_SEND;
572         } else {
573                 state->inc_state = INC_ERROR;
574                 return -1;
575         }
576 }
577
578 gint pop3_logout_send(SockInfo *sock, gpointer data)
579 {
580         pop3_gen_send(sock, "QUIT");
581
582         return POP3_LOGOUT_RECV;
583 }
584
585 gint pop3_logout_recv(SockInfo *sock, gpointer data)
586 {
587         Pop3State *state = (Pop3State *)data;
588
589         if (pop3_ok(sock, NULL) != PS_SUCCESS)
590                 state->inc_state = INC_ERROR;
591
592         return -1;
593 }
594
595 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
596 {
597         gint ok;
598         gchar buf[POPBUFSIZE + 1];
599         gchar *bufp;
600
601         if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
602                 bufp = buf;
603                 if (*bufp == '+' || *bufp == '-')
604                         bufp++;
605                 else
606                         return PS_PROTOCOL;
607
608                 while (isalpha(*bufp))
609                         bufp++;
610
611                 if (*bufp)
612                         *(bufp++) = '\0';
613
614                 if (!strcmp(buf, "+OK"))
615                         ok = PS_SUCCESS;
616                 else if (!strncmp(buf, "-ERR", 4)) {
617                         if (strstr(bufp, "lock") ||
618                                  strstr(bufp, "Lock") ||
619                                  strstr(bufp, "LOCK") ||
620                                  strstr(bufp, "wait"))
621                                 ok = PS_LOCKBUSY;
622                         else
623                                 ok = PS_PROTOCOL;
624
625                         if (*bufp)
626                                 fprintf(stderr, "POP3: %s\n", bufp);
627                 } else
628                         ok = PS_PROTOCOL;
629
630                 if (argbuf)
631                         strcpy(argbuf, bufp);
632         }
633
634         return ok;
635 }
636
637 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
638 {
639         gchar buf[POPBUFSIZE + 1];
640         va_list args;
641
642         va_start(args, format);
643         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
644         va_end(args);
645
646         if (!strncasecmp(buf, "PASS ", 5))
647                 log_print("POP3> PASS ********\n");
648         else
649                 log_print("POP3> %s\n", buf);
650
651         strcat(buf, "\r\n");
652         sock_write(sock, buf, strlen(buf));
653 }
654
655 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
656 {
657         if (sock_gets(sock, buf, size) < 0) {
658                 return PS_SOCKET;
659         } else {
660                 strretchomp(buf);
661                 log_print("POP3< %s\n", buf);
662
663                 return PS_SUCCESS;
664         }
665 }
666
667 static void pop3_sd_new_header(Pop3State *state)
668 {
669         HeaderItems *new_msg;
670         if (state->cur_msg <= state->count) {
671                 new_msg = g_new0(HeaderItems, 1); 
672                 
673                 new_msg->index              = state->cur_msg;
674                 new_msg->state              = SD_UNCHECKED;
675                 new_msg->size               = state->msg[state->cur_msg].size; 
676                 new_msg->received           = state->msg[state->cur_msg].received;
677                 new_msg->del_by_old_session = FALSE;
678                 
679                 state->ac_prefs->msg_list = g_slist_append(state->ac_prefs->msg_list, 
680                                                            new_msg);
681         }
682 }
683
684 gboolean pop3_sd_state(Pop3State *state, gint cur_state, guint *next_state) 
685 {
686         gint session = state->ac_prefs->session;
687         guint goto_state = -1;
688
689         switch (cur_state) { 
690         case POP3_GETRANGE_UIDL_RECV:
691                 switch (session) {
692                 case STYPE_DOWNLOAD:
693                 case STYPE_DELETE:
694                 case STYPE_PREVIEW_ALL:
695                         goto_state = POP3_GETSIZE_LIST_SEND;
696                 default:
697                         break;
698                 }
699                 break;
700         case POP3_GETSIZE_LIST_RECV:
701                 switch (session) {
702                 case STYPE_PREVIEW_ALL:
703                         state->cur_msg = 1;
704                 case STYPE_PREVIEW_NEW:
705                         goto_state = POP3_TOP_SEND;
706                         break;
707                 case STYPE_DELETE:
708                         if (pop3_sd_get_next(state))
709                                 goto_state = POP3_DELETE_SEND;          
710                         else
711                                 goto_state = POP3_LOGOUT_SEND;
712                         break;
713                 case STYPE_DOWNLOAD:
714                         if (pop3_sd_get_next(state))
715                                 goto_state = POP3_RETR_SEND;
716                         else
717                                 goto_state = POP3_LOGOUT_SEND;
718                 default:
719                         break;
720                 }
721                 break;
722         case POP3_TOP_RECV: 
723                 switch (session) { 
724                 case STYPE_PREVIEW_ALL:
725                 case STYPE_PREVIEW_NEW:
726                         pop3_sd_new_header(state);
727                 default:
728                         break;
729                 }
730                 break;
731         case POP3_RETR_RECV:
732                 switch (session) {
733                 case STYPE_DOWNLOAD:
734                         if (state->ac_prefs->sd_rmmail_on_download) 
735                                 goto_state = POP3_DELETE_SEND;
736                         else {
737                                 if (pop3_sd_get_next(state)) 
738                                         goto_state = POP3_RETR_SEND;
739                                 else
740                                         goto_state = POP3_LOGOUT_SEND;
741                         }
742                 default:        
743                         break;
744                 }
745                 break;
746         case POP3_DELETE_RECV:
747                 switch (session) {
748                 case STYPE_DELETE:
749                         if (pop3_sd_get_next(state)) 
750                                 goto_state = POP3_DELETE_SEND;
751                         else
752                                 goto_state =  POP3_LOGOUT_SEND;
753                         break;
754                 case STYPE_DOWNLOAD:
755                         if (pop3_sd_get_next(state)) 
756                                 goto_state = POP3_RETR_SEND;
757                         else
758                                 goto_state = POP3_LOGOUT_SEND;
759                 default:
760                         break;
761                 }
762         default:
763                 break;
764                 
765         }                 
766
767         *next_state = goto_state;
768         if (goto_state != -1)
769                 return TRUE;
770         else 
771                 return FALSE;
772 }
773
774 gboolean pop3_sd_get_next(Pop3State *state)
775 {
776         GSList *cur;
777         gint deleted_msgs = 0;
778         
779         switch (state->ac_prefs->session) {
780         case STYPE_DOWNLOAD:
781         case STYPE_DELETE:      
782                 for (cur = state->ac_prefs->msg_list; cur != NULL; cur = cur->next) {
783                         HeaderItems *items = (HeaderItems*)cur->data;
784
785                         if (items->del_by_old_session)
786                                 deleted_msgs++;
787
788                         switch (items->state) {
789                         case SD_REMOVE:
790                                 items->state = SD_REMOVED;
791                                 break;
792                         case SD_DOWNLOAD:
793                                 items->state = SD_DOWNLOADED;
794                                 break;
795                         case SD_CHECKED:
796                                 state->cur_msg = items->index - deleted_msgs;
797                                 if (state->ac_prefs->session == STYPE_DELETE)
798                                         items->state = SD_REMOVE;
799                                 else
800                                         items->state = SD_DOWNLOAD;
801                                 return TRUE;
802                         default:
803                                 break;
804                         }
805                 }
806                 return FALSE;
807         default:
808                 return FALSE;
809         }
810 }