Software /
code /
verse
Annotate
util/rsm.lua @ 497:eb86e3751978
util.vcard: Move into verse namespace since it's removed from Prosody
See Prosody rev 79431e0282a9
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 23 Jun 2023 10:12:38 +0200 |
parent | 490:6b2f31da9610 |
rev | line source |
---|---|
490
6b2f31da9610
Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents:
387
diff
changeset
|
1 local stanza = require"prosody.util.stanza".stanza; |
387
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local tostring, tonumber = tostring, tonumber; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local type = type; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local pairs = pairs; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local xmlns_rsm = 'http://jabber.org/protocol/rsm'; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local element_parsers = {}; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 do |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local parsers = element_parsers; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local function xs_int(st) |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 return tonumber((st:get_text())); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local function xs_string(st) |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 return st:get_text(); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 parsers.after = xs_string; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 parsers.before = function(st) |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 local text = st:get_text(); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 return text == "" or text; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 end; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 parsers.max = xs_int; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 parsers.index = xs_int; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 parsers.first = function(st) |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 return { index = tonumber(st.attr.index); st:get_text() }; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 end; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 parsers.last = xs_string; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 parsers.count = xs_int; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 local element_generators = setmetatable({ |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 first = function(st, data) |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 if type(data) == "table" then |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 st:tag("first", { index = data.index }):text(data[1]):up(); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 else |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 st:tag("first"):text(tostring(data)):up(); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 end; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 before = function(st, data) |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 if data == true then |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 st:tag("before"):up(); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 else |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 st:tag("before"):text(tostring(data)):up(); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 }, { |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 __index = function(_, name) |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 return function(st, data) |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 st:tag(name):text(tostring(data)):up(); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 end; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 }); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 local function parse(set) |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 local rs = {}; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 for tag in set:childtags() do |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 local name = tag.name; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 local parser = name and element_parsers[name]; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 if parser then |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 rs[name] = parser(tag); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 return rs; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 local function generate(t) |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 local st = stanza("set", { xmlns = xmlns_rsm }); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 for k,v in pairs(t) do |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 if element_parsers[k] then |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 element_generators[k](st, v); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 return st; |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 local function get(st) |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 local set = st:get_child("set", xmlns_rsm); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 if set and #set.tags > 0 then |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 return parse(set); |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 end |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 |
508bf163502b
Import util.rsm from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 return { parse = parse, generate = generate, get = get }; |