about summary refs log tree commit diff stats
path: root/src/config
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-05-11 18:13:04 +0100
committerJames Booth <boothj5@gmail.com>2014-05-11 18:13:04 +0100
commit6d955609c56a2a303eb141cbd7ba0cc2a364ef37 (patch)
tree22320f6c509cc47bcc5a5425b361204f5c5af82c /src/config
parent2aa39fc362fd9ab681e86d87eeb7109973416a21 (diff)
downloadprofani-tty-6d955609c56a2a303eb141cbd7ba0cc2a364ef37.tar.gz
Added /otr policy for contacts in account preferences
Diffstat (limited to 'src/config')
-rw-r--r--src/config/accounts.c115
-rw-r--r--src/config/accounts.h1
2 files changed, 116 insertions, 0 deletions
diff --git a/src/config/accounts.c b/src/config/accounts.c
index cf7c4c93..79b9eceb 100644
--- a/src/config/accounts.c
+++ b/src/config/accounts.c
@@ -57,6 +57,8 @@ static gchar *string_keys[] = {
 static void _fix_legacy_accounts(const char * const account_name);
 static void _save_accounts(void);
 static gchar * _get_accounts_file(void);
+static void _remove_from_list(GKeyFile *accounts, const char * const account_name, const char * const key, const char * const contact_jid);
+
 
 static void
 _accounts_load(void)
@@ -399,6 +401,118 @@ _accounts_clear_otr(const char * const account_name)
 }
 
 static void
+_accounts_add_otr_policy(const char * const account_name, const char * const contact_jid, const char * const policy)
+{
+    if (accounts_account_exists(account_name)) {
+        GString *key = g_string_new("otr.");
+        g_string_append(key, policy);
+        gsize length;
+        gchar **list = g_key_file_get_string_list(accounts, account_name, key->str, &length, NULL);
+        GList *glist = NULL;
+
+        // list found
+        if (list != NULL) {
+            int i = 0;
+            for (i = 0; i < length; i++) {
+                // item already in list, exit function
+                if (strcmp(list[i], contact_jid) == 0) {
+                    g_list_free_full(glist, g_free);
+                    g_strfreev(list);
+                    return;
+                }
+                // add item to our g_list
+                glist = g_list_append(glist, strdup(list[i]));
+            }
+
+            // item not found, add to our g_list
+            glist = g_list_append(glist, strdup(contact_jid));
+
+            // create the new list entry
+            const gchar* new_list[g_list_length(glist)+1];
+            GList *curr = glist;
+            i = 0;
+            while (curr != NULL) {
+                new_list[i++] = strdup(curr->data);
+                curr = g_list_next(curr);
+            }
+            new_list[i] = NULL;
+            g_key_file_set_string_list(accounts, account_name, key->str, new_list, g_list_length(glist));
+
+        // list not found
+        } else {
+            const gchar* new_list[2];
+            new_list[0] = strdup(contact_jid);
+            new_list[1] = NULL;
+            g_key_file_set_string_list(accounts, account_name, key->str, new_list, 1);
+        }
+
+        g_strfreev(list);
+        g_list_free_full(glist, g_free);
+        g_string_free(key, TRUE);
+
+        // check for and remove from other lists
+        if (strcmp(policy, "manual") == 0) {
+            _remove_from_list(accounts, account_name, "otr.opportunistic", contact_jid);
+            _remove_from_list(accounts, account_name, "otr.always", contact_jid);
+        }
+        if (strcmp(policy, "opportunistic") == 0) {
+            _remove_from_list(accounts, account_name, "otr.manual", contact_jid);
+            _remove_from_list(accounts, account_name, "otr.always", contact_jid);
+        }
+        if (strcmp(policy, "always") == 0) {
+            _remove_from_list(accounts, account_name, "otr.opportunistic", contact_jid);
+            _remove_from_list(accounts, account_name, "otr.manual", contact_jid);
+        }
+
+        _save_accounts();
+    }
+}
+
+static void
+_remove_from_list(GKeyFile *accounts, const char * const account_name, const char * const key, const char * const contact_jid)
+{
+    gsize length;
+    gchar **list = g_key_file_get_string_list(accounts, account_name, key, &length, NULL);
+
+    if (list != NULL) {
+        int i = 0;
+        GList *glist = NULL;
+        gboolean deleted = FALSE;
+
+        for (i = 0; i < length; i++) {
+            // item found, mark as deleted
+            if (strcmp(list[i], contact_jid) == 0) {
+                deleted = TRUE;
+            } else {
+                // add item to our g_list
+                glist = g_list_append(glist, strdup(list[i]));
+            }
+        }
+
+        if (deleted) {
+            if (g_list_length(glist) == 0) {
+                g_key_file_remove_key(accounts, account_name, key, NULL);
+            } else {
+                // create the new list entry
+                const gchar* new_list[g_list_length(glist)+1];
+                GList *curr = glist;
+                i = 0;
+                while (curr != NULL) {
+                    new_list[i++] = strdup(curr->data);
+                    curr = g_list_next(curr);
+                }
+                new_list[i] = NULL;
+                g_key_file_set_string_list(accounts, account_name, key, new_list, g_list_length(glist));
+            }
+        }
+
+        g_list_free_full(glist, g_free);
+    }
+
+    g_strfreev(list);
+}
+
+static void
 _accounts_set_muc_service(const char * const account_name, const char * const value)
 {
     if (accounts_account_exists(account_name)) {
@@ -699,5 +813,6 @@ accounts_init_module(void)
     accounts_get_priority_for_presence_type = _accounts_get_priority_for_presence_type;
     accounts_clear_password = _accounts_clear_password;
     accounts_clear_otr = _accounts_clear_otr;
+    accounts_add_otr_policy = _accounts_add_otr_policy;
 }
 
diff --git a/src/config/accounts.h b/src/config/accounts.h
index 60f8aa04..e2a75a46 100644
--- a/src/config/accounts.h
+++ b/src/config/accounts.h
@@ -67,5 +67,6 @@ gint (*accounts_get_priority_for_presence_type)(const char * const account_name,
     resource_presence_t presence_type);
 void (*accounts_clear_password)(const char * const account_name);
 void (*accounts_clear_otr)(const char * const account_name);
+void (*accounts_add_otr_policy)(const char * const account_name, const char * const contact_jid, const char * const policy);
 
 #endif