0a9ee1289131149571d3486fb32662279e0890d0
[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         GDate *curdate;
338         int  id = 0;
339         const gchar *sdate;
340         gboolean result;
341
342         if (!state->ac_prefs->rmmail || !strchr(uidl, ' '))
343                 return FALSE;
344
345         curdate = g_date_new(); 
346         g_return_val_if_fail(curdate, FALSE);   
347         
348         g_date_set_time(curdate, time(NULL));
349         
350         /* remove \r\n */
351         uidl   = g_strndup(uidl, strlen(uidl) - 2);
352         answer = g_strsplit(uidl, " ", 2);
353         id     = atoi(answer[0]);
354
355         if (NULL != (sdate = g_hash_table_lookup(state->uidl_table, answer[1]))) {
356                 int tdate    = atoi(sdate);
357                 int keep_for = atoi(state->ac_prefs->leave_time); /* FIXME: leave time should be an int */
358                 int today    = g_date_day_of_year(curdate);
359                 int nb_days  = 365;
360
361                 nb_days = g_date_is_leap_year(g_date_year(curdate)) ? 366 : 365;
362                 result = ( (tdate + keep_for)%nb_days <= today );
363         } else
364                 result = FALSE;
365
366         g_date_free(curdate);
367         g_free(uidl);
368         g_strfreev(answer);
369         
370         return result;
371 }
372
373 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
374 {
375         Pop3State *state = (Pop3State *)data;
376
377         inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
378
379         pop3_gen_send(sock, "LIST");
380
381         return POP3_GETSIZE_LIST_RECV;
382 }
383
384 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
385 {
386         Pop3State *state = (Pop3State *)data;
387         gchar buf[POPBUFSIZE];
388
389         if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
390
391         state->cur_total_bytes = 0;
392
393         while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
394                 guint num, size;
395
396                 if (buf[0] == '.') break;
397                 if (sscanf(buf, "%u %u", &num, &size) != 2)
398                         return -1;
399
400                 if (num > 0 && num <= state->count)
401                         state->msg[num].size = size;
402                 if (num > 0 && num < state->cur_msg)
403                         state->cur_total_bytes += size;
404         }
405
406         LOOKUP_NEXT_MSG();
407         if (state->ac_prefs->session_type == RETR_HEADER) 
408                 return POP3_TOP_SEND;
409         else
410                 return POP3_RETR_SEND;
411  }
412  
413 gint pop3_top_send(SockInfo *sock, gpointer data)
414 {
415         Pop3State *state = (Pop3State *)data;
416
417         inc_progress_update(state, POP3_TOP_SEND); 
418
419         pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
420
421         return POP3_TOP_RECV;
422 }
423
424 gint pop3_top_recv(SockInfo *sock, gpointer data)
425 {
426         Pop3State *state = (Pop3State *)data;
427         FILE  *fp;
428         gchar buf[POPBUFSIZE];
429         gchar *header;
430         gchar *filename, *path;
431         
432         inc_progress_update(state, POP3_TOP_RECV); 
433
434         if (pop3_ok(sock, NULL) != PS_SUCCESS) 
435                 return POP3_LOGOUT_SEND;
436
437         path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
438
439         if ( !is_dir_exist(path) )
440                 make_dir_hier(path);
441         
442         filename = g_strdup_printf("%s%i", path, state->cur_msg);
443                                    
444         if (recv_write_to_file(sock, filename) < 0) {
445                 state->inc_state = INC_NOSPACE;
446                 return -1;
447         }
448         /* we add a Complete-Size Header Item ...
449            note: overwrites first line  --> this is dirty */
450         if ( (fp = fopen(filename, "rb+")) != NULL ) {
451                 gchar *buf = g_strdup_printf("%s%i", SIZE_HEADER, 
452                                              state->msg[state->cur_msg].size);
453         
454                 if (change_file_mode_rw(fp, filename) == 0) 
455                         fprintf(fp, "%s\n", buf);
456                 fclose(fp);
457         }
458         
459         g_free(path);
460         g_free(filename);
461
462         if (state->cur_msg < state->count) {
463                 state->cur_msg++;
464                 return POP3_TOP_SEND;
465         } else
466                 return POP3_LOGOUT_SEND;
467 }
468
469 gint pop3_retr_send(SockInfo *sock, gpointer data)
470 {
471         Pop3State *state = (Pop3State *)data;
472
473         inc_progress_update(state, POP3_RETR_SEND);
474
475         pop3_gen_send(sock, "RETR %d", state->cur_msg);
476
477         return POP3_RETR_RECV;
478 }
479
480 gint pop3_retr_recv(SockInfo *sock, gpointer data)
481 {
482         Pop3State *state = (Pop3State *)data;
483         const gchar *file;
484         gint ok, drop_ok;
485         int keep_for=atoi(g_strdup(state->ac_prefs->leave_time));
486         
487         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
488                 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
489                         if (state->inc_state == INC_SUCCESS)
490                                 state->inc_state = INC_NOSPACE;
491                         return -1;
492                 }
493
494                 if ((drop_ok = inc_drop_message(file, state)) < 0) {
495                         state->inc_state = INC_ERROR;
496                         return -1;
497                 }
498         
499                 state->cur_total_bytes += state->msg[state->cur_msg].size;
500                 state->cur_total_num++;
501
502                 if (drop_ok == 0 && state->ac_prefs->rmmail && keep_for == 0) {
503                         return POP3_DELETE_SEND;
504                 }
505
506                 state->msg[state->cur_msg].received = TRUE;
507
508                 if (state->cur_msg < state->count) {
509                         state->cur_msg++;
510                         LOOKUP_NEXT_MSG();
511                         return POP3_RETR_SEND;
512                 } else
513                         return POP3_LOGOUT_SEND;
514         } else if (ok == PS_PROTOCOL)
515                 return POP3_LOGOUT_SEND;
516         else
517                 return -1;
518 }
519
520 gint pop3_delete_send(SockInfo *sock, gpointer data)
521 {
522         Pop3State *state = (Pop3State *)data;
523
524         /* inc_progress_update(state, POP3_DELETE_SEND); */
525
526         pop3_gen_send(sock, "DELE %d", state->cur_msg);
527
528         return POP3_DELETE_RECV;
529 }
530
531 gint pop3_delete_recv(SockInfo *sock, gpointer data)
532 {
533         Pop3State *state = (Pop3State *)data;
534         gint ok;
535
536         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
537                 if (state->ac_prefs->session_type == RETR_NORMAL)
538                         state->msg[state->cur_msg].deleted = TRUE;
539
540                 if (pop3_delete_header(state) == TRUE) 
541                         return POP3_DELETE_SEND;
542
543                 if (state->cur_msg < state->count) {
544                         state->cur_msg++;
545                         LOOKUP_NEXT_MSG();
546                         return POP3_RETR_SEND;
547                 } else
548                         return POP3_LOGOUT_SEND;
549         } else if (ok == PS_PROTOCOL)
550                 return POP3_LOGOUT_SEND;
551         else
552                 return -1;
553 }
554
555 gint pop3_logout_send(SockInfo *sock, gpointer data)
556 {
557         Pop3State *state = (Pop3State *)data;
558         while (state->uidl_todelete_list != NULL) {
559                 gchar **parts;
560                 gint ok;
561                 
562                 parts = g_strsplit((gchar *) state->uidl_todelete_list->data, " ", 2);
563                 state->uidl_todelete_list = g_slist_remove
564                         (state->uidl_todelete_list, state->uidl_todelete_list->data);
565                 pop3_gen_send(sock, "DELE %s", parts[0]);
566                 if ((ok = pop3_ok(sock, NULL)) != PS_SUCCESS)
567                         log_warning(_("error occurred on DELE\n"));
568         }
569         
570         inc_progress_update(state, POP3_LOGOUT_SEND);
571
572         pop3_gen_send(sock, "QUIT");
573
574         return POP3_LOGOUT_RECV;
575 }
576
577 gint pop3_logout_recv(SockInfo *sock, gpointer data)
578 {
579         if (pop3_ok(sock, NULL) == PS_SUCCESS)
580                 return -1;
581         else
582                 return -1;
583 }
584
585 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
586 {
587         gint ok;
588         gchar buf[POPBUFSIZE + 1];
589         gchar *bufp;
590
591         if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
592                 bufp = buf;
593                 if (*bufp == '+' || *bufp == '-')
594                         bufp++;
595                 else
596                         return PS_PROTOCOL;
597
598                 while (isalpha(*bufp))
599                         bufp++;
600
601                 if (*bufp)
602                         *(bufp++) = '\0';
603
604                 if (!strcmp(buf, "+OK"))
605                         ok = PS_SUCCESS;
606                 else if (!strncmp(buf, "-ERR", 4)) {
607                         if (strstr(bufp, "lock") ||
608                                  strstr(bufp, "Lock") ||
609                                  strstr(bufp, "LOCK") ||
610                                  strstr(bufp, "wait"))
611                                 ok = PS_LOCKBUSY;
612                         else
613                                 ok = PS_PROTOCOL;
614
615                         if (*bufp)
616                                 fprintf(stderr, "POP3: %s\n", bufp);
617                 } else
618                         ok = PS_PROTOCOL;
619
620                 if (argbuf)
621                         strcpy(argbuf, bufp);
622         }
623
624         return ok;
625 }
626
627 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
628 {
629         gchar buf[POPBUFSIZE + 1];
630         va_list args;
631
632         va_start(args, format);
633         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
634         va_end(args);
635
636         if (!strncasecmp(buf, "PASS ", 5))
637                 log_print("POP3> PASS ********\n");
638         else
639                 log_print("POP3> %s\n", buf);
640
641         strcat(buf, "\r\n");
642         sock_write(sock, buf, strlen(buf));
643 }
644
645 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
646 {
647         if (sock_gets(sock, buf, size) < 0) {
648                 return PS_SOCKET;
649         } else {
650                 strretchomp(buf);
651                 log_print("POP3< %s\n", buf);
652
653                 return PS_SUCCESS;
654         }
655 }
656
657 gboolean pop3_delete_header (Pop3State *state)
658 {
659         
660         if ( (state->ac_prefs->session_type == DELE_HEADER) &&
661              (g_slist_length(state->ac_prefs->to_delete) > 0) ) {
662
663                 state->cur_msg = (gint) state->ac_prefs->to_delete->data;
664                 debug_print(_("next to delete %i\n"), state->cur_msg);
665                 state->ac_prefs->to_delete = g_slist_remove 
666                         (state->ac_prefs->to_delete, state->ac_prefs->to_delete->data);
667                 return TRUE;
668         }
669         return FALSE;
670 }