Exemple #1
0
int mail_thread_sort(struct mailmessage_tree * tree,
    int (* comp_func)(struct mailmessage_tree **,
        struct mailmessage_tree **),
    int sort_sub)
{
  unsigned int cur;
  int r;
  int res;

  for(cur = 0 ; cur < carray_count(tree->node_children) ; cur ++) {
    struct mailmessage_tree * subtree;

    subtree = carray_get(tree->node_children, cur);

    if (sort_sub) {
      r = mail_thread_sort(subtree, comp_func, sort_sub);
      if (r != MAIL_NO_ERROR) {
	res = r;
	goto err;
      }
    }
  }

  qsort(carray_data(tree->node_children), carray_count(tree->node_children),
      sizeof(struct mailmessage_tree *),
	(int (*)(const void *, const void *)) comp_func);

  return MAIL_NO_ERROR;
  
 err:
  return res;
}
Exemple #2
0
void mailmh_folder_free(struct mailmh_folder * folder)
{
  unsigned int i;

  for(i = 0 ; i < carray_count(folder->fl_subfolders_tab) ; i++) {
    struct mailmh_folder * subfolder;

    subfolder = carray_get(folder->fl_subfolders_tab, i);
    if (subfolder != NULL)
      mailmh_folder_free(subfolder);
  }
  carray_free(folder->fl_subfolders_tab);
  chash_free(folder->fl_subfolders_hash);

  for(i = 0 ; i < carray_count(folder->fl_msgs_tab) ; i++) {
    struct mailmh_msg_info * msg_info;

    msg_info = carray_get(folder->fl_msgs_tab, i);
    if (msg_info != NULL)
      mailmh_msg_info_free(msg_info);
  }
  carray_free(folder->fl_msgs_tab);
  chash_free(folder->fl_msgs_hash);
  
  free(folder->fl_filename);
  free(folder->fl_name);

  free(folder);
}
Exemple #3
0
static void
display_sub_tree(MMAPString * prefix,
    struct mailmessage_tree * msg_tree,
    int level, int has_next, unsigned int * pcount)
{
  carray * list;
  uint32_t cur;
  
  if (msg_tree->node_msg != NULL) {
    print_mail_info(prefix->str, msg_tree->node_msg);
    (* pcount) ++;
  }

  list = msg_tree->node_children;
  
  if (carray_count(list) != 0) {
    char old_prefix[2];
	    
    if (level > 1) {
      memcpy(old_prefix, prefix->str + prefix->len - 2, 2);
      if (has_next)
        memcpy(prefix->str + prefix->len - 2, "| ", 2);
      else
        memcpy(prefix->str + prefix->len - 2, "  ", 2);
    }
    for(cur = 0 ; cur < carray_count(list) ; cur ++) {
      int sub_has_next;
      
      if (cur != carray_count(list) - 1) {
	if (level > 0) {
	  if (mmap_string_append(prefix, "+-") == NULL)
            return;
        }
	sub_has_next = 1;
      }
      else {
	if (level > 0) {
	  if (mmap_string_append(prefix, "\\-") == NULL)
            return;
        }
	sub_has_next = 0;
      }

      display_sub_tree(prefix, carray_get(list, cur),
          level + 1, sub_has_next, pcount);

      if (mmap_string_truncate(prefix, prefix->len - 2) == NULL) {
        return;
      }
    }
    if (level > 1) {
      memcpy(prefix->str + prefix->len - 2, old_prefix, 2);
    }
  }
}
Exemple #4
0
static int flags_store_process(mailsession * session)
{
  unsigned int i;
  MMAPString * mmapstr;
  int r;
  int res;
  struct mail_cache_db * maildb;
  struct db_session_state_data * data;
  struct mail_flags_store * flags_store;
  
  data = get_data(session);
  
  flags_store = data->db_flags_store;
  
  if (carray_count(flags_store->fls_tab) == 0)
    return MAIL_NO_ERROR;
  
  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }
  
  r = mail_cache_db_open_lock(data->db_filename, &maildb);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto free_mmapstr;
  }
  
  for(i = 0 ; i < carray_count(flags_store->fls_tab) ; i ++) {
    mailmessage * msg;
    char key[PATH_MAX];
    
    msg = carray_get(flags_store->fls_tab, i);
    
    snprintf(key, sizeof(key), "%lu-flags", (unsigned long) msg->msg_index);
    
    r = generic_cache_flags_write(maildb, mmapstr,
        key, msg->msg_flags);
  }
  
  mail_flags_store_clear(flags_store);
  
  mail_cache_db_close_unlock(data->db_filename, maildb);
  mmap_string_free(mmapstr);
  
  return MAIL_NO_ERROR;
  
 free_mmapstr:
  mmap_string_free(mmapstr);
 err:
  return res;
}
Exemple #5
0
static int mh_flags_store_process(char * flags_directory, char * quoted_mb,
				  struct mail_flags_store * flags_store)
{
  char filename_flags[PATH_MAX];
  struct mail_cache_db * cache_db_flags;
  MMAPString * mmapstr;
  unsigned int i;
  int r;
  int res;

  if (carray_count(flags_store->fls_tab) == 0)
    return MAIL_NO_ERROR;

  if (quoted_mb == NULL)
    return MAIL_NO_ERROR;

  snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
	   flags_directory, quoted_mb, FLAGS_NAME);

  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }

  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close_db_flags;
  }
  
  for(i = 0 ; i < carray_count(flags_store->fls_tab) ; i ++) {
    mailmessage * msg;

    msg = carray_get(flags_store->fls_tab, i);

    r = mhdriver_write_cached_flags(cache_db_flags, mmapstr,
        msg->msg_uid, msg->msg_flags);
  }

  mmap_string_free(mmapstr);
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);

  mail_flags_store_clear(flags_store);

  return MAIL_NO_ERROR;

 close_db_flags:
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
 err:
  return res;
}
Exemple #6
0
static int privacy_handler(struct mailprivacy * privacy,
    mailmessage * msg,
    struct mailmime * mime, struct mailmime ** result)
{
  int r;
  struct mailmime * alternative_mime;
  unsigned int i;
  
  alternative_mime = NULL;
  for(i = 0 ; i < carray_count(privacy->protocols) ; i ++) {
    struct mailprivacy_protocol * protocol;
    
    protocol = carray_get(privacy->protocols, i);
    
    if (protocol->decrypt != NULL) {
      r = protocol->decrypt(privacy, msg, mime, &alternative_mime);
      if (r == MAIL_NO_ERROR) {
        
        * result = alternative_mime;
        
        return MAIL_NO_ERROR;
      }
    }
  }
  
  return MAIL_ERROR_INVAL;
}
Exemple #7
0
LIBETPAN_EXPORT
int mailfolder_detach_parent(struct mailfolder * folder)
{
  unsigned int i;
  int r;
  
  if (folder->fld_parent == NULL)
    return MAIL_ERROR_INVAL;

  r = carray_delete_slow(folder->fld_parent->fld_children,
      folder->fld_sibling_index);
  if (r < 0)
    return MAIL_ERROR_INVAL;
  
  for(i = 0 ; i < carray_count(folder->fld_parent->fld_children) ; i ++) {
    struct mailfolder * child;
    
    child = carray_get(folder->fld_parent->fld_children, i);
    child->fld_sibling_index = i;
  }

  folder->fld_parent = NULL;
  folder->fld_sibling_index = 0;

  return MAIL_NO_ERROR;
}
Exemple #8
0
static int get_list_folders(struct mailmh_folder * folder, clist ** result)
{
  unsigned int i;
  clist * list;
  char * new_filename;
  int res;
  int r;

  list = * result;

  new_filename = strdup(folder->fl_filename);
  if (new_filename == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free;
  }

  r = mailmh_folder_update(folder);

  switch (r) {
  case MAILMH_NO_ERROR:
    break;

  default:
    res = mhdriver_mh_error_to_mail_error(r);
    free(new_filename);
    goto free;
  }

  r = clist_append(list, new_filename);
  if (r < 0) {
    free(new_filename);
    res = MAIL_ERROR_MEMORY;
    goto free;
  }
  
  if (folder->fl_subfolders_tab != NULL) {
    for(i = 0 ; i < carray_count(folder->fl_subfolders_tab) ; i++) {
      struct mailmh_folder * subfolder;

      subfolder = carray_get(folder->fl_subfolders_tab, i);

      r = get_list_folders(subfolder, &list);
      if (r != MAIL_NO_ERROR) {
		    free(new_filename);
	res = MAIL_ERROR_MEMORY;
	goto free;
      }
    }
  }

  * result = list;
  
  free(new_filename);
  return MAIL_NO_ERROR;

 free:
  clist_foreach(list, (clist_func) free, NULL);
  clist_free(list);
  return res;
}
int
maildriver_generic_get_envelopes_list(mailsession * session,
    struct mailmessage_list * env_list)
{
  int r;
  unsigned i;

  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;
    
    msg = carray_get(env_list->msg_tab, i);
    
    if (msg->msg_fields == NULL) {
      struct mailimf_fields * fields;

      r = mailmessage_fetch_envelope(msg, &fields);
      if (r != MAIL_NO_ERROR) {
	/* do nothing */
      }
      else {
	msg->msg_fields = fields;
      }
      mailmessage_flush(msg);
    }
  }

  return MAIL_NO_ERROR;
}
static int expunge_folder(mailsession * session)
{
  unsigned int i;
  int r;
  int res;
  struct maildir * md;

  check_folder(session);

  md = get_maildir_session(session);
  if (md == NULL)
    return MAIL_ERROR_BAD_STATE;

  r = maildir_update(md);
  if (r != MAILDIR_NO_ERROR) {
    res = maildirdriver_maildir_error_to_mail_error(r);
    goto err;
  }

  for(i = 0 ; i < carray_count(md->mdir_msg_list) ; i++) {
    struct maildir_msg * md_msg;

    md_msg = carray_get(md->mdir_msg_list, i);

    if ((md_msg->msg_flags & MAILDIR_FLAG_TRASHED) != 0)
      maildir_message_remove(md, md_msg->msg_uid);
  }

  return MAIL_NO_ERROR;

 err:
  return res;
}
Exemple #11
0
static int feeddriver_get_messages_list(mailsession * session,
    struct mailmessage_list ** result)
{
  unsigned int i;
  struct feed_session_state_data * data;
  unsigned int count;
  struct mailmessage_list * msg_list;
  carray * tab;
  int res;
  int r;
  
  update(session);
  data = get_data(session);
  if (data->feed_error != MAIL_NO_ERROR) {
    res = data->feed_error;
    goto err;
  }
  
  count = newsfeed_item_list_get_count(data->feed_session);
  
  tab = carray_new(count);
  if (tab == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }
  fprintf(stderr, "count: %i\n", count);
  
  for(i = 0 ; i < count ; i ++) {
    struct newsfeed_item * item;
    mailmessage * msg;
    
    item = newsfeed_get_item(data->feed_session, i);
    msg = feed_item_to_message(session, i, item);
    r = carray_add(tab, msg, NULL);
    if (r < 0) {
      res = MAIL_ERROR_MEMORY;
      goto free_tab;
    }
  }
  
  msg_list = mailmessage_list_new(tab);
  if (msg_list == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_tab;
  }
  
  * result = msg_list;
  
  return MAIL_NO_ERROR;
  
 free_tab:
  for(i = 0 ; i < carray_count(tab) ; i ++) {
    mailmessage * msg;
    
    msg = carray_get(tab, i);
    mailmessage_free(msg);
  }
 err:
  return res;
}
Exemple #12
0
int mail_build_thread(int type, char * default_from,
    struct mailmessage_list * env_list,
    struct mailmessage_tree ** result,
     int (* comp_func)(struct mailmessage_tree **,
         struct mailmessage_tree **))
{
  unsigned int i;

  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++)
    mailmessage_resolve_single_fields(carray_get(env_list->msg_tab, i));

  switch (type) {
  case MAIL_THREAD_REFERENCES:
    return mail_build_thread_references(default_from,
        env_list, result, TRUE, comp_func);

  case MAIL_THREAD_REFERENCES_NO_SUBJECT:
    return mail_build_thread_references(default_from,
        env_list, result, FALSE, comp_func);

  case MAIL_THREAD_ORDEREDSUBJECT:
    return mail_build_thread_orderedsubject(default_from,
        env_list, result, comp_func);
    
  case MAIL_THREAD_NONE:
    return mail_build_thread_none(default_from,
        env_list, result, comp_func);
    
  default:
    return MAIL_ERROR_NOT_IMPLEMENTED;
  }
}
Exemple #13
0
static int get_envelopes_list(mailsession * session,
    struct mailmessage_list * env_list)
{
  unsigned int i;
  char key[PATH_MAX];
  int r;
  struct mail_cache_db * maildb;
  int res;
  struct db_session_state_data * data;
  MMAPString * mmapstr;
  
  data = get_data(session);
  
  flags_store_process(session);
  
  r = mail_cache_db_open_lock(data->db_filename, &maildb);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }
  
  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close_db;
  }
  
  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;
    
    msg = carray_get(env_list->msg_tab, i);
    if (msg->msg_fields == NULL) {
      snprintf(key, sizeof(key), "%lu-envelope",
          (unsigned long) msg->msg_index);
      
      r = generic_cache_fields_read(maildb, mmapstr,
        key, &msg->msg_fields);
    }
    
    if (msg->msg_flags == NULL) {
      snprintf(key, sizeof(key), "%lu-flags",
          (unsigned long) msg->msg_index);
      
      r = generic_cache_flags_read(maildb, mmapstr,
          key, &msg->msg_flags);
    }
  }
  
  mmap_string_free(mmapstr);
  
  mail_cache_db_close_unlock(data->db_filename, maildb);
  
  return MAIL_NO_ERROR;
  
 close_db:
  mail_cache_db_close_unlock(data->db_filename, maildb);
 err:
  return res;
}
Exemple #14
0
static inline int is_descendant(struct mailmessage_tree * node,
			 struct mailmessage_tree * maybe_child)
{
  unsigned int i;

  for(i = 0 ; i < carray_count(node->node_children) ; i++) {
    struct mailmessage_tree * tree;

    tree = carray_get(node->node_children, i);
    if (tree == maybe_child)
      return TRUE;
    if (carray_count(tree->node_children) != 0)
      if (is_descendant(tree, maybe_child))
	return TRUE;
  }

  return FALSE;
}
Exemple #15
0
static void mailpop3_msg_info_tab_reset(carray * msg_tab)
{
  unsigned int i;

  for(i = 0 ; i < carray_count(msg_tab) ; i++) {
    struct mailpop3_msg_info * msg;
    msg = carray_get(msg_tab, i);
    msg->msg_deleted = FALSE;
  }
}
Exemple #16
0
unsigned int mailmh_folder_get_message_number(struct mailmh_folder * folder)
{
  unsigned int i;
  unsigned int count;
  
  count = 0;
  for(i = 0 ; i < carray_count(folder->fl_msgs_tab) ; i ++)
    if (carray_get(folder->fl_msgs_tab, i) != NULL)
      count ++;
  
  return count;
}
Exemple #17
0
static void mailpop3_msg_info_tab_free(carray * msg_tab)
{
  unsigned int i;

  for(i = 0 ; i < carray_count(msg_tab) ; i++) {
    struct mailpop3_msg_info * msg;
    
    msg = carray_get(msg_tab, i);
    mailpop3_msg_info_free(msg);
  }
  carray_free(msg_tab);
}
Exemple #18
0
void tcp_receiver_free(struct tcp_receiver* r)
{
	int i;
	/*释放监视的事件*/
	for (i = 0; i < carray_count(r->bevs); ++i)
		bufferevent_free(carray_at(r->bevs, i));
	/*释放监听*/
	evconnlistener_free(r->listener);

	carray_free(r->bevs);
	free(r);
}
Exemple #19
0
void mailprivacy_unregister(struct mailprivacy * privacy,
    struct mailprivacy_protocol * protocol)
{
  unsigned int i;
  
  for(i = 0 ; i < carray_count(privacy->protocols) ; i ++) {
    if (carray_get(privacy->protocols, i) == protocol) {
      carray_delete(privacy->protocols, i);
      return;
    }
  }
}
Exemple #20
0
void mailstream_certificate_chain_free(carray * certificate_chain)
{
  unsigned int i;
  
  if (certificate_chain == NULL)
    return;
  
  for(i = 0 ; i < carray_count(certificate_chain) ; i ++) {
    mmap_string_free(carray_get(certificate_chain, i));
  }
  carray_free(certificate_chain);
}
Exemple #21
0
static void print_message_list(mailsession * session)
{
  int r;
  uint32_t i;
  struct mailmessage_list * env_list;
  unsigned int count;
  
  /* get the list of messages numbers of the folder */

  r = mailsession_get_messages_list(session, &env_list);
  if (r != MAIL_NO_ERROR) {
    printf("error message list\n");
    goto err;
  }

  /* get fields content of these messages */

  r = mailsession_get_envelopes_list(session, env_list);
  if (r != MAIL_NO_ERROR) {
    printf("error envelopes list\n");
    goto free_msg_list;
  }

  /* display all the messages */
  
  count = 0;
  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;
    
    msg = carray_get(env_list->msg_tab, i);

    if (msg->msg_fields == NULL) {
      printf("could not fetch envelope of message %i\n", i);
    }
    else {
      simple_print_mail_info(msg);
      count ++;
    }
  }
  printf("  %i messages\n", count);

  /* free structure */

  mailmessage_list_free(env_list);

  return;

 free_msg_list:
  mailmessage_list_free(env_list);
 err:
  {}
}
int
maildriver_env_list_to_msg_list_no_flags(struct mailmessage_list * env_list,
    clist ** result)
{
  clist * msg_list;
  int r;
  int res;
  unsigned int i;

  msg_list = clist_new();
  if (msg_list == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }

  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;

    msg = carray_get(env_list->msg_tab, i);

    if (msg->msg_flags == NULL) {
      uint32_t * pindex;

      pindex = malloc(sizeof(* pindex));
      if (pindex == NULL) {
	res = MAIL_ERROR_MEMORY;
	goto free_msg_list;
      }

      * pindex = msg->msg_index;

      r = clist_append(msg_list, pindex);
      if (r < 0) {
	free(pindex);
	res = MAIL_ERROR_MEMORY;
	goto free_msg_list;
      }

    }
  }

  * result = msg_list;

  return MAIL_NO_ERROR;

 free_msg_list:
  clist_foreach(msg_list, (clist_func) free, NULL);
  clist_free(msg_list);
 err:
  return res;
}
Exemple #23
0
static int read_list(mailpop3 * f, carray ** result)
{
  unsigned int indx;
  uint32_t size;
  carray * msg_tab;
  struct mailpop3_msg_info * msg;
  char * line;

  msg_tab = carray_new(128);
  if (msg_tab == NULL)
    goto err;

  while (1) {
    line = read_line(f);
    if (line == NULL)
      goto free_list;

    if (mailstream_is_end_multiline(line))
      break;

    indx = strtol(line, &line, 10);

    if (!parse_space(&line))
      continue;

    size = strtol(line, &line, 10);
    
    msg = mailpop3_msg_info_new(indx, size, NULL);
    if (msg == NULL)
      goto free_list;

    if (carray_count(msg_tab) < indx) {
      int r;

      r = carray_set_size(msg_tab, indx);
      if (r == -1)
	goto free_list;
    }

    carray_set(msg_tab, indx - 1, msg);
  }

  * result = msg_tab;
  
  return MAILPOP3_NO_ERROR;

 free_list:
  mailpop3_msg_info_tab_free(msg_tab);
 err:
  return MAILPOP3_ERROR_STREAM;
}
static int flags_store_process(struct maildir * md,
    struct mail_flags_store * flags_store)
{
  unsigned int i;

  if (carray_count(flags_store->fls_tab) == 0)
    return MAIL_NO_ERROR;

  for(i = 0 ; i < carray_count(flags_store->fls_tab) ; i ++) {
    mailmessage * msg;
    uint32_t md_flags;

    msg = carray_get(flags_store->fls_tab, i);
    md_flags = maildirdriver_flags_to_maildir_flags(msg->msg_flags->fl_flags);
    md_flags &= ~MAILDIR_FLAG_NEW;

    maildir_message_change_flags(md, msg->msg_uid, md_flags);
  }

  mail_flags_store_clear(flags_store);

  return MAIL_NO_ERROR;
}
void mailmessage_tree_free_recursive(struct mailmessage_tree * tree)
{
    unsigned int i;

    for(i = 0 ; i < carray_count(tree->node_children) ; i++) {
        struct mailmessage_tree * child;

        child = carray_get(tree->node_children, i);

        mailmessage_tree_free_recursive(child);
    }

    mailmessage_tree_free(tree);
}
Exemple #26
0
static inline struct mailpop3_msg_info *
mailpop3_msg_info_tab_find_msg(carray * msg_tab, unsigned int indx)
{
  struct mailpop3_msg_info * msg;

  if (indx == 0)
    return NULL;

  if (indx > carray_count(msg_tab))
    return NULL;

  msg = carray_get(msg_tab, indx - 1);

  return msg;
}
Exemple #27
0
static struct mailprivacy_protocol *
get_protocol(struct mailprivacy * privacy, char * privacy_driver)
{
  unsigned int i;
  
  for(i = 0 ; i < carray_count(privacy->protocols) ; i ++) {
    struct mailprivacy_protocol * protocol;
    
    protocol = carray_get(privacy->protocols, i);
    if (strcasecmp(protocol->name, privacy_driver) == 0)
      return protocol;
  }
  
  return NULL;
}
Exemple #28
0
static inline time_t tree_get_date(struct mailmessage_tree * tree)
{
  if (tree->node_msg != NULL) {
    return tree->node_date;
  }
  else {
    struct mailmessage_tree * subtree;
    
    if (carray_count(tree->node_children) == 0)
      return (time_t) -1;
    
    subtree = carray_get(tree->node_children, 0);
    
    return subtree->node_date;
  }
}
Exemple #29
0
static void folder_free_msg_list(struct folder_ref_info * ref_info,
                                 struct mailmessage_list * env_list)
{
    unsigned int i;

    for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
        mailmessage * msg;
        int count;

        msg = carray_get(env_list->msg_tab, i);

        count = folder_message_unref(ref_info, msg);
    }
    carray_set_size(env_list->msg_tab, 0);
    mailmessage_list_free(env_list);
}
Exemple #30
0
static int pop3driver_messages_number(mailsession * session, const char * mb,
				      uint32_t * result)
{
  carray * msg_tab;
  int r;

  r = mailpop3_list(get_pop3_session(session), &msg_tab);
  if (r != MAILPOP3_NO_ERROR) {
	  return pop3driver_pop3_error_to_mail_error(r);
  }

  * result = carray_count(msg_tab) -
    get_pop3_session(session)->pop3_deleted_count;

  return MAIL_NO_ERROR;
}