Commit ccb3bac5 authored by Richard Bayerle's avatar Richard Bayerle
Browse files

Add 'status' to API and commands

parent 397d79f7
......@@ -188,6 +188,69 @@ cleanup:
axc_context_destroy_all(axc_ctx_p);
}
void lurch_api_status_im_handler(PurpleAccount * acc_p, const char * contact_bare_jid, void (*cb)(int32_t err, lurch_status_t status, void * user_data_p), void * user_data_p) {
int32_t ret_val = 0;
lurch_status_t status = LURCH_STATUS_DISABLED;
char * uname = (void *) 0;
char * db_fn_omemo = (void *) 0;
omemo_devicelist * dl_p = (void *) 0;
axc_context * axc_ctx_p = (void *) 0;
uname = lurch_util_uname_strip(purple_account_get_username(acc_p));
db_fn_omemo = lurch_util_uname_get_db_fn(uname, LURCH_DB_NAME_OMEMO);
ret_val = omemo_storage_chatlist_exists(contact_bare_jid, db_fn_omemo);
if (ret_val < 0 || ret_val > 1) {
purple_debug_error(MODULE_NAME, "Failed to look up %s in file %s.", contact_bare_jid, db_fn_omemo);
goto cleanup;
} else if (ret_val == 0) {
// conversation is not on blacklist, continue
} else if (ret_val == 1) {
ret_val = 0;
status = LURCH_STATUS_DISABLED;
goto cleanup;
}
ret_val = omemo_storage_user_devicelist_retrieve(contact_bare_jid, db_fn_omemo, &dl_p);
if (ret_val) {
purple_debug_error(MODULE_NAME, "Failed to get the devicelist for %s from %s.", contact_bare_jid, db_fn_omemo);
goto cleanup;
}
if (omemo_devicelist_is_empty(dl_p)) {
ret_val = 0;
status = LURCH_STATUS_NOT_SUPPORTED;
goto cleanup;
}
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 for %s.", uname);
goto cleanup;
}
ret_val = axc_session_exists_any(contact_bare_jid, axc_ctx_p);
if (ret_val < 0) {
purple_debug_error(MODULE_NAME, "Failed to look up session with %s.", contact_bare_jid);
goto cleanup;
} else if (ret_val == 0) {
ret_val = 0;
status = LURCH_STATUS_NO_SESSION;
} else {
ret_val = 0;
status = LURCH_STATUS_OK;
}
cleanup:
cb(ret_val, status, user_data_p);
g_free(uname);
g_free(db_fn_omemo);
omemo_devicelist_destroy(dl_p);
axc_context_destroy_all(axc_ctx_p);
}
static void lurch_api_marshal_VOID__POINTER_INT_POINTER_POINTER(PurpleCallback cb, va_list args, void * data, void ** return_val) {
void * arg1 = va_arg(args, void *);
gint32 arg2 = va_arg(args, guint);
......@@ -208,7 +271,7 @@ 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 6
#define NUM_OF_SIGNALS 7
const char * signal_names[NUM_OF_SIGNALS] = {
"lurch-id-show",
......@@ -216,7 +279,8 @@ const char * signal_names[NUM_OF_SIGNALS] = {
"lurch-id-remove",
"lurch-enable-im",
"lurch-disable-im",
"lurch-fp-get"
"lurch-fp-get",
"lurch-status-im"
};
const void * signal_handlers[NUM_OF_SIGNALS] = {
......@@ -225,7 +289,8 @@ const void * signal_handlers[NUM_OF_SIGNALS] = {
lurch_api_id_remove_handler,
lurch_api_enable_im_handler,
lurch_api_disable_im_handler,
lurch_api_fp_get_handler
lurch_api_fp_get_handler,
lurch_api_status_im_handler
};
const lurch_api_handler_t signal_handler_types[NUM_OF_SIGNALS] = {
......@@ -234,7 +299,8 @@ const lurch_api_handler_t signal_handler_types[NUM_OF_SIGNALS] = {
LURCH_API_HANDLER_ACC_DID_CB_DATA,
LURCH_API_HANDLER_ACC_JID_CB_DATA,
LURCH_API_HANDLER_ACC_JID_CB_DATA,
LURCH_API_HANDLER_ACC_CB_DATA
LURCH_API_HANDLER_ACC_CB_DATA,
LURCH_API_HANDLER_ACC_JID_CB_DATA
};
void lurch_api_init() {
......
......@@ -12,6 +12,13 @@ void lurch_api_init();
*/
void lurch_api_unload();
typedef enum {
LURCH_STATUS_DISABLED = 0, // manually disabled
LURCH_STATUS_NOT_SUPPORTED, // no OMEMO support, i.e. there is no devicelist node
LURCH_STATUS_NO_SESSION, // OMEMO is supported, but there is no libsignal session yet
LURCH_STATUS_OK // OMEMO is supported and session exists
} lurch_status_t;
/**
* USAGE
......
......@@ -6,6 +6,7 @@
#include "jutil.h"
#include "libomemo.h"
#include "lurch_api.h"
static void lurch_cmd_print(PurpleConversation * conv_p, const char * msg) {
purple_conversation_write(conv_p, "lurch", msg, PURPLE_MESSAGE_SYSTEM | PURPLE_MESSAGE_NO_LOG, time((void *) 0));
......@@ -129,6 +130,36 @@ void lurch_fp_show_print(int32_t err, const char * fp_printable, void * user_dat
g_free(msg);
}
void lurch_status_im_print(int32_t err, lurch_status_t status, void * user_data_p) {
PurpleConversation * conv_p = (PurpleConversation *) user_data_p;
const char * msg = (void *) 0;
if (err) {
lurch_cmd_print_err(conv_p, "Failed to get the conversation status. Check the debug log for details.");
return;
}
switch (status) {
case LURCH_STATUS_DISABLED:
msg = g_strdup_printf("You disabled OMEMO for this conversation. return is %i", status);
break;
case LURCH_STATUS_NOT_SUPPORTED:
msg = "Your contact does not support OMEMO.";
break;
case LURCH_STATUS_NO_SESSION:
msg = "Your contact supports OMEMO, but you have not established a session yet. Just start messaging!";
break;
case LURCH_STATUS_OK:
// msg = "OMEMO is enabled for this conversation.";
msg = g_strdup_printf("You enabled OMEMO for this conversation. return is %i", status);
break;
default:
msg = "Received unknown status code.";
}
lurch_cmd_print(conv_p, msg);
}
static void lurch_cmd_id(PurpleConversation * conv_p, const char * arg, const char * param) {
PurpleAccount * acc_p = purple_conversation_get_account(conv_p);
void * plugins_handle = purple_plugins_get_handle();
......@@ -180,6 +211,17 @@ static void lurch_cmd_fp(PurpleConversation * conv_p, const char * arg) {
}
}
static void lurch_cmd_status(PurpleConversation * conv_p) {
PurpleConversationType conv_type = purple_conversation_get_type(conv_p);
char * conv_bare_jid = jabber_get_bare_jid(purple_conversation_get_name(conv_p));
if (conv_type == PURPLE_CONV_TYPE_IM) {
purple_signal_emit(purple_plugins_get_handle(), "lurch-status-im", purple_conversation_get_account(conv_p), conv_bare_jid, lurch_status_im_print ,conv_p);
}
g_free(conv_bare_jid);
}
PurpleCmdRet lurch_cmd_func_v2(PurpleConversation * conv_p,
const gchar * cmd,
gchar ** args,
......@@ -197,6 +239,8 @@ PurpleCmdRet lurch_cmd_func_v2(PurpleConversation * conv_p,
lurch_cmd_id(conv_p, args[1], args[2]);
} else if (!g_strcmp0(command, "fp")) {
lurch_cmd_fp(conv_p, args[1]);
} else if (!g_strcmp0(command, "status")) {
lurch_cmd_status(conv_p);
} else {
lurch_cmd_print(conv_p, "No such command. Type '/lurch help' for a list of available commands.");
}
......
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