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