From 0fe02618e58fb93ebdf11ff38efcf71015a0ca81 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Fri, 20 Feb 2009 02:18:07 +0500 Subject: util/stanza: Added clone function --- util/stanza.lua | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'util') 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); -- cgit v1.2.3 From 97ea70c7d325fcdd6342a84464e9b7b26d9da177 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sat, 21 Feb 2009 01:13:53 +0500 Subject: Added util/timer.lua - a timer API --- util/timer.lua | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 util/timer.lua (limited to 'util') diff --git a/util/timer.lua b/util/timer.lua new file mode 100644 index 00000000..889a8c4c --- /dev/null +++ b/util/timer.lua @@ -0,0 +1,47 @@ +-- 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 ipairs = ipairs; +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(); + for _, d in ipairs(new_data) do + t_insert(data, d); + end + new_data = {}; + for i = #data,1 do + local t, func = data[i][1], data[i][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; -- cgit v1.2.3 From 073db87d448354024c663a695be2e4c62e8f506d Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 26 Feb 2009 02:42:47 +0000 Subject: Better handling of found, but unloadable, core libraries (eg. undefined symbols) --- util/dependencies.lua | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'util') 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 -- cgit v1.2.3 From b9636951c1d64ec398a18a40b55ff92d83f50009 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 26 Feb 2009 16:55:46 +0000 Subject: util.multitable: Add mt:search(), use nil for wildcard keys --- util/multitable.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'util') 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 -- cgit v1.2.3 From d2631d39ca278c6823ffc6e19a670954507c981b Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 26 Feb 2009 21:00:42 +0000 Subject: util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed --- util/timer.lua | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'util') diff --git a/util/timer.lua b/util/timer.lua index 889a8c4c..8bb1c115 100644 --- a/util/timer.lua +++ b/util/timer.lua @@ -10,7 +10,8 @@ local ns_addtimer = require "net.server".addtimer; local get_time = os.time; local t_insert = table.insert; -local ipairs = ipairs; +local t_remove = table.remove; +local ipairs, pairs = ipairs, pairs; local type = type; local data = {}; @@ -30,14 +31,19 @@ add_task = _add_task; ns_addtimer(function() local current_time = get_time(); - for _, d in ipairs(new_data) do - t_insert(data, d); + if #new_data > 0 then + for _, d in ipairs(new_data) do + t_insert(data, d); + end + new_data = {}; + elseif #data == 0 then + return; end - new_data = {}; - for i = #data,1 do - local t, func = data[i][1], data[i][2]; + + for i, d in pairs(data) do + local t, func = d[1], d[2]; if t <= current_time then - data[i] = nil; + t_remove(data, i); local r = func(); if type(r) == "number" then _add_task(r, func); end end -- cgit v1.2.3 From 0c8bb8e89f63fe33e7a320e5d8bb308c1ba80192 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 26 Feb 2009 22:17:55 +0000 Subject: util.timer: More small fixes I forgot to commit --- util/timer.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'util') diff --git a/util/timer.lua b/util/timer.lua index 8bb1c115..3db66832 100644 --- a/util/timer.lua +++ b/util/timer.lua @@ -32,18 +32,16 @@ add_task = _add_task; ns_addtimer(function() local current_time = get_time(); if #new_data > 0 then - for _, d in ipairs(new_data) do + for _, d in pairs(new_data) do t_insert(data, d); end new_data = {}; - elseif #data == 0 then - return; end for i, d in pairs(data) do local t, func = d[1], d[2]; if t <= current_time then - t_remove(data, i); + data[i] = nil; local r = func(); if type(r) == "number" then _add_task(r, func); end end -- cgit v1.2.3 From 2a5e41bed0c75505a5367e87c7c6e48220443434 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 27 Feb 2009 17:56:07 +0000 Subject: util.dataforms: First commit, incomplete --- util/dataforms.lua | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 util/dataforms.lua (limited to 'util') diff --git a/util/dataforms.lua b/util/dataforms.lua new file mode 100644 index 00000000..ba751f6f --- /dev/null +++ b/util/dataforms.lua @@ -0,0 +1,69 @@ + +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 +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 }; +} + + +--]] -- cgit v1.2.3 From 1c998c518c525feb7555f6c493f230f8b5a3ebff Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 28 Feb 2009 23:16:27 +0000 Subject: Adding initial util.pubsub --- util/pubsub.lua | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 util/pubsub.lua (limited to 'util') 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; -- cgit v1.2.3 From fbb86043bd4380e25d81a3e15774a1f8d50a94e6 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sun, 1 Mar 2009 23:34:25 +0000 Subject: util.dataforms: Return the form --- util/dataforms.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'util') diff --git a/util/dataforms.lua b/util/dataforms.lua index ba751f6f..c10a0244 100644 --- a/util/dataforms.lua +++ b/util/dataforms.lua @@ -45,6 +45,7 @@ function form_t.form(layout, data) -- Jump back up to list of fields form:up(); end + return form; end function form_t.data(layout, stanza) -- cgit v1.2.3