📄 pop.c
字号:
/* * Copyright (C) 2000-2002 Vsevolod Volkov <vvv@mutt.org.ua> * Copyright (C) 2006 Rocco Rutte <pdmef@gmx.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #if HAVE_CONFIG_H# include "config.h"#endif#include "mutt.h"#include "mutt_curses.h"#include "mx.h"#include "pop.h"#include "mutt_crypt.h"#include "bcache.h"#if USE_HCACHE#include "hcache.h"#endif#include <string.h>#include <unistd.h>/* write line to file */static int fetch_message (char *line, void *file){ FILE *f = (FILE *) file; fputs (line, f); if (fputc ('\n', f) == EOF) return -1; return 0;}/* * Read header * returns: * 0 on success * -1 - conection lost, * -2 - invalid command or execution error, * -3 - error writing to tempfile */static int pop_read_header (POP_DATA *pop_data, HEADER *h){ FILE *f; int ret, index; long length; char buf[LONG_STRING]; char tempfile[_POSIX_PATH_MAX]; mutt_mktemp (tempfile); if (!(f = safe_fopen (tempfile, "w+"))) { mutt_perror (tempfile); return -3; } snprintf (buf, sizeof (buf), "LIST %d\r\n", h->refno); ret = pop_query (pop_data, buf, sizeof (buf)); if (ret == 0) { sscanf (buf, "+OK %d %ld", &index, &length); snprintf (buf, sizeof (buf), "TOP %d 0\r\n", h->refno); ret = pop_fetch_data (pop_data, buf, NULL, fetch_message, f); if (pop_data->cmd_top == 2) { if (ret == 0) { pop_data->cmd_top = 1; dprint (1, (debugfile, "pop_read_header: set TOP capability\n")); } if (ret == -2) { pop_data->cmd_top = 0; dprint (1, (debugfile, "pop_read_header: unset TOP capability\n")); snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), _("Command TOP is not supported by server.")); } } } switch (ret) { case 0: { rewind (f); h->env = mutt_read_rfc822_header (f, h, 0, 0); h->content->length = length - h->content->offset + 1; rewind (f); while (!feof (f)) { h->content->length--; fgets (buf, sizeof (buf), f); } break; } case -2: { mutt_error ("%s", pop_data->err_msg); break; } case -3: { mutt_error _("Can't write header to temporary file!"); break; } } fclose (f); unlink (tempfile); return ret;}/* parse UIDL */static int fetch_uidl (char *line, void *data){ int i, index; CONTEXT *ctx = (CONTEXT *)data; POP_DATA *pop_data = (POP_DATA *)ctx->data; sscanf (line, "%d %s", &index, line); for (i = 0; i < ctx->msgcount; i++) if (!mutt_strcmp (line, ctx->hdrs[i]->data)) break; if (i == ctx->msgcount) { dprint (1, (debugfile, "pop_fetch_headers: new header %d %s\n", index, line)); if (i >= ctx->hdrmax) mx_alloc_memory(ctx); ctx->msgcount++; ctx->hdrs[i] = mutt_new_header (); ctx->hdrs[i]->data = safe_strdup (line); } else if (ctx->hdrs[i]->index != index - 1) pop_data->clear_cache = 1; ctx->hdrs[i]->refno = index; ctx->hdrs[i]->index = index - 1; return 0;}static int msg_cache_check (const char *id, body_cache_t *bcache, void *data){ CONTEXT *ctx; POP_DATA *pop_data; int i; if (!(ctx = (CONTEXT *)data)) return -1; if (!(pop_data = (POP_DATA *)ctx->data)) return -1; for (i = 0; i < ctx->msgcount; i++) /* if the id we get is known for a header: done (i.e. keep in cache) */ if (ctx->hdrs[i]->data && mutt_strcmp (ctx->hdrs[i]->data, id) == 0) return 0; /* message not found in context -> remove it from cache * return the result of bcache, so we stop upon its first error */ return mutt_bcache_del (bcache, id);}/* * Read headers * returns: * 0 on success * -1 - conection lost, * -2 - invalid command or execution error, * -3 - error writing to tempfile */static int pop_fetch_headers (CONTEXT *ctx){ int i, ret, old_count, new_count; unsigned short hcached = 0, bcached; POP_DATA *pop_data = (POP_DATA *)ctx->data;#ifdef USE_HCACHE header_cache_t *hc = NULL; void *data; hc = mutt_hcache_open (HeaderCache, ctx->path);#endif time (&pop_data->check_time); pop_data->clear_cache = 0; for (i = 0; i < ctx->msgcount; i++) ctx->hdrs[i]->refno = -1; old_count = ctx->msgcount; ret = pop_fetch_data (pop_data, "UIDL\r\n", NULL, fetch_uidl, ctx); new_count = ctx->msgcount; ctx->msgcount = old_count; if (pop_data->cmd_uidl == 2) { if (ret == 0) { pop_data->cmd_uidl = 1; dprint (1, (debugfile, "pop_fetch_headers: set UIDL capability\n")); } if (ret == -2 && pop_data->cmd_uidl == 2) { pop_data->cmd_uidl = 0; dprint (1, (debugfile, "pop_fetch_headers: unset UIDL capability\n")); snprintf (pop_data->err_msg, sizeof (pop_data->err_msg), _("Command UIDL is not supported by server.")); } } if (ret == 0) { for (i = 0; i < old_count; i++) if (ctx->hdrs[i]->refno == -1) ctx->hdrs[i]->deleted = 1; for (i = old_count; i < new_count; i++) { mutt_message (_("Fetching message headers... [%d/%d]"), i + 1 - old_count, new_count - old_count);#if USE_HCACHE if ((data = mutt_hcache_fetch (hc, ctx->hdrs[i]->data, strlen))) { char *uidl = safe_strdup (ctx->hdrs[i]->data); int refno = ctx->hdrs[i]->refno; int index = ctx->hdrs[i]->index; /* * - POP dynamically numbers headers and relies on h->refno * to map messages; so restore header and overwrite restored * refno with current refno, same for index * - h->data needs to a separate pointer as it's driver-specific * data freed separately elsewhere * (the old h->data should point inside a malloc'd block from * hcache so there shouldn't be a memleak here) */ HEADER *h = mutt_hcache_restore ((unsigned char *) data, NULL); mutt_free_header (&ctx->hdrs[i]); ctx->hdrs[i] = h; ctx->hdrs[i]->refno = refno; ctx->hdrs[i]->index = index; ctx->hdrs[i]->data = uidl; ret = 0; hcached = 1; } else#endif if ((ret = pop_read_header (pop_data, ctx->hdrs[i])) < 0) break;#if USE_HCACHE else { mutt_hcache_store (hc, ctx->hdrs[i]->data, ctx->hdrs[i], 0, strlen); } FREE(&data);#endif /* * faked support for flags works like this: * - if 'hcached' is 1, we have the message in our hcache: * - if we also have a body: read * - if we don't have a body: old * (if $mark_old is set which is maybe wrong as * $mark_old should be considered for syncing the * folder and not when opening it XXX) * - if 'hcached' is 0, we don't have the message in our hcache: * - if we also have a body: read * - if we don't have a body: new */ bcached = mutt_bcache_exists (pop_data->bcache, ctx->hdrs[i]->data) == 0; ctx->hdrs[i]->old = 0; ctx->hdrs[i]->read = 0; if (hcached) { if (bcached) ctx->hdrs[i]->read = 1; else if (option (OPTMARKOLD)) ctx->hdrs[i]->old = 1; } else { if (bcached) ctx->hdrs[i]->read = 1; } ctx->msgcount++; } if (i > old_count) mx_update_context (ctx, i - old_count); }#if USE_HCACHE mutt_hcache_close (hc);#endif if (ret < 0) { for (i = ctx->msgcount; i < new_count; i++) mutt_free_header (&ctx->hdrs[i]); return ret; } /* after putting the result into our structures, * clean up cache, i.e. wipe messages deleted outside * the availability of our cache */ mutt_bcache_list (pop_data->bcache, msg_cache_check, (void*)ctx); mutt_clear_error (); return (new_count - old_count);}/* open POP mailbox - fetch only headers */int pop_open_mailbox (CONTEXT *ctx){ int ret; char buf[LONG_STRING]; CONNECTION *conn; ACCOUNT acct; POP_DATA *pop_data; ciss_url_t url; if (pop_parse_path (ctx->path, &acct)) { mutt_error (_("%s is an invalid POP path"), ctx->path); mutt_sleep (2); return -1; } mutt_account_tourl (&acct, &url); url.path = NULL; url_ciss_tostring (&url, buf, sizeof (buf), 0); conn = mutt_conn_find (NULL, &acct); if (!conn) return -1; FREE (&ctx->path); ctx->path = safe_strdup (buf); pop_data = safe_calloc (1, sizeof (POP_DATA)); pop_data->conn = conn; ctx->data = pop_data; if (pop_open_connection (pop_data) < 0) return -1; conn->data = pop_data; pop_data->bcache = mutt_bcache_open (&acct, NULL); FOREVER { if (pop_reconnect (ctx) < 0) return -1; ctx->size = pop_data->size; mutt_message _("Fetching list of messages..."); ret = pop_fetch_headers (ctx); if (ret >= 0) return 0; if (ret < -1) { mutt_sleep (2); return -1; } }}/* delete all cached messages */static void pop_clear_cache (POP_DATA *pop_data){ int i; if (!pop_data->clear_cache) return; dprint (1, (debugfile, "pop_clear_cache: delete cached messages\n")); for (i = 0; i < POP_CACHE_LEN; i++) { if (pop_data->cache[i].path) { unlink (pop_data->cache[i].path); FREE (&pop_data->cache[i].path); } }}/* close POP mailbox */void pop_close_mailbox (CONTEXT *ctx)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -