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