mod_bind2/mod_bind2.lua
changeset 4797 aaa6f412dce3
child 4798 d17a1581ea30
equal deleted inserted replaced
4796:9d57aa79c5d9 4797:aaa6f412dce3
       
     1 local mm = require "core.modulemanager";
       
     2 local sm = require "core.sessionmanager";
       
     3 
       
     4 local xmlns_bind2 --[[<const>]] = "urn:xmpp:bind2:0";
       
     5 local xmlns_carbons --[[<const>]] = "urn:xmpp:carbons:2";
       
     6 
       
     7 module:depends("sasl2");
       
     8 module:depends("carbons");
       
     9 
       
    10 module:hook_tag(xmlns_sasl2, "authenticate", function (session, auth)
       
    11 	session.bind2 = auth:get_child("bind", xmlns_bind2);
       
    12 end, 1);
       
    13 
       
    14 module:hook("sasl2/c2s/success", function (event)
       
    15 	local session = event.session;
       
    16 	if not session.bind2 then return end
       
    17 
       
    18 	-- When it receives a bind 2.0 on an authenticated not-yet-bound session, the
       
    19 	-- server MUST:
       
    20 
       
    21 	-- Clear the offline messages for this user, if any, without sending them (as
       
    22 	-- they will be provided by MAM).
       
    23 	if mm.is_loaded(module.host, "offline") then
       
    24 		-- TODO
       
    25 	end
       
    26 
       
    27 	-- Perform resource binding to a random resource (see 6120)
       
    28 	if not sm.bind_resource(session, nil) then
       
    29 		-- FIXME How should this be handled even?
       
    30 		session:close("reset");
       
    31 		return true;
       
    32 	end
       
    33 
       
    34 	-- Work out which contacts have unread messages in the user's MAM archive,
       
    35 	-- how many, and what the id of the last read message is
       
    36 	-- XXX How do we know what the last read message was?
       
    37 	-- TODO archive:summary(session.username, { after = ??? });
       
    38 
       
    39 	-- Get the id of the newest stanza in the user's MAM archive
       
    40 	-- TODO archive:find(session.username, { reverse = true, limit = 1 });
       
    41 
       
    42 	-- Silently enable carbons for this session
       
    43 	session.carbons = xmlns_carbons;
       
    44 
       
    45 	-- After processing the bind stanza, as above, the server MUST respond with
       
    46 	-- an element of type 'bound' in the namespace 'urn:xmpp:bind2:0', as in the
       
    47 	-- below example
       
    48 	event.success:tag("bound", xmlns_bind2):text_tag("jid", session.full_jid):up();
       
    49 
       
    50 	session.bind2 = nil;
       
    51 end);