| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp.Roster { |
| 4 |
|
| 5 |
public class VersioningModule : XmppStreamModule { |
| 6 |
private const string NS_URI_FEATURE = "urn:xmpp:features:rosterver"; |
| 7 |
|
| 8 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "roster_versioning"); |
| 9 |
|
| 10 |
private Storage storage; |
| 11 |
|
| 12 |
public VersioningModule(Storage storage) { |
| 13 |
this.storage = storage; |
| 14 |
} |
| 15 |
|
| 16 |
public override void attach(XmppStream stream) { |
| 17 |
stream.get_module(Module.IDENTITY).pre_get_roster.connect(on_pre_get_roster); |
| 18 |
stream.get_module(Module.IDENTITY).received_roster.connect(on_received_roster); |
| 19 |
stream.get_module(Module.IDENTITY).item_updated.connect(on_item_updated); |
| 20 |
stream.get_module(Module.IDENTITY).item_removed.connect(on_item_removed); |
| 21 |
} |
| 22 |
|
| 23 |
public override void detach(XmppStream stream) { |
| 24 |
stream.get_module(Module.IDENTITY).pre_get_roster.disconnect(on_pre_get_roster); |
| 25 |
} |
| 26 |
|
| 27 |
internal override string get_ns() { return NS_URI; } |
| 28 |
internal override string get_id() { return IDENTITY.id; } |
| 29 |
|
| 30 |
private void on_pre_get_roster(XmppStream stream, Iq.Stanza iq) { |
| 31 |
StanzaNode? ver_feature = stream.features.get_subnode("ver", NS_URI_FEATURE); |
| 32 |
if (ver_feature != null) { |
| 33 |
iq.stanza.get_subnode("query", NS_URI).set_attribute("ver", storage.get_roster_version() ?? ""); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
private void on_received_roster(XmppStream stream, Collection<Item> roster, Iq.Stanza iq) { |
| 38 |
string? ver = iq.stanza.get_deep_attribute(NS_URI + ":query", NS_URI + ":ver"); |
| 39 |
if (ver != null) storage.set_roster_version(ver); |
| 40 |
if (iq.stanza.get_subnode("query", NS_URI) != null) { |
| 41 |
storage.set_roster(roster); |
| 42 |
} else { |
| 43 |
Flag flag = stream.get_flag(Flag.IDENTITY); |
| 44 |
foreach (Item item in storage.get_roster()) { |
| 45 |
flag.roster_items[item.jid] = item; |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
private void on_item_updated(XmppStream stream, Item item, Iq.Stanza iq) { |
| 51 |
string? ver = iq.stanza.get_deep_attribute(NS_URI + ":query", NS_URI + ":ver"); |
| 52 |
if (ver != null) storage.set_roster_version(ver); |
| 53 |
storage.set_item(item); |
| 54 |
} |
| 55 |
|
| 56 |
private void on_item_removed(XmppStream stream, Item item, Iq.Stanza iq) { |
| 57 |
string? ver = iq.stanza.get_deep_attribute(NS_URI + ":query", NS_URI + ":ver"); |
| 58 |
if (ver != null) storage.set_roster_version(ver); |
| 59 |
storage.remove_item(item); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public interface Storage : Object { |
| 64 |
public abstract string? get_roster_version(); |
| 65 |
public abstract Collection<Roster.Item> get_roster(); |
| 66 |
public abstract void set_roster_version(string version); |
| 67 |
public abstract void set_roster(Collection<Roster.Item> items); |
| 68 |
public abstract void set_item(Roster.Item item); |
| 69 |
public abstract void remove_item(Roster.Item item); |
| 70 |
} |
| 71 |
|
| 72 |
} |