Example #1
0
static struct mailmime * get_file_part(const char * filename, const char * mime_type,
                                       const char * text, size_t length)
{
	char * disposition_name;
	int encoding_type;
	struct mailmime_disposition * disposition;
	struct mailmime_mechanism * encoding;
	struct mailmime_content * content;
	struct mailmime * mime;
	struct mailmime_fields * mime_fields;
	
	disposition_name = NULL;
	if (filename != NULL) {
		disposition_name = strdup(filename);
	}
	disposition = mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
		disposition_name, NULL, NULL, NULL, (size_t) -1);
	content = mailmime_content_new_with_str(mime_type);
	
	encoding_type = MAILMIME_MECHANISM_BASE64;
	encoding = mailmime_mechanism_new(encoding_type, NULL);
	mime_fields = mailmime_fields_new_with_data(encoding,
		NULL, NULL, disposition, NULL);
	mime = part_new_empty(content, mime_fields, NULL, 1);
	mailmime_set_body_text(mime, (char *) text, length);
	
	return mime;
}
Example #2
0
struct mailmime_fields * mailmime_fields_new_filename(int dsp_type,
    char * filename, int encoding_type)
{
  struct mailmime_disposition * dsp;
  struct mailmime_mechanism * encoding;
  struct mailmime_fields * mime_fields;

  dsp = mailmime_disposition_new_with_data(dsp_type,
    filename, NULL, NULL, NULL, (size_t) -1);
  if (dsp == NULL)
    goto err;

  encoding = mailmime_mechanism_new(encoding_type, NULL);
  if (encoding == NULL)
    goto free_dsp;

  mime_fields = mailmime_fields_new_with_data(encoding,
			      NULL, NULL, dsp, NULL);
  if (mime_fields == NULL)
    goto free_encoding;

  return mime_fields;

 free_encoding:
  mailmime_encoding_free(encoding);
 free_dsp:
  mailmime_disposition_free(dsp);
 err:
  return NULL;
}
Example #3
0
static void prepare_mime_single(struct mailmime * mime)
{
  struct mailmime_single_fields single_fields;
  int encoding;
  int r;
  
  if (mime->mm_mime_fields != NULL) {
    mailmime_single_fields_init(&single_fields, mime->mm_mime_fields,
        mime->mm_content_type);
    if (single_fields.fld_encoding != NULL) {
      encoding = single_fields.fld_encoding->enc_type;
      switch (encoding) {
      case MAILMIME_MECHANISM_8BIT:
      case MAILMIME_MECHANISM_7BIT:
      case MAILMIME_MECHANISM_BINARY:
        single_fields.fld_encoding->enc_type =
          MAILMIME_MECHANISM_QUOTED_PRINTABLE;
        break;
      }
    }
    else {
      struct mailmime_mechanism * mechanism;
      struct mailmime_field * field;
      
      mechanism =
        mailmime_mechanism_new(MAILMIME_MECHANISM_QUOTED_PRINTABLE, NULL);
      if (mechanism == NULL)
        return;
      
      field = mailmime_field_new(MAILMIME_FIELD_TRANSFER_ENCODING,
          NULL, mechanism, NULL, NULL, 0, NULL, NULL, NULL);
      if (field == NULL) {
        mailmime_mechanism_free(mechanism);
        return;
      }
      
      r = clist_append(mime->mm_mime_fields->fld_list, field);
      if (r < 0) {
        mailmime_field_free(field);
        return;
      }
    }
  }
  
  if (mime->mm_type == MAILMIME_SINGLE) {
    switch (mime->mm_data.mm_single->dt_encoding) {
    case MAILMIME_MECHANISM_8BIT:
    case MAILMIME_MECHANISM_7BIT:
    case MAILMIME_MECHANISM_BINARY:
      mime->mm_data.mm_single->dt_encoding =
        MAILMIME_MECHANISM_QUOTED_PRINTABLE;
      mime->mm_data.mm_single->dt_encoded = 0;
      break;
    }
  }
}
Example #4
0
struct mailmime_fields * mailmime_fields_new_encoding(int type)
{
  struct mailmime_mechanism * encoding;
  struct mailmime_fields * mime_fields;

  encoding = mailmime_mechanism_new(type, NULL);
  if (encoding == NULL)
    goto err;

  mime_fields = mailmime_fields_new_with_data(encoding,
      NULL, NULL, NULL, NULL);
  if (mime_fields == NULL)
    goto free;

  return mime_fields;

 free:
  mailmime_mechanism_free(encoding);
 err:
  return NULL;
}
Example #5
0
static struct mailmime * get_text_part(const char * mime_type,
								const char * text, size_t length, int encoding_type)
{
	struct mailmime_fields * mime_fields;
	struct mailmime * mime;
	struct mailmime_content * content;
	struct mailmime_parameter * param;
	struct mailmime_disposition * disposition;
	struct mailmime_mechanism * encoding;
    
	encoding = mailmime_mechanism_new(encoding_type, NULL);
	disposition = mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,
		NULL, NULL, NULL, NULL, (size_t) -1);
	mime_fields = mailmime_fields_new_with_data(encoding,
		NULL, NULL, disposition, NULL);

	content = mailmime_content_new_with_str(mime_type);
	param = mailmime_param_new_with_data("charset", "utf-8");
	clist_append(content->ct_parameters, param);
	mime = part_new_empty(content, mime_fields, NULL, 1);
	mailmime_set_body_text(mime, (char *) text, length);
	
	return mime;
}
Example #6
0
static int mailmime_mechanism_parse(const char * message, size_t length,
				    size_t * indx,
				    struct mailmime_mechanism ** result)
{
  char * token;
  int type;
  struct mailmime_mechanism * mechanism;
  size_t cur_token;
  int r;
  int res;

  cur_token = * indx;
  
  type = MAILMIME_MECHANISM_ERROR; /* XXX - removes a gcc warning */

  token = NULL;
  r = mailimf_token_case_insensitive_parse(message, length,
					   &cur_token, "7bit");
  if (r == MAILIMF_NO_ERROR)
    type = MAILMIME_MECHANISM_7BIT;

  if (r == MAILIMF_ERROR_PARSE) {
    r = mailimf_token_case_insensitive_parse(message, length,
					     &cur_token, "8bit");
    if (r == MAILIMF_NO_ERROR)
      type = MAILMIME_MECHANISM_8BIT;
  }

  if (r == MAILIMF_ERROR_PARSE) {
    r = mailimf_token_case_insensitive_parse(message, length,
					     &cur_token, "binary");
    if (r == MAILIMF_NO_ERROR)
      type = MAILMIME_MECHANISM_BINARY;
  }

  if (r == MAILIMF_ERROR_PARSE) {
    r = mailimf_token_case_insensitive_parse(message, length,
					     &cur_token, "quoted-printable");
    if (r == MAILIMF_NO_ERROR)
      type = MAILMIME_MECHANISM_QUOTED_PRINTABLE;
  }

  if (r == MAILIMF_ERROR_PARSE) {
    r = mailimf_token_case_insensitive_parse(message, length,
					     &cur_token, "base64");
    if (r == MAILIMF_NO_ERROR)
      type = MAILMIME_MECHANISM_BASE64;
  }

  if (r == MAILIMF_ERROR_PARSE) {
    r = mailmime_token_parse(message, length, &cur_token, &token);
    if (r == MAILIMF_NO_ERROR)
      type = MAILMIME_MECHANISM_TOKEN;
  }

  if (r != MAILIMF_NO_ERROR) {
    res = r;
    goto err;
  }

  mechanism = mailmime_mechanism_new(type, token);
  if (mechanism == NULL) {
    res = MAILIMF_ERROR_MEMORY;
    goto free;
  }

  * result = mechanism;
  * indx = cur_token;

  return MAILIMF_NO_ERROR;

 free:
  if (token != NULL)
    mailmime_token_free(token);
 err:
  return res;
}
Example #7
0
struct mailmime *
mailprivacy_new_file_part(struct mailprivacy * privacy,
    char * filename,
    char * default_content_type, int default_encoding)
{
  char basename_buf[PATH_MAX];
  char * name;
  struct mailmime_mechanism * encoding;
  struct mailmime_content * content;
  struct mailmime * mime;
  int r;
  char * dup_filename;
  struct mailmime_fields * mime_fields;
  int encoding_type;
  char * content_type_str;
  int do_encoding;
  
  if (filename != NULL) {
    strncpy(basename_buf, filename, PATH_MAX);
    name = libetpan_basename(basename_buf);
  }
  else {
    name = NULL;
  }
  
  encoding = NULL;
  
  /* default content-type */
  if (default_content_type == NULL)
    content_type_str = "application/octet-stream";
  else
    content_type_str = default_content_type;
    
  content = mailmime_content_new_with_str(content_type_str);
  if (content == NULL) {
    goto free_content;
  }
  
  do_encoding = 1;
  if (content->ct_type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE) {
    struct mailmime_composite_type * composite;
    
    composite = content->ct_type->tp_data.tp_composite_type;
    
    switch (composite->ct_type) {
    case MAILMIME_COMPOSITE_TYPE_MESSAGE:
      if (strcasecmp(content->ct_subtype, "rfc822") == 0)
        do_encoding = 0;
      break;

    case MAILMIME_COMPOSITE_TYPE_MULTIPART:
      do_encoding = 0;
      break;
    }
  }
  
  if (do_encoding) {
    if (default_encoding == -1)
      encoding_type = MAILMIME_MECHANISM_BASE64;
    else
      encoding_type = default_encoding;
    
    /* default Content-Transfer-Encoding */
    encoding = mailmime_mechanism_new(encoding_type, NULL);
    if (encoding == NULL) {
      goto free_content;
    }
  }
  
  mime_fields = mailmime_fields_new_with_data(encoding,
      NULL, NULL, NULL, NULL);
  if (mime_fields == NULL) {
    goto free_content;
  }
  
  mime = mailmime_new_empty(content, mime_fields);
  if (mime == NULL) {
    goto free_mime_fields;
  }
  
  if ((filename != NULL) && (mime->mm_type == MAILMIME_SINGLE)) {
    /*
      duplicates the file so that the file can be deleted when
      the MIME part is done
    */
    dup_filename = dup_file(privacy, filename);
    if (dup_filename == NULL) {
      goto free_mime;
    }
  
    r = mailmime_set_body_file(mime, dup_filename);
    if (r != MAILIMF_NO_ERROR) {
      free(dup_filename);
      goto free_mime;
    }
  }
  
  return mime;
  
 free_mime:
  mailmime_free(mime);
  goto err;
 free_mime_fields:
  mailmime_fields_free(mime_fields);
  mailmime_content_free(content);
  goto err;
 free_content:
  if (encoding != NULL)
    mailmime_mechanism_free(encoding);
  if (content != NULL)
    mailmime_content_free(content);
 err:
  return NULL;
}