03e2081dafa4c634819675eb84a42cd100e737c3
[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 user_exists = require "core.usermanager".user_exists;
7
8 local function process_to_bare(bare, origin, stanza)
9         local sessions = bare_sessions[bare];
10         
11         local t = stanza.attr.type;
12         if t == "error" then return true; end
13         if t == "groupchat" then
14                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
15                 return true;
16         end
17
18         if sessions then
19                 -- some resources are connected
20                 sessions = sessions.sessions;
21                 
22                 if t == "headline" then
23                         for _, session in pairs(sessions) do
24                                 if session.presence and session.priority >= 0 then
25                                         session.send(stanza);
26                                 end
27                         end
28                         return true;
29                 end
30                 -- TODO find top resources willing to accept this message
31                 -- TODO then send them each the stanza
32                 return;
33         end
34         -- no resources are online
35         if t == "headline" then return true; end -- current policy is to discard headlines
36         -- chat or normal message
37         -- TODO check if the user exists
38         -- TODO if it doesn't, return an error reply
39         -- TODO otherwise, apply the default privacy list
40         -- TODO and store into offline storage
41         -- TODO or maybe the offline store can apply privacy lists
42 end
43
44 module:hook("message/full", function(data)
45         -- message to full JID recieved
46         local origin, stanza = data.origin, data.stanza;
47         
48         local session = full_sessions[stanza.attr.to];
49         if session then
50                 -- TODO fire post processing event
51                 session.send(stanza);
52                 return true;
53         else -- resource not online
54                 return process_to_bare(jid_bare(stanza.attr.to), origin, stanza);
55         end
56 end);
57
58 module:hook("message/bare", function(data)
59         -- message to bare JID recieved
60         local origin, stanza = data.origin, data.stanza;
61
62         return process_to_bare(stanza.attr.to or (origin.username..'@'..origin.host), origin, stanza);
63 end);