From 518b6721fffa447a05a7af4363be14f69811b4d0 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 1 Jun 2013 23:48:24 +0100 Subject: [PATCH] Added /roster add command --- src/command/command.c | 76 ++++++++++++++++++++++++++----------------- src/xmpp/roster.c | 10 ++++++ src/xmpp/xmpp.h | 1 + 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 6309ad75..7dbf0009 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -2008,26 +2008,6 @@ _cmd_msg(gchar **args, struct cmd_help_t help) static gboolean _cmd_roster(gchar **args, struct cmd_help_t help) { - // show roster - if (args[0] == NULL) { - GSList *list = roster_get_contacts(); - cons_show_roster(list); - return TRUE; - } - - // first arg invalid - if (strcmp(args[0], "nick") != 0) { - cons_show("Usage: %s", help.usage); - return TRUE; - } - - if (args[1] == NULL) { - cons_show("Usage: %s", help.usage); - return TRUE; - } - - char *jid = args[1]; - char *name = args[2]; jabber_conn_status_t conn_status = jabber_get_connection_status(); if (conn_status != JABBER_CONNECTED) { @@ -2035,21 +2015,59 @@ _cmd_roster(gchar **args, struct cmd_help_t help) return TRUE; } - // contact does not exist - PContact contact = roster_get_contact(jid); - if (contact == NULL) { - cons_show("Contact not found in roster: %s", jid); + // show roster + if (args[0] == NULL) { + GSList *list = roster_get_contacts(); + cons_show_roster(list); return TRUE; } - roster_change_name(jid, name); + // add contact + if (strcmp(args[0], "add") == 0) { - if (name == NULL) { - cons_show("Nickname for %s removed.", jid); - } else { - cons_show("Nickname for %s set to: %s.", jid, name); + if (args[1] == NULL) { + cons_show("Usage: %s", help.usage); + return TRUE; + } + + char *jid = args[1]; + char *name = args[2]; + + roster_add_new(jid, name); + + return TRUE; } + // change nickname + if (strcmp(args[0], "nick") == 0) { + + if (args[1] == NULL) { + cons_show("Usage: %s", help.usage); + return TRUE; + } + + char *jid = args[1]; + char *name = args[2]; + + // contact does not exist + PContact contact = roster_get_contact(jid); + if (contact == NULL) { + cons_show("Contact not found in roster: %s", jid); + return TRUE; + } + + roster_change_name(jid, name); + + if (name == NULL) { + cons_show("Nickname for %s removed.", jid); + } else { + cons_show("Nickname for %s set to: %s.", jid, name); + } + + return TRUE; + } + + cons_show("Usage: %s", help.usage); return TRUE; } diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c index ce92226c..c2aa245a 100644 --- a/src/xmpp/roster.c +++ b/src/xmpp/roster.c @@ -135,6 +135,16 @@ roster_reset_search_attempts(void) autocomplete_reset(groups_ac); } +void +roster_add_new(const char * const barejid, const char * const name) +{ + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + xmpp_stanza_t *iq = stanza_create_roster_set(ctx, barejid, name, NULL); + 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) diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index 9cc33674..6ca1444d 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -143,5 +143,6 @@ gboolean roster_add(const char * const barejid, const char * const name, GSList *groups, const char * const subscription, gboolean pending_out); 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); #endif