verse.lua

changeset 177
3c19b67a1f0f
parent 103
9e0e56393978
child 180
249197af6c01
equal deleted inserted replaced
176:7674fb1dcc41 177:3c19b67a1f0f
13 local function e() 13 local function e()
14 error("Function not implemented"); 14 error("Function not implemented");
15 end 15 end
16 local e=require"mime"; 16 local e=require"mime";
17 a"encodings" 17 a"encodings"
18 idna={};
18 stringprep={}; 19 stringprep={};
19 base64={encode=e.b64,decode=e.unb64}; 20 base64={encode=e.b64,decode=e.unb64};
21 utf8={
22 valid=(utf8 and utf8.len)and function(e)return not not utf8.len(e);end or function()return true;end;
23 };
20 return _M; 24 return _M;
21 end) 25 end)
22 package.preload['util.hashes']=(function(...) 26 package.preload['util.sha1']=(function(...)
23 local _ENV=_ENV; 27 local _ENV=_ENV;
24 local function e(t,...) 28 local function e(t,...)
25 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 29 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
26 package.loaded[t]=e; 30 package.loaded[t]=e;
27 for t=1,select("#",...)do 31 for t=1,select("#",...)do
29 end 33 end
30 _ENV=e; 34 _ENV=e;
31 _M=e; 35 _M=e;
32 return e; 36 return e;
33 end 37 end
34 local function t(t,e) 38 local m=string.len
35 error("Hash method "..e.." not available",2); 39 local o=string.char
36 end 40 local k=string.byte
37 local e=setmetatable({},{__index=t}); 41 local q=string.sub
38 local function t(e,o) 42 local c=math.floor
39 local e,a=pcall(require,e); 43 local t=require"bit"
40 if e then o(a);end 44 local j=t.bnot
41 end 45 local e=t.band
42 t("bgcrypto.md5",function(t) 46 local y=t.bor
43 e.md5=t.digest; 47 local n=t.bxor
44 e.hmac_md5=t.hmac.digest; 48 local a=t.lshift
45 end); 49 local i=t.rshift
46 t("bgcrypto.sha1",function(t) 50 local u,r,l,d,h
47 e.sha1=t.digest; 51 local function p(t,e)
48 e.hmac_sha1=t.hmac.digest; 52 return a(t,e)+i(t,32-e)
49 e.scram_Hi_sha1=function(e,a,o)return t.pbkdf2(e,a,o,20);end; 53 end
50 end); 54 local function s(i)
51 t("bgcrypto.sha256",function(t) 55 local t,a
52 e.sha256=t.digest; 56 local t=""
53 e.hmac_sha256=t.hmac.digest; 57 for n=1,8 do
54 end); 58 a=e(i,15)
55 t("bgcrypto.sha512",function(t) 59 if(a<10)then
56 e.sha512=t.digest; 60 t=o(a+48)..t
57 e.hmac_sha512=t.hmac.digest; 61 else
58 end); 62 t=o(a+87)..t
59 t("sha1",function(t) 63 end
60 e.sha1=function(e,a) 64 i=c(i/16)
61 if a then 65 end
62 return t.sha1(e); 66 return t
63 else 67 end
64 return(t.binary(e)); 68 local function g(t)
65 end 69 local i,a
66 end; 70 local n=""
67 end); 71 i=m(t)*8
68 return e; 72 t=t..o(128)
73 a=56-e(m(t),63)
74 if(a<0)then
75 a=a+64
76 end
77 for e=1,a do
78 t=t..o(0)
79 end
80 for t=1,8 do
81 n=o(e(i,255))..n
82 i=c(i/256)
83 end
84 return t..n
85 end
86 local function b(w)
87 local m,t,o,a,f,s,c,v
88 local i,i
89 local i={}
90 while(w~="")do
91 for e=0,15 do
92 i[e]=0
93 for t=1,4 do
94 i[e]=i[e]*256+k(w,e*4+t)
95 end
96 end
97 for e=16,79 do
98 i[e]=p(n(n(i[e-3],i[e-8]),n(i[e-14],i[e-16])),1)
99 end
100 m=u
101 t=r
102 o=l
103 a=d
104 f=h
105 for h=0,79 do
106 if(h<20)then
107 s=y(e(t,o),e(j(t),a))
108 c=1518500249
109 elseif(h<40)then
110 s=n(n(t,o),a)
111 c=1859775393
112 elseif(h<60)then
113 s=y(y(e(t,o),e(t,a)),e(o,a))
114 c=2400959708
115 else
116 s=n(n(t,o),a)
117 c=3395469782
118 end
119 v=p(m,5)+s+f+c+i[h]
120 f=a
121 a=o
122 o=p(t,30)
123 t=m
124 m=v
125 end
126 u=e(u+m,4294967295)
127 r=e(r+t,4294967295)
128 l=e(l+o,4294967295)
129 d=e(d+a,4294967295)
130 h=e(h+f,4294967295)
131 w=q(w,65)
132 end
133 end
134 local function a(e,t)
135 e=g(e)
136 u=1732584193
137 r=4023233417
138 l=2562383102
139 d=271733878
140 h=3285377520
141 b(e)
142 local e=s(u)..s(r)..s(l)
143 ..s(d)..s(h);
144 if t then
145 return e;
146 else
147 return(e:gsub("..",function(e)
148 return string.char(tonumber(e,16));
149 end));
150 end
151 end
152 _G.sha1={sha1=a};
153 return _G.sha1;
69 end) 154 end)
70 package.preload['lib.adhoc']=(function(...) 155 package.preload['lib.adhoc']=(function(...)
71 local _ENV=_ENV; 156 local _ENV=_ENV;
72 local function r(t,...) 157 local function d(t,...)
73 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 158 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
74 package.loaded[t]=e; 159 package.loaded[t]=e;
75 for t=1,select("#",...)do 160 for t=1,select("#",...)do
76 (select(t,...))(e); 161 (select(t,...))(e);
77 end 162 end
78 _ENV=e; 163 _ENV=e;
79 _M=e; 164 _M=e;
80 return e; 165 return e;
81 end 166 end
82 local s,i=require"util.stanza",require"util.uuid"; 167 local s,l=require"util.stanza",require"util.id".short;
83 local e="http://jabber.org/protocol/commands"; 168 local r="http://jabber.org/protocol/commands";
84 local n={} 169 local n={}
85 local h={}; 170 local h={};
86 local function o(i,o,t,a) 171 local function i(o,e,t,a)
87 local e=s.stanza("command",{xmlns=e,node=i.node,status=o}); 172 local e=s.stanza("command",{xmlns=r,node=o.node,status=e});
88 if t then e.attr.sessionid=t;end 173 if t then e.attr.sessionid=t;end
89 if a then e.attr.action=a;end 174 if a then e.attr.action=a;end
90 return e; 175 return e;
91 end 176 end
92 function h.new(a,i,e,t) 177 function h.new(e,o,a,t)
93 return{name=a,node=i,handler=e,cmdtag=o,permission=(t or"user")}; 178 return{name=e,node=o,handler=a,cmdtag=i,permission=(t or"user")};
94 end 179 end
95 function h.handle_cmd(o,h,t) 180 function h.handle_cmd(o,h,t)
96 local e=t.tags[1].attr.sessionid or i.generate(); 181 local e=t.tags[1].attr.sessionid or l();
97 local a={}; 182 local a={};
98 a.to=t.attr.to; 183 a.to=t.attr.to;
99 a.from=t.attr.from; 184 a.from=t.attr.from;
100 a.action=t.tags[1].attr.action or"execute"; 185 a.action=t.tags[1].attr.action or"execute";
101 a.form=t.tags[1]:child_with_ns("jabber:x:data"); 186 a.form=t.tags[1]:child_with_ns("jabber:x:data");
128 local a=s.stanza("actions"); 213 local a=s.stanza("actions");
129 for i,e in ipairs(e)do 214 for i,e in ipairs(e)do
130 if(e=="prev")or(e=="next")or(e=="complete")then 215 if(e=="prev")or(e=="next")or(e=="complete")then
131 a:tag(e):up(); 216 a:tag(e):up();
132 else 217 else
133 r:log("error",'Command "'..o.name.. 218 d:log("error",'Command "'..o.name..
134 '" at node "'..o.node..'" provided an invalid action "'..e..'"'); 219 '" at node "'..o.node..'" provided an invalid action "'..e..'"');
135 end 220 end
136 end 221 end
137 t:add_child(a); 222 t:add_child(a);
138 elseif a=="form"then 223 elseif a=="form"then
147 h.send(i); 232 h.send(i);
148 return true; 233 return true;
149 end 234 end
150 return h; 235 return h;
151 end) 236 end)
152 package.preload['util.stanza']=(function(...) 237 package.preload['util.table']=(function(...)
153 local _ENV=_ENV; 238 local _ENV=_ENV;
154 local function h(t,...) 239 local function e(t,...)
155 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 240 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
156 package.loaded[t]=e; 241 package.loaded[t]=e;
157 for t=1,select("#",...)do 242 for t=1,select("#",...)do
158 (select(t,...))(e); 243 (select(t,...))(e);
159 end 244 end
160 _ENV=e; 245 _ENV=e;
161 _M=e; 246 _M=e;
162 return e; 247 return e;
163 end 248 end
164 local t=table.insert; 249 return{pack=function(...)return{n=select("#",...);...}end;create=function()return{}end}
165 local s=table.remove; 250 end)
166 local p=table.concat; 251 package.preload['util.stanza']=(function(...)
167 local r=string.format; 252 local _ENV=_ENV;
168 local u=string.match; 253 local function e(t,...)
169 local w=tostring; 254 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
170 local m=setmetatable; 255 package.loaded[t]=e;
171 local n=pairs; 256 for t=1,select("#",...)do
172 local i=ipairs; 257 (select(t,...))(e);
173 local o=type; 258 end
174 local y=string.gsub; 259 _ENV=e;
175 local l=string.sub; 260 _M=e;
176 local f=string.find; 261 return e;
177 local e=os; 262 end
178 local c=not e.getenv("WINDIR"); 263 local a=error;
179 local d,a; 264 local o=table.insert;
180 if c then 265 local l=table.remove;
181 local t,e=pcall(require,"util.termcolours"); 266 local m=table.concat;
182 if t then 267 local d=string.match;
183 d,a=e.getstyle,e.getstring; 268 local f=tostring;
184 else 269 local u=setmetatable;
185 c=nil; 270 local x=getmetatable;
186 end 271 local h=pairs;
187 end 272 local s=ipairs;
188 local v="urn:ietf:params:xml:ns:xmpp-stanzas"; 273 local t=type;
189 h"stanza" 274 local j=string.gsub;
190 stanza_mt={__type="stanza"}; 275 local y=string.sub;
191 stanza_mt.__index=stanza_mt; 276 local r=string.find;
192 local e=stanza_mt; 277 local c=table.move or require"util.table".move;
193 function stanza(a,t) 278 local b=require"util.table".create;
194 local t={name=a,attr=t or{},tags={}}; 279 local v=require"util.encodings".utf8.valid;
195 return m(t,e); 280 local q,p=pcall(require,"util.termcolours");
196 end 281 local g="urn:ietf:params:xml:ns:xmpp-stanzas";
197 local h=stanza; 282 local k={xmlns=g};
283 local _ENV=nil;
284 local e={__name="stanza"};
285 e.__index=e;
286 local function i(t,e)
287 return not r(t,e and"[^\1\9\10\13\20-\255]"or"[^\9\10\13\20-\255]");
288 end
289 local function n(o,e)
290 if t(o)~="string"then
291 a("invalid "..e.." name: expected string, got "..t(o));
292 elseif#o==0 then
293 a("invalid "..e.." name: empty string");
294 elseif r(o,"[<>& '\"]")then
295 a("invalid "..e.." name: contains invalid characters");
296 elseif not i(o,e=="attribute")then
297 a("invalid "..e.." name: contains control characters");
298 elseif not v(o)then
299 a("invalid "..e.." name: contains invalid utf8");
300 end
301 end
302 local function w(e,o)
303 if t(e)~="string"then
304 a("invalid "..o.." value: expected string, got "..t(e));
305 elseif not i(e,false)then
306 a("invalid "..o.." value: contains control characters");
307 elseif not v(e)then
308 a("invalid "..o.." value: contains invalid utf8");
309 end
310 end
311 local function v(e)
312 if e~=nil then
313 if t(e)~="table"then
314 a("invalid attributes: expected table, got "..t(e));
315 end
316 for t,e in h(e)do
317 n(t,"attribute");
318 w(e,"attribute");
319 end
320 end
321 end
322 local function i(t,a,o)
323 n(t,"tag");
324 v(a);
325 local t={name=t,attr=a or{},namespaces=o,tags={}};
326 return u(t,e);
327 end
328 local function n(t)
329 return x(t)==e;
330 end
198 function e:query(e) 331 function e:query(e)
199 return self:tag("query",{xmlns=e}); 332 return self:tag("query",{xmlns=e});
200 end 333 end
201 function e:body(t,e) 334 function e:body(t,e)
202 return self:tag("body",e):text(t); 335 return self:text_tag("body",t,e);
203 end 336 end
204 function e:tag(a,e) 337 function e:text_tag(e,t,o,a)
205 local a=h(a,e); 338 return self:tag(e,o,a):text(t):up();
339 end
340 function e:tag(t,e,a)
341 local t=i(t,e,a);
206 local e=self.last_add; 342 local e=self.last_add;
207 if not e then e={};self.last_add=e;end 343 if not e then e={};self.last_add=e;end
208 (e[#e]or self):add_direct_child(a); 344 (e[#e]or self):add_direct_child(t);
209 t(e,a); 345 o(e,t);
210 return self; 346 return self;
211 end 347 end
212 function e:text(t) 348 function e:text(t)
349 if t~=nil and t~=""then
213 local e=self.last_add; 350 local e=self.last_add;
214 (e and e[#e]or self):add_direct_child(t); 351 (e and e[#e]or self):add_direct_child(t);
352 end
215 return self; 353 return self;
216 end 354 end
217 function e:up() 355 function e:up()
218 local e=self.last_add; 356 local e=self.last_add;
219 if e then s(e);end 357 if e then l(e);end
220 return self; 358 return self;
359 end
360 function e:at_top()
361 return self.last_add==nil or#self.last_add==0
221 end 362 end
222 function e:reset() 363 function e:reset()
223 self.last_add=nil; 364 self.last_add=nil;
224 return self; 365 return self;
225 end 366 end
226 function e:add_direct_child(e) 367 function e:add_direct_child(e)
227 if o(e)=="table"then 368 if n(e)then
228 t(self.tags,e); 369 o(self.tags,e);
229 end 370 o(self,e);
230 t(self,e); 371 else
372 w(e,"text");
373 o(self,e);
374 end
231 end 375 end
232 function e:add_child(t) 376 function e:add_child(t)
233 local e=self.last_add; 377 local e=self.last_add;
234 (e and e[#e]or self):add_direct_child(t); 378 (e and e[#e]or self):add_direct_child(t);
235 return self; 379 return self;
236 end 380 end
237 function e:get_child(t,a) 381 function e:remove_children(a,e)
238 for o,e in i(self.tags)do 382 e=e or self.attr.xmlns;
239 if(not t or e.name==t) 383 return self:maptags(function(t)
240 and((not a and self.attr.xmlns==e.attr.xmlns) 384 if(not a or t.name==a)and t.attr.xmlns==e then
241 or e.attr.xmlns==a)then 385 return nil;
242 return e; 386 end
243 end 387 return t;
244 end 388 end);
389 end
390 function e:get_child(a,t)
391 for o,e in s(self.tags)do
392 if(not a or e.name==a)
393 and((not t and self.attr.xmlns==e.attr.xmlns)
394 or e.attr.xmlns==t)then
395 return e;
396 end
397 end
398 return nil;
245 end 399 end
246 function e:get_child_text(e,t) 400 function e:get_child_text(e,t)
247 local e=self:get_child(e,t); 401 local e=self:get_child(e,t);
248 if e then 402 if e then
249 return e:get_text(); 403 return e:get_text();
250 end 404 end
251 return nil; 405 return nil;
252 end 406 end
407 function e:get_child_attr(e,a,t)
408 local e=self:get_child(e,a);
409 if e then
410 return e.attr[t];
411 end
412 return nil;
413 end
253 function e:child_with_name(t) 414 function e:child_with_name(t)
254 for a,e in i(self.tags)do 415 for a,e in s(self.tags)do
255 if e.name==t then return e;end 416 if e.name==t then return e;end
256 end 417 end
418 return nil;
257 end 419 end
258 function e:child_with_ns(t) 420 function e:child_with_ns(t)
259 for a,e in i(self.tags)do 421 for a,e in s(self.tags)do
260 if e.attr.xmlns==t then return e;end 422 if e.attr.xmlns==t then return e;end
261 end 423 end
424 return nil;
425 end
426 function e:get_child_with_attr(e,i,t,o,a)
427 for e in self:childtags(e,i)do
428 if(a and a(e.attr[t])or e.attr[t])==o then
429 return e;
430 end
431 end
432 return nil;
262 end 433 end
263 function e:children() 434 function e:children()
264 local e=0; 435 local e=0;
265 return function(t) 436 return function(t)
266 e=e+1 437 e=e+1
267 return t[e]; 438 return t[e];
268 end,self,e; 439 end,self,e;
269 end 440 end
270 function e:childtags(i,o) 441 function e:childtags(o,a)
271 local e=self.tags; 442 local e=self.tags;
272 local a,t=1,#e; 443 local i,t=1,#e;
273 return function() 444 return function()
274 for t=a,t do 445 for t=i,t do
275 local e=e[t]; 446 local e=e[t];
276 if(not i or e.name==i) 447 if(not o or e.name==o)
277 and((not o and self.attr.xmlns==e.attr.xmlns) 448 and((not a and self.attr.xmlns==e.attr.xmlns)
278 or e.attr.xmlns==o)then 449 or e.attr.xmlns==a)then
279 a=t+1; 450 i=t+1;
280 return e; 451 return e;
281 end 452 end
282 end 453 end
283 end; 454 end;
284 end 455 end
285 function e:maptags(i) 456 function e:maptags(h)
286 local o,t=self.tags,1; 457 local o,t=self.tags,1;
287 local n,a=#self,#o; 458 local n,i=#self,#o;
459 local s=n+1;
288 local e=1; 460 local e=1;
289 while t<=a and a>0 do 461 while t<=i and i>0 do
290 if self[e]==o[t]then 462 if self[e]==o[t]then
291 local i=i(self[e]); 463 local a=h(self[e]);
292 if i==nil then 464 if a==nil then
293 s(self,e); 465 l(self,e);
294 s(o,t); 466 l(o,t);
295 n=n-1; 467 n=n-1;
296 a=a-1; 468 i=i-1;
297 e=e-1; 469 e=e-1;
298 t=t-1; 470 t=t-1;
299 else 471 else
300 self[e]=i; 472 self[e]=a;
301 o[t]=i; 473 o[t]=a;
302 end 474 end
303 t=t+1; 475 t=t+1;
304 end 476 end
305 e=e+1; 477 e=e+1;
478 if e>s then
479 a("Invalid stanza state! Please report this error.");
480 end
306 end 481 end
307 return self; 482 return self;
308 end 483 end
309 function e:find(a) 484 function e:find(a)
310 local e=1; 485 local e=1;
311 local s=#a+1; 486 local s=#a+1;
312 repeat 487 repeat
313 local o,t,n; 488 local o,t,i;
314 local i=l(a,e,e); 489 local n=y(a,e,e);
315 if i=="@"then 490 if n=="@"then
316 return self.attr[l(a,e+1)]; 491 return self.attr[y(a,e+1)];
317 elseif i=="{"then 492 elseif n=="{"then
318 o,e=u(a,"^([^}]+)}()",e+1); 493 o,e=d(a,"^([^}]+)}()",e+1);
319 end 494 end
320 t,n,e=u(a,"^([^@/#]*)([/#]?)()",e); 495 t,i,e=d(a,"^([^@/#]*)([/#]?)()",e);
321 t=t~=""and t or nil; 496 t=t~=""and t or nil;
322 if e==s then 497 if e==s then
323 if n=="#"then 498 if i=="#"then
324 return self:get_child_text(t,o); 499 return self:get_child_text(t,o);
325 end 500 end
326 return self:get_child(t,o); 501 return self:get_child(t,o);
327 end 502 end
328 self=self:get_child(t,o); 503 self=self:get_child(t,o);
329 until not self 504 until not self
330 end 505 end
331 local l 506 local function l(t,s)
332 do 507 local n={};
333 local e={["'"]="&apos;",["\""]="&quot;",["<"]="&lt;",[">"]="&gt;",["&"]="&amp;"}; 508 for t,e in h(t.attr)do n[t]=e;end
334 function l(t)return(y(t,"['&<>\"]",e));end 509 local a,i=t.namespaces;
335 _M.xml_escape=l; 510 if a then
336 end 511 i={};
337 local function y(o,e,h,a,r) 512 for e,t in h(a)do i[e]=t;end
513 end
514 local o,a;
515 if s then
516 o={};
517 a={name=t.name,attr=n,namespaces=i,tags=o};
518 else
519 o=b(#t.tags,0);
520 a=b(#t,4);
521 a.name=t.name;
522 a.attr=n;
523 a.namespaces=i;
524 a.tags=o;
525 end
526 u(a,e);
527 if not s then
528 c(t,1,#t,1,a);
529 c(t.tags,1,#t.tags,1,o);
530 a:maptags(l);
531 end
532 return a;
533 end
534 local function u(e,o)
535 if not n(e)then
536 a("bad argument to clone: expected stanza, got "..t(e));
537 end
538 return l(e,o);
539 end
540 local l={["'"]="&apos;",["\""]="&quot;",["<"]="&lt;",[">"]="&gt;",["&"]="&amp;"};
541 local function c(e)return(j(e,"['&<>\"]",l));end
542 local function l(a,e,s,t,u)
338 local i=0; 543 local i=0;
339 local s=o.name 544 local l=a.name
340 t(e,"<"..s); 545 o(e,"<"..l);
341 for o,n in n(o.attr)do 546 for a,n in h(a.attr)do
342 if f(o,"\1",1,true)then 547 if r(a,"\1",1,true)then
343 local s,o=u(o,"^([^\1]*)\1?(.*)$"); 548 local s,a=d(a,"^([^\1]*)\1?(.*)$");
344 i=i+1; 549 i=i+1;
345 t(e," xmlns:ns"..i.."='"..a(s).."' ".."ns"..i..":"..o.."='"..a(n).."'"); 550 o(e," xmlns:ns"..i.."='"..t(s).."' ".."ns"..i..":"..a.."='"..t(n).."'");
346 elseif not(o=="xmlns"and n==r)then 551 elseif not(a=="xmlns"and n==u)then
347 t(e," "..o.."='"..a(n).."'"); 552 o(e," "..a.."='"..t(n).."'");
348 end 553 end
349 end 554 end
350 local i=#o; 555 local i=#a;
351 if i==0 then 556 if i==0 then
352 t(e,"/>"); 557 o(e,"/>");
353 else 558 else
354 t(e,">"); 559 o(e,">");
355 for i=1,i do 560 for i=1,i do
356 local i=o[i]; 561 local i=a[i];
357 if i.name then 562 if i.name then
358 h(i,e,h,a,o.attr.xmlns); 563 s(i,e,s,t,a.attr.xmlns);
359 else 564 else
360 t(e,a(i)); 565 o(e,t(i));
361 end 566 end
362 end 567 end
363 t(e,"</"..s..">"); 568 o(e,"</"..l..">");
364 end 569 end
365 end 570 end
366 function e.__tostring(t) 571 function e.__tostring(t)
367 local e={}; 572 local e={};
368 y(t,e,y,l,nil); 573 l(t,e,l,c,nil);
369 return p(e); 574 return m(e);
370 end 575 end
371 function e.top_tag(t) 576 function e.top_tag(e)
372 local e=""; 577 local e=u(e,true);
373 if t.attr then 578 return f(e):sub(1,-3)..">";
374 for t,a in n(t.attr)do if o(t)=="string"then e=e..r(" %s='%s'",t,l(w(a)));end end
375 end
376 return r("<%s%s>",t.name,e);
377 end 579 end
378 function e.get_text(e) 580 function e.get_text(e)
379 if#e.tags==0 then 581 if#e.tags==0 then
380 return p(e); 582 return m(e);
381 end 583 end
584 return nil;
382 end 585 end
383 function e.get_error(e) 586 function e.get_error(e)
384 local o,a,t; 587 local i,t,a,o;
385 local e=e:get_child("error"); 588 local e=e:get_child("error");
386 if not e then 589 if not e then
387 return nil,nil,nil; 590 return nil,nil,nil,nil;
388 end 591 end
389 o=e.attr.type; 592 i=e.attr.type;
390 for o,e in i(e.tags)do 593 for i,e in s(e.tags)do
391 if e.attr.xmlns==v then 594 if e.attr.xmlns==g then
392 if not t and e.name=="text"then 595 if not a and e.name=="text"then
393 t=e:get_text(); 596 a=e:get_text();
394 elseif not a then 597 elseif not t then
395 a=e.name; 598 t=e.name;
396 end 599 end
397 if a and t then 600 else
601 o=e;
602 end
603 if t and a and o then
398 break; 604 break;
399 end 605 end
400 end 606 end
401 end 607 return i,t or"undefined-condition",a,o;
402 return o,a or"undefined-condition",t; 608 end
403 end 609 function e.add_error(o,a,s,h,i)
404 do 610 local e;
405 local e=0; 611 if t(a)=="table"then
406 function new_id() 612 if t(a.extra)=="table"then
407 e=e+1; 613 e=a.extra;
408 return"lx"..e; 614 end
409 end 615 if t(a.context)=="table"and t(a.context.by)=="string"then i=a.context.by;end
410 end 616 a,s,h=a.type,a.condition,a.text;
411 function preserialize(e) 617 end
412 local a={name=e.name,attr=e.attr}; 618 if o.attr.from==i then
413 for i,e in i(e)do 619 i=nil;
414 if o(e)=="table"then 620 end
415 t(a,preserialize(e)); 621 o:tag("error",{type=a,by=i})
416 else 622 :tag(s,k);
417 t(a,e); 623 if e and s=="gone"and t(e.uri)=="string"then
418 end 624 o:text(e.uri);
419 end 625 end
420 return a; 626 o:up();
421 end 627 if h then o:text_tag("text",h,k);end
422 function deserialize(a) 628 if e and n(e.tag)then
629 o:add_child(e.tag);
630 elseif e and e.namespace and e.condition then
631 o:tag(e.condition,{xmlns=e.namespace}):up();
632 end
633 return o:up();
634 end
635 local function l(a)
636 local i={name=a.name,attr=a.attr};
637 for a,e in s(a)do
638 if t(e)=="table"then
639 o(i,l(e));
640 else
641 o(i,e);
642 end
643 end
644 return i;
645 end
646 e.__freeze=l;
647 local function m(a)
423 if a then 648 if a then
424 local s=a.attr; 649 local e=a.attr;
425 for e=1,#s do s[e]=nil;end 650 local o={};
426 local h={}; 651 for e,a in h(e)do
427 for e in n(s)do 652 if t(e)=="string"then
428 if f(e,"|",1,true)and not f(e,"\1",1,true)then 653 if r(e,"|",1,true)and not r(e,"\1",1,true)then
429 local t,a=u(e,"^([^|]+)|(.+)$"); 654 local e,t=d(e,"^([^|]+)|(.+)$");
430 h[t.."\1"..a]=s[e]; 655 o[e.."\1"..t]=a;
431 s[e]=nil; 656 else
432 end 657 o[e]=a;
433 end 658 end
434 for t,e in n(h)do 659 end
435 s[t]=e; 660 end
436 end 661 local o=i(a.name,o);
437 m(a,e); 662 for a,e in s(a)do
438 for t,e in i(a)do 663 if t(e)=="table"then
439 if o(e)=="table"then 664 o:add_direct_child(m(e));
440 deserialize(e); 665 elseif t(e)=="string"then
441 end 666 o:add_direct_child(e);
442 end 667 end
443 if not a.tags then 668 end
444 local n={}; 669 return o;
445 for i,e in i(a)do 670 end
446 if o(e)=="table"then 671 end
447 t(n,e); 672 local function y(e,t)
448 end 673 if not t then
449 end 674 return i("message",e);
450 a.tags=n; 675 else
451 end 676 return i("message",e):text_tag("body",t);
452 end 677 end
453 return a; 678 end
454 end 679 local function v(e)
455 local function s(a)
456 local i,o={},{};
457 for t,e in n(a.attr)do i[t]=e;end
458 local i={name=a.name,attr=i,tags=o};
459 for e=1,#a do
460 local e=a[e];
461 if e.name then
462 e=s(e);
463 t(o,e);
464 end
465 t(i,e);
466 end
467 return m(i,e);
468 end
469 clone=s;
470 function message(t,e)
471 if not e then 680 if not e then
472 return h("message",t); 681 a("iq stanzas require id and type attributes");
473 else 682 end
474 return h("message",t):tag("body"):text(e):up(); 683 if not e.id then
475 end 684 a("iq stanzas require an id attribute");
476 end 685 end
477 function iq(e) 686 if not e.type then
478 if e and not e.id then e.id=new_id();end 687 a("iq stanzas require a type attribute");
479 return h("iq",e or{id=new_id()}); 688 end
480 end 689 return i("iq",e);
481 function reply(e) 690 end
482 return h(e.name,e.attr and{to=e.attr.from,from=e.attr.to,id=e.attr.id,type=((e.name=="iq"and"result")or e.attr.type)}); 691 local function r(e)
483 end 692 if not n(e)then
484 do 693 a("bad argument to reply: expected stanza, got "..t(e));
485 local t={xmlns=v}; 694 end
486 function error_reply(e,o,i,a) 695 return i(e.name,
487 local e=reply(e); 696 {
697 to=e.attr.from,
698 from=e.attr.to,
699 id=e.attr.id,
700 type=((e.name=="iq"and"result")or e.attr.type)
701 });
702 end
703 local function w(e,s,h,i,o)
704 if not n(e)then
705 a("bad argument to error_reply: expected stanza, got "..t(e));
706 elseif e.attr.type=="error"then
707 a("bad argument to error_reply: got stanza of type error which must not be replied to");
708 end
709 local e=r(e);
488 e.attr.type="error"; 710 e.attr.type="error";
489 e:tag("error",{type=o}) 711 e:add_error(s,h,i,o);
490 :tag(i,t):up(); 712 e.last_add={e[1]};
491 if(a)then e:tag("text",t):text(a):up();end 713 return e;
492 return e; 714 end
493 end 715 local function d(e)
494 end 716 return i("presence",e);
495 function presence(e) 717 end
496 return h("presence",e); 718 local s;
497 end 719 if q then
498 if c then 720 local a,t=p.getstyle,p.getstring;
499 local s=d("yellow"); 721 local n=a("1b3967");
500 local h=d("red"); 722 local r=a("13b5ea");
501 local u=d("red"); 723 local o=a("439639");
502 local t=d("magenta"); 724 local i=a("a0ce67");
503 local h=" "..a(s,"%s")..a(t,"=")..a(h,"'%s'"); 725 local h=a("d9541e");
504 local s=a(t,"<")..a(u,"%s").."%s"..a(t,">"); 726 local a=a("e96d1f");
505 local d=s.."%s"..a(t,"</")..a(u,"%s")..a(t,">"); 727 local h=(
728 t(i,"%1")..
729 t(o,"%2")..
730 t(h,"%3")..
731 t(a,"%4")..
732 t(h,"%5")
733 );
734 local d=(
735 t(o,"%1")..
736 t(i,"%2")..
737 t(o,"%3")
738 );
739 function s(e)
740 return(e:gsub("(<[?/]?)([^ >/?]*)(.-)([?/]?>)([^<]*)",function(o,e,a,i,s)
741 return t(n,o)..t(r,e)..
742 a:gsub("([^=]+)(=)([\"'])(.-)([\"'])",h)..
743 t(n,i)..
744 s:gsub("(&#?)(%w+)(;)",d);
745 end,100));
746 end
506 function e.pretty_print(e) 747 function e.pretty_print(e)
507 local t=""; 748 return s(f(e));
508 for a,e in i(e)do
509 if o(e)=="string"then
510 t=t..l(e);
511 else
512 t=t..e:pretty_print();
513 end
514 end
515 local a="";
516 if e.attr then
517 for e,t in n(e.attr)do if o(e)=="string"then a=a..r(h,e,w(t));end end
518 end
519 return r(d,e.name,a,t,e.name);
520 end 749 end
521 function e.pretty_top_tag(e) 750 function e.pretty_top_tag(e)
522 local t=""; 751 return s(e:top_tag());
523 if e.attr then
524 for e,a in n(e.attr)do if o(e)=="string"then t=t..r(h,e,w(a));end end
525 end
526 return r(s,e.name,t);
527 end 752 end
528 else 753 else
529 e.pretty_print=e.__tostring; 754 e.pretty_print=e.__tostring;
530 e.pretty_top_tag=e.top_tag; 755 e.pretty_top_tag=e.top_tag;
531 end 756 end
532 return _M; 757 function e.indent(i,o,a)
758 if#i==0 or(#i==1 and t(i[1])=="string")then
759 return i;
760 end
761 a=a or"\t";
762 o=o or 1;
763 local e=u(i,true);
764 for i in i:children()do
765 if t(i)=="string"then
766 if i:find("%S")then
767 e:text("\n"..a:rep(o));
768 e:text(i);
769 end
770 elseif n(i)then
771 e:text("\n"..a:rep(o));
772 e:add_direct_child(i:indent(o+1,a));
773 end
774 end
775 e:text("\n"..a:rep((o-1)));
776 return e;
777 end
778 return{
779 stanza_mt=e;
780 stanza=i;
781 is_stanza=n;
782 preserialize=l;
783 deserialize=m;
784 clone=u;
785 message=y;
786 iq=v;
787 reply=r;
788 error_reply=w;
789 presence=d;
790 xml_escape=c;
791 pretty_print=s;
792 };
533 end) 793 end)
534 package.preload['util.timer']=(function(...) 794 package.preload['util.timer']=(function(...)
535 local _ENV=_ENV; 795 local _ENV=_ENV;
536 local function o(t,...) 796 local function e(t,...)
537 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 797 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
538 package.loaded[t]=e; 798 package.loaded[t]=e;
539 for t=1,select("#",...)do 799 for t=1,select("#",...)do
540 (select(t,...))(e); 800 (select(t,...))(e);
541 end 801 end
542 _ENV=e; 802 _ENV=e;
543 _M=e; 803 _M=e;
544 return e; 804 return e;
545 end 805 end
546 local a=require"net.server"; 806 local a=require"util.indexedbheap";
547 local h=math.min 807 local t=require"util.logger".init("timer");
548 local u=math.huge 808 local e=require"net.server";
549 local i=require"socket".gettime; 809 local d=require"util.time".now
550 local d=table.insert; 810 local l=type;
551 local l=pairs; 811 local h=debug.traceback;
552 local r=type; 812 local i=tostring;
553 local s={}; 813 local c=require"util.xpcall".xpcall;
554 local e={}; 814 local f=math.max;
555 o"timer" 815 local m=pairs;
816 if e.timer then
817 return e.timer;
818 end
819 local _ENV=nil;
820 local r=e.add_task;
821 local o;
822 local s=0;
823 local a=a.create();
824 local n={};
825 local e=nil;
826 local function u(e)t("error","Traceback[timer]: %s",h(i(e),2));end
827 local function h(i)
556 local t; 828 local t;
557 if not a.event then 829 local o;
558 function t(o,n) 830 while true do
559 local i=i(); 831 t=a:peek();
560 o=o+i; 832 if t==nil or t>i then break;end
561 if o>=i then 833 local t,a,e=a:pop();
562 d(e,{o,n}); 834 local s=n[e];
563 else 835 n[e]=nil;
564 local e=n(i); 836 local h,t=c(a,u,i,e,s);
565 if e and r(e)=="number"then 837 if h and l(t)=="number"then
566 return t(e,n); 838 if o then
567 end 839 o[e]={a,t+i};
568 end 840 else
569 end 841 o={[e]={a,t+i}};
570 a._addtimer(function() 842 end
571 local a=i(); 843 n[e]=s;
572 if#e>0 then 844 end
573 for a,t in l(e)do 845 end
574 d(s,t); 846 if o then
575 end 847 for o,e in m(o)do
576 e={}; 848 a:insert(e[1],e[2],o);
577 end 849 end
578 local e=u; 850 t=a:peek();
579 for n,o in l(s)do 851 end
580 local o,i=o[1],o[2]; 852 if t~=nil and s>1 and t==e then
581 if o<=a then 853 t=nil;
582 s[n]=nil; 854 else
583 local a=i(a); 855 e=t;
584 if r(a)=="number"then 856 end
585 t(a,i); 857 if t then
586 e=h(e,a); 858 return t-i;
587 end 859 end
588 else 860 s=s-1;
589 e=h(e,o-a); 861 end
590 end 862 local function u(t,u,l)
591 end 863 local i=d();
592 return e; 864 local t=i+t;
593 end); 865 local a=a:insert(u,t);
594 else 866 n[a]=l;
595 local e=a.event; 867 if e==nil or t<e then
596 local n=a.event_base; 868 e=t;
597 local a=(e.core and e.core.LEAVE)or-1; 869 if o then
598 function t(o,e) 870 o:close();
599 local t; 871 o=nil;
600 t=n:addevent(nil,0,function() 872 else
601 local e=e(i()); 873 s=s+1;
602 if e then 874 end
603 return 0,e; 875 o=r(e-i,h);
604 elseif t then 876 end
605 return a; 877 return a;
606 end 878 end
607 end 879 local function l(t)
608 ,o); 880 n[t]=nil;
609 end 881 local s,i,n=a:remove(t);
610 end 882 local t=a:peek();
611 add_task=t; 883 if t~=e and o then
612 return _M; 884 e=t;
885 o:close();
886 if e~=nil then
887 o=r(f(e-d(),0),h);
888 end
889 end
890 return s,i,n;
891 end
892 local function s(o,n)
893 local i=d();
894 local t=i+n;
895 a:reprioritize(o,n);
896 if e==nil or t<e then
897 e=t;
898 r(e-i,h);
899 end
900 return o;
901 end
902 return{
903 add_task=u;
904 stop=l;
905 reschedule=s;
906 };
613 end) 907 end)
614 package.preload['util.termcolours']=(function(...) 908 package.preload['util.termcolours']=(function(...)
615 local _ENV=_ENV; 909 local _ENV=_ENV;
616 local function a(t,...) 910 local function e(t,...)
617 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 911 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
618 package.loaded[t]=e; 912 package.loaded[t]=e;
619 for t=1,select("#",...)do 913 for t=1,select("#",...)do
620 (select(t,...))(e); 914 (select(t,...))(e);
621 end 915 end
622 _ENV=e; 916 _ENV=e;
623 _M=e; 917 _M=e;
624 return e; 918 return e;
625 end 919 end
626 local n,i=table.concat,table.insert; 920 local s,h=table.concat,table.insert;
627 local t,s=string.char,string.format; 921 local a,r=string.char,string.format;
628 local h=tonumber; 922 local o=tonumber;
629 local c=ipairs; 923 local m=ipairs;
630 local l=io.write; 924 local u=io.write;
631 local e; 925 local i=math.floor;
926 local l=type;
927 local d=setmetatable;
928 local c=pairs;
929 local t;
632 if os.getenv("WINDIR")then 930 if os.getenv("WINDIR")then
633 e=require"util.windows"; 931 t=require"util.windows";
634 end 932 end
635 local o=e and e.get_consolecolor and e.get_consolecolor(); 933 local n=t and t.get_consolecolor and t.get_consolecolor();
636 a"termcolours" 934 local _ENV=nil;
637 local u={ 935 local e={
638 reset=0;bright=1,dim=2,underscore=4,blink=5,reverse=7,hidden=8; 936 reset=0;bright=1,dim=2,underscore=4,blink=5,reverse=7,hidden=8;
639 black=30;red=31;green=32;yellow=33;blue=34;magenta=35;cyan=36;white=37; 937 black=30;red=31;green=32;yellow=33;blue=34;magenta=35;cyan=36;white=37;
640 ["black background"]=40;["red background"]=41;["green background"]=42;["yellow background"]=43;["blue background"]=44;["magenta background"]=45;["cyan background"]=46;["white background"]=47; 938 ["black background"]=40;["red background"]=41;["green background"]=42;["yellow background"]=43;
939 ["blue background"]=44;["magenta background"]=45;["cyan background"]=46;["white background"]=47;
641 bold=1,dark=2,underline=4,underlined=4,normal=0; 940 bold=1,dark=2,underline=4,underlined=4,normal=0;
642 } 941 }
643 local d={ 942 local w={
644 ["0"]=o, 943 ["0"]=n,
645 ["1"]=7+8, 944 ["1"]=7+8,
646 ["1;33"]=2+4+8, 945 ["1;33"]=2+4+8,
647 ["1;31"]=4+8 946 ["1;31"]=4+8
648 } 947 }
649 local r={ 948 local y={
650 [1]="font-weight: bold",[2]="opacity: 0.5",[4]="text-decoration: underline",[8]="visibility: hidden", 949 [1]="font-weight: bold",[2]="opacity: 0.5",[4]="text-decoration: underline",[8]="visibility: hidden",
651 [30]="color:black",[31]="color:red",[32]="color:green",[33]="color:#FFD700", 950 [30]="color:black",[31]="color:red",[32]="color:green",[33]="color:#FFD700",
652 [34]="color:blue",[35]="color: magenta",[36]="color:cyan",[37]="color: white", 951 [34]="color:blue",[35]="color: magenta",[36]="color:cyan",[37]="color: white",
653 [40]="background-color:black",[41]="background-color:red",[42]="background-color:green", 952 [40]="background-color:black",[41]="background-color:red",[42]="background-color:green",
654 [43]="background-color:yellow",[44]="background-color:blue",[45]="background-color: magenta", 953 [43]="background-color:yellow",[44]="background-color:blue",[45]="background-color: magenta",
655 [46]="background-color:cyan",[47]="background-color: white"; 954 [46]="background-color:cyan",[47]="background-color: white";
656 }; 955 };
657 local a=t(27).."[%sm%s"..t(27).."[0m"; 956 local a=a(27).."[%sm%s"..a(27).."[0m";
658 function getstring(e,t) 957 local function p(e,t)
659 if e then 958 if e then
660 return s(a,e,t); 959 return r(a,e,t);
661 else 960 else
662 return t; 961 return t;
663 end 962 end
664 end 963 end
665 function getstyle(...) 964 local function v(e)
666 local e,t={...},{}; 965 return i(e*3/32)+232;
667 for a,e in c(e)do 966 end
668 e=u[e]; 967 local function f(t,e,a)
669 if e then 968 if t==e and e==a then
670 i(t,e); 969 return v(t);
671 end 970 end
672 end 971 t=i(t*3/128);
673 return n(t,";"); 972 e=i(e*3/128);
973 a=i(a*3/128);
974 return 16+(t*36)+(e*6)+(a);
975 end
976 local function i(e)
977 local a=o(e:sub(1,2),16);
978 local t=o(e:sub(3,4),16);
979 local e=o(e:sub(5,6),16);
980 return a,t,e;
981 end
982 d(e,{__index=function(t,e)
983 if l(e)=="string"and e:find("%x%x%x%x%x%x")==1 then
984 local t=e:sub(7)==" background"and"48;5;"or"38;5;";
985 return r("%s%d",t,f(i(e)));
986 end
987 end});
988 local a={
989 red="ff0000";fuchsia="ff00ff";green="008000";white="ffffff";
990 lime="00ff00";yellow="ffff00";purple="800080";blue="0000ff";
991 aqua="00ffff";olive="808000";black="000000";navy="000080";
992 teal="008080";silver="c0c0c0";maroon="800000";gray="808080";
993 }
994 for t,a in c(a)do
995 e[t]=e[t]or e[a];
996 t,a=t.." background",a.." background"
997 e[t]=e[t]or e[a];
998 end
999 local function r(...)
1000 local t,a={...},{};
1001 for o,t in m(t)do
1002 t=e[t];
1003 if t then
1004 h(a,t);
1005 end
1006 end
1007 return s(a,";");
674 end 1008 end
675 local a="0"; 1009 local a="0";
676 function setstyle(e) 1010 local function i(e)
677 e=e or"0"; 1011 e=e or"0";
678 if e~=a then 1012 if e~=a then
679 l("\27["..e.."m"); 1013 u("\27["..e.."m");
680 a=e; 1014 a=e;
681 end 1015 end
682 end 1016 end
683 if e then 1017 if t then
684 function setstyle(t) 1018 function i(e)
685 t=t or"0"; 1019 e=e or"0";
686 if t~=a then 1020 if e~=a then
687 e.set_consolecolor(d[t]or o); 1021 t.set_consolecolor(w[e]or n);
688 a=t; 1022 a=e;
689 end 1023 end
690 end 1024 end
691 if not o then 1025 if not n then
692 function setstyle(e)end 1026 function i()end
693 end 1027 end
694 end 1028 end
695 local function a(e) 1029 local function a(t)
696 if e=="0"then return"</span>";end 1030 if t=="0"then return"</span>";end
697 local t={}; 1031 local e={};
698 for e in e:gmatch("[^;]+")do 1032 for t in t:gmatch("[^;]+")do
699 i(t,r[h(e)]); 1033 h(e,y[o(t)]);
700 end 1034 end
701 return"</span><span style='"..n(t,";").."'>"; 1035 return"</span><span style='"..s(e,";").."'>";
702 end 1036 end
703 function tohtml(e) 1037 local function t(e)
704 return e:gsub("\027%[(.-)m",a); 1038 return e:gsub("\027%[(.-)m",a);
705 end 1039 end
706 return _M; 1040 return{
1041 getstring=p;
1042 getstyle=r;
1043 setstyle=i;
1044 tohtml=t;
1045 };
707 end) 1046 end)
708 package.preload['util.uuid']=(function(...) 1047 package.preload['util.uuid']=(function(...)
709 local _ENV=_ENV; 1048 local _ENV=_ENV;
710 local function o(t,...) 1049 local function e(t,...)
711 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 1050 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
712 package.loaded[t]=e; 1051 package.loaded[t]=e;
713 for t=1,select("#",...)do 1052 for t=1,select("#",...)do
714 (select(t,...))(e); 1053 (select(t,...))(e);
715 end 1054 end
716 _ENV=e; 1055 _ENV=e;
717 _M=e; 1056 _M=e;
718 return e; 1057 return e;
719 end 1058 end
720 local n=tostring; 1059 local a=require"util.random";
721 local e=os.time; 1060 local t=a.bytes;
722 local a=os.clock; 1061 local i=require"util.hex".encode;
723 local i=require"util.hashes".sha1; 1062 local o=math.ceil;
724 o"uuid" 1063 local function e(e)
725 local t=0; 1064 return i(t(o(e/2))):sub(1,e);
1065 end
726 local function o() 1066 local function o()
727 local e=e();
728 if t>=e then e=t+1;end
729 t=e;
730 return e;
731 end
732 local function t(e)
733 return i(e..a()..n({}),true);
734 end
735 local e=t(o());
736 local function a(a)
737 e=t(e..a);
738 end
739 local function t(t)
740 if#e<t then a(o());end
741 local a=e:sub(0,t);
742 e=e:sub(t+1);
743 return a;
744 end
745 local function e()
746 return("%x"):format(t(1):byte()%4+8); 1067 return("%x"):format(t(1):byte()%4+8);
747 end 1068 end
748 function generate() 1069 local function t()
749 return t(8).."-"..t(4).."-4"..t(3).."-"..(e())..t(3).."-"..t(12); 1070 return e(8).."-"..e(4).."-4"..e(3).."-"..(o())..e(3).."-"..e(12);
750 end 1071 end
751 seed=a; 1072 return{
752 return _M; 1073 get_nibbles=e;
1074 generate=t;
1075 seed=a.seed;
1076 };
753 end) 1077 end)
754 package.preload['net.dns']=(function(...) 1078 package.preload['util.time']=(function(...)
755 local _ENV=_ENV; 1079 local _ENV=_ENV;
756 local function c(t,...) 1080 local function e(t,...)
757 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 1081 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
758 package.loaded[t]=e; 1082 package.loaded[t]=e;
759 for t=1,select("#",...)do 1083 for t=1,select("#",...)do
760 (select(t,...))(e); 1084 (select(t,...))(e);
761 end 1085 end
762 _ENV=e; 1086 _ENV=e;
763 _M=e; 1087 _M=e;
764 return e; 1088 return e;
765 end 1089 end
766 local s=require"socket"; 1090 local o=require"socket".gettime;
767 local k=require"util.timer"; 1091 local e;
768 local e,b=pcall(require,"util.windows"); 1092 local t,a=pcall(require,"system");
769 local _=(e and b)or os.getenv("WINDIR"); 1093 if t then
770 local u,E,v,a,i= 1094 e=a.monotime;
771 coroutine,io,math,string,table; 1095 end
772 local m,h,o,f,r,p,x,q,t,e,z= 1096 return{
773 ipairs,next,pairs,print,setmetatable,tostring,assert,error,unpack,select,type; 1097 now=o;
774 local e={ 1098 monotonic=e;
775 get=function(t,...) 1099 }
776 local a=e('#',...);
777 for a=1,a do
778 t=t[e(a,...)];
779 if t==nil then break;end
780 end
781 return t;
782 end;
783 set=function(a,...)
784 local n=e('#',...);
785 local s,o=e(n-1,...);
786 local t,i;
787 for n=1,n-2 do
788 local n=e(n,...)
789 local e=a[n]
790 if o==nil then
791 if e==nil then
792 return;
793 elseif h(e,h(e))then
794 t=nil;i=nil;
795 elseif t==nil then
796 t=a;i=n;
797 end
798 elseif e==nil then
799 e={};
800 a[n]=e;
801 end
802 a=e
803 end
804 if o==nil and t then
805 t[i]=nil;
806 else
807 a[s]=o;
808 return o;
809 end
810 end;
811 };
812 local d,l=e.get,e.set;
813 local j=15;
814 c('dns')
815 local t=_M;
816 local n=i.insert
817 local function c(e)
818 return(e-(e%256))/256;
819 end
820 local function y(e)
821 local t={};
822 for o,e in o(e)do
823 t[o]=e;
824 t[e]=e;
825 t[a.lower(e)]=e;
826 end
827 return t;
828 end
829 local function w(i)
830 local e={};
831 for o,i in o(i)do
832 local t=a.char(c(o),o%256);
833 e[o]=t;
834 e[i]=t;
835 e[a.lower(i)]=t;
836 end
837 return e;
838 end
839 t.types={
840 'A','NS','MD','MF','CNAME','SOA','MB','MG','MR','NULL','WKS',
841 'PTR','HINFO','MINFO','MX','TXT',
842 [28]='AAAA',[29]='LOC',[33]='SRV',
843 [252]='AXFR',[253]='MAILB',[254]='MAILA',[255]='*'};
844 t.classes={'IN','CS','CH','HS',[255]='*'};
845 t.type=y(t.types);
846 t.class=y(t.classes);
847 t.typecode=w(t.types);
848 t.classcode=w(t.classes);
849 local function g(e,i,o)
850 if a.byte(e,-1)~=46 then e=e..'.';end
851 e=a.lower(e);
852 return e,t.type[i or'A'],t.class[o or'IN'];
853 end
854 local function y(t,a,n)
855 a=a or s.gettime();
856 for o,e in m(t)do
857 if e.tod then
858 e.ttl=v.floor(e.tod-a);
859 if e.ttl<=0 then
860 t[e[e.type:lower()]]=nil;
861 i.remove(t,o);
862 return y(t,a,n);
863 end
864 elseif n=='soft'then
865 x(e.ttl==0);
866 t[e[e.type:lower()]]=nil;
867 i.remove(t,o);
868 end
869 end
870 end
871 local e={};
872 e.__index=e;
873 e.timeout=j;
874 local function j(e)
875 local e=e.type and e[e.type:lower()];
876 if z(e)~="string"then
877 return"<UNKNOWN RDATA TYPE>";
878 end
879 return e;
880 end
881 local w={
882 LOC=e.LOC_tostring;
883 MX=function(e)
884 return a.format('%2i %s',e.pref,e.mx);
885 end;
886 SRV=function(e)
887 local e=e.srv;
888 return a.format('%5d %5d %5d %s',e.priority,e.weight,e.port,e.target);
889 end;
890 };
891 local x={};
892 function x.__tostring(e)
893 local t=(w[e.type]or j)(e);
894 return a.format('%2s %-5s %6i %-28s %s',e.class,e.type,e.ttl,e.name,t);
895 end
896 local j={};
897 function j.__tostring(t)
898 local e={};
899 for a,t in m(t)do
900 n(e,p(t)..'\n');
901 end
902 return i.concat(e);
903 end
904 local w={};
905 function w.__tostring(e)
906 local a=s.gettime();
907 local t={};
908 for i,e in o(e)do
909 for i,e in o(e)do
910 for o,e in o(e)do
911 y(e,a);
912 n(t,p(e));
913 end
914 end
915 end
916 return i.concat(t);
917 end
918 function e:new()
919 local t={active={},cache={},unsorted={}};
920 r(t,e);
921 r(t.cache,w);
922 r(t.unsorted,{__mode='kv'});
923 return t;
924 end
925 function t.random(...)
926 v.randomseed(v.floor(1e4*s.gettime())%2147483648);
927 t.random=v.random;
928 return t.random(...);
929 end
930 local function v(e)
931 e=e or{};
932 e.id=e.id or t.random(0,65535);
933 e.rd=e.rd or 1;
934 e.tc=e.tc or 0;
935 e.aa=e.aa or 0;
936 e.opcode=e.opcode or 0;
937 e.qr=e.qr or 0;
938 e.rcode=e.rcode or 0;
939 e.z=e.z or 0;
940 e.ra=e.ra or 0;
941 e.qdcount=e.qdcount or 1;
942 e.ancount=e.ancount or 0;
943 e.nscount=e.nscount or 0;
944 e.arcount=e.arcount or 0;
945 local t=a.char(
946 c(e.id),e.id%256,
947 e.rd+2*e.tc+4*e.aa+8*e.opcode+128*e.qr,
948 e.rcode+16*e.z+128*e.ra,
949 c(e.qdcount),e.qdcount%256,
950 c(e.ancount),e.ancount%256,
951 c(e.nscount),e.nscount%256,
952 c(e.arcount),e.arcount%256
953 );
954 return t,e.id;
955 end
956 local function c(t)
957 local e={};
958 for t in a.gmatch(t,'[^.]+')do
959 n(e,a.char(a.len(t)));
960 n(e,t);
961 end
962 n(e,a.char(0));
963 return i.concat(e);
964 end
965 local function z(o,a,e)
966 o=c(o);
967 a=t.typecode[a or'a'];
968 e=t.classcode[e or'in'];
969 return o..a..e;
970 end
971 function e:byte(e)
972 e=e or 1;
973 local t=self.offset;
974 local o=t+e-1;
975 if o>#self.packet then
976 q(a.format('out of bounds: %i>%i',o,#self.packet));
977 end
978 self.offset=t+e;
979 return a.byte(self.packet,t,o);
980 end
981 function e:word()
982 local t,e=self:byte(2);
983 return 256*t+e;
984 end
985 function e:dword()
986 local o,t,a,e=self:byte(4);
987 return 16777216*o+65536*t+256*a+e;
988 end
989 function e:sub(e)
990 e=e or 1;
991 local t=a.sub(self.packet,self.offset,self.offset+e-1);
992 self.offset=self.offset+e;
993 return t;
994 end
995 function e:header(t)
996 local e=self:word();
997 if not self.active[e]and not t then return nil;end
998 local e={id=e};
999 local t,a=self:byte(2);
1000 e.rd=t%2;
1001 e.tc=t/2%2;
1002 e.aa=t/4%2;
1003 e.opcode=t/8%16;
1004 e.qr=t/128;
1005 e.rcode=a%16;
1006 e.z=a/16%8;
1007 e.ra=a/128;
1008 e.qdcount=self:word();
1009 e.ancount=self:word();
1010 e.nscount=self:word();
1011 e.arcount=self:word();
1012 for a,t in o(e)do e[a]=t-t%1;end
1013 return e;
1014 end
1015 function e:name()
1016 local a,t=nil,0;
1017 local e=self:byte();
1018 local o={};
1019 if e==0 then return"."end
1020 while e>0 do
1021 if e>=192 then
1022 t=t+1;
1023 if t>=20 then q('dns error: 20 pointers');end;
1024 local e=((e-192)*256)+self:byte();
1025 a=a or self.offset;
1026 self.offset=e+1;
1027 else
1028 n(o,self:sub(e)..'.');
1029 end
1030 e=self:byte();
1031 end
1032 self.offset=a or self.offset;
1033 return i.concat(o);
1034 end
1035 function e:question()
1036 local e={};
1037 e.name=self:name();
1038 e.type=t.type[self:word()];
1039 e.class=t.class[self:word()];
1040 return e;
1041 end
1042 function e:A(e)
1043 local o,t,n,i=self:byte(4);
1044 e.a=a.format('%i.%i.%i.%i',o,t,n,i);
1045 end
1046 function e:AAAA(a)
1047 local e={};
1048 for t=1,a.rdlength,2 do
1049 local t,a=self:byte(2);
1050 i.insert(e,("%02x%02x"):format(t,a));
1051 end
1052 e=i.concat(e,":"):gsub("%f[%x]0+(%x)","%1");
1053 local t={};
1054 for e in e:gmatch(":[0:]+:")do
1055 i.insert(t,e)
1056 end
1057 if#t==0 then
1058 a.aaaa=e;
1059 return
1060 elseif#t>1 then
1061 i.sort(t,function(t,e)return#t>#e end);
1062 end
1063 a.aaaa=e:gsub(t[1],"::",1):gsub("^0::","::"):gsub("::0$","::");
1064 end
1065 function e:CNAME(e)
1066 e.cname=self:name();
1067 end
1068 function e:MX(e)
1069 e.pref=self:word();
1070 e.mx=self:name();
1071 end
1072 function e:LOC_nibble_power()
1073 local e=self:byte();
1074 return((e-(e%16))/16)*(10^(e%16));
1075 end
1076 function e:LOC(e)
1077 e.version=self:byte();
1078 if e.version==0 then
1079 e.loc=e.loc or{};
1080 e.loc.size=self:LOC_nibble_power();
1081 e.loc.horiz_pre=self:LOC_nibble_power();
1082 e.loc.vert_pre=self:LOC_nibble_power();
1083 e.loc.latitude=self:dword();
1084 e.loc.longitude=self:dword();
1085 e.loc.altitude=self:dword();
1086 end
1087 end
1088 local function c(e,i,t)
1089 e=e-2147483648;
1090 if e<0 then i=t;e=-e;end
1091 local n,o,t;
1092 t=e%6e4;
1093 e=(e-t)/6e4;
1094 o=e%60;
1095 n=(e-o)/60;
1096 return a.format('%3d %2d %2.3f %s',n,o,t/1e3,i);
1097 end
1098 function e.LOC_tostring(e)
1099 local t={};
1100 n(t,a.format(
1101 '%s %s %.2fm %.2fm %.2fm %.2fm',
1102 c(e.loc.latitude,'N','S'),
1103 c(e.loc.longitude,'E','W'),
1104 (e.loc.altitude-1e7)/100,
1105 e.loc.size/100,
1106 e.loc.horiz_pre/100,
1107 e.loc.vert_pre/100
1108 ));
1109 return i.concat(t);
1110 end
1111 function e:NS(e)
1112 e.ns=self:name();
1113 end
1114 function e:SOA(e)
1115 end
1116 function e:SRV(e)
1117 e.srv={};
1118 e.srv.priority=self:word();
1119 e.srv.weight=self:word();
1120 e.srv.port=self:word();
1121 e.srv.target=self:name();
1122 end
1123 function e:PTR(e)
1124 e.ptr=self:name();
1125 end
1126 function e:TXT(e)
1127 e.txt=self:sub(self:byte());
1128 end
1129 function e:rr()
1130 local e={};
1131 r(e,x);
1132 e.name=self:name(self);
1133 e.type=t.type[self:word()]or e.type;
1134 e.class=t.class[self:word()]or e.class;
1135 e.ttl=65536*self:word()+self:word();
1136 e.rdlength=self:word();
1137 if e.ttl<=0 then
1138 e.tod=self.time+30;
1139 else
1140 e.tod=self.time+e.ttl;
1141 end
1142 local a=self.offset;
1143 local t=self[t.type[e.type]];
1144 if t then t(self,e);end
1145 self.offset=a;
1146 e.rdata=self:sub(e.rdlength);
1147 return e;
1148 end
1149 function e:rrs(t)
1150 local e={};
1151 for t=1,t do n(e,self:rr());end
1152 return e;
1153 end
1154 function e:decode(t,o)
1155 self.packet,self.offset=t,1;
1156 local t=self:header(o);
1157 if not t then return nil;end
1158 local t={header=t};
1159 t.question={};
1160 local i=self.offset;
1161 for e=1,t.header.qdcount do
1162 n(t.question,self:question());
1163 end
1164 t.question.raw=a.sub(self.packet,i,self.offset-1);
1165 if not o then
1166 if not self.active[t.header.id]or not self.active[t.header.id][t.question.raw]then
1167 self.active[t.header.id]=nil;
1168 return nil;
1169 end
1170 end
1171 t.answer=self:rrs(t.header.ancount);
1172 t.authority=self:rrs(t.header.nscount);
1173 t.additional=self:rrs(t.header.arcount);
1174 return t;
1175 end
1176 e.delays={1,3};
1177 function e:addnameserver(e)
1178 self.server=self.server or{};
1179 n(self.server,e);
1180 end
1181 function e:setnameserver(e)
1182 self.server={};
1183 self:addnameserver(e);
1184 end
1185 function e:adddefaultnameservers()
1186 if _ then
1187 if b and b.get_nameservers then
1188 for t,e in m(b.get_nameservers())do
1189 self:addnameserver(e);
1190 end
1191 end
1192 if not self.server or#self.server==0 then
1193 self:addnameserver("208.67.222.222");
1194 self:addnameserver("208.67.220.220");
1195 end
1196 else
1197 local e=E.open("/etc/resolv.conf");
1198 if e then
1199 for e in e:lines()do
1200 e=e:gsub("#.*$","")
1201 :match('^%s*nameserver%s+(.*)%s*$');
1202 if e then
1203 e:gsub("%f[%d.](%d+%.%d+%.%d+%.%d+)%f[^%d.]",function(e)
1204 self:addnameserver(e)
1205 end);
1206 end
1207 end
1208 end
1209 if not self.server or#self.server==0 then
1210 self:addnameserver("127.0.0.1");
1211 end
1212 end
1213 end
1214 function e:getsocket(o)
1215 self.socket=self.socket or{};
1216 self.socketset=self.socketset or{};
1217 local e=self.socket[o];
1218 if e then return e;end
1219 local a,t;
1220 e,t=s.udp();
1221 if e and self.socket_wrapper then e,t=self.socket_wrapper(e,self);end
1222 if not e then
1223 return nil,t;
1224 end
1225 e:settimeout(0);
1226 self.socket[o]=e;
1227 self.socketset[e]=o;
1228 a,t=e:setsockname('*',0);
1229 if not a then return self:servfail(e,t);end
1230 a,t=e:setpeername(self.server[o],53);
1231 if not a then return self:servfail(e,t);end
1232 return e;
1233 end
1234 function e:voidsocket(e)
1235 if self.socket[e]then
1236 self.socketset[self.socket[e]]=nil;
1237 self.socket[e]=nil;
1238 elseif self.socketset[e]then
1239 self.socket[self.socketset[e]]=nil;
1240 self.socketset[e]=nil;
1241 end
1242 e:close();
1243 end
1244 function e:socket_wrapper_set(e)
1245 self.socket_wrapper=e;
1246 end
1247 function e:closeall()
1248 for t,e in m(self.socket)do
1249 self.socket[t]=nil;
1250 self.socketset[e]=nil;
1251 e:close();
1252 end
1253 end
1254 function e:remember(e,t)
1255 local a,o,i=g(e.name,e.type,e.class);
1256 if t~='*'then
1257 t=o;
1258 local t=d(self.cache,i,'*',a);
1259 if t then n(t,e);end
1260 end
1261 self.cache=self.cache or r({},w);
1262 local a=d(self.cache,i,t,a)or
1263 l(self.cache,i,t,a,r({},j));
1264 if not a[e[o:lower()]]then
1265 a[e[o:lower()]]=true;
1266 n(a,e);
1267 end
1268 if t=='MX'then self.unsorted[a]=true;end
1269 end
1270 local function c(e,t)
1271 return(e.pref==t.pref)and(e.mx<t.mx)or(e.pref<t.pref);
1272 end
1273 function e:peek(o,a,t,n)
1274 o,a,t=g(o,a,t);
1275 local e=d(self.cache,t,a,o);
1276 if not e then
1277 if n then if n<=0 then return end else n=3 end
1278 e=d(self.cache,t,"CNAME",o);
1279 if not(e and e[1])then return end
1280 return self:peek(e[1].cname,a,t,n-1);
1281 end
1282 if y(e,s.gettime())and a=='*'or not h(e)then
1283 l(self.cache,t,a,o,nil);
1284 return nil;
1285 end
1286 if self.unsorted[e]then i.sort(e,c);self.unsorted[e]=nil;end
1287 return e;
1288 end
1289 function e:purge(e)
1290 if e=='soft'then
1291 self.time=s.gettime();
1292 for t,e in o(self.cache or{})do
1293 for t,e in o(e)do
1294 for t,e in o(e)do
1295 y(e,self.time,'soft')
1296 end
1297 end
1298 end
1299 else self.cache=r({},w);end
1300 end
1301 function e:query(e,a,t)
1302 e,a,t=g(e,a,t)
1303 local n=u.running();
1304 local o=d(self.wanted,t,a,e);
1305 if n and o then
1306 l(self.wanted,t,a,e,n,true);
1307 return true;
1308 end
1309 if not self.server then self:adddefaultnameservers();end
1310 local h=z(e,a,t);
1311 local o=self:peek(e,a,t);
1312 if o then return o;end
1313 local o,i=v();
1314 local o={
1315 packet=o..h,
1316 server=self.best_server,
1317 delay=1,
1318 retry=s.gettime()+self.delays[1]
1319 };
1320 self.active[i]=self.active[i]or{};
1321 self.active[i][h]=o;
1322 if n then
1323 l(self.wanted,t,a,e,n,true);
1324 end
1325 local i,h=self:getsocket(o.server)
1326 if not i then
1327 return nil,h;
1328 end
1329 i:send(o.packet)
1330 if k and self.timeout then
1331 local r=#self.server;
1332 local s=1;
1333 k.add_task(self.timeout,function()
1334 if d(self.wanted,t,a,e,n)then
1335 if s<r then
1336 s=s+1;
1337 self:servfail(i);
1338 o.server=self.best_server;
1339 i,h=self:getsocket(o.server);
1340 if i then
1341 i:send(o.packet);
1342 return self.timeout;
1343 end
1344 end
1345 self:cancel(t,a,e);
1346 end
1347 end) 1100 end)
1348 end 1101 package.preload['util.envload']=(function(...)
1349 return true;
1350 end
1351 function e:servfail(t,i)
1352 local n=self.socketset[t]
1353 t=self:voidsocket(t);
1354 self.time=s.gettime();
1355 for s,a in o(self.active)do
1356 for o,e in o(a)do
1357 if e.server==n then
1358 e.server=e.server+1
1359 if e.server>#self.server then
1360 e.server=1;
1361 end
1362 e.retries=(e.retries or 0)+1;
1363 if e.retries>=#self.server then
1364 a[o]=nil;
1365 else
1366 t,i=self:getsocket(e.server);
1367 if t then t:send(e.packet);end
1368 end
1369 end
1370 end
1371 if h(a)==nil then
1372 self.active[s]=nil;
1373 end
1374 end
1375 if n==self.best_server then
1376 self.best_server=self.best_server+1;
1377 if self.best_server>#self.server then
1378 self.best_server=1;
1379 end
1380 end
1381 return t,i;
1382 end
1383 function e:settimeout(e)
1384 self.timeout=e;
1385 end
1386 function e:receive(t)
1387 self.time=s.gettime();
1388 t=t or self.socket;
1389 local e;
1390 for a,t in o(t)do
1391 if self.socketset[t]then
1392 local t=t:receive();
1393 if t then
1394 e=self:decode(t);
1395 if e and self.active[e.header.id]
1396 and self.active[e.header.id][e.question.raw]then
1397 for a,t in o(e.answer)do
1398 self:remember(t,e.question[1].type)
1399 end
1400 local t=self.active[e.header.id];
1401 t[e.question.raw]=nil;
1402 if not h(t)then self.active[e.header.id]=nil;end
1403 if not h(self.active)then self:closeall();end
1404 local e=e.question[1];
1405 local t=d(self.wanted,e.class,e.type,e.name);
1406 if t then
1407 for e in o(t)do
1408 if u.status(e)=="suspended"then u.resume(e);end
1409 end
1410 l(self.wanted,e.class,e.type,e.name,nil);
1411 end
1412 end
1413 end
1414 end
1415 end
1416 return e;
1417 end
1418 function e:feed(a,t,e)
1419 self.time=s.gettime();
1420 local e=self:decode(t,e);
1421 if e and self.active[e.header.id]
1422 and self.active[e.header.id][e.question.raw]then
1423 for a,t in o(e.answer)do
1424 self:remember(t,e.question[1].type);
1425 end
1426 local t=self.active[e.header.id];
1427 t[e.question.raw]=nil;
1428 if not h(t)then self.active[e.header.id]=nil;end
1429 if not h(self.active)then self:closeall();end
1430 local e=e.question[1];
1431 if e then
1432 local t=d(self.wanted,e.class,e.type,e.name);
1433 if t then
1434 for e in o(t)do
1435 if u.status(e)=="suspended"then u.resume(e);end
1436 end
1437 l(self.wanted,e.class,e.type,e.name,nil);
1438 end
1439 end
1440 end
1441 return e;
1442 end
1443 function e:cancel(t,a,i)
1444 local e=d(self.wanted,t,a,i);
1445 if e then
1446 for e in o(e)do
1447 if u.status(e)=="suspended"then u.resume(e);end
1448 end
1449 l(self.wanted,t,a,i,nil);
1450 end
1451 end
1452 function e:pulse()
1453 while self:receive()do end
1454 if not h(self.active)then return nil;end
1455 self.time=s.gettime();
1456 for a,t in o(self.active)do
1457 for o,e in o(t)do
1458 if self.time>=e.retry then
1459 e.server=e.server+1;
1460 if e.server>#self.server then
1461 e.server=1;
1462 e.delay=e.delay+1;
1463 end
1464 if e.delay>#self.delays then
1465 t[o]=nil;
1466 if not h(t)then self.active[a]=nil;end
1467 if not h(self.active)then return nil;end
1468 else
1469 local t=self.socket[e.server];
1470 if t then t:send(e.packet);end
1471 e.retry=self.time+self.delays[e.delay];
1472 end
1473 end
1474 end
1475 end
1476 if h(self.active)then return true;end
1477 return nil;
1478 end
1479 function e:lookup(a,e,t)
1480 self:query(a,e,t)
1481 while self:pulse()do
1482 local e={}
1483 for t,a in m(self.socket)do
1484 e[t]=a
1485 end
1486 s.select(e,nil,4)
1487 end
1488 return self:peek(a,e,t);
1489 end
1490 function e:lookupex(o,a,t,e)
1491 return self:peek(a,t,e)or self:query(a,t,e);
1492 end
1493 function e:tohostname(e)
1494 return t.lookup(e:gsub("(%d+)%.(%d+)%.(%d+)%.(%d+)","%4.%3.%2.%1.in-addr.arpa."),"PTR");
1495 end
1496 local i={
1497 qr={[0]='query','response'},
1498 opcode={[0]='query','inverse query','server status request'},
1499 aa={[0]='non-authoritative','authoritative'},
1500 tc={[0]='complete','truncated'},
1501 rd={[0]='recursion not desired','recursion desired'},
1502 ra={[0]='recursion not available','recursion available'},
1503 z={[0]='(reserved)'},
1504 rcode={[0]='no error','format error','server failure','name error','not implemented'},
1505 type=t.type,
1506 class=t.class
1507 };
1508 local function s(t,e)
1509 return(i[e]and i[e][t[e]])or'';
1510 end
1511 function e.print(t)
1512 for o,e in o{'id','qr','opcode','aa','tc','rd','ra','z',
1513 'rcode','qdcount','ancount','nscount','arcount'}do
1514 f(a.format('%-30s','header.'..e),t.header[e],s(t.header,e));
1515 end
1516 for e,t in m(t.question)do
1517 f(a.format('question[%i].name ',e),t.name);
1518 f(a.format('question[%i].type ',e),t.type);
1519 f(a.format('question[%i].class ',e),t.class);
1520 end
1521 local r={name=1,type=1,class=1,ttl=1,rdlength=1,rdata=1};
1522 local e;
1523 for n,i in o({'answer','authority','additional'})do
1524 for h,n in o(t[i])do
1525 for o,t in o({'name','type','class','ttl','rdlength'})do
1526 e=a.format('%s[%i].%s',i,h,t);
1527 f(a.format('%-30s',e),n[t],s(n,t));
1528 end
1529 for t,o in o(n)do
1530 if not r[t]then
1531 e=a.format('%s[%i].%s',i,h,t);
1532 f(a.format('%-30s %s',p(e),p(o)));
1533 end
1534 end
1535 end
1536 end
1537 end
1538 function t.resolver()
1539 local t={active={},cache={},unsorted={},wanted={},best_server=1};
1540 r(t,e);
1541 r(t.cache,w);
1542 r(t.unsorted,{__mode='kv'});
1543 return t;
1544 end
1545 local e=t.resolver();
1546 t._resolver=e;
1547 function t.lookup(...)
1548 return e:lookup(...);
1549 end
1550 function t.tohostname(...)
1551 return e:tohostname(...);
1552 end
1553 function t.purge(...)
1554 return e:purge(...);
1555 end
1556 function t.peek(...)
1557 return e:peek(...);
1558 end
1559 function t.query(...)
1560 return e:query(...);
1561 end
1562 function t.feed(...)
1563 return e:feed(...);
1564 end
1565 function t.cancel(...)
1566 return e:cancel(...);
1567 end
1568 function t.settimeout(...)
1569 return e:settimeout(...);
1570 end
1571 function t.cache()
1572 return e.cache;
1573 end
1574 function t.socket_wrapper_set(...)
1575 return e:socket_wrapper_set(...);
1576 end
1577 return t;
1578 end)
1579 package.preload['net.adns']=(function(...)
1580 local _ENV=_ENV; 1102 local _ENV=_ENV;
1581 local function o(t,...) 1103 local function e(t,...)
1582 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 1104 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
1583 package.loaded[t]=e; 1105 package.loaded[t]=e;
1584 for t=1,select("#",...)do 1106 for t=1,select("#",...)do
1585 (select(t,...))(e); 1107 (select(t,...))(e);
1586 end 1108 end
1587 _ENV=e; 1109 _ENV=e;
1588 _M=e; 1110 _M=e;
1589 return e; 1111 return e;
1590 end 1112 end
1591 local c=require"net.server"; 1113 local t=load;
1592 local a=require"net.dns"; 1114 local i=io.open;
1593 local t=require"util.logger".init("adns"); 1115 local function s(o,a,e)
1594 local e,e=table.insert,table.remove; 1116 return t(o,a,nil,e);
1595 local n,h,l=coroutine,tostring,pcall; 1117 end
1596 local function u(a,a,t,e)return(e-t)+1;end 1118 local function h(a,o)
1597 o"adns" 1119 local e,i,n=i(a);
1598 function lookup(d,e,s,r) 1120 if not e then return e,i,n;end
1599 return n.wrap(function(o) 1121 local a,t=t(e:lines(2048),"@"..a,nil,o);
1122 e:close();
1123 return a,t;
1124 end
1125 return{envload=s,envloadfile=h};
1126 end)
1127 package.preload['util.id']=(function(...)
1128 local _ENV=_ENV;
1129 local function e(t,...)
1130 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
1131 package.loaded[t]=e;
1132 for t=1,select("#",...)do
1133 (select(t,...))(e);
1134 end
1135 _ENV=e;
1136 _M=e;
1137 return e;
1138 end
1139 local t=string.gsub;
1140 local o=require"util.random".bytes;
1141 local a=require"util.encodings".base64.encode;
1142 local i={["+"]="-",["/"]="_",["="]=""};
1143 local function e(e)
1144 return(t(a(o(e)),"[+/=]",i));
1145 end
1146 return{
1147 tiny=function()return e(3);end;
1148 short=function()return e(9);end;
1149 medium=function()return e(18);end;
1150 long=function()return e(27);end;
1151 custom=function(t)
1152 return function()return e(t);end;
1153 end;
1154 }
1155 end)
1156 package.preload['util.serialization']=(function(...)
1157 local _ENV=_ENV;
1158 local function e(t,...)
1159 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
1160 package.loaded[t]=e;
1161 for t=1,select("#",...)do
1162 (select(t,...))(e);
1163 end
1164 _ENV=e;
1165 _M=e;
1166 return e;
1167 end
1168 local _=getmetatable;
1169 local t,a=next,type;
1170 local n=string.format;
1171 local z=string.gsub;
1172 local c=string.rep;
1173 local o=string.char;
1174 local k=string.match;
1175 local q=table.concat;
1176 local g=require"util.hex".to;
1177 local p=pcall;
1178 local v=require"util.envload".envload;
1179 if not math.type then
1180 require"util.mathcompat"
1181 end
1182 local b,j=math.huge,-math.huge;
1183 local x=math.type;
1184 local function h(e)
1185 return t,e,nil;
1186 end
1187 local function s(t,e)
1188 error("Can't serialize "..a(t)..(e and": "..e or""));
1189 end
1190 local function t(e,t)
1191 return n("{__type=%q,__error=%q}",a(e),t or"fail");
1192 end
1193 local r={
1194 ['\a']=[[\a]];['\b']=[[\b]];
1195 ['\f']=[[\f]];['\n']=[[\n]];
1196 ['\r']=[[\r]];['\t']=[[\t]];
1197 ['\v']=[[\v]];['\\']=[[\\]];
1198 ['\"']=[[\"]];['\'']=[[\']];
1199 }
1200 for t=0,255 do
1201 local e=o(t);
1202 if not r[e]then
1203 r[e]=n("\\%03d",t);
1204 end
1205 end
1206 local o={
1207 ["do"]=true;["and"]=true;["else"]=true;["break"]=true;
1208 ["if"]=true;["end"]=true;["goto"]=true;["false"]=true;
1209 ["in"]=true;["for"]=true;["then"]=true;["local"]=true;
1210 ["or"]=true;["nil"]=true;["true"]=true;["until"]=true;
1211 ["elseif"]=true;["function"]=true;["not"]=true;
1212 ["repeat"]=true;["return"]=true;["while"]=true;
1213 };
1214 local function u(e)
1215 if a(e)~="table"then
1216 e={preset=e};
1217 end
1218 local i={
1219 table=true;
1220 string=true;
1221 number=true;
1222 boolean=true;
1223 ["nil"]=true;
1224 };
1225 if e.preset=="debug"then
1226 e.preset="oneline";
1227 e.freeze=true;
1228 e.fatal=false;
1229 e.fallback=t;
1230 e.unquoted=true;
1231 end
1232 if e.preset=="oneline"then
1233 e.indentwith=e.indentwith or"";
1234 e.itemstart=e.itemstart or" ";
1235 e.itemlast=e.itemlast or"";
1236 e.tend=e.tend or" }";
1237 elseif e.preset=="compact"then
1238 e.indentwith=e.indentwith or"";
1239 e.itemstart=e.itemstart or"";
1240 e.itemlast=e.itemlast or"";
1241 e.equals=e.equals or"=";
1242 e.unquoted=true;
1243 end
1244 local s=e.fallback or e.fatal==false and t or s;
1245 local function l(e)
1246 return(i[a(e)]or s)(e);
1247 end
1248 local p=e.keywords or o;
1249 local y=e.indentwith or"\t";
1250 local v=e.itemstart or"\n";
1251 local I=e.itemsep or";";
1252 local N=e.itemlast or";\n";
1253 local O=e.tstart or"{";
1254 local E=e.tend or"}";
1255 local T=e.kstart or"[";
1256 local A=e.kend or"]";
1257 local f=e.equals or" = ";
1258 local w=e.unquoted==true and"^[%a_][%w_]*$"or e.unquoted;
1259 local d=e.hex;
1260 local S=e.freeze;
1261 local m=e.maxdepth or 127;
1262 local H=e.multiref;
1263 local R=e.table_iterator or h;
1264 local function u(o,t,e,i)
1265 if t[o]then
1266 t[e],e=s(o,"table has multiple references"),e+1;
1267 return e;
1268 elseif i>m then
1269 t[e],e=s(o,"max table depth reached"),e+1;
1270 return e;
1271 end
1272 local m=o;
1273 t[o]=true;
1274 t[m]=true;
1275 if S==true then
1276 local i=_(o);
1277 if a(i)=="table"then
1278 local n=i.__name;
1279 local i=i.__freeze;
1280 if a(i)=="function"then
1281 o=i(o);
1282 if a(o)=="string"then
1283 t[e],e=o,e+1;
1284 return e;
1285 end
1286 if a(n)=="string"then
1287 t[e],e=n,e+1;
1288 end
1289 end
1290 end
1291 end
1292 t[e],e=O,e+1;
1293 local b=c(y,i);
1294 local s=1;
1295 local h,r;
1296 local d=false;
1297 for o,n in R(o)do
1298 d=true;
1299 t[e],e=v,e+1;
1300 t[e],e=b,e+1;
1301 h,r=a(o),a(n);
1302 if o==s then
1303 s=s+1;
1304 elseif w and h=="string"and
1305 not p[o]and k(o,w)then
1306 t[e],e=o,e+1;
1307 t[e],e=f,e+1;
1308 else
1309 t[e],e=T,e+1;
1310 if h=="table"then
1311 e=u(o,t,e,i+1);
1312 else
1313 t[e],e=l(o),e+1;
1314 end
1315 t[e],t[e+1],e=A,f,e+2;
1316 end
1317 if r=="table"then
1318 e=u(n,t,e,i+1);
1319 else
1320 t[e],e=l(n),e+1;
1321 end
1322 t[e],e=I,e+1;
1323 end
1324 if d then
1325 t[e-1]=N;
1326 t[e],e=c(y,i-1),e+1;
1327 end
1328 t[e],e=E,e+1;
1329 if H then
1330 t[o]=nil;
1331 t[m]=nil;
1332 end
1333 return e;
1334 end
1335 function i.table(t)
1336 local e={};
1337 u(t,e,1,1);
1338 return q(e);
1339 end
1340 local function t(e)
1341 return'"'..z(e,"[%z\1-\31\"\'\\\127-\255]",r)..'"';
1342 end
1343 if a(d)=="string"then
1344 function i.string(e)
1345 local t=t(e);
1346 if#t>(#e*2+2+#d)then
1347 return d..'"'..g(e)..'"';
1348 end
1349 return t;
1350 end
1351 else
1352 i.string=t;
1353 end
1354 function i.number(e)
1355 if x(e)=="integer"then
1356 return n("%d",e);
1357 elseif e==b then
1358 return"(1/0)";
1359 elseif e==j then
1360 return"(-1/0)";
1361 elseif e~=e then
1362 return"(0/0)";
1363 end
1364 return n("%.18g",e);
1365 end
1366 i["nil"]=function()
1367 return"nil";
1368 end
1369 function i.boolean(e)
1370 return e and"true"or"false";
1371 end
1372 return l;
1373 end
1374 local function o(e)
1375 if a(e)~="string"then return nil;end
1376 e="return "..e;
1377 local e,t=v(e,"=serialized data",{});
1378 if not e then return nil,t;end
1379 local t,e=p(e);
1380 if not t then return nil,e;end
1381 return e;
1382 end
1383 local a=u();
1384 return{
1385 new=u;
1386 serialize=function(e,t)
1387 if t==nil then
1388 return a(e);
1389 else
1390 return u(t)(e);
1391 end
1392 end;
1393 deserialize=o;
1394 };
1395 end)
1396 package.preload['util.indexedbheap']=(function(...)
1397 local _ENV=_ENV;
1398 local function e(t,...)
1399 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
1400 package.loaded[t]=e;
1401 for t=1,select("#",...)do
1402 (select(t,...))(e);
1403 end
1404 _ENV=e;
1405 _M=e;
1406 return e;
1407 end
1408 local d=setmetatable;
1409 local r=math.floor;
1410 local i=table.remove;
1411 local function l(t,s,o,n,i)
1412 local e=#t+1;
1413 while true do
1414 local a=r(e/2);
1415 if a==0 or s>t[a]then break;end
1416 t[e]=t[a];
1417 o[e]=o[a];
1418 i[o[e]]=e;
1419 e=a;
1420 end
1421 t[e]=s;
1422 o[e]=n;
1423 i[n]=e;
1424 end
1425 local function h(a,e,t,i)
1426 local n=a[e];
1427 local s=t[e];
1428 while e~=1 do
1429 local o=r(e/2);
1430 if n>=a[o]then break;end
1431 a[e]=a[o];
1432 t[e]=t[o];
1433 i[t[e]]=e;
1434 e=o;
1435 end
1436 a[e]=n;
1437 t[e]=s;
1438 i[s]=e;
1439 return e;
1440 end
1441 local function n(a,e,o,h)
1442 local s=a[e];
1443 local n=o[e];
1444 local i=#a;
1445 local t=2*e;
1446 while 2*e<=i do
1447 if t~=i and a[t]>a[t+1]then
1448 t=t+1;
1449 end
1450 if s>a[t]then
1451 a[e]=a[t];
1452 o[e]=o[t];
1453 h[o[e]]=e;
1454 else
1455 break;
1456 end
1457 e=t;
1458 t=2*e;
1459 end
1460 a[e]=s;
1461 o[e]=n;
1462 h[n]=e;
1463 return e;
1464 end
1465 local function r(e,t,o)
1466 local h=#e;
1467 if h==0 then return nil;end
1468 local s=e[1];
1469 local a=t[1];
1470 o[a]=nil;
1471 if h==1 then
1472 e[1]=nil;
1473 t[1]=nil;
1474 return s,a;
1475 end
1476 e[1]=i(e);
1477 t[1]=i(t);
1478 o[t[1]]=1;
1479 n(e,1,t,o);
1480 return s,a;
1481 end
1482 local t={};
1483 function t:insert(t,a,e)
1484 if e==nil then
1485 e=self.current_id;
1486 self.current_id=e+1;
1487 end
1488 self.items[e]=t;
1489 l(self.priorities,a,self.ids,e,self.index);
1490 return e;
1491 end
1492 function t:pop()
1493 local a,e=r(self.priorities,self.ids,self.index);
1494 if e then
1495 local t=self.items[e];
1496 self.items[e]=nil;
1497 return a,t,e;
1498 end
1499 end
1500 function t:peek()
1501 return self.priorities[1];
1502 end
1503 function t:reprioritize(e,t)
1504 local e=self.index[e];
1505 if e==nil then return;end
1506 self.priorities[e]=t;
1507 e=h(self.priorities,e,self.ids,self.index);
1508 n(self.priorities,e,self.ids,self.index);
1509 end
1510 function t:remove_index(e)
1511 local o=self.priorities[e];
1512 if o==nil then return;end
1513 local t=self.ids[e];
1514 local s=self.items[t];
1515 local a=#self.priorities;
1516 self.priorities[e]=self.priorities[a];
1517 self.ids[e]=self.ids[a];
1518 self.index[self.ids[e]]=e;
1519 i(self.priorities);
1520 i(self.ids);
1521 self.index[t]=nil;
1522 self.items[t]=nil;
1523 if a>e then
1524 e=h(self.priorities,e,self.ids,self.index);
1525 n(self.priorities,e,self.ids,self.index);
1526 end
1527 return o,s,t;
1528 end
1529 function t:remove(e)
1530 return self:remove_index(self.index[e]);
1531 end
1532 local e={__index=t};
1533 local e={
1534 create=function()
1535 return d({
1536 ids={};
1537 items={};
1538 priorities={};
1539 index={};
1540 current_id=1.5
1541 },e);
1542 end
1543 };
1544 return e;
1545 end)
1546 package.preload['util.xpcall']=(function(...)
1547 local _ENV=_ENV;
1548 local function e(t,...)
1549 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
1550 package.loaded[t]=e;
1551 for t=1,select("#",...)do
1552 (select(t,...))(e);
1553 end
1554 _ENV=e;
1555 _M=e;
1556 return e;
1557 end
1558 local e=xpcall;
1559 if select(2,e(function(e)return e end,function()end,"test"))~="test"then
1560 e=require"util.compat".xpcall;
1561 end
1562 return{
1563 xpcall=e;
1564 };
1565 end)
1566 package.preload['util.array']=(function(...)
1567 local _ENV=_ENV;
1568 local function e(t,...)
1569 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
1570 package.loaded[t]=e;
1571 for t=1,select("#",...)do
1572 (select(t,...))(e);
1573 end
1574 _ENV=e;
1575 _M=e;
1576 return e;
1577 end
1578 local r,l,u,f
1579 =table.insert,table.sort,table.remove,table.concat;
1580 local h=require"util.table".move;
1581 local s=setmetatable;
1582 local a=getmetatable;
1583 local d=math.random;
1584 local c=math.floor;
1585 local y,p=pairs,ipairs;
1586 local w=tostring;
1587 local m=type;
1588 local i={};
1589 local t={};
1590 local e={};
1591 local o={
1592 __index=e;
1593 __name="array";
1594 __tostring=function(e)return"{"..e:concat(", ").."}";end;
1595 };
1596 function o:__freeze()return self;end
1597 local function n(i,e,a,t)
1598 if m(e)=="function"then
1599 e=i.collect(e,a,t);
1600 end
1601 return s(e or{},o);
1602 end
1603 function o.__add(t,a)
1604 local e=n();
1605 return e:append(t):append(a);
1606 end
1607 function o.__eq(e,t)
1608 if a(e)~=o or a(t)~=o then
1609 return false;
1610 end
1611 if#e==#t then
1612 for a=1,#e do
1613 if e[a]~=t[a]then
1614 return false;
1615 end
1616 end
1617 else
1618 return false;
1619 end
1620 return true;
1621 end
1622 function o.__div(t,o)
1623 local a=n();
1624 local e=0;
1625 for i=1,#t do
1626 local t=o(t[i]);
1627 if t~=nil then
1628 e=e+1;
1629 a[e]=t;
1630 end
1631 end
1632 return a;
1633 end
1634 s(i,{__call=n});
1635 function e:random()
1636 return self[d(1,#self)];
1637 end
1638 function e:random_other(t)
1639 local e=#self;
1640 return self[((math.random(1,e-1)+(t-1))%e)+1];
1641 end
1642 function t.map(e,t,a)
1643 for t,o in p(t)do
1644 e[t]=a(o);
1645 end
1646 return e;
1647 end
1648 function t.filter(t,o,n)
1649 local i,a=o==t,#o;
1650 local e=1;
1651 for a=1,a do
1652 local a=o[a];
1653 if n(a)then
1654 t[e]=a;
1655 e=e+1;
1656 end
1657 end
1658 if i and e<=a then
1659 for e=e,a do
1660 t[e]=nil;
1661 end
1662 end
1663 return t;
1664 end
1665 function t.slice(a,t,o,e)
1666 if e==nil then
1667 e=-1;
1668 end
1669 if e<0 then
1670 e=#t+(e+1);
1671 end
1672 if o<0 then
1673 o=#t+(o+1);
1674 end
1675 if o<1 then
1676 o=1;
1677 end
1678 if e>#t then
1679 e=#t;
1680 end
1681 if o>e then
1682 for e=1,#a do
1683 a[e]=nil;
1684 end
1685 return a;
1686 end
1687 h(t,o,e,1,a);
1688 if t==a then
1689 h(t,#a+1,#a*2,2+e-o,t);
1690 end
1691 return a;
1692 end
1693 function t.sort(e,t,...)
1694 if t~=e then
1695 e:append(t);
1696 end
1697 l(e,...);
1698 return e;
1699 end
1700 function t.unique(o,a)
1701 local e={};
1702 return t.filter(o,a,function(t)
1703 if e[t]then
1704 return false;
1705 else
1706 e[t]=true;
1707 return true;
1708 end
1709 end);
1710 end
1711 function t.pluck(a,e,o,i)
1712 for t=1,#e do
1713 local e=e[t][o];
1714 if e==nil then
1715 e=i;
1716 end
1717 a[t]=e;
1718 end
1719 return a;
1720 end
1721 function t.reverse(e,i)
1722 local t=#i;
1723 if i==e then
1724 local a=c(t/2);
1725 t=t+1;
1726 local o;
1727 for a=1,a do
1728 o=t-a;
1729 e[a],e[o]=e[o],e[a];
1730 end
1731 else
1732 local a=t+1;
1733 for t=1,t do
1734 e[t]=i[a-t];
1735 end
1736 end
1737 return e;
1738 end
1739 function e:shuffle()
1740 local t=#self;
1741 for e=1,#self do
1742 local t=d(e,t);
1743 self[e],self[t]=self[t],self[e];
1744 end
1745 return self;
1746 end
1747 function e:append(e)
1748 h(e,1,#e,#self+1,self);
1749 return self;
1750 end
1751 function e:push(t)
1752 r(self,t);
1753 return self;
1754 end
1755 e.pop=u;
1756 function e:concat(e)
1757 return f(i.map(self,w),e);
1758 end
1759 function e:length()
1760 return#self;
1761 end
1762 function i.collect(i,a,e)
1763 local t={};
1764 while true do
1765 e=i(a,e);
1766 if e==nil then break;end
1767 r(t,e);
1768 end
1769 return s(t,o);
1770 end
1771 for t,a in y(t)do
1772 local a=a;
1773 i[t]=function(t,...)
1774 local o=n();
1775 return a(o,t,...);
1776 end
1777 e[t]=function(e,...)
1778 return a(e,e,...);
1779 end
1780 end
1781 return i;
1782 end)
1783 package.preload['util.format']=(function(...)
1784 local _ENV=_ENV;
1785 local function e(t,...)
1786 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
1787 package.loaded[t]=e;
1788 for t=1,select("#",...)do
1789 (select(t,...))(e);
1790 end
1791 _ENV=e;
1792 _M=e;
1793 return e;
1794 end
1795 local n=tostring;
1796 local m=table.unpack;
1797 local e=table.pack;
1798 local c=require"util.encodings".utf8.valid;
1799 local i=type;
1800 local l=require"util.serialization".new("debug");
1801 local r=math.type;
1802 local u={c=true,d=true,i=true,o=true,u=true,X=true,x=true,};
1803 local f={o=true;u=true;x=true;X=true};
1804 local d={
1805 ["\000"]="\226\144\128",["\001"]="\226\144\129",["\002"]="\226\144\130",
1806 ["\003"]="\226\144\131",["\004"]="\226\144\132",["\005"]="\226\144\133",
1807 ["\006"]="\226\144\134",["\007"]="\226\144\135",["\008"]="\226\144\136",
1808 ["\009"]="\226\144\137",["\010"]="\226\144\138",["\011"]="\226\144\139",
1809 ["\012"]="\226\144\140",["\013"]="\226\144\141",["\014"]="\226\144\142",
1810 ["\015"]="\226\144\143",["\016"]="\226\144\144",["\017"]="\226\144\145",
1811 ["\018"]="\226\144\146",["\019"]="\226\144\147",["\020"]="\226\144\148",
1812 ["\021"]="\226\144\149",["\022"]="\226\144\150",["\023"]="\226\144\151",
1813 ["\024"]="\226\144\152",["\025"]="\226\144\153",["\026"]="\226\144\154",
1814 ["\027"]="\226\144\155",["\028"]="\226\144\156",["\029"]="\226\144\157",
1815 ["\030"]="\226\144\158",["\031"]="\226\144\159",["\127"]="\226\144\161",
1816 };
1817 local y=pcall(string.format,"%p","");
1818 local function w(h,...)
1819 local o=e(...);
1820 local w=o.n;
1821 local a=0;
1822 h=h:gsub("%%[^cdiouxXaAeEfgGpqs%%]*[cdiouxXaAeEfgGpqs%%]",function(s)
1823 if s=="%%"then return end
1824 a=a+1;
1825 local e=o[a];
1826 if e==nil then
1827 o[a]="nil";
1828 return"(%s)";
1829 end
1830 local t=s:sub(-1);
1831 local i=i(e);
1832 if t=="s"and i=="string"and not e:find("[%z\1-\31\128-\255]")then
1833 return
1834 elseif i=="number"then
1835 if t=="g"or(t=="d"and r(e)=="integer")then return end
1836 elseif t=="s"and i~="string"then
1837 e=n(e);
1838 i="string";
1839 end
1840 if t~="s"and t~="q"and t~="p"then
1841 if i~="number"then
1842 e=n(e);
1843 t="s";
1844 s="[%s]";
1845 i="string";
1846 elseif u[t]and r(e)~="integer"then
1847 o[a]=n(e);
1848 return"[%s]";
1849 elseif f[t]and e<0 then
1850 o[a]=n(e);
1851 return"[%s]";
1852 else
1853 return
1854 end
1855 end
1856 if t=="p"and not y then
1857 e=n(e);
1858 t="s";
1859 s="[%s]";
1860 i="string";
1861 end
1862 if i=="string"and t~="p"then
1863 if not c(e)then
1864 t="q";
1865 elseif t~="q"then
1866 o[a]=e:gsub("[%z\1-\8\11-\31\127]",d):gsub("\n\t?","\n\t");
1867 return s;
1868 end
1869 end
1870 if t=="q"then
1871 o[a]=l(e);
1872 return"%s";
1873 end
1874 if t=="p"and(i=="boolean"or i=="number")then
1875 o[a]=n(e);
1876 return"[%s]";
1877 end
1878 end);
1879 while a<w do
1880 a=a+1;
1881 local e=o[a];
1882 if e==nil then
1883 o[a]="(nil)";
1884 else
1885 o[a]=n(e):gsub("[%z\1-\8\11-\31\127]",d):gsub("\n\t?","\n\t");
1886 end
1887 h=h.." [%s]"
1888 end
1889 return h:format(m(o));
1890 end
1891 return{
1892 format=w;
1893 };
1894 end)
1895 package.preload['util.promise']=(function(...)
1896 local _ENV=_ENV;
1897 local function e(t,...)
1898 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
1899 package.loaded[t]=e;
1900 for t=1,select("#",...)do
1901 (select(t,...))(e);
1902 end
1903 _ENV=e;
1904 _M=e;
1905 return e;
1906 end
1907 local o={};
1908 local a={__name="promise",__index=o};
1909 local n=require"util.xpcall".xpcall;
1910 local l=table.unpack;
1911 function a:__tostring()
1912 return"promise ("..(self._state or"invalid")..")";
1913 end
1914 local function i(e)
1915 local e=getmetatable(e);
1916 return e==a;
1917 end
1918 local function e(e,t,o,a)
1919 if not e then
1920 return a;
1921 end
1922 return function(a)
1923 local a,e=n(e,debug.traceback,a);
1924 if a then
1925 t(e);
1926 else
1927 o(e);
1928 end
1929 return true;
1930 end;
1931 end
1932 local function u(o,i,n,a,t)
1933 table.insert(o._pending_on_fulfilled,e(i,a,t,a));
1934 table.insert(o._pending_on_rejected,e(n,a,t,t));
1935 end
1936 local function r(a,o,n,t,i)
1937 e(o,t,i,t)(a.value);
1938 end
1939 local function d(i,n,o,a,t)
1940 e(o,a,t,t)(i.reason);
1941 end
1942 local function h(e,i,o,t,a)
1943 if e._state~="pending"then
1944 return;
1945 end
1946 e._state=i;
1947 e._next=o;
1948 for o,t in ipairs(t)do
1949 t(a);
1950 end
1951 e._pending_on_fulfilled=nil;
1952 e._pending_on_rejected=nil;
1953 return true;
1954 end
1955 local function s(e)
1956 local function a(t)
1957 if i(t)then
1958 t:next(s(e));
1959 elseif h(e,"fulfilled",r,e._pending_on_fulfilled,t)then
1960 e.value=t;
1961 end
1962 end
1963 local function o(t)
1964 if h(e,"rejected",d,e._pending_on_rejected,t)then
1965 e.reason=t;
1966 end
1967 end
1968 return a,o;
1969 end
1970 local d=function(e)
1971 e();
1972 end
1973 local function e(o)
1974 local e=setmetatable({_state="pending",_next=u,_pending_on_fulfilled={},_pending_on_rejected={}},a);
1600 if o then 1975 if o then
1601 t("debug","Records for %s already cached, using those...",e); 1976 d(function()
1602 d(o); 1977 local a,t=s(e);
1603 return; 1978 local o,a=n(o,debug.traceback,a,t);
1604 end 1979 if not o and e._state=="pending"then
1605 t("debug","Records for %s not in cache, sending query (%s)...",e,h(n.running())); 1980 t(a);
1606 local i,o=a.query(e,s,r); 1981 end
1607 if i then 1982 end);
1608 n.yield({r or"IN",s or"A",e,n.running()}); 1983 end
1609 t("debug","Reply for %s (%s)",e,h(n.running())); 1984 return e;
1610 end 1985 end
1611 if i then 1986 local function r(o)
1612 i,o=l(d,a.peek(e,s,r)); 1987 return e(function(n,r)
1613 else 1988 local e,a,s=0,{},false;
1614 t("error","Error sending DNS query: %s",o); 1989 local t=0;
1615 i,o=l(d,nil,o); 1990 for h,o in pairs(o)do
1616 end 1991 if i(o)then
1617 if not i then 1992 t=t+1;
1618 t("error","Error in DNS response handler: %s",h(o)); 1993 o:next(function(o)
1619 end 1994 a[h]=o;
1620 end)(a.peek(e,s,r)); 1995 e=e+1;
1621 end 1996 if e==t and s then
1622 function cancel(e,o,i) 1997 n(a);
1623 t("warn","Cancelling DNS lookup for %s",h(e[3])); 1998 end
1624 a.cancel(e[1],e[2],e[3],e[4],o); 1999 end,r);
1625 end 2000 else
1626 function new_async_socket(i,o) 2001 a[h]=o;
1627 local n="<unknown>"; 2002 end
1628 local s={}; 2003 end
2004 s=true;
2005 if e==t then
2006 n(a);
2007 end
2008 end);
2009 end
2010 local function u(n)
2011 return e(function(h)
2012 local e,t,o=0,{},false;
2013 local a=0;
2014 for s,n in pairs(n)do
2015 if i(n)then
2016 a=a+1;
2017 n:next(function(i)
2018 t[s]={status="fulfilled",value=i};
2019 e=e+1;
2020 if e==a and o then
2021 h(t);
2022 end
2023 end,function(i)
2024 t[s]={status="rejected",reason=i};
2025 e=e+1;
2026 if e==a and o then
2027 h(t);
2028 end
2029 end);
2030 else
2031 t[s]=n;
2032 end
2033 end
2034 o=true;
2035 if e==a then
2036 h(t);
2037 end
2038 end);
2039 end
2040 local function n(e,...)
2041 local t,a={...},select("#",...);
2042 return r(t):next(function(t)
2043 return e(l(t,1,a));
2044 end);
2045 end
2046 local function s(t)
2047 return e(function(e,a)
2048 for o=1,#t do
2049 t[o]:next(e,a);
2050 end
2051 end);
2052 end
2053 local function t(t)
2054 return e(function(e)
2055 e(t);
2056 end);
2057 end
2058 local function a(t)
2059 return e(function(a,e)
2060 e(t);
2061 end);
2062 end
2063 local function h(e)
2064 return t():next(function()return e();end);
2065 end
2066 function o:next(o,a)
2067 return e(function(e,t)
2068 self:_next(o,a,e,t);
2069 end);
2070 end
2071 function o:catch(e)
2072 return self:next(nil,e);
2073 end
2074 function o:finally(e)
2075 local function t(t)e();return t;end
2076 local function o(t)e();return a(t);end
2077 return self:next(t,o);
2078 end
2079 return{
2080 new=e;
2081 resolve=t;
2082 join=n;
2083 reject=a;
2084 all=r;
2085 all_settled=u;
2086 race=s;
2087 try=h;
2088 is_promise=i;
2089 set_nexttick=function(e)d=e;end;
2090 }
2091 end)
2092 package.preload['net.adns']=(function(...)
2093 local _ENV=_ENV;
2094 local function e(t,...)
2095 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
2096 package.loaded[t]=e;
2097 for t=1,select("#",...)do
2098 (select(t,...))(e);
2099 end
2100 _ENV=e;
2101 _M=e;
2102 return e;
2103 end
2104 local o=setmetatable;
2105 local c=tostring;
2106 local k=table.concat;
2107 local n=string.format;
2108 local q=string.lower;
2109 local d=string.upper;
2110 local i=function()end;
2111 local w=require"util.logger";
2112 local l=w.init("unbound");
2113 local p=require"net.server";
2114 local m=require"lunbound";
2115 local g=require"util.promise";
2116 local b=require"util.id".short;
2117 local y=require"socket".gettime;
2118 local e=require"util.dns";
2119 local h,s,u=e.classes,e.types,e.errors;
2120 local v=e.parsers;
2121 local t={hoststxt=false}
2122 local function r(e)
2123 e=e or{};
2124 for t,a in pairs(t)do
2125 if e[t]==nil then
2126 e[t]=a;
2127 end
2128 end
2129 for t,a in pairs(m.config)do
2130 if e[t]==nil then
2131 e[t]=a;
2132 end
2133 end
2134 return e;
2135 end
2136 local a;
2137 if prosody then
2138 local e=require"core.configmanager";
2139 a=r(e.get("*","unbound"));
2140 prosody.events.add_handler("config-reloaded",function()
2141 a=r(e.get("*","unbound"));
2142 end);
2143 end
2144 local function r(e,t)
2145 l("debug","Setting up net.server event handling for %s",e);
2146 return t.watchfd(e,function()
2147 l("debug","Processing queries for %s",e);
2148 e:process()
2149 end);
2150 end
2151 local t,f;
2152 local function l()
2153 t=m.new(a);
2154 f=r(t,p);
2155 end
2156 if prosody then
2157 prosody.events.add_handler("server-started",l);
2158 end
2159 local n={
2160 __tostring=function(e)
2161 if e._string then return e._string end
2162 local t=n("Status: %s",u[e.status]);
2163 if e.secure then
2164 t=t..", Secure";
2165 elseif e.bogus then
2166 t=t..n(", Bogus: %s",e.bogus);
2167 end
2168 local t={t};
2169 for a=1,#e do
2170 t[a+1]=e.qname.."\t"..h[e.qclass].."\t"..s[e.qtype].."\t"..c(e[a]);
2171 end
2172 local t=k(t,"\n");
2173 e._string=t;
2174 return t;
2175 end;
2176 };
2177 local r={};
2178 local function p(e)
2179 if not e then return end
2180 local a=u[e.rcode];
2181 local i=h[e.qclass];
2182 local t=s[e.qtype];
2183 e.status,e.class,e.type=a,i,t;
2184 local a=q(t);
2185 local i={__index=e,__tostring=function(e)return c(e[a])end};
2186 local s=v[t];
2187 for t=1,#e do
2188 if e.bogus then
2189 e[t]=nil;
2190 else
2191 e[t]=o({[a]=s(e[t])},i);
2192 end
2193 end
2194 return o(e,n);
2195 end
2196 local function m(m,u,o,a)
2197 if not t then l();end
2198 o=o and d(o)or"A";
2199 a=a and d(a)or"IN";
2200 local l,h=s[o],h[a];
2201 local d=y();
2202 local n;
2203 local i=w.init("unbound.query"..b());
2204 local function s(e,t)
2205 local s=y();
2206 r[n]=nil;
2207 if e then
2208 p(e);
2209 i("debug","Results for %s %s %s: %s (%s, %f sec)",u,a,o,e.rcode==0 and(#e.." items")or e.status,
2210 e.secure and"Secure"or e.bogus or"Insecure",s-d);
2211 else
2212 i("error","Results for %s %s %s: %s",u,a,o,c(t));
2213 end
2214 local t,e=pcall(m,e,t);
2215 if not t then i("error","Error in callback: %s",e);end
2216 end
2217 i("debug","Resolve %s %s %s",u,a,o);
2218 local e;
2219 n,e=t:resolve_async(s,u,l,h);
2220 if n then
2221 r[n]=m;
2222 else
2223 i("error","Resolver error: %s",e);
2224 end
2225 return n,e;
2226 end
2227 local function o(o,a,e)
2228 if not t then l();end
2229 a=a and d(a)or"A";
2230 e=e and d(e)or"IN";
2231 local e,a=s[a],h[e];
2232 local e,t=t:resolve(o,e,a);
2233 if not e then return e,t;end
2234 return p(e);
2235 end
2236 local function e(e)
2237 local a=r[e];
2238 t:cancel(e);
2239 if a then
2240 a(nil,"canceled");
2241 r[e]=nil;
2242 end
2243 return true;
2244 end
2245 local function t()
2246 for t in pairs(r)do e(t);end
2247 if f then f:close();end
2248 l();
2249 return true;
2250 end
2251 local function a()
2252 error"not implemented";
2253 end
2254 local a={
2255 lookup=m;
2256 cancel=e;
2257 new_async_socket=a;
2258 dns={
2259 lookup=o;
2260 cancel=e;
2261 cache=i;
2262 socket_wrapper_set=i;
2263 settimeout=i;
2264 query=i;
2265 purge=t;
2266 random=i;
2267 peek=i;
2268 types=s;
2269 classes=h;
2270 };
2271 };
2272 local function n(e,i,a,o)
2273 return g.new(function(n,t)
2274 local function s(a,e)
2275 if e then
2276 return t(e);
2277 else
2278 return n(a);
2279 end
2280 end
2281 local e,a=m(s,i,a,o)
2282 if not e then t(a);end
2283 end);
2284 end
2285 local e={
2286 lookup=function(i,e,t,a,o)
2287 return m(e,t,a,o)
2288 end;
2289 lookup_promise=n;
2290 _resolver={
2291 settimeout=function()end;
2292 closeall=function()end;
2293 };
2294 }
2295 function a.resolver()return e;end
2296 return a;
2297 end)
2298 package.preload['util.dns']=(function(...)
2299 local _ENV=_ENV;
2300 local function e(t,...)
2301 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
2302 package.loaded[t]=e;
2303 for t=1,select("#",...)do
2304 (select(t,...))(e);
2305 end
2306 _ENV=e;
2307 _M=e;
2308 return e;
2309 end
2310 local t=setmetatable;
2311 local e=table;
2312 local s=e.concat;
2313 local n=e.insert;
2314 local o=string.byte;
2315 local i=string.format;
2316 local r=string.sub;
2317 local h=require"util.dnsregistry";
2318 local c=require"util.hex".encode;
2319 local d=require"util.net".ntop;
1629 local e={}; 2320 local e={};
1630 local h; 2321 local function a(a,t)
1631 function s.onincoming(o,t) 2322 if o(a,t)==0 then return".",t+1;end
1632 if t then 2323 local d,h,i=#a,{};
1633 a.feed(e,t); 2324 t=t or 1;
1634 end 2325 repeat
1635 end 2326 i=o(a,t)or 0;
1636 function s.ondisconnect(a,i) 2327 n(h,r(a,t+1,t+i));
1637 if i then 2328 t=t+i+1;
1638 t("warn","DNS socket for %s disconnected: %s",n,i); 2329 until i==0 or t>=d;
1639 local e=o.server; 2330 return s(h,"."),t;
1640 if o.socketset[a]==o.best_server and o.best_server==#e then 2331 end
1641 t("error","Exhausted all %d configured DNS servers, next lookup will try %s again",#e,e[1]); 2332 e.CNAME=a;
1642 end 2333 e.NS=a
1643 o:servfail(a); 2334 e.PTR=a;
1644 end 2335 local x={
1645 end 2336 __tostring=function(e)
1646 e,h=c.wrapclient(i,"dns",53,s); 2337 return i("%s %s %d %d %d %d %d",e.mname,e.rname,e.serial,e.refresh,e.retry,e.expire,e.minimum);
1647 if not e then 2338 end;
1648 return nil,h; 2339 };
1649 end 2340 function e.SOA(n)
1650 e.settimeout=function()end 2341 local s,h,i;
1651 e.setsockname=function(t,...)return i:setsockname(...);end 2342 s,i=a(n,1);
1652 e.setpeername=function(o,...)n=(...);local a,t=i:setpeername(...);o:set_send(u);return a,t;end 2343 h,i=a(n,i);
1653 e.connect=function(t,...)return i:connect(...)end 2344 local
1654 e.send=function(a,e) 2345 j,q,g,k,
1655 t("debug","Sending DNS query to %s",n); 2346 b,l,r,d,
1656 return i:send(e); 2347 n,a,o,i,
1657 end 2348 u,w,c,p,
1658 return e; 2349 v,y,m,f
1659 end 2350 =o(n,i,i+19);
1660 a.socket_wrapper_set(new_async_socket); 2351 return t({
1661 return _M; 2352 mname=s;
2353 rname=h;
2354 serial=j*16777216+q*65536+g*256+k;
2355 refresh=b*16777216+l*65536+r*256+d;
2356 retry=n*16777216+a*65536+o*256+i;
2357 expire=u*16777216+w*65536+c*256+p;
2358 minimum=v*16777216+y*65536+m*256+f;
2359 },x);
2360 end
2361 e.A=d;
2362 e.AAAA=d;
2363 local d={
2364 __tostring=function(e)
2365 return i("%d %s",e.pref,e.mx)
2366 end
2367 };
2368 function e.MX(e)
2369 local i=a(e,3);
2370 local a,e=o(e,1,2);
2371 return t({
2372 pref=a*256+e;
2373 mx=i;
2374 },d);
2375 end
2376 local d={
2377 __tostring=function(e)
2378 return i("%d %d %d %s",e.priority,e.weight,e.port,e.target);
2379 end
2380 };
2381 function e.SRV(e)
2382 local i=a(e,7);
2383 local e,a,o,n,h,s=o(e,1,6);
2384 return t({
2385 priority=e*256+a;
2386 weight=o*256+n;
2387 port=h*256+s;
2388 target=i;
2389 },d);
2390 end
2391 local d={__tostring=s};
2392 function e.TXT(s)
2393 local l=#s;
2394 local h,a,i={},1;
2395 repeat
2396 i=o(s,a)or 0;
2397 n(h,r(s,a+1,a+i));
2398 a=a+i+1;
2399 until a>=l;
2400 return t(h,d);
2401 end
2402 e.SPF=e.TXT;
2403 local d={
2404 [0]="PKIX-CA";
2405 [1]="PKIX-EE";
2406 [2]="DANE-TA";
2407 [3]="DANE-EE";
2408 [255]="PrivCert";
2409 };
2410 local u={
2411 [0]="Cert",
2412 [1]="SPKI",
2413 [255]="PrivSel",
2414 };
2415 local l={
2416 [0]="Full",
2417 [1]="SHA2-256",
2418 [2]="SHA2-512",
2419 [255]="PrivMatch",
2420 };
2421 local m={
2422 __tostring=function(e)
2423 return i("%s %s %s %s",
2424 d[e.use]or e.use,
2425 u[e.select]or e.select,
2426 l[e.match]or e.match,
2427 c(e.data));
2428 end;
2429 __index={
2430 getUsage=function(e)return d[e.use]end;
2431 getSelector=function(e)return u[e.select]end;
2432 getMatchType=function(e)return l[e.match]end;
2433 }
2434 };
2435 function e.TLSA(e)
2436 local i,o,a=o(e,1,3);
2437 return t({
2438 use=i;
2439 select=o;
2440 match=a;
2441 data=r(e,4);
2442 },m);
2443 end
2444 local o={"alpn";"no-default-alpn";"port";"ipv4hint";"ech";"ipv6hint"};
2445 t(o,{__index=function(t,e)return"key"..tostring(e);end});
2446 local m={
2447 __tostring=function(e)
2448 local t={};
2449 for a=1,#e.fields do
2450 n(t,i("%s=%q",o[e.fields[a].key],tostring(e.fields[a].value)));
2451 end
2452 return i("%d %s %s",e.prio,e.name,s(t," "));
2453 end;
2454 };
2455 local r={__tostring=function(e)return s(e,", ");end}
2456 function e.SVCB(i)
2457 local o,s=i:byte(1,2);
2458 local u=o*256+s;
2459 local l,o=a(i,3);
2460 local d={};
2461 while#i>o do
2462 local s,l=i:byte(o+0,o+1);
2463 local h,a=i:byte(o+2,o+3);
2464 local s=s*256+l;
2465 local h=h*256+a;
2466 local a=i:sub(o+4,o+4-1+h)
2467 if s==1 then
2468 a=t(e.TXT(a),r);
2469 elseif s==3 then
2470 local e,t=a:byte(1,2);
2471 local e=e*256+t;
2472 a=e;
2473 elseif s==4 then
2474 local o={};
2475 for t=1,#a,4 do
2476 n(o,e.A(a:sub(t,t+3)));
2477 end
2478 a=t(o,r);
2479 elseif s==6 then
2480 local o={};
2481 for t=1,#a,16 do
2482 n(o,e.AAAA(a:sub(t,t+15)));
2483 end
2484 a=t(o,r);
2485 end
2486 n(d,{key=s,value=a,len=h});
2487 o=o+h+4;
2488 end
2489 return t({
2490 prio=u,name=l,fields=d,
2491 },m);
2492 end
2493 e.HTTPS=e.SVCB;
2494 local o={
2495 TLSA={
2496 use=d;
2497 select=u;
2498 match=l;
2499 };
2500 };
2501 local a={
2502 __tostring=function(e)
2503 return i([[\# %d %s]],#e.raw,c(e.raw));
2504 end;
2505 };
2506 local function i(e)
2507 return t({raw=e},a);
2508 end
2509 t(e,{__index=function()return i end});
2510 return{
2511 parsers=e;
2512 classes=h.classes;
2513 types=h.types;
2514 errors=h.errors;
2515 params=o;
2516 };
1662 end) 2517 end)
1663 package.preload['net.server']=(function(...) 2518 package.preload['net.server']=(function(...)
1664 local _ENV=_ENV; 2519 local _ENV=_ENV;
1665 local function e(t,...) 2520 local function e(t,...)
1666 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 2521 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
1670 end 2525 end
1671 _ENV=e; 2526 _ENV=e;
1672 _M=e; 2527 _M=e;
1673 return e; 2528 return e;
1674 end 2529 end
1675 local m=function(e) 2530 local f=function(e)
1676 return _G[e] 2531 return _G[e]
1677 end 2532 end
1678 local W,e=require("util.logger").init("socket"),table.concat; 2533 local V,e=require("util.logger").init("socket"),table.concat;
1679 local n=function(...)return W("debug",e{...});end 2534 local n=function(...)return V("debug",e{...});end
1680 local E=function(...)return W("warn",e{...});end 2535 local j=function(...)return V("warn",e{...});end
1681 local ce=1 2536 local ie=1
1682 local f=m"type" 2537 local w=f"type"
1683 local b=m"pairs" 2538 local q=f"pairs"
1684 local ue=m"ipairs" 2539 local oe=f"ipairs"
1685 local y=m"tonumber" 2540 local b=f"tonumber"
1686 local l=m"tostring" 2541 local u=f"tostring"
1687 local t=m"table" 2542 local t=f"table"
1688 local a=m"string" 2543 local a=f"string"
1689 local e=m"coroutine" 2544 local e=f"coroutine"
1690 local Y=math.min 2545 local W=math.min
1691 local re=math.huge 2546 local te=math.huge
1692 local pe=t.concat 2547 local ye=t.concat
1693 local he=t.insert 2548 local ae=t.insert
1694 local we=a.sub 2549 local be=a.sub
1695 local fe=e.wrap 2550 local pe=e.wrap
1696 local me=e.yield 2551 local ke=e.yield
1697 local O,e=pcall(require,"ssl") 2552 local x=f"socket"or require"socket"
1698 local g=m"socket"or require"socket" 2553 local P=x.gettime
1699 local F=g.gettime 2554 local e=require"util.net";
1700 local ee=g.dns.getaddrinfo 2555 local ge=e.pton;
1701 local ye=(O and e.wrap) 2556 local ve=require"util.sslconfig";
1702 local te=g.bind 2557 local Y,fe=pcall(require,"net.tls_luasec");
1703 local ve=g.select 2558 local ue=x.bind
1704 local I 2559 local we=x.select
1705 local B 2560 local U
1706 local ae 2561 local Z
2562 local ce
2563 local me
2564 local ee
2565 local l
2566 local de
2567 local J
2568 local K
2569 local le
2570 local re
2571 local ne
2572 local Q
2573 local h
2574 local se
1707 local G 2575 local G
1708 local J 2576 local he
1709 local u
1710 local ie
1711 local X
1712 local se
1713 local ne
1714 local oe
1715 local Q
1716 local s
1717 local le
1718 local Z
1719 local de
1720 local p 2577 local p
1721 local i 2578 local i
1722 local P 2579 local F
1723 local r 2580 local r
1724 local h 2581 local s
2582 local R
2583 local v
2584 local y
2585 local k
1725 local T 2586 local T
1726 local v
1727 local w
1728 local k
1729 local x
1730 local a 2587 local a
1731 local o 2588 local o
1732 local q 2589 local g
1733 local C 2590 local C
1734 local M 2591 local M
1735 local A 2592 local D
1736 local j 2593 local _
2594 local H
2595 local B
2596 local d
2597 local S
2598 local I
2599 local O
1737 local N 2600 local N
1738 local K
1739 local d
1740 local H
1741 local S
1742 local R
1743 local D
1744 local L 2601 local L
1745 local z 2602 local z
1746 local _ 2603 local E
1747 local U 2604 local A
1748 p={} 2605 p={}
1749 i={} 2606 i={}
1750 r={} 2607 r={}
1751 P={} 2608 F={}
1752 h={} 2609 s={}
1753 v={} 2610 v={}
1754 w={} 2611 y={}
1755 T={} 2612 R={}
1756 k={} 2613 k={}
1757 a=0 2614 a=0
1758 o=0 2615 o=0
1759 q=0 2616 g=0
1760 C=0 2617 C=0
1761 M=0 2618 M=0
1762 A=1 2619 D=1
1763 j=128 2620 _=128
1764 N=10 2621 H=10
1765 H=51e3*1024 2622 S=51e3*1024
1766 S=25e3*1024 2623 I=25e3*1024
1767 R=30 2624 O=30
1768 D=6e4 2625 N=6e4
1769 L=14*60 2626 L=14*60
1770 local e=package.config:sub(1,1)=="\\" 2627 local e=package.config:sub(1,1)=="\\"
1771 _=(e and math.huge)or g._SETSIZE or 1024 2628 E=(e and math.huge)or x._SETSIZE or 1024
1772 z=g._SETSIZE or 1024 2629 z=x._SETSIZE or 1024
1773 U=30 2630 A=30
1774 ne=function(y,t,m,c,g,w) 2631 re=function(w,t,m,c,q,y,v)
1775 if t:getfd()>=_ then 2632 if t:getfd()>=E then
1776 E("server.lua: Disallowed FD number: "..t:getfd()) 2633 j("server.lua: Disallowed FD number: "..t:getfd())
1777 t:close() 2634 t:close()
1778 return nil,"fd-too-large" 2635 return nil,"fd-too-large"
1779 end 2636 end
1780 local f=0 2637 local f=0
1781 local v,e=y.onconnect,y.ondisconnect 2638 local b,e=w.onconnect,w.ondisconnect
1782 local b=t.accept 2639 local g=t.accept
1783 local e={} 2640 local e={}
1784 e.shutdown=function()end 2641 e.shutdown=function()end
1785 e.ssl=function() 2642 e.ssl=function()
1786 return w~=nil 2643 return y~=nil
1787 end 2644 end
1788 e.sslctx=function() 2645 e.sslctx=function()
1789 return w 2646 return y
1790 end 2647 end
2648 e.hosts={}
1791 e.remove=function() 2649 e.remove=function()
1792 f=f-1 2650 f=f-1
1793 if e then 2651 if e then
1794 e.resume() 2652 e.resume()
1795 end 2653 end
1796 end 2654 end
1797 e.close=function() 2655 e.close=function()
1798 t:close() 2656 t:close()
1799 o=s(r,t,o) 2657 o=h(r,t,o)
1800 a=s(i,t,a) 2658 a=h(i,t,a)
1801 p[m..":"..c]=nil; 2659 p[m..":"..c]=nil;
1802 h[t]=nil 2660 s[t]=nil
1803 e=nil 2661 e=nil
1804 t=nil 2662 t=nil
1805 n"server.lua: closed server handler and removed sockets from list" 2663 n"server.lua: closed server handler and removed sockets from list"
1806 end 2664 end
1807 e.pause=function(o) 2665 e.pause=function(o)
1808 if not e.paused then 2666 if not e.paused then
1809 a=s(i,t,a) 2667 a=h(i,t,a)
1810 if o then 2668 if o then
1811 h[t]=nil 2669 s[t]=nil
1812 t:close() 2670 t:close()
1813 t=nil; 2671 t=nil;
1814 end 2672 end
1815 e.paused=true; 2673 e.paused=true;
1816 n("server.lua: server [",m,"]:",c," paused") 2674 n("server.lua: server [",m,"]:",c," paused")
1817 end 2675 end
1818 end 2676 end
1819 e.resume=function() 2677 e.resume=function()
1820 if e.paused then 2678 if e.paused then
1821 if not t then 2679 if not t then
1822 t=te(m,c,j); 2680 t=ue(m,c,_);
1823 t:settimeout(0) 2681 t:settimeout(0)
1824 end 2682 end
1825 a=u(i,t,a) 2683 a=l(i,t,a)
1826 h[t]=e 2684 s[t]=e
1827 k[e]=nil 2685 k[e]=nil
1828 e.paused=false; 2686 e.paused=false;
1829 n("server.lua: server [",m,"]:",c," resumed") 2687 n("server.lua: server [",m,"]:",c," resumed")
1830 end 2688 end
1831 end 2689 end
1843 e.pause() 2701 e.pause()
1844 k[e]=d 2702 k[e]=d
1845 n("server.lua: refused new client connection: server full") 2703 n("server.lua: refused new client connection: server full")
1846 return false 2704 return false
1847 end 2705 end
1848 local t,o=b(t) 2706 local t,o=g(t)
1849 if t then 2707 if t then
1850 local o,a=t:getpeername() 2708 local a,o=t:getpeername()
1851 local t,i,e=Z(e,y,t,o,c,a,g,w) 2709 local e,i,t=G(e,w,t,a,c,o,q,y,v)
1852 if e then 2710 if t then
1853 return false 2711 return false
1854 end 2712 end
1855 f=f+1 2713 f=f+1
1856 n("server.lua: accepted new client connection from ",l(o),":",l(a)," to ",l(c)) 2714 n("server.lua: accepted new client connection from ",u(a),":",u(o)," to ",u(c))
1857 if v and not w then 2715 if b and not v then
1858 return v(t); 2716 return b(e);
1859 end 2717 end
1860 return; 2718 return;
1861 elseif o then 2719 elseif o then
1862 n("server.lua: error with new client connection: ",l(o)) 2720 n("server.lua: error with new client connection: ",u(o))
1863 e.pause() 2721 e.pause()
1864 k[e]=d 2722 k[e]=d
1865 return false 2723 return false
1866 end 2724 end
1867 end 2725 end
1868 return e 2726 return e
1869 end 2727 end
1870 Z=function(p,f,t,z,K,A,N,g) 2728 G=function(q,c,t,H,ee,L,D,w,te,P)
1871 if t:getfd()>=_ then 2729 if t:getfd()>=E then
1872 E("server.lua: Disallowed FD number: "..t:getfd()) 2730 j("server.lua: Disallowed FD number: "..t:getfd())
1873 t:close() 2731 t:close()
1874 if p then 2732 if q then
1875 k[p]=d 2733 k[q]=d
1876 p.pause() 2734 q.pause()
1877 end 2735 end
1878 return nil,nil,"fd-too-large" 2736 return nil,nil,"fd-too-large"
1879 end 2737 end
1880 t:settimeout(0) 2738 t:settimeout(0)
1881 local y 2739 local b
1882 local _ 2740 local N
1883 local q 2741 local z
2742 local B
2743 local x
2744 local K=c.onincoming
2745 local G=c.onstatus
2746 local k=c.ondisconnect
2747 local J=c.onpredrain
2748 local X=c.ondrain
2749 local ae=c.onreadtimeout;
2750 local Q=c.ondetach
2751 local g={}
2752 local m=0
2753 local Z
1884 local V 2754 local V
1885 local P=f.onincoming 2755 local f=0
1886 local Y=f.onstatus 2756 local p=false
1887 local k=f.ondisconnect
1888 local D=f.ondrain
1889 local Q=f.onreadtimeout;
1890 local R=f.ondetach
1891 local b={}
1892 local c=0
1893 local B
1894 local L
1895 local m=0
1896 local j=false
1897 local E=false 2757 local E=false
1898 local W,F=0,0 2758 local W,F=0,0
1899 local H=H 2759 local _=S
1900 local S=S 2760 local O=I
1901 local e=b 2761 local e=g
2762 e.extra=P
2763 if P then
2764 e.servername=P.servername
2765 end
1902 e.dispatch=function() 2766 e.dispatch=function()
1903 return P 2767 return K
1904 end 2768 end
1905 e.disconnect=function() 2769 e.disconnect=function()
1906 return k 2770 return k
1907 end 2771 end
1908 e.onreadtimeout=Q; 2772 e.onreadtimeout=ae;
1909 e.setlistener=function(a,t) 2773 e.setlistener=function(a,t,o)
1910 if R then 2774 if Q then
1911 R(a) 2775 Q(a)
1912 end 2776 end
1913 P=t.onincoming 2777 K=t.onincoming
1914 k=t.ondisconnect 2778 k=t.ondisconnect
1915 Y=t.onstatus 2779 G=t.onstatus
1916 D=t.ondrain 2780 J=t.onpredrain
2781 X=t.ondrain
1917 e.onreadtimeout=t.onreadtimeout 2782 e.onreadtimeout=t.onreadtimeout
1918 R=t.ondetach 2783 Q=t.ondetach
2784 if t.onattach then
2785 t.onattach(a,o)
2786 end
2787 end
2788 e._setpending=function()
2789 x=true
1919 end 2790 end
1920 e.getstats=function() 2791 e.getstats=function()
1921 return F,W 2792 return F,W
1922 end 2793 end
1923 e.ssl=function() 2794 e.ssl=function()
1924 return V 2795 return B
1925 end 2796 end
1926 e.sslctx=function() 2797 e.sslctx=function()
1927 return g 2798 return w
1928 end 2799 end
1929 e.send=function(n,o,a,i) 2800 e.ssl_info=function()
1930 return y(t,o,a,i) 2801 return t.info and t:info()
2802 end
2803 e.ssl_peercertificate=function()
2804 if not t.getpeercertificate then return nil,"not-implemented";end
2805 return t:getpeercertificate()
2806 end
2807 e.ssl_peerverification=function()
2808 if not t.getpeerverification then return nil,{{"Chain verification not supported"}};end
2809 return t:getpeerverification();
2810 end
2811 e.ssl_peerfinished=function()
2812 if not t.getpeerfinished then return nil,"not-implemented";end
2813 return t:getpeerfinished();
2814 end
2815 e.send=function(n,i,a,o)
2816 return b(t,i,a,o)
1931 end 2817 end
1932 e.receive=function(o,a) 2818 e.receive=function(o,a)
1933 return _(t,o,a) 2819 return N(t,o,a)
1934 end 2820 end
1935 e.shutdown=function(a) 2821 e.shutdown=function(a)
1936 return q(t,a) 2822 return z(t,a)
1937 end 2823 end
1938 e.setoption=function(i,a,o) 2824 e.setoption=function(i,o,a)
1939 if t.setoption then 2825 if t.setoption then
1940 return t:setoption(a,o); 2826 return t:setoption(o,a);
1941 end 2827 end
1942 return false,"setoption not implemented"; 2828 return false,"setoption not implemented";
1943 end 2829 end
1944 e.force_close=function(t,a) 2830 e.force_close=function(t,a)
1945 if c~=0 then 2831 if m~=0 then
1946 n("server.lua: discarding unwritten data for ",l(z),":",l(A)) 2832 n("server.lua: discarding unwritten data for ",u(H),":",u(L))
1947 c=0; 2833 m=0;
1948 end 2834 end
1949 return t:close(a); 2835 return t:close(a);
1950 end 2836 end
1951 e.close=function(l,d) 2837 e.close=function(l,d)
1952 if not e then return true;end 2838 if not e then return true;end
1953 a=s(i,t,a) 2839 a=h(i,t,a)
1954 v[e]=nil 2840 v[e]=nil
1955 if c~=0 then 2841 if m~=0 then
1956 e.sendbuffer() 2842 e:sendbuffer()
1957 if c~=0 then 2843 if m~=0 then
1958 if e then 2844 if e then
1959 e.write=nil 2845 e.write=nil
1960 end 2846 end
1961 B=true 2847 Z=true
1962 return false 2848 return false
1963 end 2849 end
1964 end 2850 end
1965 if t then 2851 if t then
1966 x=q and q(t) 2852 T=z and z(t)
1967 t:close() 2853 t:close()
1968 o=s(r,t,o) 2854 o=h(r,t,o)
1969 h[t]=nil 2855 s[t]=nil
1970 t=nil 2856 t=nil
1971 else 2857 else
1972 n"server.lua: socket already closed" 2858 n"server.lua: socket already closed"
1973 end 2859 end
1974 if e then 2860 if e then
1975 w[e]=nil 2861 y[e]=nil
1976 T[e]=nil 2862 R[e]=nil
1977 local t=e; 2863 local t=e;
1978 e=nil 2864 e=nil
1979 if k then 2865 if k then
1980 k(t,d or false); 2866 k(t,d or false);
1981 k=nil 2867 k=nil
1982 end 2868 end
1983 end 2869 end
1984 if p then 2870 if q then
1985 p.remove() 2871 q.remove()
1986 end 2872 end
1987 n"server.lua: closed client handler and removed socket from list" 2873 n"server.lua: closed client handler and removed socket from list"
1988 return true 2874 return true
1989 end 2875 end
1990 e.server=function() 2876 e.server=function()
1991 return p 2877 return q
1992 end 2878 end
1993 e.ip=function() 2879 e.ip=function()
1994 return z 2880 return H
1995 end 2881 end
1996 e.serverport=function() 2882 e.serverport=function()
1997 return K 2883 return ee
1998 end 2884 end
1999 e.clientport=function() 2885 e.clientport=function()
2000 return A 2886 return L
2001 end 2887 end
2002 e.port=e.clientport 2888 e.port=e.clientport
2003 local p=function(i,a) 2889 local k=function(i,a)
2004 if not e then return false end 2890 if not e then return false end
2005 m=m+#a 2891 f=f+#a
2006 if m>H then 2892 if f>_ then
2007 T[e]="send buffer exceeded" 2893 R[e]="send buffer exceeded"
2008 e.write=G
2009 return false 2894 return false
2010 elseif t and not r[t]then 2895 elseif not E and t and not r[t]then
2011 o=u(r,t,o) 2896 o=l(r,t,o)
2012 end 2897 end
2013 c=c+1 2898 m=m+1
2014 b[c]=a 2899 g[m]=a
2015 if e then 2900 if e then
2016 w[e]=w[e]or d 2901 y[e]=y[e]or d
2017 end 2902 end
2018 return true 2903 return true
2019 end 2904 end
2020 e.write=p 2905 e.write=k
2021 e.bufferqueue=function(t) 2906 e.bufferqueue=function(t)
2022 return b 2907 return g
2023 end 2908 end
2024 e.socket=function(a) 2909 e.socket=function(a)
2025 return t 2910 return t
2026 end 2911 end
2027 e.set_mode=function(a,t) 2912 e.set_mode=function(a,t)
2028 N=t or N 2913 D=t or D
2029 return N 2914 return D
2030 end 2915 end
2031 e.set_send=function(a,t) 2916 e.set_send=function(a,t)
2032 y=t or y 2917 b=t or b
2033 return y 2918 return b
2034 end 2919 end
2035 e.bufferlen=function(o,a,t) 2920 e.bufferlen=function(o,a,t)
2036 H=t or H 2921 _=t or _
2037 S=a or S 2922 O=a or O
2038 return m,S,H 2923 return f,O,_
2039 end 2924 end
2040 e.lock_read=function(n,o) 2925 e.lock_read=function(a,t)
2041 if o==true then 2926 j("server.lua, lock_read() is deprecated, use pause() and resume()")
2927 if t==true then
2928 return a:pause()
2929 elseif t==false then
2930 return a:resume()
2931 end
2932 return p
2933 end
2934 e.pause=function(o)
2042 local o=a 2935 local o=a
2043 a=s(i,t,a) 2936 a=h(i,t,a)
2044 v[e]=nil 2937 v[e]=nil
2045 if a~=o then 2938 if a~=o then
2046 j=true 2939 p=true
2047 end 2940 end
2048 elseif o==false then 2941 return p;
2049 if j then 2942 end
2050 j=false 2943 e.resume=function(o)
2051 a=u(i,t,a) 2944 if p then
2945 p=false
2946 a=l(i,t,a)
2052 v[e]=d 2947 v[e]=d
2053 end 2948 end
2054 end 2949 return p;
2055 return j 2950 end
2056 end 2951 e.lock=function(t,a)
2057 e.pause=function(t) 2952 j("server.lua, lock() is deprecated")
2058 return t:lock_read(true); 2953 e.lock_read(t,a)
2059 end
2060 e.resume=function(t)
2061 return t:lock_read(false);
2062 end
2063 e.lock=function(i,a)
2064 e.lock_read(a)
2065 if a==true then 2954 if a==true then
2066 e.write=G 2955 e.pause_writes(t)
2956 elseif a==false then
2957 e.resume_writes(t)
2958 end
2959 return p,E
2960 end
2961 e.pause_writes=function(a)
2067 local a=o 2962 local a=o
2068 o=s(r,t,o) 2963 o=h(r,t,o)
2069 w[e]=nil 2964 y[e]=nil
2070 if o~=a then
2071 E=true 2965 E=true
2072 end 2966 end
2073 elseif a==false then 2967 e.resume_writes=function(e)
2074 e.write=p
2075 if E then
2076 E=false 2968 E=false
2077 p("") 2969 if f>0 and t then
2078 end 2970 o=l(r,t,o)
2079 end 2971 end
2080 return j,E 2972 end
2081 end 2973 local v=function()
2082 local p=function() 2974 local o,t,a=N(t,D)
2083 local a,t,o=_(t,N)
2084 if not t or(t=="wantread"or t=="timeout")then 2975 if not t or(t=="wantread"or t=="timeout")then
2085 local o=a or o or"" 2976 local a=o or a or""
2086 local a=#o 2977 local o=#a
2087 if a>S then 2978 if o>O then
2088 e:close("receive buffer exceeded") 2979 e:close("receive buffer exceeded")
2089 return false 2980 return false
2090 end 2981 end
2091 local a=a*ce 2982 local o=o*ie
2092 F=F+a 2983 F=F+o
2093 M=M+a 2984 M=M+o
2094 v[e]=d 2985 v[e]=d
2095 return P(e,o,t) 2986 if x then
2096 else 2987 x=nil
2097 n("server.lua: client ",l(z),":",l(A)," read error: ",l(t)) 2988 if c.onconnect then
2098 x=e and e:force_close(t) 2989 c.onconnect(e)
2990 end
2991 end
2992 return K(e,a,t)
2993 else
2994 n("server.lua: client ",u(H),":",u(L)," read error: ",u(t))
2995 T=e and e:force_close(t)
2099 return false 2996 return false
2100 end 2997 end
2101 end 2998 end
2102 local v=function() 2999 local p=function()
2103 local f,a,h,i,u; 3000 local w,a,i,s,l;
2104 if t then 3001 if t then
2105 i=pe(b,"",1,c) 3002 if x then
2106 f,a,h=y(t,i,1,m) 3003 x=nil
2107 u=(f or h or 0)*ce 3004 if c.onconnect then
2108 W=W+u 3005 c.onconnect(e);
2109 C=C+u 3006 end
2110 for e=c,1,-1 do 3007 end
2111 b[e]=nil 3008 if J then
2112 end 3009 J(e);
2113 else 3010 end
2114 f,a,u=false,"unexpected close",0; 3011 s=ye(g,"",1,m)
2115 end 3012 w,a,i=b(t,s,1,f)
2116 if f then 3013 l=(w or i or 0)*ie
2117 c=0 3014 W=W+l
3015 C=C+l
3016 for e=m,1,-1 do
3017 g[e]=nil
3018 end
3019 else
3020 w,a,l=false,"unexpected close",0;
3021 end
3022 if w then
2118 m=0 3023 m=0
2119 o=s(r,t,o) 3024 f=0
2120 w[e]=nil 3025 o=h(r,t,o)
2121 if D then 3026 y[e]=nil
2122 D(e) 3027 if X then
2123 end 3028 X(e)
2124 x=L and e:starttls(nil) 3029 end
2125 x=B and e:force_close() 3030 T=V and e:starttls(nil)
3031 T=Z and e:force_close()
2126 return true 3032 return true
2127 elseif h and(a=="timeout"or a=="wantwrite")then 3033 elseif i and(a=="timeout"or a=="wantwrite")then
2128 i=we(i,h+1,m) 3034 s=be(s,i+1,f)
2129 b[1]=i 3035 g[1]=s
2130 c=1 3036 m=1
2131 m=m-h 3037 f=f-i
2132 w[e]=d 3038 y[e]=d
2133 return true 3039 return true
2134 else 3040 else
2135 n("server.lua: client ",l(z),":",l(A)," write error: ",l(a)) 3041 n("server.lua: client ",u(H),":",u(L)," write error: ",u(a))
2136 x=e and e:force_close(a) 3042 T=e and e:force_close(a)
2137 return false 3043 return false
2138 end 3044 end
2139 end 3045 end
2140 local d; 3046 local d;
2141 function e.set_sslctx(w,t) 3047 function e.set_sslctx(y,t)
2142 g=t; 3048 w=t;
2143 local m,l 3049 local u,f
2144 d=fe(function(h) 3050 d=pe(function(s)
2145 local t 3051 local t
2146 for d=1,U do 3052 for d=1,A do
2147 o=(l and s(r,h,o))or o 3053 o=(f and h(r,s,o))or o
2148 a=(m and s(i,h,a))or a 3054 a=(u and h(i,s,a))or a
2149 m,l=nil,nil 3055 u,f=nil,nil
2150 d,t=h:dohandshake() 3056 d,t=s:dohandshake()
2151 if not t then 3057 if not t then
2152 n("server.lua: ssl handshake done") 3058 n("server.lua: ssl handshake done")
2153 e.readbuffer=p 3059 e.readbuffer=v
2154 e.sendbuffer=v 3060 e.sendbuffer=p
2155 d=Y and Y(e,"ssl-handshake-complete") 3061 d=G and G(e,"ssl-handshake-complete")
2156 if w.autostart_ssl and f.onconnect then 3062 if y.autostart_ssl and c.onconnect then
2157 f.onconnect(w); 3063 c.onconnect(y);
2158 if c~=0 then 3064 if m~=0 then
2159 o=u(r,h,o) 3065 o=l(r,s,o)
2160 end 3066 end
2161 end 3067 end
2162 a=u(i,h,a) 3068 a=l(i,s,a)
2163 return true 3069 return true
2164 else 3070 else
2165 if t=="wantwrite"then 3071 if t=="wantwrite"then
2166 o=u(r,h,o) 3072 o=l(r,s,o)
2167 l=true 3073 f=true
2168 elseif t=="wantread"then 3074 elseif t=="wantread"then
2169 a=u(i,h,a) 3075 a=l(i,s,a)
2170 m=true 3076 u=true
2171 else 3077 else
2172 break; 3078 break;
2173 end 3079 end
2174 t=nil; 3080 t=nil;
2175 me() 3081 ke()
2176 end 3082 end
2177 end 3083 end
2178 t="ssl handshake error: "..(t or"handshake too long"); 3084 t=(t or"handshake too long");
2179 n("server.lua: ",t); 3085 n("server.lua: ",t);
2180 x=e and e:force_close(t) 3086 T=e and e:force_close(t)
2181 return false,t 3087 return false,t
2182 end 3088 end
2183 ) 3089 )
2184 end 3090 end
2185 if O then 3091 if Y then
2186 e.starttls=function(f,m) 3092 e.starttls=function(f,c)
2187 if m then 3093 if c then
2188 e:set_sslctx(m); 3094 e:set_sslctx(c);
2189 end 3095 end
2190 if c>0 then 3096 if m>0 then
2191 n"server.lua: we need to do tls, but delaying until send buffer empty" 3097 n"server.lua: we need to do tls, but delaying until send buffer empty"
2192 L=true 3098 V=true
2193 return 3099 return
2194 end 3100 end
2195 n("server.lua: attempting to start tls on "..l(t)) 3101 n("server.lua: attempting to start tls on "..u(t))
2196 local c,m=t 3102 local m,c=t
2197 t,m=ye(t,g) 3103 t,c=w:wrap(t)
2198 if not t then 3104 if not t then
2199 n("server.lua: error while starting tls on client: ",l(m or"unknown error")) 3105 n("server.lua: error while starting tls on client: ",u(c or"unknown error"))
2200 return nil,m 3106 return nil,c
3107 end
3108 if t.sni then
3109 if f.servername then
3110 t:sni(f.servername);
3111 elseif next(w._sni_contexts)~=nil then
3112 t:sni(w._sni_contexts,true);
3113 end
2201 end 3114 end
2202 t:settimeout(0) 3115 t:settimeout(0)
2203 y=t.send 3116 b=t.send
2204 _=t.receive 3117 N=t.receive
2205 q=I 3118 z=U
2206 h[t]=e 3119 s[t]=e
2207 a=u(i,t,a) 3120 a=l(i,t,a)
2208 a=s(i,c,a) 3121 a=h(i,m,a)
2209 o=s(r,c,o) 3122 o=h(r,m,o)
2210 h[c]=nil 3123 s[m]=nil
2211 e.starttls=nil 3124 e.starttls=nil
2212 L=nil 3125 V=nil
2213 V=true 3126 B=true
2214 e.readbuffer=d 3127 e.readbuffer=d
2215 e.sendbuffer=d 3128 e.sendbuffer=d
2216 return d(t) 3129 return d(t)
2217 end 3130 end
2218 end 3131 end
2219 e.readbuffer=p 3132 e.readbuffer=v
2220 e.sendbuffer=v 3133 e.sendbuffer=p
2221 y=t.send 3134 b=t.send
2222 _=t.receive 3135 N=t.receive
2223 q=(V and I)or t.shutdown 3136 z=(B and U)or t.shutdown
2224 h[t]=e 3137 s[t]=e
2225 a=u(i,t,a) 3138 a=l(i,t,a)
2226 if g and O then 3139 if w and te and Y then
2227 n"server.lua: auto-starting ssl negotiation..." 3140 n"server.lua: auto-starting ssl negotiation..."
2228 e.autostart_ssl=true; 3141 e.autostart_ssl=true;
2229 local t,e=e:starttls(g); 3142 local t,e=e:starttls(w);
2230 if t==false then 3143 if t==false then
2231 return nil,nil,e 3144 return nil,nil,e
2232 end 3145 end
2233 end 3146 end
2234 return e,t 3147 return e,t
2235 end 3148 end
2236 I=function() 3149 U=function()
2237 end 3150 end
2238 G=function() 3151 me=function()
2239 return false 3152 return false
2240 end 3153 end
2241 u=function(t,a,e) 3154 l=function(a,t,e)
2242 if not t[a]then 3155 if not a[t]then
2243 e=e+1 3156 e=e+1
2244 t[e]=a 3157 a[e]=t
2245 t[a]=e 3158 a[t]=e
2246 end 3159 end
2247 return e; 3160 return e;
2248 end 3161 end
2249 s=function(e,i,t) 3162 h=function(e,i,t)
2250 local a=e[i] 3163 local o=e[i]
2251 if a then 3164 if o then
2252 e[i]=nil 3165 e[i]=nil
2253 local o=e[t] 3166 local a=e[t]
2254 e[t]=nil 3167 e[t]=nil
2255 if o~=i then 3168 if a~=i then
3169 e[a]=o
2256 e[o]=a 3170 e[o]=a
2257 e[a]=o
2258 end 3171 end
2259 return t-1 3172 return t-1
2260 end 3173 end
2261 return t 3174 return t
2262 end 3175 end
2263 Q=function(e) 3176 Q=function(e)
2264 o=s(r,e,o) 3177 o=h(r,e,o)
2265 a=s(i,e,a) 3178 a=h(i,e,a)
2266 h[e]=nil 3179 s[e]=nil
2267 e:close() 3180 e:close()
2268 end 3181 end
2269 local function x(e,t,o) 3182 local function c(e,t,o)
2270 local a; 3183 local a;
2271 local i=t.sendbuffer; 3184 local i=t.sendbuffer;
2272 function t.sendbuffer() 3185 function t.sendbuffer()
2273 i(); 3186 i(t);
2274 if a and t.bufferlen()<o then 3187 if a and t.bufferlen()<o then
2275 e:lock_read(false); 3188 e:lock_read(false);
2276 a=nil; 3189 a=nil;
2277 end 3190 end
2278 end 3191 end
2284 e:lock_read(true); 3197 e:lock_read(true);
2285 end 3198 end
2286 end 3199 end
2287 e:set_mode("*a"); 3200 e:set_mode("*a");
2288 end 3201 end
2289 ie=function(t,e,d,l,r) 3202 J=function(e,t,d,h)
2290 t=t or"*" 3203 e=e or"*"
3204 h=h or{}
2291 local o 3205 local o
2292 if f(d)~="table"then 3206 local r=h.tls_ctx;
3207 local c=h.tls_direct;
3208 local u=h.read_size;
3209 if w(d)~="table"then
2293 o="invalid listener table" 3210 o="invalid listener table"
2294 elseif f(t)~="string"then 3211 elseif w(e)~="string"then
2295 o="invalid address" 3212 o="invalid address"
2296 elseif f(e)~="number"or not(e>=0 and e<=65535)then 3213 elseif w(t)~="number"or not(t>=0 and t<=65535)then
2297 o="invalid port" 3214 o="invalid port"
2298 elseif p[t..":"..e]then 3215 elseif p[e..":"..t]then
2299 o="listeners on '["..t.."]:"..e.."' already exist" 3216 o="listeners on '["..e.."]:"..t.."' already exist"
2300 elseif r and not O then 3217 elseif r and not Y then
2301 o="luasec not found" 3218 o="luasec not found"
2302 end 3219 end
2303 if o then 3220 if o then
2304 E("server.lua, [",t,"]:",e,": ",o) 3221 j("server.lua, [",e,"]:",t,": ",o)
2305 return nil,o 3222 return nil,o
2306 end 3223 end
2307 local o,s=te(t,e,j) 3224 local o,h=ue(e,t,_)
2308 if s then 3225 if h then
2309 E("server.lua, [",t,"]:",e,": ",s) 3226 j("server.lua, [",e,"]:",t,": ",h)
2310 return nil,s 3227 return nil,h
2311 end 3228 end
2312 local s,d=ne(d,o,t,e,l,r) 3229 local h,d=re(d,o,e,t,u,r,c)
2313 if not s then 3230 if not h then
2314 o:close() 3231 o:close()
2315 return nil,d 3232 return nil,d
2316 end 3233 end
2317 o:settimeout(0) 3234 o:settimeout(0)
2318 a=u(i,o,a) 3235 a=l(i,o,a)
2319 p[t..":"..e]=s 3236 p[e..":"..t]=h
2320 h[o]=s 3237 s[o]=h
2321 n("server.lua: new "..(r and"ssl "or"").."server listener on '[",t,"]:",e,"'") 3238 n("server.lua: new "..(r and"ssl "or"").."server listener on '[",e,"]:",t,"'")
2322 return s 3239 return h
2323 end 3240 end
2324 se=function(t,e) 3241 de=function(i,o,t,a,e)
2325 return p[t..":"..e]; 3242 return J(i,o,t,{
2326 end 3243 read_size=a;
2327 le=function(t,e) 3244 tls_ctx=e;
2328 local a=p[t..":"..e] 3245 tls_direct=e and true or false;
3246 });
3247 end
3248 le=function(e,t)
3249 return p[e..":"..t];
3250 end
3251 se=function(e,t)
3252 local a=p[e..":"..t]
2329 if not a then 3253 if not a then
2330 return nil,"no server found on '["..t.."]:"..l(e).."'" 3254 return nil,"no server found on '["..e.."]:"..u(t).."'"
2331 end 3255 end
2332 a:close() 3256 a:close()
2333 p[t..":"..e]=nil 3257 p[e..":"..t]=nil
2334 return true 3258 return true
2335 end 3259 end
2336 J=function() 3260 ee=function()
2337 for e,t in b(h)do 3261 for t,e in q(s)do
2338 t:close() 3262 e:close()
2339 h[e]=nil 3263 s[t]=nil
2340 end 3264 end
2341 a=0 3265 a=0
2342 o=0 3266 o=0
2343 q=0 3267 g=0
2344 p={} 3268 p={}
2345 i={} 3269 i={}
2346 r={} 3270 r={}
2347 P={} 3271 F={}
2348 h={} 3272 s={}
2349 end 3273 end
2350 oe=function() 3274 ne=function()
2351 return{ 3275 return{
2352 select_timeout=A; 3276 select_timeout=D;
2353 tcp_backlog=j; 3277 tcp_backlog=_;
2354 max_send_buffer_size=H; 3278 max_send_buffer_size=S;
2355 max_receive_buffer_size=S; 3279 max_receive_buffer_size=I;
2356 select_idle_check_interval=R; 3280 select_idle_check_interval=O;
2357 send_timeout=D; 3281 send_timeout=N;
2358 read_timeout=L; 3282 read_timeout=L;
2359 max_connections=z; 3283 max_connections=z;
2360 max_ssl_handshake_roundtrips=U; 3284 max_ssl_handshake_roundtrips=A;
2361 highest_allowed_fd=_; 3285 highest_allowed_fd=E;
2362 accept_retry_interval=N; 3286 accept_retry_interval=H;
2363 } 3287 }
2364 end 3288 end
2365 de=function(e) 3289 he=function(e)
2366 if f(e)~="table"then 3290 if w(e)~="table"then
2367 return nil,"invalid settings table" 3291 return nil,"invalid settings table"
2368 end 3292 end
2369 A=y(e.select_timeout)or A 3293 D=b(e.select_timeout)or D
2370 H=y(e.max_send_buffer_size)or H 3294 S=b(e.max_send_buffer_size)or S
2371 S=y(e.max_receive_buffer_size)or S 3295 I=b(e.max_receive_buffer_size)or I
2372 R=y(e.select_idle_check_interval)or R 3296 O=b(e.select_idle_check_interval)or O
2373 j=y(e.tcp_backlog)or j 3297 _=b(e.tcp_backlog)or _
2374 D=y(e.send_timeout)or D 3298 N=b(e.send_timeout)or N
2375 L=y(e.read_timeout)or L 3299 L=b(e.read_timeout)or L
2376 N=y(e.accept_retry_interval)or N 3300 H=b(e.accept_retry_interval)or H
2377 z=e.max_connections or z 3301 z=e.max_connections or z
2378 U=e.max_ssl_handshake_roundtrips or U 3302 A=e.max_ssl_handshake_roundtrips or A
2379 _=e.highest_allowed_fd or _ 3303 E=e.highest_allowed_fd or E
2380 return true 3304 return true
2381 end 3305 end
2382 X=function(e) 3306 K=function(e)
2383 if f(e)~="function"then 3307 if w(e)~="function"then
2384 return nil,"invalid listener function" 3308 return nil,"invalid listener function"
2385 end 3309 end
2386 q=q+1 3310 g=g+1
2387 P[q]=e 3311 F[g]=e
2388 return true 3312 return true
2389 end 3313 end
2390 local l do 3314 local u do
2391 local a={}; 3315 local a={};
2392 local e={}; 3316 local t={};
2393 function l(t,a) 3317 function u(e,a)
2394 local o=F(); 3318 local o=P();
2395 t=t+o; 3319 e=e+o;
2396 if t>=o then 3320 if e>=o then
2397 he(e,{t,a}); 3321 ae(t,{e,a});
2398 else 3322 else
2399 local e=a(o); 3323 local e=a(o);
2400 if e and f(e)=="number"then 3324 if e and w(e)=="number"then
2401 return l(e,a); 3325 return u(e,a);
2402 end 3326 end
2403 end 3327 end
2404 end 3328 end
2405 X(function(t) 3329 K(function(o)
2406 if#e>0 then 3330 if#t>0 then
2407 for o,t in b(e)do 3331 for o,e in q(t)do
2408 he(a,t); 3332 ae(a,e);
2409 end 3333 end
2410 e={}; 3334 t={};
2411 end 3335 end
2412 local e=re; 3336 local e=te;
2413 for n,o in b(a)do 3337 for s,t in q(a)do
2414 local i,o=o[1],o[2]; 3338 local n,i=t[1],t[2];
2415 if i<=t then 3339 if n<=o then
2416 a[n]=nil; 3340 a[s]=nil;
2417 local t=o(t); 3341 local t=i(o);
2418 if f(t)=="number"then 3342 if w(t)=="number"then
2419 l(t,o); 3343 u(t,i);
2420 e=Y(e,t); 3344 e=W(e,t);
2421 end 3345 end
2422 else 3346 else
2423 e=Y(e,i-t); 3347 e=W(e,n-o);
2424 end 3348 end
2425 end 3349 end
2426 return e; 3350 return e;
2427 end); 3351 end);
2428 end 3352 end
2429 ae=function() 3353 ce=function()
2430 return M,C,a,o,q 3354 return M,C,a,o,g
2431 end 3355 end
2432 local e; 3356 local e;
2433 local function p(t) 3357 local function b(t)
2434 e=t; 3358 e=t;
2435 end 3359 end
2436 B=function(t) 3360 Z=function(t)
2437 if e then return"quitting";end 3361 if e then return"quitting";end
2438 if t then e="once";end 3362 if t then e="once";end
2439 d=F() 3363 d=P()
2440 repeat 3364 repeat
2441 local t=re; 3365 local t=te;
2442 for e=1,q do 3366 for e=1,g do
2443 local e=P[e](d) 3367 local e=F[e](d)
2444 if e then t=Y(t,e);end 3368 if e then t=W(t,e);end
2445 end 3369 end
2446 local t,a,o=ve(i,r,Y(A,t)) 3370 local t,a,o=we(i,r,W(D,t))
2447 for t,e in ue(t)do 3371 for t,e in oe(t)do
2448 local t=h[e] 3372 local t=s[e]
2449 if t then 3373 if t then
2450 t.readbuffer() 3374 t:readbuffer()
2451 else 3375 else
2452 Q(e) 3376 Q(e)
2453 n"server.lua: found no handler and closed socket (readlist)" 3377 n"server.lua: found no handler and closed socket (readlist)"
2454 end 3378 end
2455 end 3379 end
2456 for t,e in ue(a)do 3380 for e,t in oe(a)do
2457 local t=h[e] 3381 local e=s[t]
2458 if t then 3382 if e then
2459 t.sendbuffer() 3383 e:sendbuffer()
2460 else 3384 else
2461 Q(e) 3385 Q(t)
2462 n"server.lua: found no handler and closed socket (writelist)" 3386 n"server.lua: found no handler and closed socket (writelist)"
2463 end 3387 end
2464 end 3388 end
2465 for e,t in b(T)do 3389 for e,t in q(R)do
2466 e.disconnect()(e,t) 3390 e.disconnect()(e,t)
2467 e:force_close() 3391 e:force_close()
2468 T[e]=nil; 3392 R[e]=nil;
2469 end 3393 end
2470 d=F() 3394 d=P()
2471 if d-K>R then 3395 if d-B>O then
2472 K=d 3396 B=d
2473 for e,t in b(w)do 3397 for e,t in q(y)do
2474 if d-t>D then 3398 if d-t>N then
2475 e.disconnect()(e,"send timeout") 3399 e.disconnect()(e,"send timeout")
2476 e:force_close() 3400 e:force_close()
2477 end 3401 end
2478 end 3402 end
2479 for e,t in b(v)do 3403 for e,t in q(v)do
2480 if d-t>L then 3404 if d-t>L then
2481 if not(e.onreadtimeout)or e:onreadtimeout()~=true then 3405 if not(e.onreadtimeout)or e:onreadtimeout()~=true then
2482 e.disconnect()(e,"read timeout") 3406 e.disconnect()(e,"read timeout")
2483 e:close() 3407 e:close()
2484 else 3408 else
2485 v[e]=d 3409 v[e]=d
2486 end 3410 end
2487 end 3411 end
2488 end 3412 end
2489 end 3413 end
2490 for e,t in b(k)do 3414 for e,t in q(k)do
2491 if d-t>N then 3415 if d-t>H then
2492 k[e]=nil; 3416 k[e]=nil;
2493 e.resume(); 3417 e.resume();
2494 end 3418 end
2495 end 3419 end
2496 until e; 3420 until e;
2497 if e=="once"then e=nil;return;end 3421 if e=="once"then e=nil;return;end
2498 J(); 3422 ee();
2499 return"quitting" 3423 return"quitting"
2500 end 3424 end
2501 local function k() 3425 local function p()
2502 return B(true); 3426 return Z(true);
2503 end 3427 end
2504 local function y() 3428 local function m()
2505 return"select"; 3429 return"select";
2506 end 3430 end
2507 local c=function(l,e,t,n,d,s) 3431 local d=function(e,t,h,d,c,n,u)
2508 local e,t,d=Z(nil,n,l,e,t,"clientport",d,s) 3432 local t,e,h=G(nil,d,e,t,h,"clientport",c,n,n,u)
2509 if not e then return nil,d end 3433 if not t then return nil,h end
2510 h[t]=e 3434 s[e]=t
2511 if not s then 3435 if not n then
2512 a=u(i,t,a) 3436 t._setpending()
2513 o=u(r,t,o) 3437 a=l(i,e,a)
2514 if n.onconnect then 3438 o=l(r,e,o)
2515 local t=e.sendbuffer; 3439 end
2516 e.sendbuffer=function() 3440 return t,e
2517 e.sendbuffer=t; 3441 end
2518 n.onconnect(e); 3442 local g=function(o,t,i,r,n,a,s)
2519 return t();
2520 end
2521 end
2522 end
2523 return e,t
2524 end
2525 local b=function(a,t,n,s,i,o)
2526 local e 3443 local e
2527 if f(n)~="table"then 3444 if w(i)~="table"then
2528 e="invalid listener table" 3445 e="invalid listener table"
2529 elseif f(a)~="string"then 3446 elseif w(o)~="string"then
2530 e="invalid address" 3447 e="invalid address"
2531 elseif f(t)~="number"or not(t>=0 and t<=65535)then 3448 elseif w(t)~="number"or not(t>=0 and t<=65535)then
2532 e="invalid port" 3449 e="invalid port"
2533 elseif i and not O then 3450 elseif n and not Y then
2534 e="luasec not found" 3451 e="luasec not found"
2535 end 3452 end
2536 if ee and not o then 3453 if not a then
2537 local e,t=ee(a) 3454 local e=ge(o);
2538 if not e then return nil,t end 3455 if not e then return nil,"invalid-ip";end
2539 if e[1]and e[1].family=="inet6"then 3456 if#e==16 then
2540 o="tcp6" 3457 a="tcp6";
2541 end 3458 elseif#e==4 then
2542 end 3459 a="tcp4";
2543 local o=g[o or"tcp"] 3460 end
2544 if f(o)~="function"then 3461 end
3462 local a=x[a];
3463 if w(a)~="function"then
2545 e="invalid socket type" 3464 e="invalid socket type"
2546 end 3465 end
2547 if e then 3466 if e then
2548 E("server.lua, addclient: ",e) 3467 j("server.lua, addclient: ",e)
2549 return nil,e 3468 return nil,e
2550 end 3469 end
2551 local e,o=o() 3470 local e,a=a()
2552 if o then 3471 if a then
2553 return nil,o 3472 return nil,a
2554 end 3473 end
2555 e:settimeout(0) 3474 e:settimeout(0)
2556 local h,o=e:connect(a,t) 3475 local h,a=e:setpeername(o,t)
2557 if h or o=="timeout"or o=="Operation already in progress"then 3476 if h or a=="timeout"or a=="Operation already in progress"then
2558 return c(e,a,t,n,s,i) 3477 return d(e,o,t,i,r,n,s)
2559 else 3478 else
2560 return nil,o 3479 return nil,a
2561 end 3480 end
2562 end 3481 end
2563 local g=function(e) 3482 local k=function(e)
2564 local e=e.conn; 3483 local e=e.conn;
2565 o=s(r,e,o) 3484 o=h(r,e,o)
2566 a=s(i,e,a) 3485 a=h(i,e,a)
2567 h[e]=nil 3486 s[e]=nil
2568 end; 3487 end;
2569 local t=function(n,t,d) 3488 local i=function(d,n,t)
2570 local e=n.conn 3489 local e=d.conn
2571 h[e]=n 3490 s[e]=d
3491 if n~=nil then
3492 if n then
3493 a=l(i,e,a)
3494 else
3495 o=h(r,e,o)
3496 end
3497 end
2572 if t~=nil then 3498 if t~=nil then
2573 if t then 3499 if t then
2574 a=u(i,e,a) 3500 o=l(r,e,o)
2575 else 3501 else
2576 o=s(r,e,o) 3502 a=h(i,e,a)
2577 end
2578 end
2579 if d~=nil then
2580 if d then
2581 o=u(r,e,o)
2582 else
2583 a=s(i,e,a)
2584 end 3503 end
2585 end 3504 end
2586 end 3505 end
2587 local t=function(e,o,a) 3506 local t=function(e,o,a)
2588 local i=e 3507 local t=e
2589 if f(e)=="number"then 3508 if w(e)=="number"then
2590 i={getfd=function()return e;end} 3509 t={getfd=function()return e;end}
2591 end 3510 end
2592 local e={ 3511 local e={
2593 conn=i; 3512 conn=t;
2594 readbuffer=o or I; 3513 readbuffer=o or U;
2595 sendbuffer=a or I; 3514 sendbuffer=a or U;
2596 close=g; 3515 close=k;
2597 setflags=t; 3516 setflags=i;
2598 }; 3517 };
2599 t(e,o,a) 3518 i(e,o,a)
2600 return e 3519 return e
2601 end 3520 end
2602 m"setmetatable"(h,{__mode="k"}) 3521 f"setmetatable"(s,{__mode="k"})
2603 m"setmetatable"(v,{__mode="k"}) 3522 f"setmetatable"(v,{__mode="k"})
2604 m"setmetatable"(w,{__mode="k"}) 3523 f"setmetatable"(y,{__mode="k"})
2605 K=F() 3524 B=P()
2606 local function a(e) 3525 local function a(e)
2607 local t=W; 3526 local t=V;
2608 if e then 3527 if e then
2609 W=e; 3528 V=e;
2610 end 3529 end
2611 return t; 3530 return t;
2612 end 3531 end
2613 return{ 3532 return{
2614 _addtimer=X, 3533 _addtimer=K,
2615 add_task=l; 3534 add_task=u;
2616 addclient=b, 3535 addclient=g,
2617 wrapclient=c, 3536 wrapclient=d,
2618 watchfd=t, 3537 watchfd=t,
2619 loop=B, 3538 loop=Z,
2620 link=x, 3539 link=c,
2621 step=k, 3540 step=p,
2622 stats=ae, 3541 stats=ce,
2623 closeall=J, 3542 closeall=ee,
2624 addserver=ie, 3543 addserver=de,
2625 getserver=se, 3544 listen=J,
3545 getserver=le,
2626 setlogger=a, 3546 setlogger=a,
2627 getsettings=oe, 3547 getsettings=ne,
2628 setquitting=p, 3548 setquitting=b,
2629 removeserver=le, 3549 removeserver=se,
2630 get_backend=y, 3550 get_backend=m,
2631 changesettings=de, 3551 changesettings=he,
3552 tls_builder=function(e)
3553 return ve._new(fe.new_context,e)
3554 end,
2632 } 3555 }
2633 end) 3556 end)
2634 package.preload['util.xmppstream']=(function(...) 3557 package.preload['util.xmppstream']=(function(...)
2635 local _ENV=_ENV; 3558 local _ENV=_ENV;
2636 local function o(t,...) 3559 local function e(t,...)
2637 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 3560 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
2638 package.loaded[t]=e; 3561 package.loaded[t]=e;
2639 for t=1,select("#",...)do 3562 for t=1,select("#",...)do
2640 (select(t,...))(e); 3563 (select(t,...))(e);
2641 end 3564 end
2642 _ENV=e; 3565 _ENV=e;
2643 _M=e; 3566 _M=e;
2644 return e; 3567 return e;
2645 end 3568 end
2646 local e=require"lxp"; 3569 local e=require"lxp";
2647 local t=require"util.stanza"; 3570 local g=require"util.stanza";
2648 local p=t.stanza_mt; 3571 local y=g.stanza_mt;
2649 local f=error; 3572 local w=error;
2650 local t=tostring; 3573 local t=tostring;
2651 local l=table.insert; 3574 local d=table.insert;
2652 local w=table.concat; 3575 local b=table.concat;
2653 local T=table.remove; 3576 local E=table.remove;
2654 local y=setmetatable; 3577 local p=setmetatable;
2655 local x=pcall(e.new,{StartDoctypeDecl=false}); 3578 local T=pcall(e.new,{StartDoctypeDecl=false});
2656 local z=pcall(e.new,{XmlDecl=false}); 3579 local _=pcall(e.new,{XmlDecl=false});
2657 local a=not not e.new({}).getcurrentbytecount; 3580 local a=not not e.new({}).getcurrentbytecount;
2658 local _=1024*1024*10; 3581 local x=1024*1024*1;
2659 o"xmppstream" 3582 local _ENV=nil;
2660 local k=e.new; 3583 local v=e.new;
2661 local E={ 3584 local j={
2662 ["http://www.w3.org/XML/1998/namespace\1lang"]="xml:lang"; 3585 ["http://www.w3.org/XML/1998/namespace\1lang"]="xml:lang";
2663 ["http://www.w3.org/XML/1998/namespace\1space"]="xml:space"; 3586 ["http://www.w3.org/XML/1998/namespace\1space"]="xml:space";
2664 ["http://www.w3.org/XML/1998/namespace\1base"]="xml:base"; 3587 ["http://www.w3.org/XML/1998/namespace\1base"]="xml:base";
2665 ["http://www.w3.org/XML/1998/namespace\1id"]="xml:id"; 3588 ["http://www.w3.org/XML/1998/namespace\1id"]="xml:id";
2666 }; 3589 };
2667 local o="http://etherx.jabber.org/streams"; 3590 local h="http://etherx.jabber.org/streams";
2668 local d="\1"; 3591 local r="\1";
2669 local b="^([^"..d.."]*)"..d.."?(.*)$"; 3592 local k="^([^"..r.."]*)"..r.."?(.*)$";
2670 _M.ns_separator=d; 3593 local function o()end
2671 _M.ns_pattern=b; 3594 local function q(n,e,s)
2672 local function s()end
2673 function new_sax_handlers(n,e,h)
2674 local i={}; 3595 local i={};
2675 local g=e.streamopened; 3596 local g=e.streamopened;
2676 local v=e.streamclosed; 3597 local v=e.streamclosed;
2677 local u=e.error or function(o,a,e)f("XML stream error: "..t(a)..(e and": "..t(e)or""),2);end; 3598 local l=e.error or
2678 local k=e.handlestanza; 3599 function(o,a,e)
2679 h=h or s; 3600 w("XML stream error: "..t(a)..(e and": "..t(e)or""),2);
2680 local t=e.stream_ns or o; 3601 end;
3602 local x=e.handlestanza;
3603 s=s or o;
3604 local t=e.stream_ns or h;
2681 local m=e.stream_tag or"stream"; 3605 local m=e.stream_tag or"stream";
2682 if t~=""then 3606 if t~=""then
2683 m=t..d..m; 3607 m=t..r..m;
2684 end 3608 end
2685 local q=t..d..(e.error_tag or"error"); 3609 local z=t..r..(e.error_tag or"error");
2686 local j=e.default_ns; 3610 local q=e.default_ns;
2687 local d={}; 3611 local f="en";
2688 local s,e={}; 3612 local u={};
3613 local h,e={};
2689 local t=0; 3614 local t=0;
2690 local r=0; 3615 local r=0;
2691 function i:StartElement(c,o) 3616 function i:StartElement(c,o)
2692 if e and#s>0 then 3617 if e and#h>0 then
2693 l(e,w(s)); 3618 d(e,b(h));
2694 s={}; 3619 h={};
2695 end 3620 end
2696 local s,i=c:match(b); 3621 local h,i=c:match(k);
2697 if i==""then 3622 if i==""then
2698 s,i="",s; 3623 h,i="",h;
2699 end 3624 end
2700 if s~=j or r>0 then 3625 if h~=q or r>0 then
2701 o.xmlns=s; 3626 o.xmlns=h;
2702 r=r+1; 3627 r=r+1;
2703 end 3628 end
2704 for t=1,#o do 3629 for t=1,#o do
2705 local e=o[t]; 3630 local e=o[t];
2706 o[t]=nil; 3631 o[t]=nil;
2707 local t=E[e]; 3632 local t=j[e];
2708 if t then 3633 if t then
2709 o[t]=o[e]; 3634 o[t]=o[e];
2710 o[e]=nil; 3635 o[e]=nil;
2711 end 3636 end
2712 end 3637 end
2715 t=self:getcurrentbytecount(); 3640 t=self:getcurrentbytecount();
2716 end 3641 end
2717 if n.notopen then 3642 if n.notopen then
2718 if c==m then 3643 if c==m then
2719 r=0; 3644 r=0;
3645 f=o["xml:lang"]or f;
2720 if g then 3646 if g then
2721 if a then 3647 if a then
2722 h(t); 3648 s(t);
2723 t=0; 3649 t=0;
2724 end 3650 end
2725 g(n,o); 3651 g(n,o);
2726 end 3652 end
2727 else 3653 else
2728 u(n,"no-stream",c); 3654 l(n,"no-stream",c);
2729 end 3655 end
2730 return; 3656 return;
2731 end 3657 end
2732 if s=="jabber:client"and i~="iq"and i~="presence"and i~="message"then 3658 if h=="jabber:client"and i~="iq"and i~="presence"and i~="message"then
2733 u(n,"invalid-top-level-element"); 3659 l(n,"invalid-top-level-element");
2734 end 3660 end
2735 e=y({name=i,attr=o,tags={}},p); 3661 e=p({name=i,attr=o,tags={}},y);
2736 else 3662 else
2737 if a then 3663 if a then
2738 t=t+self:getcurrentbytecount(); 3664 t=t+self:getcurrentbytecount();
2739 end 3665 end
2740 l(d,e); 3666 d(u,e);
2741 local t=e; 3667 local t=e;
2742 e=y({name=i,attr=o,tags={}},p); 3668 e=p({name=i,attr=o,tags={}},y);
2743 l(t,e); 3669 d(t,e);
2744 l(t.tags,e); 3670 d(t.tags,e);
2745 end
2746 end
2747 if z then
2748 function i:XmlDecl(e,e,e)
2749 if a then
2750 h(self:getcurrentbytecount());
2751 end
2752 end 3671 end
2753 end 3672 end
2754 function i:StartCdataSection() 3673 function i:StartCdataSection()
2755 if a then 3674 if a then
2756 if e then 3675 if e then
2757 t=t+self:getcurrentbytecount(); 3676 t=t+self:getcurrentbytecount();
2758 else 3677 else
2759 h(self:getcurrentbytecount()); 3678 s(self:getcurrentbytecount());
2760 end 3679 end
2761 end 3680 end
2762 end 3681 end
2763 function i:EndCdataSection() 3682 function i:EndCdataSection()
2764 if a then 3683 if a then
2765 if e then 3684 if e then
2766 t=t+self:getcurrentbytecount(); 3685 t=t+self:getcurrentbytecount();
2767 else 3686 else
2768 h(self:getcurrentbytecount()); 3687 s(self:getcurrentbytecount());
2769 end 3688 end
2770 end 3689 end
2771 end 3690 end
2772 function i:CharacterData(o) 3691 function i:CharacterData(o)
2773 if e then 3692 if e then
2774 if a then 3693 if a then
2775 t=t+self:getcurrentbytecount(); 3694 t=t+self:getcurrentbytecount();
2776 end 3695 end
2777 l(s,o); 3696 d(h,o);
2778 elseif a then 3697 elseif a then
2779 h(self:getcurrentbytecount()); 3698 s(self:getcurrentbytecount());
2780 end 3699 end
2781 end 3700 end
2782 function i:EndElement(o) 3701 function i:EndElement(o)
2783 if a then 3702 if a then
2784 t=t+self:getcurrentbytecount() 3703 t=t+self:getcurrentbytecount()
2785 end 3704 end
2786 if r>0 then 3705 if r>0 then
2787 r=r-1; 3706 r=r-1;
2788 end 3707 end
2789 if e then 3708 if e then
2790 if#s>0 then 3709 if#h>0 then
2791 l(e,w(s)); 3710 d(e,b(h));
2792 s={}; 3711 h={};
2793 end 3712 end
2794 if#d==0 then 3713 if#u==0 then
2795 if a then 3714 if a then
2796 h(t); 3715 s(t);
2797 end 3716 end
2798 t=0; 3717 t=0;
2799 if o~=q then 3718 if e.attr["xml:lang"]==nil then
2800 k(n,e); 3719 e.attr["xml:lang"]=f;
2801 else 3720 end
2802 u(n,"stream-error",e); 3721 if o~=z then
3722 x(n,e);
3723 else
3724 l(n,"stream-error",e);
2803 end 3725 end
2804 e=nil; 3726 e=nil;
2805 else 3727 else
2806 e=T(d); 3728 e=E(u);
2807 end 3729 end
2808 else 3730 else
3731 if a then
3732 s(t);
3733 end
2809 if v then 3734 if v then
2810 v(n); 3735 v(n);
2811 end 3736 end
2812 end 3737 end
2813 end 3738 end
2814 local function a(e) 3739 local function o(e)
2815 u(n,"parse-error","restricted-xml","Restricted XML, see RFC 6120 section 11.1."); 3740 l(n,"parse-error","restricted-xml","Restricted XML, see RFC 6120 section 11.1.");
2816 if not e.stop or not e:stop()then 3741 if not e.stop or not e:stop()then
2817 f("Failed to abort parsing"); 3742 w("Failed to abort parsing");
2818 end 3743 end
2819 end 3744 end
2820 if x then 3745 if _ then
2821 i.StartDoctypeDecl=a; 3746 function i:XmlDecl(e,t,i)
2822 end 3747 if a then
2823 i.Comment=a; 3748 s(self:getcurrentbytecount());
2824 i.ProcessingInstruction=a; 3749 end
3750 if(t and t:lower()~="utf-8")
3751 or(i=="no")
3752 or(e and e~="1.0")then
3753 return o(self);
3754 end
3755 end
3756 end
3757 if T then
3758 i.StartDoctypeDecl=o;
3759 end
3760 i.Comment=o;
3761 i.ProcessingInstruction=o;
2825 local function a() 3762 local function a()
2826 e,s,t=nil,{},0; 3763 e,h,t=nil,{},0;
2827 d={}; 3764 u={};
2828 end 3765 end
2829 local function e(t,e) 3766 local function e(t,e)
2830 n=e; 3767 n=e;
2831 end 3768 end
2832 return i,{reset=a,set_session=e}; 3769 return i,{reset=a,set_session=e};
2833 end 3770 end
2834 function new(n,i,o) 3771 local function l(e,h,o)
2835 local e=0; 3772 local t=0;
2836 local t; 3773 local i;
2837 if a then 3774 if a then
2838 function t(t) 3775 function i(e)
2839 e=e-t; 3776 t=t-e;
2840 end 3777 end
2841 o=o or _; 3778 o=o or x;
2842 elseif o then 3779 elseif o then
2843 f("Stanza size limits are not supported on this version of LuaExpat") 3780 w("Stanza size limits are not supported on this version of LuaExpat")
2844 end 3781 end
2845 local i,n=new_sax_handlers(n,i,t); 3782 local n,d=q(e,h,i);
2846 local t=k(i,d,false); 3783 local i=v(n,r,false);
2847 local s=t.parse; 3784 local s=i.parse;
3785 function e.open_stream(e,o,a)
3786 local i=e.sends2s or e.send;
3787 local t={
3788 ["xmlns:stream"]="http://etherx.jabber.org/streams",
3789 ["xml:lang"]="en",
3790 xmlns=h.default_ns,
3791 version=e.version and(e.version>0 and"1.0"or nil),
3792 id=e.streamid or"",
3793 from=o or e.host,to=a,
3794 };
3795 if e.stream_attrs then
3796 e:stream_attrs(o,a,t)
3797 end
3798 i("<?xml version='1.0'?>"..g.stanza("stream:stream",t):top_tag());
3799 return true;
3800 end
2848 return{ 3801 return{
2849 reset=function() 3802 reset=function()
2850 t=k(i,d,false); 3803 i=v(n,r,false);
2851 s=t.parse; 3804 s=i.parse;
2852 e=0; 3805 t=0;
2853 n.reset(); 3806 d.reset();
2854 end, 3807 end,
2855 feed=function(n,i) 3808 feed=function(e,n)
2856 if a then 3809 if a then
2857 e=e+#i; 3810 t=t+#n;
2858 end 3811 end
2859 local i,t=s(t,i); 3812 local e=i;
2860 if a and e>o then 3813 local n,s=s(e,n);
3814 if a and t>o then
2861 return nil,"stanza-too-large"; 3815 return nil,"stanza-too-large";
2862 end 3816 end
2863 return i,t; 3817 if i~=e then
3818 e:parse();
3819 e:close();
3820 end
3821 return n,s;
2864 end, 3822 end,
2865 set_session=n.set_session; 3823 set_session=d.set_session;
3824 set_stanza_size_limit=function(t,e)
3825 o=e;
3826 end;
2866 }; 3827 };
2867 end 3828 end
2868 return _M; 3829 return{
3830 ns_separator=r;
3831 ns_pattern=k;
3832 new_sax_handlers=q;
3833 new=l;
3834 };
2869 end) 3835 end)
2870 package.preload['util.jid']=(function(...) 3836 package.preload['util.jid']=(function(...)
2871 local _ENV=_ENV; 3837 local _ENV=_ENV;
2872 local function a(t,...) 3838 local function e(t,...)
2873 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 3839 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
2874 package.loaded[t]=e; 3840 package.loaded[t]=e;
2875 for t=1,select("#",...)do 3841 for t=1,select("#",...)do
2876 (select(t,...))(e); 3842 (select(t,...))(e);
2877 end 3843 end
2878 _ENV=e; 3844 _ENV=e;
2879 _M=e; 3845 _M=e;
2880 return e; 3846 return e;
2881 end 3847 end
2882 local t,s=string.match,string.sub; 3848 local i=select;
2883 local l=require"util.encodings".stringprep.nodeprep; 3849 local a,h=string.match,string.sub;
2884 local d=require"util.encodings".stringprep.nameprep; 3850 local u=require"util.encodings".stringprep.nodeprep;
2885 local r=require"util.encodings".stringprep.resourceprep; 3851 local l=require"util.encodings".stringprep.nameprep;
2886 local n={ 3852 local d=require"util.encodings".stringprep.resourceprep;
3853 local o={
2887 [" "]="\\20";['"']="\\22"; 3854 [" "]="\\20";['"']="\\22";
2888 ["&"]="\\26";["'"]="\\27"; 3855 ["&"]="\\26";["'"]="\\27";
2889 ["/"]="\\2f";[":"]="\\3a"; 3856 ["/"]="\\2f";[":"]="\\3a";
2890 ["<"]="\\3c";[">"]="\\3e"; 3857 ["<"]="\\3c";[">"]="\\3e";
2891 ["@"]="\\40";["\\"]="\\5c"; 3858 ["@"]="\\40";["\\"]="\\5c";
2892 }; 3859 };
2893 local i={}; 3860 local n={};
2894 for e,t in pairs(n)do i[t]=e;end 3861 local s={};
2895 a"jid" 3862 for t,e in pairs(o)do
2896 local function o(e) 3863 n[e]=t;
2897 if not e then return;end 3864 s[e]=e:gsub("\\",o)
2898 local i,a=t(e,"^([^@/]+)@()"); 3865 end
2899 local a,o=t(e,"^([^@/]+)()",a) 3866 local _ENV=nil;
2900 if i and not a then return nil,nil,nil;end 3867 local function t(e)
2901 local t=t(e,"^/(.+)$",o); 3868 if e==nil then return;end
2902 if(not a)or((not t)and#e>=o)then return nil,nil,nil;end 3869 local i,t=a(e,"^([^@/]+)@()");
2903 return i,a,t; 3870 local t,o=a(e,"^([^@/]+)()",t);
2904 end 3871 local a=t and a(e,"^/(.+)$",o);
2905 split=o; 3872 if(t==nil)or((a==nil)and#e>=o)then return nil,nil,nil;end
2906 function bare(e) 3873 return i,t,a;
2907 local t,e=o(e); 3874 end
2908 if t and e then 3875 local function m(e)
3876 local t,e=t(e);
3877 if t~=nil and e~=nil then
2909 return t.."@"..e; 3878 return t.."@"..e;
2910 end 3879 end
2911 return e; 3880 return e;
2912 end 3881 end
2913 local function h(e) 3882 local function r(e,o)
2914 local a,e,t=o(e); 3883 local a,e,t=t(e);
2915 if e then 3884 if e~=nil and e~="."then
2916 if s(e,-1,-1)=="."then 3885 if h(e,-1,-1)=="."then
2917 e=s(e,1,-2); 3886 e=h(e,1,-2);
2918 end 3887 end
2919 e=d(e); 3888 e=l(e,o);
2920 if not e then return;end 3889 if e==nil then return;end
2921 if a then 3890 if a~=nil then
2922 a=l(a); 3891 a=u(a,o);
2923 if not a then return;end 3892 if a==nil then return;end
2924 end 3893 end
2925 if t then 3894 if t~=nil then
2926 t=r(t); 3895 t=d(t,o);
2927 if not t then return;end 3896 if t==nil then return;end
2928 end 3897 end
2929 return a,e,t; 3898 return a,e,t;
2930 end 3899 end
2931 end 3900 end
2932 prepped_split=h; 3901 local function h(t,e,a)
2933 function prep(e) 3902 if e==nil then return end
2934 local a,e,t=h(e); 3903 if t~=nil and a~=nil then
2935 if e then 3904 return t.."@"..e.."/"..a;
2936 if a then 3905 elseif t~=nil then
2937 e=a.."@"..e; 3906 return t.."@"..e;
2938 end 3907 elseif a~=nil then
2939 if t then 3908 return e.."/"..a;
2940 e=e.."/"..t; 3909 end
2941 end 3910 return e;
2942 end 3911 end
2943 return e; 3912 local function c(e,t)
2944 end 3913 local e,a,t=r(e,t);
2945 function join(a,e,t) 3914 return h(e,a,t);
2946 if a and e and t then 3915 end
2947 return a.."@"..e.."/"..t; 3916 local function f(a,e)
2948 elseif a and e then 3917 local i,n,o=t(a);
2949 return a.."@"..e; 3918 local a,t,e=t(e);
2950 elseif e and t then 3919 if(a==nil or a==i)and
2951 return e.."/"..t; 3920 (t==nil or t==n)and
2952 elseif e then 3921 (e==nil or e==o)then
2953 return e;
2954 end
2955 return nil;
2956 end
2957 function compare(t,e)
2958 local n,i,s=o(t);
2959 local a,t,e=o(e);
2960 if((a~=nil and a==n)or a==nil)and
2961 ((t~=nil and t==i)or t==nil)and
2962 ((e~=nil and e==s)or e==nil)then
2963 return true 3922 return true
2964 end 3923 end
2965 return false 3924 return false
2966 end 3925 end
2967 function escape(e)return e and(e:gsub(".",n));end 3926 local function u(e)
2968 function unescape(e)return e and(e:gsub("\\%x%x",i));end 3927 return(i(1,t(e)));
2969 return _M; 3928 end
3929 local function l(e)
3930 return(i(2,t(e)));
3931 end
3932 local function d(e)
3933 return(i(3,t(e)));
3934 end
3935 local function a(e)return e and(e:gsub("\\%x%x",s):gsub("[\"&'/:<>@ ]",o));end
3936 local function o(e)return e and(e:gsub("\\%x%x",n));end
3937 return{
3938 split=t;
3939 bare=m;
3940 prepped_split=r;
3941 join=h;
3942 prep=c;
3943 compare=f;
3944 node=u;
3945 host=l;
3946 resource=d;
3947 escape=a;
3948 unescape=o;
3949 };
2970 end) 3950 end)
2971 package.preload['util.events']=(function(...) 3951 package.preload['util.events']=(function(...)
2972 local _ENV=_ENV; 3952 local _ENV=_ENV;
2973 local function a(t,...) 3953 local function e(t,...)
2974 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 3954 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
2975 package.loaded[t]=e; 3955 package.loaded[t]=e;
2976 for t=1,select("#",...)do 3956 for t=1,select("#",...)do
2977 (select(t,...))(e); 3957 (select(t,...))(e);
2978 end 3958 end
2979 _ENV=e; 3959 _ENV=e;
2980 _M=e; 3960 _M=e;
2981 return e; 3961 return e;
2982 end 3962 end
2983 local i=pairs; 3963 local a=pairs;
2984 local r=table.insert; 3964 local d=table.insert;
2985 local s=table.sort; 3965 local w=table.remove;
2986 local h=setmetatable; 3966 local u=table.sort;
2987 local n=next; 3967 local l=setmetatable;
2988 a"events" 3968 local h=next;
2989 function new() 3969 local _ENV=nil;
3970 local function f()
3971 local o={};
3972 local t;
3973 local n={};
3974 local i={};
3975 local s=nil;
3976 local function r(n,o)
3977 local e=i[o];
3978 if not e or h(e)==nil then return;end
2990 local t={}; 3979 local t={};
2991 local e={}; 3980 for e in a(e)do
2992 local function o(o,a) 3981 d(t,e);
2993 local e=e[a]; 3982 end
2994 if not e or n(e)==nil then return;end 3983 u(t,function(a,t)return e[a]>e[t];end);
2995 local t={}; 3984 n[o]=t;
2996 for e in i(e)do
2997 r(t,e);
2998 end
2999 s(t,function(a,t)return e[a]>e[t];end);
3000 o[a]=t;
3001 return t; 3985 return t;
3002 end; 3986 end;
3003 h(t,{__index=o}); 3987 l(o,{__index=r});
3004 local function h(o,i,n) 3988 local function l(t,n,a)
3005 local a=e[o]; 3989 local e=i[t];
3006 if a then 3990 if e then
3007 a[i]=n or 0; 3991 e[n]=a or 0;
3008 else 3992 else
3009 a={[i]=n or 0}; 3993 e={[n]=a or 0};
3010 e[o]=a; 3994 i[t]=e;
3011 end 3995 end
3012 t[o]=nil; 3996 o[t]=nil;
3013 end; 3997 end;
3014 local function s(o,i) 3998 local function d(e,a)
3015 local a=e[o]; 3999 local t=i[e];
3016 if a then 4000 if t then
3017 a[i]=nil; 4001 t[a]=nil;
3018 t[o]=nil; 4002 o[e]=nil;
3019 if n(a)==nil then 4003 if h(t)==nil then
3020 e[o]=nil; 4004 i[e]=nil;
3021 end 4005 end
3022 end 4006 end
3023 end; 4007 end;
3024 local function n(e) 4008 local function c(e)
3025 for t,e in i(e)do 4009 return o[e];
3026 h(t,e);
3027 end
3028 end; 4010 end;
3029 local function a(e) 4011 local function u(e)
3030 for t,e in i(e)do 4012 for e,t in a(e)do
3031 s(t,e); 4013 l(e,t);
3032 end 4014 end
3033 end; 4015 end;
3034 local function o(e,...) 4016 local function m(e)
3035 local e=t[e]; 4017 for t,e in a(e)do
4018 d(t,e);
4019 end
4020 end;
4021 local function r(a,t)
4022 local e=o[a];
4023 if e and not s then
4024 for a=1,#e do
4025 local e=e[a](t);
4026 if e~=nil then return e;end
4027 end
4028 elseif e and s then
4029 for o=1,#e do
4030 local e=s(e[o],a,t);
4031 if e~=nil then return e;end
4032 end
4033 end
4034 end;
4035 local function f(s,h)
4036 local e=n[s]or t;
3036 if e then 4037 if e then
3037 for t=1,#e do 4038 local a=#e;
3038 local e=e[t](...); 4039 local function i(o,n)
3039 if e~=nil then return e;end 4040 a=a-1;
3040 end 4041 if a==0 then
3041 end 4042 if t==nil or e==t then
3042 end; 4043 return r(o,n);
4044 end
4045 e,a=t,#t;
4046 return e[a](i,o,n);
4047 else
4048 return e[a](i,o,n);
4049 end
4050 end
4051 return e[a](i,s,h);
4052 end
4053 return r(s,h);
4054 end
4055 local function r(a,o)
4056 local e;
4057 if a==false then
4058 e=t;
4059 if not e then
4060 e={};
4061 t=e;
4062 end
4063 else
4064 e=n[a];
4065 if not e then
4066 e={};
4067 n[a]=e;
4068 end
4069 end
4070 e[#e+1]=o;
4071 end
4072 local function h(a,o)
4073 local e;
4074 if a==false then
4075 e=t;
4076 else
4077 e=n[a];
4078 end
4079 if not e then return;end
4080 for t=#e,1,-1 do
4081 if e[t]==o then
4082 w(e,t);
4083 end
4084 end
4085 if#e==0 then
4086 if a==false then
4087 t=nil;
4088 else
4089 n[a]=nil;
4090 end
4091 end
4092 end
4093 local function a(t)
4094 local e=s;
4095 s=t;
4096 return e;
4097 end
3043 return{ 4098 return{
3044 add_handler=h; 4099 add_handler=l;
3045 remove_handler=s; 4100 remove_handler=d;
3046 add_handlers=n; 4101 add_handlers=u;
3047 remove_handlers=a; 4102 remove_handlers=m;
3048 fire_event=o; 4103 get_handlers=c;
3049 _handlers=t; 4104 wrappers={
3050 _event_map=e; 4105 add_handler=r;
4106 remove_handler=h;
3051 }; 4107 };
3052 end 4108 add_wrapper=r;
3053 return _M; 4109 remove_wrapper=h;
4110 set_debug_hook=a;
4111 fire_event=f;
4112 _handlers=o;
4113 _event_map=i;
4114 };
4115 end
4116 return{
4117 new=f;
4118 };
3054 end) 4119 end)
3055 package.preload['util.dataforms']=(function(...) 4120 package.preload['util.dataforms']=(function(...)
3056 local _ENV=_ENV; 4121 local _ENV=_ENV;
3057 local function o(t,...) 4122 local function e(t,...)
3058 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 4123 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
3059 package.loaded[t]=e; 4124 package.loaded[t]=e;
3060 for t=1,select("#",...)do 4125 for t=1,select("#",...)do
3061 (select(t,...))(e); 4126 (select(t,...))(e);
3062 end 4127 end
3063 _ENV=e; 4128 _ENV=e;
3064 _M=e; 4129 _M=e;
3065 return e; 4130 return e;
3066 end 4131 end
3067 local a=setmetatable; 4132 local e=setmetatable;
3068 local e,i=pairs,ipairs; 4133 local n=ipairs;
3069 local h,r,c=tostring,type,next; 4134 local s,p=type,next;
3070 local s=table.concat; 4135 local u=tonumber;
3071 local u=require"util.stanza"; 4136 local r=tostring;
3072 local d=require"util.jid".prep; 4137 local l=table.concat;
3073 o"dataforms" 4138 local c=require"util.stanza";
3074 local l='jabber:x:data'; 4139 local m=require"util.jid".prep;
3075 local n={}; 4140 local _ENV=nil;
3076 local e={__index=n}; 4141 local y='jabber:x:data';
3077 function new(t) 4142 local f='http://jabber.org/protocol/xdata-validate';
3078 return a(t,e); 4143 local d={};
3079 end 4144 local t={__index=d};
3080 function from_stanza(e) 4145 local function w(a)
4146 return e(a,t);
4147 end
4148 local function v(e)
3081 local o={ 4149 local o={
3082 title=e:get_child_text("title"); 4150 title=e:get_child_text("title");
3083 instructions=e:get_child_text("instructions"); 4151 instructions=e:get_child_text("instructions");
3084 }; 4152 };
3085 for t in e:childtags("field")do 4153 for t in e:childtags("field")do
3086 local a={ 4154 local e={
3087 name=t.attr.var; 4155 name=t.attr.var;
3088 label=t.attr.label; 4156 label=t.attr.label;
3089 type=t.attr.type; 4157 type=t.attr.type;
3090 required=t:get_child("required")and true or nil; 4158 required=t:get_child("required")and true or nil;
3091 value=t:get_child_text("value"); 4159 value=t:get_child_text("value");
3092 }; 4160 };
3093 o[#o+1]=a; 4161 o[#o+1]=e;
3094 if a.type then 4162 if e.type then
3095 local e={}; 4163 local a={};
3096 if a.type:match"list%-"then 4164 if e.type:match"list%-"then
3097 for t in t:childtags("option")do 4165 for e in t:childtags("option")do
3098 e[#e+1]={label=t.attr.label,value=t:get_child_text("value")}; 4166 a[#a+1]={label=e.attr.label,value=e:get_child_text("value")};
3099 end 4167 end
3100 for t in t:childtags("value")do 4168 for e in t:childtags("value")do
3101 e[#e+1]={label=t.attr.label,value=t:get_text(),default=true}; 4169 a[#a+1]={label=e.attr.label,value=e:get_text(),default=true};
3102 end 4170 end
3103 elseif a.type:match"%-multi"then 4171 elseif e.type:match"%-multi"then
3104 for t in t:childtags("value")do 4172 for e in t:childtags("value")do
3105 e[#e+1]=t.attr.label and{label=t.attr.label,value=t:get_text()}or t:get_text(); 4173 a[#a+1]=e.attr.label and{label=e.attr.label,value=e:get_text()}or e:get_text();
3106 end 4174 end
3107 if a.type=="text-multi"then 4175 if e.type=="text-multi"then
3108 a.value=s(e,"\n"); 4176 e.value=l(a,"\n");
3109 else 4177 else
3110 a.value=e; 4178 e.value=a;
3111 end 4179 end
3112 end 4180 end
3113 end 4181 end
3114 end 4182 local t=t:get_child("validate",f);
3115 return new(o); 4183 if t then
3116 end 4184 e.datatype=datatype.attr.datatype;
3117 function n.form(t,n,e) 4185 local t=t:get_child("range");
3118 local e=u.stanza("x",{xmlns=l,type=e or"form"}); 4186 if t then
4187 e.range_min=u(t.attr.min);
4188 e.range_max=u(t.attr.max);
4189 end
4190 end
4191 end
4192 return w(o);
4193 end
4194 function d.form(t,h,i)
4195 if not i then i="form"end
4196 local e=c.stanza("x",{xmlns=y,type=i});
4197 if i=="cancel"then
4198 return e;
4199 end
4200 if i~="submit"then
3119 if t.title then 4201 if t.title then
3120 e:tag("title"):text(t.title):up(); 4202 e:tag("title"):text(t.title):up();
3121 end 4203 end
3122 if t.instructions then 4204 if t.instructions then
3123 e:tag("instructions"):text(t.instructions):up(); 4205 e:tag("instructions"):text(t.instructions):up();
3124 end 4206 end
3125 for t,o in i(t)do 4207 end
3126 local a=o.type or"text-single"; 4208 for t,a in n(t)do
3127 e:tag("field",{type=a,var=o.name,label=o.label}); 4209 local o=a.type or"text-single";
3128 local t=(n and n[o.name])or o.value; 4210 e:tag("field",{type=o,var=a.var or a.name,label=i~="submit"and a.label or nil});
3129 if t then 4211 if i~="submit"then
3130 if a=="hidden"then 4212 if a.desc then
3131 if r(t)=="table"then 4213 e:text_tag("desc",a.desc);
4214 end
4215 end
4216 if i=="form"and a.datatype then
4217 e:tag("validate",{xmlns=f,datatype=a.datatype});
4218 if a.range_min or a.range_max then
4219 e:tag("range",{
4220 min=a.range_min and r(a.range_min),
4221 max=a.range_max and r(a.range_max),
4222 }):up();
4223 end
4224 e:up();
4225 end
4226 local t=a.value;
4227 local r=a.options;
4228 if h and h[a.name]~=nil then
4229 t=h[a.name];
4230 if i=="form"and s(t)=="table"
4231 and(o=="list-single"or o=="list-multi")then
4232 r,t=t,nil;
4233 end
4234 end
4235 if i=="form"and r then
4236 local a={};
4237 for o,t in n(r)do
4238 if s(t)=="table"then
4239 e:tag("option",{label=t.label}):tag("value"):text(t.value):up():up();
4240 if t.default then
4241 a[#a+1]=t.value;
4242 end
4243 else
4244 e:tag("option",{label=t}):tag("value"):text(t):up():up();
4245 end
4246 end
4247 if not t then
4248 if o=="list-single"then
4249 t=a[1];
4250 elseif o=="list-multi"then
4251 t=a;
4252 end
4253 end
4254 end
4255 if t~=nil then
4256 if s(t)=="number"then
4257 t=("%g"):format(t);
4258 end
4259 if o=="hidden"then
4260 if s(t)=="table"then
3132 e:tag("value") 4261 e:tag("value")
3133 :add_child(t) 4262 :add_child(t)
3134 :up(); 4263 :up();
3135 else 4264 else
3136 e:tag("value"):text(h(t)):up(); 4265 e:tag("value"):text(t):up();
3137 end 4266 end
3138 elseif a=="boolean"then 4267 elseif o=="boolean"then
3139 e:tag("value"):text((t and"1")or"0"):up(); 4268 e:tag("value"):text((t and"1")or"0"):up();
3140 elseif a=="fixed"then 4269 elseif o=="fixed"then
3141 elseif a=="jid-multi"then
3142 for a,t in i(t)do
3143 e:tag("value"):text(t):up(); 4270 e:tag("value"):text(t):up();
3144 end 4271 elseif o=="jid-multi"then
3145 elseif a=="jid-single"then 4272 for a,t in n(t)do
3146 e:tag("value"):text(t):up(); 4273 e:tag("value"):text(t):up();
3147 elseif a=="text-single"or a=="text-private"then 4274 end
4275 elseif o=="jid-single"then
3148 e:tag("value"):text(t):up(); 4276 e:tag("value"):text(t):up();
3149 elseif a=="text-multi"then 4277 elseif o=="text-single"or o=="text-private"then
4278 e:tag("value"):text(t):up();
4279 elseif o=="text-multi"then
3150 for t in t:gmatch("([^\r\n]+)\r?\n*")do 4280 for t in t:gmatch("([^\r\n]+)\r?\n*")do
3151 e:tag("value"):text(t):up(); 4281 e:tag("value"):text(t):up();
3152 end 4282 end
3153 elseif a=="list-single"then 4283 elseif o=="list-single"then
3154 local a=false; 4284 e:tag("value"):text(t):up();
3155 for o,t in i(t)do 4285 elseif o=="list-multi"then
3156 if r(t)=="table"then 4286 for a,t in n(t)do
3157 e:tag("option",{label=t.label}):tag("value"):text(t.value):up():up(); 4287 e:tag("value"):text(t):up();
3158 if t.default and(not a)then 4288 end
3159 e:tag("value"):text(t.value):up(); 4289 end
3160 a=true; 4290 end
3161 end 4291 local t=a.media;
3162 else 4292 if t then
3163 e:tag("option",{label=t}):tag("value"):text(h(t)):up():up(); 4293 e:tag("media",{xmlns="urn:xmpp:media-element",height=("%g"):format(t.height),width=("%g"):format(t.width)});
3164 end 4294 for a,t in n(t)do
3165 end 4295 e:tag("uri",{type=t.type}):text(t.uri):up()
3166 elseif a=="list-multi"then 4296 end
3167 for a,t in i(t)do 4297 e:up();
3168 if r(t)=="table"then 4298 end
3169 e:tag("option",{label=t.label}):tag("value"):text(t.value):up():up(); 4299 if i=="form"and a.required then
3170 if t.default then
3171 e:tag("value"):text(t.value):up();
3172 end
3173 else
3174 e:tag("option",{label=t}):tag("value"):text(h(t)):up():up();
3175 end
3176 end
3177 end
3178 end
3179 if o.required then
3180 e:tag("required"):up(); 4300 e:tag("required"):up();
3181 end 4301 end
3182 e:up(); 4302 e:up();
3183 end 4303 end
3184 return e; 4304 return e;
3185 end 4305 end
3186 local e={}; 4306 local t={};
3187 function n.data(t,n) 4307 local h={};
4308 function d.data(e,r,i)
3188 local o={}; 4309 local o={};
3189 local a={}; 4310 local a={};
3190 for i,t in i(t)do 4311 local s={};
3191 local i; 4312 for n,e in n(e)do
3192 for e in n:childtags()do 4313 local n;
3193 if t.name==e.attr.var then 4314 for t in r:childtags("field")do
3194 i=e; 4315 if(e.var or e.name)==t.attr.var then
4316 n=t;
3195 break; 4317 break;
3196 end 4318 end
3197 end 4319 end
3198 if not i then 4320 if not n then
3199 if t.required then 4321 if i and i[e.name]~=nil then
3200 a[t.name]="Required value missing"; 4322 o[e.name]=i[e.name];
3201 end 4323 elseif e.required then
3202 else 4324 a[e.name]="Required value missing";
3203 local e=e[t.type]; 4325 end
3204 if e then 4326 elseif e.name then
3205 o[t.name],a[t.name]=e(i,t.required); 4327 s[e.name]=true;
3206 end 4328 local t=t[e.type];
3207 end 4329 if t then
3208 end 4330 local t,i=t(n,e.required);
3209 if c(a)then 4331 local n=e.datatype and h[e.datatype];
3210 return o,a; 4332 if t~=nil and n then
3211 end 4333 local o,a=n(t,e);
3212 return o; 4334 if o then
3213 end 4335 t=a;
3214 e["text-single"]= 4336 else
3215 function(t,a) 4337 t,i=nil,a or("Invalid value for data of type "..e.datatype);
3216 local t=t:get_child_text("value"); 4338 end
3217 if t and#t>0 then 4339 end
3218 return t 4340 o[e.name],a[e.name]=t,i;
4341 end
4342 end
4343 end
4344 if p(a)then
4345 return o,a,s;
4346 end
4347 return o,nil,s;
4348 end
4349 local function e(e,a)
4350 local e=e:get_child_text("value");
4351 if a and(e==nil or e=="")then
4352 return nil,"Required value missing";
4353 end
4354 return e;
4355 end
4356 t["text-single"]=e;
4357 t["text-private"]=e;
4358 t["jid-single"]=
4359 function(o,a)
4360 local e,a=e(o,a);
4361 if not e then return e,a;end
4362 local a=m(e);
4363 if not a then
4364 return nil,"Invalid JID: "..e;
4365 end
4366 return a;
4367 end
4368 t["jid-multi"]=
4369 function(o,i)
4370 local a={};
4371 local e={};
4372 for t in o:childtags("value")do
4373 local t=t:get_text();
4374 local o=m(t);
4375 a[#a+1]=o;
4376 if t and not o then
4377 e[#e+1]=("Invalid JID: "..t);
4378 end
4379 end
4380 if#a>0 then
4381 return a,(#e>0 and l(e,"\n")or nil);
4382 elseif i then
4383 return nil,"Required value missing";
4384 end
4385 end
4386 t["list-multi"]=
4387 function(o,a)
4388 local e={};
4389 for t in o:childtags("value")do
4390 e[#e+1]=t:get_text();
4391 end
4392 if#e>0 then
4393 return e;
3219 elseif a then 4394 elseif a then
3220 return nil,"Required value missing"; 4395 return nil,"Required value missing";
3221 end 4396 end
3222 end 4397 end
3223 e["text-private"]= 4398 t["text-multi"]=
3224 e["text-single"]; 4399 function(a,e)
3225 e["jid-single"]= 4400 local e,a=t["list-multi"](a,e);
3226 function(t,o) 4401 if e then
3227 local t=t:get_child_text("value") 4402 e=l(e,"\n");
3228 local a=d(t); 4403 end
3229 if a and#a>0 then 4404 return e,a;
3230 return a 4405 end
3231 elseif t then 4406 t["list-single"]=e;
3232 return nil,"Invalid JID: "..t;
3233 elseif o then
3234 return nil,"Required value missing";
3235 end
3236 end
3237 e["jid-multi"]=
3238 function(o,i)
3239 local a={};
3240 local t={};
3241 for e in o:childtags("value")do
3242 local e=e:get_text();
3243 local o=d(e);
3244 a[#a+1]=o;
3245 if e and not o then
3246 t[#t+1]=("Invalid JID: "..e);
3247 end
3248 end
3249 if#a>0 then
3250 return a,(#t>0 and s(t,"\n")or nil);
3251 elseif i then
3252 return nil,"Required value missing";
3253 end
3254 end
3255 e["list-multi"]=
3256 function(o,a)
3257 local t={};
3258 for e in o:childtags("value")do
3259 t[#t+1]=e:get_text();
3260 end
3261 return t,(a and#t==0 and"Required value missing"or nil);
3262 end
3263 e["text-multi"]=
3264 function(t,a)
3265 local t,a=e["list-multi"](t,a);
3266 if t then
3267 t=s(t,"\n");
3268 end
3269 return t,a;
3270 end
3271 e["list-single"]=
3272 e["text-single"];
3273 local a={ 4407 local a={
3274 ["1"]=true,["true"]=true, 4408 ["1"]=true,["true"]=true,
3275 ["0"]=false,["false"]=false, 4409 ["0"]=false,["false"]=false,
3276 }; 4410 };
3277 e["boolean"]= 4411 t["boolean"]=
3278 function(t,o) 4412 function(o,i)
3279 local t=t:get_child_text("value"); 4413 local e,o=e(o,i);
3280 local a=a[t~=nil and t]; 4414 if not e then return e,o;end
3281 if a~=nil then 4415 local a=a[e];
4416 if a==nil then
4417 return nil,"Invalid boolean representation:"..e;
4418 end
3282 return a; 4419 return a;
3283 elseif t then 4420 end
3284 return nil,"Invalid boolean representation"; 4421 t["hidden"]=
3285 elseif o then
3286 return nil,"Required value missing";
3287 end
3288 end
3289 e["hidden"]=
3290 function(e) 4422 function(e)
3291 return e:get_child_text("value"); 4423 return e:get_child_text("value");
3292 end 4424 end
3293 return _M; 4425 h["xs:integer"]=
4426 function(e,t)
4427 local e=u(e);
4428 if not e then
4429 return false,"not a number";
4430 elseif e%1~=0 then
4431 return false,"not an integer";
4432 end
4433 if t.range_max and e>t.range_max then
4434 return false,"out of bounds";
4435 elseif t.range_min and e<t.range_min then
4436 return false,"out of bounds";
4437 end
4438 return true,e;
4439 end
4440 local function t(e)
4441 if not c.is_stanza(e)then
4442 return nil,"not a stanza object";
4443 elseif e.attr.xmlns~="jabber:x:data"or e.name~="x"then
4444 return nil,"not a dataform element";
4445 end
4446 for e in e:childtags("field")do
4447 if e.attr.var=="FORM_TYPE"then
4448 return e:get_child_text("value");
4449 end
4450 end
4451 return"";
4452 end
4453 return{
4454 new=w;
4455 from_stanza=v;
4456 get_type=t;
4457 };
3294 end) 4458 end)
3295 package.preload['util.caps']=(function(...) 4459 package.preload['util.caps']=(function(...)
3296 local _ENV=_ENV; 4460 local _ENV=_ENV;
3297 local function a(t,...) 4461 local function e(t,...)
3298 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 4462 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
3299 package.loaded[t]=e; 4463 package.loaded[t]=e;
3300 for t=1,select("#",...)do 4464 for t=1,select("#",...)do
3301 (select(t,...))(e); 4465 (select(t,...))(e);
3302 end 4466 end
3303 _ENV=e; 4467 _ENV=e;
3304 _M=e; 4468 _M=e;
3305 return e; 4469 return e;
3306 end 4470 end
3307 local l=require"util.encodings".base64.encode; 4471 local l=require"util.encodings".base64.encode;
3308 local d=require"util.hashes".sha1; 4472 local u=require"util.hashes".sha1;
3309 local n,s,h=table.insert,table.sort,table.concat; 4473 local n,s,h=table.insert,table.sort,table.concat;
3310 local r=ipairs; 4474 local r=ipairs;
3311 a"caps" 4475 local _ENV=nil;
3312 function calculate_hash(e) 4476 local function d(e)
3313 local a,o,i={},{},{}; 4477 local i,o,a={},{},{};
3314 for t,e in r(e)do 4478 for t,e in r(e)do
3315 if e.name=="identity"then 4479 if e.name=="identity"then
3316 n(a,(e.attr.category or"").."\0"..(e.attr.type or"").."\0"..(e.attr["xml:lang"]or"").."\0"..(e.attr.name or"")); 4480 n(i,(e.attr.category or"").."\0"..(e.attr.type or"").."\0"..(e.attr["xml:lang"]or"").."\0"..(e.attr.name or""));
3317 elseif e.name=="feature"then 4481 elseif e.name=="feature"then
3318 n(o,e.attr.var or""); 4482 n(o,e.attr.var or"");
3319 elseif e.name=="x"and e.attr.xmlns=="jabber:x:data"then 4483 elseif e.name=="x"and e.attr.xmlns=="jabber:x:data"then
3320 local t={}; 4484 local t={};
3321 local o; 4485 local o;
3337 end 4501 end
3338 end 4502 end
3339 s(t); 4503 s(t);
3340 t=h(t,"<"); 4504 t=h(t,"<");
3341 if o then t=o.."\0"..t;end 4505 if o then t=o.."\0"..t;end
3342 n(i,t); 4506 n(a,t);
3343 end 4507 end
3344 end 4508 end
4509 s(i);
4510 s(o);
3345 s(a); 4511 s(a);
3346 s(o); 4512 if#i>0 then i=h(i,"<"):gsub("%z","/").."<";else i="";end
3347 s(i);
3348 if#a>0 then a=h(a,"<"):gsub("%z","/").."<";else a="";end
3349 if#o>0 then o=h(o,"<").."<";else o="";end 4513 if#o>0 then o=h(o,"<").."<";else o="";end
3350 if#i>0 then i=h(i,"<"):gsub("%z","<").."<";else i="";end 4514 if#a>0 then a=h(a,"<"):gsub("%z","<").."<";else a="";end
3351 local e=a..o..i; 4515 local e=i..o..a;
3352 local t=l(d(e)); 4516 local t=l(u(e));
3353 print(("CAPS %q %q"):format(t,e));
3354 return t,e; 4517 return t,e;
3355 end 4518 end
3356 return _M; 4519 return{
4520 calculate_hash=d;
4521 };
3357 end) 4522 end)
3358 package.preload['util.vcard']=(function(...) 4523 package.preload['util.vcard']=(function(...)
3359 local _ENV=_ENV; 4524 local _ENV=_ENV;
3360 local function e(t,...) 4525 local function e(t,...)
3361 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 4526 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
3365 end 4530 end
3366 _ENV=e; 4531 _ENV=e;
3367 _M=e; 4532 _M=e;
3368 return e; 4533 return e;
3369 end 4534 end
3370 local i=require"util.stanza"; 4535 local o=require"util.stanza";
3371 local a,h=table.insert,table.concat; 4536 local a,c=table.insert,table.concat;
3372 local s=type; 4537 local h=type;
3373 local e,n,c=next,pairs,ipairs; 4538 local e,s,m=next,pairs,ipairs;
3374 local d,r,l,u; 4539 local u,l,d,r;
3375 local m="\n"; 4540 local f="\n";
3376 local o; 4541 local i;
3377 local function e() 4542 local function e()
3378 error"Not implemented" 4543 error"Not implemented"
3379 end 4544 end
3380 local function e() 4545 local function e()
3381 error"Not implemented" 4546 error"Not implemented"
3382 end 4547 end
4548 local function w(e)
4549 return e:gsub("[,:;\\]","\\%1"):gsub("\n","\\n");
4550 end
3383 local function y(e) 4551 local function y(e)
3384 return e:gsub("[,:;\\]","\\%1"):gsub("\n","\\n");
3385 end
3386 local function p(e)
3387 return e:gsub("\\?[\\nt:;,]",{ 4552 return e:gsub("\\?[\\nt:;,]",{
3388 ["\\\\"]="\\", 4553 ["\\\\"]="\\",
3389 ["\\n"]="\n", 4554 ["\\n"]="\n",
3390 ["\\r"]="\r", 4555 ["\\r"]="\r",
3391 ["\\t"]="\t", 4556 ["\\t"]="\t",
3395 [":"]="\29", 4560 [":"]="\29",
3396 [";"]="\30", 4561 [";"]="\30",
3397 [","]="\31", 4562 [","]="\31",
3398 }); 4563 });
3399 end 4564 end
3400 local function w(e) 4565 local function p(t)
3401 local a=i.stanza(e.name,{xmlns="vcard-temp"}); 4566 local a=o.stanza(t.name,{xmlns="vcard-temp"});
3402 local t=o[e.name]; 4567 local e=i[t.name];
3403 if t=="text"then 4568 if e=="text"then
3404 a:text(e[1]); 4569 a:text(t[1]);
3405 elseif s(t)=="table"then 4570 elseif h(e)=="table"then
3406 if t.types and e.TYPE then 4571 if e.types and t.TYPE then
3407 if s(e.TYPE)=="table"then 4572 if h(t.TYPE)=="table"then
3408 for o,t in n(t.types)do 4573 for o,e in s(e.types)do
3409 for o,e in n(e.TYPE)do 4574 for o,t in s(t.TYPE)do
3410 if e:upper()==t then 4575 if t:upper()==e then
3411 a:tag(t):up(); 4576 a:tag(e):up();
3412 break; 4577 break;
3413 end 4578 end
3414 end 4579 end
3415 end 4580 end
3416 else 4581 else
3417 a:tag(e.TYPE:upper()):up(); 4582 a:tag(t.TYPE:upper()):up();
3418 end 4583 end
3419 end 4584 end
3420 if t.props then 4585 if e.props then
3421 for o,t in n(t.props)do 4586 for o,e in s(e.props)do
3422 if e[t]then 4587 if t[e]then
3423 a:tag(t):up(); 4588 a:tag(e):up();
3424 end 4589 end
3425 end 4590 end
3426 end 4591 end
3427 if t.value then 4592 if e.value then
3428 a:tag(t.value):text(e[1]):up(); 4593 a:tag(e.value):text(t[1]):up();
3429 elseif t.values then 4594 elseif e.values then
3430 local o=t.values; 4595 local o=e.values;
3431 local i=o.behaviour=="repeat-last"and o[#o]; 4596 local i=o.behaviour=="repeat-last"and o[#o];
3432 for o=1,#e do 4597 for o=1,#t do
3433 a:tag(t.values[o]or i):text(e[o]):up(); 4598 a:tag(e.values[o]or i):text(t[o]):up();
3434 end 4599 end
3435 end 4600 end
3436 end 4601 end
3437 return a; 4602 return a;
3438 end 4603 end
3439 local function f(t) 4604 local function n(t)
3440 local e=i.stanza("vCard",{xmlns="vcard-temp"}); 4605 local e=o.stanza("vCard",{xmlns="vcard-temp"});
3441 for a=1,#t do 4606 for a=1,#t do
3442 e:add_child(w(t[a])); 4607 e:add_child(p(t[a]));
3443 end 4608 end
3444 return e; 4609 return e;
3445 end 4610 end
3446 function u(e) 4611 function r(e)
3447 if not e[1]or e[1].name then 4612 if not e[1]or e[1].name then
3448 return f(e) 4613 return n(e)
3449 else 4614 else
3450 local t=i.stanza("xCard",{xmlns="vcard-temp"}); 4615 local t=o.stanza("xCard",{xmlns="vcard-temp"});
3451 for a=1,#e do 4616 for a=1,#e do
3452 t:add_child(f(e[a])); 4617 t:add_child(n(e[a]));
3453 end 4618 end
3454 return t; 4619 return t;
3455 end 4620 end
3456 end 4621 end
3457 function d(t) 4622 function u(t)
3458 t=t 4623 t=t
3459 :gsub("\r\n","\n") 4624 :gsub("\r\n","\n")
3460 :gsub("\n ","") 4625 :gsub("\n ","")
3461 :gsub("\n\n+","\n"); 4626 :gsub("\n\n+","\n");
3462 local s={}; 4627 local h={};
3463 local e; 4628 local e;
3464 for t in t:gmatch("[^\n]+")do 4629 for t in t:gmatch("[^\n]+")do
3465 local t=p(t); 4630 local t=y(t);
3466 local n,t,i=t:match("^([-%a]+)(\30?[^\29]*)\29(.*)$"); 4631 local s,t,n=t:match("^([-%a]+)(\30?[^\29]*)\29(.*)$");
3467 i=i:gsub("\29",":"); 4632 n=n:gsub("\29",":");
3468 if#t>0 then 4633 if#t>0 then
3469 local o={}; 4634 local a={};
3470 for a,n,i in t:gmatch("\30([^=]+)(=?)([^\30]*)")do 4635 for e,o,i in t:gmatch("\30([^=]+)(=?)([^\30]*)")do
3471 a=a:upper(); 4636 e=e:upper();
3472 local e={}; 4637 local t={};
3473 for t in i:gmatch("[^\31]+")do 4638 for e in i:gmatch("[^\31]+")do
3474 e[#e+1]=t 4639 t[#t+1]=e
3475 e[t]=true; 4640 t[e]=true;
3476 end 4641 end
3477 if n=="="then 4642 if o=="="then
3478 o[a]=e; 4643 a[e]=t;
3479 else 4644 else
3480 o[a]=true; 4645 a[e]=true;
3481 end 4646 end
3482 end 4647 end
3483 t=o; 4648 t=a;
3484 end 4649 end
3485 if n=="BEGIN"and i=="VCARD"then 4650 if s=="BEGIN"and n=="VCARD"then
3486 e={}; 4651 e={};
3487 s[#s+1]=e; 4652 h[#h+1]=e;
3488 elseif n=="END"and i=="VCARD"then 4653 elseif s=="END"and n=="VCARD"then
3489 e=nil; 4654 e=nil;
3490 elseif e and o[n]then 4655 elseif e and i[s]then
3491 local o=o[n]; 4656 local o=i[s];
3492 local n={name=n}; 4657 local i={name=s};
3493 e[#e+1]=n; 4658 e[#e+1]=i;
3494 local s=e; 4659 local s=e;
3495 e=n; 4660 e=i;
3496 if o.types then 4661 if o.types then
3497 for o,a in c(o.types)do 4662 for o,a in m(o.types)do
3498 local a=a:lower(); 4663 local a=a:lower();
3499 if(t.TYPE and t.TYPE[a]==true) 4664 if(t.TYPE and t.TYPE[a]==true)
3500 or t[a]==true then 4665 or t[a]==true then
3501 e.TYPE=a; 4666 e.TYPE=a;
3502 end 4667 end
3503 end 4668 end
3504 end 4669 end
3505 if o.props then 4670 if o.props then
3506 for o,a in c(o.props)do 4671 for o,a in m(o.props)do
3507 if t[a]then 4672 if t[a]then
3508 if t[a]==true then 4673 if t[a]==true then
3509 e[a]=true; 4674 e[a]=true;
3510 else 4675 else
3511 for o,t in c(t[a])do 4676 for o,t in m(t[a])do
3512 e[a]=t; 4677 e[a]=t;
3513 end 4678 end
3514 end 4679 end
3515 end 4680 end
3516 end 4681 end
3517 end 4682 end
3518 if o=="text"or o.value then 4683 if o=="text"or o.value then
3519 a(e,i); 4684 a(e,n);
3520 elseif o.values then 4685 elseif o.values then
3521 local t="\30"..i; 4686 local t="\30"..n;
3522 for t in t:gmatch("\30([^\30]*)")do 4687 for t in t:gmatch("\30([^\30]*)")do
3523 a(e,t); 4688 a(e,t);
3524 end 4689 end
3525 end 4690 end
3526 e=s; 4691 e=s;
3527 end 4692 end
3528 end 4693 end
3529 return s; 4694 return h;
3530 end 4695 end
3531 local function i(t) 4696 local function n(t)
3532 local e={}; 4697 local e={};
3533 for a=1,#t do 4698 for a=1,#t do
3534 e[a]=y(t[a]); 4699 e[a]=w(t[a]);
3535 end 4700 end
3536 e=h(e,";"); 4701 e=c(e,";");
3537 local a=""; 4702 local a="";
3538 for t,e in n(t)do 4703 for t,e in s(t)do
3539 if s(t)=="string"and t~="name"then 4704 if h(t)=="string"and t~="name"then
3540 a=a..(";%s=%s"):format(t,s(e)=="table"and h(e,",")or e); 4705 a=a..(";%s=%s"):format(t,h(e)=="table"and c(e,",")or e);
3541 end 4706 end
3542 end 4707 end
3543 return("%s%s:%s"):format(t.name,a,e) 4708 return("%s%s:%s"):format(t.name,a,e)
3544 end 4709 end
3545 local function t(t) 4710 local function o(t)
3546 local e={}; 4711 local e={};
3547 a(e,"BEGIN:VCARD") 4712 a(e,"BEGIN:VCARD")
3548 for o=1,#t do 4713 for o=1,#t do
3549 a(e,i(t[o])); 4714 a(e,n(t[o]));
3550 end 4715 end
3551 a(e,"END:VCARD") 4716 a(e,"END:VCARD")
3552 return h(e,m); 4717 return c(e,f);
3553 end 4718 end
3554 function r(e) 4719 function l(e)
3555 if e[1]and e[1].name then 4720 if e[1]and e[1].name then
3556 return t(e) 4721 return o(e)
3557 else 4722 else
3558 local o={}; 4723 local t={};
3559 for a=1,#e do 4724 for a=1,#e do
3560 o[a]=t(e[a]); 4725 t[a]=o(e[a]);
3561 end 4726 end
3562 return h(o,m); 4727 return c(t,f);
3563 end 4728 end
3564 end 4729 end
3565 local function h(i) 4730 local function n(o)
3566 local t=i.name; 4731 local e=o.name;
3567 local e=o[t]; 4732 local t=i[e];
3568 local t={name=t}; 4733 local e={name=e};
3569 if e=="text"then 4734 if t=="text"then
3570 t[1]=i:get_text(); 4735 e[1]=o:get_text();
3571 elseif s(e)=="table"then 4736 elseif h(t)=="table"then
3572 if e.value then 4737 if t.value then
3573 t[1]=i:get_child_text(e.value)or""; 4738 e[1]=o:get_child_text(t.value)or"";
3574 elseif e.values then 4739 elseif t.values then
3575 local e=e.values; 4740 local t=t.values;
3576 if e.behaviour=="repeat-last"then 4741 if t.behaviour=="repeat-last"then
3577 for e=1,#i.tags do 4742 for t=1,#o.tags do
3578 a(t,i.tags[e]:get_text()or""); 4743 a(e,o.tags[t]:get_text()or"");
3579 end 4744 end
3580 else 4745 else
3581 for o=1,#e do 4746 for i=1,#t do
3582 a(t,i:get_child_text(e[o])or""); 4747 a(e,o:get_child_text(t[i])or"");
3583 end 4748 end
3584 end 4749 end
3585 elseif e.names then 4750 elseif t.names then
3586 local e=e.names; 4751 local t=t.names;
3587 for a=1,#e do 4752 for a=1,#t do
3588 if i:get_child(e[a])then 4753 if o:get_child(t[a])then
3589 t[1]=e[a]; 4754 e[1]=t[a];
3590 break; 4755 break;
3591 end 4756 end
3592 end 4757 end
3593 end 4758 end
3594 if e.props_verbatim then 4759 if t.props_verbatim then
3595 for a,e in n(e.props_verbatim)do 4760 for a,t in s(t.props_verbatim)do
3596 t[a]=e; 4761 e[a]=t;
3597 end 4762 end
3598 end 4763 end
3599 if e.types then 4764 if t.types then
3600 local e=e.types; 4765 local t=t.types;
3601 t.TYPE={}; 4766 e.TYPE={};
3602 for o=1,#e do 4767 for i=1,#t do
3603 if i:get_child(e[o])then 4768 if o:get_child(t[i])then
3604 a(t.TYPE,e[o]:lower()); 4769 a(e.TYPE,t[i]:lower());
3605 end 4770 end
3606 end 4771 end
3607 if#t.TYPE==0 then 4772 if#e.TYPE==0 then
3608 t.TYPE=nil; 4773 e.TYPE=nil;
3609 end 4774 end
3610 end 4775 end
3611 if e.props then 4776 if t.props then
3612 local e=e.props; 4777 local t=t.props;
3613 for o=1,#e do 4778 for i=1,#t do
3614 local e=e[o] 4779 local t=t[i]
3615 local o=i:get_child_text(e); 4780 local o=o:get_child_text(t);
3616 if o then 4781 if o then
3617 t[e]=t[e]or{}; 4782 e[t]=e[t]or{};
3618 a(t[e],o); 4783 a(e[t],o);
3619 end 4784 end
3620 end 4785 end
3621 end 4786 end
3622 else 4787 else
3623 return nil 4788 return nil
3624 end 4789 end
3625 return t; 4790 return e;
3626 end 4791 end
3627 local function i(e) 4792 local function o(e)
3628 local e=e.tags; 4793 local e=e.tags;
3629 local t={}; 4794 local t={};
3630 for o=1,#e do 4795 for o=1,#e do
3631 a(t,h(e[o])); 4796 a(t,n(e[o]));
3632 end 4797 end
3633 return t 4798 return t
3634 end 4799 end
3635 function l(e) 4800 function d(e)
3636 if e.attr.xmlns~="vcard-temp"then 4801 if e.attr.xmlns~="vcard-temp"then
3637 return nil,"wrong-xmlns"; 4802 return nil,"wrong-xmlns";
3638 end 4803 end
3639 if e.name=="xCard"then 4804 if e.name=="xCard"then
3640 local a={}; 4805 local a={};
3641 local t=e.tags; 4806 local t=e.tags;
3642 for e=1,#t do 4807 for e=1,#t do
3643 a[e]=i(t[e]); 4808 a[e]=o(t[e]);
3644 end 4809 end
3645 return a 4810 return a
3646 elseif e.name=="vCard"then 4811 elseif e.name=="vCard"then
3647 return i(e) 4812 return o(e)
3648 end 4813 end
3649 end 4814 end
3650 o={ 4815 i={
3651 VERSION="text", 4816 VERSION="text",
3652 FN="text", 4817 FN="text",
3653 N={ 4818 N={
3654 values={ 4819 values={
3655 "FAMILY", 4820 "FAMILY",
3767 props={"TYPE"}, 4932 props={"TYPE"},
3768 value="CRED", 4933 value="CRED",
3769 }, 4934 },
3770 DESC="text", 4935 DESC="text",
3771 }; 4936 };
3772 o.LOGO=o.PHOTO; 4937 i.LOGO=i.PHOTO;
3773 o.SOUND=o.PHOTO; 4938 i.SOUND=i.PHOTO;
3774 return{ 4939 return{
3775 from_text=d; 4940 from_text=u;
3776 to_text=r; 4941 to_text=l;
3777 from_xep54=l; 4942 from_xep54=d;
3778 to_xep54=u; 4943 to_xep54=r;
3779 lua_to_text=r; 4944 lua_to_text=l;
3780 lua_to_xep54=u; 4945 lua_to_xep54=r;
3781 text_to_lua=d; 4946 text_to_lua=u;
3782 text_to_xep54=function(...)return u(d(...));end; 4947 text_to_xep54=function(...)return r(u(...));end;
3783 xep54_to_lua=l; 4948 xep54_to_lua=d;
3784 xep54_to_text=function(...)return r(l(...))end; 4949 xep54_to_text=function(...)return l(d(...))end;
3785 }; 4950 };
3786 end) 4951 end)
3787 package.preload['util.logger']=(function(...) 4952 package.preload['util.logger']=(function(...)
3788 local _ENV=_ENV; 4953 local _ENV=_ENV;
3789 local function e(t,...) 4954 local function e(t,...)
3794 end 4959 end
3795 _ENV=e; 4960 _ENV=e;
3796 _M=e; 4961 _M=e;
3797 return e; 4962 return e;
3798 end 4963 end
3799 local e=pcall; 4964 local o=pairs;
3800 local e=string.find; 4965 local c=ipairs;
3801 local e,o,e=ipairs,pairs,setmetatable; 4966 local u=require;
3802 local a={}; 4967 local l=table.remove;
4968 local _ENV=nil;
3803 local e={}; 4969 local e={};
3804 local t; 4970 local t;
3805 function a.init(e) 4971 local function h(e)
3806 local a=t(e,"debug"); 4972 local a=t(e,"debug");
3807 local o=t(e,"info"); 4973 local o=t(e,"info");
3808 local i=t(e,"warn"); 4974 local i=t(e,"warn");
3809 local n=t(e,"error"); 4975 local n=t(e,"error");
3810 return function(t,e,...) 4976 return function(t,e,...)
3830 t[i](o,a,e,...); 4996 t[i](o,a,e,...);
3831 end 4997 end
3832 end 4998 end
3833 return e; 4999 return e;
3834 end 5000 end
3835 function a.reset() 5001 local function d()
3836 for t,e in o(e)do 5002 for t,e in o(e)do
3837 for t=1,#e do 5003 for t=1,#e do
3838 e[t]=nil; 5004 e[t]=nil;
3839 end 5005 end
3840 end 5006 end
3841 end 5007 end
3842 function a.add_level_sink(t,o) 5008 local function i(t,a)
3843 if not e[t]then 5009 if not e[t]then
3844 e[t]={o}; 5010 e[t]={a};
3845 else 5011 else
3846 e[t][#e[t]+1]=o; 5012 e[t][#e[t]+1]=a;
3847 end 5013 end
3848 end 5014 end
3849 a.new=t; 5015 local function r(s,n)
5016 local a=u"util.format".format;
5017 local function e(o,t,e,...)
5018 return s(o,t,a(e,...));
5019 end
5020 for a,t in c(n or{"debug","info","warn","error"})do
5021 i(t,e);
5022 end
5023 return e;
5024 end
5025 local function n(i)
5026 local a;
5027 for t,e in o(e)do
5028 for t=#e,1,-1 do
5029 if e[t]==i then
5030 l(e,t);
5031 a=true;
5032 end
5033 end
5034 end
3850 return a; 5035 return a;
5036 end
5037 return{
5038 init=h;
5039 make_logger=t;
5040 reset=d;
5041 add_level_sink=i;
5042 add_simple_sink=r;
5043 new=t;
5044 remove_sink=n;
5045 };
3851 end) 5046 end)
3852 package.preload['util.datetime']=(function(...) 5047 package.preload['util.datetime']=(function(...)
3853 local _ENV=_ENV; 5048 local _ENV=_ENV;
3854 local function a(t,...) 5049 local function e(t,...)
3855 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 5050 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
3856 package.loaded[t]=e; 5051 package.loaded[t]=e;
3857 for t=1,select("#",...)do 5052 for t=1,select("#",...)do
3858 (select(t,...))(e); 5053 (select(t,...))(e);
3859 end 5054 end
3861 _M=e; 5056 _M=e;
3862 return e; 5057 return e;
3863 end 5058 end
3864 local e=os.date; 5059 local e=os.date;
3865 local i=os.time; 5060 local i=os.time;
3866 local u=os.difftime; 5061 local r=os.difftime;
3867 local t=error; 5062 local t=math.floor;
3868 local s=tonumber; 5063 local s=tonumber;
3869 a"datetime" 5064 local _ENV=nil;
3870 function date(t) 5065 local function d(a)
3871 return e("!%Y-%m-%d",t); 5066 return e("!%Y-%m-%d",a and t(a)or nil);
3872 end 5067 end
3873 function datetime(t) 5068 local function l(a)
3874 return e("!%Y-%m-%dT%H:%M:%SZ",t); 5069 if a==nil or a%1==0 then
3875 end 5070 return e("!%Y-%m-%dT%H:%M:%SZ",a);
3876 function time(t) 5071 end
3877 return e("!%H:%M:%S",t); 5072 local o=a%1;
3878 end 5073 local a=t(a);
3879 function legacy(t) 5074 return e("!%Y-%m-%dT%H:%M:%S.%%06dZ",a):format(t(o*1e6));
3880 return e("!%Y%m%dT%H:%M:%S",t); 5075 end
3881 end 5076 local function c(a)
3882 function parse(o) 5077 if a==nil or a%1==0 then
3883 if o then 5078 return e("!%H:%M:%S",a);
3884 local n,h,l,d,r,t,a; 5079 end
3885 n,h,l,d,r,t,a=o:match("^(%d%d%d%d)%-?(%d%d)%-?(%d%d)T(%d%d):(%d%d):(%d%d)%.?%d*([Z+%-]?.*)$"); 5080 local o=a%1;
3886 if n then 5081 local a=t(a);
3887 local u=u(i(e("*t")),i(e("!*t"))); 5082 return e("!%H:%M:%S.%%06d",a):format(t(o*1e6));
5083 end
5084 local function u(a)
5085 return e("!%Y%m%dT%H:%M:%S",a and t(a)or nil);
5086 end
5087 local function m(a)
5088 if a then
5089 local h,l,d,u,c,a,n=a:match("^(%d%d%d%d)%-?(%d%d)%-?(%d%d)T(%d%d):(%d%d):(%d%d%.?%d*)([Z+%-]?.*)$");
5090 if h then
5091 local o=i();
5092 local r=r(i(e("*t",o)),i(e("!*t",o)));
3888 local o=0; 5093 local o=0;
3889 if a~=""and a~="Z"then 5094 if n~=""and n~="Z"then
3890 local a,t,e=a:match("([+%-])(%d%d):?(%d*)"); 5095 local a,t,e=n:match("([+%-])(%d%d):?(%d*)");
3891 if not a then return;end 5096 if not a then return;end
3892 if#e~=2 then e="0";end 5097 if#e~=2 then e="0";end
3893 t,e=s(t),s(e); 5098 t,e=s(t),s(e);
3894 o=t*60*60+e*60; 5099 o=t*60*60+e*60;
3895 if a=="-"then o=-o;end 5100 if a=="-"then o=-o;end
3896 end 5101 end
3897 t=(t+u)-o; 5102 local e=a%1;
3898 return i({year=n,month=h,day=l,hour=d,min=r,sec=t,isdst=false}); 5103 a=t(a+r)-o;
3899 end 5104 return i({year=h,month=l,day=d,hour=u,min=c,sec=a,isdst=false})+e;
3900 end 5105 end
3901 end 5106 end
3902 return _M; 5107 end
5108 return{
5109 date=d;
5110 datetime=l;
5111 time=c;
5112 legacy=u;
5113 parse=m;
5114 };
3903 end) 5115 end)
3904 package.preload['util.json']=(function(...) 5116 package.preload['util.json']=(function(...)
3905 local _ENV=_ENV; 5117 local _ENV=_ENV;
3906 local function e(t,...) 5118 local function e(t,...)
3907 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 5119 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
3911 end 5123 end
3912 _ENV=e; 5124 _ENV=e;
3913 _M=e; 5125 _M=e;
3914 return e; 5126 return e;
3915 end 5127 end
3916 local w=type; 5128 local p=type;
3917 local t,v,p,q=table.insert,table.concat,table.remove,table.sort; 5129 local t,y,c=table.insert,table.concat,table.remove;
3918 local h=string.char; 5130 local n=string.char;
3919 local j,l=tostring,tonumber; 5131 local z,d=tostring,tonumber;
3920 local u,s=pairs,ipairs; 5132 local u,r,j=pairs,ipairs,require"util.iterators".sorted_pairs;
3921 local i=next; 5133 local i=next;
3922 local e=error; 5134 local b,m=getmetatable,setmetatable;
3923 local e,r,b=newproxy,getmetatable,setmetatable; 5135 local l=print;
3924 local c=print; 5136 local a,e=pcall(require,"util.array");
3925 local a,o=pcall(require,"util.array"); 5137 local v=a and b(e())or{};
3926 local f=a and r(o())or{};
3927 local a={}; 5138 local a={};
3928 local n=e and e(true)or{}; 5139 local h=m({},{__tostring=function()return"null";end;});
3929 if r and r(n)then 5140 a.null=h;
3930 r(n).__tostring=function()return"null";end; 5141 local f={
3931 end
3932 a.null=n;
3933 local y={
3934 ["\""]="\\\"",["\\"]="\\\\",["\b"]="\\b", 5142 ["\""]="\\\"",["\\"]="\\\\",["\b"]="\\b",
3935 ["\f"]="\\f",["\n"]="\\n",["\r"]="\\r",["\t"]="\\t"}; 5143 ["\f"]="\\f",["\n"]="\\n",["\r"]="\\r",["\t"]="\\t"};
3936 local e={ 5144 for t=0,31 do
3937 ["\""]="\"",["\\"]="\\",["/"]="/", 5145 local e=n(t);
3938 b="\b",f="\f",n="\n",r="\r",t="\t"}; 5146 if not f[e]then f[e]=("\\u%.4X"):format(t);end
3939 for e=0,31 do 5147 end
3940 local t=h(e); 5148 local function q(e)
3941 if not y[t]then y[t]=("\\u%.4X"):format(e);end 5149 if e<128 then return n(e);end
3942 end
3943 local function x(e)
3944 if e<128 then return h(e);end
3945 local t=e%64; 5150 local t=e%64;
3946 if e<2048 then 5151 if e<2048 then
3947 local e=(e-t)/64; 5152 local e=(e-t)/64;
3948 return h(128+64+e,128+t); 5153 return n(128+64+e,128+t);
3949 end 5154 end
3950 local a=e%4096; 5155 local a=e%4096;
3951 local o=(a-t)/64; 5156 local o=(a-t)/64;
3952 local e=(e-a)/4096; 5157 local e=(e-a)/4096;
3953 return h(128+64+32+e,128+o,128+t); 5158 return n(128+64+32+e,128+o,128+t);
3954 end 5159 end
3955 local g={ 5160 local k={
3956 number=true, 5161 number=true,
3957 string=true, 5162 string=true,
3958 table=true, 5163 table=true,
3959 boolean=true 5164 boolean=true
3960 }; 5165 };
3961 local z={ 5166 local x={
3962 __array=true; 5167 __array=true;
3963 __hash=true; 5168 __hash=true;
3964 }; 5169 };
3965 local o,k,d,m; 5170 local o,g,s,w;
3966 function m(a,e) 5171 function w(a,e)
3967 t(e,"\""..(a:gsub(".",y)).."\""); 5172 t(e,"\""..(a:gsub(".",f)).."\"");
3968 end 5173 end
3969 function d(a,e) 5174 function s(a,e)
3970 t(e,"["); 5175 t(e,"[");
3971 if i(a)then 5176 if i(a)then
3972 for i,a in s(a)do 5177 for i,a in r(a)do
3973 o(a,e); 5178 o(a,e);
3974 t(e,","); 5179 t(e,",");
3975 end 5180 end
3976 p(e); 5181 c(e);
3977 end 5182 end
3978 t(e,"]"); 5183 t(e,"]");
3979 end 5184 end
3980 function k(l,e) 5185 function g(l,e)
3981 local a={}; 5186 local a={};
3982 local r={}; 5187 local n={};
3983 local h={}; 5188 local d={};
3984 for t,e in s(l)do 5189 for e,t in r(l)do
3985 a[t]=e; 5190 a[e]=t;
3986 end 5191 end
3987 for e,t in u(l)do 5192 for e,t in u(l)do
3988 local o,i=w(e),w(t); 5193 local o,i=p(e),p(t);
3989 if g[i]or t==n then 5194 if k[i]or t==h then
3990 if o=="string"and not z[e]then 5195 if o=="string"and not x[e]then
3991 h[e]=t; 5196 d[e]=t;
3992 elseif(g[o]or e==n)and a[e]==nil then 5197 elseif(k[o]or e==h)and a[e]==nil then
3993 r[e]=t; 5198 n[e]=t;
3994 end 5199 end
3995 end 5200 end
3996 end 5201 end
3997 if i(r)~=nil or i(h)~=nil or i(a)==nil then 5202 if i(n)~=nil or i(d)~=nil or i(a)==nil then
3998 t(e,"{"); 5203 t(e,"{");
3999 local n=#e; 5204 local r=#e;
4000 if e.ordered then 5205 local h=e.ordered and j or u;
4001 local a={}; 5206 for a,i in h(d)do
4002 for e in u(h)do 5207 w(a,e);
4003 t(a,e);
4004 end
4005 q(a);
4006 for i,a in s(a)do
4007 m(a,e);
4008 t(e,":");
4009 o(h[a],e);
4010 t(e,",");
4011 end
4012 else
4013 for a,i in u(h)do
4014 m(a,e);
4015 t(e,":"); 5208 t(e,":");
4016 o(i,e); 5209 o(i,e);
4017 t(e,","); 5210 t(e,",");
4018 end 5211 end
4019 end 5212 if i(n)~=nil then
4020 if i(r)~=nil then
4021 t(e,"\"__hash\":["); 5213 t(e,"\"__hash\":[");
4022 for i,a in u(r)do 5214 for a,i in u(n)do
5215 o(a,e);
5216 t(e,",");
4023 o(i,e); 5217 o(i,e);
4024 t(e,","); 5218 t(e,",");
4025 o(a,e); 5219 end
4026 t(e,","); 5220 c(e);
4027 end
4028 p(e);
4029 t(e,"]"); 5221 t(e,"]");
4030 t(e,","); 5222 t(e,",");
4031 end 5223 end
4032 if i(a)then 5224 if i(a)then
4033 t(e,"\"__array\":"); 5225 t(e,"\"__array\":");
4034 d(a,e); 5226 s(a,e);
4035 t(e,","); 5227 t(e,",");
4036 end 5228 end
4037 if n~=#e then p(e);end 5229 if r~=#e then c(e);end
4038 t(e,"}"); 5230 t(e,"}");
4039 else 5231 else
4040 d(a,e); 5232 s(a,e);
4041 end 5233 end
4042 end 5234 end
4043 function o(e,a) 5235 function o(e,a)
4044 local o=w(e); 5236 local o=p(e);
4045 if o=="number"then 5237 if e==h then
4046 t(a,j(e)); 5238 t(a,"null");
5239 elseif o=="number"then
5240 t(a,z(e));
4047 elseif o=="string"then 5241 elseif o=="string"then
4048 m(e,a); 5242 w(e,a);
4049 elseif o=="table"then 5243 elseif o=="table"then
4050 local t=r(e); 5244 local t=b(e);
4051 if t==f then 5245 if t==v then
4052 d(e,a); 5246 s(e,a);
4053 else 5247 else
4054 k(e,a); 5248 g(e,a);
4055 end 5249 end
4056 elseif o=="boolean"then 5250 elseif o=="boolean"then
4057 t(a,(e and"true"or"false")); 5251 t(a,(e and"true"or"false"));
4058 else 5252 else
4059 t(a,"null"); 5253 t(a,"null");
4060 end 5254 end
4061 end 5255 end
4062 function a.encode(t) 5256 function a.encode(t)
4063 local e={}; 5257 local e={};
4064 o(t,e); 5258 o(t,e);
4065 return v(e); 5259 return y(e);
4066 end 5260 end
4067 function a.encode_ordered(t) 5261 function a.encode_ordered(t)
4068 local e={ordered=true}; 5262 local e={ordered=true};
4069 o(t,e); 5263 o(t,e);
4070 return v(e); 5264 return y(e);
4071 end 5265 end
4072 function a.encode_array(t) 5266 function a.encode_array(t)
4073 local e={}; 5267 local e={};
4074 d(t,e); 5268 s(t,e);
4075 return v(e); 5269 return y(e);
4076 end 5270 end
4077 local function o(t,e) 5271 local function o(t,e)
4078 return t:find("[^ \t\r\n]",e)or e; 5272 return t:find("[^ \t\r\n]",e)or e;
4079 end 5273 end
4080 local function d(e) 5274 local function c(e)
4081 local a=e.__array; 5275 local a=e.__array;
4082 if a then 5276 if a then
4083 e.__array=nil; 5277 e.__array=nil;
4084 for o,a in s(a)do 5278 for o,a in r(a)do
4085 t(e,a); 5279 t(e,a);
4086 end 5280 end
4087 end 5281 end
4088 local a=e.__hash; 5282 local a=e.__hash;
4089 if a then 5283 if a then
4090 e.__hash=nil; 5284 e.__hash=nil;
4091 local t; 5285 local t;
4092 for o,a in s(a)do 5286 for o,a in r(a)do
4093 if t~=nil then 5287 if t~=nil then
4094 e[t]=a;t=nil; 5288 e[t]=a;t=nil;
4095 else 5289 else
4096 t=a; 5290 t=a;
4097 end 5291 end
4098 end 5292 end
4099 end 5293 end
4100 return e; 5294 return e;
4101 end 5295 end
4102 local i,r; 5296 local s,r;
4103 local function m(t,e) 5297 local function u(t,e)
4104 local s={}; 5298 local i={};
4105 while true do 5299 while true do
4106 local n,a; 5300 local n,a;
4107 e=o(t,e+1); 5301 e=o(t,e+1);
4108 if t:byte(e)~=34 then 5302 if t:byte(e)~=34 then
4109 if t:byte(e)==125 then return s,e+1;end 5303 if t:byte(e)==125 then return i,e+1;end
4110 return nil,"key expected"; 5304 return nil,"key expected";
4111 end 5305 end
4112 n,e=r(t,e); 5306 n,e=r(t,e);
4113 if n==nil then return nil,e;end 5307 if n==nil then return nil,e;end
4114 e=o(t,e); 5308 e=o(t,e);
4115 if t:byte(e)~=58 then return nil,"colon expected";end 5309 if t:byte(e)~=58 then return nil,"colon expected";end
4116 a,e=i(t,e+1); 5310 a,e=s(t,e+1);
4117 if a==nil then return nil,e;end 5311 if a==nil then return nil,e;end
4118 s[n]=a; 5312 i[n]=a;
4119 e=o(t,e); 5313 e=o(t,e);
4120 local t=t:byte(e); 5314 local t=t:byte(e);
4121 if t==125 then return d(s),e+1;end 5315 if t==125 then return c(i),e+1;end
4122 if t~=44 then return nil,"object eof";end 5316 if t~=44 then return nil,"object eof";end
4123 end 5317 end
4124 end 5318 end
4125 local function u(n,e) 5319 local function c(n,e)
4126 local s={}; 5320 local i={};
4127 local h=e;
4128 while true do 5321 while true do
4129 local a; 5322 local a,h;
4130 a,e=i(n,e+1); 5323 a,e,h=s(n,e+1,93);
4131 if a==nil then 5324 if a==nil then
4132 if n:byte(h+1)==93 then return b(s,f),h+2;end 5325 if h then
5326 if#i~=0 then
5327 return nil,"value expected";
5328 end
5329 a,e=m(i,v),e+1;
5330 end
4133 return a,e; 5331 return a,e;
4134 end 5332 end
4135 t(s,a); 5333 t(i,a);
4136 e=o(n,e); 5334 e=o(n,e);
4137 local t=n:byte(e); 5335 local t=n:byte(e);
4138 if t==93 then return b(s,f),e+1;end 5336 if t==93 then return m(i,v),e+1;end
4139 if t~=44 then return nil,"array eof";end 5337 if t~=44 then return nil,"array eof";end
4140 end 5338 end
4141 end 5339 end
4142 local t; 5340 local t;
4143 local function e(e) 5341 local function i(e)
4144 local e,t=l(e:sub(3,6),16),l(e:sub(9,12),16); 5342 local t,e=d(e:sub(3,6),16),d(e:sub(9,12),16);
4145 local e=e*1024+t-56613888; 5343 local e=t*1024+e-56613888;
5344 local o=e%64;
5345 e=(e-o)/64;
5346 local t=e%64;
5347 e=(e-t)/64;
4146 local a=e%64; 5348 local a=e%64;
4147 e=(e-a)/64; 5349 e=(e-a)/64;
4148 local t=e%64; 5350 return n(240+e,128+a,128+t,128+o);
4149 e=(e-t)/64; 5351 end
4150 local o=e%64; 5352 local function n(e)
4151 e=(e-o)/64;
4152 return h(240+e,128+o,128+t,128+a);
4153 end
4154 local function s(e)
4155 e=e:match("%x%x%x%x",3); 5353 e=e:match("%x%x%x%x",3);
4156 if e then 5354 if e then
4157 return x(l(e,16)); 5355 local e=d(e,16)
5356 if e>=55296 and e<=57343 then t=true;end
5357 return q(e);
4158 end 5358 end
4159 t=true; 5359 t=true;
4160 end 5360 end
4161 function r(o,e) 5361 function r(o,e)
4162 e=e+1; 5362 e=e+1;
4163 local a=o:find("\"",e,true); 5363 local a=o:find("\"",e,true);
4164 if a then 5364 if a then
4165 local e=o:sub(e,a-1); 5365 local e=o:sub(e,a-1);
4166 t=nil; 5366 t=nil;
4167 e=e:gsub("\\u.?.?.?.?",s); 5367 e=e:gsub("\\u[dD][89abAB]%x%x\\u[dD][cdefCDEF]%x%x",i);
5368 e=e:gsub("\\u.?.?.?.?",n);
4168 if t then return nil,"invalid escape";end 5369 if t then return nil,"invalid escape";end
4169 return e,a+1; 5370 return e,a+1;
4170 end 5371 end
4171 return nil,"string eof"; 5372 return nil,"string eof";
4172 end 5373 end
5374 local function i(e,t)
5375 local e=e:match("[0-9%.%-eE%+]+",t);
5376 return d(e),t+#e;
5377 end
4173 local function d(t,e) 5378 local function d(t,e)
4174 local t=t:match("[0-9%.%-eE%+]+",e);
4175 return l(t),e+#t;
4176 end
4177 local function s(t,e)
4178 local o,a,t=t:byte(e+1,e+3); 5379 local o,a,t=t:byte(e+1,e+3);
4179 if o==117 and a==108 and t==108 then 5380 if o==117 and a==108 and t==108 then
4180 return n,e+4; 5381 return h,e+4;
4181 end 5382 end
4182 return nil,"null parse failed"; 5383 return nil,"null parse failed";
4183 end 5384 end
4184 local function n(t,e) 5385 local function n(t,e)
4185 local a,t,o=t:byte(e+1,e+3); 5386 local o,t,a=t:byte(e+1,e+3);
4186 if a==114 and t==117 and o==101 then 5387 if o==114 and t==117 and a==101 then
4187 return true,e+4; 5388 return true,e+4;
4188 end 5389 end
4189 return nil,"true parse failed"; 5390 return nil,"true parse failed";
4190 end 5391 end
4191 local function h(t,e) 5392 local function h(t,e)
4192 local t,a,o,i=t:byte(e+1,e+4); 5393 local o,a,t,i=t:byte(e+1,e+4);
4193 if t==97 and a==108 and o==115 and i==101 then 5394 if o==97 and a==108 and t==115 and i==101 then
4194 return false,e+5; 5395 return false,e+5;
4195 end 5396 end
4196 return nil,"false parse failed"; 5397 return nil,"false parse failed";
4197 end 5398 end
4198 function i(a,t) 5399 function s(a,e,s)
4199 t=o(a,t); 5400 e=o(a,e);
4200 local e=a:byte(t); 5401 local t=a:byte(e);
4201 if e==123 then 5402 if t==123 then
4202 return m(a,t); 5403 return u(a,e);
4203 elseif e==91 then 5404 elseif t==91 then
4204 return u(a,t); 5405 return c(a,e);
4205 elseif e==34 then 5406 elseif t==34 then
4206 return r(a,t); 5407 return r(a,e);
4207 elseif e~=nil and e>=48 and e<=57 or e==45 then 5408 elseif t~=nil and t>=48 and t<=57 or t==45 then
4208 return d(a,t); 5409 return i(a,e);
4209 elseif e==110 then 5410 elseif t==110 then
4210 return s(a,t); 5411 return d(a,e);
4211 elseif e==116 then 5412 elseif t==116 then
4212 return n(a,t); 5413 return n(a,e);
4213 elseif e==102 then 5414 elseif t==102 then
4214 return h(a,t); 5415 return h(a,e);
5416 elseif t==s then
5417 return nil,e,true;
4215 else 5418 else
4216 return nil,"value expected"; 5419 return nil,"value expected";
4217 end 5420 end
4218 end 5421 end
4219 local t={ 5422 local t={
4227 ["\\t"]="\\u0009"; 5430 ["\\t"]="\\u0009";
4228 ["\\u"]="\\u"; 5431 ["\\u"]="\\u";
4229 }; 5432 };
4230 function a.decode(e) 5433 function a.decode(e)
4231 e=e:gsub("\\.",t) 5434 e=e:gsub("\\.",t)
4232 local t,a=i(e,1); 5435 local t,a=s(e,1);
4233 if t==nil then return t,a;end 5436 if t==nil then return t,a;end
4234 if e:find("[^ \t\r\n]",a)then return nil,"garbage at eof";end 5437 if e:find("[^ \t\r\n]",a)then return nil,"garbage at eof";end
4235 return t; 5438 return t;
4236 end 5439 end
4237 function a.test(e) 5440 function a.test(e)
4238 local e=a.encode(e); 5441 local e=a.encode(e);
4239 local t=a.decode(e); 5442 local t=a.decode(e);
4240 local t=a.encode(t); 5443 local t=a.encode(t);
4241 if e~=t then 5444 if e~=t then
4242 c("FAILED"); 5445 l("FAILED");
4243 c("encoded:",e); 5446 l("encoded:",e);
4244 c("recoded:",t); 5447 l("recoded:",t);
4245 else 5448 else
4246 c(e); 5449 l(e);
4247 end 5450 end
4248 return e==t; 5451 return e==t;
4249 end 5452 end
4250 return a; 5453 return a;
4251 end) 5454 end)
4252 package.preload['util.xml']=(function(...) 5455 package.preload['util.xml']=(function(...)
4253 local _ENV=_ENV; 5456 local _ENV=_ENV;
4254 local function a(t,...) 5457 local function e(t,...)
4255 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 5458 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
4256 package.loaded[t]=e; 5459 package.loaded[t]=e;
4257 for t=1,select("#",...)do 5460 for t=1,select("#",...)do
4258 (select(t,...))(e); 5461 (select(t,...))(e);
4259 end 5462 end
4260 _ENV=e; 5463 _ENV=e;
4261 _M=e; 5464 _M=e;
4262 return e; 5465 return e;
4263 end 5466 end
4264 local t=require"util.stanza"; 5467 local t=require"util.stanza";
4265 local h=require"lxp"; 5468 local u=require"lxp";
4266 a("xml") 5469 local i=table.insert;
5470 local r=table.remove;
5471 local c=error;
5472 local _ENV=nil;
4267 local e=(function() 5473 local e=(function()
4268 local n={ 5474 local d={
4269 ["http://www.w3.org/XML/1998/namespace"]="xml"; 5475 ["http://www.w3.org/XML/1998/namespace"]="xml";
4270 }; 5476 };
4271 local e="\1"; 5477 local n="\1";
4272 local i="^([^"..e.."]*)"..e.."?(.*)$"; 5478 local h="^([^"..n.."]*)"..n.."?(.*)$";
4273 return function(s) 5479 return function(l,o)
4274 local o={}; 5480 local e={};
4275 local a=t.stanza("root"); 5481 local a=t.stanza("root");
4276 function o:StartElement(t,e) 5482 local t={};
4277 local t,o=t:match(i); 5483 local s={};
4278 if o==""then 5484 function e:StartNamespaceDecl(e,a)
4279 t,o="",t; 5485 if e~=nil then
4280 end 5486 i(t,a);
4281 if t~=""then 5487 i(s,e);
4282 e.xmlns=t; 5488 end
5489 end
5490 function e:EndNamespaceDecl(e)
5491 if e~=nil then
5492 r(t);
5493 r(s);
5494 end
5495 end
5496 function e:StartElement(o,e)
5497 local o,i=o:match(h);
5498 if i==""then
5499 o,i="",o;
5500 end
5501 if o~=""then
5502 e.xmlns=o;
4283 end 5503 end
4284 for t=1,#e do 5504 for t=1,#e do
4285 local a=e[t]; 5505 local a=e[t];
4286 e[t]=nil; 5506 e[t]=nil;
4287 local t,o=a:match(i); 5507 local t,o=a:match(h);
4288 if o~=""then 5508 if o~=""then
4289 t=n[t]; 5509 t=d[t];
4290 if t then 5510 if t then
4291 e[t..":"..o]=e[a]; 5511 e[t..":"..o]=e[a];
4292 e[a]=nil; 5512 e[a]=nil;
4293 end 5513 end
4294 end 5514 end
4295 end 5515 end
4296 a:tag(o,e); 5516 local o={}
4297 end 5517 for e=1,#t do
4298 function o:CharacterData(e) 5518 o[s[e]]=t[e];
5519 end
5520 a:tag(i,e,o);
5521 end
5522 function e:CharacterData(e)
4299 a:text(e); 5523 a:text(e);
4300 end 5524 end
4301 function o:EndElement(e) 5525 function e:EndElement()
4302 a:up(); 5526 a:up();
4303 end 5527 end
4304 local n=h.new(o,"\1"); 5528 local function t(t)
4305 local e,i,t,o=n:parse(s); 5529 if not t.stop or not t:stop()then
4306 if e then e,i,t,o=n:parse();end 5530 c("Failed to abort parsing");
5531 end
5532 end
5533 e.StartDoctypeDecl=t;
5534 if not o or not o.allow_comments then
5535 e.Comment=t;
5536 end
5537 if not o or not o.allow_processing_instructions then
5538 e.ProcessingInstruction=t;
5539 end
5540 local t=u.new(e,n);
5541 local e,i,o,n=t:parse(l);
5542 if e then e,i,o,n=t:parse();end
4307 if e then 5543 if e then
4308 return a.tags[1]; 5544 return a.tags[1];
4309 else 5545 else
4310 return e,i.." (line "..t..", col "..o..")"; 5546 return e,("%s (line %d, col %d))"):format(i,o,n);
4311 end 5547 end
4312 end; 5548 end;
4313 end)(); 5549 end)();
5550 return{
4314 parse=e; 5551 parse=e;
4315 return _M; 5552 };
4316 end) 5553 end)
4317 package.preload['util.rsm']=(function(...) 5554 package.preload['util.rsm']=(function(...)
4318 local _ENV=_ENV; 5555 local _ENV=_ENV;
4319 local function e(t,...) 5556 local function e(t,...)
4320 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 5557 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
4324 end 5561 end
4325 _ENV=e; 5562 _ENV=e;
4326 _M=e; 5563 _M=e;
4327 return e; 5564 return e;
4328 end 5565 end
4329 local h=require"util.stanza".stanza; 5566 local s=require"util.stanza".stanza;
4330 local t,o=tostring,tonumber; 5567 local t,o=tostring,tonumber;
4331 local n=type; 5568 local h=type;
4332 local s=pairs; 5569 local n=pairs;
4333 local i='http://jabber.org/protocol/rsm'; 5570 local i='http://jabber.org/protocol/rsm';
4334 local a={}; 5571 local a={};
4335 do 5572 do
4336 local e=a; 5573 local e=a;
4337 local function t(e) 5574 local function t(e)
4351 return{index=o(e.attr.index);e:get_text()}; 5588 return{index=o(e.attr.index);e:get_text()};
4352 end; 5589 end;
4353 e.last=a; 5590 e.last=a;
4354 e.count=t; 5591 e.count=t;
4355 end 5592 end
4356 local r=setmetatable({ 5593 local h=setmetatable({
4357 first=function(a,e) 5594 first=function(a,e)
4358 if n(e)=="table"then 5595 if h(e)=="table"then
4359 a:tag("first",{index=e.index}):text(e[1]):up(); 5596 a:tag("first",{index=e.index}):text(e[1]):up();
4360 else 5597 else
4361 a:tag("first"):text(t(e)):up(); 5598 a:tag("first"):text(t(e)):up();
4362 end 5599 end
4363 end; 5600 end;
4364 before=function(e,a) 5601 before=function(a,e)
4365 if a==true then 5602 if e==true then
4366 e:tag("before"):up(); 5603 a:tag("before"):up();
4367 else 5604 else
4368 e:tag("before"):text(t(a)):up(); 5605 a:tag("before"):text(t(e)):up();
4369 end 5606 end
4370 end 5607 end
4371 },{ 5608 },{
4372 __index=function(e,o) 5609 __index=function(a,e)
4373 return function(e,a) 5610 return function(a,o)
4374 e:tag(o):text(t(a)):up(); 5611 a:tag(e):text(t(o)):up();
4375 end 5612 end
4376 end; 5613 end;
4377 }); 5614 });
4378 local function t(e) 5615 local function t(e)
4379 local t={}; 5616 local o={};
4380 for o in e:childtags()do 5617 for t in e:childtags()do
4381 local e=o.name; 5618 local e=t.name;
4382 local a=e and a[e]; 5619 local a=e and a[e];
4383 if a then 5620 if a then
4384 t[e]=a(o); 5621 o[e]=a(t);
4385 end 5622 end
4386 end 5623 end
4387 return t; 5624 return o;
4388 end 5625 end
4389 local function n(t) 5626 local function o(t)
4390 local e=h("set",{xmlns=i}); 5627 local e=s("set",{xmlns=i});
4391 for t,o in s(t)do 5628 for t,o in n(t)do
4392 if a[t]then 5629 if a[t]then
4393 r[t](e,o); 5630 h[t](e,o);
4394 end 5631 end
4395 end 5632 end
4396 return e; 5633 return e;
4397 end 5634 end
4398 local function a(e) 5635 local function a(e)
4399 local e=e:get_child("set",i); 5636 local e=e:get_child("set",i);
4400 if e and#e.tags>0 then 5637 if e and#e.tags>0 then
4401 return t(e); 5638 return t(e);
4402 end 5639 end
4403 end 5640 end
4404 return{parse=t,generate=n,get=a}; 5641 return{parse=t,generate=o,get=a};
4405 end) 5642 end)
4406 package.preload['util.random']=(function(...) 5643 package.preload['util.random']=(function(...)
4407 local _ENV=_ENV; 5644 local _ENV=_ENV;
4408 local function e(t,...) 5645 local function e(t,...)
4409 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 5646 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
4435 end 5672 end
4436 _ENV=e; 5673 _ENV=e;
4437 _M=e; 5674 _M=e;
4438 return e; 5675 return e;
4439 end 5676 end
4440 local o={}; 5677 local s=require"util.net";
4441 local i={__index=function(t,e)return(o[e])(t);end, 5678 local n=require"util.hex";
5679 local a={};
5680 local o={
5681 __index=function(o,t)
5682 local e=a[t];
5683 if not e then return nil;end
5684 local e=e(o);
5685 o[t]=e;
5686 return e;
5687 end,
4442 __tostring=function(e)return e.addr;end, 5688 __tostring=function(e)return e.addr;end,
4443 __eq=function(t,e)return t.addr==e.addr;end}; 5689 };
4444 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"}; 5690 o.__eq=function(t,e)
4445 local function e(e,t) 5691 if getmetatable(t)~=o or getmetatable(e)~=o then
4446 if not t then 5692 return false;
4447 local a=e:match("^%x+(.)"); 5693 end
4448 if a==":"or(not(a)and e:sub(1,1)==":")then 5694 return t.packed==e.packed;
4449 t="IPv6" 5695 end
4450 elseif a=="."then 5696 local h={
4451 t="IPv4" 5697 ["0"]="0000",["1"]="0001",["2"]="0010",["3"]="0011",
4452 end 5698 ["4"]="0100",["5"]="0101",["6"]="0110",["7"]="0111",
4453 if not t then 5699 ["8"]="1000",["9"]="1001",["A"]="1010",["B"]="1011",
4454 return nil,"invalid address"; 5700 ["C"]="1100",["D"]="1101",["E"]="1110",["F"]="1111",
4455 end 5701 };
4456 elseif t~="IPv4"and t~="IPv6"then 5702 local function t(a,e)
5703 local i;
5704 if(not e or e=="IPv6")and a:find('%',1,true)then
5705 a,i=a:match("^(.-)%%(.*)");
5706 end
5707 local t,n=s.pton(a);
5708 if not t then return t,n end
5709 if e=="IPv6"and#t~=16 then
5710 return nil,"invalid-ipv6";
5711 elseif e=="IPv4"and#t~=4 then
5712 return nil,"invalid-ipv4";
5713 elseif not e then
5714 if#t==16 then
5715 e="IPv6";
5716 elseif#t==4 then
5717 e="IPv4";
5718 else
5719 return nil,"unknown protocol";
5720 end
5721 elseif e~="IPv6"and e~="IPv4"then
4457 return nil,"invalid protocol"; 5722 return nil,"invalid protocol";
4458 end 5723 end
4459 local a; 5724 return setmetatable({addr=a,packed=t,proto=e,zone=i},o);
4460 if t=="IPv6"and e:find('%',1,true)then 5725 end
4461 e,a=e:match("^(.-)%%(.*)"); 5726 function a:normal()
4462 end 5727 return s.ntop(self.packed);
4463 if t=="IPv6"and e:find('.',1,true)then 5728 end
4464 local t; 5729 function a.bits(e)
4465 e,t=e:gsub(":(%d+)%.(%d+)%.(%d+)%.(%d+)$",function(t,e,a,o) 5730 return n.encode(e.packed):upper():gsub(".",h);
4466 return(":%04X:%04X"):format(t*256+e,a*256+o); 5731 end
4467 end); 5732 function a.bits_full(e)
4468 if t~=1 then return nil,"invalid-address";end 5733 if e.proto=="IPv4"then
4469 end 5734 e=e.toV4mapped;
4470 return setmetatable({addr=e,proto=t,zone=a},i); 5735 end
4471 end 5736 return e.bits;
4472 local function i(a) 5737 end
4473 local t=""; 5738 local e;
4474 local e={}; 5739 local function v(t,a)
5740 t,a=t.bits_full,a.bits_full;
5741 for e=1,128 do
5742 if t:sub(e,e)~=a:sub(e,e)then
5743 return e-1;
5744 end
5745 end
5746 return 128;
5747 end
5748 local n=t("::1");
5749 local w=t("127.0.0.0");
5750 local h=t("2002::");
5751 local d=t("2001::");
5752 local p=t("fe80::");
5753 local m=t("169.254.0.0");
5754 local r=t("fc00::");
5755 local i=t("fec0::");
5756 local l=t("3ffe::");
5757 local u=t("::");
5758 local f=t("ff00::");
5759 local c=t("::ffff:0:0");
5760 local function y(t)
5761 if e(t,w,8)then
5762 return 2;
5763 elseif e(t,m,16)then
5764 return 2;
5765 else
5766 return 14;
5767 end
5768 end
5769 local function w(t)
5770 if t==n then
5771 return 2;
5772 elseif e(t,p,10)then
5773 return 2;
5774 elseif e(t,i,10)then
5775 return 5;
5776 elseif e(t,f,10)then
5777 return t.packed:byte(2)%16;
5778 else
5779 return 14;
5780 end
5781 end
5782 local function m(t)
5783 if t==n then
5784 return 0;
5785 elseif e(t,h,16)then
5786 return 2;
5787 elseif e(t,d,32)then
5788 return 5;
5789 elseif e(t,r,7)then
5790 return 13;
5791 elseif e(t,i,10)then
5792 return 11;
5793 elseif e(t,l,16)then
5794 return 12;
5795 elseif e(t,u,96)then
5796 return 3;
5797 elseif e(t,c,96)then
5798 return 4;
5799 else
5800 return 1;
5801 end
5802 end
5803 local function f(t)
5804 if t==n then
5805 return 50;
5806 elseif e(t,h,16)then
5807 return 30;
5808 elseif e(t,d,32)then
5809 return 5;
5810 elseif e(t,r,7)then
5811 return 3;
5812 elseif e(t,i,10)then
5813 return 1;
5814 elseif e(t,l,16)then
5815 return 1;
5816 elseif e(t,u,96)then
5817 return 1;
5818 elseif e(t,c,96)then
5819 return 35;
5820 else
5821 return 40;
5822 end
5823 end
5824 function a:toV4mapped()
5825 if self.proto~="IPv4"then return nil,"No IPv4 address"end
5826 local e=t("::ffff:"..self.normal);
5827 return e;
5828 end
5829 function a:label()
5830 if self.proto=="IPv4"then
5831 return m(self.toV4mapped);
5832 else
5833 return m(self);
5834 end
5835 end
5836 function a:precedence()
5837 if self.proto=="IPv4"then
5838 return f(self.toV4mapped);
5839 else
5840 return f(self);
5841 end
5842 end
5843 function a:scope()
5844 if self.proto=="IPv4"then
5845 return y(self);
5846 else
5847 return w(self);
5848 end
5849 end
5850 local r=t("10.0.0.0");
5851 local h=t("172.16.0.0");
5852 local i=t("192.168.0.0");
5853 local n=t("100.64.0.0");
5854 function a:private()
5855 local t=self.scope~=14;
5856 if not t and self.proto=="IPv4"then
5857 return e(self,r,8)or e(self,h,12)or e(self,i,16)or e(self,n,10);
5858 end
5859 return t;
5860 end
5861 local function n(e)
5862 local o;
5863 local a=e:find("/",1,true);
5864 if a then
5865 o=tonumber(e:sub(a+1,-1));
5866 e=e:sub(1,a-1);
5867 end
5868 return t(e),o;
5869 end
5870 function e(a,t,e)
5871 if not e or e>=128 or t.proto=="IPv4"and e>=32 then
5872 return a==t;
5873 elseif e<1 then
5874 return true;
5875 end
5876 if a.proto~=t.proto then
4475 if a.proto=="IPv4"then 5877 if a.proto=="IPv4"then
4476 a=a.toV4mapped; 5878 a=a.toV4mapped;
4477 end 5879 elseif t.proto=="IPv4"then
4478 a=(a.addr):upper(); 5880 t=t.toV4mapped;
4479 a:gsub("([^:]*):?",function(t)e[#e+1]=t end); 5881 e=e+(128-32);
4480 if not a:match(":$")then e[#e]=nil;end 5882 end
4481 for o,a in ipairs(e)do 5883 end
4482 if a:len()==0 and o~=1 and o~=#e then 5884 return a.bits:sub(1,e)==t.bits:sub(1,e);
4483 for e=1,16*(9-#e)do 5885 end
4484 t=t.."0"; 5886 local function i(e)
4485 end 5887 return getmetatable(e)==o;
4486 else 5888 end
4487 for e=1,4-a:len()do 5889 local function o(e,a)
4488 t=t.."0000"; 5890 if a%8~=0 then
4489 end 5891 return error("ip.truncate() only supports multiples of 8 bits");
4490 for e=1,a:len()do 5892 end
4491 t=t..n[a:sub(e,e)]; 5893 local a=a/8;
4492 end 5894 if not i(e)then
5895 e=t(e);
5896 end
5897 return t(s.ntop(e.packed:sub(1,a)..("\0"):rep(#e.packed-a)))
5898 end
5899 return{
5900 new_ip=t,
5901 commonPrefixLength=v,
5902 parse_cidr=n,
5903 match=e,
5904 is_ip=i;
5905 truncate=o;
5906 };
5907 end)
5908 package.preload['util.hex']=(function(...)
5909 local _ENV=_ENV;
5910 local function e(t,...)
5911 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
5912 package.loaded[t]=e;
5913 for t=1,select("#",...)do
5914 (select(t,...))(e);
5915 end
5916 _ENV=e;
5917 _M=e;
5918 return e;
5919 end
5920 local h=string.char;
5921 local s=string.format;
5922 local o=string.gsub;
5923 local n=string.lower;
5924 local a={};
5925 local i={};
5926 do
5927 local e,t;
5928 for o=0,255 do
5929 e,t=h(o),s("%02x",o);
5930 a[e]=t;
5931 i[t]=e;
5932 end
5933 end
5934 local function t(e)
5935 return(o(e,".",a));
5936 end
5937 local function e(e)
5938 return(o(n(e),"%X*(%x%x)%X*",i));
5939 end
5940 return{
5941 encode=t,decode=e;
5942 to=t,from=e;
5943 };
5944 end)
5945 package.preload['util.net']=(function(...)
5946 local _ENV=_ENV;
5947 local function e(t,...)
5948 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
5949 package.loaded[t]=e;
5950 for t=1,select("#",...)do
5951 (select(t,...))(e);
5952 end
5953 _ENV=e;
5954 _M=e;
5955 return e;
5956 end
5957 return{
5958 pton=function(e)
5959 if e:find":"then
5960 return"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
5961 else
5962 return"\0\0\0\0"
5963 end
5964 end
5965 }
5966 end)
5967 package.preload['util.sslconfig']=(function(...)
5968 local _ENV=_ENV;
5969 local function e(t,...)
5970 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
5971 package.loaded[t]=e;
5972 for t=1,select("#",...)do
5973 (select(t,...))(e);
5974 end
5975 _ENV=e;
5976 _M=e;
5977 return e;
5978 end
5979 local n=type;
5980 local o=pairs;
5981 local t=rawset;
5982 local r=rawget;
5983 local c=error;
5984 local d=table.concat;
5985 local l=table.insert;
5986 local u=setmetatable;
5987 local m=require"util.paths".resolve_relative_path;
5988 local _ENV=nil;
5989 local i={};
5990 local e={};
5991 local f=function(e)return e end
5992 function i.options(h,s,e)
5993 local a=h[s]or{};
5994 if n(e)~="table"then e={e}end
5995 for o,e in o(e)do
5996 if e==true or e==false then
5997 a[o]=e;
5998 else
5999 a[e]=true;
6000 end
6001 end
6002 t(h,s,a)
6003 end
6004 i.verifyext=i.options;
6005 function e.options(a)
6006 local t={};
6007 for e,a in o(a)do
6008 if a then
6009 t[#t+1]=e;
4493 end 6010 end
4494 end 6011 end
4495 return t; 6012 return t;
4496 end 6013 end
4497 local function t(a,t) 6014 e.verifyext=e.options;
4498 a,t=i(a),i(t); 6015 function e.ciphers(t)
4499 for e=1,128 do 6016 if n(t)=="table"then
4500 if a:sub(e,e)~=t:sub(e,e)then 6017 return d(t,":");
4501 return e-1; 6018 end
4502 end 6019 return t;
4503 end 6020 end
4504 return 128; 6021 e.curveslist=e.ciphers;
4505 end 6022 e.ciphersuites=e.ciphers;
4506 local function h(t) 6023 function e.key(t,a)
6024 if n(t)=="string"then
6025 return m(a._basedir,t);
6026 else
6027 return nil
6028 end
6029 end
6030 e.certificate=e.key;
6031 e.cafile=e.key;
6032 e.capath=e.key;
6033 e.dhparam=e.key;
6034 local a={"sslv2","sslv3","tlsv1","tlsv1_1","tlsv1_2","tlsv1_3"};
6035 for e=1,#a do a[a[e].."+"]=e-1;end
6036 local function h(e)
6037 local t=a[e.protocol];
6038 if t then
6039 e.protocol="sslv23";
6040 for t=1,t do
6041 l(e.options,"no_"..a[t]);
6042 end
6043 end
6044 end
6045 local function s(e,a)
6046 t(e,"_cache",nil);
6047 if n(a)=="table"then
6048 for a,o in o(a)do
6049 if a:sub(1,1)~="_"then
6050 (i[a]or t)(e,a,o);
6051 end
6052 end
6053 end
6054 return e
6055 end
6056 local function n(i)
6057 local a={};
6058 for t,o in o(i)do
6059 if t:sub(1,1)~="_"then
6060 a[t]=(e[t]or f)(o,i);
6061 end
6062 end
6063 h(a);
6064 return a;
6065 end
6066 local function i(e)
6067 local a=r(e,"_cache");
6068 if a then
6069 return a,nil
6070 end
6071 local a,o=r(e,"_context_factory")(e:final(),e);
6072 if a then
6073 t(e,"_cache",a);
6074 end
6075 return a,o
6076 end
6077 local e={
6078 __index={
6079 apply=s;
6080 final=n;
6081 build=i;
6082 };
6083 __newindex=function()
6084 c("SSL config objects cannot be modified directly. Use :apply()")
6085 end;
6086 };
6087 local function a(t,a)
6088 return u({
6089 _context_factory=t,
6090 _basedir=a,
6091 options={},
6092 },e);
6093 end
6094 local function h(i)
6095 local a=a();
6096 for e,o in o(i)do
6097 t(a,e,o);
6098 end
6099 return a
6100 end
6101 e.__index.clone=h;
6102 return{
6103 apply=s;
6104 final=n;
6105 _new=a;
6106 };
6107 end)
6108 package.preload['util.paths']=(function(...)
6109 local _ENV=_ENV;
6110 local function e(t,...)
6111 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
6112 package.loaded[t]=e;
6113 for t=1,select("#",...)do
6114 (select(t,...))(e);
6115 end
6116 _ENV=e;
6117 _M=e;
6118 return e;
6119 end
6120 local i=table.concat;
6121 local t=package.config:sub(1,1);
6122 local a={}
6123 function a.resolve_relative_path(a,e)
6124 if e then
6125 a=a:gsub("%"..t.."+$","");
6126 e=e:gsub("^%.%"..t.."+","");
6127 local o;
6128 if t=="/"and e:sub(1,1)~="/"then
6129 o=true;
6130 elseif t=="\\"and(e:sub(1,1)~="/"and(e:sub(2,3)~=":\\"and e:sub(2,3)~=":/"))then
6131 o=true;
6132 end
6133 if o then
6134 return a..t..e;
6135 end
6136 end
6137 return e;
6138 end
6139 function a.glob_to_pattern(e)
6140 return"^"..e:gsub("[%p*?]",function(e)
6141 if e=="*"then
6142 return".*";
6143 elseif e=="?"then
6144 return".";
6145 else
6146 return"%"..e;
6147 end
6148 end).."$";
6149 end
6150 function a.join(a,e,o,...)
6151 if e then
6152 if o then
6153 if...then
6154 return i({a,e,o,...},t);
6155 end
6156 return a..t..e..t..o;
6157 end
6158 return a..t..e;
6159 end
6160 return a;
6161 end
6162 function a.complement_lua_path(t)
6163 local a=_VERSION:match(" (.+)$");
6164 local o=package.config:sub(3,3);
6165 local e=package.config:sub(1,1);
6166 local a=e.."lua"..e..a..e;
6167 if not string.find(package.path,t,1,true)then
6168 package.path=package.path..o..t..e.."share"..a.."?.lua";
6169 package.path=package.path..o..t..e.."share"..a.."?"..e.."init.lua";
6170 end
6171 if not string.find(package.path,t,1,true)then
6172 package.cpath=package.cpath..o..t..e.."lib"..a.."?.so";
6173 end
6174 end
6175 return a;
6176 end)
6177 package.preload['util.mathcompat']=(function(...)
6178 local _ENV=_ENV;
6179 local function e(t,...)
6180 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
6181 package.loaded[t]=e;
6182 for t=1,select("#",...)do
6183 (select(t,...))(e);
6184 end
6185 _ENV=e;
6186 _M=e;
6187 return e;
6188 end
6189 if not math.type then
6190 local function t(e)
6191 if type(e)=="number"then
6192 if e%1==0 and e~=e+1 and e~=e-1 then
6193 return"integer"
6194 else
6195 return"float"
6196 end
6197 end
6198 end
6199 _G.math.type=t
6200 end
6201 end)
6202 package.preload['util.dnsregistry']=(function(...)
6203 local _ENV=_ENV;
6204 local function e(t,...)
6205 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
6206 package.loaded[t]=e;
6207 for t=1,select("#",...)do
6208 (select(t,...))(e);
6209 end
6210 _ENV=e;
6211 _M=e;
6212 return e;
6213 end
6214 return{
6215 classes={
6216 ["IN"]=1;[1]="IN";
6217 ["CH"]=3;[3]="CH";
6218 ["HS"]=4;[4]="HS";
6219 ["ANY"]=255;[255]="ANY";
6220 };
6221 types={
6222 ["A"]=1;[1]="A";
6223 ["NS"]=2;[2]="NS";
6224 ["MD"]=3;[3]="MD";
6225 ["MF"]=4;[4]="MF";
6226 ["CNAME"]=5;[5]="CNAME";
6227 ["SOA"]=6;[6]="SOA";
6228 ["MB"]=7;[7]="MB";
6229 ["MG"]=8;[8]="MG";
6230 ["MR"]=9;[9]="MR";
6231 ["NULL"]=10;[10]="NULL";
6232 ["WKS"]=11;[11]="WKS";
6233 ["PTR"]=12;[12]="PTR";
6234 ["HINFO"]=13;[13]="HINFO";
6235 ["MINFO"]=14;[14]="MINFO";
6236 ["MX"]=15;[15]="MX";
6237 ["TXT"]=16;[16]="TXT";
6238 ["RP"]=17;[17]="RP";
6239 ["AFSDB"]=18;[18]="AFSDB";
6240 ["X25"]=19;[19]="X25";
6241 ["ISDN"]=20;[20]="ISDN";
6242 ["RT"]=21;[21]="RT";
6243 ["NSAP"]=22;[22]="NSAP";
6244 ["NSAP-PTR"]=23;[23]="NSAP-PTR";
6245 ["SIG"]=24;[24]="SIG";
6246 ["KEY"]=25;[25]="KEY";
6247 ["PX"]=26;[26]="PX";
6248 ["GPOS"]=27;[27]="GPOS";
6249 ["AAAA"]=28;[28]="AAAA";
6250 ["LOC"]=29;[29]="LOC";
6251 ["NXT"]=30;[30]="NXT";
6252 ["EID"]=31;[31]="EID";
6253 ["NIMLOC"]=32;[32]="NIMLOC";
6254 ["SRV"]=33;[33]="SRV";
6255 ["ATMA"]=34;[34]="ATMA";
6256 ["NAPTR"]=35;[35]="NAPTR";
6257 ["KX"]=36;[36]="KX";
6258 ["CERT"]=37;[37]="CERT";
6259 ["A6"]=38;[38]="A6";
6260 ["DNAME"]=39;[39]="DNAME";
6261 ["SINK"]=40;[40]="SINK";
6262 ["OPT"]=41;[41]="OPT";
6263 ["APL"]=42;[42]="APL";
6264 ["DS"]=43;[43]="DS";
6265 ["SSHFP"]=44;[44]="SSHFP";
6266 ["IPSECKEY"]=45;[45]="IPSECKEY";
6267 ["RRSIG"]=46;[46]="RRSIG";
6268 ["NSEC"]=47;[47]="NSEC";
6269 ["DNSKEY"]=48;[48]="DNSKEY";
6270 ["DHCID"]=49;[49]="DHCID";
6271 ["NSEC3"]=50;[50]="NSEC3";
6272 ["NSEC3PARAM"]=51;[51]="NSEC3PARAM";
6273 ["TLSA"]=52;[52]="TLSA";
6274 ["SMIMEA"]=53;[53]="SMIMEA";
6275 ["HIP"]=55;[55]="HIP";
6276 ["NINFO"]=56;[56]="NINFO";
6277 ["RKEY"]=57;[57]="RKEY";
6278 ["TALINK"]=58;[58]="TALINK";
6279 ["CDS"]=59;[59]="CDS";
6280 ["CDNSKEY"]=60;[60]="CDNSKEY";
6281 ["OPENPGPKEY"]=61;[61]="OPENPGPKEY";
6282 ["CSYNC"]=62;[62]="CSYNC";
6283 ["ZONEMD"]=63;[63]="ZONEMD";
6284 ["SVCB"]=64;[64]="SVCB";
6285 ["HTTPS"]=65;[65]="HTTPS";
6286 ["SPF"]=99;[99]="SPF";
6287 ["NID"]=104;[104]="NID";
6288 ["L32"]=105;[105]="L32";
6289 ["L64"]=106;[106]="L64";
6290 ["LP"]=107;[107]="LP";
6291 ["EUI48"]=108;[108]="EUI48";
6292 ["EUI64"]=109;[109]="EUI64";
6293 ["TKEY"]=249;[249]="TKEY";
6294 ["TSIG"]=250;[250]="TSIG";
6295 ["IXFR"]=251;[251]="IXFR";
6296 ["AXFR"]=252;[252]="AXFR";
6297 ["MAILB"]=253;[253]="MAILB";
6298 ["MAILA"]=254;[254]="MAILA";
6299 ["*"]=255;[255]="*";
6300 ["URI"]=256;[256]="URI";
6301 ["CAA"]=257;[257]="CAA";
6302 ["AVC"]=258;[258]="AVC";
6303 ["DOA"]=259;[259]="DOA";
6304 ["AMTRELAY"]=260;[260]="AMTRELAY";
6305 ["TA"]=32768;[32768]="TA";
6306 ["DLV"]=32769;[32769]="DLV";
6307 };
6308 errors={
6309 [0]="NoError";["NoError"]="No Error";
6310 [1]="FormErr";["FormErr"]="Format Error";
6311 [2]="ServFail";["ServFail"]="Server Failure";
6312 [3]="NXDomain";["NXDomain"]="Non-Existent Domain";
6313 [4]="NotImp";["NotImp"]="Not Implemented";
6314 [5]="Refused";["Refused"]="Query Refused";
6315 [6]="YXDomain";["YXDomain"]="Name Exists when it should not";
6316 [7]="YXRRSet";["YXRRSet"]="RR Set Exists when it should not";
6317 [8]="NXRRSet";["NXRRSet"]="RR Set that should exist does not";
6318 [9]="NotAuth";["NotAuth"]="Server Not Authoritative for zone";
6319 [10]="NotZone";["NotZone"]="Name not contained in zone";
6320 [11]="DSOTYPENI";["DSOTYPENI"]="DSO-TYPE Not Implemented";
6321 [16]="BADVERS";["BADVERS"]="Bad OPT Version";
6322 [17]="BADKEY";["BADKEY"]="Key not recognized";
6323 [18]="BADTIME";["BADTIME"]="Signature out of time window";
6324 [19]="BADMODE";["BADMODE"]="Bad TKEY Mode";
6325 [20]="BADNAME";["BADNAME"]="Duplicate key name";
6326 [21]="BADALG";["BADALG"]="Algorithm not supported";
6327 [22]="BADTRUNC";["BADTRUNC"]="Bad Truncation";
6328 [23]="BADCOOKIE";["BADCOOKIE"]="Bad/missing Server Cookie";
6329 };
6330 };
6331 end)
6332 package.preload['net.tls_luasec']=(function(...)
6333 local _ENV=_ENV;
6334 local function e(t,...)
6335 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
6336 package.loaded[t]=e;
6337 for t=1,select("#",...)do
6338 (select(t,...))(e);
6339 end
6340 _ENV=e;
6341 _M=e;
6342 return e;
6343 end
6344 local t=require"ssl";
6345 local h=t.newcontext;
6346 local s=t.context or require"ssl.context";
6347 local o=io.open;
4507 local e={}; 6348 local e={};
4508 t:gsub("([^.]*).?",function(t)e[#e+1]=tonumber(t)end); 6349 local n={__index=e};
4509 if e[1]==127 then 6350 function e:set_sni_host(a,t,e)
4510 return 2; 6351 local e,t=self._builder:clone():apply({
4511 elseif e[1]==169 and e[2]==254 then 6352 certificate=t,
4512 return 2; 6353 key=e,
4513 else 6354 }):build();
4514 return 14; 6355 if not e then
4515 end 6356 return false,t
4516 end 6357 end
4517 local function r(e) 6358 self._sni_contexts[a]=e._inner
4518 if e:match("^[0:]*1$")then 6359 return true,nil
4519 return 2; 6360 end
4520 elseif e:match("^[Ff][Ee][89ABab]")then 6361 function e:remove_sni_host(e)
4521 return 2; 6362 self._sni_contexts[e]=nil
4522 elseif e:match("^[Ff][Ee][CcDdEeFf]")then 6363 end
4523 return 5; 6364 function e:wrap(e)
4524 elseif e:match("^[Ff][Ff]")then 6365 local e,t,a=pcall(t.wrap,e,self._inner);
4525 return tonumber("0x"..e:sub(4,4)); 6366 if not e then
4526 else 6367 return nil,a
4527 return 14; 6368 end
4528 end 6369 return t,nil
4529 end 6370 end
4530 local function i(a) 6371 local function i(e,i)
4531 if t(a,e("::1","IPv6"))==128 then 6372 if type(e.dhparam)=="string"then
4532 return 0; 6373 local t,a=o(e.dhparam);
4533 elseif t(a,e("2002::","IPv6"))>=16 then 6374 if not t then return nil,"Could not open DH parameters: "..a end
4534 return 2; 6375 local a=t:read("*a");
4535 elseif t(a,e("2001::","IPv6"))>=32 then 6376 t:close();
4536 return 5; 6377 e.dhparam=function()return a;end
4537 elseif t(a,e("fc00::","IPv6"))>=7 then 6378 end
4538 return 13; 6379 local t,a=h(e);
4539 elseif t(a,e("fec0::","IPv6"))>=10 then 6380 if not t then
4540 return 11; 6381 return nil,a
4541 elseif t(a,e("3ffe::","IPv6"))>=16 then 6382 end
4542 return 12; 6383 if t and e.ciphers then
4543 elseif t(a,e("::","IPv6"))>=96 then 6384 local o;
4544 return 3; 6385 o,a=s.setcipher(t,e.ciphers);
4545 elseif t(a,e("::ffff:0:0","IPv6"))>=96 then 6386 if not o then
4546 return 4; 6387 return nil,a
4547 else 6388 end
4548 return 1; 6389 end
4549 end 6390 return setmetatable({
4550 end 6391 _inner=t,
4551 local function n(a) 6392 _builder=i,
4552 if t(a,e("::1","IPv6"))==128 then 6393 _sni_contexts={},
4553 return 50; 6394 },n),nil
4554 elseif t(a,e("2002::","IPv6"))>=16 then 6395 end
4555 return 30; 6396 return{
4556 elseif t(a,e("2001::","IPv6"))>=32 then 6397 new_context=i,
4557 return 5; 6398 };
4558 elseif t(a,e("fc00::","IPv6"))>=7 then
4559 return 3;
4560 elseif t(a,e("fec0::","IPv6"))>=10 then
4561 return 1;
4562 elseif t(a,e("3ffe::","IPv6"))>=16 then
4563 return 1;
4564 elseif t(a,e("::","IPv6"))>=96 then
4565 return 1;
4566 elseif t(a,e("::ffff:0:0","IPv6"))>=96 then
4567 return 35;
4568 else
4569 return 40;
4570 end
4571 end
4572 local function s(o)
4573 local a={};
4574 local t="::ffff:";
4575 o:gsub("([^.]*).?",function(e)a[#a+1]=tonumber(e)end);
4576 t=t..("%02x"):format(a[1]);
4577 t=t..("%02x"):format(a[2]);
4578 t=t..":"
4579 t=t..("%02x"):format(a[3]);
4580 t=t..("%02x"):format(a[4]);
4581 return e(t,"IPv6");
4582 end
4583 function o:toV4mapped()
4584 if self.proto~="IPv4"then return nil,"No IPv4 address"end
4585 local e=s(self.addr);
4586 self.toV4mapped=e;
4587 return e;
4588 end
4589 function o:label()
4590 local e;
4591 if self.proto=="IPv4"then
4592 e=i(self.toV4mapped);
4593 else
4594 e=i(self);
4595 end
4596 self.label=e;
4597 return e;
4598 end
4599 function o:precedence()
4600 local e;
4601 if self.proto=="IPv4"then
4602 e=n(self.toV4mapped);
4603 else
4604 e=n(self);
4605 end
4606 self.precedence=e;
4607 return e;
4608 end
4609 function o:scope()
4610 local e;
4611 if self.proto=="IPv4"then
4612 e=h(self.addr);
4613 else
4614 e=r(self.addr);
4615 end
4616 self.scope=e;
4617 return e;
4618 end
4619 return{new_ip=e,
4620 commonPrefixLength=t};
4621 end) 6399 end)
4622 package.preload['util.sasl.scram']=(function(...) 6400 package.preload['util.sasl.scram']=(function(...)
4623 local _ENV=_ENV; 6401 local _ENV=_ENV;
4624 local function e(t,...) 6402 local function e(t,...)
4625 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 6403 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
4629 end 6407 end
4630 _ENV=e; 6408 _ENV=e;
4631 _M=e; 6409 _M=e;
4632 return e; 6410 return e;
4633 end 6411 end
4634 local n,u=require"mime".b64,require"mime".unb64; 6412 local n,m=require"mime".b64,require"mime".unb64;
4635 local e=require"util.hashes"; 6413 local t=require"util.hashes";
4636 local a=require"bit"; 6414 local a=require"bit";
4637 local s=require"util.random"; 6415 local d=require"util.random";
4638 local d=tonumber; 6416 local p=tonumber;
4639 local h,t=string.char,string.byte; 6417 local s,e=string.char,string.byte;
4640 local i=string.gsub; 6418 local i=string.gsub;
4641 local r=a.bxor; 6419 local h=a.bxor;
4642 local function l(e,o) 6420 local function r(a,t)
4643 return(i(e,"()(.)",function(a,e) 6421 return(i(a,"()(.)",function(a,o)
4644 return h(r(t(e),t(o,a))) 6422 return s(h(e(o),e(t,a)))
4645 end)); 6423 end));
4646 end 6424 end
4647 local y,t=e.sha1,e.hmac_sha1; 6425 local f,t=t.sha1,t.hmac_sha1;
4648 local function w(o,e,i) 6426 local function w(o,e,i)
4649 local e=t(o,e.."\0\0\0\1"); 6427 local e=t(o,e.."\0\0\0\1");
4650 local a=e; 6428 local a=e;
4651 for i=2,i do 6429 for i=2,i do
4652 e=t(o,e); 6430 e=t(o,e);
4653 a=l(a,e); 6431 a=r(a,e);
4654 end 6432 end
4655 return a; 6433 return a;
4656 end 6434 end
4657 local function p(e) 6435 local function y(e)
4658 return e; 6436 return e;
4659 end 6437 end
4660 local function a(e) 6438 local function a(e)
4661 return(i(e,"[,=]",{[","]="=2C",["="]="=3D"})); 6439 return(i(e,"[,=]",{[","]="=2C",["="]="=3D"}));
4662 end 6440 end
4663 local function f(e,i) 6441 local function s(e)
6442 if e:ssl()then
6443 local e=e:socket();
6444 if e.info and e:info().protocol=="TLSv1.3"then
6445 if e.exportkeyingmaterial then
6446 return"p=tls-exporter",e:exportkeyingmaterial("EXPORTER-Channel-Binding",32,"");
6447 end
6448 elseif e.getfinished then
6449 return"p=tls-unique",e:getfinished();
6450 end
6451 end
6452 end
6453 local function h(e,o)
4664 local a="n="..a(e.username); 6454 local a="n="..a(e.username);
4665 local h=n(s.bytes(15)); 6455 local i=n(d.bytes(15));
4666 local c="r="..h; 6456 local h="r="..i;
4667 local m=a..","..c; 6457 local l=a..","..h;
4668 local o=""; 6458 local d="";
4669 local a=e.conn:ssl()and"y"or"n"; 6459 local a="n";
4670 if i=="SCRAM-SHA-1-PLUS"then 6460 if o=="SCRAM-SHA-1-PLUS"then
4671 o=e.conn:socket():getfinished(); 6461 a,d=s(e.conn);
4672 a="p=tls-unique"; 6462 elseif s(e.conn)then
6463 a="y";
4673 end 6464 end
4674 local s=a..",,"; 6465 local s=a..",,";
4675 local a=s..m; 6466 local a=s..l;
4676 local a,r=coroutine.yield(a); 6467 local a,u=coroutine.yield(a);
4677 if a~="challenge"then return false end 6468 if a~="challenge"then return false end
4678 local a,i,f=r:match("(r=[^,]+),s=([^,]*),i=(%d+)"); 6469 local a,o,c=u:match("(r=[^,]+),s=([^,]*),i=(%d+)");
4679 local d=d(f); 6470 local c=p(c);
4680 i=u(i); 6471 o=m(o);
4681 if not a or not i or not d then 6472 if not a or not o or not c then
4682 return false,"Could not parse server_first_message"; 6473 return false,"Could not parse server_first_message";
4683 elseif a:find(h,3,true)~=3 then 6474 elseif a:find(i,3,true)~=3 then
4684 return false,"nonce sent by server does not match our nonce"; 6475 return false,"nonce sent by server does not match our nonce";
4685 elseif a==c then 6476 elseif a==h then
4686 return false,"server did not append s-nonce to nonce"; 6477 return false,"server did not append s-nonce to nonce";
4687 end 6478 end
4688 local o=s..o; 6479 local i=s..d;
4689 local o="c="..n(o); 6480 local i="c="..n(i);
4690 local h=o..","..a; 6481 local h=i..","..a;
4691 local o; 6482 local i;
4692 local a; 6483 local a;
4693 local s; 6484 local s;
4694 if e.client_key and e.server_key then 6485 if e.client_key and e.server_key then
4695 a=e.client_key; 6486 a=e.client_key;
4696 s=e.server_key; 6487 s=e.server_key;
4697 else 6488 else
4698 if e.salted_password then 6489 if e.salted_password then
4699 o=e.salted_password; 6490 i=e.salted_password;
4700 elseif e.password then 6491 elseif e.password then
4701 o=w(p(e.password),i,d); 6492 i=w(y(e.password),o,c);
4702 end 6493 end
4703 s=t(o,"Server Key"); 6494 s=t(i,"Server Key");
4704 a=t(o,"Client Key"); 6495 a=t(i,"Client Key");
4705 end 6496 end
4706 local o=y(a); 6497 local o=f(a);
4707 local e=m..","..r..","..h; 6498 local e=l..","..u..","..h;
4708 local o=t(o,e); 6499 local o=t(o,e);
4709 local a=l(a,o); 6500 local a=r(a,o);
4710 local t=t(s,e); 6501 local t=t(s,e);
4711 local e="p="..n(a); 6502 local e="p="..n(a);
4712 local e=h..","..e; 6503 local e=h..","..e;
4713 local e,a=coroutine.yield(e); 6504 local e,a=coroutine.yield(e);
4714 if e~="success"then return false,"success-expected"end 6505 if e~="success"then return false,"success-expected"end
4715 local e=a:match("v=([^,]+)"); 6506 local e=a:match("v=([^,]+)");
4716 if u(e)~=t then 6507 if m(e)~=t then
4717 return false,"server signature did not match"; 6508 return false,"server signature did not match";
4718 end 6509 end
4719 return true; 6510 return true;
4720 end 6511 end
4721 return function(e,t) 6512 return function(e,t)
4722 if e.username and(e.password or(e.client_key or e.server_key))then 6513 if e.username and(e.password or(e.client_key or e.server_key))then
4723 if t=="SCRAM-SHA-1"then 6514 if t=="SCRAM-SHA-1"then
4724 return f,99; 6515 return h,99;
4725 elseif t=="SCRAM-SHA-1-PLUS"then 6516 elseif t=="SCRAM-SHA-1-PLUS"then
4726 local e=e.conn:ssl()and e.conn:socket(); 6517 if s(e.conn)then
4727 if e and e.getfinished then 6518 return h,100;
4728 return f,100;
4729 end 6519 end
4730 end 6520 end
4731 end 6521 end
4732 end 6522 end
4733 end) 6523 end)
4766 return function(t,e) 6556 return function(t,e)
4767 if e=="ANONYMOUS"then 6557 if e=="ANONYMOUS"then
4768 return function() 6558 return function()
4769 return coroutine.yield()=="success"; 6559 return coroutine.yield()=="success";
4770 end,0; 6560 end,0;
6561 end
6562 end
6563 end)
6564 package.preload['util.sasl.oauthbearer']=(function(...)
6565 local _ENV=_ENV;
6566 local function e(t,...)
6567 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
6568 package.loaded[t]=e;
6569 for t=1,select("#",...)do
6570 (select(t,...))(e);
6571 end
6572 _ENV=e;
6573 _M=e;
6574 return e;
6575 end
6576 return function(a,e)
6577 if e=="OAUTHBEARER"and a.username then
6578 return function(e)
6579 local t=e.bearer_token and("Bearer "..e.bearer_token)or"";
6580 local t,a=coroutine.yield("n,a="..e.username.."@"..e.host..",\001auth="..t.."\001");
6581 if t=="success"then
6582 return true;
6583 elseif t=="challenge"then
6584 e:event("oauth-failure",{
6585 json=a;
6586 });
6587 if coroutine.yield("\001")~="failure"then
6588 error("Unexpected SASL state: expected failure after challenge");
6589 end
6590 return false;
6591 end
6592 end,a.bearer_token and 6 or 4;
4771 end 6593 end
4772 end 6594 end
4773 end) 6595 end)
4774 package.preload['verse.plugins.tls']=(function(...) 6596 package.preload['verse.plugins.tls']=(function(...)
4775 local _ENV=_ENV; 6597 local _ENV=_ENV;
4791 if o:get_child("starttls",t)and e.conn.starttls then 6613 if o:get_child("starttls",t)and e.conn.starttls then
4792 e:debug("Negotiating TLS..."); 6614 e:debug("Negotiating TLS...");
4793 e:send(a.stanza("starttls",{xmlns=t})); 6615 e:send(a.stanza("starttls",{xmlns=t}));
4794 return true; 6616 return true;
4795 elseif not e.conn.starttls and not e.secure then 6617 elseif not e.conn.starttls and not e.secure then
4796 e:warn("SSL libary (LuaSec) not loaded, so TLS not available"); 6618 e:warn("SSL library (LuaSec) not loaded, so TLS not available");
4797 elseif not e.secure then 6619 elseif not e.secure then
4798 e:debug("Server doesn't offer TLS :("); 6620 e:debug("Server doesn't offer TLS :(");
4799 end 6621 end
4800 end 6622 end
4801 local function o(t) 6623 local function o(t)
4802 if t.name=="proceed"then 6624 if t.name=="proceed"then
4803 e:debug("Server says proceed, handshake starting..."); 6625 e:debug("Server says proceed, handshake starting...");
4804 e.conn:starttls(e.ssl or{mode="client",protocol="sslv23",options="no_sslv2",capath="/etc/ssl/certs"},true); 6626 local t=a.tls_builder(".")
6627 :apply({mode="client",protocol="sslv23",options="no_sslv2",capath="/etc/ssl/certs"})
6628 :apply(e.ssl or{});
6629 e.conn:starttls(t:build(),true);
4805 end 6630 end
4806 end 6631 end
4807 local function a(t) 6632 local function a(t)
4808 if t=="ssl-handshake-complete"then 6633 if t=="ssl-handshake-complete"then
4809 e.secure=true; 6634 e.secure=true;
4828 _ENV=e; 6653 _ENV=e;
4829 _M=e; 6654 _M=e;
4830 return e; 6655 return e;
4831 end 6656 end
4832 local i=require"verse"; 6657 local i=require"verse";
4833 local s,h=require"mime".b64,require"mime".unb64; 6658 local h,r=require"mime".b64,require"mime".unb64;
4834 local a="urn:ietf:params:xml:ns:xmpp-sasl"; 6659 local o="urn:ietf:params:xml:ns:xmpp-sasl";
4835 function i.plugins.sasl(e) 6660 function i.plugins.sasl(e)
4836 local function r(t) 6661 local function d(t)
4837 if e.authenticated then return;end 6662 if e.authenticated then return;end
4838 e:debug("Authenticating with SASL..."); 6663 e:debug("Authenticating with SASL...");
4839 local t=t:get_child("mechanisms",a); 6664 local t=t:get_child("mechanisms",o);
4840 if not t then return end 6665 if not t then return end
4841 local o={}; 6666 local a={};
4842 local n={}; 6667 local n={};
6668 local s={};
4843 for t in t:childtags("mechanism")do 6669 for t in t:childtags("mechanism")do
4844 t=t:get_text(); 6670 t=t:get_text();
4845 e:debug("Server offers %s",t); 6671 e:debug("Server offers %s",t);
4846 if not o[t]then 6672 s[t]=true;
6673 if not a[t]then
4847 local i=t:match("[^-]+"); 6674 local i=t:match("[^-]+");
4848 local s,a=pcall(require,"util.sasl."..i:lower()); 6675 local s,o=pcall(require,"util.sasl."..i:lower());
4849 if s then 6676 if s then
4850 e:debug("Loaded SASL %s module",i); 6677 e:debug("Loaded SASL %s module",i);
4851 o[t],n[t]=a(e,t); 6678 a[t],n[t]=o(e,t);
4852 elseif not tostring(a):match("not found")then 6679 elseif not tostring(o):match("not found")then
4853 e:debug("Loading failed: %s",tostring(a)); 6680 e:debug("Loading failed: %s",tostring(o));
4854 end 6681 end
4855 end 6682 end
4856 end 6683 end
4857 local t={}; 6684 local t={};
4858 for e in pairs(o)do 6685 for e in pairs(a)do
4859 table.insert(t,e); 6686 table.insert(t,e);
4860 end 6687 end
4861 if not t[1]then 6688 if not t[1]then
4862 e:event("authentication-failure",{condition="no-supported-sasl-mechanisms"}); 6689 e:event("authentication-failure",{condition="no-supported-sasl-mechanisms",mechanisms=s});
4863 e:close(); 6690 e:close();
4864 return; 6691 return;
4865 end 6692 end
4866 table.sort(t,function(t,e)return n[t]>n[e];end); 6693 table.sort(t,function(t,e)return n[t]>n[e];end);
4867 local t,n=t[1]; 6694 local t,n=t[1];
4868 e:debug("Selecting %s mechanism...",t); 6695 e:debug("Selecting %s mechanism...",t);
4869 e.sasl_mechanism=coroutine.wrap(o[t]); 6696 e.sasl_mechanism=coroutine.wrap(a[t]);
4870 n=e:sasl_mechanism(t); 6697 n=e:sasl_mechanism(t);
4871 local t=i.stanza("auth",{xmlns=a,mechanism=t}); 6698 local t=i.stanza("auth",{xmlns=o,mechanism=t});
4872 if n then 6699 if n then
4873 t:text(s(n)); 6700 t:text(h(n));
4874 end 6701 end
4875 e:send(t); 6702 e:send(t);
4876 return true; 6703 return true;
4877 end 6704 end
4878 local function o(t) 6705 local function a(t)
4879 if t.name=="failure"then 6706 if t.name=="failure"then
4880 local a=t.tags[1]; 6707 local a=t.tags[1];
4881 local t=t:get_child_text("text"); 6708 local t=t:get_child_text("text");
4882 e:event("authentication-failure",{condition=a.name,text=t}); 6709 e:event("authentication-failure",{condition=a.name,text=t});
4883 e:close(); 6710 e:close();
4884 return false; 6711 return false;
4885 end 6712 end
4886 local t,o=e.sasl_mechanism(t.name,h(t:get_text())); 6713 local t,a=e.sasl_mechanism(t.name,r(t:get_text()));
4887 if not t then 6714 if not t then
4888 e:event("authentication-failure",{condition=o}); 6715 e:event("authentication-failure",{condition=a});
4889 e:close(); 6716 e:close();
4890 return false; 6717 return false;
4891 elseif t==true then 6718 elseif t==true then
4892 e:event("authentication-success"); 6719 e:event("authentication-success");
4893 e.authenticated=true 6720 e.authenticated=true
4894 e:reopen(); 6721 e:reopen();
4895 else 6722 else
4896 e:send(i.stanza("response",{xmlns=a}):text(s(t))); 6723 e:send(i.stanza("response",{xmlns=o}):text(h(t)));
4897 end 6724 end
4898 return true; 6725 return true;
4899 end 6726 end
4900 e:hook("stream-features",r,300); 6727 e:hook("stream-features",d,300);
4901 e:hook("stream/"..a,o); 6728 e:hook("stream/"..o,a);
4902 return true; 6729 return true;
4903 end 6730 end
4904 end) 6731 end)
4905 package.preload['verse.plugins.bind']=(function(...) 6732 package.preload['verse.plugins.bind']=(function(...)
4906 local _ENV=_ENV; 6733 local _ENV=_ENV;
4919 local a="urn:ietf:params:xml:ns:xmpp-bind"; 6746 local a="urn:ietf:params:xml:ns:xmpp-bind";
4920 function t.plugins.bind(e) 6747 function t.plugins.bind(e)
4921 local function o(o) 6748 local function o(o)
4922 if e.bound then return;end 6749 if e.bound then return;end
4923 e:debug("Binding resource..."); 6750 e:debug("Binding resource...");
4924 e:send_iq(t.iq({type="set"}):tag("bind",{xmlns=a}):tag("resource"):text(e.resource), 6751 e:send_iq(t.iq({id="bind",type="set"}):tag("bind",{xmlns=a}):tag("resource"):text(e.resource),
4925 function(t) 6752 function(t)
4926 if t.attr.type=="result"then 6753 if t.attr.type=="result"then
4927 local t=t 6754 local t=t
4928 :get_child("bind",a) 6755 :get_child("bind",a)
4929 :get_child_text("jid"); 6756 :get_child_text("jid");
4930 e.username,e.host,e.resource=i.split(t); 6757 e.username,e.host,e.resource=i.split(t);
4931 e.jid,e.bound=t,true; 6758 e.jid,e.bound=t,true;
4932 e:event("bind-success",{jid=t}); 6759 e:event("bind-success",{jid=t});
4933 elseif t.attr.type=="error"then 6760 elseif t.attr.type=="error"then
4934 local a=t:child_with_name("error"); 6761 local a=t:child_with_name("error");
4935 local o,a,t=t:get_error(); 6762 local t,a,o=t:get_error();
4936 e:event("bind-failure",{error=a,text=t,type=o}); 6763 e:event("bind-failure",{error=a,text=o,type=t});
4937 end 6764 end
4938 end); 6765 end);
4939 end 6766 end
4940 e:hook("stream-features",o,200); 6767 e:hook("stream-features",o,200);
4941 return true; 6768 return true;
4942 end
4943 end)
4944 package.preload['verse.plugins.session']=(function(...)
4945 local _ENV=_ENV;
4946 local function e(t,...)
4947 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
4948 package.loaded[t]=e;
4949 for t=1,select("#",...)do
4950 (select(t,...))(e);
4951 end
4952 _ENV=e;
4953 _M=e;
4954 return e;
4955 end
4956 local a=require"verse";
4957 local o="urn:ietf:params:xml:ns:xmpp-session";
4958 function a.plugins.session(e)
4959 local function n(t)
4960 local t=t:get_child("session",o);
4961 if t and not t:get_child("optional")then
4962 local function i(t)
4963 e:debug("Establishing Session...");
4964 e:send_iq(a.iq({type="set"}):tag("session",{xmlns=o}),
4965 function(t)
4966 if t.attr.type=="result"then
4967 e:event("session-success");
4968 elseif t.attr.type=="error"then
4969 local t,a,o=t:get_error();
4970 e:event("session-failure",{error=a,text=o,type=t});
4971 end
4972 end);
4973 return true;
4974 end
4975 e:hook("bind-success",i);
4976 end
4977 end
4978 e:hook("stream-features",n);
4979 return true;
4980 end
4981 end)
4982 package.preload['verse.plugins.legacy']=(function(...)
4983 local _ENV=_ENV;
4984 local function e(t,...)
4985 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
4986 package.loaded[t]=e;
4987 for t=1,select("#",...)do
4988 (select(t,...))(e);
4989 end
4990 _ENV=e;
4991 _M=e;
4992 return e;
4993 end
4994 local o=require"verse";
4995 local n=require"util.uuid".generate;
4996 local i="jabber:iq:auth";
4997 function o.plugins.legacy(e)
4998 local function s(t)
4999 local a=t:get_child("query",i);
5000 if t.attr.type~="result"or not a then
5001 local o,a,t=t:get_error();
5002 e:debug("warn","%s %s: %s",o,a,t);
5003 end
5004 local t={
5005 username=e.username;
5006 password=e.password;
5007 resource=e.resource or n();
5008 digest=false,sequence=false,token=false;
5009 };
5010 local o=o.iq({to=e.host,type="set"})
5011 :tag("query",{xmlns=i});
5012 if#a>0 then
5013 for a in a:childtags()do
5014 local a=a.name;
5015 local i=t[a];
5016 if i then
5017 o:tag(a):text(t[a]):up();
5018 elseif i==nil then
5019 local t="feature-not-implemented";
5020 e:event("authentication-failure",{condition=t});
5021 return false;
5022 end
5023 end
5024 else
5025 for t,e in pairs(t)do
5026 if e then
5027 o:tag(t):text(e):up();
5028 end
5029 end
5030 end
5031 e:send_iq(o,function(a)
5032 if a.attr.type=="result"then
5033 e.resource=t.resource;
5034 e.jid=t.username.."@"..e.host.."/"..t.resource;
5035 e:event("authentication-success");
5036 e:event("bind-success",e.jid);
5037 else
5038 local a,t,a=a:get_error();
5039 e:event("authentication-failure",{condition=t});
5040 end
5041 end);
5042 end
5043 local function a(t)
5044 if not t.version then
5045 e:send_iq(o.iq({type="get"})
5046 :tag("query",{xmlns="jabber:iq:auth"})
5047 :tag("username"):text(e.username),
5048 s);
5049 end
5050 end
5051 e:hook("opened",a);
5052 end
5053 end)
5054 package.preload['verse.plugins.compression']=(function(...)
5055 local _ENV=_ENV;
5056 local function e(t,...)
5057 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
5058 package.loaded[t]=e;
5059 for t=1,select("#",...)do
5060 (select(t,...))(e);
5061 end
5062 _ENV=e;
5063 _M=e;
5064 return e;
5065 end
5066 local t=require"verse";
5067 local e=require"zlib";
5068 local a="http://jabber.org/features/compress"
5069 local a="http://jabber.org/protocol/compress"
5070 local o="http://etherx.jabber.org/streams";
5071 local n=9;
5072 local function i(o)
5073 local i,e=pcall(e.deflate,n);
5074 if i==false then
5075 local t=t.stanza("failure",{xmlns=a}):tag("setup-failed");
5076 o:send(t);
5077 o:error("Failed to create zlib.deflate filter: %s",tostring(e));
5078 return
5079 end
5080 return e
5081 end
5082 local function s(o)
5083 local i,e=pcall(e.inflate);
5084 if i==false then
5085 local t=t.stanza("failure",{xmlns=a}):tag("setup-failed");
5086 o:send(t);
5087 o:error("Failed to create zlib.inflate filter: %s",tostring(e));
5088 return
5089 end
5090 return e
5091 end
5092 local function h(e,o)
5093 function e:send(i)
5094 local i,o,n=pcall(o,tostring(i),'sync');
5095 if i==false then
5096 e:close({
5097 condition="undefined-condition";
5098 text=o;
5099 extra=t.stanza("failure",{xmlns=a}):tag("processing-failed");
5100 });
5101 e:warn("Compressed send failed: %s",tostring(o));
5102 return;
5103 end
5104 e.conn:write(o);
5105 end;
5106 end
5107 local function r(e,i)
5108 local s=e.data
5109 e.data=function(n,o)
5110 e:debug("Decompressing data...");
5111 local i,o,h=pcall(i,o);
5112 if i==false then
5113 e:close({
5114 condition="undefined-condition";
5115 text=o;
5116 extra=t.stanza("failure",{xmlns=a}):tag("processing-failed");
5117 });
5118 stream:warn("%s",tostring(o));
5119 return;
5120 end
5121 return s(n,o);
5122 end;
5123 end
5124 function t.plugins.compression(e)
5125 local function n(o)
5126 if not e.compressed then
5127 local o=o:child_with_name("compression");
5128 if o then
5129 for o in o:children()do
5130 local o=o[1]
5131 if o=="zlib"then
5132 e:send(t.stanza("compress",{xmlns=a}):tag("method"):text("zlib"))
5133 e:debug("Enabled compression using zlib.")
5134 return true;
5135 end
5136 end
5137 session:debug("Remote server supports no compression algorithm we support.")
5138 end
5139 end
5140 end
5141 local function o(a)
5142 if a.name=="compressed"then
5143 e:debug("Activating compression...")
5144 local a=i(e);
5145 if not a then return end
5146 local t=s(e);
5147 if not t then return end
5148 h(e,a);
5149 r(e,t);
5150 e.compressed=true;
5151 e:reopen();
5152 elseif a.name=="failure"then
5153 e:warn("Failed to establish compression");
5154 end
5155 end
5156 e:hook("stream-features",n,250);
5157 e:hook("stream/"..a,o);
5158 end 6769 end
5159 end) 6770 end)
5160 package.preload['verse.plugins.smacks']=(function(...) 6771 package.preload['verse.plugins.smacks']=(function(...)
5161 local _ENV=_ENV; 6772 local _ENV=_ENV;
5162 local function e(t,...) 6773 local function e(t,...)
5167 end 6778 end
5168 _ENV=e; 6779 _ENV=e;
5169 _M=e; 6780 _M=e;
5170 return e; 6781 return e;
5171 end 6782 end
5172 local i=require"verse"; 6783 local n=require"verse";
5173 local h=require"socket".gettime; 6784 local r=require"socket".gettime;
5174 local s="urn:xmpp:sm:3"; 6785 local s="urn:xmpp:sm:3";
5175 function i.plugins.smacks(e) 6786 function n.plugins.smacks(e)
5176 local t={}; 6787 local t=nil;
5177 local a=0; 6788 local a=nil;
5178 local r=h(); 6789 local h=nil;
5179 local o; 6790 local o;
5180 local n=0; 6791 local i=nil;
5181 local function d(t) 6792 local function m(t)
5182 if t.attr.xmlns=="jabber:client"or not t.attr.xmlns then 6793 if i and(t.attr.xmlns=="jabber:client"or not t.attr.xmlns)then
5183 n=n+1; 6794 i=i+1;
5184 e:debug("Increasing handled stanzas to %d for %s",n,t:top_tag()); 6795 e:debug("Increasing handled stanzas to %d for %s",i,t:top_tag());
5185 end 6796 end
5186 end 6797 end
5187 local function l(a) 6798 local function c(a)
5188 if a.name and not a.attr.xmlns then 6799 if t and(a.name and not a.attr.xmlns)then
5189 t[#t+1]=tostring(a); 6800 t[#t+1]=tostring(a);
5190 r=h(); 6801 h=r();
5191 if not o then 6802 if not o then
5192 o=true; 6803 o=true;
5193 e:debug("Waiting to send ack request..."); 6804 e:debug("Waiting to send ack request...");
5194 i.add_task(1,function() 6805 n.add_task(1,function()
5195 if#t==0 then 6806 if#t==0 then
5196 o=false; 6807 o=false;
5197 return; 6808 return;
5198 end 6809 end
5199 local a=h()-r; 6810 local a=r()-h;
5200 if a<1 and#t<10 then 6811 if a<1 and#t<10 then
5201 return 1-a; 6812 return 1-a;
5202 end 6813 end
5203 e:debug("Time up, sending <r>..."); 6814 e:debug("Time up, sending <r>...");
5204 o=false; 6815 o=false;
5205 e:send(i.stanza("r",{xmlns=s})); 6816 e:send(n.stanza("r",{xmlns=s}));
5206 end); 6817 end);
5207 end 6818 end
5208 end 6819 end
5209 end 6820 end
5210 local function h() 6821 local function u()
5211 e:debug("smacks: connection lost"); 6822 e:debug("smacks: connection lost");
5212 e.stream_management_supported=nil; 6823 e.stream_management_supported=nil;
5213 if e.resumption_token then 6824 if e.resumption_token then
5214 e:debug("smacks: have resumption token, reconnecting in 1s..."); 6825 e:debug("smacks: have resumption token, reconnecting in 1s...");
5215 e.authenticated=nil; 6826 e.authenticated=nil;
5216 i.add_task(1,function() 6827 n.add_task(1,function()
5217 e:connect(e.connect_host or e.host,e.connect_port or 5222); 6828 e:connect(e.connect_host or e.host,e.connect_port or 5222);
5218 end); 6829 end);
5219 return true; 6830 return true;
5220 end 6831 end
5221 end 6832 end
5222 local function u() 6833 local function d()
5223 e.resumption_token=nil; 6834 e.resumption_token=nil;
5224 e:unhook("disconnected",h); 6835 end
5225 end 6836 local function l(o)
5226 local function r(o)
5227 if o.name=="r"then 6837 if o.name=="r"then
5228 e:debug("Ack requested... acking %d handled stanzas",n); 6838 e:debug("Ack requested... acking %d handled stanzas",i);
5229 e:send(i.stanza("a",{xmlns=s,h=tostring(n)})); 6839 e:send(n.stanza("a",{xmlns=s,h=tostring(i)}));
5230 elseif o.name=="a"then 6840 elseif o.name=="a"then
5231 local o=tonumber(o.attr.h); 6841 local o=tonumber(o.attr.h);
5232 if o>a then 6842 if o>a then
5233 local i=#t; 6843 local i=#t;
5234 for a=a+1,o do 6844 for a=a+1,o do
5235 table.remove(t,1); 6845 table.remove(t,1);
5236 end 6846 end
5237 e:debug("Received ack: New ack: "..o.." Last ack: "..a.." Unacked stanzas now: "..#t.." (was "..i..")"); 6847 e:debug("Received ack: New ack: "..o.." Last ack: "..a.." Unacked stanzas now: "..#t.." (was "..i..")");
5238 a=o; 6848 a=o;
5239 else 6849 elseif o<a then
5240 e:warn("Received bad ack for "..o.." when last ack was "..a); 6850 e:warn("Received bad ack for "..o.." when last ack was "..a);
5241 end 6851 end
5242 elseif o.name=="enabled"then 6852 elseif o.name=="enabled"then
6853 i=0;
6854 e.pre_smacks_features=nil;
5243 if o.attr.id then 6855 if o.attr.id then
5244 e.resumption_token=o.attr.id; 6856 e.resumption_token=o.attr.id;
5245 e:hook("closed",u,100);
5246 e:hook("disconnected",h,100);
5247 end 6857 end
5248 elseif o.name=="resumed"then 6858 elseif o.name=="resumed"then
6859 e.pre_smacks_features=nil;
5249 local o=tonumber(o.attr.h); 6860 local o=tonumber(o.attr.h);
5250 if o>a then 6861 if o>a then
5251 local i=#t; 6862 local i=#t;
5252 for a=a+1,o do 6863 for a=a+1,o do
5253 table.remove(t,1); 6864 table.remove(t,1);
5259 e:send(t[a]); 6870 e:send(t[a]);
5260 end 6871 end
5261 t={}; 6872 t={};
5262 e:debug("Resumed successfully"); 6873 e:debug("Resumed successfully");
5263 e:event("resumed"); 6874 e:event("resumed");
6875 elseif o.name=="failed"then
6876 e.bound=nil
6877 e.smacks=nil
6878 a=nil
6879 i=nil
6880 t={};
6881 local t=e.pre_smacks_features;
6882 e.pre_smacks_features=nil;
6883 e:event("stream-features",t);
5264 else 6884 else
5265 e:warn("Don't know how to handle "..s.."/"..o.name); 6885 e:warn("Don't know how to handle "..s.."/"..o.name);
5266 end 6886 end
5267 end 6887 end
5268 local function t() 6888 local function o()
5269 if not e.smacks then 6889 if e.stream_management_supported and not e.smacks then
5270 e:debug("smacks: sending enable"); 6890 e:debug("smacks: sending enable");
5271 e:send(i.stanza("enable",{xmlns=s,resume="true"})); 6891 t={};
6892 a=0;
6893 h=r();
6894 e:send(n.stanza("enable",{xmlns=s,resume="true"}));
5272 e.smacks=true; 6895 e.smacks=true;
5273 e:hook("stanza",d); 6896 end
5274 e:hook("outgoing",l); 6897 end
5275 end 6898 local function a(t)
5276 end 6899 if t:get_child("sm",s)then
5277 local function a(a) 6900 e.pre_smacks_features=t;
5278 if a:get_child("sm",s)then
5279 e.stream_management_supported=true; 6901 e.stream_management_supported=true;
5280 if e.smacks and e.bound then 6902 if e.smacks and e.bound then
5281 e:debug("Resuming stream with %d handled stanzas",n); 6903 e:debug("Resuming stream with %d handled stanzas",i);
5282 e:send(i.stanza("resume",{xmlns=s, 6904 e:send(n.stanza("resume",{xmlns=s,
5283 h=n,previd=e.resumption_token})); 6905 h=tostring(i),previd=e.resumption_token}));
5284 return true; 6906 return true;
5285 else 6907 else
5286 e:hook("bind-success",t,1);
5287 end 6908 end
5288 end 6909 end
5289 end 6910 end
5290 e:hook("stream-features",a,250); 6911 e:hook("stream-features",a,250);
5291 e:hook("stream/"..s,r); 6912 e:hook("stream/"..s,l);
6913 e:hook("bind-success",o,1);
6914 e:hook("stanza",m);
6915 e:hook("outgoing",c);
6916 e:hook("closed",d,100);
6917 e:hook("disconnected",u,100);
5292 end 6918 end
5293 end) 6919 end)
5294 package.preload['verse.plugins.keepalive']=(function(...) 6920 package.preload['verse.plugins.keepalive']=(function(...)
5295 local _ENV=_ENV; 6921 local _ENV=_ENV;
5296 local function e(t,...) 6922 local function e(t,...)
5322 end 6948 end
5323 _ENV=e; 6949 _ENV=e;
5324 _M=e; 6950 _M=e;
5325 return e; 6951 return e;
5326 end 6952 end
5327 local a=require"verse"; 6953 local t=require"verse";
5328 local e=require("mime").b64; 6954 local e=require("mime").b64;
5329 local e=require("util.hashes").sha1; 6955 local e=require("util.hashes").sha1;
5330 local r=require"util.caps".calculate_hash; 6956 local s=require"util.caps".calculate_hash;
5331 local s="http://jabber.org/protocol/caps"; 6957 local h="http://jabber.org/protocol/caps";
5332 local e="http://jabber.org/protocol/disco"; 6958 local e="http://jabber.org/protocol/disco";
5333 local i=e.."#info"; 6959 local o=e.."#info";
5334 local o=e.."#items"; 6960 local i=e.."#items";
5335 function a.plugins.disco(e) 6961 function t.plugins.disco(e)
5336 e:add_plugin("presence"); 6962 e:add_plugin("presence");
5337 local n={ 6963 local a={
5338 __index=function(t,e) 6964 __index=function(t,e)
5339 local a={identities={},features={}}; 6965 local a={identities={},features={}};
5340 if e=="identities"or e=="features"then 6966 if e=="identities"or e=="features"then
5341 return t[false][e] 6967 return t[false][e]
5342 end 6968 end
5343 t[e]=a; 6969 t[e]=a;
5344 return a; 6970 return a;
5345 end, 6971 end,
5346 }; 6972 };
5347 local t={ 6973 local n={
5348 __index=function(a,t) 6974 __index=function(t,a)
5349 local e={}; 6975 local e={};
5350 a[t]=e; 6976 t[a]=e;
5351 return e; 6977 return e;
5352 end, 6978 end,
5353 }; 6979 };
5354 e.disco={ 6980 e.disco={
5355 cache={}, 6981 cache={},
5357 [false]={ 6983 [false]={
5358 identities={ 6984 identities={
5359 {category='client',type='pc',name='Verse'}, 6985 {category='client',type='pc',name='Verse'},
5360 }, 6986 },
5361 features={ 6987 features={
5362 [s]=true, 6988 [h]=true,
6989 [o]=true,
5363 [i]=true, 6990 [i]=true,
5364 [o]=true,
5365 }, 6991 },
5366 }, 6992 },
5367 },n); 6993 },a);
5368 items=setmetatable({[false]={}},t); 6994 items=setmetatable({[false]={}},n);
5369 }; 6995 };
5370 e.caps={} 6996 e.caps={}
5371 e.caps.node='http://code.matthewwild.co.uk/verse/' 6997 e.caps.node='http://code.matthewwild.co.uk/verse/'
5372 local function h(t) 6998 local function r(a)
5373 local o=e.disco.info[t or false]; 6999 local i=e.disco.info[a or false];
5374 if t and t==e.caps.node.."#"..e.caps.hash then 7000 if a and a==e.caps.node.."#"..e.caps.hash then
5375 o=e.disco.info[false]; 7001 i=e.disco.info[false];
5376 end 7002 end
5377 local n,o=o.identities,o.features 7003 local n,i=i.identities,i.features
5378 local e=a.stanza("query",{ 7004 local e=t.stanza("query",{
5379 xmlns=i, 7005 xmlns=o,
5380 node=t, 7006 node=a,
5381 }); 7007 });
5382 for a,t in pairs(n)do 7008 for a,t in pairs(n)do
5383 e:tag('identity',t):up() 7009 e:tag('identity',t):up()
5384 end 7010 end
5385 for t in pairs(o)do 7011 for t in pairs(i)do
5386 e:tag('feature',{var=t}):up() 7012 e:tag('feature',{var=t}):up()
5387 end 7013 end
5388 return e; 7014 return e;
5389 end 7015 end
5390 setmetatable(e.caps,{ 7016 setmetatable(e.caps,{
5391 __call=function(...) 7017 __call=function(...)
5392 local t=r(h()) 7018 local a=s(r())
5393 e.caps.hash=t; 7019 e.caps.hash=a;
5394 return a.stanza('c',{ 7020 return t.stanza('c',{
5395 xmlns=s, 7021 xmlns=h,
5396 hash='sha-1', 7022 hash='sha-1',
5397 node=e.caps.node, 7023 node=e.caps.node,
5398 ver=t 7024 ver=a
5399 }) 7025 })
5400 end 7026 end
5401 }) 7027 })
5402 function e:set_identity(a,t) 7028 function e:set_identity(e,t)
5403 self.disco.info[t or false].identities={a}; 7029 self.disco.info[t or false].identities={e};
5404 e:resend_presence(); 7030 end
5405 end 7031 function e:add_identity(t,e)
5406 function e:add_identity(a,t) 7032 local e=self.disco.info[e or false].identities;
5407 local t=self.disco.info[t or false].identities; 7033 e[#e+1]=t;
5408 t[#t+1]=a; 7034 end
5409 e:resend_presence(); 7035 function e:add_disco_feature(e,t)
5410 end 7036 local e=e.var or e;
5411 function e:add_disco_feature(t,a) 7037 self.disco.info[t or false].features[e]=true;
5412 local t=t.var or t; 7038 end
5413 self.disco.info[a or false].features[t]=true; 7039 function e:remove_disco_feature(e,t)
5414 e:resend_presence(); 7040 local e=e.var or e;
5415 end 7041 self.disco.info[t or false].features[e]=nil;
5416 function e:remove_disco_feature(t,a)
5417 local t=t.var or t;
5418 self.disco.info[a or false].features[t]=nil;
5419 e:resend_presence();
5420 end 7042 end
5421 function e:add_disco_item(t,e) 7043 function e:add_disco_item(t,e)
5422 local e=self.disco.items[e or false]; 7044 local e=self.disco.items[e or false];
5423 e[#e+1]=t; 7045 e[#e+1]=t;
5424 end 7046 end
5428 if e[t]==a then 7050 if e[t]==a then
5429 table.remove(e,t); 7051 table.remove(e,t);
5430 end 7052 end
5431 end 7053 end
5432 end 7054 end
5433 function e:jid_has_identity(t,a,e) 7055 function e:jid_has_identity(t,e,a)
5434 local o=self.disco.cache[t]; 7056 local o=self.disco.cache[t];
5435 if not o then 7057 if not o then
5436 return nil,"no-cache"; 7058 return nil,"no-cache";
5437 end 7059 end
5438 local t=self.disco.cache[t].identities; 7060 local t=self.disco.cache[t].identities;
5439 if e then 7061 if a then
5440 return t[a.."/"..e]or false; 7062 return t[e.."/"..a]or false;
5441 end 7063 end
5442 for e in pairs(t)do 7064 for t in pairs(t)do
5443 if e:match("^(.*)/")==a then 7065 if t:match("^(.*)/")==e then
5444 return true; 7066 return true;
5445 end 7067 end
5446 end 7068 end
5447 end 7069 end
5448 function e:jid_supports(e,t) 7070 function e:jid_supports(e,t)
5450 if not e or not e.features then 7072 if not e or not e.features then
5451 return nil,"no-cache"; 7073 return nil,"no-cache";
5452 end 7074 end
5453 return e.features[t]or false; 7075 return e.features[t]or false;
5454 end 7076 end
5455 function e:get_local_services(a,o) 7077 function e:get_local_services(o,a)
5456 local e=self.disco.cache[self.host]; 7078 local e=self.disco.cache[self.host];
5457 if not(e)or not(e.items)then 7079 if not(e)or not(e.items)then
5458 return nil,"no-cache"; 7080 return nil,"no-cache";
5459 end 7081 end
5460 local t={}; 7082 local t={};
5461 for i,e in ipairs(e.items)do 7083 for i,e in ipairs(e.items)do
5462 if self:jid_has_identity(e.jid,a,o)then 7084 if self:jid_has_identity(e.jid,o,a)then
5463 table.insert(t,e.jid); 7085 table.insert(t,e.jid);
5464 end 7086 end
5465 end 7087 end
5466 return t; 7088 return t;
5467 end 7089 end
5486 if e==0 then 7108 if e==0 then
5487 return a(t); 7109 return a(t);
5488 end 7110 end
5489 end); 7111 end);
5490 end 7112 end
5491 function e:disco_info(e,t,s) 7113 function e:disco_info(e,a,h)
5492 local a=a.iq({to=e,type="get"}) 7114 local t=t.iq({to=e,type="get"})
5493 :tag("query",{xmlns=i,node=t}); 7115 :tag("query",{xmlns=o,node=a});
5494 self:send_iq(a,function(o) 7116 self:send_iq(t,function(t)
5495 if o.attr.type=="error"then 7117 if t.attr.type=="error"then
5496 return s(nil,o:get_error()); 7118 return h(nil,t:get_error());
5497 end 7119 end
5498 local n,a={},{}; 7120 local i,s,n={},{},{};
5499 for e in o:get_child("query",i):childtags()do 7121 for e in t:get_child("query",o):childtags()do
7122 self:debug("DISCO {%s}%s",e.attr.xmlns or"",e.name);
5500 if e.name=="identity"then 7123 if e.name=="identity"then
5501 n[e.attr.category.."/"..e.attr.type]=e.attr.name or true; 7124 i[e.attr.category.."/"..e.attr.type]=e.attr.name or true;
5502 elseif e.name=="feature"then 7125 elseif e.name=="feature"then
5503 a[e.attr.var]=true; 7126 s[e.attr.var]=true;
7127 end
7128 end
7129 for e in t:get_child("query",o):childtags("x","jabber:x:data")do
7130 self:debug("DISCO {%s}%s",e.attr.xmlns or"",e.name);
7131 local t=e:get_child_with_attr("field",nil,"var","FORM_TYPE");
7132 local a=t and t:get_child_text("value");
7133 self:debug("FORM: %s (%s)",a,t);
7134 if a then
7135 n[a]=e;
5504 end 7136 end
5505 end 7137 end
5506 if not self.disco.cache[e]then 7138 if not self.disco.cache[e]then
5507 self.disco.cache[e]={nodes={}}; 7139 self.disco.cache[e]={nodes={}};
5508 end 7140 end
5509 if t then 7141 if a then
5510 if not self.disco.cache[e].nodes[t]then 7142 if not self.disco.cache[e].nodes[a]then
5511 self.disco.cache[e].nodes[t]={nodes={}}; 7143 self.disco.cache[e].nodes[a]={nodes={}};
5512 end 7144 end
5513 self.disco.cache[e].nodes[t].identities=n; 7145 self.disco.cache[e].nodes[a].identities=i;
5514 self.disco.cache[e].nodes[t].features=a; 7146 self.disco.cache[e].nodes[a].features=s;
5515 else 7147 self.disco.cache[e].nodes[a].extended=n;
5516 self.disco.cache[e].identities=n; 7148 else
5517 self.disco.cache[e].features=a; 7149 self.disco.cache[e].identities=i;
5518 end 7150 self.disco.cache[e].features=s;
5519 return s(self.disco.cache[e]); 7151 self.disco.cache[e].extended=n;
7152 end
7153 return h(self.disco.cache[e]);
5520 end); 7154 end);
5521 end 7155 end
5522 function e:disco_items(t,i,n) 7156 function e:disco_items(a,o,n)
5523 local a=a.iq({to=t,type="get"}) 7157 local t=t.iq({to=a,type="get"})
5524 :tag("query",{xmlns=o,node=i}); 7158 :tag("query",{xmlns=i,node=o});
5525 self:send_iq(a,function(e) 7159 self:send_iq(t,function(t)
5526 if e.attr.type=="error"then 7160 if t.attr.type=="error"then
5527 return n(nil,e:get_error()); 7161 return n(nil,t:get_error());
5528 end 7162 end
5529 local a={}; 7163 local e={};
5530 for e in e:get_child("query",o):childtags()do 7164 for t in t:get_child("query",i):childtags()do
5531 if e.name=="item"then 7165 if t.name=="item"then
5532 table.insert(a,{ 7166 table.insert(e,{
5533 name=e.attr.name; 7167 name=t.attr.name;
5534 jid=e.attr.jid; 7168 jid=t.attr.jid;
5535 node=e.attr.node; 7169 node=t.attr.node;
5536 }); 7170 });
5537 end 7171 end
5538 end 7172 end
5539 if not self.disco.cache[t]then 7173 if not self.disco.cache[a]then
5540 self.disco.cache[t]={nodes={}}; 7174 self.disco.cache[a]={nodes={}};
5541 end 7175 end
5542 if i then 7176 if o then
5543 if not self.disco.cache[t].nodes[i]then 7177 if not self.disco.cache[a].nodes[o]then
5544 self.disco.cache[t].nodes[i]={nodes={}}; 7178 self.disco.cache[a].nodes[o]={nodes={}};
5545 end 7179 end
5546 self.disco.cache[t].nodes[i].items=a; 7180 self.disco.cache[a].nodes[o].items=e;
5547 else 7181 else
5548 self.disco.cache[t].items=a; 7182 self.disco.cache[a].items=e;
5549 end 7183 end
5550 return n(a); 7184 return n(e);
5551 end); 7185 end);
5552 end 7186 end
5553 e:hook("iq/"..i,function(t) 7187 e:hook("iq/"..o,function(a)
5554 local o=t.tags[1]; 7188 local o=a.tags[1];
5555 if t.attr.type=='get'and o.name=="query"then 7189 if a.attr.type=='get'and o.name=="query"then
5556 local o=h(o.attr.node); 7190 local o=r(o.attr.node);
5557 local t=a.reply(t):add_child(o); 7191 local t=t.reply(a):add_child(o);
5558 e:send(t); 7192 e:send(t);
5559 return true 7193 return true
5560 end 7194 end
5561 end); 7195 end);
5562 e:hook("iq/"..o,function(i) 7196 e:hook("iq/"..i,function(a)
5563 local t=i.tags[1]; 7197 local o=a.tags[1];
5564 if i.attr.type=='get'and t.name=="query"then 7198 if a.attr.type=='get'and o.name=="query"then
5565 local n=e.disco.items[t.attr.node or false]; 7199 local n=e.disco.items[o.attr.node or false];
5566 local t=a.reply(i):tag('query',{ 7200 local t=t.reply(a):tag('query',{
5567 xmlns=o, 7201 xmlns=i,
5568 node=t.attr.node 7202 node=o.attr.node
5569 }) 7203 })
5570 for a=1,#n do 7204 for a=1,#n do
5571 t:tag('item',n[a]):up() 7205 t:tag('item',n[a]):up()
5572 end 7206 end
5573 e:send(t); 7207 e:send(t);
5576 end); 7210 end);
5577 local t; 7211 local t;
5578 e:hook("ready",function() 7212 e:hook("ready",function()
5579 if t then return;end 7213 if t then return;end
5580 t=true; 7214 t=true;
5581 local function i(t) 7215 local function o(t)
5582 local a=e.disco.cache[t]; 7216 local a=e.disco.cache[t];
5583 if a then 7217 if a then
5584 for a in pairs(a.identities)do 7218 for a in pairs(a.identities)do
5585 local o,a=a:match("^(.*)/(.*)$"); 7219 local o,a=a:match("^(.*)/(.*)$");
5586 print(t,o,a) 7220 print(t,o,a)
5589 }); 7223 });
5590 end 7224 end
5591 end 7225 end
5592 end 7226 end
5593 e:disco_info(e.host,nil,function() 7227 e:disco_info(e.host,nil,function()
5594 i(e.host); 7228 o(e.host);
5595 end); 7229 end);
5596 e:disco_local_services(function(t) 7230 e:disco_local_services(function(t)
5597 for a,t in ipairs(t)do 7231 for a,t in ipairs(t)do
5598 i(t.jid); 7232 o(t.jid);
5599 end 7233 end
5600 e:event("ready"); 7234 e:event("ready");
5601 end); 7235 end);
5602 return true; 7236 return true;
5603 end,50); 7237 end,50);
5604 e:hook("presence-out",function(t) 7238 e:hook("presence-out",function(t)
5605 if not t:get_child("c",s)then 7239 t:remove_children("c",h);
5606 t:reset():add_child(e:caps()):reset(); 7240 t:reset():add_child(e:caps()):reset();
5607 end
5608 end,10); 7241 end,10);
5609 end 7242 end
5610 end) 7243 end)
5611 package.preload['verse.plugins.version']=(function(...) 7244 package.preload['verse.plugins.version']=(function(...)
5612 local _ENV=_ENV; 7245 local _ENV=_ENV;
5619 _ENV=e; 7252 _ENV=e;
5620 _M=e; 7253 _M=e;
5621 return e; 7254 return e;
5622 end 7255 end
5623 local o=require"verse"; 7256 local o=require"verse";
5624 local a="jabber:iq:version"; 7257 local t="jabber:iq:version";
5625 local function i(e,t) 7258 local function a(t,e)
5626 e.name=t.name; 7259 t.name=e.name;
5627 e.version=t.version; 7260 t.version=e.version;
5628 e.platform=t.platform; 7261 t.platform=e.platform;
5629 end 7262 end
5630 function o.plugins.version(e) 7263 function o.plugins.version(e)
5631 e.version={set=i}; 7264 e.version={set=a};
5632 e:hook("iq/"..a,function(t) 7265 e:hook("iq/"..t,function(a)
5633 if t.attr.type~="get"then return;end 7266 if a.attr.type~="get"then return;end
5634 local t=o.reply(t) 7267 local t=o.reply(a)
5635 :tag("query",{xmlns=a}); 7268 :tag("query",{xmlns=t});
5636 if e.version.name then 7269 if e.version.name then
5637 t:tag("name"):text(tostring(e.version.name)):up(); 7270 t:tag("name"):text(tostring(e.version.name)):up();
5638 end 7271 end
5639 if e.version.version then 7272 if e.version.version then
5640 t:tag("version"):text(tostring(e.version.version)):up() 7273 t:tag("version"):text(tostring(e.version.version)):up()
5643 t:tag("os"):text(e.version.platform); 7276 t:tag("os"):text(e.version.platform);
5644 end 7277 end
5645 e:send(t); 7278 e:send(t);
5646 return true; 7279 return true;
5647 end); 7280 end);
5648 function e:query_version(e,t) 7281 function e:query_version(i,a)
5649 t=t or function(e)return self:event("version/response",e);end 7282 a=a or function(e)return self:event("version/response",e);end
5650 self:send_iq(o.iq({type="get",to=e}) 7283 self:send_iq(o.iq({type="get",to=i})
5651 :tag("query",{xmlns=a}), 7284 :tag("query",{xmlns=t}),
5652 function(o) 7285 function(o)
5653 if o.attr.type=="result"then 7286 if o.attr.type=="result"then
5654 local e=o:get_child("query",a); 7287 local e=o:get_child("query",t);
5655 local a=e and e:get_child_text("name"); 7288 local t=e and e:get_child_text("name");
5656 local o=e and e:get_child_text("version"); 7289 local o=e and e:get_child_text("version");
5657 local e=e and e:get_child_text("os"); 7290 local e=e and e:get_child_text("os");
5658 t({ 7291 a({
5659 name=a; 7292 name=t;
5660 version=o; 7293 version=o;
5661 platform=e; 7294 platform=e;
5662 }); 7295 });
5663 else 7296 else
5664 local a,e,o=o:get_error(); 7297 local o,e,t=o:get_error();
5665 t({ 7298 a({
5666 error=true; 7299 error=true;
5667 condition=e; 7300 condition=e;
5668 text=o; 7301 text=t;
5669 type=a; 7302 type=o;
5670 }); 7303 });
5671 end 7304 end
5672 end); 7305 end);
5673 end 7306 end
5674 return true; 7307 return true;
5685 _ENV=e; 7318 _ENV=e;
5686 _M=e; 7319 _M=e;
5687 return e; 7320 return e;
5688 end 7321 end
5689 local a=require"verse"; 7322 local a=require"verse";
5690 local n=require"socket".gettime; 7323 local s=require"socket".gettime;
7324 local o=require"util.id".short;
5691 local i="urn:xmpp:ping"; 7325 local i="urn:xmpp:ping";
5692 function a.plugins.ping(e) 7326 function a.plugins.ping(e)
5693 function e:ping(t,o) 7327 function e:ping(t,n)
5694 local s=n(); 7328 local h=s();
5695 e:send_iq(a.iq{to=t,type="get"}:tag("ping",{xmlns=i}), 7329 local o=o();
7330 local a=a.iq{id=o,to=t,type="get"}:tag("ping",{xmlns=i});
7331 e:send_iq(a,
5696 function(e) 7332 function(e)
5697 if e.attr.type=="error"then 7333 if e.attr.type=="error"then
5698 local i,e,a=e:get_error(); 7334 local o,e,a=e:get_error();
5699 if e~="service-unavailable"and e~="feature-not-implemented"then 7335 if e~="service-unavailable"and e~="feature-not-implemented"then
5700 o(nil,t,{type=i,condition=e,text=a}); 7336 n(nil,t,{type=o,condition=e,text=a});
5701 return; 7337 return;
5702 end 7338 end
5703 end 7339 end
5704 o(n()-s,t); 7340 n(s()-h,t);
5705 end); 7341 end);
7342 return o;
5706 end 7343 end
5707 e:hook("iq/"..i,function(t) 7344 e:hook("iq/"..i,function(t)
5708 return e:send(a.reply(t)); 7345 return e:send(a.reply(t));
5709 end); 7346 end);
5710 return true; 7347 return true;
5746 local e=tonumber(t.attr.seconds); 7383 local e=tonumber(t.attr.seconds);
5747 a({ 7384 a({
5748 seconds=e or nil; 7385 seconds=e or nil;
5749 }); 7386 });
5750 else 7387 else
5751 local o,t,e=e:get_error(); 7388 local o,e,t=e:get_error();
5752 a({ 7389 a({
5753 error=true; 7390 error=true;
5754 condition=t; 7391 condition=e;
7392 text=t;
7393 type=o;
7394 });
7395 end
7396 end);
7397 end
7398 return true;
7399 end
7400 end)
7401 package.preload['verse.plugins.time']=(function(...)
7402 local _ENV=_ENV;
7403 local function e(t,...)
7404 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
7405 package.loaded[t]=e;
7406 for t=1,select("#",...)do
7407 (select(t,...))(e);
7408 end
7409 _ENV=e;
7410 _M=e;
7411 return e;
7412 end
7413 local e=require"verse";
7414 local i=require"util.datetime";
7415 local o="urn:xmpp:time";
7416 function e.plugins.time(t)
7417 function t:query_time(a,t)
7418 t=t or function(e)return self:event("time/response",e);end
7419 self:send_iq(e.iq({type="get",to=a})
7420 :tag("time",{xmlns=o}),
7421 function(a)
7422 if a.attr.type=="result"then
7423 local e=a:get_child("time",o);
7424 local e={
7425 tzo=e:get_child_text("tzo");
7426 utc=e:get_child_text("utc");
7427 };
7428 if e.utc then
7429 e.timestamp=i.parse(e.utc);
7430 end
7431 t(e);
7432 else
7433 local o,a,e=a:get_error();
7434 t({
7435 error=true;
7436 condition=a;
5755 text=e; 7437 text=e;
5756 type=o; 7438 type=o;
5757 }); 7439 });
5758 end 7440 end
5759 end); 7441 end);
5771 end 7453 end
5772 _ENV=e; 7454 _ENV=e;
5773 _M=e; 7455 _M=e;
5774 return e; 7456 return e;
5775 end 7457 end
5776 local o=require"verse"; 7458 local a=require"verse";
5777 local a="urn:xmpp:blocking"; 7459 local o="urn:xmpp:blocking";
5778 function o.plugins.blocking(e) 7460 function a.plugins.blocking(e)
5779 e.blocking={}; 7461 e.blocking={};
5780 function e.blocking:block_jid(i,t) 7462 function e.blocking:block_jid(i,t)
5781 e:send_iq(o.iq{type="set"} 7463 e:send_iq(a.iq{type="set"}
5782 :tag("block",{xmlns=a}) 7464 :tag("block",{xmlns=o})
5783 :tag("item",{jid=i}) 7465 :tag("item",{jid=i})
5784 ,function()return t and t(true);end 7466 ,function()return t and t(true);end
5785 ,function()return t and t(false);end 7467 ,function()return t and t(false);end
5786 ); 7468 );
5787 end 7469 end
5788 function e.blocking:unblock_jid(i,t) 7470 function e.blocking:unblock_jid(i,t)
5789 e:send_iq(o.iq{type="set"} 7471 e:send_iq(a.iq{type="set"}
5790 :tag("unblock",{xmlns=a}) 7472 :tag("unblock",{xmlns=o})
5791 :tag("item",{jid=i}) 7473 :tag("item",{jid=i})
5792 ,function()return t and t(true);end 7474 ,function()return t and t(true);end
5793 ,function()return t and t(false);end 7475 ,function()return t and t(false);end
5794 ); 7476 );
5795 end 7477 end
5796 function e.blocking:unblock_all_jids(t) 7478 function e.blocking:unblock_all_jids(t)
5797 e:send_iq(o.iq{type="set"} 7479 e:send_iq(a.iq{type="set"}
5798 :tag("unblock",{xmlns=a}) 7480 :tag("unblock",{xmlns=o})
5799 ,function()return t and t(true);end 7481 ,function()return t and t(true);end
5800 ,function()return t and t(false);end 7482 ,function()return t and t(false);end
5801 ); 7483 );
5802 end 7484 end
5803 function e.blocking:get_blocked_jids(t) 7485 function e.blocking:get_blocked_jids(t)
5804 e:send_iq(o.iq{type="get"} 7486 e:send_iq(a.iq{type="get"}
5805 :tag("blocklist",{xmlns=a}) 7487 :tag("blocklist",{xmlns=o})
5806 ,function(e) 7488 ,function(e)
5807 local a=e:get_child("blocklist",a); 7489 local a=e:get_child("blocklist",o);
5808 if not a then return t and t(false);end 7490 if not a then return t and t(false);end
5809 local e={}; 7491 local e={};
5810 for t in a:childtags()do 7492 for t in a:childtags()do
5811 e[#e+1]=t.attr.jid; 7493 e[#e+1]=t.attr.jid;
5812 end 7494 end
5827 end 7509 end
5828 _ENV=e; 7510 _ENV=e;
5829 _M=e; 7511 _M=e;
5830 return e; 7512 return e;
5831 end 7513 end
5832 local o=require"verse"; 7514 local a=require"verse";
5833 local e=require"util.timer"; 7515 local e=require"util.timer";
5834 local n=require"util.uuid".generate; 7516 local n=require"util.id".short;
5835 local i="urn:xmpp:jingle:1"; 7517 local i="urn:xmpp:jingle:1";
5836 local r="urn:xmpp:jingle:errors:1"; 7518 local r="urn:xmpp:jingle:errors:1";
5837 local t={}; 7519 local t={};
5838 t.__index=t; 7520 t.__index=t;
5839 local e={}; 7521 local e={};
5840 local e={}; 7522 local e={};
5841 function o.plugins.jingle(e) 7523 function a.plugins.jingle(e)
5842 e:hook("ready",function() 7524 e:hook("ready",function()
5843 e:add_disco_feature(i); 7525 e:add_disco_feature(i);
5844 end,10); 7526 end,10);
5845 function e:jingle(a) 7527 function e:jingle(o)
5846 return o.eventable(setmetatable(base or{ 7528 return a.eventable(setmetatable(base or{
5847 role="initiator"; 7529 role="initiator";
5848 peer=a; 7530 peer=o;
5849 sid=n(); 7531 sid=n();
5850 stream=e; 7532 stream=e;
5851 },t)); 7533 },t));
5852 end 7534 end
5853 function e:register_jingle_transport(e) 7535 function e:register_jingle_transport(e)
5854 end 7536 end
5855 function e:register_jingle_content_type(e) 7537 function e:register_jingle_content_type(e)
5856 end 7538 end
5857 local function u(n) 7539 local function u(n)
5858 local s=n:get_child("jingle",i); 7540 local s=n:get_child("jingle",i);
5859 local a=s.attr.sid; 7541 local h=s.attr.sid;
5860 local h=s.attr.action; 7542 local o=s.attr.action;
5861 local a=e:event("jingle/"..a,n); 7543 local h=e:event("jingle/"..h,n);
5862 if a==true then 7544 if h==true then
5863 e:send(o.reply(n)); 7545 e:send(a.reply(n));
5864 return true; 7546 return true;
5865 end 7547 end
5866 if h~="session-initiate"then 7548 if o~="session-initiate"then
5867 local t=o.error_reply(n,"cancel","item-not-found") 7549 local t=a.error_reply(n,"cancel","item-not-found")
5868 :tag("unknown-session",{xmlns=r}):up(); 7550 :tag("unknown-session",{xmlns=r}):up();
5869 e:send(t); 7551 e:send(t);
5870 return; 7552 return;
5871 end 7553 end
5872 local l=s.attr.sid; 7554 local l=s.attr.sid;
5873 local a=o.eventable{ 7555 local o=a.eventable{
5874 role="receiver"; 7556 role="receiver";
5875 peer=n.attr.from; 7557 peer=n.attr.from;
5876 sid=l; 7558 sid=l;
5877 stream=e; 7559 stream=e;
5878 }; 7560 };
5879 setmetatable(a,t); 7561 setmetatable(o,t);
5880 local h; 7562 local h;
5881 local d,r; 7563 local r,d;
5882 for t in s:childtags()do 7564 for t in s:childtags()do
5883 if t.name=="content"and t.attr.xmlns==i then 7565 if t.name=="content"and t.attr.xmlns==i then
5884 local o=t:child_with_name("description"); 7566 local i=t:child_with_name("description");
5885 local i=o.attr.xmlns; 7567 local a=i.attr.xmlns;
5886 if i then 7568 if a then
5887 local e=e:event("jingle/content/"..i,a,o); 7569 local e=e:event("jingle/content/"..a,o,i);
5888 if e then 7570 if e then
5889 d=e; 7571 r=e;
5890 end 7572 end
5891 end 7573 end
5892 local o=t:child_with_name("transport"); 7574 local a=t:child_with_name("transport");
5893 local i=o.attr.xmlns; 7575 local i=a.attr.xmlns;
5894 r=e:event("jingle/transport/"..i,a,o); 7576 d=e:event("jingle/transport/"..i,o,a);
5895 if d and r then 7577 if r and d then
5896 h=t; 7578 h=t;
5897 break; 7579 break;
5898 end 7580 end
5899 end 7581 end
5900 end 7582 end
7583 if not r then
7584 e:send(a.error_reply(n,"cancel","feature-not-implemented","The specified content is not supported"));
7585 return true;
7586 end
5901 if not d then 7587 if not d then
5902 e:send(o.error_reply(n,"cancel","feature-not-implemented","The specified content is not supported")); 7588 e:send(a.error_reply(n,"cancel","feature-not-implemented","The specified transport is not supported"));
5903 return true; 7589 return true;
5904 end 7590 end
5905 if not r then 7591 e:send(a.reply(n));
5906 e:send(o.error_reply(n,"cancel","feature-not-implemented","The specified transport is not supported")); 7592 o.content_tag=h;
5907 return true; 7593 o.creator,o.name=h.attr.creator,h.attr.name;
5908 end 7594 o.content,o.transport=r,d;
5909 e:send(o.reply(n)); 7595 function o:decline()
5910 a.content_tag=h;
5911 a.creator,a.name=h.attr.creator,h.attr.name;
5912 a.content,a.transport=d,r;
5913 function a:decline()
5914 end 7596 end
5915 e:hook("jingle/"..l,function(e) 7597 e:hook("jingle/"..l,function(e)
5916 if e.attr.from~=a.peer then 7598 if e.attr.from~=o.peer then
5917 return false; 7599 return false;
5918 end 7600 end
5919 local e=e:get_child("jingle",i); 7601 local e=e:get_child("jingle",i);
5920 return a:handle_command(e); 7602 return o:handle_command(e);
5921 end); 7603 end);
5922 e:event("jingle",a); 7604 e:event("jingle",o);
5923 return true; 7605 return true;
5924 end 7606 end
5925 function t:handle_command(a) 7607 function t:handle_command(a)
5926 local t=a.attr.action; 7608 local t=a.attr.action;
5927 e:debug("Handling Jingle command: %s",t); 7609 e:debug("Handling Jingle command: %s",t);
5938 e:warn("Unhandled Jingle command: %s",t); 7620 e:warn("Unhandled Jingle command: %s",t);
5939 return nil; 7621 return nil;
5940 end 7622 end
5941 return true; 7623 return true;
5942 end 7624 end
5943 function t:send_command(a,t,e) 7625 function t:send_command(e,o,t)
5944 local t=o.iq({to=self.peer,type="set"}) 7626 local e=a.iq({to=self.peer,type="set"})
5945 :tag("jingle",{ 7627 :tag("jingle",{
5946 xmlns=i, 7628 xmlns=i,
5947 sid=self.sid, 7629 sid=self.sid,
5948 action=a, 7630 action=e,
5949 initiator=self.role=="initiator"and self.stream.jid or nil, 7631 initiator=self.role=="initiator"and self.stream.jid or nil,
5950 responder=self.role=="responder"and self.jid or nil, 7632 responder=self.role=="responder"and self.jid or nil,
5951 }):add_child(t); 7633 }):add_child(o);
5952 if not e then 7634 if not t then
5953 self.stream:send(t); 7635 self.stream:send(e);
5954 else 7636 else
5955 self.stream:send_iq(t,e); 7637 self.stream:send_iq(e,t);
5956 end 7638 end
5957 end 7639 end
5958 function t:accept(a) 7640 function t:accept(t)
5959 local t=o.iq({to=self.peer,type="set"}) 7641 local a=a.iq({to=self.peer,type="set"})
5960 :tag("jingle",{ 7642 :tag("jingle",{
5961 xmlns=i, 7643 xmlns=i,
5962 sid=self.sid, 7644 sid=self.sid,
5963 action="session-accept", 7645 action="session-accept",
5964 responder=e.jid, 7646 responder=e.jid,
5965 }) 7647 })
5966 :tag("content",{creator=self.creator,name=self.name}); 7648 :tag("content",{creator=self.creator,name=self.name});
5967 local o=self.content:generate_accept(self.content_tag:child_with_name("description"),a); 7649 local o=self.content:generate_accept(self.content_tag:child_with_name("description"),t);
5968 t:add_child(o); 7650 a:add_child(o);
5969 local a=self.transport:generate_accept(self.content_tag:child_with_name("transport"),a); 7651 local t=self.transport:generate_accept(self.content_tag:child_with_name("transport"),t);
5970 t:add_child(a); 7652 a:add_child(t);
5971 local a=self; 7653 local t=self;
5972 e:send_iq(t,function(t) 7654 e:send_iq(a,function(a)
5973 if t.attr.type=="error"then 7655 if a.attr.type=="error"then
5974 local a,t,a=t:get_error(); 7656 local a,t,a=a:get_error();
5975 e:error("session-accept rejected: %s",t); 7657 e:error("session-accept rejected: %s",t);
5976 return false; 7658 return false;
5977 end 7659 end
5978 a.transport:connect(function(t) 7660 t.transport:connect(function(a)
5979 e:warn("CONNECTED (receiver)!!!"); 7661 e:warn("CONNECTED (receiver)!!!");
5980 a.state="active"; 7662 t.state="active";
5981 a:event("connected",t); 7663 t:event("connected",a);
5982 end); 7664 end);
5983 end); 7665 end);
5984 end 7666 end
5985 e:hook("iq/"..i,u); 7667 e:hook("iq/"..i,u);
5986 return true; 7668 return true;
5987 end 7669 end
5988 function t:offer(t,a) 7670 function t:offer(t,o)
5989 local e=o.iq({to=self.peer,type="set"}) 7671 local e=a.iq({to=self.peer,type="set"})
5990 :tag("jingle",{xmlns=i,action="session-initiate", 7672 :tag("jingle",{xmlns=i,action="session-initiate",
5991 initiator=self.stream.jid,sid=self.sid}); 7673 initiator=self.stream.jid,sid=self.sid});
5992 e:tag("content",{creator=self.role,name=t}); 7674 e:tag("content",{creator=self.role,name=t});
5993 local t=self.stream:event("jingle/describe/"..t,a); 7675 local t=self.stream:event("jingle/describe/"..t,o);
5994 if not t then 7676 if not t then
5995 return false,"Unknown content type"; 7677 return false,"Unknown content type";
5996 end 7678 end
5997 e:add_child(t); 7679 e:add_child(t);
5998 local t=self.stream:event("jingle/transport/".."urn:xmpp:jingle:transports:s5b:1",self); 7680 local t=self.stream:event("jingle/transport/".."urn:xmpp:jingle:transports:s5b:1",self);
6014 end 7696 end
6015 end); 7697 end);
6016 self.state="pending"; 7698 self.state="pending";
6017 end 7699 end
6018 function t:terminate(e) 7700 function t:terminate(e)
6019 local e=o.stanza("reason"):tag(e or"success"); 7701 local e=a.stanza("reason"):tag(e or"success");
6020 self:send_command("session-terminate",e,function(e) 7702 self:send_command("session-terminate",e,function(e)
6021 self.state="terminated"; 7703 self.state="terminated";
6022 self.transport:disconnect(); 7704 self.transport:disconnect();
6023 self:destroy(); 7705 self:destroy();
6024 end); 7706 end);
6075 _ENV=e; 7757 _ENV=e;
6076 _M=e; 7758 _M=e;
6077 return e; 7759 return e;
6078 end 7760 end
6079 local i=require"verse"; 7761 local i=require"verse";
6080 local o=require"ltn12"; 7762 local n=require"ltn12";
6081 local s=package.config:sub(1,1); 7763 local s=package.config:sub(1,1);
6082 local a="urn:xmpp:jingle:apps:file-transfer:4"; 7764 local a="urn:xmpp:jingle:apps:file-transfer:4";
6083 function i.plugins.jingle_ft(t) 7765 function i.plugins.jingle_ft(t)
6084 t:hook("ready",function() 7766 t:hook("ready",function()
6085 t:add_disco_feature(a); 7767 t:add_disco_feature(a);
6086 end,10); 7768 end,10);
6087 local n={type="file"}; 7769 local o={type="file"};
6088 function n:generate_accept(t,e) 7770 function o:generate_accept(t,e)
6089 if e and e.save_file then 7771 if e and e.save_file then
6090 self.jingle:hook("connected",function() 7772 self.jingle:hook("connected",function()
6091 local e=o.sink.file(io.open(e.save_file,"w+")); 7773 local e=n.sink.file(io.open(e.save_file,"w+"));
6092 self.jingle:set_sink(e); 7774 self.jingle:set_sink(e);
6093 end); 7775 end);
6094 end 7776 end
6095 return t; 7777 return t;
6096 end 7778 end
6097 local n={__index=n}; 7779 local o={__index=o};
6098 t:hook("jingle/content/"..a,function(t,e) 7780 t:hook("jingle/content/"..a,function(t,e)
6099 local e=e:get_child("file"); 7781 local e=e:get_child("file");
6100 local e={ 7782 local e={
6101 name=e:get_child_text("name"); 7783 name=e:get_child_text("name");
6102 size=tonumber(e:get_child_text("size")); 7784 size=tonumber(e:get_child_text("size"));
6103 desc=e:get_child_text("desc"); 7785 desc=e:get_child_text("desc");
6104 date=e:get_child_text("date"); 7786 date=e:get_child_text("date");
6105 }; 7787 };
6106 return setmetatable({jingle=t,file=e},n); 7788 return setmetatable({jingle=t,file=e},o);
6107 end); 7789 end);
6108 t:hook("jingle/describe/file",function(e) 7790 t:hook("jingle/describe/file",function(e)
6109 local t; 7791 local t;
6110 if e.timestamp then 7792 if e.timestamp then
6111 t=os.date("!%Y-%m-%dT%H:%M:%SZ",e.timestamp); 7793 t=os.date("!%Y-%m-%dT%H:%M:%SZ",e.timestamp);
6119 :up(); 7801 :up();
6120 end); 7802 end);
6121 function t:send_file(i,t) 7803 function t:send_file(i,t)
6122 local e,a=io.open(t); 7804 local e,a=io.open(t);
6123 if not e then return e,a;end 7805 if not e then return e,a;end
6124 local a=e:seek("end",0); 7806 local o=e:seek("end",0);
6125 e:seek("set",0); 7807 e:seek("set",0);
6126 local o=o.source.file(e); 7808 local a=n.source.file(e);
6127 local e=self:jingle(i); 7809 local e=self:jingle(i);
6128 e:offer("file",{ 7810 e:offer("file",{
6129 filename=t:match("[^"..s.."]+$"); 7811 filename=t:match("[^"..s.."]+$");
6130 size=a; 7812 size=o;
6131 }); 7813 });
6132 e:hook("connected",function() 7814 e:hook("connected",function()
6133 e:set_source(o,true); 7815 e:set_source(a,true);
6134 end); 7816 end);
6135 return e; 7817 return e;
6136 end 7818 end
6137 end 7819 end
6138 end) 7820 end)
6146 end 7828 end
6147 _ENV=e; 7829 _ENV=e;
6148 _M=e; 7830 _M=e;
6149 return e; 7831 return e;
6150 end 7832 end
6151 local t=require"verse"; 7833 local a=require"verse";
6152 local o="urn:xmpp:jingle:transports:s5b:1"; 7834 local o="urn:xmpp:jingle:transports:s5b:1";
6153 local r="http://jabber.org/protocol/bytestreams"; 7835 local d="http://jabber.org/protocol/bytestreams";
6154 local h=require"util.hashes".sha1; 7836 local h=require"util.hashes".sha1;
6155 local s=require"util.uuid".generate; 7837 local l=require"util.id".short;
6156 local function d(e,t) 7838 local function r(e,s)
6157 local function a() 7839 local function n()
6158 e:unhook("connected",a); 7840 e:unhook("connected",n);
6159 return true; 7841 return true;
6160 end 7842 end
6161 local function i(t) 7843 local function i(t)
6162 e:unhook("incoming-raw",i); 7844 e:unhook("incoming-raw",i);
6163 if t:sub(1,2)~="\005\000"then 7845 if t:sub(1,2)~="\005\000"then
6164 return e:event("error","connection-failure"); 7846 return e:event("error","connection-failure");
6165 end 7847 end
6166 e:event("connected"); 7848 e:event("connected");
6167 return true; 7849 return true;
6168 end 7850 end
6169 local function o(a) 7851 local function o(t)
6170 e:unhook("incoming-raw",o); 7852 e:unhook("incoming-raw",o);
6171 if a~="\005\000"then 7853 if t~="\005\000"then
6172 local t="version-mismatch"; 7854 local a="version-mismatch";
6173 if a:sub(1,1)=="\005"then 7855 if t:sub(1,1)=="\005"then
6174 t="authentication-failure"; 7856 a="authentication-failure";
6175 end 7857 end
6176 return e:event("error",t); 7858 return e:event("error",a);
6177 end 7859 end
6178 e:send(string.char(5,1,0,3,#t)..t.."\0\0"); 7860 e:send(string.char(5,1,0,3,#s)..s.."\0\0");
6179 e:hook("incoming-raw",i,100); 7861 e:hook("incoming-raw",i,100);
6180 return true; 7862 return true;
6181 end 7863 end
6182 e:hook("connected",a,200); 7864 e:hook("connected",n,200);
6183 e:hook("incoming-raw",o,100); 7865 e:hook("incoming-raw",o,100);
6184 e:send("\005\001\000"); 7866 e:send("\005\001\000");
6185 end 7867 end
6186 local function n(a,e,i) 7868 local function s(o,e,i)
6187 local e=t.new(nil,{ 7869 local e=a.new(nil,{
6188 streamhosts=e, 7870 streamhosts=e,
6189 current_host=0; 7871 current_host=0;
6190 }); 7872 });
6191 local function t(o) 7873 local function t(a)
6192 if o then 7874 if a then
6193 return a(nil,o.reason); 7875 return o(nil,a.reason);
6194 end 7876 end
6195 if e.current_host<#e.streamhosts then 7877 if e.current_host<#e.streamhosts then
6196 e.current_host=e.current_host+1; 7878 e.current_host=e.current_host+1;
6197 e:debug("Attempting to connect to "..e.streamhosts[e.current_host].host..":"..e.streamhosts[e.current_host].port.."..."); 7879 e:debug("Attempting to connect to "..e.streamhosts[e.current_host].host..":"..e.streamhosts[e.current_host].port.."...");
6198 local t,a=e:connect( 7880 local a,t=e:connect(
6199 e.streamhosts[e.current_host].host, 7881 e.streamhosts[e.current_host].host,
6200 e.streamhosts[e.current_host].port 7882 e.streamhosts[e.current_host].port
6201 ); 7883 );
6202 if not t then 7884 if not a then
6203 e:debug("Error connecting to proxy (%s:%s): %s", 7885 e:debug("Error connecting to proxy (%s:%s): %s",
6204 e.streamhosts[e.current_host].host, 7886 e.streamhosts[e.current_host].host,
6205 e.streamhosts[e.current_host].port, 7887 e.streamhosts[e.current_host].port,
6206 a 7888 t
6207 ); 7889 );
6208 else 7890 else
6209 e:debug("Connecting..."); 7891 e:debug("Connecting...");
6210 end 7892 end
6211 d(e,i); 7893 r(e,i);
6212 return true; 7894 return true;
6213 end 7895 end
6214 e:unhook("disconnected",t); 7896 e:unhook("disconnected",t);
6215 return a(nil); 7897 return o(nil);
6216 end 7898 end
6217 e:hook("disconnected",t,100); 7899 e:hook("disconnected",t,100);
6218 e:hook("connected",function() 7900 e:hook("connected",function()
6219 e:unhook("disconnected",t); 7901 e:unhook("disconnected",t);
6220 a(e.streamhosts[e.current_host],e); 7902 o(e.streamhosts[e.current_host],e);
6221 end,100); 7903 end,100);
6222 t(); 7904 t();
6223 return e; 7905 return e;
6224 end 7906 end
6225 function t.plugins.jingle_s5b(e) 7907 function a.plugins.jingle_s5b(e)
6226 e:hook("ready",function() 7908 e:hook("ready",function()
6227 e:add_disco_feature(o); 7909 e:add_disco_feature(o);
6228 end,10); 7910 end,10);
6229 local a={}; 7911 local t={};
6230 function a:generate_initiate() 7912 function t:generate_initiate()
6231 self.s5b_sid=s(); 7913 self.s5b_sid=l();
6232 local a=t.stanza("transport",{xmlns=o, 7914 local i=a.stanza("transport",{xmlns=o,
6233 mode="tcp",sid=self.s5b_sid}); 7915 mode="tcp",sid=self.s5b_sid});
6234 local t=0; 7916 local t=0;
6235 for i,o in pairs(e.proxy65.available_streamhosts)do 7917 for a,o in pairs(e.proxy65.available_streamhosts)do
6236 t=t+1; 7918 t=t+1;
6237 a:tag("candidate",{jid=i,host=o.host, 7919 i:tag("candidate",{jid=a,host=o.host,
6238 port=o.port,cid=i,priority=t,type="proxy"}):up(); 7920 port=o.port,cid=a,priority=t,type="proxy"}):up();
6239 end 7921 end
6240 e:debug("Have %d proxies",t) 7922 e:debug("Have %d proxies",t)
6241 return a; 7923 return i;
6242 end 7924 end
6243 function a:generate_accept(e) 7925 function t:generate_accept(e)
6244 local a={}; 7926 local t={};
6245 self.s5b_peer_candidates=a; 7927 self.s5b_peer_candidates=t;
6246 self.s5b_mode=e.attr.mode or"tcp"; 7928 self.s5b_mode=e.attr.mode or"tcp";
6247 self.s5b_sid=e.attr.sid or self.jingle.sid; 7929 self.s5b_sid=e.attr.sid or self.jingle.sid;
6248 for e in e:childtags()do 7930 for e in e:childtags()do
6249 a[e.attr.cid]={ 7931 t[e.attr.cid]={
6250 type=e.attr.type; 7932 type=e.attr.type;
6251 jid=e.attr.jid; 7933 jid=e.attr.jid;
6252 host=e.attr.host; 7934 host=e.attr.host;
6253 port=tonumber(e.attr.port)or 0; 7935 port=tonumber(e.attr.port)or 0;
6254 priority=tonumber(e.attr.priority)or 0; 7936 priority=tonumber(e.attr.priority)or 0;
6255 cid=e.attr.cid; 7937 cid=e.attr.cid;
6256 }; 7938 };
6257 end 7939 end
6258 local e=t.stanza("transport",{xmlns=o}); 7940 local e=a.stanza("transport",{xmlns=o});
6259 return e; 7941 return e;
6260 end 7942 end
6261 function a:connect(i) 7943 function t:connect(i)
6262 e:warn("Connecting!"); 7944 e:warn("Connecting!");
6263 local a={}; 7945 local t={};
6264 for t,e in pairs(self.s5b_peer_candidates or{})do 7946 for a,e in pairs(self.s5b_peer_candidates or{})do
6265 a[#a+1]=e; 7947 t[#t+1]=e;
6266 end 7948 end
6267 if#a>0 then 7949 if#t>0 then
6268 self.connecting_peer_candidates=true; 7950 self.connecting_peer_candidates=true;
6269 local function s(e,a) 7951 local function n(e,t)
6270 self.jingle:send_command("transport-info",t.stanza("content",{creator=self.creator,name=self.name}) 7952 self.jingle:send_command("transport-info",a.stanza("content",{creator=self.creator,name=self.name})
6271 :tag("transport",{xmlns=o,sid=self.s5b_sid}) 7953 :tag("transport",{xmlns=o,sid=self.s5b_sid})
6272 :tag("candidate-used",{cid=e.cid})); 7954 :tag("candidate-used",{cid=e.cid}));
6273 self.onconnect_callback=i; 7955 self.onconnect_callback=i;
6274 self.conn=a; 7956 self.conn=t;
6275 end 7957 end
6276 local e=h(self.s5b_sid..self.peer..e.jid,true); 7958 local e=h(self.s5b_sid..self.peer..e.jid,true);
6277 n(s,a,e); 7959 s(n,t,e);
6278 else 7960 else
6279 e:warn("Actually, I'm going to wait for my peer to tell me its streamhost..."); 7961 e:warn("Actually, I'm going to wait for my peer to tell me its streamhost...");
6280 self.onconnect_callback=i; 7962 self.onconnect_callback=i;
6281 end 7963 end
6282 end 7964 end
6283 function a:info_received(a) 7965 function t:info_received(t)
6284 e:warn("Info received"); 7966 e:warn("Info received");
6285 local s=a:child_with_name("content"); 7967 local n=t:child_with_name("content");
6286 local i=s:child_with_name("transport"); 7968 local i=n:child_with_name("transport");
6287 if i:get_child("candidate-used")and not self.connecting_peer_candidates then 7969 if i:get_child("candidate-used")and not self.connecting_peer_candidates then
6288 local a=i:child_with_name("candidate-used"); 7970 local t=i:child_with_name("candidate-used");
6289 if a then 7971 if t then
6290 local function i(i,e) 7972 local function r(i,e)
6291 if self.jingle.role=="initiator"then 7973 if self.jingle.role=="initiator"then
6292 self.jingle.stream:send_iq(t.iq({to=i.jid,type="set"}) 7974 self.jingle.stream:send_iq(a.iq({to=i.jid,type="set"})
6293 :tag("query",{xmlns=r,sid=self.s5b_sid}) 7975 :tag("query",{xmlns=d,sid=self.s5b_sid})
6294 :tag("activate"):text(self.jingle.peer),function(i) 7976 :tag("activate"):text(self.jingle.peer),function(i)
6295 if i.attr.type=="result"then 7977 if i.attr.type=="result"then
6296 self.jingle:send_command("transport-info",t.stanza("content",s.attr) 7978 self.jingle:send_command("transport-info",a.stanza("content",n.attr)
6297 :tag("transport",{xmlns=o,sid=self.s5b_sid}) 7979 :tag("transport",{xmlns=o,sid=self.s5b_sid})
6298 :tag("activated",{cid=a.attr.cid})); 7980 :tag("activated",{cid=t.attr.cid}));
6299 self.conn=e; 7981 self.conn=e;
6300 self.onconnect_callback(e); 7982 self.onconnect_callback(e);
6301 else 7983 else
6302 self.jingle.stream:error("Failed to activate bytestream"); 7984 self.jingle.stream:error("Failed to activate bytestream");
6303 end 7985 end
6304 end); 7986 end);
6305 end 7987 end
6306 end 7988 end
6307 self.jingle.stream:debug("CID: %s",self.jingle.stream.proxy65.available_streamhosts[a.attr.cid]); 7989 self.jingle.stream:debug("CID: %s",self.jingle.stream.proxy65.available_streamhosts[t.attr.cid]);
6308 local t={ 7990 local t={
6309 self.jingle.stream.proxy65.available_streamhosts[a.attr.cid]; 7991 self.jingle.stream.proxy65.available_streamhosts[t.attr.cid];
6310 }; 7992 };
6311 local e=h(self.s5b_sid..e.jid..self.peer,true); 7993 local e=h(self.s5b_sid..e.jid..self.peer,true);
6312 n(i,t,e); 7994 s(r,t,e);
6313 end 7995 end
6314 elseif i:get_child("activated")then 7996 elseif i:get_child("activated")then
6315 self.onconnect_callback(self.conn); 7997 self.onconnect_callback(self.conn);
6316 end 7998 end
6317 end 7999 end
6318 function a:disconnect() 8000 function t:disconnect()
6319 if self.conn then 8001 if self.conn then
6320 self.conn:close(); 8002 self.conn:close();
6321 end 8003 end
6322 end 8004 end
6323 function a:handle_accepted(e) 8005 function t:handle_accepted(e)
6324 end 8006 end
6325 local t={__index=a}; 8007 local t={__index=t};
6326 e:hook("jingle/transport/"..o,function(e) 8008 e:hook("jingle/transport/"..o,function(e)
6327 return setmetatable({ 8009 return setmetatable({
6328 role=e.role, 8010 role=e.role,
6329 peer=e.peer, 8011 peer=e.peer,
6330 stream=e.stream, 8012 stream=e.stream,
6344 _ENV=e; 8026 _ENV=e;
6345 _M=e; 8027 _M=e;
6346 return e; 8028 return e;
6347 end 8029 end
6348 local a=require"verse"; 8030 local a=require"verse";
6349 local d=require"util.uuid"; 8031 local h=require"util.id".short;
6350 local r=require"util.hashes".sha1; 8032 local r=require"util.hashes".sha1;
6351 local n={}; 8033 local n={};
6352 n.__index=n; 8034 n.__index=n;
6353 local i="http://jabber.org/protocol/bytestreams"; 8035 local i="http://jabber.org/protocol/bytestreams";
6354 local s; 8036 local s;
6415 function e:refuse() 8097 function e:refuse()
6416 end 8098 end
6417 t:event("proxy65/request",e); 8099 t:event("proxy65/request",e);
6418 end); 8100 end);
6419 end 8101 end
6420 function n:new(t,h) 8102 function n:new(t,n)
6421 local e=a.new(nil,{ 8103 local e=a.new(nil,{
6422 target_jid=t; 8104 target_jid=t;
6423 bytestream_sid=d.generate(); 8105 bytestream_sid=h();
6424 }); 8106 });
6425 local o=a.iq{type="set",to=t} 8107 local o=a.iq{type="set",to=t}
6426 :tag("query",{xmlns=i,mode="tcp",sid=e.bytestream_sid}); 8108 :tag("query",{xmlns=i,mode="tcp",sid=e.bytestream_sid});
6427 for t,e in ipairs(h or self.proxies)do 8109 for t,e in ipairs(n or self.proxies)do
6428 o:tag("streamhost",e):up(); 8110 o:tag("streamhost",e):up();
6429 end 8111 end
6430 self.stream:send_iq(o,function(o) 8112 self.stream:send_iq(o,function(o)
6431 if o.attr.type=="error"then 8113 if o.attr.type=="error"then
6432 local a,t,o=o:get_error(); 8114 local t,a,o=o:get_error();
6433 e:event("connection-failed",{conn=e,type=a,condition=t,text=o}); 8115 e:event("connection-failed",{conn=e,type=t,condition=a,text=o});
6434 else 8116 else
6435 local o=o.tags[1]:get_child("streamhost-used"); 8117 local o=o.tags[1]:get_child("streamhost-used");
6436 e.streamhost_jid=o.attr.jid; 8118 e.streamhost_jid=o.attr.jid;
6437 local n,o; 8119 local h,o;
6438 for a,t in ipairs(h or self.proxies)do 8120 for a,t in ipairs(n or self.proxies)do
6439 if t.jid==e.streamhost_jid then 8121 if t.jid==e.streamhost_jid then
6440 n,o=t.host,t.port; 8122 h,o=t.host,t.port;
6441 break; 8123 break;
6442 end 8124 end
6443 end 8125 end
6444 e:connect(n,o); 8126 e:connect(h,o);
6445 local function o() 8127 local function o()
6446 e:unhook("connected",o); 8128 e:unhook("connected",o);
6447 local t=a.iq{to=e.streamhost_jid,type="set"} 8129 local t=a.iq{to=e.streamhost_jid,type="set"}
6448 :tag("query",{xmlns=i,sid=e.bytestream_sid}) 8130 :tag("query",{xmlns=i,sid=e.bytestream_sid})
6449 :tag("activate"):text(t); 8131 :tag("activate"):text(t);
6458 s(self.stream,e,e.bytestream_sid,self.stream.jid,t); 8140 s(self.stream,e,e.bytestream_sid,self.stream.jid,t);
6459 end 8141 end
6460 end); 8142 end);
6461 return e; 8143 return e;
6462 end 8144 end
6463 function s(i,e,a,t,o) 8145 function s(i,e,t,a,o)
6464 local a=r(a..t..o); 8146 local t=r(t..a..o);
6465 local function t() 8147 local function a()
6466 e:unhook("connected",t); 8148 e:unhook("connected",a);
6467 return true; 8149 return true;
6468 end 8150 end
6469 local function o(t) 8151 local function o(t)
6470 e:unhook("incoming-raw",o); 8152 e:unhook("incoming-raw",o);
6471 if t:sub(1,2)~="\005\000"then 8153 if t:sub(1,2)~="\005\000"then
6472 return e:event("error","connection-failure"); 8154 return e:event("error","connection-failure");
6473 end 8155 end
6474 e:event("connected"); 8156 e:event("connected");
6475 return true; 8157 return true;
6476 end 8158 end
6477 local function i(t) 8159 local function i(a)
6478 e:unhook("incoming-raw",i); 8160 e:unhook("incoming-raw",i);
6479 if t~="\005\000"then 8161 if a~="\005\000"then
6480 local a="version-mismatch"; 8162 local t="version-mismatch";
6481 if t:sub(1,1)=="\005"then 8163 if a:sub(1,1)=="\005"then
6482 a="authentication-failure"; 8164 t="authentication-failure";
6483 end 8165 end
6484 return e:event("error",a); 8166 return e:event("error",t);
6485 end 8167 end
6486 e:send(string.char(5,1,0,3,#a)..a.."\0\0"); 8168 e:send(string.char(5,1,0,3,#t)..t.."\0\0");
6487 e:hook("incoming-raw",o,100); 8169 e:hook("incoming-raw",o,100);
6488 return true; 8170 return true;
6489 end 8171 end
6490 e:hook("connected",t,200); 8172 e:hook("connected",a,200);
6491 e:hook("incoming-raw",i,100); 8173 e:hook("incoming-raw",i,100);
6492 e:send("\005\001\000"); 8174 e:send("\005\001\000");
6493 end 8175 end
6494 end) 8176 end)
6495 package.preload['verse.plugins.jingle_ibb']=(function(...) 8177 package.preload['verse.plugins.jingle_ibb']=(function(...)
6504 _M=e; 8186 _M=e;
6505 return e; 8187 return e;
6506 end 8188 end
6507 local e=require"verse"; 8189 local e=require"verse";
6508 local i=require"util.encodings".base64; 8190 local i=require"util.encodings".base64;
6509 local s=require"util.uuid".generate; 8191 local h=require"util.id".short;
6510 local n="urn:xmpp:jingle:transports:ibb:1"; 8192 local n="urn:xmpp:jingle:transports:ibb:1";
6511 local o="http://jabber.org/protocol/ibb"; 8193 local o="http://jabber.org/protocol/ibb";
6512 assert(i.encode("This is a test.")=="VGhpcyBpcyBhIHRlc3Qu","Base64 encoding failed"); 8194 assert(i.encode("This is a test.")=="VGhpcyBpcyBhIHRlc3Qu","Base64 encoding failed");
6513 assert(i.decode("VGhpcyBpcyBhIHRlc3Qu")=="This is a test.","Base64 decoding failed"); 8195 assert(i.decode("VGhpcyBpcyBhIHRlc3Qu")=="This is a test.","Base64 decoding failed");
6514 local t=table.concat
6515 local a={}; 8196 local a={};
6516 local t={__index=a}; 8197 local t={__index=a};
6517 local function h(a) 8198 local function s(a)
6518 local t=setmetatable({stream=a},t) 8199 local t=setmetatable({stream=a},t)
6519 t=e.eventable(t); 8200 t=e.eventable(t);
6520 return t; 8201 return t;
6521 end 8202 end
6522 function a:initiate(e,t,a) 8203 function a:initiate(t,e,a)
6523 self.block=2048; 8204 self.block=2048;
6524 self.stanza=a or'iq'; 8205 self.stanza=a or'iq';
6525 self.peer=e; 8206 self.peer=t;
6526 self.sid=t or tostring(self):match("%x+$"); 8207 self.sid=e or tostring(self):match("%x+$");
6527 self.iseq=0; 8208 self.iseq=0;
6528 self.oseq=0; 8209 self.oseq=0;
6529 local e=function(e) 8210 local e=function(e)
6530 return self:feed(e) 8211 return self:feed(e)
6531 end 8212 end
6532 self.feeder=e; 8213 self.feeder=e;
6533 print("Hooking incomming IQs"); 8214 print("Hooking incoming IQs");
6534 local t=self.stream; 8215 local t=self.stream;
6535 t:hook("iq/"..o,e) 8216 t:hook("iq/"..o,e)
6536 if a=="message"then 8217 if a=="message"then
6537 t:hook("message",e) 8218 t:hook("message",e)
6538 end 8219 end
6618 a:hook("ready",function() 8299 a:hook("ready",function()
6619 a:add_disco_feature(n); 8300 a:add_disco_feature(n);
6620 end,10); 8301 end,10);
6621 local t={}; 8302 local t={};
6622 function t:_setup() 8303 function t:_setup()
6623 local e=h(self.stream); 8304 local e=s(self.stream);
6624 e.sid=self.sid or e.sid; 8305 e.sid=self.sid or e.sid;
6625 e.stanza=self.stanza or e.stanza; 8306 e.stanza=self.stanza or e.stanza;
6626 e.block=self.block or e.block; 8307 e.block=self.block or e.block;
6627 e:initiate(self.peer,self.sid,self.stanza); 8308 e:initiate(self.peer,self.sid,self.stanza);
6628 self.conn=e; 8309 self.conn=e;
6629 end 8310 end
6630 function t:generate_initiate() 8311 function t:generate_initiate()
6631 print("ibb:generate_initiate() as "..self.role); 8312 print("ibb:generate_initiate() as "..self.role);
6632 local t=s(); 8313 local t=h();
6633 self.sid=t; 8314 self.sid=t;
6634 self.stanza='iq'; 8315 self.stanza='iq';
6635 self.block=2048; 8316 self.block=2048;
6636 local e=e.stanza("transport",{xmlns=n, 8317 local e=e.stanza("transport",{xmlns=n,
6637 sid=self.sid,stanza=self.stanza,["block-size"]=self.block}); 8318 sid=self.sid,stanza=self.stanza,["block-size"]=self.block});
6692 _ENV=e; 8373 _ENV=e;
6693 _M=e; 8374 _M=e;
6694 return e; 8375 return e;
6695 end 8376 end
6696 local i=require"verse"; 8377 local i=require"verse";
6697 local n=table.insert; 8378 local h=table.insert;
6698 local o="http://jabber.org/protocol/pubsub"; 8379 local o="http://jabber.org/protocol/pubsub";
6699 local h="http://jabber.org/protocol/pubsub#owner"; 8380 local s="http://jabber.org/protocol/pubsub#owner";
6700 local a="http://jabber.org/protocol/pubsub#event"; 8381 local r="http://jabber.org/protocol/pubsub#event";
6701 local e={}; 8382 local e={};
6702 local s={__index=e}; 8383 local n={__index=e};
6703 function i.plugins.pubsub(e) 8384 function i.plugins.pubsub(e)
6704 e.pubsub=setmetatable({stream=e},s); 8385 e.pubsub=setmetatable({stream=e},n);
6705 e:hook("message",function(t) 8386 e:hook("message",function(t)
6706 local o=t.attr.from; 8387 local a=t.attr.from;
6707 for t in t:childtags("event",a)do 8388 for t in t:childtags("event",r)do
6708 local t=t:get_child("items"); 8389 local t=t:get_child("items");
6709 if t then 8390 if t then
6710 local a=t.attr.node; 8391 local o=t.attr.node;
6711 for t in t:childtags("item")do 8392 for t in t:childtags("item")do
6712 e:event("pubsub/event",{ 8393 e:event("pubsub/event",{
6713 from=o; 8394 from=a;
6714 node=a; 8395 node=o;
6715 item=t; 8396 item=t;
6716 }); 8397 });
6717 end 8398 end
8399 for t in t:childtags("retract")do
8400 e:event("pubsub/retraction",{
8401 from=a;
8402 node=o;
8403 item=t;
8404 });
8405 end
6718 end 8406 end
6719 end 8407 end
6720 end); 8408 end);
6721 return true; 8409 return true;
6722 end 8410 end
6723 function e:create(e,a,t) 8411 function e:create(a,e,t)
6724 return self:service(e):node(a):create(nil,t); 8412 return self:service(a):node(e):create(nil,t);
6725 end 8413 end
6726 function e:subscribe(a,e,o,t) 8414 function e:subscribe(o,t,a,e)
6727 return self:service(a):node(e):subscribe(o,nil,t); 8415 return self:service(o):node(t):subscribe(a,nil,e);
6728 end 8416 end
6729 function e:publish(e,a,o,i,t) 8417 function e:publish(e,t,o,a,i)
6730 return self:service(e):node(a):publish(o,nil,i,t); 8418 return self:service(e):node(t):publish(o,nil,a,i);
6731 end 8419 end
6732 local a={}; 8420 local a={};
6733 local t={__index=a}; 8421 local t={__index=a};
6734 function e:service(e) 8422 function e:service(e)
6735 return setmetatable({stream=self.stream,service=e},t) 8423 return setmetatable({stream=self.stream,service=e},t)
6736 end 8424 end
6737 local function t(h,e,r,a,s,n,t) 8425 local function t(d,r,e,n,h,s,t,a)
6738 local e=i.iq{type=h or"get",to=e} 8426 local e=i.iq{type=d or"get",to=r}
6739 :tag("pubsub",{xmlns=r or o}) 8427 :tag("pubsub",{xmlns=e or o})
6740 if a then e:tag(a,{node=s,jid=n});end 8428 local o={node=h,jid=s};
6741 if t then e:tag("item",{id=t~=true and t or nil});end 8429 if a then
8430 for t,e in pairs(a)do
8431 o[t]=e;
8432 end
8433 end
8434 if n then e:tag(n,o);end
8435 if t then
8436 e:tag("item",{id=t~=true and t or nil});
8437 end
6742 return e; 8438 return e;
6743 end 8439 end
6744 function a:subscriptions(a) 8440 function a:subscriptions(a)
6745 self.stream:send_iq(t(nil,self.service,nil,"subscriptions") 8441 self.stream:send_iq(t(nil,self.service,nil,"subscriptions")
6746 ,a and function(t) 8442 ,a and function(e)
6747 if t.attr.type=="result"then 8443 if e.attr.type=="result"then
6748 local e=t:get_child("pubsub",o); 8444 local e=e:get_child("pubsub",o);
6749 local e=e and e:get_child("subscriptions"); 8445 local e=e and e:get_child("subscriptions");
6750 local o={}; 8446 local o={};
6751 if e then 8447 if e then
6752 for t in e:childtags("subscription")do 8448 for e in e:childtags("subscription")do
6753 local e=self:node(t.attr.node) 8449 local t=self:node(e.attr.node)
6754 e.subscription=t; 8450 t.subscription=e;
6755 e.subscribed_jid=t.attr.jid; 8451 t.subscribed_jid=e.attr.jid;
6756 n(o,e); 8452 h(o,t);
6757 end 8453 end
6758 end 8454 end
6759 a(o); 8455 a(o);
6760 else 8456 else
6761 a(false,t:get_error()); 8457 a(false,e:get_error());
6762 end 8458 end
6763 end or nil); 8459 end or nil);
6764 end 8460 end
6765 function a:affiliations(a) 8461 function a:affiliations(a)
6766 self.stream:send_iq(t(nil,self.service,nil,"affiliations") 8462 self.stream:send_iq(t(nil,self.service,nil,"affiliations")
6767 ,a and function(e) 8463 ,a and function(e)
6768 if e.attr.type=="result"then 8464 if e.attr.type=="result"then
6769 local e=e:get_child("pubsub",o); 8465 local e=e:get_child("pubsub",o);
6770 local e=e and e:get_child("affiliations")or{}; 8466 local e=e and e:get_child("affiliations")or{};
6771 local t={}; 8467 local o={};
6772 if e then 8468 if e then
6773 for e in e:childtags("affiliation")do 8469 for e in e:childtags("affiliation")do
6774 local a=self:node(e.attr.node) 8470 local t=self:node(e.attr.node)
6775 a.affiliation=e; 8471 t.affiliation=e;
6776 n(t,a); 8472 h(o,t);
6777 end 8473 end
6778 end 8474 end
6779 a(t); 8475 a(o);
6780 else 8476 else
6781 a(false,e:get_error()); 8477 a(false,e:get_error());
6782 end 8478 end
6783 end or nil); 8479 end or nil);
6784 end 8480 end
6795 local e={}; 8491 local e={};
6796 local o={__index=e}; 8492 local o={__index=e};
6797 function a:node(e) 8493 function a:node(e)
6798 return setmetatable({stream=self.stream,service=self.service,node=e},o) 8494 return setmetatable({stream=self.stream,service=self.service,node=e},o)
6799 end 8495 end
6800 function s:__call(t,e) 8496 function n:__call(t,e)
6801 local t=self:service(t); 8497 local t=self:service(t);
6802 return e and t:node(e)or t; 8498 return e and t:node(e)or t;
6803 end 8499 end
6804 function e:hook(a,o) 8500 function e:hook(a,o)
6805 self._hooks=self._hooks or setmetatable({},{__mode='kv'}); 8501 self._hooks=self._hooks or setmetatable({},{__mode='kv'});
6820 for e in pairs(self._hooks)do 8516 for e in pairs(self._hooks)do
6821 self.stream:unhook("pubsub/event",e); 8517 self.stream:unhook("pubsub/event",e);
6822 end 8518 end
6823 end 8519 end
6824 end 8520 end
6825 function e:create(a,e) 8521 function e:create(e,a)
6826 if a~=nil then 8522 if e~=nil then
6827 error("Not implemented yet."); 8523 error("Not implemented yet.");
6828 else 8524 else
6829 self.stream:send_iq(t("set",self.service,nil,"create",self.node),e); 8525 self.stream:send_iq(t("set",self.service,nil,"create",self.node),a);
6830 end 8526 end
6831 end 8527 end
6832 function e:configure(e,a) 8528 function e:configure(e,a)
6833 if e~=nil then 8529 if e~=nil then
6834 error("Not implemented yet."); 8530 error("Not implemented yet.");
6835 end 8531 end
6836 self.stream:send_iq(t("set",self.service,nil,e==nil and"default"or"configure",self.node),a); 8532 self.stream:send_iq(t("set",self.service,nil,e==nil and"default"or"configure",self.node),a);
6837 end 8533 end
6838 function e:publish(a,e,o,i) 8534 function e:publish(i,a,e,o)
6839 if e~=nil then 8535 if a~=nil then
6840 error("Node configuration is not implemented yet."); 8536 error("Node configuration is not implemented yet.");
6841 end 8537 end
6842 self.stream:send_iq(t("set",self.service,nil,"publish",self.node,nil,a or true) 8538 self.stream:send_iq(t("set",self.service,nil,"publish",self.node,nil,i or true)
6843 :add_child(o) 8539 :add_child(e)
6844 ,i); 8540 ,o);
6845 end 8541 end
6846 function e:subscribe(e,o,a) 8542 function e:subscribe(e,a,o)
6847 e=e or self.stream.jid; 8543 e=e or self.stream.jid;
6848 if o~=nil then 8544 if a~=nil then
6849 error("Subscription configuration is not implemented yet."); 8545 error("Subscription configuration is not implemented yet.");
6850 end 8546 end
6851 self.stream:send_iq(t("set",self.service,nil,"subscribe",self.node,e) 8547 self.stream:send_iq(t("set",self.service,nil,"subscribe",self.node,e)
6852 ,a); 8548 ,o);
6853 end 8549 end
6854 function e:subscription(e) 8550 function e:subscription(e)
6855 error("Not implemented yet."); 8551 error("Not implemented yet.");
6856 end 8552 end
6857 function e:affiliation(e) 8553 function e:affiliation(e)
6863 ,a); 8559 ,a);
6864 end 8560 end
6865 function e:configure_subscription(e,e) 8561 function e:configure_subscription(e,e)
6866 error("Not implemented yet."); 8562 error("Not implemented yet.");
6867 end 8563 end
6868 function e:items(a,e) 8564 function e:items(t,e)
6869 if a then 8565 if t then
6870 self.stream:send_iq(t("get",self.service,nil,"items",self.node) 8566 return self:item(nil,e);
6871 ,e);
6872 else 8567 else
6873 self.stream:disco_items(self.service,self.node,e); 8568 self.stream:disco_items(self.service,self.node,e);
6874 end 8569 end
6875 end 8570 end
6876 function e:item(e,a) 8571 function e:item(e,a)
6877 self.stream:send_iq(t("get",self.service,nil,"items",self.node,nil,e) 8572 self.stream:send_iq(t("get",self.service,nil,"items",self.node,nil,e)
6878 ,a); 8573 ,a);
6879 end 8574 end
6880 function e:retract(a,e) 8575 function e:retract(o,e,a)
6881 self.stream:send_iq(t("set",self.service,nil,"retract",self.node,nil,a) 8576 if type(e)=="function"then
6882 ,e); 8577 e,a=false,e;
6883 end 8578 end
6884 function e:purge(a,e) 8579 self.stream:send_iq(
6885 assert(not a,"Not implemented yet."); 8580 t(
6886 self.stream:send_iq(t("set",self.service,h,"purge",self.node) 8581 "set",
6887 ,e); 8582 self.service,
6888 end 8583 nil,
6889 function e:delete(a,e) 8584 "retract",
6890 assert(not a,"Not implemented yet."); 8585 self.node,
6891 self.stream:send_iq(t("set",self.service,h,"delete",self.node) 8586 nil,
6892 ,e); 8587 o,
8588 {notify=e and"1"or nil}
8589 ),
8590 a
8591 );
8592 end
8593 function e:purge(e,a)
8594 self.stream:send_iq(
8595 t(
8596 "set",
8597 self.service,
8598 s,
8599 "purge",
8600 self.node,
8601 nil,
8602 nil,
8603 {notify=e and"1"or nil}
8604 ),
8605 a
8606 );
8607 end
8608 function e:delete(e,a)
8609 assert(not e,"Not implemented yet.");
8610 self.stream:send_iq(t("set",self.service,s,"delete",self.node)
8611 ,a);
6893 end 8612 end
6894 end) 8613 end)
6895 package.preload['verse.plugins.pep']=(function(...) 8614 package.preload['verse.plugins.pep']=(function(...)
6896 local _ENV=_ENV; 8615 local _ENV=_ENV;
6897 local function e(t,...) 8616 local function e(t,...)
6902 end 8621 end
6903 _ENV=e; 8622 _ENV=e;
6904 _M=e; 8623 _M=e;
6905 return e; 8624 return e;
6906 end 8625 end
6907 local e=require"verse"; 8626 local t=require"verse";
6908 local t="http://jabber.org/protocol/pubsub"; 8627 local e="http://jabber.org/protocol/pubsub";
6909 local t=t.."#event"; 8628 local e=e.."#event";
6910 function e.plugins.pep(e) 8629 function t.plugins.pep(e)
6911 e:add_plugin("disco"); 8630 e:add_plugin("disco");
6912 e:add_plugin("pubsub"); 8631 e:add_plugin("pubsub");
6913 e.pep={}; 8632 e.pep={};
6914 e:hook("pubsub/event",function(t) 8633 e:hook("pubsub/event",function(t)
6915 return e:event("pep/"..t.node,{from=t.from,item=t.item.tags[1]}); 8634 return e:event("pep/"..t.node,{from=t.from,id=t.item.attr.id,item=t.item.tags[1]});
6916 end); 8635 end);
6917 function e:hook_pep(t,i,o) 8636 function e:hook_pep(t,i,o)
6918 local a=e.events._handlers["pep/"..t]; 8637 local a=e.events._handlers["pep/"..t];
6919 if not(a)or#a==0 then 8638 if not(a)or#a==0 then
6920 e:add_disco_feature(t.."+notify"); 8639 e:add_disco_feature(t.."+notify");
6946 return e; 8665 return e;
6947 end 8666 end
6948 local o=require"verse"; 8667 local o=require"verse";
6949 local n=require"lib.adhoc"; 8668 local n=require"lib.adhoc";
6950 local t="http://jabber.org/protocol/commands"; 8669 local t="http://jabber.org/protocol/commands";
6951 local s="jabber:x:data"; 8670 local r="jabber:x:data";
6952 local a={}; 8671 local a={};
6953 a.__index=a; 8672 a.__index=a;
6954 local i={}; 8673 local i={};
6955 function o.plugins.adhoc(e) 8674 function o.plugins.adhoc(e)
6956 e:add_plugin("disco"); 8675 e:add_plugin("disco");
6964 end 8683 end
6965 e:debug("adhoc calling callback") 8684 e:debug("adhoc calling callback")
6966 return o(t); 8685 return o(t);
6967 end); 8686 end);
6968 end 8687 end
6969 function e:execute_command(t,i,o) 8688 function e:execute_command(o,i,t)
6970 local e=setmetatable({ 8689 local e=setmetatable({
6971 stream=e,jid=t, 8690 stream=e,jid=o,
6972 command=i,callback=o 8691 command=i,callback=t
6973 },a); 8692 },a);
6974 return e:execute(); 8693 return e:execute();
6975 end 8694 end
6976 local function h(t,e) 8695 local function s(t,e)
6977 if not(e)or e=="user"then return true;end 8696 if not(e)or e=="user"then return true;end
6978 if type(e)=="function"then 8697 if type(e)=="function"then
6979 return e(t); 8698 return e(t);
6980 end 8699 end
6981 end 8700 end
6982 function e:add_adhoc_command(o,a,h,s) 8701 function e:add_adhoc_command(o,a,h,s)
6983 i[a]=n.new(o,a,h,s); 8702 i[a]=n.new(o,a,h,s);
6984 e:add_disco_item({jid=e.jid,node=a,name=o},t); 8703 e:add_disco_item({jid=e.jid,node=a,name=o},t);
6985 return i[a]; 8704 return i[a];
6986 end 8705 end
6987 local function s(a) 8706 local function h(t)
6988 local t=a.tags[1]; 8707 local a=t.tags[1];
6989 local t=t.attr.node; 8708 local a=a.attr.node;
6990 local t=i[t]; 8709 local a=i[a];
6991 if not t then return;end 8710 if not a then return;end
6992 if not h(a.attr.from,t.permission)then 8711 if not s(t.attr.from,a.permission)then
6993 e:send(o.error_reply(a,"auth","forbidden","You don't have permission to execute this command"):up() 8712 e:send(o.error_reply(t,"auth","forbidden","You don't have permission to execute this command"):up()
6994 :add_child(t:cmdtag("canceled") 8713 :add_child(a:cmdtag("canceled")
6995 :tag("note",{type="error"}):text("You don't have permission to execute this command"))); 8714 :tag("note",{type="error"}):text("You don't have permission to execute this command")));
6996 return true 8715 return true
6997 end 8716 end
6998 return n.handle_cmd(t,{send=function(t)return e:send(t)end},a); 8717 return n.handle_cmd(a,{send=function(t)return e:send(t)end},t);
6999 end 8718 end
7000 e:hook("iq/"..t,function(e) 8719 e:hook("iq/"..t,function(e)
7001 local a=e.attr.type; 8720 local t=e.attr.type;
7002 local t=e.tags[1].name; 8721 local a=e.tags[1].name;
7003 if a=="set"and t=="command"then 8722 if t=="set"and a=="command"then
7004 return s(e); 8723 return h(e);
7005 end 8724 end
7006 end); 8725 end);
7007 end 8726 end
7008 function a:_process_response(e) 8727 function a:_process_response(e)
7009 if e.attr.type=="error"then 8728 if e.attr.type=="error"then
7012 return; 8731 return;
7013 end 8732 end
7014 local e=e:get_child("command",t); 8733 local e=e:get_child("command",t);
7015 self.status=e.attr.status; 8734 self.status=e.attr.status;
7016 self.sessionid=e.attr.sessionid; 8735 self.sessionid=e.attr.sessionid;
7017 self.form=e:get_child("x",s); 8736 self.form=e:get_child("x",r);
7018 self.note=e:get_child("note"); 8737 self.note=e:get_child("note");
7019 self.callback(self); 8738 self.callback(self);
7020 end 8739 end
7021 function a:execute() 8740 function a:execute()
7022 local e=o.iq({to=self.jid,type="set"}) 8741 local e=o.iq({to=self.jid,type="set"})
7049 _ENV=e; 8768 _ENV=e;
7050 _M=e; 8769 _M=e;
7051 return e; 8770 return e;
7052 end 8771 end
7053 local a=require"verse"; 8772 local a=require"verse";
8773 local o=require"util.stanza";
7054 function a.plugins.presence(t) 8774 function a.plugins.presence(t)
7055 t.last_presence=nil; 8775 t.last_presence=nil;
7056 t:hook("presence-out",function(e) 8776 t:hook("presence-out",function(e)
7057 if not e.attr.to then 8777 if not e.attr.to then
7058 t.last_presence=e; 8778 t.last_presence=o.clone(e);
7059 end 8779 end
7060 end,1); 8780 end,1);
7061 function t:resend_presence() 8781 function t:resend_presence()
7062 if self.last_presence then 8782 if self.last_presence then
7063 t:send(self.last_presence); 8783 t:send(self.last_presence);
7092 end 8812 end
7093 _ENV=e; 8813 _ENV=e;
7094 _M=e; 8814 _M=e;
7095 return e; 8815 return e;
7096 end 8816 end
7097 local a=require"verse"; 8817 local t=require"verse";
7098 local t="jabber:iq:private"; 8818 local a="jabber:iq:private";
7099 function a.plugins.private(o) 8819 function t.plugins.private(o)
7100 function o:private_set(i,o,e,n) 8820 function o:private_set(i,o,e,n)
7101 local t=a.iq({type="set"}) 8821 local t=t.iq({type="set"})
7102 :tag("query",{xmlns=t}); 8822 :tag("query",{xmlns=a});
7103 if e then 8823 if e then
7104 if e.name==i and e.attr and e.attr.xmlns==o then 8824 if e.name==i and e.attr and e.attr.xmlns==o then
7105 t:add_child(e); 8825 t:add_child(e);
7106 else 8826 else
7107 t:tag(i,{xmlns=o}) 8827 t:tag(i,{xmlns=o})
7108 :add_child(e); 8828 :add_child(e);
7109 end 8829 end
7110 end 8830 end
7111 self:send_iq(t,n); 8831 self:send_iq(t,n);
7112 end 8832 end
7113 function o:private_get(o,e,i) 8833 function o:private_get(e,o,i)
7114 self:send_iq(a.iq({type="get"}) 8834 self:send_iq(t.iq({type="get"})
7115 :tag("query",{xmlns=t}) 8835 :tag("query",{xmlns=a})
7116 :tag(o,{xmlns=e}), 8836 :tag(e,{xmlns=o}),
7117 function(a) 8837 function(t)
7118 if a.attr.type=="result"then 8838 if t.attr.type=="result"then
7119 local t=a:get_child("query",t); 8839 local t=t:get_child("query",a);
7120 local e=t:get_child(o,e); 8840 local e=t:get_child(e,o);
7121 i(e); 8841 i(e);
7122 end 8842 end
7123 end); 8843 end);
7124 end 8844 end
7125 end 8845 end
7151 t:hook("stream-features",function(e) 8871 t:hook("stream-features",function(e)
7152 if e:get_child("ver",o)then 8872 if e:get_child("ver",o)then
7153 h=true; 8873 h=true;
7154 end 8874 end
7155 end); 8875 end);
7156 local function s(t) 8876 local function s(e)
7157 local e=i.stanza("item",{xmlns=a}); 8877 local t=i.stanza("item",{xmlns=a});
7158 for a,t in pairs(t)do 8878 for a,e in pairs(e)do
7159 if a~="groups"then 8879 if a~="groups"then
7160 e.attr[a]=t; 8880 t.attr[a]=e;
7161 else 8881 else
7162 for a=1,#t do 8882 for a=1,#e do
7163 e:tag("group"):text(t[a]):up(); 8883 t:tag("group"):text(e[a]):up();
7164 end 8884 end
7165 end 8885 end
7166 end 8886 end
7167 return e; 8887 return t;
7168 end 8888 end
7169 local function d(a) 8889 local function d(a)
7170 local e={}; 8890 local e={};
7171 local t={}; 8891 local t={};
7172 e.groups=t; 8892 e.groups=t;
7187 return{ 8907 return{
7188 ver=e.ver, 8908 ver=e.ver,
7189 items=e.items, 8909 items=e.items,
7190 }; 8910 };
7191 end 8911 end
7192 function e:add_contact(n,o,h,e) 8912 function e:add_contact(h,o,n,e)
7193 local o={jid=n,name=o,groups=h}; 8913 local o={jid=h,name=o,groups=n};
7194 local a=i.iq({type="set"}) 8914 local a=i.iq({type="set"})
7195 :tag("query",{xmlns=a}) 8915 :tag("query",{xmlns=a})
7196 :add_child(s(o)); 8916 :add_child(s(o));
7197 t:send_iq(a,function(t) 8917 t:send_iq(a,function(t)
7198 if not e then return end 8918 if not e then return end
7247 end); 8967 end);
7248 end 8968 end
7249 t:hook("iq/"..a,function(o) 8969 t:hook("iq/"..a,function(o)
7250 local s,n=o.attr.type,o.attr.from; 8970 local s,n=o.attr.type,o.attr.from;
7251 if s=="set"and(not n or n==l(t.jid))then 8971 if s=="set"and(not n or n==l(t.jid))then
7252 local n=o:get_child("query",a); 8972 local s=o:get_child("query",a);
7253 local a=n and n:get_child("item"); 8973 local n=s and s:get_child("item");
8974 if n then
8975 local o,a;
8976 local i=n.attr.jid;
8977 if n.attr.subscription=="remove"then
8978 o="removed"
8979 a=d(i);
8980 else
8981 o=e.items[i]and"changed"or"added";
8982 r(n)
8983 a=e.items[i];
8984 end
8985 e.ver=s.attr.ver;
7254 if a then 8986 if a then
7255 local i,o; 8987 t:event("roster/item-"..o,a);
7256 local s=a.attr.jid;
7257 if a.attr.subscription=="remove"then
7258 i="removed"
7259 o=d(s);
7260 else
7261 i=e.items[s]and"changed"or"added";
7262 r(a)
7263 o=e.items[s];
7264 end
7265 e.ver=n.attr.ver;
7266 if o then
7267 t:event("roster/item-"..i,o);
7268 end 8988 end
7269 end 8989 end
7270 t:send(i.reply(o)) 8990 t:send(i.reply(o))
7271 return true; 8991 return true;
7272 end 8992 end
7361 }; 9081 };
7362 local t=t:event(e.name,a); 9082 local t=t:event(e.name,a);
7363 return t or(e.name=="message")or nil; 9083 return t or(e.name=="message")or nil;
7364 end 9084 end
7365 end,500); 9085 end,500);
7366 function o:join_room(n,h,t) 9086 function o:join_room(n,h,t,r)
7367 if not h then 9087 if not h then
7368 return false,"no nickname supplied" 9088 return false,"no nickname supplied"
7369 end 9089 end
7370 t=t or{}; 9090 t=t or{};
7371 local e=setmetatable(i.eventable{ 9091 local e=setmetatable(i.eventable{
7426 e.subject=t; 9146 e.subject=t;
7427 return e:event("subject-changed",{from=o,to=t,by=a.sender,event=a}); 9147 return e:event("subject-changed",{from=o,to=t,by=a.sender,event=a});
7428 end 9148 end
7429 end,2e3); 9149 end,2e3);
7430 local t=i.presence():tag("x",{xmlns=s}):reset(); 9150 local t=i.presence():tag("x",{xmlns=s}):reset();
9151 if r then
9152 t:get_child("x",s):tag("password"):text(r):reset();
9153 end
7431 self:event("pre-groupchat/joining",t); 9154 self:event("pre-groupchat/joining",t);
7432 e:send(t) 9155 e:send(t)
7433 self:event("groupchat/joining",e); 9156 self:event("groupchat/joining",e);
7434 return e; 9157 return e;
7435 end 9158 end
7469 if t then 9192 if t then
7470 e:tag("status"):text(t); 9193 e:tag("status"):text(t);
7471 end 9194 end
7472 self:send(e); 9195 self:send(e);
7473 end 9196 end
7474 function a:admin_set(a,t,o,e) 9197 function a:admin_set(e,t,a,o)
7475 self:send(i.iq({type="set"}) 9198 self:send(i.iq({type="set"})
7476 :query(s.."#admin") 9199 :query(s.."#admin")
7477 :tag("item",{nick=a,[t]=o}) 9200 :tag("item",{nick=e,[t]=a})
7478 :tag("reason"):text(e or"")); 9201 :tag("reason"):text(o or""));
7479 end 9202 end
7480 function a:set_role(t,e,a) 9203 function a:set_role(a,e,t)
7481 self:admin_set(t,"role",e,a); 9204 self:admin_set(a,"role",e,t);
7482 end 9205 end
7483 function a:set_affiliation(a,e,t) 9206 function a:set_affiliation(t,a,e)
7484 self:admin_set(a,"affiliation",e,t); 9207 self:admin_set(t,"affiliation",a,e);
7485 end 9208 end
7486 function a:kick(e,t) 9209 function a:kick(t,e)
7487 self:set_role(e,"none",t); 9210 self:set_role(t,"none",e);
7488 end 9211 end
7489 function a:ban(t,e) 9212 function a:ban(e,t)
7490 self:set_affiliation(t,"outcast",e); 9213 self:set_affiliation(e,"outcast",t);
7491 end 9214 end
7492 end) 9215 end)
7493 package.preload['verse.plugins.vcard']=(function(...) 9216 package.preload['verse.plugins.vcard']=(function(...)
7494 local _ENV=_ENV; 9217 local _ENV=_ENV;
7495 local function e(t,...) 9218 local function e(t,...)
7547 _M=e; 9270 _M=e;
7548 return e; 9271 return e;
7549 end 9272 end
7550 local n=require"verse"; 9273 local n=require"verse";
7551 local i="vcard-temp:x:update"; 9274 local i="vcard-temp:x:update";
7552 local s=require("util.hashes").sha1; 9275 local r=require("util.hashes").sha1;
7553 local e,t=pcall(function() 9276 local e,t=pcall(function()
7554 local e=require("util.encodings").base64.decode; 9277 local e=require("util.encodings").base64.decode;
7555 assert(e("SGVsbG8=")=="Hello") 9278 assert(e("SGVsbG8=")=="Hello")
7556 return e; 9279 return e;
7557 end); 9280 end);
7564 local h=t; 9287 local h=t;
7565 function n.plugins.vcard_update(e) 9288 function n.plugins.vcard_update(e)
7566 e:add_plugin("vcard"); 9289 e:add_plugin("vcard");
7567 e:add_plugin("presence"); 9290 e:add_plugin("presence");
7568 local t; 9291 local t;
7569 local function r(a) 9292 local function s(a)
7570 local o; 9293 local o;
7571 for e=1,#a do 9294 for e=1,#a do
7572 if a[e].name=="PHOTO"then 9295 if a[e].name=="PHOTO"then
7573 o=a[e][1]; 9296 o=a[e][1];
7574 break 9297 break
7575 end 9298 end
7576 end 9299 end
7577 if o then 9300 if o then
7578 local a=s(h(o),true); 9301 local a=r(h(o),true);
7579 t=n.stanza("x",{xmlns=i}) 9302 t=n.stanza("x",{xmlns=i})
7580 :tag("photo"):text(a); 9303 :tag("photo"):text(a);
7581 e:resend_presence() 9304 e:resend_presence()
7582 else 9305 else
7583 t=nil; 9306 t=nil;
7587 e:hook("ready",function() 9310 e:hook("ready",function()
7588 if a then return;end 9311 if a then return;end
7589 a=true; 9312 a=true;
7590 e:get_vcard(nil,function(t) 9313 e:get_vcard(nil,function(t)
7591 if t then 9314 if t then
7592 r(t) 9315 s(t)
7593 end 9316 end
7594 e:event("ready"); 9317 e:event("ready");
7595 end); 9318 end);
7596 return true; 9319 return true;
7597 end,3); 9320 end,3);
7612 end 9335 end
7613 _ENV=e; 9336 _ENV=e;
7614 _M=e; 9337 _M=e;
7615 return e; 9338 return e;
7616 end 9339 end
7617 local a=require"verse"; 9340 local o=require"verse";
7618 local o="urn:xmpp:carbons:2"; 9341 local a="urn:xmpp:carbons:2";
7619 local n="urn:xmpp:forward:0"; 9342 local n="urn:xmpp:forward:0";
7620 local s=os.time; 9343 local h=os.time;
7621 local r=require"util.datetime".parse; 9344 local s=require"util.datetime".parse;
7622 local h=require"util.jid".bare; 9345 local r=require"util.jid".bare;
7623 function a.plugins.carbons(e) 9346 function o.plugins.carbons(e)
7624 local t={}; 9347 local t={};
7625 t.enabled=false; 9348 t.enabled=false;
7626 e.carbons=t; 9349 e.carbons=t;
7627 function t:enable(i) 9350 function t:enable(i)
7628 e:send_iq(a.iq{type="set"} 9351 e:send_iq(o.iq{type="set"}
7629 :tag("enable",{xmlns=o}) 9352 :tag("enable",{xmlns=a})
7630 ,function(e) 9353 ,function(e)
7631 local e=e.attr.type=="result"; 9354 local e=e.attr.type=="result";
7632 if e then 9355 if e then
7633 t.enabled=true; 9356 t.enabled=true;
7634 end 9357 end
7636 i(e); 9359 i(e);
7637 end 9360 end
7638 end or nil); 9361 end or nil);
7639 end 9362 end
7640 function t:disable(i) 9363 function t:disable(i)
7641 e:send_iq(a.iq{type="set"} 9364 e:send_iq(o.iq{type="set"}
7642 :tag("disable",{xmlns=o}) 9365 :tag("disable",{xmlns=a})
7643 ,function(e) 9366 ,function(e)
7644 local e=e.attr.type=="result"; 9367 local e=e.attr.type=="result";
7645 if e then 9368 if e then
7646 t.enabled=false; 9369 t.enabled=false;
7647 end 9370 end
7650 end 9373 end
7651 end or nil); 9374 end or nil);
7652 end 9375 end
7653 local i; 9376 local i;
7654 e:hook("bind-success",function() 9377 e:hook("bind-success",function()
7655 i=h(e.jid); 9378 i=r(e.jid);
7656 end); 9379 end);
7657 e:hook("message",function(a) 9380 e:hook("message",function(o)
7658 local t=a:get_child(nil,o); 9381 local t=o:get_child(nil,a);
7659 if a.attr.from==i and t then 9382 if o.attr.from==i and t then
7660 local o=t.name; 9383 local o=t.name;
7661 local t=t:get_child("forwarded",n); 9384 local t=t:get_child("forwarded",n);
7662 local a=t and t:get_child("message","jabber:client"); 9385 local a=t and t:get_child("message","jabber:client");
7663 local t=t:get_child("delay","urn:xmpp:delay"); 9386 local t=t:get_child("delay","urn:xmpp:delay");
7664 local t=t and t.attr.stamp; 9387 local t=t and t.attr.stamp;
7665 t=t and r(t); 9388 t=t and s(t);
7666 if a then 9389 if a then
7667 return e:event("carbon",{ 9390 return e:event("carbon",{
7668 dir=o, 9391 dir=o,
7669 stanza=a, 9392 stanza=a,
7670 timestamp=t or s(), 9393 timestamp=t or h(),
7671 }); 9394 });
7672 end 9395 end
7673 end 9396 end
7674 end,1); 9397 end,1);
7675 end 9398 end
7687 return e; 9410 return e;
7688 end 9411 end
7689 local a=require"verse"; 9412 local a=require"verse";
7690 local t=require"util.stanza"; 9413 local t=require"util.stanza";
7691 local e="urn:xmpp:mam:2" 9414 local e="urn:xmpp:mam:2"
7692 local n="urn:xmpp:forward:0"; 9415 local c="urn:xmpp:forward:0";
7693 local c="urn:xmpp:delay"; 9416 local u="urn:xmpp:delay";
7694 local d=require"util.uuid".generate; 9417 local i=require"util.id".short;
7695 local m=require"util.datetime".parse; 9418 local m=require"util.datetime".parse;
7696 local s=require"util.datetime".datetime; 9419 local r=require"util.datetime".datetime;
7697 local o=require"util.dataforms".new; 9420 local o=require"util.dataforms".new;
7698 local h=require"util.rsm"; 9421 local h=require"util.rsm";
7699 local l={}; 9422 local d={};
7700 local u=o{ 9423 local l=o{
7701 {name="FORM_TYPE";type="hidden";value=e;}; 9424 {name="FORM_TYPE";type="hidden";value=e;};
7702 {name="with";type="jid-single";}; 9425 {name="with";type="jid-single";};
7703 {name="start";type="text-single"}; 9426 {name="start";type="text-single"};
7704 {name="end";type="text-single";}; 9427 {name="end";type="text-single";};
7705 }; 9428 };
7706 function a.plugins.archive(i) 9429 function a.plugins.archive(o)
7707 function i:query_archive(o,a,r) 9430 function o:query_archive(o,a,n)
7708 local d=d(); 9431 local i=i();
7709 local o=t.iq{type="set",to=o} 9432 local o=t.iq{id=i,type="set",to=o}
7710 :tag("query",{xmlns=e,queryid=d}); 9433 :tag("query",{xmlns=e,queryid=i});
7711 local i,t=tonumber(a["start"]),tonumber(a["end"]); 9434 local t,s=tonumber(a["start"]),tonumber(a["end"]);
7712 a["start"]=i and s(i); 9435 a["start"]=t and r(t);
7713 a["end"]=t and s(t); 9436 a["end"]=s and r(s);
7714 o:add_child(u:form(a,"submit")); 9437 o:add_child(l:form(a,"submit"));
7715 o:add_child(h.generate(a)); 9438 o:add_child(h.generate(a));
7716 local t={}; 9439 local t={};
7717 local function i(o) 9440 local function s(a)
7718 local a=o:get_child("result",e); 9441 local e=a:get_child("result",e);
7719 if a and a.attr.queryid==d then 9442 if e and e.attr.queryid==i then
7720 local e=a:get_child("forwarded",n); 9443 local a=e:get_child("forwarded",c);
7721 e=e or o:get_child("forwarded",n); 9444 local i=e.attr.id;
7722 local o=a.attr.id; 9445 local e=a:get_child("delay",u);
7723 local a=e:get_child("delay",c); 9446 local o=e and m(e.attr.stamp)or nil;
7724 local a=a and m(a.attr.stamp)or nil; 9447 local e=a:get_child("message","jabber:client")
7725 local e=e:get_child("message","jabber:client") 9448 t[#t+1]={id=i,stamp=o,message=e};
7726 t[#t+1]={id=o,stamp=a,message=e};
7727 return true 9449 return true
7728 end 9450 end
7729 end 9451 end
7730 self:hook("message",i,1); 9452 self:hook("message",s,1);
7731 self:send_iq(o,function(a) 9453 self:send_iq(o,function(a)
7732 self:unhook("message",i); 9454 self:unhook("message",s);
7733 if a.attr.type=="error"then 9455 if a.attr.type=="error"then
7734 self:warn(table.concat({a:get_error()}," ")) 9456 self:warn(table.concat({a:get_error()}," "))
7735 r(false,a:get_error()) 9457 n(false,a:get_error())
7736 return true; 9458 return true;
7737 end 9459 end
7738 local e=a:get_child("fin",e) 9460 local e=a:get_child("fin",e)
7739 if e then 9461 if e then
9462 t.complete=e.attr.complete=="true"or e.attr.complete=="1";
7740 local e=h.get(e); 9463 local e=h.get(e);
7741 for a,e in pairs(e or l)do t[a]=e;end 9464 for e,a in pairs(e or d)do t[e]=a;end
7742 end 9465 end
7743 r(t); 9466 n(t);
7744 return true 9467 return true
7745 end); 9468 end);
7746 end 9469 end
7747 local n={ 9470 local i={
7748 always=true,[true]="always", 9471 always=true,[true]="always",
7749 never=false,[false]="never", 9472 never=false,[false]="never",
7750 roster="roster", 9473 roster="roster",
7751 } 9474 }
7752 local function s(t) 9475 local function n(t)
7753 local e={}; 9476 local e={};
7754 local a=t.attr.default; 9477 local a=t.attr.default;
7755 if a then 9478 if a then
7756 e[false]=n[a]; 9479 e[false]=i[a];
7757 end 9480 end
7758 local a=t:get_child("always"); 9481 local a=t:get_child("always");
7759 if a then 9482 if a then
7760 for t in a:childtags("jid")do 9483 for t in a:childtags("jid")do
7761 local t=t:get_text(); 9484 local t=t:get_text();
7769 e[t]=false; 9492 e[t]=false;
7770 end 9493 end
7771 end 9494 end
7772 return e; 9495 return e;
7773 end 9496 end
7774 local function h(o) 9497 local function s(o)
7775 local a 9498 local a
7776 a,o[false]=o[false],nil; 9499 a,o[false]=o[false],nil;
7777 if a~=nil then 9500 if a~=nil then
7778 a=n[a]; 9501 a=i[a];
7779 end 9502 end
7780 local i=t.stanza("prefs",{xmlns=e,default=a}) 9503 local i=t.stanza("prefs",{xmlns=e,default=a})
7781 local a=t.stanza("always"); 9504 local a=t.stanza("always");
7782 local e=t.stanza("never"); 9505 local e=t.stanza("never");
7783 for o,t in pairs(o)do 9506 for t,o in pairs(o)do
7784 (t and a or e):tag("jid"):text(o):up(); 9507 (o and a or e):tag("jid"):text(t):up();
7785 end 9508 end
7786 return i:add_child(a):add_child(e); 9509 return i:add_child(a):add_child(e);
7787 end 9510 end
7788 function i:archive_prefs_get(a) 9511 function o:archive_prefs_get(a)
7789 self:send_iq(t.iq{type="get"}:tag("prefs",{xmlns=e}), 9512 self:send_iq(t.iq{type="get"}:tag("prefs",{xmlns=e}),
7790 function(e) 9513 function(e)
7791 if e and e.attr.type=="result"and e.tags[1]then 9514 if e and e.attr.type=="result"and e.tags[1]then
7792 local t=s(e.tags[1]); 9515 local t=n(e.tags[1]);
7793 a(t,e); 9516 a(t,e);
7794 else 9517 else
7795 a(nil,e); 9518 a(nil,e);
7796 end 9519 end
7797 end); 9520 end);
7798 end 9521 end
7799 function i:archive_prefs_set(e,a) 9522 function o:archive_prefs_set(e,a)
7800 self:send_iq(t.iq{type="set"}:add_child(h(e)),a); 9523 self:send_iq(t.iq{type="set"}:add_child(s(e)),a);
7801 end 9524 end
9525 end
9526 end)
9527 package.preload['verse.plugins.browsing']=(function(...)
9528 local _ENV=_ENV;
9529 local function e(t,...)
9530 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
9531 package.loaded[t]=e;
9532 for t=1,select("#",...)do
9533 (select(t,...))(e);
9534 end
9535 _ENV=e;
9536 _M=e;
9537 return e;
9538 end
9539 local a=require"verse";
9540 local o="urn:xmpp:browsing:0";
9541 function a.plugins.browsing(e)
9542 e:add_plugin("pep");
9543 function e:browsing(t,i)
9544 if type(t)=="string"then
9545 t={uri=t};
9546 end
9547 local a=a.stanza("page",{xmlns=o})
9548 for e,t in pairs(t)do
9549 a:tag(e):text(t):up();
9550 end
9551 return e:publish_pep(a,i);
9552 end
9553 e:hook_pep(o,function(a)
9554 local t=a.item;
9555 return e:event("browsing",{
9556 from=a.from;
9557 description=t:get_child_text"description";
9558 keywords=t:get_child_text"keywords";
9559 title=t:get_child_text"title";
9560 uri=t:get_child_text"uri";
9561 });
9562 end);
7802 end 9563 end
7803 end) 9564 end)
7804 package.preload['util.http']=(function(...) 9565 package.preload['util.http']=(function(...)
7805 local _ENV=_ENV; 9566 local _ENV=_ENV;
7806 local function e(t,...) 9567 local function e(t,...)
7811 end 9572 end
7812 _ENV=e; 9573 _ENV=e;
7813 _M=e; 9574 _M=e;
7814 return e; 9575 return e;
7815 end 9576 end
7816 local t,n=string.format,string.char; 9577 local n,t=string.format,string.char;
7817 local o,s,h=pairs,ipairs,tonumber; 9578 local r,h=pairs,ipairs;
7818 local i,d=table.insert,table.concat; 9579 local i,o=table.insert,table.concat;
7819 local function r(e) 9580 local e={};
7820 return e and(e:gsub("[^a-zA-Z0-9.~_-]",function(e)return t("%%%02x",e:byte());end)); 9581 for a=0,255 do
7821 end 9582 local t=t(a);
7822 local function a(e) 9583 local a=n("%%%02x",a);
7823 return e and(e:gsub("%%(%x%x)",function(e)return n(h(e,16));end)); 9584 e[t]=a;
9585 e[a]=t;
9586 e[a:upper()]=t;
9587 end
9588 local function n(t)
9589 return t and(t:gsub("[^a-zA-Z0-9.~_-]",e));
9590 end
9591 local function a(t)
9592 return t and(t:gsub("%%%x%x",e));
7824 end 9593 end
7825 local function e(e) 9594 local function e(e)
7826 return e and(e:gsub("%W",function(e) 9595 return e and(n(e):gsub("%%20","+"));
7827 if e~=" "then 9596 end
7828 return t("%%%02x",e:byte()); 9597 local function s(a)
7829 else 9598 local t={};
7830 return"+"; 9599 if a[1]then
7831 end 9600 for o,a in h(a)do
7832 end)); 9601 i(t,e(a.name).."="..e(a.value));
7833 end 9602 end
7834 local function n(t) 9603 else
7835 local a={}; 9604 for a,o in r(a)do
7836 if t[1]then 9605 i(t,e(a).."="..e(o));
7837 for o,t in s(t)do 9606 end
7838 i(a,e(t.name).."="..e(t.value)); 9607 end
7839 end 9608 return o(t,"&");
7840 else 9609 end
7841 for o,t in o(t)do 9610 local function h(e)
7842 i(a,e(o).."="..e(t));
7843 end
7844 end
7845 return d(a,"&");
7846 end
7847 local function s(e)
7848 if not e:match("=")then return a(e);end 9611 if not e:match("=")then return a(e);end
7849 local o={}; 9612 local o={};
7850 for e,t in e:gmatch("([^=&]*)=([^&]*)")do 9613 for t,e in e:gmatch("([^=&]*)=([^&]*)")do
7851 e,t=e:gsub("%+","%%20"),t:gsub("%+","%%20"); 9614 t,e=t:gsub("%+","%%20"),e:gsub("%+","%%20");
7852 e,t=a(e),a(t); 9615 t,e=a(t),a(e);
7853 i(o,{name=e,value=t}); 9616 i(o,{name=t,value=e});
7854 o[e]=t; 9617 o[t]=e;
7855 end 9618 end
7856 return o; 9619 return o;
7857 end 9620 end
7858 local function o(e,t) 9621 local function i(e,t)
7859 e=","..e:gsub("[ \t]",""):lower()..","; 9622 e=","..e:gsub("[ \t]",""):lower()..",";
7860 return e:find(","..t:lower()..",",1,true)~=nil; 9623 return e:find(","..t:lower()..",",1,true)~=nil;
7861 end 9624 end
9625 local function o(e,t)
9626 if t then
9627 if e:sub(-1,-1)~="/"then e=e.."/";end
9628 else
9629 if e:sub(-1,-1)=="/"then e=e:sub(1,-2);end
9630 end
9631 if e:sub(1,1)~="/"then e="/"..e;end
9632 return e;
9633 end
7862 return{ 9634 return{
7863 urlencode=r,urldecode=a; 9635 urlencode=n,urldecode=a;
7864 formencode=n,formdecode=s; 9636 formencode=s,formdecode=h;
7865 contains_token=o; 9637 contains_token=i;
9638 normalize_path=o;
7866 }; 9639 };
7867 end) 9640 end)
7868 package.preload['net.http.parser']=(function(...) 9641 package.preload['net.http.parser']=(function(...)
7869 local _ENV=_ENV; 9642 local _ENV=_ENV;
7870 local function e(t,...) 9643 local function e(t,...)
7875 end 9648 end
7876 _ENV=e; 9649 _ENV=e;
7877 _M=e; 9650 _M=e;
7878 return e; 9651 return e;
7879 end 9652 end
7880 local m=tonumber; 9653 local u=tonumber;
7881 local a=assert; 9654 local g=assert;
7882 local v=require"socket.url".parse; 9655 local z=require"socket.url".parse;
7883 local t=require"util.http".urldecode; 9656 local t=require"util.http".urldecode;
7884 local function b(e) 9657 local j=require"util.dbuffer";
9658 local function x(e)
7885 e=t((e:gsub("//+","/"))); 9659 e=t((e:gsub("//+","/")));
7886 if e:sub(1,1)~="/"then 9660 if e:sub(1,1)~="/"then
7887 e="/"..e; 9661 e="/"..e;
7888 end 9662 end
7889 local t=0; 9663 local t=0;
7897 return nil; 9671 return nil;
7898 end 9672 end
7899 end 9673 end
7900 return e; 9674 return e;
7901 end 9675 end
7902 local y={}; 9676 local q={};
7903 function y.new(c,h,e,y) 9677 function q.new(l,s,e,h)
7904 local d=true; 9678 local f=true;
7905 if not e or e=="server"then d=false;else a(e=="client","Invalid parser type");end 9679 if not e or e=="server"then f=false;else g(e=="client","Invalid parser type");end
7906 local e=""; 9680 local v=u(h and h().body_size_limit)or 10*1024*1024;
7907 local p,o,r; 9681 local n=u(h and h().head_size_limit)or 10*1024;
7908 local s=nil; 9682 local k=u(h and h().buffer_size_limit)or v*2;
9683 local a=j.new(k);
9684 local d;
9685 local r=nil;
9686 local e;
7909 local t; 9687 local t;
7910 local a; 9688 local w;
7911 local u; 9689 local o;
7912 local n;
7913 return{ 9690 return{
7914 feed=function(l,i) 9691 feed=function(c,i)
7915 if n then return nil,"parse has failed";end 9692 if o then return nil,"parse has failed";end
7916 if not i then 9693 if not i then
7917 if s and d and not a then 9694 if r and f and not t then
7918 t.body=e; 9695 a:collapse();
7919 c(t); 9696 e.body=a:read_chunk()or"";
7920 elseif e~=""then 9697 e.partial=nil;
7921 n=true;return h(); 9698 l(e);
9699 r=nil;
9700 elseif a:length()~=0 then
9701 o=true;return s("unexpected-eof");
7922 end 9702 end
7923 return; 9703 return;
7924 end 9704 end
7925 e=e..i; 9705 if not a:write(i)then o=true;return s("max-buffer-size-exceeded");end
7926 while#e>0 do 9706 while a:length()>0 do
7927 if s==nil then 9707 if r==nil then
7928 local f=e:find("\r\n\r\n",nil,true); 9708 local b=a:sub(1,n):find("\r\n\r\n",nil,true);
7929 if not f then return;end 9709 if not b then return;end
7930 local w,r,l,i,g; 9710 local p,c,m,i,g;
7931 local c; 9711 local y;
7932 local o={}; 9712 local n={};
7933 for t in e:sub(1,f+1):gmatch("([^\r\n]+)\r\n")do 9713 for t in a:read(b+3):gmatch("([^\r\n]+)\r\n")do
7934 if c then 9714 if y then
7935 local e,t=t:match("^([^%s:]+): *(.*)$"); 9715 local e,t=t:match("^([^%s:]+): *(.*)$");
7936 if not e then n=true;return h("invalid-header-line");end 9716 if not e then o=true;return s("invalid-header-line");end
7937 e=e:lower(); 9717 e=e:lower();
7938 o[e]=o[e]and o[e]..","..t or t; 9718 n[e]=n[e]and n[e]..","..t or t;
7939 else 9719 else
7940 c=t; 9720 y=t;
7941 if d then 9721 if f then
7942 l,i,g=t:match("^HTTP/(1%.[01]) (%d%d%d) (.*)$"); 9722 m,i,g=t:match("^HTTP/(1%.[01]) (%d%d%d) (.*)$");
7943 i=m(i); 9723 i=u(i);
7944 if not i then n=true;return h("invalid-status-line");end 9724 if not i then o=true;return s("invalid-status-line");end
7945 u=not 9725 w=not
7946 ((y and y().method=="HEAD") 9726 ((h and h().method=="HEAD")
7947 or(i==204 or i==304 or i==301) 9727 or(i==204 or i==304 or i==301)
7948 or(i>=100 and i<200)); 9728 or(i>=100 and i<200));
7949 else 9729 else
7950 w,r,l=t:match("^(%w+) (%S+) HTTP/(1%.[01])$"); 9730 p,c,m=t:match("^(%w+) (%S+) HTTP/(1%.[01])$");
7951 if not w then n=true;return h("invalid-status-line");end 9731 if not p then o=true;return s("invalid-status-line");end
7952 end 9732 end
7953 end 9733 end
7954 end 9734 end
7955 if not c then n=true;return h("invalid-status-line");end 9735 if not y then o=true;return s("invalid-status-line");end
7956 p=u and o["transfer-encoding"]=="chunked"; 9736 d=w and n["transfer-encoding"]=="chunked";
7957 a=m(o["content-length"]); 9737 t=u(n["content-length"]);
9738 if f then
9739 if not w then t=0;end
9740 e={
9741 code=i;
9742 httpversion=m;
9743 headers=n;
9744 body=false;
9745 body_length=t;
9746 chunked=d;
9747 partial=true;
9748 responseversion=m;
9749 responseheaders=n;
9750 };
9751 else
9752 local a;
9753 if c:byte()==47 then
9754 local t,e=c:match("([^?]*).?(.*)");
9755 if e==""then e=nil;end
9756 a={path=t,query=e};
9757 else
9758 a=z(c);
9759 if not(a and a.path)then o=true;return s("invalid-url");end
9760 end
9761 c=x(a.path);
9762 n.host=a.host or n.host;
9763 t=t or 0;
9764 e={
9765 method=p;
9766 url=a;
9767 path=c;
9768 httpversion=m;
9769 headers=n;
9770 body=false;
9771 body_sink=nil;
9772 chunked=d;
9773 partial=true;
9774 };
9775 end
9776 if not t or t>v then
9777 l(e);
9778 if not e.body_sink and(t and t>v)then
9779 o=true;
9780 return s("content-length-limit-exceeded");
9781 end
9782 end
9783 if d and not e.body_sink then
9784 l(e);
9785 if not e.body_sink then
9786 e.body_buffer=j.new(k);
9787 end
9788 end
9789 r=true;
9790 end
9791 if r then
7958 if d then 9792 if d then
7959 if not u then a=0;end 9793 local n=a:sub(1,512);
7960 t={ 9794 local t,i=n:match("^(%x+)[^\r\n]*\r\n()");
7961 code=i; 9795 if not t then return;end
7962 httpversion=l; 9796 t=t and u(t,16);
7963 headers=o; 9797 if not t then o=true;return s("invalid-chunk-size");end
7964 body=u and""or nil; 9798 if t==0 and n:find("\r\n\r\n",i-2,true)then
7965 responseversion=l; 9799 local t=e.body_buffer;
7966 responseheaders=o; 9800 if t then
7967 }; 9801 e.body_buffer=nil;
7968 else 9802 t:collapse();
7969 local e; 9803 e.body=t:read_chunk()or"";
7970 if r:byte()==47 then 9804 end
7971 local a,t=r:match("([^?]*).?(.*)"); 9805 a:collapse();
7972 if t==""then t=nil;end 9806 local t=a:read_chunk();
7973 e={path=a,query=t}; 9807 t=t:gsub("^.-\r\n\r\n","");
7974 else 9808 a:write(t);
7975 e=v(r); 9809 r,d=nil,nil;
7976 if not(e and e.path)then n=true;return h("invalid-url");end 9810 e.partial=nil;
7977 end 9811 l(e);
7978 r=b(e.path); 9812 elseif a:length()-i-1>=t then
7979 o.host=e.host or o.host; 9813 a:discard(i-1);
7980 a=a or 0; 9814 (e.body_sink or e.body_buffer):write(a:read(t));
7981 t={ 9815 a:discard(2);
7982 method=w;
7983 url=e;
7984 path=r;
7985 httpversion=l;
7986 headers=o;
7987 body=nil;
7988 };
7989 end
7990 e=e:sub(f+4);
7991 s=true;
7992 end
7993 if s then
7994 if d then
7995 if p then
7996 if not e:find("\r\n",nil,true)then
7997 return;
7998 end
7999 if not o then
8000 o,r=e:match("^(%x+)[^\r\n]*\r\n()");
8001 o=o and m(o,16);
8002 if not o then n=true;return h("invalid-chunk-size");end
8003 end
8004 if o==0 and e:find("\r\n\r\n",r-2,true)then
8005 s,o=nil,nil;
8006 e=e:gsub("^.-\r\n\r\n","");
8007 c(t);
8008 elseif#e-r-2>=o then
8009 t.body=t.body..e:sub(r,r+(o-1));
8010 e=e:sub(r+o+2);
8011 o,r=nil,nil;
8012 else 9816 else
8013 break; 9817 break;
8014 end 9818 end
8015 elseif a and#e>=a then 9819 elseif e.body_sink then
8016 if t.code==101 then 9820 local i=a:read_chunk(t);
8017 t.body,e=e,""; 9821 while i and(not t or t>0)do
8018 else 9822 if e.body_sink:write(i)then
8019 t.body,e=e:sub(1,a),e:sub(a+1); 9823 if t then
8020 end 9824 t=t-#i;
8021 s=nil;c(t); 9825 end
9826 i=a:read_chunk(t);
9827 else
9828 o=true;
9829 return s("body-sink-write-failure");
9830 end
9831 end
9832 if t==0 then
9833 r=nil;
9834 e.partial=nil;
9835 l(e);
9836 end
9837 elseif not t or a:length()>=t then
9838 g(not d)
9839 e.body=t and a:read(t)or a:read_chunk()or"";
9840 r=nil;
9841 e.partial=nil;
9842 l(e);
8022 else 9843 else
8023 break; 9844 break;
8024 end 9845 end
8025 elseif#e>=a then
8026 t.body,e=e:sub(1,a),e:sub(a+1);
8027 s=nil;c(t);
8028 else 9846 else
8029 break; 9847 break;
8030 end
8031 end 9848 end
8032 end 9849 end
8033 end; 9850 end;
8034 }; 9851 };
8035 end 9852 end
8036 return y; 9853 return q;
8037 end) 9854 end)
8038 package.preload['net.http']=(function(...) 9855 package.preload['net.http']=(function(...)
8039 local _ENV=_ENV; 9856 local _ENV=_ENV;
8040 local function a(t,...) 9857 local function e(t,...)
8041 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 9858 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
8042 package.loaded[t]=e; 9859 package.loaded[t]=e;
8043 for t=1,select("#",...)do 9860 for t=1,select("#",...)do
8044 (select(t,...))(e); 9861 (select(t,...))(e);
8045 end 9862 end
8046 _ENV=e; 9863 _ENV=e;
8047 _M=e; 9864 _M=e;
8048 return e; 9865 return e;
8049 end 9866 end
8050 local g=require"socket" 9867 local O=require"util.encodings".base64.encode;
8051 local b=require"util.encodings".base64.encode; 9868 local A=require"socket.url"
8052 local l=require"socket.url" 9869 local E=require"net.http.parser".new;
8053 local u=require"net.http.parser".new; 9870 local d=require"util.http";
8054 local s=require"util.http"; 9871 local T=require"util.events";
8055 local k=pcall(require,"ssl"); 9872 local _=require"util.x509".verify_identity;
8056 local q=require"net.server" 9873 local k=require"util.promise";
8057 local d,o=table.insert,table.concat; 9874 local g=require"net.http.errors";
8058 local m=pairs; 9875 local b=require"net.resolvers.basic";
8059 local y,h,w,f,r= 9876 local v=require"net.connect".connect;
8060 tonumber,tostring,xpcall,select,debug.traceback; 9877 local y=pcall(require,"ssl");
8061 local p,v=assert,error 9878 local s,f=table.insert,table.concat;
8062 local c=require"util.logger".init("http"); 9879 local l=pairs;
8063 a"http" 9880 local p,c,t=
8064 local i={}; 9881 tonumber,tostring,debug.traceback;
8065 local n={default_port=80,default_mode="*a"}; 9882 local q=os.time;
8066 function n.onconnect(t) 9883 local z=require"util.xpcall".xpcall;
8067 local e=i[t]; 9884 local w=error
8068 local a={e.method or"GET"," ",e.path," HTTP/1.1\r\n"}; 9885 local o=require"util.logger".init("http");
8069 if e.query then 9886 local _ENV=nil;
8070 d(a,4,"?"..e.query); 9887 local a={};
8071 end 9888 local function x(e)return(c(e):match("%x+$"));end
8072 t:write(o(a)); 9889 local i={default_port=80,default_mode="*a"};
8073 local a={[2]=": ",[4]="\r\n"}; 9890 local function j(e)o("error","Traceback[http]: %s",t(c(e),2));return e;end
8074 for e,i in m(e.headers)do 9891 local function h(e,t,...)
8075 a[1],a[3]=e,i; 9892 if not t then
8076 t:write(o(a)); 9893 o("error","Request '%s': error in callback: %s",e.id,(...));
8077 end 9894 if not e.suppress_errors then
8078 t:write("\r\n"); 9895 w(...);
8079 if e.body then 9896 end
8080 t:write(e.body); 9897 end
8081 end 9898 return...;
8082 end 9899 end
8083 function n.onincoming(a,t) 9900 local function r(e)
8084 local e=i[a]; 9901 local t=e.conn;
8085 if not e then 9902 if t then
8086 c("warn","Received response from connection %s with no request attached!",h(a)); 9903 e.conn=nil;
8087 return; 9904 t:close()
8088 end 9905 end
8089 if t and e.reader then 9906 end
8090 e:reader(t); 9907 local function m(e,t)
8091 end 9908 if e.callback then
8092 end 9909 e.callback(t or"cancelled",0,e);
8093 function n.ondisconnect(t,a) 9910 e.callback=nil;
8094 local e=i[t]; 9911 end
8095 if e and e.conn then 9912 if e.conn then
8096 e:reader(nil,a); 9913 r(e);
8097 end 9914 end
8098 i[t]=nil; 9915 end
8099 end 9916 local function u(e,n,t)
8100 function n.ondetach(e)
8101 i[e]=nil;
8102 end
8103 local function x(e,a,i)
8104 if not e.parser then 9917 if not e.parser then
8105 local function o(t) 9918 local function i(t)
8106 if e.callback then 9919 if e.callback then
8107 e.callback(t or"connection-closed",0,e); 9920 e.callback(t or"connection-closed",0,e);
8108 e.callback=nil; 9921 e.callback=nil;
8109 end 9922 end
8110 destroy_request(e); 9923 r(e);
8111 end 9924 end
8112 if not a then 9925 if not n then
8113 o(i); 9926 i(t);
8114 return; 9927 return;
8115 end 9928 end
8116 local function a(t) 9929 local a;
9930 local function n(t)
9931 if t.partial then
9932 o("debug","Request '%s': partial response (%s%s)",
9933 e.id,
9934 t.chunked and"chunked, "or"",
9935 t.body_length and("%d bytes"):format(t.body_length)or"unknown length"
9936 );
9937 if e.streaming_handler then
9938 o("debug","Request '%s': Streaming via handler",e.id);
9939 t.body_sink,a=e.streaming_handler(t);
9940 end
9941 return;
9942 elseif a then
9943 o("debug","Request '%s': Finalizing response stream");
9944 a(t);
9945 end
8117 if e.callback then 9946 if e.callback then
8118 e.callback(t.body,t.code,t,e); 9947 e.callback(t.body,t.code,t,e);
8119 e.callback=nil; 9948 e.callback=nil;
8120 end 9949 end
8121 destroy_request(e); 9950 r(e);
8122 end 9951 end
8123 local function t() 9952 local function t()
8124 return e; 9953 return e;
8125 end 9954 end
8126 e.parser=u(a,o,"client",t); 9955 e.parser=E(n,i,"client",t);
8127 end 9956 end
8128 e.parser:feed(a); 9957 e.parser:feed(n);
8129 end 9958 end
8130 local function j(e)c("error","Traceback[http]: %s",r(h(e),2));end 9959 function i.onconnect(t)
8131 function request(e,t,r) 9960 local e=a[t];
8132 local e=l.parse(e); 9961 e.write=function(...)return e.conn:write(...);end
9962 local r=e.callback;
9963 e.callback=function(i,a,n,s)
9964 do
9965 local t={http=e.http,url=e.url,request=e,response=n,content=i,code=a,callback=e.callback};
9966 e.http.events.fire_event("response",t);
9967 i,a,n=t.content,t.code,t.response;
9968 end
9969 o("debug","Request '%s': Calling callback, status %s",e.id,a or"---");
9970 return h(e.id,z(r,j,i,a,n,s));
9971 end
9972 e.reader=u;
9973 e.state="status";
9974 e.cancel=m;
9975 a[e.conn]=e;
9976 if not e.insecure and t:ssl()then
9977 local a=t:socket();
9978 local o=a.getpeerverification and a:getpeerverification();
9979 if not o then
9980 e.callback("certificate-chain-invalid",0,e);
9981 e.callback=nil;
9982 t:close();
9983 return;
9984 end
9985 local a=a.getpeercertificate and a:getpeercertificate();
9986 if not a or not _(e.host,false,a)then
9987 e.callback("certificate-verify-failed",0,e);
9988 e.callback=nil;
9989 t:close();
9990 return;
9991 end
9992 end
9993 local a={e.method or"GET"," ",e.path," HTTP/1.1\r\n"};
9994 if e.query then
9995 s(a,4,"?"..e.query);
9996 end
9997 for e,t in l(e.headers)do
9998 s(a,e..": "..t.."\r\n");
9999 end
10000 s(a,"\r\n")
10001 t:write(f(a));
10002 if e.body then
10003 t:write(e.body);
10004 end
10005 end
10006 function i.onincoming(t,i)
10007 local e=a[t];
10008 if not e then
10009 o("warn","Received response from connection %s with no request attached!",t);
10010 return;
10011 end
10012 if i and e.reader then
10013 e:reader(i);
10014 end
10015 end
10016 function i.ondisconnect(t,o)
10017 local e=a[t];
10018 if e and e.conn then
10019 e:reader(nil,o or"closed");
10020 end
10021 a[t]=nil;
10022 end
10023 function i.onattach(e,t)
10024 a[e]=t;
10025 t.conn=e;
10026 end
10027 function i.ondetach(e)
10028 a[e]=nil;
10029 end
10030 function i.onfail(e,t)
10031 e.http.events.fire_event("request-connection-error",{http=e.http,request=e,url=e.url,err=t});
10032 e.callback(t or"connection failed",0,e);
10033 end
10034 local function f(a,s,t,n)
10035 local e=A.parse(s);
8133 if not(e and e.host)then 10036 if not(e and e.host)then
8134 r(nil,0,e); 10037 n("invalid-url",0,e);
8135 return nil,"invalid-url"; 10038 return nil,"invalid-url";
8136 end 10039 end
10040 e.url=s;
10041 e.http=a;
10042 e.time=q();
8137 if not e.path then 10043 if not e.path then
8138 e.path="/"; 10044 e.path="/";
8139 end 10045 end
8140 local l,o,s; 10046 e.id=t and t.id or x(e);
8141 local u,a=e.host,e.port; 10047 do
8142 local d=u; 10048 local o={http=a,url=s,request=e,options=t,callback=n};
8143 if(a=="80"and e.scheme=="http") 10049 local a=a.events.fire_event("pre-request",o);
8144 or(a=="443"and e.scheme=="https")then 10050 if a then
8145 a=nil; 10051 return a;
8146 elseif a then 10052 end
8147 d=d..":"..a; 10053 e,s,t,e.callback=o.request,o.url,o.options,o.callback;
8148 end 10054 end
8149 o={ 10055 local d,h,u;
8150 ["Host"]=d; 10056 local m,n=e.host,e.port;
10057 local r=m;
10058 if(n=="80"and e.scheme=="http")
10059 or(n=="443"and e.scheme=="https")then
10060 n=nil;
10061 elseif n then
10062 r=r..":"..n;
10063 end
10064 h={
10065 ["Host"]=r;
8151 ["User-Agent"]="Prosody XMPP Server"; 10066 ["User-Agent"]="Prosody XMPP Server";
8152 }; 10067 };
8153 if e.userinfo then 10068 if e.userinfo then
8154 o["Authorization"]="Basic "..b(e.userinfo); 10069 h["Authorization"]="Basic "..O(e.userinfo);
8155 end 10070 end
8156 if t then 10071 if t then
8157 e.onlystatus=t.onlystatus; 10072 e.onlystatus=t.onlystatus;
8158 s=t.body; 10073 u=t.body;
8159 if s then 10074 if u then
8160 l="POST"; 10075 d="POST";
8161 o["Content-Length"]=h(#s); 10076 h["Content-Length"]=c(#u);
8162 o["Content-Type"]="application/x-www-form-urlencoded"; 10077 h["Content-Type"]="application/x-www-form-urlencoded";
8163 end 10078 end
8164 if t.method then l=t.method;end 10079 if t.method then d=t.method;end
8165 if t.headers then 10080 if t.headers then
8166 for e,t in m(t.headers)do 10081 for t,e in l(t.headers)do
8167 o[e]=t; 10082 h[t]=e;
8168 end 10083 end
8169 end 10084 end
8170 end 10085 e.insecure=t.insecure;
8171 e.method,e.headers,e.body=l,o,s; 10086 e.suppress_errors=t.suppress_errors;
10087 e.streaming_handler=t.streaming_handler;
10088 end
10089 o("debug","Making %s %s request '%s' to %s",e.scheme:upper(),d or"GET",e.id,(t and t.suppress_url and r)or s);
10090 e.method,e.headers,e.body=d,h,u;
8172 local o=e.scheme=="https"; 10091 local o=e.scheme=="https";
8173 if o and not k then 10092 if o and not y then
8174 v("SSL not available, unable to contact https URL"); 10093 w("SSL not available, unable to contact https URL");
8175 end 10094 end
8176 local h=a and y(a)or(o and 443 or 80); 10095 local r=n and p(n)or(o and 443 or 80);
8177 local a=g.tcp(); 10096 local n=a.options and a.options.use_dane;
8178 a:settimeout(10); 10097 local h=false;
8179 local d,s=a:connect(u,h);
8180 if not d and s~="timeout"then
8181 r(nil,0,e);
8182 return nil,s;
8183 end
8184 local s=false;
8185 if o then 10098 if o then
8186 s=t and t.sslctx or{mode="client",protocol="sslv23",options={"no_sslv2","no_sslv3"}}; 10099 h=t and t.sslctx or a.options and a.options.sslctx;
8187 end 10100 if t and t.use_dane~=nil then
8188 e.handler,e.conn=p(q.wrapclient(a,u,h,n,"*a",s)); 10101 n=t.use_dane;
8189 e.write=function(...)return e.handler:write(...);end 10102 end
8190 e.callback=function(i,t,o,a)c("debug","Calling callback, status %s",t or"---");return f(2,w(function()return r(i,t,o,a)end,j));end 10103 end
8191 e.reader=x; 10104 local t=b.new(m,r,"tcp",{servername=e.host;use_dane=n});
8192 e.state="status"; 10105 v(t,i,{sslctx=h},e);
8193 i[e.handler]=e; 10106 a.events.fire_event("request",{http=a,request=e,url=s});
8194 return e; 10107 return e;
8195 end 10108 end
8196 function destroy_request(e) 10109 local function e(t)
8197 if e.conn then 10110 local e={
8198 e.conn=nil; 10111 options=t;
8199 e.handler:close() 10112 request=function(o,a,t,e)
8200 end 10113 if e~=nil then
8201 end 10114 return f(o,a,t,e);
8202 local e,t=s.urlencode,s.urldecode; 10115 else
8203 local o,a=s.formencode,s.formdecode; 10116 return k.new(function(i,n)
8204 _M.urlencode,_M.urldecode=e,t; 10117 f(o,a,t,function(t,a,e,o)
8205 _M.formencode,_M.formdecode=o,a; 10118 if a==0 then
8206 return _M; 10119 n(g.new(t,{request=e}));
10120 else
10121 e.request=o;
10122 i(e);
10123 end
10124 end);
10125 end);
10126 end
10127 end;
10128 new=t and function(o)
10129 local a={};
10130 for e,t in l(t)do a[e]=t;end
10131 if o then
10132 for t,e in l(o)do a[t]=e;end
10133 end
10134 return e(a);
10135 end or e;
10136 events=T.new();
10137 };
10138 return e;
10139 end
10140 local t=e({
10141 sslctx={mode="client",protocol="sslv23",options={"no_sslv2","no_sslv3"},alpn="http/1.1",verify="peer"};
10142 suppress_errors=true;
10143 });
10144 return{
10145 request=function(e,a,o)
10146 return t:request(e,a,o);
10147 end;
10148 default=t;
10149 new=e;
10150 events=t.events;
10151 urlencode=d.urlencode;
10152 urldecode=d.urldecode;
10153 formencode=d.formencode;
10154 formdecode=d.formdecode;
10155 destroy_request=r;
10156 features={
10157 sni=true;
10158 };
10159 };
10160 end)
10161 package.preload['util.x509']=(function(...)
10162 local _ENV=_ENV;
10163 local function e(t,...)
10164 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
10165 package.loaded[t]=e;
10166 for t=1,select("#",...)do
10167 (select(t,...))(e);
10168 end
10169 _ENV=e;
10170 _M=e;
10171 return e;
10172 end
10173 local o=require"util.encodings".stringprep.nameprep;
10174 local r=require"util.encodings".idna.to_ascii;
10175 local y=require"util.encodings".idna.to_unicode;
10176 local c=require"util.encodings".base64;
10177 local e=require"util.logger".init("x509");
10178 local p=require"util.multitable";
10179 local v=string.format;
10180 local d=ipairs;
10181 local _ENV=nil;
10182 local u="2.5.4.3";
10183 local h="2.5.29.17";
10184 local s="1.3.6.1.5.5.7.8.5";
10185 local n="1.3.6.1.5.5.7.8.7";
10186 local function l(o,a)
10187 local t=r(o)
10188 if t==nil then
10189 e("info","Host %s failed IDNA ToASCII operation",o)
10190 return false
10191 end
10192 t=t:lower()
10193 local o=t:gsub("^[^.]+%.","")
10194 for i=1,#a do
10195 local a=a[i]
10196 if t==a:lower()then
10197 e("debug","Cert dNSName %s matched hostname",a);
10198 return true
10199 end
10200 if a:match("^%*%.")then
10201 local t=a:gsub("^[^.]+%.","")
10202 if o==t:lower()then
10203 e("debug","Cert dNSName %s matched hostname",a);
10204 return true
10205 end
10206 end
10207 end
10208 return false
10209 end
10210 local function w(a,t)
10211 local i=o(a)
10212 for a=1,#t do
10213 local t=t[a]
10214 if t:match("[@/]")then
10215 e("debug","Ignoring xmppAddr %s because it's not a bare domain",t)
10216 else
10217 local a=o(t)
10218 if a==nil then
10219 e("info","Ignoring xmppAddr %s, failed nameprep!",t)
10220 else
10221 if i==a then
10222 e("debug","Cert xmppAddr %s matched hostname",t)
10223 return true
10224 end
10225 end
10226 end
10227 end
10228 return false
10229 end
10230 local function f(i,o,t)
10231 local a=r(i)
10232 if a==nil then
10233 e("info","Host %s failed IDNA ToASCII operation",i);
10234 return false
10235 end
10236 if o:match("^_")==nil then o="_"..o end
10237 a=a:lower();
10238 local n=a:gsub("^[^.]+%.","")
10239 for i=1,#t do
10240 local i,t=t[i]:match("^(_[^.]+)%.(.*)");
10241 if o==i then
10242 if a==t:lower()then
10243 e("debug","Cert SRVName %s matched hostname",t);
10244 return true;
10245 end
10246 if t:match("^%*%.")then
10247 local a=t:gsub("^[^.]+%.","")
10248 if n==a:lower()then
10249 e("debug","Cert SRVName %s matched hostname",t)
10250 return true
10251 end
10252 end
10253 if a==t:lower()then
10254 e("debug","Cert SRVName %s matched hostname",t);
10255 return true
10256 end
10257 end
10258 end
10259 return false
10260 end
10261 local function m(o,a,i)
10262 if i.setencode then
10263 i:setencode("utf8");
10264 end
10265 local t=i:extensions()
10266 if t[h]then
10267 local e=t[h];
10268 local t=false
10269 if e[s]then
10270 t=true
10271 if a=="_xmpp-client"or a=="_xmpp-server"then
10272 if w(o,e[s])then return true end
10273 end
10274 end
10275 if e[n]then
10276 t=true
10277 if a and f(o,a,e[n])then return true end
10278 end
10279 if e["dNSName"]then
10280 t=true
10281 if l(o,e["dNSName"])then return true end
10282 end
10283 if e["uniformResourceIdentifier"]then
10284 t=true
10285 end
10286 if t then return false end
10287 end
10288 local a=i:subject()
10289 local t=nil
10290 for o=1,#a do
10291 local a=a[o]
10292 if a["oid"]==u then
10293 if t then
10294 e("info","Certificate has multiple common names")
10295 return false
10296 end
10297 t=a["value"];
10298 end
10299 end
10300 if t then
10301 return l(o,{t})
10302 end
10303 return false
10304 end
10305 local function l(i)
10306 if i.setencode then
10307 i:setencode("utf8");
10308 end
10309 local a=p.new();
10310 local e=i:extensions();
10311 local t=e[h];
10312 if t then
10313 if t["dNSName"]then
10314 for t,e in d(t["dNSName"])do
10315 local t=e:sub(1,2)=="*.";
10316 if t then e=e:sub(3);end
10317 e=y(o(e));
10318 if e then
10319 if t then e="*."..e;end
10320 a:set(e,"*",true);
10321 end
10322 end
10323 end
10324 if t[s]then
10325 for t,e in d(t[s])do
10326 e=o(e);
10327 if e then
10328 a:set(e,"xmpp-client",true);
10329 a:set(e,"xmpp-server",true);
10330 end
10331 end
10332 end
10333 if t[n]then
10334 for t,e in d(t[n])do
10335 local t,e=e:match("^_([^.]+)%.(.*)");
10336 if t then
10337 e=o(e);
10338 if e then
10339 a:set(e,t,true);
10340 end
10341 end
10342 end
10343 end
10344 end
10345 local e=i:subject();
10346 for t=1,#e do
10347 local e=e[t];
10348 if e.oid==u then
10349 local e=o(e.value);
10350 if e and r(e)then
10351 a:set(e,"*",true);
10352 end
10353 end
10354 end
10355 return a.data;
10356 end
10357 local t="%-%-%-%-%-BEGIN ([A-Z ]+)%-%-%-%-%-\r?\n"..
10358 "([0-9A-Za-z+/=\r\n]*)\r?\n%-%-%-%-%-END %1%-%-%-%-%-";
10359 local function a(e)
10360 local e,t=e:match(t);
10361 if e and t then
10362 return c.decode(t),e;
10363 end
10364 end
10365 local n=('.'):rep(64);
10366 local i="-----BEGIN %s-----\n%s\n-----END %s-----\n"
10367 local function o(t,e)
10368 e=e and e:upper()or"CERTIFICATE";
10369 t=c.encode(t);
10370 return v(i,e,t:gsub(n,'%0\n',(#t-1)/64),e);
10371 end
10372 return{
10373 verify_identity=m;
10374 get_identities=l;
10375 pem2der=a;
10376 der2pem=o;
10377 };
8207 end) 10378 end)
8208 package.preload['verse.bosh']=(function(...) 10379 package.preload['verse.bosh']=(function(...)
8209 local _ENV=_ENV; 10380 local _ENV=_ENV;
8210 local function e(t,...) 10381 local function e(t,...)
8211 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 10382 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
8215 end 10386 end
8216 _ENV=e; 10387 _ENV=e;
8217 _M=e; 10388 _M=e;
8218 return e; 10389 return e;
8219 end 10390 end
8220 local h=require"util.xmppstream".new; 10391 local n=require"util.xmppstream".new;
8221 local i=require"util.stanza"; 10392 local o=require"util.stanza";
8222 require"net.httpclient_listener"; 10393 require"net.httpclient_listener";
8223 local o=require"net.http"; 10394 local a=require"net.http";
8224 local e=setmetatable({},{__index=verse.stream_mt}); 10395 local e=setmetatable({},{__index=verse.stream_mt});
8225 e.__index=e; 10396 e.__index=e;
8226 local n="http://etherx.jabber.org/streams"; 10397 local h="http://etherx.jabber.org/streams";
8227 local s="http://jabber.org/protocol/httpbind"; 10398 local s="http://jabber.org/protocol/httpbind";
8228 local a=5; 10399 local t=5;
8229 function verse.new_bosh(a,t) 10400 function verse.new_bosh(a,t)
8230 local t={ 10401 local t={
8231 bosh_conn_pool={}; 10402 bosh_conn_pool={};
8232 bosh_waiting_requests={}; 10403 bosh_waiting_requests={};
8233 bosh_rid=math.random(1,999999); 10404 bosh_rid=math.random(1,999999);
8245 function e:connect() 10416 function e:connect()
8246 self:_send_session_request(); 10417 self:_send_session_request();
8247 end 10418 end
8248 function e:send(e) 10419 function e:send(e)
8249 self:debug("Putting into BOSH send buffer: %s",tostring(e)); 10420 self:debug("Putting into BOSH send buffer: %s",tostring(e));
8250 self.bosh_outgoing_buffer[#self.bosh_outgoing_buffer+1]=i.clone(e); 10421 self.bosh_outgoing_buffer[#self.bosh_outgoing_buffer+1]=o.clone(e);
8251 self:flush(); 10422 self:flush();
8252 end 10423 end
8253 function e:flush() 10424 function e:flush()
8254 if self.connected 10425 if self.connected
8255 and#self.bosh_waiting_requests<self.bosh_max_requests 10426 and#self.bosh_waiting_requests<self.bosh_max_requests
8257 or#self.bosh_outgoing_buffer>0 10428 or#self.bosh_outgoing_buffer>0
8258 or self.bosh_need_restart)then 10429 or self.bosh_need_restart)then
8259 self:debug("Flushing..."); 10430 self:debug("Flushing...");
8260 local e=self:_make_body(); 10431 local e=self:_make_body();
8261 local t=self.bosh_outgoing_buffer; 10432 local t=self.bosh_outgoing_buffer;
8262 for a,o in ipairs(t)do 10433 for o,a in ipairs(t)do
8263 e:add_child(o); 10434 e:add_child(a);
8264 t[a]=nil; 10435 t[o]=nil;
8265 end 10436 end
8266 self:_make_request(e); 10437 self:_make_request(e);
8267 else 10438 else
8268 self:debug("Decided not to flush."); 10439 self:debug("Decided not to flush.");
8269 end 10440 end
8270 end 10441 end
8271 function e:_make_request(i) 10442 function e:_make_request(o)
8272 local e,t=o.request(self.bosh_url,{body=tostring(i)},function(o,e,t) 10443 local e,t=a.request(self.bosh_url,{body=tostring(o)},function(i,e,a)
8273 if e~=0 then 10444 if e~=0 then
8274 self.inactive_since=nil; 10445 self.inactive_since=nil;
8275 return self:_handle_response(o,e,t); 10446 return self:_handle_response(i,e,a);
8276 end 10447 end
8277 local e=os.time(); 10448 local e=os.time();
8278 if not self.inactive_since then 10449 if not self.inactive_since then
8279 self.inactive_since=e; 10450 self.inactive_since=e;
8280 elseif e-self.inactive_since>self.bosh_max_inactivity then 10451 elseif e-self.inactive_since>self.bosh_max_inactivity then
8281 return self:_disconnected(); 10452 return self:_disconnected();
8282 else 10453 else
8283 self:debug("%d seconds left to reconnect, retrying in %d seconds...", 10454 self:debug("%d seconds left to reconnect, retrying in %d seconds...",
8284 self.bosh_max_inactivity-(e-self.inactive_since),a); 10455 self.bosh_max_inactivity-(e-self.inactive_since),t);
8285 end 10456 end
8286 timer.add_task(a,function() 10457 timer.add_task(t,function()
8287 self:debug("Retrying request..."); 10458 self:debug("Retrying request...");
8288 for e,a in ipairs(self.bosh_waiting_requests)do 10459 for t,e in ipairs(self.bosh_waiting_requests)do
8289 if a==t then 10460 if e==a then
8290 table.remove(self.bosh_waiting_requests,e); 10461 table.remove(self.bosh_waiting_requests,t);
8291 break; 10462 break;
8292 end 10463 end
8293 end 10464 end
8294 self:_make_request(i); 10465 self:_make_request(o);
8295 end); 10466 end);
8296 end); 10467 end);
8297 if e then 10468 if e then
8298 table.insert(self.bosh_waiting_requests,e); 10469 table.insert(self.bosh_waiting_requests,e);
8299 else 10470 else
8311 e.attr["xml:lang"]="en"; 10482 e.attr["xml:lang"]="en";
8312 e.attr.ver="1.6"; 10483 e.attr.ver="1.6";
8313 e.attr.from=self.jid; 10484 e.attr.from=self.jid;
8314 e.attr.to=self.host; 10485 e.attr.to=self.host;
8315 e.attr.secure='true'; 10486 e.attr.secure='true';
8316 o.request(self.bosh_url,{body=tostring(e)},function(e,t) 10487 a.request(self.bosh_url,{body=tostring(e)},function(e,t)
8317 if t==0 then 10488 if t==0 then
8318 return self:_disconnected(); 10489 return self:_disconnected();
8319 end 10490 end
8320 local e=self:_parse_response(e) 10491 local e=self:_parse_response(e)
8321 if not e then 10492 if not e then
8331 self.connected=true; 10502 self.connected=true;
8332 self:event("connected"); 10503 self:event("connected");
8333 self:_handle_response_payload(e); 10504 self:_handle_response_payload(e);
8334 end); 10505 end);
8335 end 10506 end
8336 function e:_handle_response(o,t,e) 10507 function e:_handle_response(a,t,e)
8337 if self.bosh_waiting_requests[1]~=e then 10508 if self.bosh_waiting_requests[1]~=e then
8338 self:warn("Server replied to request that wasn't the oldest"); 10509 self:warn("Server replied to request that wasn't the oldest");
8339 for a,t in ipairs(self.bosh_waiting_requests)do 10510 for t,a in ipairs(self.bosh_waiting_requests)do
8340 if t==e then 10511 if a==e then
8341 self.bosh_waiting_requests[a]=nil; 10512 self.bosh_waiting_requests[t]=nil;
8342 break; 10513 break;
8343 end 10514 end
8344 end 10515 end
8345 else 10516 else
8346 table.remove(self.bosh_waiting_requests,1); 10517 table.remove(self.bosh_waiting_requests,1);
8347 end 10518 end
8348 local e=self:_parse_response(o); 10519 local e=self:_parse_response(a);
8349 if e then 10520 if e then
8350 self:_handle_response_payload(e); 10521 self:_handle_response_payload(e);
8351 end 10522 end
8352 self:flush(); 10523 self:flush();
8353 end 10524 end
8354 function e:_handle_response_payload(t) 10525 function e:_handle_response_payload(t)
8355 local e=t.tags; 10526 local e=t.tags;
8356 for t=1,#e do 10527 for t=1,#e do
8357 local e=e[t]; 10528 local e=e[t];
8358 if e.attr.xmlns==n then 10529 if e.attr.xmlns==h then
8359 self:event("stream-"..e.name,e); 10530 self:event("stream-"..e.name,e);
8360 elseif e.attr.xmlns then 10531 elseif e.attr.xmlns then
8361 self:event("stream/"..e.attr.xmlns,e); 10532 self:event("stream/"..e.attr.xmlns,e);
8362 else 10533 else
8363 self:event("stanza",e); 10534 self:event("stanza",e);
8379 self:debug("%s",debug.traceback()); 10550 self:debug("%s",debug.traceback());
8380 self:_disconnected(); 10551 self:_disconnected();
8381 return; 10552 return;
8382 end 10553 end
8383 local t={notopen=true,stream=self}; 10554 local t={notopen=true,stream=self};
8384 local a=h(t,a); 10555 local a=n(t,a);
8385 a:feed(e); 10556 a:feed(e);
8386 return t.payload; 10557 return t.payload;
8387 end 10558 end
8388 function e:_make_body() 10559 function e:_make_body()
8389 self.bosh_rid=self.bosh_rid+1; 10560 self.bosh_rid=self.bosh_rid+1;
8411 _ENV=e; 10582 _ENV=e;
8412 _M=e; 10583 _M=e;
8413 return e; 10584 return e;
8414 end 10585 end
8415 local t=require"verse"; 10586 local t=require"verse";
8416 local o=t.stream_mt; 10587 local i=t.stream_mt;
8417 local d=require"util.jid".split; 10588 local d=require"util.jid".split;
8418 local h=require"net.adns"; 10589 local r=require"net.adns";
8419 local e=require"lxp";
8420 local a=require"util.stanza"; 10590 local a=require"util.stanza";
10591 local o=require"util.id".short;
10592 math.randomseed((require"socket".gettime()*1e6)%2147483648);
8421 t.message,t.presence,t.iq,t.stanza,t.reply,t.error_reply= 10593 t.message,t.presence,t.iq,t.stanza,t.reply,t.error_reply=
8422 a.message,a.presence,a.iq,a.stanza,a.reply,a.error_reply; 10594 a.message,a.presence,a.iq,a.stanza,a.reply,a.error_reply;
8423 local r=require"util.xmppstream".new; 10595 function t.iq(e)
10596 if not e.id then
10597 e.id=o();
10598 end
10599 return a.iq(e);
10600 end
10601 local s=require"util.xmppstream".new;
8424 local n="http://etherx.jabber.org/streams"; 10602 local n="http://etherx.jabber.org/streams";
8425 local function s(t,e) 10603 local function h(e,t)
8426 return t.priority<e.priority or(t.priority==e.priority and t.weight>e.weight); 10604 if e.priority==t.priority then
8427 end 10605 if not e.weight_r then
8428 local i={ 10606 e.weight_r=math.random();
10607 end
10608 if not t.weight_r then
10609 t.weight_r=math.random();
10610 end
10611 return(1+e.weight)*e.weight_r>(1+t.weight)*t.weight_r;
10612 end
10613 return e.priority<t.priority;
10614 end
10615 local o={
8429 stream_ns=n, 10616 stream_ns=n,
8430 stream_tag="stream", 10617 stream_tag="stream",
8431 default_ns="jabber:client"}; 10618 default_ns="jabber:client"};
8432 function i.streamopened(e,t) 10619 function o.streamopened(e,t)
8433 e.stream_id=t.id; 10620 e.stream_id=t.id;
8434 if not e:event("opened",t)then 10621 if not e:event("opened",t)then
8435 e.notopen=nil; 10622 e.notopen=nil;
8436 end 10623 end
8437 return true; 10624 return true;
8438 end 10625 end
8439 function i.streamclosed(e) 10626 function o.streamclosed(e)
8440 e.notopen=true; 10627 e.notopen=true;
8441 if not e.closed then 10628 if not e.closed then
8442 e:send("</stream:stream>"); 10629 e:send("</stream:stream>");
8443 e.closed=true; 10630 e.closed=true;
8444 end 10631 end
8445 e:event("closed"); 10632 e:event("closed");
8446 return e:close("stream closed") 10633 return e:close("stream closed")
8447 end 10634 end
8448 function i.handlestanza(t,e) 10635 function o.handlestanza(t,e)
8449 if e.attr.xmlns==n then 10636 if e.attr.xmlns==n then
8450 return t:event("stream-"..e.name,e); 10637 return t:event("stream-"..e.name,e);
8451 elseif e.attr.xmlns then 10638 elseif e.attr.xmlns then
8452 return t:event("stream/"..e.attr.xmlns,e); 10639 return t:event("stream/"..e.attr.xmlns,e);
8453 end 10640 end
8454 return t:event("stanza",e); 10641 return t:event("stanza",e);
8455 end 10642 end
8456 function i.error(a,t,e) 10643 function o.error(a,t,e)
8457 if a:event(t,e)==nil then 10644 if a:event(t,e)==nil then
8458 if e then 10645 if e then
8459 local t=e:get_child(nil,"urn:ietf:params:xml:ns:xmpp-streams"); 10646 local t=e:get_child(nil,"urn:ietf:params:xml:ns:xmpp-streams");
8460 local e=e:get_child_text("text","urn:ietf:params:xml:ns:xmpp-streams"); 10647 local e=e:get_child_text("text","urn:ietf:params:xml:ns:xmpp-streams");
8461 error(t.name..(e and": "..e or"")); 10648 error(t.name..(e and": "..e or""));
8462 else 10649 else
8463 error(e and e.name or t or"unknown-error"); 10650 error(e and e.name or t or"unknown-error");
8464 end 10651 end
8465 end 10652 end
8466 end 10653 end
8467 function o:reset() 10654 function i:reset()
8468 if self.stream then 10655 if self.stream then
8469 self.stream:reset(); 10656 self.stream:reset();
8470 else 10657 else
8471 self.stream=r(self,i); 10658 self.stream=s(self,o);
8472 end 10659 end
8473 self.notopen=true; 10660 self.notopen=true;
8474 return true; 10661 return true;
8475 end 10662 end
8476 function o:connect_client(e,a) 10663 function i:connect_client(e,i,a,o)
8477 self.jid,self.password=e,a; 10664 self.jid,self.password=e,i;
10665 self.client_key,self.server_key=a,o;
8478 self.username,self.host,self.resource=d(e); 10666 self.username,self.host,self.resource=d(e);
8479 self:add_plugin("tls"); 10667 self:add_plugin("tls");
8480 self:add_plugin("sasl"); 10668 self:add_plugin("sasl");
8481 self:add_plugin("bind"); 10669 self:add_plugin("bind");
8482 self:add_plugin("session");
8483 function self.data(t,e) 10670 function self.data(t,e)
8484 local t,a=self.stream:feed(e); 10671 local t,a=self.stream:feed(e);
8485 if t then return;end 10672 if t then return;end
8486 self:debug("Received invalid XML (%s) %d bytes: %s",tostring(a),#e,e:sub(1,300):gsub("[\r\n]+"," ")); 10673 self:debug("Received invalid XML (%s) %d bytes: %s",tostring(a),#e,e:sub(1,300):gsub("[\r\n]+"," "));
8487 self:close("xml-not-well-formed"); 10674 self:close("xml-not-well-formed");
8488 end 10675 end
8489 self:hook("connected",function()self:reopen();end); 10676 self:hook("connected",function()self:reopen();end);
8490 self:hook("incoming-raw",function(e)return self.data(self.conn,e);end); 10677 self:hook("incoming-raw",function(e)return self.data(self.conn,e);end);
10678 self:hook("read-timeout",function()self:send(" ");return true;end,-1);
8491 self.curr_id=0; 10679 self.curr_id=0;
8492 self.tracked_iqs={}; 10680 self.tracked_iqs={};
8493 self:hook("stanza",function(t) 10681 self:hook("stanza",function(e)
8494 local e,a=t.attr.id,t.attr.type; 10682 local t,a=e.attr.id,e.attr.type;
8495 if e and t.name=="iq"and(a=="result"or a=="error")and self.tracked_iqs[e]then 10683 if t and e.name=="iq"and(a=="result"or a=="error")and self.tracked_iqs[t]then
8496 self.tracked_iqs[e](t); 10684 self.tracked_iqs[t](e);
8497 self.tracked_iqs[e]=nil; 10685 self.tracked_iqs[t]=nil;
8498 return true; 10686 return true;
8499 end 10687 end
8500 end); 10688 end);
8501 self:hook("stanza",function(e) 10689 self:hook("stanza",function(e)
8502 local a; 10690 local a;
8532 local function e() 10720 local function e()
8533 self:event("ready"); 10721 self:event("ready");
8534 end 10722 end
8535 self:hook("session-success",e,-1) 10723 self:hook("session-success",e,-1)
8536 self:hook("bind-success",e,-1); 10724 self:hook("bind-success",e,-1);
8537 local t=self.close; 10725 local e=self.close;
8538 function self:close(e) 10726 function self:close(t)
8539 self.close=t; 10727 self.close=e;
8540 if not self.closed then 10728 if not self.closed then
8541 self:send("</stream:stream>"); 10729 self:send("</stream:stream>");
8542 self.closed=true; 10730 self.closed=true;
8543 else 10731 else
8544 return self:close(e); 10732 return self:close(t);
8545 end 10733 end
8546 end 10734 end
8547 local function a() 10735 local function a()
8548 self:connect(self.connect_host or self.host,self.connect_port or 5222); 10736 self:connect(self.connect_host or self.host,self.connect_port or 5222);
8549 end 10737 end
8550 if not(self.connect_host or self.connect_port)then 10738 if not(self.connect_host or self.connect_port)then
8551 h.lookup(function(t) 10739 r.lookup(function(t)
8552 if t then 10740 if t then
8553 local e={}; 10741 local e={};
8554 self.srv_hosts=e; 10742 self.srv_hosts=e;
8555 for a,t in ipairs(t)do 10743 for a,t in ipairs(t)do
8556 table.insert(e,t.srv); 10744 table.insert(e,t.srv);
8557 end 10745 end
8558 table.sort(e,s); 10746 table.sort(e,h);
8559 local t=e[1]; 10747 local t=e[1];
8560 self.srv_choice=1; 10748 self.srv_choice=1;
8561 if t then 10749 if t then
8562 self.connect_host,self.connect_port=t.target,t.port; 10750 self.connect_host,self.connect_port=t.target,t.port;
8563 self:debug("Best record found, will connect to %s:%d",self.connect_host or self.host,self.connect_port or 5222); 10751 self:debug("Best record found, will connect to %s:%d",self.connect_host or self.host,self.connect_port or 5222);
8579 end,"_xmpp-client._tcp."..(self.host)..".","SRV"); 10767 end,"_xmpp-client._tcp."..(self.host)..".","SRV");
8580 else 10768 else
8581 a(); 10769 a();
8582 end 10770 end
8583 end 10771 end
8584 function o:reopen() 10772 function i:reopen()
8585 self:reset(); 10773 self:reset();
8586 self:send(a.stanza("stream:stream",{to=self.host,["xmlns:stream"]='http://etherx.jabber.org/streams', 10774 self:send(a.stanza("stream:stream",{to=self.host,["xmlns:stream"]='http://etherx.jabber.org/streams',
8587 xmlns="jabber:client",version="1.0"}):top_tag()); 10775 xmlns="jabber:client",version="1.0",["xml:lang"]=self.lang}):top_tag());
8588 end 10776 end
8589 function o:send_iq(t,a) 10777 function i:send_iq(e,a)
8590 local e=self:new_id(); 10778 local t=e.attr.id or uuid.generate();
8591 self.tracked_iqs[e]=a; 10779 self.tracked_iqs[t]=a;
8592 t.attr.id=e; 10780 e.attr.id=t;
8593 self:send(t); 10781 self:send(e);
8594 end
8595 function o:new_id()
8596 self.curr_id=self.curr_id+1;
8597 return tostring(self.curr_id);
8598 end 10782 end
8599 end) 10783 end)
8600 package.preload['verse.component']=(function(...) 10784 package.preload['verse.component']=(function(...)
8601 local _ENV=_ENV; 10785 local _ENV=_ENV;
8602 local function e(t,...) 10786 local function e(t,...)
8607 end 10791 end
8608 _ENV=e; 10792 _ENV=e;
8609 _M=e; 10793 _M=e;
8610 return e; 10794 return e;
8611 end 10795 end
8612 local t=require"verse"; 10796 local a=require"verse";
8613 local a=t.stream_mt; 10797 local t=a.stream_mt;
8614 local h=require"util.jid".split; 10798 local d=require"util.jid".split;
8615 local e=require"lxp"; 10799 local e=require"lxp";
8616 local o=require"util.stanza"; 10800 local o=require"util.stanza";
8617 local d=require"util.hashes".sha1; 10801 local r=require"util.hashes".sha1;
8618 t.message,t.presence,t.iq,t.stanza,t.reply,t.error_reply= 10802 a.message,a.presence,a.iq,a.stanza,a.reply,a.error_reply=
8619 o.message,o.presence,o.iq,o.stanza,o.reply,o.error_reply; 10803 o.message,o.presence,o.iq,o.stanza,o.reply,o.error_reply;
8620 local r=require"util.xmppstream".new; 10804 local h=require"util.xmppstream".new;
8621 local s="http://etherx.jabber.org/streams"; 10805 local s="http://etherx.jabber.org/streams";
8622 local i="jabber:component:accept"; 10806 local i="jabber:component:accept";
8623 local n={ 10807 local n={
8624 stream_ns=s, 10808 stream_ns=s,
8625 stream_tag="stream", 10809 stream_tag="stream",
8640 elseif e.attr.xmlns or e.name=="handshake"then 10824 elseif e.attr.xmlns or e.name=="handshake"then
8641 return t:event("stream/"..(e.attr.xmlns or i),e); 10825 return t:event("stream/"..(e.attr.xmlns or i),e);
8642 end 10826 end
8643 return t:event("stanza",e); 10827 return t:event("stanza",e);
8644 end 10828 end
8645 function a:reset() 10829 function t:reset()
8646 if self.stream then 10830 if self.stream then
8647 self.stream:reset(); 10831 self.stream:reset();
8648 else 10832 else
8649 self.stream=r(self,n); 10833 self.stream=h(self,n);
8650 end 10834 end
8651 self.notopen=true; 10835 self.notopen=true;
8652 return true; 10836 return true;
8653 end 10837 end
8654 function a:connect_component(e,n) 10838 function t:connect_component(e,n)
8655 self.jid,self.password=e,n; 10839 self.jid,self.password=e,n;
8656 self.username,self.host,self.resource=h(e); 10840 self.username,self.host,self.resource=d(e);
8657 function self.data(t,e) 10841 function self.data(a,e)
8658 local t,o=self.stream:feed(e); 10842 local a,o=self.stream:feed(e);
8659 if t then return;end 10843 if a then return;end
8660 a:debug("Received invalid XML (%s) %d bytes: %s",tostring(o),#e,e:sub(1,300):gsub("[\r\n]+"," ")); 10844 t:debug("Received invalid XML (%s) %d bytes: %s",tostring(o),#e,e:sub(1,300):gsub("[\r\n]+"," "));
8661 a:close("xml-not-well-formed"); 10845 t:close("xml-not-well-formed");
8662 end 10846 end
8663 self:hook("incoming-raw",function(e)return self.data(self.conn,e);end); 10847 self:hook("incoming-raw",function(e)return self.data(self.conn,e);end);
8664 self.curr_id=0; 10848 self.curr_id=0;
8665 self.tracked_iqs={}; 10849 self.tracked_iqs={};
8666 self:hook("stanza",function(e) 10850 self:hook("stanza",function(t)
8667 local t,a=e.attr.id,e.attr.type; 10851 local e,a=t.attr.id,t.attr.type;
8668 if t and e.name=="iq"and(a=="result"or a=="error")and self.tracked_iqs[t]then 10852 if e and t.name=="iq"and(a=="result"or a=="error")and self.tracked_iqs[e]then
8669 self.tracked_iqs[t](e); 10853 self.tracked_iqs[e](t);
8670 self.tracked_iqs[t]=nil; 10854 self.tracked_iqs[e]=nil;
8671 return true; 10855 return true;
8672 end 10856 end
8673 end); 10857 end);
8674 self:hook("stanza",function(e) 10858 self:hook("stanza",function(e)
8675 local a; 10859 local t;
8676 if e.attr.xmlns==nil or e.attr.xmlns=="jabber:client"then 10860 if e.attr.xmlns==nil or e.attr.xmlns=="jabber:client"then
8677 if e.name=="iq"and(e.attr.type=="get"or e.attr.type=="set")then 10861 if e.name=="iq"and(e.attr.type=="get"or e.attr.type=="set")then
8678 local o=e.tags[1]and e.tags[1].attr.xmlns; 10862 local o=e.tags[1]and e.tags[1].attr.xmlns;
8679 if o then 10863 if o then
8680 a=self:event("iq/"..o,e); 10864 t=self:event("iq/"..o,e);
8681 if not a then 10865 if not t then
8682 a=self:event("iq",e); 10866 t=self:event("iq",e);
8683 end 10867 end
8684 end 10868 end
8685 if a==nil then 10869 if t==nil then
8686 self:send(t.error_reply(e,"cancel","service-unavailable")); 10870 self:send(a.error_reply(e,"cancel","service-unavailable"));
8687 return true; 10871 return true;
8688 end 10872 end
8689 else 10873 else
8690 a=self:event(e.name,e); 10874 t=self:event(e.name,e);
8691 end 10875 end
8692 end 10876 end
8693 return a; 10877 return t;
8694 end,-1); 10878 end,-1);
8695 self:hook("opened",function(e) 10879 self:hook("opened",function(e)
8696 print(self.jid,self.stream_id,e.id); 10880 print(self.jid,self.stream_id,e.id);
8697 local e=d(self.stream_id..n,true); 10881 local e=r(self.stream_id..n,true);
8698 self:send(o.stanza("handshake",{xmlns=i}):text(e)); 10882 self:send(o.stanza("handshake",{xmlns=i}):text(e));
8699 self:hook("stream/"..i,function(e) 10883 self:hook("stream/"..i,function(e)
8700 if e.name=="handshake"then 10884 if e.name=="handshake"then
8701 self:event("authentication-success"); 10885 self:event("authentication-success");
8702 end 10886 end
8707 end 10891 end
8708 self:hook("authentication-success",e,-1); 10892 self:hook("authentication-success",e,-1);
8709 self:connect(self.connect_host or self.host,self.connect_port or 5347); 10893 self:connect(self.connect_host or self.host,self.connect_port or 5347);
8710 self:reopen(); 10894 self:reopen();
8711 end 10895 end
8712 function a:reopen() 10896 function t:reopen()
8713 self:reset(); 10897 self:reset();
8714 self:send(o.stanza("stream:stream",{to=self.jid,["xmlns:stream"]='http://etherx.jabber.org/streams', 10898 self:send(o.stanza("stream:stream",{to=self.jid,["xmlns:stream"]='http://etherx.jabber.org/streams',
8715 xmlns=i,version="1.0"}):top_tag()); 10899 xmlns=i,version="1.0"}):top_tag());
8716 end 10900 end
8717 function a:close(t) 10901 function t:close(t)
8718 if not self.notopen then 10902 if not self.notopen then
8719 self:send("</stream:stream>"); 10903 self:send("</stream:stream>");
8720 end 10904 end
8721 local e=self.conn.disconnect(); 10905 local e=self.conn.disconnect();
8722 self.conn:close(); 10906 self.conn:close();
8723 e(conn,t); 10907 e(conn,t);
8724 end 10908 end
8725 function a:send_iq(t,a) 10909 function t:send_iq(e,a)
8726 local e=self:new_id(); 10910 local t=self:new_id();
8727 self.tracked_iqs[e]=a; 10911 self.tracked_iqs[t]=a;
8728 t.attr.id=e; 10912 e.attr.id=t;
8729 self:send(t); 10913 self:send(e);
8730 end 10914 end
8731 function a:new_id() 10915 function t:new_id()
8732 self.curr_id=self.curr_id+1; 10916 self.curr_id=self.curr_id+1;
8733 return tostring(self.curr_id); 10917 return tostring(self.curr_id);
8734 end 10918 end
8735 end) 10919 end)
8736 pcall(require,"luarocks.require"); 10920 pcall(require,"luarocks.require");
8737 local h=require"socket"; 10921 local h=require"socket";
8738 pcall(require,"ssl"); 10922 pcall(require,"ssl");
8739 local a=require"net.server"; 10923 local a=require"net.server";
8740 local s=require"util.events"; 10924 local s=require"util.events";
8741 local o=require"util.logger"; 10925 local o=require"util.logger";
10926 local n=require"util.format".format;
8742 local e={}; 10927 local e={};
8743 e.server=a; 10928 e.server=a;
8744 local t={}; 10929 local t={};
8745 t.__index=t; 10930 t.__index=t;
8746 e.stream_mt=t; 10931 e.stream_mt=t;
8767 end 10952 end
8768 e.add_task=require"util.timer".add_task; 10953 e.add_task=require"util.timer".add_task;
8769 e.logger=o.init; 10954 e.logger=o.init;
8770 e.new_logger=o.init; 10955 e.new_logger=o.init;
8771 e.log=e.logger("verse"); 10956 e.log=e.logger("verse");
8772 local function n(t,...)
8773 local e,a,o=0,{...},select('#',...);
8774 return(t:gsub("%%(.)",function(t)if e<=o then e=e+1;return tostring(a[e]);end end));
8775 end
8776 function e.set_log_handler(e,t) 10957 function e.set_log_handler(e,t)
8777 t=t or{"debug","info","warn","error"}; 10958 t=t or{"debug","info","warn","error"};
8778 o.reset(); 10959 o.reset();
8779 if io.type(e)=="file"then 10960 if io.type(e)=="file"then
8780 local o=e; 10961 local o=e;
8781 function e(t,a,e) 10962 function e(a,t,e)
8782 o:write(t,"\t",a,"\t",e,"\n"); 10963 o:write(a,"\t",t,"\t",e,"\n");
8783 end 10964 end
8784 end 10965 end
8785 if e then 10966 if e then
8786 local function i(o,a,t,...) 10967 local function a(t,a,o,...)
8787 return e(o,a,n(t,...)); 10968 return e(t,a,n(o,...));
8788 end 10969 end
8789 for t,e in ipairs(t)do 10970 for t,e in ipairs(t)do
8790 o.add_level_sink(e,i); 10971 o.add_level_sink(e,a);
8791 end 10972 end
8792 end 10973 end
8793 end 10974 end
8794 function e._default_log_handler(a,t,o) 10975 function e._default_log_handler(a,o,t)
8795 return io.stderr:write(a,"\t",t,"\t",o,"\n"); 10976 return io.stderr:write(a,"\t",o,"\t",t,"\n");
8796 end 10977 end
8797 e.set_log_handler(e._default_log_handler,{"error"}); 10978 e.set_log_handler(e._default_log_handler,{"error"});
8798 local function o(t) 10979 local function o(t)
8799 e.log("error","Error: %s",t); 10980 e.log("error","Error: %s",t);
8800 e.log("error","Traceback: %s",debug.traceback()); 10981 e.log("error","Traceback: %s",debug.traceback());
8808 function e.step() 10989 function e.step()
8809 return xpcall(a.step,o); 10990 return xpcall(a.step,o);
8810 end 10991 end
8811 function e.quit() 10992 function e.quit()
8812 return a.setquitting("once"); 10993 return a.setquitting("once");
10994 end
10995 function e.tls_builder(...)
10996 return a.tls_builder(...);
8813 end 10997 end
8814 function t:listen(t,o) 10998 function t:listen(t,o)
8815 t=t or"localhost"; 10999 t=t or"localhost";
8816 o=o or 0; 11000 o=o or 0;
8817 local e,a=a.addserver(t,o,e.new_listener(self,"server"),"*a"); 11001 local e,a=a.addserver(t,o,e.new_listener(self,"server"),"*a");
8819 self:debug("Bound to %s:%s",t,o); 11003 self:debug("Bound to %s:%s",t,o);
8820 self.server=e; 11004 self.server=e;
8821 end 11005 end
8822 return e,a; 11006 return e,a;
8823 end 11007 end
8824 function t:connect(i,o) 11008 function t:connect(o,i)
8825 i=i or"localhost"; 11009 o=o or"localhost";
8826 o=tonumber(o)or 5222; 11010 i=tonumber(i)or 5222;
8827 local n=h.tcp() 11011 local n=h.tcp()
8828 n:settimeout(0); 11012 n:settimeout(0);
8829 n:setoption("keepalive",true); 11013 n:setoption("keepalive",true);
8830 local s,t=n:connect(i,o); 11014 local s,t=n:connect(o,i);
8831 if not s and t~="timeout"then 11015 if not s and t~="timeout"then
8832 self:warn("connect() to %s:%d failed: %s",i,o,t); 11016 self:warn("connect() to %s:%d failed: %s",o,i,t);
8833 return self:event("disconnected",{reason=t})or false,t; 11017 return self:event("disconnected",{reason=t})or false,t;
8834 end 11018 end
8835 local e=a.wrapclient(n,i,o,e.new_listener(self),"*a"); 11019 local e=a.wrapclient(n,o,i,e.new_listener(self),"*a");
8836 if not e then 11020 if not e then
8837 self:warn("connection initialisation failed: %s",t); 11021 self:warn("connection initialisation failed: %s",t);
8838 return self:event("disconnected",{reason=t})or false,t; 11022 return self:event("disconnected",{reason=t})or false,t;
8839 end 11023 end
8840 self:set_conn(e); 11024 self:set_conn(e);
8853 if not self.conn then 11037 if not self.conn then
8854 e.log("error","Attempt to close disconnected connection - possibly a bug"); 11038 e.log("error","Attempt to close disconnected connection - possibly a bug");
8855 return; 11039 return;
8856 end 11040 end
8857 local e=self.conn.disconnect(); 11041 local e=self.conn.disconnect();
11042 self:event("shutdown");
8858 self.conn:close(); 11043 self.conn:close();
8859 e(self.conn,t); 11044 e(self.conn,t);
8860 end 11045 end
8861 function t:debug(...) 11046 function t:debug(...)
8862 return self.logger("debug",...); 11047 return self.logger("debug",...);
8882 end 11067 end
8883 function e.eventable(e) 11068 function e.eventable(e)
8884 e.events=s.new(); 11069 e.events=s.new();
8885 e.hook,e.unhook=t.hook,t.unhook; 11070 e.hook,e.unhook=t.hook,t.unhook;
8886 local t=e.events.fire_event; 11071 local t=e.events.fire_event;
8887 function e:event(e,...) 11072 function e:event(e,...)return t(e,...);end
8888 return t(e,...);
8889 end
8890 return e; 11073 return e;
8891 end 11074 end
8892 function t:add_plugin(t) 11075 function t:add_plugin(t)
8893 if self.plugins[t]then return true;end 11076 if self.plugins[t]then return true;end
8894 if require("verse.plugins."..t)then 11077 if require("verse.plugins."..t)then
8895 local a,e=e.plugins[t](self); 11078 local e,a=e.plugins[t](self);
8896 if a~=false then 11079 if e~=false then
8897 self:debug("Loaded %s plugin",t); 11080 self:debug("Loaded %s plugin",t);
8898 self.plugins[t]=true; 11081 self.plugins[t]=true;
8899 else 11082 else
8900 self:warn("Failed to load %s plugin: %s",t,e); 11083 self:warn("Failed to load %s plugin: %s",t,a);
8901 end 11084 end
8902 end 11085 end
8903 return self; 11086 return self;
8904 end 11087 end
8905 function e.new_listener(t) 11088 function e.new_listener(t)
8927 t:event("drained"); 11110 t:event("drained");
8928 end 11111 end
8929 function a.onstatus(a,e) 11112 function a.onstatus(a,e)
8930 t:event("status",e); 11113 t:event("status",e);
8931 end 11114 end
11115 function a.onreadtimeout(e)
11116 return t:event("read-timeout");
11117 end
8932 return a; 11118 return a;
8933 end 11119 end
8934 return e; 11120 return e;

mercurial