From 5b8b9074a294e32cbec5f64f61d867dbf0ca1d51 Mon Sep 17 00:00:00 2001 From: John Hernandez <129467592+H3rnand3zzz@users.noreply.github.com> Date: Thu, 13 Apr 2023 15:23:45 +0200 Subject: [PATCH] Add nickname support for /roster remove Add support of name/nickname instead of only JID for `/roster remove` command. Add tests for it as well. --- src/command/cmd_ac.c | 2 +- src/command/cmd_defs.c | 2 +- src/command/cmd_funcs.c | 13 ++++++----- tests/functionaltests/functionaltests.c | 1 + tests/functionaltests/test_roster.c | 29 +++++++++++++++++++++++++ tests/functionaltests/test_roster.h | 1 + tests/unittests/test_cmd_roster.c | 22 +++++++++++++++++++ tests/unittests/test_cmd_roster.h | 1 + tests/unittests/unittests.c | 1 + 9 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c index 3561449e..5678dbff 100644 --- a/src/command/cmd_ac.c +++ b/src/command/cmd_ac.c @@ -2292,7 +2292,7 @@ _roster_autocomplete(ProfWin* window, const char* const input, gboolean previous if (result) { return result; } - result = autocomplete_param_with_func(input, "/roster remove", roster_barejid_autocomplete, previous, NULL); + result = autocomplete_param_with_func(input, "/roster remove", roster_contact_autocomplete, previous, NULL); if (result) { return result; } diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index 12ebf4a1..fe993e39 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -290,7 +290,7 @@ static const struct cmd_t command_defs[] = { "/roster size ", "/roster wrap on|off", "/roster add []", - "/roster remove ", + "/roster remove ", "/roster remove_all contacts", "/roster nick ", "/roster clearnick ", diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index f6736156..7c533a45 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -2922,14 +2922,17 @@ cmd_roster(ProfWin* window, const char* const command, gchar** args) cons_show("You are not currently connected."); return TRUE; } - char* jid = args[1]; - if (jid == NULL) { + char* usr = args[1]; + if (usr == NULL) { cons_bad_cmd_usage(command); - } else { - roster_send_remove(jid); + return TRUE; } + char* barejid = roster_barejid_from_name(usr); + if (barejid == NULL) { + barejid = usr; + } + roster_send_remove(barejid); return TRUE; - } else if (strcmp(args[0], "remove_all") == 0) { if (g_strcmp0(args[1], "contacts") != 0) { cons_bad_cmd_usage(command); diff --git a/tests/functionaltests/functionaltests.c b/tests/functionaltests/functionaltests.c index 5e8656b7..bee95ad3 100644 --- a/tests/functionaltests/functionaltests.c +++ b/tests/functionaltests/functionaltests.c @@ -83,6 +83,7 @@ int main(int argc, char* argv[]) { PROF_FUNC_TEST(sends_new_item), PROF_FUNC_TEST(sends_new_item_nick), PROF_FUNC_TEST(sends_remove_item), + PROF_FUNC_TEST(sends_remove_item_nick), PROF_FUNC_TEST(sends_nick_change), PROF_FUNC_TEST(send_software_version_request), diff --git a/tests/functionaltests/test_roster.c b/tests/functionaltests/test_roster.c index aa06016e..c16764be 100644 --- a/tests/functionaltests/test_roster.c +++ b/tests/functionaltests/test_roster.c @@ -92,6 +92,35 @@ sends_remove_item(void **state) assert_true(prof_output_exact("Roster item removed: buddy1@localhost")); } +void +sends_remove_item_nick(void **state) +{ + prof_connect_with_roster( + "" + "" + ); + + stbbr_for_query("jabber:iq:roster", + "" + "" + "" + "" + "" + ); + + prof_input("/roster remove Bobby"); + + assert_true(stbbr_received( + "" + "" + "" + "" + "" + )); + + assert_true(prof_output_exact("Roster item removed: buddy1@localhost")); +} + void sends_nick_change(void **state) { diff --git a/tests/functionaltests/test_roster.h b/tests/functionaltests/test_roster.h index 9b34e82a..885dfc55 100644 --- a/tests/functionaltests/test_roster.h +++ b/tests/functionaltests/test_roster.h @@ -1,4 +1,5 @@ void sends_new_item(void **state); void sends_new_item_nick(void **state); void sends_remove_item(void **state); +void sends_remove_item_nick(void **state); void sends_nick_change(void **state); diff --git a/tests/unittests/test_cmd_roster.c b/tests/unittests/test_cmd_roster.c index 3908ba58..d848235b 100644 --- a/tests/unittests/test_cmd_roster.c +++ b/tests/unittests/test_cmd_roster.c @@ -114,12 +114,34 @@ cmd_roster_remove_sends_roster_remove_request(void** state) char* jid = "bob@server.org"; gchar* args[] = { "remove", jid, NULL }; + roster_create(); + roster_add("bob@server.org", "bob", NULL, "both", FALSE); + will_return(connection_get_status, JABBER_CONNECTED); expect_string(roster_send_remove, barejid, jid); gboolean result = cmd_roster(NULL, CMD_ROSTER, args); assert_true(result); + roster_destroy(); +} + +void +cmd_roster_remove_nickname_sends_roster_remove_request(void** state) +{ + char* jid = "bob@server.org"; + gchar* args[] = { "remove", "bob", NULL }; + + roster_create(); + roster_add("bob@server.org", "bob", NULL, "both", FALSE); + + will_return(connection_get_status, JABBER_CONNECTED); + + expect_string(roster_send_remove, barejid, jid); + + gboolean result = cmd_roster(NULL, CMD_ROSTER, args); + assert_true(result); + roster_destroy(); } void diff --git a/tests/unittests/test_cmd_roster.h b/tests/unittests/test_cmd_roster.h index 49bd8db3..bc57f267 100644 --- a/tests/unittests/test_cmd_roster.h +++ b/tests/unittests/test_cmd_roster.h @@ -7,6 +7,7 @@ void cmd_roster_add_shows_message_when_no_jid(void** state); void cmd_roster_add_sends_roster_add_request(void** state); void cmd_roster_remove_shows_message_when_no_jid(void** state); void cmd_roster_remove_sends_roster_remove_request(void** state); +void cmd_roster_remove_nickname_sends_roster_remove_request(void** state); void cmd_roster_nick_shows_message_when_no_jid(void** state); void cmd_roster_nick_shows_message_when_no_nick(void** state); void cmd_roster_nick_shows_message_when_no_contact_exists(void** state); diff --git a/tests/unittests/unittests.c b/tests/unittests/unittests.c index 2724aaa2..30f18c5c 100644 --- a/tests/unittests/unittests.c +++ b/tests/unittests/unittests.c @@ -584,6 +584,7 @@ main(int argc, char* argv[]) unit_test(cmd_roster_add_sends_roster_add_request), unit_test(cmd_roster_remove_shows_message_when_no_jid), unit_test(cmd_roster_remove_sends_roster_remove_request), + unit_test(cmd_roster_remove_nickname_sends_roster_remove_request), unit_test(cmd_roster_nick_shows_message_when_no_jid), unit_test(cmd_roster_nick_shows_message_when_no_nick), unit_test(cmd_roster_nick_shows_message_when_no_contact_exists),