Comparison

plugins/mod_message.lua @ 1274:50babb72edac

mod_message: mod_message now handles all cases
author Waqas Hussain <waqas20@gmail.com>
date Mon, 01 Jun 2009 18:11:01 +0500
parent 1272:28f9041d8c55
child 1275:850cf92b8ad4
comparison
equal deleted inserted replaced
1273:85353014ff34 1274:50babb72edac
2 local full_sessions = full_sessions; 2 local full_sessions = full_sessions;
3 local bare_sessions = bare_sessions; 3 local bare_sessions = bare_sessions;
4 4
5 local jid_bare = require "util.jid".bare; 5 local jid_bare = require "util.jid".bare;
6 local user_exists = require "core.usermanager".user_exists; 6 local user_exists = require "core.usermanager".user_exists;
7 local offlinemanager = require "core.offlinemanager";
8
9 local function select_top_resources(user)
10 local priority = 0;
11 local recipients = {};
12 for _, session in pairs(user.sessions) do -- find resource with greatest priority
13 if session.presence then
14 local p = session.priority;
15 if p > priority then
16 priority = p;
17 recipients = {session};
18 elseif p == priority then
19 t_insert(recipients, session);
20 end
21 end
22 end
23 return recipients;
24 end
7 25
8 local function process_to_bare(bare, origin, stanza) 26 local function process_to_bare(bare, origin, stanza)
9 local sessions = bare_sessions[bare]; 27 local user = bare_sessions[bare];
10 28
11 local t = stanza.attr.type; 29 local t = stanza.attr.type;
12 if t == "error" then return true; end 30 if t == "error" then return true; end
13 if t == "groupchat" then 31 if t == "groupchat" then
14 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); 32 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
15 return true; 33 return true;
16 end 34 end
17 35 if t == "headline" then
18 if sessions then 36 if user then
19 -- some resources are connected 37 for _, session in pairs(user.sessions) do
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 38 if session.presence and session.priority >= 0 then
25 session.send(stanza); 39 session.send(stanza);
26 end 40 end
27 end 41 end
42 end -- current policy is to discard headlines if no recipient is available
43 return true;
44 end
45 -- chat or normal message
46 if user then -- some resources are connected
47 local recipients = select_top_resources(user);
48 if #recipients > 0 then
49 for i=1,#recipients do
50 recipients[i].send(stanza);
51 end
28 return true; 52 return true;
29 end 53 end
30 -- TODO find top resources willing to accept this message
31 -- TODO then send them each the stanza
32 return;
33 end 54 end
34 -- no resources are online 55 -- no resources are online
35 if t == "headline" then return true; end -- current policy is to discard headlines 56 local node, host = jid_split(bare);
36 -- chat or normal message 57 if user_exists(node, host) then
37 -- TODO check if the user exists 58 -- TODO apply the default privacy list
38 -- TODO if it doesn't, return an error reply 59 offlinemanager.store(node, host, stanza);
39 -- TODO otherwise, apply the default privacy list 60 else
40 -- TODO and store into offline storage 61 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
41 -- TODO or maybe the offline store can apply privacy lists 62 end
63 return true;
42 end 64 end
43 65
44 module:hook("message/full", function(data) 66 module:hook("message/full", function(data)
45 -- message to full JID recieved 67 -- message to full JID recieved
46 local origin, stanza = data.origin, data.stanza; 68 local origin, stanza = data.origin, data.stanza;