1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

xep-0084: Download avatars

Proof of concept.
Needs error checking, a nice interface, general cleanup..
This commit is contained in:
Michael Vetter 2019-12-18 12:46:03 +01:00
parent e3538cf739
commit c2aa585615
3 changed files with 113 additions and 3 deletions

View File

@ -34,6 +34,7 @@
*/ */
#include <glib.h> #include <glib.h>
#include <stdio.h>
#include "log.h" #include "log.h"
#include "xmpp/connection.h" #include "xmpp/connection.h"
@ -42,10 +43,13 @@
#include "xmpp/message.h" #include "xmpp/message.h"
#include "xmpp/stanza.h" #include "xmpp/stanza.h"
#include "ui/ui.h" #include "ui/ui.h"
#include "config/files.h"
char *looking_for = NULL; char *looking_for = NULL;
static int _avatar_metadata_nofication(xmpp_stanza_t *const stanza, void *const userdata); 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 void
avatar_pep_subscribe(void) avatar_pep_subscribe(void)
@ -68,15 +72,14 @@ static int
_avatar_metadata_nofication(xmpp_stanza_t *const stanza, void *const userdata) _avatar_metadata_nofication(xmpp_stanza_t *const stanza, void *const userdata)
{ {
const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
from = from;
if (!(looking_for && if (!(looking_for &&
(g_strcmp0(looking_for, from) == 0))) { (g_strcmp0(looking_for, from) == 0))) {
return 1; return 1;
} }
free(looking_for); // free(looking_for);
looking_for = NULL; // looking_for = NULL;
xmpp_stanza_t *root = NULL; xmpp_stanza_t *root = NULL;
xmpp_stanza_t *event = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB_EVENT); 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); const char *id = xmpp_stanza_get_id(info);
cons_show("Id for %s is: %s", from, id); cons_show("Id for %s is: %s", from, id);
avatar_request_item_by_id(from, id);
} }
return 1; 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;
}

View File

@ -2523,3 +2523,34 @@ stanza_get_child_by_name_and_ns(xmpp_stanza_t * const stanza, const char * const
return child; 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;
}

View File

@ -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_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 #endif