mod_muc_webchat_url: Don't save templated value
[prosody-modules.git] / mod_block_strangers / mod_block_strangers.lua
blob00319bc59954121667d5f4516a735ddf8b1c558e
2 local st = require"util.stanza";
3 local jid_split = require "util.jid".split;
4 local jid_bare = require "util.jid".bare;
5 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
6 local full_sessions = prosody.full_sessions;
8 local function has_directed_presence(user, jid)
9 local session = full_sessions[user];
10 if session then
11 local directed = session.directed;
12 if directed then
13 return directed[jid];
14 end
15 end
16 end
18 function check_subscribed(event)
19 local stanza = event.stanza;
20 local to_user, to_host, to_resource = jid_split(stanza.attr.to);
21 local from_jid = jid_bare(stanza.attr.from);
22 if to_user and not has_directed_presence(stanza.attr.to, from_jid) and not is_contact_subscribed(to_user, to_host, from_jid) then
23 -- Allow all messages from your own jid
24 if from_jid == to_user.."@"..to_host then
25 return nil; -- Pass through
26 end
27 if to_resource and stanza.attr.type == "groupchat"
28 or stanza.name == "iq" and (stanza.attr.type == "result" or stanza.attr.type == "error") then
29 return nil; -- Pass through
30 end
31 if stanza.name == "iq" and ( stanza.attr.type == "get" or stanza.attr.type == "set" ) then
32 event.origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
33 end
34 return true; -- Drop stanza
35 end
36 end
38 module:hook("message/bare", check_subscribed, 200);
39 module:hook("message/full", check_subscribed, 200);
40 module:hook("iq/full", check_subscribed, 200);