Diff

util/jid.lua @ 4407:f78c6f5fa090

util.jid: Added escape() and unescape().
author Waqas Hussain <waqas20@gmail.com>
date Tue, 01 Nov 2011 23:41:32 +0500
parent 3480:97831dfe7f72
child 5776:bd0ff8ae98a8
child 5944:f3817912e8b2
line wrap: on
line diff
--- a/util/jid.lua	Wed Oct 26 18:58:01 2011 -0400
+++ b/util/jid.lua	Tue Nov 01 23:41:32 2011 +0500
@@ -13,6 +13,16 @@
 local nameprep = require "util.encodings".stringprep.nameprep;
 local resourceprep = require "util.encodings".stringprep.resourceprep;
 
+local escapes = {
+	[" "] = "\\20"; ['"'] = "\\22";
+	["&"] = "\\26"; ["'"] = "\\27";
+	["/"] = "\\2f"; [":"] = "\\3a";
+	["<"] = "\\3c"; [">"] = "\\3e";
+	["@"] = "\\40"; ["\\"] = "\\5c";
+};
+local unescapes = {};
+for k,v in pairs(escapes) do unescapes[v] = k; end
+
 module "jid"
 
 local function _split(jid)
@@ -91,4 +101,7 @@
 	return false
 end
 
+function escape(s) return s and (s:gsub(".", escapes)); end
+function unescape(s) return s and (s:gsub("\\%x%x", unescapes)); end
+
 return _M;