util/jid: string prepping functions added: prepped_split and prep
authorWaqas Hussain <waqas20@gmail.com>
Thu, 15 Jan 2009 04:34:55 +0500
changeset 717 ab428c579cbc
parent 716 d61eabc678a6
child 718 aa78dfb26593
util/jid: string prepping functions added: prepped_split and prep
util/jid.lua
--- a/util/jid.lua	Wed Jan 14 23:04:16 2009 +0500
+++ b/util/jid.lua	Thu Jan 15 04:34:55 2009 +0500
@@ -20,6 +20,9 @@
 
 
 local match = string.match;
+local nodeprep = require "util.encodings".stringprep.nodeprep;
+local nameprep = require "util.encodings".stringprep.nameprep;
+local resourceprep = require "util.encodings".stringprep.resourceprep;
 
 module "jid"
 
@@ -41,4 +44,34 @@
 	return host;
 end
 
+function prepped_split(jid)
+	local node, host, resource = split(jid);
+	if host then
+		host = nameprep(host);
+		if not host then return; end
+		if node then
+			node = nodeprep(node);
+			if not node then return; end
+		end
+		if resource then
+			resource = resourceprep(resource);
+			if not resource then return; end
+		end
+		return node, host, resource;
+	end
+end
+
+function prep(jid)
+	local node, host, resource = prepped_split(jid);
+	if host then
+		if node then
+			host = node .. "@" .. host;
+		end
+		if resource then
+			host = host .. "/" .. resource;
+		end
+	end
+	return host;
+end
+
 return _M;