static int
nntpdriver_cached_get_envelopes_list(mailsession * session,
				     struct mailmessage_list * env_list)
{
  int r;
  unsigned int i;
  struct nntp_cached_session_state_data * cached_data;
  uint32_t first;
  uint32_t last;
  struct nntp_session_state_data * ancestor_data;
  char filename_env[PATH_MAX];
  char filename_flags[PATH_MAX];
  struct mail_cache_db * cache_db_env;
  struct mail_cache_db * cache_db_flags;
  MMAPString * mmapstr;
  int res;
  char cache_dir[PATH_MAX];

  cached_data = get_cached_data(session);
  ancestor_data = get_ancestor_data(session);

  nntp_flags_store_process(cached_data->nntp_flags_directory,
      ancestor_data->nntp_group_name,
      cached_data->nntp_flags_store);

  if (ancestor_data->nntp_group_name == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  /* read articles sequence */

  read_article_seq(session, &first, &last);

  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }

  snprintf(filename_env, PATH_MAX, "%s/%s/%s",
      cached_data->nntp_cache_directory,
      ancestor_data->nntp_group_name, ENV_NAME);

  r = mail_cache_db_open_lock(filename_env, &cache_db_env);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto free_mmapstr;
  }

  snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
      cached_data->nntp_flags_directory,
      ancestor_data->nntp_group_name, FLAGS_NAME);

  /* fill with cached */
  
  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {   
    mailmessage * msg;
    struct mailimf_fields * fields;

    msg = carray_get(env_list->msg_tab, i);

    if ((msg->msg_index < first) || (msg->msg_index > last))
      continue;

    if (msg->msg_fields == NULL) {
      r = get_cached_envelope(cache_db_env, mmapstr,
          session, msg->msg_index, &fields);
      if (r == MAIL_NO_ERROR) {
        msg->msg_fields = fields;
        msg->msg_cached = TRUE;
      }
    }
  }
  
  mail_cache_db_close_unlock(filename_env, cache_db_env);

  r = mailsession_get_envelopes_list(get_ancestor(session), env_list);

  if (r != MAIL_NO_ERROR) {
    res = r;
    goto free_mmapstr;
  }

  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto free_mmapstr;
  }
  
  /* add flags */
  
  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;

    msg = carray_get(env_list->msg_tab, i);
    
    if (msg->msg_flags == NULL) {
      struct mail_flags * flags;
      
      r = nntpdriver_get_cached_flags(cache_db_flags, mmapstr,
          msg->msg_index, &flags);
      if (r == MAIL_NO_ERROR) {
	msg->msg_flags = flags;
      }
      else {
        msg->msg_flags = mail_flags_new_empty();
        if (msg->msg_fields == NULL) {
          msg->msg_flags->fl_flags |= MAIL_FLAG_CANCELLED;
          mailmessage_check(msg);
        }
      }
    }
  }
  
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
  
  r = mail_cache_db_open_lock(filename_env, &cache_db_env);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto free_mmapstr;
  }
  
  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto close_db_env;
  }

  /* must write cache */

  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;

    msg = carray_get(env_list->msg_tab, i);

    if (msg->msg_fields != NULL) {
      if (!msg->msg_cached) {
	r = write_cached_envelope(cache_db_env, mmapstr,
            session, msg->msg_index, msg->msg_fields);
      }
    }

    if (msg->msg_flags != NULL) {
      r = nntpdriver_write_cached_flags(cache_db_flags, mmapstr,
          msg->msg_index, msg->msg_flags);
    }
  }

  first = 0;
  last = 0;
  if (carray_count(env_list->msg_tab) > 0) {
    mailmessage * msg;

    msg = carray_get(env_list->msg_tab, 0);
    first = msg->msg_index;

    msg = carray_get(env_list->msg_tab, carray_count(env_list->msg_tab) - 1);
    last = msg->msg_index;
  }

  /* write articles sequence */

  write_article_seq(session, first, last);

  /* flush cache */
  
  maildriver_cache_clean_up(cache_db_env, cache_db_flags, env_list);
  
  /* remove cache files */
  
  snprintf(cache_dir, PATH_MAX, "%s/%s",
      cached_data->nntp_cache_directory, ancestor_data->nntp_group_name);
  
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
  mail_cache_db_close_unlock(filename_env, cache_db_env);
  mmap_string_free(mmapstr);

  maildriver_message_cache_clean_up(cache_dir, env_list,
      get_uid_from_filename);

  return MAIL_NO_ERROR;

 close_db_env:
  mail_cache_db_close_unlock(filename_env, cache_db_env);
 free_mmapstr:
  mmap_string_free(mmapstr);
 err:
  return res;
}
static int nntp_get_flags(mailmessage * msg_info,
    struct mail_flags ** result)
{
  int r;
  struct mail_flags * flags;
  struct mail_cache_db * cache_db_flags;
  char filename_flags[PATH_MAX];
  int res;
  MMAPString * mmapstr;

  if (msg_info->msg_flags != NULL) {
    * result = msg_info->msg_flags;
    
    return MAIL_NO_ERROR;
  }

  flags = mail_flags_store_get(get_cached_session_data(msg_info)->nntp_flags_store, msg_info->msg_index);

  if (flags == NULL) {
    struct nntp_cached_session_state_data * cached_data;
    struct nntp_session_state_data * ancestor_data;
    
    cached_data = get_cached_session_data(msg_info);
    
    ancestor_data = get_ancestor_session_data(msg_info);
    if (ancestor_data->nntp_group_name == NULL) {
      res = MAIL_ERROR_BAD_STATE;
      goto err;
    }
    
    snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
        cached_data->nntp_flags_directory,
        ancestor_data->nntp_group_name, FLAGS_NAME);
    
    r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
    if (r < 0) {
      res = MAIL_ERROR_FILE;
      goto err;
    }
    
    mmapstr = mmap_string_new("");
    if (mmapstr == NULL) {
      res = MAIL_ERROR_MEMORY;
      goto close_db_flags;
    }
    
    r = nntpdriver_get_cached_flags(cache_db_flags, mmapstr,
        msg_info->msg_index, &flags);
    if (r != MAIL_NO_ERROR) {
      flags = mail_flags_new_empty();
      if (flags == NULL) {
	res = MAIL_ERROR_MEMORY;
	goto free_mmapstr;
      }
    }

    mmap_string_free(mmapstr);
    mail_cache_db_close_unlock(filename_flags, cache_db_flags);
  }

  msg_info->msg_flags = flags;

  * result = flags;

  return MAIL_NO_ERROR;

 free_mmapstr:
  mmap_string_free(mmapstr);
 close_db_flags:
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
 err:
  return res;
}
static int nntpdriver_cached_status_folder(mailsession * session,
    const char * mb, uint32_t * result_messages, uint32_t * result_recent,
    uint32_t * result_unseen)
{
  int res;
  struct nntp_cached_session_state_data * cached_data;
  struct nntp_session_state_data * ancestor_data;
  char filename_flags[PATH_MAX];
  struct mail_cache_db * cache_db_flags;
  MMAPString * mmapstr;
  uint32_t i;
  int r;
  uint32_t recent;
  uint32_t unseen;
  uint32_t first;
  uint32_t last;
  uint32_t count;
  uint32_t additionnal;

  r = nntpdriver_cached_select_folder(session, mb);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto err;
  }

  read_article_seq(session, &first, &last);

  count = 0;
  recent = 0;
  unseen = 0;
  
  ancestor_data = get_ancestor_data(session);
  cached_data = get_cached_data(session);
  if (ancestor_data->nntp_group_name == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  if (ancestor_data->nntp_group_info->grp_first > first)
    first = ancestor_data->nntp_group_info->grp_first;
  if (last < first)
    last = ancestor_data->nntp_group_info->grp_last;

  snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
      cached_data->nntp_flags_directory,
      ancestor_data->nntp_group_name, FLAGS_NAME);

  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }

  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close_db_flags;
  }

  for(i = first ; i <= last ; i++) {
    struct mail_flags * flags;
    
    r = nntpdriver_get_cached_flags(cache_db_flags, mmapstr,
        i, &flags);
    if (r == MAIL_NO_ERROR) {
      if ((flags->fl_flags & MAIL_FLAG_CANCELLED) != 0) {
        mail_flags_free(flags);
        continue;
      }
      
      count ++;
      if ((flags->fl_flags & MAIL_FLAG_NEW) != 0) {
	recent ++;
      }
      if ((flags->fl_flags & MAIL_FLAG_SEEN) == 0) {
	unseen ++;
      }
      mail_flags_free(flags);
    }
  }

  if ((count == 0) && (first != last)) {
    count = last - first + 1;
    recent = count;
    unseen = count;
  }
  
  additionnal = ancestor_data->nntp_group_info->grp_last - last;
  recent += additionnal;
  unseen += additionnal;
  
  mmap_string_free(mmapstr);
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);

  * result_messages = count;
  * result_recent = recent;
  * result_unseen = unseen;
  
  return MAIL_NO_ERROR;

 close_db_flags:
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
 err:
  return res;
}