about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorDustin Lagoy <dustin@lagoy.org>2021-04-28 09:17:02 -0400
committerDustin Lagoy <dustin@lagoy.org>2021-04-28 09:17:02 -0400
commited4d2fcfb2b99a049164c602feea7ccf4d0d8bea (patch)
tree9edc0cf6faf9f8a4459914f365a1ef4ed4cdd379 /src
parent4a94c051a4425003ba9d215af3e7b2c1898f2cbb (diff)
downloadprofani-tty-ed4d2fcfb2b99a049164c602feea7ccf4d0d8bea.tar.gz
Add plugin prof_get_nick_from_roster function
Diffstat (limited to 'src')
-rw-r--r--src/plugins/api.c7
-rw-r--r--src/plugins/api.h1
-rw-r--r--src/plugins/c_api.c7
-rw-r--r--src/plugins/profapi.c1
-rw-r--r--src/plugins/profapi.h1
-rw-r--r--src/plugins/python_api.c23
-rw-r--r--src/xmpp/roster_list.c24
-rw-r--r--src/xmpp/roster_list.h1
8 files changed, 65 insertions, 0 deletions
diff --git a/src/plugins/api.c b/src/plugins/api.c
index 2c8983aa..c3207175 100644
--- a/src/plugins/api.c
+++ b/src/plugins/api.c
@@ -55,6 +55,7 @@
 #include "plugins/disco.h"
 #include "ui/ui.h"
 #include "ui/window_list.h"
+#include "xmpp/roster_list.h"
 
 void
 api_cons_alert(void)
@@ -239,6 +240,12 @@ api_get_current_nick(void)
     }
 }
 
+char*
+api_get_nick_from_roster(const char* barejid)
+{
+    return roster_get_display_name(barejid);
+}
+
 char**
 api_get_current_occupants(void)
 {
diff --git a/src/plugins/api.h b/src/plugins/api.h
index f0842c4e..5496aca9 100644
--- a/src/plugins/api.h
+++ b/src/plugins/api.h
@@ -49,6 +49,7 @@ char* api_get_current_recipient(void);
 char* api_get_current_muc(void);
 gboolean api_current_win_is_console(void);
 char* api_get_current_nick(void);
+char* api_get_nick_from_roster(const char* barejid);
 char** api_get_current_occupants(void);
 
 char* api_get_room_nick(const char* barejid);
diff --git a/src/plugins/c_api.c b/src/plugins/c_api.c
index 0f236074..3b369e78 100644
--- a/src/plugins/c_api.c
+++ b/src/plugins/c_api.c
@@ -195,6 +195,12 @@ c_api_get_current_nick(void)
     return api_get_current_nick();
 }
 
+static char*
+c_api_get_nick_from_roster(const char* barejid)
+{
+    return api_get_nick_from_roster(barejid);
+}
+
 static char**
 c_api_get_current_occupants(void)
 {
@@ -483,6 +489,7 @@ c_api_init(void)
     prof_get_current_muc = c_api_get_current_muc;
     prof_current_win_is_console = c_api_current_win_is_console;
     prof_get_current_nick = c_api_get_current_nick;
+    prof_get_nick_from_roster = c_api_get_nick_from_roster;
     prof_get_current_occupants = c_api_get_current_occupants;
     prof_get_room_nick = c_api_get_room_nick;
     prof_log_debug = c_api_log_debug;
diff --git a/src/plugins/profapi.c b/src/plugins/profapi.c
index 75475631..c44eb95d 100644
--- a/src/plugins/profapi.c
+++ b/src/plugins/profapi.c
@@ -64,6 +64,7 @@ char* (*prof_get_current_recipient)(void) = NULL;
 char* (*prof_get_current_muc)(void) = NULL;
 int (*prof_current_win_is_console)(void) = NULL;
 char* (*prof_get_current_nick)(void) = NULL;
+char* (*prof_get_nick_from_roster)(const char *barejid) = NULL;
 char** (*prof_get_current_occupants)(void) = NULL;
 
 char* (*prof_get_room_nick)(const char *barejid) = NULL;
diff --git a/src/plugins/profapi.h b/src/plugins/profapi.h
index cfca45c3..b2305477 100644
--- a/src/plugins/profapi.h
+++ b/src/plugins/profapi.h
@@ -74,6 +74,7 @@ char* (*prof_get_current_recipient)(void);
 char* (*prof_get_current_muc)(void);
 int (*prof_current_win_is_console)(void);
 char* (*prof_get_current_nick)(void);
+char* (*prof_get_nick_from_roster)(const char *barejid);
 char** (*prof_get_current_occupants)(void);
 
 char* (*prof_get_room_nick)(const char *barejid);
diff --git a/src/plugins/python_api.c b/src/plugins/python_api.c
index fe66d99e..5eacb56d 100644
--- a/src/plugins/python_api.c
+++ b/src/plugins/python_api.c
@@ -46,6 +46,7 @@
 #include "plugins/python_plugins.h"
 #include "plugins/callbacks.h"
 #include "plugins/autocompleters.h"
+#include "xmpp/roster_list.h"
 
 static char* _python_plugin_name(void);
 
@@ -441,6 +442,27 @@ python_api_get_current_nick(PyObject* self, PyObject* args)
 }
 
 static PyObject*
+python_api_get_nick_from_roster(PyObject* self, PyObject* args)
+{
+    PyObject* barejid = NULL;
+    if (!PyArg_ParseTuple(args, "O", &barejid)) {
+        Py_RETURN_NONE;
+    }
+
+    char* barejid_str = python_str_or_unicode_to_string(barejid);
+
+    allow_python_threads();
+    char* nick = roster_get_display_name(barejid_str);
+    free(barejid_str);
+    disable_python_threads();
+    if (nick) {
+        return Py_BuildValue("s", nick);
+    } else {
+        Py_RETURN_NONE;
+    }
+}
+
+static PyObject*
 python_api_get_current_occupants(PyObject* self, PyObject* args)
 {
     allow_python_threads();
@@ -1487,6 +1509,7 @@ static PyMethodDef apiMethods[] = {
     { "get_current_recipient", python_api_get_current_recipient, METH_VARARGS, "Return the jid of the recipient of the current window." },
     { "get_current_muc", python_api_get_current_muc, METH_VARARGS, "Return the jid of the room of the current window." },
     { "get_current_nick", python_api_get_current_nick, METH_VARARGS, "Return nickname in current room." },
+    { "get_nick_from_roster", python_api_get_nick_from_roster, METH_VARARGS, "Return nickname in roster of barejid." },
     { "get_current_occupants", python_api_get_current_occupants, METH_VARARGS, "Return list of occupants in current room." },
     { "current_win_is_console", python_api_current_win_is_console, METH_VARARGS, "Returns whether the current window is the console." },
     { "get_room_nick", python_api_get_room_nick, METH_VARARGS, "Return the nickname used in the specified room, or None if not in the room." },
diff --git a/src/xmpp/roster_list.c b/src/xmpp/roster_list.c
index 04d16a17..9d7ac427 100644
--- a/src/xmpp/roster_list.c
+++ b/src/xmpp/roster_list.c
@@ -171,6 +171,30 @@ roster_get_contact(const char* const barejid)
 }
 
 char*
+roster_get_display_name(const char* const barejid)
+{
+    assert(roster != NULL);
+
+    GString* result = g_string_new("");
+
+    PContact contact = roster_get_contact(barejid);
+    if (contact) {
+        if (p_contact_name(contact)) {
+            g_string_append(result, p_contact_name(contact));
+        } else {
+            g_string_append(result, barejid);
+        }
+    } else {
+        g_string_append(result, barejid);
+    }
+
+    char* result_str = result->str;
+    g_string_free(result, FALSE);
+
+    return result_str;
+}
+
+char*
 roster_get_msg_display_name(const char* const barejid, const char* const resource)
 {
     assert(roster != NULL);
diff --git a/src/xmpp/roster_list.h b/src/xmpp/roster_list.h
index e47a29cb..f9548d97 100644
--- a/src/xmpp/roster_list.h
+++ b/src/xmpp/roster_list.h
@@ -70,6 +70,7 @@ GList* roster_get_groups(void);
 char* roster_group_autocomplete(const char* const search_str, gboolean previous, void* context);
 char* roster_barejid_autocomplete(const char* const search_str, gboolean previous, void* context);
 GSList* roster_get_contacts_by_presence(const char* const presence);
+char* roster_get_display_name(const char* const barejid);
 char* roster_get_msg_display_name(const char* const barejid, const char* const resource);
 gint roster_compare_name(PContact a, PContact b);
 gint roster_compare_presence(PContact a, PContact b);