From c2aa585615d35186fbf8ada94f8974bcb386ce8d Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 18 Dec 2019 12:46:03 +0100 Subject: xep-0084: Download avatars Proof of concept. Needs error checking, a nice interface, general cleanup.. --- src/xmpp/avatar.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) (limited to 'src/xmpp/avatar.c') 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; } -- cgit 1.4.1-2-gfad0