| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp.Xep.Bookmarks2 { |
| 4 |
|
| 5 |
public const string NS_URI = "urn:xmpp:bookmarks:1"; |
| 6 |
public const string NS_URI_COMPAT = NS_URI + "#compat"; |
| 7 |
|
| 8 |
public class Module : BookmarksProvider, XmppStreamModule { |
| 9 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0402_bookmarks2"); |
| 10 |
|
| 11 |
public async Set<Conference>? get_conferences(XmppStream stream) { |
| 12 |
HashMap<Jid, Conference>? hm = null; |
| 13 |
|
| 14 |
Flag? flag = stream.get_flag(Flag.IDENTITY); |
| 15 |
if (flag != null) { |
| 16 |
hm = flag.conferences; |
| 17 |
} else { |
| 18 |
Gee.List<StanzaNode>? items = yield stream.get_module(Pubsub.Module.IDENTITY).request_all(stream, stream.get_flag(Bind.Flag.IDENTITY).my_jid.bare_jid, NS_URI); |
| 19 |
if (items == null) return null; |
| 20 |
|
| 21 |
hm = new HashMap<Jid, Conference>(Jid.hash_func, Jid.equals_func); |
| 22 |
foreach (StanzaNode item_node in items) { |
| 23 |
Conference? conference = parse_item_node(item_node.sub_nodes[0], item_node.get_attribute("id")); |
| 24 |
if (conference == null) continue; |
| 25 |
hm[conference.jid] = conference; |
| 26 |
} |
| 27 |
stream.add_flag(new Flag(hm)); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
var ret = new HashSet<Conference>(); |
| 32 |
foreach (var conference in hm.values) { |
| 33 |
ret.add(conference); |
| 34 |
} |
| 35 |
return ret; |
| 36 |
} |
| 37 |
|
| 38 |
public async void add_conference(XmppStream stream, Conference conference) { |
| 39 |
StanzaNode conference_node = new StanzaNode.build("conference", NS_URI).add_self_xmlns() |
| 40 |
.put_attribute("autojoin", conference.autojoin.to_string()); |
| 41 |
if (conference.name != null) { |
| 42 |
conference_node.put_attribute("name", conference.name); |
| 43 |
} |
| 44 |
if (conference.nick != null) { |
| 45 |
conference_node.put_node((new StanzaNode.build("nick", NS_URI)).put_node(new StanzaNode.text(conference.nick))); |
| 46 |
} |
| 47 |
if (conference.password != null) { |
| 48 |
conference_node.put_node((new StanzaNode.build("password", NS_URI)).put_node(new StanzaNode.text(conference.password))); |
| 49 |
} |
| 50 |
var publish_options = new Pubsub.PublishOptions() |
| 51 |
.set_persist_items(true) |
| 52 |
.set_max_items("max") |
| 53 |
.set_send_last_published_item("never") |
| 54 |
.set_access_model("whitelist"); |
| 55 |
|
| 56 |
yield stream.get_module(Pubsub.Module.IDENTITY).publish(stream, stream.get_flag(Bind.Flag.IDENTITY).my_jid.bare_jid, NS_URI, conference.jid.to_string(), conference_node, publish_options); |
| 57 |
} |
| 58 |
|
| 59 |
public async void replace_conference(XmppStream stream, Jid muc_jid, Conference modified_conference) { |
| 60 |
yield add_conference(stream, modified_conference); |
| 61 |
} |
| 62 |
|
| 63 |
public async void remove_conference(XmppStream stream, Conference conference) { |
| 64 |
yield stream.get_module(Pubsub.Module.IDENTITY).retract_item(stream, |
| 65 |
stream.get_flag(Bind.Flag.IDENTITY).my_jid.bare_jid, |
| 66 |
NS_URI, |
| 67 |
conference.jid.to_string()); |
| 68 |
} |
| 69 |
|
| 70 |
private void on_pupsub_item(XmppStream stream, Jid jid, string id, StanzaNode? node) { |
| 71 |
if (!jid.equals(stream.get_flag(Bind.Flag.IDENTITY).my_jid.bare_jid)) { |
| 72 |
warning("Received alleged bookmarks:1 item from %s, ignoring", jid.to_string()); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
Conference conference = parse_item_node(node, id); |
| 77 |
Flag? flag = stream.get_flag(Flag.IDENTITY); |
| 78 |
if (flag != null) { |
| 79 |
flag.conferences[conference.jid] = conference; |
| 80 |
} |
| 81 |
conference_added(stream, conference); |
| 82 |
} |
| 83 |
|
| 84 |
private void on_pupsub_retract(XmppStream stream, Jid jid, string id) { |
| 85 |
if (!jid.equals(stream.get_flag(Bind.Flag.IDENTITY).my_jid.bare_jid)) { |
| 86 |
warning("Received alleged bookmarks:1 retract from %s, ignoring", jid.to_string()); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
try { |
| 91 |
Jid jid_parsed = new Jid(id); |
| 92 |
Flag? flag = stream.get_flag(Flag.IDENTITY); |
| 93 |
if (flag != null) { |
| 94 |
flag.conferences.unset(jid_parsed); |
| 95 |
} |
| 96 |
conference_removed(stream, jid_parsed); |
| 97 |
} catch (InvalidJidError e) { |
| 98 |
warning("Ignoring conference bookmark update with invalid Jid: %s", e.message); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
private Conference? parse_item_node(StanzaNode conference_node, string id) { |
| 103 |
Conference conference = new Conference(); |
| 104 |
try { |
| 105 |
Jid jid_parsed = new Jid(id); |
| 106 |
if (jid_parsed.resourcepart != null) return null; |
| 107 |
conference.jid = jid_parsed; |
| 108 |
} catch (InvalidJidError e) { |
| 109 |
warning("Ignoring conference bookmark update with invalid Jid: %s", e.message); |
| 110 |
return null; |
| 111 |
} |
| 112 |
|
| 113 |
if (conference_node.name != "conference" || conference_node.ns_uri != NS_URI) return null; |
| 114 |
|
| 115 |
conference.name = conference_node.get_attribute("name", NS_URI); |
| 116 |
conference.autojoin = conference_node.get_attribute_bool("autojoin", false, NS_URI); |
| 117 |
conference.nick = conference_node.get_deep_string_content("nick"); |
| 118 |
conference.password = conference_node.get_deep_string_content("password"); |
| 119 |
return conference; |
| 120 |
} |
| 121 |
|
| 122 |
public override void attach(XmppStream stream) { |
| 123 |
stream.get_module(Pubsub.Module.IDENTITY).add_filtered_notification(stream, NS_URI, on_pupsub_item, on_pupsub_retract, null); |
| 124 |
} |
| 125 |
|
| 126 |
public override void detach(XmppStream stream) { |
| 127 |
stream.get_module(Pubsub.Module.IDENTITY).remove_filtered_notification(stream, NS_URI); |
| 128 |
} |
| 129 |
|
| 130 |
public override string get_ns() { return NS_URI; } |
| 131 |
public override string get_id() { return IDENTITY.id; } |
| 132 |
} |
| 133 |
|
| 134 |
public class Flag : XmppStreamFlag { |
| 135 |
public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "bookmarks2"); |
| 136 |
|
| 137 |
public Flag(HashMap<Jid, Conference> conferences) { |
| 138 |
this.conferences = conferences; |
| 139 |
} |
| 140 |
|
| 141 |
public HashMap<Jid, Conference> conferences = new HashMap<Jid, Conference>(Jid.hash_func, Jid.equals_func); |
| 142 |
|
| 143 |
public override string get_ns() { return NS_URI; } |
| 144 |
public override string get_id() { return IDENTITY.id; } |
| 145 |
} |
| 146 |
|
| 147 |
} |