1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Merge pull request #1529 from dustinlagoy/access-roster-from-plugins

Access roster from plugins
This commit is contained in:
Michael Vetter 2021-07-01 09:41:07 +02:00 committed by GitHub
commit aae252e1b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 171 additions and 0 deletions

View File

@ -138,6 +138,18 @@ Retrieve the users nickname in a chat room, when in a chat room window.
*/
char* prof_get_current_nick(void);
/**
Retrieve the nickname for a given barejid if it is in the roster.
@return the users nickname e.g. "eddie", or the input barejid if it is not in the roster.
*/
char* prof_get_name_from_roster(const char *barejid);
/**
Retrieve the barejid for a given nickname if it is in the roster.
@return the users barejid e.g. "eddie@server.tld", or NULLL if the nickname is not in the roster.
*/
char* prof_get_barejid_from_roster(const char *name);
/**
Retrieve nicknames of all occupants in a chat room, when in a chat room window.
@return nicknames of all occupants in the current room or an empty list if not in a chat room window.

View File

@ -253,6 +253,24 @@ def get_current_nick():
pass
def get_name_from_roster(barejid):
"""Retrieve a nickname from a barejid if it is in the roster.
:return: the users nickname e.g. "eddie", or the input barejid if it is not in the roster.
:rtype: str
"""
pass
def get_barejid_from_roster(name):
"""Retrieve the barejid for a given nickname if it is in the roster.
:return: the users barejid e.g. "eddie@server.tld", or ``None`` if the nickname is not in the roster.
:rtype: str
"""
pass
def get_current_occupants():
"""Retrieve nicknames of all occupants in a chat room, when in a chat room window.

View File

@ -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,18 @@ api_get_current_nick(void)
}
}
char*
api_get_name_from_roster(const char* barejid)
{
return roster_get_display_name(barejid);
}
char*
api_get_barejid_from_roster(const char* name)
{
return roster_barejid_from_name(name);
}
char**
api_get_current_occupants(void)
{

View File

@ -49,6 +49,8 @@ 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_name_from_roster(const char* barejid);
char* api_get_barejid_from_roster(const char* name);
char** api_get_current_occupants(void);
char* api_get_room_nick(const char* barejid);

View File

@ -195,6 +195,18 @@ c_api_get_current_nick(void)
return api_get_current_nick();
}
static char*
c_api_get_name_from_roster(const char* barejid)
{
return api_get_name_from_roster(barejid);
}
static char*
c_api_get_barejid_from_roster(const char* name)
{
return api_get_barejid_from_roster(name);
}
static char**
c_api_get_current_occupants(void)
{
@ -483,6 +495,8 @@ 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_name_from_roster = c_api_get_name_from_roster;
prof_get_barejid_from_roster = c_api_get_barejid_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;

View File

@ -64,6 +64,8 @@ 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_name_from_roster)(const char *barejid) = NULL;
char* (*prof_get_barejid_from_roster)(const char *name) = NULL;
char** (*prof_get_current_occupants)(void) = NULL;
char* (*prof_get_room_nick)(const char *barejid) = NULL;

View File

@ -74,6 +74,8 @@ 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_name_from_roster)(const char *barejid);
char* (*prof_get_barejid_from_roster)(const char *name);
char** (*prof_get_current_occupants)(void);
char* (*prof_get_room_nick)(const char *barejid);

View File

@ -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);
@ -440,6 +441,48 @@ python_api_get_current_nick(PyObject* self, PyObject* args)
}
}
static PyObject*
python_api_get_name_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* name = roster_get_display_name(barejid_str);
free(barejid_str);
disable_python_threads();
if (name) {
return Py_BuildValue("s", name);
} else {
Py_RETURN_NONE;
}
}
static PyObject*
python_api_get_barejid_from_roster(PyObject* self, PyObject* args)
{
PyObject* name = NULL;
if (!PyArg_ParseTuple(args, "O", &name)) {
Py_RETURN_NONE;
}
char* name_str = python_str_or_unicode_to_string(name);
allow_python_threads();
char* barejid = roster_barejid_from_name(name_str);
free(name_str);
disable_python_threads();
if (barejid) {
return Py_BuildValue("s", barejid);
} else {
Py_RETURN_NONE;
}
}
static PyObject*
python_api_get_current_occupants(PyObject* self, PyObject* args)
{
@ -1487,6 +1530,8 @@ 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_name_from_roster", python_api_get_name_from_roster, METH_VARARGS, "Return nickname in roster of barejid." },
{ "get_barejid_from_roster", python_api_get_barejid_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." },

View File

@ -170,6 +170,30 @@ roster_get_contact(const char* const barejid)
return contact;
}
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)
{

View File

@ -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);

View File

@ -689,3 +689,35 @@ remove_contact_with_remaining_in_group(void** state)
g_list_free_full(groups_res, free);
roster_destroy();
}
void
get_contact_display_name(void** state)
{
roster_create();
roster_add("person@server.org", "nickname", NULL, NULL, FALSE);
assert_string_equal("nickname", roster_get_display_name("person@server.org"));
roster_destroy();
}
void
get_contact_display_name_is_barejid_if_name_is_empty(void** state)
{
roster_create();
roster_add("person@server.org", NULL, NULL, NULL, FALSE);
assert_string_equal("person@server.org", roster_get_display_name("person@server.org"));
roster_destroy();
}
void
get_contact_display_name_is_passed_barejid_if_contact_does_not_exist(void** state)
{
roster_create();
assert_string_equal("person@server.org", roster_get_display_name("person@server.org"));
roster_destroy();
}

View File

@ -30,3 +30,6 @@ void add_contacts_with_different_groups(void** state);
void add_contacts_with_same_groups(void** state);
void add_contacts_with_overlapping_groups(void** state);
void remove_contact_with_remaining_in_group(void** state);
void get_contact_display_name(void** state);
void get_contact_display_name_is_barejid_if_name_is_empty(void** state);
void get_contact_display_name_is_passed_barejid_if_contact_does_not_exist(void** state);

View File

@ -219,6 +219,9 @@ main(int argc, char* argv[])
unit_test(add_contacts_with_same_groups),
unit_test(add_contacts_with_overlapping_groups),
unit_test(remove_contact_with_remaining_in_group),
unit_test(get_contact_display_name),
unit_test(get_contact_display_name_is_barejid_if_name_is_empty),
unit_test(get_contact_display_name_is_passed_barejid_if_contact_does_not_exist),
unit_test_setup_teardown(returns_false_when_chat_session_does_not_exist,
init_chat_sessions,