util.x509: Only collect commonNames that pass idna
[prosody.git] / util / filters.lua
blobf30dfd9cd717d16a8ff93f0a16b2e1ab4e3fb07f
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 t_insert, t_remove = table.insert, table.remove;
11 local _ENV = nil;
12 -- luacheck: std none
14 local new_filter_hooks = {};
16 local function initialize(session)
17 if not session.filters then
18 local filters = {};
19 session.filters = filters;
21 function session.filter(type, data)
22 local filter_list = filters[type];
23 if filter_list then
24 for i = 1, #filter_list do
25 data = filter_list[i](data, session);
26 if data == nil then break; end
27 end
28 end
29 return data;
30 end
31 end
33 for i=1,#new_filter_hooks do
34 new_filter_hooks[i](session);
35 end
37 return session.filter;
38 end
40 local function add_filter(session, type, callback, priority)
41 if not session.filters then
42 initialize(session);
43 end
45 local filter_list = session.filters[type];
46 if not filter_list then
47 filter_list = {};
48 session.filters[type] = filter_list;
49 elseif filter_list[callback] then
50 return; -- Filter already added
51 end
53 priority = priority or 0;
55 local i = 0;
56 repeat
57 i = i + 1;
58 until not filter_list[i] or filter_list[filter_list[i]] < priority;
60 t_insert(filter_list, i, callback);
61 filter_list[callback] = priority;
62 end
64 local function remove_filter(session, type, callback)
65 if not session.filters then return; end
66 local filter_list = session.filters[type];
67 if filter_list and filter_list[callback] then
68 for i=1, #filter_list do
69 if filter_list[i] == callback then
70 t_remove(filter_list, i);
71 filter_list[callback] = nil;
72 return true;
73 end
74 end
75 end
76 end
78 local function add_filter_hook(callback)
79 t_insert(new_filter_hooks, callback);
80 end
82 local function remove_filter_hook(callback)
83 for i=1,#new_filter_hooks do
84 if new_filter_hooks[i] == callback then
85 t_remove(new_filter_hooks, i);
86 end
87 end
88 end
90 return {
91 initialize = initialize;
92 add_filter = add_filter;
93 remove_filter = remove_filter;
94 add_filter_hook = add_filter_hook;
95 remove_filter_hook = remove_filter_hook;