mod_block_strangers/mod_block_strangers.lua
author Kim Alvefur <zash@zash.se>
Wed, 01 Jun 2016 14:50:39 +0200
changeset 2200 09f6e1a09b2b
parent 2064 bd0c5d546bf8
child 2423 045d594a3707
permissions -rw-r--r--
mod_block_strangers: Allow stanzas form jids the user has sent directed presence to
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
772
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
1933
82834df1dea6 mod_block_strangers: Add missing import of util.stanza
Kim Alvefur <zash@zash.se>
parents: 1932
diff changeset
     2
local st = require"util.stanza";
772
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local jid_split = require "util.jid".split;
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
local jid_bare = require "util.jid".bare;
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
2200
09f6e1a09b2b mod_block_strangers: Allow stanzas form jids the user has sent directed presence to
Kim Alvefur <zash@zash.se>
parents: 2064
diff changeset
     6
local full_sessions = prosody.full_sessions;
09f6e1a09b2b mod_block_strangers: Allow stanzas form jids the user has sent directed presence to
Kim Alvefur <zash@zash.se>
parents: 2064
diff changeset
     7
09f6e1a09b2b mod_block_strangers: Allow stanzas form jids the user has sent directed presence to
Kim Alvefur <zash@zash.se>
parents: 2064
diff changeset
     8
local function has_directed_presence(user, jid)
09f6e1a09b2b mod_block_strangers: Allow stanzas form jids the user has sent directed presence to
Kim Alvefur <zash@zash.se>
parents: 2064
diff changeset
     9
	local session = full_sessions[user];
09f6e1a09b2b mod_block_strangers: Allow stanzas form jids the user has sent directed presence to
Kim Alvefur <zash@zash.se>
parents: 2064
diff changeset
    10
	return session and session.directed[jid];
09f6e1a09b2b mod_block_strangers: Allow stanzas form jids the user has sent directed presence to
Kim Alvefur <zash@zash.se>
parents: 2064
diff changeset
    11
end
772
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
function check_subscribed(event)
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
	local stanza = event.stanza;
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
	local to_user, to_host, to_resource = jid_split(stanza.attr.to);
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
	local from_jid = jid_bare(stanza.attr.from);
2200
09f6e1a09b2b mod_block_strangers: Allow stanzas form jids the user has sent directed presence to
Kim Alvefur <zash@zash.se>
parents: 2064
diff changeset
    17
	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
2064
bd0c5d546bf8 mod_block_strangers: Allow iq/full responses through
Matthew Wild <mwild1@gmail.com>
parents: 1933
diff changeset
    18
		if to_resource and stanza.attr.type == "groupchat"
bd0c5d546bf8 mod_block_strangers: Allow iq/full responses through
Matthew Wild <mwild1@gmail.com>
parents: 1933
diff changeset
    19
		or stanza.name == "iq" and (stanza.attr.type == "result" or stanza.attr.type == "error") then
772
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
			return nil; -- Pass through
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
		end
1932
252b634b065d mod_block_strangers: Bounce IQ stanzas (they MUST be replied to)
Kim Alvefur <zash@zash.se>
parents: 1325
diff changeset
    22
		if stanza.name == "iq" and ( stanza.attr.type == "get" or stanza.attr.type == "set" ) then
252b634b065d mod_block_strangers: Bounce IQ stanzas (they MUST be replied to)
Kim Alvefur <zash@zash.se>
parents: 1325
diff changeset
    23
			event.origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
252b634b065d mod_block_strangers: Bounce IQ stanzas (they MUST be replied to)
Kim Alvefur <zash@zash.se>
parents: 1325
diff changeset
    24
		end
1325
b21236b6b8d8 Backed out changeset 853a382c9bd6
Kim Alvefur <zash@zash.se>
parents: 1324
diff changeset
    25
		return true; -- Drop stanza
772
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
	end
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
end
954532e273be mod_block_strangers: Module to block message and iqs from people not on your roster
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
774
52caf54fc270 mod_block_strangers: Bump handler priority to 200 (just because)
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    29
module:hook("message/bare", check_subscribed, 200);
52caf54fc270 mod_block_strangers: Bump handler priority to 200 (just because)
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    30
module:hook("message/full", check_subscribed, 200);
52caf54fc270 mod_block_strangers: Bump handler priority to 200 (just because)
Matthew Wild <mwild1@gmail.com>
parents: 772
diff changeset
    31
module:hook("iq/full", check_subscribed, 200);