Mercurial > prosody-modules
comparison mod_blocking/mod_blocking.lua @ 164:0b238b2b0801
mod_blocking: Support for the "unblock all JIDs" case, and fix saving of rules after removing a JID
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 03 Jun 2010 11:20:26 +0100 |
| parents | 9fe6d314fd07 |
| children | 4f58ddade1db |
comparison
equal
deleted
inserted
replaced
| 163:9fe6d314fd07 | 164:0b238b2b0801 |
|---|---|
| 44 if item.type == "jid" and item.action == "deny" and item.value == jid then | 44 if item.type == "jid" and item.action == "deny" and item.value == jid then |
| 45 table.remove(items, i); | 45 table.remove(items, i); |
| 46 return true; | 46 return true; |
| 47 end | 47 end |
| 48 end | 48 end |
| 49 datamanager.store(username, host, "privacy", privacy_lists); | |
| 50 end | |
| 51 | |
| 52 function remove_all_blocked_jids(username, host) | |
| 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 for i=#items,1 do -- order must be unique | |
| 61 item = items[i]; | |
| 62 if item.type == "jid" and item.action == "deny" then | |
| 63 table.remove(items, i); | |
| 64 end | |
| 65 end | |
| 66 datamanager.store(username, host, "privacy", privacy_lists); | |
| 49 end | 67 end |
| 50 | 68 |
| 51 function get_blocked_jids(username, host) | 69 function get_blocked_jids(username, host) |
| 52 -- Return array of blocked JIDs in default privacy list | 70 -- Return array of blocked JIDs in default privacy list |
| 53 local privacy_lists = datamanager.load(username, host, "privacy") or {}; | 71 local privacy_lists = datamanager.load(username, host, "privacy") or {}; |
| 67 return jid_list; | 85 return jid_list; |
| 68 end | 86 end |
| 69 | 87 |
| 70 function handle_blocking_command(session, stanza) | 88 function handle_blocking_command(session, stanza) |
| 71 local username, host = jid_split(stanza.attr.from); | 89 local username, host = jid_split(stanza.attr.from); |
| 72 if stanza.attr.type == "set" and stanza.tags[1].name == "block" then | 90 if stanza.attr.type == "set" then |
| 73 local block = stanza.tags[1]:get_child("block"); | 91 if stanza.tags[1].name == "block" then |
| 74 local block_jid_list = {}; | 92 local block = stanza.tags[1]:get_child("block"); |
| 75 for item in block:childtags() do | 93 local block_jid_list = {}; |
| 76 block_jid_list[#block_jid_list+1] = item.attr.jid; | 94 for item in block:childtags() do |
| 77 end | 95 block_jid_list[#block_jid_list+1] = item.attr.jid; |
| 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 | 96 end |
| 97 if #block_jid_list == 0 then | |
| 98 --FIXME: Reply bad-request | |
| 99 else | |
| 100 for _, jid in ipairs(block_jid_list) do | |
| 101 add_blocked_jid(username, host, jid); | |
| 102 end | |
| 103 session.send(st.reply(stanza)); | |
| 104 end | |
| 105 elseif stanza.tags[1].name == "unblock" then | |
| 106 remove_all_blocked_jids(username, host); | |
| 84 session.send(st.reply(stanza)); | 107 session.send(st.reply(stanza)); |
| 85 end | 108 end |
| 86 elseif stanza.attr.type == "get" and stanza.tags[1].name == "blocklist" then | 109 elseif stanza.attr.type == "get" and stanza.tags[1].name == "blocklist" then |
| 87 local reply = st.reply(stanza):tag("blocklist", { xmlns = xmlns_block }); | 110 local reply = st.reply(stanza):tag("blocklist", { xmlns = xmlns_block }); |
| 88 local blocked_jids = get_blocked_jids(username, host); | 111 local blocked_jids = get_blocked_jids(username, host); |
