From 6bb0a34965cfb0f57f3e2a19ba8f4584f1e799ed Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 13 Nov 2008 03:47:44 +0000 Subject: Add new top_tag() method to stanzas --- util/stanza.lua | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'util') diff --git a/util/stanza.lua b/util/stanza.lua index 52f372cc..5339b91e 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -105,6 +105,14 @@ function stanza_mt.__tostring(t) return s_format("<%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 -- cgit v1.2.3 From a3018d64216b7df0b18deaa5bb0bc1625c1ed670 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Thu, 13 Nov 2008 12:07:53 +0500 Subject: Fixed stanza deserialization --- util/stanza.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'util') diff --git a/util/stanza.lua b/util/stanza.lua index 52f372cc..79d3167e 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -149,6 +149,9 @@ function deserialize(stanza) end stanza.tags = tags; end + if not stanza.last_add then + stanza.last_add = {}; + end end return stanza; -- cgit v1.2.3 From 6c0003661af9cee25d27dcee5b7cd4520c105cdd Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Thu, 13 Nov 2008 12:10:42 +0500 Subject: Added support for storing (and removing), loading and appending to lists of data to datamanager (for supporting offline messages) --- util/datamanager.lua | 69 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 6 deletions(-) (limited to 'util') diff --git a/util/datamanager.lua b/util/datamanager.lua index aad370d1..0f00da1b 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -1,6 +1,6 @@ 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; @@ -9,6 +9,7 @@ local os_remove = os.remove; local tostring = tostring; 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; -- cgit v1.2.3 From 5358129300fc848b130811999adb945773f171fb Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Thu, 13 Nov 2008 12:12:19 +0500 Subject: Added util.datetime: Utility methods to support XEP-0082: XMPP Date and Time Profiles --- util/datetime.lua | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 util/datetime.lua (limited to 'util') 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; -- cgit v1.2.3 From 428fc65bbc595d80cb678dadcc9ea7cae055d5b9 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 02:41:37 +0000 Subject: Some more logging fixes --- util/logger.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'util') diff --git a/util/logger.lua b/util/logger.lua index 8d983605..fd5d3d80 100644 --- a/util/logger.lua +++ b/util/logger.lua @@ -13,9 +13,9 @@ function init(name) level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; end if ... then - print(level, format(message, ...)); + print(name, level, format(message, ...)); else - print(level, message); + print(name, level, message); end end end -- cgit v1.2.3 From d67940a1d7700c0389341c42f845b91e8b32eea6 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 16:03:33 +0000 Subject: A treat for Linux users ;) --- util/logger.lua | 17 +++++++++++++++-- util/stanza.lua | 48 ++++++++++++++++++++++++++++++++++++++++++++---- util/termcolours.lua | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 util/termcolours.lua (limited to 'util') diff --git a/util/logger.lua b/util/logger.lua index fd5d3d80..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(name, level, format(message, ...)); + print(name, getstring(logstyles[level], level), format(message, ...)); else - print(name, level, message); + print(name, getstring(logstyles[level], level), message); end end end diff --git a/util/stanza.lua b/util/stanza.lua index cfa33c5b..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" @@ -157,9 +160,6 @@ function deserialize(stanza) end stanza.tags = tags; end - if not stanza.last_add then - stanza.last_add = {}; - end end return stanza; @@ -195,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, ""); + local tag_format = top_tag_format.."%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; -- cgit v1.2.3 From fc57c3a09a9ca234f45bb3a1e9b3d8fd8d48d2e0 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Sat, 15 Nov 2008 05:33:14 +0500 Subject: Some bugs fixed --- util/datamanager.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util') diff --git a/util/datamanager.lua b/util/datamanager.lua index 0f00da1b..80b35733 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -6,7 +6,7 @@ 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; -- cgit v1.2.3