Comparison

util/stanza.lua @ 1:b8787e859fd2

Switched to new connection framework, courtesy of the luadch project Now supports SSL on 5223 Beginning support for presence (aka. the proper routing of stanzas)
author matthew
date Sun, 24 Aug 2008 01:51:02 +0000
parent 0:3e3171b59028
child 2:9bb397205f26
comparison
equal deleted inserted replaced
0:3e3171b59028 1:b8787e859fd2
1 local t_insert = table.insert; 1 local t_insert = table.insert;
2 local t_remove = table.remove; 2 local t_remove = table.remove;
3 local format = string.format; 3 local format = string.format;
4 local tostring = tostring; 4 local tostring = tostring;
5 local setmetatable= setmetatable; 5 local setmetatable = setmetatable;
6 local pairs = pairs; 6 local pairs = pairs;
7 local ipairs = ipairs; 7 local ipairs = ipairs;
8 local type = type;
8 9
9 module "stanza" 10 module "stanza"
10 11
11 stanza_mt = {}; 12 stanza_mt = {};
12 stanza_mt.__index = stanza_mt; 13 stanza_mt.__index = stanza_mt;
13 14
14 function stanza(name, attr) 15 function stanza(name, attr)
15 local stanza = { name = name, attr = attr or {}, last_add = {}}; 16 local stanza = { name = name, attr = attr or {}, tags = {}, last_add = {}};
16 return setmetatable(stanza, stanza_mt); 17 return setmetatable(stanza, stanza_mt);
17 end 18 end
18 19
19 function stanza_mt:iq(attrs) 20 function stanza_mt:iq(attrs)
20 return self + stanza("iq", attrs) 21 return self + stanza("iq", attrs)
44 t_remove(self.last_add); 45 t_remove(self.last_add);
45 return self; 46 return self;
46 end 47 end
47 48
48 function stanza_mt:add_child(child) 49 function stanza_mt:add_child(child)
50 if type(child) == "table" then
51 t_insert(self.tags, child);
52 end
49 t_insert(self, child); 53 t_insert(self, child);
50 end 54 end
51 55
52 function stanza_mt:child_with_name(name) 56 function stanza_mt:child_with_name(name)
53 for _, child in ipairs(self) do 57 for _, child in ipairs(self) do
54 if child.name == name then return child; end 58 if child.name == name then return child; end
55 end 59 end
60 end
61
62 function stanza_mt:children()
63 local i = 0;
64 return function (a)
65 i = i + 1
66 local v = a[i]
67 if v then return v; end
68 end, self, i;
69
56 end 70 end
57 71
58 function stanza_mt.__tostring(t) 72 function stanza_mt.__tostring(t)
59 local children_text = ""; 73 local children_text = "";
60 for n, child in ipairs(t) do 74 for n, child in ipairs(t) do
61 children_text = children_text .. tostring(child); 75 children_text = children_text .. tostring(child);
62 end 76 end
63 77
64 local attr_string = ""; 78 local attr_string = "";
65 if t.attr then 79 if t.attr then
66 for k, v in pairs(t.attr) do attr_string = attr_string .. format(" %s='%s'", k, tostring(v)); end 80 for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. format(" %s='%s'", k, tostring(v)); end end
67 end 81 end
68 82
69 return format("<%s%s>%s</%s>", t.name, attr_string, children_text, t.name); 83 return format("<%s%s>%s</%s>", t.name, attr_string, children_text, t.name);
70 end 84 end
71 85
72 function stanza_mt.__add(s1, s2) 86 function stanza_mt.__add(s1, s2)
73 return s:add_child(s2); 87 return s1:add_child(s2);
74 end 88 end
75 89
76 90
77 do 91 do
78 local id = 0; 92 local id = 0;