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