aboutsummaryrefslogtreecommitdiffstats
path: root/teal-src/prosody/util
diff options
context:
space:
mode:
Diffstat (limited to 'teal-src/prosody/util')
-rw-r--r--teal-src/prosody/util/array.d.tl9
-rw-r--r--teal-src/prosody/util/async.d.tl42
-rw-r--r--teal-src/prosody/util/bitcompat.d.tl8
-rw-r--r--teal-src/prosody/util/compat.d.tl4
-rw-r--r--teal-src/prosody/util/crand.d.tl6
-rw-r--r--teal-src/prosody/util/crypto.d.tl57
-rw-r--r--teal-src/prosody/util/dataforms.d.tl54
-rw-r--r--teal-src/prosody/util/datamapper.tl379
-rw-r--r--teal-src/prosody/util/datetime.d.tl9
-rw-r--r--teal-src/prosody/util/encodings.d.tl27
-rw-r--r--teal-src/prosody/util/error.d.tl78
-rw-r--r--teal-src/prosody/util/format.d.tl4
-rw-r--r--teal-src/prosody/util/hashes.d.tl35
-rw-r--r--teal-src/prosody/util/hex.d.tl8
-rw-r--r--teal-src/prosody/util/http.d.tl9
-rw-r--r--teal-src/prosody/util/human/io.d.tl28
-rw-r--r--teal-src/prosody/util/human/units.d.tl8
-rw-r--r--teal-src/prosody/util/id.d.tl9
-rw-r--r--teal-src/prosody/util/interpolation.d.tl6
-rw-r--r--teal-src/prosody/util/ip.d.tl20
-rw-r--r--teal-src/prosody/util/jid.d.tl15
-rw-r--r--teal-src/prosody/util/json.d.tl18
-rw-r--r--teal-src/prosody/util/jsonpointer.tl46
-rw-r--r--teal-src/prosody/util/jsonschema.tl505
-rw-r--r--teal-src/prosody/util/jwt.d.tl38
-rw-r--r--teal-src/prosody/util/logger.d.tl18
-rw-r--r--teal-src/prosody/util/mathcompat.tl15
-rw-r--r--teal-src/prosody/util/net.d.tl13
-rw-r--r--teal-src/prosody/util/poll.d.tl31
-rw-r--r--teal-src/prosody/util/pposix.d.tl109
-rw-r--r--teal-src/prosody/util/promise.d.tl22
-rw-r--r--teal-src/prosody/util/queue.d.tl21
-rw-r--r--teal-src/prosody/util/random.d.tl4
-rw-r--r--teal-src/prosody/util/ringbuffer.d.tl20
-rw-r--r--teal-src/prosody/util/roles.d.tl32
-rw-r--r--teal-src/prosody/util/serialization.d.tl34
-rw-r--r--teal-src/prosody/util/set.d.tl22
-rw-r--r--teal-src/prosody/util/signal.d.tl41
-rw-r--r--teal-src/prosody/util/smqueue.tl99
-rw-r--r--teal-src/prosody/util/stanza.d.tl145
-rw-r--r--teal-src/prosody/util/strbitop.d.tl7
-rw-r--r--teal-src/prosody/util/struct.d.tl6
-rw-r--r--teal-src/prosody/util/table.d.tl7
-rw-r--r--teal-src/prosody/util/termcolours.d.tl7
-rw-r--r--teal-src/prosody/util/time.d.tl6
-rw-r--r--teal-src/prosody/util/timer.d.tl8
-rw-r--r--teal-src/prosody/util/uuid.d.tl8
-rw-r--r--teal-src/prosody/util/xtemplate.tl108
48 files changed, 2205 insertions, 0 deletions
diff --git a/teal-src/prosody/util/array.d.tl b/teal-src/prosody/util/array.d.tl
new file mode 100644
index 00000000..70bf2624
--- /dev/null
+++ b/teal-src/prosody/util/array.d.tl
@@ -0,0 +1,9 @@
+local record array_t<T>
+ { T }
+end
+
+local record lib
+ metamethod __call : function () : array_t
+end
+
+return lib
diff --git a/teal-src/prosody/util/async.d.tl b/teal-src/prosody/util/async.d.tl
new file mode 100644
index 00000000..a2e41cd6
--- /dev/null
+++ b/teal-src/prosody/util/async.d.tl
@@ -0,0 +1,42 @@
+local record lib
+ ready : function () : boolean
+ waiter : function (num : integer, allow_many : boolean) : function (), function ()
+ guarder : function () : function (id : function ()) : function () | nil
+ record runner_t<T>
+ func : function (T)
+ thread : thread
+ enum state_e
+ -- from Lua manual
+ "running"
+ "suspended"
+ "normal"
+ "dead"
+
+ -- from util.async
+ "ready"
+ "error"
+ end
+ state : state_e
+ notified_state : state_e
+ queue : { T }
+ type watcher_t = function (runner_t<T>, ... : any)
+ type watchers_t = { state_e : watcher_t }
+ data : any
+ id : string
+
+ run : function (runner_t<T>, T) : boolean, state_e, integer
+ enqueue : function (runner_t<T>, T) : runner_t<T>
+ log : function (runner_t<T>, string, string, ... : any)
+ onready : function (runner_t<T>, function) : runner_t<T>
+ onready : function (runner_t<T>, function) : runner_t<T>
+ onwaiting : function (runner_t<T>, function) : runner_t<T>
+ onerror : function (runner_t<T>, function) : runner_t<T>
+ end
+ runner : function <T>(function (T), runner_t.watchers_t, any) : runner_t<T>
+ wait_for : function (any) : any, any
+ sleep : function (t:number)
+
+ -- set_nexttick = function(new_next_tick) next_tick = new_next_tick; end;
+ -- set_schedule_function = function (new_schedule_function) schedule_task = new_schedule_function; end;
+end
+return lib
diff --git a/teal-src/prosody/util/bitcompat.d.tl b/teal-src/prosody/util/bitcompat.d.tl
new file mode 100644
index 00000000..18adf725
--- /dev/null
+++ b/teal-src/prosody/util/bitcompat.d.tl
@@ -0,0 +1,8 @@
+local record lib
+ band : function (integer, integer, ... : integer) : integer
+ bor : function (integer, integer, ... : integer) : integer
+ bxor : function (integer, integer, ... : integer) : integer
+ lshift : function (integer, integer) : integer
+ rshift : function (integer, integer) : integer
+end
+return lib
diff --git a/teal-src/prosody/util/compat.d.tl b/teal-src/prosody/util/compat.d.tl
new file mode 100644
index 00000000..da9c6083
--- /dev/null
+++ b/teal-src/prosody/util/compat.d.tl
@@ -0,0 +1,4 @@
+local record lib
+ xpcall : function (function, function, ...:any):boolean, any
+end
+return lib
diff --git a/teal-src/prosody/util/crand.d.tl b/teal-src/prosody/util/crand.d.tl
new file mode 100644
index 00000000..b40cb67e
--- /dev/null
+++ b/teal-src/prosody/util/crand.d.tl
@@ -0,0 +1,6 @@
+local record lib
+ bytes : function (n : integer) : string
+ enum sourceid "OpenSSL" "arc4random()" "Linux" end
+ _source : sourceid
+end
+return lib
diff --git a/teal-src/prosody/util/crypto.d.tl b/teal-src/prosody/util/crypto.d.tl
new file mode 100644
index 00000000..866185d0
--- /dev/null
+++ b/teal-src/prosody/util/crypto.d.tl
@@ -0,0 +1,57 @@
+local record lib
+ record key
+ private_pem : function (key) : string
+ public_pem : function (key) : string
+ get_type : function (key) : string
+ end
+
+ type base_evp_sign = function (key, message : string) : string
+ type base_evp_verify = function (key, message : string, signature : string) : boolean
+
+ ed25519_sign : base_evp_sign
+ ed25519_verify : base_evp_verify
+
+ ecdsa_sha256_sign : base_evp_sign
+ ecdsa_sha256_verify : base_evp_verify
+ ecdsa_sha384_sign : base_evp_sign
+ ecdsa_sha384_verify : base_evp_verify
+ ecdsa_sha512_sign : base_evp_sign
+ ecdsa_sha512_verify : base_evp_verify
+
+ rsassa_pkcs1_sha256_sign : base_evp_sign
+ rsassa_pkcs1_sha256_verify : base_evp_verify
+ rsassa_pkcs1_sha384_sign : base_evp_sign
+ rsassa_pkcs1_sha384_verify : base_evp_verify
+ rsassa_pkcs1_sha512_sign : base_evp_sign
+ rsassa_pkcs1_sha512_verify : base_evp_verify
+
+ rsassa_pss_sha256_sign : base_evp_sign
+ rsassa_pss_sha256_verify : base_evp_verify
+ rsassa_pss_sha384_sign : base_evp_sign
+ rsassa_pss_sha384_verify : base_evp_verify
+ rsassa_pss_sha512_sign : base_evp_sign
+ rsassa_pss_sha512_verify : base_evp_verify
+
+ type Levp_encrypt = function (key : string, iv : string, plaintext : string) : string
+ type Levp_decrypt = function (key : string, iv : string, ciphertext : string) : string, string
+
+ aes_128_gcm_encrypt : Levp_encrypt
+ aes_128_gcm_decrypt : Levp_decrypt
+ aes_256_gcm_encrypt : Levp_encrypt
+ aes_256_gcm_decrypt : Levp_decrypt
+
+ aes_256_ctr_encrypt : Levp_encrypt
+ aes_256_ctr_decrypt : Levp_decrypt
+
+ generate_ed25519_keypair : function () : key
+
+ import_private_pem : function (string) : key
+ import_public_pem : function (string) : key
+
+ parse_ecdsa_signature : function (string, integer) : string, string
+ build_ecdsa_signature : function (r : string, s : string) : string
+
+ version : string
+ _LIBCRYPTO_VERSION : string
+end
+return lib
diff --git a/teal-src/prosody/util/dataforms.d.tl b/teal-src/prosody/util/dataforms.d.tl
new file mode 100644
index 00000000..3dad2776
--- /dev/null
+++ b/teal-src/prosody/util/dataforms.d.tl
@@ -0,0 +1,54 @@
+local stanza_t = require "prosody.util.stanza".stanza_t
+
+local record lib
+ record dataform
+ title : string
+ instructions : string
+
+ record form_field
+
+ enum field_type
+ "boolean"
+ "fixed"
+ "hidden"
+ "jid-multi"
+ "jid-single"
+ "list-multi"
+ "list-single"
+ "text-multi"
+ "text-private"
+ "text-single"
+ end
+
+ type : field_type
+ var : string -- protocol name
+ name : string -- internal name
+
+ label : string
+ desc : string
+
+ datatype : string
+ range_min : number
+ range_max : number
+
+ value : any -- depends on field_type
+ options : table
+ end
+
+ { form_field }
+
+ enum form_type
+ "form"
+ "submit"
+ "cancel"
+ "result"
+ end
+
+ form : function ( dataform, { string : any }, form_type ) : stanza_t
+ data : function ( dataform, stanza_t ) : { string : any }
+ end
+
+ new : function ( dataform ) : dataform
+end
+
+return lib
diff --git a/teal-src/prosody/util/datamapper.tl b/teal-src/prosody/util/datamapper.tl
new file mode 100644
index 00000000..89d5e29b
--- /dev/null
+++ b/teal-src/prosody/util/datamapper.tl
@@ -0,0 +1,379 @@
+-- Copyright (C) 2021 Kim Alvefur
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+-- Based on
+-- https://json-schema.org/draft/2020-12/json-schema-core.html
+-- https://json-schema.org/draft/2020-12/json-schema-validation.html
+-- http://spec.openapis.org/oas/v3.0.1#xmlObject
+-- https://github.com/OAI/OpenAPI-Specification/issues/630 (text:true)
+--
+-- XML Object Extensions:
+-- text to refer to the text content at the same time as attributes
+-- x_name_is_value for enum fields where the <tag-name/> is the value
+-- x_single_attribute for <tag attr="this"/>
+--
+-- TODO pointers
+-- TODO cleanup / refactor
+-- TODO s/number/integer/ once we have appropriate math.type() compat
+--
+
+if not math.type then require "prosody.util.mathcompat" end
+
+local st = require "prosody.util.stanza";
+local json = require "prosody.util.json"
+local pointer = require "prosody.util.jsonpointer";
+
+local json_type_name = json.json_type_name;
+local json_schema_object = require "prosody.util.jsonschema"
+local type schema_t = boolean | json_schema_object
+
+local function toboolean ( s : string ) : boolean
+ if s == "true" or s == "1" then
+ return true
+ elseif s == "false" or s == "0" then
+ return false
+ elseif s then
+ return true
+ end
+end
+
+local function totype(t : json_type_name, s : string) : any
+ if not s then return nil end
+ if t == "string" then
+ return s;
+ elseif t == "boolean" then
+ return toboolean(s)
+ elseif t == "number" or t == "integer" then
+ return tonumber(s)
+ end
+end
+
+local enum value_goes
+ "in_tag_name"
+ "in_text"
+ "in_text_tag"
+ "in_attribute"
+ "in_single_attribute"
+ "in_children"
+ "in_wrapper"
+end
+
+local function resolve_schema(schema : schema_t, root : json_schema_object) : schema_t
+ if schema is json_schema_object then
+ if schema["$ref"] and schema["$ref"]:sub(1, 1) == "#" then
+ return pointer.resolve(root as table, schema["$ref"]:sub(2)) as schema_t;
+ end
+ end
+ return schema;
+end
+
+local function guess_schema_type(schema : json_schema_object) : json_type_name
+ local schema_types = schema.type
+ if schema_types is json_type_name then
+ return schema_types
+ elseif schema_types ~= nil then
+ error "schema has unsupported 'type' property"
+ elseif schema.properties then
+ return "object"
+ elseif schema.items then
+ return "array"
+ end
+ return "string" -- default assumption
+end
+
+local function unpack_propschema( propschema : schema_t, propname : string, current_ns : string )
+ : json_type_name, value_goes, string, string, string, string, { any }
+ local proptype : json_type_name = "string"
+ local value_where : value_goes = propname and "in_text_tag" or "in_text"
+ local name = propname
+ local namespace : string
+ local prefix : string
+ local single_attribute : string
+ local enums : { any }
+
+ if propschema is json_schema_object then
+ proptype = guess_schema_type(propschema);
+ elseif propschema is string then -- Teal says this can never be a string, but it could before so best be sure
+ error("schema as string is not supported: "..propschema.." {"..current_ns.."}"..propname)
+ end
+
+ if proptype == "object" or proptype == "array" then
+ value_where = "in_children"
+ end
+
+ if propschema is json_schema_object then
+ local xml = propschema.xml
+ if xml then
+ if xml.name then
+ name = xml.name
+ end
+ if xml.namespace and xml.namespace ~= current_ns then
+ namespace = xml.namespace
+ end
+ if xml.prefix then
+ prefix = xml.prefix
+ end
+ if proptype == "array" and xml.wrapped then
+ value_where = "in_wrapper"
+ elseif xml.attribute then
+ value_where = "in_attribute"
+ elseif xml.text then
+ value_where = "in_text"
+ elseif xml.x_name_is_value then
+ value_where = "in_tag_name"
+ elseif xml.x_single_attribute then
+ single_attribute = xml.x_single_attribute
+ value_where = "in_single_attribute"
+ end
+ end
+ if propschema["const"] then
+ enums = { propschema["const"] }
+ elseif propschema["enum"] then
+ enums = propschema["enum"]
+ end
+ end
+
+ return proptype, value_where, name, namespace, prefix, single_attribute, enums
+end
+
+local parse_object : function (schema : schema_t, s : st.stanza_t, root : json_schema_object) : { string : any }
+local parse_array : function (schema : schema_t, s : st.stanza_t, root : json_schema_object) : { any }
+
+local function extract_value (s : st.stanza_t, value_where : value_goes, proptype : json.json_type_name, name : string, namespace : string, prefix : string, single_attribute : string, enums : { any }) : string
+ if value_where == "in_tag_name" then
+ local c : st.stanza_t
+ if proptype == "boolean" then
+ c = s:get_child(name, namespace);
+ elseif enums and proptype == "string" then
+ -- XXX O(n²) ?
+ -- Probably better to flip the table and loop over :childtags(nil, ns), should be 2xO(n)
+ -- BUT works first, optimize later
+ for i = 1, #enums do
+ c = s:get_child(enums[i] as string, namespace);
+ if c then break end
+ end
+ else
+ c = s:get_child(nil, namespace);
+ end
+ if c then
+ return c.name;
+ end
+ elseif value_where == "in_attribute" then
+ local attr = name
+ if prefix then
+ attr = prefix .. ':' .. name
+ elseif namespace and namespace ~= s.attr.xmlns then
+ attr = namespace .. "\1" .. name
+ end
+ return s.attr[attr]
+
+ elseif value_where == "in_text" then
+ return s:get_text()
+
+ elseif value_where == "in_single_attribute" then
+ local c = s:get_child(name, namespace)
+ return c and c.attr[single_attribute]
+ elseif value_where == "in_text_tag" then
+ return s:get_child_text(name, namespace)
+ end
+end
+
+function parse_object (schema : schema_t, s : st.stanza_t, root : json_schema_object) : { string : any }
+ local out : { string : any } = {}
+ schema = resolve_schema(schema, root)
+ if schema is json_schema_object and schema.properties then
+ for prop, propschema in pairs(schema.properties) do
+ propschema = resolve_schema(propschema, root)
+
+ local proptype, value_where, name, namespace, prefix, single_attribute, enums = unpack_propschema(propschema, prop, s.attr.xmlns)
+
+ if value_where == "in_children" and propschema is json_schema_object then
+ if proptype == "object" then
+ local c = s:get_child(name, namespace)
+ if c then
+ out[prop] = parse_object(propschema, c, root);
+ end
+ elseif proptype == "array" then
+ local a = parse_array(propschema, s, root);
+ if a and a[1] ~= nil then
+ out[prop] = a;
+ end
+ else
+ error "unreachable"
+ end
+ elseif value_where == "in_wrapper" and propschema is json_schema_object and proptype == "array" then
+ local wrapper = s:get_child(name, namespace);
+ if wrapper then
+ out[prop] = parse_array(propschema, wrapper, root);
+ end
+ else
+ local value : string = extract_value (s, value_where, proptype, name, namespace, prefix, single_attribute, enums)
+
+ out[prop] = totype(proptype, value)
+ end
+ end
+ end
+
+ return out
+end
+
+function parse_array (schema : json_schema_object, s : st.stanza_t, root : json_schema_object) : { any }
+ local itemschema : schema_t = resolve_schema(schema.items, root);
+ local proptype, value_where, child_name, namespace, prefix, single_attribute, enums = unpack_propschema(itemschema, nil, s.attr.xmlns)
+ local attr_name : string
+ if value_where == "in_single_attribute" then -- FIXME this shouldn't be needed
+ value_where = "in_attribute";
+ attr_name = single_attribute;
+ end
+ local out : { any } = {}
+
+ if proptype == "object" then
+ if itemschema is json_schema_object then
+ for c in s:childtags(child_name, namespace) do
+ table.insert(out, parse_object(itemschema, c, root));
+ end
+ else
+ error "array items must be schema object"
+ end
+ elseif proptype == "array" then
+ if itemschema is json_schema_object then
+ for c in s:childtags(child_name, namespace) do
+ table.insert(out, parse_array(itemschema, c, root));
+ end
+ end
+ else
+ for c in s:childtags(child_name, namespace) do
+ local value : string = extract_value (c, value_where, proptype, attr_name or child_name, namespace, prefix, single_attribute, enums)
+
+ table.insert(out, totype(proptype, value));
+ end
+ end
+ return out;
+end
+
+local function parse (schema : json_schema_object, s : st.stanza_t) : table
+ local s_type = guess_schema_type(schema)
+ if s_type == "object" then
+ return parse_object(schema, s, schema)
+ elseif s_type == "array" then
+ return parse_array(schema, s, schema)
+ else
+ error "top-level scalars unsupported"
+ end
+end
+
+local function toxmlstring(proptype : json_type_name, v : any) : string
+ if proptype == "string" and v is string then
+ return v
+ elseif proptype == "number" and v is number then
+ return string.format("%g", v)
+ elseif proptype == "integer" and v is number then -- TODO is integer
+ return string.format("%d", v)
+ elseif proptype == "boolean" then
+ return v and "1" or "0"
+ end
+end
+
+local unparse : function (json_schema_object, table, string, string, st.stanza_t, json_schema_object) : st.stanza_t
+
+local function unparse_property(out : st.stanza_t, v : any, proptype : json_type_name, propschema : schema_t, value_where : value_goes, name : string, namespace : string, current_ns : string, prefix : string, single_attribute : string, root : json_schema_object)
+
+ if value_where == "in_attribute" then
+ local attr = name
+ if prefix then
+ attr = prefix .. ':' .. name
+ elseif namespace and namespace ~= current_ns then
+ attr = namespace .. "\1" .. name
+ end
+
+ out.attr[attr] = toxmlstring(proptype, v)
+ elseif value_where == "in_text" then
+ out:text(toxmlstring(proptype, v))
+ elseif value_where == "in_single_attribute" then
+ assert(single_attribute)
+ local propattr : { string : string } = {}
+
+ if namespace and namespace ~= current_ns then
+ propattr.xmlns = namespace
+ end
+
+ propattr[single_attribute] = toxmlstring(proptype, v)
+ out:tag(name, propattr):up();
+
+ else
+ local propattr : { string : string }
+ if namespace ~= current_ns then
+ propattr = { xmlns = namespace }
+ end
+ if value_where == "in_tag_name" then
+ if proptype == "string" and v is string then
+ out:tag(v, propattr):up();
+ elseif proptype == "boolean" and v == true then
+ out:tag(name, propattr):up();
+ end
+ elseif proptype == "object" and propschema is json_schema_object and v is table then
+ local c = unparse(propschema, v, name, namespace, nil, root);
+ if c then
+ out:add_direct_child(c);
+ end
+ elseif proptype == "array" and propschema is json_schema_object and v is table then
+ if value_where == "in_wrapper" then
+ local c = unparse(propschema, v, name, namespace, nil, root);
+ if c then
+ out:add_direct_child(c);
+ end
+ else
+ unparse(propschema, v, name, namespace, out, root);
+ end
+ else
+ out:text_tag(name, toxmlstring(proptype, v), propattr)
+ end
+ end
+end
+
+function unparse ( schema : json_schema_object, t : table, current_name : string, current_ns : string, ctx : st.stanza_t, root : json_schema_object ) : st.stanza_t
+
+ if root == nil then root = schema end
+
+ if schema.xml then
+ if schema.xml.name then
+ current_name = schema.xml.name
+ end
+ if schema.xml.namespace then
+ current_ns = schema.xml.namespace
+ end
+ -- TODO prefix?
+ end
+
+ local out = ctx or st.stanza(current_name, { xmlns = current_ns })
+
+ local s_type = guess_schema_type(schema)
+ if s_type == "object" then
+
+ for prop, propschema in pairs(schema.properties) do
+ propschema = resolve_schema(propschema, root)
+ local v = t[prop]
+
+ if v ~= nil then
+ local proptype, value_where, name, namespace, prefix, single_attribute = unpack_propschema(propschema, prop, current_ns)
+ unparse_property(out, v, proptype, propschema, value_where, name, namespace, current_ns, prefix, single_attribute, root)
+ end
+ end
+ return out;
+
+ elseif s_type == "array" then
+ local itemschema = resolve_schema(schema.items, root)
+ local proptype, value_where, name, namespace, prefix, single_attribute = unpack_propschema(itemschema, current_name, current_ns)
+ for _, item in ipairs(t as { string }) do
+ unparse_property(out, item, proptype, itemschema, value_where, name, namespace, current_ns, prefix, single_attribute, root)
+ end
+ return out;
+ end
+end
+
+return {
+ parse = parse,
+ unparse = unparse,
+}
diff --git a/teal-src/prosody/util/datetime.d.tl b/teal-src/prosody/util/datetime.d.tl
new file mode 100644
index 00000000..9f770a73
--- /dev/null
+++ b/teal-src/prosody/util/datetime.d.tl
@@ -0,0 +1,9 @@
+local record lib
+ date : function (t : number) : string
+ datetime : function (t : number) : string
+ time : function (t : number) : string
+ legacy : function (t : number) : string
+ parse : function (t : string) : number
+end
+
+return lib
diff --git a/teal-src/prosody/util/encodings.d.tl b/teal-src/prosody/util/encodings.d.tl
new file mode 100644
index 00000000..58aefa5e
--- /dev/null
+++ b/teal-src/prosody/util/encodings.d.tl
@@ -0,0 +1,27 @@
+-- TODO many actually return Maybe(String)
+local record lib
+ record base64
+ encode : function (s : string) : string
+ decode : function (s : string) : string
+ end
+ record stringprep
+ nameprep : function (s : string, strict : boolean) : string
+ nodeprep : function (s : string, strict : boolean) : string
+ resourceprep : function (s : string, strict : boolean) : string
+ saslprep : function (s : string, strict : boolean) : string
+ end
+ record idna
+ to_ascii : function (s : string) : string
+ to_unicode : function (s : string) : string
+ end
+ record utf8
+ valid : function (s : string) : boolean
+ length : function (s : string) : integer
+ end
+ record confusable
+ skeleton : function (s : string) : string
+ end
+ version : string
+end
+return lib
+
diff --git a/teal-src/prosody/util/error.d.tl b/teal-src/prosody/util/error.d.tl
new file mode 100644
index 00000000..4c3a7196
--- /dev/null
+++ b/teal-src/prosody/util/error.d.tl
@@ -0,0 +1,78 @@
+local enum error_type
+ "auth"
+ "cancel"
+ "continue"
+ "modify"
+ "wait"
+end
+
+local enum error_condition
+ "bad-request"
+ "conflict"
+ "feature-not-implemented"
+ "forbidden"
+ "gone"
+ "internal-server-error"
+ "item-not-found"
+ "jid-malformed"
+ "not-acceptable"
+ "not-allowed"
+ "not-authorized"
+ "policy-violation"
+ "recipient-unavailable"
+ "redirect"
+ "registration-required"
+ "remote-server-not-found"
+ "remote-server-timeout"
+ "resource-constraint"
+ "service-unavailable"
+ "subscription-required"
+ "undefined-condition"
+ "unexpected-request"
+end
+
+local record protoerror
+ type : error_type
+ condition : error_condition
+ text : string
+ code : integer
+end
+
+local record Error
+ type : error_type
+ condition : error_condition
+ text : string
+ code : integer
+ context : { any : any }
+ source : string
+end
+
+local type compact_registry_item = { string, string, string, string }
+local type compact_registry = { compact_registry_item }
+local type registry = { string : protoerror }
+local type context = { string : any }
+
+local record error_registry_wrapper
+ source : string
+ registry : registry
+ new : function (string, context) : Error
+ coerce : function (any, string) : any, Error
+ wrap : function (Error) : Error
+ wrap : function (string, context) : Error
+ is_error : function (any) : boolean
+end
+
+local record lib
+ record configure_opt
+ auto_inject_traceback : boolean
+ end
+ new : function (protoerror, context, { string : protoerror }, string) : Error
+ init : function (string, string, registry | compact_registry) : error_registry_wrapper
+ init : function (string, registry | compact_registry) : error_registry_wrapper
+ is_error : function (any) : boolean
+ coerce : function (any, string) : any, Error
+ from_stanza : function (table, context, string) : Error
+ configure : function
+end
+
+return lib
diff --git a/teal-src/prosody/util/format.d.tl b/teal-src/prosody/util/format.d.tl
new file mode 100644
index 00000000..1ff77c97
--- /dev/null
+++ b/teal-src/prosody/util/format.d.tl
@@ -0,0 +1,4 @@
+local record lib
+ format : function (string, ... : any) : string
+end
+return lib
diff --git a/teal-src/prosody/util/hashes.d.tl b/teal-src/prosody/util/hashes.d.tl
new file mode 100644
index 00000000..64c5a12b
--- /dev/null
+++ b/teal-src/prosody/util/hashes.d.tl
@@ -0,0 +1,35 @@
+local type hash = function (msg : string, hex : boolean) : string
+local type hmac = function (key : string, msg : string, hex : boolean) : string
+local type kdf = function (pass : string, salt : string, i : integer) : string
+
+local record lib
+ sha1 : hash
+ sha224 : hash
+ sha256 : hash
+ sha384 : hash
+ sha512 : hash
+ md5 : hash
+ sha3_256 : hash
+ sha3_512 : hash
+ blake2s256 : hash
+ blake2b512 : hash
+ hmac_sha1 : hmac
+ hmac_sha224 : hmac
+ hmac_sha256 : hmac
+ hmac_sha384 :hmac
+ hmac_sha512 : hmac
+ hmac_md5 : hmac
+ hmac_sha3_256 : hmac
+ hmac_sha3_512 : hmac
+ hmac_blake2s256 : hmac
+ hmac_blake2b512 : hmac
+ scram_Hi_sha1 : kdf
+ pbkdf2_hmac_sha1 : kdf
+ pbkdf2_hmac_sha256 : kdf
+ hkdf_hmac_sha256 : kdf
+ hkdf_hmac_sha384 : kdf
+ equals : function (string, string) : boolean
+ version : string
+ _LIBCRYPTO_VERSION : string
+end
+return lib
diff --git a/teal-src/prosody/util/hex.d.tl b/teal-src/prosody/util/hex.d.tl
new file mode 100644
index 00000000..9d84540b
--- /dev/null
+++ b/teal-src/prosody/util/hex.d.tl
@@ -0,0 +1,8 @@
+local type s2s = function (s : string) : string
+local record lib
+ to : s2s
+ from : s2s
+ encode : s2s
+ decode : s2s
+end
+return lib
diff --git a/teal-src/prosody/util/http.d.tl b/teal-src/prosody/util/http.d.tl
new file mode 100644
index 00000000..ecbe35c3
--- /dev/null
+++ b/teal-src/prosody/util/http.d.tl
@@ -0,0 +1,9 @@
+local record lib
+ urlencode : function (s : string) : string
+ urldecode : function (s : string) : string
+ formencode : function (f : { string : string }) : string
+ formdecode : function (s : string) : { string : string }
+ contains_token : function (field : string, token : string) : boolean
+ normalize_path : function (path : string) : string
+end
+return lib
diff --git a/teal-src/prosody/util/human/io.d.tl b/teal-src/prosody/util/human/io.d.tl
new file mode 100644
index 00000000..e4f64cd1
--- /dev/null
+++ b/teal-src/prosody/util/human/io.d.tl
@@ -0,0 +1,28 @@
+local record lib
+ getchar : function (n : integer) : string
+ getline : function () : string
+ getpass : function () : string
+ show_yesno : function (prompt : string) : boolean
+ read_password : function () : string
+ show_prompt : function (prompt : string) : boolean
+ printf : function (fmt : string, ... : any)
+ padleft : function (s : string, width : integer) : string
+ padright : function (s : string, width : integer) : string
+
+ -- {K:V} vs T ?
+ record tablerow<K,V>
+ width : integer | string -- generate an 1..100 % enum?
+ title : string
+ mapper : function (V, {K:V}) : string
+ key : K
+ enum alignments
+ "left"
+ "right"
+ end
+ align : alignments
+ end
+ type getrow = function<K,V> ({ K : V }) : string
+ table : function<K,V> ({ tablerow<K,V> }, width : integer) : getrow<K,V>
+end
+
+return lib
diff --git a/teal-src/prosody/util/human/units.d.tl b/teal-src/prosody/util/human/units.d.tl
new file mode 100644
index 00000000..3db17c3a
--- /dev/null
+++ b/teal-src/prosody/util/human/units.d.tl
@@ -0,0 +1,8 @@
+local lib = record
+ enum logbase
+ "b" -- 1024
+ end
+ adjust : function (number, string) : number, string
+ format : function (number, string, logbase) : string
+end
+return lib
diff --git a/teal-src/prosody/util/id.d.tl b/teal-src/prosody/util/id.d.tl
new file mode 100644
index 00000000..4b6c93b7
--- /dev/null
+++ b/teal-src/prosody/util/id.d.tl
@@ -0,0 +1,9 @@
+local record lib
+ short : function () : string
+ medium : function () : string
+ long : function () : string
+ custom : function (integer) : function () : string
+
+end
+return lib
+
diff --git a/teal-src/prosody/util/interpolation.d.tl b/teal-src/prosody/util/interpolation.d.tl
new file mode 100644
index 00000000..fb653edf
--- /dev/null
+++ b/teal-src/prosody/util/interpolation.d.tl
@@ -0,0 +1,6 @@
+local type renderer = function (string, { string : any }) : string
+local type filter = function (string, any) : string
+local record lib
+ new : function (string, string, funcs : { string : filter }) : renderer
+end
+return lib
diff --git a/teal-src/prosody/util/ip.d.tl b/teal-src/prosody/util/ip.d.tl
new file mode 100644
index 00000000..79e901c6
--- /dev/null
+++ b/teal-src/prosody/util/ip.d.tl
@@ -0,0 +1,20 @@
+local record iplib
+ enum protocol
+ "IPv6"
+ "IPv4"
+ end
+ record ip_t
+ addr : string
+ packed : string
+ proto : protocol
+ zone : string
+ end
+
+ new_ip : function (string, protocol) : ip_t
+ commonPrefixLength : function (ip_t, ip_t) : integer
+ parse_cidr : function (string) : ip_t, integer
+ match : function (ip_t, ip_t, integer) : boolean
+ is_ip : function (any) : boolean
+ truncate : function (ip_t, integer) : ip_t
+end
+return iplib
diff --git a/teal-src/prosody/util/jid.d.tl b/teal-src/prosody/util/jid.d.tl
new file mode 100644
index 00000000..897318d9
--- /dev/null
+++ b/teal-src/prosody/util/jid.d.tl
@@ -0,0 +1,15 @@
+local record lib
+ split : function (string) : string, string, string
+ bare : function (string) : string
+ prepped_split : function (string, boolean) : string, string, string
+ join : function (string, string, string) : string
+ prep : function (string, boolean) : string
+ compare : function (string, string) : boolean
+ node : function (string) : string
+ host : function (string) : string
+ resource : function (string) : string
+ escape : function (string) : string
+ unescape : function (string) : string
+end
+
+return lib
diff --git a/teal-src/prosody/util/json.d.tl b/teal-src/prosody/util/json.d.tl
new file mode 100644
index 00000000..a1c25004
--- /dev/null
+++ b/teal-src/prosody/util/json.d.tl
@@ -0,0 +1,18 @@
+local record lib
+ encode : function (any) : string
+ decode : function (string) : any, string
+
+ enum json_type_name
+ "null"
+ "boolean"
+ "object"
+ "array"
+ "number"
+ "string"
+ "integer"
+ end
+
+ type null_type = (nil)
+ null : null_type
+end
+return lib
diff --git a/teal-src/prosody/util/jsonpointer.tl b/teal-src/prosody/util/jsonpointer.tl
new file mode 100644
index 00000000..5ea99732
--- /dev/null
+++ b/teal-src/prosody/util/jsonpointer.tl
@@ -0,0 +1,46 @@
+
+local enum ptr_error
+ "invalid-table"
+ "invalid-path"
+end
+
+local function unescape_token(escaped_token : string) : string
+ local unescaped = escaped_token:gsub("~1", "/"):gsub("~0", "~")
+ return unescaped
+end
+
+local function resolve_json_pointer(ref : any, path : string) : any, ptr_error
+ local ptr_len = #path+1
+ for part, pos in path:gmatch("/([^/]*)()") do
+ local token = unescape_token(part)
+ if not ref is table then
+ return nil
+ end
+ local idx = next(ref)
+ local new_ref : any
+
+ if idx is string then
+ new_ref = ref[token]
+ elseif idx is integer then
+ local i = tonumber(token)
+ if token == "-" then i = #(ref as {any}) + 1 end
+ new_ref = ref[i+1]
+ else
+ return nil, "invalid-table"
+ end
+
+ if pos as integer == ptr_len then
+ return new_ref
+ elseif new_ref is table then
+ ref = new_ref
+ elseif not ref is table then
+ return nil, "invalid-path"
+ end
+
+ end
+ return ref
+end
+
+return {
+ resolve = resolve_json_pointer,
+}
diff --git a/teal-src/prosody/util/jsonschema.tl b/teal-src/prosody/util/jsonschema.tl
new file mode 100644
index 00000000..1cc2314f
--- /dev/null
+++ b/teal-src/prosody/util/jsonschema.tl
@@ -0,0 +1,505 @@
+-- Copyright (C) Kim Alvefur
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+-- Based on
+-- https://json-schema.org/draft/2020-12/json-schema-core.html
+-- https://json-schema.org/draft/2020-12/json-schema-validation.html
+--
+
+if not math.type then require "prosody.util.mathcompat" end
+
+
+local utf8_enc = rawget(_G, "utf8") or require"prosody.util.encodings".utf8;
+local utf8_len = utf8_enc.len or function(s : string) : integer
+ local _, count = s:gsub("[%z\001-\127\194-\253][\128-\191]*", "");
+ return count;
+end;
+
+local json = require "prosody.util.json"
+local null = json.null;
+
+local pointer = require "prosody.util.jsonpointer"
+
+local type json_type_name = json.json_type_name
+
+-- json_type_name here is non-standard
+local type schema_t = boolean | json_schema_object
+
+local record json_schema_object
+ type json_type_name = json.json_type_name
+ type schema_object = json_schema_object
+
+ -- json-schema-core meta stuff
+ ["$schema"] : string
+ ["$vocabulary"] : { string : boolean }
+ ["$id"] : string
+ ["$comment"] : string
+ ["$defs"] : { string : schema_t }
+ ["$anchor"] : string -- NYI
+ ["$dynamicAnchor"] : string -- NYI
+ ["$ref"] : string
+ ["$dynamicRef"] : string -- NYI
+
+ -- combinations
+ allOf : { schema_t }
+ anyOf : { schema_t }
+ oneOf : { schema_t }
+
+ -- conditional logic
+ ["not"] : schema_t
+ ["if"] : schema_t
+ ["then"] : schema_t
+ ["else"] : schema_t
+
+ dependentRequired : { string : { string } }
+
+ -- arrays
+ prefixItems : { schema_t }
+ items : schema_t
+ contains : schema_t
+
+ -- objects
+ properties : { string : schema_t }
+ patternProperties: { string : schema_t } -- NYI
+ additionalProperties: schema_t
+ propertyNames : schema_t
+
+ -- unevaluated
+ unevaluatedItems : schema_t -- NYI
+ unevaluatedProperties : schema_t -- NYI
+
+ -- json-schema-validation
+ type : json_type_name | { json_type_name }
+ enum : { any }
+ const : any
+
+ -- numbers
+ multipleOf : number
+ maximum : number
+ exclusiveMaximum : number
+ minimum : number
+ exclusiveMinimum : number
+
+ -- strings
+ maxLength : integer
+ minLength : integer
+ pattern : string -- NYI
+
+ -- arrays
+ maxItems : integer
+ minItems : integer
+ uniqueItems : boolean
+ maxContains : integer
+ minContains : integer
+
+ -- objects
+ maxProperties : integer -- NYI
+ minProperties : integer -- NYI
+ required : { string }
+ dependentSchemas : { string : schema_t }
+
+ -- semantic format
+ format : string
+
+ -- for Lua
+ luaPatternProperties: { string : schema_t }
+ luaPattern : string
+
+ -- xml
+ record xml_t
+ name : string
+ namespace : string
+ prefix : string
+ attribute : boolean
+ wrapped : boolean
+
+ -- nonstantard, maybe in the future
+ text : boolean
+ x_name_is_value : boolean
+ x_single_attribute : string
+ end
+
+ xml : xml_t
+
+ -- descriptive
+ title : string
+ description : string
+ deprecated : boolean
+ readOnly : boolean
+ writeOnly : boolean
+
+ -- methods
+ validate : function (schema : schema_t, data : any, root : json_schema_object, sloc : string, iloc : string, errs:errors) : boolean, errors
+end
+
+-- TODO validator function per schema property
+
+local function simple_validate(schema : json_type_name | { json_type_name }, data : any) : boolean
+ if schema == nil then
+ return true
+ elseif schema == "object" and data is table then
+ return type(data) == "table" and (next(data)==nil or type((next(data, nil))) == "string")
+ elseif schema == "array" and data is table then
+ return type(data) == "table" and (next(data)==nil or type((next(data, nil))) == "number")
+ elseif schema == "integer" then
+ return math.type(data) == schema
+ elseif schema == "null" then
+ return data == null
+ elseif schema is { json_type_name } then
+ for _, one in ipairs(schema as { json_type_name }) do
+ if simple_validate(one, data) then
+ return true
+ end
+ end
+ return false
+ else
+ return type(data) == schema
+ end
+end
+
+local record validation_error
+ instanceLocation : string
+ schemaLocation : string
+ error : string
+end
+local type errors = { validation_error }
+local function mkerr(sloc:string,iloc:string,err:string) : validation_error
+ return { schemaLocation = sloc; instanceLocation = iloc; error = err };
+end
+
+local function validate (schema : schema_t, data : any, root : json_schema_object, sloc : string, iloc : string, errs:errors) : boolean, errors
+ if schema is boolean then
+ return schema
+ end
+
+ if root == nil then
+ root = schema as json_schema_object
+ iloc = ""
+ sloc = ""
+ errs = {};
+ end
+
+ if schema["$ref"] and schema["$ref"]:sub(1,1) == "#" then
+ local referenced = pointer.resolve(root as table, schema["$ref"]:sub(2)) as schema_t
+ if referenced ~= nil and referenced ~= root and referenced ~= schema then
+ if not validate(referenced, data, root, schema["$ref"], iloc, errs) then
+ table.insert(errs, mkerr(sloc.."/$ref", iloc, "Subschema failed validation"))
+ return false, errs;
+ end
+ end
+ end
+
+ if not simple_validate(schema.type, data) then
+ table.insert(errs, mkerr(sloc.."/type", iloc, "unexpected type"));
+ return false, errs;
+ end
+
+ if schema.type == "object" then
+ if data is table then
+ -- just check that there the keys are all strings
+ for k in pairs(data) do
+ if not k is string then
+ table.insert(errs, mkerr(sloc.."/type", iloc, "'object' had non-string keys"));
+ return false, errs;
+ end
+ end
+ end
+ end
+
+ if schema.type == "array" then
+ if data is table then
+ -- just check that there the keys are all numbers
+ for i in pairs(data) do
+ if not i is integer then
+ table.insert(errs, mkerr(sloc.."/type", iloc, "'array' had non-integer keys"));
+ return false, errs;
+ end
+ end
+ end
+ end
+
+ if schema["enum"] ~= nil then
+ local match = false
+ for _, v in ipairs(schema["enum"]) do
+ if v == data then
+ -- FIXME supposed to do deep-compare
+ match = true
+ break
+ end
+ end
+ if not match then
+ table.insert(errs, mkerr(sloc.."/enum", iloc, "not one of the enumerated values"));
+ return false, errs;
+ end
+ end
+
+ -- XXX this is measured in byte, while JSON measures in ... bork
+ -- TODO use utf8.len?
+ if data is string then
+ if schema.maxLength and utf8_len(data) > schema.maxLength then
+ table.insert(errs, mkerr(sloc.."/maxLength", iloc, "string too long"))
+ return false, errs;
+ end
+ if schema.minLength and utf8_len(data) < schema.minLength then
+ table.insert(errs, mkerr(sloc.."/maxLength", iloc, "string too short"))
+ return false, errs;
+ end
+ if schema.luaPattern and not data:match(schema.luaPattern) then
+ table.insert(errs, mkerr(sloc.."/luaPattern", iloc, "string does not match pattern"))
+ return false, errs;
+ end
+ end
+
+ if data is number then
+ if schema.multipleOf and (data == 0 or data % schema.multipleOf ~= 0) then
+ table.insert(errs, mkerr(sloc.."/luaPattern", iloc, "not a multiple"))
+ return false, errs;
+ end
+
+ if schema.maximum and not ( data <= schema.maximum ) then
+ table.insert(errs, mkerr(sloc.."/maximum", iloc, "number exceeds maximum"))
+ return false, errs;
+ end
+
+ if schema.exclusiveMaximum and not ( data < schema.exclusiveMaximum ) then
+ table.insert(errs, mkerr(sloc.."/exclusiveMaximum", iloc, "number exceeds exclusive maximum"))
+ return false, errs;
+ end
+
+ if schema.minimum and not ( data >= schema.minimum ) then
+ table.insert(errs, mkerr(sloc.."/minimum", iloc, "number below minimum"))
+ return false, errs;
+ end
+
+ if schema.exclusiveMinimum and not ( data > schema.exclusiveMinimum ) then
+ table.insert(errs, mkerr(sloc.."/exclusiveMinimum", iloc, "number below exclusive minimum"))
+ return false, errs;
+ end
+ end
+
+ if schema.allOf then
+ for i, sub in ipairs(schema.allOf) do
+ if not validate(sub, data, root, sloc.."/allOf/"..i, iloc, errs) then
+ table.insert(errs, mkerr(sloc.."/allOf", iloc, "did not match all subschemas"))
+ return false, errs;
+ end
+ end
+ end
+
+ if schema.oneOf then
+ local valid = 0
+ for i, sub in ipairs(schema.oneOf) do
+ if validate(sub, data, root, sloc.."/oneOf"..i, iloc, errs) then
+ valid = valid + 1
+ end
+ end
+ if valid ~= 1 then
+ table.insert(errs, mkerr(sloc.."/oneOf", iloc, "did not match exactly one subschema"))
+ return false, errs;
+ end
+ end
+
+ if schema.anyOf then
+ local match = false
+ for i, sub in ipairs(schema.anyOf) do
+ if validate(sub, data, root, sloc.."/anyOf/"..i, iloc, errs) then
+ match = true
+ break
+ end
+ end
+ if not match then
+ table.insert(errs, mkerr(sloc.."/anyOf", iloc, "did not match any subschema"))
+ return false, errs;
+ end
+ end
+
+ if schema["not"] then
+ if validate(schema["not"], data, root, sloc.."/not", iloc, errs) then
+ table.insert(errs, mkerr(sloc.."/not", iloc, "did match subschema"))
+ return false, errs;
+ end
+ end
+
+ if schema["if"] ~= nil then
+ if validate(schema["if"], data, root, sloc.."/if", iloc, errs) then
+ if schema["then"] then
+ if not validate(schema["then"], data, root, sloc.."/then", iloc, errs) then
+ table.insert(errs, mkerr(sloc.."/then", iloc, "did not match subschema"))
+ return false, errs;
+ end
+ end
+ else
+ if schema["else"] then
+ if not validate(schema["else"], data, root, sloc.."/else", iloc, errs) then
+ table.insert(errs, mkerr(sloc.."/else", iloc, "did not match subschema"))
+ return false, errs;
+ end
+ end
+ end
+ end
+
+ if schema.const ~= nil and schema.const ~= data then
+ table.insert(errs, mkerr(sloc.."/const", iloc, "did not match constant value"))
+ return false, errs;
+ end
+
+ if data is table then
+ -- tables combine object and array behavior, thus we do both kinds of
+ -- validations in this block, which could be useful for validating Lua
+ -- tables
+
+ if schema.maxItems and #(data as {any}) > schema.maxItems then
+ table.insert(errs, mkerr(sloc.."/maxItems", iloc, "too many items"))
+ return false, errs;
+ end
+
+ if schema.minItems and #(data as {any}) < schema.minItems then
+ table.insert(errs, mkerr(sloc.."/minItems", iloc, "too few items"))
+ return false, errs;
+ end
+
+ if schema.required then
+ for _, k in ipairs(schema.required) do
+ if data[k] == nil then
+ table.insert(errs, mkerr(sloc.."/required", iloc.."/"..tostring(k), "missing required property"))
+ return false, errs;
+ end
+ end
+ end
+
+ if schema.dependentRequired then
+ for k, reqs in pairs(schema.dependentRequired) do
+ if data[k] ~= nil then
+ for _, req in ipairs(reqs) do
+ if data[req] == nil then
+ table.insert(errs, mkerr(sloc.."/dependentRequired", iloc, "missing dependent required property"))
+ return false, errs;
+ end
+ end
+ end
+ end
+ end
+
+ if schema.propertyNames ~= nil then
+ -- could be used to validate non-string keys of Lua tables
+ for k in pairs(data) do
+ if not validate(schema.propertyNames, k, root, sloc.."/propertyNames", iloc.."/"..tostring(k), errs) then
+ table.insert(errs, mkerr(sloc.."/propertyNames", iloc.."/"..tostring(k), "a property name did not match subschema"))
+ return false, errs;
+ end
+ end
+ end
+
+ -- additionalProperties applies to properties not validated by properties
+ -- or patternProperties, so we must keep track of properties validated by
+ -- the later
+ local seen_properties : { string : boolean } = {}
+
+ if schema.properties then
+ for k, sub in pairs(schema.properties) do
+ if data[k] ~= nil and not validate(sub, data[k], root, sloc.."/"..tostring(k), iloc.."/"..tostring(k), errs) then
+ table.insert(errs, mkerr(sloc.."/"..tostring(k), iloc.."/"..tostring(k), "a property did not match subschema"))
+ return false, errs;
+ end
+ seen_properties[k] = true
+ end
+ end
+
+ if schema.luaPatternProperties then
+ -- like patternProperties, but Lua patterns
+ for pattern, sub in pairs(schema.luaPatternProperties) do
+ for k in pairs(data) do
+ if k is string and k:match(pattern) then
+ if not validate(sub, data[k], root, sloc.."/luaPatternProperties", iloc, errs) then
+ table.insert(errs, mkerr(sloc.."/luaPatternProperties/"..pattern, iloc.."/"..tostring(k), "a property did not match subschema"))
+ return false, errs;
+ end
+ seen_properties[k] = true
+ end
+ end
+ end
+ end
+
+ if schema.additionalProperties ~= nil then
+ for k, v in pairs(data) do
+ if not seen_properties[k as string] then
+ if not validate(schema.additionalProperties, v, root, sloc.."/additionalProperties", iloc.."/"..tostring(k), errs) then
+ table.insert(errs, mkerr(sloc.."/additionalProperties", iloc.."/"..tostring(k), "additional property did not match subschema"))
+ return false, errs;
+ end
+ end
+ end
+ end
+
+ if schema.dependentSchemas then
+ for k, sub in pairs(schema.dependentSchemas) do
+ if data[k] ~= nil and not validate(sub, data, root, sloc.."/dependentSchemas/"..k, iloc, errs) then
+ table.insert(errs, mkerr(sloc.."/dependentSchemas", iloc.."/"..tostring(k), "did not match dependent subschema"))
+ return false, errs;
+ end
+ end
+ end
+
+ if schema.uniqueItems then
+ -- only works for scalars, would need to deep-compare for objects/arrays/tables
+ local values : { any : boolean } = {}
+ for _, v in pairs(data) do
+ if values[v] then
+ table.insert(errs, mkerr(sloc.."/uniqueItems", iloc, "had duplicate items"))
+ return false, errs;
+ end
+ values[v] = true
+ end
+ end
+
+ local p = 0
+ if schema.prefixItems ~= nil then
+ for i, s in ipairs(schema.prefixItems) do
+ if data[i] == nil then
+ break
+ elseif validate(s, data[i], root, sloc.."/prefixItems/"..i, iloc.."/"..i, errs) then
+ p = i
+ else
+ table.insert(errs, mkerr(sloc.."/prefixItems/"..i, iloc.."/"..tostring(i), "did not match subschema"))
+ return false, errs;
+ end
+ end
+ end
+
+ if schema.items ~= nil then
+ for i = p+1, #(data as {any}) do
+ if not validate(schema.items, data[i], root, sloc, iloc.."/"..i, errs) then
+ table.insert(errs, mkerr(sloc.."/prefixItems/"..i, iloc.."/"..i, "did not match subschema"))
+ return false, errs;
+ end
+ end
+ end
+
+ if schema.contains ~= nil then
+ local found = 0
+ for i = 1, #(data as {any}) do
+ if validate(schema.contains, data[i], root, sloc.."/contains", iloc.."/"..i, errs) then
+ found = found + 1
+ else
+ table.insert(errs, mkerr(sloc.."/contains", iloc.."/"..i, "did not match subschema"))
+ end
+ end
+ if found < (schema.minContains or 1) then
+ table.insert(errs, mkerr(sloc.."/minContains", iloc, "too few matches"))
+ return false, errs;
+ elseif found > (schema.maxContains or math.huge) then
+ table.insert(errs, mkerr(sloc.."/maxContains", iloc, "too many matches"))
+ return false, errs;
+ end
+ end
+ end
+
+ return true;
+end
+
+
+json_schema_object.validate = validate;
+
+return json_schema_object;
diff --git a/teal-src/prosody/util/jwt.d.tl b/teal-src/prosody/util/jwt.d.tl
new file mode 100644
index 00000000..b3d0cd9e
--- /dev/null
+++ b/teal-src/prosody/util/jwt.d.tl
@@ -0,0 +1,38 @@
+local crypto = require "prosody.util.crypto"
+local record jwtlib
+ enum algorithm
+ "HS256"
+ "HS384"
+ "HS512"
+ "ES256"
+ "ES512"
+ "RS256"
+ "RS384"
+ "RS512"
+ "PS256"
+ "PS384"
+ "PS512"
+ end
+ type payload = { string : any }
+ type signer_t = function (payload : payload) : string
+ type verifier_t = function (token : string) : payload
+ enum key_type
+ "rsaEncryption"
+ "id-ecPublicKey"
+ end
+ record algorithm_t
+ sign : signer_t
+ verify : verifier_t
+ load_key : function (key : string) : crypto.key
+ end
+ init : function (algorithm, private_key : string, public_key : string, table) : signer_t, verifier_t
+ new_signer : function (algorithm, string, table) : signer_t
+ new_verifier : function (algorithm, string, table) : verifier_t
+ _algorithms : {
+ algorithm : algorithm_t
+ }
+ -- Deprecated
+ sign : function (private_key : string, payload) : string
+ verify : function (string) : payload
+end
+return jwtlib
diff --git a/teal-src/prosody/util/logger.d.tl b/teal-src/prosody/util/logger.d.tl
new file mode 100644
index 00000000..db29adfd
--- /dev/null
+++ b/teal-src/prosody/util/logger.d.tl
@@ -0,0 +1,18 @@
+local record util
+ enum loglevel
+ "debug"
+ "info"
+ "warn"
+ "error"
+ end
+ type logger = function ( loglevel, string, ...:any )
+ type sink = function ( string, loglevel, string, ...:any )
+ type simple_sink = function ( string, loglevel, string )
+ init : function ( string ) : logger
+ make_logger : function ( string, loglevel ) : function ( string, ...:any )
+ reset : function ()
+ add_level_sink : function ( loglevel, sink )
+ add_simple_sink : function ( simple_sink, { loglevel } )
+end
+
+return util
diff --git a/teal-src/prosody/util/mathcompat.tl b/teal-src/prosody/util/mathcompat.tl
new file mode 100644
index 00000000..1e3f9bab
--- /dev/null
+++ b/teal-src/prosody/util/mathcompat.tl
@@ -0,0 +1,15 @@
+if not math.type then
+ local enum number_subtype
+ "float" "integer"
+ end
+ local function math_type(t:any) : number_subtype
+ if t is number then
+ if t % 1 == 0 and t ~= t+1 and t ~= t-1 then
+ return "integer"
+ else
+ return "float"
+ end
+ end
+ end
+ _G.math.type = math_type
+end
diff --git a/teal-src/prosody/util/net.d.tl b/teal-src/prosody/util/net.d.tl
new file mode 100644
index 00000000..1040fcef
--- /dev/null
+++ b/teal-src/prosody/util/net.d.tl
@@ -0,0 +1,13 @@
+
+local enum type_strings
+ "both"
+ "ipv4"
+ "ipv6"
+end
+
+local record lib
+ local_addresses : function (type_strings, boolean) : { string }
+ pton : function (string):string
+ ntop : function (string):string
+end
+return lib
diff --git a/teal-src/prosody/util/poll.d.tl b/teal-src/prosody/util/poll.d.tl
new file mode 100644
index 00000000..8df56d57
--- /dev/null
+++ b/teal-src/prosody/util/poll.d.tl
@@ -0,0 +1,31 @@
+local record state
+ enum waiterr
+ "timeout"
+ "signal"
+ end
+ add : function (state, integer, boolean, boolean) : boolean
+ add : function (state, integer, boolean, boolean) : nil, string, integer
+ set : function (state, integer, boolean, boolean) : boolean
+ set : function (state, integer, boolean, boolean) : nil, string, integer
+ del : function (state, integer) : boolean
+ del : function (state, integer) : nil, string, integer
+ wait : function (state, integer) : integer, boolean, boolean
+ wait : function (state, integer) : nil, string, integer
+ wait : function (state, integer) : nil, waiterr
+ getfd : function (state) : integer
+end
+
+local record lib
+ new : function () : state
+ EEXIST : integer
+ EMFILE : integer
+ ENOENT : integer
+ enum api_backend
+ "epoll"
+ "poll"
+ "select"
+ end
+ api : api_backend
+end
+
+return lib
diff --git a/teal-src/prosody/util/pposix.d.tl b/teal-src/prosody/util/pposix.d.tl
new file mode 100644
index 00000000..d7780835
--- /dev/null
+++ b/teal-src/prosody/util/pposix.d.tl
@@ -0,0 +1,109 @@
+local record pposix
+ enum syslog_facility
+ "auth"
+ "authpriv"
+ "cron"
+ "daemon"
+ "ftp"
+ "kern"
+ "local0"
+ "local1"
+ "local2"
+ "local3"
+ "local4"
+ "local5"
+ "local6"
+ "local7"
+ "lpr"
+ "mail"
+ "syslog"
+ "user"
+ "uucp"
+ end
+
+ enum syslog_level
+ "debug"
+ "info"
+ "notice"
+ "warn"
+ "error"
+ end
+
+ enum ulimit_resource
+ "CORE"
+ "CPU"
+ "DATA"
+ "FSIZE"
+ "NOFILE"
+ "STACK"
+ "MEMLOCK"
+ "NPROC"
+ "RSS"
+ "NICE"
+ end
+
+ enum ulimit_unlimited
+ "unlimited"
+ end
+
+ type ulimit_limit = integer | ulimit_unlimited
+
+ record utsname
+ sysname : string
+ nodename : string
+ release : string
+ version : string
+ machine : string
+ domainname : string
+ end
+
+ record memoryinfo
+ allocated : integer
+ allocated_mmap : integer
+ used : integer
+ unused : integer
+ returnable : integer
+ end
+
+ abort : function ()
+
+ daemonize : function () : boolean, string
+
+ syslog_open : function (ident : string, facility : syslog_facility)
+ syslog_close : function ()
+ syslog_log : function (level : syslog_level, src : string, msg : string)
+ syslog_setminlevel : function (level : syslog_level)
+
+ getpid : function () : integer
+ getuid : function () : integer
+ getgid : function () : integer
+
+ setuid : function (uid : integer | string) : boolean, string -- string|integer
+ setgid : function (uid : integer | string) : boolean, string
+ initgroups : function (user : string, gid : integer) : boolean, string
+
+ umask : function (umask : string) : string
+
+ mkdir : function (dir : string) : boolean, string
+
+ setrlimit : function (resource : ulimit_resource, soft : ulimit_limit, hard : ulimit_limit) : boolean, string
+ getrlimit : function (resource : ulimit_resource) : boolean, ulimit_limit, ulimit_limit
+ getrlimit : function (resource : ulimit_resource) : boolean, string
+
+ uname : function () : utsname
+
+ setenv : function (key : string, value : string) : boolean
+
+ meminfo : function () : memoryinfo
+
+ atomic_append : function (f : FILE, s : string) : boolean, string, integer
+ remove_blocks : function (f : FILE, integer, integer)
+
+ isatty : function(FILE) : boolean
+
+ ENOENT : integer
+ _NAME : string
+ _VESRION : string
+end
+
+return pposix
diff --git a/teal-src/prosody/util/promise.d.tl b/teal-src/prosody/util/promise.d.tl
new file mode 100644
index 00000000..d895a828
--- /dev/null
+++ b/teal-src/prosody/util/promise.d.tl
@@ -0,0 +1,22 @@
+
+local record lib
+ type resolve_func = function (any)
+ type promise_body = function (resolve_func, resolve_func)
+
+ record Promise<A, B>
+ type on_resolved = function (A) : any
+ type on_rejected = function (B) : any
+ next : function (Promise, on_resolved, on_rejected) : Promise<any, any>
+ end
+
+ new : function (promise_body) : Promise
+ resolve : function (any) : Promise
+ reject : function (any) : Promise
+ all : function ({ Promise }) : Promise
+ all_settled : function ({ Promise }) : Promise
+ race : function ({ Promise }) : Promise
+ try : function
+ is_promise : function(any) : boolean
+end
+
+return lib
diff --git a/teal-src/prosody/util/queue.d.tl b/teal-src/prosody/util/queue.d.tl
new file mode 100644
index 00000000..cb8458e7
--- /dev/null
+++ b/teal-src/prosody/util/queue.d.tl
@@ -0,0 +1,21 @@
+local record lib
+ record queue<T>
+ size : integer
+ count : function (queue<T>) : integer
+ enum push_errors
+ "queue full"
+ end
+
+ push : function (queue<T>, T) : boolean, push_errors
+ pop : function (queue<T>) : T
+ peek : function (queue<T>) : T
+ replace : function (queue<T>, T) : boolean, push_errors
+ type iterator = function (T, integer) : integer, T
+ items : function (queue<T>) : iterator, T, integer
+ type consume_iter = function (queue<T>) : T
+ consume : function (queue<T>) : consume_iter
+ end
+
+ new : function<T> (size:integer, allow_wrapping:boolean) : queue<T>
+end
+return lib;
diff --git a/teal-src/prosody/util/random.d.tl b/teal-src/prosody/util/random.d.tl
new file mode 100644
index 00000000..83ff2fcc
--- /dev/null
+++ b/teal-src/prosody/util/random.d.tl
@@ -0,0 +1,4 @@
+local record lib
+ bytes : function (n:integer):string
+end
+return lib
diff --git a/teal-src/prosody/util/ringbuffer.d.tl b/teal-src/prosody/util/ringbuffer.d.tl
new file mode 100644
index 00000000..e4726d68
--- /dev/null
+++ b/teal-src/prosody/util/ringbuffer.d.tl
@@ -0,0 +1,20 @@
+local record lib
+ record ringbuffer
+ find : function (ringbuffer, string) : integer
+ discard : function (ringbuffer, integer) : boolean
+ read : function (ringbuffer, integer, boolean) : string
+ readuntil : function (ringbuffer, string) : string
+ write : function (ringbuffer, string) : integer
+ size : function (ringbuffer) : integer
+ length : function (ringbuffer) : integer
+ sub : function (ringbuffer, integer, integer) : string
+ byte : function (ringbuffer, integer, integer) : integer...
+ free : function (ringbuffer) : integer
+ end
+
+ new : function (integer) : ringbuffer
+end
+
+return lib
+
+
diff --git a/teal-src/prosody/util/roles.d.tl b/teal-src/prosody/util/roles.d.tl
new file mode 100644
index 00000000..fef4f88a
--- /dev/null
+++ b/teal-src/prosody/util/roles.d.tl
@@ -0,0 +1,32 @@
+local record util_roles
+
+ type context = any
+
+ record Role
+ id : string
+ name : string
+ description : string
+ default : boolean
+ priority : number -- or integer?
+ permissions : { string : boolean }
+
+ may : function (Role, string, context)
+ clone : function (Role, role_config)
+ set_permission : function (Role, string, boolean, boolean)
+ end
+
+ is_role : function (any) : boolean
+
+ record role_config
+ name : string
+ description : string
+ default : boolean
+ priority : number -- or integer?
+ inherits : { Role }
+ permissions : { string : boolean }
+ end
+
+ new : function (role_config, Role) : Role
+end
+
+return util_roles
diff --git a/teal-src/prosody/util/serialization.d.tl b/teal-src/prosody/util/serialization.d.tl
new file mode 100644
index 00000000..528c9705
--- /dev/null
+++ b/teal-src/prosody/util/serialization.d.tl
@@ -0,0 +1,34 @@
+local record _M
+ enum preset
+ "debug"
+ "oneline"
+ "compact"
+ "pretty"
+ end
+ type fallback = function (any, string) : string
+ record config
+ preset : preset
+ fallback : fallback
+ fatal : boolean
+ keywords : { string : boolean }
+ indentwith : string
+ itemstart : string
+ itemsep : string
+ itemlast : string
+ tstart : string
+ tend : string
+ kstart : string
+ kend : string
+ equals : string
+ unquoted : boolean | string
+ hex : string
+ freeze : boolean
+ maxdepth : integer
+ multirefs : boolean
+ table_pairs : function
+ end
+ type serializer = function (any) : string
+ new : function (config|preset) : serializer
+ serialize : function (any, config|preset) : string
+end
+return _M
diff --git a/teal-src/prosody/util/set.d.tl b/teal-src/prosody/util/set.d.tl
new file mode 100644
index 00000000..a23dd258
--- /dev/null
+++ b/teal-src/prosody/util/set.d.tl
@@ -0,0 +1,22 @@
+local record lib
+ record Set<T>
+ add : function<T> (Set<T>, T)
+ contains : function<T> (Set<T>, T) : boolean
+ contains_set : function<T> (Set<T>, Set<T>) : boolean
+ items : function<T> (Set<T>) : function<T> (Set<T>, T) : T
+ remove : function<T> (Set<T>, T)
+ add_list : function<T> (Set<T>, { T })
+ include : function<T> (Set<T>, Set<T>)
+ exclude : function<T> (Set<T>, Set<T>)
+ empty : function<T> (Set<T>) : boolean
+ end
+
+ new : function<T> ({ T }) : Set<T>
+ is_set : function (any) : boolean
+ union : function<T> (Set<T>, Set<T>) : Set <T>
+ difference : function<T> (Set<T>, Set<T>) : Set <T>
+ intersection : function<T> (Set<T>, Set<T>) : Set <T>
+ xor : function<T> (Set<T>, Set<T>) : Set <T>
+end
+
+return lib
diff --git a/teal-src/prosody/util/signal.d.tl b/teal-src/prosody/util/signal.d.tl
new file mode 100644
index 00000000..290cf08f
--- /dev/null
+++ b/teal-src/prosody/util/signal.d.tl
@@ -0,0 +1,41 @@
+local record lib
+ enum Signal
+ "SIGABRT"
+ "SIGALRM"
+ "SIGBUS"
+ "SIGCHLD"
+ "SIGCLD"
+ "SIGCONT"
+ "SIGFPE"
+ "SIGHUP"
+ "SIGILL"
+ "SIGINT"
+ "SIGIO"
+ "SIGIOT"
+ "SIGKILL"
+ "SIGPIPE"
+ "SIGPOLL"
+ "SIGPROF"
+ "SIGQUIT"
+ "SIGSEGV"
+ "SIGSTKFLT"
+ "SIGSTOP"
+ "SIGSYS"
+ "SIGTERM"
+ "SIGTRAP"
+ "SIGTTIN"
+ "SIGTTOU"
+ "SIGURG"
+ "SIGUSR1"
+ "SIGUSR2"
+ "SIGVTALRM"
+ "SIGWINCH"
+ "SIGXCPU"
+ "SIGXFSZ"
+ end
+ signal : function (integer | Signal, function, boolean) : boolean
+ raise : function (integer | Signal)
+ kill : function (integer, integer | Signal)
+ -- enum : integer
+end
+return lib
diff --git a/teal-src/prosody/util/smqueue.tl b/teal-src/prosody/util/smqueue.tl
new file mode 100644
index 00000000..821aee57
--- /dev/null
+++ b/teal-src/prosody/util/smqueue.tl
@@ -0,0 +1,99 @@
+local queue = require "prosody.util.queue";
+
+local record lib
+ -- T would typically be util.stanza
+ record smqueue<T>
+ _queue : queue.queue<T>
+ _head : integer
+ _tail : integer
+
+ enum ack_errors
+ "tail"
+ "head"
+ "pop"
+ end
+ push : function (smqueue, T)
+ ack : function (smqueue, integer) : { T }, ack_errors
+ resumable : function (smqueue<T>) : boolean
+ resume : function (smqueue<T>) : queue.queue.iterator, any, integer
+ type consume_iter = function (smqueue<T>) : T
+ consume : function (smqueue<T>) : consume_iter
+
+ table : function (smqueue<T>) : { T }
+ end
+ new : function <T>(integer) : smqueue<T>
+end
+
+local type smqueue = lib.smqueue;
+
+function smqueue:push(v)
+ self._head = self._head + 1;
+ -- Wraps instead of errors
+ assert(self._queue:push(v));
+end
+
+function smqueue:ack(h : integer) : { any }, smqueue.ack_errors
+ if h < self._tail then
+ return nil, "tail";
+ elseif h > self._head then
+ return nil, "head";
+ end
+ -- TODO optimize? cache table fields
+ local acked = {};
+ self._tail = h;
+ local expect = self._head - self._tail;
+ while expect < self._queue:count() do
+ local v = self._queue:pop();
+ if not v then return nil, "pop"; end
+ table.insert(acked, v);
+ end
+ return acked;
+end
+
+function smqueue:count_unacked() : integer
+ return self._head - self._tail;
+end
+
+function smqueue:count_acked() : integer
+ return self._tail;
+end
+
+function smqueue:resumable() : boolean
+ return self._queue:count() >= (self._head - self._tail);
+end
+
+function smqueue:resume() : queue.queue.iterator, any, integer
+ return self._queue:items();
+end
+
+function smqueue:consume() : queue.queue.consume_iter
+ return self._queue:consume()
+end
+
+-- Compatibility layer, plain ol' table
+function smqueue:table() : { any }
+ local t : { any } = {};
+ for i, v in self:resume() do
+ t[i] = v;
+ end
+ return t;
+end
+
+local function freeze(q : smqueue<any>) : { string:integer }
+ return { head = q._head, tail = q._tail }
+end
+
+local queue_mt = {
+ --
+ __name = "smqueue";
+ __index = smqueue;
+ __len = smqueue.count_unacked;
+ __freeze = freeze;
+}
+
+function lib.new<T>(size : integer) : queue.queue<T>
+ assert(size>0);
+ return setmetatable({ _head = 0; _tail = 0; _queue = queue.new(size, true) }, queue_mt);
+end
+
+return lib;
diff --git a/teal-src/prosody/util/stanza.d.tl b/teal-src/prosody/util/stanza.d.tl
new file mode 100644
index 00000000..e1ab2105
--- /dev/null
+++ b/teal-src/prosody/util/stanza.d.tl
@@ -0,0 +1,145 @@
+local record lib
+
+ type children_iter = function ( stanza_t ) : stanza_t
+ type childtags_iter = function () : stanza_t
+ type maptags_cb = function ( stanza_t ) : stanza_t
+
+
+ enum stanza_error_type
+ "auth"
+ "cancel"
+ "continue"
+ "modify"
+ "wait"
+ end
+ enum stanza_error_condition
+ "bad-request"
+ "conflict"
+ "feature-not-implemented"
+ "forbidden"
+ "gone"
+ "internal-server-error"
+ "item-not-found"
+ "jid-malformed"
+ "not-acceptable"
+ "not-allowed"
+ "not-authorized"
+ "policy-violation"
+ "recipient-unavailable"
+ "redirect"
+ "registration-required"
+ "remote-server-not-found"
+ "remote-server-timeout"
+ "resource-constraint"
+ "service-unavailable"
+ "subscription-required"
+ "undefined-condition"
+ "unexpected-request"
+ end
+
+ record stanza_t
+ name : string
+ attr : { string : string }
+ { stanza_t | string }
+ tags : { stanza_t }
+
+ query : function ( stanza_t, string ) : stanza_t
+ body : function ( stanza_t, string, { string : string } ) : stanza_t
+ text_tag : function ( stanza_t, string, string, { string : string } ) : stanza_t
+ tag : function ( stanza_t, string, { string : string } ) : stanza_t
+ text : function ( stanza_t, string ) : stanza_t
+ up : function ( stanza_t ) : stanza_t
+ at_top : function ( stanza_t ) : boolean
+ reset : function ( stanza_t ) : stanza_t
+ add_direct_child : function ( stanza_t, stanza_t )
+ add_child : function ( stanza_t, stanza_t )
+ remove_children : function ( stanza_t, string, string ) : stanza_t
+
+ get_child : function ( stanza_t, string, string ) : stanza_t
+ get_text : function ( stanza_t ) : string
+ get_child_text : function ( stanza_t, string, string ) : string
+ get_child_attr : function ( stanza_t, string, string ) : string
+ get_child_with_attr : function ( stanza_t, string, string, string, function (string) : boolean ) : string
+ child_with_name : function ( stanza_t, string, string ) : stanza_t
+ child_with_ns : function ( stanza_t, string, string ) : stanza_t
+ children : function ( stanza_t ) : children_iter, stanza_t, integer
+ childtags : function ( stanza_t, string, string ) : childtags_iter
+ maptags : function ( stanza_t, maptags_cb ) : stanza_t
+ find : function ( stanza_t, string ) : stanza_t | string
+
+ top_tag : function ( stanza_t ) : string
+ pretty_print : function ( stanza_t ) : string
+ pretty_top_tag : function ( stanza_t ) : string
+
+ -- FIXME Represent util.error support
+ get_error : function ( stanza_t ) : stanza_error_type, stanza_error_condition, string, stanza_t
+ add_error : function ( stanza_t, stanza_error_type, stanza_error_condition, string, string )
+ indent : function ( stanza_t, integer, string ) : stanza_t
+ end
+
+ record serialized_stanza_t
+ name : string
+ attr : { string : string }
+ { serialized_stanza_t | string }
+ end
+
+ record message_attr
+ ["xml:lang"] : string
+ from : string
+ id : string
+ to : string
+ type : message_type
+ enum message_type
+ "chat"
+ "error"
+ "groupchat"
+ "headline"
+ "normal"
+ end
+ end
+
+ record presence_attr
+ ["xml:lang"] : string
+ from : string
+ id : string
+ to : string
+ type : presence_type
+ enum presence_type
+ "error"
+ "probe"
+ "subscribe"
+ "subscribed"
+ "unsubscribe"
+ "unsubscribed"
+ end
+ end
+
+ record iq_attr
+ ["xml:lang"] : string
+ from : string
+ id : string
+ to : string
+ type : iq_type
+ enum iq_type
+ "error"
+ "get"
+ "result"
+ "set"
+ end
+ end
+
+ stanza : function ( string, { string : string } ) : stanza_t
+ is_stanza : function ( any ) : boolean
+ preserialize : function ( stanza_t ) : serialized_stanza_t
+ deserialize : function ( serialized_stanza_t ) : stanza_t
+ clone : function ( stanza_t, boolean ) : stanza_t
+ message : function ( message_attr, string ) : stanza_t
+ iq : function ( iq_attr ) : stanza_t
+ reply : function ( stanza_t ) : stanza_t
+ error_reply : function ( stanza_t, stanza_error_type, stanza_error_condition, string, string ) : stanza_t
+ presence : function ( presence_attr ) : stanza_t
+ xml_escape : function ( string ) : string
+ pretty_print : function ( string ) : string
+end
+
+return lib
diff --git a/teal-src/prosody/util/strbitop.d.tl b/teal-src/prosody/util/strbitop.d.tl
new file mode 100644
index 00000000..86577ef2
--- /dev/null
+++ b/teal-src/prosody/util/strbitop.d.tl
@@ -0,0 +1,7 @@
+local record mod
+ sand : function (string, string) : string
+ sor : function (string, string) : string
+ sxor : function (string, string) : string
+ common_prefix_bits : function (string, string) : integer
+end
+return mod
diff --git a/teal-src/prosody/util/struct.d.tl b/teal-src/prosody/util/struct.d.tl
new file mode 100644
index 00000000..201aaa23
--- /dev/null
+++ b/teal-src/prosody/util/struct.d.tl
@@ -0,0 +1,6 @@
+local record lib
+ pack : function (string, ...:any) : string
+ unpack : function(string, string, integer) : any...
+ size : function(string) : integer
+end
+return lib
diff --git a/teal-src/prosody/util/table.d.tl b/teal-src/prosody/util/table.d.tl
new file mode 100644
index 00000000..67e5d0f0
--- /dev/null
+++ b/teal-src/prosody/util/table.d.tl
@@ -0,0 +1,7 @@
+local record lib
+ create : function (narr:integer, nrec:integer):table
+ pack : function (...:any):{any}
+ move : function (table, integer, integer, integer, table) : table
+end
+return lib
+
diff --git a/teal-src/prosody/util/termcolours.d.tl b/teal-src/prosody/util/termcolours.d.tl
new file mode 100644
index 00000000..226259aa
--- /dev/null
+++ b/teal-src/prosody/util/termcolours.d.tl
@@ -0,0 +1,7 @@
+local record lib
+ getstring : function (string, string) : string
+ getstyle : function (...:string) : string
+ setstyle : function (string) : string
+ tohtml : function (string) : string
+end
+return lib
diff --git a/teal-src/prosody/util/time.d.tl b/teal-src/prosody/util/time.d.tl
new file mode 100644
index 00000000..e159706b
--- /dev/null
+++ b/teal-src/prosody/util/time.d.tl
@@ -0,0 +1,6 @@
+
+local record lib
+ now : function () : number
+ monotonic : function () : number
+end
+return lib
diff --git a/teal-src/prosody/util/timer.d.tl b/teal-src/prosody/util/timer.d.tl
new file mode 100644
index 00000000..a6394cf3
--- /dev/null
+++ b/teal-src/prosody/util/timer.d.tl
@@ -0,0 +1,8 @@
+local record util_timer
+ record task end
+ type timer_callback = function (number) : number
+ add_task : function ( number, timer_callback, any ) : task
+ stop : function ( task )
+ reschedule : function ( task, number ) : task
+end
+return util_timer
diff --git a/teal-src/prosody/util/uuid.d.tl b/teal-src/prosody/util/uuid.d.tl
new file mode 100644
index 00000000..284a4e4c
--- /dev/null
+++ b/teal-src/prosody/util/uuid.d.tl
@@ -0,0 +1,8 @@
+local record lib
+ get_nibbles : function (number) : string
+ generate : function () : string
+
+ seed : function (string)
+end
+return lib
+
diff --git a/teal-src/prosody/util/xtemplate.tl b/teal-src/prosody/util/xtemplate.tl
new file mode 100644
index 00000000..4d293359
--- /dev/null
+++ b/teal-src/prosody/util/xtemplate.tl
@@ -0,0 +1,108 @@
+-- render(template, stanza) --> string
+-- {path} --> stanza:find(path)
+-- {{ns}name/child|each({ns}name){sub-template}}
+
+--[[
+template ::= "{" path ("|" name ("(" args ")")? (template)? )* "}"
+path ::= defined by util.stanza
+name ::= %w+
+args ::= anything with balanced ( ) pairs
+]]
+
+local s_gsub = string.gsub;
+local s_match = string.match;
+local s_sub = string.sub;
+local t_concat = table.concat;
+
+local st = require "prosody.util.stanza";
+
+local type escape_t = function (string) : string
+local type filter_t = function (string | st.stanza_t, string | st.stanza_t, string) : string | st.stanza_t, boolean
+local type filter_coll = { string : filter_t }
+
+local function render(template : string, root : st.stanza_t, escape : escape_t, filters : filter_coll) : string
+ escape = escape or st.xml_escape;
+
+ return (s_gsub(template, "(%s*)(%b{})(%s*)", function(pre_blank : string, block : string, post_blank : string) : string
+ local inner = s_sub(block, 2, -2);
+ if inner:sub(1, 1) == "-" then
+ pre_blank = "";
+ inner = inner:sub(2);
+ end
+ if inner:sub(-1, -1) == "-" then
+ post_blank = "";
+ inner = inner:sub(1, -2);
+ end
+ local path, pipe, pos = s_match(inner, "^([^|]+)(|?)()");
+ if not path is string then return end
+ local value : string | st.stanza_t
+ if path == "." then
+ value = root;
+ elseif path == "#" then
+ value = root:get_text();
+ else
+ value = root:find(path);
+ end
+ local is_escaped = false;
+
+ while pipe == "|" do
+ local func, args, tmpl, p = s_match(inner, "^(%w+)(%b())(%b{})()", pos as integer);
+ if not func then func, args, p = s_match(inner, "^(%w+)(%b())()", pos as integer); end
+ if not func then func, tmpl, p = s_match(inner, "^(%w+)(%b{})()", pos as integer); end
+ if not func then func, p = s_match(inner, "^(%w+)()", pos as integer); end
+ if not func then break end
+ if tmpl then tmpl = s_sub(tmpl, 2, -2); end
+ if args then args = s_sub(args, 2, -2); end
+
+ if func == "each" and tmpl then
+ if not st.is_stanza(value) then
+ return pre_blank..post_blank;
+ end
+ if not args then value, args = root, path; end
+ local ns, name = s_match(args, "^(%b{})(.*)$");
+ if ns then ns = s_sub(ns, 2, -2); else name, ns = args, nil; end
+ if ns == "" then ns = nil; end
+ if name == "" then name = nil; end
+ local out, i = {}, 1;
+ for c in (value as st.stanza_t):childtags(name, ns) do
+ out[i], i = render(tmpl, c, escape, filters), i + 1;
+ end
+ value = t_concat(out);
+ is_escaped = true;
+ elseif func == "and" and tmpl then
+ local condition = value;
+ if args then condition = root:find(args); end
+ if condition then
+ value = render(tmpl, root, escape, filters);
+ is_escaped = true;
+ end
+ elseif func == "or" and tmpl then
+ local condition = value;
+ if args then condition = root:find(args); end
+ if not condition then
+ value = render(tmpl, root, escape, filters);
+ is_escaped = true;
+ end
+ elseif filters and filters[func] then
+ local f = filters[func];
+ value, is_escaped = f(value, args, tmpl);
+ else
+ error("No such filter function: " .. func);
+ end
+ pipe, pos = s_match(inner, "^(|?)()", p as integer);
+ end
+
+ if value is string then
+ if not is_escaped then value = escape(value); end
+ return pre_blank .. value .. post_blank;
+ elseif st.is_stanza(value) then
+ value = value:get_text();
+ if value then
+ return pre_blank .. escape(value) .. post_blank;
+ end
+ end
+ return pre_blank .. post_blank;
+ end));
+end
+
+return { render = render };