From 5bb3fab17c08842723dd6af995e150e4d337fa7b Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 13 Apr 2014 01:01:53 +0100 Subject: [PATCH 1/4] Implemented "/bookmark add" for private storage Issue #194 --- src/command/command.c | 6 +- src/xmpp/bookmark.c | 165 +++++++++++++++++++----------------------- src/xmpp/stanza.c | 97 ++++++++++++++++++++++++- src/xmpp/stanza.h | 2 +- 4 files changed, 175 insertions(+), 95 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 4174f8a8..c8d34c0d 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -300,10 +300,10 @@ static struct cmd_t command_defs[] = { "/bookmark", cmd_bookmark, parse_args, 0, 4, NULL, - { "/bookmark [add|list|remove] [room@server] [autojoin on|off] [nick nickname]", + { "/bookmark [add|list|remove] [room@server] [autojoin] [nick]", "Manage bookmarks.", - { "/bookmark [add|list|remove] [room@server] [autojoin on|off] [nick nickname]", - "---------------------------------------------------------------------------", + { "/bookmark [add|list|remove] [room@server] [autojoin] [nick]", + "-----------------------------------------------------------", "Manage bookmarks.", NULL } } }, diff --git a/src/xmpp/bookmark.c b/src/xmpp/bookmark.c index 6cc4775f..1e1c0ea5 100644 --- a/src/xmpp/bookmark.c +++ b/src/xmpp/bookmark.c @@ -74,12 +74,22 @@ bookmark_request(void) xmpp_timed_handler_add(conn, _bookmark_handle_delete, BOOKMARK_TIMEOUT, id); xmpp_id_handler_add(conn, _bookmark_handle_result, id, id); - iq = stanza_create_storage_bookmarks(ctx); + iq = stanza_create_bookmarks_storage_request(ctx); xmpp_stanza_set_id(iq, id); xmpp_send(conn, iq); xmpp_stanza_release(iq); } +static int +_match_bookmark_by_jid(gconstpointer a, gconstpointer b) +{ + Bookmark *bookmark_a = (Bookmark *) a; + Bookmark *bookmark_b = (Bookmark *) b; + + return strcmp(bookmark_a->jid, bookmark_b->jid); +} + + static gboolean _bookmark_add(const char *jid, const char *nick, gboolean autojoin) { @@ -91,103 +101,81 @@ _bookmark_add(const char *jid, const char *nick, gboolean autojoin) xmpp_conn_t *conn = connection_get_conn(); xmpp_ctx_t *ctx = connection_get_ctx(); - /* TODO: send request */ - xmpp_stanza_t *stanza = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(stanza, STANZA_NAME_IQ); + /* this may be command for modifying */ + Bookmark *item = malloc(sizeof(*item)); + item->jid = strdup(jid); + if (nick != NULL) { + item->nick = strdup(nick); + } else { + item->nick = NULL; + } + item->autojoin = autojoin; + + GList *found = g_list_find_custom(bookmark_list, item, _match_bookmark_by_jid); + if (found != NULL) { + bookmark_list = g_list_remove_link(bookmark_list, found); + } + bookmark_list = g_list_append(bookmark_list, item); + + autocomplete_remove(bookmark_ac, jid); + autocomplete_add(bookmark_ac, jid); + + xmpp_stanza_t *iq = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(iq, STANZA_NAME_IQ); char *id = generate_unique_id("bookmark_add"); - xmpp_stanza_set_id(stanza, id); - xmpp_stanza_set_type(stanza, STANZA_TYPE_SET); + xmpp_stanza_set_id(iq, id); + xmpp_stanza_set_type(iq, STANZA_TYPE_SET); - xmpp_stanza_t *pubsub = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(pubsub, STANZA_NAME_PUBSUB); - xmpp_stanza_set_ns(pubsub, STANZA_NS_PUBSUB); - xmpp_stanza_add_child(stanza, pubsub); - - xmpp_stanza_t *publish = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(publish, STANZA_NAME_PUBLISH); - xmpp_stanza_set_attribute(publish, STANZA_ATTR_NODE, "storage:bookmarks"); - xmpp_stanza_add_child(pubsub, publish); - - xmpp_stanza_t *item = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(item, STANZA_NAME_ITEM); - xmpp_stanza_add_child(publish, item); + xmpp_stanza_t *query = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(query, STANZA_NAME_QUERY); + xmpp_stanza_set_ns(query, "jabber:iq:private"); xmpp_stanza_t *storage = xmpp_stanza_new(ctx); xmpp_stanza_set_name(storage, STANZA_NAME_STORAGE); - xmpp_stanza_set_ns(storage, "storage;bookmarks"); - xmpp_stanza_add_child(item, storage); + xmpp_stanza_set_ns(storage, "storage:bookmarks"); - xmpp_stanza_t *conference = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(conference, STANZA_NAME_CONFERENCE); - xmpp_stanza_set_attribute(conference, STANZA_ATTR_JID, jid); + GList *curr = bookmark_list; + while (curr != NULL) { + Bookmark *bookmark = curr->data; + xmpp_stanza_t *conference = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(conference, STANZA_NAME_CONFERENCE); + xmpp_stanza_set_attribute(conference, STANZA_ATTR_JID, bookmark->jid); - if (autojoin) { - xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "true"); - } else { - xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "false"); + Jid *jidp = jid_create(bookmark->jid); + xmpp_stanza_set_attribute(conference, STANZA_ATTR_NAME, jidp->localpart); + jid_destroy(jidp); + + if (bookmark->autojoin) { + xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "true"); + } else { + xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "false"); + } + + if (bookmark->nick != NULL) { + xmpp_stanza_t *nick_st = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(nick_st, STANZA_NAME_NICK); + xmpp_stanza_t *nick_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(nick_text, bookmark->nick); + xmpp_stanza_add_child(nick_st, nick_text); + xmpp_stanza_add_child(conference, nick_st); + + xmpp_stanza_release(nick_text); + xmpp_stanza_release(nick_st); + } + + xmpp_stanza_add_child(storage, conference); + xmpp_stanza_release(conference); + + curr = curr->next; } - xmpp_stanza_add_child(storage, conference); + xmpp_stanza_add_child(query, storage); + xmpp_stanza_add_child(iq, query); + xmpp_stanza_release(storage); + xmpp_stanza_release(query); - if (nick != NULL) { - xmpp_stanza_t *nick_st = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(nick_st, STANZA_NAME_NICK); - xmpp_stanza_set_text(nick_st, nick); - xmpp_stanza_add_child(conference, nick_st); - } - - xmpp_stanza_t *publish_options = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(publish_options, STANZA_NAME_PUBLISH_OPTIONS); - xmpp_stanza_add_child(pubsub, publish_options); - - xmpp_stanza_t *x = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(x, STANZA_NAME_X); - xmpp_stanza_set_ns(x, STANZA_NS_DATA); - xmpp_stanza_set_attribute(x, STANZA_ATTR_TYPE, "submit"); - xmpp_stanza_add_child(publish_options, x); - - xmpp_stanza_t *form_type = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(form_type, STANZA_NAME_FIELD); - xmpp_stanza_set_attribute(form_type, STANZA_ATTR_VAR, "FORM_TYPE"); - xmpp_stanza_set_attribute(form_type, STANZA_ATTR_TYPE, "hidden"); - xmpp_stanza_t *form_type_value = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(form_type_value, STANZA_NAME_VALUE); - xmpp_stanza_t *form_type_value_text = xmpp_stanza_new(ctx); - xmpp_stanza_set_text(form_type_value_text, "http://jabber.org/protocol/pubsub#publish-options"); - xmpp_stanza_add_child(form_type_value, form_type_value_text); - xmpp_stanza_add_child(form_type, form_type_value); - xmpp_stanza_add_child(x, form_type); - - xmpp_stanza_t *persist_items = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(persist_items, STANZA_NAME_FIELD); - xmpp_stanza_set_attribute(persist_items, STANZA_ATTR_VAR, "pubsub#persist_items"); - xmpp_stanza_t *persist_items_value = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(persist_items_value, STANZA_NAME_VALUE); - xmpp_stanza_t *persist_items_value_text = xmpp_stanza_new(ctx); - xmpp_stanza_set_text(persist_items_value_text, "true"); - xmpp_stanza_add_child(persist_items_value, persist_items_value_text); - xmpp_stanza_add_child(persist_items, persist_items_value); - xmpp_stanza_add_child(x, persist_items); - - xmpp_stanza_t *access_model = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(access_model, STANZA_NAME_FIELD); - xmpp_stanza_set_attribute(access_model, STANZA_ATTR_VAR, "pubsub#access_model"); - xmpp_stanza_t *access_model_value = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(access_model_value, STANZA_NAME_VALUE); - xmpp_stanza_t *access_model_value_text = xmpp_stanza_new(ctx); - xmpp_stanza_set_text(access_model_value_text, "whitelist"); - xmpp_stanza_add_child(access_model_value, access_model_value_text); - xmpp_stanza_add_child(access_model, access_model_value); - xmpp_stanza_add_child(x, access_model); - - xmpp_send(conn, stanza); - xmpp_stanza_release(stanza); - - /* TODO: manage bookmark_list */ - - /* this may be command for modifying */ - autocomplete_remove(bookmark_ac, jid); - autocomplete_add(bookmark_ac, jid); + xmpp_send(conn, iq); + xmpp_stanza_release(iq); return added; } @@ -307,7 +295,6 @@ _bookmark_handle_result(xmpp_conn_t * const conn, item->autojoin = autojoin_val; bookmark_list = g_list_append(bookmark_list, item); - /* TODO: preference whether autojoin */ if (autojoin_val) { if (autojoin_count < BOOKMARK_AUTOJOIN_MAX) { diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 38b72004..8000da62 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -38,7 +38,7 @@ static int _field_compare(FormField *f1, FormField *f2); #if 0 xmpp_stanza_t * -stanza_create_pubsub_bookmarks(xmpp_ctx_t *ctx) +stanza_create_bookmarks_pubsub_request(xmpp_ctx_t *ctx) { xmpp_stanza_t *iq, *pubsub, *items; @@ -66,7 +66,7 @@ stanza_create_pubsub_bookmarks(xmpp_ctx_t *ctx) #endif xmpp_stanza_t * -stanza_create_storage_bookmarks(xmpp_ctx_t *ctx) +stanza_create_bookmarks_storage_request(xmpp_ctx_t *ctx) { xmpp_stanza_t *iq, *query, *storage; @@ -93,6 +93,99 @@ stanza_create_storage_bookmarks(xmpp_ctx_t *ctx) return iq; } +#if 0 +xmpp_stanza_t * +stanza_create_bookmarks_pubsub_add(xmpp_ctx_t *ctx, const char * const jid, + const gboolean autojoin, const char * const nick) +{ + xmpp_stanza_t *stanza = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(stanza, STANZA_NAME_IQ); + char *id = generate_unique_id("bookmark_add"); + xmpp_stanza_set_id(stanza, id); + xmpp_stanza_set_type(stanza, STANZA_TYPE_SET); + + xmpp_stanza_t *pubsub = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(pubsub, STANZA_NAME_PUBSUB); + xmpp_stanza_set_ns(pubsub, STANZA_NS_PUBSUB); + xmpp_stanza_add_child(stanza, pubsub); + + xmpp_stanza_t *publish = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(publish, STANZA_NAME_PUBLISH); + xmpp_stanza_set_attribute(publish, STANZA_ATTR_NODE, "storage:bookmarks"); + xmpp_stanza_add_child(pubsub, publish); + + xmpp_stanza_t *item = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(item, STANZA_NAME_ITEM); + xmpp_stanza_add_child(publish, item); + + xmpp_stanza_t *conference = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(conference, STANZA_NAME_CONFERENCE); + xmpp_stanza_set_ns(conference, "storage:bookmarks"); + xmpp_stanza_set_attribute(conference, STANZA_ATTR_JID, jid); + + if (autojoin) { + xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "true"); + } else { + xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "false"); + } + + if (nick != NULL) { + xmpp_stanza_t *nick_st = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(nick_st, STANZA_NAME_NICK); + xmpp_stanza_set_text(nick_st, nick); + xmpp_stanza_add_child(conference, nick_st); + } + + xmpp_stanza_add_child(item, conference); + + xmpp_stanza_t *publish_options = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(publish_options, STANZA_NAME_PUBLISH_OPTIONS); + xmpp_stanza_add_child(pubsub, publish_options); + + xmpp_stanza_t *x = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(x, STANZA_NAME_X); + xmpp_stanza_set_ns(x, STANZA_NS_DATA); + xmpp_stanza_set_attribute(x, STANZA_ATTR_TYPE, "submit"); + xmpp_stanza_add_child(publish_options, x); + + xmpp_stanza_t *form_type = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(form_type, STANZA_NAME_FIELD); + xmpp_stanza_set_attribute(form_type, STANZA_ATTR_VAR, "FORM_TYPE"); + xmpp_stanza_set_attribute(form_type, STANZA_ATTR_TYPE, "hidden"); + xmpp_stanza_t *form_type_value = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(form_type_value, STANZA_NAME_VALUE); + xmpp_stanza_t *form_type_value_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(form_type_value_text, "http://jabber.org/protocol/pubsub#publish-options"); + xmpp_stanza_add_child(form_type_value, form_type_value_text); + xmpp_stanza_add_child(form_type, form_type_value); + xmpp_stanza_add_child(x, form_type); + + xmpp_stanza_t *persist_items = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(persist_items, STANZA_NAME_FIELD); + xmpp_stanza_set_attribute(persist_items, STANZA_ATTR_VAR, "pubsub#persist_items"); + xmpp_stanza_t *persist_items_value = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(persist_items_value, STANZA_NAME_VALUE); + xmpp_stanza_t *persist_items_value_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(persist_items_value_text, "true"); + xmpp_stanza_add_child(persist_items_value, persist_items_value_text); + xmpp_stanza_add_child(persist_items, persist_items_value); + xmpp_stanza_add_child(x, persist_items); + + xmpp_stanza_t *access_model = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(access_model, STANZA_NAME_FIELD); + xmpp_stanza_set_attribute(access_model, STANZA_ATTR_VAR, "pubsub#access_model"); + xmpp_stanza_t *access_model_value = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(access_model_value, STANZA_NAME_VALUE); + xmpp_stanza_t *access_model_value_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(access_model_value_text, "whitelist"); + xmpp_stanza_add_child(access_model_value, access_model_value_text); + xmpp_stanza_add_child(access_model, access_model_value); + xmpp_stanza_add_child(x, access_model); + + return stanza; +} +#endif + xmpp_stanza_t * stanza_create_chat_state(xmpp_ctx_t *ctx, const char * const recipient, const char * const state) diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h index e7a08139..d71d3ea1 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -148,7 +148,7 @@ typedef struct data_form_t { GSList *fields; } DataForm; -xmpp_stanza_t* stanza_create_storage_bookmarks(xmpp_ctx_t *ctx); +xmpp_stanza_t* stanza_create_bookmarks_storage_request(xmpp_ctx_t *ctx); xmpp_stanza_t* stanza_create_chat_state(xmpp_ctx_t *ctx, const char * const recipient, const char * const state); From f7daaf09538298e7548407a2c5f3c35232e98c66 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 13 Apr 2014 01:10:54 +0100 Subject: [PATCH 2/4] Clean up after removing bookmark Issue #194 --- src/xmpp/bookmark.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/xmpp/bookmark.c b/src/xmpp/bookmark.c index 1e1c0ea5..08b36412 100644 --- a/src/xmpp/bookmark.c +++ b/src/xmpp/bookmark.c @@ -114,6 +114,8 @@ _bookmark_add(const char *jid, const char *nick, gboolean autojoin) GList *found = g_list_find_custom(bookmark_list, item, _match_bookmark_by_jid); if (found != NULL) { bookmark_list = g_list_remove_link(bookmark_list, found); + _bookmark_item_destroy(found->data); + g_list_free(found); } bookmark_list = g_list_append(bookmark_list, item); From 0b2f810431cd1b763be7edf286f6a4bb238e7614 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 13 Apr 2014 01:20:36 +0100 Subject: [PATCH 3/4] Added bookmark autocomplete to /join Issue #194 --- src/command/command.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/command/command.c b/src/command/command.c index c8d34c0d..d7cf67e3 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -1835,6 +1835,12 @@ _join_autocomplete(char *input, int *size) gboolean result = FALSE; input[*size] = '\0'; + + found = autocomplete_param_with_func(input, size, "/join", bookmark_find); + if (found != NULL) { + return found; + } + gchar **args = parse_args(input, 2, 4, &result); if ((strncmp(input, "/join", 5) == 0) && (result == TRUE)) { From 73c146c65ae8e2a5751d3f3138c5b8dc80379bef Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 13 Apr 2014 01:39:54 +0100 Subject: [PATCH 4/4] Implemented "/bookmark remove" for private storage Issue #194 --- src/xmpp/bookmark.c | 178 +++++++++++++++++++++++++------------------- 1 file changed, 101 insertions(+), 77 deletions(-) diff --git a/src/xmpp/bookmark.c b/src/xmpp/bookmark.c index 08b36412..2734b7c1 100644 --- a/src/xmpp/bookmark.c +++ b/src/xmpp/bookmark.c @@ -50,6 +50,8 @@ static int _bookmark_handle_result(xmpp_conn_t * const conn, static int _bookmark_handle_delete(xmpp_conn_t * const conn, void * const userdata); static void _bookmark_item_destroy(gpointer item); +static int _match_bookmark_by_jid(gconstpointer a, gconstpointer b); +static void _send_bookmarks(void); void bookmark_request(void) @@ -80,16 +82,6 @@ bookmark_request(void) xmpp_stanza_release(iq); } -static int -_match_bookmark_by_jid(gconstpointer a, gconstpointer b) -{ - Bookmark *bookmark_a = (Bookmark *) a; - Bookmark *bookmark_b = (Bookmark *) b; - - return strcmp(bookmark_a->jid, bookmark_b->jid); -} - - static gboolean _bookmark_add(const char *jid, const char *nick, gboolean autojoin) { @@ -98,9 +90,6 @@ _bookmark_add(const char *jid, const char *nick, gboolean autojoin) added = FALSE; } - xmpp_conn_t *conn = connection_get_conn(); - xmpp_ctx_t *ctx = connection_get_ctx(); - /* this may be command for modifying */ Bookmark *item = malloc(sizeof(*item)); item->jid = strdup(jid); @@ -122,62 +111,7 @@ _bookmark_add(const char *jid, const char *nick, gboolean autojoin) autocomplete_remove(bookmark_ac, jid); autocomplete_add(bookmark_ac, jid); - xmpp_stanza_t *iq = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(iq, STANZA_NAME_IQ); - char *id = generate_unique_id("bookmark_add"); - xmpp_stanza_set_id(iq, id); - 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, "jabber:iq:private"); - - xmpp_stanza_t *storage = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(storage, STANZA_NAME_STORAGE); - xmpp_stanza_set_ns(storage, "storage:bookmarks"); - - GList *curr = bookmark_list; - while (curr != NULL) { - Bookmark *bookmark = curr->data; - xmpp_stanza_t *conference = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(conference, STANZA_NAME_CONFERENCE); - xmpp_stanza_set_attribute(conference, STANZA_ATTR_JID, bookmark->jid); - - Jid *jidp = jid_create(bookmark->jid); - xmpp_stanza_set_attribute(conference, STANZA_ATTR_NAME, jidp->localpart); - jid_destroy(jidp); - - if (bookmark->autojoin) { - xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "true"); - } else { - xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "false"); - } - - if (bookmark->nick != NULL) { - xmpp_stanza_t *nick_st = xmpp_stanza_new(ctx); - xmpp_stanza_set_name(nick_st, STANZA_NAME_NICK); - xmpp_stanza_t *nick_text = xmpp_stanza_new(ctx); - xmpp_stanza_set_text(nick_text, bookmark->nick); - xmpp_stanza_add_child(nick_st, nick_text); - xmpp_stanza_add_child(conference, nick_st); - - xmpp_stanza_release(nick_text); - xmpp_stanza_release(nick_st); - } - - xmpp_stanza_add_child(storage, conference); - xmpp_stanza_release(conference); - - curr = curr->next; - } - - xmpp_stanza_add_child(query, storage); - xmpp_stanza_add_child(iq, query); - xmpp_stanza_release(storage); - xmpp_stanza_release(query); - - xmpp_send(conn, iq); - xmpp_stanza_release(iq); + _send_bookmarks(); return added; } @@ -185,18 +119,35 @@ _bookmark_add(const char *jid, const char *nick, gboolean autojoin) static gboolean _bookmark_remove(const char *jid, gboolean autojoin) { - gboolean removed = FALSE; - if (autocomplete_contains(bookmark_ac, jid)) { - removed = TRUE; - } - /* TODO: manage bookmark_list */ + Bookmark *item = malloc(sizeof(*item)); + item->jid = strdup(jid); + item->nick = NULL; + item->autojoin = autojoin; + + GList *found = g_list_find_custom(bookmark_list, item, _match_bookmark_by_jid); + _bookmark_item_destroy(item); + gboolean removed = found != NULL; + + // set autojoin FALSE if (autojoin) { - /* TODO: just set autojoin=0 */ + if (found != NULL) { + Bookmark *bookmark = found->data; + bookmark->autojoin = FALSE; + g_list_free(found); + } + + // remove bookmark } else { - /* TODO: send request */ + if (found != NULL) { + bookmark_list = g_list_remove_link(bookmark_list, found); + _bookmark_item_destroy(found->data); + g_list_free(found); + } autocomplete_remove(bookmark_ac, jid); } + _send_bookmarks(); + return removed; } @@ -359,6 +310,79 @@ _bookmark_item_destroy(gpointer item) free(p); } +static int +_match_bookmark_by_jid(gconstpointer a, gconstpointer b) +{ + Bookmark *bookmark_a = (Bookmark *) a; + Bookmark *bookmark_b = (Bookmark *) b; + + return strcmp(bookmark_a->jid, bookmark_b->jid); +} + +static void +_send_bookmarks(void) +{ + xmpp_conn_t *conn = connection_get_conn(); + xmpp_ctx_t *ctx = connection_get_ctx(); + + xmpp_stanza_t *iq = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(iq, STANZA_NAME_IQ); + char *id = generate_unique_id("bookmarks_update"); + xmpp_stanza_set_id(iq, id); + 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, "jabber:iq:private"); + + xmpp_stanza_t *storage = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(storage, STANZA_NAME_STORAGE); + xmpp_stanza_set_ns(storage, "storage:bookmarks"); + + GList *curr = bookmark_list; + while (curr != NULL) { + Bookmark *bookmark = curr->data; + xmpp_stanza_t *conference = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(conference, STANZA_NAME_CONFERENCE); + xmpp_stanza_set_attribute(conference, STANZA_ATTR_JID, bookmark->jid); + + Jid *jidp = jid_create(bookmark->jid); + xmpp_stanza_set_attribute(conference, STANZA_ATTR_NAME, jidp->localpart); + jid_destroy(jidp); + + if (bookmark->autojoin) { + xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "true"); + } else { + xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "false"); + } + + if (bookmark->nick != NULL) { + xmpp_stanza_t *nick_st = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(nick_st, STANZA_NAME_NICK); + xmpp_stanza_t *nick_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(nick_text, bookmark->nick); + xmpp_stanza_add_child(nick_st, nick_text); + xmpp_stanza_add_child(conference, nick_st); + + xmpp_stanza_release(nick_text); + xmpp_stanza_release(nick_st); + } + + xmpp_stanza_add_child(storage, conference); + xmpp_stanza_release(conference); + + curr = curr->next; + } + + xmpp_stanza_add_child(query, storage); + xmpp_stanza_add_child(iq, query); + xmpp_stanza_release(storage); + xmpp_stanza_release(query); + + xmpp_send(conn, iq); + xmpp_stanza_release(iq); +} + void bookmark_init_module(void) { @@ -367,4 +391,4 @@ bookmark_init_module(void) bookmark_get_list = _bookmark_get_list; bookmark_find = _bookmark_find; bookmark_autocomplete_reset = _bookmark_autocomplete_reset; -} +} \ No newline at end of file