Exemplo n.º 1
0
LIBETPAN_EXPORT
void mailimf_message_free(struct mailimf_message * message)
{
  mailimf_body_free(message->msg_body);
  mailimf_fields_free(message->msg_fields);
  free(message);
}
Exemplo n.º 2
0
static int imap_get_bodystructure(mailmessage * msg_info,
				  struct mailmime ** result)
{
  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_body * imap_body;
  struct mailmime * body;
  int res;
  struct mailimf_fields * fields;
  struct mailmime * new_body;
  struct mailmime_content * content_message;
  struct mailimap_envelope * envelope;
  uint32_t uid;
  char * references;
  size_t ref_size;
  clistiter * cur;

  if (msg_info->msg_mime != NULL) {
    * result = msg_info->msg_mime;

    return MAIL_NO_ERROR;
  }

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

  fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
  if (fetch_type == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_set;
  }

  fetch_att = mailimap_fetch_att_new_uid();
  if (fetch_att == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_fetch_type;
  }

  r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
  if (r != MAILIMAP_NO_ERROR) {
    mailimap_fetch_att_free(fetch_att);
    res = MAIL_ERROR_MEMORY;
    goto free_fetch_type;
  }

  fetch_att = mailimap_fetch_att_new_bodystructure();
  if (fetch_att == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_fetch_type;
  }

  r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
  if (r != MAILIMAP_NO_ERROR) {
    mailimap_fetch_att_free(fetch_att);
    res = MAIL_ERROR_MEMORY;
    goto free_fetch_type;
  }

  r = imap_add_envelope_fetch_att(fetch_type);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto free_fetch_type;
  }
  

  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);
  }

  cur = clist_begin(fetch_result);
  if (cur == NULL) {
    mailimap_fetch_list_free(fetch_result);
    return MAIL_ERROR_FETCH;
  }

  msg_att = clist_content(cur);

  uid = 0;
  references = NULL;
  ref_size = 0;
  imap_body = NULL;
  envelope = NULL;

  r = imap_get_msg_att_info(msg_att,
      &uid, &envelope, &references, &ref_size, NULL, &imap_body);
  if (r != MAIL_NO_ERROR) {
    mailimap_fetch_list_free(fetch_result);
    res = r;
    goto err;
  }

  if (uid != msg_info->msg_index) {
    mailimap_fetch_list_free(fetch_result);
    res = MAIL_ERROR_MSG_NOT_FOUND;
    goto err;
  }

  if (imap_body == NULL) {
    mailimap_fetch_list_free(fetch_result);
    res = MAIL_ERROR_FETCH;
    goto err;
  }

  r = imap_body_to_body(imap_body, &body);
  if (r != MAIL_NO_ERROR) {
    mailimap_fetch_list_free(fetch_result);
    res = r;
    goto err;
  }

  fields = NULL;
  if (envelope != NULL) {
    r = imap_env_to_fields(envelope, references, ref_size, &fields);
    if (r != MAIL_NO_ERROR) {
      mailmime_free(body);
      mailimap_fetch_list_free(fetch_result);
      res = r;
      goto err;
    }
  }

  content_message = mailmime_get_content_message();
  if (content_message == NULL) {
    if (fields != NULL)
      mailimf_fields_free(fields);
    mailmime_free(body);
    mailimap_fetch_list_free(fetch_result);
    res = MAIL_ERROR_MEMORY;
    goto err;
  }

  new_body = mailmime_new(MAILMIME_MESSAGE, NULL,
      0, NULL, content_message,
      NULL, NULL, NULL, NULL, fields, body);

  if (new_body == NULL) {
    mailmime_content_free(content_message);
    if (fields != NULL)
      mailimf_fields_free(fields);
    mailmime_free(body);
    mailimap_fetch_list_free(fetch_result);
    res = MAIL_ERROR_MEMORY;
    goto err;
  }
  msg_info->msg_mime = new_body;
  
  mailimap_fetch_list_free(fetch_result);

  * result = new_body;

  return MAIL_NO_ERROR;

 free_fetch_type:
  mailimap_fetch_type_free(fetch_type);
 free_set:
  mailimap_set_free(set);
 err:
  return res;
}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
0
static mailmessage * feed_item_to_message(mailsession * session,
    unsigned int num,
    struct newsfeed_item * item)
{
  struct mailimf_fields * fields;
  struct mailimf_date_time * date_time;
  time_t time_modified;
  struct mailimf_mailbox_list * from;
  mailmessage * msg;
  char * subject;
  const char * subject_const;
  char * msg_id;
  int r;
  const char * author_const;
  
  from = NULL;
  author_const = newsfeed_item_get_author(item);
  if (author_const != NULL) {
    char * author;
    char * addr_spec;
    struct mailimf_mailbox * mb;
    
    author = strdup(author_const);
    if (author == NULL) {
      goto err;
    }
    
    from = mailimf_mailbox_list_new_empty();
    if (from == NULL) {
      free(author);
      goto err;
    }
    addr_spec = strdup("*****@*****.**");
    if (addr_spec == NULL) {
      free(author);
      goto free_from;
    }
      
    /* XXX - encode author with MIME */
    mb = mailimf_mailbox_new(author, addr_spec);
    if (mb == NULL) {
      free(addr_spec);
      free(author);
      goto free_from;
    }
    
    r = mailimf_mailbox_list_add(from, mb);
    if (r != MAILIMF_NO_ERROR) {
      mailimf_mailbox_free(mb);
      goto free_from;
    }
  }
  
  date_time = NULL;
  time_modified = newsfeed_item_get_date_modified(item);
  if (time_modified != (time_t) -1) {
    date_time = mailimf_get_date(time_modified);
    if (date_time == NULL) {
      goto free_from;
    }
  }
  
  subject = NULL;
  subject_const = newsfeed_item_get_title(item);
  if (subject_const != NULL) {
    subject = make_quoted_printable("utf-8", subject_const);
    if (subject == NULL) {
      goto free_date;
    }
  }
  
  msg_id = mailimf_get_message_id();
  if (msg_id == NULL) {
    goto free_subject;
  }
  
  fields = mailimf_fields_new_with_data_all(date_time,
      from,
      NULL,
      NULL,
      NULL,
      NULL,
      NULL,
      msg_id,
      NULL,
      NULL,
      subject);
  
  msg = mailmessage_new();
  r = mailmessage_init(msg, session, feed_message_driver, num, 0);
  if (r != MAIL_NO_ERROR) {
    goto free_fields;
  }
  msg->msg_fields = fields;
  
  return msg;
  
 free_fields:
  mailimf_fields_free(fields);
  goto err;
 free_subject:
  free(subject);
 free_date:
  mailimf_date_time_free(date_time);
 free_from:
  mailimf_mailbox_list_free(from);
 err:
  return NULL;
}
Exemplo n.º 5
0
int main(int argc, char ** argv)
{
  struct mailimf_fields * fields;
  char * text;
  char * filename;
  struct mailmime * message;
  struct mailmime * text_part;
  struct mailmime * file_part;
  int r;
  int col;

  if (argc < 3) {
    printf("syntax: compose-msg \"text\" filename\n");
    return 1;
  }

  fields = build_fields();
  if (fields == NULL)
    goto err;

  message = build_message(fields);
  if (message == NULL)
    goto free_fields;

  text = argv[1];
  text_part = build_body_text(text);
  if (text_part == NULL)
    goto free_message;

  filename = argv[2];
  file_part = build_body_file(filename);
  if (file_part == NULL)
    goto free_text;

  r = mailmime_smart_add_part(message, text_part);
  if (r != MAILIMF_NO_ERROR)
    goto free_file;

  r = mailmime_smart_add_part(message, file_part);
  if (r != MAILIMF_NO_ERROR)
    goto free_file_alone;
  
  col = 0;
  mailmime_write(stdout, &col, message);

  mailmime_free(message);

  return 0;

 free_file_alone:
  mailmime_free(file_part);
  goto free_text;
 free_file:
  mailmime_free(file_part);
 free_text:
  mailmime_free(text_part);
 free_message:
  mailmime_free(message);
  goto err;
 free_fields:
  mailimf_fields_free(fields);
 err:
  printf("error memory\n");
  return 1;
}
Exemplo n.º 6
0
static int append_message_flags(mailsession * session,
    const char * message, size_t size, struct mail_flags * flags)
{
  carray * msglist;
  unsigned int i;
  uint32_t * msg;
  uint32_t num;
  char key_value[PATH_MAX];
  MMAPString * mmapstr;
  struct mail_cache_db * maildb;
  struct db_session_state_data * data;
  size_t cur_token;
  struct mailimf_fields * fields;
  int r;
  int res;
  
  data = get_data(session);
  
  r = mail_cache_db_open_lock(data->db_filename, &maildb);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }
  
  num = 0;
  r = db_get_next_msg_number(maildb, &num);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto err;
  }
  
  r = db_get_message_list(maildb, &msglist);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto close_db;
  }
  
  msg = malloc(sizeof(* msg));
  if (msg == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_msglist;
  }
  
  * msg = num;
  
  r = carray_add(msglist, msg, NULL);
  if (r < 0) {
    res = MAIL_ERROR_MEMORY;
    free(msg);
    goto free_msglist;
  }
  
  r = db_set_message_list(maildb, msglist);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto free_msglist;
  }
  
  /* free msglist */
  
  for(i = 0 ; i < carray_count(msglist) ; i ++) {
    msg = carray_get(msglist, i);
    free(msg);
  }
  carray_free(msglist);
  
  snprintf(key_value, sizeof(key_value), "%lu", (unsigned long) num);
  
  r = mail_cache_db_put(maildb, key_value, strlen(key_value),
      message, size);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto close_db;
  }
  
  /* write envelope */
  
  cur_token = 0;
  r = mailimf_envelope_fields_parse(message, size, &cur_token, &fields);
  if (r != MAILIMF_NO_ERROR) {
    res = MAIL_ERROR_PARSE;
    goto close_db;
  }
  
  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close_db;
  }
  
  cur_token = 0;
  r = mailimf_cache_fields_write(mmapstr, &cur_token, fields);
  if (r != MAIL_NO_ERROR) {
    res = r;
    mmap_string_free(mmapstr);
    goto close_db;
  }
  
  snprintf(key_value, sizeof(key_value), "%lu-envelope", (unsigned long) num);
  
  r = mail_cache_db_put(maildb, key_value, strlen(key_value),
      mmapstr->str, mmapstr->len);
  
  mmap_string_free(mmapstr);
  
  mailimf_fields_free(fields);
  
  /* write flags */
  
  if (flags != NULL) {
    snprintf(key_value, sizeof(key_value), "%lu-flags", (unsigned long) num);
    
    mmapstr = mmap_string_new("");
    if (mmapstr == NULL) {
      res = MAIL_ERROR_MEMORY;
      goto close_db;
    }
    
    r = generic_cache_flags_write(maildb, mmapstr,
        key_value, flags);
    
    mmap_string_free(mmapstr);
    
    if (r != MAIL_NO_ERROR) {
      res = MAIL_ERROR_FILE;
      goto close_db;
    }
  }
  
  mail_cache_db_close_unlock(data->db_filename, maildb);
  
  return MAIL_NO_ERROR;
  
 free_msglist:
  for(i = 0 ; i < carray_count(msglist) ; i ++) {
    msg = carray_get(msglist, i);
    free(msg);
  }
  carray_free(msglist);
 close_db:
  mail_cache_db_close_unlock(data->db_filename, maildb);
 err:
  return res;
}