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