From ea8543ee8db625f87d7e79a2f3d71da458b2743f Mon Sep 17 00:00:00 2001 From: Ampli-fier <22896574+Ampli-fier@users.noreply.github.com> Date: Wed, 14 Feb 2018 22:41:41 +0100 Subject: [PATCH] new function to cleanup contacts --- cmdline/cmdline.c | 5 +++++ src/mrmailbox-private.h | 1 + src/mrmailbox.c | 40 ++++++++++++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/cmdline/cmdline.c b/cmdline/cmdline.c index 101ca4c2..aa2289aa 100644 --- a/cmdline/cmdline.c +++ b/cmdline/cmdline.c @@ -378,6 +378,7 @@ char* mrmailbox_cmdline(mrmailbox_t* mailbox, const char* cmdline) "addcontact [] \n" "contactinfo \n" "delcontact \n" + "cleanupcontacts\n" "======================================Misc.==\n" "event \n" "fileinfo \n" @@ -1022,6 +1023,10 @@ char* mrmailbox_cmdline(mrmailbox_t* mailbox, const char* cmdline) ret = safe_strdup("ERROR: Argument missing."); } } + else if( strcmp(cmd, "cleanupcontacts")==0 ) + { + ret = mrmailbox_cleanup_contacts(mailbox)? COMMAND_SUCCEEDED : COMMAND_FAILED; + } /******************************************************************************* * Misc. diff --git a/src/mrmailbox-private.h b/src/mrmailbox-private.h index 117f90ca..f15f5cbe 100644 --- a/src/mrmailbox-private.h +++ b/src/mrmailbox-private.h @@ -90,6 +90,7 @@ void mrmailbox_add_or_lookup_contacts_by_mailbox_list__(mrmailbox_t* void mrmailbox_add_or_lookup_contacts_by_address_list__(mrmailbox_t* ths, struct mailimf_address_list* adr_list, int origin, mrarray_t* ids, int* check_self); int mrmailbox_get_archived_count__ (mrmailbox_t*); int mrmailbox_reset_tables (mrmailbox_t*, int bits); /* reset tables but leaves server configuration, 1=jobs, 2=e2ee, 8=rest but server config */ +int mrmailbox_cleanup_contacts (mrmailbox_t* ths); /* remove all contacts that are not used (e.g. in a chat, or blocked */ size_t mrmailbox_get_real_contact_cnt__ (mrmailbox_t*); uint32_t mrmailbox_add_or_lookup_contact__ (mrmailbox_t*, const char* display_name /*can be NULL*/, const char* addr_spec, int origin, int* sth_modified); int mrmailbox_get_contact_origin__ (mrmailbox_t*, uint32_t id, int* ret_blocked); diff --git a/src/mrmailbox.c b/src/mrmailbox.c index 01c1426d..7127d0de 100644 --- a/src/mrmailbox.c +++ b/src/mrmailbox.c @@ -1459,12 +1459,12 @@ int mrmailbox_get_archived_count__(mrmailbox_t* mailbox) /** -+ * Reset database tables. This function is called from Core cmdline. -+ * -+ * Argument is a bitmask, executing single or multiple actions in one call. -+ * -+ * e.g. bitmask 7 triggers actions definded with bits 1, 2 and 4. -+ */ + * Reset database tables. This function is called from Core cmdline. + * + * Argument is a bitmask, executing single or multiple actions in one call. + * + * e.g. bitmask 7 triggers actions definded with bits 1, 2 and 4. + */ int mrmailbox_reset_tables(mrmailbox_t* ths, int bits) { if( ths == NULL || ths->m_magic != MR_MAILBOX_MAGIC ) { @@ -1509,6 +1509,34 @@ int mrmailbox_reset_tables(mrmailbox_t* ths, int bits) return 1; } +/** + * Clean up the contacts table. This function is called from Core cmdline. + * + * All contacts not involved in a chat, not blocked and not being a deaddrop + * are removed. + * + * Deleted contacts from the OS address book normally stay in the contacts + * database. With this cleanup, they are also removed, as well as all + * auto-added contacts, unless they are used in a chat or for blocking purpose. + * + */ +int mrmailbox_cleanup_contacts(mrmailbox_t* ths) +{ + if( ths == NULL || ths->m_magic != MR_MAILBOX_MAGIC ) { + return 0; + } + + mrmailbox_log_info(ths, 0, "Cleaning up contacts ..."); + + mrsqlite3_lock(ths->m_sql); + + mrsqlite3_execute__(ths->m_sql, "DELETE FROM contacts WHERE id>" MR_STRINGIFY(MR_CONTACT_ID_LAST_SPECIAL) " AND blocked=0 AND NOT EXISTS (SELECT contact_id FROM chats_contacts where contacts.id = chats_contacts.contact_id) AND NOT EXISTS (select from_id from msgs WHERE msgs.from_id = contacts.id);"); + + mrsqlite3_unlock(ths->m_sql); + + return 1; +} + /** * Find out the version of the Delta Chat core library.