From 84f1c4b96fa36694f70989a1636fe0e315c16023 Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 10 Jan 2014 00:08:49 +0000 Subject: [PATCH] WIP - OTR support --- src/command/command.c | 20 ++++++++--- src/command/commands.c | 20 ++++++++++- src/otr.c | 81 +++++++++++++++++++++++++++++------------- src/otr.h | 1 + src/server_events.c | 18 ++++++++-- 5 files changed, 109 insertions(+), 31 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 1788e25e..9aca7185 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -1140,12 +1140,22 @@ cmd_execute_default(const char * const inp) } else { #ifdef HAVE_LIBOTR char *encrypted = otr_encrypt_message(recipient, inp); - message_send(encrypted, recipient); - otr_free_message(encrypted); + 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); -#endif - if (prefs_get_boolean(PREF_CHLOG)) { const char *jid = jabber_get_fulljid(); Jid *jidp = jid_create(jid); @@ -1154,6 +1164,8 @@ 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 257abe61..b71e2af8 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -916,6 +916,24 @@ cmd_msg(gchar **args, struct cmd_help_t help) usr_jid = usr; } 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); + 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 send message,"); + } + +#else message_send(msg, usr_jid); ui_outgoing_msg("me", usr_jid, msg); @@ -924,7 +942,7 @@ 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); - } +#endif return TRUE; } else { diff --git a/src/otr.c b/src/otr.c index e468bdea..2f2ddf3c 100644 --- a/src/otr.c +++ b/src/otr.c @@ -36,23 +36,37 @@ static char *jid; static OtrlPolicy cb_policy(void *opdata, ConnContext *context) { - cons_debug("cb_policy"); - return OTRL_POLICY_DEFAULT ^ OTRL_POLICY_ALLOW_V1; +// cons_debug("cb_policy"); + return OTRL_POLICY_DEFAULT; } static void cb_create_privkey(void *opdata, const char *accountname, const char *protocol) { - cons_debug("cb_create_privkey()"); +// cons_debug("cb_create_privkey accountname: %s, protocol: %s", accountname, protocol); +// GString *key_filename = g_string_new("./"); +// g_string_append(key_filename, accountname); +// g_string_append(key_filename, "_key.txt"); + +// gcry_error_t err = 0; + +// err = otrl_privkey_generate(user_state, key_filename->str, accountname, protocol); +// if (!err == GPG_ERR_NO_ERROR) { +// cons_debug("Failed to generate private key"); +// g_string_free(key_filename, TRUE); +// return; +// } +// otrl_privkey_read(user_state, key_filename->str); +// cons_debug("Generated private key"); } static int cb_is_logged_in(void *opdata, const char *accountname, const char *protocol, const char *recipient) { - cons_debug("cb_is_logged_in: account: %s, protocol: %s, recipient: %s", - accountname, protocol, recipient); +// cons_debug("cb_is_logged_in: account: %s, protocol: %s, recipient: %s", +// accountname, protocol, recipient); return -1; } @@ -60,8 +74,10 @@ static void cb_inject_message(void *opdata, const char *accountname, const char *protocol, const char *recipient, const char *message) { - cons_debug("cb_inject_message: account: %s, protocol, %s, recipient: %s, message: %s", - accountname, protocol, recipient, message); +// cons_debug("cb_inject_message: account: %s, protocol, %s, recipient: %s, message: %s", +// accountname, protocol, recipient, message); + char *msg_decrypt = NULL; + otrl_message_receiving(user_state, &ops, NULL, recipient, protocol, accountname, message, &msg_decrypt, 0, NULL, NULL); } static void @@ -69,73 +85,72 @@ cb_notify(void *opdata, OtrlNotifyLevel level, const char *accountname, const char *protocol, const char *username, const char *title, const char *primary, const char *secondary) { - cons_debug("cb_notify"); +// cons_debug("cb_notify"); } static int cb_display_otr_message(void *opdata, const char *accountname, const char *protocol, const char *username, const char *msg) { - cons_debug("cb_display_otr_message: account: %s, protocol: %s, user: %s, msg: %s", - accountname, protocol, username, msg); + cons_show_error("%s", msg); return 0; } static const char * cb_protocol_name(void *opdata, const char *protocol) { - cons_debug("cb_protocol_name: %s", protocol); - return protocol; +// cons_debug("cb_protocol_name: %s", protocol); + return "xmpp"; } static void cb_new_fingerprint(void *opdata, OtrlUserState us, const char *accountname, const char *protocol, const char *username, unsigned char fingerprint[20]) { - cons_debug("cb_new_fingerprint: account: %s, protocol: %s, username: %s", - accountname, protocol, username); +// cons_debug("cb_new_fingerprint: account: %s, protocol: %s, username: %s", +// accountname, protocol, username); } static void cb_protocol_name_free(void *opdata, const char *protocol_name) { - cons_debug("cb_protocol_name_free: %s", protocol_name); +// cons_debug("cb_protocol_name_free: %s", protocol_name); } static void cb_update_context_list(void *opdata) { - cons_debug("cb_update_context_list"); +// cons_debug("cb_update_context_list"); } static void cb_write_fingerprints(void *opdata) { - cons_debug("cb_write_fingerprints"); +// cons_debug("cb_write_fingerprints"); } static void cb_gone_secure(void *opdata, ConnContext *context) { - cons_debug("cb_gone_secure"); +// cons_debug("cb_gone_secure"); } static void cb_gone_insecure(void *opdata, ConnContext *context) { - cons_debug("cb_gone_insecure"); +// cons_debug("cb_gone_insecure"); } static void cb_still_secure(void *opdata, ConnContext *context, int is_reply) { - cons_debug("cb_still_secure: is_reply = %d", is_reply); +// cons_debug("cb_still_secure: is_reply = %d", is_reply); } static void cb_log_message(void *opdata, const char *message) { - cons_debug("cb_log_message: %s", message); +// cons_debug("cb_log_message: %s", message); } void @@ -170,10 +185,14 @@ otr_account_load(ProfAccount *account) GString *key_filename = g_string_new("./"); g_string_append(key_filename, account->jid); + g_string_append(key_filename, "/"); + g_string_append(key_filename, account->jid); g_string_append(key_filename, "_key.txt"); GString *fp_filename = g_string_new("./"); g_string_append(fp_filename, account->jid); + g_string_append(fp_filename, "/"); + g_string_append(fp_filename, account->jid); g_string_append(fp_filename, "_fingerprints.txt"); user_state = otrl_userstate_create(); @@ -191,6 +210,7 @@ otr_account_load(ProfAccount *account) cons_debug("Generated private key"); } + if (!g_file_test(fp_filename->str, G_FILE_TEST_IS_REGULAR)) { cons_debug("Fingerprints not found, creating file"); err = otrl_privkey_write_fingerprints(user_state, fp_filename->str); @@ -228,7 +248,7 @@ otr_account_load(ProfAccount *account) char * otr_encrypt_message(const char * const to, const char * const message) { - cons_debug("otr_encrypt_message, account: %s, protocol: xmpp, recipient: %s", jid, to); +// cons_debug("otr_encrypt_message, account: %s, recipient: %s, message: %s", jid, to, message); gcry_error_t err; char *newmessage = NULL; @@ -243,9 +263,9 @@ otr_encrypt_message(const char * const to, const char * const message) 0, &newmessage, NULL, - &ops); + NULL); if (!err == GPG_ERR_NO_ERROR) { - cons_debug("Error encrypting, result: %s", newmessage); +// cons_debug("Error encrypting, result: %s", newmessage); return NULL; } else { cons_debug("Encrypted, result: %s", newmessage); @@ -253,6 +273,19 @@ otr_encrypt_message(const char * const to, const char * const message) } } +char * +otr_decrypt_message(const char * const from, const char * const message) +{ +// cons_debug("otr_decrypt_message, account: %s, from: %s, message: %s", jid, from, 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) { + return decrypted; + } else { + return NULL; + } +} + void otr_free_message(char *message) { diff --git a/src/otr.h b/src/otr.h index 42005382..ae8e2cb1 100644 --- a/src/otr.h +++ b/src/otr.h @@ -28,6 +28,7 @@ void otr_init(void); void otr_account_load(ProfAccount *account); char * otr_encrypt_message(const char * const to, const char * const message); +char * otr_decrypt_message(const char * const from, const char * const message); void otr_free_message(char *message); #endif diff --git a/src/server_events.c b/src/server_events.c index ac940c86..48dae2ea 100644 --- a/src/server_events.c +++ b/src/server_events.c @@ -28,6 +28,7 @@ #include "config/preferences.h" #include "roster_list.h" #include "ui/ui.h" +#include "otr.h" void handle_error_message(const char *from, const char *err_msg) @@ -169,17 +170,30 @@ handle_duck_result(const char * const result) void handle_incoming_message(char *from, char *message, gboolean priv) { - ui_incoming_msg(from, message, NULL, priv); + char *newmessage; + if (!priv) { + newmessage = otr_decrypt_message(from, message); + if (newmessage == NULL) { + return; + } + } else { + newmessage = message; + } + + ui_incoming_msg(from, newmessage, 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); + chat_log_chat(jidp->barejid, from_jid->barejid, newmessage, PROF_IN_LOG, NULL); jid_destroy(jidp); jid_destroy(from_jid); } + + if (!priv) + otr_free_message(newmessage); } void