| 1 |
namespace Xmpp.Xep.VCard { |
| 2 |
private const string NS_URI = "vcard-temp"; |
| 3 |
private const string NS_URI_UPDATE = NS_URI + ":x:update"; |
| 4 |
|
| 5 |
public async Bytes? fetch_image(XmppStream stream, Jid jid, string hash) { |
| 6 |
Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("vCard", NS_URI).add_self_xmlns()) { to=jid }; |
| 7 |
Iq.Stanza iq_res = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq); |
| 8 |
|
| 9 |
if (iq_res.is_error()) return null; |
| 10 |
string? res = iq_res.stanza.get_deep_string_content(@"$NS_URI:vCard", "PHOTO", "BINVAL"); |
| 11 |
if (res == null) return null; |
| 12 |
Bytes content = new Bytes.take(Base64.decode(res)); |
| 13 |
string sha1 = Checksum.compute_for_bytes(ChecksumType.SHA1, content); |
| 14 |
if (sha1 != hash) return null; |
| 15 |
|
| 16 |
return content; |
| 17 |
} |
| 18 |
|
| 19 |
public class Module : XmppStreamModule { |
| 20 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0153_vcard_based_avatars"); |
| 21 |
|
| 22 |
public signal void received_avatar_hash(XmppStream stream, Jid jid, string hash); |
| 23 |
|
| 24 |
public override void attach(XmppStream stream) { |
| 25 |
stream.get_module(Presence.Module.IDENTITY).received_presence.connect(on_received_presence); |
| 26 |
} |
| 27 |
|
| 28 |
public override void detach(XmppStream stream) { |
| 29 |
stream.get_module(Presence.Module.IDENTITY).received_presence.disconnect(on_received_presence); |
| 30 |
} |
| 31 |
|
| 32 |
public override string get_ns() { return NS_URI; } |
| 33 |
public override string get_id() { return IDENTITY.id; } |
| 34 |
|
| 35 |
private void on_received_presence(XmppStream stream, Presence.Stanza presence) { |
| 36 |
if (presence.type_ != Presence.Stanza.TYPE_AVAILABLE) { |
| 37 |
return; |
| 38 |
} |
| 39 |
StanzaNode? update_node = presence.stanza.get_subnode("x", NS_URI_UPDATE); |
| 40 |
if (update_node == null) return; |
| 41 |
StanzaNode? photo_node = update_node.get_subnode("photo", NS_URI_UPDATE); |
| 42 |
if (photo_node == null) return; |
| 43 |
string? sha1 = photo_node.get_string_content(); |
| 44 |
if (sha1 == null) return; |
| 45 |
received_avatar_hash(stream, presence.from, sha1); |
| 46 |
} |
| 47 |
} |
| 48 |
} |