Merge with backout
[prosody.git] / plugins / mod_message.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local full_sessions = full_sessions;
11 local bare_sessions = bare_sessions;
12
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;
17
18 local function process_to_bare(bare, origin, stanza)
19         local user = bare_sessions[bare];
20         
21         local t = stanza.attr.type;
22         if t == "error" then
23                 -- discard
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
30                                         session.send(stanza);
31                                 end
32                         end
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;
37                         if recipients then
38                                 for i=1,#recipients do
39                                         recipients[i].send(stanza);
40                                 end
41                                 return true;
42                         end
43                 end
44                 -- no resources are online
45                 local node, host = jid_split(bare);
46                 local ok
47                 if user_exists(node, host) then
48                         -- TODO apply the default privacy list
49
50                         ok = module:fire_event('message/offline/handle', {
51                             origin = origin,
52                             stanza = stanza,
53                         });
54                 end
55
56                 if not ok then
57                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
58                 end
59         end
60         return true;
61 end
62
63 module:hook("message/full", function(data)
64         -- message to full JID recieved
65         local origin, stanza = data.origin, data.stanza;
66         
67         local session = full_sessions[stanza.attr.to];
68         if session then
69                 -- TODO fire post processing event
70                 session.send(stanza);
71                 return true;
72         else -- resource not online
73                 return process_to_bare(jid_bare(stanza.attr.to), origin, stanza);
74         end
75 end);
76
77 module:hook("message/bare", function(data)
78         -- message to bare JID recieved
79         local origin, stanza = data.origin, data.stanza;
80
81         return process_to_bare(stanza.attr.to or (origin.username..'@'..origin.host), origin, stanza);
82 end);
83
84 module:add_feature("msgoffline");