Merge with 0.4
authorMatthew Wild <mwild1@gmail.com>
Sun, 3 May 2009 00:11:21 +0000 (01:11 +0100)
committerMatthew Wild <mwild1@gmail.com>
Sun, 3 May 2009 00:11:21 +0000 (01:11 +0100)
core/componentmanager.lua
core/eventmanager2.lua [new file with mode: 0644]
core/stanza_router.lua
net/http.lua
net/httpserver.lua
net/server.lua
net/xmppclient_listener.lua
net/xmppserver_listener.lua
plugins/mod_bosh.lua
plugins/mod_presence.lua [new file with mode: 0644]

index 93e2ae7b1d64ec54c9db705fca3eb7bf13f3b92b..d76b78495ad092010e034d75002b6fb50bd3e62d 100644 (file)
@@ -80,8 +80,7 @@ end
 
 function create_component(host, component)
        -- TODO check for host well-formedness
-       local session = session or { type = "component", host = host, connected = true, s2sout = {} };
-       return session;
+       return { type = "component", host = host, connected = true, s2sout = {} };
 end
 
 function register_component(host, component, session)
diff --git a/core/eventmanager2.lua b/core/eventmanager2.lua
new file mode 100644 (file)
index 0000000..4783d0a
--- /dev/null
@@ -0,0 +1,6 @@
+\r
+local events = require "util.events".new();\r
+\r
+module "eventmanager"\r
+\r
+return events;\r
index 08224c8ddac927cba338d73ea4b4761ae3829d3a..80b51cc62431277137f3e2c827eadb1f2db62aa5 100644 (file)
@@ -10,6 +10,8 @@
 
 local log = require "util.logger".init("stanzarouter")
 
+local hosts = _G.hosts;
+
 local st = require "util.stanza";
 local send_s2s = require "core.s2smanager".send_to_host;
 local user_exists = require "core.usermanager".user_exists;
@@ -24,9 +26,9 @@ local s2s_make_authenticated = require "core.s2smanager".make_authenticated;
 local modules_handle_stanza = require "core.modulemanager".handle_stanza;
 local component_handle_stanza = require "core.componentmanager".handle_stanza;
 
-local handle_outbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
-local handle_inbound_presence_subscriptions_and_probes = require "core.presencemanager".handle_inbound_presence_subscriptions_and_probes;
-local handle_normal_presence = require "core.presencemanager".handle_normal_presence;
+local handle_outbound_presence_subscriptions_and_probes = function()end;--require "core.presencemanager".handle_outbound_presence_subscriptions_and_probes;
+local handle_inbound_presence_subscriptions_and_probes = function()end;--require "core.presencemanager".handle_inbound_presence_subscriptions_and_probes;
+local handle_normal_presence = function()end;--require "core.presencemanager".handle_normal_presence;
 
 local format = string.format;
 local tostring = tostring;
@@ -40,6 +42,7 @@ local ipairs = ipairs;
 local jid_split = require "util.jid".split;
 local jid_prepped_split = require "util.jid".prepped_split;
 local print = print;
+local fire_event = require "core.eventmanager2".fire_event;
 local function checked_error_reply(origin, stanza)
        if (stanza.attr.xmlns == "jabber:client" or stanza.attr.xmlns == "jabber:server") and stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
                origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME correct error?
@@ -100,6 +103,8 @@ function core_process_stanza(origin, stanza)
 
        -- FIXME do stanzas not of jabber:client get handled by components?
        if (origin.type == "s2sin" or origin.type == "c2s" or origin.type == "component") and (not xmlns or xmlns == "jabber:server" or xmlns == "jabber:client") then                  
+               local event_data = {origin=origin, stanza=stanza};
+               fire_event(tostring(host or origin.host).."/"..stanza.name, event_data);
                if origin.type == "s2sin" and not origin.dummy then
                        local host_status = origin.hosts[from_host];
                        if not host_status or not host_status.authed then -- remote server trying to impersonate some other server?
index 29da41ea825f89d290e61f8253fe5d15bf630b7d..24828ffeb8d848af6cdd12b01d9c1695eef6ca33 100644 (file)
@@ -9,8 +9,8 @@ local connlisteners_get = require "net.connlisteners".get;
 local listener = connlisteners_get("httpclient") or error("No httpclient listener!");
 
 local t_insert, t_concat = table.insert, table.concat;
-local tonumber, tostring, pairs, xpcall, select, debug_traceback = 
-        tonumber, tostring, pairs, xpcall, select, debug.traceback;
+local tonumber, tostring, pairs, xpcall, select, debug_traceback, char = 
+        tonumber, tostring, pairs, xpcall, select, debug.traceback, string.char;
 
 local log = require "util.logger".init("http");
 local print = function () end
index 906ffaf74bb64185c2b67146d4c4b9a50cd01c55..0ecf84ea4e84bf5a24c010bced0c73e2400682b1 100644 (file)
@@ -8,10 +8,10 @@ local connlisteners_get = require "net.connlisteners".get;
 local listener;
 
 local t_insert, t_concat = table.insert, table.concat;
-local s_match, s_gmatch = string.match, string.gmatch;
+local s_match, s_gmatch, s_char = string.match, string.gmatch;
 local tonumber, tostring, pairs = tonumber, tostring, pairs;
 
-local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
+local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = s_char(tonumber("0x"..k)); return t[k]; end });
 local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
 
 local log = require "util.logger".init("httpserver");
index b2b19c70992375fea4c7e293ff422876cbee058d..9e42b8ff8006fe2d9b5d7c991b71dae905a4c20e 100644 (file)
@@ -791,7 +791,7 @@ local wrapclient = function( socket, ip, serverport, listeners, pattern, sslctx,
 end\r
 \r
 local addclient = function( address, port, listeners, pattern, sslctx, startssl )\r
-    local client, err = socket.tcp( )\r
+    local client, err = luasocket.tcp( )\r
     if err then\r
         return nil, err\r
     end\r
@@ -800,7 +800,7 @@ local addclient = function( address, port, listeners, pattern, sslctx, startssl
     if err then    -- try again\r
         local handler = wrapclient( client, address, port, listeners )\r
     else\r
-        wrapconnection( server, listeners, socket, address, port, "clientport", pattern, sslctx, startssl )\r
+        wrapconnection( nil, listeners, client, address, port, "clientport", pattern, sslctx, startssl )\r
     end\r
 end\r
 \r
index 4485283492993131e0570f5e752bd497f580ff15..dbb87b912e934ff22631250f164bb7da78c1494a 100644 (file)
@@ -9,6 +9,7 @@
 
 
 local logger = require "logger";
+local log = logger.init("xmppclient_listener");
 local lxp = require "lxp"
 local init_xmlhandlers = require "core.xmlhandlers"
 local sm_new_session = require "core.sessionmanager".new_session;
@@ -20,10 +21,11 @@ local t_concat = table.concat;
 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end
 local m_random = math.random;
 local format = string.format;
-local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session");
+local sessionmanager = require "core.sessionmanager";
+local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
 local sm_streamopened = sessionmanager.streamopened;
 local sm_streamclosed = sessionmanager.streamclosed;
-local st = stanza;
+local st = require "util.stanza";
 
 local stream_callbacks = { stream_tag = "http://etherx.jabber.org/streams|stream", 
                default_ns = "jabber:client",
index 0216e8dce382ec388b5bb8271d5c070c268c91cf..ddfb185e5562a79eae7643228094688df1bdef66 100644 (file)
@@ -9,9 +9,9 @@
 
 
 local logger = require "logger";
+local log = logger.init("xmppserver_listener");
 local lxp = require "lxp"
 local init_xmlhandlers = require "core.xmlhandlers"
-local sm_new_session = require "core.sessionmanager".new_session;
 local s2s_new_incoming = require "core.s2smanager".new_incoming;
 local s2s_streamopened = require "core.s2smanager".streamopened;
 local s2s_streamclosed = require "core.s2smanager".streamclosed;
@@ -42,8 +42,9 @@ local t_concat = table.concat;
 local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end
 local m_random = math.random;
 local format = string.format;
-local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session");
-local st = stanza;
+local sessionmanager = require "core.sessionmanager";
+local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
+local st = require "util.stanza";
 
 local sessions = {};
 local xmppserver = { default_port = 5269, default_mode = "*a" };
index d15f363db302c09035bcab4a08c5a403d46742ab..653af8bbdbfb419a5c2d7f6fca59fcc3b5a3a3f3 100644 (file)
@@ -1,6 +1,7 @@
 
 module.host = "*" -- Global module
 
+local hosts = _G.hosts;
 local lxp = require "lxp";
 local init_xmlhandlers = require "core.xmlhandlers"
 local server = require "net.server";
diff --git a/plugins/mod_presence.lua b/plugins/mod_presence.lua
new file mode 100644 (file)
index 0000000..2fb976b
--- /dev/null
@@ -0,0 +1,279 @@
+-- Prosody IM v0.4\r
+-- Copyright (C) 2008-2009 Matthew Wild\r
+-- Copyright (C) 2008-2009 Waqas Hussain\r
+-- \r
+-- This project is MIT/X11 licensed. Please see the\r
+-- COPYING file in the source package for more information.\r
+--\r
+\r
+\r
+\r
+local log = require "util.logger".init("mod_presence")\r
+\r
+local require = require;\r
+local pairs, ipairs = pairs, ipairs;\r
+local t_concat = table.concat;\r
+local s_find = string.find;\r
+local tonumber = tonumber;\r
+\r
+local st = require "util.stanza";\r
+local jid_split = require "util.jid".split;\r
+local jid_bare = require "util.jid".bare;\r
+local hosts = hosts;\r
+\r
+local rostermanager = require "core.rostermanager";\r
+local sessionmanager = require "core.sessionmanager";\r
+local offlinemanager = require "core.offlinemanager";\r
+\r
+local _core_route_stanza = core_route_stanza;\r
+local core_route_stanza;\r
+function core_route_stanza(origin, stanza)\r
+       if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then\r
+               local node, host = jid_split(stanza.attr.to);\r
+               host = hosts[host];\r
+               if host and host.type == "local" then\r
+                       handle_inbound_presence_subscriptions_and_probes(origin, stanza, jid_bare(stanza.attr.from), jid_bare(stanza.attr.to), core_route_stanza);\r
+                       return;\r
+               end\r
+       end\r
+       _core_route_stanza(origin, stanza);\r
+end\r
+\r
+function handle_presence(origin, stanza, from_bare, to_bare, core_route_stanza, inbound)\r
+       local type = stanza.attr.type;\r
+       if type and type ~= "unavailable" and type ~= "error" then\r
+               if inbound then\r
+                       handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);\r
+               else\r
+                       handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);\r
+               end\r
+       elseif not inbound and not stanza.attr.to then\r
+               handle_normal_presence(origin, stanza, core_route_stanza);\r
+       else\r
+               core_route_stanza(origin, stanza);\r
+       end\r
+end\r
+\r
+function handle_normal_presence(origin, stanza, core_route_stanza)\r
+       if origin.roster then\r
+               for jid in pairs(origin.roster) do -- broadcast to all interested contacts\r
+                       local subscription = origin.roster[jid].subscription;\r
+                       if subscription == "both" or subscription == "from" then\r
+                               stanza.attr.to = jid;\r
+                               core_route_stanza(origin, stanza);\r
+                       end\r
+               end\r
+               local node, host = jid_split(stanza.attr.from);\r
+               for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast to all resources\r
+                       if res ~= origin and res.presence then -- to resource\r
+                               stanza.attr.to = res.full_jid;\r
+                               core_route_stanza(origin, stanza);\r
+                       end\r
+               end\r
+               if stanza.attr.type == nil and not origin.presence then -- initial presence\r
+                       local probe = st.presence({from = origin.full_jid, type = "probe"});\r
+                       for jid in pairs(origin.roster) do -- probe all contacts we are subscribed to\r
+                               local subscription = origin.roster[jid].subscription;\r
+                               if subscription == "both" or subscription == "to" then\r
+                                       probe.attr.to = jid;\r
+                                       core_route_stanza(origin, probe);\r
+                               end\r
+                       end\r
+                       for _, res in pairs(hosts[host].sessions[node].sessions) do -- broadcast from all available resources\r
+                               if res ~= origin and res.presence then\r
+                                       res.presence.attr.to = origin.full_jid;\r
+                                       core_route_stanza(res, res.presence);\r
+                                       res.presence.attr.to = nil;\r
+                               end\r
+                       end\r
+                       if origin.roster.pending then -- resend incoming subscription requests\r
+                               for jid in pairs(origin.roster.pending) do\r
+                                       origin.send(st.presence({type="subscribe", from=jid})); -- TODO add to attribute? Use original?\r
+                               end\r
+                       end\r
+                       local request = st.presence({type="subscribe", from=origin.username.."@"..origin.host});\r
+                       for jid, item in pairs(origin.roster) do -- resend outgoing subscription requests\r
+                               if item.ask then\r
+                                       request.attr.to = jid;\r
+                                       core_route_stanza(origin, request);\r
+                               end\r
+                       end\r
+                       local offline = offlinemanager.load(node, host);\r
+                       if offline then\r
+                               for _, msg in ipairs(offline) do\r
+                                       origin.send(msg); -- FIXME do we need to modify to/from in any way?\r
+                               end\r
+                               offlinemanager.deleteAll(node, host);\r
+                       end\r
+               end\r
+               origin.priority = 0;\r
+               if stanza.attr.type == "unavailable" then\r
+                       origin.presence = nil;\r
+                       if origin.directed then\r
+                               local old_from = stanza.attr.from;\r
+                               stanza.attr.from = origin.full_jid;\r
+                               for jid in pairs(origin.directed) do\r
+                                       stanza.attr.to = jid;\r
+                                       core_route_stanza(origin, stanza);\r
+                               end\r
+                               stanza.attr.from = old_from;\r
+                               origin.directed = nil;\r
+                       end\r
+               else\r
+                       origin.presence = stanza;\r
+                       local priority = stanza:child_with_name("priority");\r
+                       if priority and #priority > 0 then\r
+                               priority = t_concat(priority);\r
+                               if s_find(priority, "^[+-]?[0-9]+$") then\r
+                                       priority = tonumber(priority);\r
+                                       if priority < -128 then priority = -128 end\r
+                                       if priority > 127 then priority = 127 end\r
+                                       origin.priority = priority;\r
+                               end\r
+                       end\r
+               end\r
+               stanza.attr.to = nil; -- reset it\r
+       else\r
+               log("error", "presence recieved from client with no roster");\r
+       end\r
+end\r
+\r
+function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza)\r
+       local h = hosts[host];\r
+       local count = 0;\r
+       if h and h.type == "local" then\r
+               local u = h.sessions[user];\r
+               if u then\r
+                       for k, session in pairs(u.sessions) do\r
+                               local pres = session.presence;\r
+                               if pres then\r
+                                       pres.attr.to = jid;\r
+                                       pres.attr.from = session.full_jid;\r
+                                       core_route_stanza(session, pres);\r
+                                       pres.attr.to = nil;\r
+                                       pres.attr.from = nil;\r
+                                       count = count + 1;\r
+                               end\r
+                       end\r
+               end\r
+       end\r
+       log("info", "broadcasted presence of "..count.." resources from "..user.."@"..host.." to "..jid);\r
+       return count;\r
+end\r
+\r
+function handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
+       local node, host = jid_split(from_bare);\r
+       local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
+       stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
+       log("debug", "outbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
+       if stanza.attr.type == "subscribe" then\r
+               -- 1. route stanza\r
+               -- 2. roster push (subscription = none, ask = subscribe)\r
+               if rostermanager.set_contact_pending_out(node, host, to_bare) then\r
+                       rostermanager.roster_push(node, host, to_bare);\r
+               end -- else file error\r
+               core_route_stanza(origin, stanza);\r
+       elseif stanza.attr.type == "unsubscribe" then\r
+               -- 1. route stanza\r
+               -- 2. roster push (subscription = none or from)\r
+               if rostermanager.unsubscribe(node, host, to_bare) then\r
+                       rostermanager.roster_push(node, host, to_bare); -- FIXME do roster push when roster has in fact not changed?\r
+               end -- else file error\r
+               core_route_stanza(origin, stanza);\r
+       elseif stanza.attr.type == "subscribed" then\r
+               -- 1. route stanza\r
+               -- 2. roster_push ()\r
+               -- 3. send_presence_of_available_resources\r
+               if rostermanager.subscribed(node, host, to_bare) then\r
+                       rostermanager.roster_push(node, host, to_bare);\r
+               end\r
+               core_route_stanza(origin, stanza);\r
+               send_presence_of_available_resources(node, host, to_bare, origin, core_route_stanza);\r
+       elseif stanza.attr.type == "unsubscribed" then\r
+               -- 1. route stanza\r
+               -- 2. roster push (subscription = none or to)\r
+               if rostermanager.unsubscribed(node, host, to_bare) then\r
+                       rostermanager.roster_push(node, host, to_bare);\r
+               end\r
+               core_route_stanza(origin, stanza);\r
+       end\r
+       stanza.attr.from, stanza.attr.to = st_from, st_to;\r
+end\r
+\r
+function handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza)\r
+       local node, host = jid_split(to_bare);\r
+       local st_from, st_to = stanza.attr.from, stanza.attr.to;\r
+       stanza.attr.from, stanza.attr.to = from_bare, to_bare;\r
+       log("debug", "inbound presence "..stanza.attr.type.." from "..from_bare.." for "..to_bare);\r
+       if stanza.attr.type == "probe" then\r
+               if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
+                       if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
+                               -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
+                       end\r
+               else\r
+                       core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="unsubscribed"}));\r
+               end\r
+       elseif stanza.attr.type == "subscribe" then\r
+               if rostermanager.is_contact_subscribed(node, host, from_bare) then\r
+                       core_route_stanza(origin, st.presence({from=to_bare, to=from_bare, type="subscribed"})); -- already subscribed\r
+                       -- Sending presence is not clearly stated in the RFC, but it seems appropriate\r
+                       if 0 == send_presence_of_available_resources(node, host, from_bare, origin, core_route_stanza) then\r
+                               -- TODO send last recieved unavailable presence (or we MAY do nothing, which is fine too)\r
+                       end\r
+               else\r
+                       if not rostermanager.is_contact_pending_in(node, host, from_bare) then\r
+                               if rostermanager.set_contact_pending_in(node, host, from_bare) then\r
+                                       sessionmanager.send_to_available_resources(node, host, stanza);\r
+                               end -- TODO else return error, unable to save\r
+                       end\r
+               end\r
+       elseif stanza.attr.type == "unsubscribe" then\r
+               if rostermanager.process_inbound_unsubscribe(node, host, from_bare) then\r
+                       rostermanager.roster_push(node, host, from_bare);\r
+               end\r
+       elseif stanza.attr.type == "subscribed" then\r
+               if rostermanager.process_inbound_subscription_approval(node, host, from_bare) then\r
+                       rostermanager.roster_push(node, host, from_bare);\r
+               end\r
+       elseif stanza.attr.type == "unsubscribed" then\r
+               if rostermanager.process_inbound_subscription_cancellation(node, host, from_bare) then\r
+                       rostermanager.roster_push(node, host, from_bare);\r
+               end\r
+       end -- discard any other type\r
+       stanza.attr.from, stanza.attr.to = st_from, st_to;\r
+end\r
+\r
+local function presence_handler(data)\r
+       local origin, stanza = data.origin, data.stanza;\r
+       local to = stanza.attr.to;\r
+       local node, host = jid_split(to);\r
+       local to_bare = jid_bare(to);\r
+       local from_bare = jid_bare(stanza.attr.from);\r
+       if origin.type == "c2s" then\r
+               if to ~= nil and not(origin.roster[to_bare] and (origin.roster[to_bare].subscription == "both" or origin.roster[to_bare].subscription == "from")) then -- directed presence\r
+                       origin.directed = origin.directed or {};\r
+                       origin.directed[to] = true;\r
+               end\r
+               if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then\r
+                       handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);\r
+               elseif not to then\r
+                       handle_normal_presence(origin, stanza, core_route_stanza);\r
+               else\r
+                       core_route_stanza(origin, stanza);\r
+               end\r
+       elseif (origin.type == "s2sin" or origin.type == "component") and hosts[host] then\r
+               if stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" and stanza.attr.type ~= "error" then\r
+                       handle_inbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare, core_route_stanza);\r
+               else\r
+                       core_route_stanza(origin, stanza);\r
+               end\r
+       end\r
+end\r
+\r
+local add_handler = require "core.eventmanager2".add_handler;\r
+local remove_handler = require "core.eventmanager2".remove_handler;\r
+\r
+add_handler(module:get_host().."/presence", presence_handler);\r
+module.unload = function()\r
+       remove_handler(module:get_host().."/presence", presence_handler);\r
+end\r