about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-04-29 22:59:44 +0100
committerJames Booth <boothj5@gmail.com>2015-04-29 22:59:44 +0100
commit665c34414d7534016fb8136cffbadd35793dec7b (patch)
tree0fc82d254101cfa66a0a53edf061db7f3d958762
parent0df8b8beff8aeeae072beee8fc3ed69f9d132fb2 (diff)
downloadprofani-tty-665c34414d7534016fb8136cffbadd35793dec7b.tar.gz
Return result on OTR message sending
-rw-r--r--src/command/commands.c30
-rw-r--r--src/event/client_events.c18
-rw-r--r--src/event/client_events.h2
-rw-r--r--src/event/ui_events.c2
-rw-r--r--src/event/ui_events.h2
-rw-r--r--src/otr/otr.c18
-rw-r--r--src/otr/otr.h10
-rw-r--r--src/ui/core.c26
-rw-r--r--src/ui/ui.h2
-rw-r--r--tests/otr/stub_otr.c11
-rw-r--r--tests/ui/stub_ui.c2
11 files changed, 74 insertions, 49 deletions
diff --git a/src/command/commands.c b/src/command/commands.c
index f583fa44..3ef37fc1 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -110,7 +110,7 @@ cmd_execute_default(const char * inp)
     case WIN_CHAT:
     {
         ProfChatWin *chatwin = wins_get_current_chat();
-        cl_ev_send_msg(chatwin->barejid, inp);
+        cl_ev_send_msg(chatwin, inp);
         break;
     }
     case WIN_PRIVATE:
@@ -1327,23 +1327,23 @@ cmd_msg(gchar **args, struct cmd_help_t help)
             barejid = usr;
         }
 
+        ProfChatWin *chatwin = wins_get_chat(barejid);
+        if (!chatwin) {
+            chatwin = ui_ev_new_chat_win(barejid);
+        }
+        ui_ev_focus_win((ProfWin*)chatwin);
+
         if (msg) {
-            cl_ev_send_msg(barejid, msg);
-            return TRUE;
+            cl_ev_send_msg(chatwin, msg);
         } else {
-            ProfWin *window = (ProfWin*)wins_get_chat(barejid);
-            if (!window) {
-                window = ui_ev_new_chat_win(barejid);
-            }
-            ui_ev_focus_win(window);
-
 #ifdef HAVE_LIBOTR
             if (otr_is_secure(barejid)) {
                 ui_gone_secure(barejid, otr_is_trusted(barejid));
             }
 #endif
-            return TRUE;
         }
+
+        return TRUE;
     }
 }
 
@@ -3141,7 +3141,7 @@ cmd_tiny(gchar **args, struct cmd_help_t help)
     case WIN_CHAT:
     {
         ProfChatWin *chatwin = wins_get_current_chat();
-        cl_ev_send_msg(chatwin->barejid, tiny);
+        cl_ev_send_msg(chatwin, tiny);
         break;
     }
     case WIN_PRIVATE:
@@ -4169,11 +4169,11 @@ cmd_otr(gchar **args, struct cmd_help_t help)
                 barejid = contact;
             }
 
-            ProfWin *window = (ProfWin*)wins_get_chat(barejid);
-            if (!window) {
-                window = ui_ev_new_chat_win(barejid);
+            ProfChatWin *chatwin = wins_get_chat(barejid);
+            if (!chatwin) {
+                chatwin = ui_ev_new_chat_win(barejid);
             }
-            ui_ev_focus_win(window);
+            ui_ev_focus_win((ProfWin*)chatwin);
 
             if (ui_current_win_is_otr()) {
                 ui_current_print_formatted_line('!', 0, "You are already in an OTR session.");
diff --git a/src/event/client_events.c b/src/event/client_events.c
index 70de5f6c..af8b833c 100644
--- a/src/event/client_events.c
+++ b/src/event/client_events.c
@@ -37,6 +37,7 @@
 #include "config.h"
 #include "log.h"
 #include "ui/ui.h"
+#include "ui/windows.h"
 #include "xmpp/xmpp.h"
 #ifdef HAVE_LIBOTR
 #include "otr/otr.h"
@@ -60,14 +61,21 @@ cl_ev_connect_account(ProfAccount *account)
 }
 
 void
-cl_ev_send_msg(const char * const barejid, const char * const msg)
+cl_ev_send_msg(ProfChatWin *chatwin, const char * const msg)
 {
+    chat_state_active(chatwin->state);
+
 #ifdef HAVE_LIBOTR
-    otr_on_message_send(barejid, msg);
+    prof_otrsendres_t res = otr_on_message_send(chatwin->barejid, msg);
+    if (res != PROF_OTRSUCCESS) {
+        char *errmsg = otr_senderror_str(res);
+        // TODO reference passed window
+        ui_current_error_line(errmsg);
+    }
 #else
-    char *id = message_send_chat(barejid, msg);
-    chat_log_msg_out(barejid, msg);
-    ui_outgoing_chat_msg(barejid, msg, id);
+    char *id = message_send_chat(chatwin->barejid, msg);
+    chat_log_msg_out(chatwin->barejid, msg);
+    ui_outgoing_chat_msg(chatwin->barejid, msg, id);
     free(id);
 #endif
 }
diff --git a/src/event/client_events.h b/src/event/client_events.h
index fcf26523..c074b230 100644
--- a/src/event/client_events.h
+++ b/src/event/client_events.h
@@ -38,7 +38,7 @@
 jabber_conn_status_t cl_ev_connect_jid(const char * const jid, const char * const passwd, const char * const altdomain, const int port);
 jabber_conn_status_t cl_ev_connect_account(ProfAccount *account);
 
-void cl_ev_send_msg(const char * const barejid, const char * const msg);
+void cl_ev_send_msg(ProfChatWin *chatwin, const char * const msg);
 void cl_ev_send_muc_msg(const char * const roomjid, const char * const msg);
 void cl_ev_send_priv_msg(const char * const fulljid, const char * const msg);
 
diff --git a/src/event/ui_events.c b/src/event/ui_events.c
index 5e16cc43..2ad7562d 100644
--- a/src/event/ui_events.c
+++ b/src/event/ui_events.c
@@ -40,7 +40,7 @@ ui_ev_focus_win(ProfWin *win)
     ui_switch_win(win);
 }
 
-ProfWin*
+ProfChatWin*
 ui_ev_new_chat_win(const char * const barejid)
 {
     return ui_new_chat_win(barejid);
diff --git a/src/event/ui_events.h b/src/event/ui_events.h
index 9e2dc7ee..b7075e61 100644
--- a/src/event/ui_events.h
+++ b/src/event/ui_events.h
@@ -36,6 +36,6 @@
 #define UI_EVENTS_H
 
 void ui_ev_focus_win(ProfWin *win);
-ProfWin* ui_ev_new_chat_win(const char * const barejid);
+ProfChatWin* ui_ev_new_chat_win(const char * const barejid);
 
 #endif
\ No newline at end of file
diff --git a/src/otr/otr.c b/src/otr/otr.c
index 7507cd56..46ad491c 100644
--- a/src/otr/otr.c
+++ b/src/otr/otr.c
@@ -313,7 +313,7 @@ otr_on_message_recv(const char * const barejid, const char * const resource, con
     otr_free_message(decrypted);
 }
 
-void
+prof_otrsendres_t
 otr_on_message_send(const char * const barejid, const char * const message)
 {
     char *id = NULL;
@@ -328,11 +328,11 @@ otr_on_message_send(const char * const barejid, const char * const message)
             ui_outgoing_chat_msg(barejid, message, id);
             otr_free_message(encrypted);
         } else {
-            cons_show_error("Failed to encrypt and send message.");
+            return PROF_OTRENCFAIL;
         }
 
     } else if (policy == PROF_OTRPOLICY_ALWAYS) {
-        cons_show_error("Failed to send message. OTR policy set to: always");
+        return PROF_OTRPOLICYFAIL;
 
     } else if (policy == PROF_OTRPOLICY_OPPORTUNISTIC) {
         char *otr_tagged_msg = otr_tag_message(message);
@@ -348,6 +348,8 @@ otr_on_message_send(const char * const barejid, const char * const message)
     }
 
     free(id);
+
+    return PROF_OTRSUCCESS;
 }
 
 void
@@ -741,6 +743,16 @@ otr_decrypt_message(const char * const from, const char * const message, gboolea
     }
 }
 
+char*
+otr_senderror_str(prof_otrsendres_t res)
+{
+    switch (res) {
+    case PROF_OTRENCFAIL:    return "Failed to encrypt and send message.";
+    case PROF_OTRPOLICYFAIL: return "Failed to send message. OTR policy set to: always";
+    default:                 return "Unknown OTR error.";
+    }
+}
+
 void
 otr_free_message(char *message)
 {
diff --git a/src/otr/otr.h b/src/otr/otr.h
index 8e1d22df..6f1103df 100644
--- a/src/otr/otr.h
+++ b/src/otr/otr.h
@@ -46,6 +46,12 @@ typedef enum {
     PROF_OTRPOLICY_ALWAYS
 } prof_otrpolicy_t;
 
+typedef enum {
+    PROF_OTRENCFAIL,
+    PROF_OTRPOLICYFAIL,
+    PROF_OTRSUCCESS
+} prof_otrsendres_t;
+
 OtrlUserState otr_userstate(void);
 OtrlMessageAppOps* otr_messageops(void);
 GHashTable* otr_smpinitators(void);
@@ -58,7 +64,7 @@ void otr_poll(void);
 void otr_on_connect(ProfAccount *account);
 
 void otr_on_message_recv(const char * const barejid, const char * const resource, const char * const message);
-void otr_on_message_send(const char * const barejid, const char * const message);
+prof_otrsendres_t otr_on_message_send(const char * const barejid, const char * const message);
 
 void otr_keygen(ProfAccount *account);
 
@@ -88,4 +94,6 @@ void otr_free_message(char *message);
 
 prof_otrpolicy_t otr_get_policy(const char * const recipient);
 
+char* otr_senderror_str(prof_otrsendres_t res);
+
 #endif
diff --git a/src/ui/core.c b/src/ui/core.c
index a4ed45f3..40f9dbbf 100644
--- a/src/ui/core.c
+++ b/src/ui/core.c
@@ -1395,7 +1395,7 @@ ui_open_xmlconsole_win(void)
     }
 }
 
-ProfWin*
+ProfChatWin*
 ui_new_chat_win(const char * const barejid)
 {
     ProfWin *window = wins_new_chat(barejid);
@@ -1421,7 +1421,7 @@ ui_new_chat_win(const char * const barejid)
         }
     }
 
-    return window;
+    return chatwin;
 }
 
 void
@@ -1429,40 +1429,28 @@ ui_outgoing_chat_msg(const char * const barejid, const char * const message, cha
 {
     ProfWin *window = (ProfWin*)wins_get_chat(barejid);
 
-    // create new window
-    if (!window) {
-        window = ui_new_chat_win(barejid);
-    }
-
-    ProfChatWin *chatwin = (ProfChatWin*)window;
-    chat_state_active(chatwin->state);
-
     if (prefs_get_boolean(PREF_RECEIPTS_REQUEST) && id) {
         win_print_with_receipt(window, '-', NULL, 0, THEME_TEXT_ME, "me", message, id);
     } else {
         win_print(window, '-', NULL, 0, THEME_TEXT_ME, "me", message);
     }
-
-    int num = wins_get_num(window);
-    ui_switch_win_num(num);
 }
 
 void
 ui_outgoing_chat_msg_carbon(const char * const barejid, const char * const message)
 {
-    ProfWin *window = (ProfWin*)wins_get_chat(barejid);
+    ProfChatWin *chatwin = wins_get_chat(barejid);
 
     // create new window
-    if (!window) {
-        window = ui_new_chat_win(barejid);
+    if (!chatwin) {
+        chatwin = ui_new_chat_win(barejid);
     }
 
-    ProfChatWin *chatwin = (ProfChatWin*)window;
     chat_state_active(chatwin->state);
 
-    win_print(window, '-', NULL, 0, THEME_TEXT_ME, "me", message);
+    win_print((ProfWin*)chatwin, '-', NULL, 0, THEME_TEXT_ME, "me", message);
 
-    int num = wins_get_num(window);
+    int num = wins_get_num((ProfWin*)chatwin);
     status_bar_active(num);
 }
 
diff --git a/src/ui/ui.h b/src/ui/ui.h
index d5e9c28c..11457988 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -88,7 +88,7 @@ void ui_handle_otr_error(const char * const barejid, const char * const message)
 unsigned long ui_get_idle_time(void);
 void ui_reset_idle_time(void);
 void ui_new_private_win(const char * const fulljid);
-ProfWin* ui_new_chat_win(const char * const barejid);
+ProfChatWin* ui_new_chat_win(const char * const barejid);
 void ui_print_system_msg_from_recipient(const char * const barejid, const char *message);
 gint ui_unread(void);
 void ui_close_connected_win(int index);
diff --git a/tests/otr/stub_otr.c b/tests/otr/stub_otr.c
index eb676877..9815957f 100644
--- a/tests/otr/stub_otr.c
+++ b/tests/otr/stub_otr.c
@@ -42,7 +42,10 @@ char* otr_start_query(void)
 void otr_poll(void) {}
 void otr_on_connect(ProfAccount *account) {}
 void otr_on_message_recv(const char * const barejid, const char * const resource, const char * const message) {}
-void otr_on_message_send(const char * const barejid, const char * const message) {}
+prof_otrsendres_t otr_on_message_send(const char * const barejid, const char * const message)
+{
+    return PROF_OTRSUCCESS;
+}
 
 void otr_keygen(ProfAccount *account)
 {
@@ -106,3 +109,9 @@ prof_otrpolicy_t otr_get_policy(const char * const recipient)
 {
     return PROF_OTRPOLICY_MANUAL;
 }
+
+char* otr_senderror_str(prof_otrsendres_t res)
+{
+    return NULL;
+}
+
diff --git a/tests/ui/stub_ui.c b/tests/ui/stub_ui.c
index 54faa50a..70be97ef 100644
--- a/tests/ui/stub_ui.c
+++ b/tests/ui/stub_ui.c
@@ -105,7 +105,7 @@ unsigned long ui_get_idle_time(void)
 
 void ui_reset_idle_time(void) {}
 void ui_new_private_win(const char * const fulljid) {}
-ProfWin* ui_new_chat_win(const char * const barejid)
+ProfChatWin* ui_new_chat_win(const char * const barejid)
 {
     return NULL;
 }