about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorPaul Fariello <paul@fariello.eu>2019-04-09 08:22:35 +0320
committerPaul Fariello <paul@fariello.eu>2019-04-10 17:24:56 +0200
commit0bc660400b9a62ae2982b1ec6c374de13ec54477 (patch)
tree9dda2dbd9913e3a90f1fb7da4597b2f7683378c4
parent25eb138d0b1cdddb5b0338f84c5b5f4dca0098ca (diff)
downloadprofani-tty-0bc660400b9a62ae2982b1ec6c374de13ec54477.tar.gz
Automatically starts OMEMO if one identity is trusted
-rw-r--r--src/command/cmd_funcs.c50
-rw-r--r--src/omemo/omemo.c29
-rw-r--r--src/omemo/omemo.h1
-rw-r--r--tests/unittests/omemo/stub_omemo.c6
4 files changed, 80 insertions, 6 deletions
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c
index e1f62ace..b004c4e1 100644
--- a/src/command/cmd_funcs.c
+++ b/src/command/cmd_funcs.c
@@ -2150,17 +2150,67 @@ cmd_msg(ProfWin *window, const char *const command, gchar **args)
         }
         ui_focus_win((ProfWin*)chatwin);
 
+#ifdef HAVE_OMEMO
+#ifndef HAVE_LIBOTR
+        if (omemo_is_trusted_jid(barejid)) {
+            omemo_start_session(barejid);
+            chatwin->is_omemo = TRUE;
+        }
+
+        if (msg) {
+            cl_ev_send_msg(chatwin, msg, NULL);
+        }
+
+        return TRUE;
+#endif
+#endif
+
+#ifdef HAVE_OMEMO
+#ifdef HAVE_LIBOTR
+        if (omemo_is_trusted_jid(barejid) && otr_is_secure(barejid)) {
+            win_println(window, THEME_DEFAULT, '!', "Chat could be either OMEMO or OTR encrypted. Use '/omemo start %s' or '/otr start %s' to start a session.", usr, usr);
+            return TRUE;
+        } else if (omemo_is_trusted_jid(barejid)) {
+            omemo_start_session(barejid);
+            chatwin->is_omemo = TRUE;
+        }
+
         if (msg) {
             cl_ev_send_msg(chatwin, msg, NULL);
         } else {
+            if (otr_is_secure(barejid)) {
+                chatwin_otr_secured(chatwin, otr_is_trusted(barejid));
+            }
+        }
+
+        return TRUE;
+#endif
+#endif
+
+#ifndef HAVE_OMEMO
 #ifdef HAVE_LIBOTR
+        if (msg) {
+            cl_ev_send_msg(chatwin, msg, NULL);
+        } else {
             if (otr_is_secure(barejid)) {
                 chatwin_otr_secured(chatwin, otr_is_trusted(barejid));
             }
+        }
+
+        return TRUE;
 #endif
+#endif
+
+#ifndef HAVE_OMEMO
+#ifndef HAVE_LIBOTR
+        if (msg) {
+            cl_ev_send_msg(chatwin, msg, NULL);
         }
 
         return TRUE;
+#endif
+#endif
+
     }
 }
 
diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c
index 377a2637..1d641e4c 100644
--- a/src/omemo/omemo.c
+++ b/src/omemo/omemo.c
@@ -910,6 +910,21 @@ omemo_known_device_identities(const char *const jid)
 }
 
 gboolean
+omemo_is_trusted_jid(const char *const jid)
+{
+    GHashTable *trusted = g_hash_table_lookup(omemo_ctx.identity_key_store.trusted, jid);
+    if (!trusted) {
+        return FALSE;
+    }
+
+    if (g_hash_table_size(trusted) > 0) {
+        return TRUE;
+    }
+
+    return FALSE;
+}
+
+gboolean
 omemo_is_trusted_identity(const char *const jid, const char *const fingerprint)
 {
     GHashTable *known_identities = g_hash_table_lookup(omemo_ctx.known_devices, jid);
@@ -1262,6 +1277,13 @@ _load_trust(void)
     if (groups) {
         int i;
         for (i = 0; groups[i] != NULL; i++) {
+            GHashTable *trusted;
+
+            trusted = g_hash_table_lookup(omemo_ctx.identity_key_store.trusted, groups[i]);
+            if (!trusted) {
+                trusted = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free);
+                g_hash_table_insert(omemo_ctx.identity_key_store.trusted, strdup(groups[i]), trusted);
+            }
 
             keys = g_key_file_get_keys(omemo_ctx.trust_keyfile, groups[i], NULL, NULL);
             int j;
@@ -1273,11 +1295,6 @@ _load_trust(void)
                 signal_buffer *buffer = signal_buffer_create(key, key_len);
                 g_free(key);
                 uint32_t device_id = strtoul(keys[j], NULL, 10);
-                GHashTable *trusted = g_hash_table_lookup(omemo_ctx.identity_key_store.trusted, groups[i]);
-                if (!trusted) {
-                    trusted = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free);
-                    g_hash_table_insert(omemo_ctx.identity_key_store.trusted, strdup(groups[i]), trusted);
-                }
                 g_hash_table_insert(trusted, GINT_TO_POINTER(device_id), buffer);
             }
             g_strfreev(keys);
@@ -1299,7 +1316,7 @@ _load_sessions(void)
             device_store = g_hash_table_lookup(omemo_ctx.session_store, groups[i]);
             if (!device_store) {
                 device_store = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free);
-                g_hash_table_insert(omemo_ctx.session_store, groups[i], device_store);
+                g_hash_table_insert(omemo_ctx.session_store, strdup(groups[i]), device_store);
             }
 
             char **keys = g_key_file_get_keys(omemo_ctx.sessions_keyfile, groups[i], NULL, NULL);
diff --git a/src/omemo/omemo.h b/src/omemo/omemo.h
index e9186dfa..dcae9266 100644
--- a/src/omemo/omemo.h
+++ b/src/omemo/omemo.h
@@ -40,6 +40,7 @@ char *omemo_own_fingerprint(gboolean formatted);
 void omemo_trust(const char *const jid, const char *const fingerprint);
 void omemo_untrust(const char *const jid, const char *const fingerprint);
 GList *omemo_known_device_identities(const char *const jid);
+gboolean omemo_is_trusted_jid(const char *const jid);
 gboolean omemo_is_trusted_identity(const char *const jid, const char *const fingerprint);
 char *omemo_fingerprint_autocomplete(const char *const search_str, gboolean previous);
 void omemo_fingerprint_autocomplete_reset(void);
diff --git a/tests/unittests/omemo/stub_omemo.c b/tests/unittests/omemo/stub_omemo.c
index 84398378..5b1d02df 100644
--- a/tests/unittests/omemo/stub_omemo.c
+++ b/tests/unittests/omemo/stub_omemo.c
@@ -22,6 +22,12 @@ omemo_format_fingerprint(const char *const fingerprint)
 void omemo_generate_crypto_materials(ProfAccount *account) {}
 
 gboolean
+omemo_is_trusted_jid(const char *const jid)
+{
+    return TRUE;
+}
+
+gboolean
 omemo_is_trusted_identity(const char *const jid, const char *const fingerprint)
 {
     return TRUE;