util/stanza.lua

Fri, 22 Aug 2008 21:09:04 +0000

author
matthew
date
Fri, 22 Aug 2008 21:09:04 +0000
changeset 0
3e3171b59028
child 1
b8787e859fd2
permissions
-rw-r--r--

First commit, where do you want to go tomorrow?

0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
1 local t_insert = table.insert;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
2 local t_remove = table.remove;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
3 local format = string.format;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
4 local tostring = tostring;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
5 local setmetatable= setmetatable;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
6 local pairs = pairs;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
7 local ipairs = ipairs;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
8
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
9 module "stanza"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
10
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
11 stanza_mt = {};
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
12 stanza_mt.__index = stanza_mt;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
13
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
14 function stanza(name, attr)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
15 local stanza = { name = name, attr = attr or {}, last_add = {}};
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
16 return setmetatable(stanza, stanza_mt);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
17 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
18
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
19 function stanza_mt:iq(attrs)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
20 return self + stanza("iq", attrs)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
21 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
22 function stanza_mt:message(attrs)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
23 return self + stanza("message", attrs)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
24 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
25 function stanza_mt:presence(attrs)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
26 return self + stanza("presence", attrs)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
27 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
28 function stanza_mt:query(xmlns)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
29 return self:tag("query", { xmlns = xmlns });
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
30 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
31 function stanza_mt:tag(name, attrs)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
32 local s = stanza(name, attrs);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
33 (self.last_add[#self.last_add] or self):add_child(s);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
34 t_insert(self.last_add, s);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
35 return self;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
36 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
37
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
38 function stanza_mt:text(text)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
39 (self.last_add[#self.last_add] or self):add_child(text);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
40 return self;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
41 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
42
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
43 function stanza_mt:up()
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
44 t_remove(self.last_add);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
45 return self;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
46 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
47
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
48 function stanza_mt:add_child(child)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
49 t_insert(self, child);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
50 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
51
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
52 function stanza_mt:child_with_name(name)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
53 for _, child in ipairs(self) do
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
54 if child.name == name then return child; end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
55 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
56 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
57
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
58 function stanza_mt.__tostring(t)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
59 local children_text = "";
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
60 for n, child in ipairs(t) do
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
61 children_text = children_text .. tostring(child);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
62 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
63
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
64 local attr_string = "";
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
65 if t.attr then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
66 for k, v in pairs(t.attr) do attr_string = attr_string .. format(" %s='%s'", k, tostring(v)); end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
67 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
68
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
69 return format("<%s%s>%s</%s>", t.name, attr_string, children_text, t.name);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
70 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
71
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
72 function stanza_mt.__add(s1, s2)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
73 return s:add_child(s2);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
74 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
75
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
76
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
77 do
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
78 local id = 0;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
79 function new_id()
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
80 id = id + 1;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
81 return "lx"..id;
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
82 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
83 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
84
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
85 function message(attr, body)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
86 if not body then
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
87 return stanza("message", attr);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
88 else
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
89 return stanza("message", attr):tag("body"):text(body);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
90 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
91 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
92 function iq(attr)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
93 if attr and not attr.id then attr.id = new_id(); end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
94 return stanza("iq", attr or { id = new_id() });
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
95 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
96
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
97 function reply(orig)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
98 return stanza(orig.name, orig.attr and { to = orig.attr.from, from = orig.attr.to, id = orig.attr.id, type = ((orig.name == "iq" and "result") or nil) });
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
99 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
100
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
101 function presence(attr)
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
102 return stanza("presence", attr);
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
103 end
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
104

mercurial