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