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