mod_posix: Some (perhaps temporary) changes to re-lock the pidfile after truncating...
[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 local offlinemanager = require "core.offlinemanager";
18 local t_insert = table.insert;
19
20 local function process_to_bare(bare, origin, stanza)
21         local user = bare_sessions[bare];
22         
23         local t = stanza.attr.type;
24         if t == "error" then
25                 -- discard
26         elseif t == "groupchat" then
27                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
28         elseif t == "headline" then
29                 if user then
30                         for _, session in pairs(user.sessions) do
31                                 if session.presence and session.priority >= 0 then
32                                         session.send(stanza);
33                                 end
34                         end
35                 end  -- current policy is to discard headlines if no recipient is available
36         else -- chat or normal message
37                 if user then -- some resources are connected
38                         local recipients = user.top_resources;
39                         if recipients then
40                                 for i=1,#recipients do
41                                         recipients[i].send(stanza);
42                                 end
43                                 return true;
44                         end
45                 end
46                 -- no resources are online
47                 local node, host = jid_split(bare);
48                 if user_exists(node, host) then
49                         -- TODO apply the default privacy list
50                         offlinemanager.store(node, host, stanza);
51                 else
52                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
53                 end
54         end
55         return true;
56 end
57
58 module:hook("message/full", function(data)
59         -- message to full JID recieved
60         local origin, stanza = data.origin, data.stanza;
61         
62         local session = full_sessions[stanza.attr.to];
63         if session then
64                 -- TODO fire post processing event
65                 session.send(stanza);
66                 return true;
67         else -- resource not online
68                 return process_to_bare(jid_bare(stanza.attr.to), origin, stanza);
69         end
70 end);
71
72 module:hook("message/bare", function(data)
73         -- message to bare JID recieved
74         local origin, stanza = data.origin, data.stanza;
75
76         return process_to_bare(stanza.attr.to or (origin.username..'@'..origin.host), origin, stanza);
77 end);
78
79 module:add_feature("msgoffline");