2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local full_sessions = full_sessions;
11 local bare_sessions = bare_sessions;
13 local st = require "util.stanza";
14 local jid_bare = require "util.jid".bare;
15 local jid_split = require "util.jid".split;
16 local user_exists = require "core.usermanager".user_exists;
18 local function process_to_bare(bare, origin, stanza)
19 local user = bare_sessions[bare];
21 local t = stanza.attr.type;
24 elseif t == "groupchat" then
25 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
26 elseif t == "headline" then
27 if user and stanza.attr.to == bare then
28 for _, session in pairs(user.sessions) do
29 if session.presence and session.priority >= 0 then
33 end -- current policy is to discard headlines if no recipient is available
34 else -- chat or normal message
35 if user then -- some resources are connected
36 local recipients = user.top_resources;
39 for i=1,#recipients do
40 sent = recipients[i].send(stanza) or sent;
47 -- no resources are online
48 local node, host = jid_split(bare);
50 if user_exists(node, host) then
51 -- TODO apply the default privacy list
53 ok = module:fire_event('message/offline/handle', {
60 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
66 module:hook("message/full", function(data)
67 -- message to full JID recieved
68 local origin, stanza = data.origin, data.stanza;
70 local session = full_sessions[stanza.attr.to];
71 if session and session.send(stanza) then
73 else -- resource not online
74 return process_to_bare(jid_bare(stanza.attr.to), origin, stanza);
78 module:hook("message/bare", function(data)
79 -- message to bare JID recieved
80 local origin, stanza = data.origin, data.stanza;
82 return process_to_bare(stanza.attr.to or (origin.username..'@'..origin.host), origin, stanza);
85 module:add_feature("msgoffline");