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