about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-01-11 21:44:24 +0000
committerJames Booth <boothj5@gmail.com>2014-01-11 21:44:24 +0000
commitf35e485bd4d5b6bfa7997b48f5b1f9c13576e26b (patch)
treea0d62dc5fa9ef9991e1619e29d4306feb4d25f46
parent5a7eba518d4193696051601e77d75dc3d9051567 (diff)
downloadprofani-tty-f35e485bd4d5b6bfa7997b48f5b1f9c13576e26b.tar.gz
Handle window creation for incoming and outgoing OTR sessions
-rw-r--r--src/command/command.c2
-rw-r--r--src/command/commands.c6
-rw-r--r--src/otr.c20
-rw-r--r--src/otr.h3
-rw-r--r--src/ui/core.c29
-rw-r--r--src/ui/ui.h1
6 files changed, 53 insertions, 8 deletions
diff --git a/src/command/command.c b/src/command/command.c
index eaa72197..9d028801 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -1141,7 +1141,7 @@ cmd_execute_default(const char * const inp)
                 ui_current_print_line("You are not currently connected.");
             } else {
 #ifdef HAVE_LIBOTR
-                if (ui_current_win_is_otr()) {
+                if (otr_is_secure(recipient)) {
                     char *encrypted = otr_encrypt_message(recipient, inp);
                     if (encrypted != NULL) {
                         message_send(encrypted, recipient);
diff --git a/src/command/commands.c b/src/command/commands.c
index a7bf7087..bb18ff2f 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -919,7 +919,7 @@ cmd_msg(gchar **args, struct cmd_help_t help)
         }
         if (msg != NULL) {
 #ifdef HAVE_LIBOTR
-            if (ui_current_win_is_otr()) {
+            if (otr_is_secure(usr_jid)) {
                 char *encrypted = otr_encrypt_message(usr_jid, msg);
                 if (encrypted != NULL) {
                     message_send(encrypted, usr_jid);
@@ -2341,12 +2341,8 @@ cmd_otr(gchar **args, struct cmd_help_t help)
             if (!otr_key_loaded()) {
                 ui_current_print_line("You have not generated or loaded a private key, use '/otr gen'");
             } else {
-                ui_current_print_line("Starting OTR session");
                 char *recipient = ui_current_recipient();
                 message_send("?OTR?", recipient);
-                ui_current_set_otr(TRUE);
-                // refresh to show OTR in titlebar
-                ui_switch_win(ui_current_win_index());
             }
         }
         return TRUE;
diff --git a/src/otr.c b/src/otr.c
index a0274e3a..7e25bdf5 100644
--- a/src/otr.c
+++ b/src/otr.c
@@ -120,6 +120,7 @@ static void
 cb_gone_secure(void *opdata, ConnContext *context)
 {
 //    cons_debug("cb_gone_secure");
+    ui_gone_secure(context->username);
 }
 
 static void
@@ -332,6 +333,23 @@ otr_key_loaded(void)
     return data_loaded;
 }
 
+gboolean
+otr_is_secure(const char * const recipient)
+{
+    ConnContext *context = otrl_context_find(user_state, recipient, jid, "xmpp",
+        0, NULL, NULL, NULL);
+
+    if (context == NULL) {
+        return FALSE;
+    }
+
+    if (context->msgstate != OTRL_MSGSTATE_ENCRYPTED) {
+        return FALSE;
+    } else {
+        return TRUE;
+    }
+}
+
 char *
 otr_get_my_fingerprint(void)
 {
@@ -343,7 +361,7 @@ otr_get_my_fingerprint(void)
 }
 
 char *
-otr_get_their_fingerprint(char *recipient)
+otr_get_their_fingerprint(const char * const recipient)
 {
     ConnContext *context = otrl_context_find(user_state, recipient, jid, "xmpp",
         0, NULL, NULL, NULL);
diff --git a/src/otr.h b/src/otr.h
index 5008b2d3..0331a5a3 100644
--- a/src/otr.h
+++ b/src/otr.h
@@ -30,9 +30,10 @@ void otr_on_connect(ProfAccount *account);
 void otr_keygen(ProfAccount *account);
 
 gboolean otr_key_loaded(void);
+gboolean otr_is_secure(const char * const recipient);
 
 char * otr_get_my_fingerprint(void);
-char * otr_get_their_fingerprint(char *recipient);
+char * otr_get_their_fingerprint(const char * const recipient);
 
 char * otr_encrypt_message(const char * const to, const char * const message);
 char * otr_decrypt_message(const char * const from, const char * const message);
diff --git a/src/ui/core.c b/src/ui/core.c
index 74a4d66e..397c6a0e 100644
--- a/src/ui/core.c
+++ b/src/ui/core.c
@@ -48,6 +48,7 @@
 #include "jid.h"
 #include "log.h"
 #include "muc.h"
+#include "otr.h"
 #include "ui/ui.h"
 #include "ui/window.h"
 #include "ui/windows.h"
@@ -245,6 +246,11 @@ _ui_incoming_msg(const char * const from, const char * const message,
     ProfWin *window = wins_get_by_recipient(from);
     if (window == NULL) {
         window = wins_new(from, win_type);
+#ifdef HAVE_LIBOTR
+        if (otr_is_secure(from)) {
+            window->is_otr = TRUE;
+        }
+#endif
         win_created = TRUE;
     }
 
@@ -582,6 +588,23 @@ _ui_next_win(void)
 }
 
 static void
+_ui_gone_secure(char *recipient)
+{
+    ProfWin *window = wins_get_by_recipient(recipient);
+    if (window != NULL) {
+        window->is_otr = TRUE;
+
+        if (wins_is_current(window)) {
+            GString *recipient_str = _get_recipient_string(window);
+            title_bar_set_recipient(recipient_str->str);
+            g_string_free(recipient_str, TRUE);
+            title_bar_draw();
+            wins_refresh_current();
+        }
+    }
+}
+
+static void
 _ui_previous_win(void)
 {
     ui_current_page_off();
@@ -966,6 +989,11 @@ _ui_outgoing_msg(const char * const from, const char * const to,
             window = wins_new(to, WIN_PRIVATE);
         } else {
             window = wins_new(to, WIN_CHAT);
+#ifdef HAVE_LIBOTR
+            if (otr_is_secure(to)) {
+                window->is_otr = TRUE;
+            }
+#endif
         }
 
         jid_destroy(jid);
@@ -1634,4 +1662,5 @@ ui_init_module(void)
     ui_ask_password = _ui_ask_password;
     ui_current_win_is_otr = _ui_current_win_is_otr;
     ui_current_set_otr = _ui_current_set_otr;
+    ui_gone_secure = _ui_gone_secure;
 }
diff --git a/src/ui/ui.h b/src/ui/ui.h
index 546f606a..6f2e6c4f 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -61,6 +61,7 @@ void (*ui_handle_special_keys)(const wint_t * const ch, const char * const inp,
 void (*ui_switch_win)(const int i);
 void (*ui_next_win)(void);
 void (*ui_previous_win)(void);
+void (*ui_gone_secure)(char *recipient);
 unsigned long (*ui_get_idle_time)(void);
 void (*ui_reset_idle_time)(void);
 void (*ui_new_chat_win)(const char * const to);