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