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