initial message templates support
[claws.git] / src / pop.c
blob48853c49a175c7073abb861564a1d7499320b45b
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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.
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.
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.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <glib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <ctype.h>
29 #include <unistd.h>
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"
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);
44 gint pop3_greeting_recv(SockInfo *sock, gpointer data)
46 Pop3State *state = (Pop3State *)data;
47 gchar buf[POPBUFSIZE];
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;
59 gint pop3_getauth_user_send(SockInfo *sock, gpointer data)
61 Pop3State *state = (Pop3State *)data;
63 g_return_val_if_fail(state->user != NULL, -1);
65 inc_progress_update(state, POP3_GETAUTH_USER_SEND);
67 pop3_gen_send(sock, "USER %s", state->user);
69 return POP3_GETAUTH_USER_RECV;
72 gint pop3_getauth_user_recv(SockInfo *sock, gpointer data)
74 if (pop3_ok(sock, NULL) == PS_SUCCESS)
75 return POP3_GETAUTH_PASS_SEND;
76 else
77 return -1;
80 gint pop3_getauth_pass_send(SockInfo *sock, gpointer data)
82 Pop3State *state = (Pop3State *)data;
84 g_return_val_if_fail(state->pass != NULL, -1);
86 pop3_gen_send(sock, "PASS %s", state->pass);
88 return POP3_GETAUTH_PASS_RECV;
91 gint pop3_getauth_pass_recv(SockInfo *sock, gpointer data)
93 Pop3State *state = (Pop3State *)data;
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;
105 gint pop3_getauth_apop_send(SockInfo *sock, gpointer data)
107 Pop3State *state = (Pop3State *)data;
108 gchar *start, *end;
109 gchar *apop_str;
110 gchar md5sum[33];
112 g_return_val_if_fail(state->user != NULL, -1);
113 g_return_val_if_fail(state->pass != NULL, -1);
115 inc_progress_update(state, POP3_GETAUTH_APOP_SEND);
117 if ((start = strchr(state->greeting, '<')) == NULL) {
118 log_warning(_("Required APOP timestamp not found "
119 "in greeting\n"));
120 return -1;
123 if ((end = strchr(start, '>')) == NULL || end == start + 1) {
124 log_warning(_("Timestamp syntax error in greeting\n"));
125 return -1;
128 *(end + 1) = '\0';
130 apop_str = g_strconcat(start, state->pass, NULL);
131 md5_hex_digest(md5sum, apop_str);
132 g_free(apop_str);
134 pop3_gen_send(sock, "APOP %s %s", state->user, md5sum);
136 return POP3_GETAUTH_APOP_RECV;
139 gint pop3_getauth_apop_recv(SockInfo *sock, gpointer data)
141 Pop3State *state = (Pop3State *)data;
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;
153 gint pop3_getrange_stat_send(SockInfo *sock, gpointer data)
155 Pop3State *state = (Pop3State *)data;
157 inc_progress_update(state, POP3_GETRANGE_STAT_SEND);
159 pop3_gen_send(sock, "STAT");
161 return POP3_GETRANGE_STAT_RECV;
164 gint pop3_getrange_stat_recv(SockInfo *sock, gpointer data)
166 Pop3State *state = (Pop3State *)data;
167 gchar buf[POPBUFSIZE + 1];
168 gint ok;
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;
188 } else if (ok == PS_PROTOCOL)
189 return POP3_LOGOUT_SEND;
190 else
191 return -1;
194 gint pop3_getrange_last_send(SockInfo *sock, gpointer data)
196 Pop3State *state = (Pop3State *)data;
198 inc_progress_update(state, POP3_GETRANGE_LAST_SEND);
200 pop3_gen_send(sock, "LAST");
202 return POP3_GETRANGE_LAST_RECV;
205 gint pop3_getrange_last_recv(SockInfo *sock, gpointer data)
207 Pop3State *state = (Pop3State *)data;
208 gchar buf[POPBUFSIZE + 1];
210 if (pop3_ok(sock, buf) == PS_SUCCESS) {
211 gint last;
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;
225 } else
226 return POP3_GETSIZE_LIST_SEND;
229 gint pop3_getrange_uidl_send(SockInfo *sock, gpointer data)
231 Pop3State *state = (Pop3State *)data;
233 inc_progress_update(state, POP3_GETRANGE_UIDL_SEND);
235 pop3_gen_send(sock, "UIDL");
237 return POP3_GETRANGE_UIDL_RECV;
240 gint pop3_getrange_uidl_recv(SockInfo *sock, gpointer data)
242 Pop3State *state = (Pop3State *)data;
243 gboolean new = FALSE;
244 gchar buf[POPBUFSIZE];
245 gchar id[IDLEN + 1];
247 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_GETRANGE_LAST_SEND;
249 if (!state->id_table) new = TRUE;
251 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
252 gint num;
254 if (buf[0] == '.') break;
255 if (sscanf(buf, "%d %" Xstr(IDLEN) "s", &num, id) != 2)
256 continue;
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;
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));
273 if (new == TRUE)
274 return POP3_GETSIZE_LIST_SEND;
275 else
276 return POP3_LOGOUT_SEND;
279 gint pop3_getsize_list_send(SockInfo *sock, gpointer data)
281 Pop3State *state = (Pop3State *)data;
283 inc_progress_update(state, POP3_GETSIZE_LIST_SEND);
285 pop3_gen_send(sock, "LIST");
287 return POP3_GETSIZE_LIST_RECV;
290 gint pop3_getsize_list_recv(SockInfo *sock, gpointer data)
292 Pop3State *state = (Pop3State *)data;
293 gchar buf[POPBUFSIZE];
295 if (pop3_ok(sock, NULL) != PS_SUCCESS) return POP3_LOGOUT_SEND;
297 state->sizes = g_new0(gint, state->count + 1);
298 state->cur_total_bytes = 0;
300 while (sock_gets(sock, buf, sizeof(buf)) >= 0) {
301 guint num, size;
303 if (buf[0] == '.') break;
304 if (sscanf(buf, "%u %u", &num, &size) != 2)
305 return -1;
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;
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++;
320 return POP3_RETR_SEND;
323 gint pop3_retr_send(SockInfo *sock, gpointer data)
325 Pop3State *state = (Pop3State *)data;
327 inc_progress_update(state, POP3_RETR_SEND);
329 pop3_gen_send(sock, "RETR %d", state->cur_msg);
331 return POP3_RETR_RECV;
334 gint pop3_retr_recv(SockInfo *sock, gpointer data)
336 Pop3State *state = (Pop3State *)data;
337 const gchar *file;
338 gint ok, drop_ok;
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;
346 if ((drop_ok = inc_drop_message(file, state)) < 0) {
347 state->inc_state = INC_ERROR;
348 return -1;
351 state->cur_total_bytes += state->sizes[state->cur_msg];
352 state->cur_total_num++;
354 if (drop_ok == 0 && state->ac_prefs->rmmail)
355 return POP3_DELETE_SEND;
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);
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++;
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;
382 gint pop3_delete_send(SockInfo *sock, gpointer data)
384 Pop3State *state = (Pop3State *)data;
386 /*inc_progress_update(state, POP3_DELETE_SEND);*/
388 pop3_gen_send(sock, "DELE %d", state->cur_msg);
390 return POP3_DELETE_RECV;
393 gint pop3_delete_recv(SockInfo *sock, gpointer data)
395 Pop3State *state = (Pop3State *)data;
396 gint ok;
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++;
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;
416 gint pop3_logout_send(SockInfo *sock, gpointer data)
418 Pop3State *state = (Pop3State *)data;
420 inc_progress_update(state, POP3_LOGOUT_SEND);
422 pop3_gen_send(sock, "QUIT");
424 return POP3_LOGOUT_RECV;
427 gint pop3_logout_recv(SockInfo *sock, gpointer data)
429 if (pop3_ok(sock, NULL) == PS_SUCCESS)
430 return -1;
431 else
432 return -1;
435 static gint pop3_ok(SockInfo *sock, gchar *argbuf)
437 gint ok;
438 gchar buf[POPBUFSIZE + 1];
439 gchar *bufp;
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;
448 while (isalpha(*bufp))
449 bufp++;
451 if (*bufp)
452 *(bufp++) = '\0';
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;
465 if (*bufp)
466 fprintf(stderr, "POP3: %s\n", bufp);
467 } else
468 ok = PS_PROTOCOL;
470 if (argbuf)
471 strcpy(argbuf, bufp);
474 return ok;
477 static void pop3_gen_send(SockInfo *sock, const gchar *format, ...)
479 gchar buf[POPBUFSIZE + 1];
480 va_list args;
482 va_start(args, format);
483 g_vsnprintf(buf, sizeof(buf) - 2, format, args);
484 va_end(args);
486 if (!strncasecmp(buf, "PASS ", 5))
487 log_print("POP3> PASS ********\n");
488 else
489 log_print("POP3> %s\n", buf);
491 strcat(buf, "\r\n");
492 sock_write(sock, buf, strlen(buf));
495 static gint pop3_gen_recv(SockInfo *sock, gchar *buf, gint size)
497 if (sock_gets(sock, buf, size) < 0) {
498 return PS_SOCKET;
499 } else {
500 strretchomp(buf);
501 log_print("POP3< %s\n", buf);
503 return PS_SUCCESS;