dropped PSPELL_PATH macro
[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                 guint num, size;
302
303                 if (buf[0] == '.') break;
304                 if (sscanf(buf, "%u %u", &num, &size) != 2)
305                         return -1;
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         while (state->sizes[state->cur_msg] == 0) {
314                 if (state->cur_msg == state->count)
315                         return POP3_LOGOUT_SEND;
316                 else
317                         state->cur_msg++;
318         }
319
320         return POP3_RETR_SEND;
321 }
322
323 gint pop3_retr_send(SockInfo *sock, gpointer data)
324 {
325         Pop3State *state = (Pop3State *)data;
326
327         inc_progress_update(state, POP3_RETR_SEND);
328
329         pop3_gen_send(sock, "RETR %d", state->cur_msg);
330
331         return POP3_RETR_RECV;
332 }
333
334 gint pop3_retr_recv(SockInfo *sock, gpointer data)
335 {
336         Pop3State *state = (Pop3State *)data;
337         const gchar *file;
338         gint ok, drop_ok;
339
340         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
341                 if (recv_write_to_file(sock, (file = get_tmp_file())) < 0) {
342                         state->inc_state = INC_NOSPACE;
343                         return -1;
344                 }
345
346                 if ((drop_ok = inc_drop_message(file, state)) < 0) {
347                         state->inc_state = INC_ERROR;
348                         return -1;
349                 }
350
351                 state->cur_total_bytes += state->sizes[state->cur_msg];
352                 state->cur_total_num++;
353
354                 if (drop_ok == 0 && state->ac_prefs->rmmail)
355                         return POP3_DELETE_SEND;
356
357                 if (state->new_id_list) {
358                         state->id_list = g_slist_append
359                                 (state->id_list, state->new_id_list->data);
360                         state->new_id_list =
361                                 g_slist_remove(state->new_id_list,
362                                                state->new_id_list->data);
363                 }
364
365                 if (state->cur_msg < state->count) {
366                         state->cur_msg++;
367                         while (state->sizes[state->cur_msg] == 0) {
368                                 if (state->cur_msg == state->count)
369                                         return POP3_LOGOUT_SEND;
370                                 else
371                                         state->cur_msg++;
372                         }
373                         return POP3_RETR_SEND;
374                 } else
375                         return POP3_LOGOUT_SEND;
376         } else if (ok == PS_PROTOCOL)
377                 return POP3_LOGOUT_SEND;
378         else
379                 return -1;
380 }
381
382 gint pop3_delete_send(SockInfo *sock, gpointer data)
383 {
384         Pop3State *state = (Pop3State *)data;
385
386         /*inc_progress_update(state, POP3_DELETE_SEND);*/
387
388         pop3_gen_send(sock, "DELE %d", state->cur_msg);
389
390         return POP3_DELETE_RECV;
391 }
392
393 gint pop3_delete_recv(SockInfo *sock, gpointer data)
394 {
395         Pop3State *state = (Pop3State *)data;
396         gint ok;
397
398         if ((ok = pop3_ok(sock, NULL)) == PS_SUCCESS) {
399                 if (state->cur_msg < state->count) {
400                         state->cur_msg++;
401                         while (state->sizes[state->cur_msg] == 0) {
402                                 if (state->cur_msg == state->count)
403                                         return POP3_LOGOUT_SEND;
404                                 else
405                                         state->cur_msg++;
406                         }
407                         return POP3_RETR_SEND;
408                 } else
409                         return POP3_LOGOUT_SEND;
410         } else if (ok == PS_PROTOCOL)
411                 return POP3_LOGOUT_SEND;
412         else
413                 return -1;
414 }
415
416 gint pop3_logout_send(SockInfo *sock, gpointer data)
417 {
418         Pop3State *state = (Pop3State *)data;
419
420         inc_progress_update(state, POP3_LOGOUT_SEND);
421
422         pop3_gen_send(sock, "QUIT");
423
424         return POP3_LOGOUT_RECV;
425 }
426
427 gint pop3_logout_recv(SockInfo *sock, gpointer data)
428 {
429         if (pop3_ok(sock, NULL) == PS_SUCCESS)
430                 return -1;
431         else
432                 return -1;
433 }
434
435 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
436 {
437         gint ok;
438         gchar buf[POPBUFSIZE + 1];
439         gchar *bufp;
440
441         if ((ok = pop3_gen_recv(sock, buf, sizeof(buf))) == PS_SUCCESS) {
442                 bufp = buf;
443                 if (*bufp == '+' || *bufp == '-')
444                         bufp++;
445                 else
446                         return PS_PROTOCOL;
447
448                 while (isalpha(*bufp))
449                         bufp++;
450
451                 if (*bufp)
452                         *(bufp++) = '\0';
453
454                 if (!strcmp(buf, "+OK"))
455                         ok = PS_SUCCESS;
456                 else if (!strncmp(buf, "-ERR", 4)) {
457                         if (strstr(bufp, "lock") ||
458                                  strstr(bufp, "Lock") ||
459                                  strstr(bufp, "LOCK") ||
460                                  strstr(bufp, "wait"))
461                                 ok = PS_LOCKBUSY;
462                         else
463                                 ok = PS_PROTOCOL;
464
465                         if (*bufp)
466                                 fprintf(stderr, "POP3: %s\n", bufp);
467                 } else
468                         ok = PS_PROTOCOL;
469
470                 if (argbuf)
471                         strcpy(argbuf, bufp);
472         }
473
474         return ok;
475 }
476
477 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
478 {
479         gchar buf[POPBUFSIZE + 1];
480         va_list args;
481
482         va_start(args, format);
483         g_vsnprintf(buf, sizeof(buf) - 2, format, args);
484         va_end(args);
485
486         if (!strncasecmp(buf, "PASS ", 5))
487                 log_print("POP3> PASS ********\n");
488         else
489                 log_print("POP3> %s\n", buf);
490
491         strcat(buf, "\r\n");
492         sock_write(sock, buf, strlen(buf));
493 }
494
495 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
496 {
497         if (sock_gets(sock, buf, size) < 0) {
498                 return PS_SOCKET;
499         } else {
500                 strretchomp(buf);
501                 log_print("POP3< %s\n", buf);
502
503                 return PS_SUCCESS;
504         }
505 }