From 8cf79c1fbe0cafeb495e7a404b75a043bc4cfc9e Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Tue, 23 Apr 2013 14:41:52 -0400 Subject: util.json: Make encode(decode("[]"))=="[]". --- util/json.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/util/json.lua b/util/json.lua index ff7351a7..e8de4d2d 100644 --- a/util/json.lua +++ b/util/json.lua @@ -18,6 +18,9 @@ local error = error; local newproxy, getmetatable = newproxy, getmetatable; local print = print; +local has_array, array = pcall(require, "util.array"); +local array_mt = hasarray and getmetatable(array()) or {}; + --module("json") local json = {}; @@ -165,7 +168,12 @@ function simplesave(o, buffer) elseif t == "string" then stringsave(o, buffer); elseif t == "table" then - tablesave(o, buffer); + local mt = getmetatable(o); + if mt == array_mt then + arraysave(o, buffer); + else + tablesave(o, buffer); + end elseif t == "boolean" then t_insert(buffer, (o and "true" or "false")); else @@ -237,7 +245,7 @@ function json.decode(json) local readvalue; local function readarray() - local t = {}; + local t = setmetatable({}, array_mt); next(); -- skip '[' skipstuff(); if ch == "]" then next(); return t; end -- cgit v1.2.3 From 0cba3cd26003a0b17465353ebb7a159d02ad7056 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Tue, 23 Apr 2013 15:55:49 -0400 Subject: util.json: New, improved, fixed codepoint to UTF-8 conversion. --- util/json.lua | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/util/json.lua b/util/json.lua index e8de4d2d..9c2dd2c6 100644 --- a/util/json.lua +++ b/util/json.lua @@ -2,8 +2,6 @@ -- Copyright (C) 2008-2010 Matthew Wild -- Copyright (C) 2008-2010 Waqas Hussain -- --- utf8char copyright (C) 2007 Rici Lake --- -- This project is MIT/X11 licensed. Please see the -- COPYING file in the source package for more information. -- @@ -41,32 +39,19 @@ for i=0,31 do if not escapes[ch] then escapes[ch] = ("\\u%.4X"):format(i); end end -local function utf8char(i) - if i >= 0 then - i = i - i%1 - if i < 128 then - return s_char(i) - else - local c1 = i % 64 - i = (i - c1) / 64 - if i < 32 then - return s_char(0xC0+i, 0x80+c1) - else - local c2 = i % 64 - i = (i - c2) / 64 - if i < 16 and (i ~= 13 or c2 < 32) then - return s_char(0xE0+i, 0x80+c2, 0x80+c1) - elseif i >= 16 and i < 0x110 then - local c3 = i % 64 - i = (i - c3) / 64 - return s_char(0xF0+i, 0x80+c3, 0x80+c2, 0x80+c1) - end - end - end +local function codepoint_to_utf8(code) + if code < 0x80 then return s_char(code); end + local bits0_6 = code % 64; + if code < 0x800 then + local bits6_5 = (code - bits0_6) / 64; + return s_char(0x80 + 0x40 + bits6_5, 0x80 + bits0_6); end + local bits0_12 = code % 4096; + local bits6_6 = (bits0_12 - bits0_6) / 64; + local bits12_4 = (code - bits0_12) / 4096; + return s_char(0x80 + 0x40 + 0x20 + bits12_4, 0x80 + bits6_6, 0x80 + bits0_6); end - local valid_types = { number = true, string = true, @@ -292,7 +277,7 @@ function json.decode(json) if not ch:match("[0-9a-fA-F]") then error("invalid unicode escape sequence in string"); end seq = seq..ch; end - s = s..utf8char(tonumber(seq, 16)); + s = s..codepoint_to_utf8(tonumber(seq, 16)); next(); else error("invalid escape sequence in string"); end end -- cgit v1.2.3 From b50dc4174b59176cd9baff6c81936c7dde56b69b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 25 Apr 2013 17:50:22 +0200 Subject: mod_c2s: Refactor building to allways tostring() it and only call send once --- plugins/mod_c2s.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/plugins/mod_c2s.lua b/plugins/mod_c2s.lua index cafd0c71..efef8763 100644 --- a/plugins/mod_c2s.lua +++ b/plugins/mod_c2s.lua @@ -133,25 +133,25 @@ local function session_close(session, reason) session.send(st.stanza("stream:stream", default_stream_attr):top_tag()); end if reason then -- nil == no err, initiated by us, false == initiated by client + local stream_error = st.stanza("stream:error"); if type(reason) == "string" then -- assume stream error - log("debug", "Disconnecting client, is: %s", reason); - session.send(st.stanza("stream:error"):tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' })); + stream_error:tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }); elseif type(reason) == "table" then if reason.condition then - local stanza = st.stanza("stream:error"):tag(reason.condition, stream_xmlns_attr):up(); + stream_error:tag(reason.condition, stream_xmlns_attr):up(); if reason.text then - stanza:tag("text", stream_xmlns_attr):text(reason.text):up(); + stream_error:tag("text", stream_xmlns_attr):text(reason.text):up(); end if reason.extra then - stanza:add_child(reason.extra); + stream_error:add_child(reason.extra); end - log("debug", "Disconnecting client, is: %s", tostring(stanza)); - session.send(stanza); elseif reason.name then -- a stanza - log("debug", "Disconnecting client, is: %s", tostring(reason)); - session.send(reason); + stream_error = reason; end end + stream_error = tostring(stream_error); + log("debug", "Disconnecting client, is: %s", stream_error); + session.send(stream_error); end session.send(""); -- cgit v1.2.3 From 55913ddacf9ddb21e20d865c193bff04fd778876 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 25 Apr 2013 20:36:55 +0100 Subject: MUC: add __tostring on room metatable --- plugins/muc/muc.lib.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/muc/muc.lib.lua b/plugins/muc/muc.lib.lua index b6167a19..3af8c766 100644 --- a/plugins/muc/muc.lib.lua +++ b/plugins/muc/muc.lib.lua @@ -88,6 +88,10 @@ local function getText(stanza, path) return getUsingPath(stanza, path, true); en local room_mt = {}; room_mt.__index = room_mt; +function room_mt:__tostring() + return "MUC room ("..self.jid..")"; +end + function room_mt:get_default_role(affiliation) if affiliation == "owner" or affiliation == "admin" then return "moderator"; -- cgit v1.2.3 From 55b90a8abd0e65e476c7df3fe0ff437a99607f3e Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 25 Apr 2013 20:37:20 +0100 Subject: mod_admin_telnet: Add muc:create(room) (thanks SkyBlue) --- plugins/mod_admin_telnet.lua | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/plugins/mod_admin_telnet.lua b/plugins/mod_admin_telnet.lua index e1b90684..2622a5f9 100644 --- a/plugins/mod_admin_telnet.lua +++ b/plugins/mod_admin_telnet.lua @@ -903,13 +903,23 @@ local console_room_mt = { end; }; -function def_env.muc:room(room_jid) - local room_name, host = jid_split(room_jid); +local function check_muc(jid) + local room_name, host = jid_split(jid); if not hosts[host] then return nil, "No such host: "..host; elseif not hosts[host].modules.muc then return nil, "Host '"..host.."' is not a MUC service"; end + return room_name, host; +end + +function def_env.muc:create(room_jid) + local room, host = check_muc(room_jid); + return hosts[host].modules.muc.create_room(room_jid); +end + +function def_env.muc:room(room_jid) + local room_name, host = check_muc(room_jid); local room_obj = hosts[host].modules.muc.rooms[room_jid]; if not room_obj then return nil, "No such room: "..room_jid; -- cgit v1.2.3