| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp.Xep.BlockingCommand { |
| 4 |
|
| 5 |
private const string NS_URI = "urn:xmpp:blocking"; |
| 6 |
|
| 7 |
public class Module : XmppStreamModule, Iq.Handler { |
| 8 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0191_blocking_command"); |
| 9 |
|
| 10 |
public signal void block_push_received(XmppStream stream, Gee.List<string> jids); |
| 11 |
public signal void unblock_push_received(XmppStream stream, Gee.List<string> jids); |
| 12 |
public signal void unblock_all_received(XmppStream stream); |
| 13 |
|
| 14 |
public bool is_blocked(XmppStream stream, string jid) { |
| 15 |
if (is_supported(stream)) { |
| 16 |
return stream.get_flag(Flag.IDENTITY).blocklist.contains(jid); |
| 17 |
} else { |
| 18 |
return false; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
public bool block(XmppStream stream, string[] jids) { |
| 23 |
if (jids.length == 0) return false; // This would otherwise be a bad-request error. |
| 24 |
|
| 25 |
StanzaNode block_node = new StanzaNode.build("block", NS_URI).add_self_xmlns(); |
| 26 |
fill_node_with_items(block_node, jids); |
| 27 |
Iq.Stanza iq = new Iq.Stanza.set(block_node); |
| 28 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, null); |
| 29 |
return true; |
| 30 |
} |
| 31 |
|
| 32 |
public bool unblock(XmppStream stream, string[] jids) { |
| 33 |
if (jids.length == 0) return false; // This would otherwise unblock all blocked JIDs. |
| 34 |
|
| 35 |
StanzaNode unblock_node = new StanzaNode.build("unblock", NS_URI).add_self_xmlns(); |
| 36 |
fill_node_with_items(unblock_node, jids); |
| 37 |
Iq.Stanza iq = new Iq.Stanza.set(unblock_node); |
| 38 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, null); |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
public void unblock_all(XmppStream stream) { |
| 43 |
StanzaNode unblock_node = new StanzaNode.build("unblock", NS_URI).add_self_xmlns(); |
| 44 |
Iq.Stanza iq = new Iq.Stanza.set(unblock_node); |
| 45 |
stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, null); |
| 46 |
} |
| 47 |
|
| 48 |
public bool is_supported(XmppStream stream) { |
| 49 |
return stream.has_flag(Flag.IDENTITY); |
| 50 |
} |
| 51 |
|
| 52 |
private async void on_iq_set(XmppStream stream, Iq.Stanza iq) { |
| 53 |
StanzaNode? block_node = iq.stanza.get_subnode("block", NS_URI); |
| 54 |
StanzaNode? unblock_node = iq.stanza.get_subnode("unblock", NS_URI); |
| 55 |
Gee.List<string> jids; |
| 56 |
if (block_node != null) { |
| 57 |
jids = get_jids_from_items(block_node); |
| 58 |
stream.get_flag(Flag.IDENTITY).blocklist.add_all(jids); |
| 59 |
block_push_received(stream, jids); |
| 60 |
} else if (unblock_node != null) { |
| 61 |
jids = get_jids_from_items(unblock_node); |
| 62 |
if (jids.size > 0) { |
| 63 |
stream.get_flag(Flag.IDENTITY).blocklist.remove_all(jids); |
| 64 |
unblock_push_received(stream, jids); |
| 65 |
} else { |
| 66 |
stream.get_flag(Flag.IDENTITY).blocklist.clear(); |
| 67 |
unblock_all_received(stream); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public override void attach(XmppStream stream) { |
| 73 |
stream.get_module(Iq.Module.IDENTITY).register_for_namespace(NS_URI, this); |
| 74 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); |
| 75 |
stream.stream_negotiated.connect(on_stream_negotiated); |
| 76 |
} |
| 77 |
|
| 78 |
public override void detach(XmppStream stream) { |
| 79 |
stream.get_module(Iq.Module.IDENTITY).unregister_from_namespace(NS_URI, this); |
| 80 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); |
| 81 |
stream.stream_negotiated.disconnect(on_stream_negotiated); |
| 82 |
} |
| 83 |
|
| 84 |
public override string get_ns() { return NS_URI; } |
| 85 |
public override string get_id() { return IDENTITY.id; } |
| 86 |
|
| 87 |
private async void on_stream_negotiated(XmppStream stream) { |
| 88 |
bool has_feature = yield stream.get_module(ServiceDiscovery.Module.IDENTITY).has_entity_feature(stream, stream.remote_name, NS_URI); |
| 89 |
if (has_feature) { |
| 90 |
stream.add_flag(new Flag()); |
| 91 |
stream.get_flag(Flag.IDENTITY).blocklist = yield get_blocklist(stream); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
private async Gee.List<string> get_blocklist(XmppStream stream) { |
| 96 |
StanzaNode blocklist_node = new StanzaNode.build("blocklist", NS_URI).add_self_xmlns(); |
| 97 |
Iq.Stanza iq = new Iq.Stanza.get(blocklist_node); |
| 98 |
|
| 99 |
Iq.Stanza result_iq = yield stream.get_module(Iq.Module.IDENTITY).send_iq_async(stream, iq); |
| 100 |
StanzaNode? node = result_iq.stanza.get_subnode("blocklist", NS_URI); |
| 101 |
if (node != null) { |
| 102 |
return get_jids_from_items(node); |
| 103 |
} |
| 104 |
return new ArrayList<string>(); |
| 105 |
} |
| 106 |
|
| 107 |
private Gee.List<string> get_jids_from_items(StanzaNode node) { |
| 108 |
Gee.List<StanzaNode> item_nodes = node.get_subnodes("item", NS_URI); |
| 109 |
Gee.List<string> jids = new ArrayList<string>(); |
| 110 |
foreach (StanzaNode item_node in item_nodes) { |
| 111 |
string? jid = item_node.get_attribute("jid", NS_URI); |
| 112 |
if (jid != null) { |
| 113 |
jids.add(jid); |
| 114 |
} |
| 115 |
} |
| 116 |
return jids; |
| 117 |
} |
| 118 |
|
| 119 |
private void fill_node_with_items(StanzaNode node, string[] jids) { |
| 120 |
foreach (string jid in jids) { |
| 121 |
StanzaNode item_node = new StanzaNode.build("item", NS_URI).add_self_xmlns(); |
| 122 |
item_node.set_attribute("jid", jid, NS_URI); |
| 123 |
node.put_node(item_node); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
public class Flag : XmppStreamFlag { |
| 129 |
public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "blocking_command"); |
| 130 |
|
| 131 |
public Gee.List<string> blocklist; |
| 132 |
|
| 133 |
public override string get_ns() { return NS_URI; } |
| 134 |
public override string get_id() { return IDENTITY.id; } |
| 135 |
} |
| 136 |
|
| 137 |
} |