struct mailmime_fields *
mailprivacy_mime_fields_dup(struct mailprivacy * privacy,
    struct mailmime_fields * mime_fields)
{
  FILE * f;
  char tmp_file[PATH_MAX];
  int col;
  int r;
  struct mailmime_fields * dup_mime_fields;
  int fd;
  char * mapping;
  struct stat stat_info;
  struct mailimf_fields * fields;
  size_t cur_token;
  
  f = mailprivacy_get_tmp_file(privacy, tmp_file, sizeof(tmp_file));
  if (f == NULL)
    goto err;
  
  col = 0;
  r = mailmime_fields_write(f, &col, mime_fields);
  if (r != MAILIMF_NO_ERROR)
    goto unlink;
  
  fflush(f);
  
  fd = fileno(f);
  if (fd == -1)
    goto unlink;
  
  r = fstat(fd, &stat_info);
  if (r < 0)
    goto unlink;
  
  mapping = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (mapping == (char *)MAP_FAILED)
    goto unlink;
  
  cur_token = 0;
  r = mailimf_optional_fields_parse(mapping, stat_info.st_size,
      &cur_token, &fields);
  if (r != MAILIMF_NO_ERROR)
    goto unmap;
  
  r = mailmime_fields_parse(fields, &dup_mime_fields);
  mailimf_fields_free(fields);
  if (r != MAILIMF_NO_ERROR)
    goto unmap;
  
  munmap(mapping, stat_info.st_size);
  fclose(f);
  unlink(tmp_file);

  return dup_mime_fields;

 unmap:
  munmap(mapping, stat_info.st_size);
 unlink:
  fclose(f);
  unlink(tmp_file);
 err:
  return NULL;
}
Beispiel #2
0
int maildriver_generic_search_messages(mailsession * session, char * charset,
    struct mail_search_key * key,
    struct mail_search_result ** result)
{
  int header;
  clist * list;
  struct mail_search_result * search_result;
  int r;
  struct mailmessage_list * env_list;
  int res;
  unsigned int i;

  header = is_search_header_only(key);

  r = mailsession_get_messages_list(session, &env_list);
  if (r != MAIL_NO_ERROR)
    return r;

  list = NULL;
  for(i = 0 ; i < carray_count(env_list->tab) ; i ++) {
    char * message;
    size_t length;
    struct mail_info * info;
    uint32_t flags;
    struct mailimf_fields * fields;
    size_t cur_token;

    info = carray_get(env_list->tab, i);

    if (!header) {
      r = mailsession_fetch_message(session, info->indx, &message, &length);
      if (r != MAIL_NO_ERROR) {
	res = r;
	goto free_list;
      }

      cur_token = 0;
      r = mailimf_optional_fields_parse(message, length,
					&cur_token, &fields);
      if (r != MAILIMF_NO_ERROR) {
	res = MAIL_ERROR_PARSE;
	goto free_list;
      }
    }
    else {
      char * msg_header;
      int r;
      size_t cur_token;
      size_t header_len;

      r = mailsession_fetch_message_header(session, info->indx, &msg_header,
					   &header_len);
      if (r != MAIL_NO_ERROR) {
	res = r;
	goto free_list;
      }

      message = NULL;
      cur_token = 0;
      r = mailimf_optional_fields_parse(msg_header, header_len,
					&cur_token, &fields);
      if (r != MAILIMF_NO_ERROR) {
	res = MAIL_ERROR_PARSE;
	goto free_list;
      }

      mailsession_fetch_result_free(session, msg_header);
    }

    r = mailsession_get_message_flags(session, info->indx, &flags);
    if (r != MAIL_NO_ERROR) {
      res = r;
      goto free_list;
    }

    if (match_messages(message, info->size, fields, flags,
		       charset, key)) {
      uint32_t * pnum;

      pnum = malloc(sizeof(* pnum));
      if (pnum == NULL) {
	if (message != NULL)
	  mailsession_fetch_result_free(session, message);
	res = MAIL_ERROR_MEMORY;
	goto free_list;
      }

      * pnum = info->indx;

      r = clist_append(list, pnum);
      if (r < 0) {
	free(pnum);
	if (message != NULL)
	  mailsession_fetch_result_free(session, message);
	res = MAIL_ERROR_MEMORY;
	goto free_list;
      }
    }

    if (message != NULL)
      mailsession_fetch_result_free(session, message);
  }

  search_result =  mail_search_result_new(list);
  if (search_result == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_list;
  }

  * result = search_result;

  return MAIL_NO_ERROR;

 free_list:
  clist_foreach(list, (clist_func) free, NULL);
  clist_free(list);
  mailmessage_list_free(env_list);
  return res;
}