diff options
author | James Booth <boothj5@gmail.com> | 2013-05-22 21:15:05 +0100 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2013-05-22 21:15:05 +0100 |
commit | 15c782059435bbea671938124d6ff43dc0186b7e (patch) | |
tree | 2df2fa8bcb3b62e67d57def132fd5d4e45da945d /src/xmpp | |
parent | fce295d64a810367053edab9909c9c04e90d512c (diff) | |
download | profani-tty-15c782059435bbea671938124d6ff43dc0186b7e.tar.gz |
Reuse roster_add in roster_update
Diffstat (limited to 'src/xmpp')
-rw-r--r-- | src/xmpp/roster.c | 317 |
1 files changed, 154 insertions, 163 deletions
diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c index 4a4f1709..9d2b765f 100644 --- a/src/xmpp/roster.c +++ b/src/xmpp/roster.c @@ -78,115 +78,6 @@ roster_request(void) xmpp_stanza_release(iq); } -char * -roster_barejid_from_name(const char * const name) -{ - return g_hash_table_lookup(name_to_barejid, name); -} - -static int -_roster_handle_push(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, - void * const userdata) -{ - xmpp_stanza_t *query = - xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); - xmpp_stanza_t *item = - xmpp_stanza_get_child_by_name(query, STANZA_NAME_ITEM); - - if (item == NULL) { - return 1; - } - - // if from attribute exists and it is not current users barejid, ignore push - Jid *my_jid = jid_create(jabber_get_fulljid()); - const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); - if ((from != NULL) && (strcmp(from, my_jid->barejid) != 0)) { - jid_destroy(my_jid); - return 1; - } - jid_destroy(my_jid); - - const char *barejid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID); - const char *name = xmpp_stanza_get_attribute(item, STANZA_ATTR_NAME); - const char *sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION); - const char *ask = xmpp_stanza_get_attribute(item, STANZA_ATTR_ASK); - - // remove from roster - if (g_strcmp0(sub, "remove") == 0) { - // remove barejid and name - autocomplete_remove(barejid_ac, barejid); - autocomplete_remove(name_ac, name); - g_hash_table_remove(name_to_barejid, name); - - // remove each fulljid - PContact contact = roster_get_contact(barejid); - GList *resources = p_contact_get_available_resources(contact); - while (resources != NULL) { - GString *fulljid = g_string_new(strdup(barejid)); - g_string_append(fulljid, "/"); - g_string_append(fulljid, strdup(resources->data)); - autocomplete_remove(fulljid_ac, fulljid->str); - g_string_free(fulljid, TRUE); - resources = g_list_next(resources); - } - - // remove the contact - g_hash_table_remove(contacts, barejid); - - // otherwise update local roster - } else { - - // check for pending out subscriptions - gboolean pending_out = FALSE; - if ((ask != NULL) && (strcmp(ask, "subscribe") == 0)) { - pending_out = TRUE; - } - - // update the local roster - roster_update(barejid, name, sub, pending_out); - } - - return 1; -} - -static int -_roster_handle_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, - void * const userdata) -{ - const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); - - // handle initial roster response - if (g_strcmp0(id, "roster") == 0) { - xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); - xmpp_stanza_t *item = xmpp_stanza_get_children(query); - - while (item != NULL) { - const char *barejid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID); - const char *name = xmpp_stanza_get_attribute(item, STANZA_ATTR_NAME); - const char *sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION); - - gboolean pending_out = FALSE; - const char *ask = xmpp_stanza_get_attribute(item, STANZA_ATTR_ASK); - if (g_strcmp0(ask, "subscribe") == 0) { - pending_out = TRUE; - } - - gboolean added = roster_add(barejid, name, sub, pending_out); - - if (!added) { - log_warning("Attempt to add contact twice: %s", barejid); - } - - item = xmpp_stanza_get_next(item); - } - - contact_presence_t conn_presence = accounts_get_login_presence(jabber_get_account_name()); - presence_update(conn_presence, NULL, 0); - } - - return 1; -} - void roster_init(void) { @@ -252,6 +143,51 @@ roster_add(const char * const barejid, const char * const name, return added; } +void +roster_update(const char * const barejid, const char * const name, + const char * const subscription, gboolean pending_out) +{ + PContact contact = g_hash_table_lookup(contacts, barejid); + + if (contact == NULL) { + roster_add(barejid, name, subscription, pending_out); + } else { + p_contact_set_subscription(contact, subscription); + p_contact_set_pending_out(contact, pending_out); + + const char * const new_handle = name; + const char * current_handle = NULL; + if (p_contact_name(contact) != NULL) { + current_handle = strdup(p_contact_name(contact)); + } + + p_contact_set_name(contact, new_handle); + + // current handle exists already + if (current_handle != NULL) { + autocomplete_remove(name_ac, current_handle); + g_hash_table_remove(name_to_barejid, current_handle); + + if (new_handle != NULL) { + autocomplete_add(name_ac, strdup(new_handle)); + g_hash_table_insert(name_to_barejid, strdup(new_handle), strdup(barejid)); + } else { + autocomplete_add(name_ac, strdup(barejid)); + g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); + } + // no current handle + } else { + if (new_handle != NULL) { + autocomplete_remove(name_ac, barejid); + g_hash_table_remove(name_to_barejid, barejid); + + autocomplete_add(name_ac, strdup(new_handle)); + g_hash_table_insert(name_to_barejid, strdup(new_handle), strdup(barejid)); + } + } + } +} + gboolean roster_update_presence(const char * const barejid, Resource *resource, GDateTime *last_activity) @@ -340,60 +276,6 @@ roster_change_name(const char * const barejid, const char * const new_name) } } -void -roster_update(const char * const barejid, const char * const name, - const char * const subscription, gboolean pending_out) -{ - PContact contact = g_hash_table_lookup(contacts, barejid); - - if (contact == NULL) { - contact = p_contact_new(barejid, name, subscription, NULL, pending_out); - g_hash_table_insert(contacts, strdup(barejid), contact); - if (name != NULL) { - autocomplete_add(name_ac, strdup(name)); - g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid)); - } else { - autocomplete_add(name_ac, strdup(barejid)); - g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); - } - autocomplete_add(barejid_ac, strdup(barejid)); - } else { - p_contact_set_subscription(contact, subscription); - p_contact_set_pending_out(contact, pending_out); - - const char * const new_handle = name; - const char * current_handle = NULL; - if (p_contact_name(contact) != NULL) { - current_handle = strdup(p_contact_name(contact)); - } - - p_contact_set_name(contact, new_handle); - - // current handle exists already - if (current_handle != NULL) { - autocomplete_remove(name_ac, current_handle); - g_hash_table_remove(name_to_barejid, current_handle); - - if (new_handle != NULL) { - autocomplete_add(name_ac, strdup(new_handle)); - g_hash_table_insert(name_to_barejid, strdup(new_handle), strdup(barejid)); - } else { - autocomplete_add(name_ac, strdup(barejid)); - g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); - } - // no current handle - } else { - if (new_handle != NULL) { - autocomplete_remove(name_ac, barejid); - g_hash_table_remove(name_to_barejid, barejid); - - autocomplete_add(name_ac, strdup(new_handle)); - g_hash_table_insert(name_to_barejid, strdup(new_handle), strdup(barejid)); - } - } - } -} - gboolean roster_has_pending_subscriptions(void) { @@ -447,12 +329,121 @@ roster_find_resource(char *search_str) return autocomplete_complete(fulljid_ac, search_str); } +char * +roster_barejid_from_name(const char * const name) +{ + return g_hash_table_lookup(name_to_barejid, name); +} + PContact roster_get_contact(const char const *barejid) { return g_hash_table_lookup(contacts, barejid); } +static int +_roster_handle_push(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, + void * const userdata) +{ + xmpp_stanza_t *query = + xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); + xmpp_stanza_t *item = + xmpp_stanza_get_child_by_name(query, STANZA_NAME_ITEM); + + if (item == NULL) { + return 1; + } + + // if from attribute exists and it is not current users barejid, ignore push + Jid *my_jid = jid_create(jabber_get_fulljid()); + const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + if ((from != NULL) && (strcmp(from, my_jid->barejid) != 0)) { + jid_destroy(my_jid); + return 1; + } + jid_destroy(my_jid); + + const char *barejid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID); + const char *name = xmpp_stanza_get_attribute(item, STANZA_ATTR_NAME); + const char *sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION); + const char *ask = xmpp_stanza_get_attribute(item, STANZA_ATTR_ASK); + + // remove from roster + if (g_strcmp0(sub, "remove") == 0) { + // remove barejid and name + autocomplete_remove(barejid_ac, barejid); + autocomplete_remove(name_ac, name); + g_hash_table_remove(name_to_barejid, name); + + // remove each fulljid + PContact contact = roster_get_contact(barejid); + GList *resources = p_contact_get_available_resources(contact); + while (resources != NULL) { + GString *fulljid = g_string_new(strdup(barejid)); + g_string_append(fulljid, "/"); + g_string_append(fulljid, strdup(resources->data)); + autocomplete_remove(fulljid_ac, fulljid->str); + g_string_free(fulljid, TRUE); + resources = g_list_next(resources); + } + + // remove the contact + g_hash_table_remove(contacts, barejid); + + // otherwise update local roster + } else { + + // check for pending out subscriptions + gboolean pending_out = FALSE; + if ((ask != NULL) && (strcmp(ask, "subscribe") == 0)) { + pending_out = TRUE; + } + + // update the local roster + roster_update(barejid, name, sub, pending_out); + } + + return 1; +} + +static int +_roster_handle_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, + void * const userdata) +{ + const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); + + // handle initial roster response + if (g_strcmp0(id, "roster") == 0) { + xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); + xmpp_stanza_t *item = xmpp_stanza_get_children(query); + + while (item != NULL) { + const char *barejid = xmpp_stanza_get_attribute(item, STANZA_ATTR_JID); + const char *name = xmpp_stanza_get_attribute(item, STANZA_ATTR_NAME); + const char *sub = xmpp_stanza_get_attribute(item, STANZA_ATTR_SUBSCRIPTION); + + gboolean pending_out = FALSE; + const char *ask = xmpp_stanza_get_attribute(item, STANZA_ATTR_ASK); + if (g_strcmp0(ask, "subscribe") == 0) { + pending_out = TRUE; + } + + gboolean added = roster_add(barejid, name, sub, pending_out); + + if (!added) { + log_warning("Attempt to add contact twice: %s", barejid); + } + + item = xmpp_stanza_get_next(item); + } + + contact_presence_t conn_presence = accounts_get_login_presence(jabber_get_account_name()); + presence_update(conn_presence, NULL, 0); + } + + return 1; +} + static gboolean _key_equals(void *key1, void *key2) { |