aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2010-06-02 12:20:29 +0100
committerMatthew Wild <mwild1@gmail.com>2010-06-02 12:20:29 +0100
commit8f8e28d750549b090b9c84c49d44d0a9ec398070 (patch)
tree7967986afa65c6c1fffe548a74787f1d06410db7
parentb72412126e1df14c79a6aff417c6acc8751f489c (diff)
downloadprosody-8f8e28d750549b090b9c84c49d44d0a9ec398070.tar.gz
prosody-8f8e28d750549b090b9c84c49d44d0a9ec398070.zip
util.filters: New utility library for managing filters on an object
-rw-r--r--util/filters.lua61
1 files changed, 61 insertions, 0 deletions
diff --git a/util/filters.lua b/util/filters.lua
new file mode 100644
index 00000000..09a81cdb
--- /dev/null
+++ b/util/filters.lua
@@ -0,0 +1,61 @@
+-- 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;
+
+module "filters"
+
+local function initialize_session(session)
+ local filters = {};
+ session.filters = filters;
+
+ function session.filter(type, data)
+ local filter_list = filters[type];
+ for i = 1, #filter_list do
+ data = filter_list[i](data);
+ end
+ return data;
+ end
+end
+
+function add_filter(session, type, callback, priority)
+ if not session.filters then
+ initialize_session(session);
+ end
+
+ local filter_list = session.filters[type];
+ if not filter_list then
+ filter_list = {};
+ session.filters[type] = filter_list;
+ 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
+
+function remove_filter(session, type, callback)
+ 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
+
+return _M; \ No newline at end of file