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