diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/dataforms.lua | 70 | ||||
-rw-r--r-- | util/dependencies.lua | 30 | ||||
-rw-r--r-- | util/multitable.lua | 49 | ||||
-rw-r--r-- | util/pubsub.lua | 60 | ||||
-rw-r--r-- | util/stanza.lua | 21 | ||||
-rw-r--r-- | util/timer.lua | 51 |
6 files changed, 274 insertions, 7 deletions
diff --git a/util/dataforms.lua b/util/dataforms.lua new file mode 100644 index 00000000..c10a0244 --- /dev/null +++ b/util/dataforms.lua @@ -0,0 +1,70 @@ + +module "dataforms" + +local xmlns_forms = 'jabber:x:data'; + +local form_t = {}; +local form_mt = { __index = form_t }; + +function new(layout) + return setmetatable(layout, form_mt); +end + +local form_x_attr = { xmlns = xmlns_forms }; + +function form_t.form(layout, data) + local form = st.tag("x", form_x_attr); + for n, field in ipairs(layout) do + local field_type = field.type; + -- Add field tag + form:tag("field", { type = field_type, var = field.name }); + + local value = data[field.name]; + + -- Add value, depending on type + if field_type == "hidden" then + if type(value) == "table" then + -- Assume an XML snippet + form:add_child(value); + elseif value then + form:text(tostring(value)); + end + elseif field_type == "boolean" then + form:text((value and "1") or "0"); + elseif field_type == "fixed" then + + elseif field_type == "jid-multi" then + for _, jid in ipairs(value) do + form:tag("value"):text(jid):up(); + end + elseif field_type == "jid-single" then + form:tag("value"):text(value):up(); + + end + + -- Jump back up to list of fields + form:up(); + end + return form; +end + +function form_t.data(layout, stanza) + +end + + + +--[[ + +Layout: +{ + + title = "MUC Configuration", + instructions = [[Use this form to configure options for this MUC room.]], + + { name = "FORM_TYPE", type = "hidden", required = true }; + { name = "field-name", type = "field-type", required = false }; +} + + +--]] diff --git a/util/dependencies.lua b/util/dependencies.lua index 09b7b018..6ba5f670 100644 --- a/util/dependencies.lua +++ b/util/dependencies.lua @@ -9,7 +9,7 @@ local fatal; -local function softreq(...) local ok, lib = pcall(require, ...); if ok then return lib; else return nil; end end +local function softreq(...) local ok, lib = pcall(require, ...); if ok then return lib; else return nil, lib; end end local function missingdep(name, sources, msg) print(""); @@ -51,19 +51,37 @@ if not ssl then end end -local encodings = softreq "util.encodings" +local encodings, err = softreq "util.encodings" if not encodings then - missingdep("util.encodings", { ["Windows"] = "Make sure you have encodings.dll from the Prosody distribution in util/"; + if err:match("not found") then + missingdep("util.encodings", { ["Windows"] = "Make sure you have encodings.dll from the Prosody distribution in util/"; ["GNU/Linux"] = "Run './configure' and 'make' in the Prosody source directory to build util/encodings.so"; }); + else + print "***********************************" + print("util/encodings couldn't be loaded. Check that you have a recent version of libidn"); + print "" + print("The full error was:"); + print(err) + print "***********************************" + end fatal = true; end -local encodings = softreq "util.hashes" -if not encodings then - missingdep("util.hashes", { ["Windows"] = "Make sure you have hashes.dll from the Prosody distribution in util/"; +local hashes, err = softreq "util.hashes" +if not hashes then + if err:match("not found") then + missingdep("util.hashes", { ["Windows"] = "Make sure you have hashes.dll from the Prosody distribution in util/"; ["GNU/Linux"] = "Run './configure' and 'make' in the Prosody source directory to build util/hashes.so"; }); + else + print "***********************************" + print("util/hashes couldn't be loaded. Check that you have a recent version of OpenSSL (libcrypto in particular)"); + print "" + print("The full error was:"); + print(err) + print "***********************************" + end fatal = true; end diff --git a/util/multitable.lua b/util/multitable.lua index e5e87521..397f9ebe 100644 --- a/util/multitable.lua +++ b/util/multitable.lua @@ -82,6 +82,53 @@ local function remove(self, ...) end +local function s(t, n, results, _end, ...) + if t == nil then return; end + local k = select(n, ...); + if n == _end then + if k == nil then + for _, v in pairs(t) do + t_insert(results, v); + end + else + t_insert(results, t[k]); + end + return; + end + if k then + v = t[k]; + if v then + s(v, n+1, results, _end, ...); + end + else + for _,b in pairs(t) do + s(b, n+1, results, _end, ...); + end + end +end + +-- Search for keys, nil == wildcard +local function search(self, ...) + local _end = select('#', ...); + for n = _end,1 do + if select(n, ...) then _end = n; break; end + end + local results = {}; + s(self.data, 1, results, _end, ...); + return results; +end + +-- Append results to an existing list +local function search_add(self, results, ...) + if not results then results = {}; end + local _end = select('#', ...); + for n = _end,1 do + if select(n, ...) then _end = n; break; end + end + s(self.data, 1, results, _end, ...); + return results; +end + function new() return { data = {}; @@ -89,6 +136,8 @@ function new() add = add; set = set; remove = remove; + search = search; + search_add = search_add; }; end diff --git a/util/pubsub.lua b/util/pubsub.lua new file mode 100644 index 00000000..c1e35e3c --- /dev/null +++ b/util/pubsub.lua @@ -0,0 +1,60 @@ + +local ipairs, pairs, setmetatable, type = + ipairs, pairs, setmetatable, type; + +module "pubsub" + +local pubsub_node_mt = { __index = _M }; + +function new_node(name) + return setmetatable({ name = name, subscribers = {} }, pubsub_node_mt); +end + +function set_subscribers(node, subscribers_list, list_type) + local subscribers = node.subscribers; + + if list_type == "array" then + for _, jid in ipairs(subscribers_list) do + if not subscribers[jid] then + node:add_subscriber(jid); + end + end + elseif (not list_type) or list_type == "set" then + for jid in pairs(subscribers_list) do + if type(jid) == "string" then + node:add_subscriber(jid); + end + end + end +end + +function get_subscribers(node) + return node.subscribers; +end + +function publish(node, item, dispatcher, data) + local subscribers = node.subscribers; + for i = 1,#subscribers do + item.attr.to = subscribers[i]; + dispatcher(data, item); + end +end + +function add_subscriber(node, jid) + local subscribers = node.subscribers; + if not subscribers[jid] then + local space = #subscribers; + subscribers[space] = jid; + subscribers[jid] = space; + end +end + +function remove_subscriber(node, subscriber) + local subscribers = node.subscribers; + if subscribers[jid] then + subscribers[subscribers[jid]] = nil; + subscribers[jid] = nil; + end +end + +return _M; diff --git a/util/stanza.lua b/util/stanza.lua index 1c1cab0e..5bc15609 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -12,9 +12,10 @@ local t_concat = table.concat; local t_remove = table.remove; local t_concat = table.concat; local s_format = string.format; -local s_match = string.match; +local s_match = string.match; local tostring = tostring; local setmetatable = setmetatable; +local getmetatable = getmetatable; local pairs = pairs; local ipairs = ipairs; local type = type; @@ -215,6 +216,24 @@ function deserialize(stanza) return stanza; end +function clone(stanza) + local lookup_table = {}; + local function _copy(object) + if type(object) ~= "table" then + return object; + elseif lookup_table[object] then + return lookup_table[object]; + end + local new_table = {}; + lookup_table[object] = new_table; + for index, value in pairs(object) do + new_table[_copy(index)] = _copy(value); + end + return setmetatable(new_table, getmetatable(object)); + end + return _copy(stanza) +end + function message(attr, body) if not body then return stanza("message", attr); diff --git a/util/timer.lua b/util/timer.lua new file mode 100644 index 00000000..3db66832 --- /dev/null +++ b/util/timer.lua @@ -0,0 +1,51 @@ +-- Prosody IM v0.3 +-- Copyright (C) 2008-2009 Matthew Wild +-- Copyright (C) 2008-2009 Waqas Hussain +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- + + +local ns_addtimer = require "net.server".addtimer; +local get_time = os.time; +local t_insert = table.insert; +local t_remove = table.remove; +local ipairs, pairs = ipairs, pairs; +local type = type; + +local data = {}; +local new_data = {}; + +module "timer" + +local function _add_task(delay, func) + local current_time = get_time(); + delay = delay + current_time; + if delay >= current_time then + t_insert(new_data, {delay, func}); + else func(); end +end + +add_task = _add_task; + +ns_addtimer(function() + local current_time = get_time(); + if #new_data > 0 then + for _, d in pairs(new_data) do + t_insert(data, d); + end + new_data = {}; + end + + for i, d in pairs(data) do + local t, func = d[1], d[2]; + if t <= current_time then + data[i] = nil; + local r = func(); + if type(r) == "number" then _add_task(r, func); end + end + end +end); + +return _M; |