fix folder update stats (I hope)
[claws.git] / src / pop.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 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
40 static gint pop3_ok(SockInfo *sock, gchar *argbuf);
41 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...);
42 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size);
43
44 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
45 {
46         Pop3State *state = (Pop3State *)data;
47         gchar buf[POPBUFSIZE];
48
49         if (pop3_ok(sock, buf) == PS_SUCCESS) {
50                 if (state->ac_prefs->protocol == A_APOP) {
51                         state->greeting = g_strdup(buf);
52                         return POP3_GETAUTH_APOP_SEND;
53                 } else
54                         return POP3_GETAUTH_USER_SEND;
55         } else
56                 return -1;
57 }
58
59 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
60 {
61         Pop3State *state = (Pop3State *)data;
62
63         g_return_val_if_fail(state->user != NULL, -1);
64
65         inc_progress_update(state, POP3_GETAUTH_USER_SEND);
66
67         pop3_gen_send(sock, "USER %s", state->user);
68
69         return POP3_GETAUTH_USER_RECV;
70 }
71
72 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
73 {
74         if (pop3_ok(sock, NULL) == PS_SUCCESS)
75                 return POP3_GETAUTH_PASS_SEND;
76         else
77                 return -1;
78 }
79
80 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
81 {
82         Pop3State *state = (Pop3State *)data;
83
84         g_return_val_if_fail(state->pass != NULL, -1);
85
86         pop3_gen_send(sock, "PASS %s", state->pass);
87
88         return POP3_GETAUTH_PASS_RECV;
89 }
90
91 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
92 {
93         Pop3State *state = (Pop3State *)data;
94
95         if (pop3_ok(sock, NULL) == PS_SUCCESS)
96                 return POP3_GETRANGE_STAT_SEND;
97         else {
98                 log_warning(_("error occurred on authorization\n"));
99                 state->error_val = PS_AUTHFAIL;
100                 return -1;
101         }
102 }
103
104 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
105 {
106         Pop3State *state = (Pop3State *)data;
107         gchar *start, *end;
108         gchar *apop_str;
109         gchar md5sum[33];
110
111         g_return_val_if_fail(state->user != NULL, -1);
112         g_return_val_if_fail(state->pass != NULL, -1);
113
114         inc_progress_update(state, POP3_GETAUTH_APOP_SEND);
115
116         if ((start = strchr(state->greeting, '<')) == NULL) {
117                 log_warning(_("Required APOP timestamp not found "
118                               "in greeting\n"));
119                 return -1;
120         }
121
122         if ((end = strchr(start, '>')) == NULL || end == start + 1) {
123                 log_warning(_("Timestamp syntax error in greeting\n"));
124                 return -1;
125         }
126
127         *(end + 1) = '\0';
128
129         apop_str = g_strconcat(start, state->pass, NULL);
130         md5_hex_digest(md5sum, apop_str);
131         g_free(apop_str);
132
133         pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
134
135         return POP3_GETAUTH_APOP_RECV;
136 }
137
138 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
139 {
140         Pop3State *state = (Pop3State *)data;
141
142         if (pop3_ok(sock, NULL) == PS_SUCCESS)
143                 return POP3_GETRANGE_STAT_SEND;
144         else {
145                 log_warning(_("error occurred on authorization\n"));
146                 state->error_val = PS_AUTHFAIL;
147                 return -1;
148         }
149 }
150
151 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
152 {
153         Pop3State *state = (Pop3State *)data;
154
155         inc_progress_update(state, POP3_GETRANGE_STAT_SEND);
156
157         pop3_gen_send(sock, "STAT");
158
159         return POP3_GETRANGE_STAT_RECV;
160 }
161
162 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
163 {
164         Pop3State *state = (Pop3State *)data;
165         gchar buf[POPBUFSIZE + 1];
166         gint ok;
167
168         if ((ok = pop3_ok(sock, buf)) == PS_SUCCESS) {
169                 if (sscanf(buf, "%d %d", &state->count, &state->total_bytes)
170                     != 2) {
171                         log_warning(_("POP3 protocol error\n"));
172                         return -1;
173                 } else {
174                         if (state->count == 0)
175                                 return POP3_LOGOUT_SEND;
176                         else {
177                                 state->cur_msg = 1;
178                                 state->new = state->count;
179                                 if (state->ac_prefs->rmmail ||
180                                     state->ac_prefs->getall)
181                                         return POP3_GETSIZE_LIST_SEND;
182                                 else
183                                         return POP3_GETRANGE_UIDL_SEND;
184                         }
185                 }
186         } else if (ok == PS_PROTOCOL)
187                 return POP3_LOGOUT_SEND;
188         else
189                 return -1;
190 }
191
192 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
193 {
194         Pop3State *state = (Pop3State *)data;
195
196         inc_progress_update(state, POP3_GETRANGE_LAST_SEND);
197
198         pop3_gen_send(sock, "LAST");
199
200         return POP3_GETRANGE_LAST_RECV;
201 }
202
203 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
204 {
205         Pop3State *state = (Pop3State *)data;
206         gchar buf[POPBUFSIZE + 1];
207
208         if (pop3_ok(sock, buf) == PS_SUCCESS) {
209                 gint last;
210
211                 if (sscanf(buf, "%d", &last) == 0) {
212                         log_warning(_("POP3 protocol error\n"));
213                         return -1;
214                 } else {
215                         if (state->count == last)
216                                 return POP3_LOGOUT_SEND;
217                         else {
218                                 state->new = state->count - last;
219                                 state->cur_msg = last + 1;
220                                 return POP3_GETSIZE_LIST_SEND;
221                         }
222                 }
223         } else
224                 return POP3_GETSIZE_LIST_SEND;
225 }
226
227 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
228 {
229         Pop3State *state = (Pop3State *)data;
230
231         inc_progress_update(state, POP3_GETRANGE_UIDL_SEND);
232
233         pop3_gen_send(sock, "UIDL");
234
235         return POP3_GETRANGE_UIDL_RECV;
236 }
237
238 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
239 {
240         Pop3State *state = (Pop3State *)data;
241         gboolean new = FALSE;
242         gchar buf[POPBUFSIZE];
243         gchar id[IDLEN + 1];
244
245         if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
246
247         if (!state->id_table) new = TRUE;
248
249         while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
250                 gint num;
251
252                 if (buf[0] == '.') break;
253                 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
254                         continue;
255
256                 if (new == FALSE &&
257                     g_hash_table_lookup(state->id_table, id) == NULL) {
258                         state->new = state->count - num + 1;
259                         state->cur_msg = num;
260                         new = TRUE;
261                 }
262
263                 if (new == TRUE)
264                         state->new_id_list = g_slist_append
265                                 (state->new_id_list, g_strdup(id));
266                 else
267                         state->id_list = g_slist_append
268                                 (state->id_list, g_strdup(id));
269         }
270
271         if (new == TRUE)
272                 return POP3_GETSIZE_LIST_SEND;
273         else
274                 return POP3_LOGOUT_SEND;
275 }
276
277 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
278 {
279         Pop3State *state = (Pop3State *)data;
280
281         inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
282
283         pop3_gen_send(sock, "LIST");
284
285         return POP3_GETSIZE_LIST_RECV;
286 }
287
288 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
289 {
290         Pop3State *state = (Pop3State *)data;
291         gchar buf[POPBUFSIZE];
292
293         if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
294
295         state->sizes = g_new0(gint, state->count + 1);
296         state->cur_total_bytes = 0;
297
298         while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
299                 gint num, size;
300
301                 if (buf[0] == '.') break;
302                 if (sscanf(buf, "%d %d", &num, &size) != 2)
303                         break;
304
305                 if (num <= state->count)
306                         state->sizes[num] = size;
307                 if (num < state->cur_msg)
308                         state->cur_total_bytes += size;
309         }
310
311         return POP3_RETR_SEND;
312 }
313
314 gint pop3_retr_send(SockInfo *sock, gpointer data)
315 {
316         Pop3State *state = (Pop3State *)data;
317
318         inc_progress_update(state, POP3_RETR_SEND);
319
320         pop3_gen_send(sock, "RETR %d", state->cur_msg);
321
322         return POP3_RETR_RECV;
323 }
324
325 gint pop3_retr_recv(SockInfo *sock, gpointer data)
326 {
327         Pop3State *state = (Pop3State *)data;
328         const gchar *file;
329         gint ok, drop_ok;
330
331         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
332                 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
333                         state->inc_state = INC_NOSPACE;
334                         return -1;
335                 }
336
337                 state->cur_total_bytes += state->sizes[state->cur_msg];
338
339                 if ((drop_ok = inc_drop_message(file, state)) < 0) {
340                         state->inc_state = INC_ERROR;
341                         return -1;
342                 }
343                 if (drop_ok == 0 && state->ac_prefs->rmmail)
344                         return POP3_DELETE_SEND;
345
346                 if (state->new_id_list) {
347                         state->id_list = g_slist_append
348                                 (state->id_list, state->new_id_list->data);
349                         state->new_id_list =
350                                 g_slist_remove(state->new_id_list,
351                                                state->new_id_list->data);
352                 }
353
354                 if (state->cur_msg < state->count) {
355                         state->cur_msg++;
356                         return POP3_RETR_SEND;
357                 } else
358                         return POP3_LOGOUT_SEND;
359         } else if (ok == PS_PROTOCOL)
360                 return POP3_LOGOUT_SEND;
361         else
362                 return -1;
363 }
364
365 gint pop3_delete_send(SockInfo *sock, gpointer data)
366 {
367         Pop3State *state = (Pop3State *)data;
368
369         /*inc_progress_update(state, POP3_DELETE_SEND);*/
370
371         pop3_gen_send(sock, "DELE %d", state->cur_msg);
372
373         return POP3_DELETE_RECV;
374 }
375
376 gint pop3_delete_recv(SockInfo *sock, gpointer data)
377 {
378         Pop3State *state = (Pop3State *)data;
379         gint ok;
380
381         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
382                 if (state->cur_msg < state->count) {
383                         state->cur_msg++;
384                         return POP3_RETR_SEND;
385                 } else
386                         return POP3_LOGOUT_SEND;
387         } else if (ok == PS_PROTOCOL)
388                 return POP3_LOGOUT_SEND;
389         else
390                 return -1;
391 }
392
393 gint pop3_logout_send(SockInfo *sock, gpointer data)
394 {
395         Pop3State *state = (Pop3State *)data;
396
397         inc_progress_update(state, POP3_LOGOUT_SEND);
398
399         pop3_gen_send(sock, "QUIT");
400
401         return POP3_LOGOUT_RECV;
402 }
403
404 gint pop3_logout_recv(SockInfo *sock, gpointer data)
405 {
406         if (pop3_ok(sock, NULL) == PS_SUCCESS)
407                 return -1;
408         else
409                 return -1;
410 }
411
412 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
413 {
414         gint ok;
415         gchar buf[POPBUFSIZE + 1];
416         gchar *bufp;
417
418         if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
419                 bufp = buf;
420                 if (*bufp == '+' || *bufp == '-')
421                         bufp++;
422                 else
423                         return PS_PROTOCOL;
424
425                 while (isalpha(*bufp))
426                         bufp++;
427
428                 if (*bufp)
429                         *(bufp++) = '\0';
430
431                 if (!strcmp(buf, "+OK"))
432                         ok = PS_SUCCESS;
433                 else if (!strncmp(buf, "-ERR", 4)) {
434                         if (strstr(bufp, "lock") ||
435                                  strstr(bufp, "Lock") ||
436                                  strstr(bufp, "LOCK") ||
437                                  strstr(bufp, "wait"))
438                                 ok = PS_LOCKBUSY;
439                         else
440                                 ok = PS_PROTOCOL;
441
442                         if (*bufp)
443                                 fprintf(stderr, "POP3: %s\n", bufp);
444                 } else
445                         ok = PS_PROTOCOL;
446
447                 if (argbuf)
448                         strcpy(argbuf, bufp);
449         }
450
451         return ok;
452 }
453
454 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
455 {
456         gchar buf[POPBUFSIZE + 1];
457         va_list args;
458
459         va_start(args, format);
460         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
461         va_end(args);
462
463         if (!strncasecmp(buf, "PASS ", 5))
464                 log_print("POP3> PASS ********\n");
465         else
466                 log_print("POP3> %s\n", buf);
467
468         strcat(buf, "\r\n");
469         sock_write(sock, buf, strlen(buf));
470 }
471
472 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
473 {
474         if (sock_gets(sock, buf, size) < 0) {
475                 return PS_SOCKET;
476         } else {
477                 strretchomp(buf);
478                 log_print("POP3< %s\n", buf);
479
480                 return PS_SUCCESS;
481         }
482 }