2017-03-02 14:37:32 +00:00
|
|
|
using Xmpp.Core;
|
|
|
|
|
|
|
|
namespace Xmpp.Xep.VCard {
|
|
|
|
private const string NS_URI = "vcard-temp";
|
|
|
|
private const string NS_URI_UPDATE = NS_URI + ":x:update";
|
|
|
|
|
|
|
|
public class Module : XmppStreamModule {
|
2017-03-19 11:55:36 +00:00
|
|
|
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0027_current_pgp_usage");
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
public signal void received_avatar(XmppStream stream, string jid, string id);
|
|
|
|
|
|
|
|
private PixbufStorage storage;
|
|
|
|
|
|
|
|
public Module(PixbufStorage storage) {
|
|
|
|
this.storage = storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void attach(XmppStream stream) {
|
|
|
|
Iq.Module.require(stream);
|
|
|
|
Presence.Module.require(stream);
|
2017-03-11 00:40:42 +00:00
|
|
|
stream.get_module(Presence.Module.IDENTITY).received_presence.connect(on_received_presence);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void detach(XmppStream stream) {
|
2017-03-11 00:40:42 +00:00
|
|
|
stream.get_module(Presence.Module.IDENTITY).received_presence.disconnect(on_received_presence);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void require(XmppStream stream) {
|
2017-03-11 00:40:42 +00:00
|
|
|
if (stream.get_module(IDENTITY) == null) stderr.printf("VCardModule required but not attached!\n"); ;
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override string get_ns() { return NS_URI; }
|
2017-03-19 11:55:36 +00:00
|
|
|
public override string get_id() { return IDENTITY.id; }
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
private void on_received_presence(XmppStream stream, Presence.Stanza presence) {
|
|
|
|
StanzaNode? update_node = presence.stanza.get_subnode("x", NS_URI_UPDATE);
|
|
|
|
if (update_node == null) return;
|
|
|
|
StanzaNode? photo_node = update_node.get_subnode("photo", NS_URI_UPDATE);
|
|
|
|
if (photo_node == null) return;
|
|
|
|
string? sha1 = photo_node.get_string_content();
|
|
|
|
if (sha1 == null) return;
|
|
|
|
if (storage.has_image(sha1)) {
|
2017-03-19 11:55:36 +00:00
|
|
|
if (stream.get_flag(Muc.Flag.IDENTITY).is_occupant(presence.from)) {
|
2017-03-02 14:37:32 +00:00
|
|
|
received_avatar(stream, presence.from, sha1);
|
|
|
|
} else {
|
|
|
|
received_avatar(stream, get_bare_jid(presence.from), sha1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Iq.Stanza iq = new Iq.Stanza.get(new StanzaNode.build("vCard", NS_URI).add_self_xmlns());
|
2017-03-19 11:55:36 +00:00
|
|
|
if (stream.get_flag(Muc.Flag.IDENTITY).is_occupant(presence.from)) {
|
2017-03-02 14:37:32 +00:00
|
|
|
iq.to = presence.from;
|
|
|
|
} else {
|
|
|
|
iq.to = get_bare_jid(presence.from);
|
|
|
|
}
|
2017-06-13 16:14:59 +00:00
|
|
|
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, on_received_vcard);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 16:14:59 +00:00
|
|
|
private void on_received_vcard(XmppStream stream, Iq.Stanza iq) {
|
2017-03-11 22:04:58 +00:00
|
|
|
if (iq.is_error()) return;
|
2017-03-12 12:19:04 +00:00
|
|
|
string? res = iq.stanza.get_deep_string_content(@"$NS_URI:vCard", "PHOTO", "BINVAL");
|
|
|
|
if (res == null) return;
|
|
|
|
uint8[] content = Base64.decode(res);
|
|
|
|
string sha1 = Checksum.compute_for_data(ChecksumType.SHA1, content);
|
|
|
|
storage.store(sha1, content);
|
2017-03-11 22:04:58 +00:00
|
|
|
stream.get_module(IDENTITY).received_avatar(stream, iq.from, sha1);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|