util.x509: Return sets of services per identity
[prosody.git] / util / helpers.lua
blob02257ffadadb31a2e5c33da6b446f97ec0506312
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
9 local debug = require "util.debug";
11 -- Helper functions for debugging
13 local log = require "util.logger".init("util.debug");
15 local function log_events(events, name, logger)
16 local f = events.fire_event;
17 if not f then
18 error("Object does not appear to be a util.events object");
19 end
20 logger = logger or log;
21 name = name or tostring(events);
22 function events.fire_event(event, ...)
23 logger("debug", "%s firing event: %s", name, event);
24 return f(event, ...);
25 end
26 events[events.fire_event] = f;
27 return events;
28 end
30 local function revert_log_events(events)
31 events.fire_event, events[events.fire_event] = events[events.fire_event], nil; -- :))
32 end
34 local function log_host_events(host)
35 return log_events(prosody.hosts[host].events, host);
36 end
38 local function revert_log_host_events(host)
39 return revert_log_events(prosody.hosts[host].events);
40 end
42 local function show_events(events, specific_event)
43 local event_handlers = events._handlers;
44 local events_array = {};
45 local event_handler_arrays = {};
46 for event, priorities in pairs(events._event_map) do
47 local handlers = event_handlers[event];
48 if handlers and (event == specific_event or not specific_event) then
49 table.insert(events_array, event);
50 local handler_strings = {};
51 for i, handler in ipairs(handlers) do
52 local upvals = debug.string_from_var_table(debug.get_upvalues_table(handler));
53 handler_strings[i] = " "..(priorities[handler] or "?")..": "..tostring(handler)..(upvals and ("\n "..upvals) or "");
54 end
55 event_handler_arrays[event] = handler_strings;
56 end
57 end
58 table.sort(events_array);
59 local i = 1;
60 while i <= #events_array do
61 local handlers = event_handler_arrays[events_array[i]];
62 for j=#handlers, 1, -1 do
63 table.insert(events_array, i+1, handlers[j]);
64 end
65 if i > 1 then events_array[i] = "\n"..events_array[i]; end
66 i = i + #handlers + 1
67 end
68 return table.concat(events_array, "\n");
69 end
71 local function get_upvalue(f, get_name)
72 local i, name, value = 0;
73 repeat
74 i = i + 1;
75 name, value = debug.getupvalue(f, i);
76 until name == get_name or name == nil;
77 return value;
78 end
80 return {
81 log_host_events = log_host_events;
82 revert_log_host_events = revert_log_host_events;
83 log_events = log_events;
84 revert_log_events = revert_log_events;
85 show_events = show_events;
86 get_upvalue = get_upvalue;