Commit 4c66d200 authored by Richard Bayerle's avatar Richard Bayerle
Browse files

Add function for retrieving the device's fingerprint to API.

parent 911e6e83
......@@ -2132,36 +2132,6 @@ static void lurch_conv_updated_cb(PurpleConversation * conv_p, PurpleConvUpdateT
}
}
/**
* Creates a fingerprint which resembles the one displayed by Conversations etc.
* Also useful for avoiding the smileys produced by ':d'...
*
* @param fp The fingerprint string as returned by purple_base16_encode_chunked
* @return A newly allocated string which contains the fingerprint in printable format, or NULL.
*/
static char * lurch_fp_printable(const char * fp) {
char ** split = (void *) 0;
char * temp1 = (void *) 0;
char * temp2 = (void *) 0;
if (!fp) {
return (void *) 0;
}
split = g_strsplit(fp, ":", 0);
temp2 = g_strdup("");
for (int i = 1; i <= 32; i += 4) {
temp1 = g_strconcat(temp2, split[i], split[i+1], split[i+2], split[i+3], " ", NULL);
g_free(temp2);
temp2 = g_strdup(temp1);
g_free(temp1);
}
g_strfreev(split);
return temp2;
}
static PurpleCmdRet lurch_cmd_func(PurpleConversation * conv_p,
const gchar * cmd,
gchar ** args,
......@@ -2337,7 +2307,7 @@ static PurpleCmdRet lurch_cmd_func(PurpleConversation * conv_p,
}
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
fp_printable = lurch_fp_printable(fp);
fp_printable = lurch_util_fp_get_printable(fp);
msg = g_strdup_printf("This device's fingerprint is:\n%s\n"
"You should make sure that your conversation partner gets displayed the same for this device.", fp_printable);
} else if (!g_strcmp0(args[2], "conv")) {
......@@ -2349,7 +2319,7 @@ static PurpleCmdRet lurch_cmd_func(PurpleConversation * conv_p,
}
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
fp_printable = lurch_fp_printable(fp);
fp_printable = lurch_util_fp_get_printable(fp);
temp_msg_1 = g_strdup_printf("The devices participating in this conversation and their fingerprints are as follows:\n"
"This device's (%s:%i) fingerprint:\n%s\n", uname, id, fp_printable);
......@@ -2373,7 +2343,7 @@ static PurpleCmdRet lurch_cmd_func(PurpleConversation * conv_p,
g_free(fp);
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
fp_printable = lurch_fp_printable(fp);
fp_printable = lurch_util_fp_get_printable(fp);
axc_buf_free(key_buf_p);
key_buf_p = (void *) 0;
......@@ -2407,7 +2377,7 @@ static PurpleCmdRet lurch_cmd_func(PurpleConversation * conv_p,
g_free(fp);
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
fp_printable = lurch_fp_printable(fp);
fp_printable = lurch_util_fp_get_printable(fp);
axc_buf_free(key_buf_p);
key_buf_p = (void *) 0;
......
......@@ -11,8 +11,7 @@
#define MODULE_NAME "lurch-api"
void lurch_api_id_show_handler(PurpleAccount * acc_p, void (*cb)(int32_t err, uint32_t id, void * user_data_p), void * user_data_p) {
int ret_val = 0;
int32_t err = 0;
int32_t ret_val = 0;
char * uname = (void *) 0;
axc_context * axc_ctx_p = (void *) 0;
uint32_t id = 0;
......@@ -22,19 +21,17 @@ void lurch_api_id_show_handler(PurpleAccount * acc_p, void (*cb)(int32_t err, ui
ret_val = lurch_util_axc_get_init_ctx(uname, &axc_ctx_p);
if (ret_val) {
purple_debug_error(MODULE_NAME, "Failed to create axc ctx.\n");
err = ret_val;
goto cleanup;
}
ret_val = axc_get_device_id(axc_ctx_p, &id);
if (ret_val) {
purple_debug_error(MODULE_NAME, "Failed to access axc db %s. Does the path seem correct?", axc_context_get_db_fn(axc_ctx_p));
err = ret_val;
goto cleanup;
}
cleanup:
cb(err, id, user_data_p);
cb(ret_val, id, user_data_p);
axc_context_destroy_all(axc_ctx_p);
g_free(uname);
......@@ -106,6 +103,40 @@ void lurch_api_disable_im_handler(PurpleAccount * acc_p, const char * contact_ba
g_free(db_fn_omemo);
}
void lurch_api_fp_get_handler(PurpleAccount * acc_p, void (*cb)(int32_t err, const char * fp_printable, void * user_data_p), void * user_data_p) {
int32_t ret_val = 0;
char * uname = (void *) 0;
axc_context * axc_ctx_p = (void *) 0;
axc_buf * key_buf_p = (void *) 0;
gchar * fp = (void *) 0;
char * fp_printable = (void *) 0;
uname = lurch_util_uname_strip(purple_account_get_username(acc_p));
ret_val = lurch_util_axc_get_init_ctx(uname, &axc_ctx_p);
if (ret_val) {
purple_debug_error(MODULE_NAME, "Failed to create axc ctx.\n");
goto cleanup;
}
ret_val = axc_key_load_public_own(axc_ctx_p, &key_buf_p);
if (ret_val) {
purple_debug_error("Failed to load public key from axc db %s.", axc_context_get_db_fn(axc_ctx_p));
goto cleanup;
}
fp = purple_base16_encode_chunked(axc_buf_get_data(key_buf_p), axc_buf_get_len(key_buf_p));
fp_printable = lurch_util_fp_get_printable(fp);
cleanup:
cb(ret_val, fp_printable, user_data_p);
g_free(fp_printable);
g_free(fp);
axc_buf_free(key_buf_p);
axc_context_destroy_all(axc_ctx_p);
}
typedef enum {
LURCH_API_HANDLER_ACC_CB_DATA = 0,
LURCH_API_HANDLER_ACC_JID_CB_DATA
......@@ -115,27 +146,30 @@ typedef enum {
* When adding a new signal: increase this number and add the name, handler function, and handler function type
* to the respective array.
*/
#define NUM_OF_SIGNALS 4
#define NUM_OF_SIGNALS 5
const char * signal_names[NUM_OF_SIGNALS] = {
"lurch-id-show",
"lurch-id-list",
"lurch-enable-im",
"lurch-disable-im"
"lurch-disable-im",
"lurch-fp-get"
};
const void * signal_handlers[NUM_OF_SIGNALS] = {
lurch_api_id_show_handler,
lurch_api_id_list_handler,
lurch_api_enable_im_handler,
lurch_api_disable_im_handler
lurch_api_disable_im_handler,
lurch_api_fp_get_handler
};
const lurch_api_handler_t signal_handler_types[NUM_OF_SIGNALS] = {
LURCH_API_HANDLER_ACC_CB_DATA,
LURCH_API_HANDLER_ACC_CB_DATA,
LURCH_API_HANDLER_ACC_JID_CB_DATA,
LURCH_API_HANDLER_ACC_JID_CB_DATA
LURCH_API_HANDLER_ACC_JID_CB_DATA,
LURCH_API_HANDLER_ACC_CB_DATA
};
void lurch_api_init() {
......@@ -171,7 +205,7 @@ void lurch_api_init() {
);
break;
default:
purple_debug_fatal(MODULE_NAME, "Unknown hander function type, aborting initialization.");
purple_debug_fatal(MODULE_NAME, "Unknown handler function type, aborting initialization.");
}
purple_signal_connect(
......
......@@ -24,8 +24,8 @@ static void lurch_cmd_help(PurpleConversation * conv_p) {
" - '/lurch id remove <id>': Removes the device ID <id> from this account's device list.\n"
" - '/lurch fp show': Displays this device's key fingerprint.\n"
" - '/lurch fp conv': Displays the fingerprints of all devices participating in this conversation.\n"
// add "status"
" - '/lurch help': Displays this message.\n" // X
//TODO: add "status"
" - '/lurch help': Displays this message.\n"
" - '/lurch uninstall': Uninstalls this device from OMEMO by removing its device ID from the devicelist.";
lurch_cmd_print(conv_p, help_message);
......@@ -102,6 +102,21 @@ void lurch_disable_print(int32_t err, void * user_data_p) {
lurch_cmd_print(conv_p, "Successfully disabled OMEMO.");
}
void lurch_fp_show_print(int32_t err, const char * fp_printable, void * user_data_p) {
PurpleConversation * conv_p = (PurpleConversation *) user_data_p;
char * msg = (void *) 0;
if (err) {
lurch_cmd_print_err(conv_p, "Failed to get this device's fingerprint. Check the debug log for details.");
return;
}
msg = g_strdup_printf("This device's fingerprint is %s.", fp_printable);
lurch_cmd_print(conv_p, msg);
g_free(msg);
}
static void lurch_cmd_id(PurpleConversation * conv_p, const char * arg) {
PurpleAccount * acc_p = purple_conversation_get_account(conv_p);
......@@ -136,6 +151,16 @@ static void lurch_cmd_disable(PurpleConversation * conv_p) {
g_free(conv_bare_jid);
}
static void lurch_cmd_fp(PurpleConversation * conv_p, const char * arg) {
PurpleAccount * acc_p = purple_conversation_get_account(conv_p);
if (!g_strcmp0(arg, "show")) {
purple_signal_emit(purple_plugins_get_handle(), "lurch-fp-get", acc_p, lurch_fp_show_print, conv_p);
} else {
lurch_cmd_print(conv_p, "Valid arguments for 'fp' are 'show'.");
}
}
PurpleCmdRet lurch_cmd_func_v2(PurpleConversation * conv_p,
const gchar * cmd,
gchar ** args,
......@@ -151,6 +176,8 @@ PurpleCmdRet lurch_cmd_func_v2(PurpleConversation * conv_p,
lurch_cmd_disable(conv_p);
} else if (!g_strcmp0(command, "id")) {
lurch_cmd_id(conv_p, args[1]);
} else if (!g_strcmp0(command, "fp")) {
lurch_cmd_fp(conv_p, args[1]);
} else {
lurch_cmd_print(conv_p, "No such command. Type '/lurch help' for a list of available commands.");
}
......
......@@ -121,4 +121,27 @@ char * lurch_util_uname_strip(const char * uname) {
char * lurch_util_uname_get_db_fn(const char * uname, const char * which) {
return g_strconcat(purple_user_dir(), "/", uname, "_", which, LURCH_DB_SUFFIX, NULL);
}
char * lurch_util_fp_get_printable(const char * fp) {
char ** split = (void *) 0;
char * temp1 = (void *) 0;
char * temp2 = (void *) 0;
if (!fp) {
return (void *) 0;
}
split = g_strsplit(fp, ":", 0);
temp2 = g_strdup("");
for (int i = 1; i <= 32; i += 4) {
temp1 = g_strconcat(temp2, split[i], split[i+1], split[i+2], split[i+3], " ", NULL);
g_free(temp2);
temp2 = g_strdup(temp1);
g_free(temp1);
}
g_strfreev(split);
return temp2;
}
\ No newline at end of file
......@@ -38,4 +38,13 @@ char * lurch_util_uname_strip(const char * uname);
* @param which Either LURCH_DB_NAME_OMEMO or LURCH_DB_NAME_AXC
* @return The path string. free() when done with it.
*/
char * lurch_util_uname_get_db_fn(const char * uname, const char * which);
\ No newline at end of file
char * lurch_util_uname_get_db_fn(const char * uname, const char * which);
/**
* Creates a fingerprint which resembles the one displayed by Conversations etc.
* Also useful for avoiding the smileys produced by ':d'...
*
* @param fp The fingerprint string as returned by purple_base16_encode_chunked
* @return A newly allocated string which contains the fingerprint in printable format, or NULL.
*/
char * lurch_util_fp_get_printable(const char * fp);
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment