2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local t_insert
, t_remove
= table.insert
, table.remove;
14 local new_filter_hooks
= {};
16 local function initialize(session
)
17 if not session
.filters
then
19 session
.filters
= filters
;
21 function session
.filter(type, data
)
22 local filter_list
= filters
[type];
24 for i
= 1, #filter_list
do
25 data
= filter_list
[i
](data
, session
);
26 if data
== nil then break; end
33 for i
=1,#new_filter_hooks
do
34 new_filter_hooks
[i
](session
);
37 return session
.filter
;
40 local function add_filter(session
, type, callback
, priority
)
41 if not session
.filters
then
45 local filter_list
= session
.filters
[type];
46 if not filter_list
then
48 session
.filters
[type] = filter_list
;
49 elseif filter_list
[callback
] then
50 return; -- Filter already added
53 priority
= priority
or 0;
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
;
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;
78 local function add_filter_hook(callback
)
79 t_insert(new_filter_hooks
, callback
);
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
);
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
;