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