sync with 0.4.66cvs2
[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         pop3_gen_send(sock, "LAST");
195
196         return POP3_GETRANGE_LAST_RECV;
197 }
198
199 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
200 {
201         Pop3State *state = (Pop3State *)data;
202         gchar buf[POPBUFSIZE + 1];
203
204         if (pop3_ok(sock, buf) == PS_SUCCESS) {
205                 gint last;
206
207                 if (sscanf(buf, "%d", &last) == 0) {
208                         log_warning(_("POP3 protocol error\n"));
209                         return -1;
210                 } else {
211                         if (state->count == last)
212                                 return POP3_LOGOUT_SEND;
213                         else {
214                                 state->new = state->count - last;
215                                 state->cur_msg = last + 1;
216                                 return POP3_GETSIZE_LIST_SEND;
217                         }
218                 }
219         } else
220                 return POP3_GETSIZE_LIST_SEND;
221 }
222
223 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
224 {
225         pop3_gen_send(sock, "UIDL");
226
227         return POP3_GETRANGE_UIDL_RECV;
228 }
229
230 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
231 {
232         Pop3State *state = (Pop3State *)data;
233         gboolean new = FALSE;
234         gchar buf[POPBUFSIZE];
235         gchar id[IDLEN + 1];
236
237         if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
238
239         if (!state->id_table) new = TRUE;
240
241         while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
242                 gint num;
243
244                 if (buf[0] == '.') break;
245                 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
246                         continue;
247
248                 if (new == FALSE &&
249                     g_hash_table_lookup(state->id_table, id) == NULL) {
250                         state->new = state->count - num + 1;
251                         state->cur_msg = num;
252                         new = TRUE;
253                 }
254
255                 if (new == TRUE)
256                         state->new_id_list = g_slist_append
257                                 (state->new_id_list, g_strdup(id));
258                 else
259                         state->id_list = g_slist_append
260                                 (state->id_list, g_strdup(id));
261         }
262
263         if (new == TRUE)
264                 return POP3_GETSIZE_LIST_SEND;
265         else
266                 return POP3_LOGOUT_SEND;
267 }
268
269 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
270 {
271         pop3_gen_send(sock, "LIST");
272
273         return POP3_GETSIZE_LIST_RECV;
274 }
275
276 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
277 {
278         Pop3State *state = (Pop3State *)data;
279         gchar buf[POPBUFSIZE];
280
281         if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
282
283         state->sizes = g_new0(gint, state->count + 1);
284         state->cur_total_bytes = 0;
285
286         while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
287                 gint num, size;
288
289                 if (buf[0] == '.') break;
290                 if (sscanf(buf, "%d %d", &num, &size) != 2)
291                         continue;
292
293                 if (num <= state->count)
294                         state->sizes[num] = size;
295                 if (num < state->cur_msg)
296                         state->cur_total_bytes += size;
297         }
298
299         return POP3_RETR_SEND;
300 }
301
302 gint pop3_retr_send(SockInfo *sock, gpointer data)
303 {
304         Pop3State *state = (Pop3State *)data;
305
306         inc_progress_update(state, POP3_RETR_SEND);
307
308         pop3_gen_send(sock, "RETR %d", state->cur_msg);
309
310         return POP3_RETR_RECV;
311 }
312
313 gint pop3_retr_recv(SockInfo *sock, gpointer data)
314 {
315         Pop3State *state = (Pop3State *)data;
316         const gchar *file;
317         gint ok, drop_ok;
318
319         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
320                 state->cur_msg_bytes = 0;
321
322                 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
323                         state->inc_state = INC_NOSPACE;
324                         return -1;
325                 }
326
327                 state->cur_total_bytes += state->sizes[state->cur_msg];
328
329                 if ((drop_ok = inc_drop_message(file, state)) < 0) {
330                         state->inc_state = INC_ERROR;
331                         return -1;
332                 }
333                 if (drop_ok == 0 && state->ac_prefs->rmmail)
334                         return POP3_DELETE_SEND;
335
336                 if (state->new_id_list) {
337                         state->id_list = g_slist_append
338                                 (state->id_list, state->new_id_list->data);
339                         state->new_id_list =
340                                 g_slist_remove(state->new_id_list,
341                                                state->new_id_list->data);
342                 }
343
344                 if (state->cur_msg < state->count) {
345                         state->cur_msg++;
346                         return POP3_RETR_SEND;
347                 } else
348                         return POP3_LOGOUT_SEND;
349         } else if (ok == PS_PROTOCOL)
350                 return POP3_LOGOUT_SEND;
351         else
352                 return -1;
353 }
354
355 gint pop3_delete_send(SockInfo *sock, gpointer data)
356 {
357         Pop3State *state = (Pop3State *)data;
358
359         //inc_progress_update(state, POP3_DELETE_SEND);
360
361         pop3_gen_send(sock, "DELE %d", state->cur_msg);
362
363         return POP3_DELETE_RECV;
364 }
365
366 gint pop3_delete_recv(SockInfo *sock, gpointer data)
367 {
368         Pop3State *state = (Pop3State *)data;
369         gint ok;
370
371         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
372                 if (state->cur_msg < state->count) {
373                         state->cur_msg++;
374                         return POP3_RETR_SEND;
375                 } else
376                         return POP3_LOGOUT_SEND;
377         } else if (ok == PS_PROTOCOL)
378                 return POP3_LOGOUT_SEND;
379         else
380                 return -1;
381 }
382
383 gint pop3_logout_send(SockInfo *sock, gpointer data)
384 {
385         Pop3State *state = (Pop3State *)data;
386
387         inc_progress_update(state, POP3_LOGOUT_SEND);
388
389         pop3_gen_send(sock, "QUIT");
390
391         return POP3_LOGOUT_RECV;
392 }
393
394 gint pop3_logout_recv(SockInfo *sock, gpointer data)
395 {
396         if (pop3_ok(sock, NULL) == PS_SUCCESS)
397                 return -1;
398         else
399                 return -1;
400 }
401
402 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
403 {
404         gint ok;
405         gchar buf[POPBUFSIZE + 1];
406         gchar *bufp;
407
408         if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
409                 bufp = buf;
410                 if (*bufp == '+' || *bufp == '-')
411                         bufp++;
412                 else
413                         return PS_PROTOCOL;
414
415                 while (isalpha(*bufp))
416                         bufp++;
417
418                 if (*bufp)
419                         *(bufp++) = '\0';
420
421                 if (!strcmp(buf, "+OK"))
422                         ok = PS_SUCCESS;
423                 else if (!strncmp(buf, "-ERR", 4)) {
424                         if (strstr(bufp, "lock") ||
425                                  strstr(bufp, "Lock") ||
426                                  strstr(bufp, "LOCK") ||
427                                  strstr(bufp, "wait"))
428                                 ok = PS_LOCKBUSY;
429                         else
430                                 ok = PS_PROTOCOL;
431
432                         if (*bufp)
433                                 fprintf(stderr, "POP3: %s\n", bufp);
434                 } else
435                         ok = PS_PROTOCOL;
436
437                 if (argbuf)
438                         strcpy(argbuf, bufp);
439         }
440
441         return ok;
442 }
443
444 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
445 {
446         gchar buf[POPBUFSIZE + 1];
447         va_list args;
448
449         va_start(args, format);
450         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
451         va_end(args);
452
453         if (!strncasecmp(buf, "PASS ", 5))
454                 log_print("POP3> PASS ********\n");
455         else
456                 log_print("POP3> %s\n", buf);
457
458         strcat(buf, "\r\n");
459         sock_write(sock, buf, strlen(buf));
460 }
461
462 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
463 {
464         if (sock_gets(sock, buf, size) < 0) {
465                 return PS_SOCKET;
466         } else {
467                 strretchomp(buf);
468                 log_print("POP3< %s\n", buf);
469
470                 return PS_SUCCESS;
471         }
472 }