Beispiel #1
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);
}
Beispiel #2
0
void mailprivacy_free(struct mailprivacy * privacy)
{
  carray_free(privacy->protocols);
  chash_free(privacy->mime_ref);
  chash_free(privacy->mmapstr);
  chash_free(privacy->msg_ref);
  free(privacy->tmp_dir);
  free(privacy);
}
void mailprivacy_smime_done(struct mailprivacy * privacy)
{
  mailprivacy_unregister(privacy, &smime_protocol);
  chash_free(private_keys);
  private_keys = NULL;
  chash_free(certificates);
  certificates = NULL;
  if (CAfile != NULL) {
    unlink(CAfile);
    free(CAfile);
  }
  CAfile = NULL;
  CAcert_dir[0] = '\0';
}
Beispiel #4
0
void mail_flags_store_free(struct mail_flags_store * flags_store)
{
  mail_flags_store_clear(flags_store);
  chash_free(flags_store->fls_hash);
  carray_free(flags_store->fls_tab);
  free(flags_store);
}
Beispiel #5
0
static struct folder_ref_info *
folder_ref_info_new(struct mailfolder * folder
                    /*, struct message_folder_finder * msg_folder_finder */)
{
    struct folder_ref_info * ref_info;

    ref_info = malloc(sizeof(* ref_info));
    if (ref_info == NULL)
        goto err;

    ref_info->folder = folder;

    ref_info->msg_hash = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
    if (ref_info->msg_hash == NULL)
        goto free;

    ref_info->uid_hash = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYNONE);
    if (ref_info->uid_hash == NULL)
        goto free_msg_hash;

    ref_info->lost_session = 1;

    return ref_info;

free_msg_hash:
    chash_free(ref_info->msg_hash);
free:
    free(ref_info);
err:
    return NULL;
}
void mailprivacy_smime_encryption_id_list_clear(struct mailprivacy * privacy,
    mailmessage * msg)
{
  clist * encryption_id_list;
  clistiter * iter;
  
  LOCK();
  encryption_id_list = get_list(privacy, msg);
  if (encryption_id_list != NULL) {
    chashdatum key;
    
    for(iter = clist_begin(encryption_id_list) ;
        iter != NULL ; iter = clist_next(iter)) {
      char * str;
      
      str = clist_content(iter);
      free(str);
    }
    clist_free(encryption_id_list);
    
    key.data = &msg;
    key.len = sizeof(msg);
    chash_delete(encryption_id_hash, &key, NULL);
    
    if (chash_count(encryption_id_hash) == 0) {
      chash_free(encryption_id_hash);
      encryption_id_hash = NULL;
    }
  }
  UNLOCK();
}
Beispiel #7
0
int main(int argc, char* argv[])
{
    chash* table = chash_new();
    char* text = read_entire_file(fopen(argv[1], "r"));
    char* token = strtok(text, " \t\n");
    while (token)
    {
        char* cleaned_word = clean_word(token);
        if (cleaned_word != NULL)
        {
            see_word(table, cleaned_word);
            free(cleaned_word);
        }
        token = strtok(NULL, " \t\n");
    }

    chash_iterator iter;
    chash_iterator_init(&iter, table);
    char *key; void *value;
    pair* pairs = malloc(chash_size(table) * sizeof(*pairs));
    int i = 0;
    while (chash_iterator_next(&iter, &key, &value)) {
      pairs[i].key = key;
      pairs[i].value = value;
      i++;
    }
    qsort(pairs, chash_size(table), sizeof(pair), compare_by_frequency);
    printf("Words sorted by frequency:\n");
    for (i = 0; i < chash_size(table); ++i)
        printf("%s: %d\n", pairs[i].key, *((int*)pairs[i].value));
    chash_free(table);
    return 0;
}
Beispiel #8
0
void libetpan_engine_free(struct mailengine * engine)
{
    chash_free(engine->storage_hash);
#ifdef LIBETPAN_REENTRANT
    pthread_mutex_destroy(&engine->storage_hash_lock);
#endif
    free(engine);
}
Beispiel #9
0
void nntp_main_done(gboolean have_connectivity)
{
	nntp_disconnect_all(have_connectivity);
	etpan_thread_manager_stop(thread_manager);
#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
	return;
#endif
	etpan_thread_manager_join(thread_manager);
	
	g_source_remove(thread_manager_signal);
	g_io_channel_unref(io_channel);
	
	etpan_thread_manager_free(thread_manager);
	
	chash_free(session_hash);
	chash_free(nntp_hash);
}
Beispiel #10
0
struct mailprivacy * mailprivacy_new(char * tmp_dir, int make_alternative)
{
  struct mailprivacy * privacy;
  
  privacy = malloc(sizeof(* privacy));
  if (privacy == NULL)
    goto err;
  
  privacy->tmp_dir = strdup(tmp_dir); 
  if (privacy->tmp_dir == NULL)
    goto free;
  
  privacy->msg_ref = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
  if (privacy->msg_ref == NULL)
    goto free_tmp_dir;
  
  privacy->mmapstr = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
  if (privacy->mmapstr == NULL)
    goto free_msg_ref;
  
  privacy->mime_ref = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
  if (privacy->mime_ref == NULL)
    goto free_mmapstr;
  
  privacy->protocols = carray_new(16);
  if (privacy->protocols == NULL)
    goto free_mime_ref;

  privacy->make_alternative = make_alternative;
  
  return privacy;
  
 free_mime_ref:
  chash_free(privacy->mime_ref);
 free_mmapstr:
  chash_free(privacy->mmapstr);
 free_msg_ref:
  chash_free(privacy->msg_ref);
 free_tmp_dir:
  free(privacy->tmp_dir);
 free:
  free(privacy);
 err:
  return NULL;
}
Beispiel #11
0
int mmap_string_unref(char * str)
{
  MMAPString * string;
  chash * ht;
  chashdatum key;
  chashdatum data;
  int r;

  if (str == NULL)
    return -1;
  
  MUTEX_LOCK(&mmapstring_lock);

  ht = mmapstring_hashtable;
  
  if (ht == NULL) {

	MUTEX_UNLOCK(&mmapstring_lock);
    return -1;
  }
  
  key.data = &str;
  key.len = sizeof(str);

  r = chash_get(ht, &key, &data);
  if (r < 0)
    string = NULL;
  else
    string = data.data;
  
  if (string != NULL) {
    chash_delete(ht, &key, NULL);
    if (chash_count(ht) == 0) {
      chash_free(ht);
      mmapstring_hashtable = NULL;
    }
  }
  
  MUTEX_UNLOCK(&mmapstring_lock);

  if (string != NULL) {
    mmap_string_free(string);
    return 0;
  }
  else
    return -1;
}
Beispiel #12
0
static void pop3driver_cached_uninitialize(mailsession * session)
{
  struct pop3_cached_session_state_data * data;

  data = get_cached_data(session);

  pop3_flags_store_process(data->pop3_flags_directory,
      data->pop3_flags_store);

  mail_flags_store_free(data->pop3_flags_store); 

  chash_free(data->pop3_flags_hash);
  mailsession_free(data->pop3_ancestor);
  free(data);
  
  session->sess_data = NULL;
}
void
shardcache_client_destroy(shardcache_client_t *c)
{
    if (c->thread) {
        __sync_fetch_and_add(&c->quit, 1);
        pthread_join(c->thread, NULL);
        CONDITION_SIGNAL(c->wakeup_cond, c->wakeup_lock);
        CONDITION_DESTROY(c->wakeup_cond);
        MUTEX_DESTROY(c->wakeup_lock);
    }
    queue_destroy(c->async_jobs);
    chash_free(c->chash);
    shardcache_free_nodes(c->shards, c->num_shards);
    free((void *)c->auth);
    connections_pool_destroy(c->connections);
    free(c);
}
Beispiel #14
0
void claws_mailmbox_folder_free(struct claws_mailmbox_folder * folder)
{
  unsigned int i;

  for(i = 0 ; i < carray_count(folder->mb_tab) ; i++) {
    struct claws_mailmbox_msg_info * info;

    info = carray_get(folder->mb_tab, i);
    if (info != NULL)
      claws_mailmbox_msg_info_free(info);
  }

  carray_free(folder->mb_tab);
  
  chash_free(folder->mb_hash);

  free(folder);
}
Beispiel #15
0
struct claws_mailmbox_folder * claws_mailmbox_folder_new(const char * mb_filename)
{
  struct claws_mailmbox_folder * folder;

  folder = malloc(sizeof(* folder));
  if (folder == NULL)
    goto err;

  strncpy(folder->mb_filename, mb_filename, PATH_MAX);

  folder->mb_mtime = (time_t) -1;

  folder->mb_fd = -1;
  folder->mb_read_only = TRUE;
  folder->mb_no_uid = TRUE;

  folder->mb_changed = FALSE;
  folder->mb_deleted_count = 0;
  
  folder->mb_mapping = NULL;
  folder->mb_mapping_size = 0;

  folder->mb_written_uid = 0;
  folder->mb_max_uid = 0;

  folder->mb_hash = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
  if (folder->mb_hash == NULL)
    goto free;
  
  folder->mb_tab = carray_new(128);
  if (folder->mb_tab == NULL)
    goto free_hash;

  return folder;

 free_hash:
  chash_free(folder->mb_hash);
 free:
  free(folder);
 err:
  return NULL;
}
int mailprivacy_smime_init(struct mailprivacy * privacy)
{
  mailprivacy_smime_init_lock();
  certificates = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYALL);
  if (certificates == NULL)
    goto err;

  private_keys = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYALL);
  if (private_keys == NULL)
    goto free_cert;
  
  CAcert_dir[0] = '\0';
  
  return mailprivacy_register(privacy, &smime_protocol);
  
 free_cert:
  chash_free(certificates);
 err:
  return MAIL_ERROR_MEMORY;
}
Beispiel #17
0
static int
mail_build_thread_references(char * default_from,
    struct mailmessage_list * env_list,
    struct mailmessage_tree ** result,
    int use_subject, 
    int (* comp_func)(struct mailmessage_tree **,
        struct mailmessage_tree **))
{
  int r;
  int res;
  chash * msg_id_hash;
  unsigned int cur;
  struct mailmessage_tree * root;
  carray * rootlist;
  carray * msg_list;
  unsigned int i;
  chash * subject_hash;

  msg_id_hash = chash_new(128, CHASH_COPYNONE);
  if (msg_id_hash == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }

  root = mailmessage_tree_new(NULL, (time_t) -1, NULL);
  if (root == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_hash;
  }
  rootlist = root->node_children;

  msg_list = carray_new(128);
  if (msg_list == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_root;
  }

  /* collect message-ID */
  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;
    char * msgid;
    struct mailmessage_tree * env_tree;
    chashdatum hashkey;
    chashdatum hashdata;
    chashdatum hashold;
    time_t date;

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

    if (msg == NULL)
      continue;

    if (msg->msg_fields != NULL) {
      msgid = get_msg_id(msg);

      if (msgid == NULL) {
	msgid = mailimf_get_message_id();
      }
      else {
	hashkey.data = msgid;
	hashkey.len = strlen(msgid);
	
	if (chash_get(msg_id_hash, &hashkey, &hashdata) == 0)
	  msgid = mailimf_get_message_id();
	else
	  msgid = strdup(msgid);
      }
      
      if (msgid == NULL) {
	res = MAIL_ERROR_MEMORY;
	goto free_list;
      }
      
      date = get_date(msg);
      
      env_tree = mailmessage_tree_new(msgid, date, msg);
      if (env_tree == NULL) {
	res = MAIL_ERROR_MEMORY;
	goto free_list;
      }
      
      r = carray_add(msg_list, env_tree, NULL);
      if (r < 0) {
	mailmessage_tree_free(env_tree);
	res = MAIL_ERROR_MEMORY;
	goto free_list;
      }
      
      hashkey.data = msgid;
      hashkey.len = strlen(msgid);
      
      hashdata.data = env_tree;
      hashdata.len = 0;
      
      r = chash_set(msg_id_hash, &hashkey, &hashdata, &hashold);
      if (r < 0) {
	res = MAIL_ERROR_MEMORY;
	goto free_list;
      }
    }
  }

  /* (1) for all messages */

  for(cur = 0 ; cur < carray_count(msg_list) ; cur ++) {
    struct mailmessage_tree * env_tree;
    mailmessage * msg;
    clist * ref;

    env_tree = carray_get(msg_list, cur);

    msg = env_tree->node_msg;

    ref = NULL;
    if (msg != NULL) {
      ref = get_ref(msg);
      if (ref == NULL)
	ref = get_in_reply_to(msg);
    }      

    /* (A) Using the Message IDs in the message's references, link
       the corresponding messages (those whose Message-ID header
       line contains the given reference Message ID) together as
       parent/child.
    */

    if (ref != NULL) {
      /* try to start a tree */

      clistiter * cur_ref;
      chashdatum hashkey;
      chashdatum hashdata;
      chashdatum hashold;
      struct mailmessage_tree * env_cur_tree;
      struct mailmessage_tree * last_env_cur_tree;

      env_cur_tree = NULL;
      for(cur_ref = clist_begin(ref) ; cur_ref != NULL ;
	  cur_ref = clist_next(cur_ref)) {
	char * msgid;

	last_env_cur_tree = env_cur_tree;

	msgid = clist_content(cur_ref);

	hashkey.data = msgid;
	hashkey.len = strlen(msgid);
	
	r = chash_get(msg_id_hash, &hashkey, &hashdata);
	if (r < 0) {
	  /* not found, create a dummy message */
	  msgid = strdup(msgid);
	  if (msgid == NULL) {
	    res = MAIL_ERROR_MEMORY;
	    goto free_list;
	  }

	  env_cur_tree = mailmessage_tree_new(msgid, (time_t) -1, NULL);
	  if (env_cur_tree == NULL) {
	    free(msgid);
	    res = MAIL_ERROR_MEMORY;
	    goto free_list;
	  }

	  r = carray_add(msg_list, env_cur_tree, NULL);
	  if (r < 0) {
	    mailmessage_tree_free(env_cur_tree);
	    res = MAIL_ERROR_MEMORY;
	    goto free_list;
	  }

	  hashkey.data = msgid;
	  hashkey.len = strlen(msgid);
	    
	  hashdata.data = env_cur_tree;
	  hashdata.len = 0;
	  
	  r = chash_set(msg_id_hash, &hashkey, &hashdata, &hashold);
	  if (r < 0) {
	    res = MAIL_ERROR_MEMORY;
	    goto free_list;
	  }
	}
	else {
	  env_cur_tree = hashdata.data;
	}

	if (last_env_cur_tree != NULL) {
	  if (env_cur_tree->node_parent == NULL) {
	    /* make it one child */
	    if (env_cur_tree != last_env_cur_tree) {
	      if (!is_descendant(env_cur_tree, last_env_cur_tree)) {
                /* set parent */
		env_cur_tree->node_parent = last_env_cur_tree;
		r = carray_add(last_env_cur_tree->node_children,
                    env_cur_tree, NULL);
		if (r < 0) {
		  res = MAIL_ERROR_MEMORY;
		  goto free_list;
		}
	      }
	    }
	  }
	}
      }

      /* (B) Create a parent/child link between the last reference
	 (or NIL if there are no references) and the current message.
	 If the current message already has a parent, it is probably
	 the result of a truncated References header line, so break
	 the current parent/child link before creating the new
	 correct one.
      */
      
      last_env_cur_tree = env_cur_tree;
      
      if (last_env_cur_tree != NULL) {
	if (env_tree->node_parent == NULL) {
	  if (last_env_cur_tree != env_tree) {
	    if (!is_descendant(env_tree, last_env_cur_tree)) {
              /* set parent */
	      env_tree->node_parent = last_env_cur_tree;
	      r = carray_add(last_env_cur_tree->node_children, env_tree, NULL);
	      if (r < 0) {
		res = MAIL_ERROR_MEMORY;
		goto free_list;
	      }
	    }
	  }
	}
      }
    }
  }

  chash_free(msg_id_hash);
  msg_id_hash = NULL;

  /* (2) Gather together all of the messages that have no parents
     and make them all children (siblings of one another) of a dummy
     parent (the "root").
  */

  for(cur = 0 ; cur < carray_count(msg_list) ; cur ++) {
    struct mailmessage_tree * env_tree;

    env_tree = carray_get(msg_list, cur);
    if (env_tree->node_parent == NULL) {
      r = carray_add(rootlist, env_tree, NULL);
      if (r < 0) {
	res = MAIL_ERROR_MEMORY;
	goto free_list;
      }
      /* set parent */
      env_tree->node_parent = root;
    }
  }

  carray_free(msg_list);
  msg_list = NULL;

  /* (3) Prune dummy messages from the thread tree.
   */

  cur = 0;
  while (cur < carray_count(rootlist)) {
    r = delete_dummy(rootlist, rootlist, cur, &cur);
    if (r != MAIL_NO_ERROR) {
      res = r;
      goto free_list;
    }
  }

  /* (4) Sort the messages under the root (top-level siblings only)
     by sent date.
  */

  r = mail_thread_sort(root, mailthread_tree_timecomp, FALSE);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto free_list;
  }

  if (use_subject) {

    /* (5) Gather together messages under the root that have the same
       extracted subject text.

       (A) Create a table for associating extracted subjects with
       messages.
    */

    subject_hash = chash_new(128, CHASH_COPYVALUE);
    if (subject_hash == NULL) {
      res = MAIL_ERROR_MEMORY;
      goto free_list;
    }

    /*
      (B) Populate the subject table with one message per
      extracted subject.  For each child of the root:
    */

    for(cur = 0 ; cur < carray_count(rootlist) ; cur ++) {
      struct mailmessage_tree * env_tree;
      chashdatum key;
      chashdatum data;
      char * base_subject;

      env_tree = carray_get(rootlist, cur);

      /*
	(i) Find the subject of this thread by extracting the
	base subject from the current message, or its first child
	if the current message is a dummy.
      */

      r = get_thread_subject(default_from, env_tree, &base_subject);

      /*
	(ii) If the extracted subject is empty, skip this
	message.
      */

      if (r == MAIL_ERROR_SUBJECT_NOT_FOUND) {
	/* no subject found */
	continue;
      }
      else if (r == MAIL_NO_ERROR) {
	if (* base_subject == '\0') {
	  /* subject empty */
	  free(base_subject);
	  continue;
	}
	else {
	  /* do nothing */
	}
      }
      else {
	res = r;
	goto free_subject_hash;
      }

      env_tree->node_base_subject = base_subject;

      /*
	(iii) Lookup the message associated with this extracted
	subject in the table.
      */

      key.data = base_subject;
      key.len = strlen(base_subject);

      r = chash_get(subject_hash, &key, &data);

      if (r < 0) {
	/*
	  (iv) If there is no message in the table with this
	  subject, add the current message and the extracted
	  subject to the subject table.
	*/

	data.data = &cur;
	data.len = sizeof(cur);

	r = chash_set(subject_hash, &key, &data, NULL);
	if (r < 0) {
	  res = MAIL_ERROR_MEMORY;
	  goto free_subject_hash;
	}
      }
      else {
	/*
	  Otherwise, replace the message in the table with the
	  current message if the message in the table is not a
	  dummy AND either of the following criteria are true:
	  The current message is a dummy, OR                  
	  The message in the table is a reply or forward (its
	  original subject contains a subj-refwd part and/or a
	  "(fwd)" subj-trailer) and the current message is not.
	*/
	struct mailmessage_tree * msg_in_table;
	unsigned int * iter_in_table;
	int replace;

	iter_in_table = data.data;
	msg_in_table = carray_get(rootlist, cur);

	replace = FALSE;
	/* message is dummy if info is NULL */
	if (msg_in_table->node_msg != NULL) {

	  if (env_tree->node_msg == NULL)
	    replace = TRUE;
	  else {
	    if (env_tree->node_is_reply && !env_tree->node_is_reply)
	      replace = TRUE;
	  }
	}
 
	if (replace) {
	  data.data = &cur;
	  data.len = sizeof(cur);
	
	  r = chash_set(subject_hash, &key, &data, NULL);
	  if (r < 0) {
	    res = MAIL_ERROR_MEMORY;
	    goto free_subject_hash;
	  }
	}
      }
    }

    /*
      (C) Merge threads with the same subject.  For each child of
      the root:
    */

    cur = 0;
    while (cur < carray_count(rootlist)) {
      struct mailmessage_tree * env_tree;
      chashdatum key;
      chashdatum data;
      struct mailmessage_tree * main_tree;
      unsigned int * main_cur;

      env_tree = carray_get(rootlist, cur);

      if (env_tree == NULL)
        goto next_msg;

      /*
	(i) Find the subject of this thread as in step 4.B.i
	above.
      */
    
      /* already done in tree->node_base_subject */
    
      /*
	(ii) If the extracted subject is empty, skip this
	message.
      */
    
      if (env_tree->node_base_subject == NULL)
	goto next_msg;

      if (* env_tree->node_base_subject == '\0')
	goto next_msg;

      /*
	(iii) Lookup the message associated with this extracted
	subject in the table.
      */

      key.data = env_tree->node_base_subject;
      key.len = strlen(env_tree->node_base_subject);

      r = chash_get(subject_hash, &key, &data);
      if (r < 0)
	goto next_msg;

      /*
	(iv) If the message in the table is the current message,
	skip this message. 
      */
    
      main_cur = data.data;
      if (* main_cur == cur)
	goto next_msg;

      /*
	Otherwise, merge the current message with the one in the
	table using the following rules:
      */

      main_tree = carray_get(rootlist, * main_cur);

      /*
	If both messages are dummies, append the current
	message's children to the children of the message in
	the table (the children of both messages become
	siblings), and then delete the current message.
      */

      if ((env_tree->node_msg == NULL) && (main_tree->node_msg == NULL)) {
        unsigned int old_size;

        old_size = carray_count(main_tree->node_children);

        r = carray_set_size(main_tree->node_children, old_size +
            carray_count(env_tree->node_children));
        if (r < 0) {
          res = MAIL_ERROR_MEMORY;
          goto free_subject_hash;
        }

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

          child = carray_get(env_tree->node_children, i);
          carray_set(main_tree->node_children, old_size + i, child);
          /* set parent */
          child->node_parent = main_tree;
        }
        carray_set_size(env_tree->node_children, 0);
	/* this is the only case where children can be NULL,
	   this is before freeing it */
	mailmessage_tree_free(env_tree);
        carray_delete_fast(rootlist, cur);
      }

      /*
	If the message in the table is a dummy and the current
	message is not, make the current message a child of
	the message in the table (a sibling of it's children).
      */

      else if (main_tree->node_msg == NULL) {
	r = carray_add(main_tree->node_children, env_tree, NULL);
	if (r < 0) {
	  res = MAIL_ERROR_MEMORY;
	  goto free_subject_hash;
	}
        /* set parent */
        env_tree->node_parent = main_tree;

	carray_delete_fast(rootlist, cur);
      }

      /*
	If the current message is a reply or forward and the
	message in the table is not, make the current message
	a child of the message in the table (a sibling of it's
	children).
      */

      else if (env_tree->node_is_reply && !main_tree->node_is_reply) {
	r = carray_add(main_tree->node_children, env_tree, NULL);
	if (r < 0) {
	  res = MAIL_ERROR_MEMORY;
	  goto free_subject_hash;
	}
        /* set parent */
        env_tree->node_parent = main_tree;

	carray_delete_fast(rootlist, cur);
      }

      /*
	Otherwise, create a new dummy message and make both
	the current message and the message in the table
	children of the dummy.  Then replace the message in
	the table with the dummy message.
	Note: Subject comparisons are case-insensitive, as
	described under "Internationalization
	Considerations."
      */

      else {
	struct mailmessage_tree * new_main_tree;
	char * base_subject;
        unsigned int last;

	new_main_tree = mailmessage_tree_new(NULL, (time_t) -1, NULL);
	if (new_main_tree == NULL) {
	  res = MAIL_ERROR_MEMORY;
	  goto free_subject_hash;
	}

	/* main_tree->node_base_subject is never NULL */

	base_subject = strdup(main_tree->node_base_subject);
	if (base_subject == NULL) {
	  mailmessage_tree_free(new_main_tree);
	  res = MAIL_ERROR_MEMORY;
	  goto free_subject_hash;
	}

	new_main_tree->node_base_subject = base_subject;

	r = carray_add(rootlist, new_main_tree, &last);
	if (r < 0) {
	  mailmessage_tree_free(new_main_tree);
	  res = MAIL_ERROR_MEMORY;
	  goto free_subject_hash;
	}

	r = carray_add(new_main_tree->node_children, main_tree, NULL);
	if (r < 0) {
	  res = MAIL_ERROR_MEMORY;
	  goto free_subject_hash;
	}
        /* set parent */
        main_tree->node_parent = new_main_tree;

	carray_delete_fast(rootlist, * main_cur);

	r = carray_add(new_main_tree->node_children, env_tree, NULL);
	if (r < 0) {
	  res = MAIL_ERROR_MEMORY;
	  goto free_subject_hash;
	}
        /* set parent */
        env_tree->node_parent = new_main_tree;

	carray_delete_fast(rootlist, cur);

	data.data = &last;
	data.len = sizeof(last);
      
	r = chash_set(subject_hash, &key, &data, NULL);

	if (r < 0) {
	  res = MAIL_ERROR_MEMORY;
	  goto free_subject_hash;
	}
      }

      continue;

    next_msg:
      cur ++;
      continue;
    }
    
    i = 0;
    for(cur = 0 ; cur < carray_count(rootlist) ; cur ++) {
      struct mailmessage_tree * env_tree;

      env_tree = carray_get(rootlist, cur);
      if (env_tree == NULL)
        continue;
      
      carray_set(rootlist, i, env_tree);
      i ++;
    }
    carray_set_size(rootlist, i);
    
    chash_free(subject_hash);
  }

  /*
    (6) Traverse the messages under the root and sort each set of
    siblings by sent date.  Traverse the messages in such a way
    that the "youngest" set of siblings are sorted first, and the
    "oldest" set of siblings are sorted last (grandchildren are
    sorted before children, etc).

    In the case of an exact match on
    sent date or if either of the Date: headers used in a
    comparison can not be parsed, use the order in which the
    messages appear in the mailbox (that is, by sequence number) to
    determine the order.  In the case of a dummy message (which can
    only occur with top-level siblings), use its first child for
    sorting.
  */

#if 0
  if (comp_func != NULL) {
    r = mail_thread_sort(root, comp_func, TRUE);
    if (r != MAIL_NO_ERROR) {
      res = r;
      goto free_list;
    }
  }
#endif
  if (comp_func == NULL)
    comp_func = mailthread_tree_timecomp;
  
  r = mail_thread_sort(root, comp_func, TRUE);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto free_list;
  }
  
  * result = root;

  return MAIL_NO_ERROR;

 free_subject_hash:
  chash_free(subject_hash);
 free_list:
  if (msg_list != NULL) {
    for(i = 0 ; i < carray_count(msg_list) ; i ++)
      mailmessage_tree_free(carray_get(msg_list, i));
    carray_free(msg_list);
  }
 free_root:
  mailmessage_tree_free_recursive(root);
 free_hash:
  if (msg_id_hash != NULL)
    chash_free(msg_id_hash);
 err:
  return res;
}
Beispiel #18
0
static int expunge_folder(mailsession * session)
{
  int r;
  char key_value[PATH_MAX];
  struct mail_cache_db * maildb;
  carray * msglist;
  unsigned int i;
  struct db_session_state_data * data;
  int res;
  chash * msg_table;
  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;
  }
  
  r = db_get_message_list(maildb, &msglist);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto close_db;
  }
  
  msg_table = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
  if (msg_table == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_msglist;
  }
  
  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_msgtable;
  }
  
  i = 0;
  while (i < carray_count(msglist)) {
    uint32_t num;
    uint32_t * msg;
    chashdatum key;
    chashdatum value;
    struct mail_flags * flags;
    int deleted;
    
    msg = carray_get(msglist, i);
    num = * msg;
    
    deleted = 0;
    snprintf(key_value, sizeof(key_value), "%lu-flags",
        (unsigned long) num);
    r = generic_cache_flags_read(maildb, mmapstr, key_value, &flags);
    if (r == MAIL_NO_ERROR) {
      if ((flags->fl_flags & MAIL_FLAG_DELETED) != 0)
        deleted = 1;
    }
    
    if (!deleted) {
      snprintf(key_value, sizeof(key_value), "%lu", (unsigned long) num);
      key.data = key_value;
      key.len = strlen(key_value);
      chash_set(msg_table, &key, &value, NULL);
      
      snprintf(key_value, sizeof(key_value), "%lu-envelope",
          (unsigned long) num);
      key.data = key_value;
      key.len = strlen(key_value);
      chash_set(msg_table, &key, &value, NULL);
      
      snprintf(key_value, sizeof(key_value), "%lu-flags",
          (unsigned long) num);
      key.data = key_value;
      key.len = strlen(key_value);
      chash_set(msg_table, &key, &value, NULL);
      
      i ++;
    }
    else {
      free(msg);
      carray_delete(msglist, i);
    }
  }
  
  mmap_string_free(mmapstr);
  
  r = mail_cache_db_clean_up(maildb, msg_table);
  
  chash_free(msg_table);
  
  r = db_set_message_list(maildb, msglist);
  
  for(i = 0 ; i < carray_count(msglist) ; i ++) {
    uint32_t * msg;
    
    msg = carray_get(msglist, i);
    free(msg);
  }
  carray_free(msglist);
  
  mail_cache_db_close_unlock(data->db_filename, maildb);
  
  return MAIL_NO_ERROR;

 free_msgtable:
  chash_free(msg_table);
 free_msglist:
  for(i = 0 ; i < carray_count(msglist) ; i ++) {
    uint32_t * msg;
    
    msg = carray_get(msglist, i);
    free(msg);
  }
 close_db:
  mail_cache_db_close_unlock(data->db_filename, maildb);
 err:
  return res;
}
Beispiel #19
0
static void folder_ref_info_free(struct folder_ref_info * ref_info)
{
    chash_free(ref_info->uid_hash);
    chash_free(ref_info->msg_hash);
    free(ref_info);
}
Beispiel #20
0
struct mailmh_folder * mailmh_folder_new(struct mailmh_folder * parent,
					 const char * name)
{
  char * filename;
  char * parent_filename;

  struct mailmh_folder * folder;

  folder = malloc(sizeof(* folder));
  if (folder == NULL)
    goto err;

  if (parent == NULL) {
    filename = strdup(name);
    if (filename == NULL)
      goto free_folder;
  }
  else {
    parent_filename = parent->fl_filename;
    filename = malloc(strlen(parent_filename) + strlen(name) + 2);
    if (filename == NULL)
      goto free_folder;

    strcpy(filename, parent_filename);
    strcat(filename, MAIL_DIR_SEPARATOR_S);
    strcat(filename, name);
  }

  folder->fl_filename = filename;

  folder->fl_name = strdup(name);
  if (folder->fl_name == NULL)
    goto free_filename;

  folder->fl_msgs_tab = carray_new(128);
  if (folder->fl_msgs_tab == NULL)
    goto free_name;

  folder->fl_msgs_hash = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
  if (folder->fl_msgs_hash == NULL)
    goto free_msgs_tab;

  folder->fl_subfolders_tab = carray_new(128);
  if (folder->fl_subfolders_tab == NULL)
    goto free_msgs_hash;

  folder->fl_subfolders_hash = chash_new(128, CHASH_COPYNONE);
  if (folder->fl_subfolders_hash == NULL)
    goto free_subfolders_tab;

  folder->fl_mtime = 0;
  folder->fl_parent = parent;
  folder->fl_max_index = 0;

  return folder;

 free_subfolders_tab:
  carray_free(folder->fl_subfolders_tab);
 free_msgs_hash:
  chash_free(folder->fl_msgs_hash);
 free_msgs_tab:
  carray_free(folder->fl_msgs_tab);
 free_name:
  free(folder->fl_name);
 free_filename:
  free(folder->fl_filename);
 free_folder:
  free(folder);
 err:
  return NULL;
}
Beispiel #21
0
int maildriver_message_cache_clean_up(char * cache_dir,
    struct mailmessage_list * env_list,
    void (* get_uid_from_filename)(char *))
{
  chash * hash_exist;
  DIR * d;
  char cached_filename[PATH_MAX];
  struct dirent * ent;
  char keyname[PATH_MAX];
  unsigned int i;
  int res;
  int r;
  
  /* remove files */
    
  hash_exist = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYALL);
  if (hash_exist == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }
  
  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;
    chashdatum key;
    chashdatum value;
    
    msg = carray_get(env_list->msg_tab, i);
      
    key.data = msg->msg_uid;
    key.len = strlen(msg->msg_uid);
    value.data = NULL;
    value.len = 0;
    r = chash_set(hash_exist, &key, &value, NULL);
    if (r < 0) {
      res = MAIL_ERROR_MEMORY;
      goto free;
    }
  }
  
  d = opendir(cache_dir);
  while ((ent = readdir(d)) != NULL) {
    chashdatum key;
    chashdatum value;
    
    if (strcmp(ent->d_name, ".") == 0)
      continue;
    
    if (strcmp(ent->d_name, "..") == 0)
      continue;
      
    if (strstr(ent->d_name, ".db") != NULL)
      continue;
    
    strncpy(keyname, ent->d_name, sizeof(keyname));
    keyname[sizeof(keyname) - 1] = '\0';
    
    get_uid_from_filename(keyname);
    
    if (* keyname == '\0')
      continue;
    
    key.data = keyname;
    key.len = strlen(keyname);
    
    r = chash_get(hash_exist, &key, &value);
    if (r < 0) {
      snprintf(cached_filename, sizeof(cached_filename),
          "%s/%s", cache_dir, ent->d_name);
      unlink(cached_filename);
    }
  }
  closedir(d);
    
  chash_free(hash_exist);
  
  return MAIL_NO_ERROR;

 free:
  chash_free(hash_exist);
 err:
  return res;
}
Beispiel #22
0
int maildriver_cache_clean_up(struct mail_cache_db * cache_db_env,
    struct mail_cache_db * cache_db_flags,
    struct mailmessage_list * env_list)
{
  chash * hash_exist;
  int res;
  int r;
  char keyname[PATH_MAX];
  unsigned int i;
  
  /* flush cache */
  
  hash_exist = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYALL);
  if (hash_exist == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }

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

    msg = carray_get(env_list->msg_tab, i);
    
    value.data = NULL;
    value.len = 0;
    
    if (cache_db_env != NULL) {
      snprintf(keyname, PATH_MAX, "%s-envelope", msg->msg_uid);
      
      key.data = keyname;
      key.len = strlen(keyname);
      r = chash_set(hash_exist, &key, &value, NULL);
      if (r < 0) {
        res = MAIL_ERROR_MEMORY;
        goto free;
      }
    }
        
    if (cache_db_flags != NULL) {
      snprintf(keyname, PATH_MAX, "%s-flags", msg->msg_uid);
      
      key.data = keyname;
      key.len = strlen(keyname);
      r = chash_set(hash_exist, &key, &value, NULL);
      if (r < 0) {
        res = MAIL_ERROR_MEMORY;
        goto free;
      }
    }
  }
  
  /* clean up */
  if (cache_db_env != NULL)
    mail_cache_db_clean_up(cache_db_env, hash_exist);
  if (cache_db_flags != NULL)
    mail_cache_db_clean_up(cache_db_flags, hash_exist);
  
  chash_free(hash_exist);
  
  return MAIL_NO_ERROR;

 free:
  chash_free(hash_exist);
 err:
  return res;
}
Beispiel #23
0
static void storage_ref_info_free(struct storage_ref_info * ref_info)
{
    chash_free(ref_info->folder_ref_info);
    free(ref_info);
}