mod_pep: Fix undefined global access
[prosody.git] / plugins / mod_message.lua
1
2 local full_sessions = full_sessions;
3 local bare_sessions = bare_sessions;
4
5 local jid_bare = require "util.jid".bare;
6 local jid_split = require "util.jid".split;
7 local user_exists = require "core.usermanager".user_exists;
8 local offlinemanager = require "core.offlinemanager";
9 local t_insert = table.insert;
10
11 local function select_top_resources(user)
12         local priority = 0;
13         local recipients = {};
14         for _, session in pairs(user.sessions) do -- find resource with greatest priority
15                 if session.presence then
16                         -- TODO check active privacy list for session
17                         local p = session.priority;
18                         if p > priority then
19                                 priority = p;
20                                 recipients = {session};
21                         elseif p == priority then
22                                 t_insert(recipients, session);
23                         end
24                 end
25         end
26         return recipients;
27 end
28
29 local function process_to_bare(bare, origin, stanza)
30         local user = bare_sessions[bare];
31         
32         local t = stanza.attr.type;
33         if t == "error" then
34                 -- discard
35         elseif t == "groupchat" then
36                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
37         elseif t == "headline" then
38                 if user then
39                         for _, session in pairs(user.sessions) do
40                                 if session.presence and session.priority >= 0 then
41                                         session.send(stanza);
42                                 end
43                         end
44                 end  -- current policy is to discard headlines if no recipient is available
45         else -- chat or normal message
46                 if user then -- some resources are connected
47                         local recipients = select_top_resources(user);
48                         if #recipients > 0 then
49                                 for i=1,#recipients do
50                                         recipients[i].send(stanza);
51                                 end
52                                 return true;
53                         end
54                 end
55                 -- no resources are online
56                 local node, host = jid_split(bare);
57                 if user_exists(node, host) then
58                         -- TODO apply the default privacy list
59                         offlinemanager.store(node, host, stanza);
60                 else
61                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
62                 end
63         end
64         return true;
65 end
66
67 module:hook("message/full", function(data)
68         -- message to full JID recieved
69         local origin, stanza = data.origin, data.stanza;
70         
71         local session = full_sessions[stanza.attr.to];
72         if session then
73                 -- TODO fire post processing event
74                 session.send(stanza);
75                 return true;
76         else -- resource not online
77                 return process_to_bare(jid_bare(stanza.attr.to), origin, stanza);
78         end
79 end);
80
81 module:hook("message/bare", function(data)
82         -- message to bare JID recieved
83         local origin, stanza = data.origin, data.stanza;
84
85         return process_to_bare(stanza.attr.to or (origin.username..'@'..origin.host), origin, stanza);
86 end);