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
];
11 local directed
= session
.directed
;
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
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
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"));
34 return true; -- Drop stanza
38 module
:hook("message/bare", check_subscribed
, 200);
39 module
:hook("message/full", check_subscribed
, 200);
40 module
:hook("iq/full", check_subscribed
, 200);