about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-03-16 17:53:41 +0000
committerJames Booth <boothj5@gmail.com>2014-03-16 17:53:41 +0000
commit11c04d9fcd0706109ea88f081fa073d802e27032 (patch)
tree390acaa03ae5d5945025db46c1fe1d1379bb0054
parentbcafba2de6cdc9a056b0d69823ad62be0bfed159 (diff)
downloadprofani-tty-11c04d9fcd0706109ea88f081fa073d802e27032.tar.gz
Added /roster clearnick command
-rw-r--r--src/command/command.c5
-rw-r--r--src/command/commands.c78
-rw-r--r--tests/test_cmd_roster.c141
-rw-r--r--tests/test_cmd_roster.h7
-rw-r--r--tests/testsuite.c7
-rw-r--r--tests/xmpp/mock_xmpp.c27
-rw-r--r--tests/xmpp/mock_xmpp.h3
7 files changed, 234 insertions, 34 deletions
diff --git a/src/command/command.c b/src/command/command.c
index 8d4ccf8e..dc8a685f 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -1015,6 +1015,7 @@ cmd_init(void)
     roster_ac = autocomplete_new();
     autocomplete_add(roster_ac, "add");
     autocomplete_add(roster_ac, "nick");
+    autocomplete_add(roster_ac, "clearnick");
     autocomplete_add(roster_ac, "remove");
 
     group_ac = autocomplete_new();
@@ -1564,6 +1565,10 @@ _roster_autocomplete(char *input, int *size)
     if (result != NULL) {
         return result;
     }
+    result = autocomplete_param_with_func(input, size, "/roster clearnick", roster_find_jid);
+    if (result != NULL) {
+        return result;
+    }
     result = autocomplete_param_with_func(input, size, "/roster remove", roster_find_jid);
     if (result != NULL) {
         return result;
diff --git a/src/command/commands.c b/src/command/commands.c
index c7a8e0d5..4a7418f4 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -1200,49 +1200,41 @@ cmd_roster(gchar **args, struct cmd_help_t help)
         GSList *list = roster_get_contacts();
         cons_show_roster(list);
         return TRUE;
-    }
 
     // add contact
-    if (strcmp(args[0], "add") == 0) {
-
-        if (args[1] == NULL) {
+    } else if (strcmp(args[0], "add") == 0) {
+        char *jid = args[1];
+        if (jid == NULL) {
             cons_show("Usage: %s", help.usage);
-            return TRUE;
+        } else {
+            char *name = args[2];
+            roster_send_add_new(jid, name);
         }
-
-        char *jid = args[1];
-        char *name = args[2];
-
-        roster_send_add_new(jid, name);
-
         return TRUE;
-    }
 
     // remove contact
-    if (strcmp(args[0], "remove") == 0) {
-
-        if (args[1] == NULL) {
+    } else if (strcmp(args[0], "remove") == 0) {
+        char *jid = args[1];
+        if (jid == NULL) {
             cons_show("Usage: %s", help.usage);
-            return TRUE;
+        } else {
+            roster_send_remove(jid);
         }
-
-        char *jid = args[1];
-
-        roster_send_remove(jid);
-
         return TRUE;
-    }
 
     // change nickname
-    if (strcmp(args[0], "nick") == 0) {
-
-        if (args[1] == NULL) {
+    } else if (strcmp(args[0], "nick") == 0) {
+        char *jid = args[1];
+        if (jid == NULL) {
             cons_show("Usage: %s", help.usage);
             return TRUE;
         }
 
-        char *jid = args[1];
         char *name = args[2];
+        if (name == NULL) {
+            cons_show("Usage: %s", help.usage);
+            return TRUE;
+        }
 
         // contact does not exist
         PContact contact = roster_get_contact(jid);
@@ -1256,17 +1248,37 @@ cmd_roster(gchar **args, struct cmd_help_t help)
         GSList *groups = p_contact_groups(contact);
         roster_send_name_change(barejid, name, groups);
 
-        if (name == NULL) {
-            cons_show("Nickname for %s removed.", jid);
-        } else {
-            cons_show("Nickname for %s set to: %s.", jid, name);
+        cons_show("Nickname for %s set to: %s.", jid, name);
+
+        return TRUE;
+
+    // remove nickname
+    } else if (strcmp(args[0], "clearnick") == 0) {
+        char *jid = args[1];
+        if (jid == NULL) {
+            cons_show("Usage: %s", help.usage);
+            return TRUE;
+        }
+
+        // contact does not exist
+        PContact contact = roster_get_contact(jid);
+        if (contact == NULL) {
+            cons_show("Contact not found in roster: %s", jid);
+            return TRUE;
         }
 
+        const char *barejid = p_contact_barejid(contact);
+        roster_change_name(contact, NULL);
+        GSList *groups = p_contact_groups(contact);
+        roster_send_name_change(barejid, NULL, groups);
+
+        cons_show("Nickname for %s removed.", jid);
+
+        return TRUE;
+    } else {
+        cons_show("Usage: %s", help.usage);
         return TRUE;
     }
-
-    cons_show("Usage: %s", help.usage);
-    return TRUE;
 }
 
 gboolean
diff --git a/tests/test_cmd_roster.c b/tests/test_cmd_roster.c
index e59f5e5e..3624cd77 100644
--- a/tests/test_cmd_roster.c
+++ b/tests/test_cmd_roster.c
@@ -118,7 +118,7 @@ void cmd_roster_remove_shows_message_when_no_jid(void **state)
     free(help);
 }
 
-void cmd_roster_remove_sends_roster_remove_request(void)
+void cmd_roster_remove_sends_roster_remove_request(void **state)
 {
     char *jid = "bob@server.org";
     CommandHelp *help = malloc(sizeof(CommandHelp));
@@ -133,3 +133,142 @@ void cmd_roster_remove_sends_roster_remove_request(void)
 
     free(help);
 }
+
+void cmd_roster_nick_shows_message_when_no_jid(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    help->usage = "some usage";
+    gchar *args[] = { "nick", NULL };
+
+    mock_connection_status(JABBER_CONNECTED);
+    expect_cons_show("Usage: some usage");
+
+    gboolean result = cmd_roster(args, *help);
+    assert_true(result);
+
+    free(help);
+}
+
+void cmd_roster_nick_shows_message_when_no_nick(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    help->usage = "some usage";
+    gchar *args[] = { "nick", "bob@server.org", NULL };
+
+    mock_connection_status(JABBER_CONNECTED);
+    expect_cons_show("Usage: some usage");
+
+    gboolean result = cmd_roster(args, *help);
+    assert_true(result);
+
+    free(help);
+}
+
+void cmd_roster_nick_shows_message_when_no_contact_exists(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    help->usage = "some usage";
+    gchar *args[] = { "nick", "bob@server.org", "bobster", NULL };
+
+    roster_init();
+    mock_connection_status(JABBER_CONNECTED);
+    expect_cons_show("Contact not found in roster: bob@server.org");
+
+    gboolean result = cmd_roster(args, *help);
+    assert_true(result);
+
+    free(help);
+    roster_free();
+}
+
+void cmd_roster_nick_sends_name_change_request(void **state)
+{
+    char *jid = "bob@server.org";
+    char *nick = "bobster";
+    mock_cons_show();
+    mock_roster_send_name_change();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    gchar *args[] = { "nick", jid, nick, NULL };
+
+    roster_init();
+    GSList *groups = NULL;
+    groups = g_slist_append(groups, "group1");
+    roster_add(jid, "bob", groups, "both", FALSE);
+
+    mock_connection_status(JABBER_CONNECTED);
+    roster_send_name_change_expect(jid, nick, groups);
+    expect_cons_show("Nickname for bob@server.org set to: bobster.");
+
+    gboolean result = cmd_roster(args, *help);
+    assert_true(result);
+
+    PContact contact = roster_get_contact(jid);
+    assert_string_equal(p_contact_name(contact), nick);
+
+    free(help);
+    roster_free();
+}
+
+void cmd_roster_clearnick_shows_message_when_no_jid(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    help->usage = "some usage";
+    gchar *args[] = { "clearnick", NULL };
+
+    mock_connection_status(JABBER_CONNECTED);
+    expect_cons_show("Usage: some usage");
+
+    gboolean result = cmd_roster(args, *help);
+    assert_true(result);
+
+    free(help);
+}
+
+void cmd_roster_clearnick_shows_message_when_no_contact_exists(void **state)
+{
+    mock_cons_show();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    help->usage = "some usage";
+    gchar *args[] = { "clearnick", "bob@server.org", NULL };
+
+    roster_init();
+    mock_connection_status(JABBER_CONNECTED);
+    expect_cons_show("Contact not found in roster: bob@server.org");
+
+    gboolean result = cmd_roster(args, *help);
+    assert_true(result);
+
+    free(help);
+    roster_free();
+}
+
+void cmd_roster_clearnick_sends_name_change_request_with_empty_nick(void **state)
+{
+    char *jid = "bob@server.org";
+    mock_cons_show();
+    mock_roster_send_name_change();
+    CommandHelp *help = malloc(sizeof(CommandHelp));
+    gchar *args[] = { "clearnick", jid, NULL };
+
+    roster_init();
+    GSList *groups = NULL;
+    groups = g_slist_append(groups, "group1");
+    roster_add(jid, "bob", groups, "both", FALSE);
+
+    mock_connection_status(JABBER_CONNECTED);
+    roster_send_name_change_expect(jid, NULL, groups);
+    expect_cons_show("Nickname for bob@server.org removed.");
+
+    gboolean result = cmd_roster(args, *help);
+    assert_true(result);
+
+    PContact contact = roster_get_contact(jid);
+    assert_null(p_contact_name(contact));
+
+    free(help);
+    roster_free();
+}
diff --git a/tests/test_cmd_roster.h b/tests/test_cmd_roster.h
index 418b3bc5..79f69ec8 100644
--- a/tests/test_cmd_roster.h
+++ b/tests/test_cmd_roster.h
@@ -7,3 +7,10 @@ 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_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);
+void cmd_roster_nick_sends_name_change_request(void **state);
+void cmd_roster_clearnick_shows_message_when_no_jid(void **state);
+void cmd_roster_clearnick_shows_message_when_no_contact_exists(void **state);
+void cmd_roster_clearnick_sends_name_change_request_with_empty_nick(void **state);
diff --git a/tests/testsuite.c b/tests/testsuite.c
index bc669735..f5bc8faf 100644
--- a/tests/testsuite.c
+++ b/tests/testsuite.c
@@ -511,6 +511,13 @@ int 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_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),
+        unit_test(cmd_roster_nick_sends_name_change_request),
+        unit_test(cmd_roster_clearnick_shows_message_when_no_jid),
+        unit_test(cmd_roster_clearnick_shows_message_when_no_contact_exists),
+        unit_test(cmd_roster_clearnick_sends_name_change_request_with_empty_nick),
     };
 
     return run_tests(all_tests);
diff --git a/tests/xmpp/mock_xmpp.c b/tests/xmpp/mock_xmpp.c
index 43f31e6c..9174e569 100644
--- a/tests/xmpp/mock_xmpp.c
+++ b/tests/xmpp/mock_xmpp.c
@@ -109,6 +109,15 @@ _mock_roster_send_remove(const char * const barejid)
     check_expected(barejid);
 }
 
+static void
+_mock_roster_send_name_change(const char * const barejid, const char * const new_name,
+    GSList *groups)
+{
+    check_expected(barejid);
+    check_expected(new_name);
+    check_expected(groups);
+}
+
 void
 mock_jabber_connect_with_details(void)
 {
@@ -165,6 +174,12 @@ mock_roster_send_remove(void)
 }
 
 void
+mock_roster_send_name_change(void)
+{
+    roster_send_name_change = _mock_roster_send_name_change;
+}
+
+void
 bookmark_get_list_returns(GList *bookmarks)
 {
     bookmark_get_list = _mock_bookmark_get_list;
@@ -298,3 +313,15 @@ roster_send_remove_expect(char *jid)
 {
     expect_string(_mock_roster_send_remove, barejid, jid);
 }
+
+void
+roster_send_name_change_expect(char *jid, char *nick, GSList *groups)
+{
+    expect_string(_mock_roster_send_name_change, barejid, jid);
+    if (nick == NULL) {
+        expect_value(_mock_roster_send_name_change, new_name, NULL);
+    } else {
+        expect_string(_mock_roster_send_name_change, new_name, nick);
+    }
+    expect_memory(_mock_roster_send_name_change, groups, groups, sizeof(GSList));
+}
diff --git a/tests/xmpp/mock_xmpp.h b/tests/xmpp/mock_xmpp.h
index a917f9b8..a5f93fd2 100644
--- a/tests/xmpp/mock_xmpp.h
+++ b/tests/xmpp/mock_xmpp.h
@@ -42,4 +42,7 @@ void roster_send_add_new_expect(char *jid, char *nick);
 void mock_roster_send_remove(void);
 void roster_send_remove_expect(char *jid);
 
+void mock_roster_send_name_change(void);
+void roster_send_name_change_expect(char *jid, char *name, GSList *groups);
+
 #endif