be paranoid on the checks
[claws.git] / src / pop.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 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 #include <time.h>
31 #include <errno.h>
32
33 #include "intl.h"
34 #include "pop.h"
35 #include "md5.h"
36 #include "prefs_account.h"
37 #include "utils.h"
38 #include "recv.h"
39 #include "folder.h"
40 #include "procmsg.h"
41 #include "procheader.h"
42 #include "msgcache.h"
43
44 #include "log.h"
45 #include "hooks.h"
46
47 static gint pop3_greeting_recv          (Pop3Session *session,
48                                          const gchar *msg);
49 static gint pop3_getauth_user_send      (Pop3Session *session);
50 static gint pop3_getauth_pass_send      (Pop3Session *session);
51 static gint pop3_getauth_apop_send      (Pop3Session *session);
52 #if USE_OPENSSL
53 static gint pop3_stls_send              (Pop3Session *session);
54 static gint pop3_stls_recv              (Pop3Session *session);
55 #endif
56 static gint pop3_getrange_stat_send     (Pop3Session *session);
57 static gint pop3_getrange_stat_recv     (Pop3Session *session,
58                                          const gchar *msg);
59 static gint pop3_getrange_last_send     (Pop3Session *session);
60 static gint pop3_getrange_last_recv     (Pop3Session *session,
61                                          const gchar *msg);
62 static gint pop3_getrange_uidl_send     (Pop3Session *session);
63 static gint pop3_getrange_uidl_recv     (Pop3Session *session,
64                                          const gchar *data,
65                                          guint        len);
66 static gint pop3_getsize_list_send      (Pop3Session *session);
67 static gint pop3_getsize_list_recv      (Pop3Session *session,
68                                          const gchar *data,
69                                          guint        len);
70 static gint pop3_retr_send              (Pop3Session *session);
71 static gint pop3_retr_recv              (Pop3Session *session,
72                                          const gchar *data,
73                                          guint        len);
74 static gint pop3_delete_send            (Pop3Session *session);
75 static gint pop3_delete_recv            (Pop3Session *session);
76 static gint pop3_logout_send            (Pop3Session *session);
77
78 static void pop3_gen_send               (Pop3Session    *session,
79                                          const gchar    *format, ...);
80
81 static void pop3_session_destroy        (Session        *session);
82
83 static gint pop3_write_msg_to_file      (const gchar    *file,
84                                          const gchar    *data,
85                                          guint           len,
86                                          const gchar    *prefix);
87
88 static Pop3State pop3_lookup_next       (Pop3Session    *session);
89 static Pop3ErrorValue pop3_ok           (Pop3Session    *session,
90                                          const gchar    *msg);
91
92 static gint pop3_session_recv_msg               (Session        *session,
93                                                  const gchar    *msg);
94 static gint pop3_session_recv_data_finished     (Session        *session,
95                                                  guchar         *data,
96                                                  guint           len);
97
98 static gchar *pop3_get_filename_for_partial_mail(Pop3Session    *session, 
99                                                  gchar          *muidl);
100 static void pop3_delete_old_partial     (const gchar    *file);
101
102 static gint pop3_greeting_recv(Pop3Session *session, const gchar *msg)
103 {
104         session->state = POP3_GREETING;
105
106         session->greeting = g_strdup(msg);
107         return PS_SUCCESS;
108 }
109
110 #if USE_OPENSSL
111 static gint pop3_stls_send(Pop3Session *session)
112 {
113         session->state = POP3_STLS;
114         pop3_gen_send(session, "STLS");
115         return PS_SUCCESS;
116 }
117
118 static gint pop3_stls_recv(Pop3Session *session)
119 {
120         if (session_start_tls(SESSION(session)) < 0) {
121                 session->error_val = PS_SOCKET;
122                 return -1;
123         }
124         return PS_SUCCESS;
125 }
126 #endif /* USE_OPENSSL */
127
128 static gint pop3_getauth_user_send(Pop3Session *session)
129 {
130         g_return_val_if_fail(session->user != NULL, -1);
131
132         session->state = POP3_GETAUTH_USER;
133         pop3_gen_send(session, "USER %s", session->user);
134         return PS_SUCCESS;
135 }
136
137 static gint pop3_getauth_pass_send(Pop3Session *session)
138 {
139         g_return_val_if_fail(session->pass != NULL, -1);
140
141         session->state = POP3_GETAUTH_PASS;
142         pop3_gen_send(session, "PASS %s", session->pass);
143         return PS_SUCCESS;
144 }
145
146 static gint pop3_getauth_apop_send(Pop3Session *session)
147 {
148         gchar *start, *end;
149         gchar *apop_str;
150         gchar md5sum[33];
151
152         g_return_val_if_fail(session->user != NULL, -1);
153         g_return_val_if_fail(session->pass != NULL, -1);
154
155         session->state = POP3_GETAUTH_APOP;
156
157         if ((start = strchr(session->greeting, '<')) == NULL) {
158                 log_warning(_("Required APOP timestamp not found "
159                             "in greeting\n"));
160                 session->error_val = PS_PROTOCOL;
161                 return -1;
162         }
163
164         if ((end = strchr(start, '>')) == NULL || end == start + 1) {
165                 log_warning(_("Timestamp syntax error in greeting\n"));
166                 session->error_val = PS_PROTOCOL;
167                 return -1;
168         }
169
170         *(end + 1) = '\0';
171
172         apop_str = g_strconcat(start, session->pass, NULL);
173         md5_hex_digest(md5sum, apop_str);
174         g_free(apop_str);
175
176         pop3_gen_send(session, "APOP %s %s", session->user, md5sum);
177
178         return PS_SUCCESS;
179 }
180
181 static gint pop3_getrange_stat_send(Pop3Session *session)
182 {
183         session->state = POP3_GETRANGE_STAT;
184         pop3_gen_send(session, "STAT");
185         return PS_SUCCESS;
186 }
187
188 static gint pop3_getrange_stat_recv(Pop3Session *session, const gchar *msg)
189 {
190         if (sscanf(msg, "%d %d", &session->count, &session->total_bytes) != 2) {
191                 log_warning(_("POP3 protocol error\n"));
192                 session->error_val = PS_PROTOCOL;
193                 return -1;
194         } else {
195                 if (session->count == 0) {
196                         session->uidl_is_valid = TRUE;
197                 } else {
198                         session->msg = g_new0(Pop3MsgInfo, session->count + 1);
199                         session->cur_msg = 1;
200                 }
201         }
202
203         return PS_SUCCESS;
204 }
205
206 static gint pop3_getrange_last_send(Pop3Session *session)
207 {
208         session->state = POP3_GETRANGE_LAST;
209         pop3_gen_send(session, "LAST");
210         return PS_SUCCESS;
211 }
212
213 static gint pop3_getrange_last_recv(Pop3Session *session, const gchar *msg)
214 {
215         gint last;
216
217         if (sscanf(msg, "%d", &last) == 0) {
218                 log_warning(_("POP3 protocol error\n"));
219                 session->error_val = PS_PROTOCOL;
220                 return -1;
221         } else {
222                 if (session->count > last) {
223                         session->new_msg_exist = TRUE;
224                         session->cur_msg = last + 1;
225                 } else
226                         session->cur_msg = 0;
227         }
228
229         return PS_SUCCESS;
230 }
231
232 static gint pop3_getrange_uidl_send(Pop3Session *session)
233 {
234         session->state = POP3_GETRANGE_UIDL;
235         pop3_gen_send(session, "UIDL");
236         return PS_SUCCESS;
237 }
238
239 static gint pop3_getrange_uidl_recv(Pop3Session *session, const gchar *data,
240                                     guint len)
241 {
242         gchar id[IDLEN + 1];
243         gchar buf[POPBUFSIZE];
244         gint buf_len;
245         gint num;
246         time_t recv_time;
247         gint partial_recv;
248         const gchar *p = data;
249         const gchar *lastp = data + len;
250         const gchar *newline;
251
252         while (p < lastp) {
253                 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
254                         return -1;
255                 buf_len = MIN(newline - p, sizeof(buf) - 1);
256                 memcpy(buf, p, buf_len);
257                 buf[buf_len] = '\0';
258
259                 p = newline + 1;
260                 if (p < lastp && *p == '\n') p++;
261
262                 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
263                         return -1;
264                 if (num <= 0 || num > session->count)
265                         return -1;
266
267                 session->msg[num].uidl = g_strdup(id);
268
269                 recv_time = (time_t)g_hash_table_lookup(
270                                         session->uidl_table, id);
271                 session->msg[num].recv_time = recv_time;
272
273                 partial_recv = (gint)g_hash_table_lookup(
274                                         session->partial_recv_table, id);
275
276                 if (!session->ac_prefs->getall && recv_time != RECV_TIME_NONE) {
277                         session->msg[num].received = 
278                                 (partial_recv != POP3_MUST_COMPLETE_RECV);
279                         session->msg[num].partial_recv = partial_recv;
280
281                 }
282                 if (!session->new_msg_exist &&
283                     (session->ac_prefs->getall || recv_time == RECV_TIME_NONE ||
284                      session->ac_prefs->rmmail)) {
285                         session->cur_msg = num;
286                         session->new_msg_exist = TRUE;
287                 }
288         }
289
290         session->uidl_is_valid = TRUE;
291         return PS_SUCCESS;
292 }
293
294 static gint pop3_getsize_list_send(Pop3Session *session)
295 {
296         session->state = POP3_GETSIZE_LIST;
297         pop3_gen_send(session, "LIST");
298         return PS_SUCCESS;
299 }
300
301 static gint pop3_getsize_list_recv(Pop3Session *session, const gchar *data,
302                                    guint len)
303 {
304         gchar buf[POPBUFSIZE];
305         gint buf_len;
306         guint num, size;
307         const gchar *p = data;
308         const gchar *lastp = data + len;
309         const gchar *newline;
310
311         while (p < lastp) {
312                 if ((newline = memchr(p, '\r', lastp - p)) == NULL)
313                         return -1;
314                 buf_len = MIN(newline - p, sizeof(buf) - 1);
315                 memcpy(buf, p, buf_len);
316                 buf[buf_len] = '\0';
317
318                 p = newline + 1;
319                 if (p < lastp && *p == '\n') p++;
320
321                 if (sscanf(buf, "%u %u", &num, &size) != 2) {
322                         session->error_val = PS_PROTOCOL;
323                         return -1;
324                 }
325
326                 if (num > 0 && num <= session->count)
327                         session->msg[num].size = size;
328                 if (num > 0 && num < session->cur_msg)
329                         session->cur_total_bytes += size;
330         }
331
332         return PS_SUCCESS;
333 }
334
335 static gint pop3_retr_send(Pop3Session *session)
336 {
337         session->state = POP3_RETR;
338         pop3_gen_send(session, "RETR %d", session->cur_msg);
339         return PS_SUCCESS;
340 }
341
342 static gint pop3_retr_recv(Pop3Session *session, const gchar *data, guint len)
343 {
344         gchar *file;
345         gint drop_ok;
346         MailReceiveData mail_receive_data;
347
348         mail_receive_data.session = session;
349         mail_receive_data.data = g_strndup(data, len);
350         hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
351
352         file = get_tmp_file();
353         if (pop3_write_msg_to_file(file, mail_receive_data.data,
354                 strlen(mail_receive_data.data), NULL) < 0) {
355                 g_free(file);
356                 g_free(mail_receive_data.data);
357                 session->error_val = PS_IOERR;
358                 return -1;
359         }
360         g_free(mail_receive_data.data);
361
362         if (session->msg[session->cur_msg].partial_recv 
363             == POP3_MUST_COMPLETE_RECV) {
364                 gchar *old_file = pop3_get_filename_for_partial_mail(
365                                 session, session->msg[session->cur_msg].uidl);
366                 
367                 if (old_file) {
368                         pop3_delete_old_partial (old_file);
369                         g_free(old_file);
370                 }
371         } 
372
373         /* drop_ok: 0: success 1: don't receive -1: error */
374         drop_ok = session->drop_message(session, file);
375
376         g_free(file);
377         if (drop_ok < 0) {
378                 session->error_val = PS_IOERR;
379                 return -1;
380         }
381         
382         session->cur_total_bytes += session->msg[session->cur_msg].size;
383         session->cur_total_recv_bytes += session->msg[session->cur_msg].size;
384         session->cur_total_num++;
385
386         session->msg[session->cur_msg].received = TRUE;
387         session->msg[session->cur_msg].partial_recv = POP3_TOTALLY_RECEIVED;
388
389         session->msg[session->cur_msg].recv_time =
390                 drop_ok == 1 ? RECV_TIME_KEEP : session->current_time;
391
392         return PS_SUCCESS;
393 }
394
395 static gint pop3_top_send(Pop3Session *session, gint max_size)
396 {
397         gint num_lines = (max_size*1024)/82; /* consider lines to be 80 chars */
398         session->state = POP3_TOP;
399         pop3_gen_send(session, "TOP %d %d", session->cur_msg, num_lines);
400         return PS_SUCCESS;
401 }
402
403 static gint pop3_top_recv(Pop3Session *session, const gchar *data, guint len)
404 {
405         gchar *file;
406         gint drop_ok;
407         MailReceiveData mail_receive_data;
408         gchar *partial_notice = NULL;
409         
410         mail_receive_data.session = session;
411         mail_receive_data.data = g_strndup(data, len);
412         hooks_invoke(MAIL_RECEIVE_HOOKLIST, &mail_receive_data);
413
414         partial_notice = g_strdup_printf("SC-Marked-For-Download: 0\n"
415                                          "SC-Partially-Retrieved: %s\n"
416                                          "SC-Account-Server: %s\n"
417                                          "SC-Account-Login: %s\n"
418                                          "SC-Message-Size: %d",
419                                          session->msg[session->cur_msg].uidl,
420                                          session->ac_prefs->recv_server,
421                                          session->ac_prefs->userid,
422                                          session->msg[session->cur_msg].size);
423         file = get_tmp_file();
424         if (pop3_write_msg_to_file(file, mail_receive_data.data,
425                 strlen(mail_receive_data.data), partial_notice) < 0) {
426                 g_free(file);
427                 g_free(mail_receive_data.data);
428                 session->error_val = PS_IOERR;
429                 g_free(partial_notice);
430                 return -1;
431         }
432         g_free(mail_receive_data.data);
433         g_free(partial_notice);
434
435         /* drop_ok: 0: success 1: don't receive -1: error */
436         drop_ok = session->drop_message(session, file);
437         g_free(file);
438         if (drop_ok < 0) {
439                 session->error_val = PS_IOERR;
440                 return -1;
441         }
442
443         session->cur_total_bytes += session->msg[session->cur_msg].size;
444         session->cur_total_recv_bytes += session->msg[session->cur_msg].size;
445         session->cur_total_num++;
446
447         session->msg[session->cur_msg].received = TRUE;
448         session->msg[session->cur_msg].partial_recv = POP3_PARTIALLY_RECEIVED;
449         session->msg[session->cur_msg].recv_time =
450                 drop_ok == 1 ? RECV_TIME_KEEP : session->current_time;
451
452         return PS_SUCCESS;
453 }
454
455 static gint pop3_delete_send(Pop3Session *session)
456 {
457         session->state = POP3_DELETE;
458         pop3_gen_send(session, "DELE %d", session->cur_msg);
459         return PS_SUCCESS;
460 }
461
462 static gint pop3_delete_recv(Pop3Session *session)
463 {
464         session->msg[session->cur_msg].deleted = TRUE;
465         return PS_SUCCESS;
466 }
467
468 static gint pop3_logout_send(Pop3Session *session)
469 {
470         session->state = POP3_LOGOUT;
471         pop3_gen_send(session, "QUIT");
472         return PS_SUCCESS;
473 }
474
475 static void pop3_gen_send(Pop3Session *session, const gchar *format, ...)
476 {
477         gchar buf[POPBUFSIZE + 1];
478         va_list args;
479
480         va_start(args, format);
481         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
482         va_end(args);
483
484         if (!strncasecmp(buf, "PASS ", 5))
485                 log_print("POP3> PASS ********\n");
486         else
487                 log_print("POP3> %s\n", buf);
488
489         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
490 }
491
492 Session *pop3_session_new(PrefsAccount *account)
493 {
494         Pop3Session *session;
495
496         g_return_val_if_fail(account != NULL, NULL);
497
498         session = g_new0(Pop3Session, 1);
499
500         session_init(SESSION(session));
501
502         SESSION(session)->type = SESSION_POP3;
503
504         SESSION(session)->recv_msg = pop3_session_recv_msg;
505         SESSION(session)->recv_data_finished = pop3_session_recv_data_finished;
506         SESSION(session)->send_data_finished = NULL;
507
508         SESSION(session)->destroy = pop3_session_destroy;
509
510         session->state = POP3_READY;
511         session->ac_prefs = account;
512         session->pop_before_smtp = FALSE;
513         pop3_get_uidl_table(account, session);
514         session->current_time = time(NULL);
515         session->error_val = PS_SUCCESS;
516         session->error_msg = NULL;
517
518         return SESSION(session);
519 }
520
521 static void pop3_session_destroy(Session *session)
522 {
523         Pop3Session *pop3_session = POP3_SESSION(session);
524         gint n;
525
526         g_return_if_fail(session != NULL);
527
528         for (n = 1; n <= pop3_session->count; n++)
529                 g_free(pop3_session->msg[n].uidl);
530         g_free(pop3_session->msg);
531
532         if (pop3_session->uidl_table) {
533                 hash_free_strings(pop3_session->uidl_table);
534                 g_hash_table_destroy(pop3_session->uidl_table);
535         }
536
537         if (pop3_session->partial_recv_table) {
538                 hash_free_strings(pop3_session->partial_recv_table);
539                 g_hash_table_destroy(pop3_session->partial_recv_table);
540         }
541
542         g_free(pop3_session->greeting);
543         g_free(pop3_session->user);
544         g_free(pop3_session->pass);
545         g_free(pop3_session->error_msg);
546 }
547
548 void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session)
549 {
550         GHashTable *table;
551         GHashTable *partial_recv_table;
552         gchar *path;
553         FILE *fp;
554         gchar buf[POPBUFSIZE];
555         gchar uidl[POPBUFSIZE];
556         time_t recv_time;
557         time_t now;
558         gint partial_recv;
559         
560         table = g_hash_table_new(g_str_hash, g_str_equal);
561         partial_recv_table = g_hash_table_new(g_str_hash, g_str_equal);
562
563         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
564                            "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server,
565                            "-", ac_prefs->userid, NULL);
566         if ((fp = fopen(path, "rb")) == NULL) {
567                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
568                 g_free(path);
569                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
570                                    "uidl-", ac_prefs->recv_server,
571                                    "-", ac_prefs->userid, NULL);
572                 if ((fp = fopen(path, "rb")) == NULL) {
573                         if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
574                         g_free(path);
575                         session->uidl_table = table;
576                         session->partial_recv_table = partial_recv_table;
577                         return;
578                 }
579         }
580         g_free(path);
581
582         now = time(NULL);
583
584         while (fgets(buf, sizeof(buf), fp) != NULL) {
585                 gchar tmp[POPBUFSIZE];
586                 strretchomp(buf);
587                 recv_time = RECV_TIME_NONE;
588                 partial_recv = POP3_TOTALLY_RECEIVED;
589                 
590                 if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, &tmp) < 2) {
591                         if (sscanf(buf, "%s", uidl) != 1)
592                                 continue;
593                         else {
594                                 recv_time = now;
595                         }
596                 }
597                 if (recv_time == RECV_TIME_NONE)
598                         recv_time = RECV_TIME_RECEIVED;
599                 g_hash_table_insert(table, g_strdup(uidl),
600                                     GINT_TO_POINTER(recv_time));
601                 if (strlen(tmp) == 1)
602                         partial_recv = atoi(tmp); /* totally received ?*/
603                 else
604                         partial_recv = POP3_MUST_COMPLETE_RECV;
605
606                 g_hash_table_insert(partial_recv_table, g_strdup(uidl),
607                                     GINT_TO_POINTER(partial_recv));
608         }
609
610         fclose(fp);
611         session->uidl_table = table;
612         session->partial_recv_table = partial_recv_table;
613         
614         return;
615 }
616
617 int pop3_msg_in_uidl_list(const gchar *server, const gchar *login, 
618                           const gchar *muidl)
619 {
620         gchar *path;
621         FILE *fp;
622         gchar buf[POPBUFSIZE];
623         gchar uidl[POPBUFSIZE];
624         time_t recv_time;
625         time_t now;
626         gint partial_recv;
627         
628         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
629                            "uidl", G_DIR_SEPARATOR_S, server,
630                            "-", login, NULL);
631         if ((fp = fopen(path, "rb")) == NULL) {
632                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
633                 g_free(path);
634                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
635                                    "uidl-", server,
636                                    "-", login, NULL);
637                 if ((fp = fopen(path, "rb")) == NULL) {
638                         if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
639                         g_free(path);
640                         return FALSE;
641                 }
642         }
643         g_free(path);
644
645         now = time(NULL);
646
647         while (fgets(buf, sizeof(buf), fp) != NULL) {
648                 gchar tmp[POPBUFSIZE];
649                 strretchomp(buf);
650                 recv_time = RECV_TIME_NONE;
651                 partial_recv = POP3_TOTALLY_RECEIVED;
652                 
653                 if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, &tmp) < 2) {
654                         if (sscanf(buf, "%s", uidl) != 1)
655                                 continue;
656                         else {
657                                 recv_time = now;
658                         }
659                 }
660                 if (!strcmp(uidl, muidl)) {
661                         fclose(fp);
662                         return TRUE;
663                 }
664         }
665
666         fclose(fp);     
667         return FALSE;
668 }
669
670 static gchar *pop3_get_filename_for_partial_mail(Pop3Session *session, 
671                                                  gchar *muidl)
672 {
673         gchar *path;
674         gchar *result = NULL;
675         FILE *fp;
676         gchar buf[POPBUFSIZE];
677         gchar uidl[POPBUFSIZE];
678         time_t recv_time;
679         time_t now;
680         gint partial_recv;
681         
682         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
683                            "uidl", G_DIR_SEPARATOR_S, 
684                            session->ac_prefs->recv_server,
685                            "-", session->ac_prefs->userid, NULL);
686         if ((fp = fopen(path, "rb")) == NULL) {
687                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
688                 g_free(path);
689                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
690                                    "uidl-", session->ac_prefs->recv_server,
691                                    "-", session->ac_prefs->userid, NULL);
692                 if ((fp = fopen(path, "rb")) == NULL) {
693                         if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
694                         g_free(path);
695                         return result;
696                 }
697         }
698         g_free(path);
699
700         now = time(NULL);
701
702         while (fgets(buf, sizeof(buf), fp) != NULL) {
703                 gchar tmp[POPBUFSIZE];
704                 strretchomp(buf);
705                 recv_time = RECV_TIME_NONE;
706                 partial_recv = POP3_TOTALLY_RECEIVED;
707                 
708                 if (sscanf(buf, "%s\t%ld\t%s", uidl, &recv_time, &tmp) < 2) {
709                         if (sscanf(buf, "%s", uidl) != 1)
710                                 continue;
711                         else {
712                                 recv_time = now;
713                         }
714                 }
715                 if (!strcmp(muidl, uidl)) {
716                         result = strdup(tmp);
717                         break;
718                 }
719         }
720
721         fclose(fp);
722         
723         return result;
724 }
725
726 static int pop3_uidl_mark_mail(MsgInfo *msginfo, int download)
727 {
728         gchar *path;
729         gchar *pathnew;
730         FILE *fp;
731         FILE *fpnew;
732         gchar buf[POPBUFSIZE];
733         gchar uidl[POPBUFSIZE];
734         time_t recv_time;
735         time_t now;
736         int len;
737         int start = TRUE;
738         gchar partial_recv[POPBUFSIZE];
739         int err = -1;
740
741         gchar *filename;
742         MsgInfo *tinfo;
743         filename = procmsg_get_message_file_path(msginfo);
744         if (!filename) {
745                 g_warning("can't get message file path.\n");
746                 return err;
747         }
748         tinfo = procheader_parse_file(filename, msginfo->flags, TRUE, TRUE);
749
750         if (!tinfo->account_server
751         ||  !tinfo->account_login
752         ||  !tinfo->partial_recv) {
753                 goto bail;
754         }
755         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
756                            "uidl", G_DIR_SEPARATOR_S, tinfo->account_server,
757                            "-", tinfo->account_login, NULL);
758         if ((fp = fopen(path, "rb")) == NULL) {
759                 perror("fopen1");
760                 if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
761                 g_free(path);
762                 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
763                                    "uidl-", tinfo->account_server,
764                                    "-", tinfo->account_login, NULL);
765                 if ((fp = fopen(path, "rb")) == NULL) {
766                         if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
767                         g_free(path);
768                 }
769                 goto bail;
770         }
771
772         pathnew = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
773                            "uidl", G_DIR_SEPARATOR_S, tinfo->account_server,
774                            "-", tinfo->account_login, ".new", NULL);
775         if ((fpnew = fopen(pathnew, "wb")) == NULL) {
776                 perror("fopen2");
777                 fclose(fp);
778                 g_free(pathnew);
779                 goto bail;
780         }
781         
782         now = time(NULL);
783
784         while (fgets(buf, sizeof(buf), fp) != NULL) {
785                 strretchomp(buf);
786                 recv_time = RECV_TIME_NONE;
787                 sprintf(partial_recv,"0");
788                 
789                 if (sscanf(buf, "%s\t%ld\t%s", 
790                            uidl, &recv_time, &partial_recv) < 2) {
791                         if (sscanf(buf, "%s", uidl) != 1)
792                                 continue;
793                         else {
794                                 recv_time = now;
795                         }
796                 }
797                 if (strcmp(tinfo->partial_recv, uidl)) {
798                         fprintf(fpnew, "%s\t%ld\t%s\n", 
799                                 uidl, recv_time, partial_recv);
800                 } else {
801                         gchar *stat = NULL;
802                         if (download == POP3_PARTIAL_DLOAD_DLOAD) {
803                                 gchar *folder_id = folder_item_get_identifier(
804                                                         msginfo->folder);
805                                 stat = g_strdup_printf("%s:%d",
806                                         folder_id, msginfo->msgnum);
807                                 g_free(folder_id);
808                         }
809                         else if (download == POP3_PARTIAL_DLOAD_UNKN)
810                                 stat = strdup("1");
811                         else if (download == POP3_PARTIAL_DLOAD_DELE)
812                                 stat = strdup("0");
813                         
814                         fprintf(fpnew, "%s\t%ld\t%s\n", 
815                                 uidl, recv_time, stat);
816                         g_free(stat);
817                 }
818         }
819         fclose(fpnew);
820         fclose(fp);
821
822         move_file(pathnew, path, TRUE);
823
824         g_free(path);
825         g_free(pathnew);
826         
827         if ((fp = fopen(filename,"rb")) == NULL) {
828                 perror("fopen3");
829                 goto bail;
830         }
831         pathnew = g_strdup_printf("%s.new", filename);
832         if ((fpnew = fopen(pathnew, "wb")) == NULL) {
833                 perror("fopen4");
834                 fclose(fp);
835                 g_free(pathnew);
836                 goto bail;
837         }
838         
839         while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
840                 buf[len]='\0';
841                 if (start) {
842                         start = FALSE;
843                         fprintf(fpnew, "SC-Marked-For-Download: %d\n", 
844                                         download);
845                         
846                         if(strlen(buf) > strlen("SC-Marked-For-Download: x\n")
847                         && !strncmp(buf, "SC-Marked-For-Download:", 
848                                     strlen("SC-Marked-For-Download:"))) {
849                                 fprintf(fpnew, "%s", 
850                                  buf+strlen("SC-Marked-For-Download: x\n"));
851                                 continue;
852                         }
853                 }
854                 fprintf(fpnew, "%s", buf);
855         }
856         fclose(fpnew);
857         fclose(fp);
858         unlink(filename);
859         rename(pathnew, filename);
860         g_free(pathnew);
861         msginfo->planned_download = download;
862         msgcache_update_msg(msginfo->folder->cache, msginfo);
863
864         err = 0;
865 bail:
866         g_free(filename);
867         procmsg_msginfo_free(tinfo);
868         
869         return err;
870 }
871
872 /* Partial download:
873  * A mail which has been completely downloaded will have no special headers,
874  * and its entry in the uidl file will end by 0 (POP3_TOTALLY_RECEIVED);
875  *
876  * A mail which has been partially downloaded will have some special headers,
877  * and its entry in the uidl file will first be 1 (POP3_PARTIALLY_RECEIVED);
878  * the special headers will be including "SC-Marked-For-Download" which can 
879  * have three values:
880  * 0 (POP3_PARTIAL_DLOAD_UNKN) meaning that the user has not yet chosen to
881  *  download the mail or let it be deleted - this header is absent until the
882  *  user first chooses an action
883  * 1 (POP3_PARTIAL_DLOAD_DLOAD) meaning that the user wants to finish 
884  *  downloading the mail
885  * 2 (POP3_PARTIAL_DLOAD_DELE) meaning that the user does not want to finish
886  *  downloading the mail
887  * When updating this header to POP3_PARTIAL_DLOAD_DLOAD, the uidl line of
888  * this mail will end with the mail's physical path, which Sylpheed will remove
889  * after having downloaded the complete mail. msg->partial_recv will equal
890  * 2 (POP3_MUST_COMPLETE_RECV).
891  * When updating this header to POP3_PARTIAL_DLOAD_DELE, the uidl line of
892  * this mail will be 0 (POP3_TOTALLY_RECEIVED), which will let Sylpheed delete
893  * this mail from the server as soon as the leave_time preference specifies.
894  */
895  
896 int pop3_mark_for_delete(MsgInfo *msginfo)
897 {
898         return pop3_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DELE);
899 }
900
901 int pop3_mark_for_download(MsgInfo *msginfo)
902 {
903         return pop3_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_DLOAD);
904 }
905
906 int pop3_unmark(MsgInfo *msginfo)
907 {
908         return pop3_uidl_mark_mail(msginfo, POP3_PARTIAL_DLOAD_UNKN);
909 }
910
911 static void pop3_delete_old_partial(const gchar *file) 
912 {
913         gchar *id = strdup(file);
914         gchar *snum = strrchr(file, ':');
915         int num = 0;
916         FolderItem *item = NULL;
917
918         debug_print("too big message updated,should remove %s\n", file);
919
920         if (snum) {
921                 snum++;
922         } else {
923                 g_free(id);
924                 return; /* not a real problem */
925         }
926
927         num = atoi(snum);
928
929         if (strrchr(id, ':'))
930                 *(strrchr(id, ':'))='\0';
931
932         item = folder_find_item_from_identifier(id);
933         if (item) {
934                 debug_print("removing %d in %s\n", num, id);
935                 folder_item_remove_msg(item, num);
936         } 
937         g_free(id);
938 }
939
940 gint pop3_write_uidl_list(Pop3Session *session)
941 {
942         gchar *path;
943         FILE *fp;
944         Pop3MsgInfo *msg;
945         gint n;
946
947         if (!session->uidl_is_valid) return 0;
948
949         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
950                            "uidl", G_DIR_SEPARATOR_S,
951                            session->ac_prefs->recv_server,
952                            "-", session->ac_prefs->userid, NULL);
953         if ((fp = fopen(path, "wb")) == NULL) {
954                 FILE_OP_ERROR(path, "fopen");
955                 g_free(path);
956                 return -1;
957         }
958
959         for (n = 1; n <= session->count; n++) {
960                 msg = &session->msg[n];
961                 if (msg->uidl && msg->received && !msg->deleted) {
962                         fprintf(fp, "%s\t%ld\t%d\n", 
963                                 msg->uidl, msg->recv_time, msg->partial_recv);
964                 }
965         }
966
967         if (fclose(fp) == EOF) FILE_OP_ERROR(path, "fclose");
968         g_free(path);
969
970         return 0;
971 }
972
973 static gint pop3_write_msg_to_file(const gchar *file, const gchar *data,
974                                    guint len, const gchar *prefix)
975 {
976         FILE *fp;
977         const gchar *prev, *cur;
978
979         g_return_val_if_fail(file != NULL, -1);
980
981         if ((fp = fopen(file, "wb")) == NULL) {
982                 FILE_OP_ERROR(file, "fopen");
983                 return -1;
984         }
985
986         if (change_file_mode_rw(fp, file) < 0)
987                 FILE_OP_ERROR(file, "chmod");
988
989         if (prefix != NULL) {
990                 fprintf(fp, prefix);
991                 fprintf(fp, "\n");
992         }
993         
994         /* +------------------+----------------+--------------------------+ *
995          * ^data              ^prev            ^cur             data+len-1^ */
996
997         prev = data;
998         while ((cur = memchr(prev, '\r', len - (prev - data))) != NULL) {
999                 if ((cur > prev && fwrite(prev, cur - prev, 1, fp) < 1) ||
1000                     fputc('\n', fp) == EOF) {
1001                         FILE_OP_ERROR(file, "fwrite");
1002                         g_warning("can't write to file: %s\n", file);
1003                         fclose(fp);
1004                         unlink(file);
1005                         return -1;
1006                 }
1007
1008                 if (cur == data + len - 1) {
1009                         prev = cur + 1;
1010                         break;
1011                 }
1012
1013                 if (*(cur + 1) == '\n')
1014                         prev = cur + 2;
1015                 else
1016                         prev = cur + 1;
1017
1018                 if (prev - data < len - 1 && *prev == '.' && *(prev + 1) == '.')
1019                         prev++;
1020
1021                 if (prev - data >= len)
1022                         break;
1023         }
1024
1025         if (prev - data < len &&
1026             fwrite(prev, len - (prev - data), 1, fp) < 1) {
1027                 FILE_OP_ERROR(file, "fwrite");
1028                 g_warning("can't write to file: %s\n", file);
1029                 fclose(fp);
1030                 unlink(file);
1031                 return -1;
1032         }
1033         if (data[len - 1] != '\r' && data[len - 1] != '\n') {
1034                 if (fputc('\n', fp) == EOF) {
1035                         FILE_OP_ERROR(file, "fputc");
1036                         g_warning("can't write to file: %s\n", file);
1037                         fclose(fp);
1038                         unlink(file);
1039                         return -1;
1040                 }
1041         }
1042
1043         if (fclose(fp) == EOF) {
1044                 FILE_OP_ERROR(file, "fclose");
1045                 unlink(file);
1046                 return -1;
1047         }
1048
1049         return 0;
1050 }
1051
1052 static Pop3State pop3_lookup_next(Pop3Session *session)
1053 {
1054         Pop3MsgInfo *msg;
1055         PrefsAccount *ac = session->ac_prefs;
1056         gint size;
1057         gboolean size_limit_over;
1058
1059         for (;;) {
1060                 msg = &session->msg[session->cur_msg];
1061                 size = msg->size;
1062                 size_limit_over =
1063                     (ac->enable_size_limit &&
1064                      ac->size_limit > 0 &&
1065                      size > ac->size_limit * 1024);
1066
1067                 if (ac->rmmail &&
1068                     msg->recv_time != RECV_TIME_NONE &&
1069                     msg->recv_time != RECV_TIME_KEEP &&
1070                     msg->partial_recv == POP3_TOTALLY_RECEIVED &&
1071                     session->current_time - msg->recv_time >=
1072                     ac->msg_leave_time * 24 * 60 * 60) {
1073                         log_message
1074                                 (_("POP3: Deleting expired message "
1075                                    "%d\n"), session->cur_msg);
1076                         pop3_delete_send(session);
1077                         return POP3_DELETE;
1078                 }
1079
1080                 if (size_limit_over) {
1081                         if (!msg->received && msg->partial_recv != 
1082                             POP3_MUST_COMPLETE_RECV) {
1083                                 pop3_top_send(session, ac->size_limit);
1084                                 return POP3_TOP;
1085                         } else if (msg->partial_recv == POP3_MUST_COMPLETE_RECV)
1086                                 break;
1087
1088                         log_message
1089                                 (_("POP3: Skipping message %d (%d bytes)\n"),
1090                                    session->cur_msg, size);
1091                 }
1092                 
1093                 if (size == 0 || msg->received || size_limit_over) {
1094                         session->cur_total_bytes += size;
1095                         if (session->cur_msg == session->count) {
1096                                 pop3_logout_send(session);
1097                                 return POP3_LOGOUT;
1098                         } else
1099                                 session->cur_msg++;
1100                 } else
1101                         break;
1102         }
1103
1104         pop3_retr_send(session);
1105         return POP3_RETR;
1106 }
1107
1108 static Pop3ErrorValue pop3_ok(Pop3Session *session, const gchar *msg)
1109 {
1110         Pop3ErrorValue ok;
1111
1112         log_print("POP3< %s\n", msg);
1113
1114         if (!strncmp(msg, "+OK", 3))
1115                 ok = PS_SUCCESS;
1116         else if (!strncmp(msg, "-ERR", 4)) {
1117                 if (strstr(msg + 4, "lock") ||
1118                     strstr(msg + 4, "Lock") ||
1119                     strstr(msg + 4, "LOCK") ||
1120                     strstr(msg + 4, "wait")) {
1121                         log_warning(_("mailbox is locked\n"));
1122                         ok = PS_LOCKBUSY;
1123                 } else if (strcasestr(msg + 4, "timeout")) {
1124                         log_warning(_("session timeout\n"));
1125                         ok = PS_ERROR;
1126                 } else {
1127                         switch (session->state) {
1128 #if USE_OPENSSL
1129                         case POP3_STLS:
1130                                 log_warning(_("can't start TLS session\n"));
1131                                 ok = PS_ERROR;
1132                                 break;
1133 #endif
1134                         case POP3_GETAUTH_USER:
1135                         case POP3_GETAUTH_PASS:
1136                         case POP3_GETAUTH_APOP:
1137                                 log_warning(_("error occurred on authentication\n"));
1138                                 ok = PS_AUTHFAIL;
1139                                 break;
1140                         case POP3_GETRANGE_LAST:
1141                         case POP3_GETRANGE_UIDL:
1142                         case POP3_TOP:
1143                                 log_warning(_("command not supported\n"));
1144                                 ok = PS_NOTSUPPORTED;
1145                                 break;
1146                                 
1147                         default:
1148                                 log_warning(_("error occurred on POP3 session\n"));
1149                                 ok = PS_ERROR;
1150                         }
1151                 }
1152
1153                 g_free(session->error_msg);
1154                 session->error_msg = g_strdup(msg);
1155                 fprintf(stderr, "POP3: %s\n", msg);
1156         } else
1157                 ok = PS_PROTOCOL;
1158
1159         session->error_val = ok;
1160         return ok;
1161 }
1162
1163 static gint pop3_session_recv_msg(Session *session, const gchar *msg)
1164 {
1165         Pop3Session *pop3_session = POP3_SESSION(session);
1166         Pop3ErrorValue val = PS_SUCCESS;
1167         const guchar *body;
1168
1169         body = msg;
1170         if (pop3_session->state != POP3_GETRANGE_UIDL_RECV &&
1171             pop3_session->state != POP3_GETSIZE_LIST_RECV) {
1172                 val = pop3_ok(pop3_session, msg);
1173                 if (val != PS_SUCCESS) {
1174                         if (val != PS_NOTSUPPORTED) {
1175                                 pop3_session->state = POP3_ERROR;
1176                                 return -1;
1177                         }
1178                 }
1179
1180                 if (*body == '+' || *body == '-')
1181                         body++;
1182                 while (isalpha(*body))
1183                         body++;
1184                 while (isspace(*body))
1185                         body++;
1186         }
1187
1188         switch (pop3_session->state) {
1189         case POP3_READY:
1190         case POP3_GREETING:
1191                 pop3_greeting_recv(pop3_session, body);
1192 #if USE_OPENSSL
1193                 if (pop3_session->ac_prefs->ssl_pop == SSL_STARTTLS)
1194                         pop3_stls_send(pop3_session);
1195                 else
1196 #endif
1197                 if (pop3_session->ac_prefs->protocol == A_APOP)
1198                         pop3_getauth_apop_send(pop3_session);
1199                 else
1200                         pop3_getauth_user_send(pop3_session);
1201                 break;
1202 #if USE_OPENSSL
1203         case POP3_STLS:
1204                 if (pop3_stls_recv(pop3_session) != PS_SUCCESS)
1205                         return -1;
1206                 if (pop3_session->ac_prefs->protocol == A_APOP)
1207                         pop3_getauth_apop_send(pop3_session);
1208                 else
1209                         pop3_getauth_user_send(pop3_session);
1210                 break;
1211 #endif
1212         case POP3_GETAUTH_USER:
1213                 pop3_getauth_pass_send(pop3_session);
1214                 break;
1215         case POP3_GETAUTH_PASS:
1216         case POP3_GETAUTH_APOP:
1217                 if (!pop3_session->pop_before_smtp)
1218                         pop3_getrange_stat_send(pop3_session);
1219                 else
1220                         pop3_logout_send(pop3_session);
1221                 break;
1222         case POP3_GETRANGE_STAT:
1223                 if (pop3_getrange_stat_recv(pop3_session, body) < 0)
1224                         return -1;
1225                 if (pop3_session->count > 0)
1226                         pop3_getrange_uidl_send(pop3_session);
1227                 else
1228                         pop3_logout_send(pop3_session);
1229                 break;
1230         case POP3_GETRANGE_LAST:
1231                 if (val == PS_NOTSUPPORTED)
1232                         pop3_session->error_val = PS_SUCCESS;
1233                 else if (pop3_getrange_last_recv(pop3_session, body) < 0)
1234                         return -1;
1235                 if (pop3_session->cur_msg > 0)
1236                         pop3_getsize_list_send(pop3_session);
1237                 else
1238                         pop3_logout_send(pop3_session);
1239                 break;
1240         case POP3_GETRANGE_UIDL:
1241                 if (val == PS_NOTSUPPORTED) {
1242                         pop3_session->error_val = PS_SUCCESS;
1243                         pop3_getrange_last_send(pop3_session);
1244                 } else {
1245                         pop3_session->state = POP3_GETRANGE_UIDL_RECV;
1246                         session_recv_data(session, 0, ".\r\n");
1247                 }
1248                 break;
1249         case POP3_GETSIZE_LIST:
1250                 pop3_session->state = POP3_GETSIZE_LIST_RECV;
1251                 session_recv_data(session, 0, ".\r\n");
1252                 break;
1253         case POP3_RETR:
1254                 pop3_session->state = POP3_RETR_RECV;
1255                 session_recv_data(session, 0, ".\r\n");
1256                 break;
1257         case POP3_TOP:
1258                 if (val == PS_NOTSUPPORTED) {
1259                         pop3_session->error_val = PS_SUCCESS;
1260                 } else {
1261                         pop3_session->state = POP3_TOP_RECV;
1262                         session_recv_data(session, 0, ".\r\n");
1263                 }
1264                 break;
1265         case POP3_DELETE:
1266                 pop3_delete_recv(pop3_session);
1267                 if (pop3_session->cur_msg == pop3_session->count)
1268                         pop3_logout_send(pop3_session);
1269                 else {
1270                         pop3_session->cur_msg++;
1271                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1272                                 return -1;
1273                 }
1274                 break;
1275         case POP3_LOGOUT:
1276                 session_disconnect(session);
1277                 break;
1278         case POP3_ERROR:
1279         default:
1280                 return -1;
1281         }
1282
1283         return 0;
1284 }
1285
1286 static gint pop3_session_recv_data_finished(Session *session, guchar *data,
1287                                             guint len)
1288 {
1289         Pop3Session *pop3_session = POP3_SESSION(session);
1290         Pop3ErrorValue val = PS_SUCCESS;
1291
1292         switch (pop3_session->state) {
1293         case POP3_GETRANGE_UIDL_RECV:
1294                 val = pop3_getrange_uidl_recv(pop3_session, data, len);
1295                 if (val == PS_SUCCESS) {
1296                         if (pop3_session->new_msg_exist)
1297                                 pop3_getsize_list_send(pop3_session);
1298                         else
1299                                 pop3_logout_send(pop3_session);
1300                 } else
1301                         return -1;
1302                 break;
1303         case POP3_GETSIZE_LIST_RECV:
1304                 val = pop3_getsize_list_recv(pop3_session, data, len);
1305                 if (val == PS_SUCCESS) {
1306                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1307                                 return -1;
1308                 } else
1309                         return -1;
1310                 break;
1311         case POP3_RETR_RECV:
1312                 if (pop3_retr_recv(pop3_session, data, len) < 0)
1313                         return -1;
1314
1315                 if (pop3_session->ac_prefs->rmmail &&
1316                     pop3_session->ac_prefs->msg_leave_time == 0 &&
1317                     pop3_session->msg[pop3_session->cur_msg].recv_time
1318                     != RECV_TIME_KEEP)
1319                         pop3_delete_send(pop3_session);
1320                 else if (pop3_session->cur_msg == pop3_session->count)
1321                         pop3_logout_send(pop3_session);
1322                 else {
1323                         pop3_session->cur_msg++;
1324                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1325                                 return -1;
1326                 }
1327                 break;
1328         case POP3_TOP_RECV:
1329                 if (pop3_top_recv(pop3_session, data, len) < 0)
1330                         return -1;
1331
1332                 if (pop3_session->cur_msg == pop3_session->count)
1333                         pop3_logout_send(pop3_session);
1334                 else {
1335                         pop3_session->cur_msg++;
1336                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1337                                 return -1;
1338                 }
1339                 break;
1340         case POP3_TOP:
1341                 log_warning(_("TOP command unsupported\n"));
1342                 if (pop3_session->cur_msg == pop3_session->count)
1343                         pop3_logout_send(pop3_session);
1344                 else {
1345                         pop3_session->cur_msg++;
1346                         if (pop3_lookup_next(pop3_session) == POP3_ERROR)
1347                                 return -1;
1348                 }
1349                 break;
1350         case POP3_ERROR:
1351         default:
1352                 return -1;
1353         }
1354
1355         return 0;
1356 }