Software /
code /
prosody
Comparison
core/moduleapi.lua @ 6666:42fdc8dddf04
Merge 0.10->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 06 May 2015 19:26:53 +0100 |
parent | 6647:0c363fddcdd9 |
parent | 6659:0a494394cd3e |
child | 6791:e813e8cf6046 |
comparison
equal
deleted
inserted
replaced
6650:cf15b18e9810 | 6666:42fdc8dddf04 |
---|---|
8 | 8 |
9 local config = require "core.configmanager"; | 9 local config = require "core.configmanager"; |
10 local modulemanager; -- This gets set from modulemanager | 10 local modulemanager; -- This gets set from modulemanager |
11 local array = require "util.array"; | 11 local array = require "util.array"; |
12 local set = require "util.set"; | 12 local set = require "util.set"; |
13 local it = require "util.iterators"; | |
13 local logger = require "util.logger"; | 14 local logger = require "util.logger"; |
14 local pluginloader = require "util.pluginloader"; | 15 local pluginloader = require "util.pluginloader"; |
15 local timer = require "util.timer"; | 16 local timer = require "util.timer"; |
16 local resolve_relative_path = require"util.paths".resolve_relative_path; | 17 local resolve_relative_path = require"util.paths".resolve_relative_path; |
17 local measure = require "core.statsmanager".measure; | 18 local measure = require "core.statsmanager".measure; |
19 local st = require "util.stanza"; | |
18 | 20 |
19 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; | 21 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; |
20 local error, setmetatable, type = error, setmetatable, type; | 22 local error, setmetatable, type = error, setmetatable, type; |
21 local ipairs, pairs, select = ipairs, pairs, select; | 23 local ipairs, pairs, select = ipairs, pairs, select; |
22 local tonumber, tostring = tonumber, tostring; | 24 local tonumber, tostring = tonumber, tostring; |
54 | 56 |
55 function api:set_global() | 57 function api:set_global() |
56 self.host = "*"; | 58 self.host = "*"; |
57 -- Update the logger | 59 -- Update the logger |
58 local _log = logger.init("mod_"..self.name); | 60 local _log = logger.init("mod_"..self.name); |
59 self.log = function (self, ...) return _log(...); end; | 61 self.log = function (self, ...) return _log(...); end; --luacheck: ignore self |
60 self._log = _log; | 62 self._log = _log; |
61 self.global = true; | 63 self.global = true; |
62 end | 64 end |
63 | 65 |
64 function api:add_feature(xmlns) | 66 function api:add_feature(xmlns) |
65 self:add_item("feature", xmlns); | 67 self:add_item("feature", xmlns); |
66 end | 68 end |
67 function api:add_identity(category, type, name) | 69 function api:add_identity(category, identity_type, name) |
68 self:add_item("identity", {category = category, type = type, name = name}); | 70 self:add_item("identity", {category = category, type = identity_type, name = name}); |
69 end | 71 end |
70 function api:add_extension(data) | 72 function api:add_extension(data) |
71 self:add_item("extension", data); | 73 self:add_item("extension", data); |
72 end | 74 end |
73 function api:has_feature(xmlns) | 75 function api:has_feature(xmlns) |
74 for _, feature in ipairs(self:get_host_items("feature")) do | 76 for _, feature in ipairs(self:get_host_items("feature")) do |
75 if feature == xmlns then return true; end | 77 if feature == xmlns then return true; end |
76 end | 78 end |
77 return false; | 79 return false; |
78 end | 80 end |
79 function api:has_identity(category, type, name) | 81 function api:has_identity(category, identity_type, name) |
80 for _, id in ipairs(self:get_host_items("identity")) do | 82 for _, id in ipairs(self:get_host_items("identity")) do |
81 if id.category == category and id.type == type and id.name == name then | 83 if id.category == category and id.type == identity_type and id.name == name then |
82 return true; | 84 return true; |
83 end | 85 end |
84 end | 86 end |
85 return false; | 87 return false; |
86 end | 88 end |
93 self.event_handlers:set(object, event, handler, true); | 95 self.event_handlers:set(object, event, handler, true); |
94 return object.add_handler(event, handler, priority); | 96 return object.add_handler(event, handler, priority); |
95 end | 97 end |
96 | 98 |
97 function api:unhook_object_event(object, event, handler) | 99 function api:unhook_object_event(object, event, handler) |
100 self.event_handlers:set(object, event, handler, nil); | |
98 return object.remove_handler(event, handler); | 101 return object.remove_handler(event, handler); |
99 end | 102 end |
100 | 103 |
101 function api:hook(event, handler, priority) | 104 function api:hook(event, handler, priority) |
102 return self:hook_object_event((hosts[self.host] or prosody).events, event, handler, priority); | 105 return self:hook_object_event((hosts[self.host] or prosody).events, event, handler, priority); |
129 function api:wrap_event(event, handler) | 132 function api:wrap_event(event, handler) |
130 return self:wrap_object_event((hosts[self.host] or prosody).events, event, handler); | 133 return self:wrap_object_event((hosts[self.host] or prosody).events, event, handler); |
131 end | 134 end |
132 | 135 |
133 function api:wrap_global(event, handler) | 136 function api:wrap_global(event, handler) |
134 return self:hook_object_event(prosody.events, event, handler, priority); | 137 return self:hook_object_event(prosody.events, event, handler); |
135 end | 138 end |
136 | 139 |
137 function api:require(lib) | 140 function api:require(lib) |
138 local f, n = pluginloader.load_code(self.name, lib..".lib.lua", self.environment); | 141 local f, n = pluginloader.load_code(self.name, lib..".lib.lua", self.environment); |
139 if not f then | 142 if not f then |
327 function api:get_host_items(key) | 330 function api:get_host_items(key) |
328 local result = modulemanager.get_items(key, self.host) or {}; | 331 local result = modulemanager.get_items(key, self.host) or {}; |
329 return result; | 332 return result; |
330 end | 333 end |
331 | 334 |
332 function api:handle_items(type, added_cb, removed_cb, existing) | 335 function api:handle_items(item_type, added_cb, removed_cb, existing) |
333 self:hook("item-added/"..type, added_cb); | 336 self:hook("item-added/"..item_type, added_cb); |
334 self:hook("item-removed/"..type, removed_cb); | 337 self:hook("item-removed/"..item_type, removed_cb); |
335 if existing ~= false then | 338 if existing ~= false then |
336 for _, item in ipairs(self:get_host_items(type)) do | 339 for _, item in ipairs(self:get_host_items(item_type)) do |
337 added_cb({ item = item }); | 340 added_cb({ item = item }); |
338 end | 341 end |
339 end | 342 end |
340 end | 343 end |
341 | 344 |
362 | 365 |
363 function api:send(stanza) | 366 function api:send(stanza) |
364 return core_post_stanza(hosts[self.host], stanza); | 367 return core_post_stanza(hosts[self.host], stanza); |
365 end | 368 end |
366 | 369 |
370 function api:broadcast(jids, stanza, iter) | |
371 for jid in (iter or it.values)(jids) do | |
372 local new_stanza = st.clone(stanza); | |
373 new_stanza.attr.to = jid; | |
374 core_post_stanza(hosts[self.host], new_stanza); | |
375 end | |
376 end | |
377 | |
367 local timer_methods = { } | 378 local timer_methods = { } |
368 local timer_mt = { | 379 local timer_mt = { |
369 __index = timer_methods; | 380 __index = timer_methods; |
370 } | 381 } |
371 function timer_methods:stop( ) | 382 function timer_methods:stop( ) |
397 function api:load_resource(path, mode) | 408 function api:load_resource(path, mode) |
398 path = resolve_relative_path(self:get_directory(), path); | 409 path = resolve_relative_path(self:get_directory(), path); |
399 return io.open(path, mode); | 410 return io.open(path, mode); |
400 end | 411 end |
401 | 412 |
402 function api:open_store(name, type) | 413 function api:open_store(name, store_type) |
403 return require"core.storagemanager".open(self.host, name or self.name, type); | 414 return require"core.storagemanager".open(self.host, name or self.name, store_type); |
404 end | 415 end |
405 | 416 |
406 function api:measure(name, type) | 417 function api:measure(name, stat_type) |
407 return measure(type, "/"..self.host.."/mod_"..self.name.."/"..name); | 418 return measure(stat_type, "/"..self.host.."/mod_"..self.name.."/"..name); |
408 end | 419 end |
409 | 420 |
410 function api:measure_object_event(events_object, event_name, stat_name) | 421 function api:measure_object_event(events_object, event_name, stat_name) |
411 local m = self:measure(stat_name or event_name, "duration"); | 422 local m = self:measure(stat_name or event_name, "duration"); |
412 local function handler(handlers, event_name, event_data) | 423 local function handler(handlers, _event_name, _event_data) |
413 local finished = m(); | 424 local finished = m(); |
414 local ret = handlers(event_name, event_data); | 425 local ret = handlers(_event_name, _event_data); |
415 finished(); | 426 finished(); |
416 return ret; | 427 return ret; |
417 end | 428 end |
418 return self:hook_object_event(events_object, event_name, handler); | 429 return self:hook_object_event(events_object, event_name, handler); |
419 end | 430 end |
420 | 431 |
421 function api:measure_event(event_name, stat_name) | 432 function api:measure_event(event_name, stat_name) |
422 return self:hook_object_event((hosts[self.host] or prosody).events.wrappers, event_name, handler); | 433 return self:measure_object_event((hosts[self.host] or prosody).events.wrappers, event_name, stat_name); |
423 end | 434 end |
424 | 435 |
425 function api:measure_global_event(event_name, stat_name) | 436 function api:measure_global_event(event_name, stat_name) |
426 return self:hook_object_event(prosody.events.wrappers, event_name, handler); | 437 return self:measure_object_event(prosody.events.wrappers, event_name, stat_name); |
427 end | 438 end |
428 | 439 |
429 function api.init(mm) | 440 function api.init(mm) |
430 modulemanager = mm; | 441 modulemanager = mm; |
431 return api; | 442 return api; |