static int body_body_to_mmapstr(char * data, size_t size,
    char ** result, size_t * result_len)
{
  size_t cur_token;
  int res;
  int r;
  
  cur_token = 0;
  
  /* skip header */
  
  /* MIME header */
  
  while (1) {
    r = mailimf_ignore_field_parse(data, size, &cur_token);
    if (r == MAILIMF_NO_ERROR) {
      /* do nothing */
    }
    else
      break;
  }
  
  r = mailimf_crlf_parse(data, size, &cur_token);
  if ((r != MAILIMF_NO_ERROR) && (r != MAILIMF_ERROR_PARSE)) {
    res = maildriver_imf_error_to_mail_error(r);
    goto err;
  }
  
  return body_to_mmapstr(data + cur_token, size - cur_token,
      result, result_len);
  
 err:
  return res;
}
Beispiel #2
0
int
mailmessage_generic_fetch_section_body(mailmessage * msg_info,
				       struct mailmime * mime,
				       char ** result,
				       size_t * result_len)
{
  MMAPString * mmapstr;
  int r;
  int res;
  size_t cur_token;

  cur_token = 0;

  if (mime->mm_type == MAILMIME_MESSAGE) {

    /* skip header */

    while (1) {
      r = mailimf_ignore_field_parse(mime->mm_body->dt_data.dt_text.dt_data,
          mime->mm_body->dt_data.dt_text.dt_length, &cur_token);
      if (r == MAILIMF_NO_ERROR) {
	/* do nothing */
      }
      else
	break;
    }
    
    r = mailimf_crlf_parse(mime->mm_body->dt_data.dt_text.dt_data,
        mime->mm_body->dt_data.dt_text.dt_length, &cur_token);
    if ((r != MAILIMF_NO_ERROR) && (r != MAILIMF_ERROR_PARSE)) {
      res = maildriver_imf_error_to_mail_error(r);
      goto err;
    }
  }

  mmapstr = mmap_string_new_len(mime->mm_body->dt_data.dt_text.dt_data +
      cur_token, mime->mm_body->dt_data.dt_text.dt_length - cur_token);
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }

  r = mmap_string_ref(mmapstr);
  if (r < 0) {
    res = MAIL_ERROR_MEMORY;
    goto free_mmap;
  }

  * result = mmapstr->str;
  * result_len = mmapstr->len;

  return MAIL_NO_ERROR;

 free_mmap:
  mmap_string_free(mmapstr);
 err:
  return res;
}
Beispiel #3
0
int mailmessage_generic_fetch_header(mailmessage * msg_info,
				     char ** result,
				     size_t * result_len)
{
  int r;
  char * message;
  size_t cur_token;
  size_t length;
  MMAPString * mmapstr;
  char * headers;
  int res;
  struct generic_message_t * msg;

  msg = msg_info->msg_data;
  r = mailmessage_generic_prefetch(msg_info);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto err;
  }

  message = msg->msg_message;
  length = msg->msg_length;
  cur_token = 0;
  
  while (1) {
    r = mailimf_ignore_field_parse(message, length, &cur_token);
    if (r == MAILIMF_NO_ERROR) {
      /* do nothing */
    }
    else
      break;
  }
  mailimf_crlf_parse(message, length, &cur_token);
  
  mmapstr = mmap_string_new_len(message, cur_token);
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }
  
  r = mmap_string_ref(mmapstr);
  if (r < 0) {
    res = MAIL_ERROR_MEMORY;
    goto free_mmap;
  }
  
  headers = mmapstr->str;

  * result = headers;
  * result_len = cur_token;

  return MAIL_NO_ERROR;

 free_mmap:
  mmap_string_free(mmapstr);
 err:
  return res;
}
static int body_to_mmapstr(char * data, size_t size,
    char ** result, size_t * result_len)
{
  MMAPString * mmapstr;
  size_t cur_token;
  int res;
  int r;
  
  cur_token = 0;
  
  /* skip header */
  
  while (1) {
    r = mailimf_ignore_field_parse(data, size, &cur_token);
    if (r == MAILIMF_NO_ERROR) {
      /* do nothing */
    }
    else
      break;
  }
  
  r = mailimf_crlf_parse(data, size, &cur_token);
  if ((r != MAILIMF_NO_ERROR) && (r != MAILIMF_ERROR_PARSE)) {
    res = maildriver_imf_error_to_mail_error(r);
    goto err;
  }
  
  mmapstr = mmap_string_new_len(data + cur_token, size - cur_token);
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    
    goto err;
  }
  
  r = mmap_string_ref(mmapstr);
  if (r != 0) {
    mmap_string_free(mmapstr);
    res = MAIL_ERROR_MEMORY;
    
    goto err;
  }
  
  * result = mmapstr->str;
  * result_len = mmapstr->len;

  return MAIL_NO_ERROR;

 err:
  return res;
}
Beispiel #5
0
static char * write_fixed_message(char * str,
				  const char * message, size_t size,
				  uint32_t uid, int force_no_uid)
{
  size_t fixed_size;
  size_t cur_token;
  size_t left;
  int end;
  int r;
  const char * cur_src;
  size_t numlen;

  cur_token = 0;

  fixed_size = 0;

  /* headers */

  end = FALSE;
  while (!end) {
    size_t begin;
    int ignore;

    ignore = FALSE;
    begin = cur_token;
    if (cur_token + strlen(UID_HEADER) <= size) {
      if (message[cur_token] == 'X') {
	if (strncasecmp(message + cur_token, UID_HEADER,
			strlen(UID_HEADER)) == 0) {
	  ignore = TRUE;
	}
      }
    }

    r = mailimf_ignore_field_parse(message, size, &cur_token);
    switch (r) {
    case MAILIMF_NO_ERROR:
      if (!ignore) {
	memcpy(str, message + begin, cur_token - begin);
	str += cur_token - begin;
      }
      break;
    case MAILIMF_ERROR_PARSE:
    default:
      end = TRUE;
      break;
    }
  }

  if (!force_no_uid) {
    /* UID header */
    
    memcpy(str, UID_HEADER " ", strlen(UID_HEADER " "));
    str += strlen(UID_HEADER " ");
#ifdef CRLF_BADNESS
    numlen = snprintf(str, 20, "%i\r\n", uid);
#else
    numlen = snprintf(str, 20, "%i\n", uid);
#endif
    str += numlen;
  }

  /* body */

  cur_src = message + cur_token;
  left = size - cur_token;
  while (left > 0) {
    size_t count;
    const char * next;

    count = 0;
    next = NULL;
    str = write_fixed_line(str, cur_src, left, &next, &count);
    
    cur_src = next;
    left -= count;
  }

  return str;
}
Beispiel #6
0
static size_t get_fixed_message_size(const char * message, size_t size,
				     uint32_t uid, int force_no_uid)
{
  size_t fixed_size;
  size_t cur_token;
  size_t left;
  const char * next;
  const char * cur;
  int end;
  int r;
  uint32_t tmp_uid;

  cur_token = 0;

  fixed_size = 0;

  /* headers */

  end = FALSE;
  while (!end) {
    size_t begin;
    int ignore;

    ignore = FALSE;
    begin = cur_token;
    if (cur_token + strlen(UID_HEADER) <= size) {
      if (message[cur_token] == 'X') {
	if (strncasecmp(message + cur_token, UID_HEADER,
			strlen(UID_HEADER)) == 0) {
	  ignore = TRUE;
	}
      }
    }

    r = mailimf_ignore_field_parse(message, size, &cur_token);
    switch (r) {
    case MAILIMF_NO_ERROR:
      if (!ignore)
	fixed_size += cur_token - begin;
      break;
    case MAILIMF_ERROR_PARSE:
    default:
      end = TRUE;
      break;
    }
  }

  if (!force_no_uid) {
    /* UID header */
    
#ifdef CRLF_BADNESS
    fixed_size += strlen(UID_HEADER " \r\n");
#else
    fixed_size += strlen(UID_HEADER " \n");
#endif
    
    tmp_uid = uid;
    while (tmp_uid >= 10) {
      tmp_uid /= 10;
      fixed_size ++;
    }
    fixed_size ++;
  }

  /* body */

  left = size - cur_token;
  next = message + cur_token;
  while (left > 0) {
    size_t count;
    size_t fixed_count;

    cur = next;

    if (!get_fixed_line_size(cur, left, &next, &count, &fixed_count))
      break;

    fixed_size += fixed_count;
    left -= count;
  }

  return fixed_size;
}
Beispiel #7
0
static int match_messages(char * message,
    size_t size,
    struct mailimf_fields * fields,
    int32_t flags,
    char * charset,
    struct mail_search_key * key)
{
  clistiter * cur;
  size_t length;
  size_t cur_token;
  int r;

  switch (key->type) {

    /* flags */
  case MAIL_SEARCH_KEY_ANSWERED:
    return ((flags & MAIL_FLAG_ANSWERED) != 0);

  case MAIL_SEARCH_KEY_FLAGGED:
    return ((flags & MAIL_FLAG_FLAGGED) != 0);

  case MAIL_SEARCH_KEY_DELETED:
    return ((flags & MAIL_FLAG_DELETED) != 0);

  case MAIL_SEARCH_KEY_RECENT:
    return ((flags & MAIL_FLAG_NEW) != 0) &&
      ((flags & MAIL_FLAG_SEEN) == 0);

  case MAIL_SEARCH_KEY_SEEN:
    return ((flags & MAIL_FLAG_SEEN) != 0);

  case MAIL_SEARCH_KEY_NEW:
    return ((flags & MAIL_FLAG_NEW) != 0);

  case MAIL_SEARCH_KEY_OLD:
    return ((flags & MAIL_FLAG_NEW) == 0);

  case MAIL_SEARCH_KEY_UNANSWERED:
    return ((flags & MAIL_FLAG_ANSWERED) == 0);

  case MAIL_SEARCH_KEY_UNDELETED:
    return ((flags & MAIL_FLAG_DELETED) == 0);

  case MAIL_SEARCH_KEY_UNFLAGGED:
    return ((flags & MAIL_FLAG_FLAGGED) == 0);

  case MAIL_SEARCH_KEY_UNSEEN:
    return ((flags & MAIL_FLAG_SEEN) == 0);

    /* headers */
  case MAIL_SEARCH_KEY_BCC:
    return match_header(fields, "Bcc", key->bcc);

  case MAIL_SEARCH_KEY_CC:
    return match_header(fields, "Cc", key->cc);

  case MAIL_SEARCH_KEY_FROM:
    return match_header(fields, "From", key->from);

  case MAIL_SEARCH_KEY_SUBJECT:
    return match_header(fields, "Subject", key->subject);

  case MAIL_SEARCH_KEY_TO:
    return match_header(fields, "To", key->to);

  case MAIL_SEARCH_KEY_HEADER:
    return match_header(fields, key->header_name, key->header_value);

    /* date */
  case MAIL_SEARCH_KEY_BEFORE:
    return (comp_date(fields, key->before) <= 0);

  case MAIL_SEARCH_KEY_ON:
    return (comp_date(fields, key->before) == 0);

  case MAIL_SEARCH_KEY_SINCE:
    return (comp_date(fields, key->before) >= 0);
    
    /* boolean */
  case MAIL_SEARCH_KEY_NOT:
    return (!match_messages(message, size, fields, flags, charset, key->not));
  case MAIL_SEARCH_KEY_OR:
    return (match_messages(message, size, fields, flags, charset, key->or1) ||
	    match_messages(message, size, fields, flags, charset, key->or2));

  case MAIL_SEARCH_KEY_MULTIPLE:
    for(cur = clist_begin(key->multiple) ; cur != NULL ;
        cur = clist_next(cur)) {
      if (!match_messages(message, size, fields, flags, charset,
              clist_content(cur)))
	return FALSE;
    }

    return TRUE;

    /* size */
  case MAIL_SEARCH_KEY_SMALLER:
    return (size <= key->smaller);

  case MAIL_SEARCH_KEY_LARGER:
    return (size >= key->larger);

  case MAIL_SEARCH_KEY_BODY:
    length = strlen(message);

    cur_token = 0;
    while (1) {
      r = mailimf_ignore_field_parse(message, length, &cur_token);
      if (r == MAILIMF_NO_ERROR) {
	/* do nothing */
      }
      else
	break;
    }

    return (strstr(message + cur_token, key->body) != NULL);

  case MAIL_SEARCH_KEY_TEXT:
    return (strstr(message, key->body) != NULL);
    
  case MAIL_SEARCH_KEY_ALL:
  default:
    return TRUE;
  }
}