fixed local account
[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->rmmail || 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
317         state->uidl_is_valid = TRUE;
318
319         if (new == TRUE)
320                 return POP3_GETSIZE_LIST_SEND;
321         else
322                 return POP3_LOGOUT_SEND;
323 }
324
325 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
326 {
327         Pop3State *state = (Pop3State *)data;
328
329         inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
330
331         pop3_gen_send(sock, "LIST");
332
333         return POP3_GETSIZE_LIST_RECV;
334 }
335
336 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
337 {
338         Pop3State *state = (Pop3State *)data;
339         gchar buf[POPBUFSIZE];
340
341         if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
342
343         state->cur_total_bytes = 0;
344
345         while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
346                 guint num, size;
347
348                 if (buf[0] == '.') break;
349                 if (sscanf(buf, "%u %u", &num, &size) != 2)
350                         return -1;
351
352                 if (num > 0 && num <= state->count)
353                         state->msg[num].size = size;
354                 if (num > 0 && num < state->cur_msg)
355                         state->cur_total_bytes += size;
356         }
357
358         LOOKUP_NEXT_MSG();
359         if (state->ac_prefs->session_type == RETR_HEADER) 
360                 return POP3_TOP_SEND;
361         else
362                 return POP3_RETR_SEND;
363  }
364  
365 gint pop3_top_send(SockInfo *sock, gpointer data)
366 {
367         Pop3State *state = (Pop3State *)data;
368
369         inc_progress_update(state, POP3_TOP_SEND); 
370
371         pop3_gen_send(sock, "TOP %i 0", state->cur_msg );
372
373         return POP3_TOP_RECV;
374 }
375
376 gint pop3_top_recv(SockInfo *sock, gpointer data)
377 {
378         Pop3State *state = (Pop3State *)data;
379         FILE  *fp;
380         gchar buf[POPBUFSIZE];
381         gchar *header;
382         gchar *filename, *path;
383         
384         inc_progress_update(state, POP3_TOP_RECV); 
385
386         if (pop3_ok(sock, NULL) != PS_SUCCESS) 
387                 return POP3_LOGOUT_SEND;
388
389         path = g_strconcat(get_header_cache_dir(), G_DIR_SEPARATOR_S, NULL);
390
391         if ( !is_dir_exist(path) )
392                 make_dir_hier(path);
393         
394         filename = g_strdup_printf("%s%i", path, state->cur_msg);
395                                    
396         if (recv_write_to_file(sock, filename) < 0) {
397                 state->inc_state = INC_NOSPACE;
398                 return -1;
399         }
400         /* we add a Complete-Size Header Item ...
401            note: overwrites first line  --> this is dirty */
402         if ( (fp = fopen(filename, "r+")) != NULL ) {
403                 gchar *buf = g_strdup_printf("%s%i", SIZE_HEADER, 
404                                              state->msg[state->cur_msg].size);
405         
406                 if (change_file_mode_rw(fp, filename) == 0) 
407                         fprintf(fp, "%s\n", buf);
408                 fclose(fp);
409         }
410         
411         g_free(path);
412         g_free(filename);
413
414         if (state->cur_msg < state->count) {
415                 state->cur_msg++;
416                 return POP3_TOP_SEND;
417         } else
418                 return POP3_LOGOUT_SEND;
419 }
420
421 gint pop3_retr_send(SockInfo *sock, gpointer data)
422 {
423         Pop3State *state = (Pop3State *)data;
424
425         inc_progress_update(state, POP3_RETR_SEND);
426
427         pop3_gen_send(sock, "RETR %d", state->cur_msg);
428
429         return POP3_RETR_RECV;
430 }
431
432 gint pop3_retr_recv(SockInfo *sock, gpointer data)
433 {
434         Pop3State *state = (Pop3State *)data;
435         const gchar *file;
436         gint ok, drop_ok;
437
438         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
439                 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
440                         if (state->inc_state == INC_SUCCESS)
441                                 state->inc_state = INC_NOSPACE;
442                         return -1;
443                 }
444
445                 if ((drop_ok = inc_drop_message(file, state)) < 0) {
446                         state->inc_state = INC_ERROR;
447                         return -1;
448                 }
449
450                 state->cur_total_bytes += state->msg[state->cur_msg].size;
451                 state->cur_total_num++;
452
453                 if (drop_ok == 0 && state->ac_prefs->rmmail)
454                         return POP3_DELETE_SEND;
455
456                 state->msg[state->cur_msg].received = TRUE;
457
458                 if (state->cur_msg < state->count) {
459                         state->cur_msg++;
460                         LOOKUP_NEXT_MSG();
461                         return POP3_RETR_SEND;
462                 } else
463                         return POP3_LOGOUT_SEND;
464         } else if (ok == PS_PROTOCOL)
465                 return POP3_LOGOUT_SEND;
466         else
467                 return -1;
468 }
469
470 gint pop3_delete_send(SockInfo *sock, gpointer data)
471 {
472         Pop3State *state = (Pop3State *)data;
473
474         /* inc_progress_update(state, POP3_DELETE_SEND); */
475
476         pop3_gen_send(sock, "DELE %d", state->cur_msg);
477
478         return POP3_DELETE_RECV;
479 }
480
481 gint pop3_delete_recv(SockInfo *sock, gpointer data)
482 {
483         Pop3State *state = (Pop3State *)data;
484         gint ok;
485
486         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
487                 if (state->ac_prefs->session_type == RETR_NORMAL)
488                         state->msg[state->cur_msg].deleted = TRUE;
489
490                 if (pop3_delete_header(state) == TRUE) 
491                         return POP3_DELETE_SEND;
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_logout_send(SockInfo *sock, gpointer data)
506 {
507         Pop3State *state = (Pop3State *)data;
508
509         inc_progress_update(state, POP3_LOGOUT_SEND);
510
511         pop3_gen_send(sock, "QUIT");
512
513         return POP3_LOGOUT_RECV;
514 }
515
516 gint pop3_logout_recv(SockInfo *sock, gpointer data)
517 {
518         if (pop3_ok(sock, NULL) == PS_SUCCESS)
519                 return -1;
520         else
521                 return -1;
522 }
523
524 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
525 {
526         gint ok;
527         gchar buf[POPBUFSIZE + 1];
528         gchar *bufp;
529
530         if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
531                 bufp = buf;
532                 if (*bufp == '+' || *bufp == '-')
533                         bufp++;
534                 else
535                         return PS_PROTOCOL;
536
537                 while (isalpha(*bufp))
538                         bufp++;
539
540                 if (*bufp)
541                         *(bufp++) = '\0';
542
543                 if (!strcmp(buf, "+OK"))
544                         ok = PS_SUCCESS;
545                 else if (!strncmp(buf, "-ERR", 4)) {
546                         if (strstr(bufp, "lock") ||
547                                  strstr(bufp, "Lock") ||
548                                  strstr(bufp, "LOCK") ||
549                                  strstr(bufp, "wait"))
550                                 ok = PS_LOCKBUSY;
551                         else
552                                 ok = PS_PROTOCOL;
553
554                         if (*bufp)
555                                 fprintf(stderr, "POP3: %s\n", bufp);
556                 } else
557                         ok = PS_PROTOCOL;
558
559                 if (argbuf)
560                         strcpy(argbuf, bufp);
561         }
562
563         return ok;
564 }
565
566 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
567 {
568         gchar buf[POPBUFSIZE + 1];
569         va_list args;
570
571         va_start(args, format);
572         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
573         va_end(args);
574
575         if (!strncasecmp(buf, "PASS ", 5))
576                 log_print("POP3> PASS ********\n");
577         else
578                 log_print("POP3> %s\n", buf);
579
580         strcat(buf, "\r\n");
581         sock_write(sock, buf, strlen(buf));
582 }
583
584 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
585 {
586         if (sock_gets(sock, buf, size) < 0) {
587                 return PS_SOCKET;
588         } else {
589                 strretchomp(buf);
590                 log_print("POP3< %s\n", buf);
591
592                 return PS_SUCCESS;
593         }
594 }
595
596 gboolean pop3_delete_header (Pop3State *state)
597 {
598         
599         if ( (state->ac_prefs->session_type == DELE_HEADER) &&
600              (g_slist_length(state->ac_prefs->to_delete) > 0) ) {
601
602                 state->cur_msg = (gint) state->ac_prefs->to_delete->data;
603                 debug_print(_("next to delete %i\n"), state->cur_msg);
604                 state->ac_prefs->to_delete = g_slist_remove 
605                         (state->ac_prefs->to_delete, state->ac_prefs->to_delete->data);
606                 return TRUE;
607         }
608         return FALSE;
609 }