verse.lua

Thu, 03 Dec 2020 17:05:27 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 03 Dec 2020 17:05:27 +0000
changeset 0
550f506de75a
permissions
-rw-r--r--

Initial commit

0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 package.preload['util.encodings']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local function a(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local function e()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 error("Function not implemented");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local e=require"mime";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 a"encodings"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 idna={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 stringprep={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 base64={encode=e.b64,decode=e.unb64};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 utf8={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 valid=(utf8 and utf8.len)and function(e)return not not utf8.len(e);end or function()return true;end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 package.preload['util.hashes']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local function e(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 error("Hash method "..e.." not available",2);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local e=setmetatable({},{__index=e});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local function t(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local e,o=pcall(require,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 if e then a(o);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 t("bgcrypto.md5",function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 e.md5=t.digest;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 e.hmac_md5=t.hmac.digest;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 t("bgcrypto.sha1",function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 e.sha1=t.digest;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 e.hmac_sha1=t.hmac.digest;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 e.scram_Hi_sha1=function(a,o,e)return t.pbkdf2(a,o,e,20);end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 t("bgcrypto.sha256",function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 e.sha256=t.digest;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 e.hmac_sha256=t.hmac.digest;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 t("bgcrypto.sha512",function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 e.sha512=t.digest;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 e.hmac_sha512=t.hmac.digest;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 t("sha1",function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 e.sha1=function(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 return t.sha1(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 return(t.binary(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 package.preload['lib.adhoc']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local function d(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local s,i=require"util.stanza",require"util.uuid";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local r="http://jabber.org/protocol/commands";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 local n={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 local h={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 local function o(e,o,a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 local e=s.stanza("command",{xmlns=r,node=e.node,status=o});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 if a then e.attr.sessionid=a;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 if t then e.attr.action=t;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 function h.new(t,i,e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 return{name=t,node=i,handler=e,cmdtag=o,permission=(a or"user")};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 function h.handle_cmd(o,h,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 local e=t.tags[1].attr.sessionid or i.generate();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 a.to=t.attr.to;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 a.from=t.attr.from;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 a.action=t.tags[1].attr.action or"execute";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 a.form=t.tags[1]:child_with_ns("jabber:x:data");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 local a,i=o:handler(a,n[e]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 n[e]=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 local i=s.reply(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 if a.status=="completed"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 n[e]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 t=o:cmdtag("completed",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 elseif a.status=="canceled"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 n[e]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 t=o:cmdtag("canceled",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 elseif a.status=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 n[e]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 i=s.error_reply(i,a.error.type,a.error.condition,a.error.message);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 h.send(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 t=o:cmdtag("executing",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 for a,e in pairs(a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 if a=="info"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 t:tag("note",{type="info"}):text(e):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 elseif a=="warn"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 t:tag("note",{type="warn"}):text(e):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 elseif a=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 t:tag("note",{type="error"}):text(e.message):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 elseif a=="actions"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local a=s.stanza("actions");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 for i,e in ipairs(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 if(e=="prev")or(e=="next")or(e=="complete")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 a:tag(e):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 d:log("error",'Command "'..o.name..
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 '" at node "'..o.node..'" provided an invalid action "'..e..'"');
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 t:add_child(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 elseif a=="form"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 t:add_child((e.layout or e):form(e.values));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 elseif a=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 t:add_child((e.layout or e):form(e.values,"result"));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 elseif a=="other"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 t:add_child(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 i:add_child(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 h.send(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 return h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 package.preload['util.stanza']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 local i=error;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 local t=table.insert;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 local l=table.remove;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 local p=table.concat;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 local r=string.format;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 local c=string.match;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 local f=tostring;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 local w=setmetatable;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 local q=getmetatable;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 local n=pairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 local s=ipairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 local a=type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 local j=string.gsub;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 local m=string.sub;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 local u=string.find;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 local e=os;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 local h=require"util.encodings".utf8.valid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 local y=not e.getenv("WINDIR");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 local d,o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 if y then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 local t,e=pcall(require,"util.termcolours");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 d,o=e.getstyle,e.getstring;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 y=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 local v="urn:ietf:params:xml:ns:xmpp-stanzas";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 local _ENV=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 local e={__name="stanza"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 e.__index=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 local function g(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 if a(e)~="string"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 i("invalid "..t.." name: expected string, got "..a(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 elseif#e==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 i("invalid "..t.." name: empty string");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 elseif u(e,"[<>& '\"]")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 i("invalid "..t.." name: contains invalid characters");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 elseif not h(e)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 i("invalid "..t.." name: contains invalid utf8");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 local function b(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 if a(e)~="string"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 i("invalid "..t.." value: expected string, got "..a(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 elseif not h(e)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 i("invalid "..t.." value: contains invalid utf8");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 local function k(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 if e~=nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 if a(e)~="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 i("invalid attributes, expected table got "..a(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 for t,e in n(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 g(t,"attribute");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 b(e,"attribute");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 if a(e)~="string"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 i("invalid attribute value for '"..t.."': expected string, got "..a(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 elseif not h(e)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 i("invalid attribute value for '"..t.."': contains invalid utf8");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 local function h(t,a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 g(t,"tag");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 k(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 local t={name=t,attr=a or{},namespaces=o,tags={}};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 return w(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 local function g(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 return q(t)==e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 function e:query(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 return self:tag("query",{xmlns=e});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 function e:body(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 return self:tag("body",e):text(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 function e:text_tag(o,a,t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 return self:tag(o,t,e):text(a):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 function e:tag(e,a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 local a=h(e,a,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 local e=self.last_add;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 if not e then e={};self.last_add=e;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 (e[#e]or self):add_direct_child(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 t(e,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 return self;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 function e:text(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 if t~=nil and t~=""then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 local e=self.last_add;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 (e and e[#e]or self):add_direct_child(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 return self;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 function e:up()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 local e=self.last_add;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 if e then l(e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 return self;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 function e:reset()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 self.last_add=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 return self;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 function e:add_direct_child(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 if g(e)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 t(self.tags,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 t(self,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 b(e,"text");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 t(self,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 function e:add_child(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 local e=self.last_add;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 (e and e[#e]or self):add_direct_child(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 return self;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 function e:remove_children(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 e=e or self.attr.xmlns;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 return self:maptags(function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 if(not a or t.name==a)and t.attr.xmlns==e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 return nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 function e:get_child(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 for o,e in s(self.tags)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 if(not a or e.name==a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301 and((not t and self.attr.xmlns==e.attr.xmlns)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 or e.attr.xmlns==t)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 function e:get_child_text(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 local e=self:get_child(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 return e:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 return nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314 function e:child_with_name(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 for a,e in s(self.tags)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 if e.name==t then return e;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319 function e:child_with_ns(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 for a,e in s(self.tags)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 if e.attr.xmlns==t then return e;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 function e:children()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
325 local e=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
326 return function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
327 e=e+1
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
328 return t[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
329 end,self,e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
330 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
331 function e:childtags(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
332 local e=self.tags;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
333 local o,i=1,#e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
334 return function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
335 for i=o,i do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
336 local e=e[i];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
337 if(not t or e.name==t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
338 and((not a and self.attr.xmlns==e.attr.xmlns)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
339 or e.attr.xmlns==a)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
340 o=i+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
341 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
342 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
343 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
344 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
345 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
346 function e:maptags(h)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
347 local o,t=self.tags,1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
348 local n,a=#self,#o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
349 local s=n+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
350 local e=1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
351 while t<=a and a>0 do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
352 if self[e]==o[t]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
353 local i=h(self[e]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
354 if i==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
355 l(self,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
356 l(o,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
357 n=n-1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
358 a=a-1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
359 e=e-1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
360 t=t-1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
361 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
362 self[e]=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
363 o[t]=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
364 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
365 t=t+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
366 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
367 e=e+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
368 if e>s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
369 i("Invalid stanza state! Please report this error.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
370 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
371 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
372 return self;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
373 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
374 function e:find(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
375 local e=1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
376 local s=#a+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
377 repeat
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
378 local o,t,i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
379 local n=m(a,e,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
380 if n=="@"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
381 return self.attr[m(a,e+1)];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
382 elseif n=="{"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
383 o,e=c(a,"^([^}]+)}()",e+1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
384 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
385 t,i,e=c(a,"^([^@/#]*)([/#]?)()",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
386 t=t~=""and t or nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
387 if e==s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
388 if i=="#"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
389 return self:get_child_text(t,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
390 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
391 return self:get_child(t,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
392 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
393 self=self:get_child(t,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
394 until not self
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
395 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
396 local i={["'"]="&apos;",["\""]="&quot;",["<"]="&lt;",[">"]="&gt;",["&"]="&amp;"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
397 local function l(e)return(j(e,"['&<>\"]",i));end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
398 local function m(o,e,s,a,r)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
399 local i=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
400 local h=o.name
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
401 t(e,"<"..h);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
402 for o,n in n(o.attr)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
403 if u(o,"\1",1,true)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
404 local o,s=c(o,"^([^\1]*)\1?(.*)$");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
405 i=i+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
406 t(e," xmlns:ns"..i.."='"..a(o).."' ".."ns"..i..":"..s.."='"..a(n).."'");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
407 elseif not(o=="xmlns"and n==r)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
408 t(e," "..o.."='"..a(n).."'");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
409 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
410 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
411 local i=#o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
412 if i==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
413 t(e,"/>");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
414 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
415 t(e,">");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
416 for i=1,i do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
417 local i=o[i];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
418 if i.name then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
419 s(i,e,s,a,o.attr.xmlns);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
420 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
421 t(e,a(i));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
422 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
423 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
424 t(e,"</"..h..">");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
425 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
426 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
427 function e.__tostring(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
428 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
429 m(t,e,m,l,nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
430 return p(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
431 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
432 function e.top_tag(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
433 local t="";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
434 if e.attr then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
435 for e,o in n(e.attr)do if a(e)=="string"then t=t..r(" %s='%s'",e,l(f(o)));end end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
436 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
437 return r("<%s%s>",e.name,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
438 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
439 function e.get_text(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
440 if#e.tags==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
441 return p(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
442 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
443 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
444 function e.get_error(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
445 local o,t,e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
446 local a=a:get_child("error");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
447 if not a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
448 return nil,nil,nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
449 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
450 o=a.attr.type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
451 for o,a in s(a.tags)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
452 if a.attr.xmlns==v then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
453 if not e and a.name=="text"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
454 e=a:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
455 elseif not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
456 t=a.name;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
457 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
458 if t and e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
459 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
460 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
461 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
462 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
463 return o,t or"undefined-condition",e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
464 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
465 local function m(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
466 local i={name=o.name,attr=o.attr};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
467 for o,e in s(o)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
468 if a(e)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
469 t(i,m(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
470 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
471 t(i,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
472 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
473 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
474 return i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
475 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
476 e.__freeze=m;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
477 local function p(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
478 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
479 local i=o.attr;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
480 for e=1,#i do i[e]=nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
481 local h={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
482 for e in n(i)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
483 if u(e,"|",1,true)and not u(e,"\1",1,true)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
484 local a,t=c(e,"^([^|]+)|(.+)$");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
485 h[a.."\1"..t]=i[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
486 i[e]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
487 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
488 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
489 for e,t in n(h)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
490 i[e]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
491 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
492 w(o,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
493 for t,e in s(o)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
494 if a(e)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
495 p(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
496 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
497 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
498 if not o.tags then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
499 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
500 for n,i in s(o)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
501 if a(i)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
502 t(e,i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
503 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
504 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
505 o.tags=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
506 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
507 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
508 return o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
509 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
510 local function u(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
511 local i,h={},{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
512 for t,e in n(a.attr)do i[t]=e;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
513 local s,o=a.namespaces;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
514 if s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
515 o={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
516 for e,t in n(s)do o[e]=t;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
517 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
518 local o={name=a.name,attr=i,namespaces=o,tags=h};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
519 for e=1,#a do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
520 local e=a[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
521 if e.name then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
522 e=u(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
523 t(h,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
524 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
525 t(o,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
526 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
527 return w(o,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
528 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
529 local function w(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
530 if not e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
531 return h("message",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
532 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
533 return h("message",t):tag("body"):text(e):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
534 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
535 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
536 local function b(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
537 if not(e and e.id)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
538 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
539 return h("iq",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
540 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
541 local function c(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
542 return h(e.name,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
543 e.attr and{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
544 to=e.attr.from,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
545 from=e.attr.to,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
546 id=e.attr.id,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
547 type=((e.name=="iq"and"result")or e.attr.type)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
548 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
549 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
550 local t={xmlns=v};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
551 local function v(e,i,o,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
552 local e=c(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
553 e.attr.type="error";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
554 e:tag("error",{type=i})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
555 :tag(o,t):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
556 if a then e:tag("text",t):text(a):up();end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
557 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
558 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
559 local function k(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
560 return h("presence",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
561 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
562 if y then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
563 local i=d("yellow");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
564 local u=d("red");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
565 local h=d("red");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
566 local t=d("magenta");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
567 local i=" "..o(i,"%s")..o(t,"=")..o(u,"'%s'");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
568 local d=o(t,"<")..o(h,"%s").."%s"..o(t,">");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
569 local h=d.."%s"..o(t,"</")..o(h,"%s")..o(t,">");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
570 function e.pretty_print(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
571 local e="";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
572 for o,t in s(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
573 if a(t)=="string"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
574 e=e..l(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
575 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
576 e=e..t:pretty_print();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
577 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
578 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
579 local o="";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
580 if t.attr then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
581 for e,t in n(t.attr)do if a(e)=="string"then o=o..r(i,e,f(t));end end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
582 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
583 return r(h,t.name,o,e,t.name);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
584 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
585 function e.pretty_top_tag(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
586 local e="";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
587 if t.attr then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
588 for t,o in n(t.attr)do if a(t)=="string"then e=e..r(i,t,f(o));end end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
589 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
590 return r(d,t.name,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
591 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
592 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
593 e.pretty_print=e.__tostring;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
594 e.pretty_top_tag=e.top_tag;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
595 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
596 return{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
597 stanza_mt=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
598 stanza=h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
599 is_stanza=g;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
600 preserialize=m;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
601 deserialize=p;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
602 clone=u;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
603 message=w;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
604 iq=b;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
605 reply=c;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
606 error_reply=v;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
607 presence=k;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
608 xml_escape=l;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
609 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
610 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
611 package.preload['util.timer']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
612 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
613 local function u(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
614 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
615 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
616 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
617 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
618 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
619 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
620 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
621 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
622 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
623 local o=require"net.server";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
624 local h=math.min
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
625 local l=math.huge
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
626 local i=require"socket".gettime;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
627 local s=table.insert;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
628 local d=pairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
629 local r=type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
630 local n={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
631 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
632 u"timer"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
633 local e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
634 if not o.event then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
635 function e(t,n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
636 local i=i();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
637 t=t+i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
638 if t>=i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
639 s(a,{t,n});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
640 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
641 local t=n(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
642 if t and r(t)=="number"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
643 return e(t,n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
644 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
645 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
646 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
647 o._addtimer(function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
648 local o=i();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
649 if#a>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
650 for t,e in d(a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
651 s(n,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
652 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
653 a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
654 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
655 local t=l;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
656 for d,a in d(n)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
657 local s,i=a[1],a[2];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
658 if s<=o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
659 n[d]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
660 local a=i(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
661 if r(a)=="number"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
662 e(a,i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
663 t=h(t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
664 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
665 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
666 t=h(t,s-o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
667 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
668 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
669 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
670 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
671 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
672 local t=o.event;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
673 local n=o.event_base;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
674 local o=(t.core and t.core.LEAVE)or-1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
675 function e(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
676 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
677 t=n:addevent(nil,0,function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
678 local e=e(i());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
679 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
680 return 0,e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
681 elseif t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
682 return o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
683 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
684 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
685 ,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
686 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
687 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
688 add_task=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
689 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
690 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
691 package.preload['util.termcolours']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
692 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
693 local function s(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
694 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
695 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
696 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
697 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
698 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
699 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
700 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
701 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
702 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
703 local i,n=table.concat,table.insert;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
704 local t,a=string.char,string.format;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
705 local h=tonumber;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
706 local r=ipairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
707 local d=io.write;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
708 local e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
709 if os.getenv("WINDIR")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
710 e=require"util.windows";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
711 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
712 local o=e and e.get_consolecolor and e.get_consolecolor();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
713 s"termcolours"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
714 local s={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
715 reset=0;bright=1,dim=2,underscore=4,blink=5,reverse=7,hidden=8;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
716 black=30;red=31;green=32;yellow=33;blue=34;magenta=35;cyan=36;white=37;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
717 ["black background"]=40;["red background"]=41;["green background"]=42;["yellow background"]=43;["blue background"]=44;["magenta background"]=45;["cyan background"]=46;["white background"]=47;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
718 bold=1,dark=2,underline=4,underlined=4,normal=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
719 }
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
720 local l={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
721 ["0"]=o,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
722 ["1"]=7+8,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
723 ["1;33"]=2+4+8,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
724 ["1;31"]=4+8
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
725 }
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
726 local u={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
727 [1]="font-weight: bold",[2]="opacity: 0.5",[4]="text-decoration: underline",[8]="visibility: hidden",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
728 [30]="color:black",[31]="color:red",[32]="color:green",[33]="color:#FFD700",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
729 [34]="color:blue",[35]="color: magenta",[36]="color:cyan",[37]="color: white",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
730 [40]="background-color:black",[41]="background-color:red",[42]="background-color:green",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
731 [43]="background-color:yellow",[44]="background-color:blue",[45]="background-color: magenta",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
732 [46]="background-color:cyan",[47]="background-color: white";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
733 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
734 local c=t(27).."[%sm%s"..t(27).."[0m";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
735 function getstring(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
736 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
737 return a(c,t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
738 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
739 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
740 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
741 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
742 function getstyle(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
743 local e,t={...},{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
744 for a,e in r(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
745 e=s[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
746 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
747 n(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
748 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
749 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
750 return i(t,";");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
751 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
752 local a="0";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
753 function setstyle(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
754 e=e or"0";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
755 if e~=a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
756 d("\27["..e.."m");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
757 a=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
758 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
759 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
760 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
761 function setstyle(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
762 t=t or"0";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
763 if t~=a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
764 e.set_consolecolor(l[t]or o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
765 a=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
766 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
767 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
768 if not o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
769 function setstyle(e)end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
770 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
771 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
772 local function a(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
773 if t=="0"then return"</span>";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
774 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
775 for t in t:gmatch("[^;]+")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
776 n(e,u[h(t)]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
777 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
778 return"</span><span style='"..i(e,";").."'>";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
779 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
780 function tohtml(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
781 return e:gsub("\027%[(.-)m",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
782 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
783 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
784 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
785 package.preload['util.uuid']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
786 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
787 local function a(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
788 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
789 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
790 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
791 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
792 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
793 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
794 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
795 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
796 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
797 local i=tostring;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
798 local e=os.time;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
799 local o=os.clock;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
800 local n=require"util.hashes".sha1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
801 a"uuid"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
802 local t=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
803 local function a()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
804 local e=e();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
805 if t>=e then e=t+1;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
806 t=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
807 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
808 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
809 local function t(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
810 return n(e..o()..i({}),true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
811 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
812 local e=t(a());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
813 local function o(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
814 e=t(e..a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
815 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
816 local function t(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
817 if#e<t then o(a());end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
818 local a=e:sub(0,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
819 e=e:sub(t+1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
820 return a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
821 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
822 local function e()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
823 return("%x"):format(t(1):byte()%4+8);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
824 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
825 function generate()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
826 return t(8).."-"..t(4).."-4"..t(3).."-"..(e())..t(3).."-"..t(12);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
827 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
828 seed=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
829 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
830 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
831 package.preload['net.dns']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
832 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
833 local function c(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
834 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
835 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
836 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
837 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
838 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
839 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
840 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
841 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
842 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
843 local n=require"socket";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
844 local j=require"util.timer";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
845 local e,y=pcall(require,"util.windows");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
846 local E=(e and y)or os.getenv("WINDIR");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
847 local u,_,v,a,i=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
848 coroutine,io,math,string,table;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
849 local f,s,o,m,r,b,z,x,t,e,q=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
850 ipairs,next,pairs,print,setmetatable,tostring,assert,error,unpack,select,type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
851 local e={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
852 get=function(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
853 local a=e('#',...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
854 for a=1,a do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
855 t=t[e(a,...)];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
856 if t==nil then break;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
857 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
858 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
859 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
860 set=function(a,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
861 local i=e('#',...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
862 local h,o=e(i-1,...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
863 local t,n;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
864 for i=1,i-2 do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
865 local i=e(i,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
866 local e=a[i]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
867 if o==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
868 if e==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
869 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
870 elseif s(e,s(e))then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
871 t=nil;n=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
872 elseif t==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
873 t=a;n=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
874 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
875 elseif e==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
876 e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
877 a[i]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
878 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
879 a=e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
880 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
881 if o==nil and t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
882 t[n]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
883 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
884 a[h]=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
885 return o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
886 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
887 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
888 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
889 local d,l=e.get,e.set;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
890 local k=15;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
891 c('dns')
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
892 local t=_M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
893 local h=i.insert
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
894 local function c(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
895 return(e-(e%256))/256;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
896 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
897 local function p(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
898 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
899 for o,e in o(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
900 t[o]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
901 t[e]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
902 t[a.lower(e)]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
903 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
904 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
905 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
906 local function w(i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
907 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
908 for t,i in o(i)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
909 local o=a.char(c(t),t%256);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
910 e[t]=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
911 e[i]=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
912 e[a.lower(i)]=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
913 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
914 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
915 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
916 t.types={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
917 'A','NS','MD','MF','CNAME','SOA','MB','MG','MR','NULL','WKS',
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
918 'PTR','HINFO','MINFO','MX','TXT',
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
919 [28]='AAAA',[29]='LOC',[33]='SRV',
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
920 [252]='AXFR',[253]='MAILB',[254]='MAILA',[255]='*'};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
921 t.classes={'IN','CS','CH','HS',[255]='*'};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
922 t.type=p(t.types);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
923 t.class=p(t.classes);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
924 t.typecode=w(t.types);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
925 t.classcode=w(t.classes);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
926 local function g(e,o,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
927 if a.byte(e,-1)~=46 then e=e..'.';end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
928 e=a.lower(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
929 return e,t.type[o or'A'],t.class[i or'IN'];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
930 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
931 local function p(t,a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
932 a=a or n.gettime();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
933 for n,e in f(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
934 if e.tod then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
935 e.ttl=v.floor(e.tod-a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
936 if e.ttl<=0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
937 t[e[e.type:lower()]]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
938 i.remove(t,n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
939 return p(t,a,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
940 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
941 elseif o=='soft'then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
942 z(e.ttl==0);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
943 t[e[e.type:lower()]]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
944 i.remove(t,n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
945 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
946 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
947 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
948 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
949 e.__index=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
950 e.timeout=k;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
951 local function k(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
952 local e=e.type and e[e.type:lower()];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
953 if q(e)~="string"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
954 return"<UNKNOWN RDATA TYPE>";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
955 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
956 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
957 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
958 local w={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
959 LOC=e.LOC_tostring;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
960 MX=function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
961 return a.format('%2i %s',e.pref,e.mx);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
962 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
963 SRV=function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
964 local e=e.srv;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
965 return a.format('%5d %5d %5d %s',e.priority,e.weight,e.port,e.target);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
966 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
967 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
968 local q={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
969 function q.__tostring(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
970 local t=(w[e.type]or k)(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
971 return a.format('%2s %-5s %6i %-28s %s',e.class,e.type,e.ttl,e.name,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
972 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
973 local k={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
974 function k.__tostring(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
975 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
976 for a,t in f(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
977 h(e,b(t)..'\n');
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
978 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
979 return i.concat(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
980 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
981 local w={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
982 function w.__tostring(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
983 local a=n.gettime();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
984 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
985 for i,t in o(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
986 for i,t in o(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
987 for o,t in o(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
988 p(t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
989 h(e,b(t));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
990 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
991 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
992 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
993 return i.concat(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
994 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
995 function e:new()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
996 local t={active={},cache={},unsorted={}};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
997 r(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
998 r(t.cache,w);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
999 r(t.unsorted,{__mode='kv'});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1000 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1001 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1002 function t.random(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1003 v.randomseed(v.floor(1e4*n.gettime())%2147483648);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1004 t.random=v.random;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1005 return t.random(...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1006 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1007 local function v(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1008 e=e or{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1009 e.id=e.id or t.random(0,65535);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1010 e.rd=e.rd or 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1011 e.tc=e.tc or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1012 e.aa=e.aa or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1013 e.opcode=e.opcode or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1014 e.qr=e.qr or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1015 e.rcode=e.rcode or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1016 e.z=e.z or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1017 e.ra=e.ra or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1018 e.qdcount=e.qdcount or 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1019 e.ancount=e.ancount or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1020 e.nscount=e.nscount or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1021 e.arcount=e.arcount or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1022 local t=a.char(
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1023 c(e.id),e.id%256,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1024 e.rd+2*e.tc+4*e.aa+8*e.opcode+128*e.qr,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1025 e.rcode+16*e.z+128*e.ra,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1026 c(e.qdcount),e.qdcount%256,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1027 c(e.ancount),e.ancount%256,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1028 c(e.nscount),e.nscount%256,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1029 c(e.arcount),e.arcount%256
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1030 );
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1031 return t,e.id;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1032 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1033 local function c(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1034 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1035 for t in a.gmatch(t,'[^.]+')do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1036 h(e,a.char(a.len(t)));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1037 h(e,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1038 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1039 h(e,a.char(0));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1040 return i.concat(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1041 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1042 local function z(o,a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1043 o=c(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1044 a=t.typecode[a or'a'];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1045 e=t.classcode[e or'in'];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1046 return o..a..e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1047 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1048 function e:byte(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1049 e=e or 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1050 local o=self.offset;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1051 local t=o+e-1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1052 if t>#self.packet then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1053 x(a.format('out of bounds: %i>%i',t,#self.packet));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1054 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1055 self.offset=o+e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1056 return a.byte(self.packet,o,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1057 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1058 function e:word()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1059 local e,t=self:byte(2);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1060 return 256*e+t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1061 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1062 function e:dword()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1063 local t,e,a,o=self:byte(4);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1064 return 16777216*t+65536*e+256*a+o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1065 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1066 function e:sub(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1067 e=e or 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1068 local t=a.sub(self.packet,self.offset,self.offset+e-1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1069 self.offset=self.offset+e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1070 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1071 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1072 function e:header(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1073 local e=self:word();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1074 if not self.active[e]and not t then return nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1075 local e={id=e};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1076 local t,a=self:byte(2);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1077 e.rd=t%2;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1078 e.tc=t/2%2;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1079 e.aa=t/4%2;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1080 e.opcode=t/8%16;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1081 e.qr=t/128;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1082 e.rcode=a%16;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1083 e.z=a/16%8;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1084 e.ra=a/128;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1085 e.qdcount=self:word();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1086 e.ancount=self:word();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1087 e.nscount=self:word();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1088 e.arcount=self:word();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1089 for a,t in o(e)do e[a]=t-t%1;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1090 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1091 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1092 function e:name()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1093 local t,a=nil,0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1094 local e=self:byte();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1095 local o={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1096 if e==0 then return"."end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1097 while e>0 do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1098 if e>=192 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1099 a=a+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1100 if a>=20 then x('dns error: 20 pointers');end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1101 local e=((e-192)*256)+self:byte();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1102 t=t or self.offset;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1103 self.offset=e+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1104 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1105 h(o,self:sub(e)..'.');
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1106 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1107 e=self:byte();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1108 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1109 self.offset=t or self.offset;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1110 return i.concat(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1111 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1112 function e:question()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1113 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1114 e.name=self:name();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1115 e.type=t.type[self:word()];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1116 e.class=t.class[self:word()];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1117 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1118 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1119 function e:A(i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1120 local o,e,t,n=self:byte(4);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1121 i.a=a.format('%i.%i.%i.%i',o,e,t,n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1122 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1123 function e:AAAA(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1124 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1125 for t=1,a.rdlength,2 do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1126 local a,t=self:byte(2);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1127 i.insert(e,("%02x%02x"):format(a,t));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1128 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1129 e=i.concat(e,":"):gsub("%f[%x]0+(%x)","%1");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1130 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1131 for e in e:gmatch(":[0:]+:")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1132 i.insert(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1133 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1134 if#t==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1135 a.aaaa=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1136 return
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1137 elseif#t>1 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1138 i.sort(t,function(t,e)return#t>#e end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1139 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1140 a.aaaa=e:gsub(t[1],"::",1):gsub("^0::","::"):gsub("::0$","::");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1141 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1142 function e:CNAME(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1143 e.cname=self:name();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1144 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1145 function e:MX(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1146 e.pref=self:word();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1147 e.mx=self:name();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1148 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1149 function e:LOC_nibble_power()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1150 local e=self:byte();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1151 return((e-(e%16))/16)*(10^(e%16));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1152 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1153 function e:LOC(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1154 e.version=self:byte();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1155 if e.version==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1156 e.loc=e.loc or{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1157 e.loc.size=self:LOC_nibble_power();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1158 e.loc.horiz_pre=self:LOC_nibble_power();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1159 e.loc.vert_pre=self:LOC_nibble_power();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1160 e.loc.latitude=self:dword();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1161 e.loc.longitude=self:dword();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1162 e.loc.altitude=self:dword();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1163 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1164 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1165 local function c(e,i,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1166 e=e-2147483648;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1167 if e<0 then i=t;e=-e;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1168 local n,o,t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1169 t=e%6e4;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1170 e=(e-t)/6e4;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1171 o=e%60;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1172 n=(e-o)/60;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1173 return a.format('%3d %2d %2.3f %s',n,o,t/1e3,i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1174 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1175 function e.LOC_tostring(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1176 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1177 h(t,a.format(
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1178 '%s %s %.2fm %.2fm %.2fm %.2fm',
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1179 c(e.loc.latitude,'N','S'),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1180 c(e.loc.longitude,'E','W'),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1181 (e.loc.altitude-1e7)/100,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1182 e.loc.size/100,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1183 e.loc.horiz_pre/100,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1184 e.loc.vert_pre/100
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1185 ));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1186 return i.concat(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1187 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1188 function e:NS(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1189 e.ns=self:name();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1190 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1191 function e:SOA(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1192 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1193 function e:SRV(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1194 e.srv={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1195 e.srv.priority=self:word();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1196 e.srv.weight=self:word();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1197 e.srv.port=self:word();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1198 e.srv.target=self:name();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1199 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1200 function e:PTR(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1201 e.ptr=self:name();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1202 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1203 function e:TXT(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1204 e.txt=self:sub(self:byte());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1205 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1206 function e:rr()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1207 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1208 r(e,q);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1209 e.name=self:name(self);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1210 e.type=t.type[self:word()]or e.type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1211 e.class=t.class[self:word()]or e.class;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1212 e.ttl=65536*self:word()+self:word();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1213 e.rdlength=self:word();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1214 if e.ttl<=0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1215 e.tod=self.time+30;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1216 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1217 e.tod=self.time+e.ttl;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1218 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1219 local a=self.offset;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1220 local t=self[t.type[e.type]];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1221 if t then t(self,e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1222 self.offset=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1223 e.rdata=self:sub(e.rdlength);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1224 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1225 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1226 function e:rrs(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1227 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1228 for t=1,t do h(e,self:rr());end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1229 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1230 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1231 function e:decode(t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1232 self.packet,self.offset=t,1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1233 local t=self:header(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1234 if not t then return nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1235 local t={header=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1236 t.question={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1237 local i=self.offset;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1238 for e=1,t.header.qdcount do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1239 h(t.question,self:question());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1240 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1241 t.question.raw=a.sub(self.packet,i,self.offset-1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1242 if not o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1243 if not self.active[t.header.id]or not self.active[t.header.id][t.question.raw]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1244 self.active[t.header.id]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1245 return nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1246 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1247 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1248 t.answer=self:rrs(t.header.ancount);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1249 t.authority=self:rrs(t.header.nscount);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1250 t.additional=self:rrs(t.header.arcount);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1251 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1252 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1253 e.delays={1,3};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1254 function e:addnameserver(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1255 self.server=self.server or{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1256 h(self.server,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1257 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1258 function e:setnameserver(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1259 self.server={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1260 self:addnameserver(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1261 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1262 function e:adddefaultnameservers()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1263 if E then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1264 if y and y.get_nameservers then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1265 for t,e in f(y.get_nameservers())do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1266 self:addnameserver(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1267 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1268 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1269 if not self.server or#self.server==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1270 self:addnameserver("208.67.222.222");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1271 self:addnameserver("208.67.220.220");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1272 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1273 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1274 local e=_.open("/etc/resolv.conf");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1275 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1276 for e in e:lines()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1277 e=e:gsub("#.*$","")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1278 :match('^%s*nameserver%s+(.*)%s*$');
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1279 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1280 e:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1281 self:addnameserver(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1282 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1283 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1284 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1285 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1286 if not self.server or#self.server==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1287 self:addnameserver("127.0.0.1");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1288 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1289 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1290 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1291 function e:getsocket(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1292 self.socket=self.socket or{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1293 self.socketset=self.socketset or{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1294 local e=self.socket[a];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1295 if e then return e;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1296 local o,t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1297 e,t=n.udp();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1298 if e and self.socket_wrapper then e,t=self.socket_wrapper(e,self);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1299 if not e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1300 return nil,t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1301 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1302 e:settimeout(0);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1303 self.socket[a]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1304 self.socketset[e]=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1305 o,t=e:setsockname('*',0);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1306 if not o then return self:servfail(e,t);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1307 o,t=e:setpeername(self.server[a],53);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1308 if not o then return self:servfail(e,t);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1309 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1310 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1311 function e:voidsocket(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1312 if self.socket[e]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1313 self.socketset[self.socket[e]]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1314 self.socket[e]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1315 elseif self.socketset[e]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1316 self.socket[self.socketset[e]]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1317 self.socketset[e]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1318 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1319 e:close();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1320 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1321 function e:socket_wrapper_set(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1322 self.socket_wrapper=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1323 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1324 function e:closeall()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1325 for t,e in f(self.socket)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1326 self.socket[t]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1327 self.socketset[e]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1328 e:close();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1329 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1330 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1331 function e:remember(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1332 local a,o,i=g(e.name,e.type,e.class);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1333 if t~='*'then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1334 t=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1335 local t=d(self.cache,i,'*',a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1336 if t then h(t,e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1337 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1338 self.cache=self.cache or r({},w);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1339 local a=d(self.cache,i,t,a)or
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1340 l(self.cache,i,t,a,r({},k));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1341 if not a[e[o:lower()]]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1342 a[e[o:lower()]]=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1343 h(a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1344 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1345 if t=='MX'then self.unsorted[a]=true;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1346 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1347 local function c(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1348 return(t.pref==e.pref)and(t.mx<e.mx)or(t.pref<e.pref);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1349 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1350 function e:peek(o,t,a,h)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1351 o,t,a=g(o,t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1352 local e=d(self.cache,a,t,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1353 if not e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1354 if h then if h<=0 then return end else h=3 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1355 e=d(self.cache,a,"CNAME",o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1356 if not(e and e[1])then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1357 return self:peek(e[1].cname,t,a,h-1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1358 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1359 if p(e,n.gettime())and t=='*'or not s(e)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1360 l(self.cache,a,t,o,nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1361 return nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1362 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1363 if self.unsorted[e]then i.sort(e,c);self.unsorted[e]=nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1364 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1365 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1366 function e:purge(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1367 if e=='soft'then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1368 self.time=n.gettime();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1369 for t,e in o(self.cache or{})do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1370 for t,e in o(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1371 for t,e in o(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1372 p(e,self.time,'soft')
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1373 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1374 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1375 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1376 else self.cache=r({},w);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1377 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1378 function e:query(a,t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1379 a,t,e=g(a,t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1380 local s=u.running();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1381 local o=d(self.wanted,e,t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1382 if s and o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1383 l(self.wanted,e,t,a,s,true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1384 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1385 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1386 if not self.server then self:adddefaultnameservers();end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1387 local h=z(a,t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1388 local o=self:peek(a,t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1389 if o then return o;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1390 local o,i=v();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1391 local o={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1392 packet=o..h,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1393 server=self.best_server,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1394 delay=1,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1395 retry=n.gettime()+self.delays[1]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1396 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1397 self.active[i]=self.active[i]or{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1398 self.active[i][h]=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1399 if s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1400 l(self.wanted,e,t,a,s,true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1401 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1402 local i,h=self:getsocket(o.server)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1403 if not i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1404 return nil,h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1405 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1406 i:send(o.packet)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1407 if j and self.timeout then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1408 local r=#self.server;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1409 local n=1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1410 j.add_task(self.timeout,function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1411 if d(self.wanted,e,t,a,s)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1412 if n<r then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1413 n=n+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1414 self:servfail(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1415 o.server=self.best_server;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1416 i,h=self:getsocket(o.server);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1417 if i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1418 i:send(o.packet);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1419 return self.timeout;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1420 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1421 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1422 self:cancel(e,t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1423 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1424 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1425 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1426 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1427 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1428 function e:servfail(t,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1429 local h=self.socketset[t]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1430 t=self:voidsocket(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1431 self.time=n.gettime();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1432 for n,a in o(self.active)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1433 for o,e in o(a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1434 if e.server==h then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1435 e.server=e.server+1
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1436 if e.server>#self.server then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1437 e.server=1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1438 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1439 e.retries=(e.retries or 0)+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1440 if e.retries>=#self.server then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1441 a[o]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1442 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1443 t,i=self:getsocket(e.server);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1444 if t then t:send(e.packet);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1445 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1446 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1447 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1448 if s(a)==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1449 self.active[n]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1450 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1451 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1452 if h==self.best_server then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1453 self.best_server=self.best_server+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1454 if self.best_server>#self.server then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1455 self.best_server=1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1456 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1457 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1458 return t,i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1459 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1460 function e:settimeout(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1461 self.timeout=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1462 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1463 function e:receive(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1464 self.time=n.gettime();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1465 t=t or self.socket;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1466 local e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1467 for a,t in o(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1468 if self.socketset[t]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1469 local t=t:receive();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1470 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1471 e=self:decode(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1472 if e and self.active[e.header.id]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1473 and self.active[e.header.id][e.question.raw]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1474 for a,t in o(e.answer)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1475 self:remember(t,e.question[1].type)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1476 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1477 local t=self.active[e.header.id];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1478 t[e.question.raw]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1479 if not s(t)then self.active[e.header.id]=nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1480 if not s(self.active)then self:closeall();end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1481 local e=e.question[1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1482 local t=d(self.wanted,e.class,e.type,e.name);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1483 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1484 for e in o(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1485 if u.status(e)=="suspended"then u.resume(e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1486 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1487 l(self.wanted,e.class,e.type,e.name,nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1488 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1489 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1490 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1491 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1492 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1493 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1494 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1495 function e:feed(a,t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1496 self.time=n.gettime();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1497 local e=self:decode(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1498 if e and self.active[e.header.id]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1499 and self.active[e.header.id][e.question.raw]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1500 for a,t in o(e.answer)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1501 self:remember(t,e.question[1].type);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1502 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1503 local t=self.active[e.header.id];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1504 t[e.question.raw]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1505 if not s(t)then self.active[e.header.id]=nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1506 if not s(self.active)then self:closeall();end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1507 local e=e.question[1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1508 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1509 local t=d(self.wanted,e.class,e.type,e.name);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1510 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1511 for e in o(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1512 if u.status(e)=="suspended"then u.resume(e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1513 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1514 l(self.wanted,e.class,e.type,e.name,nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1515 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1516 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1517 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1518 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1519 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1520 function e:cancel(e,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1521 local i=d(self.wanted,e,t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1522 if i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1523 for e in o(i)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1524 if u.status(e)=="suspended"then u.resume(e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1525 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1526 l(self.wanted,e,t,a,nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1527 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1528 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1529 function e:pulse()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1530 while self:receive()do end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1531 if not s(self.active)then return nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1532 self.time=n.gettime();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1533 for i,t in o(self.active)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1534 for a,e in o(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1535 if self.time>=e.retry then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1536 e.server=e.server+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1537 if e.server>#self.server then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1538 e.server=1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1539 e.delay=e.delay+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1540 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1541 if e.delay>#self.delays then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1542 t[a]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1543 if not s(t)then self.active[i]=nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1544 if not s(self.active)then return nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1545 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1546 local t=self.socket[e.server];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1547 if t then t:send(e.packet);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1548 e.retry=self.time+self.delays[e.delay];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1549 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1550 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1551 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1552 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1553 if s(self.active)then return true;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1554 return nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1555 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1556 function e:lookup(o,a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1557 self:query(o,a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1558 while self:pulse()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1559 local e={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1560 for a,t in f(self.socket)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1561 e[a]=t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1562 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1563 n.select(e,nil,4)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1564 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1565 return self:peek(o,a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1566 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1567 function e:lookupex(o,e,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1568 return self:peek(e,t,a)or self:query(e,t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1569 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1570 function e:tohostname(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1571 return t.lookup(e:gsub("(%d+)%.(%d+)%.(%d+)%.(%d+)","%4.%3.%2.%1.in-addr.arpa."),"PTR");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1572 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1573 local i={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1574 qr={[0]='query','response'},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1575 opcode={[0]='query','inverse query','server status request'},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1576 aa={[0]='non-authoritative','authoritative'},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1577 tc={[0]='complete','truncated'},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1578 rd={[0]='recursion not desired','recursion desired'},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1579 ra={[0]='recursion not available','recursion available'},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1580 z={[0]='(reserved)'},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1581 rcode={[0]='no error','format error','server failure','name error','not implemented'},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1582 type=t.type,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1583 class=t.class
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1584 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1585 local function s(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1586 return(i[e]and i[e][t[e]])or'';
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1587 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1588 function e.print(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1589 for o,e in o{'id','qr','opcode','aa','tc','rd','ra','z',
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1590 'rcode','qdcount','ancount','nscount','arcount'}do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1591 m(a.format('%-30s','header.'..e),t.header[e],s(t.header,e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1592 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1593 for t,e in f(t.question)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1594 m(a.format('question[%i].name ',t),e.name);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1595 m(a.format('question[%i].type ',t),e.type);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1596 m(a.format('question[%i].class ',t),e.class);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1597 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1598 local r={name=1,type=1,class=1,ttl=1,rdlength=1,rdata=1};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1599 local e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1600 for i,n in o({'answer','authority','additional'})do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1601 for h,i in o(t[n])do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1602 for o,t in o({'name','type','class','ttl','rdlength'})do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1603 e=a.format('%s[%i].%s',n,h,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1604 m(a.format('%-30s',e),i[t],s(i,t));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1605 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1606 for t,o in o(i)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1607 if not r[t]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1608 e=a.format('%s[%i].%s',n,h,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1609 m(a.format('%-30s %s',b(e),b(o)));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1610 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1611 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1612 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1613 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1614 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1615 function t.resolver()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1616 local t={active={},cache={},unsorted={},wanted={},best_server=1};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1617 r(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1618 r(t.cache,w);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1619 r(t.unsorted,{__mode='kv'});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1620 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1621 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1622 local e=t.resolver();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1623 t._resolver=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1624 function t.lookup(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1625 return e:lookup(...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1626 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1627 function t.tohostname(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1628 return e:tohostname(...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1629 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1630 function t.purge(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1631 return e:purge(...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1632 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1633 function t.peek(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1634 return e:peek(...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1635 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1636 function t.query(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1637 return e:query(...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1638 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1639 function t.feed(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1640 return e:feed(...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1641 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1642 function t.cancel(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1643 return e:cancel(...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1644 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1645 function t.settimeout(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1646 return e:settimeout(...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1647 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1648 function t.cache()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1649 return e.cache;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1650 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1651 function t.socket_wrapper_set(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1652 return e:socket_wrapper_set(...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1653 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1654 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1655 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1656 package.preload['net.adns']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1657 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1658 local function o(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1659 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1660 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1661 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1662 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1663 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1664 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1665 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1666 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1667 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1668 local c=require"net.server";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1669 local a=require"net.dns";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1670 local e=require"util.logger".init("adns");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1671 local t,t=table.insert,table.remove;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1672 local n,s,l=coroutine,tostring,pcall;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1673 local function u(a,a,t,e)return(e-t)+1;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1674 o"adns"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1675 function lookup(d,t,h,r)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1676 return n.wrap(function(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1677 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1678 e("debug","Records for %s already cached, using those...",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1679 d(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1680 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1681 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1682 e("debug","Records for %s not in cache, sending query (%s)...",t,s(n.running()));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1683 local i,o=a.query(t,h,r);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1684 if i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1685 n.yield({r or"IN",h or"A",t,n.running()});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1686 e("debug","Reply for %s (%s)",t,s(n.running()));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1687 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1688 if i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1689 i,o=l(d,a.peek(t,h,r));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1690 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1691 e("error","Error sending DNS query: %s",o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1692 i,o=l(d,nil,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1693 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1694 if not i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1695 e("error","Error in DNS response handler: %s",s(o));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1696 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1697 end)(a.peek(t,h,r));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1698 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1699 function cancel(t,o,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1700 e("warn","Cancelling DNS lookup for %s",s(t[3]));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1701 a.cancel(t[1],t[2],t[3],t[4],o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1702 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1703 function new_async_socket(i,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1704 local n="<unknown>";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1705 local s={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1706 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1707 local h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1708 function s.onincoming(o,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1709 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1710 a.feed(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1711 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1712 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1713 function s.ondisconnect(i,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1714 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1715 e("warn","DNS socket for %s disconnected: %s",n,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1716 local t=o.server;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1717 if o.socketset[i]==o.best_server and o.best_server==#t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1718 e("error","Exhausted all %d configured DNS servers, next lookup will try %s again",#t,t[1]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1719 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1720 o:servfail(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1721 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1722 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1723 t,h=c.wrapclient(i,"dns",53,s);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1724 if not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1725 return nil,h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1726 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1727 t.settimeout=function()end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1728 t.setsockname=function(e,...)return i:setsockname(...);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1729 t.setpeername=function(o,...)n=(...);local e,a=i:setpeername(...);o:set_send(u);return e,a;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1730 t.connect=function(e,...)return i:connect(...)end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1731 t.send=function(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1732 e("debug","Sending DNS query to %s",n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1733 return i:send(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1734 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1735 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1736 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1737 a.socket_wrapper_set(new_async_socket);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1738 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1739 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1740 package.preload['net.server']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1741 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1742 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1743 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1744 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1745 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1746 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1747 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1748 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1749 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1750 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1751 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1752 local m=function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1753 return _G[e]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1754 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1755 local W,e=require("util.logger").init("socket"),table.concat;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1756 local n=function(...)return W("debug",e{...});end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1757 local E=function(...)return W("warn",e{...});end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1758 local ue=1
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1759 local f=m"type"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1760 local q=m"pairs"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1761 local me=m"ipairs"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1762 local y=m"tonumber"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1763 local u=m"tostring"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1764 local t=m"table"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1765 local a=m"string"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1766 local e=m"coroutine"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1767 local Y=math.min
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1768 local ce=math.huge
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1769 local fe=t.concat
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1770 local de=t.insert
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1771 local ye=a.sub
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1772 local we=e.wrap
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1773 local pe=e.yield
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1774 local I,e=pcall(require,"ssl")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1775 local b=m"socket"or require"socket"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1776 local P=b.gettime
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1777 local le=b.dns.getaddrinfo
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1778 local ve=(I and e.wrap)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1779 local se=b.bind
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1780 local be=b.select
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1781 local O
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1782 local Z
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1783 local ie
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1784 local V
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1785 local B
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1786 local l
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1787 local ne
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1788 local X
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1789 local oe
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1790 local te
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1791 local ae
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1792 local J
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1793 local s
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1794 local he
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1795 local ee
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1796 local re
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1797 local p
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1798 local i
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1799 local C
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1800 local r
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1801 local h
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1802 local A
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1803 local v
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1804 local w
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1805 local k
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1806 local z
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1807 local a
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1808 local o
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1809 local g
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1810 local F
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1811 local M
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1812 local T
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1813 local j
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1814 local R
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1815 local Q
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1816 local d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1817 local S
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1818 local L
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1819 local U
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1820 local D
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1821 local H
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1822 local _
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1823 local x
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1824 local N
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1825 p={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1826 i={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1827 r={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1828 C={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1829 h={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1830 v={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1831 w={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1832 A={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1833 k={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1834 a=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1835 o=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1836 g=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1837 F=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1838 M=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1839 T=1
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1840 j=128
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1841 R=10
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1842 S=51e3*1024
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1843 L=25e3*1024
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1844 U=30
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1845 D=6e4
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1846 H=14*60
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1847 local e=package.config:sub(1,1)=="\\"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1848 x=(e and math.huge)or b._SETSIZE or 1024
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1849 _=b._SETSIZE or 1024
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1850 N=30
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1851 te=function(y,t,m,c,g,w)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1852 if t:getfd()>=x then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1853 E("server.lua: Disallowed FD number: "..t:getfd())
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1854 t:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1855 return nil,"fd-too-large"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1856 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1857 local f=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1858 local v,e=y.onconnect,y.ondisconnect
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1859 local b=t.accept
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1860 local e={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1861 e.shutdown=function()end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1862 e.ssl=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1863 return w~=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1864 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1865 e.sslctx=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1866 return w
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1867 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1868 e.remove=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1869 f=f-1
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1870 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1871 e.resume()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1872 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1873 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1874 e.close=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1875 t:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1876 o=s(r,t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1877 a=s(i,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1878 p[m..":"..c]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1879 h[t]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1880 e=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1881 t=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1882 n"server.lua: closed server handler and removed sockets from list"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1883 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1884 e.pause=function(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1885 if not e.paused then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1886 a=s(i,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1887 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1888 h[t]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1889 t:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1890 t=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1891 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1892 e.paused=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1893 n("server.lua: server [",m,"]:",c," paused")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1894 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1895 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1896 e.resume=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1897 if e.paused then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1898 if not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1899 t=se(m,c,j);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1900 t:settimeout(0)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1901 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1902 a=l(i,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1903 h[t]=e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1904 k[e]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1905 e.paused=false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1906 n("server.lua: server [",m,"]:",c," resumed")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1907 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1908 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1909 e.ip=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1910 return m
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1911 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1912 e.serverport=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1913 return c
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1914 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1915 e.socket=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1916 return t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1917 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1918 e.readbuffer=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1919 if a>=_ or o>=_ then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1920 e.pause()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1921 k[e]=d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1922 n("server.lua: refused new client connection: server full")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1923 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1924 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1925 local t,o=b(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1926 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1927 local o,a=t:getpeername()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1928 local e,i,t=ee(e,y,t,o,c,a,g,w)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1929 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1930 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1931 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1932 f=f+1
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1933 n("server.lua: accepted new client connection from ",u(o),":",u(a)," to ",u(c))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1934 if v and not w then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1935 return v(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1936 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1937 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1938 elseif o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1939 n("server.lua: error with new client connection: ",u(o))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1940 e.pause()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1941 k[e]=d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1942 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1943 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1944 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1945 return e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1946 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1947 ee=function(b,f,t,R,X,D,H,q)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1948 if t:getfd()>=x then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1949 E("server.lua: Disallowed FD number: "..t:getfd())
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1950 t:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1951 if b then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1952 k[b]=d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1953 b.pause()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1954 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1955 return nil,nil,"fd-too-large"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1956 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1957 t:settimeout(0)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1958 local y
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1959 local T
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1960 local g
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1961 local K
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1962 local G=f.onincoming
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1963 local B=f.onstatus
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1964 local k=f.ondisconnect
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1965 local W=f.ondrain
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1966 local J=f.onreadtimeout;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1967 local C=f.ondetach
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1968 local p={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1969 local c=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1970 local Q
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1971 local U
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1972 local m=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1973 local j=false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1974 local E=false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1975 local Y,P=0,0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1976 local x=S
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1977 local _=L
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1978 local e=p
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1979 e.dispatch=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1980 return G
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1981 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1982 e.disconnect=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1983 return k
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1984 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1985 e.onreadtimeout=J;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1986 e.setlistener=function(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1987 if C then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1988 C(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1989 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1990 G=t.onincoming
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1991 k=t.ondisconnect
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1992 B=t.onstatus
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1993 W=t.ondrain
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1994 e.onreadtimeout=t.onreadtimeout
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1995 C=t.ondetach
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1996 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1997 e.getstats=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1998 return P,Y
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1999 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2000 e.ssl=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2001 return K
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2002 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2003 e.sslctx=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2004 return q
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2005 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2006 e.send=function(n,a,o,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2007 return y(t,a,o,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2008 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2009 e.receive=function(o,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2010 return T(t,o,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2011 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2012 e.shutdown=function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2013 return g(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2014 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2015 e.setoption=function(i,a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2016 if t.setoption then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2017 return t:setoption(a,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2018 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2019 return false,"setoption not implemented";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2020 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2021 e.force_close=function(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2022 if c~=0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2023 n("server.lua: discarding unwritten data for ",u(R),":",u(D))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2024 c=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2025 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2026 return t:close(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2027 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2028 e.close=function(l,d)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2029 if not e then return true;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2030 a=s(i,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2031 v[e]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2032 if c~=0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2033 e.sendbuffer()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2034 if c~=0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2035 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2036 e.write=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2037 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2038 Q=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2039 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2040 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2041 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2042 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2043 z=g and g(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2044 t:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2045 o=s(r,t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2046 h[t]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2047 t=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2048 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2049 n"server.lua: socket already closed"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2050 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2051 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2052 w[e]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2053 A[e]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2054 local t=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2055 e=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2056 if k then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2057 k(t,d or false);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2058 k=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2059 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2060 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2061 if b then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2062 b.remove()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2063 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2064 n"server.lua: closed client handler and removed socket from list"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2065 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2066 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2067 e.server=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2068 return b
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2069 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2070 e.ip=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2071 return R
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2072 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2073 e.serverport=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2074 return X
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2075 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2076 e.clientport=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2077 return D
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2078 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2079 e.port=e.clientport
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2080 local b=function(i,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2081 if not e then return false end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2082 m=m+#a
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2083 if m>x then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2084 A[e]="send buffer exceeded"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2085 e.write=V
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2086 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2087 elseif t and not r[t]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2088 o=l(r,t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2089 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2090 c=c+1
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2091 p[c]=a
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2092 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2093 w[e]=w[e]or d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2094 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2095 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2096 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2097 e.write=b
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2098 e.bufferqueue=function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2099 return p
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2100 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2101 e.socket=function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2102 return t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2103 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2104 e.set_mode=function(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2105 H=t or H
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2106 return H
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2107 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2108 e.set_send=function(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2109 y=t or y
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2110 return y
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2111 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2112 e.bufferlen=function(o,a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2113 x=t or x
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2114 _=a or _
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2115 return m,_,x
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2116 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2117 e.lock_read=function(n,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2118 if o==true then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2119 local o=a
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2120 a=s(i,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2121 v[e]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2122 if a~=o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2123 j=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2124 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2125 elseif o==false then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2126 if j then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2127 j=false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2128 a=l(i,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2129 v[e]=d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2130 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2131 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2132 return j
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2133 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2134 e.pause=function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2135 return t:lock_read(true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2136 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2137 e.resume=function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2138 return t:lock_read(false);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2139 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2140 e.lock=function(i,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2141 e.lock_read(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2142 if a==true then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2143 e.write=V
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2144 local a=o
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2145 o=s(r,t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2146 w[e]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2147 if o~=a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2148 E=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2149 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2150 elseif a==false then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2151 e.write=b
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2152 if E then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2153 E=false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2154 b("")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2155 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2156 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2157 return j,E
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2158 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2159 local v=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2160 local a,t,o=T(t,H)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2161 if not t or(t=="wantread"or t=="timeout")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2162 local o=a or o or""
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2163 local a=#o
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2164 if a>_ then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2165 e:close("receive buffer exceeded")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2166 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2167 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2168 local a=a*ue
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2169 P=P+a
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2170 M=M+a
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2171 v[e]=d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2172 return G(e,o,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2173 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2174 n("server.lua: client ",u(R),":",u(D)," read error: ",u(t))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2175 z=e and e:force_close(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2176 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2177 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2178 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2179 local p=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2180 local f,a,h,i,l;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2181 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2182 i=fe(p,"",1,c)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2183 f,a,h=y(t,i,1,m)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2184 l=(f or h or 0)*ue
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2185 Y=Y+l
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2186 F=F+l
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2187 for e=c,1,-1 do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2188 p[e]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2189 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2190 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2191 f,a,l=false,"unexpected close",0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2192 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2193 if f then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2194 c=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2195 m=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2196 o=s(r,t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2197 w[e]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2198 if W then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2199 W(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2200 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2201 z=U and e:starttls(nil)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2202 z=Q and e:force_close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2203 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2204 elseif h and(a=="timeout"or a=="wantwrite")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2205 i=ye(i,h+1,m)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2206 p[1]=i
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2207 c=1
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2208 m=m-h
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2209 w[e]=d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2210 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2211 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2212 n("server.lua: client ",u(R),":",u(D)," write error: ",u(a))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2213 z=e and e:force_close(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2214 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2215 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2216 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2217 local d;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2218 function e.set_sslctx(w,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2219 q=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2220 local u,m
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2221 d=we(function(h)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2222 local t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2223 for d=1,N do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2224 o=(m and s(r,h,o))or o
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2225 a=(u and s(i,h,a))or a
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2226 u,m=nil,nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2227 d,t=h:dohandshake()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2228 if not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2229 n("server.lua: ssl handshake done")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2230 e.readbuffer=v
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2231 e.sendbuffer=p
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2232 d=B and B(e,"ssl-handshake-complete")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2233 if w.autostart_ssl and f.onconnect then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2234 f.onconnect(w);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2235 if c~=0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2236 o=l(r,h,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2237 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2238 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2239 a=l(i,h,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2240 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2241 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2242 if t=="wantwrite"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2243 o=l(r,h,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2244 m=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2245 elseif t=="wantread"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2246 a=l(i,h,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2247 u=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2248 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2249 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2250 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2251 t=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2252 pe()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2253 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2254 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2255 t="ssl handshake error: "..(t or"handshake too long");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2256 n("server.lua: ",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2257 z=e and e:force_close(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2258 return false,t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2259 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2260 )
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2261 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2262 if I then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2263 e.starttls=function(f,m)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2264 if m then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2265 e:set_sslctx(m);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2266 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2267 if c>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2268 n"server.lua: we need to do tls, but delaying until send buffer empty"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2269 U=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2270 return
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2271 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2272 n("server.lua: attempting to start tls on "..u(t))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2273 local m,c=t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2274 t,c=ve(t,q)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2275 if not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2276 n("server.lua: error while starting tls on client: ",u(c or"unknown error"))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2277 return nil,c
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2278 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2279 t:settimeout(0)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2280 y=t.send
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2281 T=t.receive
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2282 g=O
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2283 h[t]=e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2284 a=l(i,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2285 a=s(i,m,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2286 o=s(r,m,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2287 h[m]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2288 e.starttls=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2289 U=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2290 K=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2291 e.readbuffer=d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2292 e.sendbuffer=d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2293 return d(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2294 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2295 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2296 e.readbuffer=v
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2297 e.sendbuffer=p
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2298 y=t.send
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2299 T=t.receive
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2300 g=(K and O)or t.shutdown
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2301 h[t]=e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2302 a=l(i,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2303 if q and I then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2304 n"server.lua: auto-starting ssl negotiation..."
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2305 e.autostart_ssl=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2306 local t,e=e:starttls(q);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2307 if t==false then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2308 return nil,nil,e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2309 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2310 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2311 return e,t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2312 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2313 O=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2314 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2315 V=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2316 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2317 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2318 l=function(t,a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2319 if not t[a]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2320 e=e+1
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2321 t[e]=a
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2322 t[a]=e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2323 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2324 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2325 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2326 s=function(e,o,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2327 local i=e[o]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2328 if i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2329 e[o]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2330 local a=e[t]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2331 e[t]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2332 if a~=o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2333 e[a]=i
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2334 e[i]=a
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2335 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2336 return t-1
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2337 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2338 return t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2339 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2340 J=function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2341 o=s(r,e,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2342 a=s(i,e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2343 h[e]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2344 e:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2345 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2346 local function z(e,a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2347 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2348 local i=a.sendbuffer;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2349 function a.sendbuffer()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2350 i();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2351 if t and a.bufferlen()<o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2352 e:lock_read(false);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2353 t=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2354 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2355 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2356 local i=e.readbuffer;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2357 function e.readbuffer()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2358 i();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2359 if not t and a.bufferlen()>=o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2360 t=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2361 e:lock_read(true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2362 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2363 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2364 e:set_mode("*a");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2365 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2366 ne=function(e,t,d,u,r)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2367 e=e or"*"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2368 local o
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2369 if f(d)~="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2370 o="invalid listener table"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2371 elseif f(e)~="string"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2372 o="invalid address"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2373 elseif f(t)~="number"or not(t>=0 and t<=65535)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2374 o="invalid port"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2375 elseif p[e..":"..t]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2376 o="listeners on '["..e.."]:"..t.."' already exist"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2377 elseif r and not I then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2378 o="luasec not found"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2379 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2380 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2381 E("server.lua, [",e,"]:",t,": ",o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2382 return nil,o
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2383 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2384 local o,s=se(e,t,j)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2385 if s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2386 E("server.lua, [",e,"]:",t,": ",s)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2387 return nil,s
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2388 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2389 local s,d=te(d,o,e,t,u,r)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2390 if not s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2391 o:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2392 return nil,d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2393 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2394 o:settimeout(0)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2395 a=l(i,o,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2396 p[e..":"..t]=s
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2397 h[o]=s
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2398 n("server.lua: new "..(r and"ssl "or"").."server listener on '[",e,"]:",t,"'")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2399 return s
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2400 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2401 oe=function(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2402 return p[e..":"..t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2403 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2404 he=function(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2405 local a=p[e..":"..t]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2406 if not a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2407 return nil,"no server found on '["..e.."]:"..u(t).."'"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2408 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2409 a:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2410 p[e..":"..t]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2411 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2412 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2413 B=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2414 for e,t in q(h)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2415 t:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2416 h[e]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2417 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2418 a=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2419 o=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2420 g=0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2421 p={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2422 i={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2423 r={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2424 C={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2425 h={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2426 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2427 ae=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2428 return{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2429 select_timeout=T;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2430 tcp_backlog=j;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2431 max_send_buffer_size=S;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2432 max_receive_buffer_size=L;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2433 select_idle_check_interval=U;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2434 send_timeout=D;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2435 read_timeout=H;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2436 max_connections=_;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2437 max_ssl_handshake_roundtrips=N;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2438 highest_allowed_fd=x;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2439 accept_retry_interval=R;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2440 }
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2441 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2442 re=function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2443 if f(e)~="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2444 return nil,"invalid settings table"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2445 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2446 T=y(e.select_timeout)or T
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2447 S=y(e.max_send_buffer_size)or S
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2448 L=y(e.max_receive_buffer_size)or L
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2449 U=y(e.select_idle_check_interval)or U
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2450 j=y(e.tcp_backlog)or j
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2451 D=y(e.send_timeout)or D
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2452 H=y(e.read_timeout)or H
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2453 R=y(e.accept_retry_interval)or R
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2454 _=e.max_connections or _
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2455 N=e.max_ssl_handshake_roundtrips or N
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2456 x=e.highest_allowed_fd or x
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2457 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2458 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2459 X=function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2460 if f(e)~="function"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2461 return nil,"invalid listener function"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2462 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2463 g=g+1
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2464 C[g]=e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2465 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2466 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2467 local u do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2468 local o={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2469 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2470 function u(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2471 local o=P();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2472 e=e+o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2473 if e>=o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2474 de(t,{e,a});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2475 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2476 local e=a(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2477 if e and f(e)=="number"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2478 return u(e,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2479 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2480 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2481 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2482 X(function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2483 if#t>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2484 for a,e in q(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2485 de(o,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2486 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2487 t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2488 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2489 local e=ce;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2490 for s,t in q(o)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2491 local i,n=t[1],t[2];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2492 if i<=a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2493 o[s]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2494 local t=n(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2495 if f(t)=="number"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2496 u(t,n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2497 e=Y(e,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2498 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2499 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2500 e=Y(e,i-a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2501 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2502 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2503 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2504 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2505 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2506 ie=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2507 return M,F,a,o,g
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2508 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2509 local e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2510 local function c(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2511 e=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2512 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2513 Z=function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2514 if e then return"quitting";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2515 if t then e="once";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2516 d=P()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2517 repeat
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2518 local t=ce;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2519 for e=1,g do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2520 local e=C[e](d)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2521 if e then t=Y(t,e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2522 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2523 local t,a,o=be(i,r,Y(T,t))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2524 for t,e in me(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2525 local t=h[e]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2526 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2527 t.readbuffer()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2528 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2529 J(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2530 n"server.lua: found no handler and closed socket (readlist)"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2531 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2532 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2533 for e,t in me(a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2534 local e=h[t]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2535 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2536 e.sendbuffer()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2537 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2538 J(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2539 n"server.lua: found no handler and closed socket (writelist)"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2540 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2541 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2542 for e,t in q(A)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2543 e.disconnect()(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2544 e:force_close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2545 A[e]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2546 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2547 d=P()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2548 if d-Q>U then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2549 Q=d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2550 for e,t in q(w)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2551 if d-t>D then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2552 e.disconnect()(e,"send timeout")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2553 e:force_close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2554 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2555 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2556 for e,t in q(v)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2557 if d-t>H then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2558 if not(e.onreadtimeout)or e:onreadtimeout()~=true then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2559 e.disconnect()(e,"read timeout")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2560 e:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2561 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2562 v[e]=d
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2563 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2564 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2565 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2566 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2567 for e,t in q(k)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2568 if d-t>R then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2569 k[e]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2570 e.resume();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2571 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2572 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2573 until e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2574 if e=="once"then e=nil;return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2575 B();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2576 return"quitting"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2577 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2578 local function y()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2579 return Z(true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2580 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2581 local function g()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2582 return"select";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2583 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2584 local n=function(t,d,u,n,e,s)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2585 local e,t,d=ee(nil,n,t,d,u,"clientport",e,s)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2586 if not e then return nil,d end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2587 h[t]=e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2588 if not s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2589 a=l(i,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2590 o=l(r,t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2591 if n.onconnect then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2592 local t=e.sendbuffer;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2593 e.sendbuffer=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2594 e.sendbuffer=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2595 n.onconnect(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2596 return t();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2597 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2598 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2599 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2600 return e,t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2601 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2602 local b=function(a,t,s,r,i,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2603 local e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2604 if f(s)~="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2605 e="invalid listener table"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2606 elseif f(a)~="string"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2607 e="invalid address"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2608 elseif f(t)~="number"or not(t>=0 and t<=65535)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2609 e="invalid port"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2610 elseif i and not I then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2611 e="luasec not found"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2612 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2613 if le and not o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2614 local e,t=le(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2615 if not e then return nil,t end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2616 if e[1]and e[1].family=="inet6"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2617 o="tcp6"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2618 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2619 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2620 local o=b[o or"tcp"]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2621 if f(o)~="function"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2622 e="invalid socket type"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2623 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2624 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2625 E("server.lua, addclient: ",e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2626 return nil,e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2627 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2628 local o,e=o()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2629 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2630 return nil,e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2631 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2632 o:settimeout(0)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2633 local h,e=o:connect(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2634 if h or e=="timeout"or e=="Operation already in progress"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2635 return n(o,a,t,s,r,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2636 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2637 return nil,e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2638 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2639 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2640 local p=function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2641 local e=e.conn;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2642 o=s(r,e,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2643 a=s(i,e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2644 h[e]=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2645 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2646 local t=function(t,n,d)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2647 local e=t.conn
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2648 h[e]=t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2649 if n~=nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2650 if n then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2651 a=l(i,e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2652 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2653 o=s(r,e,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2654 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2655 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2656 if d~=nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2657 if d then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2658 o=l(r,e,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2659 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2660 a=s(i,e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2661 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2662 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2663 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2664 local a=function(e,a,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2665 local o=e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2666 if f(e)=="number"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2667 o={getfd=function()return e;end}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2668 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2669 local e={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2670 conn=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2671 readbuffer=a or O;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2672 sendbuffer=i or O;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2673 close=p;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2674 setflags=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2675 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2676 t(e,a,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2677 return e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2678 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2679 m"setmetatable"(h,{__mode="k"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2680 m"setmetatable"(v,{__mode="k"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2681 m"setmetatable"(w,{__mode="k"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2682 Q=P()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2683 local function t(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2684 local t=W;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2685 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2686 W=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2687 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2688 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2689 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2690 return{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2691 _addtimer=X,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2692 add_task=u;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2693 addclient=b,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2694 wrapclient=n,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2695 watchfd=a,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2696 loop=Z,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2697 link=z,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2698 step=y,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2699 stats=ie,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2700 closeall=B,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2701 addserver=ne,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2702 getserver=oe,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2703 setlogger=t,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2704 getsettings=ae,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2705 setquitting=c,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2706 removeserver=he,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2707 get_backend=g,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2708 changesettings=re,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2709 }
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2710 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2711 package.preload['util.xmppstream']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2712 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2713 local function o(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2714 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2715 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2716 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2717 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2718 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2719 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2720 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2721 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2722 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2723 local e=require"lxp";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2724 local t=require"util.stanza";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2725 local g=t.stanza_mt;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2726 local c=error;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2727 local t=tostring;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2728 local d=table.insert;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2729 local p=table.concat;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2730 local k=table.remove;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2731 local v=setmetatable;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2732 local q=pcall(e.new,{StartDoctypeDecl=false});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2733 local x=pcall(e.new,{XmlDecl=false});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2734 local a=not not e.new({}).getcurrentbytecount;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2735 local j=1024*1024*10;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2736 o"xmppstream"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2737 local b=e.new;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2738 local z={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2739 ["http://www.w3.org/XML/1998/namespace\1lang"]="xml:lang";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2740 ["http://www.w3.org/XML/1998/namespace\1space"]="xml:space";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2741 ["http://www.w3.org/XML/1998/namespace\1base"]="xml:base";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2742 ["http://www.w3.org/XML/1998/namespace\1id"]="xml:id";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2743 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2744 local o="http://etherx.jabber.org/streams";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2745 local r="\1";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2746 local y="^([^"..r.."]*)"..r.."?(.*)$";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2747 _M.ns_separator=r;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2748 _M.ns_pattern=y;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2749 local function h()end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2750 function new_sax_handlers(n,e,s)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2751 local i={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2752 local w=e.streamopened;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2753 local f=e.streamclosed;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2754 local l=e.error or function(o,a,e)c("XML stream error: "..t(a)..(e and": "..t(e)or""),2);end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2755 local b=e.handlestanza;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2756 s=s or h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2757 local t=e.stream_ns or o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2758 local m=e.stream_tag or"stream";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2759 if t~=""then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2760 m=t..r..m;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2761 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2762 local j=t..r..(e.error_tag or"error");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2763 local _=e.default_ns;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2764 local u={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2765 local h,e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2766 local t=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2767 local r=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2768 function i:StartElement(c,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2769 if e and#h>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2770 d(e,p(h));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2771 h={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2772 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2773 local h,i=c:match(y);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2774 if i==""then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2775 h,i="",h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2776 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2777 if h~=_ or r>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2778 o.xmlns=h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2779 r=r+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2780 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2781 for t=1,#o do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2782 local e=o[t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2783 o[t]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2784 local t=z[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2785 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2786 o[t]=o[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2787 o[e]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2788 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2789 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2790 if not e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2791 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2792 t=self:getcurrentbytecount();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2793 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2794 if n.notopen then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2795 if c==m then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2796 r=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2797 if w then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2798 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2799 s(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2800 t=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2801 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2802 w(n,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2803 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2804 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2805 l(n,"no-stream",c);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2806 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2807 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2808 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2809 if h=="jabber:client"and i~="iq"and i~="presence"and i~="message"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2810 l(n,"invalid-top-level-element");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2811 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2812 e=v({name=i,attr=o,tags={}},g);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2813 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2814 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2815 t=t+self:getcurrentbytecount();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2816 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2817 d(u,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2818 local t=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2819 e=v({name=i,attr=o,tags={}},g);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2820 d(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2821 d(t.tags,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2822 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2823 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2824 if x then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2825 function i:XmlDecl(e,e,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2826 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2827 s(self:getcurrentbytecount());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2828 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2829 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2830 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2831 function i:StartCdataSection()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2832 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2833 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2834 t=t+self:getcurrentbytecount();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2835 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2836 s(self:getcurrentbytecount());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2837 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2838 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2839 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2840 function i:EndCdataSection()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2841 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2842 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2843 t=t+self:getcurrentbytecount();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2844 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2845 s(self:getcurrentbytecount());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2846 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2847 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2848 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2849 function i:CharacterData(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2850 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2851 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2852 t=t+self:getcurrentbytecount();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2853 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2854 d(h,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2855 elseif a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2856 s(self:getcurrentbytecount());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2857 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2858 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2859 function i:EndElement(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2860 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2861 t=t+self:getcurrentbytecount()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2862 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2863 if r>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2864 r=r-1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2865 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2866 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2867 if#h>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2868 d(e,p(h));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2869 h={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2870 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2871 if#u==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2872 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2873 s(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2874 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2875 t=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2876 if o~=j then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2877 b(n,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2878 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2879 l(n,"stream-error",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2880 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2881 e=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2882 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2883 e=k(u);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2884 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2885 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2886 if f then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2887 f(n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2888 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2889 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2890 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2891 local function a(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2892 l(n,"parse-error","restricted-xml","Restricted XML, see RFC 6120 section 11.1.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2893 if not e.stop or not e:stop()then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2894 c("Failed to abort parsing");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2895 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2896 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2897 if q then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2898 i.StartDoctypeDecl=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2899 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2900 i.Comment=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2901 i.ProcessingInstruction=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2902 local function a()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2903 e,h,t=nil,{},0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2904 u={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2905 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2906 local function t(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2907 n=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2908 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2909 return i,{reset=a,set_session=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2910 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2911 function new(i,n,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2912 local e=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2913 local o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2914 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2915 function o(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2916 e=e-a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2917 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2918 t=t or j;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2919 elseif t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2920 c("Stanza size limits are not supported on this version of LuaExpat")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2921 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2922 local i,s=new_sax_handlers(i,n,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2923 local o=b(i,r,false);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2924 local n=o.parse;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2925 return{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2926 reset=function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2927 o=b(i,r,false);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2928 n=o.parse;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2929 e=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2930 s.reset();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2931 end,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2932 feed=function(s,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2933 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2934 e=e+#i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2935 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2936 local o,i=n(o,i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2937 if a and e>t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2938 return nil,"stanza-too-large";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2939 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2940 return o,i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2941 end,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2942 set_session=s.set_session;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2943 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2944 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2945 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2946 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2947 package.preload['util.jid']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2948 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2949 local function a(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2950 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2951 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2952 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2953 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2954 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2955 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2956 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2957 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2958 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2959 local t,h=string.match,string.sub;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2960 local r=require"util.encodings".stringprep.nodeprep;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2961 local d=require"util.encodings".stringprep.nameprep;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2962 local l=require"util.encodings".stringprep.resourceprep;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2963 local n={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2964 [" "]="\\20";['"']="\\22";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2965 ["&"]="\\26";["'"]="\\27";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2966 ["/"]="\\2f";[":"]="\\3a";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2967 ["<"]="\\3c";[">"]="\\3e";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2968 ["@"]="\\40";["\\"]="\\5c";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2969 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2970 local s={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2971 for t,e in pairs(n)do s[e]=t;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2972 a"jid"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2973 local function a(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2974 if not e then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2975 local i,a=t(e,"^([^@/]+)@()");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2976 local a,o=t(e,"^([^@/]+)()",a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2977 if i and not a then return nil,nil,nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2978 local t=t(e,"^/(.+)$",o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2979 if(not a)or((not t)and#e>=o)then return nil,nil,nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2980 return i,a,t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2981 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2982 split=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2983 function bare(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2984 local t,e=a(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2985 if t and e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2986 return t.."@"..e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2987 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2988 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2989 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2990 local function o(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2991 local t,e,a=a(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2992 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2993 if h(e,-1,-1)=="."then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2994 e=h(e,1,-2);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2995 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2996 e=d(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2997 if not e then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2998 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2999 t=r(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3000 if not t then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3001 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3002 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3003 a=l(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3004 if not a then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3005 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3006 return t,e,a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3007 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3008 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3009 prepped_split=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3010 function prep(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3011 local t,e,a=o(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3012 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3013 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3014 e=t.."@"..e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3015 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3016 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3017 e=e.."/"..a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3018 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3019 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3020 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3021 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3022 function join(t,e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3023 if t and e and a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3024 return t.."@"..e.."/"..a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3025 elseif t and e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3026 return t.."@"..e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3027 elseif e and a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3028 return e.."/"..a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3029 elseif e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3030 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3031 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3032 return nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3033 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3034 function compare(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3035 local n,o,i=a(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3036 local a,t,e=a(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3037 if((a~=nil and a==n)or a==nil)and
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3038 ((t~=nil and t==o)or t==nil)and
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3039 ((e~=nil and e==i)or e==nil)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3040 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3041 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3042 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3043 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3044 function escape(e)return e and(e:gsub(".",n));end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3045 function unescape(e)return e and(e:gsub("\\%x%x",s));end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3046 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3047 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3048 package.preload['util.events']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3049 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3050 local function a(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3051 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3052 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3053 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3054 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3055 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3056 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3057 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3058 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3059 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3060 local i=pairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3061 local o=table.insert;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3062 local n=table.sort;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3063 local s=setmetatable;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3064 local h=next;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3065 a"events"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3066 function new()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3067 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3068 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3069 local function r(s,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3070 local e=e[a];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3071 if not e or h(e)==nil then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3072 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3073 for e in i(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3074 o(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3075 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3076 n(t,function(a,t)return e[a]>e[t];end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3077 s[a]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3078 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3079 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3080 s(t,{__index=r});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3081 local function s(o,n,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3082 local a=e[o];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3083 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3084 a[n]=i or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3085 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3086 a={[n]=i or 0};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3087 e[o]=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3088 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3089 t[o]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3090 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3091 local function n(o,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3092 local a=e[o];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3093 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3094 a[i]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3095 t[o]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3096 if h(a)==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3097 e[o]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3098 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3099 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3100 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3101 local function h(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3102 for t,e in i(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3103 s(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3104 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3105 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3106 local function o(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3107 for e,t in i(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3108 n(e,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3109 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3110 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3111 local function a(e,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3112 local e=t[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3113 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3114 for t=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3115 local e=e[t](...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3116 if e~=nil then return e;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3117 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3118 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3119 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3120 return{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3121 add_handler=s;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3122 remove_handler=n;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3123 add_handlers=h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3124 remove_handlers=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3125 fire_event=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3126 _handlers=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3127 _event_map=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3128 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3129 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3130 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3131 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3132 package.preload['util.dataforms']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3133 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3134 local function o(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3135 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3136 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3137 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3138 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3139 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3140 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3141 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3142 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3143 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3144 local a=setmetatable;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3145 local e,i=pairs,ipairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3146 local n,s,c=tostring,type,next;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3147 local r=table.concat;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3148 local u=require"util.stanza";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3149 local d=require"util.jid".prep;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3150 o"dataforms"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3151 local l='jabber:x:data';
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3152 local h={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3153 local t={__index=h};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3154 function new(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3155 return a(e,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3156 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3157 function from_stanza(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3158 local o={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3159 title=e:get_child_text("title");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3160 instructions=e:get_child_text("instructions");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3161 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3162 for e in e:childtags("field")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3163 local a={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3164 name=e.attr.var;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3165 label=e.attr.label;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3166 type=e.attr.type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3167 required=e:get_child("required")and true or nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3168 value=e:get_child_text("value");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3169 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3170 o[#o+1]=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3171 if a.type then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3172 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3173 if a.type:match"list%-"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3174 for e in e:childtags("option")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3175 t[#t+1]={label=e.attr.label,value=e:get_child_text("value")};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3176 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3177 for e in e:childtags("value")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3178 t[#t+1]={label=e.attr.label,value=e:get_text(),default=true};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3179 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3180 elseif a.type:match"%-multi"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3181 for e in e:childtags("value")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3182 t[#t+1]=e.attr.label and{label=e.attr.label,value=e:get_text()}or e:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3183 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3184 if a.type=="text-multi"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3185 a.value=r(t,"\n");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3186 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3187 a.value=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3188 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3189 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3190 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3191 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3192 return new(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3193 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3194 function h.form(t,h,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3195 local e=u.stanza("x",{xmlns=l,type=e or"form"});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3196 if t.title then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3197 e:tag("title"):text(t.title):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3198 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3199 if t.instructions then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3200 e:tag("instructions"):text(t.instructions):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3201 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3202 for t,o in i(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3203 local a=o.type or"text-single";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3204 e:tag("field",{type=a,var=o.name,label=o.label});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3205 local t=(h and h[o.name])or o.value;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3206 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3207 if a=="hidden"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3208 if s(t)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3209 e:tag("value")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3210 :add_child(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3211 :up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3212 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3213 e:tag("value"):text(n(t)):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3214 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3215 elseif a=="boolean"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3216 e:tag("value"):text((t and"1")or"0"):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3217 elseif a=="fixed"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3218 elseif a=="jid-multi"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3219 for a,t in i(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3220 e:tag("value"):text(t):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3221 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3222 elseif a=="jid-single"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3223 e:tag("value"):text(t):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3224 elseif a=="text-single"or a=="text-private"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3225 e:tag("value"):text(t):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3226 elseif a=="text-multi"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3227 for t in t:gmatch("([^\r\n]+)\r?\n*")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3228 e:tag("value"):text(t):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3229 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3230 elseif a=="list-single"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3231 local a=false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3232 for o,t in i(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3233 if s(t)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3234 e:tag("option",{label=t.label}):tag("value"):text(t.value):up():up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3235 if t.default and(not a)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3236 e:tag("value"):text(t.value):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3237 a=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3238 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3239 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3240 e:tag("option",{label=t}):tag("value"):text(n(t)):up():up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3241 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3242 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3243 elseif a=="list-multi"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3244 for a,t in i(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3245 if s(t)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3246 e:tag("option",{label=t.label}):tag("value"):text(t.value):up():up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3247 if t.default then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3248 e:tag("value"):text(t.value):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3249 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3250 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3251 e:tag("option",{label=t}):tag("value"):text(n(t)):up():up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3252 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3253 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3254 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3255 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3256 if o.required then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3257 e:tag("required"):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3258 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3259 e:up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3260 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3261 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3262 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3263 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3264 function h.data(t,n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3265 local o={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3266 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3267 for i,t in i(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3268 local i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3269 for e in n:childtags()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3270 if t.name==e.attr.var then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3271 i=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3272 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3273 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3274 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3275 if not i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3276 if t.required then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3277 a[t.name]="Required value missing";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3278 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3279 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3280 local e=e[t.type];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3281 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3282 o[t.name],a[t.name]=e(i,t.required);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3283 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3284 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3285 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3286 if c(a)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3287 return o,a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3288 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3289 return o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3290 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3291 e["text-single"]=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3292 function(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3293 local t=t:get_child_text("value");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3294 if t and#t>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3295 return t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3296 elseif a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3297 return nil,"Required value missing";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3298 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3299 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3300 e["text-private"]=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3301 e["text-single"];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3302 e["jid-single"]=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3303 function(t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3304 local a=t:get_child_text("value")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3305 local t=d(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3306 if t and#t>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3307 return t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3308 elseif a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3309 return nil,"Invalid JID: "..a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3310 elseif o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3311 return nil,"Required value missing";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3312 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3313 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3314 e["jid-multi"]=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3315 function(o,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3316 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3317 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3318 for e in o:childtags("value")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3319 local e=e:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3320 local o=d(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3321 a[#a+1]=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3322 if e and not o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3323 t[#t+1]=("Invalid JID: "..e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3324 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3325 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3326 if#a>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3327 return a,(#t>0 and r(t,"\n")or nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3328 elseif i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3329 return nil,"Required value missing";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3330 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3331 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3332 e["list-multi"]=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3333 function(a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3334 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3335 for e in a:childtags("value")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3336 t[#t+1]=e:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3337 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3338 return t,(o and#t==0 and"Required value missing"or nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3339 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3340 e["text-multi"]=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3341 function(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3342 local t,a=e["list-multi"](t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3343 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3344 t=r(t,"\n");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3345 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3346 return t,a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3347 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3348 e["list-single"]=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3349 e["text-single"];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3350 local a={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3351 ["1"]=true,["true"]=true,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3352 ["0"]=false,["false"]=false,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3353 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3354 e["boolean"]=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3355 function(t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3356 local t=t:get_child_text("value");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3357 local a=a[t~=nil and t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3358 if a~=nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3359 return a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3360 elseif t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3361 return nil,"Invalid boolean representation";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3362 elseif o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3363 return nil,"Required value missing";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3364 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3365 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3366 e["hidden"]=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3367 function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3368 return e:get_child_text("value");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3369 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3370 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3371 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3372 package.preload['util.caps']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3373 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3374 local function a(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3375 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3376 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3377 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3378 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3379 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3380 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3381 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3382 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3383 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3384 local d=require"util.encodings".base64.encode;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3385 local l=require"util.hashes".sha1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3386 local n,s,h=table.insert,table.sort,table.concat;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3387 local r=ipairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3388 a"caps"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3389 function calculate_hash(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3390 local i,o,a={},{},{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3391 for t,e in r(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3392 if e.name=="identity"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3393 n(i,(e.attr.category or"").."\0"..(e.attr.type or"").."\0"..(e.attr["xml:lang"]or"").."\0"..(e.attr.name or""));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3394 elseif e.name=="feature"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3395 n(o,e.attr.var or"");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3396 elseif e.name=="x"and e.attr.xmlns=="jabber:x:data"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3397 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3398 local o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3399 for a,e in r(e.tags)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3400 if e.name=="field"and e.attr.var then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3401 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3402 for t,e in r(e.tags)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3403 e=#e.tags==0 and e:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3404 if e then n(a,e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3405 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3406 s(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3407 if e.attr.var=="FORM_TYPE"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3408 o=a[1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3409 elseif#a>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3410 n(t,e.attr.var.."\0"..h(a,"<"));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3411 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3412 n(t,e.attr.var);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3413 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3414 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3415 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3416 s(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3417 t=h(t,"<");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3418 if o then t=o.."\0"..t;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3419 n(a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3420 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3421 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3422 s(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3423 s(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3424 s(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3425 if#i>0 then i=h(i,"<"):gsub("%z","/").."<";else i="";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3426 if#o>0 then o=h(o,"<").."<";else o="";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3427 if#a>0 then a=h(a,"<"):gsub("%z","<").."<";else a="";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3428 local e=i..o..a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3429 local t=d(l(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3430 return t,e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3431 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3432 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3433 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3434 package.preload['util.vcard']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3435 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3436 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3437 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3438 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3439 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3440 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3441 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3442 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3443 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3444 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3445 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3446 local n=require"util.stanza";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3447 local a,c=table.insert,table.concat;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3448 local h=type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3449 local e,r,f=next,pairs,ipairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3450 local d,l,u,m;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3451 local w="\n";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3452 local i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3453 local function e()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3454 error"Not implemented"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3455 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3456 local function e()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3457 error"Not implemented"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3458 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3459 local function y(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3460 return e:gsub("[,:;\\]","\\%1"):gsub("\n","\\n");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3461 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3462 local function s(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3463 return e:gsub("\\?[\\nt:;,]",{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3464 ["\\\\"]="\\",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3465 ["\\n"]="\n",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3466 ["\\r"]="\r",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3467 ["\\t"]="\t",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3468 ["\\:"]=":",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3469 ["\\;"]=";",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3470 ["\\,"]=",",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3471 [":"]="\29",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3472 [";"]="\30",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3473 [","]="\31",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3474 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3475 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3476 local function p(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3477 local a=n.stanza(e.name,{xmlns="vcard-temp"});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3478 local t=i[e.name];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3479 if t=="text"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3480 a:text(e[1]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3481 elseif h(t)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3482 if t.types and e.TYPE then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3483 if h(e.TYPE)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3484 for o,t in r(t.types)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3485 for o,e in r(e.TYPE)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3486 if e:upper()==t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3487 a:tag(t):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3488 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3489 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3490 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3491 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3492 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3493 a:tag(e.TYPE:upper()):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3494 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3495 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3496 if t.props then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3497 for o,t in r(t.props)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3498 if e[t]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3499 a:tag(t):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3500 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3501 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3502 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3503 if t.value then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3504 a:tag(t.value):text(e[1]):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3505 elseif t.values then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3506 local o=t.values;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3507 local i=o.behaviour=="repeat-last"and o[#o];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3508 for o=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3509 a:tag(t.values[o]or i):text(e[o]):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3510 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3511 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3512 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3513 return a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3514 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3515 local function o(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3516 local e=n.stanza("vCard",{xmlns="vcard-temp"});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3517 for a=1,#t do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3518 e:add_child(p(t[a]));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3519 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3520 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3521 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3522 function m(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3523 if not e[1]or e[1].name then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3524 return o(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3525 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3526 local t=n.stanza("xCard",{xmlns="vcard-temp"});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3527 for a=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3528 t:add_child(o(e[a]));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3529 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3530 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3531 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3532 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3533 function d(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3534 t=t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3535 :gsub("\r\n","\n")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3536 :gsub("\n ","")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3537 :gsub("\n\n+","\n");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3538 local h={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3539 local e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3540 for t in t:gmatch("[^\n]+")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3541 local t=s(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3542 local s,t,n=t:match("^([-%a]+)(\30?[^\29]*)\29(.*)$");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3543 n=n:gsub("\29",":");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3544 if#t>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3545 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3546 for e,o,i in t:gmatch("\30([^=]+)(=?)([^\30]*)")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3547 e=e:upper();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3548 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3549 for e in i:gmatch("[^\31]+")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3550 t[#t+1]=e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3551 t[e]=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3552 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3553 if o=="="then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3554 a[e]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3555 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3556 a[e]=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3557 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3558 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3559 t=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3560 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3561 if s=="BEGIN"and n=="VCARD"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3562 e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3563 h[#h+1]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3564 elseif s=="END"and n=="VCARD"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3565 e=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3566 elseif e and i[s]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3567 local o=i[s];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3568 local i={name=s};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3569 e[#e+1]=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3570 local s=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3571 e=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3572 if o.types then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3573 for o,a in f(o.types)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3574 local a=a:lower();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3575 if(t.TYPE and t.TYPE[a]==true)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3576 or t[a]==true then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3577 e.TYPE=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3578 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3579 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3580 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3581 if o.props then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3582 for o,a in f(o.props)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3583 if t[a]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3584 if t[a]==true then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3585 e[a]=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3586 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3587 for o,t in f(t[a])do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3588 e[a]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3589 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3590 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3591 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3592 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3593 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3594 if o=="text"or o.value then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3595 a(e,n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3596 elseif o.values then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3597 local t="\30"..n;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3598 for t in t:gmatch("\30([^\30]*)")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3599 a(e,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3600 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3601 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3602 e=s;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3603 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3604 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3605 return h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3606 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3607 local function n(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3608 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3609 for a=1,#t do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3610 e[a]=y(t[a]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3611 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3612 e=c(e,";");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3613 local a="";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3614 for t,e in r(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3615 if h(t)=="string"and t~="name"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3616 a=a..(";%s=%s"):format(t,h(e)=="table"and c(e,",")or e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3617 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3618 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3619 return("%s%s:%s"):format(t.name,a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3620 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3621 local function o(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3622 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3623 a(e,"BEGIN:VCARD")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3624 for o=1,#t do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3625 a(e,n(t[o]));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3626 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3627 a(e,"END:VCARD")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3628 return c(e,w);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3629 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3630 function l(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3631 if e[1]and e[1].name then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3632 return o(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3633 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3634 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3635 for t=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3636 a[t]=o(e[t]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3637 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3638 return c(a,w);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3639 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3640 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3641 local function n(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3642 local t=o.name;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3643 local e=i[t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3644 local t={name=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3645 if e=="text"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3646 t[1]=o:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3647 elseif h(e)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3648 if e.value then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3649 t[1]=o:get_child_text(e.value)or"";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3650 elseif e.values then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3651 local e=e.values;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3652 if e.behaviour=="repeat-last"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3653 for e=1,#o.tags do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3654 a(t,o.tags[e]:get_text()or"");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3655 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3656 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3657 for i=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3658 a(t,o:get_child_text(e[i])or"");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3659 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3660 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3661 elseif e.names then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3662 local e=e.names;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3663 for a=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3664 if o:get_child(e[a])then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3665 t[1]=e[a];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3666 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3667 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3668 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3669 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3670 if e.props_verbatim then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3671 for a,e in r(e.props_verbatim)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3672 t[a]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3673 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3674 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3675 if e.types then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3676 local e=e.types;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3677 t.TYPE={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3678 for i=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3679 if o:get_child(e[i])then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3680 a(t.TYPE,e[i]:lower());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3681 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3682 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3683 if#t.TYPE==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3684 t.TYPE=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3685 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3686 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3687 if e.props then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3688 local e=e.props;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3689 for i=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3690 local e=e[i]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3691 local o=o:get_child_text(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3692 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3693 t[e]=t[e]or{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3694 a(t[e],o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3695 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3696 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3697 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3698 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3699 return nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3700 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3701 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3702 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3703 local function o(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3704 local t=e.tags;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3705 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3706 for o=1,#t do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3707 a(e,n(t[o]));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3708 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3709 return e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3710 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3711 function u(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3712 if e.attr.xmlns~="vcard-temp"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3713 return nil,"wrong-xmlns";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3714 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3715 if e.name=="xCard"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3716 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3717 local e=e.tags;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3718 for a=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3719 t[a]=o(e[a]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3720 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3721 return t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3722 elseif e.name=="vCard"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3723 return o(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3724 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3725 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3726 i={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3727 VERSION="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3728 FN="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3729 N={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3730 values={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3731 "FAMILY",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3732 "GIVEN",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3733 "MIDDLE",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3734 "PREFIX",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3735 "SUFFIX",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3736 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3737 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3738 NICKNAME="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3739 PHOTO={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3740 props_verbatim={ENCODING={"b"}},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3741 props={"TYPE"},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3742 value="BINVAL",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3743 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3744 BDAY="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3745 ADR={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3746 types={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3747 "HOME",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3748 "WORK",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3749 "POSTAL",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3750 "PARCEL",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3751 "DOM",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3752 "INTL",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3753 "PREF",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3754 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3755 values={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3756 "POBOX",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3757 "EXTADD",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3758 "STREET",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3759 "LOCALITY",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3760 "REGION",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3761 "PCODE",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3762 "CTRY",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3763 }
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3764 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3765 LABEL={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3766 types={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3767 "HOME",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3768 "WORK",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3769 "POSTAL",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3770 "PARCEL",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3771 "DOM",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3772 "INTL",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3773 "PREF",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3774 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3775 value="LINE",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3776 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3777 TEL={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3778 types={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3779 "HOME",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3780 "WORK",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3781 "VOICE",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3782 "FAX",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3783 "PAGER",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3784 "MSG",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3785 "CELL",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3786 "VIDEO",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3787 "BBS",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3788 "MODEM",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3789 "ISDN",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3790 "PCS",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3791 "PREF",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3792 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3793 value="NUMBER",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3794 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3795 EMAIL={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3796 types={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3797 "HOME",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3798 "WORK",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3799 "INTERNET",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3800 "PREF",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3801 "X400",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3802 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3803 value="USERID",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3804 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3805 JABBERID="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3806 MAILER="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3807 TZ="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3808 GEO={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3809 values={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3810 "LAT",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3811 "LON",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3812 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3813 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3814 TITLE="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3815 ROLE="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3816 LOGO="copy of PHOTO",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3817 AGENT="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3818 ORG={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3819 values={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3820 behaviour="repeat-last",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3821 "ORGNAME",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3822 "ORGUNIT",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3823 }
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3824 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3825 CATEGORIES={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3826 values="KEYWORD",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3827 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3828 NOTE="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3829 PRODID="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3830 REV="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3831 SORTSTRING="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3832 SOUND="copy of PHOTO",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3833 UID="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3834 URL="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3835 CLASS={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3836 names={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3837 "PUBLIC",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3838 "PRIVATE",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3839 "CONFIDENTIAL",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3840 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3841 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3842 KEY={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3843 props={"TYPE"},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3844 value="CRED",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3845 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3846 DESC="text",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3847 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3848 i.LOGO=i.PHOTO;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3849 i.SOUND=i.PHOTO;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3850 return{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3851 from_text=d;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3852 to_text=l;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3853 from_xep54=u;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3854 to_xep54=m;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3855 lua_to_text=l;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3856 lua_to_xep54=m;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3857 text_to_lua=d;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3858 text_to_xep54=function(...)return m(d(...));end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3859 xep54_to_lua=u;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3860 xep54_to_text=function(...)return l(u(...))end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3861 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3862 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3863 package.preload['util.logger']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3864 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3865 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3866 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3867 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3868 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3869 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3870 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3871 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3872 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3873 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3874 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3875 local e=pcall;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3876 local e=string.find;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3877 local e,i,e=ipairs,pairs,setmetatable;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3878 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3879 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3880 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3881 function a.init(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3882 local o=t(e,"debug");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3883 local a=t(e,"info");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3884 local i=t(e,"warn");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3885 local n=t(e,"error");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3886 return function(e,t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3887 if e=="debug"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3888 return o(t,...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3889 elseif e=="info"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3890 return a(t,...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3891 elseif e=="warn"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3892 return i(t,...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3893 elseif e=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3894 return n(t,...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3895 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3896 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3897 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3898 function t(i,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3899 local t=e[a];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3900 if not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3901 t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3902 e[a]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3903 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3904 local e=function(o,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3905 for e=1,#t do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3906 t[e](i,a,o,...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3907 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3908 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3909 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3910 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3911 function a.reset()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3912 for t,e in i(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3913 for t=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3914 e[t]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3915 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3916 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3917 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3918 function a.add_level_sink(t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3919 if not e[t]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3920 e[t]={o};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3921 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3922 e[t][#e[t]+1]=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3923 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3924 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3925 a.new=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3926 return a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3927 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3928 package.preload['util.datetime']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3929 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3930 local function a(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3931 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3932 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3933 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3934 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3935 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3936 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3937 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3938 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3939 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3940 local e=os.date;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3941 local i=os.time;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3942 local u=os.difftime;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3943 local t=error;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3944 local r=tonumber;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3945 a"datetime"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3946 function date(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3947 return e("!%Y-%m-%d",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3948 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3949 function datetime(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3950 return e("!%Y-%m-%dT%H:%M:%SZ",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3951 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3952 function time(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3953 return e("!%H:%M:%S",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3954 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3955 function legacy(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3956 return e("!%Y%m%dT%H:%M:%S",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3957 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3958 function parse(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3959 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3960 local n,d,l,s,h,a,o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3961 n,d,l,s,h,a,o=t:match("^(%d%d%d%d)%-?(%d%d)%-?(%d%d)T(%d%d):(%d%d):(%d%d)%.?%d*([Z+%-]?.*)$");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3962 if n then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3963 local u=u(i(e("*t")),i(e("!*t")));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3964 local t=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3965 if o~=""and o~="Z"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3966 local o,a,e=o:match("([+%-])(%d%d):?(%d*)");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3967 if not o then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3968 if#e~=2 then e="0";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3969 a,e=r(a),r(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3970 t=a*60*60+e*60;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3971 if o=="-"then t=-t;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3972 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3973 a=(a+u)-t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3974 return i({year=n,month=d,day=l,hour=s,min=h,sec=a,isdst=false});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3975 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3976 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3977 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3978 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3979 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3980 package.preload['util.json']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3981 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3982 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3983 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3984 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3985 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3986 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3987 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3988 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3989 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3990 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3991 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3992 local y=type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3993 local t,p,v,j=table.insert,table.concat,table.remove,table.sort;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3994 local s=string.char;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3995 local q,m=tostring,tonumber;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3996 local u,r=pairs,ipairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3997 local n=next;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3998 local e=error;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3999 local e,h,g=newproxy,getmetatable,setmetatable;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4000 local c=print;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4001 local a,o=pcall(require,"util.array");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4002 local f=a and h(o())or{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4003 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4004 local i=e and e(true)or{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4005 if h and h(i)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4006 h(i).__tostring=function()return"null";end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4007 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4008 a.null=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4009 local w={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4010 ["\""]="\\\"",["\\"]="\\\\",["\b"]="\\b",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4011 ["\f"]="\\f",["\n"]="\\n",["\r"]="\\r",["\t"]="\\t"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4012 local e={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4013 ["\""]="\"",["\\"]="\\",["/"]="/",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4014 b="\b",f="\f",n="\n",r="\r",t="\t"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4015 for t=0,31 do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4016 local e=s(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4017 if not w[e]then w[e]=("\\u%.4X"):format(t);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4018 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4019 local function x(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4020 if e<128 then return s(e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4021 local t=e%64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4022 if e<2048 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4023 local e=(e-t)/64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4024 return s(128+64+e,128+t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4025 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4026 local a=e%4096;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4027 local o=(a-t)/64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4028 local e=(e-a)/4096;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4029 return s(128+64+32+e,128+o,128+t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4030 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4031 local k={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4032 number=true,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4033 string=true,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4034 table=true,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4035 boolean=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4036 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4037 local z={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4038 __array=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4039 __hash=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4040 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4041 local o,b,d,l;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4042 function l(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4043 t(a,"\""..(e:gsub(".",w)).."\"");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4044 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4045 function d(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4046 t(e,"[");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4047 if n(a)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4048 for i,a in r(a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4049 o(a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4050 t(e,",");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4051 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4052 v(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4053 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4054 t(e,"]");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4055 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4056 function b(c,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4057 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4058 local h={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4059 local s={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4060 for e,t in r(c)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4061 a[e]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4062 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4063 for e,t in u(c)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4064 local o,n=y(e),y(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4065 if k[n]or t==i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4066 if o=="string"and not z[e]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4067 s[e]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4068 elseif(k[o]or e==i)and a[e]==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4069 h[e]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4070 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4071 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4072 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4073 if n(h)~=nil or n(s)~=nil or n(a)==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4074 t(e,"{");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4075 local c=#e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4076 if e.ordered then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4077 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4078 for e in u(s)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4079 t(a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4080 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4081 j(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4082 for i,a in r(a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4083 l(a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4084 t(e,":");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4085 o(s[a],e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4086 t(e,",");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4087 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4088 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4089 for i,a in u(s)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4090 l(i,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4091 t(e,":");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4092 o(a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4093 t(e,",");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4094 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4095 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4096 if n(h)~=nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4097 t(e,"\"__hash\":[");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4098 for i,a in u(h)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4099 o(i,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4100 t(e,",");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4101 o(a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4102 t(e,",");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4103 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4104 v(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4105 t(e,"]");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4106 t(e,",");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4107 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4108 if n(a)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4109 t(e,"\"__array\":");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4110 d(a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4111 t(e,",");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4112 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4113 if c~=#e then v(e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4114 t(e,"}");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4115 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4116 d(a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4117 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4118 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4119 function o(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4120 local o=y(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4121 if o=="number"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4122 t(a,q(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4123 elseif o=="string"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4124 l(e,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4125 elseif o=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4126 local t=h(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4127 if t==f then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4128 d(e,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4129 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4130 b(e,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4131 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4132 elseif o=="boolean"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4133 t(a,(e and"true"or"false"));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4134 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4135 t(a,"null");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4136 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4137 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4138 function a.encode(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4139 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4140 o(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4141 return p(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4142 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4143 function a.encode_ordered(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4144 local e={ordered=true};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4145 o(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4146 return p(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4147 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4148 function a.encode_array(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4149 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4150 d(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4151 return p(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4152 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4153 local function o(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4154 return t:find("[^ \t\r\n]",e)or e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4155 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4156 local function d(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4157 local a=e.__array;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4158 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4159 e.__array=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4160 for o,a in r(a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4161 t(e,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4162 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4163 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4164 local a=e.__hash;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4165 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4166 e.__hash=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4167 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4168 for o,a in r(a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4169 if t~=nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4170 e[t]=a;t=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4171 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4172 t=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4173 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4174 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4175 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4176 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4177 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4178 local n,h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4179 local function u(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4180 local s={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4181 while true do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4182 local a,i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4183 e=o(t,e+1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4184 if t:byte(e)~=34 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4185 if t:byte(e)==125 then return s,e+1;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4186 return nil,"key expected";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4187 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4188 a,e=h(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4189 if a==nil then return nil,e;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4190 e=o(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4191 if t:byte(e)~=58 then return nil,"colon expected";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4192 i,e=n(t,e+1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4193 if i==nil then return nil,e;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4194 s[a]=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4195 e=o(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4196 local t=t:byte(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4197 if t==125 then return d(s),e+1;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4198 if t~=44 then return nil,"object eof";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4199 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4200 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4201 local function l(i,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4202 local s={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4203 local h=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4204 while true do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4205 local a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4206 a,e=n(i,e+1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4207 if a==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4208 if i:byte(h+1)==93 then return g(s,f),h+2;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4209 return a,e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4210 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4211 t(s,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4212 e=o(i,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4213 local t=i:byte(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4214 if t==93 then return g(s,f),e+1;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4215 if t~=44 then return nil,"array eof";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4216 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4217 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4218 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4219 local function e(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4220 local t,e=m(e:sub(3,6),16),m(e:sub(9,12),16);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4221 local e=t*1024+e-56613888;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4222 local o=e%64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4223 e=(e-o)/64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4224 local t=e%64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4225 e=(e-t)/64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4226 local a=e%64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4227 e=(e-a)/64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4228 return s(240+e,128+a,128+t,128+o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4229 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4230 local function s(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4231 e=e:match("%x%x%x%x",3);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4232 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4233 return x(m(e,16));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4234 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4235 t=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4236 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4237 function h(o,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4238 e=e+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4239 local a=o:find("\"",e,true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4240 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4241 local e=o:sub(e,a-1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4242 t=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4243 e=e:gsub("\\u.?.?.?.?",s);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4244 if t then return nil,"invalid escape";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4245 return e,a+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4246 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4247 return nil,"string eof";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4248 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4249 local function d(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4250 local t=t:match("[0-9%.%-eE%+]+",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4251 return m(t),e+#t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4252 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4253 local function s(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4254 local o,a,t=t:byte(e+1,e+3);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4255 if o==117 and a==108 and t==108 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4256 return i,e+4;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4257 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4258 return nil,"null parse failed";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4259 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4260 local function i(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4261 local t,a,o=t:byte(e+1,e+3);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4262 if t==114 and a==117 and o==101 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4263 return true,e+4;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4264 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4265 return nil,"true parse failed";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4266 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4267 local function r(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4268 local i,o,a,t=t:byte(e+1,e+4);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4269 if i==97 and o==108 and a==115 and t==101 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4270 return false,e+5;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4271 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4272 return nil,"false parse failed";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4273 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4274 function n(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4275 t=o(a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4276 local e=a:byte(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4277 if e==123 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4278 return u(a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4279 elseif e==91 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4280 return l(a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4281 elseif e==34 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4282 return h(a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4283 elseif e~=nil and e>=48 and e<=57 or e==45 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4284 return d(a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4285 elseif e==110 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4286 return s(a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4287 elseif e==116 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4288 return i(a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4289 elseif e==102 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4290 return r(a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4291 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4292 return nil,"value expected";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4293 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4294 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4295 local t={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4296 ["\\\""]="\\u0022";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4297 ["\\\\"]="\\u005c";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4298 ["\\/"]="\\u002f";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4299 ["\\b"]="\\u0008";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4300 ["\\f"]="\\u000C";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4301 ["\\n"]="\\u000A";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4302 ["\\r"]="\\u000D";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4303 ["\\t"]="\\u0009";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4304 ["\\u"]="\\u";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4305 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4306 function a.decode(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4307 e=e:gsub("\\.",t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4308 local t,a=n(e,1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4309 if t==nil then return t,a;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4310 if e:find("[^ \t\r\n]",a)then return nil,"garbage at eof";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4311 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4312 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4313 function a.test(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4314 local e=a.encode(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4315 local t=a.decode(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4316 local t=a.encode(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4317 if e~=t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4318 c("FAILED");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4319 c("encoded:",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4320 c("recoded:",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4321 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4322 c(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4323 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4324 return e==t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4325 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4326 return a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4327 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4328 package.preload['util.xml']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4329 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4330 local function a(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4331 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4332 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4333 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4334 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4335 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4336 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4337 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4338 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4339 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4340 local t=require"util.stanza";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4341 local n=require"lxp";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4342 a("xml")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4343 local e=(function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4344 local s={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4345 ["http://www.w3.org/XML/1998/namespace"]="xml";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4346 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4347 local e="\1";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4348 local i="^([^"..e.."]*)"..e.."?(.*)$";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4349 return function(h)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4350 local o={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4351 local a=t.stanza("root");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4352 function o:StartElement(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4353 local t,o=t:match(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4354 if o==""then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4355 t,o="",t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4356 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4357 if t~=""then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4358 e.xmlns=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4359 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4360 for t=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4361 local a=e[t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4362 e[t]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4363 local t,o=a:match(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4364 if o~=""then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4365 t=s[t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4366 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4367 e[t..":"..o]=e[a];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4368 e[a]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4369 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4370 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4371 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4372 a:tag(o,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4373 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4374 function o:CharacterData(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4375 a:text(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4376 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4377 function o:EndElement(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4378 a:up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4379 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4380 local n=n.new(o,"\1");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4381 local e,i,o,t=n:parse(h);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4382 if e then e,i,o,t=n:parse();end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4383 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4384 return a.tags[1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4385 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4386 return e,i.." (line "..o..", col "..t..")";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4387 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4388 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4389 end)();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4390 parse=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4391 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4392 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4393 package.preload['util.rsm']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4394 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4395 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4396 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4397 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4398 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4399 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4400 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4401 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4402 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4403 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4404 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4405 local n=require"util.stanza".stanza;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4406 local a,o=tostring,tonumber;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4407 local s=type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4408 local h=pairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4409 local i='http://jabber.org/protocol/rsm';
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4410 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4411 do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4412 local e=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4413 local function t(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4414 return o((e:get_text()));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4415 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4416 local function a(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4417 return t:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4418 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4419 e.after=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4420 e.before=function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4421 local e=e:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4422 return e==""or e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4423 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4424 e.max=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4425 e.index=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4426 e.first=function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4427 return{index=o(e.attr.index);e:get_text()};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4428 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4429 e.last=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4430 e.count=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4431 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4432 local r=setmetatable({
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4433 first=function(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4434 if s(e)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4435 t:tag("first",{index=e.index}):text(e[1]):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4436 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4437 t:tag("first"):text(a(e)):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4438 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4439 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4440 before=function(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4441 if t==true then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4442 e:tag("before"):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4443 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4444 e:tag("before"):text(a(t)):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4445 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4446 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4447 },{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4448 __index=function(e,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4449 return function(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4450 t:tag(o):text(a(e)):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4451 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4452 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4453 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4454 local function o(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4455 local o={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4456 for a in e:childtags()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4457 local e=a.name;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4458 local t=e and t[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4459 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4460 o[e]=t(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4461 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4462 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4463 return o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4464 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4465 local function s(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4466 local a=n("set",{xmlns=i});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4467 for e,o in h(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4468 if t[e]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4469 r[e](a,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4470 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4471 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4472 return a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4473 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4474 local function t(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4475 local e=e:get_child("set",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4476 if e and#e.tags>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4477 return o(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4478 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4479 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4480 return{parse=o,generate=s,get=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4481 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4482 package.preload['util.random']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4483 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4484 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4485 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4486 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4487 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4488 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4489 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4490 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4491 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4492 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4493 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4494 local e=io.open("/dev/urandom","r");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4495 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4496 return{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4497 seed=function()end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4498 bytes=function(t)return e:read(t);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4499 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4500 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4501 local e=require"crypto"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4502 return e.rand;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4503 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4504 package.preload['util.ip']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4505 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4506 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4507 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4508 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4509 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4510 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4511 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4512 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4513 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4514 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4515 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4516 local o={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4517 local i={__index=function(e,t)return(o[t])(e);end,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4518 __tostring=function(e)return e.addr;end,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4519 __eq=function(t,e)return t.addr==e.addr;end};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4520 local n={["0"]="0000",["1"]="0001",["2"]="0010",["3"]="0011",["4"]="0100",["5"]="0101",["6"]="0110",["7"]="0111",["8"]="1000",["9"]="1001",["A"]="1010",["B"]="1011",["C"]="1100",["D"]="1101",["E"]="1110",["F"]="1111"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4521 local function e(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4522 if not e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4523 local a=t:match("^%x+(.)");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4524 if a==":"or(not(a)and t:sub(1,1)==":")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4525 e="IPv6"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4526 elseif a=="."then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4527 e="IPv4"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4528 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4529 if not e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4530 return nil,"invalid address";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4531 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4532 elseif e~="IPv4"and e~="IPv6"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4533 return nil,"invalid protocol";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4534 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4535 local a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4536 if e=="IPv6"and t:find('%',1,true)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4537 t,a=t:match("^(.-)%%(.*)");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4538 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4539 if e=="IPv6"and t:find('.',1,true)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4540 local e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4541 t,e=t:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$",function(t,e,o,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4542 return(":%04X:%04X"):format(t*256+e,o*256+a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4543 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4544 if e~=1 then return nil,"invalid-address";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4545 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4546 return setmetatable({addr=t,proto=e,zone=a},i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4547 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4548 local function i(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4549 local t="";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4550 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4551 if e.proto=="IPv4"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4552 e=e.toV4mapped;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4553 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4554 e=(e.addr):upper();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4555 e:gsub("([^:]*):?",function(e)a[#a+1]=e end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4556 if not e:match(":$")then a[#a]=nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4557 for o,e in ipairs(a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4558 if e:len()==0 and o~=1 and o~=#a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4559 for e=1,16*(9-#a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4560 t=t.."0";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4561 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4562 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4563 for e=1,4-e:len()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4564 t=t.."0000";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4565 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4566 for a=1,e:len()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4567 t=t..n[e:sub(a,a)];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4568 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4569 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4570 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4571 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4572 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4573 local function t(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4574 a,t=i(a),i(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4575 for e=1,128 do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4576 if a:sub(e,e)~=t:sub(e,e)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4577 return e-1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4578 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4579 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4580 return 128;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4581 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4582 local function r(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4583 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4584 t:gsub("([^.]*).?",function(t)e[#e+1]=tonumber(t)end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4585 if e[1]==127 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4586 return 2;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4587 elseif e[1]==169 and e[2]==254 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4588 return 2;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4589 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4590 return 14;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4591 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4592 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4593 local function h(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4594 if e:match("^[0:]*1$")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4595 return 2;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4596 elseif e:match("^[Ff][Ee][89ABab]")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4597 return 2;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4598 elseif e:match("^[Ff][Ee][CcDdEeFf]")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4599 return 5;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4600 elseif e:match("^[Ff][Ff]")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4601 return tonumber("0x"..e:sub(4,4));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4602 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4603 return 14;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4604 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4605 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4606 local function i(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4607 if t(a,e("::1","IPv6"))==128 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4608 return 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4609 elseif t(a,e("2002::","IPv6"))>=16 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4610 return 2;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4611 elseif t(a,e("2001::","IPv6"))>=32 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4612 return 5;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4613 elseif t(a,e("fc00::","IPv6"))>=7 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4614 return 13;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4615 elseif t(a,e("fec0::","IPv6"))>=10 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4616 return 11;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4617 elseif t(a,e("3ffe::","IPv6"))>=16 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4618 return 12;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4619 elseif t(a,e("::","IPv6"))>=96 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4620 return 3;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4621 elseif t(a,e("::ffff:0:0","IPv6"))>=96 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4622 return 4;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4623 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4624 return 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4625 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4626 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4627 local function n(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4628 if t(a,e("::1","IPv6"))==128 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4629 return 50;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4630 elseif t(a,e("2002::","IPv6"))>=16 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4631 return 30;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4632 elseif t(a,e("2001::","IPv6"))>=32 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4633 return 5;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4634 elseif t(a,e("fc00::","IPv6"))>=7 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4635 return 3;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4636 elseif t(a,e("fec0::","IPv6"))>=10 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4637 return 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4638 elseif t(a,e("3ffe::","IPv6"))>=16 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4639 return 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4640 elseif t(a,e("::","IPv6"))>=96 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4641 return 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4642 elseif t(a,e("::ffff:0:0","IPv6"))>=96 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4643 return 35;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4644 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4645 return 40;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4646 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4647 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4648 local function s(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4649 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4650 local t="::ffff:";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4651 o:gsub("([^.]*).?",function(e)a[#a+1]=tonumber(e)end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4652 t=t..("%02x"):format(a[1]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4653 t=t..("%02x"):format(a[2]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4654 t=t..":"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4655 t=t..("%02x"):format(a[3]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4656 t=t..("%02x"):format(a[4]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4657 return e(t,"IPv6");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4658 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4659 function o:toV4mapped()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4660 if self.proto~="IPv4"then return nil,"No IPv4 address"end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4661 local e=s(self.addr);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4662 self.toV4mapped=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4663 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4664 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4665 function o:label()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4666 local e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4667 if self.proto=="IPv4"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4668 e=i(self.toV4mapped);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4669 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4670 e=i(self);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4671 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4672 self.label=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4673 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4674 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4675 function o:precedence()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4676 local e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4677 if self.proto=="IPv4"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4678 e=n(self.toV4mapped);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4679 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4680 e=n(self);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4681 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4682 self.precedence=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4683 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4684 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4685 function o:scope()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4686 local e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4687 if self.proto=="IPv4"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4688 e=r(self.addr);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4689 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4690 e=h(self.addr);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4691 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4692 self.scope=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4693 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4694 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4695 return{new_ip=e,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4696 commonPrefixLength=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4697 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4698 package.preload['util.time']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4699 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4700 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4701 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4702 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4703 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4704 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4705 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4706 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4707 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4708 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4709 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4710 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4711 package.preload['util.sasl.scram']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4712 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4713 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4714 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4715 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4716 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4717 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4718 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4719 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4720 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4721 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4722 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4723 local n,m=require"mime".b64,require"mime".unb64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4724 local e=require"util.hashes";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4725 local a=require"bit";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4726 local s=require"util.random";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4727 local u=tonumber;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4728 local o,t=string.char,string.byte;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4729 local i=string.gsub;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4730 local a=a.bxor;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4731 local function c(n,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4732 return(i(n,"()(.)",function(n,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4733 return o(a(t(i),t(e,n)))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4734 end));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4735 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4736 local w,t=e.sha1,e.hmac_sha1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4737 local function y(o,e,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4738 local e=t(o,e.."\0\0\0\1");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4739 local a=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4740 for i=2,i do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4741 e=t(o,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4742 a=c(a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4743 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4744 return a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4745 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4746 local function f(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4747 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4748 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4749 local function a(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4750 return(i(e,"[,=]",{[","]="=2C",["="]="=3D"}));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4751 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4752 local function d(e,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4753 local a="n="..a(e.username);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4754 local d=n(s.bytes(15));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4755 local r="r="..d;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4756 local h=a..","..r;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4757 local i="";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4758 local a=e.conn:ssl()and"y"or"n";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4759 if o=="SCRAM-SHA-1-PLUS"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4760 i=e.conn:socket():getfinished();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4761 a="p=tls-unique";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4762 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4763 local s=a..",,";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4764 local a=s..h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4765 local a,l=coroutine.yield(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4766 if a~="challenge"then return false end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4767 local a,o,p=l:match("(r=[^,]+),s=([^,]*),i=(%d+)");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4768 local u=u(p);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4769 o=m(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4770 if not a or not o or not u then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4771 return false,"Could not parse server_first_message";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4772 elseif a:find(d,3,true)~=3 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4773 return false,"nonce sent by server does not match our nonce";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4774 elseif a==r then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4775 return false,"server did not append s-nonce to nonce";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4776 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4777 local i=s..i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4778 local i="c="..n(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4779 local r=i..","..a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4780 local a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4781 local i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4782 local s;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4783 if e.client_key and e.server_key then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4784 i=e.client_key;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4785 s=e.server_key;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4786 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4787 if e.salted_password then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4788 a=e.salted_password;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4789 elseif e.password then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4790 a=y(f(e.password),o,u);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4791 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4792 s=t(a,"Server Key");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4793 i=t(a,"Client Key");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4794 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4795 local a=w(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4796 local e=h..","..l..","..r;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4797 local a=t(a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4798 local a=c(i,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4799 local o=t(s,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4800 local e="p="..n(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4801 local e=r..","..e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4802 local t,e=coroutine.yield(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4803 if t~="success"then return false,"success-expected"end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4804 local e=e:match("v=([^,]+)");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4805 if m(e)~=o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4806 return false,"server signature did not match";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4807 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4808 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4809 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4810 return function(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4811 if e.username and(e.password or(e.client_key or e.server_key))then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4812 if t=="SCRAM-SHA-1"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4813 return d,99;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4814 elseif t=="SCRAM-SHA-1-PLUS"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4815 local e=e.conn:ssl()and e.conn:socket();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4816 if e and e.getfinished then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4817 return d,100;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4818 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4819 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4820 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4821 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4822 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4823 package.preload['util.sasl.plain']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4824 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4825 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4826 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4827 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4828 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4829 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4830 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4831 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4832 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4833 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4834 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4835 return function(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4836 if t=="PLAIN"and e.username and e.password then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4837 return function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4838 return"success"==coroutine.yield((e.authzid or"").."\0"..e.username.."\0"..e.password);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4839 end,5;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4840 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4841 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4842 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4843 package.preload['util.sasl.anonymous']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4844 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4845 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4846 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4847 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4848 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4849 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4850 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4851 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4852 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4853 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4854 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4855 return function(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4856 if e=="ANONYMOUS"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4857 return function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4858 return coroutine.yield()=="success";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4859 end,0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4860 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4861 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4862 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4863 package.preload['verse.plugins.tls']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4864 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4865 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4866 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4867 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4868 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4869 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4870 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4871 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4872 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4873 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4874 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4875 local a=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4876 local t="urn:ietf:params:xml:ns:xmpp-tls";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4877 function a.plugins.tls(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4878 local function i(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4879 if e.authenticated then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4880 if o:get_child("starttls",t)and e.conn.starttls then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4881 e:debug("Negotiating TLS...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4882 e:send(a.stanza("starttls",{xmlns=t}));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4883 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4884 elseif not e.conn.starttls and not e.secure then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4885 e:warn("SSL library (LuaSec) not loaded, so TLS not available");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4886 elseif not e.secure then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4887 e:debug("Server doesn't offer TLS :(");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4888 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4889 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4890 local function o(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4891 if t.name=="proceed"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4892 e:debug("Server says proceed, handshake starting...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4893 e.conn:starttls(e.ssl or{mode="client",protocol="sslv23",options="no_sslv2",capath="/etc/ssl/certs"},true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4894 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4895 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4896 local function a(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4897 if t=="ssl-handshake-complete"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4898 e.secure=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4899 e:debug("Re-opening stream...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4900 e:reopen();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4901 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4902 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4903 e:hook("stream-features",i,400);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4904 e:hook("stream/"..t,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4905 e:hook("status",a,400);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4906 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4907 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4908 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4909 package.preload['verse.plugins.sasl']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4910 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4911 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4912 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4913 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4914 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4915 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4916 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4917 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4918 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4919 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4920 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4921 local n=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4922 local s,h=require"mime".b64,require"mime".unb64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4923 local a="urn:ietf:params:xml:ns:xmpp-sasl";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4924 function n.plugins.sasl(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4925 local function r(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4926 if e.authenticated then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4927 e:debug("Authenticating with SASL...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4928 local t=t:get_child("mechanisms",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4929 if not t then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4930 local o={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4931 local i={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4932 for t in t:childtags("mechanism")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4933 t=t:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4934 e:debug("Server offers %s",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4935 if not o[t]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4936 local n=t:match("[^-]+");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4937 local s,a=pcall(require,"util.sasl."..n:lower());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4938 if s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4939 e:debug("Loaded SASL %s module",n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4940 o[t],i[t]=a(e,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4941 elseif not tostring(a):match("not found")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4942 e:debug("Loading failed: %s",tostring(a));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4943 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4944 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4945 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4946 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4947 for e in pairs(o)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4948 table.insert(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4949 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4950 if not t[1]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4951 e:event("authentication-failure",{condition="no-supported-sasl-mechanisms"});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4952 e:close();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4953 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4954 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4955 table.sort(t,function(e,t)return i[e]>i[t];end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4956 local t,i=t[1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4957 e:debug("Selecting %s mechanism...",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4958 e.sasl_mechanism=coroutine.wrap(o[t]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4959 i=e:sasl_mechanism(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4960 local t=n.stanza("auth",{xmlns=a,mechanism=t});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4961 if i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4962 t:text(s(i));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4963 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4964 e:send(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4965 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4966 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4967 local function i(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4968 if t.name=="failure"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4969 local a=t.tags[1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4970 local t=t:get_child_text("text");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4971 e:event("authentication-failure",{condition=a.name,text=t});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4972 e:close();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4973 return false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4974 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4975 local t,o=e.sasl_mechanism(t.name,h(t:get_text()));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4976 if not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4977 e:event("authentication-failure",{condition=o});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4978 e:close();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4979 return false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4980 elseif t==true then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4981 e:event("authentication-success");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4982 e.authenticated=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4983 e:reopen();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4984 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4985 e:send(n.stanza("response",{xmlns=a}):text(s(t)));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4986 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4987 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4988 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4989 e:hook("stream-features",r,300);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4990 e:hook("stream/"..a,i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4991 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4992 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4993 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4994 package.preload['verse.plugins.bind']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4995 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4996 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4997 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4998 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4999 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5000 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5001 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5002 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5003 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5004 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5005 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5006 local t=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5007 local o=require"util.jid";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5008 local a="urn:ietf:params:xml:ns:xmpp-bind";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5009 function t.plugins.bind(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5010 local function i(i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5011 if e.bound then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5012 e:debug("Binding resource...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5013 e:send_iq(t.iq({type="set"}):tag("bind",{xmlns=a}):tag("resource"):text(e.resource),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5014 function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5015 if t.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5016 local t=t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5017 :get_child("bind",a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5018 :get_child_text("jid");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5019 e.username,e.host,e.resource=o.split(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5020 e.jid,e.bound=t,true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5021 e:event("bind-success",{jid=t});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5022 elseif t.attr.type=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5023 local a=t:child_with_name("error");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5024 local t,a,o=t:get_error();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5025 e:event("bind-failure",{error=a,text=o,type=t});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5026 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5027 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5028 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5029 e:hook("stream-features",i,200);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5030 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5031 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5032 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5033 package.preload['verse.plugins.session']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5034 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5035 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5036 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5037 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5038 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5039 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5040 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5041 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5042 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5043 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5044 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5045 local a=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5046 local t="urn:ietf:params:xml:ns:xmpp-session";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5047 function a.plugins.session(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5048 local function i(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5049 local o=o:get_child("session",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5050 if o and not o:get_child("optional")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5051 local function o(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5052 e:debug("Establishing Session...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5053 e:send_iq(a.iq({type="set"}):tag("session",{xmlns=t}),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5054 function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5055 if t.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5056 e:event("session-success");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5057 elseif t.attr.type=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5058 local t,o,a=t:get_error();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5059 e:event("session-failure",{error=o,text=a,type=t});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5060 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5061 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5062 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5063 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5064 e:hook("bind-success",o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5065 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5066 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5067 e:hook("stream-features",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5068 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5069 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5070 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5071 package.preload['verse.plugins.legacy']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5072 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5073 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5074 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5075 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5076 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5077 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5078 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5079 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5080 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5081 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5082 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5083 local i=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5084 local s=require"util.uuid".generate;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5085 local a="jabber:iq:auth";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5086 function i.plugins.legacy(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5087 local function n(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5088 local o=t:get_child("query",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5089 if t.attr.type~="result"or not o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5090 local t,a,o=t:get_error();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5091 e:debug("warn","%s %s: %s",t,a,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5092 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5093 local t={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5094 username=e.username;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5095 password=e.password;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5096 resource=e.resource or s();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5097 digest=false,sequence=false,token=false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5098 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5099 local a=i.iq({to=e.host,type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5100 :tag("query",{xmlns=a});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5101 if#o>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5102 for o in o:childtags()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5103 local o=o.name;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5104 local i=t[o];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5105 if i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5106 a:tag(o):text(t[o]):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5107 elseif i==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5108 local t="feature-not-implemented";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5109 e:event("authentication-failure",{condition=t});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5110 return false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5111 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5112 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5113 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5114 for t,e in pairs(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5115 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5116 a:tag(t):text(e):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5117 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5118 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5119 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5120 e:send_iq(a,function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5121 if a.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5122 e.resource=t.resource;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5123 e.jid=t.username.."@"..e.host.."/"..t.resource;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5124 e:event("authentication-success");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5125 e:event("bind-success",e.jid);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5126 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5127 local a,t,a=a:get_error();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5128 e:event("authentication-failure",{condition=t});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5129 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5130 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5131 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5132 local function a(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5133 if not t.version then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5134 e:send_iq(i.iq({type="get"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5135 :tag("query",{xmlns="jabber:iq:auth"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5136 :tag("username"):text(e.username),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5137 n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5138 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5139 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5140 e:hook("opened",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5141 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5142 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5143 package.preload['verse.plugins.compression']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5144 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5145 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5146 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5147 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5148 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5149 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5150 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5151 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5152 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5153 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5154 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5155 local t=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5156 local i=require"zlib";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5157 local e="http://jabber.org/features/compress"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5158 local a="http://jabber.org/protocol/compress"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5159 local e="http://etherx.jabber.org/streams";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5160 local o=9;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5161 local function s(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5162 local i,o=pcall(i.deflate,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5163 if i==false then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5164 local t=t.stanza("failure",{xmlns=a}):tag("setup-failed");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5165 e:send(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5166 e:error("Failed to create zlib.deflate filter: %s",tostring(o));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5167 return
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5168 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5169 return o
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5170 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5171 local function n(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5172 local i,e=pcall(i.inflate);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5173 if i==false then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5174 local t=t.stanza("failure",{xmlns=a}):tag("setup-failed");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5175 o:send(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5176 o:error("Failed to create zlib.inflate filter: %s",tostring(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5177 return
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5178 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5179 return e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5180 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5181 local function i(e,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5182 function e:send(i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5183 local i,o,n=pcall(o,tostring(i),'sync');
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5184 if i==false then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5185 e:close({
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5186 condition="undefined-condition";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5187 text=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5188 extra=t.stanza("failure",{xmlns=a}):tag("processing-failed");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5189 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5190 e:warn("Compressed send failed: %s",tostring(o));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5191 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5192 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5193 e.conn:write(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5194 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5195 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5196 local function h(e,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5197 local n=e.data
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5198 e.data=function(i,s)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5199 e:debug("Decompressing data...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5200 local s,o,h=pcall(o,s);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5201 if s==false then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5202 e:close({
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5203 condition="undefined-condition";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5204 text=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5205 extra=t.stanza("failure",{xmlns=a}):tag("processing-failed");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5206 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5207 stream:warn("%s",tostring(o));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5208 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5209 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5210 return n(i,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5211 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5212 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5213 function t.plugins.compression(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5214 local function d(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5215 if not e.compressed then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5216 local o=o:child_with_name("compression");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5217 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5218 for o in o:children()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5219 local o=o[1]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5220 if o=="zlib"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5221 e:send(t.stanza("compress",{xmlns=a}):tag("method"):text("zlib"))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5222 e:debug("Enabled compression using zlib.")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5223 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5224 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5225 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5226 session:debug("Remote server supports no compression algorithm we support.")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5227 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5228 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5229 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5230 local function r(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5231 if o.name=="compressed"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5232 e:debug("Activating compression...")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5233 local a=s(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5234 if not a then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5235 local t=n(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5236 if not t then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5237 i(e,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5238 h(e,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5239 e.compressed=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5240 e:reopen();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5241 elseif o.name=="failure"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5242 e:warn("Failed to establish compression");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5243 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5244 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5245 e:hook("stream-features",d,250);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5246 e:hook("stream/"..a,r);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5247 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5248 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5249 package.preload['verse.plugins.smacks']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5250 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5251 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5252 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5253 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5254 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5255 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5256 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5257 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5258 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5259 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5260 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5261 local n=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5262 local h=require"socket".gettime;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5263 local s="urn:xmpp:sm:3";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5264 function n.plugins.smacks(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5265 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5266 local a=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5267 local r=h();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5268 local o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5269 local i=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5270 local function l(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5271 if t.attr.xmlns=="jabber:client"or not t.attr.xmlns then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5272 i=i+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5273 e:debug("Increasing handled stanzas to %d for %s",i,t:top_tag());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5274 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5275 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5276 local function u(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5277 if a.name and not a.attr.xmlns then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5278 t[#t+1]=tostring(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5279 r=h();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5280 if not o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5281 o=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5282 e:debug("Waiting to send ack request...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5283 n.add_task(1,function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5284 if#t==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5285 o=false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5286 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5287 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5288 local a=h()-r;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5289 if a<1 and#t<10 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5290 return 1-a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5291 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5292 e:debug("Time up, sending <r>...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5293 o=false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5294 e:send(n.stanza("r",{xmlns=s}));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5295 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5296 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5297 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5298 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5299 local function h()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5300 e:debug("smacks: connection lost");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5301 e.stream_management_supported=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5302 if e.resumption_token then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5303 e:debug("smacks: have resumption token, reconnecting in 1s...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5304 e.authenticated=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5305 n.add_task(1,function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5306 e:connect(e.connect_host or e.host,e.connect_port or 5222);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5307 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5308 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5309 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5310 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5311 local function d()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5312 e.resumption_token=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5313 e:unhook("disconnected",h);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5314 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5315 local function r(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5316 if o.name=="r"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5317 e:debug("Ack requested... acking %d handled stanzas",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5318 e:send(n.stanza("a",{xmlns=s,h=tostring(i)}));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5319 elseif o.name=="a"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5320 local o=tonumber(o.attr.h);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5321 if o>a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5322 local i=#t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5323 for a=a+1,o do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5324 table.remove(t,1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5325 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5326 e:debug("Received ack: New ack: "..o.." Last ack: "..a.." Unacked stanzas now: "..#t.." (was "..i..")");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5327 a=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5328 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5329 e:warn("Received bad ack for "..o.." when last ack was "..a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5330 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5331 elseif o.name=="enabled"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5332 if o.attr.id then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5333 e.resumption_token=o.attr.id;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5334 e:hook("closed",d,100);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5335 e:hook("disconnected",h,100);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5336 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5337 elseif o.name=="resumed"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5338 local o=tonumber(o.attr.h);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5339 if o>a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5340 local i=#t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5341 for a=a+1,o do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5342 table.remove(t,1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5343 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5344 e:debug("Received ack: New ack: "..o.." Last ack: "..a.." Unacked stanzas now: "..#t.." (was "..i..")");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5345 a=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5346 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5347 for a=1,#t do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5348 e:send(t[a]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5349 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5350 t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5351 e:debug("Resumed successfully");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5352 e:event("resumed");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5353 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5354 e:warn("Don't know how to handle "..s.."/"..o.name);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5355 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5356 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5357 local function t()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5358 if not e.smacks then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5359 e:debug("smacks: sending enable");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5360 e:send(n.stanza("enable",{xmlns=s,resume="true"}));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5361 e.smacks=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5362 e:hook("stanza",l);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5363 e:hook("outgoing",u);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5364 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5365 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5366 local function o(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5367 if a:get_child("sm",s)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5368 e.stream_management_supported=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5369 if e.smacks and e.bound then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5370 e:debug("Resuming stream with %d handled stanzas",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5371 e:send(n.stanza("resume",{xmlns=s,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5372 h=i,previd=e.resumption_token}));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5373 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5374 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5375 e:hook("bind-success",t,1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5376 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5377 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5378 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5379 e:hook("stream-features",o,250);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5380 e:hook("stream/"..s,r);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5381 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5382 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5383 package.preload['verse.plugins.keepalive']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5384 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5385 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5386 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5387 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5388 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5389 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5390 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5391 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5392 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5393 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5394 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5395 local t=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5396 function t.plugins.keepalive(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5397 e.keepalive_timeout=e.keepalive_timeout or 300;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5398 t.add_task(e.keepalive_timeout,function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5399 e.conn:write(" ");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5400 return e.keepalive_timeout;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5401 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5402 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5403 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5404 package.preload['verse.plugins.disco']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5405 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5406 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5407 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5408 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5409 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5410 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5411 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5412 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5413 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5414 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5415 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5416 local t=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5417 local e=require("mime").b64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5418 local e=require("util.hashes").sha1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5419 local s=require"util.caps".calculate_hash;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5420 local n="http://jabber.org/protocol/caps";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5421 local e="http://jabber.org/protocol/disco";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5422 local i=e.."#info";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5423 local o=e.."#items";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5424 function t.plugins.disco(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5425 e:add_plugin("presence");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5426 local a={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5427 __index=function(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5428 local t={identities={},features={}};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5429 if e=="identities"or e=="features"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5430 return a[false][e]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5431 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5432 a[e]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5433 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5434 end,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5435 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5436 local h={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5437 __index=function(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5438 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5439 t[a]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5440 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5441 end,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5442 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5443 e.disco={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5444 cache={},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5445 info=setmetatable({
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5446 [false]={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5447 identities={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5448 {category='client',type='pc',name='Verse'},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5449 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5450 features={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5451 [n]=true,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5452 [i]=true,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5453 [o]=true,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5454 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5455 },
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5456 },a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5457 items=setmetatable({[false]={}},h);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5458 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5459 e.caps={}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5460 e.caps.node='http://code.matthewwild.co.uk/verse/'
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5461 local function h(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5462 local o=e.disco.info[a or false];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5463 if a and a==e.caps.node.."#"..e.caps.hash then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5464 o=e.disco.info[false];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5465 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5466 local n,o=o.identities,o.features
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5467 local e=t.stanza("query",{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5468 xmlns=i,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5469 node=a,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5470 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5471 for a,t in pairs(n)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5472 e:tag('identity',t):up()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5473 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5474 for t in pairs(o)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5475 e:tag('feature',{var=t}):up()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5476 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5477 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5478 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5479 setmetatable(e.caps,{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5480 __call=function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5481 local a=s(h())
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5482 e.caps.hash=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5483 return t.stanza('c',{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5484 xmlns=n,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5485 hash='sha-1',
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5486 node=e.caps.node,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5487 ver=a
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5488 })
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5489 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5490 })
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5491 function e:set_identity(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5492 self.disco.info[t or false].identities={a};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5493 e:resend_presence();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5494 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5495 function e:add_identity(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5496 local t=self.disco.info[t or false].identities;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5497 t[#t+1]=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5498 e:resend_presence();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5499 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5500 function e:add_disco_feature(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5501 local t=t.var or t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5502 self.disco.info[a or false].features[t]=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5503 e:resend_presence();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5504 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5505 function e:remove_disco_feature(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5506 local t=t.var or t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5507 self.disco.info[a or false].features[t]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5508 e:resend_presence();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5509 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5510 function e:add_disco_item(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5511 local e=self.disco.items[e or false];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5512 e[#e+1]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5513 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5514 function e:remove_disco_item(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5515 local e=self.disco.items[e or false];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5516 for t=#e,1,-1 do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5517 if e[t]==a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5518 table.remove(e,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5519 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5520 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5521 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5522 function e:jid_has_identity(e,a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5523 local o=self.disco.cache[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5524 if not o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5525 return nil,"no-cache";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5526 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5527 local e=self.disco.cache[e].identities;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5528 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5529 return e[a.."/"..t]or false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5530 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5531 for e in pairs(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5532 if e:match("^(.*)/")==a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5533 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5534 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5535 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5536 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5537 function e:jid_supports(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5538 local e=self.disco.cache[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5539 if not e or not e.features then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5540 return nil,"no-cache";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5541 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5542 return e.features[t]or false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5543 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5544 function e:get_local_services(o,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5545 local e=self.disco.cache[self.host];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5546 if not(e)or not(e.items)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5547 return nil,"no-cache";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5548 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5549 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5550 for i,e in ipairs(e.items)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5551 if self:jid_has_identity(e.jid,o,a)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5552 table.insert(t,e.jid);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5553 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5554 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5555 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5556 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5557 function e:disco_local_services(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5558 self:disco_items(self.host,nil,function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5559 if not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5560 return a({});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5561 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5562 local e=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5563 local function o()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5564 e=e-1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5565 if e==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5566 return a(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5567 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5568 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5569 for a,t in ipairs(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5570 if t.jid then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5571 e=e+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5572 self:disco_info(t.jid,nil,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5573 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5574 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5575 if e==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5576 return a(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5577 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5578 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5579 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5580 function e:disco_info(e,a,s)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5581 local t=t.iq({to=e,type="get"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5582 :tag("query",{xmlns=i,node=a});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5583 self:send_iq(t,function(n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5584 if n.attr.type=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5585 return s(nil,n:get_error());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5586 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5587 local o,t={},{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5588 for e in n:get_child("query",i):childtags()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5589 if e.name=="identity"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5590 o[e.attr.category.."/"..e.attr.type]=e.attr.name or true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5591 elseif e.name=="feature"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5592 t[e.attr.var]=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5593 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5594 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5595 if not self.disco.cache[e]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5596 self.disco.cache[e]={nodes={}};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5597 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5598 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5599 if not self.disco.cache[e].nodes[a]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5600 self.disco.cache[e].nodes[a]={nodes={}};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5601 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5602 self.disco.cache[e].nodes[a].identities=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5603 self.disco.cache[e].nodes[a].features=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5604 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5605 self.disco.cache[e].identities=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5606 self.disco.cache[e].features=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5607 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5608 return s(self.disco.cache[e]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5609 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5610 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5611 function e:disco_items(a,i,n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5612 local t=t.iq({to=a,type="get"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5613 :tag("query",{xmlns=o,node=i});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5614 self:send_iq(t,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5615 if e.attr.type=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5616 return n(nil,e:get_error());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5617 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5618 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5619 for e in e:get_child("query",o):childtags()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5620 if e.name=="item"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5621 table.insert(t,{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5622 name=e.attr.name;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5623 jid=e.attr.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5624 node=e.attr.node;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5625 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5626 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5627 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5628 if not self.disco.cache[a]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5629 self.disco.cache[a]={nodes={}};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5630 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5631 if i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5632 if not self.disco.cache[a].nodes[i]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5633 self.disco.cache[a].nodes[i]={nodes={}};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5634 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5635 self.disco.cache[a].nodes[i].items=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5636 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5637 self.disco.cache[a].items=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5638 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5639 return n(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5640 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5641 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5642 e:hook("iq/"..i,function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5643 local o=a.tags[1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5644 if a.attr.type=='get'and o.name=="query"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5645 local o=h(o.attr.node);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5646 local t=t.reply(a):add_child(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5647 e:send(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5648 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5649 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5650 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5651 e:hook("iq/"..o,function(i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5652 local a=i.tags[1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5653 if i.attr.type=='get'and a.name=="query"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5654 local n=e.disco.items[a.attr.node or false];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5655 local t=t.reply(i):tag('query',{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5656 xmlns=o,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5657 node=a.attr.node
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5658 })
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5659 for a=1,#n do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5660 t:tag('item',n[a]):up()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5661 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5662 e:send(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5663 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5664 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5665 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5666 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5667 e:hook("ready",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5668 if t then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5669 t=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5670 local function o(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5671 local a=e.disco.cache[t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5672 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5673 for a in pairs(a.identities)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5674 local o,a=a:match("^(.*)/(.*)$");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5675 print(t,o,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5676 e:event("disco/service-discovered/"..o,{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5677 type=a,jid=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5678 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5679 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5680 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5681 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5682 e:disco_info(e.host,nil,function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5683 o(e.host);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5684 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5685 e:disco_local_services(function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5686 for a,t in ipairs(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5687 o(t.jid);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5688 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5689 e:event("ready");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5690 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5691 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5692 end,50);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5693 e:hook("presence-out",function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5694 t:remove_children("c",n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5695 t:reset():add_child(e:caps()):reset();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5696 end,10);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5697 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5698 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5699 package.preload['verse.plugins.version']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5700 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5701 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5702 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5703 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5704 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5705 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5706 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5707 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5708 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5709 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5710 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5711 local o=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5712 local a="jabber:iq:version";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5713 local function i(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5714 t.name=e.name;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5715 t.version=e.version;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5716 t.platform=e.platform;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5717 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5718 function o.plugins.version(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5719 e.version={set=i};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5720 e:hook("iq/"..a,function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5721 if t.attr.type~="get"then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5722 local t=o.reply(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5723 :tag("query",{xmlns=a});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5724 if e.version.name then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5725 t:tag("name"):text(tostring(e.version.name)):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5726 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5727 if e.version.version then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5728 t:tag("version"):text(tostring(e.version.version)):up()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5729 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5730 if e.version.platform then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5731 t:tag("os"):text(e.version.platform);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5732 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5733 e:send(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5734 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5735 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5736 function e:query_version(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5737 t=t or function(e)return self:event("version/response",e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5738 self:send_iq(o.iq({type="get",to=e})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5739 :tag("query",{xmlns=a}),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5740 function(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5741 if o.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5742 local e=o:get_child("query",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5743 local o=e and e:get_child_text("name");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5744 local a=e and e:get_child_text("version");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5745 local e=e and e:get_child_text("os");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5746 t({
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5747 name=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5748 version=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5749 platform=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5750 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5751 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5752 local o,a,e=o:get_error();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5753 t({
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5754 error=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5755 condition=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5756 text=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5757 type=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5758 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5759 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5760 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5761 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5762 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5763 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5764 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5765 package.preload['verse.plugins.ping']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5766 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5767 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5768 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5769 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5770 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5771 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5772 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5773 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5774 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5775 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5776 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5777 local a=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5778 local n=require"socket".gettime;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5779 local i="urn:xmpp:ping";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5780 function a.plugins.ping(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5781 function e:ping(t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5782 local s=n();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5783 e:send_iq(a.iq{to=t,type="get"}:tag("ping",{xmlns=i}),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5784 function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5785 if e.attr.type=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5786 local a,e,i=e:get_error();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5787 if e~="service-unavailable"and e~="feature-not-implemented"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5788 o(nil,t,{type=a,condition=e,text=i});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5789 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5790 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5791 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5792 o(n()-s,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5793 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5794 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5795 e:hook("iq/"..i,function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5796 return e:send(a.reply(t));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5797 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5798 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5799 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5800 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5801 package.preload['verse.plugins.uptime']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5802 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5803 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5804 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5805 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5806 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5807 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5808 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5809 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5810 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5811 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5812 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5813 local o=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5814 local t="jabber:iq:last";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5815 local function a(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5816 e.starttime=t.starttime;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5817 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5818 function o.plugins.uptime(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5819 e.uptime={set=a};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5820 e:hook("iq/"..t,function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5821 if a.attr.type~="get"then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5822 local t=o.reply(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5823 :tag("query",{seconds=tostring(os.difftime(os.time(),e.uptime.starttime)),xmlns=t});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5824 e:send(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5825 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5826 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5827 function e:query_uptime(i,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5828 a=a or function(t)return e:event("uptime/response",t);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5829 e:send_iq(o.iq({type="get",to=i})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5830 :tag("query",{xmlns=t}),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5831 function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5832 local t=e:get_child("query",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5833 if e.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5834 local e=tonumber(t.attr.seconds);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5835 a({
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5836 seconds=e or nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5837 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5838 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5839 local t,e,o=e:get_error();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5840 a({
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5841 error=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5842 condition=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5843 text=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5844 type=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5845 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5846 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5847 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5848 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5849 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5850 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5851 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5852 package.preload['verse.plugins.blocking']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5853 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5854 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5855 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5856 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5857 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5858 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5859 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5860 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5861 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5862 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5863 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5864 local a=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5865 local o="urn:xmpp:blocking";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5866 function a.plugins.blocking(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5867 e.blocking={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5868 function e.blocking:block_jid(i,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5869 e:send_iq(a.iq{type="set"}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5870 :tag("block",{xmlns=o})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5871 :tag("item",{jid=i})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5872 ,function()return t and t(true);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5873 ,function()return t and t(false);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5874 );
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5875 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5876 function e.blocking:unblock_jid(i,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5877 e:send_iq(a.iq{type="set"}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5878 :tag("unblock",{xmlns=o})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5879 :tag("item",{jid=i})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5880 ,function()return t and t(true);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5881 ,function()return t and t(false);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5882 );
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5883 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5884 function e.blocking:unblock_all_jids(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5885 e:send_iq(a.iq{type="set"}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5886 :tag("unblock",{xmlns=o})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5887 ,function()return t and t(true);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5888 ,function()return t and t(false);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5889 );
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5890 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5891 function e.blocking:get_blocked_jids(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5892 e:send_iq(a.iq{type="get"}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5893 :tag("blocklist",{xmlns=o})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5894 ,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5895 local a=e:get_child("blocklist",o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5896 if not a then return t and t(false);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5897 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5898 for t in a:childtags()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5899 e[#e+1]=t.attr.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5900 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5901 return t and t(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5902 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5903 ,function(e)return t and t(false);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5904 );
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5905 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5906 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5907 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5908 package.preload['verse.plugins.jingle']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5909 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5910 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5911 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5912 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5913 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5914 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5915 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5916 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5917 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5918 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5919 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5920 local o=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5921 local e=require"util.timer";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5922 local n=require"util.uuid".generate;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5923 local i="urn:xmpp:jingle:1";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5924 local h="urn:xmpp:jingle:errors:1";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5925 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5926 t.__index=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5927 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5928 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5929 function o.plugins.jingle(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5930 e:hook("ready",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5931 e:add_disco_feature(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5932 end,10);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5933 function e:jingle(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5934 return o.eventable(setmetatable(base or{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5935 role="initiator";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5936 peer=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5937 sid=n();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5938 stream=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5939 },t));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5940 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5941 function e:register_jingle_transport(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5942 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5943 function e:register_jingle_content_type(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5944 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5945 local function u(n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5946 local d=n:get_child("jingle",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5947 local a=d.attr.sid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5948 local s=d.attr.action;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5949 local a=e:event("jingle/"..a,n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5950 if a==true then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5951 e:send(o.reply(n));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5952 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5953 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5954 if s~="session-initiate"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5955 local t=o.error_reply(n,"cancel","item-not-found")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5956 :tag("unknown-session",{xmlns=h}):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5957 e:send(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5958 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5959 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5960 local l=d.attr.sid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5961 local a=o.eventable{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5962 role="receiver";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5963 peer=n.attr.from;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5964 sid=l;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5965 stream=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5966 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5967 setmetatable(a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5968 local r;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5969 local h,s;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5970 for t in d:childtags()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5971 if t.name=="content"and t.attr.xmlns==i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5972 local o=t:child_with_name("description");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5973 local i=o.attr.xmlns;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5974 if i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5975 local e=e:event("jingle/content/"..i,a,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5976 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5977 h=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5978 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5979 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5980 local o=t:child_with_name("transport");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5981 local i=o.attr.xmlns;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5982 s=e:event("jingle/transport/"..i,a,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5983 if h and s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5984 r=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5985 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5986 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5987 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5988 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5989 if not h then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5990 e:send(o.error_reply(n,"cancel","feature-not-implemented","The specified content is not supported"));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5991 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5992 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5993 if not s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5994 e:send(o.error_reply(n,"cancel","feature-not-implemented","The specified transport is not supported"));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5995 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5996 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5997 e:send(o.reply(n));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5998 a.content_tag=r;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5999 a.creator,a.name=r.attr.creator,r.attr.name;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6000 a.content,a.transport=h,s;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6001 function a:decline()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6002 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6003 e:hook("jingle/"..l,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6004 if e.attr.from~=a.peer then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6005 return false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6006 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6007 local e=e:get_child("jingle",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6008 return a:handle_command(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6009 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6010 e:event("jingle",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6011 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6012 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6013 function t:handle_command(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6014 local t=a.attr.action;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6015 e:debug("Handling Jingle command: %s",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6016 if t=="session-terminate"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6017 self:destroy();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6018 elseif t=="session-accept"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6019 self:handle_accepted(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6020 elseif t=="transport-info"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6021 e:debug("Handling transport-info");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6022 self.transport:info_received(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6023 elseif t=="transport-replace"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6024 e:error("Peer wanted to swap transport, not implemented");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6025 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6026 e:warn("Unhandled Jingle command: %s",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6027 return nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6028 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6029 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6030 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6031 function t:send_command(e,a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6032 local e=o.iq({to=self.peer,type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6033 :tag("jingle",{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6034 xmlns=i,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6035 sid=self.sid,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6036 action=e,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6037 initiator=self.role=="initiator"and self.stream.jid or nil,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6038 responder=self.role=="responder"and self.jid or nil,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6039 }):add_child(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6040 if not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6041 self.stream:send(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6042 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6043 self.stream:send_iq(e,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6044 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6045 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6046 function t:accept(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6047 local t=o.iq({to=self.peer,type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6048 :tag("jingle",{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6049 xmlns=i,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6050 sid=self.sid,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6051 action="session-accept",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6052 responder=e.jid,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6053 })
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6054 :tag("content",{creator=self.creator,name=self.name});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6055 local o=self.content:generate_accept(self.content_tag:child_with_name("description"),a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6056 t:add_child(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6057 local a=self.transport:generate_accept(self.content_tag:child_with_name("transport"),a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6058 t:add_child(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6059 local a=self;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6060 e:send_iq(t,function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6061 if t.attr.type=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6062 local a,t,a=t:get_error();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6063 e:error("session-accept rejected: %s",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6064 return false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6065 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6066 a.transport:connect(function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6067 e:warn("CONNECTED (receiver)!!!");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6068 a.state="active";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6069 a:event("connected",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6070 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6071 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6072 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6073 e:hook("iq/"..i,u);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6074 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6075 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6076 function t:offer(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6077 local e=o.iq({to=self.peer,type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6078 :tag("jingle",{xmlns=i,action="session-initiate",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6079 initiator=self.stream.jid,sid=self.sid});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6080 e:tag("content",{creator=self.role,name=t});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6081 local t=self.stream:event("jingle/describe/"..t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6082 if not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6083 return false,"Unknown content type";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6084 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6085 e:add_child(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6086 local t=self.stream:event("jingle/transport/".."urn:xmpp:jingle:transports:s5b:1",self);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6087 self.transport=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6088 e:add_child(t:generate_initiate());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6089 self.stream:debug("Hooking %s","jingle/"..self.sid);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6090 self.stream:hook("jingle/"..self.sid,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6091 if e.attr.from~=self.peer then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6092 return false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6093 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6094 local e=e:get_child("jingle",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6095 return self:handle_command(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6096 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6097 self.stream:send_iq(e,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6098 if e.attr.type=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6099 self.state="terminated";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6100 local t,a,e=e:get_error();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6101 return self:event("error",{type=t,condition=a,text=e});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6102 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6103 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6104 self.state="pending";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6105 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6106 function t:terminate(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6107 local e=o.stanza("reason"):tag(e or"success");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6108 self:send_command("session-terminate",e,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6109 self.state="terminated";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6110 self.transport:disconnect();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6111 self:destroy();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6112 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6113 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6114 function t:destroy()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6115 self:event("terminated");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6116 self.stream:unhook("jingle/"..self.sid,self.handle_command);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6117 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6118 function t:handle_accepted(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6119 local e=e:child_with_name("transport");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6120 self.transport:handle_accepted(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6121 self.transport:connect(function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6122 self.stream:debug("CONNECTED (initiator)!")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6123 self.state="active";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6124 self:event("connected",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6125 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6126 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6127 function t:set_source(a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6128 local function t()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6129 local e,i=a();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6130 if e and e~=""then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6131 self.transport.conn:send(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6132 elseif e==""then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6133 return t();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6134 elseif e==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6135 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6136 self:terminate();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6137 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6138 self.transport.conn:unhook("drained",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6139 a=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6140 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6141 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6142 self.transport.conn:hook("drained",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6143 t();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6144 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6145 function t:set_sink(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6146 self.transport.conn:hook("incoming-raw",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6147 self.transport.conn:hook("disconnected",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6148 self.stream:debug("Closing sink...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6149 local e=e.reason;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6150 if e=="closed"then e=nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6151 t(nil,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6152 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6153 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6154 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6155 package.preload['verse.plugins.jingle_ft']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6156 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6157 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6158 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6159 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6160 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6161 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6162 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6163 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6164 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6165 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6166 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6167 local i=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6168 local o=require"ltn12";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6169 local s=package.config:sub(1,1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6170 local t="urn:xmpp:jingle:apps:file-transfer:4";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6171 function i.plugins.jingle_ft(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6172 e:hook("ready",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6173 e:add_disco_feature(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6174 end,10);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6175 local a={type="file"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6176 function a:generate_accept(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6177 if e and e.save_file then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6178 self.jingle:hook("connected",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6179 local e=o.sink.file(io.open(e.save_file,"w+"));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6180 self.jingle:set_sink(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6181 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6182 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6183 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6184 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6185 local a={__index=a};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6186 e:hook("jingle/content/"..t,function(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6187 local e=e:get_child("file");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6188 local e={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6189 name=e:get_child_text("name");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6190 size=tonumber(e:get_child_text("size"));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6191 desc=e:get_child_text("desc");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6192 date=e:get_child_text("date");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6193 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6194 return setmetatable({jingle=t,file=e},a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6195 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6196 e:hook("jingle/describe/file",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6197 local a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6198 if e.timestamp then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6199 a=os.date("!%Y-%m-%dT%H:%M:%SZ",e.timestamp);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6200 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6201 return i.stanza("description",{xmlns=t})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6202 :tag("file")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6203 :tag("name"):text(e.filename):up()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6204 :tag("size"):text(tostring(e.size)):up()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6205 :tag("date"):text(a):up()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6206 :tag("desc"):text(e.description):up()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6207 :up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6208 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6209 function e:send_file(n,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6210 local e,a=io.open(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6211 if not e then return e,a;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6212 local i=e:seek("end",0);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6213 e:seek("set",0);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6214 local a=o.source.file(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6215 local e=self:jingle(n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6216 e:offer("file",{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6217 filename=t:match("[^"..s.."]+$");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6218 size=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6219 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6220 e:hook("connected",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6221 e:set_source(a,true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6222 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6223 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6224 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6225 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6226 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6227 package.preload['verse.plugins.jingle_s5b']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6228 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6229 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6230 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6231 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6232 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6233 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6234 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6235 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6236 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6237 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6238 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6239 local a=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6240 local o="urn:xmpp:jingle:transports:s5b:1";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6241 local r="http://jabber.org/protocol/bytestreams";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6242 local h=require"util.hashes".sha1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6243 local s=require"util.uuid".generate;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6244 local function d(e,n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6245 local function i()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6246 e:unhook("connected",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6247 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6248 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6249 local function o(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6250 e:unhook("incoming-raw",o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6251 if t:sub(1,2)~="\005\000"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6252 return e:event("error","connection-failure");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6253 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6254 e:event("connected");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6255 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6256 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6257 local function a(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6258 e:unhook("incoming-raw",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6259 if t~="\005\000"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6260 local a="version-mismatch";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6261 if t:sub(1,1)=="\005"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6262 a="authentication-failure";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6263 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6264 return e:event("error",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6265 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6266 e:send(string.char(5,1,0,3,#n)..n.."\0\0");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6267 e:hook("incoming-raw",o,100);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6268 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6269 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6270 e:hook("connected",i,200);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6271 e:hook("incoming-raw",a,100);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6272 e:send("\005\001\000");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6273 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6274 local function n(o,e,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6275 local e=a.new(nil,{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6276 streamhosts=e,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6277 current_host=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6278 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6279 local function t(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6280 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6281 return o(nil,a.reason);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6282 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6283 if e.current_host<#e.streamhosts then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6284 e.current_host=e.current_host+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6285 e:debug("Attempting to connect to "..e.streamhosts[e.current_host].host..":"..e.streamhosts[e.current_host].port.."...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6286 local a,t=e:connect(
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6287 e.streamhosts[e.current_host].host,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6288 e.streamhosts[e.current_host].port
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6289 );
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6290 if not a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6291 e:debug("Error connecting to proxy (%s:%s): %s",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6292 e.streamhosts[e.current_host].host,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6293 e.streamhosts[e.current_host].port,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6294 t
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6295 );
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6296 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6297 e:debug("Connecting...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6298 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6299 d(e,i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6300 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6301 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6302 e:unhook("disconnected",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6303 return o(nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6304 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6305 e:hook("disconnected",t,100);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6306 e:hook("connected",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6307 e:unhook("disconnected",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6308 o(e.streamhosts[e.current_host],e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6309 end,100);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6310 t();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6311 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6312 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6313 function a.plugins.jingle_s5b(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6314 e:hook("ready",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6315 e:add_disco_feature(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6316 end,10);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6317 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6318 function t:generate_initiate()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6319 self.s5b_sid=s();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6320 local i=a.stanza("transport",{xmlns=o,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6321 mode="tcp",sid=self.s5b_sid});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6322 local t=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6323 for a,o in pairs(e.proxy65.available_streamhosts)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6324 t=t+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6325 i:tag("candidate",{jid=a,host=o.host,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6326 port=o.port,cid=a,priority=t,type="proxy"}):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6327 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6328 e:debug("Have %d proxies",t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6329 return i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6330 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6331 function t:generate_accept(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6332 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6333 self.s5b_peer_candidates=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6334 self.s5b_mode=e.attr.mode or"tcp";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6335 self.s5b_sid=e.attr.sid or self.jingle.sid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6336 for e in e:childtags()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6337 t[e.attr.cid]={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6338 type=e.attr.type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6339 jid=e.attr.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6340 host=e.attr.host;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6341 port=tonumber(e.attr.port)or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6342 priority=tonumber(e.attr.priority)or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6343 cid=e.attr.cid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6344 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6345 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6346 local e=a.stanza("transport",{xmlns=o});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6347 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6348 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6349 function t:connect(i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6350 e:warn("Connecting!");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6351 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6352 for a,e in pairs(self.s5b_peer_candidates or{})do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6353 t[#t+1]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6354 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6355 if#t>0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6356 self.connecting_peer_candidates=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6357 local function s(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6358 self.jingle:send_command("transport-info",a.stanza("content",{creator=self.creator,name=self.name})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6359 :tag("transport",{xmlns=o,sid=self.s5b_sid})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6360 :tag("candidate-used",{cid=e.cid}));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6361 self.onconnect_callback=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6362 self.conn=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6363 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6364 local e=h(self.s5b_sid..self.peer..e.jid,true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6365 n(s,t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6366 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6367 e:warn("Actually, I'm going to wait for my peer to tell me its streamhost...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6368 self.onconnect_callback=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6369 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6370 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6371 function t:info_received(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6372 e:warn("Info received");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6373 local s=t:child_with_name("content");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6374 local i=s:child_with_name("transport");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6375 if i:get_child("candidate-used")and not self.connecting_peer_candidates then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6376 local t=i:child_with_name("candidate-used");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6377 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6378 local function i(i,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6379 if self.jingle.role=="initiator"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6380 self.jingle.stream:send_iq(a.iq({to=i.jid,type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6381 :tag("query",{xmlns=r,sid=self.s5b_sid})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6382 :tag("activate"):text(self.jingle.peer),function(i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6383 if i.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6384 self.jingle:send_command("transport-info",a.stanza("content",s.attr)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6385 :tag("transport",{xmlns=o,sid=self.s5b_sid})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6386 :tag("activated",{cid=t.attr.cid}));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6387 self.conn=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6388 self.onconnect_callback(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6389 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6390 self.jingle.stream:error("Failed to activate bytestream");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6391 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6392 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6393 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6394 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6395 self.jingle.stream:debug("CID: %s",self.jingle.stream.proxy65.available_streamhosts[t.attr.cid]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6396 local t={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6397 self.jingle.stream.proxy65.available_streamhosts[t.attr.cid];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6398 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6399 local e=h(self.s5b_sid..e.jid..self.peer,true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6400 n(i,t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6401 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6402 elseif i:get_child("activated")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6403 self.onconnect_callback(self.conn);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6404 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6405 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6406 function t:disconnect()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6407 if self.conn then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6408 self.conn:close();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6409 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6410 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6411 function t:handle_accepted(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6412 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6413 local t={__index=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6414 e:hook("jingle/transport/"..o,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6415 return setmetatable({
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6416 role=e.role,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6417 peer=e.peer,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6418 stream=e.stream,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6419 jingle=e,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6420 },t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6421 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6422 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6423 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6424 package.preload['verse.plugins.proxy65']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6425 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6426 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6427 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6428 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6429 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6430 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6431 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6432 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6433 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6434 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6435 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6436 local a=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6437 local r=require"util.uuid";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6438 local d=require"util.hashes".sha1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6439 local n={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6440 n.__index=n;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6441 local i="http://jabber.org/protocol/bytestreams";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6442 local s;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6443 function a.plugins.proxy65(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6444 t.proxy65=setmetatable({stream=t},n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6445 t.proxy65.available_streamhosts={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6446 local e=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6447 t:hook("disco/service-discovered/proxy",function(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6448 if o.type=="bytestreams"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6449 e=e+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6450 t:send_iq(a.iq({to=o.jid,type="get"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6451 :tag("query",{xmlns=i}),function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6452 e=e-1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6453 if a.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6454 local e=a:get_child("query",i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6455 :get_child("streamhost").attr;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6456 t.proxy65.available_streamhosts[e.jid]={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6457 jid=e.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6458 host=e.host;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6459 port=tonumber(e.port);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6460 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6461 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6462 if e==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6463 t:event("proxy65/discovered-proxies",t.proxy65.available_streamhosts);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6464 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6465 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6466 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6467 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6468 t:hook("iq/"..i,function(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6469 local e=a.new(nil,{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6470 initiator_jid=o.attr.from,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6471 streamhosts={},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6472 current_host=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6473 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6474 for t in o.tags[1]:childtags()do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6475 if t.name=="streamhost"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6476 table.insert(e.streamhosts,t.attr);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6477 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6478 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6479 local function i()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6480 if e.current_host<#e.streamhosts then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6481 e.current_host=e.current_host+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6482 e:connect(
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6483 e.streamhosts[e.current_host].host,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6484 e.streamhosts[e.current_host].port
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6485 );
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6486 s(t,e,o.tags[1].attr.sid,o.attr.from,t.jid);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6487 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6488 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6489 e:unhook("disconnected",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6490 t:send(a.error_reply(o,"cancel","item-not-found"));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6491 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6492 function e:accept()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6493 e:hook("disconnected",i,100);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6494 e:hook("connected",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6495 e:unhook("disconnected",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6496 local e=a.reply(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6497 :tag("query",o.tags[1].attr)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6498 :tag("streamhost-used",{jid=e.streamhosts[e.current_host].jid});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6499 t:send(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6500 end,100);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6501 i();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6502 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6503 function e:refuse()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6504 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6505 t:event("proxy65/request",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6506 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6507 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6508 function n:new(t,h)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6509 local e=a.new(nil,{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6510 target_jid=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6511 bytestream_sid=r.generate();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6512 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6513 local o=a.iq{type="set",to=t}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6514 :tag("query",{xmlns=i,mode="tcp",sid=e.bytestream_sid});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6515 for t,e in ipairs(h or self.proxies)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6516 o:tag("streamhost",e):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6517 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6518 self.stream:send_iq(o,function(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6519 if o.attr.type=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6520 local t,o,a=o:get_error();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6521 e:event("connection-failed",{conn=e,type=t,condition=o,text=a});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6522 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6523 local o=o.tags[1]:get_child("streamhost-used");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6524 e.streamhost_jid=o.attr.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6525 local o,n;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6526 for a,t in ipairs(h or self.proxies)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6527 if t.jid==e.streamhost_jid then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6528 o,n=t.host,t.port;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6529 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6530 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6531 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6532 e:connect(o,n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6533 local function o()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6534 e:unhook("connected",o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6535 local t=a.iq{to=e.streamhost_jid,type="set"}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6536 :tag("query",{xmlns=i,sid=e.bytestream_sid})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6537 :tag("activate"):text(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6538 self.stream:send_iq(t,function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6539 if t.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6540 e:event("connected",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6541 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6542 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6543 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6544 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6545 e:hook("connected",o,100);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6546 s(self.stream,e,e.bytestream_sid,self.stream.jid,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6547 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6548 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6549 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6550 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6551 function s(i,e,a,t,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6552 local t=d(a..t..o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6553 local function a()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6554 e:unhook("connected",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6555 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6556 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6557 local function o(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6558 e:unhook("incoming-raw",o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6559 if t:sub(1,2)~="\005\000"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6560 return e:event("error","connection-failure");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6561 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6562 e:event("connected");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6563 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6564 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6565 local function i(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6566 e:unhook("incoming-raw",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6567 if a~="\005\000"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6568 local t="version-mismatch";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6569 if a:sub(1,1)=="\005"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6570 t="authentication-failure";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6571 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6572 return e:event("error",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6573 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6574 e:send(string.char(5,1,0,3,#t)..t.."\0\0");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6575 e:hook("incoming-raw",o,100);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6576 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6577 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6578 e:hook("connected",a,200);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6579 e:hook("incoming-raw",i,100);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6580 e:send("\005\001\000");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6581 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6582 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6583 package.preload['verse.plugins.jingle_ibb']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6584 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6585 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6586 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6587 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6588 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6589 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6590 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6591 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6592 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6593 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6594 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6595 local e=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6596 local i=require"util.encodings".base64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6597 local s=require"util.uuid".generate;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6598 local n="urn:xmpp:jingle:transports:ibb:1";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6599 local o="http://jabber.org/protocol/ibb";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6600 assert(i.encode("This is a test.")=="VGhpcyBpcyBhIHRlc3Qu","Base64 encoding failed");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6601 assert(i.decode("VGhpcyBpcyBhIHRlc3Qu")=="This is a test.","Base64 decoding failed");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6602 local t=table.concat
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6603 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6604 local t={__index=a};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6605 local function h(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6606 local t=setmetatable({stream=a},t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6607 t=e.eventable(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6608 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6609 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6610 function a:initiate(t,e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6611 self.block=2048;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6612 self.stanza=a or'iq';
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6613 self.peer=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6614 self.sid=e or tostring(self):match("%x+$");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6615 self.iseq=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6616 self.oseq=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6617 local e=function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6618 return self:feed(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6619 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6620 self.feeder=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6621 print("Hooking incoming IQs");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6622 local t=self.stream;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6623 t:hook("iq/"..o,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6624 if a=="message"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6625 t:hook("message",e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6626 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6627 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6628 function a:open(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6629 self.stream:send_iq(e.iq{to=self.peer,type="set"}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6630 :tag("open",{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6631 xmlns=o,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6632 ["block-size"]=self.block,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6633 sid=self.sid,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6634 stanza=self.stanza
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6635 })
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6636 ,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6637 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6638 if e.attr.type~="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6639 t(true)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6640 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6641 t(false,e:get_error())
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6642 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6643 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6644 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6645 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6646 function a:send(n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6647 local a=self.stanza;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6648 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6649 if a=="iq"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6650 t=e.iq{type="set",to=self.peer}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6651 elseif a=="message"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6652 t=e.message{to=self.peer}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6653 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6654 local e=self.oseq;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6655 self.oseq=e+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6656 t:tag("data",{xmlns=o,sid=self.sid,seq=e})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6657 :text(i.encode(n));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6658 if a=="iq"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6659 self.stream:send_iq(t,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6660 self:event(e.attr.type=="result"and"drained"or"error");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6661 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6662 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6663 stream:send(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6664 self:event("drained");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6665 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6666 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6667 function a:feed(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6668 if t.attr.from~=self.peer then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6669 local a=t[1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6670 if a.attr.sid~=self.sid then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6671 local n;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6672 if a.name=="open"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6673 self:event("connected");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6674 self.stream:send(e.reply(t))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6675 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6676 elseif a.name=="data"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6677 local o=t:get_child_text("data",o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6678 local a=tonumber(a.attr.seq);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6679 local n=self.iseq;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6680 if o and a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6681 if a~=n then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6682 self.stream:send(e.error_reply(t,"cancel","not-acceptable","Wrong sequence. Packet lost?"))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6683 self:close();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6684 self:event("error");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6685 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6686 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6687 self.iseq=a+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6688 local a=i.decode(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6689 if self.stanza=="iq"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6690 self.stream:send(e.reply(t))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6691 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6692 self:event("incoming-raw",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6693 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6694 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6695 elseif a.name=="close"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6696 self.stream:send(e.reply(t))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6697 self:close();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6698 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6699 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6700 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6701 function a:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6702 self.stream:unhook("iq/"..o,self.feeder)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6703 self:event("disconnected");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6704 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6705 function e.plugins.jingle_ibb(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6706 a:hook("ready",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6707 a:add_disco_feature(n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6708 end,10);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6709 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6710 function t:_setup()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6711 local e=h(self.stream);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6712 e.sid=self.sid or e.sid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6713 e.stanza=self.stanza or e.stanza;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6714 e.block=self.block or e.block;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6715 e:initiate(self.peer,self.sid,self.stanza);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6716 self.conn=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6717 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6718 function t:generate_initiate()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6719 print("ibb:generate_initiate() as "..self.role);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6720 local t=s();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6721 self.sid=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6722 self.stanza='iq';
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6723 self.block=2048;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6724 local e=e.stanza("transport",{xmlns=n,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6725 sid=self.sid,stanza=self.stanza,["block-size"]=self.block});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6726 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6727 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6728 function t:generate_accept(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6729 print("ibb:generate_accept() as "..self.role);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6730 local e=t.attr;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6731 self.sid=e.sid or self.sid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6732 self.stanza=e.stanza or self.stanza;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6733 self.block=e["block-size"]or self.block;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6734 self:_setup();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6735 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6736 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6737 function t:connect(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6738 if not self.conn then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6739 self:_setup();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6740 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6741 local e=self.conn;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6742 print("ibb:connect() as "..self.role);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6743 if self.role=="initiator"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6744 e:open(function(a,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6745 assert(a,table.concat({...},", "));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6746 t(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6747 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6748 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6749 t(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6750 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6751 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6752 function t:info_received(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6753 print("ibb:info_received()");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6754 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6755 function t:disconnect()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6756 if self.conn then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6757 self.conn:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6758 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6759 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6760 function t:handle_accepted(e)end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6761 local t={__index=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6762 a:hook("jingle/transport/"..n,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6763 return setmetatable({
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6764 role=e.role,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6765 peer=e.peer,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6766 stream=e.stream,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6767 jingle=e,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6768 },t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6769 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6770 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6771 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6772 package.preload['verse.plugins.pubsub']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6773 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6774 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6775 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6776 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6777 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6778 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6779 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6780 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6781 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6782 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6783 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6784 local o=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6785 local s=table.insert;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6786 local i="http://jabber.org/protocol/pubsub";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6787 local h="http://jabber.org/protocol/pubsub#owner";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6788 local r="http://jabber.org/protocol/pubsub#event";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6789 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6790 local n={__index=e};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6791 function o.plugins.pubsub(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6792 e.pubsub=setmetatable({stream=e},n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6793 e:hook("message",function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6794 local a=t.attr.from;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6795 for t in t:childtags("event",r)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6796 local t=t:get_child("items");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6797 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6798 local o=t.attr.node;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6799 for t in t:childtags("item")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6800 e:event("pubsub/event",{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6801 from=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6802 node=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6803 item=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6804 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6805 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6806 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6807 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6808 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6809 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6810 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6811 function e:create(e,t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6812 return self:service(e):node(t):create(nil,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6813 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6814 function e:subscribe(a,o,t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6815 return self:service(a):node(o):subscribe(t,nil,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6816 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6817 function e:publish(i,o,a,t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6818 return self:service(i):node(o):publish(a,nil,t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6819 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6820 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6821 local t={__index=a};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6822 function e:service(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6823 return setmetatable({stream=self.stream,service=e},t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6824 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6825 local function t(t,n,s,a,r,h,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6826 local t=o.iq{type=t or"get",to=n}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6827 :tag("pubsub",{xmlns=s or i})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6828 if a then t:tag(a,{node=r,jid=h});end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6829 if e then t:tag("item",{id=e~=true and e or nil});end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6830 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6831 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6832 function a:subscriptions(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6833 self.stream:send_iq(t(nil,self.service,nil,"subscriptions")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6834 ,o and function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6835 if a.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6836 local e=a:get_child("pubsub",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6837 local e=e and e:get_child("subscriptions");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6838 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6839 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6840 for t in e:childtags("subscription")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6841 local e=self:node(t.attr.node)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6842 e.subscription=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6843 e.subscribed_jid=t.attr.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6844 s(a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6845 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6846 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6847 o(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6848 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6849 o(false,a:get_error());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6850 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6851 end or nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6852 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6853 function a:affiliations(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6854 self.stream:send_iq(t(nil,self.service,nil,"affiliations")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6855 ,e and function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6856 if t.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6857 local t=t:get_child("pubsub",i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6858 local a=t and t:get_child("affiliations")or{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6859 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6860 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6861 for a in a:childtags("affiliation")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6862 local e=self:node(a.attr.node)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6863 e.affiliation=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6864 s(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6865 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6866 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6867 e(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6868 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6869 e(false,t:get_error());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6870 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6871 end or nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6872 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6873 function a:nodes(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6874 self.stream:disco_items(self.service,nil,function(e,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6875 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6876 for t=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6877 e[t]=self:node(e[t].node);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6878 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6879 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6880 a(e,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6881 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6882 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6883 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6884 local o={__index=e};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6885 function a:node(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6886 return setmetatable({stream=self.stream,service=self.service,node=e},o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6887 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6888 function n:__call(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6889 local t=self:service(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6890 return e and t:node(e)or t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6891 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6892 function e:hook(a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6893 self._hooks=self._hooks or setmetatable({},{__mode='kv'});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6894 local function t(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6895 if(not e.service or e.from==self.service)and e.node==self.node then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6896 return a(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6897 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6898 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6899 self._hooks[a]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6900 self.stream:hook("pubsub/event",t,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6901 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6902 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6903 function e:unhook(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6904 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6905 local e=self._hooks[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6906 self.stream:unhook("pubsub/event",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6907 elseif self._hooks then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6908 for e in pairs(self._hooks)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6909 self.stream:unhook("pubsub/event",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6910 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6911 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6912 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6913 function e:create(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6914 if a~=nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6915 error("Not implemented yet.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6916 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6917 self.stream:send_iq(t("set",self.service,nil,"create",self.node),e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6918 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6919 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6920 function e:configure(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6921 if e~=nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6922 error("Not implemented yet.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6923 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6924 self.stream:send_iq(t("set",self.service,nil,e==nil and"default"or"configure",self.node),a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6925 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6926 function e:publish(i,a,o,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6927 if a~=nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6928 error("Node configuration is not implemented yet.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6929 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6930 self.stream:send_iq(t("set",self.service,nil,"publish",self.node,nil,i or true)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6931 :add_child(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6932 ,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6933 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6934 function e:subscribe(e,a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6935 e=e or self.stream.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6936 if a~=nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6937 error("Subscription configuration is not implemented yet.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6938 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6939 self.stream:send_iq(t("set",self.service,nil,"subscribe",self.node,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6940 ,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6941 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6942 function e:subscription(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6943 error("Not implemented yet.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6944 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6945 function e:affiliation(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6946 error("Not implemented yet.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6947 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6948 function e:unsubscribe(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6949 e=e or self.subscribed_jid or self.stream.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6950 self.stream:send_iq(t("set",self.service,nil,"unsubscribe",self.node,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6951 ,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6952 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6953 function e:configure_subscription(e,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6954 error("Not implemented yet.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6955 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6956 function e:items(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6957 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6958 self.stream:send_iq(t("get",self.service,nil,"items",self.node)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6959 ,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6960 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6961 self.stream:disco_items(self.service,self.node,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6962 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6963 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6964 function e:item(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6965 self.stream:send_iq(t("get",self.service,nil,"items",self.node,nil,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6966 ,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6967 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6968 function e:retract(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6969 self.stream:send_iq(t("set",self.service,nil,"retract",self.node,nil,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6970 ,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6971 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6972 function e:purge(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6973 assert(not a,"Not implemented yet.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6974 self.stream:send_iq(t("set",self.service,h,"purge",self.node)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6975 ,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6976 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6977 function e:delete(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6978 assert(not a,"Not implemented yet.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6979 self.stream:send_iq(t("set",self.service,h,"delete",self.node)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6980 ,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6981 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6982 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6983 package.preload['verse.plugins.pep']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6984 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6985 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6986 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6987 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6988 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6989 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6990 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6991 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6992 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6993 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6994 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6995 local e=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6996 local t="http://jabber.org/protocol/pubsub";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6997 local t=t.."#event";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6998 function e.plugins.pep(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6999 e:add_plugin("disco");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7000 e:add_plugin("pubsub");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7001 e.pep={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7002 e:hook("pubsub/event",function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7003 return e:event("pep/"..t.node,{from=t.from,item=t.item.tags[1]});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7004 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7005 function e:hook_pep(t,o,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7006 local a=e.events._handlers["pep/"..t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7007 if not(a)or#a==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7008 e:add_disco_feature(t.."+notify");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7009 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7010 e:hook("pep/"..t,o,i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7011 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7012 function e:unhook_pep(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7013 e:unhook("pep/"..t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7014 local a=e.events._handlers["pep/"..t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7015 if not(a)or#a==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7016 e:remove_disco_feature(t.."+notify");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7017 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7018 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7019 function e:publish_pep(t,a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7020 return e.pubsub:service(nil):node(a or t.attr.xmlns):publish(o or"current",nil,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7021 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7022 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7023 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7024 package.preload['verse.plugins.adhoc']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7025 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7026 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7027 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7028 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7029 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7030 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7031 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7032 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7033 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7034 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7035 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7036 local o=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7037 local n=require"lib.adhoc";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7038 local t="http://jabber.org/protocol/commands";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7039 local s="jabber:x:data";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7040 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7041 a.__index=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7042 local i={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7043 function o.plugins.adhoc(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7044 e:add_plugin("disco");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7045 e:add_disco_feature(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7046 function e:query_commands(a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7047 e:disco_items(a,t,function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7048 e:debug("adhoc list returned")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7049 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7050 for o,a in ipairs(a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7051 t[a.node]=a.name;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7052 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7053 e:debug("adhoc calling callback")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7054 return o(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7055 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7056 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7057 function e:execute_command(t,o,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7058 local e=setmetatable({
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7059 stream=e,jid=t,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7060 command=o,callback=i
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7061 },a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7062 return e:execute();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7063 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7064 local function r(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7065 if not(e)or e=="user"then return true;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7066 if type(e)=="function"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7067 return e(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7068 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7069 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7070 function e:add_adhoc_command(o,a,s,h)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7071 i[a]=n.new(o,a,s,h);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7072 e:add_disco_item({jid=e.jid,node=a,name=o},t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7073 return i[a];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7074 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7075 local function s(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7076 local t=a.tags[1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7077 local t=t.attr.node;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7078 local t=i[t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7079 if not t then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7080 if not r(a.attr.from,t.permission)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7081 e:send(o.error_reply(a,"auth","forbidden","You don't have permission to execute this command"):up()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7082 :add_child(t:cmdtag("canceled")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7083 :tag("note",{type="error"}):text("You don't have permission to execute this command")));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7084 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7085 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7086 return n.handle_cmd(t,{send=function(t)return e:send(t)end},a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7087 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7088 e:hook("iq/"..t,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7089 local a=e.attr.type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7090 local t=e.tags[1].name;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7091 if a=="set"and t=="command"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7092 return s(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7093 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7094 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7095 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7096 function a:_process_response(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7097 if e.attr.type=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7098 self.status="canceled";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7099 self.callback(self,{});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7100 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7101 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7102 local e=e:get_child("command",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7103 self.status=e.attr.status;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7104 self.sessionid=e.attr.sessionid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7105 self.form=e:get_child("x",s);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7106 self.note=e:get_child("note");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7107 self.callback(self);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7108 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7109 function a:execute()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7110 local e=o.iq({to=self.jid,type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7111 :tag("command",{xmlns=t,node=self.command});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7112 self.stream:send_iq(e,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7113 self:_process_response(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7114 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7115 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7116 function a:next(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7117 local e=o.iq({to=self.jid,type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7118 :tag("command",{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7119 xmlns=t,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7120 node=self.command,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7121 sessionid=self.sessionid
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7122 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7123 if a then e:add_child(a);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7124 self.stream:send_iq(e,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7125 self:_process_response(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7126 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7127 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7128 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7129 package.preload['verse.plugins.presence']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7130 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7131 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7132 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7133 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7134 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7135 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7136 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7137 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7138 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7139 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7140 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7141 local a=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7142 function a.plugins.presence(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7143 t.last_presence=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7144 t:hook("presence-out",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7145 if not e.attr.to then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7146 t.last_presence=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7147 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7148 end,1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7149 function t:resend_presence()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7150 if self.last_presence then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7151 t:send(self.last_presence);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7152 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7153 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7154 function t:set_status(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7155 local a=a.presence();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7156 if type(e)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7157 if e.show then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7158 a:tag("show"):text(e.show):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7159 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7160 if e.priority or e.prio then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7161 a:tag("priority"):text(tostring(e.priority or e.prio)):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7162 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7163 if e.status or e.msg then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7164 a:tag("status"):text(e.status or e.msg):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7165 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7166 elseif type(e)=="string"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7167 a:tag("status"):text(e):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7168 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7169 t:send(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7170 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7171 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7172 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7173 package.preload['verse.plugins.private']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7174 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7175 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7176 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7177 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7178 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7179 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7180 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7181 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7182 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7183 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7184 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7185 local t=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7186 local a="jabber:iq:private";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7187 function t.plugins.private(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7188 function o:private_set(o,i,e,n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7189 local t=t.iq({type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7190 :tag("query",{xmlns=a});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7191 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7192 if e.name==o and e.attr and e.attr.xmlns==i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7193 t:add_child(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7194 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7195 t:tag(o,{xmlns=i})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7196 :add_child(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7197 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7198 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7199 self:send_iq(t,n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7200 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7201 function o:private_get(e,o,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7202 self:send_iq(t.iq({type="get"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7203 :tag("query",{xmlns=a})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7204 :tag(e,{xmlns=o}),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7205 function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7206 if t.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7207 local t=t:get_child("query",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7208 local e=t:get_child(e,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7209 i(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7210 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7211 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7212 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7213 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7214 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7215 package.preload['verse.plugins.roster']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7216 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7217 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7218 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7219 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7220 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7221 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7222 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7223 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7224 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7225 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7226 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7227 local i=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7228 local d=require"util.jid".bare;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7229 local a="jabber:iq:roster";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7230 local o="urn:xmpp:features:rosterver";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7231 local n=table.insert;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7232 function i.plugins.roster(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7233 local s=false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7234 local e={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7235 items={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7236 ver="";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7237 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7238 t.roster=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7239 t:hook("stream-features",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7240 if e:get_child("ver",o)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7241 s=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7242 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7243 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7244 local function h(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7245 local t=i.stanza("item",{xmlns=a});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7246 for a,e in pairs(e)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7247 if a~="groups"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7248 t.attr[a]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7249 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7250 for a=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7251 t:tag("group"):text(e[a]):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7252 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7253 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7254 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7255 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7256 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7257 local function r(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7258 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7259 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7260 e.groups=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7261 for t,a in pairs(o.attr)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7262 if t~="xmlns"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7263 e[t]=a
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7264 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7265 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7266 for e in o:childtags("group")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7267 n(a,e:get_text())
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7268 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7269 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7270 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7271 function e:load(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7272 e.ver,e.items=t.ver,t.items;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7273 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7274 function e:dump()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7275 return{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7276 ver=e.ver,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7277 items=e.items,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7278 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7279 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7280 function e:add_contact(o,n,s,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7281 local o={jid=o,name=n,groups=s};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7282 local a=i.iq({type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7283 :tag("query",{xmlns=a})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7284 :add_child(h(o));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7285 t:send_iq(a,function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7286 if not e then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7287 if t.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7288 e(true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7289 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7290 e(nil,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7291 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7292 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7293 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7294 function e:delete_contact(o,n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7295 o=(type(o)=="table"and o.jid)or o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7296 local s={jid=o,subscription="remove"}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7297 if not e.items[o]then return false,"item-not-found";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7298 t:send_iq(i.iq({type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7299 :tag("query",{xmlns=a})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7300 :add_child(h(s)),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7301 function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7302 if not n then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7303 if e.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7304 n(true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7305 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7306 n(nil,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7307 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7308 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7309 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7310 local function h(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7311 local t=r(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7312 e.items[t.jid]=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7313 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7314 local function r(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7315 local a=e.items[t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7316 e.items[t]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7317 return a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7318 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7319 function e:fetch(n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7320 t:send_iq(i.iq({type="get"}):tag("query",{xmlns=a,ver=s and e.ver or nil}),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7321 function(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7322 if o.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7323 local t=o:get_child("query",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7324 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7325 e.items={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7326 for t in t:childtags("item")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7327 h(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7328 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7329 e.ver=t.attr.ver or"";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7330 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7331 n(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7332 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7333 n(nil,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7334 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7335 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7336 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7337 t:hook("iq/"..a,function(n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7338 local s,o=n.attr.type,n.attr.from;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7339 if s=="set"and(not o or o==d(t.jid))then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7340 local s=n:get_child("query",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7341 local o=s and s:get_child("item");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7342 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7343 local i,a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7344 local n=o.attr.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7345 if o.attr.subscription=="remove"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7346 i="removed"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7347 a=r(n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7348 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7349 i=e.items[n]and"changed"or"added";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7350 h(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7351 a=e.items[n];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7352 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7353 e.ver=s.attr.ver;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7354 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7355 t:event("roster/item-"..i,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7356 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7357 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7358 t:send(i.reply(n))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7359 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7360 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7361 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7362 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7363 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7364 package.preload['verse.plugins.register']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7365 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7366 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7367 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7368 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7369 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7370 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7371 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7372 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7373 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7374 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7375 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7376 local t=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7377 local o="jabber:iq:register";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7378 function t.plugins.register(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7379 local function a(i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7380 if i:get_child("register","http://jabber.org/features/iq-register")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7381 local t=t.iq({to=e.host_,type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7382 :tag("query",{xmlns=o})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7383 :tag("username"):text(e.username):up()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7384 :tag("password"):text(e.password):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7385 if e.register_email then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7386 t:tag("email"):text(e.register_email):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7387 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7388 e:send_iq(t,function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7389 if t.attr.type=="result"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7390 e:event("registration-success");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7391 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7392 local a,t,o=t:get_error();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7393 e:debug("Registration failed: %s",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7394 e:event("registration-failure",{type=a,condition=t,text=o});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7395 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7396 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7397 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7398 e:debug("In-band registration not offered by server");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7399 e:event("registration-failure",{condition="service-unavailable"});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7400 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7401 e:unhook("stream-features",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7402 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7403 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7404 e:hook("stream-features",a,310);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7405 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7406 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7407 package.preload['verse.plugins.groupchat']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7408 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7409 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7410 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7411 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7412 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7413 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7414 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7415 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7416 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7417 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7418 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7419 local i=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7420 local e=require"util.events";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7421 local n=require"util.jid";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7422 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7423 a.__index=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7424 local h="urn:xmpp:delay";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7425 local s="http://jabber.org/protocol/muc";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7426 function i.plugins.groupchat(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7427 o:add_plugin("presence")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7428 o.rooms={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7429 o:hook("stanza",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7430 local a=n.bare(e.attr.from);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7431 if not a then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7432 local t=o.rooms[a]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7433 if not t and e.attr.to and a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7434 t=o.rooms[e.attr.to.." "..a]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7435 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7436 if t and t.opts.source and e.attr.to~=t.opts.source then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7437 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7438 local o=select(3,n.split(e.attr.from));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7439 local n=e:get_child_text("body");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7440 local i=e:get_child("delay",h);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7441 local a={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7442 room_jid=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7443 room=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7444 sender=t.occupants[o];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7445 nick=o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7446 body=n;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7447 stanza=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7448 delay=(i and i.attr.stamp);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7449 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7450 local t=t:event(e.name,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7451 return t or(e.name=="message")or nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7452 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7453 end,500);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7454 function o:join_room(n,h,t,r)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7455 if not h then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7456 return false,"no nickname supplied"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7457 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7458 t=t or{};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7459 local e=setmetatable(i.eventable{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7460 stream=o,jid=n,nick=h,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7461 subject=nil,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7462 occupants={},
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7463 opts=t,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7464 },a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7465 if t.source then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7466 self.rooms[t.source.." "..n]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7467 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7468 self.rooms[n]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7469 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7470 local a=e.occupants;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7471 e:hook("presence",function(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7472 local t=o.nick or h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7473 if not a[t]and o.stanza.attr.type~="unavailable"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7474 a[t]={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7475 nick=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7476 jid=o.stanza.attr.from;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7477 presence=o.stanza;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7478 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7479 local o=o.stanza:get_child("x",s.."#user");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7480 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7481 local e=o:get_child("item");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7482 if e and e.attr then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7483 a[t].real_jid=e.attr.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7484 a[t].affiliation=e.attr.affiliation;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7485 a[t].role=e.attr.role;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7486 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7487 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7488 if t==e.nick then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7489 e.stream:event("groupchat/joined",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7490 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7491 e:event("occupant-joined",a[t]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7492 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7493 elseif a[t]and o.stanza.attr.type=="unavailable"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7494 if t==e.nick then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7495 e.stream:event("groupchat/left",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7496 if e.opts.source then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7497 self.rooms[e.opts.source.." "..n]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7498 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7499 self.rooms[n]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7500 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7501 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7502 a[t].presence=o.stanza;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7503 e:event("occupant-left",a[t]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7504 a[t]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7505 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7506 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7507 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7508 e:hook("message",function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7509 local t=a.stanza:get_child_text("subject");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7510 if not t then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7511 t=#t>0 and t or nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7512 if t~=e.subject then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7513 local o=e.subject;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7514 e.subject=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7515 return e:event("subject-changed",{from=o,to=t,by=a.sender,event=a});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7516 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7517 end,2e3);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7518 local t=i.presence():tag("x",{xmlns=s}):reset();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7519 if r then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7520 t:get_child("x",s):tag("password"):text(r):reset();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7521 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7522 self:event("pre-groupchat/joining",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7523 e:send(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7524 self:event("groupchat/joining",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7525 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7526 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7527 o:hook("presence-out",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7528 if not e.attr.to then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7529 for a,t in pairs(o.rooms)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7530 t:send(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7531 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7532 e.attr.to=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7533 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7534 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7535 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7536 function a:send(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7537 if e.name=="message"and not e.attr.type then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7538 e.attr.type="groupchat";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7539 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7540 if e.name=="presence"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7541 e.attr.to=self.jid.."/"..self.nick;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7542 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7543 if e.attr.type=="groupchat"or not e.attr.to then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7544 e.attr.to=self.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7545 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7546 if self.opts.source then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7547 e.attr.from=self.opts.source
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7548 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7549 self.stream:send(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7550 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7551 function a:send_message(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7552 self:send(i.message():tag("body"):text(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7553 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7554 function a:set_subject(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7555 self:send(i.message():tag("subject"):text(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7556 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7557 function a:leave(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7558 self.stream:event("groupchat/leaving",self);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7559 local t=i.presence({type="unavailable"});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7560 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7561 t:tag("status"):text(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7562 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7563 self:send(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7564 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7565 function a:admin_set(o,a,t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7566 self:send(i.iq({type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7567 :query(s.."#admin")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7568 :tag("item",{nick=o,[a]=t})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7569 :tag("reason"):text(e or""));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7570 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7571 function a:set_role(e,a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7572 self:admin_set(e,"role",a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7573 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7574 function a:set_affiliation(a,t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7575 self:admin_set(a,"affiliation",t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7576 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7577 function a:kick(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7578 self:set_role(e,"none",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7579 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7580 function a:ban(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7581 self:set_affiliation(e,"outcast",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7582 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7583 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7584 package.preload['verse.plugins.vcard']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7585 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7586 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7587 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7588 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7589 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7590 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7591 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7592 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7593 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7594 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7595 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7596 local i=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7597 local o=require"util.vcard";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7598 local e="vcard-temp";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7599 function i.plugins.vcard(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7600 function a:get_vcard(n,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7601 a:send_iq(i.iq({to=n,type="get"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7602 :tag("vCard",{xmlns=e}),t and function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7603 local e=a:get_child("vCard",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7604 if a.attr.type=="result"and e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7605 e=o.from_xep54(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7606 t(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7607 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7608 t(false)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7609 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7610 end or nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7611 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7612 function a:set_vcard(e,n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7613 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7614 if type(e)=="table"and e.name then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7615 t=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7616 elseif type(e)=="string"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7617 t=o.to_xep54(o.from_text(e)[1]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7618 elseif type(e)=="table"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7619 t=o.to_xep54(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7620 error("Converting a table to vCard not implemented")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7621 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7622 if not t then return false end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7623 a:debug("setting vcard to %s",tostring(t));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7624 a:send_iq(i.iq({type="set"})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7625 :add_child(t),n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7626 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7627 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7628 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7629 package.preload['verse.plugins.vcard_update']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7630 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7631 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7632 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7633 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7634 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7635 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7636 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7637 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7638 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7639 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7640 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7641 local n=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7642 local i="vcard-temp:x:update";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7643 local s=require("util.hashes").sha1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7644 local e,t=pcall(function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7645 local e=require("util.encodings").base64.decode;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7646 assert(e("SGVsbG8=")=="Hello")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7647 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7648 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7649 if not e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7650 e,t=pcall(function()return require("mime").unb64;end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7651 if not e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7652 error("Could not find a base64 decoder")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7653 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7654 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7655 local h=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7656 function n.plugins.vcard_update(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7657 e:add_plugin("vcard");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7658 e:add_plugin("presence");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7659 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7660 local function r(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7661 local o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7662 for e=1,#a do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7663 if a[e].name=="PHOTO"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7664 o=a[e][1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7665 break
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7666 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7667 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7668 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7669 local a=s(h(o),true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7670 t=n.stanza("x",{xmlns=i})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7671 :tag("photo"):text(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7672 e:resend_presence()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7673 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7674 t=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7675 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7676 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7677 local a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7678 e:hook("ready",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7679 if a then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7680 a=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7681 e:get_vcard(nil,function(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7682 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7683 r(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7684 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7685 e:event("ready");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7686 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7687 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7688 end,3);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7689 e:hook("presence-out",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7690 if t and not e:get_child("x",i)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7691 e:add_child(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7692 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7693 end,10);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7694 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7695 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7696 package.preload['verse.plugins.carbons']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7697 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7698 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7699 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7700 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7701 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7702 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7703 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7704 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7705 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7706 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7707 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7708 local a=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7709 local o="urn:xmpp:carbons:2";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7710 local s="urn:xmpp:forward:0";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7711 local n=os.time;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7712 local h=require"util.datetime".parse;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7713 local r=require"util.jid".bare;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7714 function a.plugins.carbons(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7715 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7716 t.enabled=false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7717 e.carbons=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7718 function t:enable(i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7719 e:send_iq(a.iq{type="set"}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7720 :tag("enable",{xmlns=o})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7721 ,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7722 local e=e.attr.type=="result";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7723 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7724 t.enabled=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7725 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7726 if i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7727 i(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7728 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7729 end or nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7730 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7731 function t:disable(i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7732 e:send_iq(a.iq{type="set"}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7733 :tag("disable",{xmlns=o})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7734 ,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7735 local e=e.attr.type=="result";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7736 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7737 t.enabled=false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7738 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7739 if i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7740 i(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7741 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7742 end or nil);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7743 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7744 local i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7745 e:hook("bind-success",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7746 i=r(e.jid);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7747 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7748 e:hook("message",function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7749 local t=a:get_child(nil,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7750 if a.attr.from==i and t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7751 local o=t.name;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7752 local t=t:get_child("forwarded",s);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7753 local a=t and t:get_child("message","jabber:client");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7754 local t=t:get_child("delay","urn:xmpp:delay");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7755 local t=t and t.attr.stamp;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7756 t=t and h(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7757 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7758 return e:event("carbon",{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7759 dir=o,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7760 stanza=a,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7761 timestamp=t or n(),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7762 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7763 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7764 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7765 end,1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7766 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7767 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7768 package.preload['verse.plugins.archive']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7769 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7770 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7771 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7772 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7773 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7774 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7775 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7776 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7777 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7778 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7779 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7780 local o=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7781 local t=require"util.stanza";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7782 local a="urn:xmpp:mam:2"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7783 local l="urn:xmpp:forward:0";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7784 local d="urn:xmpp:delay";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7785 local i=require"util.uuid".generate;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7786 local u=require"util.datetime".parse;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7787 local h=require"util.datetime".datetime;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7788 local e=require"util.dataforms".new;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7789 local r=require"util.rsm";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7790 local c={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7791 local m=e{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7792 {name="FORM_TYPE";type="hidden";value=a;};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7793 {name="with";type="jid-single";};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7794 {name="start";type="text-single"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7795 {name="end";type="text-single";};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7796 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7797 function o.plugins.archive(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7798 function o:query_archive(o,e,n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7799 local i=i();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7800 local o=t.iq{type="set",to=o}
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7801 :tag("query",{xmlns=a,queryid=i});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7802 local t,s=tonumber(e["start"]),tonumber(e["end"]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7803 e["start"]=t and h(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7804 e["end"]=s and h(s);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7805 o:add_child(m:form(e,"submit"));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7806 o:add_child(r.generate(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7807 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7808 local function s(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7809 local e=e:get_child("result",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7810 if e and e.attr.queryid==i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7811 local a=e:get_child("forwarded",l);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7812 local o=e.attr.id;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7813 local e=a:get_child("delay",d);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7814 local e=e and u(e.attr.stamp)or nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7815 local a=a:get_child("message","jabber:client")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7816 t[#t+1]={id=o,stamp=e,message=a};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7817 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7818 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7819 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7820 self:hook("message",s,1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7821 self:send_iq(o,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7822 self:unhook("message",s);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7823 if e.attr.type=="error"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7824 self:warn(table.concat({e:get_error()}," "))
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7825 n(false,e:get_error())
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7826 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7827 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7828 local e=e:get_child("fin",a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7829 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7830 local e=r.get(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7831 for a,e in pairs(e or c)do t[a]=e;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7832 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7833 n(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7834 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7835 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7836 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7837 local i={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7838 always=true,[true]="always",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7839 never=false,[false]="never",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7840 roster="roster",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7841 }
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7842 local function s(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7843 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7844 local a=t.attr.default;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7845 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7846 e[false]=i[a];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7847 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7848 local a=t:get_child("always");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7849 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7850 for t in a:childtags("jid")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7851 local t=t:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7852 e[t]=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7853 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7854 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7855 local t=t:get_child("never");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7856 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7857 for t in t:childtags("jid")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7858 local t=t:get_text();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7859 e[t]=false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7860 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7861 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7862 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7863 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7864 local function n(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7865 local e
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7866 e,o[false]=o[false],nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7867 if e~=nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7868 e=i[e];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7869 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7870 local a=t.stanza("prefs",{xmlns=a,default=e})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7871 local e=t.stanza("always");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7872 local t=t.stanza("never");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7873 for o,a in pairs(o)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7874 (a and e or t):tag("jid"):text(o):up();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7875 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7876 return a:add_child(e):add_child(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7877 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7878 function o:archive_prefs_get(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7879 self:send_iq(t.iq{type="get"}:tag("prefs",{xmlns=a}),
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7880 function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7881 if e and e.attr.type=="result"and e.tags[1]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7882 local t=s(e.tags[1]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7883 o(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7884 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7885 o(nil,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7886 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7887 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7888 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7889 function o:archive_prefs_set(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7890 self:send_iq(t.iq{type="set"}:add_child(n(e)),a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7891 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7892 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7893 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7894 package.preload['util.http']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7895 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7896 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7897 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7898 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7899 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7900 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7901 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7902 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7903 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7904 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7905 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7906 local t,s=string.format,string.char;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7907 local d,n,o=pairs,ipairs,tonumber;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7908 local i,h=table.insert,table.concat;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7909 local function r(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7910 return e and(e:gsub("[^a-zA-Z0-9.~_-]",function(e)return t("%%%02x",e:byte());end));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7911 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7912 local function a(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7913 return e and(e:gsub("%%(%x%x)",function(e)return s(o(e,16));end));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7914 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7915 local function e(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7916 return e and(e:gsub("%W",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7917 if e~=" "then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7918 return t("%%%02x",e:byte());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7919 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7920 return"+";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7921 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7922 end));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7923 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7924 local function s(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7925 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7926 if t[1]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7927 for o,t in n(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7928 i(a,e(t.name).."="..e(t.value));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7929 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7930 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7931 for o,t in d(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7932 i(a,e(o).."="..e(t));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7933 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7934 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7935 return h(a,"&");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7936 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7937 local function n(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7938 if not e:match("=")then return a(e);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7939 local o={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7940 for t,e in e:gmatch("([^=&]*)=([^&]*)")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7941 t,e=t:gsub("%+","%%20"),e:gsub("%+","%%20");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7942 t,e=a(t),a(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7943 i(o,{name=t,value=e});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7944 o[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7945 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7946 return o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7947 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7948 local function t(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7949 e=","..e:gsub("[ \t]",""):lower()..",";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7950 return e:find(","..t:lower()..",",1,true)~=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7951 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7952 return{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7953 urlencode=r,urldecode=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7954 formencode=s,formdecode=n;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7955 contains_token=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7956 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7957 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7958 package.preload['net.http.parser']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7959 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7960 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7961 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7962 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7963 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7964 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7965 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7966 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7967 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7968 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7969 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7970 local w=tonumber;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7971 local a=assert;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7972 local b=require"socket.url".parse;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7973 local t=require"util.http".urldecode;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7974 local function g(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7975 e=t((e:gsub("//+","/")));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7976 if e:sub(1,1)~="/"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7977 e="/"..e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7978 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7979 local t=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7980 for e in e:gmatch("([^/]+)/")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7981 if e==".."then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7982 t=t-1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7983 elseif e~="."then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7984 t=t+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7985 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7986 if t<0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7987 return nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7988 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7989 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7990 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7991 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7992 local y={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7993 function y.new(u,h,e,p)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7994 local d=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7995 if not e or e=="server"then d=false;else a(e=="client","Invalid parser type");end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7996 local e="";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7997 local y,a,r;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7998 local s=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7999 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8000 local o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8001 local c;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8002 local n;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8003 return{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8004 feed=function(l,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8005 if n then return nil,"parse has failed";end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8006 if not i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8007 if s and d and not o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8008 t.body=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8009 u(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8010 elseif e~=""then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8011 n=true;return h();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8012 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8013 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8014 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8015 e=e..i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8016 while#e>0 do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8017 if s==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8018 local m=e:find("\r\n\r\n",nil,true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8019 if not m then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8020 local u,r,l,i,v;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8021 local f;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8022 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8023 for t in e:sub(1,m+1):gmatch("([^\r\n]+)\r\n")do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8024 if f then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8025 local e,t=t:match("^([^%s:]+): *(.*)$");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8026 if not e then n=true;return h("invalid-header-line");end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8027 e=e:lower();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8028 a[e]=a[e]and a[e]..","..t or t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8029 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8030 f=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8031 if d then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8032 l,i,v=t:match("^HTTP/(1%.[01]) (%d%d%d) (.*)$");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8033 i=w(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8034 if not i then n=true;return h("invalid-status-line");end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8035 c=not
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8036 ((p and p().method=="HEAD")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8037 or(i==204 or i==304 or i==301)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8038 or(i>=100 and i<200));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8039 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8040 u,r,l=t:match("^(%w+) (%S+) HTTP/(1%.[01])$");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8041 if not u then n=true;return h("invalid-status-line");end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8042 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8043 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8044 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8045 if not f then n=true;return h("invalid-status-line");end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8046 y=c and a["transfer-encoding"]=="chunked";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8047 o=w(a["content-length"]);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8048 if d then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8049 if not c then o=0;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8050 t={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8051 code=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8052 httpversion=l;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8053 headers=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8054 body=c and""or nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8055 responseversion=l;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8056 responseheaders=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8057 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8058 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8059 local e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8060 if r:byte()==47 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8061 local a,t=r:match("([^?]*).?(.*)");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8062 if t==""then t=nil;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8063 e={path=a,query=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8064 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8065 e=b(r);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8066 if not(e and e.path)then n=true;return h("invalid-url");end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8067 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8068 r=g(e.path);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8069 a.host=e.host or a.host;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8070 o=o or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8071 t={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8072 method=u;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8073 url=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8074 path=r;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8075 httpversion=l;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8076 headers=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8077 body=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8078 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8079 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8080 e=e:sub(m+4);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8081 s=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8082 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8083 if s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8084 if d then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8085 if y then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8086 if not e:find("\r\n",nil,true)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8087 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8088 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8089 if not a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8090 a,r=e:match("^(%x+)[^\r\n]*\r\n()");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8091 a=a and w(a,16);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8092 if not a then n=true;return h("invalid-chunk-size");end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8093 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8094 if a==0 and e:find("\r\n\r\n",r-2,true)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8095 s,a=nil,nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8096 e=e:gsub("^.-\r\n\r\n","");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8097 u(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8098 elseif#e-r-2>=a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8099 t.body=t.body..e:sub(r,r+(a-1));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8100 e=e:sub(r+a+2);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8101 a,r=nil,nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8102 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8103 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8104 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8105 elseif o and#e>=o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8106 if t.code==101 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8107 t.body,e=e,"";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8108 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8109 t.body,e=e:sub(1,o),e:sub(o+1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8110 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8111 s=nil;u(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8112 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8113 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8114 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8115 elseif#e>=o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8116 t.body,e=e:sub(1,o),e:sub(o+1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8117 s=nil;u(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8118 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8119 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8120 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8121 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8122 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8123 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8124 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8125 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8126 return y;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8127 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8128 package.preload['net.http']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8129 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8130 local function a(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8131 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8132 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8133 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8134 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8135 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8136 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8137 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8138 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8139 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8140 local y=require"socket"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8141 local q=require"util.encodings".base64.encode;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8142 local r=require"socket.url"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8143 local d=require"net.http.parser".new;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8144 local h=require"util.http";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8145 local x=pcall(require,"ssl");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8146 local j=require"net.server"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8147 local l,o=table.insert,table.concat;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8148 local m=pairs;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8149 local v,c,p,b,s=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8150 tonumber,tostring,xpcall,select,debug.traceback;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8151 local g,k=assert,error
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8152 local u=require"util.logger".init("http");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8153 a"http"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8154 local i={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8155 local n={default_port=80,default_mode="*a"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8156 function n.onconnect(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8157 local e=i[t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8158 local a={e.method or"GET"," ",e.path," HTTP/1.1\r\n"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8159 if e.query then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8160 l(a,4,"?"..e.query);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8161 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8162 t:write(o(a));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8163 local a={[2]=": ",[4]="\r\n"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8164 for e,i in m(e.headers)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8165 a[1],a[3]=e,i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8166 t:write(o(a));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8167 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8168 t:write("\r\n");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8169 if e.body then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8170 t:write(e.body);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8171 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8172 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8173 function n.onincoming(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8174 local e=i[a];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8175 if not e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8176 u("warn","Received response from connection %s with no request attached!",c(a));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8177 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8178 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8179 if t and e.reader then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8180 e:reader(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8181 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8182 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8183 function n.ondisconnect(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8184 local e=i[t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8185 if e and e.conn then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8186 e:reader(nil,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8187 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8188 i[t]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8189 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8190 function n.ondetach(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8191 i[e]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8192 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8193 local function f(e,o,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8194 if not e.parser then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8195 local function a(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8196 if e.callback then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8197 e.callback(t or"connection-closed",0,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8198 e.callback=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8199 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8200 destroy_request(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8201 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8202 if not o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8203 a(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8204 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8205 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8206 local function o(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8207 if e.callback then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8208 e.callback(t.body,t.code,t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8209 e.callback=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8210 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8211 destroy_request(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8212 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8213 local function t()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8214 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8215 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8216 e.parser=d(o,a,"client",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8217 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8218 e.parser:feed(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8219 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8220 local function w(e)u("error","Traceback[http]: %s",s(c(e),2));end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8221 function request(e,t,l)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8222 local e=r.parse(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8223 if not(e and e.host)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8224 l(nil,0,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8225 return nil,"invalid-url";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8226 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8227 if not e.path then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8228 e.path="/";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8229 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8230 local d,o,s;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8231 local r,a=e.host,e.port;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8232 local h=r;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8233 if(a=="80"and e.scheme=="http")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8234 or(a=="443"and e.scheme=="https")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8235 a=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8236 elseif a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8237 h=h..":"..a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8238 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8239 o={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8240 ["Host"]=h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8241 ["User-Agent"]="Prosody XMPP Server";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8242 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8243 if e.userinfo then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8244 o["Authorization"]="Basic "..q(e.userinfo);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8245 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8246 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8247 e.onlystatus=t.onlystatus;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8248 s=t.body;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8249 if s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8250 d="POST";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8251 o["Content-Length"]=c(#s);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8252 o["Content-Type"]="application/x-www-form-urlencoded";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8253 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8254 if t.method then d=t.method;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8255 if t.headers then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8256 for t,e in m(t.headers)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8257 o[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8258 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8259 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8260 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8261 e.method,e.headers,e.body=d,o,s;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8262 local o=e.scheme=="https";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8263 if o and not x then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8264 k("SSL not available, unable to contact https URL");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8265 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8266 local s=a and v(a)or(o and 443 or 80);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8267 local a=y.tcp();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8268 a:settimeout(10);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8269 local d,h=a:connect(r,s);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8270 if not d and h~="timeout"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8271 l(nil,0,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8272 return nil,h;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8273 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8274 local h=false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8275 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8276 h=t and t.sslctx or{mode="client",protocol="sslv23",options={"no_sslv2","no_sslv3"}};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8277 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8278 e.handler,e.conn=g(j.wrapclient(a,r,s,n,"*a",h));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8279 e.write=function(...)return e.handler:write(...);end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8280 e.callback=function(a,t,o,i)u("debug","Calling callback, status %s",t or"---");return b(2,p(function()return l(a,t,o,i)end,w));end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8281 e.reader=f;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8282 e.state="status";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8283 i[e.handler]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8284 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8285 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8286 function destroy_request(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8287 if e.conn then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8288 e.conn=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8289 e.handler:close()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8290 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8291 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8292 local e,a=h.urlencode,h.urldecode;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8293 local t,o=h.formencode,h.formdecode;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8294 _M.urlencode,_M.urldecode=e,a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8295 _M.formencode,_M.formdecode=t,o;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8296 return _M;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8297 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8298 package.preload['util.x509']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8299 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8300 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8301 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8302 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8303 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8304 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8305 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8306 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8307 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8308 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8309 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8310 local i=require"util.encodings".stringprep.nameprep;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8311 local l=require"util.encodings".idna.to_ascii;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8312 local d=require"util.encodings".base64;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8313 local e=require"util.logger".init("x509");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8314 local c=string.format;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8315 local _ENV=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8316 local f="2.5.4.3";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8317 local r="2.5.29.17";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8318 local s="1.3.6.1.5.5.7.8.5";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8319 local h="1.3.6.1.5.5.7.8.7";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8320 local function n(a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8321 local t=l(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8322 if t==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8323 e("info","Host %s failed IDNA ToASCII operation",a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8324 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8325 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8326 t=t:lower()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8327 local i=t:gsub("^[^.]+%.","")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8328 for a=1,#o do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8329 local a=o[a]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8330 if t==a:lower()then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8331 e("debug","Cert dNSName %s matched hostname",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8332 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8333 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8334 if a:match("^%*%.")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8335 local t=a:gsub("^[^.]+%.","")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8336 if i==t:lower()then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8337 e("debug","Cert dNSName %s matched hostname",a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8338 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8339 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8340 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8341 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8342 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8343 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8344 local function m(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8345 local o=i(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8346 for a=1,#t do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8347 local t=t[a]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8348 if t:match("[@/]")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8349 e("debug","Ignoring xmppAddr %s because it's not a bare domain",t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8350 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8351 local a=i(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8352 if a==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8353 e("info","Ignoring xmppAddr %s, failed nameprep!",t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8354 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8355 if o==a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8356 e("debug","Cert xmppAddr %s matched hostname",t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8357 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8358 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8359 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8360 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8361 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8362 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8363 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8364 local function u(t,o,i)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8365 local a=l(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8366 if a==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8367 e("info","Host %s failed IDNA ToASCII operation",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8368 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8369 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8370 if o:match("^_")==nil then o="_"..o end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8371 a=a:lower();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8372 local n=a:gsub("^[^.]+%.","")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8373 for t=1,#i do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8374 local i,t=i[t]:match("^(_[^.]+)%.(.*)");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8375 if o==i then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8376 if a==t:lower()then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8377 e("debug","Cert SRVName %s matched hostname",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8378 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8379 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8380 if t:match("^%*%.")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8381 local a=t:gsub("^[^.]+%.","")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8382 if n==a:lower()then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8383 e("debug","Cert SRVName %s matched hostname",t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8384 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8385 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8386 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8387 if a==t:lower()then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8388 e("debug","Cert SRVName %s matched hostname",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8389 return true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8390 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8391 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8392 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8393 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8394 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8395 local function l(a,i,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8396 if o.setencode then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8397 o:setencode("utf8");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8398 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8399 local t=o:extensions()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8400 if t[r]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8401 local e=t[r];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8402 local t=false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8403 if e[s]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8404 t=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8405 if i=="_xmpp-client"or i=="_xmpp-server"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8406 if m(a,e[s])then return true end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8407 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8408 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8409 if e[h]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8410 t=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8411 if i and u(a,i,e[h])then return true end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8412 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8413 if e["dNSName"]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8414 t=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8415 if n(a,e["dNSName"])then return true end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8416 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8417 if e["uniformResourceIdentifier"]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8418 t=true
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8419 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8420 if t then return false end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8421 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8422 local o=o:subject()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8423 local t=nil
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8424 for a=1,#o do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8425 local a=o[a]
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8426 if a["oid"]==f then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8427 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8428 e("info","Certificate has multiple common names")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8429 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8430 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8431 t=a["value"];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8432 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8433 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8434 if t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8435 return n(a,{t})
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8436 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8437 return false
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8438 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8439 local e="%-%-%-%-%-BEGIN ([A-Z ]+)%-%-%-%-%-\r?\n"..
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8440 "([0-9A-Za-z+/=\r\n]*)\r?\n%-%-%-%-%-END %1%-%-%-%-%-";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8441 local function a(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8442 local t,e=t:match(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8443 if t and e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8444 return d.decode(e),t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8445 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8446 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8447 local o=('.'):rep(64);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8448 local n="-----BEGIN %s-----\n%s\n-----END %s-----\n"
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8449 local function i(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8450 e=e and e:upper()or"CERTIFICATE";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8451 t=d.encode(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8452 return c(n,e,t:gsub(o,'%0\n',(#t-1)/64),e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8453 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8454 return{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8455 verify_identity=l;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8456 pem2der=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8457 der2pem=i;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8458 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8459 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8460 package.preload['verse.bosh']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8461 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8462 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8463 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8464 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8465 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8466 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8467 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8468 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8469 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8470 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8471 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8472 local h=require"util.xmppstream".new;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8473 local i=require"util.stanza";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8474 require"net.httpclient_listener";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8475 local t=require"net.http";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8476 local e=setmetatable({},{__index=verse.stream_mt});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8477 e.__index=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8478 local s="http://etherx.jabber.org/streams";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8479 local n="http://jabber.org/protocol/httpbind";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8480 local o=5;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8481 function verse.new_bosh(a,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8482 local t={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8483 bosh_conn_pool={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8484 bosh_waiting_requests={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8485 bosh_rid=math.random(1,999999);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8486 bosh_outgoing_buffer={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8487 bosh_url=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8488 conn={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8489 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8490 function t:reopen()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8491 self.bosh_need_restart=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8492 self:flush();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8493 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8494 local t=verse.new(a,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8495 return setmetatable(t,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8496 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8497 function e:connect()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8498 self:_send_session_request();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8499 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8500 function e:send(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8501 self:debug("Putting into BOSH send buffer: %s",tostring(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8502 self.bosh_outgoing_buffer[#self.bosh_outgoing_buffer+1]=i.clone(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8503 self:flush();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8504 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8505 function e:flush()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8506 if self.connected
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8507 and#self.bosh_waiting_requests<self.bosh_max_requests
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8508 and(#self.bosh_waiting_requests==0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8509 or#self.bosh_outgoing_buffer>0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8510 or self.bosh_need_restart)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8511 self:debug("Flushing...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8512 local e=self:_make_body();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8513 local t=self.bosh_outgoing_buffer;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8514 for o,a in ipairs(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8515 e:add_child(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8516 t[o]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8517 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8518 self:_make_request(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8519 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8520 self:debug("Decided not to flush.");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8521 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8522 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8523 function e:_make_request(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8524 local e,t=t.request(self.bosh_url,{body=tostring(a)},function(i,e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8525 if e~=0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8526 self.inactive_since=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8527 return self:_handle_response(i,e,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8528 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8529 local e=os.time();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8530 if not self.inactive_since then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8531 self.inactive_since=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8532 elseif e-self.inactive_since>self.bosh_max_inactivity then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8533 return self:_disconnected();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8534 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8535 self:debug("%d seconds left to reconnect, retrying in %d seconds...",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8536 self.bosh_max_inactivity-(e-self.inactive_since),o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8537 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8538 timer.add_task(o,function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8539 self:debug("Retrying request...");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8540 for e,a in ipairs(self.bosh_waiting_requests)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8541 if a==t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8542 table.remove(self.bosh_waiting_requests,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8543 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8544 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8545 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8546 self:_make_request(a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8547 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8548 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8549 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8550 table.insert(self.bosh_waiting_requests,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8551 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8552 self:warn("Request failed instantly: %s",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8553 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8554 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8555 function e:_disconnected()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8556 self.connected=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8557 self:event("disconnected");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8558 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8559 function e:_send_session_request()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8560 local e=self:_make_body();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8561 e.attr.hold="1";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8562 e.attr.wait="60";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8563 e.attr["xml:lang"]="en";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8564 e.attr.ver="1.6";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8565 e.attr.from=self.jid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8566 e.attr.to=self.host;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8567 e.attr.secure='true';
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8568 t.request(self.bosh_url,{body=tostring(e)},function(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8569 if e==0 then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8570 return self:_disconnected();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8571 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8572 local e=self:_parse_response(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8573 if not e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8574 self:warn("Invalid session creation response");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8575 self:_disconnected();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8576 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8577 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8578 self.bosh_sid=e.attr.sid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8579 self.bosh_wait=tonumber(e.attr.wait);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8580 self.bosh_hold=tonumber(e.attr.hold);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8581 self.bosh_max_inactivity=tonumber(e.attr.inactivity);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8582 self.bosh_max_requests=tonumber(e.attr.requests)or self.bosh_hold;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8583 self.connected=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8584 self:event("connected");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8585 self:_handle_response_payload(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8586 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8587 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8588 function e:_handle_response(t,a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8589 if self.bosh_waiting_requests[1]~=e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8590 self:warn("Server replied to request that wasn't the oldest");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8591 for t,a in ipairs(self.bosh_waiting_requests)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8592 if a==e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8593 self.bosh_waiting_requests[t]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8594 break;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8595 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8596 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8597 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8598 table.remove(self.bosh_waiting_requests,1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8599 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8600 local e=self:_parse_response(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8601 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8602 self:_handle_response_payload(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8603 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8604 self:flush();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8605 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8606 function e:_handle_response_payload(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8607 local e=t.tags;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8608 for t=1,#e do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8609 local e=e[t];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8610 if e.attr.xmlns==s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8611 self:event("stream-"..e.name,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8612 elseif e.attr.xmlns then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8613 self:event("stream/"..e.attr.xmlns,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8614 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8615 self:event("stanza",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8616 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8617 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8618 if t.attr.type=="terminate"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8619 self:_disconnected({reason=t.attr.condition});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8620 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8621 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8622 local a={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8623 stream_ns="http://jabber.org/protocol/httpbind",stream_tag="body",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8624 default_ns="jabber:client",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8625 streamopened=function(e,t)e.notopen=nil;e.payload=verse.stanza("body",t);return true;end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8626 handlestanza=function(e,t)e.payload:add_child(t);end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8627 };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8628 function e:_parse_response(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8629 self:debug("Parsing response: %s",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8630 if e==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8631 self:debug("%s",debug.traceback());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8632 self:_disconnected();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8633 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8634 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8635 local t={notopen=true,stream=self};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8636 local a=h(t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8637 a:feed(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8638 return t.payload;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8639 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8640 function e:_make_body()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8641 self.bosh_rid=self.bosh_rid+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8642 local e=verse.stanza("body",{
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8643 xmlns=n;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8644 content="text/xml; charset=utf-8";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8645 sid=self.bosh_sid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8646 rid=self.bosh_rid;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8647 });
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8648 if self.bosh_need_restart then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8649 self.bosh_need_restart=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8650 e.attr.restart='true';
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8651 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8652 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8653 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8654 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8655 package.preload['verse.client']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8656 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8657 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8658 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8659 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8660 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8661 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8662 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8663 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8664 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8665 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8666 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8667 local t=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8668 local o=t.stream_mt;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8669 local h=require"util.jid".split;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8670 local s=require"net.adns";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8671 local a=require"util.stanza";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8672 t.message,t.presence,t.iq,t.stanza,t.reply,t.error_reply=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8673 a.message,a.presence,a.iq,a.stanza,a.reply,a.error_reply;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8674 local d=require"util.xmppstream".new;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8675 local n="http://etherx.jabber.org/streams";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8676 local function r(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8677 return t.priority<e.priority or(t.priority==e.priority and t.weight>e.weight);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8678 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8679 local i={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8680 stream_ns=n,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8681 stream_tag="stream",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8682 default_ns="jabber:client"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8683 function i.streamopened(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8684 e.stream_id=t.id;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8685 if not e:event("opened",t)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8686 e.notopen=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8687 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8688 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8689 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8690 function i.streamclosed(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8691 e.notopen=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8692 if not e.closed then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8693 e:send("</stream:stream>");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8694 e.closed=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8695 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8696 e:event("closed");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8697 return e:close("stream closed")
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8698 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8699 function i.handlestanza(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8700 if e.attr.xmlns==n then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8701 return t:event("stream-"..e.name,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8702 elseif e.attr.xmlns then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8703 return t:event("stream/"..e.attr.xmlns,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8704 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8705 return t:event("stanza",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8706 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8707 function i.error(a,t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8708 if a:event(t,e)==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8709 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8710 local t=e:get_child(nil,"urn:ietf:params:xml:ns:xmpp-streams");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8711 local e=e:get_child_text("text","urn:ietf:params:xml:ns:xmpp-streams");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8712 error(t.name..(e and": "..e or""));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8713 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8714 error(e and e.name or t or"unknown-error");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8715 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8716 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8717 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8718 function o:reset()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8719 if self.stream then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8720 self.stream:reset();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8721 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8722 self.stream=d(self,i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8723 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8724 self.notopen=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8725 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8726 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8727 function o:connect_client(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8728 self.jid,self.password=e,a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8729 self.username,self.host,self.resource=h(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8730 self:add_plugin("tls");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8731 self:add_plugin("sasl");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8732 self:add_plugin("bind");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8733 self:add_plugin("session");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8734 function self.data(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8735 local t,a=self.stream:feed(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8736 if t then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8737 self:debug("Received invalid XML (%s) %d bytes: %s",tostring(a),#e,e:sub(1,300):gsub("[\r\n]+"," "));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8738 self:close("xml-not-well-formed");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8739 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8740 self:hook("connected",function()self:reopen();end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8741 self:hook("incoming-raw",function(e)return self.data(self.conn,e);end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8742 self.curr_id=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8743 self.tracked_iqs={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8744 self:hook("stanza",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8745 local t,a=e.attr.id,e.attr.type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8746 if t and e.name=="iq"and(a=="result"or a=="error")and self.tracked_iqs[t]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8747 self.tracked_iqs[t](e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8748 self.tracked_iqs[t]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8749 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8750 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8751 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8752 self:hook("stanza",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8753 local a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8754 if e.attr.xmlns==nil or e.attr.xmlns=="jabber:client"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8755 if e.name=="iq"and(e.attr.type=="get"or e.attr.type=="set")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8756 local o=e.tags[1]and e.tags[1].attr.xmlns;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8757 if o then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8758 a=self:event("iq/"..o,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8759 if not a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8760 a=self:event("iq",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8761 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8762 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8763 if a==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8764 self:send(t.error_reply(e,"cancel","service-unavailable"));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8765 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8766 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8767 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8768 a=self:event(e.name,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8769 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8770 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8771 return a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8772 end,-1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8773 self:hook("outgoing",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8774 if e.name then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8775 self:event("stanza-out",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8776 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8777 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8778 self:hook("stanza-out",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8779 if not e.attr.xmlns then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8780 self:event(e.name.."-out",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8781 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8782 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8783 local function e()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8784 self:event("ready");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8785 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8786 self:hook("session-success",e,-1)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8787 self:hook("bind-success",e,-1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8788 local e=self.close;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8789 function self:close(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8790 self.close=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8791 if not self.closed then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8792 self:send("</stream:stream>");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8793 self.closed=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8794 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8795 return self:close(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8796 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8797 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8798 local function t()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8799 self:connect(self.connect_host or self.host,self.connect_port or 5222);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8800 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8801 if not(self.connect_host or self.connect_port)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8802 s.lookup(function(a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8803 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8804 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8805 self.srv_hosts=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8806 for a,t in ipairs(a)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8807 table.insert(e,t.srv);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8808 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8809 table.sort(e,r);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8810 local a=e[1];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8811 self.srv_choice=1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8812 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8813 self.connect_host,self.connect_port=a.target,a.port;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8814 self:debug("Best record found, will connect to %s:%d",self.connect_host or self.host,self.connect_port or 5222);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8815 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8816 self:hook("disconnected",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8817 if self.srv_hosts and self.srv_choice<#self.srv_hosts then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8818 self.srv_choice=self.srv_choice+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8819 local e=e[self.srv_choice];
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8820 self.connect_host,self.connect_port=e.target,e.port;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8821 t();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8822 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8823 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8824 end,1e3);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8825 self:hook("connected",function()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8826 self.srv_hosts=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8827 end,1e3);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8828 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8829 t();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8830 end,"_xmpp-client._tcp."..(self.host)..".","SRV");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8831 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8832 t();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8833 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8834 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8835 function o:reopen()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8836 self:reset();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8837 self:send(a.stanza("stream:stream",{to=self.host,["xmlns:stream"]='http://etherx.jabber.org/streams',
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8838 xmlns="jabber:client",version="1.0"}):top_tag());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8839 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8840 function o:send_iq(e,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8841 local t=self:new_id();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8842 self.tracked_iqs[t]=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8843 e.attr.id=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8844 self:send(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8845 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8846 function o:new_id()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8847 self.curr_id=self.curr_id+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8848 return tostring(self.curr_id);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8849 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8850 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8851 package.preload['verse.component']=(function(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8852 local _ENV=_ENV;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8853 local function e(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8854 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8855 package.loaded[t]=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8856 for t=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8857 (select(t,...))(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8858 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8859 _ENV=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8860 _M=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8861 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8862 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8863 local o=require"verse";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8864 local t=o.stream_mt;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8865 local d=require"util.jid".split;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8866 local e=require"lxp";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8867 local a=require"util.stanza";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8868 local h=require"util.hashes".sha1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8869 o.message,o.presence,o.iq,o.stanza,o.reply,o.error_reply=
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8870 a.message,a.presence,a.iq,a.stanza,a.reply,a.error_reply;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8871 local r=require"util.xmppstream".new;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8872 local s="http://etherx.jabber.org/streams";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8873 local i="jabber:component:accept";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8874 local n={
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8875 stream_ns=s,
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8876 stream_tag="stream",
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8877 default_ns=i};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8878 function n.streamopened(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8879 e.stream_id=t.id;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8880 if not e:event("opened",t)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8881 e.notopen=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8882 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8883 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8884 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8885 function n.streamclosed(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8886 return e:event("closed");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8887 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8888 function n.handlestanza(t,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8889 if e.attr.xmlns==s then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8890 return t:event("stream-"..e.name,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8891 elseif e.attr.xmlns or e.name=="handshake"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8892 return t:event("stream/"..(e.attr.xmlns or i),e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8893 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8894 return t:event("stanza",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8895 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8896 function t:reset()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8897 if self.stream then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8898 self.stream:reset();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8899 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8900 self.stream=r(self,n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8901 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8902 self.notopen=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8903 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8904 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8905 function t:connect_component(e,n)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8906 self.jid,self.password=e,n;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8907 self.username,self.host,self.resource=d(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8908 function self.data(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8909 local o,a=self.stream:feed(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8910 if o then return;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8911 t:debug("Received invalid XML (%s) %d bytes: %s",tostring(a),#e,e:sub(1,300):gsub("[\r\n]+"," "));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8912 t:close("xml-not-well-formed");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8913 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8914 self:hook("incoming-raw",function(e)return self.data(self.conn,e);end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8915 self.curr_id=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8916 self.tracked_iqs={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8917 self:hook("stanza",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8918 local t,a=e.attr.id,e.attr.type;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8919 if t and e.name=="iq"and(a=="result"or a=="error")and self.tracked_iqs[t]then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8920 self.tracked_iqs[t](e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8921 self.tracked_iqs[t]=nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8922 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8923 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8924 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8925 self:hook("stanza",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8926 local t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8927 if e.attr.xmlns==nil or e.attr.xmlns=="jabber:client"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8928 if e.name=="iq"and(e.attr.type=="get"or e.attr.type=="set")then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8929 local a=e.tags[1]and e.tags[1].attr.xmlns;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8930 if a then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8931 t=self:event("iq/"..a,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8932 if not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8933 t=self:event("iq",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8934 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8935 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8936 if t==nil then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8937 self:send(o.error_reply(e,"cancel","service-unavailable"));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8938 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8939 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8940 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8941 t=self:event(e.name,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8942 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8943 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8944 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8945 end,-1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8946 self:hook("opened",function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8947 print(self.jid,self.stream_id,e.id);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8948 local e=h(self.stream_id..n,true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8949 self:send(a.stanza("handshake",{xmlns=i}):text(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8950 self:hook("stream/"..i,function(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8951 if e.name=="handshake"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8952 self:event("authentication-success");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8953 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8954 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8955 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8956 local function e()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8957 self:event("ready");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8958 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8959 self:hook("authentication-success",e,-1);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8960 self:connect(self.connect_host or self.host,self.connect_port or 5347);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8961 self:reopen();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8962 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8963 function t:reopen()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8964 self:reset();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8965 self:send(a.stanza("stream:stream",{to=self.jid,["xmlns:stream"]='http://etherx.jabber.org/streams',
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8966 xmlns=i,version="1.0"}):top_tag());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8967 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8968 function t:close(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8969 if not self.notopen then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8970 self:send("</stream:stream>");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8971 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8972 local t=self.conn.disconnect();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8973 self.conn:close();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8974 t(conn,e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8975 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8976 function t:send_iq(t,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8977 local e=self:new_id();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8978 self.tracked_iqs[e]=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8979 t.attr.id=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8980 self:send(t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8981 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8982 function t:new_id()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8983 self.curr_id=self.curr_id+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8984 return tostring(self.curr_id);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8985 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8986 end)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8987 pcall(require,"luarocks.require");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8988 local n=require"socket";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8989 pcall(require,"ssl");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8990 local a=require"net.server";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8991 local s=require"util.events";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8992 local o=require"util.logger";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8993 local e={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8994 e.server=a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8995 local t={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8996 t.__index=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8997 e.stream_mt=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8998 e.plugins={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8999 function e.init(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9000 for e=1,select("#",...)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9001 local t,a=pcall(require,"verse."..select(e,...));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9002 if not t then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9003 error("Verse connection module not found: verse."..select(e,...)..a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9004 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9005 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9006 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9007 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9008 local i=0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9009 function e.new(o,a)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9010 local t=setmetatable(a or{},t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9011 i=i+1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9012 t.id=tostring(i);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9013 t.logger=o or e.new_logger("stream"..t.id);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9014 t.events=s.new();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9015 t.plugins={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9016 t.verse=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9017 return t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9018 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9019 e.add_task=require"util.timer".add_task;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9020 e.logger=o.init;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9021 e.new_logger=o.init;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9022 e.log=e.logger("verse");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9023 local function i(t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9024 local e,a,o=0,{...},select('#',...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9025 return(t:gsub("%%(.)",function(t)if e<=o then e=e+1;return tostring(a[e]);end end));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9026 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9027 function e.set_log_handler(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9028 t=t or{"debug","info","warn","error"};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9029 o.reset();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9030 if io.type(e)=="file"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9031 local t=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9032 function e(e,a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9033 t:write(e,"\t",a,"\t",o,"\n");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9034 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9035 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9036 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9037 local function n(o,a,t,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9038 return e(o,a,i(t,...));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9039 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9040 for t,e in ipairs(t)do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9041 o.add_level_sink(e,n);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9042 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9043 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9044 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9045 function e._default_log_handler(t,a,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9046 return io.stderr:write(t,"\t",a,"\t",o,"\n");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9047 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9048 e.set_log_handler(e._default_log_handler,{"error"});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9049 local function o(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9050 e.log("error","Error: %s",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9051 e.log("error","Traceback: %s",debug.traceback());
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9052 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9053 function e.set_error_handler(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9054 o=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9055 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9056 function e.loop()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9057 return xpcall(a.loop,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9058 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9059 function e.step()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9060 return xpcall(a.step,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9061 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9062 function e.quit()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9063 return a.setquitting("once");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9064 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9065 function t:listen(o,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9066 o=o or"localhost";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9067 t=t or 0;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9068 local e,a=a.addserver(o,t,e.new_listener(self,"server"),"*a");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9069 if e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9070 self:debug("Bound to %s:%s",o,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9071 self.server=e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9072 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9073 return e,a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9074 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9075 function t:connect(i,o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9076 i=i or"localhost";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9077 o=tonumber(o)or 5222;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9078 local n=n.tcp()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9079 n:settimeout(0);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9080 n:setoption("keepalive",true);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9081 local s,t=n:connect(i,o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9082 if not s and t~="timeout"then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9083 self:warn("connect() to %s:%d failed: %s",i,o,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9084 return self:event("disconnected",{reason=t})or false,t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9085 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9086 local e=a.wrapclient(n,i,o,e.new_listener(self),"*a");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9087 if not e then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9088 self:warn("connection initialisation failed: %s",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9089 return self:event("disconnected",{reason=t})or false,t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9090 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9091 self:set_conn(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9092 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9093 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9094 function t:set_conn(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9095 self.conn=t;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9096 self.send=function(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9097 self:event("outgoing",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9098 e=tostring(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9099 self:event("outgoing-raw",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9100 return t:write(e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9101 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9102 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9103 function t:close(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9104 if not self.conn then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9105 e.log("error","Attempt to close disconnected connection - possibly a bug");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9106 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9107 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9108 local e=self.conn.disconnect();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9109 self.conn:close();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9110 e(self.conn,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9111 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9112 function t:debug(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9113 return self.logger("debug",...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9114 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9115 function t:info(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9116 return self.logger("info",...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9117 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9118 function t:warn(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9119 return self.logger("warn",...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9120 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9121 function t:error(...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9122 return self.logger("error",...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9123 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9124 function t:event(e,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9125 self:debug("Firing event: "..tostring(e));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9126 return self.events.fire_event(e,...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9127 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9128 function t:hook(e,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9129 return self.events.add_handler(e,...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9130 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9131 function t:unhook(e,t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9132 return self.events.remove_handler(e,t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9133 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9134 function e.eventable(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9135 e.events=s.new();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9136 e.hook,e.unhook=t.hook,t.unhook;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9137 local t=e.events.fire_event;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9138 function e:event(e,...)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9139 return t(e,...);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9140 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9141 return e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9142 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9143 function t:add_plugin(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9144 if self.plugins[t]then return true;end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9145 if require("verse.plugins."..t)then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9146 local e,a=e.plugins[t](self);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9147 if e~=false then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9148 self:debug("Loaded %s plugin",t);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9149 self.plugins[t]=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9150 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9151 self:warn("Failed to load %s plugin: %s",t,a);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9152 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9153 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9154 return self;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9155 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9156 function e.new_listener(t)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9157 local a={};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9158 function a.onconnect(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9159 if t.server then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9160 local a=e.new();
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9161 o:setlistener(e.new_listener(a));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9162 a:set_conn(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9163 t:event("connected",{client=a});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9164 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9165 t.connected=true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9166 t:event("connected");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9167 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9168 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9169 function a.onincoming(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9170 t:event("incoming-raw",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9171 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9172 function a.ondisconnect(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9173 if a~=t.conn then return end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9174 t.connected=false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9175 t:event("disconnected",{reason=e});
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9176 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9177 function a.ondrain(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9178 t:event("drained");
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9179 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9180 function a.onstatus(a,e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9181 t:event("status",e);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9182 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9183 return a;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9184 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9185 return e;

mercurial