add 'Selective download'
[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
31 #include "intl.h"
32 #include "pop.h"
33 #include "socket.h"
34 #include "md5.h"
35 #include "prefs_account.h"
36 #include "utils.h"
37 #include "inc.h"
38 #include "recv.h"
39 #include "selective_download.h"
40
41 #define LOOKUP_NEXT_MSG() \
42         for (;;) { \
43                 gint size = state->sizes[state->cur_msg]; \
44  \
45                 if (size == 0 || \
46                     (state->ac_prefs->enable_size_limit && \
47                      state->ac_prefs->size_limit > 0 && \
48                      size > state->ac_prefs->size_limit * 1024)) { \
49                         log_print(_("Skipping message %d\n"), state->cur_msg); \
50                         if (size > 0) { \
51                                 state->cur_total_bytes += size; \
52                                 state->cur_total_num++; \
53                                 if (state->new_id_list) { \
54                                         state->id_list = g_slist_append(state->id_list, state->new_id_list->data); \
55                                         state->new_id_list = g_slist_remove(state->new_id_list, state->new_id_list->data); \
56                                 } \
57                         } \
58                         if (state->cur_msg == state->count) \
59                                 return POP3_LOGOUT_SEND; \
60                         else \
61                                 state->cur_msg++; \
62                 } else \
63                         break; \
64         }
65
66 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
67 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
68 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
69 static gboolean pop3_delete_header (Pop3State *state);
70
71 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
72 {
73         Pop3State *state = (Pop3State *)data;
74         gchar buf[POPBUFSIZE];
75
76         if (pop3_ok(sock, buf) == PS_SUCCESS) {
77                 if (state->ac_prefs->protocol == A_APOP) {
78                         state->greeting = g_strdup(buf);
79                         return POP3_GETAUTH_APOP_SEND;
80                 } else
81                         return POP3_GETAUTH_USER_SEND;
82         } else
83                 return -1;
84 }
85
86 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
87 {
88         Pop3State *state = (Pop3State *)data;
89
90         g_return_val_if_fail(state->user != NULL, -1);
91
92         inc_progress_update(state, POP3_GETAUTH_USER_SEND);
93
94         pop3_gen_send(sock, "USER %s", state->user);
95
96         return POP3_GETAUTH_USER_RECV;
97 }
98
99 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
100 {
101         Pop3State *state = (Pop3State *)data;
102
103         if (pop3_ok(sock, NULL) == PS_SUCCESS)
104                 return POP3_GETAUTH_PASS_SEND;
105         else {
106                 log_warning(_("error occurred on authentication\n"));
107                 state->error_val = PS_AUTHFAIL;
108                 state->inc_state = INC_AUTH_FAILED;
109                 return -1;
110         }
111 }
112
113 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
114 {
115         Pop3State *state = (Pop3State *)data;
116
117         g_return_val_if_fail(state->pass != NULL, -1);
118
119         pop3_gen_send(sock, "PASS %s", state->pass);
120
121         return POP3_GETAUTH_PASS_RECV;
122 }
123
124 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
125 {
126         Pop3State *state = (Pop3State *)data;
127
128         if (pop3_ok(sock, NULL) == PS_SUCCESS) {
129                 
130                 if (pop3_delete_header(state) == TRUE)
131                         return POP3_DELETE_SEND;
132                 else
133                         return POP3_GETRANGE_STAT_SEND;
134         }
135         else {
136                 log_warning(_("error occurred on authentication\n"));
137                 state->error_val = PS_AUTHFAIL;
138                 state->inc_state = INC_AUTH_FAILED;
139                 return -1;
140         }
141 }
142
143 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
144 {
145         Pop3State *state = (Pop3State *)data;
146         gchar *start, *end;
147         gchar *apop_str;
148         gchar md5sum[33];
149
150         g_return_val_if_fail(state->user != NULL, -1);
151         g_return_val_if_fail(state->pass != NULL, -1);
152
153         inc_progress_update(state, POP3_GETAUTH_APOP_SEND);
154
155         if ((start = strchr(state->greeting, '<')) == NULL) {
156                 log_warning(_("Required APOP timestamp not found "
157                               "in greeting\n"));
158                 return -1;
159         }
160
161         if ((end = strchr(start, '>')) == NULL || end == start + 1) {
162                 log_warning(_("Timestamp syntax error in greeting\n"));
163                 return -1;
164         }
165
166         *(end + 1) = '\0';
167
168         apop_str = g_strconcat(start, state->pass, NULL);
169         md5_hex_digest(md5sum, apop_str);
170         g_free(apop_str);
171
172         pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
173
174         return POP3_GETAUTH_APOP_RECV;
175 }
176
177 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
178 {
179         Pop3State *state = (Pop3State *)data;
180
181         if (pop3_ok(sock, NULL) == PS_SUCCESS) {
182                 
183                 if (pop3_delete_header(state) == TRUE)
184                         return POP3_DELETE_SEND;
185                 else
186                 return POP3_GETRANGE_STAT_SEND;
187         }
188
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_getrange_stat_send(SockInfo *sock, gpointer data)
198 {
199         Pop3State *state = (Pop3State *)data;
200
201         inc_progress_update(state, POP3_GETRANGE_STAT_SEND);
202
203         pop3_gen_send(sock, "STAT");
204
205         return POP3_GETRANGE_STAT_RECV;
206 }
207
208 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
209 {
210         Pop3State *state = (Pop3State *)data;
211         gchar buf[POPBUFSIZE + 1];
212         gint ok;
213
214         if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
215                 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
216                     != 2) {
217                         log_warning(_("POP3 protocol error\n"));
218                         return -1;
219                 } else {
220                         if (state->count == 0)
221                                 return POP3_LOGOUT_SEND;
222                         else {
223                                 state->cur_msg = 1;
224                                 state->new = state->count;
225                                 if (state->ac_prefs->rmmail ||
226                                     state->ac_prefs->getall)
227                                         return POP3_GETSIZE_LIST_SEND;
228                                 else
229                                         return POP3_GETRANGE_UIDL_SEND;
230                         }
231                 }
232         } else if (ok == PS_PROTOCOL)
233                 return POP3_LOGOUT_SEND;
234         else
235                 return -1;
236 }
237
238 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
239 {
240         Pop3State *state = (Pop3State *)data;
241
242         inc_progress_update(state, POP3_GETRANGE_LAST_SEND);
243
244         pop3_gen_send(sock, "LAST");
245
246         return POP3_GETRANGE_LAST_RECV;
247 }
248
249 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
250 {
251         Pop3State *state = (Pop3State *)data;
252         gchar buf[POPBUFSIZE + 1];
253
254         if (pop3_ok(sock, buf) == PS_SUCCESS) {
255                 gint last;
256
257                 if (sscanf(buf, "%d", &last) == 0) {
258                         log_warning(_("POP3 protocol error\n"));
259                         return -1;
260                 } else {
261                         if (state->count == last)
262                                 return POP3_LOGOUT_SEND;
263                         else {
264                                 state->new = state->count - last;
265                                 state->cur_msg = last + 1;
266                                 return POP3_GETSIZE_LIST_SEND;
267                         }
268                 }
269         } else
270                 return POP3_GETSIZE_LIST_SEND;
271 }
272
273 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
274 {
275         Pop3State *state = (Pop3State *)data;
276
277         inc_progress_update(state, POP3_GETRANGE_UIDL_SEND);
278
279         pop3_gen_send(sock, "UIDL");
280
281         return POP3_GETRANGE_UIDL_RECV;
282 }
283
284 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
285 {
286         Pop3State *state = (Pop3State *)data;
287         gboolean new = FALSE;
288         gchar buf[POPBUFSIZE];
289         gchar id[IDLEN + 1];
290
291         if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
292
293         if (!state->id_table) new = TRUE;
294
295         while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
296                 gint num;
297
298                 if (buf[0] == '.') break;
299                 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
300                         continue;
301
302                 if (new == FALSE &&
303                     g_hash_table_lookup(state->id_table, id) == NULL) {
304                         state->new = state->count - num + 1;
305                         state->cur_msg = num;
306                         new = TRUE;
307                 }
308
309                 if (new == TRUE)
310                         state->new_id_list = g_slist_append
311                                 (state->new_id_list, g_strdup(id));
312                 else
313                         state->id_list = g_slist_append
314                                 (state->id_list, g_strdup(id));
315         }
316
317         if (new == TRUE)
318                 return POP3_GETSIZE_LIST_SEND;
319         else
320                 return POP3_LOGOUT_SEND;
321 }
322
323 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
324 {
325         Pop3State *state = (Pop3State *)data;
326
327         inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
328
329         pop3_gen_send(sock, "LIST");
330
331         return POP3_GETSIZE_LIST_RECV;
332 }
333
334 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
335 {
336         Pop3State *state = (Pop3State *)data;
337         gchar buf[POPBUFSIZE];
338
339         if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
340
341         state->sizes = g_new0(gint, state->count + 1);
342         state->cur_total_bytes = 0;
343
344         while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
345                 guint num, size;
346
347                 if (buf[0] == '.') break;
348                 if (sscanf(buf, "%u %u", &num, &size) != 2)
349                         return -1;
350
351                 if (num > 0 && num <= state->count)
352                         state->sizes[num] = size;
353                 if (num > 0 && num < state->cur_msg)
354                         state->cur_total_bytes += size;
355         }
356
357         LOOKUP_NEXT_MSG();
358         if (state->ac_prefs->session_type == RETR_HEADER) 
359                 return POP3_TOP_SEND;
360         else
361                 return POP3_RETR_SEND;
362  }
363  
364 gint pop3_top_send(SockInfo *sock, gpointer data)
365 {
366         Pop3State *state = (Pop3State *)data;
367
368         inc_progress_update(state, POP3_TOP_SEND); 
369
370         pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
371
372         return POP3_TOP_RECV;
373 }
374
375 gint pop3_top_recv(SockInfo *sock, gpointer data)
376 {
377         Pop3State *state = (Pop3State *)data;
378         FILE  *fp;
379         gchar buf[POPBUFSIZE];
380         gchar *header;
381         gchar *filename, *path;
382         
383         inc_progress_update(state, POP3_TOP_RECV); 
384
385         if (pop3_ok(sock, NULL) != PS_SUCCESS) 
386                 return POP3_LOGOUT_SEND;
387
388         path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
389
390         if ( !is_dir_exist(path) )
391                 make_dir_hier(path);
392         
393         filename = g_strdup_printf("%s%i", path, state->cur_msg);
394                                    
395         if (recv_write_to_file(sock, filename) < 0) {
396                 state->inc_state = INC_NOSPACE;
397                 return -1;
398         }
399         /* we add a Complete-Size Header Item ...
400            note: overwrites first line  --> this is dirty */
401         if ( (fp = fopen(filename, "r+")) != NULL ) {
402                 gchar *buf = g_strdup_printf("%s%i", SIZE_HEADER, 
403                                              state->sizes[state->cur_msg]);
404         
405                 if (change_file_mode_rw(fp, filename) == 0) 
406                         fprintf(fp, "%s\n", buf);
407                 fclose(fp);
408         }
409         
410         g_free(path);
411         g_free(filename);
412
413         if (state->cur_msg < state->count) {
414                 state->cur_msg++;
415                 return POP3_TOP_SEND;
416         } else
417                 return POP3_LOGOUT_SEND;
418 }
419
420 gint pop3_retr_send(SockInfo *sock, gpointer data)
421 {
422         Pop3State *state = (Pop3State *)data;
423
424         inc_progress_update(state, POP3_RETR_SEND);
425
426         pop3_gen_send(sock, "RETR %d", state->cur_msg);
427
428         return POP3_RETR_RECV;
429 }
430
431 gint pop3_retr_recv(SockInfo *sock, gpointer data)
432 {
433         Pop3State *state = (Pop3State *)data;
434         const gchar *file;
435         gint ok, drop_ok;
436
437         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
438                 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
439                         state->inc_state = INC_NOSPACE;
440                         return -1;
441                 }
442
443                 if ((drop_ok = inc_drop_message(file, state)) < 0) {
444                         state->inc_state = INC_ERROR;
445                         return -1;
446                 }
447
448                 state->cur_total_bytes += state->sizes[state->cur_msg];
449                 state->cur_total_num++;
450
451                 if (drop_ok == 0 && state->ac_prefs->rmmail)
452                         return POP3_DELETE_SEND;
453
454                 if (state->new_id_list) {
455                         state->id_list = g_slist_append
456                                 (state->id_list, state->new_id_list->data);
457                         state->new_id_list =
458                                 g_slist_remove(state->new_id_list,
459                                                state->new_id_list->data);
460                 }
461
462                 if (state->cur_msg < state->count) {
463                         state->cur_msg++;
464                         LOOKUP_NEXT_MSG();
465                         return POP3_RETR_SEND;
466                 } else
467                         return POP3_LOGOUT_SEND;
468         } else if (ok == PS_PROTOCOL)
469                 return POP3_LOGOUT_SEND;
470         else
471                 return -1;
472 }
473
474 gint pop3_delete_send(SockInfo *sock, gpointer data)
475 {
476         Pop3State *state = (Pop3State *)data;
477
478         /* inc_progress_update(state, POP3_DELETE_SEND); */
479
480         pop3_gen_send(sock, "DELE %d", state->cur_msg);
481
482         return POP3_DELETE_RECV;
483 }
484
485 gint pop3_delete_recv(SockInfo *sock, gpointer data)
486 {
487         Pop3State *state = (Pop3State *)data;
488         gint ok;
489
490         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
491
492                 if (pop3_delete_header(state) == TRUE) 
493                         return POP3_DELETE_SEND;
494
495                 if (state->cur_msg < state->count) {
496                         state->cur_msg++;
497                         LOOKUP_NEXT_MSG();
498                         return POP3_RETR_SEND;
499                 } else
500                         return POP3_LOGOUT_SEND;
501         } else if (ok == PS_PROTOCOL)
502                 return POP3_LOGOUT_SEND;
503         else
504                 return -1;
505 }
506
507 gint pop3_logout_send(SockInfo *sock, gpointer data)
508 {
509         Pop3State *state = (Pop3State *)data;
510
511         inc_progress_update(state, POP3_LOGOUT_SEND);
512
513         pop3_gen_send(sock, "QUIT");
514
515         return POP3_LOGOUT_RECV;
516 }
517
518 gint pop3_logout_recv(SockInfo *sock, gpointer data)
519 {
520         if (pop3_ok(sock, NULL) == PS_SUCCESS)
521                 return -1;
522         else
523                 return -1;
524 }
525
526 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
527 {
528         gint ok;
529         gchar buf[POPBUFSIZE + 1];
530         gchar *bufp;
531
532         if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
533                 bufp = buf;
534                 if (*bufp == '+' || *bufp == '-')
535                         bufp++;
536                 else
537                         return PS_PROTOCOL;
538
539                 while (isalpha(*bufp))
540                         bufp++;
541
542                 if (*bufp)
543                         *(bufp++) = '\0';
544
545                 if (!strcmp(buf, "+OK"))
546                         ok = PS_SUCCESS;
547                 else if (!strncmp(buf, "-ERR", 4)) {
548                         if (strstr(bufp, "lock") ||
549                                  strstr(bufp, "Lock") ||
550                                  strstr(bufp, "LOCK") ||
551                                  strstr(bufp, "wait"))
552                                 ok = PS_LOCKBUSY;
553                         else
554                                 ok = PS_PROTOCOL;
555
556                         if (*bufp)
557                                 fprintf(stderr, "POP3: %s\n", bufp);
558                 } else
559                         ok = PS_PROTOCOL;
560
561                 if (argbuf)
562                         strcpy(argbuf, bufp);
563         }
564
565         return ok;
566 }
567
568 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
569 {
570         gchar buf[POPBUFSIZE + 1];
571         va_list args;
572
573         va_start(args, format);
574         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
575         va_end(args);
576
577         if (!strncasecmp(buf, "PASS ", 5))
578                 log_print("POP3> PASS ********\n");
579         else
580                 log_print("POP3> %s\n", buf);
581
582         strcat(buf, "\r\n");
583         sock_write(sock, buf, strlen(buf));
584 }
585
586 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
587 {
588         if (sock_gets(sock, buf, size) < 0) {
589                 return PS_SOCKET;
590         } else {
591                 strretchomp(buf);
592                 log_print("POP3< %s\n", buf);
593
594                 return PS_SUCCESS;
595         }
596 }
597
598 gboolean pop3_delete_header (Pop3State *state)
599 {
600         
601         if ( (state->ac_prefs->session_type == DELE_HEADER) &&
602              (g_slist_length(state->ac_prefs->to_delete) > 0) ) {
603
604                 state->cur_msg = (gint) state->ac_prefs->to_delete->data;
605                 debug_print(_("next to delete %i\n"), state->cur_msg);
606                 state->ac_prefs->to_delete = g_slist_remove 
607                         (state->ac_prefs->to_delete, state->ac_prefs->to_delete->data);
608                 return TRUE;
609         }
610         return FALSE;
611 }