Merge 0.10->trunk
[prosody.git] / core / moduleapi.lua
index 9a0c5223b466af0f45fd30f535aa6c5aed66048c..9b773f89c5314ba7e9a0771722869cf69d79d8c3 100644 (file)
@@ -20,9 +20,10 @@ local st = require "util.stanza";
 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
 local error, setmetatable, type = error, setmetatable, type;
 local ipairs, pairs, select = ipairs, pairs, select;
-local unpack = table.unpack or unpack;
 local tonumber, tostring = tonumber, tostring;
 local require = require;
+local pack = table.pack or function(...) return {n=select("#",...), ...}; end -- table.pack is only in 5.2
+local unpack = table.unpack or unpack; --luacheck: ignore 113 -- renamed in 5.2
 
 local prosody = prosody;
 local hosts = prosody.hosts;
@@ -374,8 +375,8 @@ function api:provides(name, item)
        self:add_item(name.."-provider", item);
 end
 
-function api:send(stanza)
-       return core_post_stanza(hosts[self.host], stanza);
+function api:send(stanza, origin)
+       return core_post_stanza(origin or hosts[self.host], stanza);
 end
 
 function api:broadcast(jids, stanza, iter)
@@ -386,11 +387,29 @@ function api:broadcast(jids, stanza, iter)
        end
 end
 
-function api:add_timer(delay, callback)
-       return timer.add_task(delay, function (t)
-               if self.loaded == false then return; end
-               return callback(t);
-       end);
+local timer_methods = { }
+local timer_mt = {
+       __index = timer_methods;
+}
+function timer_methods:stop( )
+       timer.stop(self.id);
+end
+timer_methods.disarm = timer_methods.stop
+function timer_methods:reschedule(delay)
+       timer.reschedule(self.id, delay)
+end
+
+local function timer_callback(now, id, t) --luacheck: ignore 212/id
+       if t.module_env.loaded == false then return; end
+       return t.callback(now, unpack(t, 1, t.n));
+end
+
+function api:add_timer(delay, callback, ...)
+       local t = pack(...)
+       t.module_env = self;
+       t.callback = callback;
+       t.id = timer.add_task(delay, timer_callback, t);
+       return setmetatable(t, timer_mt);
 end
 
 local path_sep = package.config:sub(1,1);