+local function process_to_bare(bare, origin, stanza)
+ local user = bare_sessions[bare];
+
+ local t = stanza.attr.type;
+ if t == "error" then
+ -- discard
+ elseif t == "groupchat" then
+ origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
+ elseif t == "headline" then
+ if user and stanza.attr.to == bare then
+ for _, session in pairs(user.sessions) do
+ if session.presence and session.priority >= 0 then
+ session.send(stanza);
+ end
+ end
+ end -- current policy is to discard headlines if no recipient is available
+ else -- chat or normal message
+ if user then -- some resources are connected
+ local recipients = user.top_resources;
+ if recipients then
+ local sent;
+ for i=1,#recipients do
+ sent = recipients[i].send(stanza) or sent;
+ end
+ if sent then
+ return true;
+ end
+ end
+ end
+ -- no resources are online
+ local node, host = jid_split(bare);
+ local ok
+ if user_exists(node, host) then
+ -- TODO apply the default privacy list
+
+ ok = module:fire_event('message/offline/handle', {
+ origin = origin,
+ stanza = stanza,
+ });
+ end
+
+ if not ok then
+ origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
+ end
+ end
+ return true;
+end
+