comparison mod_privacy/mod_privacy.lua @ 16:35e74c1094a7

mod_privacy: order must be non-negativ integer and unique, group must be existing in the roster, subscription can only be to,from,both or none, action must be either deny or allow.
author Thilo Cestonaro <thilo@cestona.ro>
date Mon, 28 Sep 2009 18:17:20 +0200
parents 14b18ef8b554
children ccb07c0efc7e
comparison
equal deleted inserted replaced
15:14b18ef8b554 16:35e74c1094a7
13 local datamanager = require "util.datamanager"; 13 local datamanager = require "util.datamanager";
14 local bare_sessions = bare_sessions; 14 local bare_sessions = bare_sessions;
15 local util_Jid = require "util.jid"; 15 local util_Jid = require "util.jid";
16 local jid_bare = util_Jid.bare; 16 local jid_bare = util_Jid.bare;
17 local jid_split = util_Jid.split; 17 local jid_split = util_Jid.split;
18 local load_roster = require "core.rostermanager".load_roster;
19 local to_number = _G.tonumber;
18 20
19 function findNamedList (privacy_lists, name) 21 function findNamedList (privacy_lists, name)
20 local ret = nil 22 local ret = nil
21 if privacy_lists.lists == nil then 23 if privacy_lists.lists == nil then
22 module:log("debug", "no lists loaded.") 24 module:log("debug", "no lists loaded.")
75 return true; 77 return true;
76 end 78 end
77 return false; 79 return false;
78 end 80 end
79 81
80 function createOrReplaceList (privacy_lists, origin, stanza, name, entries) 82 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster)
81 module:log("info", "User requests to create / replace list named %s, item count: %d", name, #entries); 83 module:log("info", "User requests to create / replace list named %s, item count: %d", name, #entries);
82 local ret = true; 84 local ret = true;
83 local idx = findNamedList(privacy_lists, name); 85 local idx = findNamedList(privacy_lists, name);
84 local bare_jid = origin.username.."@"..origin.host; 86 local bare_jid = origin.username.."@"..origin.host;
85 87
89 91
90 if idx == nil then 92 if idx == nil then
91 idx = #privacy_lists.lists + 1; 93 idx = #privacy_lists.lists + 1;
92 end 94 end
93 95
96 local orderCheck = {};
94 local list = {}; 97 local list = {};
95 list.name = name; 98 list.name = name;
96 list.items = {}; 99 list.items = {};
97 100
98 for _,item in ipairs(entries) do 101 for _,item in ipairs(entries) do
102 if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then
103 return "bad-request";
104 end
99 local tmp = {}; 105 local tmp = {};
106 orderCheck[item.attr.order] = true;
107
100 tmp["type"] = item.attr.type; 108 tmp["type"] = item.attr.type;
101 tmp["value"] = item.attr.value; 109 tmp["value"] = item.attr.value;
102 tmp["action"] = item.attr.action; 110 tmp["action"] = item.attr.action;
103 tmp["order"] = item.attr.order; 111 tmp["order"] = to_number(item.attr.order);
104 tmp["presence-in"] = false; 112 tmp["presence-in"] = false;
105 tmp["presence-out"] = false; 113 tmp["presence-out"] = false;
106 tmp["message"] = false; 114 tmp["message"] = false;
107 tmp["iq"] = false; 115 tmp["iq"] = false;
108 116
109 if #item.tags > 0 then 117 if #item.tags > 0 then
110 for _,tag in ipairs(item.tags) do 118 for _,tag in ipairs(item.tags) do
111 tmp[tag.name] = true; 119 tmp[tag.name] = true;
112 end 120 end
113 end 121 end
122
123 if tmp.type == "group" then
124 local found = false;
125 local roster = load_roster(origin.username, origin.host);
126 local groups = roster.groups;
127 if groups == nil then
128 return "item-not-found";
129 end
130 for _,group in ipairs(groups) do
131 if group == tmp.value then
132 found = true;
133 end
134 end
135 if found == false then
136 return "item-not-found";
137 end
138 elseif tmp.type == "subscription" then
139 if tmp.value ~= "both" and
140 tmp.value ~= "to" and
141 tmp.value ~= "from" and
142 tmp.value ~= "none" then
143 return "bad-request";
144 end
145 end
146
147 if tmp.action ~= "deny" and tmp.action ~= "allow" then
148 return "bad-request";
149 end
150
114 list.items[#list.items + 1] = tmp; 151 list.items[#list.items + 1] = tmp;
115 end 152 end
116 153
117 table.sort(list, sortByOrder); 154 table.sort(list, sortByOrder);
118 155
193 elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list 230 elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list
194 if #tag.tags == 0 then -- Client removes a privacy list 231 if #tag.tags == 0 then -- Client removes a privacy list
195 valid = deleteList(privacy_lists, origin, stanza, tag.attr.name); 232 valid = deleteList(privacy_lists, origin, stanza, tag.attr.name);
196 else -- Client edits a privacy list 233 else -- Client edits a privacy list
197 valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags) 234 valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags)
235 if valid ~= true then
236 err_reply = st.error_reply(stanza, "cancel", valid);
237 valid = false;
238 end
198 end 239 end
199 end 240 end
200 end 241 end
201 end 242 end
202 elseif stanza.attr.type == "get" then 243 elseif stanza.attr.type == "get" then
287 (evilJid.host and item.value == evilJid.host) then 328 (evilJid.host and item.value == evilJid.host) then
288 module:log("debug", "jid matched."); 329 module:log("debug", "jid matched.");
289 apply = true; 330 apply = true;
290 block = (item.action == "deny"); 331 block = (item.action == "deny");
291 elseif item.type == "group" then 332 elseif item.type == "group" then
292 local groups = origin.roster[jid_bare(stanza.from)].groups; 333 local roster = load_roster(node_, host_);
334 local groups = roster.groups;
293 for _,group in ipairs(groups) do 335 for _,group in ipairs(groups) do
294 if group == item.value then 336 if group == item.value then
295 module:log("debug", "group matched."); 337 module:log("debug", "group matched.");
296 apply = true; 338 apply = true;
297 block = (item.action == "deny"); 339 block = (item.action == "deny");