From 01bdc6ae5ac83331ac76beb71cdd0f11e5bb58ca Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 2 Jun 2013 00:06:05 +0100 Subject: [PATCH] Added /roster remove command --- src/command/command.c | 17 +++++++++++++++++ src/xmpp/roster.c | 13 +++++++++++++ src/xmpp/stanza.c | 23 +++++++++++++++++++++++ src/xmpp/stanza.h | 2 ++ src/xmpp/xmpp.h | 1 + 5 files changed, 56 insertions(+) diff --git a/src/command/command.c b/src/command/command.c index 7dbf0009..89751fa3 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -918,7 +918,9 @@ cmd_init(void) autocomplete_add(wins_ac, strdup("tidy")); roster_ac = autocomplete_new(); + autocomplete_add(roster_ac, strdup("add")); autocomplete_add(roster_ac, strdup("nick")); + autocomplete_add(roster_ac, strdup("remove")); theme_load_ac = NULL; @@ -2038,6 +2040,21 @@ _cmd_roster(gchar **args, struct cmd_help_t help) return TRUE; } + // remove contact + if (strcmp(args[0], "remove") == 0) { + + if (args[1] == NULL) { + cons_show("Usage: %s", help.usage); + return TRUE; + } + + char *jid = args[1]; + + roster_remove(jid); + + return TRUE; + } + // change nickname if (strcmp(args[0], "nick") == 0) { diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c index c2aa245a..18000cd4 100644 --- a/src/xmpp/roster.c +++ b/src/xmpp/roster.c @@ -145,6 +145,16 @@ roster_add_new(const char * const barejid, const char * const name) xmpp_stanza_release(iq); } +void +roster_remove(const char * const barejid) +{ + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + xmpp_stanza_t *iq = stanza_create_roster_remove_set(ctx, barejid); + xmpp_send(conn, iq); + xmpp_stanza_release(iq); +} + gboolean roster_add(const char * const barejid, const char * const name, GSList *groups, const char * const subscription, gboolean pending_out) @@ -366,6 +376,9 @@ _roster_handle_push(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, // remove from roster if (g_strcmp0(sub, "remove") == 0) { // remove barejid and name + if (name == NULL) { + name = barejid; + } autocomplete_remove(barejid_ac, barejid); autocomplete_remove(name_ac, name); g_hash_table_remove(name_to_barejid, name); diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 78d85c18..59a89671 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -89,6 +89,29 @@ stanza_create_message(xmpp_ctx_t *ctx, const char * const recipient, return msg; } +xmpp_stanza_t * +stanza_create_roster_remove_set(xmpp_ctx_t *ctx, const char * const barejid) +{ + xmpp_stanza_t *iq = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(iq, STANZA_NAME_IQ); + xmpp_stanza_set_type(iq, STANZA_TYPE_SET); + + xmpp_stanza_t *query = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(query, STANZA_NAME_QUERY); + xmpp_stanza_set_ns(query, XMPP_NS_ROSTER); + + xmpp_stanza_t *item = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(item, STANZA_NAME_ITEM); + xmpp_stanza_set_attribute(item, STANZA_ATTR_JID, barejid); + xmpp_stanza_set_attribute(item, STANZA_ATTR_SUBSCRIPTION, "remove"); + + xmpp_stanza_add_child(query, item); + xmpp_stanza_add_child(iq, query); + + return iq; + +} + xmpp_stanza_t * stanza_create_roster_set(xmpp_ctx_t *ctx, const char * const jid, const char * const handle, GSList *groups) diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h index 169497bc..b35486e8 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -180,5 +180,7 @@ char * stanza_get_show(xmpp_stanza_t *stanza, char *def); xmpp_stanza_t * stanza_create_roster_set(xmpp_ctx_t *ctx, const char * const jid, const char * const handle, GSList *groups); +xmpp_stanza_t * stanza_create_roster_remove_set(xmpp_ctx_t *ctx, + const char * const barejid); #endif diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index 6ca1444d..5469519b 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -144,5 +144,6 @@ gboolean roster_add(const char * const barejid, const char * const name, void roster_change_name(const char * const barejid, const char * const new_name); char * roster_barejid_from_name(const char * const name); void roster_add_new(const char * const barejid, const char * const name); +void roster_remove(const char * const barejid); #endif