Debian Codegraph @

xmpp-vala/src/module/xep/0084_user_avatars.vala

raw
1 namespace Xmpp.Xep.UserAvatars {
2 private const string NS_URI = "urn:xmpp:avatar";
3 private const string NS_URI_DATA = NS_URI + ":data";
4 private const string NS_URI_METADATA = NS_URI + ":metadata";
5
6 public void publish_png(XmppStream stream, uint8[] image, int width, int height) {
7 string sha1 = Checksum.compute_for_data(ChecksumType.SHA1, image);
8 StanzaNode data_node = new StanzaNode.build("data", NS_URI_DATA).add_self_xmlns()
9 .put_node(new StanzaNode.text(Base64.encode(image)));
10 stream.get_module(Pubsub.Module.IDENTITY).publish.begin(stream, null, NS_URI_DATA, sha1, data_node);
11
12 StanzaNode metadata_node = new StanzaNode.build("metadata", NS_URI_METADATA).add_self_xmlns();
13 StanzaNode info_node = new StanzaNode.build("info", NS_URI_METADATA)
14 .put_attribute("bytes", image.length.to_string())
15 .put_attribute("id", sha1)
16 .put_attribute("width", width.to_string())
17 .put_attribute("height", height.to_string())
18 .put_attribute("type", "image/png");
19 metadata_node.put_node(info_node);
20 stream.get_module(Pubsub.Module.IDENTITY).publish.begin(stream, null, NS_URI_METADATA, sha1, metadata_node);
21 }
22
23 public void unset_avatar(XmppStream stream) {
24 StanzaNode metadata_node = new StanzaNode.build("metadata", NS_URI_METADATA).add_self_xmlns();
25 stream.get_module(Pubsub.Module.IDENTITY).delete_node(stream, null, NS_URI_METADATA);
26 }
27
28 public async Bytes? fetch_image(XmppStream stream, Jid jid, string hash) {
29 Gee.List<StanzaNode>? items = yield stream.get_module(Pubsub.Module.IDENTITY).request_all(stream, jid, NS_URI_DATA);
30 if (items == null || items.size == 0 || items[0].sub_nodes.size == 0) return null;
31
32 StanzaNode node = items[0].sub_nodes[0];
33 string? id = items[0].get_attribute("id", Pubsub.NS_URI);
34 if (id == null) return null;
35
36 Bytes image = new Bytes.take(Base64.decode(node.get_string_content()));
37 string sha1 = Checksum.compute_for_bytes(ChecksumType.SHA1, image);
38 if (sha1 != id) {
39 warning("sha sum did not match for avatar from %s expected=%s actual=%s", jid.to_string(), id, sha1);
40 return null;
41 }
42 return image;
43 }
44
45 public class Module : XmppStreamModule {
46 public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0084_user_avatars");
47
48 public signal void received_avatar_hash(XmppStream stream, Jid jid, string id);
49 public signal void avatar_removed(XmppStream stream, Jid jid);
50
51 public override void attach(XmppStream stream) {
52 stream.get_module(Pubsub.Module.IDENTITY).add_filtered_notification(stream, NS_URI_METADATA, on_pupsub_item, null, on_pubsub_delete);
53 }
54
55 public override void detach(XmppStream stream) {
56 stream.get_module(Pubsub.Module.IDENTITY).remove_filtered_notification(stream, NS_URI_METADATA);
57 }
58
59 public void on_pupsub_item(XmppStream stream, Jid jid, string hash, StanzaNode? node) {
60 foreach (var info_node in node.get_subnodes("info", NS_URI_METADATA)) {
61 if (info_node.get_attribute("id") == hash) {
62 string? type = info_node == null ? null : info_node.get_attribute("type");
63 if (type != "image/png" && type != "image/jpeg") return;
64 received_avatar_hash(stream, jid, hash);
65 return;
66 }
67 }
68 }
69
70 public void on_pubsub_delete(XmppStream stream, Jid jid) {
71 avatar_removed(stream, jid);
72 }
73
74 public override string get_ns() { return NS_URI; }
75 public override string get_id() { return IDENTITY.id; }
76 }
77 }