6ea299c267c3df706686b2916514a45efc903bfb
[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->msg[state->cur_msg].size; \
44                 gboolean size_limit_over = \
45                     (state->ac_prefs->enable_size_limit && \
46                      state->ac_prefs->size_limit > 0 && \
47                      size > state->ac_prefs->size_limit * 1024); \
48  \
49                 if (size_limit_over) \
50                         log_print(_("POP3: Skipping message %d (%d bytes)\n"), \
51                                   state->cur_msg, size); \
52  \
53                 if (size == 0 || state->msg[state->cur_msg].received || \
54                     size_limit_over) { \
55                         if (size > 0) \
56                                 state->cur_total_bytes += size; \
57                         if (state->cur_msg == state->count) \
58                                 return POP3_LOGOUT_SEND; \
59                         else \
60                                 state->cur_msg++; \
61                 } else \
62                         break; \
63         }
64
65 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
66 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
67 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
68 static gboolean pop3_delete_header (Pop3State *state);
69 static gboolean should_delete (const char *uidl, gpointer data); 
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                                 state->uidl_is_valid = TRUE;
222                                 return POP3_LOGOUT_SEND;
223                         } else {
224                                 state->msg = g_new0
225                                         (Pop3MsgInfo, state->count + 1);
226                                 state->cur_msg = 1;
227                                 return POP3_GETRANGE_UIDL_SEND;
228                         }
229                 }
230         } else if (ok == PS_PROTOCOL)
231                 return POP3_LOGOUT_SEND;
232         else
233                 return -1;
234 }
235
236 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
237 {
238         Pop3State *state = (Pop3State *)data;
239
240         inc_progress_update(state, POP3_GETRANGE_LAST_SEND);
241
242         pop3_gen_send(sock, "LAST");
243
244         return POP3_GETRANGE_LAST_RECV;
245 }
246
247 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
248 {
249         Pop3State *state = (Pop3State *)data;
250         gchar buf[POPBUFSIZE + 1];
251
252         if (pop3_ok(sock, buf) == PS_SUCCESS) {
253                 gint last;
254
255                 if (sscanf(buf, "%d", &last) == 0) {
256                         log_warning(_("POP3 protocol error\n"));
257                         return -1;
258                 } else {
259                         if (state->count == last)
260                                 return POP3_LOGOUT_SEND;
261                         else {
262                                 state->cur_msg = last + 1;
263                                 return POP3_GETSIZE_LIST_SEND;
264                         }
265                 }
266         } else
267                 return POP3_GETSIZE_LIST_SEND;
268 }
269
270 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
271 {
272         Pop3State *state = (Pop3State *)data;
273
274         inc_progress_update(state, POP3_GETRANGE_UIDL_SEND);
275
276         pop3_gen_send(sock, "UIDL");
277
278         return POP3_GETRANGE_UIDL_RECV;
279 }
280
281 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
282 {
283         Pop3State *state = (Pop3State *)data;
284         gboolean new = FALSE;
285         gboolean get_all = FALSE;
286         gchar buf[POPBUFSIZE];
287         gchar id[IDLEN + 1];
288
289         if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
290
291         if (!state->uidl_table) new = TRUE;
292         if (state->ac_prefs->getall)
293                 get_all = 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                 if (num <= 0 || num > state->count) continue;
302
303                 state->msg[num].uidl = g_strdup(id);
304
305                 if (state->uidl_table) {
306                         if (!get_all &&
307                             g_hash_table_lookup(state->uidl_table, id) != NULL)
308                                 state->msg[num].received = TRUE;
309                         else {
310                                 if (new == FALSE) {
311                                         state->cur_msg = num;
312                                         new = TRUE;
313                                 }
314                         }
315                 }
316
317                 if (should_delete(buf, (Pop3State *) state))
318                         state->uidl_todelete_list = g_slist_append
319                                         (state->uidl_todelete_list, g_strdup(buf));             
320                 
321         }
322
323         state->uidl_is_valid = TRUE;
324
325         if (new == TRUE)
326                 return POP3_GETSIZE_LIST_SEND;
327         else
328                 return POP3_LOGOUT_SEND;
329 }
330
331 static gboolean should_delete(const char *uidl, gpointer data) 
332 {
333         /* answer[0] will contain id
334          * answer[0] will contain uidl */
335         Pop3State *state = (Pop3State *) data;
336         gchar **answer;
337         int  id;
338         gboolean result;
339         int tdate, keep_for, today, nb_days;
340         const gchar *sdate;
341         GDate curdate;
342         gchar *tuidl;
343
344         if (!state->ac_prefs->rmmail || !strchr(uidl, ' '))
345                 return FALSE;
346
347         /* remove \r\n */
348         tuidl  = g_strndup(uidl, strlen(uidl) - 2);
349         answer = g_strsplit(uidl, " ", 2);
350         id     = atoi(answer[0]);
351
352         if (NULL != (sdate = g_hash_table_lookup(state->uidl_table, answer[1]))) {
353                 tdate    = atoi(sdate);
354                 keep_for = atoi(state->ac_prefs->leave_time); /* FIXME: leave time should be an int */
355
356                 g_date_clear(&curdate, time(NULL));
357                 g_date_set_time(&curdate, time(NULL));
358                 today = g_date_day_of_year(&curdate);
359                 
360                 nb_days = g_date_is_leap_year(g_date_year(&curdate)) ? 366 : 365;
361                 result  = ((tdate + keep_for) % nb_days <= today);
362         } else
363                 result = FALSE;
364
365         g_free(tuidl);
366         g_strfreev(answer);
367         
368         return result;
369 }
370
371 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
372 {
373         Pop3State *state = (Pop3State *)data;
374
375         inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
376
377         pop3_gen_send(sock, "LIST");
378
379         return POP3_GETSIZE_LIST_RECV;
380 }
381
382 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
383 {
384         Pop3State *state = (Pop3State *)data;
385         gchar buf[POPBUFSIZE];
386
387         if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
388
389         state->cur_total_bytes = 0;
390
391         while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
392                 guint num, size;
393
394                 if (buf[0] == '.') break;
395                 if (sscanf(buf, "%u %u", &num, &size) != 2)
396                         return -1;
397
398                 if (num > 0 && num <= state->count)
399                         state->msg[num].size = size;
400                 if (num > 0 && num < state->cur_msg)
401                         state->cur_total_bytes += size;
402         }
403
404         LOOKUP_NEXT_MSG();
405         if (state->ac_prefs->session_type == RETR_HEADER) 
406                 return POP3_TOP_SEND;
407         else
408                 return POP3_RETR_SEND;
409  }
410  
411 gint pop3_top_send(SockInfo *sock, gpointer data)
412 {
413         Pop3State *state = (Pop3State *)data;
414
415         inc_progress_update(state, POP3_TOP_SEND); 
416
417         pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
418
419         return POP3_TOP_RECV;
420 }
421
422 gint pop3_top_recv(SockInfo *sock, gpointer data)
423 {
424         Pop3State *state = (Pop3State *)data;
425         FILE  *fp;
426         gchar buf[POPBUFSIZE];
427         gchar *header;
428         gchar *filename, *path;
429         
430         inc_progress_update(state, POP3_TOP_RECV); 
431
432         if (pop3_ok(sock, NULL) != PS_SUCCESS) 
433                 return POP3_LOGOUT_SEND;
434
435         path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
436
437         if ( !is_dir_exist(path) )
438                 make_dir_hier(path);
439         
440         filename = g_strdup_printf("%s%i", path, state->cur_msg);
441                                    
442         if (recv_write_to_file(sock, filename) < 0) {
443                 state->inc_state = INC_NOSPACE;
444                 return -1;
445         }
446         /* we add a Complete-Size Header Item ...
447            note: overwrites first line  --> this is dirty */
448         if ( (fp = fopen(filename, "rb+")) != NULL ) {
449                 gchar *buf = g_strdup_printf("%s%i", SIZE_HEADER, 
450                                              state->msg[state->cur_msg].size);
451         
452                 if (change_file_mode_rw(fp, filename) == 0) 
453                         fprintf(fp, "%s\n", buf);
454                 g_free(buf);    
455                 fclose(fp);
456         }
457         
458         g_free(path);
459         g_free(filename);
460
461         if (state->cur_msg < state->count) {
462                 state->cur_msg++;
463                 return POP3_TOP_SEND;
464         } else
465                 return POP3_LOGOUT_SEND;
466 }
467
468 gint pop3_retr_send(SockInfo *sock, gpointer data)
469 {
470         Pop3State *state = (Pop3State *)data;
471
472         inc_progress_update(state, POP3_RETR_SEND);
473
474         pop3_gen_send(sock, "RETR %d", state->cur_msg);
475
476         return POP3_RETR_RECV;
477 }
478
479 gint pop3_retr_recv(SockInfo *sock, gpointer data)
480 {
481         Pop3State *state = (Pop3State *)data;
482         const gchar *file;
483         gint ok, drop_ok;
484         int keep_for;
485         
486         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
487                 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
488                         if (state->inc_state == INC_SUCCESS)
489                                 state->inc_state = INC_NOSPACE;
490                         return -1;
491                 }
492
493                 if ((drop_ok = inc_drop_message(file, state)) < 0) {
494                         state->inc_state = INC_ERROR;
495                         return -1;
496                 }
497         
498                 state->cur_total_bytes += state->msg[state->cur_msg].size;
499                 state->cur_total_num++;
500
501                 keep_for = (state->ac_prefs && state->ac_prefs->leave_time) ? 
502                            atoi(state->ac_prefs->leave_time) : 0;
503
504                 if (drop_ok == 0 && state->ac_prefs->rmmail && keep_for == 0)
505                         return POP3_DELETE_SEND;
506
507                 state->msg[state->cur_msg].received = TRUE;
508
509                 if (state->cur_msg < state->count) {
510                         state->cur_msg++;
511                         LOOKUP_NEXT_MSG();
512                         return POP3_RETR_SEND;
513                 } else
514                         return POP3_LOGOUT_SEND;
515         } else if (ok == PS_PROTOCOL)
516                 return POP3_LOGOUT_SEND;
517         else
518                 return -1;
519 }
520
521 gint pop3_delete_send(SockInfo *sock, gpointer data)
522 {
523         Pop3State *state = (Pop3State *)data;
524
525         /* inc_progress_update(state, POP3_DELETE_SEND); */
526
527         pop3_gen_send(sock, "DELE %d", state->cur_msg);
528
529         return POP3_DELETE_RECV;
530 }
531
532 gint pop3_delete_recv(SockInfo *sock, gpointer data)
533 {
534         Pop3State *state = (Pop3State *)data;
535         gint ok;
536
537         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
538                 if (state->ac_prefs->session_type == RETR_NORMAL)
539                         state->msg[state->cur_msg].deleted = TRUE;
540
541                 if (pop3_delete_header(state) == TRUE) 
542                         return POP3_DELETE_SEND;
543
544                 if (state->cur_msg < state->count) {
545                         state->cur_msg++;
546                         LOOKUP_NEXT_MSG();
547                         return POP3_RETR_SEND;
548                 } else
549                         return POP3_LOGOUT_SEND;
550         } else if (ok == PS_PROTOCOL)
551                 return POP3_LOGOUT_SEND;
552         else
553                 return -1;
554 }
555
556 gint pop3_logout_send(SockInfo *sock, gpointer data)
557 {
558         Pop3State *state = (Pop3State *)data;
559         gchar **parts;
560         
561         while (state->uidl_todelete_list != NULL) {
562                 /*
563                  * FIXME: doesn't feel right - no checks for parts
564                  */
565                 parts = g_strsplit((gchar *) state->uidl_todelete_list->data, " ", 2);
566                 state->uidl_todelete_list = g_slist_remove
567                         (state->uidl_todelete_list, state->uidl_todelete_list->data);
568                 pop3_gen_send(sock, "DELE %s", parts[0]);
569                 if (pop3_ok(sock, NULL) != PS_SUCCESS)
570                         log_warning(_("error occurred on DELE\n"));
571                 g_strfreev(parts);      
572         }
573         
574         inc_progress_update(state, POP3_LOGOUT_SEND);
575
576         pop3_gen_send(sock, "QUIT");
577
578         return POP3_LOGOUT_RECV;
579 }
580
581 gint pop3_logout_recv(SockInfo *sock, gpointer data)
582 {
583         if (pop3_ok(sock, NULL) == PS_SUCCESS)
584                 return -1;
585         else
586                 return -1;
587 }
588
589 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
590 {
591         gint ok;
592         gchar buf[POPBUFSIZE + 1];
593         gchar *bufp;
594
595         if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
596                 bufp = buf;
597                 if (*bufp == '+' || *bufp == '-')
598                         bufp++;
599                 else
600                         return PS_PROTOCOL;
601
602                 while (isalpha(*bufp))
603                         bufp++;
604
605                 if (*bufp)
606                         *(bufp++) = '\0';
607
608                 if (!strcmp(buf, "+OK"))
609                         ok = PS_SUCCESS;
610                 else if (!strncmp(buf, "-ERR", 4)) {
611                         if (strstr(bufp, "lock") ||
612                                  strstr(bufp, "Lock") ||
613                                  strstr(bufp, "LOCK") ||
614                                  strstr(bufp, "wait"))
615                                 ok = PS_LOCKBUSY;
616                         else
617                                 ok = PS_PROTOCOL;
618
619                         if (*bufp)
620                                 fprintf(stderr, "POP3: %s\n", bufp);
621                 } else
622                         ok = PS_PROTOCOL;
623
624                 if (argbuf)
625                         strcpy(argbuf, bufp);
626         }
627
628         return ok;
629 }
630
631 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
632 {
633         gchar buf[POPBUFSIZE + 1];
634         va_list args;
635
636         va_start(args, format);
637         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
638         va_end(args);
639
640         if (!strncasecmp(buf, "PASS ", 5))
641                 log_print("POP3> PASS ********\n");
642         else
643                 log_print("POP3> %s\n", buf);
644
645         strcat(buf, "\r\n");
646         sock_write(sock, buf, strlen(buf));
647 }
648
649 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
650 {
651         if (sock_gets(sock, buf, size) < 0) {
652                 return PS_SOCKET;
653         } else {
654                 strretchomp(buf);
655                 log_print("POP3< %s\n", buf);
656
657                 return PS_SUCCESS;
658         }
659 }
660
661 gboolean pop3_delete_header (Pop3State *state)
662 {
663         
664         if ( (state->ac_prefs->session_type == DELE_HEADER) &&
665              (g_slist_length(state->ac_prefs->to_delete) > 0) ) {
666
667                 state->cur_msg = (gint) state->ac_prefs->to_delete->data;
668                 debug_print(_("next to delete %i\n"), state->cur_msg);
669                 state->ac_prefs->to_delete = g_slist_remove 
670                         (state->ac_prefs->to_delete, state->ac_prefs->to_delete->data);
671                 return TRUE;
672         }
673         return FALSE;
674 }