about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-01-11 17:03:01 +0000
committerJames Booth <boothj5@gmail.com>2014-01-11 17:03:01 +0000
commit9daefe170d7fa1f50620acdd0a9073b98ba373f9 (patch)
treea49f4852f9452bc4a594e8145a0fdc12529e455b /src
parent5c13538e6b71cc0d1f5fe49017a99dceecfd007a (diff)
downloadprofani-tty-9daefe170d7fa1f50620acdd0a9073b98ba373f9.tar.gz
Require '/otr start' from both parties before encryption used both ways
Diffstat (limited to 'src')
-rw-r--r--src/command/command.c25
-rw-r--r--src/command/commands.c47
-rw-r--r--src/otr.c24
-rw-r--r--src/otr.h2
-rw-r--r--src/server_events.c14
-rw-r--r--src/ui/core.c16
-rw-r--r--src/ui/ui.h2
-rw-r--r--src/ui/window.c1
-rw-r--r--src/ui/window.h1
9 files changed, 114 insertions, 18 deletions
diff --git a/src/command/command.c b/src/command/command.c
index 3f220cee..1a7c23da 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -1140,10 +1140,24 @@ cmd_execute_default(const char * const inp)
                 ui_current_print_line("You are not currently connected.");
             } else {
 #ifdef HAVE_LIBOTR
-                char *encrypted = otr_encrypt_message(recipient, inp);
-                if (encrypted != NULL) {
-                    message_send(encrypted, recipient);
-                    otr_free_message(encrypted);
+                if (ui_current_win_is_otr()) {
+                    char *encrypted = otr_encrypt_message(recipient, inp);
+                    if (encrypted != NULL) {
+                        message_send(encrypted, recipient);
+                        otr_free_message(encrypted);
+                        if (prefs_get_boolean(PREF_CHLOG)) {
+                            const char *jid = jabber_get_fulljid();
+                            Jid *jidp = jid_create(jid);
+                            chat_log_chat(jidp->barejid, recipient, inp, PROF_OUT_LOG, NULL);
+                            jid_destroy(jidp);
+                        }
+
+                        ui_outgoing_msg("me", recipient, inp);
+                    } else {
+                        cons_show_error("Failed to send message.");
+                    }
+                } else {
+                    message_send(inp, recipient);
                     if (prefs_get_boolean(PREF_CHLOG)) {
                         const char *jid = jabber_get_fulljid();
                         Jid *jidp = jid_create(jid);
@@ -1152,8 +1166,6 @@ cmd_execute_default(const char * const inp)
                     }
 
                     ui_outgoing_msg("me", recipient, inp);
-                } else {
-                    cons_show_error("Failed to send message.");
                 }
 #else
                 message_send(inp, recipient);
@@ -1166,7 +1178,6 @@ cmd_execute_default(const char * const inp)
 
                 ui_outgoing_msg("me", recipient, inp);
 #endif
-
             }
             break;
 
diff --git a/src/command/commands.c b/src/command/commands.c
index 649c084e..6b0b38f9 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -42,6 +42,8 @@
 #include "tools/parser.h"
 #include "tools/tinyurl.h"
 #include "ui/ui.h"
+#include "ui/window.h"
+#include "ui/windows.h"
 #include "xmpp/xmpp.h"
 #include "xmpp/bookmark.h"
 
@@ -917,10 +919,24 @@ cmd_msg(gchar **args, struct cmd_help_t help)
         }
         if (msg != NULL) {
 #ifdef HAVE_LIBOTR
-            char *encrypted = otr_encrypt_message(usr_jid, msg);
-            if (encrypted != NULL) {
-                message_send(encrypted, usr_jid);
-                otr_free_message(encrypted);
+            if (ui_current_win_is_otr()) {
+                char *encrypted = otr_encrypt_message(usr_jid, msg);
+                if (encrypted != NULL) {
+                    message_send(encrypted, usr_jid);
+                    otr_free_message(encrypted);
+                    ui_outgoing_msg("me", usr_jid, msg);
+
+                    if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) {
+                        const char *jid = jabber_get_fulljid();
+                        Jid *jidp = jid_create(jid);
+                        chat_log_chat(jidp->barejid, usr_jid, msg, PROF_OUT_LOG, NULL);
+                        jid_destroy(jidp);
+                    }
+                } else {
+                    cons_show_error("Failed to encrypt and send message,");
+                }
+            } else {
+                message_send(msg, usr_jid);
                 ui_outgoing_msg("me", usr_jid, msg);
 
                 if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) {
@@ -929,10 +945,8 @@ cmd_msg(gchar **args, struct cmd_help_t help)
                     chat_log_chat(jidp->barejid, usr_jid, msg, PROF_OUT_LOG, NULL);
                     jid_destroy(jidp);
                 }
-            } else {
-                cons_show_error("Failed to send message,");
             }
-
+            return TRUE;
 #else
             message_send(msg, usr_jid);
             ui_outgoing_msg("me", usr_jid, msg);
@@ -942,9 +956,10 @@ cmd_msg(gchar **args, struct cmd_help_t help)
                 Jid *jidp = jid_create(jid);
                 chat_log_chat(jidp->barejid, usr_jid, msg, PROF_OUT_LOG, NULL);
                 jid_destroy(jidp);
+            }
+            return TRUE;
 #endif
 
-            return TRUE;
         } else {
             const char * jid = NULL;
 
@@ -2300,6 +2315,22 @@ cmd_otr(gchar **args, struct cmd_help_t help)
         char *fingerprint = otr_get_fingerprint();
         cons_show("Your fingerprint: %s", fingerprint);
         return TRUE;
+    } else if (strcmp(args[0], "start") == 0) {
+        win_type_t win_type = ui_current_win_type();
+
+        if (win_type != WIN_CHAT) {
+            ui_current_print_line("You must be in a regular chat window to start an OTR session.");
+        } else if (ui_current_win_is_otr()) {
+            ui_current_print_line("You are already in an OTR session.");
+        } else {
+            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");
+                ui_current_set_otr(TRUE);
+            }
+        }
+        return TRUE;
     } else {
         cons_show("Usage: %s", help.usage);
         return TRUE;
diff --git a/src/otr.c b/src/otr.c
index b5be978d..dabf944e 100644
--- a/src/otr.c
+++ b/src/otr.c
@@ -301,6 +301,7 @@ otr_keygen(ProfAccount *account)
         g_string_free(basedir, TRUE);
         g_string_free(keysfilename, TRUE);
         log_error("Failed to load private key");
+        data_loaded = FALSE;
         return;
     }
 
@@ -309,15 +310,24 @@ otr_keygen(ProfAccount *account)
         g_string_free(basedir, TRUE);
         g_string_free(keysfilename, TRUE);
         log_error("Failed to load fingerprints");
+        data_loaded = FALSE;
         return;
     }
 
+    data_loaded = TRUE;
+
     g_string_free(basedir, TRUE);
     g_string_free(keysfilename, TRUE);
     g_string_free(fpsfilename, TRUE);
     return;
 }
 
+gboolean
+otr_key_loaded(void)
+{
+    return data_loaded;
+}
+
 char *
 otr_get_fingerprint(void)
 {
@@ -361,12 +371,20 @@ otr_decrypt_message(const char * const from, const char * const message)
 {
     cons_debug("Decrypting message: %s", message);
     char *decrypted = NULL;
-    int ignore_mesage = otrl_message_receiving(user_state, &ops, NULL, jid, "xmpp", from, message, &decrypted, 0, NULL, NULL);
-    if (!ignore_mesage) {
+    int result = otrl_message_receiving(user_state, &ops, NULL, jid, "xmpp", from, message, &decrypted, 0, NULL, NULL);
+
+    // internal libotr message, ignore
+    if (result == 1) {
+        return NULL;
+
+    // message was decrypted, return to user
+    } else if (decrypted != NULL) {
         cons_debug("Decrypted message: %s", decrypted);
         return decrypted;
+
+    // normal non OTR message
     } else {
-        return NULL;
+        return strdup(message);
     }
 }
 
diff --git a/src/otr.h b/src/otr.h
index 924a18f6..59477ab0 100644
--- a/src/otr.h
+++ b/src/otr.h
@@ -29,6 +29,8 @@ void otr_init(void);
 void otr_on_connect(ProfAccount *account);
 void otr_keygen(ProfAccount *account);
 
+gboolean otr_key_loaded(void);
+
 char * otr_get_fingerprint(void);
 
 char * otr_encrypt_message(const char * const to, const char * const message);
diff --git a/src/server_events.c b/src/server_events.c
index e83fbf27..5e478a51 100644
--- a/src/server_events.c
+++ b/src/server_events.c
@@ -171,6 +171,7 @@ handle_duck_result(const char * const result)
 void
 handle_incoming_message(char *from, char *message, gboolean priv)
 {
+#ifdef HAVE_LIBOTR
     char *newmessage;
     if (!priv) {
         newmessage = otr_decrypt_message(from, message);
@@ -195,6 +196,19 @@ handle_incoming_message(char *from, char *message, gboolean priv)
 
     if (!priv)
         otr_free_message(newmessage);
+#else
+    ui_incoming_msg(from, message, NULL, priv);
+    ui_current_page_off();
+
+    if (prefs_get_boolean(PREF_CHLOG) && !priv) {
+        Jid *from_jid = jid_create(from);
+        const char *jid = jabber_get_fulljid();
+        Jid *jidp = jid_create(jid);
+        chat_log_chat(jidp->barejid, from_jid->barejid, message, PROF_IN_LOG, NULL);
+        jid_destroy(jidp);
+        jid_destroy(from_jid);
+    }
+#endif
 }
 
 void
diff --git a/src/ui/core.c b/src/ui/core.c
index afda184a..4ff43ff1 100644
--- a/src/ui/core.c
+++ b/src/ui/core.c
@@ -693,6 +693,20 @@ _ui_current_win_type(void)
     return current->type;
 }
 
+static gboolean
+_ui_current_win_is_otr(void)
+{
+    ProfWin *current = wins_get_current();
+    return current->is_otr;
+}
+
+static void
+_ui_current_set_otr(gboolean value)
+{
+    ProfWin *current = wins_get_current();
+    current->is_otr = value;
+}
+
 static int
 _ui_current_win_index(void)
 {
@@ -1609,4 +1623,6 @@ ui_init_module(void)
     ui_unread = _ui_unread;
     ui_win_unread = _ui_win_unread;
     ui_ask_password = _ui_ask_password;
+    ui_current_win_is_otr = _ui_current_win_is_otr;
+    ui_current_set_otr = _ui_current_set_otr;
 }
diff --git a/src/ui/ui.h b/src/ui/ui.h
index e5d4118d..546f606a 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -77,6 +77,8 @@ void (*ui_close_current)(void);
 void (*ui_clear_current)(void);
 win_type_t (*ui_current_win_type)(void);
 int (*ui_current_win_index)(void);
+gboolean (*ui_current_win_is_otr)(void);
+void (*ui_current_set_otr)(gboolean value);
 char* (*ui_current_recipient)(void);
 void (*ui_current_print_line)(const char * const msg, ...);
 void (*ui_current_error_line)(const char * const msg);
diff --git a/src/ui/window.c b/src/ui/window.c
index daf9bef0..c144dc06 100644
--- a/src/ui/window.c
+++ b/src/ui/window.c
@@ -54,6 +54,7 @@ win_create(const char * const title, int cols, win_type_t type)
     new_win->unread = 0;
     new_win->history_shown = 0;
     new_win->type = type;
+    new_win->is_otr = FALSE;
     scrollok(new_win->win, TRUE);
 
     return new_win;
diff --git a/src/ui/window.h b/src/ui/window.h
index 4c97429a..4a65488e 100644
--- a/src/ui/window.h
+++ b/src/ui/window.h
@@ -48,6 +48,7 @@ typedef struct prof_win_t {
     char *from;
     WINDOW *win;
     win_type_t type;
+    gboolean is_otr;
     int y_pos;
     int paged;
     int unread;