| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp.Xep.Pubsub { |
| 4 |
public const string NS_URI = "http://jabber.org/protocol/pubsub"; |
| 5 |
private const string NS_URI_EVENT = NS_URI + "#event"; |
| 6 |
private const string NS_URI_OWNER = NS_URI + "#owner"; |
| 7 |
public const string NS_URI_ERROR = NS_URI + "#errors"; |
| 8 |
|
| 9 |
public const string ACCESS_MODEL_AUTHORIZE = "authorize"; |
| 10 |
public const string ACCESS_MODEL_OPEN = "open"; |
| 11 |
public const string ACCESS_MODEL_PRESENCE = "presence"; |
| 12 |
public const string ACCESS_MODEL_ROSTER = "roster"; |
| 13 |
public const string ACCESS_MODEL_WHITELIST = "whitelist"; |
| 14 |
|
| 15 |
public class Module : XmppStreamModule { |
| 16 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0060_pubsub_module"); |
| 17 |
|
| 18 |
private HashMap<string, ItemListenerDelegate> item_listeners = new HashMap<string, ItemListenerDelegate>(); |
| 19 |
private HashMap<string, RetractListenerDelegate> retract_listeners = new HashMap<string, RetractListenerDelegate>(); |
| 20 |
private HashMap<string, DeleteListenerDelegate> delete_listeners = new HashMap<string, DeleteListenerDelegate>(); |
| 21 |
|
| 22 |
public void add_filtered_notification(XmppStream stream, string node, |
| 23 |
owned ItemListenerDelegate.ResultFunc? item_listener, |
| 24 |
owned RetractListenerDelegate.ResultFunc? retract_listener, |
| 25 |
owned DeleteListenerDelegate.ResultFunc? delete_listener) { |
| 26 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature_notify(stream, node); |
| 27 |
if (item_listener != null) { |
| 28 |
item_listeners[node] = new ItemListenerDelegate((owned)item_listener); |
| 29 |
} |
| 30 |
if (retract_listener != null) { |
| 31 |
retract_listeners[node] = new RetractListenerDelegate((owned)retract_listener); |
| 32 |
} |
| 33 |
if (delete_listener != null) { |
| 34 |
delete_listeners[node] = new DeleteListenerDelegate((owned)delete_listener); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public void remove_filtered_notification(XmppStream stream, string node) { |
| 39 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature_notify(stream, node); |
| 40 |
item_listeners.unset(node); |
| 41 |
retract_listeners.unset(node); |
| 42 |
} |
| 43 |
|
| 44 |
public async Gee.List<StanzaNode>? request_all(XmppStream stream, Jid jid, string node) { // TODO multiple nodes gehen auch |
| 45 |
Iq.Stanza request_iq = new Iq.Stanza.get(new StanzaNode.build("pubsub", NS_URI).add_self_xmlns().put_node(new StanzaNode.build("items", NS_URI).put_attribute("node", node))); |
| 46 |
request_iq.to = jid; |
| 47 |
|
| 48 |
Iq.Stanza iq_res = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, request_iq); |
| 49 |
|
| 50 |
StanzaNode event_node = iq_res.stanza.get_subnode("pubsub", NS_URI); |
| 51 |
if (event_node == null) return null; |
| 52 |
StanzaNode items_node = event_node.get_subnode("items", NS_URI); |
| 53 |
if (items_node == null) return null; |
| 54 |
|
| 55 |
return items_node.get_subnodes("item", NS_URI); |
| 56 |
} |
| 57 |
|
| 58 |
public delegate void OnResult(XmppStream stream, Jid jid, string? id, StanzaNode? node); |
| 59 |
public void request(XmppStream stream, Jid jid, string node, owned OnResult listener) { // TODO multiple nodes gehen auch |
| 60 |
Iq.Stanza request_iq = new Iq.Stanza.get(new StanzaNode.build("pubsub", NS_URI).add_self_xmlns().put_node(new StanzaNode.build("items", NS_URI).put_attribute("node", node))); |
| 61 |
request_iq.to = jid; |
| 62 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, request_iq, (stream, iq) => { |
| 63 |
StanzaNode event_node = iq.stanza.get_subnode("pubsub", NS_URI); |
| 64 |
StanzaNode items_node = event_node != null ? event_node.get_subnode("items", NS_URI) : null; |
| 65 |
StanzaNode item_node = items_node != null ? items_node.get_subnode("item", NS_URI) : null; |
| 66 |
string? id = item_node != null ? item_node.get_attribute("id", NS_URI) : null; |
| 67 |
listener(stream, iq.from, id, item_node != null ? item_node.sub_nodes[0] : null); |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
public async bool publish( |
| 72 |
XmppStream stream, Jid? jid, string node_id, string? item_id, StanzaNode content, |
| 73 |
PublishOptions? publish_options = null, |
| 74 |
bool try_reconfiguring = true |
| 75 |
) { |
| 76 |
StanzaNode pubsub_node = new StanzaNode.build("pubsub", NS_URI).add_self_xmlns(); |
| 77 |
StanzaNode publish_node = new StanzaNode.build("publish", NS_URI).put_attribute("node", node_id); |
| 78 |
pubsub_node.put_node(publish_node); |
| 79 |
StanzaNode items_node = new StanzaNode.build("item", NS_URI); |
| 80 |
if (item_id != null) items_node.put_attribute("id", item_id); |
| 81 |
items_node.put_node(content); |
| 82 |
publish_node.put_node(items_node); |
| 83 |
|
| 84 |
// Send along our requirements for the node |
| 85 |
if (publish_options != null) { |
| 86 |
StanzaNode publish_options_node = new StanzaNode.build("publish-options", NS_URI); |
| 87 |
pubsub_node.put_node(publish_options_node); |
| 88 |
|
| 89 |
DataForms.DataForm data_form = new DataForms.DataForm(); |
| 90 |
DataForms.DataForm.HiddenField form_type_field = new DataForms.DataForm.HiddenField() { var="FORM_TYPE" }; |
| 91 |
form_type_field.set_value_string(NS_URI + "#publish-options"); |
| 92 |
data_form.add_field(form_type_field); |
| 93 |
|
| 94 |
foreach (string field_name in publish_options.settings.keys) { |
| 95 |
DataForms.DataForm.Field field = new DataForms.DataForm.Field() { var=field_name }; |
| 96 |
field.set_value_string(publish_options.settings[field_name]); |
| 97 |
data_form.add_field(field); |
| 98 |
} |
| 99 |
publish_options_node.put_node(data_form.get_submit_node()); |
| 100 |
} |
| 101 |
|
| 102 |
Iq.Stanza iq = new Iq.Stanza.set(pubsub_node); |
| 103 |
|
| 104 |
// If the node was configured differently before, reconfigure it to meet our requirements and try again |
| 105 |
Iq.Stanza iq_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq); |
| 106 |
if (iq_result.is_error()) { |
| 107 |
if (publish_options == null || !try_reconfiguring) return false; |
| 108 |
bool precondition_not_met = iq_result.get_error().error_node.get_subnode("precondition-not-met", NS_URI_ERROR) != null; |
| 109 |
if (precondition_not_met) { |
| 110 |
bool success = yield change_node_config(stream, jid, node_id, publish_options); |
| 111 |
if (!success) return false; |
| 112 |
return yield publish(stream, jid, node_id, item_id, content, publish_options, false); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
public async bool retract_item(XmppStream stream, Jid? jid, string node_id, string item_id) { |
| 120 |
StanzaNode pubsub_node = new StanzaNode.build("pubsub", NS_URI).add_self_xmlns() |
| 121 |
.put_node(new StanzaNode.build("retract", NS_URI).put_attribute("node", node_id).put_attribute("notify", "true") |
| 122 |
.put_node(new StanzaNode.build("item", NS_URI).put_attribute("id", item_id))); |
| 123 |
|
| 124 |
Iq.Stanza iq = new Iq.Stanza.set(pubsub_node); |
| 125 |
bool ok = true; |
| 126 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, result_iq) => { |
| 127 |
ok = !result_iq.is_error(); |
| 128 |
Idle.add(retract_item.callback); |
| 129 |
}); |
| 130 |
yield; |
| 131 |
|
| 132 |
return ok; |
| 133 |
} |
| 134 |
|
| 135 |
public void delete_node(XmppStream stream, Jid? jid, string node_id) { |
| 136 |
StanzaNode pubsub_node = new StanzaNode.build("pubsub", NS_URI_OWNER).add_self_xmlns(); |
| 137 |
StanzaNode publish_node = new StanzaNode.build("delete", NS_URI_OWNER).put_attribute("node", node_id); |
| 138 |
pubsub_node.put_node(publish_node); |
| 139 |
|
| 140 |
Iq.Stanza iq = new Iq.Stanza.set(pubsub_node); |
| 141 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, null); |
| 142 |
} |
| 143 |
|
| 144 |
public async DataForms.DataForm? request_node_config(XmppStream stream, Jid? jid, string node_id) { |
| 145 |
StanzaNode pubsub_node = new StanzaNode.build("pubsub", NS_URI_OWNER).add_self_xmlns(); |
| 146 |
StanzaNode publish_node = new StanzaNode.build("configure", NS_URI_OWNER).put_attribute("node", node_id); |
| 147 |
pubsub_node.put_node(publish_node); |
| 148 |
|
| 149 |
Iq.Stanza iq = new Iq.Stanza.get(pubsub_node); |
| 150 |
Iq.Stanza result_iq = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq); |
| 151 |
StanzaNode? data_form_node = result_iq.stanza.get_deep_subnode(Pubsub.NS_URI_OWNER + ":pubsub", Pubsub.NS_URI_OWNER + ":configure", "jabber:x:data:x"); |
| 152 |
if (data_form_node == null) return null; |
| 153 |
return DataForms.DataForm.create_from_node(data_form_node); |
| 154 |
} |
| 155 |
|
| 156 |
public async bool submit_node_config(XmppStream stream, DataForms.DataForm data_form, string node_id) { |
| 157 |
StanzaNode submit_node = data_form.get_submit_node(); |
| 158 |
|
| 159 |
StanzaNode pubsub_node = new StanzaNode.build("pubsub", Pubsub.NS_URI_OWNER).add_self_xmlns(); |
| 160 |
StanzaNode publish_node = new StanzaNode.build("configure", Pubsub.NS_URI_OWNER).put_attribute("node", node_id); |
| 161 |
pubsub_node.put_node(publish_node); |
| 162 |
publish_node.put_node(submit_node); |
| 163 |
|
| 164 |
|
| 165 |
Iq.Stanza iq = new Iq.Stanza.set(pubsub_node); |
| 166 |
Iq.Stanza iq_result = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq); |
| 167 |
|
| 168 |
return !iq_result.is_error(); |
| 169 |
} |
| 170 |
|
| 171 |
public override void attach(XmppStream stream) { |
| 172 |
stream.get_module(MessageModule.IDENTITY).received_message.connect(on_received_message); |
| 173 |
} |
| 174 |
|
| 175 |
public override void detach(XmppStream stream) { |
| 176 |
stream.get_module(MessageModule.IDENTITY).received_message.disconnect(on_received_message); |
| 177 |
} |
| 178 |
|
| 179 |
public override string get_ns() { return NS_URI; } |
| 180 |
public override string get_id() { return IDENTITY.id; } |
| 181 |
|
| 182 |
private void on_received_message(XmppStream stream, MessageStanza message) { |
| 183 |
StanzaNode event_node = message.stanza.get_subnode("event", NS_URI_EVENT); |
| 184 |
if (event_node == null) return; |
| 185 |
|
| 186 |
if (!message.from.is_bare()) { |
| 187 |
warning("Got a PEP message from a full JID (%s), ignoring.", message.from.to_string()); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
StanzaNode items_node = event_node.get_subnode("items", NS_URI_EVENT); |
| 192 |
if (items_node != null) { |
| 193 |
string node = items_node.get_attribute("node", NS_URI_EVENT); |
| 194 |
|
| 195 |
StanzaNode? item_node = items_node.get_subnode("item", NS_URI_EVENT); |
| 196 |
if (item_node != null) { |
| 197 |
string id = item_node.get_attribute("id", NS_URI_EVENT); |
| 198 |
|
| 199 |
if (item_listeners.has_key(node)) { |
| 200 |
item_listeners[node].on_result(stream, message.from, id, item_node.sub_nodes[0]); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
StanzaNode? retract_node = items_node.get_subnode("retract", NS_URI_EVENT); |
| 205 |
if (retract_node != null) { |
| 206 |
string id = retract_node.get_attribute("id", NS_URI_EVENT); |
| 207 |
|
| 208 |
if (retract_listeners.has_key(node)) { |
| 209 |
retract_listeners[node].on_result(stream, message.from, id); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
StanzaNode? delete_node = event_node.get_subnode("delete", NS_URI_EVENT); |
| 215 |
if (delete_node != null) { |
| 216 |
string node = delete_node.get_attribute("node", NS_URI_EVENT); |
| 217 |
|
| 218 |
if (delete_listeners.has_key(node)) { |
| 219 |
delete_listeners[node].on_result(stream, message.from); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
private async bool change_node_config(XmppStream stream, Jid jid, string node, PublishOptions publish_options) { |
| 225 |
DataForms.DataForm? data_form = yield stream.get_module(Pubsub.Module.IDENTITY).request_node_config(stream, null, node); |
| 226 |
if (data_form == null) return false; |
| 227 |
|
| 228 |
foreach (DataForms.DataForm.Field field in data_form.fields) { |
| 229 |
if (publish_options.settings.has_key(field.var) && publish_options.settings[field.var] != field.get_value_string()) { |
| 230 |
field.set_value_string(publish_options.settings[field.var]); |
| 231 |
} |
| 232 |
} |
| 233 |
return yield stream.get_module(Pubsub.Module.IDENTITY).submit_node_config(stream, data_form, node); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
public class PublishOptions { |
| 238 |
public HashMap<string, string> settings = new HashMap<string, string>(); |
| 239 |
|
| 240 |
public PublishOptions set_persist_items(bool persist) { |
| 241 |
settings["pubsub#persist_items"] = persist.to_string(); |
| 242 |
return this; |
| 243 |
} |
| 244 |
|
| 245 |
public PublishOptions set_max_items(string max) { |
| 246 |
settings["pubsub#max_items"] = max; |
| 247 |
return this; |
| 248 |
} |
| 249 |
|
| 250 |
public PublishOptions set_send_last_published_item(string send) { |
| 251 |
settings["pubsub#send_last_published_item"] = send.to_string(); |
| 252 |
return this; |
| 253 |
} |
| 254 |
|
| 255 |
public PublishOptions set_access_model(string model) { |
| 256 |
settings["pubsub#access_model"] = model; |
| 257 |
return this; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
public class ItemListenerDelegate { |
| 262 |
public delegate void ResultFunc(XmppStream stream, Jid jid, string id, StanzaNode? node); |
| 263 |
public ResultFunc on_result { get; private owned set; } |
| 264 |
|
| 265 |
public ItemListenerDelegate(owned ResultFunc on_result) { |
| 266 |
this.on_result = (owned) on_result; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
public class RetractListenerDelegate { |
| 271 |
public delegate void ResultFunc(XmppStream stream, Jid jid, string id); |
| 272 |
public ResultFunc on_result { get; private owned set; } |
| 273 |
|
| 274 |
public RetractListenerDelegate(owned ResultFunc on_result) { |
| 275 |
this.on_result = (owned) on_result; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
public class DeleteListenerDelegate { |
| 280 |
public delegate void ResultFunc(XmppStream stream, Jid jid); |
| 281 |
public ResultFunc on_result { get; private owned set; } |
| 282 |
|
| 283 |
public DeleteListenerDelegate(owned ResultFunc on_result) { |
| 284 |
this.on_result = (owned) on_result; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
} |