diff --git a/src/xmpp/avatar.c b/src/xmpp/avatar.c index 2685472a..0a09f43e 100644 --- a/src/xmpp/avatar.c +++ b/src/xmpp/avatar.c @@ -34,6 +34,7 @@ */ #include +#include #include "log.h" #include "xmpp/connection.h" @@ -42,10 +43,13 @@ #include "xmpp/message.h" #include "xmpp/stanza.h" #include "ui/ui.h" +#include "config/files.h" char *looking_for = NULL; static int _avatar_metadata_nofication(xmpp_stanza_t *const stanza, void *const userdata); +void avatar_request_item_by_id(const char *jid, const char *id); +int avatar_request_item_handler(xmpp_stanza_t *const stanza, void *const userdata); void avatar_pep_subscribe(void) @@ -68,15 +72,14 @@ static int _avatar_metadata_nofication(xmpp_stanza_t *const stanza, void *const userdata) { const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); - from = from; if (!(looking_for && (g_strcmp0(looking_for, from) == 0))) { return 1; } - free(looking_for); - looking_for = NULL; +// free(looking_for); +// looking_for = NULL; xmpp_stanza_t *root = NULL; xmpp_stanza_t *event = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB_EVENT); @@ -108,7 +111,81 @@ _avatar_metadata_nofication(xmpp_stanza_t *const stanza, void *const userdata) const char *id = xmpp_stanza_get_id(info); cons_show("Id for %s is: %s", from, id); + avatar_request_item_by_id(from, id); } return 1; } + +void +avatar_request_item_by_id(const char *jid, const char *id) +{ + xmpp_ctx_t * const ctx = connection_get_ctx(); + //char *id = connection_create_stanza_id(); + + xmpp_stanza_t *iq = stanza_create_avatar_retrieve_data_request(ctx, id, jid); + iq_id_handler_add("retrieve1", avatar_request_item_handler, free, NULL); + + iq_send_stanza(iq); + + //free(id); + xmpp_stanza_release(iq); +} + +int +avatar_request_item_handler(xmpp_stanza_t *const stanza, void *const userdata) +{ + const char *from_attr = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + + if (!from_attr) { + return 1; + } + + if (g_strcmp0(from_attr, looking_for) != 0) { + return 1; + } + free(looking_for); + looking_for = NULL; + + xmpp_stanza_t *pubsub = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB); + if (!pubsub) { + return 1; + } + + xmpp_stanza_t *items = xmpp_stanza_get_child_by_name(pubsub, "items"); + if (!items) { + return 1; + } + + xmpp_stanza_t *item = xmpp_stanza_get_child_by_name(items, "item"); + if (!item) { + return 1; + } + + xmpp_stanza_t *data = stanza_get_child_by_name_and_ns(item, "data", STANZA_NS_USER_AVATAR_DATA); + if (!data) { + return 1; + } + + char *buf = xmpp_stanza_get_text(data); + gsize size; + gchar *de = (gchar*)g_base64_decode(buf, &size); + free(buf); + GError *err = NULL; + char *path = files_get_data_path(""); + GString *filename = g_string_new(path); + g_string_append(filename, from_attr); + g_string_append(filename, ".png"); + free(path); + + if (g_file_set_contents (filename->str, de, size, &err) == FALSE) { + log_error("Unable to save picture: %s", err->message); + cons_show("Unable to save picture %s", err->message); + g_error_free(err); + } + + g_string_free(filename, TRUE); + free(de); + + return 1; +} diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 7a744fad..dc0da68b 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -2523,3 +2523,34 @@ stanza_get_child_by_name_and_ns(xmpp_stanza_t * const stanza, const char * const return child; } + +xmpp_stanza_t* +stanza_create_avatar_retrieve_data_request(xmpp_ctx_t *ctx, const char *const id, const char *const jid) +{ + xmpp_stanza_t *iq = xmpp_iq_new(ctx, STANZA_TYPE_GET, "retrieve1"); + xmpp_stanza_set_to(iq, jid); + + 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_t *items = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(items, "items"); + char *node = g_strdup_printf("%s", STANZA_NS_USER_AVATAR_DATA); + xmpp_stanza_set_attribute(items, STANZA_ATTR_NODE, node); + g_free(node); + + xmpp_stanza_t *item = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(item, STANZA_NAME_ITEM); + xmpp_stanza_set_attribute(item, "id", id); + + xmpp_stanza_add_child(items, item); + xmpp_stanza_add_child(pubsub, items); + xmpp_stanza_add_child(iq, pubsub); + + xmpp_stanza_release(item); + xmpp_stanza_release(items); + xmpp_stanza_release(pubsub); + + return iq; +} diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h index c7e04703..6e41b81d 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -354,4 +354,6 @@ void stanza_free_caps(XMPPCaps *caps); xmpp_stanza_t* stanza_get_child_by_name_and_ns(xmpp_stanza_t * const stanza, const char * const name, const char * const ns); +xmpp_stanza_t* stanza_create_avatar_retrieve_data_request(xmpp_ctx_t *ctx, const char *const id, const char *const jid); + #endif