struct mailimap_section *
mailimap_section_new_part(struct mailimap_section_part * part)
{
  struct mailimap_section_spec * spec;
  struct mailimap_section * section;

  spec = mailimap_section_spec_new(MAILIMAP_SECTION_SPEC_SECTION_PART,
				   NULL, part, NULL);
  if (spec == NULL)
    return NULL;

  section = mailimap_section_new(spec);
  if (section == NULL) {
    /* detach section_part so that it will not be freed */
    spec->sec_data.sec_part = NULL;
    mailimap_section_spec_free(spec);
    return NULL;
  }

  return section;
}
static struct mailimap_section *
mailimap_section_new_msgtext(struct mailimap_section_msgtext * msgtext)
{
  struct mailimap_section_spec * spec;
  struct mailimap_section * section;

  spec = mailimap_section_spec_new(MAILIMAP_SECTION_SPEC_SECTION_MSGTEXT,
				   msgtext, NULL, NULL);
  if (spec == NULL)
    return NULL;

  section = mailimap_section_new(spec);
  if (section == NULL) {
    /* detach section_msgtext so that it will not be freed */
    spec->sec_data.sec_msgtext = NULL;
    mailimap_section_spec_free(spec);
    return NULL;
  }
  
  return section;
}
static int imap_fetch(mailmessage * msg_info,
		      char ** result,
		      size_t * result_len)
{
  int r;
  struct mailimap_set * set;
  struct mailimap_fetch_att * fetch_att;
  struct mailimap_fetch_type * fetch_type;
  clist * fetch_result;
  struct mailimap_msg_att * msg_att;
  struct mailimap_msg_att_item * msg_att_item;
  char * text;
  size_t text_length;
  int res;
  clistiter * cur;
  struct mailimap_section * section;

  set = mailimap_set_new_single(msg_info->msg_index);
  if (set == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }

#if 0
  fetch_att = mailimap_fetch_att_new_rfc822();
  if (fetch_att == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_set;
  }

  fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
  if (fetch_type == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_fetch_att;
  }

  r = mailimap_uid_fetch(get_imap_session(msg_info->session), set,
			 fetch_type, &fetch_result);

  mailimap_fetch_type_free(fetch_type);
#endif

  section = mailimap_section_new(NULL);
  if (section == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_set;
  }
  
  fetch_att = mailimap_fetch_att_new_body_peek_section(section);
  if (fetch_att == NULL) {
    mailimap_section_free(section);
    res = MAIL_ERROR_MEMORY;
    goto free_set;
  }
  
  fetch_type = mailimap_fetch_type_new_fetch_att(fetch_att);
  if (fetch_type == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_fetch_att;
  }

  r = mailimap_uid_fetch(get_imap_session(msg_info), set,
			 fetch_type, &fetch_result);
  
  mailimap_fetch_type_free(fetch_type);
  mailimap_set_free(set);
  
  switch (r) {
  case MAILIMAP_NO_ERROR:
    break;
  default:
    return imap_error_to_mail_error(r);
  }
  
  if (clist_begin(fetch_result) == NULL) {
    mailimap_fetch_list_free(fetch_result);
    return MAIL_ERROR_FETCH;
  }

  msg_att = clist_begin(fetch_result)->data;

  text = NULL;
  text_length = 0;

  for(cur = clist_begin(msg_att->att_list) ; cur != NULL ;
      cur = clist_next(cur)) {
    msg_att_item = clist_content(cur);

    if (msg_att_item->att_type == MAILIMAP_MSG_ATT_ITEM_STATIC) {
#if 0
      if (msg_att_item->msg_att_static->type == MAILIMAP_MSG_ATT_RFC822) {
	text = msg_att_item->msg_att_static->rfc822;
	msg_att_item->msg_att_static->rfc822 = NULL;
	text_length = msg_att_item->msg_att_static->length;
      }
#endif
      if (msg_att_item->att_data.att_static->att_type ==
	  MAILIMAP_MSG_ATT_BODY_SECTION) {
	text = msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part;
        /* detach */
	msg_att_item->att_data.att_static->att_data.att_body_section->sec_body_part = NULL;
	text_length =
	  msg_att_item->att_data.att_static->att_data.att_body_section->sec_length;
      }
    }
  }

  mailimap_fetch_list_free(fetch_result);

  if (text == NULL)
    return MAIL_ERROR_FETCH;

  * result = text;
  * result_len = text_length;
  
  return MAIL_NO_ERROR;

 free_fetch_att:
  mailimap_fetch_att_free(fetch_att);
 free_set:
  mailimap_set_free(set);
 err:
  return res;
}