Beispiel #1
0
/* update POP mailbox - delete messages from server */
int pop_sync_mailbox(CONTEXT *ctx, int *index_hint)
{
    int i, j, ret = 0;
    char buf[LONG_STRING];
    POP_DATA *pop_data = (POP_DATA *)ctx->data;
    progress_t progress;

    pop_data->check_time = 0;

    FOREVER
    {
        if (pop_reconnect(ctx) < 0)
            return -1;

        mutt_progress_init(&progress, _("Marking messages deleted..."),
                           M_PROGRESS_MSG, WriteInc, ctx->deleted);

        for (i = 0, j = 0, ret = 0; ret == 0
             && i < ctx->msgcount; i++) {
            if (ctx->hdrs[i]->deleted
                && (ctx->hdrs[i]->refno != -1)) {
                j++;

                if (!ctx->quiet)
                    mutt_progress_update(&progress, j, -1);
                snprintf(buf, sizeof(buf), "DELE %d\r\n", ctx->hdrs[i]->refno);

                if ((ret = pop_query(pop_data, buf, sizeof(buf))) == 0) {
                    mutt_bcache_del(pop_data->bcache, ctx->hdrs[i]->data);
                }
            }

        }

        if (ret == 0) {
            strfcpy(buf, "QUIT\r\n", sizeof(buf));
            ret = pop_query(pop_data, buf, sizeof(buf));
        }

        if (ret == 0) {
            pop_data->clear_cache = 1;
            pop_clear_cache(pop_data);
            pop_data->status = POP_DISCONNECTED;
            return 0;
        }

        if (ret == -2) {
            mutt_error("%s", pop_data->err_msg);
            mutt_sleep(2);
            return -1;
        }
    }
}
Beispiel #2
0
/* 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;

    safe_free(&ctx->path);
    ctx->path = safe_strdup(buf);

    pop_data = safe_calloc(1, sizeof(POP_DATA));
    pop_data->conn = conn;
    ctx->data = pop_data;
    ctx->mx_close = pop_close_mailbox;

    if (pop_open_connection(pop_data) < 0)
        return -1;

    conn->data = pop_data;
    pop_data->bcache = mutt_bcache_open(&acct, NULL);

    /* init (hard-coded) ACL rights */
    memset(ctx->rights, 0, sizeof(ctx->rights));
    mutt_bit_set(ctx->rights, M_ACL_SEEN);
    mutt_bit_set(ctx->rights, M_ACL_DELETE);
    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;
        }
    }
}
Beispiel #3
0
/* fetch message from POP server */
int pop_fetch_message(MESSAGE *msg, CONTEXT *ctx, int msgno)
{
    int ret;
    void *uidl;
    char buf[LONG_STRING];
    char path[_POSIX_PATH_MAX];
    progress_t progressbar;
    POP_DATA *pop_data = (POP_DATA *)ctx->data;
    POP_CACHE *cache;
    HEADER *h = ctx->hdrs[msgno];
    unsigned short bcache = 1;

    /* see if we already have the message in body cache */
    if ((msg->fp = mutt_bcache_get(pop_data->bcache, h->data)))
        return 0;

    /*
     * see if we already have the message in our cache in
     * case $message_cachedir is unset
     */
    cache = &pop_data->cache[h->index % POP_CACHE_LEN];

    if (cache->path) {
        if (cache->index == h->index) {
            /* yes, so just return a pointer to the message */
            msg->fp = fopen(cache->path, "r");

            if (msg->fp)
                return 0;

            mutt_perror(cache->path);
            mutt_sleep(2);
            return -1;
        } else {
            /* clear the previous entry */
            unlink(cache->path);
            safe_free(&cache->path);
        }
    }

    FOREVER
    {
        if (pop_reconnect(ctx) < 0)
            return -1;

        /* verify that massage index is correct */
        if (h->refno < 0) {
            mutt_error _(
                "The message index is incorrect. Try reopening the mailbox.");
            mutt_sleep(2);
            return -1;
        }

        mutt_progress_init(&progressbar, _(
                               "Fetching message..."),
                           M_PROGRESS_SIZE, NetInc,
                           h->content->length + h->content->offset - 1);

        /* see if we can put in body cache; use our cache as fallback */
        if (!(msg->fp = mutt_bcache_put(pop_data->bcache, h->data, 1))) {
            /* no */
            bcache = 0;
            mutt_mktemp(path, sizeof(path));

            if (!(msg->fp = safe_fopen(path, "w+"))) {
                mutt_perror(path);
                mutt_sleep(2);
                return -1;
            }
        }

        snprintf(buf, sizeof(buf), "RETR %d\r\n", h->refno);

        ret =
            pop_fetch_data(pop_data, buf, &progressbar, fetch_message, msg->fp);

        if (ret == 0)
            break;

        safe_fclose(&msg->fp);

        /* if RETR failed (e.g. connection closed), be sure to remove either
         * the file in bcache or from POP's own cache since the next iteration
         * of the loop will re-attempt to put() the message */
        if (!bcache)
            unlink(path);

        if (ret == -2) {
            mutt_error("%s", pop_data->err_msg);
            mutt_sleep(2);
            return -1;
        }

        if (ret == -3) {
            mutt_error _("Can't write message to temporary file!");
            mutt_sleep(2);
            return -1;
        }
    }

    /* Update the header information.  Previously, we only downloaded a
     * portion of the headers, those required for the main display.
     */
    if (bcache)
        mutt_bcache_commit(pop_data->bcache, h->data);
    else {
        cache->index = h->index;
        cache->path = safe_strdup(path);
    }
    rewind(msg->fp);
    uidl = h->data;

    /* we replace envelop, key in subj_hash has to be updated as well */
    if (ctx->subj_hash
        && h->env->real_subj)
        hash_delete(ctx->subj_hash, h->env->real_subj, h, NULL);
    mutt_free_envelope(&h->env);
    h->env = mutt_read_rfc822_header(msg->fp, h, 0, 0);

    if (ctx->subj_hash
        && h->env->real_subj)
        hash_insert(ctx->subj_hash, h->env->real_subj, h, 1);

    h->data = uidl;
    h->lines = 0;
    fgets(buf, sizeof(buf), msg->fp);

    while (!feof(msg->fp)) {
        ctx->hdrs[msgno]->lines++;
        fgets(buf, sizeof(buf), msg->fp);
    }

    h->content->length = ftello(msg->fp) - h->content->offset;

    /* This needs to be done in case this is a multipart message */
    if (!WithCrypto)
        h->security = crypt_query(h->content);

    mutt_clear_error();
    rewind(msg->fp);

    return 0;
}
Beispiel #4
0
/* update POP mailbox - delete messages from server */
int pop_sync_mailbox (CONTEXT *ctx, int *index_hint)
{
  int i, j, ret = 0;
  char buf[LONG_STRING];
  POP_DATA *pop_data = (POP_DATA *)ctx->data;
  progress_t progress;
#ifdef USE_HCACHE
  header_cache_t *hc = NULL;
#endif

  pop_data->check_time = 0;

  FOREVER
  {
    if (pop_reconnect (ctx) < 0)
      return -1;

    mutt_progress_init (&progress, _("Marking messages deleted..."),
			M_PROGRESS_MSG, WriteInc, ctx->deleted);

#if USE_HCACHE
    hc = pop_hcache_open (pop_data, ctx->path);
#endif

    for (i = 0, j = 0, ret = 0; ret == 0 && i < ctx->msgcount; i++)
    {
      if (ctx->hdrs[i]->deleted && ctx->hdrs[i]->refno != -1)
      {
	j++;
	if (!ctx->quiet)
	  mutt_progress_update (&progress, j, -1);
	snprintf (buf, sizeof (buf), "DELE %d\r\n", ctx->hdrs[i]->refno);
	if ((ret = pop_query (pop_data, buf, sizeof (buf))) == 0)
	{
	  mutt_bcache_del (pop_data->bcache, ctx->hdrs[i]->data);
#if USE_HCACHE
	  mutt_hcache_delete (hc, ctx->hdrs[i]->data, strlen);
#endif
	}
      }

#if USE_HCACHE
      if (ctx->hdrs[i]->changed)
      {
	mutt_hcache_store (hc, ctx->hdrs[i]->data, ctx->hdrs[i], 0, strlen, M_GENERATE_UIDVALIDITY);
      }
#endif

    }

#if USE_HCACHE
    mutt_hcache_close (hc);
#endif

    if (ret == 0)
    {
      strfcpy (buf, "QUIT\r\n", sizeof (buf));
      ret = pop_query (pop_data, buf, sizeof (buf));
    }

    if (ret == 0)
    {
      pop_data->clear_cache = 1;
      pop_clear_cache (pop_data);
      pop_data->status = POP_DISCONNECTED;
      return 0;
    }

    if (ret == -2)
    {
      mutt_error ("%s", pop_data->err_msg);
      mutt_sleep (2);
      return -1;
    }
  }
}