aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2008-11-15 13:47:17 +0100
committerTobias Markmann <tm@ayena.de>2008-11-15 13:47:17 +0100
commitefb34b5c4af68c37a568e61986a0b93535a50814 (patch)
treeba77cb7ad14158323a31bfb9e332c508e94d5b31 /util
parent18e785078a2edf69bf4ec728290a18de2ed28cd7 (diff)
parentaefcb845c34c7bf15a370812b28b5da27fbc983b (diff)
downloadprosody-efb34b5c4af68c37a568e61986a0b93535a50814.tar.gz
prosody-efb34b5c4af68c37a568e61986a0b93535a50814.zip
Merging my new SASL code with Waqas' adjusted saslauth module.
Diffstat (limited to 'util')
-rw-r--r--util/datamanager.lua71
-rw-r--r--util/datetime.lua28
-rw-r--r--util/logger.lua17
-rw-r--r--util/stanza.lua53
-rw-r--r--util/termcolours.lua33
5 files changed, 192 insertions, 10 deletions
diff --git a/util/datamanager.lua b/util/datamanager.lua
index aad370d1..80b35733 100644
--- a/util/datamanager.lua
+++ b/util/datamanager.lua
@@ -1,14 +1,15 @@
local format = string.format;
local setmetatable, type = setmetatable, type;
-local pairs = pairs;
+local pairs, ipairs = pairs, ipairs;
local char = string.char;
local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
local log = log;
local io_open = io.open;
local os_remove = os.remove;
-local tostring = tostring;
+local tostring, tonumber = tostring, tonumber;
local error = error;
local next = next;
+local t_insert = table.insert;
local indent = function(f, i)
for n = 1, i do
@@ -69,13 +70,14 @@ end
------- API -------------
-function getpath(username, host, datastore)
+function getpath(username, host, datastore, ext)
+ ext = ext or "dat";
if username then
- return format("data/%s/%s/%s.dat", encode(host), datastore, encode(username));
+ return format("data/%s/%s/%s.%s", encode(host), datastore, encode(username), ext);
elseif host then
- return format("data/%s/%s.dat", encode(host), datastore);
+ return format("data/%s/%s.%s", encode(host), datastore, ext);
else
- return format("data/%s.dat", datastore);
+ return format("data/%s.%s", datastore, ext);
end
end
@@ -115,4 +117,59 @@ function store(username, host, datastore, data)
return true;
end
-return _M; \ No newline at end of file
+function list_append(username, host, datastore, data)
+ if not data then return; end
+ -- save the datastore
+ local f, msg = io_open(getpath(username, host, datastore, "list"), "a+");
+ if not f then
+ log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
+ return;
+ end
+ f:write("item(");
+ simplesave(f, data, 1);
+ f:write(");\n");
+ f:close();
+ return true;
+end
+
+function list_store(username, host, datastore, data)
+ if not data then
+ data = {};
+ end
+ -- save the datastore
+ local f, msg = io_open(getpath(username, host, datastore, "list"), "w+");
+ if not f then
+ log("error", "Unable to write to "..datastore.." storage ('"..msg.."') for user: "..(username or "nil").."@"..(host or "nil"));
+ return;
+ end
+ for _, d in ipairs(data) do
+ f:write("item(");
+ simplesave(f, d, 1);
+ f:write(");\n");
+ end
+ f:close();
+ if not next(data) then -- try to delete empty datastore
+ os_remove(getpath(username, host, datastore, "list"));
+ end
+ -- we write data even when we are deleting because lua doesn't have a
+ -- platform independent way of checking for non-exisitng files
+ return true;
+end
+
+function list_load(username, host, datastore)
+ local data, ret = loadfile(getpath(username, host, datastore, "list"));
+ if not data then
+ log("warn", "Failed to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
+ return nil;
+ end
+ local items = {};
+ setfenv(data, {item = function(i) t_insert(items, i); end});
+ local success, ret = pcall(data);
+ if not success then
+ log("error", "Unable to load "..datastore.." storage ('"..ret.."') for user: "..(username or "nil").."@"..(host or "nil"));
+ return nil;
+ end
+ return items;
+end
+
+return _M;
diff --git a/util/datetime.lua b/util/datetime.lua
new file mode 100644
index 00000000..077cbb67
--- /dev/null
+++ b/util/datetime.lua
@@ -0,0 +1,28 @@
+-- XEP-0082: XMPP Date and Time Profiles
+
+local os_date = os.date;
+local error = error;
+
+module "datetime"
+
+function date()
+ return os_date("!%Y-%m-%d");
+end
+
+function datetime()
+ return os_date("!%Y-%m-%dT%H:%M:%SZ");
+end
+
+function time()
+ return os_date("!%H:%M:%S");
+end
+
+function legacy()
+ return os_date("!%Y%m%dT%H:%M:%S");
+end
+
+function parse(s)
+ error("datetime.parse: Not implemented"); -- TODO
+end
+
+return _M;
diff --git a/util/logger.lua b/util/logger.lua
index 8d983605..f93cafc1 100644
--- a/util/logger.lua
+++ b/util/logger.lua
@@ -3,8 +3,21 @@ local format = string.format;
local print = print;
local debug = debug;
local tostring = tostring;
+
+local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
+local do_pretty_printing = not os.getenv("WINDIR");
+
module "logger"
+local logstyles = {};
+
+--TODO: This should be done in config, but we don't have proper config yet
+if do_pretty_printing then
+ logstyles["info"] = getstyle("bold");
+ logstyles["warn"] = getstyle("bold", "yellow");
+ logstyles["error"] = getstyle("bold", "red");
+end
+
function init(name)
--name = nil; -- While this line is not commented, will automatically fill in file/line number info
return function (level, message, ...)
@@ -13,9 +26,9 @@ function init(name)
level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline;
end
if ... then
- print(level, format(message, ...));
+ print(name, getstring(logstyles[level], level), format(message, ...));
else
- print(level, message);
+ print(name, getstring(logstyles[level], level), message);
end
end
end
diff --git a/util/stanza.lua b/util/stanza.lua
index 52f372cc..5a6ba8c5 100644
--- a/util/stanza.lua
+++ b/util/stanza.lua
@@ -10,8 +10,11 @@ local next = next;
local print = print;
local unpack = unpack;
local s_gsub = string.gsub;
+local os = os;
+
+local do_pretty_printing = not os.getenv("WINDIR");
+local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
-local debug = debug;
local log = require "util.logger".init("stanza");
module "stanza"
@@ -105,6 +108,14 @@ function stanza_mt.__tostring(t)
return s_format("<%s%s>%s</%s>", t.name, attr_string, children_text, t.name);
end
+function stanza_mt.top_tag(t)
+ local attr_string = "";
+ if t.attr then
+ for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(" %s='%s'", k, tostring(v)); end end
+ end
+ return s_format("<%s%s>", t.name, attr_string);
+end
+
function stanza_mt.__add(s1, s2)
return s1:add_direct_child(s2);
end
@@ -184,4 +195,44 @@ function presence(attr)
return stanza("presence", attr);
end
+if do_pretty_printing then
+ local style_attrk = getstyle("yellow");
+ local style_attrv = getstyle("red");
+ local style_tagname = getstyle("red");
+ local style_punc = getstyle("magenta");
+
+ local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'");
+ local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">");
+ --local tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">").."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
+ local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
+ function stanza_mt.pretty_print(t)
+ local children_text = "";
+ for n, child in ipairs(t) do
+ if type(child) == "string" then
+ children_text = children_text .. xml_escape(child);
+ else
+ children_text = children_text .. child:pretty_print();
+ end
+ end
+
+ local attr_string = "";
+ if t.attr then
+ for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end
+ end
+ return s_format(tag_format, t.name, attr_string, children_text, t.name);
+ end
+
+ function stanza_mt.pretty_top_tag(t)
+ local attr_string = "";
+ if t.attr then
+ for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end
+ end
+ return s_format(top_tag_format, t.name, attr_string);
+ end
+else
+ -- Sorry, fresh out of colours for you guys ;)
+ stanza_mt.pretty_print = stanza_mt.__tostring;
+ stanza_mt.pretty_top_tag = stanza_mt.top_tag;
+end
+
return _M;
diff --git a/util/termcolours.lua b/util/termcolours.lua
new file mode 100644
index 00000000..5cf5b555
--- /dev/null
+++ b/util/termcolours.lua
@@ -0,0 +1,33 @@
+local t_concat, t_insert = table.concat, table.insert;
+local char, format = string.char, string.format;
+local ipairs = ipairs;
+module "termcolours"
+
+local stylemap = {
+ reset = 0; bright = 1, dim = 2, underscore = 4, blink = 5, reverse = 7, hidden = 8;
+ black = 30; red = 31; green = 32; yellow = 33; blue = 34; magenta = 35; cyan = 36; white = 37;
+ ["black background"] = 40; ["red background"] = 41; ["green background"] = 42; ["yellow background"] = 43; ["blue background"] = 44; ["magenta background"] = 45; ["cyan background"] = 46; ["white background"] = 47;
+ bold = 1, dark = 2, underline = 4, underlined = 4, normal = 0;
+ }
+
+local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m";
+function getstring(style, text)
+ if style then
+ return format(fmt_string, style, text);
+ else
+ return text;
+ end
+end
+
+function getstyle(...)
+ local styles, result = { ... }, {};
+ for i, style in ipairs(styles) do
+ style = stylemap[style];
+ if style then
+ t_insert(result, style);
+ end
+ end
+ return t_concat(result, ";");
+end
+
+return _M;