aboutsummaryrefslogtreecommitdiffstats
path: root/util/filters.lua
blob: f405c0bdd91f70a47a51fa3e2a11d0cba8f93b97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
-- Prosody IM
-- Copyright (C) 2008-2010 Matthew Wild
-- Copyright (C) 2008-2010 Waqas Hussain
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--

local t_insert, t_remove = table.insert, table.remove;

local _ENV = nil;

local new_filter_hooks = {};

local function initialize(session)
	if not session.filters then
		local filters = {};
		session.filters = filters;

		function session.filter(type, data)
			local filter_list = filters[type];
			if filter_list then
				for i = 1, #filter_list do
					data = filter_list[i](data, session);
					if data == nil then break; end
				end
			end
			return data;
		end
	end

	for i=1,#new_filter_hooks do
		new_filter_hooks[i](session);
	end

	return session.filter;
end

local function add_filter(session, type, callback, priority)
	if not session.filters then
		initialize(session);
	end

	local filter_list = session.filters[type];
	if not filter_list then
		filter_list = {};
		session.filters[type] = filter_list;
	elseif filter_list[callback] then
		return; -- Filter already added
	end

	priority = priority or 0;

	local i = 0;
	repeat
		i = i + 1;
	until not filter_list[i] or filter_list[filter_list[i]] < priority;

	t_insert(filter_list, i, callback);
	filter_list[callback] = priority;
end

local function remove_filter(session, type, callback)
	if not session.filters then return; end
	local filter_list = session.filters[type];
	if filter_list and filter_list[callback] then
		for i=1, #filter_list do
			if filter_list[i] == callback then
				t_remove(filter_list, i);
				filter_list[callback] = nil;
				return true;
			end
		end
	end
end

local function add_filter_hook(callback)
	t_insert(new_filter_hooks, callback);
end

local function remove_filter_hook(callback)
	for i=1,#new_filter_hooks do
		if new_filter_hooks[i] == callback then
			t_remove(new_filter_hooks, i);
		end
	end
end

return {
	initialize = initialize;
	add_filter = add_filter;
	remove_filter = remove_filter;
	add_filter_hook = add_filter_hook;
	remove_filter_hook = remove_filter_hook;
};