Software /
code /
prosody-modules
Comparison
mod_blocking/mod_blocking.lua @ 161:fda7faee7677
mod_blocking: XEP-0191 Simple Communications Blocking, should work, but not tested. Requires mod_privacy be loaded.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 03 Jun 2010 01:56:49 +0100 |
child | 163:9fe6d314fd07 |
comparison
equal
deleted
inserted
replaced
160:9a7671720dec | 161:fda7faee7677 |
---|---|
1 module:add_feature("urn:xmpp:blocking"); | |
2 | |
3 -- Add JID to default privacy list | |
4 function add_blocked_jid(username, host, jid) | |
5 local privacy_lists = datamanager.load(username, host, "privacy") or {}; | |
6 local default_list_name = privacy_lists.default; | |
7 if not default_list_name then | |
8 default_list_name = "blocklist"; | |
9 privacy_lists.default = default_list_name; | |
10 end | |
11 local default_list = privacy_lists.list[default_list_name]; | |
12 if not default_list then | |
13 default_list = { name = default_list_name, items = {} }; | |
14 privacy_lists.lists[default_list_name] = default_list; | |
15 end | |
16 local items = default_list.items; | |
17 local order = items[1].order; -- Must come first | |
18 for i=1,#items do -- order must be unique | |
19 items[i].order = items[i].order + 1; | |
20 end | |
21 table.insert(items, 1, { type = "jid" | |
22 , action = "deny" | |
23 , value = jid | |
24 , message = false | |
25 , ["presence-out"] = false | |
26 , ["presence-in"] = false | |
27 , iq = false | |
28 , order = order | |
29 }; | |
30 datamanager.store(username, host, "privacy", privacy_lists); | |
31 end | |
32 | |
33 -- Remove JID from default privacy list | |
34 function remove_blocked_jid(username, host, jid) | |
35 local privacy_lists = datamanager.load(username, host, "privacy") or {}; | |
36 local default_list_name = privacy_lists.default; | |
37 if not default_list_name then return; end | |
38 local default_list = privacy_lists.list[default_list_name]; | |
39 if not default_list then return; end | |
40 local items = default_list.items; | |
41 local item; | |
42 for i=1,#items do -- order must be unique | |
43 item = items[i]; | |
44 if item.type == "jid" and item.value == jid then | |
45 table.remove(items, i); | |
46 return true; | |
47 end | |
48 end | |
49 end | |
50 | |
51 function get_blocked_jids(username, host) | |
52 -- Return array of blocked JIDs in default privacy list | |
53 local privacy_lists = datamanager.load(username, host, "privacy") or {}; | |
54 local default_list_name = privacy_lists.default; | |
55 if not default_list_name then return {}; end | |
56 local default_list = privacy_lists.list[default_list_name]; | |
57 if not default_list then return {}; end | |
58 local items = default_list.items; | |
59 local item; | |
60 local jid_list = {}; | |
61 for i=1,#items do -- order must be unique | |
62 item = items[i]; | |
63 if item.type == "jid" then | |
64 jid_list[#jid_list+1] = item.value; | |
65 end | |
66 end | |
67 return jid_list; | |
68 end | |
69 | |
70 function handle_blocking_command(session, stanza) | |
71 local username, host = jid_split(stanza.attr.from); | |
72 if stanza.attr.type == "set" and stanza.tags[1].name == "block" then | |
73 local block = stanza.tags[1]:get_child("block"); | |
74 local block_jid_list = {}; | |
75 for item in block:childtags() do | |
76 block_jid_list[#block_jid_list+1] = item.attr.jid; | |
77 end | |
78 if #block_jid_list == 0 then | |
79 --FIXME: Reply bad-request | |
80 else | |
81 for _, jid in ipairs(block_jid_list) do | |
82 add_blocked_jid(username, host, jid); | |
83 end | |
84 session.send(st.reply(stanza)); | |
85 end | |
86 elseif stanza.attr.type == "get" and stanza.tags[1].name == "blocklist" then | |
87 local reply = st.reply(stanza):tag("blocklist", { xmlns = xmlns_block }); | |
88 local blocked_jids = get_blocked_jids(username, host); | |
89 for _, jid in ipairs(blocked_jids) do | |
90 reply:tag("item", { jid = jid }):up(); | |
91 end | |
92 session.send(reply); | |
93 else | |
94 --FIXME: Need to respond with service-unavailable | |
95 end | |
96 end | |
97 | |
98 module:add_iq_handler("c2s", xmlns_blocking, handle_blocking_command); |