verse.lua

changeset 180
249197af6c01
parent 177
3c19b67a1f0f
equal deleted inserted replaced
179:8a9bf558aead 180:249197af6c01
21 utf8={ 21 utf8={
22 valid=(utf8 and utf8.len)and function(e)return not not utf8.len(e);end or function()return true;end; 22 valid=(utf8 and utf8.len)and function(e)return not not utf8.len(e);end or function()return true;end;
23 }; 23 };
24 return _M; 24 return _M;
25 end) 25 end)
26 package.preload['util.sha1']=(function(...) 26 package.preload['util.hashes']=(function(...)
27 local _ENV=_ENV; 27 local _ENV=_ENV;
28 local function e(t,...) 28 local function e(t,...)
29 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 29 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
30 package.loaded[t]=e; 30 package.loaded[t]=e;
31 for t=1,select("#",...)do 31 for t=1,select("#",...)do
33 end 33 end
34 _ENV=e; 34 _ENV=e;
35 _M=e; 35 _M=e;
36 return e; 36 return e;
37 end 37 end
38 local m=string.len 38 local function t(t,e)
39 local o=string.char 39 error("Hash method "..e.." not available",2);
40 local k=string.byte 40 end
41 local q=string.sub 41 local e=setmetatable({},{__index=t});
42 local c=math.floor 42 local function t(t,o)
43 local t=require"bit" 43 local e,a;
44 local j=t.bnot 44 if t=="util.sha1"then
45 local e=t.band 45 e,a=true,require(t)
46 local y=t.bor 46 else
47 local n=t.bxor 47 e,a=pcall(require,t);
48 local a=t.lshift 48 end
49 local i=t.rshift 49 if e then o(a);end
50 local u,r,l,d,h 50 end
51 local function p(t,e) 51 local function i(e)
52 return a(t,e)+i(t,32-e) 52 return(e:gsub(".",function(e)
53 end 53 return("%02x"):format(e:byte());
54 local function s(i)
55 local t,a
56 local t=""
57 for n=1,8 do
58 a=e(i,15)
59 if(a<10)then
60 t=o(a+48)..t
61 else
62 t=o(a+87)..t
63 end
64 i=c(i/16)
65 end
66 return t
67 end
68 local function g(t)
69 local i,a
70 local n=""
71 i=m(t)*8
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)); 54 end));
150 end 55 end
151 end 56 t("util.sha1",function(t)
152 _G.sha1={sha1=a}; 57 e.sha1=t.sha1;
153 return _G.sha1; 58 end);
59 t("bgcrypto.md5",function(t)
60 e.md5=t.digest;
61 e.hmac_md5=t.hmac.digest;
62 end);
63 t("bgcrypto.sha1",function(t)
64 e.sha1=t.digest;
65 e.hmac_sha1=t.hmac.digest;
66 e.scram_Hi_sha1=function(a,o,e)return t.pbkdf2(a,o,e,20);end;
67 end);
68 t("bgcrypto.sha256",function(t)
69 e.sha256=t.digest;
70 e.hmac_sha256=t.hmac.digest;
71 end);
72 t("bgcrypto.sha512",function(t)
73 e.sha512=t.digest;
74 e.hmac_sha512=t.hmac.digest;
75 end);
76 t("sha1",function(t)
77 e.sha1=function(e,a)
78 if a then
79 return t.sha1(e);
80 else
81 return(t.binary(e));
82 end
83 end;
84 end);
85 t("openssl.digest",function(o)
86 local function a(t)
87 return function(e,a)
88 local e=o.new(t):final(e);
89 if a then
90 return i(e);
91 end
92 return e;
93 end;
94 end
95 e.sha1=a("sha1");
96 end);
97 return e;
154 end) 98 end)
155 package.preload['lib.adhoc']=(function(...) 99 package.preload['lib.adhoc']=(function(...)
156 local _ENV=_ENV; 100 local _ENV=_ENV;
157 local function d(t,...) 101 local function r(t,...)
158 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 102 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
159 package.loaded[t]=e; 103 package.loaded[t]=e;
160 for t=1,select("#",...)do 104 for t=1,select("#",...)do
161 (select(t,...))(e); 105 (select(t,...))(e);
162 end 106 end
163 _ENV=e; 107 _ENV=e;
164 _M=e; 108 _M=e;
165 return e; 109 return e;
166 end 110 end
167 local s,l=require"util.stanza",require"util.id".short; 111 local s,i=require"util.stanza",require"util.id".short;
168 local r="http://jabber.org/protocol/commands"; 112 local e="http://jabber.org/protocol/commands";
169 local n={} 113 local n={}
170 local h={}; 114 local h={};
171 local function i(o,e,t,a) 115 local function o(i,o,t,a)
172 local e=s.stanza("command",{xmlns=r,node=o.node,status=e}); 116 local e=s.stanza("command",{xmlns=e,node=i.node,status=o});
173 if t then e.attr.sessionid=t;end 117 if t then e.attr.sessionid=t;end
174 if a then e.attr.action=a;end 118 if a then e.attr.action=a;end
175 return e; 119 return e;
176 end 120 end
177 function h.new(e,o,a,t) 121 function h.new(e,a,i,t)
178 return{name=e,node=o,handler=a,cmdtag=i,permission=(t or"user")}; 122 return{name=e,node=a,handler=i,cmdtag=o,permission=(t or"user")};
179 end 123 end
180 function h.handle_cmd(o,h,t) 124 function h.handle_cmd(o,h,e)
181 local e=t.tags[1].attr.sessionid or l(); 125 local t=e.tags[1].attr.sessionid or i();
182 local a={}; 126 local a={};
183 a.to=t.attr.to; 127 a.to=e.attr.to;
184 a.from=t.attr.from; 128 a.from=e.attr.from;
185 a.action=t.tags[1].attr.action or"execute"; 129 a.action=e.tags[1].attr.action or"execute";
186 a.form=t.tags[1]:child_with_ns("jabber:x:data"); 130 a.form=e.tags[1]:child_with_ns("jabber:x:data");
187 local a,i=o:handler(a,n[e]); 131 local a,i=o:handler(a,n[t]);
188 n[e]=i; 132 n[t]=i;
189 local i=s.reply(t); 133 local i=s.reply(e);
190 local t; 134 local e;
191 if a.status=="completed"then 135 if a.status=="completed"then
192 n[e]=nil; 136 n[t]=nil;
193 t=o:cmdtag("completed",e); 137 e=o:cmdtag("completed",t);
194 elseif a.status=="canceled"then 138 elseif a.status=="canceled"then
195 n[e]=nil; 139 n[t]=nil;
196 t=o:cmdtag("canceled",e); 140 e=o:cmdtag("canceled",t);
197 elseif a.status=="error"then 141 elseif a.status=="error"then
198 n[e]=nil; 142 n[t]=nil;
199 i=s.error_reply(i,a.error.type,a.error.condition,a.error.message); 143 i=s.error_reply(i,a.error.type,a.error.condition,a.error.message);
200 h.send(i); 144 h.send(i);
201 return true; 145 return true;
202 else 146 else
203 t=o:cmdtag("executing",e); 147 e=o:cmdtag("executing",t);
204 end 148 end
205 for a,e in pairs(a)do 149 for a,t in pairs(a)do
206 if a=="info"then 150 if a=="info"then
207 t:tag("note",{type="info"}):text(e):up(); 151 e:tag("note",{type="info"}):text(t):up();
208 elseif a=="warn"then 152 elseif a=="warn"then
209 t:tag("note",{type="warn"}):text(e):up(); 153 e:tag("note",{type="warn"}):text(t):up();
210 elseif a=="error"then 154 elseif a=="error"then
211 t:tag("note",{type="error"}):text(e.message):up(); 155 e:tag("note",{type="error"}):text(t.message):up();
212 elseif a=="actions"then 156 elseif a=="actions"then
213 local a=s.stanza("actions"); 157 local a=s.stanza("actions");
214 for i,e in ipairs(e)do 158 for i,t in ipairs(t)do
215 if(e=="prev")or(e=="next")or(e=="complete")then 159 if(t=="prev")or(t=="next")or(t=="complete")then
216 a:tag(e):up(); 160 a:tag(t):up();
217 else 161 else
218 d:log("error",'Command "'..o.name.. 162 r:log("error",'Command "'..o.name..
219 '" at node "'..o.node..'" provided an invalid action "'..e..'"'); 163 '" at node "'..o.node..'" provided an invalid action "'..t..'"');
220 end 164 end
221 end 165 end
222 t:add_child(a); 166 e:add_child(a);
223 elseif a=="form"then 167 elseif a=="form"then
224 t:add_child((e.layout or e):form(e.values)); 168 e:add_child((t.layout or t):form(t.values));
225 elseif a=="result"then 169 elseif a=="result"then
226 t:add_child((e.layout or e):form(e.values,"result")); 170 e:add_child((t.layout or t):form(t.values,"result"));
227 elseif a=="other"then 171 elseif a=="other"then
228 t:add_child(e); 172 e:add_child(t);
229 end 173 end
230 end 174 end
231 i:add_child(t); 175 i:add_child(e);
232 h.send(i); 176 h.send(i);
233 return true; 177 return true;
234 end 178 end
235 return h; 179 return h;
236 end) 180 end)
244 end 188 end
245 _ENV=e; 189 _ENV=e;
246 _M=e; 190 _M=e;
247 return e; 191 return e;
248 end 192 end
249 return{pack=function(...)return{n=select("#",...);...}end;create=function()return{}end} 193 return{
194 pack=function(...)return{n=select("#",...);...}end;
195 create=function()return{}end;
196 move=function(o,t,i,a,e)
197 e=e or o;
198 if i>=t then
199 local n=(i-t)+1;
200 if a>i or a<=t or o~=e then
201 for i=0,n-1 do
202 e[a+i]=o[t+i];
203 end
204 else
205 for i=n-1,0,-1 do
206 e[a+i]=o[t+i];
207 end
208 end
209 end
210 return e;
211 end;
212 }
213 end)
214 package.preload['util.sha1']=(function(...)
215 local _ENV=_ENV;
216 local function e(t,...)
217 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
218 package.loaded[t]=e;
219 for t=1,select("#",...)do
220 (select(t,...))(e);
221 end
222 _ENV=e;
223 _M=e;
224 return e;
225 end
226 local m=string.len
227 local a=string.char
228 local j=string.byte
229 local k=string.sub
230 local c=math.floor
231 local t=require"util.bit"
232 local q=t.bnot
233 local e=t.band
234 local y=t.bor
235 local n=t.bxor
236 local i=t.lshift
237 local o=t.rshift
238 local l,u,d,h,s
239 local function p(t,e)
240 return i(t,e)+o(t,32-e)
241 end
242 local function r(i)
243 local t,o
244 local t=""
245 for n=1,8 do
246 o=e(i,15)
247 if(o<10)then
248 t=a(o+48)..t
249 else
250 t=a(o+87)..t
251 end
252 i=c(i/16)
253 end
254 return t
255 end
256 local function b(t)
257 local i,o
258 local n=""
259 i=m(t)*8
260 t=t..a(128)
261 o=56-e(m(t),63)
262 if(o<0)then
263 o=o+64
264 end
265 for e=1,o do
266 t=t..a(0)
267 end
268 for t=1,8 do
269 n=a(e(i,255))..n
270 i=c(i/256)
271 end
272 return t..n
273 end
274 local function g(f)
275 local m,t,o,a,w,r,c,v
276 local i,i
277 local i={}
278 while(f~="")do
279 for e=0,15 do
280 i[e]=0
281 for t=1,4 do
282 i[e]=i[e]*256+j(f,e*4+t)
283 end
284 end
285 for e=16,79 do
286 i[e]=p(n(n(i[e-3],i[e-8]),n(i[e-14],i[e-16])),1)
287 end
288 m=l
289 t=u
290 o=d
291 a=h
292 w=s
293 for s=0,79 do
294 if(s<20)then
295 r=y(e(t,o),e(q(t),a))
296 c=1518500249
297 elseif(s<40)then
298 r=n(n(t,o),a)
299 c=1859775393
300 elseif(s<60)then
301 r=y(y(e(t,o),e(t,a)),e(o,a))
302 c=2400959708
303 else
304 r=n(n(t,o),a)
305 c=3395469782
306 end
307 v=p(m,5)+r+w+c+i[s]
308 w=a
309 a=o
310 o=p(t,30)
311 t=m
312 m=v
313 end
314 l=e(l+m,4294967295)
315 u=e(u+t,4294967295)
316 d=e(d+o,4294967295)
317 h=e(h+a,4294967295)
318 s=e(s+w,4294967295)
319 f=k(f,65)
320 end
321 end
322 local function t(e,t)
323 e=b(e)
324 l=1732584193
325 u=4023233417
326 d=2562383102
327 h=271733878
328 s=3285377520
329 g(e)
330 local e=r(l)..r(u)..r(d)
331 ..r(h)..r(s);
332 if t then
333 return e;
334 else
335 return(e:gsub("..",function(e)
336 return string.char(tonumber(e,16));
337 end));
338 end
339 end
340 return{
341 sha1=t;
342 };
343 end)
344 package.preload['util.bit']=(function(...)
345 local _ENV=_ENV;
346 local function e(t,...)
347 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
348 package.loaded[t]=e;
349 for t=1,select("#",...)do
350 (select(t,...))(e);
351 end
352 _ENV=e;
353 _M=e;
354 return e;
355 end
356 local o=type;
357 local h=tonumber;
358 local n=setmetatable;
359 local u=error;
360 local l=tostring;
361 local c={[0]=0;[1]=1;[2]=2;[3]=3;[4]=4;[5]=5;[6]=6;[7]=7;[8]=8;[9]=9;[10]=10;[11]=11;[12]=12;[13]=13;[14]=14;[15]=15;[16]=1;[17]=0;[18]=3;[19]=2;[20]=5;[21]=4;[22]=7;[23]=6;[24]=9;[25]=8;[26]=11;[27]=10;[28]=13;[29]=12;[30]=15;[31]=14;[32]=2;[33]=3;[34]=0;[35]=1;[36]=6;[37]=7;[38]=4;[39]=5;[40]=10;[41]=11;[42]=8;[43]=9;[44]=14;[45]=15;[46]=12;[47]=13;[48]=3;[49]=2;[50]=1;[51]=0;[52]=7;[53]=6;[54]=5;[55]=4;[56]=11;[57]=10;[58]=9;[59]=8;[60]=15;[61]=14;[62]=13;[63]=12;[64]=4;[65]=5;[66]=6;[67]=7;[68]=0;[69]=1;[70]=2;[71]=3;[72]=12;[73]=13;[74]=14;[75]=15;[76]=8;[77]=9;[78]=10;[79]=11;[80]=5;[81]=4;[82]=7;[83]=6;[84]=1;[85]=0;[86]=3;[87]=2;[88]=13;[89]=12;[90]=15;[91]=14;[92]=9;[93]=8;[94]=11;[95]=10;[96]=6;[97]=7;[98]=4;[99]=5;[100]=2;[101]=3;[102]=0;[103]=1;[104]=14;[105]=15;[106]=12;[107]=13;[108]=10;[109]=11;[110]=8;[111]=9;[112]=7;[113]=6;[114]=5;[115]=4;[116]=3;[117]=2;[118]=1;[119]=0;[120]=15;[121]=14;[122]=13;[123]=12;[124]=11;[125]=10;[126]=9;[127]=8;[128]=8;[129]=9;[130]=10;[131]=11;[132]=12;[133]=13;[134]=14;[135]=15;[136]=0;[137]=1;[138]=2;[139]=3;[140]=4;[141]=5;[142]=6;[143]=7;[144]=9;[145]=8;[146]=11;[147]=10;[148]=13;[149]=12;[150]=15;[151]=14;[152]=1;[153]=0;[154]=3;[155]=2;[156]=5;[157]=4;[158]=7;[159]=6;[160]=10;[161]=11;[162]=8;[163]=9;[164]=14;[165]=15;[166]=12;[167]=13;[168]=2;[169]=3;[170]=0;[171]=1;[172]=6;[173]=7;[174]=4;[175]=5;[176]=11;[177]=10;[178]=9;[179]=8;[180]=15;[181]=14;[182]=13;[183]=12;[184]=3;[185]=2;[186]=1;[187]=0;[188]=7;[189]=6;[190]=5;[191]=4;[192]=12;[193]=13;[194]=14;[195]=15;[196]=8;[197]=9;[198]=10;[199]=11;[200]=4;[201]=5;[202]=6;[203]=7;[204]=0;[205]=1;[206]=2;[207]=3;[208]=13;[209]=12;[210]=15;[211]=14;[212]=9;[213]=8;[214]=11;[215]=10;[216]=5;[217]=4;[218]=7;[219]=6;[220]=1;[221]=0;[222]=3;[223]=2;[224]=14;[225]=15;[226]=12;[227]=13;[228]=10;[229]=11;[230]=8;[231]=9;[232]=6;[233]=7;[234]=4;[235]=5;[236]=2;[237]=3;[238]=0;[239]=1;[240]=15;[241]=14;[242]=13;[243]=12;[244]=11;[245]=10;[246]=9;[247]=8;[248]=7;[249]=6;[250]=5;[251]=4;[252]=3;[253]=2;[254]=1;[255]=0;};
362 local w={[0]=0;[1]=1;[2]=2;[3]=3;[4]=4;[5]=5;[6]=6;[7]=7;[8]=8;[9]=9;[10]=10;[11]=11;[12]=12;[13]=13;[14]=14;[15]=15;[16]=1;[17]=1;[18]=3;[19]=3;[20]=5;[21]=5;[22]=7;[23]=7;[24]=9;[25]=9;[26]=11;[27]=11;[28]=13;[29]=13;[30]=15;[31]=15;[32]=2;[33]=3;[34]=2;[35]=3;[36]=6;[37]=7;[38]=6;[39]=7;[40]=10;[41]=11;[42]=10;[43]=11;[44]=14;[45]=15;[46]=14;[47]=15;[48]=3;[49]=3;[50]=3;[51]=3;[52]=7;[53]=7;[54]=7;[55]=7;[56]=11;[57]=11;[58]=11;[59]=11;[60]=15;[61]=15;[62]=15;[63]=15;[64]=4;[65]=5;[66]=6;[67]=7;[68]=4;[69]=5;[70]=6;[71]=7;[72]=12;[73]=13;[74]=14;[75]=15;[76]=12;[77]=13;[78]=14;[79]=15;[80]=5;[81]=5;[82]=7;[83]=7;[84]=5;[85]=5;[86]=7;[87]=7;[88]=13;[89]=13;[90]=15;[91]=15;[92]=13;[93]=13;[94]=15;[95]=15;[96]=6;[97]=7;[98]=6;[99]=7;[100]=6;[101]=7;[102]=6;[103]=7;[104]=14;[105]=15;[106]=14;[107]=15;[108]=14;[109]=15;[110]=14;[111]=15;[112]=7;[113]=7;[114]=7;[115]=7;[116]=7;[117]=7;[118]=7;[119]=7;[120]=15;[121]=15;[122]=15;[123]=15;[124]=15;[125]=15;[126]=15;[127]=15;[128]=8;[129]=9;[130]=10;[131]=11;[132]=12;[133]=13;[134]=14;[135]=15;[136]=8;[137]=9;[138]=10;[139]=11;[140]=12;[141]=13;[142]=14;[143]=15;[144]=9;[145]=9;[146]=11;[147]=11;[148]=13;[149]=13;[150]=15;[151]=15;[152]=9;[153]=9;[154]=11;[155]=11;[156]=13;[157]=13;[158]=15;[159]=15;[160]=10;[161]=11;[162]=10;[163]=11;[164]=14;[165]=15;[166]=14;[167]=15;[168]=10;[169]=11;[170]=10;[171]=11;[172]=14;[173]=15;[174]=14;[175]=15;[176]=11;[177]=11;[178]=11;[179]=11;[180]=15;[181]=15;[182]=15;[183]=15;[184]=11;[185]=11;[186]=11;[187]=11;[188]=15;[189]=15;[190]=15;[191]=15;[192]=12;[193]=13;[194]=14;[195]=15;[196]=12;[197]=13;[198]=14;[199]=15;[200]=12;[201]=13;[202]=14;[203]=15;[204]=12;[205]=13;[206]=14;[207]=15;[208]=13;[209]=13;[210]=15;[211]=15;[212]=13;[213]=13;[214]=15;[215]=15;[216]=13;[217]=13;[218]=15;[219]=15;[220]=13;[221]=13;[222]=15;[223]=15;[224]=14;[225]=15;[226]=14;[227]=15;[228]=14;[229]=15;[230]=14;[231]=15;[232]=14;[233]=15;[234]=14;[235]=15;[236]=14;[237]=15;[238]=14;[239]=15;[240]=15;[241]=15;[242]=15;[243]=15;[244]=15;[245]=15;[246]=15;[247]=15;[248]=15;[249]=15;[250]=15;[251]=15;[252]=15;[253]=15;[254]=15;[255]=15;};
363 local p={[0]=0;[1]=0;[2]=0;[3]=0;[4]=0;[5]=0;[6]=0;[7]=0;[8]=0;[9]=0;[10]=0;[11]=0;[12]=0;[13]=0;[14]=0;[15]=0;[16]=0;[17]=1;[18]=0;[19]=1;[20]=0;[21]=1;[22]=0;[23]=1;[24]=0;[25]=1;[26]=0;[27]=1;[28]=0;[29]=1;[30]=0;[31]=1;[32]=0;[33]=0;[34]=2;[35]=2;[36]=0;[37]=0;[38]=2;[39]=2;[40]=0;[41]=0;[42]=2;[43]=2;[44]=0;[45]=0;[46]=2;[47]=2;[48]=0;[49]=1;[50]=2;[51]=3;[52]=0;[53]=1;[54]=2;[55]=3;[56]=0;[57]=1;[58]=2;[59]=3;[60]=0;[61]=1;[62]=2;[63]=3;[64]=0;[65]=0;[66]=0;[67]=0;[68]=4;[69]=4;[70]=4;[71]=4;[72]=0;[73]=0;[74]=0;[75]=0;[76]=4;[77]=4;[78]=4;[79]=4;[80]=0;[81]=1;[82]=0;[83]=1;[84]=4;[85]=5;[86]=4;[87]=5;[88]=0;[89]=1;[90]=0;[91]=1;[92]=4;[93]=5;[94]=4;[95]=5;[96]=0;[97]=0;[98]=2;[99]=2;[100]=4;[101]=4;[102]=6;[103]=6;[104]=0;[105]=0;[106]=2;[107]=2;[108]=4;[109]=4;[110]=6;[111]=6;[112]=0;[113]=1;[114]=2;[115]=3;[116]=4;[117]=5;[118]=6;[119]=7;[120]=0;[121]=1;[122]=2;[123]=3;[124]=4;[125]=5;[126]=6;[127]=7;[128]=0;[129]=0;[130]=0;[131]=0;[132]=0;[133]=0;[134]=0;[135]=0;[136]=8;[137]=8;[138]=8;[139]=8;[140]=8;[141]=8;[142]=8;[143]=8;[144]=0;[145]=1;[146]=0;[147]=1;[148]=0;[149]=1;[150]=0;[151]=1;[152]=8;[153]=9;[154]=8;[155]=9;[156]=8;[157]=9;[158]=8;[159]=9;[160]=0;[161]=0;[162]=2;[163]=2;[164]=0;[165]=0;[166]=2;[167]=2;[168]=8;[169]=8;[170]=10;[171]=10;[172]=8;[173]=8;[174]=10;[175]=10;[176]=0;[177]=1;[178]=2;[179]=3;[180]=0;[181]=1;[182]=2;[183]=3;[184]=8;[185]=9;[186]=10;[187]=11;[188]=8;[189]=9;[190]=10;[191]=11;[192]=0;[193]=0;[194]=0;[195]=0;[196]=4;[197]=4;[198]=4;[199]=4;[200]=8;[201]=8;[202]=8;[203]=8;[204]=12;[205]=12;[206]=12;[207]=12;[208]=0;[209]=1;[210]=0;[211]=1;[212]=4;[213]=5;[214]=4;[215]=5;[216]=8;[217]=9;[218]=8;[219]=9;[220]=12;[221]=13;[222]=12;[223]=13;[224]=0;[225]=0;[226]=2;[227]=2;[228]=4;[229]=4;[230]=6;[231]=6;[232]=8;[233]=8;[234]=10;[235]=10;[236]=12;[237]=12;[238]=14;[239]=14;[240]=0;[241]=1;[242]=2;[243]=3;[244]=4;[245]=5;[246]=6;[247]=7;[248]=8;[249]=9;[250]=10;[251]=11;[252]=12;[253]=13;[254]=14;[255]=15;}
364 local y={[0]=15;[1]=14;[2]=13;[3]=12;[4]=11;[5]=10;[6]=9;[7]=8;[8]=7;[9]=6;[10]=5;[11]=4;[12]=3;[13]=2;[14]=1;[15]=0;};
365 local d={[0]=0;[1]=0;[2]=1;[3]=1;[4]=2;[5]=2;[6]=3;[7]=3;[8]=4;[9]=4;[10]=5;[11]=5;[12]=6;[13]=6;[14]=7;[15]=7;};
366 local r={[0]=0;[1]=8;[2]=0;[3]=8;[4]=0;[5]=8;[6]=0;[7]=8;[8]=0;[9]=8;[10]=0;[11]=8;[12]=0;[13]=8;[14]=0;[15]=8;};
367 local b={[0]=0;[1]=2;[2]=4;[3]=6;[4]=8;[5]=10;[6]=12;[7]=14;[8]=0;[9]=2;[10]=4;[11]=6;[12]=8;[13]=10;[14]=12;[15]=14;};
368 local v={[0]=0;[1]=0;[2]=0;[3]=0;[4]=0;[5]=0;[6]=0;[7]=0;[8]=1;[9]=1;[10]=1;[11]=1;[12]=1;[13]=1;[14]=1;[15]=1;};
369 local m={[0]=0;[1]=0;[2]=0;[3]=0;[4]=0;[5]=0;[6]=0;[7]=0;[8]=8;[9]=8;[10]=8;[11]=8;[12]=8;[13]=8;[14]=8;[15]=8;};
370 local i={__tostring=function(e)return("%x%x%x%x%x%x%x%x"):format(e[1],e[2],e[3],e[4],e[5],e[6],e[7],e[8]);end};
371 local function s(a,t,e)
372 return n({
373 e[a[1]*16+t[1]];
374 e[a[2]*16+t[2]];
375 e[a[3]*16+t[3]];
376 e[a[4]*16+t[4]];
377 e[a[5]*16+t[5]];
378 e[a[6]*16+t[6]];
379 e[a[7]*16+t[7]];
380 e[a[8]*16+t[8]];
381 },i);
382 end
383 local function a(e,t)
384 return n({
385 t[e[1]];
386 t[e[2]];
387 t[e[3]];
388 t[e[4]];
389 t[e[5]];
390 t[e[6]];
391 t[e[7]];
392 t[e[8]];
393 },i);
394 end
395 local function f(t,e)return s(t,e,c);end
396 local function c(e,t)return s(e,t,w);end
397 local function w(e,t)return s(e,t,p);end
398 local function p(e)return a(e,y);end
399 local function s(t)
400 local a=0;
401 for e=1,8 do
402 local o=d[t[e]]+a;
403 a=r[t[e]];
404 t[e]=o;
405 end
406 end
407 local function y(e,t)
408 local e={e[1],e[2],e[3],e[4],e[5],e[6],e[7],e[8]};
409 for t=1,t do s(e);end
410 return n(e,i);
411 end
412 local function s(e)
413 local a=m[e[1]];
414 for t=1,8 do
415 local o=d[e[t]]+a;
416 a=r[e[t]];
417 e[t]=o;
418 end
419 end
420 local function r(e,t)
421 local e={e[1],e[2],e[3],e[4],e[5],e[6],e[7],e[8]};
422 for t=1,t do s(e);end
423 return n(e,i);
424 end
425 local function s(t)
426 local a=0;
427 for e=8,1,-1 do
428 local o=b[t[e]]+a;
429 a=v[t[e]];
430 t[e]=o;
431 end
432 end
433 local function d(e,t)
434 local e={e[1],e[2],e[3],e[4],e[5],e[6],e[7],e[8]};
435 for t=1,t do s(e);end
436 return n(e,i);
437 end
438 local function a(e)
439 if o(e)=="number"then e=("%x"):format(e);
440 elseif o(e)=="table"then return e;
441 elseif o(e)~="string"then u("string expected, got "..o(e),2);end
442 local a={0,0,0,0,0,0,0,0};
443 e="00000000"..e;
444 e=e:sub(-8);
445 for t=1,8 do
446 a[t]=h(e:sub(t,t),16)or u("Number format error",2);
447 end
448 return n(a,i);
449 end
450 local function t(t)
451 return function(e,...)
452 if o(e)~="table"then e=a(e);end
453 e=t(e,...);
454 e=h(l(e),16);
455 if e>2147483647 then e=e-1-4294967295;end
456 return e;
457 end;
458 end
459 local function i(i)
460 return function(e,t,...)
461 if o(e)~="table"then e=a(e);end
462 if o(t)~="table"then t=a(t);end
463 e=i(e,t,...);
464 e=h(l(e),16);
465 if e>2147483647 then e=e-1-4294967295;end
466 return e;
467 end;
468 end
469 return{
470 bits=32;
471 bxor=i(f);
472 bor=i(c);
473 band=i(w);
474 bnot=t(p);
475 lshift=t(d);
476 rshift=t(y);
477 arshift=t(r);
478 cast=t(a);
479 };
250 end) 480 end)
251 package.preload['util.stanza']=(function(...) 481 package.preload['util.stanza']=(function(...)
252 local _ENV=_ENV; 482 local _ENV=_ENV;
253 local function e(t,...) 483 local function e(t,...)
254 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 484 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
261 return e; 491 return e;
262 end 492 end
263 local a=error; 493 local a=error;
264 local o=table.insert; 494 local o=table.insert;
265 local l=table.remove; 495 local l=table.remove;
266 local m=table.concat; 496 local y=table.concat;
267 local d=string.match; 497 local d=string.match;
268 local f=tostring; 498 local p=tostring;
269 local u=setmetatable; 499 local m=setmetatable;
270 local x=getmetatable; 500 local q=getmetatable;
271 local h=pairs; 501 local h=pairs;
272 local s=ipairs; 502 local s=ipairs;
273 local t=type; 503 local t=type;
274 local j=string.gsub; 504 local x=string.gsub;
275 local y=string.sub; 505 local u=string.sub;
276 local r=string.find; 506 local r=string.find;
277 local c=table.move or require"util.table".move; 507 local c=table.move or require"util.table".move;
278 local b=require"util.table".create; 508 local w=require"util.table".create;
279 local v=require"util.encodings".utf8.valid; 509 local i=require"util.encodings".utf8.valid;
280 local q,p=pcall(require,"util.termcolours"); 510 local j,f=pcall(require,"util.termcolours");
281 local g="urn:ietf:params:xml:ns:xmpp-stanzas"; 511 local b="urn:ietf:params:xml:ns:xmpp-stanzas";
282 local k={xmlns=g}; 512 local v={xmlns=b};
283 local _ENV=nil; 513 local _ENV=nil;
284 local e={__name="stanza"}; 514 local e={__name="stanza"};
285 e.__index=e; 515 e.__index=e;
286 local function i(t,e) 516 local function n(e,t)
287 return not r(t,e and"[^\1\9\10\13\20-\255]"or"[^\9\10\13\20-\255]"); 517 return not r(e,t and"[^\1\9\10\13\20-\255]"or"[^\9\10\13\20-\255]");
288 end 518 end
289 local function n(o,e) 519 local function k(o,e)
290 if t(o)~="string"then 520 if t(o)~="string"then
291 a("invalid "..e.." name: expected string, got "..t(o)); 521 a("invalid "..e.." name: expected string, got "..t(o));
292 elseif#o==0 then 522 elseif#o==0 then
293 a("invalid "..e.." name: empty string"); 523 a("invalid "..e.." name: empty string");
294 elseif r(o,"[<>& '\"]")then 524 elseif r(o,"[<>& '\"]")then
295 a("invalid "..e.." name: contains invalid characters"); 525 a("invalid "..e.." name: contains invalid characters");
296 elseif not i(o,e=="attribute")then 526 elseif not n(o,e=="attribute")then
297 a("invalid "..e.." name: contains control characters"); 527 a("invalid "..e.." name: contains control characters");
298 elseif not v(o)then 528 elseif not i(o)then
299 a("invalid "..e.." name: contains invalid utf8"); 529 a("invalid "..e.." name: contains invalid utf8");
300 end 530 end
301 end 531 end
302 local function w(e,o) 532 local function g(e,o)
303 if t(e)~="string"then 533 if t(e)~="string"then
304 a("invalid "..o.." value: expected string, got "..t(e)); 534 a("invalid "..o.." value: expected string, got "..t(e));
305 elseif not i(e,false)then 535 elseif not n(e,false)then
306 a("invalid "..o.." value: contains control characters"); 536 a("invalid "..o.." value: contains control characters");
307 elseif not v(e)then 537 elseif not i(e)then
308 a("invalid "..o.." value: contains invalid utf8"); 538 a("invalid "..o.." value: contains invalid utf8");
309 end 539 end
310 end 540 end
311 local function v(e) 541 local function n(e)
312 if e~=nil then 542 if e~=nil then
313 if t(e)~="table"then 543 if t(e)~="table"then
314 a("invalid attributes: expected table, got "..t(e)); 544 a("invalid attributes: expected table, got "..t(e));
315 end 545 end
316 for t,e in h(e)do 546 for e,t in h(e)do
317 n(t,"attribute"); 547 k(e,"attribute");
318 w(e,"attribute"); 548 g(t,"attribute");
319 end 549 end
320 end 550 end
321 end 551 end
322 local function i(t,a,o) 552 local function i(t,a,o)
323 n(t,"tag"); 553 k(t,"tag");
324 v(a); 554 n(a);
325 local t={name=t,attr=a or{},namespaces=o,tags={}}; 555 local t={name=t,attr=a or{},namespaces=o,tags={}};
326 return u(t,e); 556 return m(t,e);
327 end 557 end
328 local function n(t) 558 local function n(t)
329 return x(t)==e; 559 return q(t)==e;
330 end 560 end
331 function e:query(e) 561 function e:query(e)
332 return self:tag("query",{xmlns=e}); 562 return self:tag("query",{xmlns=e});
333 end 563 end
334 function e:body(t,e) 564 function e:body(t,e)
335 return self:text_tag("body",t,e); 565 return self:text_tag("body",t,e);
336 end 566 end
337 function e:text_tag(e,t,o,a) 567 function e:text_tag(a,o,t,e)
338 return self:tag(e,o,a):text(t):up(); 568 return self:tag(a,t,e):text(o):up();
339 end 569 end
340 function e:tag(t,e,a) 570 function e:tag(e,t,a)
341 local t=i(t,e,a); 571 local t=i(e,t,a);
342 local e=self.last_add; 572 local e=self.last_add;
343 if not e then e={};self.last_add=e;end 573 if not e then e={};self.last_add=e;end
344 (e[#e]or self):add_direct_child(t); 574 (e[#e]or self):add_direct_child(t);
345 o(e,t); 575 o(e,t);
346 return self; 576 return self;
347 end 577 end
348 function e:text(t) 578 function e:text(e)
349 if t~=nil and t~=""then 579 if e~=nil and e~=""then
350 local e=self.last_add; 580 local t=self.last_add;
351 (e and e[#e]or self):add_direct_child(t); 581 (t and t[#t]or self):add_direct_child(e);
352 end 582 end
353 return self; 583 return self;
354 end 584 end
355 function e:up() 585 function e:up()
356 local e=self.last_add; 586 local e=self.last_add;
367 function e:add_direct_child(e) 597 function e:add_direct_child(e)
368 if n(e)then 598 if n(e)then
369 o(self.tags,e); 599 o(self.tags,e);
370 o(self,e); 600 o(self,e);
371 else 601 else
372 w(e,"text"); 602 g(e,"text");
373 o(self,e); 603 o(self,e);
374 end 604 end
375 end 605 end
376 function e:add_child(t) 606 function e:add_child(t)
377 local e=self.last_add; 607 local e=self.last_add;
378 (e and e[#e]or self):add_direct_child(t); 608 (e and e[#e]or self):add_direct_child(t);
379 return self; 609 return self;
380 end 610 end
381 function e:remove_children(a,e) 611 function e:remove_children(a,t)
382 e=e or self.attr.xmlns; 612 t=t or self.attr.xmlns;
383 return self:maptags(function(t) 613 return self:maptags(function(e)
384 if(not a or t.name==a)and t.attr.xmlns==e then 614 if(not a or e.name==a)and e.attr.xmlns==t then
385 return nil; 615 return nil;
386 end 616 end
387 return t; 617 return e;
388 end); 618 end);
389 end 619 end
390 function e:get_child(a,t) 620 function e:get_child(t,a)
391 for o,e in s(self.tags)do 621 for o,e in s(self.tags)do
392 if(not a or e.name==a) 622 if(not t or e.name==t)
393 and((not t and self.attr.xmlns==e.attr.xmlns) 623 and((not a and self.attr.xmlns==e.attr.xmlns)
394 or e.attr.xmlns==t)then 624 or e.attr.xmlns==a)then
395 return e; 625 return e;
396 end 626 end
397 end 627 end
398 return nil; 628 return nil;
399 end 629 end
400 function e:get_child_text(e,t) 630 function e:get_child_text(t,e)
401 local e=self:get_child(e,t); 631 local e=self:get_child(t,e);
402 if e then 632 if e then
403 return e:get_text(); 633 return e:get_text();
404 end 634 end
405 return nil; 635 return nil;
406 end 636 end
407 function e:get_child_attr(e,a,t) 637 function e:get_child_attr(a,e,t)
408 local e=self:get_child(e,a); 638 local e=self:get_child(a,e);
409 if e then 639 if e then
410 return e.attr[t]; 640 return e.attr[t];
411 end 641 end
412 return nil; 642 return nil;
413 end 643 end
421 for a,e in s(self.tags)do 651 for a,e in s(self.tags)do
422 if e.attr.xmlns==t then return e;end 652 if e.attr.xmlns==t then return e;end
423 end 653 end
424 return nil; 654 return nil;
425 end 655 end
426 function e:get_child_with_attr(e,i,t,o,a) 656 function e:get_child_with_attr(e,o,a,i,t)
427 for e in self:childtags(e,i)do 657 for e in self:childtags(e,o)do
428 if(a and a(e.attr[t])or e.attr[t])==o then 658 if(t and t(e.attr[a])or e.attr[a])==i then
429 return e; 659 return e;
430 end 660 end
431 end 661 end
432 return nil; 662 return nil;
433 end 663 end
436 return function(t) 666 return function(t)
437 e=e+1 667 e=e+1
438 return t[e]; 668 return t[e];
439 end,self,e; 669 end,self,e;
440 end 670 end
441 function e:childtags(o,a) 671 function e:childtags(t,a)
442 local e=self.tags; 672 local e=self.tags;
443 local i,t=1,#e; 673 local i,o=1,#e;
444 return function() 674 return function()
445 for t=i,t do 675 for o=i,o do
446 local e=e[t]; 676 local e=e[o];
447 if(not o or e.name==o) 677 if(not t or e.name==t)
448 and((not a and self.attr.xmlns==e.attr.xmlns) 678 and((not a and self.attr.xmlns==e.attr.xmlns)
449 or e.attr.xmlns==a)then 679 or e.attr.xmlns==a)then
450 i=t+1; 680 i=o+1;
451 return e; 681 return e;
452 end 682 end
453 end 683 end
454 end; 684 end;
455 end 685 end
456 function e:maptags(h) 686 function e:maptags(h)
457 local o,t=self.tags,1; 687 local i,t=self.tags,1;
458 local n,i=#self,#o; 688 local n,o=#self,#i;
459 local s=n+1; 689 local s=n+1;
460 local e=1; 690 local e=1;
461 while t<=i and i>0 do 691 while t<=o and o>0 do
462 if self[e]==o[t]then 692 if self[e]==i[t]then
463 local a=h(self[e]); 693 local a=h(self[e]);
464 if a==nil then 694 if a==nil then
465 l(self,e); 695 l(self,e);
466 l(o,t); 696 l(i,t);
467 n=n-1; 697 n=n-1;
468 i=i-1; 698 o=o-1;
469 e=e-1; 699 e=e-1;
470 t=t-1; 700 t=t-1;
471 else 701 else
472 self[e]=a; 702 self[e]=a;
473 o[t]=a; 703 i[t]=a;
474 end 704 end
475 t=t+1; 705 t=t+1;
476 end 706 end
477 e=e+1; 707 e=e+1;
478 if e>s then 708 if e>s then
484 function e:find(a) 714 function e:find(a)
485 local e=1; 715 local e=1;
486 local s=#a+1; 716 local s=#a+1;
487 repeat 717 repeat
488 local o,t,i; 718 local o,t,i;
489 local n=y(a,e,e); 719 local n=u(a,e,e);
490 if n=="@"then 720 if n=="@"then
491 return self.attr[y(a,e+1)]; 721 return self.attr[u(a,e+1)];
492 elseif n=="{"then 722 elseif n=="{"then
493 o,e=d(a,"^([^}]+)}()",e+1); 723 o,e=d(a,"^([^}]+)}()",e+1);
494 end 724 end
495 t,i,e=d(a,"^([^@/#]*)([/#]?)()",e); 725 t,i,e=d(a,"^([^@/#]*)([/#]?)()",e);
496 t=t~=""and t or nil; 726 t=t~=""and t or nil;
501 return self:get_child(t,o); 731 return self:get_child(t,o);
502 end 732 end
503 self=self:get_child(t,o); 733 self=self:get_child(t,o);
504 until not self 734 until not self
505 end 735 end
506 local function l(t,s) 736 local function u(t,s)
507 local n={}; 737 local n={};
508 for t,e in h(t.attr)do n[t]=e;end 738 for t,e in h(t.attr)do n[t]=e;end
509 local a,i=t.namespaces; 739 local a,i=t.namespaces;
510 if a then 740 if a then
511 i={}; 741 i={};
514 local o,a; 744 local o,a;
515 if s then 745 if s then
516 o={}; 746 o={};
517 a={name=t.name,attr=n,namespaces=i,tags=o}; 747 a={name=t.name,attr=n,namespaces=i,tags=o};
518 else 748 else
519 o=b(#t.tags,0); 749 o=w(#t.tags,0);
520 a=b(#t,4); 750 a=w(#t,4);
521 a.name=t.name; 751 a.name=t.name;
522 a.attr=n; 752 a.attr=n;
523 a.namespaces=i; 753 a.namespaces=i;
524 a.tags=o; 754 a.tags=o;
525 end 755 end
526 u(a,e); 756 m(a,e);
527 if not s then 757 if not s then
528 c(t,1,#t,1,a); 758 c(t,1,#t,1,a);
529 c(t.tags,1,#t.tags,1,o); 759 c(t.tags,1,#t.tags,1,o);
530 a:maptags(l); 760 a:maptags(u);
531 end 761 end
532 return a; 762 return a;
533 end 763 end
534 local function u(e,o) 764 local function l(e,o)
535 if not n(e)then 765 if not n(e)then
536 a("bad argument to clone: expected stanza, got "..t(e)); 766 a("bad argument to clone: expected stanza, got "..t(e));
537 end 767 end
538 return l(e,o); 768 return u(e,o);
539 end 769 end
540 local l={["'"]="&apos;",["\""]="&quot;",["<"]="&lt;",[">"]="&gt;",["&"]="&amp;"}; 770 local u={["'"]="&apos;",["\""]="&quot;",["<"]="&lt;",[">"]="&gt;",["&"]="&amp;"};
541 local function c(e)return(j(e,"['&<>\"]",l));end 771 local function m(e)return(x(e,"['&<>\"]",u));end
542 local function l(a,e,s,t,u) 772 local function u(a,e,l,t,u)
543 local i=0; 773 local i=0;
544 local l=a.name 774 local s=a.name
545 o(e,"<"..l); 775 o(e,"<"..s);
546 for a,n in h(a.attr)do 776 for a,n in h(a.attr)do
547 if r(a,"\1",1,true)then 777 if r(a,"\1",1,true)then
548 local s,a=d(a,"^([^\1]*)\1?(.*)$"); 778 local s,a=d(a,"^([^\1]*)\1?(.*)$");
549 i=i+1; 779 i=i+1;
550 o(e," xmlns:ns"..i.."='"..t(s).."' ".."ns"..i..":"..a.."='"..t(n).."'"); 780 o(e," xmlns:ns"..i.."='"..t(s).."' ".."ns"..i..":"..a.."='"..t(n).."'");
558 else 788 else
559 o(e,">"); 789 o(e,">");
560 for i=1,i do 790 for i=1,i do
561 local i=a[i]; 791 local i=a[i];
562 if i.name then 792 if i.name then
563 s(i,e,s,t,a.attr.xmlns); 793 l(i,e,l,t,a.attr.xmlns);
564 else 794 else
565 o(e,t(i)); 795 o(e,t(i));
566 end 796 end
567 end 797 end
568 o(e,"</"..l..">"); 798 o(e,"</"..s..">");
569 end 799 end
570 end 800 end
571 function e.__tostring(t) 801 function e.__tostring(t)
572 local e={}; 802 local e={};
573 l(t,e,l,c,nil); 803 u(t,e,u,m,nil);
574 return m(e); 804 return y(e);
575 end 805 end
576 function e.top_tag(e) 806 function e.top_tag(e)
577 local e=u(e,true); 807 local e=l(e,true);
578 return f(e):sub(1,-3)..">"; 808 return p(e):sub(1,-3)..">";
579 end 809 end
580 function e.get_text(e) 810 function e.get_text(e)
581 if#e.tags==0 then 811 if#e.tags==0 then
582 return m(e); 812 return y(e);
583 end 813 end
584 return nil; 814 return nil;
585 end 815 end
586 function e.get_error(e) 816 function e.get_error(e)
587 local i,t,a,o; 817 local i,t,a,o;
589 if not e then 819 if not e then
590 return nil,nil,nil,nil; 820 return nil,nil,nil,nil;
591 end 821 end
592 i=e.attr.type; 822 i=e.attr.type;
593 for i,e in s(e.tags)do 823 for i,e in s(e.tags)do
594 if e.attr.xmlns==g then 824 if e.attr.xmlns==b then
595 if not a and e.name=="text"then 825 if not a and e.name=="text"then
596 a=e:get_text(); 826 a=e:get_text();
597 elseif not t then 827 elseif not t then
598 t=e.name; 828 t=e.name;
599 end 829 end
617 end 847 end
618 if o.attr.from==i then 848 if o.attr.from==i then
619 i=nil; 849 i=nil;
620 end 850 end
621 o:tag("error",{type=a,by=i}) 851 o:tag("error",{type=a,by=i})
622 :tag(s,k); 852 :tag(s,v);
623 if e and s=="gone"and t(e.uri)=="string"then 853 if e and s=="gone"and t(e.uri)=="string"then
624 o:text(e.uri); 854 o:text(e.uri);
625 end 855 end
626 o:up(); 856 o:up();
627 if h then o:text_tag("text",h,k);end 857 if h then o:text_tag("text",h,v);end
628 if e and n(e.tag)then 858 if e and n(e.tag)then
629 o:add_child(e.tag); 859 o:add_child(e.tag);
630 elseif e and e.namespace and e.condition then 860 elseif e and e.namespace and e.condition then
631 o:tag(e.condition,{xmlns=e.namespace}):up(); 861 o:tag(e.condition,{xmlns=e.namespace}):up();
632 end 862 end
633 return o:up(); 863 return o:up();
634 end 864 end
635 local function l(a) 865 local function u(i)
636 local i={name=a.name,attr=a.attr}; 866 local a={name=i.name,attr=i.attr};
637 for a,e in s(a)do 867 for i,e in s(i)do
638 if t(e)=="table"then 868 if t(e)=="table"then
639 o(i,l(e)); 869 o(a,u(e));
640 else 870 else
641 o(i,e); 871 o(a,e);
642 end 872 end
643 end 873 end
644 return i; 874 return a;
645 end 875 end
646 e.__freeze=l; 876 e.__freeze=u;
647 local function m(a) 877 local function c(a)
648 if a then 878 if a then
649 local e=a.attr; 879 local e=a.attr;
650 local o={}; 880 local o={};
651 for e,a in h(e)do 881 for e,a in h(e)do
652 if t(e)=="string"then 882 if t(e)=="string"then
659 end 889 end
660 end 890 end
661 local o=i(a.name,o); 891 local o=i(a.name,o);
662 for a,e in s(a)do 892 for a,e in s(a)do
663 if t(e)=="table"then 893 if t(e)=="table"then
664 o:add_direct_child(m(e)); 894 o:add_direct_child(c(e));
665 elseif t(e)=="string"then 895 elseif t(e)=="string"then
666 o:add_direct_child(e); 896 o:add_direct_child(e);
667 end 897 end
668 end 898 end
669 return o; 899 return o;
670 end 900 end
671 end 901 end
672 local function y(e,t) 902 local function v(t,e)
673 if not t then 903 if not e then
674 return i("message",e); 904 return i("message",t);
675 else 905 else
676 return i("message",e):text_tag("body",t); 906 return i("message",t):text_tag("body",e);
677 end 907 end
678 end 908 end
679 local function v(e) 909 local function w(e)
680 if not e then 910 if not e then
681 a("iq stanzas require id and type attributes"); 911 a("iq stanzas require id and type attributes");
682 end 912 end
683 if not e.id then 913 if not e.id then
684 a("iq stanzas require an id attribute"); 914 a("iq stanzas require an id attribute");
686 if not e.type then 916 if not e.type then
687 a("iq stanzas require a type attribute"); 917 a("iq stanzas require a type attribute");
688 end 918 end
689 return i("iq",e); 919 return i("iq",e);
690 end 920 end
691 local function r(e) 921 local function h(e)
692 if not n(e)then 922 if not n(e)then
693 a("bad argument to reply: expected stanza, got "..t(e)); 923 a("bad argument to reply: expected stanza, got "..t(e));
694 end 924 end
695 return i(e.name, 925 return i(e.name,
696 { 926 {
698 from=e.attr.to, 928 from=e.attr.to,
699 id=e.attr.id, 929 id=e.attr.id,
700 type=((e.name=="iq"and"result")or e.attr.type) 930 type=((e.name=="iq"and"result")or e.attr.type)
701 }); 931 });
702 end 932 end
703 local function w(e,s,h,i,o) 933 local function y(e,r,o,i,s)
704 if not n(e)then 934 if not n(e)then
705 a("bad argument to error_reply: expected stanza, got "..t(e)); 935 a("bad argument to error_reply: expected stanza, got "..t(e));
706 elseif e.attr.type=="error"then 936 elseif e.attr.type=="error"then
707 a("bad argument to error_reply: got stanza of type error which must not be replied to"); 937 a("bad argument to error_reply: got stanza of type error which must not be replied to");
708 end 938 end
709 local e=r(e); 939 local e=h(e);
710 e.attr.type="error"; 940 e.attr.type="error";
711 e:add_error(s,h,i,o); 941 e:add_error(r,o,i,s);
712 e.last_add={e[1]}; 942 e.last_add={e[1]};
713 return e; 943 return e;
714 end 944 end
715 local function d(e) 945 local function d(e)
716 return i("presence",e); 946 return i("presence",e);
717 end 947 end
718 local s; 948 local s;
719 if q then 949 if j then
720 local a,t=p.getstyle,p.getstring; 950 local a,t=f.getstyle,f.getstring;
721 local n=a("1b3967"); 951 local h=a("1b3967");
722 local r=a("13b5ea"); 952 local r=a("13b5ea");
723 local o=a("439639"); 953 local o=a("439639");
724 local i=a("a0ce67"); 954 local n=a("a0ce67");
725 local h=a("d9541e"); 955 local i=a("d9541e");
726 local a=a("e96d1f"); 956 local a=a("e96d1f");
727 local h=( 957 local a=(
728 t(i,"%1").. 958 t(n,"%1")..
729 t(o,"%2").. 959 t(o,"%2")..
730 t(h,"%3").. 960 t(i,"%3")..
731 t(a,"%4").. 961 t(a,"%4")..
732 t(h,"%5") 962 t(i,"%5")
733 ); 963 );
734 local d=( 964 local o=(
735 t(o,"%1").. 965 t(o,"%1")..
736 t(i,"%2").. 966 t(n,"%2")..
737 t(o,"%3") 967 t(o,"%3")
738 ); 968 );
739 function s(e) 969 function s(e)
740 return(e:gsub("(<[?/]?)([^ >/?]*)(.-)([?/]?>)([^<]*)",function(o,e,a,i,s) 970 return(e:gsub("(<[?/]?)([^ >/?]*)(.-)([?/]?>)([^<]*)",function(e,i,s,n,d)
741 return t(n,o)..t(r,e).. 971 return t(h,e)..t(r,i)..
742 a:gsub("([^=]+)(=)([\"'])(.-)([\"'])",h).. 972 s:gsub("([^=]+)(=)([\"'])(.-)([\"'])",a)..
743 t(n,i).. 973 t(h,n)..
744 s:gsub("(&#?)(%w+)(;)",d); 974 d:gsub("(&#?)(%w+)(;)",o);
745 end,100)); 975 end,100));
746 end 976 end
747 function e.pretty_print(e) 977 function e.pretty_print(e)
748 return s(f(e)); 978 return s(p(e));
749 end 979 end
750 function e.pretty_top_tag(e) 980 function e.pretty_top_tag(e)
751 return s(e:top_tag()); 981 return s(e:top_tag());
752 end 982 end
753 else 983 else
754 e.pretty_print=e.__tostring; 984 e.pretty_print=e.__tostring;
755 e.pretty_top_tag=e.top_tag; 985 e.pretty_top_tag=e.top_tag;
756 end 986 end
757 function e.indent(i,o,a) 987 function e.indent(i,e,a)
758 if#i==0 or(#i==1 and t(i[1])=="string")then 988 if#i==0 or(#i==1 and t(i[1])=="string")then
759 return i; 989 return i;
760 end 990 end
761 a=a or"\t"; 991 a=a or"\t";
762 o=o or 1; 992 e=e or 1;
763 local e=u(i,true); 993 local o=l(i,true);
764 for i in i:children()do 994 for i in i:children()do
765 if t(i)=="string"then 995 if t(i)=="string"then
766 if i:find("%S")then 996 if i:find("%S")then
767 e:text("\n"..a:rep(o)); 997 o:text("\n"..a:rep(e));
768 e:text(i); 998 o:text(i);
769 end 999 end
770 elseif n(i)then 1000 elseif n(i)then
771 e:text("\n"..a:rep(o)); 1001 o:text("\n"..a:rep(e));
772 e:add_direct_child(i:indent(o+1,a)); 1002 o:add_direct_child(i:indent(e+1,a));
773 end 1003 end
774 end 1004 end
775 e:text("\n"..a:rep((o-1))); 1005 o:text("\n"..a:rep((e-1)));
776 return e; 1006 return o;
777 end 1007 end
778 return{ 1008 return{
779 stanza_mt=e; 1009 stanza_mt=e;
780 stanza=i; 1010 stanza=i;
781 is_stanza=n; 1011 is_stanza=n;
782 preserialize=l; 1012 preserialize=u;
783 deserialize=m; 1013 deserialize=c;
784 clone=u; 1014 clone=l;
785 message=y; 1015 message=v;
786 iq=v; 1016 iq=w;
787 reply=r; 1017 reply=h;
788 error_reply=w; 1018 error_reply=y;
789 presence=d; 1019 presence=d;
790 xml_escape=c; 1020 xml_escape=m;
791 pretty_print=s; 1021 pretty_print=s;
792 }; 1022 };
793 end) 1023 end)
794 package.preload['util.timer']=(function(...) 1024 package.preload['util.timer']=(function(...)
795 local _ENV=_ENV; 1025 local _ENV=_ENV;
805 end 1035 end
806 local a=require"util.indexedbheap"; 1036 local a=require"util.indexedbheap";
807 local t=require"util.logger".init("timer"); 1037 local t=require"util.logger".init("timer");
808 local e=require"net.server"; 1038 local e=require"net.server";
809 local d=require"util.time".now 1039 local d=require"util.time".now
810 local l=type; 1040 local m=type;
811 local h=debug.traceback; 1041 local i=debug.traceback;
812 local i=tostring; 1042 local r=tostring;
813 local c=require"util.xpcall".xpcall; 1043 local f=require"util.xpcall".xpcall;
814 local f=math.max; 1044 local c=math.max;
815 local m=pairs; 1045 local u=pairs;
816 if e.timer then 1046 if e.timer then
817 return e.timer; 1047 return e.timer;
818 end 1048 end
819 local _ENV=nil; 1049 local _ENV=nil;
820 local r=e.add_task; 1050 local h=e.add_task;
821 local o; 1051 local o;
822 local s=0; 1052 local s=0;
823 local a=a.create(); 1053 local a=a.create();
824 local n={}; 1054 local n={};
825 local e=nil; 1055 local e=nil;
826 local function u(e)t("error","Traceback[timer]: %s",h(i(e),2));end 1056 local function l(e)t("error","Traceback[timer]: %s",i(r(e),2));end
827 local function h(i) 1057 local function r(o)
828 local t; 1058 local t;
829 local o; 1059 local i;
830 while true do 1060 while true do
831 t=a:peek(); 1061 t=a:peek();
832 if t==nil or t>i then break;end 1062 if t==nil or t>o then break;end
833 local t,a,e=a:pop(); 1063 local a,t,e=a:pop();
834 local s=n[e]; 1064 local s=n[e];
835 n[e]=nil; 1065 n[e]=nil;
836 local h,t=c(a,u,i,e,s); 1066 local h,a=f(t,l,o,e,s);
837 if h and l(t)=="number"then 1067 if h and m(a)=="number"then
838 if o then 1068 if i then
839 o[e]={a,t+i}; 1069 i[e]={t,a+o};
840 else 1070 else
841 o={[e]={a,t+i}}; 1071 i={[e]={t,a+o}};
842 end 1072 end
843 n[e]=s; 1073 n[e]=s;
844 end 1074 end
845 end 1075 end
846 if o then 1076 if i then
847 for o,e in m(o)do 1077 for o,e in u(i)do
848 a:insert(e[1],e[2],o); 1078 a:insert(e[1],e[2],o);
849 end 1079 end
850 t=a:peek(); 1080 t=a:peek();
851 end 1081 end
852 if t~=nil and s>1 and t==e then 1082 if t~=nil and s>1 and t==e then
853 t=nil; 1083 t=nil;
854 else 1084 else
855 e=t; 1085 e=t;
856 end 1086 end
857 if t then 1087 if t then
858 return t-i; 1088 return t-o;
859 end 1089 end
860 s=s-1; 1090 s=s-1;
861 end 1091 end
862 local function u(t,u,l) 1092 local function l(t,l,u)
863 local i=d(); 1093 local i=d();
864 local t=i+t; 1094 local t=i+t;
865 local a=a:insert(u,t); 1095 local a=a:insert(l,t);
866 n[a]=l; 1096 n[a]=u;
867 if e==nil or t<e then 1097 if e==nil or t<e then
868 e=t; 1098 e=t;
869 if o then 1099 if o then
870 o:close(); 1100 o:close();
871 o=nil; 1101 o=nil;
872 else 1102 else
873 s=s+1; 1103 s=s+1;
874 end 1104 end
875 o=r(e-i,h); 1105 o=h(e-i,r);
876 end 1106 end
877 return a; 1107 return a;
878 end 1108 end
879 local function l(t) 1109 local function u(t)
880 n[t]=nil; 1110 n[t]=nil;
881 local s,i,n=a:remove(t); 1111 local s,n,i=a:remove(t);
882 local t=a:peek(); 1112 local t=a:peek();
883 if t~=e and o then 1113 if t~=e and o then
884 e=t; 1114 e=t;
885 o:close(); 1115 o:close();
886 if e~=nil then 1116 if e~=nil then
887 o=r(f(e-d(),0),h); 1117 o=h(c(e-d(),0),r);
888 end 1118 end
889 end 1119 end
890 return s,i,n; 1120 return s,n,i;
891 end 1121 end
892 local function s(o,n) 1122 local function s(o,i)
893 local i=d(); 1123 local n=d();
894 local t=i+n; 1124 local t=n+i;
895 a:reprioritize(o,n); 1125 a:reprioritize(o,i);
896 if e==nil or t<e then 1126 if e==nil or t<e then
897 e=t; 1127 e=t;
898 r(e-i,h); 1128 h(e-n,r);
899 end 1129 end
900 return o; 1130 return o;
901 end 1131 end
902 return{ 1132 return{
903 add_task=u; 1133 add_task=l;
904 stop=l; 1134 stop=u;
905 reschedule=s; 1135 reschedule=s;
906 }; 1136 };
907 end) 1137 end)
908 package.preload['util.termcolours']=(function(...) 1138 package.preload['util.termcolours']=(function(...)
909 local _ENV=_ENV; 1139 local _ENV=_ENV;
915 end 1145 end
916 _ENV=e; 1146 _ENV=e;
917 _M=e; 1147 _M=e;
918 return e; 1148 return e;
919 end 1149 end
920 local s,h=table.concat,table.insert; 1150 local r,h=table.concat,table.insert;
921 local a,r=string.char,string.format; 1151 local t,s=string.char,string.format;
922 local o=tonumber; 1152 local o=tonumber;
923 local m=ipairs; 1153 local c=ipairs;
924 local u=io.write; 1154 local m=io.write;
925 local i=math.floor; 1155 local i=math.floor;
926 local l=type; 1156 local u=type;
927 local d=setmetatable; 1157 local d=setmetatable;
928 local c=pairs; 1158 local l=pairs;
929 local t; 1159 local a;
930 if os.getenv("WINDIR")then 1160 if os.getenv("WINDIR")then
931 t=require"util.windows"; 1161 a=require"util.windows";
932 end 1162 end
933 local n=t and t.get_consolecolor and t.get_consolecolor(); 1163 local n=a and a.get_consolecolor and a.get_consolecolor();
934 local _ENV=nil; 1164 local _ENV=nil;
935 local e={ 1165 local e={
936 reset=0;bright=1,dim=2,underscore=4,blink=5,reverse=7,hidden=8; 1166 reset=0;bright=1,dim=2,underscore=4,blink=5,reverse=7,hidden=8;
937 black=30;red=31;green=32;yellow=33;blue=34;magenta=35;cyan=36;white=37; 1167 black=30;red=31;green=32;yellow=33;blue=34;magenta=35;cyan=36;white=37;
938 ["black background"]=40;["red background"]=41;["green background"]=42;["yellow background"]=43; 1168 ["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; 1169 ["blue background"]=44;["magenta background"]=45;["cyan background"]=46;["white background"]=47;
940 bold=1,dark=2,underline=4,underlined=4,normal=0; 1170 bold=1,dark=2,underline=4,underlined=4,normal=0;
941 } 1171 }
942 local w={ 1172 local y={
943 ["0"]=n, 1173 ["0"]=n,
944 ["1"]=7+8, 1174 ["1"]=7+8,
945 ["1;33"]=2+4+8, 1175 ["1;33"]=2+4+8,
946 ["1;31"]=4+8 1176 ["1;31"]=4+8
947 } 1177 }
948 local y={ 1178 local f={
949 [1]="font-weight: bold",[2]="opacity: 0.5",[4]="text-decoration: underline",[8]="visibility: hidden", 1179 [1]="font-weight: bold",[2]="opacity: 0.5",[4]="text-decoration: underline",[8]="visibility: hidden",
950 [30]="color:black",[31]="color:red",[32]="color:green",[33]="color:#FFD700", 1180 [30]="color:black",[31]="color:red",[32]="color:green",[33]="color:#FFD700",
951 [34]="color:blue",[35]="color: magenta",[36]="color:cyan",[37]="color: white", 1181 [34]="color:blue",[35]="color: magenta",[36]="color:cyan",[37]="color: white",
952 [40]="background-color:black",[41]="background-color:red",[42]="background-color:green", 1182 [40]="background-color:black",[41]="background-color:red",[42]="background-color:green",
953 [43]="background-color:yellow",[44]="background-color:blue",[45]="background-color: magenta", 1183 [43]="background-color:yellow",[44]="background-color:blue",[45]="background-color: magenta",
954 [46]="background-color:cyan",[47]="background-color: white"; 1184 [46]="background-color:cyan",[47]="background-color: white";
955 }; 1185 };
956 local a=a(27).."[%sm%s"..a(27).."[0m"; 1186 local w=t(27).."[%sm%s"..t(27).."[0m";
957 local function p(e,t) 1187 local function p(t,e)
958 if e then 1188 if t then
959 return r(a,e,t); 1189 return s(w,t,e);
960 else 1190 else
961 return t; 1191 return e;
962 end 1192 end
963 end 1193 end
964 local function v(e) 1194 local function w(e)
965 return i(e*3/32)+232; 1195 return i(e*3/32)+232;
966 end 1196 end
967 local function f(t,e,a) 1197 local function v(e,t,a)
968 if t==e and e==a then 1198 if e==t and t==a then
969 return v(t); 1199 return w(e);
970 end 1200 end
1201 e=i(e*3/128);
971 t=i(t*3/128); 1202 t=i(t*3/128);
972 e=i(e*3/128);
973 a=i(a*3/128); 1203 a=i(a*3/128);
974 return 16+(t*36)+(e*6)+(a); 1204 return 16+(e*36)+(t*6)+(a);
975 end 1205 end
976 local function i(e) 1206 local function t(e)
977 local a=o(e:sub(1,2),16); 1207 local t=o(e:sub(1,2),16);
978 local t=o(e:sub(3,4),16); 1208 local a=o(e:sub(3,4),16);
979 local e=o(e:sub(5,6),16); 1209 local e=o(e:sub(5,6),16);
980 return a,t,e; 1210 return t,a,e;
981 end 1211 end
982 d(e,{__index=function(t,e) 1212 d(e,{__index=function(a,e)
983 if l(e)=="string"and e:find("%x%x%x%x%x%x")==1 then 1213 if u(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;"; 1214 local a=e:sub(7)==" background"and"48;5;"or"38;5;";
985 return r("%s%d",t,f(i(e))); 1215 return s("%s%d",a,v(t(e)));
986 end 1216 end
987 end}); 1217 end});
988 local a={ 1218 local t={
989 red="ff0000";fuchsia="ff00ff";green="008000";white="ffffff"; 1219 red="ff0000";fuchsia="ff00ff";green="008000";white="ffffff";
990 lime="00ff00";yellow="ffff00";purple="800080";blue="0000ff"; 1220 lime="00ff00";yellow="ffff00";purple="800080";blue="0000ff";
991 aqua="00ffff";olive="808000";black="000000";navy="000080"; 1221 aqua="00ffff";olive="808000";black="000000";navy="000080";
992 teal="008080";silver="c0c0c0";maroon="800000";gray="808080"; 1222 teal="008080";silver="c0c0c0";maroon="800000";gray="808080";
993 } 1223 }
994 for t,a in c(a)do 1224 for t,a in l(t)do
995 e[t]=e[t]or e[a]; 1225 e[t]=e[t]or e[a];
996 t,a=t.." background",a.." background" 1226 t,a=t.." background",a.." background"
997 e[t]=e[t]or e[a]; 1227 e[t]=e[t]or e[a];
998 end 1228 end
999 local function r(...) 1229 local function s(...)
1000 local t,a={...},{}; 1230 local t,a={...},{};
1001 for o,t in m(t)do 1231 for o,t in c(t)do
1002 t=e[t]; 1232 t=e[t];
1003 if t then 1233 if t then
1004 h(a,t); 1234 h(a,t);
1005 end 1235 end
1006 end 1236 end
1007 return s(a,";"); 1237 return r(a,";");
1008 end 1238 end
1009 local a="0"; 1239 local t="0";
1010 local function i(e) 1240 local function i(e)
1011 e=e or"0"; 1241 e=e or"0";
1012 if e~=a then 1242 if e~=t then
1013 u("\27["..e.."m"); 1243 m("\27["..e.."m");
1014 a=e; 1244 t=e;
1015 end 1245 end
1016 end 1246 end
1017 if t then 1247 if a then
1018 function i(e) 1248 function i(e)
1019 e=e or"0"; 1249 e=e or"0";
1020 if e~=a then 1250 if e~=t then
1021 t.set_consolecolor(w[e]or n); 1251 a.set_consolecolor(y[e]or n);
1022 a=e; 1252 t=e;
1023 end 1253 end
1024 end 1254 end
1025 if not n then 1255 if not n then
1026 function i()end 1256 function i()end
1027 end 1257 end
1028 end 1258 end
1029 local function a(t) 1259 local function a(t)
1030 if t=="0"then return"</span>";end 1260 if t=="0"then return"</span>";end
1031 local e={}; 1261 local e={};
1032 for t in t:gmatch("[^;]+")do 1262 for t in t:gmatch("[^;]+")do
1033 h(e,y[o(t)]); 1263 h(e,f[o(t)]);
1034 end 1264 end
1035 return"</span><span style='"..s(e,";").."'>"; 1265 return"</span><span style='"..r(e,";").."'>";
1036 end 1266 end
1037 local function t(e) 1267 local function t(e)
1038 return e:gsub("\027%[(.-)m",a); 1268 return e:gsub("\027%[(.-)m",a);
1039 end 1269 end
1040 return{ 1270 return{
1041 getstring=p; 1271 getstring=p;
1042 getstyle=r; 1272 getstyle=s;
1043 setstyle=i; 1273 setstyle=i;
1044 tohtml=t; 1274 tohtml=t;
1045 }; 1275 };
1046 end) 1276 end)
1047 package.preload['util.uuid']=(function(...) 1277 package.preload['util.uuid']=(function(...)
1054 end 1284 end
1055 _ENV=e; 1285 _ENV=e;
1056 _M=e; 1286 _M=e;
1057 return e; 1287 return e;
1058 end 1288 end
1059 local a=require"util.random"; 1289 local t=require"util.random";
1060 local t=a.bytes; 1290 local a=t.bytes;
1061 local i=require"util.hex".encode; 1291 local o=require"util.hex".encode;
1062 local o=math.ceil; 1292 local i=math.ceil;
1063 local function e(e) 1293 local function e(e)
1064 return i(t(o(e/2))):sub(1,e); 1294 return o(a(i(e/2))):sub(1,e);
1065 end 1295 end
1066 local function o() 1296 local function o()
1067 return("%x"):format(t(1):byte()%4+8); 1297 return("%x"):format(a(1):byte()%4+8);
1068 end 1298 end
1069 local function t() 1299 local function a()
1070 return e(8).."-"..e(4).."-4"..e(3).."-"..(o())..e(3).."-"..e(12); 1300 return e(8).."-"..e(4).."-4"..e(3).."-"..(o())..e(3).."-"..e(12);
1071 end 1301 end
1072 return{ 1302 return{
1073 get_nibbles=e; 1303 get_nibbles=e;
1074 generate=t; 1304 generate=a;
1075 seed=a.seed; 1305 seed=t.seed;
1076 }; 1306 };
1077 end) 1307 end)
1078 package.preload['util.time']=(function(...) 1308 package.preload['util.time']=(function(...)
1079 local _ENV=_ENV; 1309 local _ENV=_ENV;
1080 local function e(t,...) 1310 local function e(t,...)
1085 end 1315 end
1086 _ENV=e; 1316 _ENV=e;
1087 _M=e; 1317 _M=e;
1088 return e; 1318 return e;
1089 end 1319 end
1090 local o=require"socket".gettime; 1320 local a=require"socket".gettime;
1091 local e; 1321 local e;
1092 local t,a=pcall(require,"system"); 1322 local t,o=pcall(require,"system");
1093 if t then 1323 if t then
1094 e=a.monotime; 1324 e=o.monotime;
1095 end 1325 end
1096 return{ 1326 return{
1097 now=o; 1327 now=a;
1098 monotonic=e; 1328 monotonic=e;
1099 } 1329 }
1100 end) 1330 end)
1101 package.preload['util.envload']=(function(...) 1331 package.preload['util.envload']=(function(...)
1102 local _ENV=_ENV; 1332 local _ENV=_ENV;
1109 _ENV=e; 1339 _ENV=e;
1110 _M=e; 1340 _M=e;
1111 return e; 1341 return e;
1112 end 1342 end
1113 local t=load; 1343 local t=load;
1114 local i=io.open; 1344 local n=io.open;
1115 local function s(o,a,e) 1345 local function s(o,a,e)
1116 return t(o,a,nil,e); 1346 return t(o,a,nil,e);
1117 end 1347 end
1118 local function h(a,o) 1348 local function i(a,i)
1119 local e,i,n=i(a); 1349 local e,n,o=n(a);
1120 if not e then return e,i,n;end 1350 if not e then return e,n,o;end
1121 local a,t=t(e:lines(2048),"@"..a,nil,o); 1351 local a,t=t(e:lines(2048),"@"..a,nil,i);
1122 e:close(); 1352 e:close();
1123 return a,t; 1353 return a,t;
1124 end 1354 end
1125 return{envload=s,envloadfile=h}; 1355 return{envload=s,envloadfile=i};
1126 end) 1356 end)
1127 package.preload['util.id']=(function(...) 1357 package.preload['util.id']=(function(...)
1128 local _ENV=_ENV; 1358 local _ENV=_ENV;
1129 local function e(t,...) 1359 local function e(t,...)
1130 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 1360 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
1134 end 1364 end
1135 _ENV=e; 1365 _ENV=e;
1136 _M=e; 1366 _M=e;
1137 return e; 1367 return e;
1138 end 1368 end
1139 local t=string.gsub; 1369 local o=string.gsub;
1140 local o=require"util.random".bytes; 1370 local t=require"util.random".bytes;
1141 local a=require"util.encodings".base64.encode; 1371 local a=require"util.encodings".base64.encode;
1142 local i={["+"]="-",["/"]="_",["="]=""}; 1372 local i={["+"]="-",["/"]="_",["="]=""};
1143 local function e(e) 1373 local function e(e)
1144 return(t(a(o(e)),"[+/=]",i)); 1374 return(o(a(t(e)),"[+/=]",i));
1145 end 1375 end
1146 return{ 1376 return{
1147 tiny=function()return e(3);end; 1377 tiny=function()return e(3);end;
1148 short=function()return e(9);end; 1378 short=function()return e(9);end;
1149 medium=function()return e(18);end; 1379 medium=function()return e(18);end;
1163 end 1393 end
1164 _ENV=e; 1394 _ENV=e;
1165 _M=e; 1395 _M=e;
1166 return e; 1396 return e;
1167 end 1397 end
1168 local _=getmetatable; 1398 local u=getmetatable;
1169 local t,a=next,type; 1399 local e,o=next,type;
1170 local n=string.format; 1400 local n=string.format;
1171 local z=string.gsub; 1401 local p=string.gsub;
1172 local c=string.rep; 1402 local y=string.rep;
1173 local o=string.char; 1403 local a=string.char;
1174 local k=string.match; 1404 local g=string.match;
1175 local q=table.concat; 1405 local H=table.concat;
1176 local g=require"util.hex".to; 1406 local S=require"util.hex".to;
1177 local p=pcall; 1407 local N=pcall;
1178 local v=require"util.envload".envload; 1408 local v=require"util.envload".envload;
1179 if not math.type then 1409 if not math.type then
1180 require"util.mathcompat" 1410 require"util.mathcompat"
1181 end 1411 end
1182 local b,j=math.huge,-math.huge; 1412 local z,A=math.huge,-math.huge;
1183 local x=math.type; 1413 local j=math.type;
1184 local function h(e) 1414 local function l(t)
1185 return t,e,nil; 1415 return e,t,nil;
1186 end 1416 end
1187 local function s(t,e) 1417 local function h(t,e)
1188 error("Can't serialize "..a(t)..(e and": "..e or"")); 1418 error("Can't serialize "..o(t)..(e and": "..e or""));
1189 end 1419 end
1190 local function t(e,t) 1420 local function t(t,e)
1191 return n("{__type=%q,__error=%q}",a(e),t or"fail"); 1421 return n("{__type=%q,__error=%q}",o(t),e or"fail");
1192 end 1422 end
1193 local r={ 1423 local r={
1194 ['\a']=[[\a]];['\b']=[[\b]]; 1424 ['\a']=[[\a]];['\b']=[[\b]];
1195 ['\f']=[[\f]];['\n']=[[\n]]; 1425 ['\f']=[[\f]];['\n']=[[\n]];
1196 ['\r']=[[\r]];['\t']=[[\t]]; 1426 ['\r']=[[\r]];['\t']=[[\t]];
1197 ['\v']=[[\v]];['\\']=[[\\]]; 1427 ['\v']=[[\v]];['\\']=[[\\]];
1198 ['\"']=[[\"]];['\'']=[[\']]; 1428 ['\"']=[[\"]];['\'']=[[\']];
1199 } 1429 }
1200 for t=0,255 do 1430 for t=0,255 do
1201 local e=o(t); 1431 local e=a(t);
1202 if not r[e]then 1432 if not r[e]then
1203 r[e]=n("\\%03d",t); 1433 r[e]=n("\\%03d",t);
1204 end 1434 end
1205 end 1435 end
1206 local o={ 1436 local a={
1207 ["do"]=true;["and"]=true;["else"]=true;["break"]=true; 1437 ["do"]=true;["and"]=true;["else"]=true;["break"]=true;
1208 ["if"]=true;["end"]=true;["goto"]=true;["false"]=true; 1438 ["if"]=true;["end"]=true;["goto"]=true;["false"]=true;
1209 ["in"]=true;["for"]=true;["then"]=true;["local"]=true; 1439 ["in"]=true;["for"]=true;["then"]=true;["local"]=true;
1210 ["or"]=true;["nil"]=true;["true"]=true;["until"]=true; 1440 ["or"]=true;["nil"]=true;["true"]=true;["until"]=true;
1211 ["elseif"]=true;["function"]=true;["not"]=true; 1441 ["elseif"]=true;["function"]=true;["not"]=true;
1212 ["repeat"]=true;["return"]=true;["while"]=true; 1442 ["repeat"]=true;["return"]=true;["while"]=true;
1213 }; 1443 };
1214 local function u(e) 1444 local function s(e)
1215 if a(e)~="table"then 1445 if o(e)~="table"then
1216 e={preset=e}; 1446 e={preset=e};
1217 end 1447 end
1218 local i={ 1448 local i={
1219 table=true; 1449 table=true;
1220 string=true; 1450 string=true;
1239 e.itemstart=e.itemstart or""; 1469 e.itemstart=e.itemstart or"";
1240 e.itemlast=e.itemlast or""; 1470 e.itemlast=e.itemlast or"";
1241 e.equals=e.equals or"="; 1471 e.equals=e.equals or"=";
1242 e.unquoted=true; 1472 e.unquoted=true;
1243 end 1473 end
1244 local s=e.fallback or e.fatal==false and t or s; 1474 local s=e.fallback or e.fatal==false and t or h;
1245 local function l(e) 1475 local function h(e)
1246 return(i[a(e)]or s)(e); 1476 return(i[o(e)]or s)(e);
1247 end 1477 end
1248 local p=e.keywords or o; 1478 local q=e.keywords or a;
1249 local y=e.indentwith or"\t"; 1479 local f=e.indentwith or"\t";
1250 local v=e.itemstart or"\n"; 1480 local b=e.itemstart or"\n";
1251 local I=e.itemsep or";"; 1481 local v=e.itemsep or";";
1252 local N=e.itemlast or";\n"; 1482 local I=e.itemlast or";\n";
1253 local O=e.tstart or"{"; 1483 local w=e.tstart or"{";
1254 local E=e.tend or"}"; 1484 local N=e.tend or"}";
1255 local T=e.kstart or"["; 1485 local k=e.kstart or"[";
1256 local A=e.kend or"]"; 1486 local x=e.kend or"]";
1257 local f=e.equals or" = "; 1487 local c=e.equals or" = ";
1258 local w=e.unquoted==true and"^[%a_][%w_]*$"or e.unquoted; 1488 local m=e.unquoted==true and"^[%a_][%w_]*$"or e.unquoted;
1259 local d=e.hex; 1489 local d=e.hex;
1260 local S=e.freeze; 1490 local T=e.freeze;
1261 local m=e.maxdepth or 127; 1491 local E=e.maxdepth or 127;
1262 local H=e.multiref; 1492 local _=e.multiref;
1263 local R=e.table_iterator or h; 1493 local O=e.table_iterator or l;
1264 local function u(o,t,e,i) 1494 local function l(a,t,e,i)
1265 if t[o]then 1495 if t[a]then
1266 t[e],e=s(o,"table has multiple references"),e+1; 1496 t[e],e=s(a,"table has multiple references"),e+1;
1267 return e; 1497 return e;
1268 elseif i>m then 1498 elseif i>E then
1269 t[e],e=s(o,"max table depth reached"),e+1; 1499 t[e],e=s(a,"max table depth reached"),e+1;
1270 return e; 1500 return e;
1271 end 1501 end
1272 local m=o; 1502 local d=a;
1273 t[o]=true; 1503 t[a]=true;
1274 t[m]=true; 1504 t[d]=true;
1275 if S==true then 1505 if T==true then
1276 local i=_(o); 1506 local i=u(a);
1277 if a(i)=="table"then 1507 if o(i)=="table"then
1278 local n=i.__name; 1508 local n=i.__name;
1279 local i=i.__freeze; 1509 local i=i.__freeze;
1280 if a(i)=="function"then 1510 if o(i)=="function"then
1281 o=i(o); 1511 a=i(a);
1282 if a(o)=="string"then 1512 if o(a)=="string"then
1283 t[e],e=o,e+1; 1513 t[e],e=a,e+1;
1284 return e; 1514 return e;
1285 end 1515 end
1286 if a(n)=="string"then 1516 if o(n)=="string"then
1287 t[e],e=n,e+1; 1517 t[e],e=n,e+1;
1288 end 1518 end
1289 end 1519 end
1290 end 1520 end
1291 end 1521 end
1292 t[e],e=O,e+1; 1522 t[e],e=w,e+1;
1293 local b=c(y,i); 1523 local p=y(f,i);
1294 local s=1; 1524 local r=1;
1295 local h,r; 1525 local n,u;
1296 local d=false; 1526 local w=false;
1297 for o,n in R(o)do 1527 for a,s in O(a)do
1298 d=true; 1528 w=true;
1529 t[e],e=b,e+1;
1530 t[e],e=p,e+1;
1531 n,u=o(a),o(s);
1532 if a==r then
1533 r=r+1;
1534 elseif m and n=="string"and
1535 not q[a]and g(a,m)then
1536 t[e],e=a,e+1;
1537 t[e],e=c,e+1;
1538 else
1539 t[e],e=k,e+1;
1540 if n=="table"then
1541 e=l(a,t,e,i+1);
1542 else
1543 t[e],e=h(a),e+1;
1544 end
1545 t[e],t[e+1],e=x,c,e+2;
1546 end
1547 if u=="table"then
1548 e=l(s,t,e,i+1);
1549 else
1550 t[e],e=h(s),e+1;
1551 end
1299 t[e],e=v,e+1; 1552 t[e],e=v,e+1;
1300 t[e],e=b,e+1; 1553 end
1301 h,r=a(o),a(n); 1554 if w then
1302 if o==s then 1555 t[e-1]=I;
1303 s=s+1; 1556 t[e],e=y(f,i-1),e+1;
1304 elseif w and h=="string"and 1557 end
1305 not p[o]and k(o,w)then 1558 t[e],e=N,e+1;
1306 t[e],e=o,e+1; 1559 if _ then
1307 t[e],e=f,e+1; 1560 t[a]=nil;
1308 else 1561 t[d]=nil;
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 1562 end
1333 return e; 1563 return e;
1334 end 1564 end
1335 function i.table(t) 1565 function i.table(t)
1336 local e={}; 1566 local e={};
1337 u(t,e,1,1); 1567 l(t,e,1,1);
1338 return q(e); 1568 return H(e);
1339 end 1569 end
1340 local function t(e) 1570 local function a(e)
1341 return'"'..z(e,"[%z\1-\31\"\'\\\127-\255]",r)..'"'; 1571 return'"'..p(e,"[%z\1-\31\"\'\\\127-\255]",r)..'"';
1342 end 1572 end
1343 if a(d)=="string"then 1573 if o(d)=="string"then
1344 function i.string(e) 1574 function i.string(e)
1345 local t=t(e); 1575 local t=a(e);
1346 if#t>(#e*2+2+#d)then 1576 if#t>(#e*2+2+#d)then
1347 return d..'"'..g(e)..'"'; 1577 return d..'"'..S(e)..'"';
1348 end 1578 end
1349 return t; 1579 return t;
1350 end 1580 end
1351 else 1581 else
1352 i.string=t; 1582 i.string=a;
1353 end 1583 end
1354 function i.number(e) 1584 function i.number(e)
1355 if x(e)=="integer"then 1585 if j(e)=="integer"then
1356 return n("%d",e); 1586 return n("%d",e);
1357 elseif e==b then 1587 elseif e==z then
1358 return"(1/0)"; 1588 return"(1/0)";
1359 elseif e==j then 1589 elseif e==A then
1360 return"(-1/0)"; 1590 return"(-1/0)";
1361 elseif e~=e then 1591 elseif e~=e then
1362 return"(0/0)"; 1592 return"(0/0)";
1363 end 1593 end
1364 return n("%.18g",e); 1594 return n("%.18g",e);
1367 return"nil"; 1597 return"nil";
1368 end 1598 end
1369 function i.boolean(e) 1599 function i.boolean(e)
1370 return e and"true"or"false"; 1600 return e and"true"or"false";
1371 end 1601 end
1372 return l; 1602 return h;
1373 end 1603 end
1374 local function o(e) 1604 local function i(e)
1375 if a(e)~="string"then return nil;end 1605 if o(e)~="string"then return nil;end
1376 e="return "..e; 1606 e="return "..e;
1377 local e,t=v(e,"=serialized data",{}); 1607 local e,t=v(e,"=serialized data",{});
1378 if not e then return nil,t;end 1608 if not e then return nil,t;end
1379 local t,e=p(e); 1609 local t,e=N(e);
1380 if not t then return nil,e;end 1610 if not t then return nil,e;end
1381 return e; 1611 return e;
1382 end 1612 end
1383 local a=u(); 1613 local a=s();
1384 return{ 1614 return{
1385 new=u; 1615 new=s;
1386 serialize=function(e,t) 1616 serialize=function(e,t)
1387 if t==nil then 1617 if t==nil then
1388 return a(e); 1618 return a(e);
1389 else 1619 else
1390 return u(t)(e); 1620 return s(t)(e);
1391 end 1621 end
1392 end; 1622 end;
1393 deserialize=o; 1623 deserialize=i;
1394 }; 1624 };
1395 end) 1625 end)
1396 package.preload['util.indexedbheap']=(function(...) 1626 package.preload['util.indexedbheap']=(function(...)
1397 local _ENV=_ENV; 1627 local _ENV=_ENV;
1398 local function e(t,...) 1628 local function e(t,...)
1404 _ENV=e; 1634 _ENV=e;
1405 _M=e; 1635 _M=e;
1406 return e; 1636 return e;
1407 end 1637 end
1408 local d=setmetatable; 1638 local d=setmetatable;
1409 local r=math.floor; 1639 local h=math.floor;
1410 local i=table.remove; 1640 local i=table.remove;
1411 local function l(t,s,o,n,i) 1641 local function l(t,i,o,n,s)
1412 local e=#t+1; 1642 local e=#t+1;
1413 while true do 1643 while true do
1414 local a=r(e/2); 1644 local a=h(e/2);
1415 if a==0 or s>t[a]then break;end 1645 if a==0 or i>t[a]then break;end
1416 t[e]=t[a]; 1646 t[e]=t[a];
1417 o[e]=o[a]; 1647 o[e]=o[a];
1418 i[o[e]]=e; 1648 s[o[e]]=e;
1419 e=a; 1649 e=a;
1420 end 1650 end
1421 t[e]=s; 1651 t[e]=i;
1422 o[e]=n; 1652 o[e]=n;
1423 i[n]=e; 1653 s[n]=e;
1424 end 1654 end
1425 local function h(a,e,t,i) 1655 local function r(t,e,a,n)
1426 local n=a[e]; 1656 local i=t[e];
1427 local s=t[e]; 1657 local s=a[e];
1428 while e~=1 do 1658 while e~=1 do
1429 local o=r(e/2); 1659 local o=h(e/2);
1430 if n>=a[o]then break;end 1660 if i>=t[o]then break;end
1661 t[e]=t[o];
1431 a[e]=a[o]; 1662 a[e]=a[o];
1432 t[e]=t[o]; 1663 n[a[e]]=e;
1433 i[t[e]]=e;
1434 e=o; 1664 e=o;
1435 end 1665 end
1436 a[e]=n; 1666 t[e]=i;
1437 t[e]=s; 1667 a[e]=s;
1438 i[s]=e; 1668 n[s]=e;
1439 return e; 1669 return e;
1440 end 1670 end
1441 local function n(a,e,o,h) 1671 local function n(a,e,o,h)
1442 local s=a[e]; 1672 local s=a[e];
1443 local n=o[e]; 1673 local n=o[e];
1460 a[e]=s; 1690 a[e]=s;
1461 o[e]=n; 1691 o[e]=n;
1462 h[n]=e; 1692 h[n]=e;
1463 return e; 1693 return e;
1464 end 1694 end
1465 local function r(e,t,o) 1695 local function u(e,t,a)
1466 local h=#e; 1696 local h=#e;
1467 if h==0 then return nil;end 1697 if h==0 then return nil;end
1468 local s=e[1]; 1698 local s=e[1];
1469 local a=t[1]; 1699 local o=t[1];
1470 o[a]=nil; 1700 a[o]=nil;
1471 if h==1 then 1701 if h==1 then
1472 e[1]=nil; 1702 e[1]=nil;
1473 t[1]=nil; 1703 t[1]=nil;
1474 return s,a; 1704 return s,o;
1475 end 1705 end
1476 e[1]=i(e); 1706 e[1]=i(e);
1477 t[1]=i(t); 1707 t[1]=i(t);
1478 o[t[1]]=1; 1708 a[t[1]]=1;
1479 n(e,1,t,o); 1709 n(e,1,t,a);
1480 return s,a; 1710 return s,o;
1481 end 1711 end
1482 local t={}; 1712 local t={};
1483 function t:insert(t,a,e) 1713 function t:insert(a,t,e)
1484 if e==nil then 1714 if e==nil then
1485 e=self.current_id; 1715 e=self.current_id;
1486 self.current_id=e+1; 1716 self.current_id=e+1;
1487 end 1717 end
1488 self.items[e]=t; 1718 self.items[e]=a;
1489 l(self.priorities,a,self.ids,e,self.index); 1719 l(self.priorities,t,self.ids,e,self.index);
1490 return e; 1720 return e;
1491 end 1721 end
1492 function t:pop() 1722 function t:pop()
1493 local a,e=r(self.priorities,self.ids,self.index); 1723 local a,e=u(self.priorities,self.ids,self.index);
1494 if e then 1724 if e then
1495 local t=self.items[e]; 1725 local t=self.items[e];
1496 self.items[e]=nil; 1726 self.items[e]=nil;
1497 return a,t,e; 1727 return a,t,e;
1498 end 1728 end
1502 end 1732 end
1503 function t:reprioritize(e,t) 1733 function t:reprioritize(e,t)
1504 local e=self.index[e]; 1734 local e=self.index[e];
1505 if e==nil then return;end 1735 if e==nil then return;end
1506 self.priorities[e]=t; 1736 self.priorities[e]=t;
1507 e=h(self.priorities,e,self.ids,self.index); 1737 e=r(self.priorities,e,self.ids,self.index);
1508 n(self.priorities,e,self.ids,self.index); 1738 n(self.priorities,e,self.ids,self.index);
1509 end 1739 end
1510 function t:remove_index(e) 1740 function t:remove_index(e)
1511 local o=self.priorities[e]; 1741 local o=self.priorities[e];
1512 if o==nil then return;end 1742 if o==nil then return;end
1519 i(self.priorities); 1749 i(self.priorities);
1520 i(self.ids); 1750 i(self.ids);
1521 self.index[t]=nil; 1751 self.index[t]=nil;
1522 self.items[t]=nil; 1752 self.items[t]=nil;
1523 if a>e then 1753 if a>e then
1524 e=h(self.priorities,e,self.ids,self.index); 1754 e=r(self.priorities,e,self.ids,self.index);
1525 n(self.priorities,e,self.ids,self.index); 1755 n(self.priorities,e,self.ids,self.index);
1526 end 1756 end
1527 return o,s,t; 1757 return o,s,t;
1528 end 1758 end
1529 function t:remove(e) 1759 function t:remove(e)
1573 end 1803 end
1574 _ENV=e; 1804 _ENV=e;
1575 _M=e; 1805 _M=e;
1576 return e; 1806 return e;
1577 end 1807 end
1578 local r,l,u,f 1808 local d,l,u,m
1579 =table.insert,table.sort,table.remove,table.concat; 1809 =table.insert,table.sort,table.remove,table.concat;
1580 local h=require"util.table".move; 1810 local s=require"util.table".move;
1581 local s=setmetatable; 1811 local h=setmetatable;
1582 local a=getmetatable; 1812 local a=getmetatable;
1583 local d=math.random; 1813 local r=math.random;
1584 local c=math.floor; 1814 local y=math.floor;
1585 local y,p=pairs,ipairs; 1815 local p,w=pairs,ipairs;
1586 local w=tostring; 1816 local f=tostring;
1587 local m=type; 1817 local c=type;
1588 local i={}; 1818 local i={};
1589 local t={}; 1819 local t={};
1590 local e={}; 1820 local e={};
1591 local o={ 1821 local o={
1592 __index=e; 1822 __index=e;
1593 __name="array"; 1823 __name="array";
1594 __tostring=function(e)return"{"..e:concat(", ").."}";end; 1824 __tostring=function(e)return"{"..e:concat(", ").."}";end;
1595 }; 1825 };
1596 function o:__freeze()return self;end 1826 function o:__freeze()return self;end
1597 local function n(i,e,a,t) 1827 local function n(t,e,a,i)
1598 if m(e)=="function"then 1828 if c(e)=="function"then
1599 e=i.collect(e,a,t); 1829 e=t.collect(e,a,i);
1600 end 1830 end
1601 return s(e or{},o); 1831 return h(e or{},o);
1602 end 1832 end
1603 function o.__add(t,a) 1833 function o.__add(a,e)
1604 local e=n(); 1834 local t=n();
1605 return e:append(t):append(a); 1835 return t:append(a):append(e);
1606 end 1836 end
1607 function o.__eq(e,t) 1837 function o.__eq(e,t)
1608 if a(e)~=o or a(t)~=o then 1838 if a(e)~=o or a(t)~=o then
1609 return false; 1839 return false;
1610 end 1840 end
1617 else 1847 else
1618 return false; 1848 return false;
1619 end 1849 end
1620 return true; 1850 return true;
1621 end 1851 end
1622 function o.__div(t,o) 1852 function o.__div(a,i)
1623 local a=n(); 1853 local t=n();
1624 local e=0; 1854 local e=0;
1625 for i=1,#t do 1855 for o=1,#a do
1626 local t=o(t[i]); 1856 local a=i(a[o]);
1627 if t~=nil then 1857 if a~=nil then
1628 e=e+1; 1858 e=e+1;
1629 a[e]=t; 1859 t[e]=a;
1630 end 1860 end
1631 end 1861 end
1632 return a; 1862 return t;
1633 end 1863 end
1634 s(i,{__call=n}); 1864 h(i,{__call=n});
1635 function e:random() 1865 function e:random()
1636 return self[d(1,#self)]; 1866 return self[r(1,#self)];
1637 end 1867 end
1638 function e:random_other(t) 1868 function e:random_other(t)
1639 local e=#self; 1869 local e=#self;
1640 return self[((math.random(1,e-1)+(t-1))%e)+1]; 1870 return self[((math.random(1,e-1)+(t-1))%e)+1];
1641 end 1871 end
1642 function t.map(e,t,a) 1872 function t.map(e,t,o)
1643 for t,o in p(t)do 1873 for t,a in w(t)do
1644 e[t]=a(o); 1874 e[t]=o(a);
1645 end 1875 end
1646 return e; 1876 return e;
1647 end 1877 end
1648 function t.filter(t,o,n) 1878 function t.filter(t,a,n)
1649 local i,a=o==t,#o; 1879 local i,o=a==t,#a;
1650 local e=1; 1880 local e=1;
1651 for a=1,a do 1881 for o=1,o do
1652 local a=o[a]; 1882 local a=a[o];
1653 if n(a)then 1883 if n(a)then
1654 t[e]=a; 1884 t[e]=a;
1655 e=e+1; 1885 e=e+1;
1656 end 1886 end
1657 end 1887 end
1658 if i and e<=a then 1888 if i and e<=o then
1659 for e=e,a do 1889 for e=e,o do
1660 t[e]=nil; 1890 t[e]=nil;
1661 end 1891 end
1662 end 1892 end
1663 return t; 1893 return t;
1664 end 1894 end
1665 function t.slice(a,t,o,e) 1895 function t.slice(o,t,a,e)
1666 if e==nil then 1896 if e==nil then
1667 e=-1; 1897 e=-1;
1668 end 1898 end
1669 if e<0 then 1899 if e<0 then
1670 e=#t+(e+1); 1900 e=#t+(e+1);
1671 end 1901 end
1672 if o<0 then 1902 if a<0 then
1673 o=#t+(o+1); 1903 a=#t+(a+1);
1674 end 1904 end
1675 if o<1 then 1905 if a<1 then
1676 o=1; 1906 a=1;
1677 end 1907 end
1678 if e>#t then 1908 if e>#t then
1679 e=#t; 1909 e=#t;
1680 end 1910 end
1681 if o>e then 1911 if a>e then
1682 for e=1,#a do 1912 for e=1,#o do
1683 a[e]=nil; 1913 o[e]=nil;
1684 end 1914 end
1685 return a; 1915 return o;
1686 end 1916 end
1687 h(t,o,e,1,a); 1917 s(t,a,e,1,o);
1688 if t==a then 1918 if t==o then
1689 h(t,#a+1,#a*2,2+e-o,t); 1919 s(t,#o+1,#o*2,2+e-a,t);
1690 end 1920 end
1691 return a; 1921 return o;
1692 end 1922 end
1693 function t.sort(e,t,...) 1923 function t.sort(e,t,...)
1694 if t~=e then 1924 if t~=e then
1695 e:append(t); 1925 e:append(t);
1696 end 1926 end
1697 l(e,...); 1927 l(e,...);
1698 return e; 1928 return e;
1699 end 1929 end
1700 function t.unique(o,a) 1930 function t.unique(o,e)
1701 local e={}; 1931 local a={};
1702 return t.filter(o,a,function(t) 1932 return t.filter(o,e,function(e)
1703 if e[t]then 1933 if a[e]then
1704 return false; 1934 return false;
1705 else 1935 else
1706 e[t]=true; 1936 a[e]=true;
1707 return true; 1937 return true;
1708 end 1938 end
1709 end); 1939 end);
1710 end 1940 end
1711 function t.pluck(a,e,o,i) 1941 function t.pluck(t,e,i,o)
1712 for t=1,#e do 1942 for a=1,#e do
1713 local e=e[t][o]; 1943 local e=e[a][i];
1714 if e==nil then 1944 if e==nil then
1715 e=i; 1945 e=o;
1716 end 1946 end
1717 a[t]=e; 1947 t[a]=e;
1718 end 1948 end
1719 return a; 1949 return t;
1720 end 1950 end
1721 function t.reverse(e,i) 1951 function t.reverse(e,i)
1722 local t=#i; 1952 local t=#i;
1723 if i==e then 1953 if i==e then
1724 local a=c(t/2); 1954 local a=y(t/2);
1725 t=t+1; 1955 t=t+1;
1726 local o; 1956 local o;
1727 for a=1,a do 1957 for a=1,a do
1728 o=t-a; 1958 o=t-a;
1729 e[a],e[o]=e[o],e[a]; 1959 e[a],e[o]=e[o],e[a];
1737 return e; 1967 return e;
1738 end 1968 end
1739 function e:shuffle() 1969 function e:shuffle()
1740 local t=#self; 1970 local t=#self;
1741 for e=1,#self do 1971 for e=1,#self do
1742 local t=d(e,t); 1972 local t=r(e,t);
1743 self[e],self[t]=self[t],self[e]; 1973 self[e],self[t]=self[t],self[e];
1744 end 1974 end
1745 return self; 1975 return self;
1746 end 1976 end
1747 function e:append(e) 1977 function e:append(e)
1748 h(e,1,#e,#self+1,self); 1978 s(e,1,#e,#self+1,self);
1749 return self; 1979 return self;
1750 end 1980 end
1751 function e:push(t) 1981 function e:push(t)
1752 r(self,t); 1982 d(self,t);
1753 return self; 1983 return self;
1754 end 1984 end
1755 e.pop=u; 1985 e.pop=u;
1756 function e:concat(e) 1986 function e:concat(e)
1757 return f(i.map(self,w),e); 1987 return m(i.map(self,f),e);
1758 end 1988 end
1759 function e:length() 1989 function e:length()
1760 return#self; 1990 return#self;
1761 end 1991 end
1762 function i.collect(i,a,e) 1992 function i.collect(i,a,e)
1763 local t={}; 1993 local t={};
1764 while true do 1994 while true do
1765 e=i(a,e); 1995 e=i(a,e);
1766 if e==nil then break;end 1996 if e==nil then break;end
1767 r(t,e); 1997 d(t,e);
1768 end 1998 end
1769 return s(t,o); 1999 return h(t,o);
1770 end 2000 end
1771 for t,a in y(t)do 2001 for t,a in p(t)do
1772 local a=a; 2002 local a=a;
1773 i[t]=function(t,...) 2003 i[t]=function(o,...)
1774 local o=n(); 2004 local t=n();
1775 return a(o,t,...); 2005 return a(t,o,...);
1776 end 2006 end
1777 e[t]=function(e,...) 2007 e[t]=function(e,...)
1778 return a(e,e,...); 2008 return a(e,e,...);
1779 end 2009 end
1780 end 2010 end
1791 _ENV=e; 2021 _ENV=e;
1792 _M=e; 2022 _M=e;
1793 return e; 2023 return e;
1794 end 2024 end
1795 local n=tostring; 2025 local n=tostring;
1796 local m=table.unpack; 2026 local p=table.unpack;
1797 local e=table.pack; 2027 local e=table.pack;
1798 local c=require"util.encodings".utf8.valid; 2028 local y=require"util.encodings".utf8.valid;
1799 local i=type; 2029 local i=type;
1800 local l=require"util.serialization".new("debug"); 2030 local w=require"util.serialization".new("debug");
1801 local r=math.type; 2031 local d=math.type;
1802 local u={c=true,d=true,i=true,o=true,u=true,X=true,x=true,}; 2032 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}; 2033 local l={o=true;u=true;x=true;X=true};
1804 local d={ 2034 local r={
1805 ["\000"]="\226\144\128",["\001"]="\226\144\129",["\002"]="\226\144\130", 2035 ["\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", 2036 ["\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", 2037 ["\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", 2038 ["\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", 2039 ["\012"]="\226\144\140",["\013"]="\226\144\141",["\014"]="\226\144\142",
1812 ["\021"]="\226\144\149",["\022"]="\226\144\150",["\023"]="\226\144\151", 2042 ["\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", 2043 ["\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", 2044 ["\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", 2045 ["\030"]="\226\144\158",["\031"]="\226\144\159",["\127"]="\226\144\161",
1816 }; 2046 };
1817 local y=pcall(string.format,"%p",""); 2047 local c=pcall(string.format,"%p","");
1818 local function w(h,...) 2048 local function f(h,...)
1819 local o=e(...); 2049 local o=e(...);
1820 local w=o.n; 2050 local m=o.n;
1821 local a=0; 2051 local a=0;
1822 h=h:gsub("%%[^cdiouxXaAeEfgGpqs%%]*[cdiouxXaAeEfgGpqs%%]",function(s) 2052 h=h:gsub("%%[^cdiouxXaAeEfgGpqs%%]*[cdiouxXaAeEfgGpqs%%]",function(s)
1823 if s=="%%"then return end 2053 if s=="%%"then return end
1824 a=a+1; 2054 a=a+1;
1825 local e=o[a]; 2055 local e=o[a];
1830 local t=s:sub(-1); 2060 local t=s:sub(-1);
1831 local i=i(e); 2061 local i=i(e);
1832 if t=="s"and i=="string"and not e:find("[%z\1-\31\128-\255]")then 2062 if t=="s"and i=="string"and not e:find("[%z\1-\31\128-\255]")then
1833 return 2063 return
1834 elseif i=="number"then 2064 elseif i=="number"then
1835 if t=="g"or(t=="d"and r(e)=="integer")then return end 2065 if t=="g"or(t=="d"and d(e)=="integer")then return end
1836 elseif t=="s"and i~="string"then 2066 elseif t=="s"and i~="string"then
1837 e=n(e); 2067 e=n(e);
1838 i="string"; 2068 i="string";
1839 end 2069 end
1840 if t~="s"and t~="q"and t~="p"then 2070 if t~="s"and t~="q"and t~="p"then
1841 if i~="number"then 2071 if i~="number"then
1842 e=n(e); 2072 e=n(e);
1843 t="s"; 2073 t="s";
1844 s="[%s]"; 2074 s="[%s]";
1845 i="string"; 2075 i="string";
1846 elseif u[t]and r(e)~="integer"then 2076 elseif u[t]and d(e)~="integer"then
1847 o[a]=n(e); 2077 o[a]=n(e);
1848 return"[%s]"; 2078 return"[%s]";
1849 elseif f[t]and e<0 then 2079 elseif l[t]and e<0 then
1850 o[a]=n(e); 2080 o[a]=n(e);
1851 return"[%s]"; 2081 return"[%s]";
1852 else 2082 else
1853 return 2083 return
1854 end 2084 end
1855 end 2085 end
1856 if t=="p"and not y then 2086 if t=="p"and not c then
1857 e=n(e); 2087 e=n(e);
1858 t="s"; 2088 t="s";
1859 s="[%s]"; 2089 s="[%s]";
1860 i="string"; 2090 i="string";
1861 end 2091 end
1862 if i=="string"and t~="p"then 2092 if i=="string"and t~="p"then
1863 if not c(e)then 2093 if not y(e)then
1864 t="q"; 2094 t="q";
1865 elseif t~="q"then 2095 elseif t~="q"then
1866 o[a]=e:gsub("[%z\1-\8\11-\31\127]",d):gsub("\n\t?","\n\t"); 2096 o[a]=e:gsub("[%z\1-\8\11-\31\127]",r):gsub("\n\t?","\n\t");
1867 return s; 2097 return s;
1868 end 2098 end
1869 end 2099 end
1870 if t=="q"then 2100 if t=="q"then
1871 o[a]=l(e); 2101 o[a]=w(e);
1872 return"%s"; 2102 return"%s";
1873 end 2103 end
1874 if t=="p"and(i=="boolean"or i=="number")then 2104 if t=="p"and(i=="boolean"or i=="number")then
1875 o[a]=n(e); 2105 o[a]=n(e);
1876 return"[%s]"; 2106 return"[%s]";
1877 end 2107 end
1878 end); 2108 end);
1879 while a<w do 2109 while a<m do
1880 a=a+1; 2110 a=a+1;
1881 local e=o[a]; 2111 local e=o[a];
1882 if e==nil then 2112 if e==nil then
1883 o[a]="(nil)"; 2113 o[a]="(nil)";
1884 else 2114 else
1885 o[a]=n(e):gsub("[%z\1-\8\11-\31\127]",d):gsub("\n\t?","\n\t"); 2115 o[a]=n(e):gsub("[%z\1-\8\11-\31\127]",r):gsub("\n\t?","\n\t");
1886 end 2116 end
1887 h=h.." [%s]" 2117 h=h.." [%s]"
1888 end 2118 end
1889 return h:format(m(o)); 2119 return h:format(p(o));
1890 end 2120 end
1891 return{ 2121 return{
1892 format=w; 2122 format=f;
1893 }; 2123 };
1894 end) 2124 end)
1895 package.preload['util.promise']=(function(...) 2125 package.preload['util.promise']=(function(...)
1896 local _ENV=_ENV; 2126 local _ENV=_ENV;
1897 local function e(t,...) 2127 local function e(t,...)
1902 end 2132 end
1903 _ENV=e; 2133 _ENV=e;
1904 _M=e; 2134 _M=e;
1905 return e; 2135 return e;
1906 end 2136 end
1907 local o={}; 2137 local i={};
1908 local a={__name="promise",__index=o}; 2138 local n={__name="promise",__index=i};
1909 local n=require"util.xpcall".xpcall; 2139 local s=require"util.xpcall".xpcall;
1910 local l=table.unpack; 2140 local l=table.unpack;
1911 function a:__tostring() 2141 function n:__tostring()
1912 return"promise ("..(self._state or"invalid")..")"; 2142 return"promise ("..(self._state or"invalid")..")";
1913 end 2143 end
1914 local function i(e) 2144 local function o(e)
1915 local e=getmetatable(e); 2145 local e=getmetatable(e);
1916 return e==a; 2146 return e==n;
1917 end 2147 end
1918 local function e(e,t,o,a) 2148 local function e(e,a,o,t)
1919 if not e then 2149 if not e then
1920 return a; 2150 return t;
1921 end 2151 end
1922 return function(a) 2152 return function(t)
1923 local a,e=n(e,debug.traceback,a); 2153 local t,e=s(e,debug.traceback,t);
1924 if a then 2154 if t then
1925 t(e); 2155 a(e);
1926 else 2156 else
1927 o(e); 2157 o(e);
1928 end 2158 end
1929 return true; 2159 return true;
1930 end; 2160 end;
1931 end 2161 end
1932 local function u(o,i,n,a,t) 2162 local function u(o,i,n,a,t)
1933 table.insert(o._pending_on_fulfilled,e(i,a,t,a)); 2163 table.insert(o._pending_on_fulfilled,e(i,a,t,a));
1934 table.insert(o._pending_on_rejected,e(n,a,t,t)); 2164 table.insert(o._pending_on_rejected,e(n,a,t,t));
1935 end 2165 end
1936 local function r(a,o,n,t,i) 2166 local function d(i,o,n,t,a)
1937 e(o,t,i,t)(a.value); 2167 e(o,t,a,t)(i.value);
1938 end 2168 end
1939 local function d(i,n,o,a,t) 2169 local function r(i,n,o,a,t)
1940 e(o,a,t,t)(i.reason); 2170 e(o,a,t,t)(i.reason);
1941 end 2171 end
1942 local function h(e,i,o,t,a) 2172 local function a(e,t,a,o,i)
1943 if e._state~="pending"then 2173 if e._state~="pending"then
1944 return; 2174 return;
1945 end 2175 end
1946 e._state=i; 2176 e._state=t;
1947 e._next=o; 2177 e._next=a;
1948 for o,t in ipairs(t)do 2178 for a,t in ipairs(o)do
1949 t(a); 2179 t(i);
1950 end 2180 end
1951 e._pending_on_fulfilled=nil; 2181 e._pending_on_fulfilled=nil;
1952 e._pending_on_rejected=nil; 2182 e._pending_on_rejected=nil;
1953 return true; 2183 return true;
1954 end 2184 end
1955 local function s(e) 2185 local function h(e)
1956 local function a(t) 2186 local function i(t)
1957 if i(t)then 2187 if o(t)then
1958 t:next(s(e)); 2188 t:next(h(e));
1959 elseif h(e,"fulfilled",r,e._pending_on_fulfilled,t)then 2189 elseif a(e,"fulfilled",d,e._pending_on_fulfilled,t)then
1960 e.value=t; 2190 e.value=t;
1961 end 2191 end
1962 end 2192 end
1963 local function o(t) 2193 local function o(t)
1964 if h(e,"rejected",d,e._pending_on_rejected,t)then 2194 if a(e,"rejected",r,e._pending_on_rejected,t)then
1965 e.reason=t; 2195 e.reason=t;
1966 end 2196 end
1967 end 2197 end
1968 return a,o; 2198 return i,o;
1969 end 2199 end
1970 local d=function(e) 2200 local d=function(e)
1971 e(); 2201 e();
1972 end 2202 end
1973 local function e(o) 2203 local function e(a)
1974 local e=setmetatable({_state="pending",_next=u,_pending_on_fulfilled={},_pending_on_rejected={}},a); 2204 local e=setmetatable({_state="pending",_next=u,_pending_on_fulfilled={},_pending_on_rejected={}},n);
1975 if o then 2205 if a then
1976 d(function() 2206 d(function()
1977 local a,t=s(e); 2207 local o,t=h(e);
1978 local o,a=n(o,debug.traceback,a,t); 2208 local a,o=s(a,debug.traceback,o,t);
1979 if not o and e._state=="pending"then 2209 if not a and e._state=="pending"then
1980 t(a); 2210 t(o);
1981 end 2211 end
1982 end); 2212 end);
1983 end 2213 end
1984 return e; 2214 return e;
1985 end 2215 end
1986 local function r(o) 2216 local function r(i)
1987 return e(function(n,r) 2217 return e(function(n,r)
1988 local e,a,s=0,{},false; 2218 local e,a,h=0,{},false;
1989 local t=0; 2219 local t=0;
1990 for h,o in pairs(o)do 2220 for s,i in pairs(i)do
1991 if i(o)then 2221 if o(i)then
1992 t=t+1; 2222 t=t+1;
1993 o:next(function(o) 2223 i:next(function(o)
1994 a[h]=o; 2224 a[s]=o;
1995 e=e+1; 2225 e=e+1;
1996 if e==t and s then 2226 if e==t and h then
1997 n(a); 2227 n(a);
1998 end 2228 end
1999 end,r); 2229 end,r);
2000 else 2230 else
2001 a[h]=o; 2231 a[s]=i;
2002 end 2232 end
2003 end 2233 end
2004 s=true; 2234 h=true;
2005 if e==t then 2235 if e==t then
2006 n(a); 2236 n(a);
2007 end 2237 end
2008 end); 2238 end);
2009 end 2239 end
2010 local function u(n) 2240 local function u(n)
2011 return e(function(h) 2241 return e(function(s)
2012 local e,t,o=0,{},false; 2242 local e,t,i=0,{},false;
2013 local a=0; 2243 local a=0;
2014 for s,n in pairs(n)do 2244 for n,h in pairs(n)do
2015 if i(n)then 2245 if o(h)then
2016 a=a+1; 2246 a=a+1;
2017 n:next(function(i) 2247 h:next(function(o)
2018 t[s]={status="fulfilled",value=i}; 2248 t[n]={status="fulfilled",value=o};
2019 e=e+1; 2249 e=e+1;
2020 if e==a and o then 2250 if e==a and i then
2021 h(t); 2251 s(t);
2022 end 2252 end
2023 end,function(i) 2253 end,function(o)
2024 t[s]={status="rejected",reason=i}; 2254 t[n]={status="rejected",reason=o};
2025 e=e+1; 2255 e=e+1;
2026 if e==a and o then 2256 if e==a and i then
2027 h(t); 2257 s(t);
2028 end 2258 end
2029 end); 2259 end);
2030 else 2260 else
2031 t[s]=n; 2261 t[n]=h;
2032 end 2262 end
2033 end 2263 end
2034 o=true; 2264 i=true;
2035 if e==a then 2265 if e==a then
2036 h(t); 2266 s(t);
2037 end 2267 end
2038 end); 2268 end);
2039 end 2269 end
2040 local function n(e,...) 2270 local function s(t,...)
2041 local t,a={...},select("#",...); 2271 local e,a={...},select("#",...);
2042 return r(t):next(function(t) 2272 return r(e):next(function(e)
2043 return e(l(t,1,a)); 2273 return t(l(e,1,a));
2044 end); 2274 end);
2045 end 2275 end
2046 local function s(t) 2276 local function n(t)
2047 return e(function(e,a) 2277 return e(function(o,a)
2048 for o=1,#t do 2278 for e=1,#t do
2049 t[o]:next(e,a); 2279 t[e]:next(o,a);
2050 end 2280 end
2051 end); 2281 end);
2052 end 2282 end
2053 local function t(t) 2283 local function a(t)
2054 return e(function(e) 2284 return e(function(e)
2055 e(t); 2285 e(t);
2056 end); 2286 end);
2057 end 2287 end
2058 local function a(t) 2288 local function t(t)
2059 return e(function(a,e) 2289 return e(function(a,e)
2060 e(t); 2290 e(t);
2061 end); 2291 end);
2062 end 2292 end
2063 local function h(e) 2293 local function h(e)
2064 return t():next(function()return e();end); 2294 return a():next(function()return e();end);
2065 end 2295 end
2066 function o:next(o,a) 2296 function i:next(t,a)
2067 return e(function(e,t) 2297 return e(function(e,o)
2068 self:_next(o,a,e,t); 2298 self:_next(t,a,e,o);
2069 end); 2299 end);
2070 end 2300 end
2071 function o:catch(e) 2301 function i:catch(e)
2072 return self:next(nil,e); 2302 return self:next(nil,e);
2073 end 2303 end
2074 function o:finally(e) 2304 function i:finally(e)
2075 local function t(t)e();return t;end 2305 local function i(t)e();return t;end
2076 local function o(t)e();return a(t);end 2306 local function o(a)e();return t(a);end
2077 return self:next(t,o); 2307 return self:next(i,o);
2078 end 2308 end
2079 return{ 2309 return{
2080 new=e; 2310 new=e;
2081 resolve=t; 2311 resolve=a;
2082 join=n; 2312 join=s;
2083 reject=a; 2313 reject=t;
2084 all=r; 2314 all=r;
2085 all_settled=u; 2315 all_settled=u;
2086 race=s; 2316 race=n;
2087 try=h; 2317 try=h;
2088 is_promise=i; 2318 is_promise=o;
2089 set_nexttick=function(e)d=e;end; 2319 set_nexttick=function(e)d=e;end;
2090 } 2320 }
2091 end) 2321 end)
2092 package.preload['net.adns']=(function(...) 2322 package.preload['net.adns']=(function(...)
2093 local _ENV=_ENV; 2323 local _ENV=_ENV;
2099 end 2329 end
2100 _ENV=e; 2330 _ENV=e;
2101 _M=e; 2331 _M=e;
2102 return e; 2332 return e;
2103 end 2333 end
2104 local o=setmetatable; 2334 local a=setmetatable;
2105 local c=tostring; 2335 local f=tostring;
2106 local k=table.concat; 2336 local y=table.concat;
2107 local n=string.format; 2337 local n=string.format;
2108 local q=string.lower; 2338 local m=string.lower;
2109 local d=string.upper; 2339 local u=string.upper;
2110 local i=function()end; 2340 local i=function()end;
2111 local w=require"util.logger"; 2341 local w=require"util.logger";
2112 local l=w.init("unbound"); 2342 local o=w.init("unbound");
2113 local p=require"net.server"; 2343 local k=require"net.server";
2114 local m=require"lunbound"; 2344 local s=require"lunbound";
2115 local g=require"util.promise"; 2345 local g=require"util.promise";
2116 local b=require"util.id".short; 2346 local b=require"util.id".short;
2117 local y=require"socket".gettime; 2347 local p=require"socket".gettime;
2118 local e=require"util.dns"; 2348 local e=require"util.dns";
2119 local h,s,u=e.classes,e.types,e.errors; 2349 local h,r,l=e.classes,e.types,e.errors;
2120 local v=e.parsers; 2350 local v=e.parsers;
2121 local t={hoststxt=false} 2351 local t={hoststxt=false}
2122 local function r(e) 2352 local function d(e)
2123 e=e or{}; 2353 e=e or{};
2124 for t,a in pairs(t)do 2354 for t,a in pairs(t)do
2125 if e[t]==nil then 2355 if e[t]==nil then
2126 e[t]=a; 2356 e[t]=a;
2127 end 2357 end
2128 end 2358 end
2129 for t,a in pairs(m.config)do 2359 for t,a in pairs(s.config)do
2130 if e[t]==nil then 2360 if e[t]==nil then
2131 e[t]=a; 2361 e[t]=a;
2132 end 2362 end
2133 end 2363 end
2134 return e; 2364 return e;
2135 end 2365 end
2136 local a; 2366 local t;
2137 if prosody then 2367 if prosody then
2138 local e=require"core.configmanager"; 2368 local e=require"core.configmanager";
2139 a=r(e.get("*","unbound")); 2369 t=d(e.get("*","unbound"));
2140 prosody.events.add_handler("config-reloaded",function() 2370 prosody.events.add_handler("config-reloaded",function()
2141 a=r(e.get("*","unbound")); 2371 t=d(e.get("*","unbound"));
2142 end); 2372 end);
2143 end 2373 end
2144 local function r(e,t) 2374 local function q(e,t)
2145 l("debug","Setting up net.server event handling for %s",e); 2375 o("debug","Setting up net.server event handling for %s",e);
2146 return t.watchfd(e,function() 2376 return t.watchfd(e,function()
2147 l("debug","Processing queries for %s",e); 2377 o("debug","Processing queries for %s",e);
2148 e:process() 2378 e:process()
2149 end); 2379 end);
2150 end 2380 end
2151 local t,f; 2381 local o,c;
2152 local function l() 2382 local function d()
2153 t=m.new(a); 2383 o=s.new(t);
2154 f=r(t,p); 2384 c=q(o,k);
2155 end 2385 end
2156 if prosody then 2386 if prosody then
2157 prosody.events.add_handler("server-started",l); 2387 prosody.events.add_handler("server-started",d);
2158 end 2388 end
2159 local n={ 2389 local s={
2160 __tostring=function(e) 2390 __tostring=function(e)
2161 if e._string then return e._string end 2391 if e._string then return e._string end
2162 local t=n("Status: %s",u[e.status]); 2392 local t=n("Status: %s",l[e.status]);
2163 if e.secure then 2393 if e.secure then
2164 t=t..", Secure"; 2394 t=t..", Secure";
2165 elseif e.bogus then 2395 elseif e.bogus then
2166 t=t..n(", Bogus: %s",e.bogus); 2396 t=t..n(", Bogus: %s",e.bogus);
2167 end 2397 end
2168 local t={t}; 2398 local a={t};
2169 for a=1,#e do 2399 for t=1,#e do
2170 t[a+1]=e.qname.."\t"..h[e.qclass].."\t"..s[e.qtype].."\t"..c(e[a]); 2400 a[t+1]=e.qname.."\t"..h[e.qclass].."\t"..r[e.qtype].."\t"..f(e[t]);
2171 end 2401 end
2172 local t=k(t,"\n"); 2402 local t=y(a,"\n");
2173 e._string=t; 2403 e._string=t;
2174 return t; 2404 return t;
2175 end; 2405 end;
2176 }; 2406 };
2177 local r={}; 2407 local n={};
2178 local function p(e) 2408 local function y(e)
2179 if not e then return end 2409 if not e then return end
2180 local a=u[e.rcode]; 2410 local i=l[e.rcode];
2181 local i=h[e.qclass]; 2411 local o=h[e.qclass];
2182 local t=s[e.qtype]; 2412 local t=r[e.qtype];
2183 e.status,e.class,e.type=a,i,t; 2413 e.status,e.class,e.type=i,o,t;
2184 local a=q(t); 2414 local o=m(t);
2185 local i={__index=e,__tostring=function(e)return c(e[a])end}; 2415 local i={__index=e,__tostring=function(e)return f(e[o])end};
2186 local s=v[t]; 2416 local n=v[t];
2187 for t=1,#e do 2417 for t=1,#e do
2188 if e.bogus then 2418 if e.bogus then
2189 e[t]=nil; 2419 e[t]=nil;
2190 else 2420 else
2191 e[t]=o({[a]=s(e[t])},i); 2421 e[t]=a({[o]=n(e[t])},i);
2192 end 2422 end
2193 end 2423 end
2194 return o(e,n); 2424 return a(e,s);
2195 end 2425 end
2196 local function m(m,u,o,a) 2426 local function m(c,l,a,t)
2197 if not t then l();end 2427 if not o then d();end
2198 o=o and d(o)or"A"; 2428 a=a and u(a)or"A";
2199 a=a and d(a)or"IN"; 2429 t=t and u(t)or"IN";
2200 local l,h=s[o],h[a]; 2430 local u,d=r[a],h[t];
2201 local d=y(); 2431 local h=p();
2202 local n; 2432 local s;
2203 local i=w.init("unbound.query"..b()); 2433 local i=w.init("unbound.query"..b());
2204 local function s(e,t) 2434 local function r(e,o)
2205 local s=y(); 2435 local r=p();
2206 r[n]=nil; 2436 n[s]=nil;
2207 if e then 2437 if e then
2208 p(e); 2438 y(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, 2439 i("debug","Results for %s %s %s: %s (%s, %f sec)",l,t,a,e.rcode==0 and(#e.." items")or e.status,
2210 e.secure and"Secure"or e.bogus or"Insecure",s-d); 2440 e.secure and"Secure"or e.bogus or"Insecure",r-h);
2211 else 2441 else
2212 i("error","Results for %s %s %s: %s",u,a,o,c(t)); 2442 i("error","Results for %s %s %s: %s",l,t,a,f(o));
2213 end 2443 end
2214 local t,e=pcall(m,e,t); 2444 local e,t=pcall(c,e,o);
2215 if not t then i("error","Error in callback: %s",e);end 2445 if not e then i("error","Error in callback: %s",t);end
2216 end 2446 end
2217 i("debug","Resolve %s %s %s",u,a,o); 2447 i("debug","Resolve %s %s %s",l,t,a);
2218 local e; 2448 local e;
2219 n,e=t:resolve_async(s,u,l,h); 2449 s,e=o:resolve_async(r,l,u,d);
2220 if n then 2450 if s then
2221 r[n]=m; 2451 n[s]=c;
2222 else 2452 else
2223 i("error","Resolver error: %s",e); 2453 i("error","Resolver error: %s",e);
2224 end 2454 end
2225 return n,e; 2455 return s,e;
2226 end 2456 end
2227 local function o(o,a,e) 2457 local function s(a,e,t)
2228 if not t then l();end 2458 if not o then d();end
2229 a=a and d(a)or"A"; 2459 e=e and u(e)or"A";
2230 e=e and d(e)or"IN"; 2460 t=t and u(t)or"IN";
2231 local e,a=s[a],h[e]; 2461 local t,e=r[e],h[t];
2232 local e,t=t:resolve(o,e,a); 2462 local e,t=o:resolve(a,t,e);
2233 if not e then return e,t;end 2463 if not e then return e,t;end
2234 return p(e); 2464 return y(e);
2235 end 2465 end
2236 local function e(e) 2466 local function e(e)
2237 local a=r[e]; 2467 local t=n[e];
2238 t:cancel(e); 2468 o:cancel(e);
2239 if a then 2469 if t then
2240 a(nil,"canceled"); 2470 t(nil,"canceled");
2241 r[e]=nil; 2471 n[e]=nil;
2242 end 2472 end
2243 return true; 2473 return true;
2244 end 2474 end
2475 local function a()
2476 for t in pairs(n)do e(t);end
2477 if c then c:close();end
2478 d();
2479 return true;
2480 end
2245 local function t() 2481 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"; 2482 error"not implemented";
2253 end 2483 end
2254 local a={ 2484 local a={
2255 lookup=m; 2485 lookup=m;
2256 cancel=e; 2486 cancel=e;
2257 new_async_socket=a; 2487 new_async_socket=t;
2258 dns={ 2488 dns={
2259 lookup=o; 2489 lookup=s;
2260 cancel=e; 2490 cancel=e;
2261 cache=i; 2491 cache=i;
2262 socket_wrapper_set=i; 2492 socket_wrapper_set=i;
2263 settimeout=i; 2493 settimeout=i;
2264 query=i; 2494 query=i;
2265 purge=t; 2495 purge=a;
2266 random=i; 2496 random=i;
2267 peek=i; 2497 peek=i;
2268 types=s; 2498 types=r;
2269 classes=h; 2499 classes=h;
2270 }; 2500 };
2271 }; 2501 };
2272 local function n(e,i,a,o) 2502 local function h(e,s,n,i)
2273 return g.new(function(n,t) 2503 return g.new(function(o,t)
2274 local function s(a,e) 2504 local function a(a,e)
2275 if e then 2505 if e then
2276 return t(e); 2506 return t(e);
2277 else 2507 else
2278 return n(a); 2508 return o(a);
2279 end 2509 end
2280 end 2510 end
2281 local e,a=m(s,i,a,o) 2511 local e,a=m(a,s,n,i)
2282 if not e then t(a);end 2512 if not e then t(a);end
2283 end); 2513 end);
2284 end 2514 end
2285 local e={ 2515 local e={
2286 lookup=function(i,e,t,a,o) 2516 lookup=function(i,e,t,a,o)
2287 return m(e,t,a,o) 2517 return m(e,t,a,o)
2288 end; 2518 end;
2289 lookup_promise=n; 2519 lookup_promise=h;
2290 _resolver={ 2520 _resolver={
2291 settimeout=function()end; 2521 settimeout=function()end;
2292 closeall=function()end; 2522 closeall=function()end;
2293 }; 2523 };
2294 } 2524 }
2311 local e=table; 2541 local e=table;
2312 local s=e.concat; 2542 local s=e.concat;
2313 local n=e.insert; 2543 local n=e.insert;
2314 local o=string.byte; 2544 local o=string.byte;
2315 local i=string.format; 2545 local i=string.format;
2316 local r=string.sub; 2546 local h=string.sub;
2317 local h=require"util.dnsregistry"; 2547 local c=require"util.dnsregistry";
2318 local c=require"util.hex".encode; 2548 local m=require"util.hex".encode;
2319 local d=require"util.net".ntop; 2549 local r=require"util.net".ntop;
2320 local e={}; 2550 local e={};
2321 local function a(a,t) 2551 local function a(i,t)
2322 if o(a,t)==0 then return".",t+1;end 2552 if o(i,t)==0 then return".",t+1;end
2323 local d,h,i=#a,{}; 2553 local d,r,a=#i,{};
2324 t=t or 1; 2554 t=t or 1;
2325 repeat 2555 repeat
2326 i=o(a,t)or 0; 2556 a=o(i,t)or 0;
2327 n(h,r(a,t+1,t+i)); 2557 n(r,h(i,t+1,t+a));
2328 t=t+i+1; 2558 t=t+a+1;
2329 until i==0 or t>=d; 2559 until a==0 or t>=d;
2330 return s(h,"."),t; 2560 return s(r,"."),t;
2331 end 2561 end
2332 e.CNAME=a; 2562 e.CNAME=a;
2333 e.NS=a 2563 e.NS=a
2334 e.PTR=a; 2564 e.PTR=a;
2335 local x={ 2565 local p={
2336 __tostring=function(e) 2566 __tostring=function(e)
2337 return i("%s %s %d %d %d %d %d",e.mname,e.rname,e.serial,e.refresh,e.retry,e.expire,e.minimum); 2567 return i("%s %s %d %d %d %d %d",e.mname,e.rname,e.serial,e.refresh,e.retry,e.expire,e.minimum);
2338 end; 2568 end;
2339 }; 2569 };
2340 function e.SOA(n) 2570 function e.SOA(n)
2341 local s,h,i; 2571 local h,s,i;
2342 s,i=a(n,1); 2572 h,i=a(n,1);
2343 h,i=a(n,i); 2573 s,i=a(n,i);
2344 local 2574 local
2345 j,q,g,k, 2575 m,f,w,y,
2346 b,l,r,d, 2576 v,g,b,j,
2347 n,a,o,i, 2577 x,q,k,c,
2348 u,w,c,p, 2578 u,i,n,a,
2349 v,y,m,f 2579 o,r,l,d
2350 =o(n,i,i+19); 2580 =o(n,i,i+19);
2351 return t({ 2581 return t({
2352 mname=s; 2582 mname=h;
2353 rname=h; 2583 rname=s;
2354 serial=j*16777216+q*65536+g*256+k; 2584 serial=m*16777216+f*65536+w*256+y;
2355 refresh=b*16777216+l*65536+r*256+d; 2585 refresh=v*16777216+g*65536+b*256+j;
2356 retry=n*16777216+a*65536+o*256+i; 2586 retry=x*16777216+q*65536+k*256+c;
2357 expire=u*16777216+w*65536+c*256+p; 2587 expire=u*16777216+i*65536+n*256+a;
2358 minimum=v*16777216+y*65536+m*256+f; 2588 minimum=o*16777216+r*65536+l*256+d;
2359 },x); 2589 },p);
2360 end 2590 end
2361 e.A=d; 2591 e.A=r;
2362 e.AAAA=d; 2592 e.AAAA=r;
2363 local d={ 2593 local r={
2364 __tostring=function(e) 2594 __tostring=function(e)
2365 return i("%d %s",e.pref,e.mx) 2595 return i("%d %s",e.pref,e.mx)
2366 end 2596 end
2367 }; 2597 };
2368 function e.MX(e) 2598 function e.MX(e)
2369 local i=a(e,3); 2599 local i=a(e,3);
2370 local a,e=o(e,1,2); 2600 local a,e=o(e,1,2);
2371 return t({ 2601 return t({
2372 pref=a*256+e; 2602 pref=a*256+e;
2373 mx=i; 2603 mx=i;
2374 },d); 2604 },r);
2375 end 2605 end
2376 local d={ 2606 local r={
2377 __tostring=function(e) 2607 __tostring=function(e)
2378 return i("%d %d %d %s",e.priority,e.weight,e.port,e.target); 2608 return i("%d %d %d %s",e.priority,e.weight,e.port,e.target);
2379 end 2609 end
2380 }; 2610 };
2381 function e.SRV(e) 2611 function e.SRV(e)
2382 local i=a(e,7); 2612 local a=a(e,7);
2383 local e,a,o,n,h,s=o(e,1,6); 2613 local e,o,h,n,i,s=o(e,1,6);
2384 return t({ 2614 return t({
2385 priority=e*256+a; 2615 priority=e*256+o;
2386 weight=o*256+n; 2616 weight=h*256+n;
2387 port=h*256+s; 2617 port=i*256+s;
2388 target=i; 2618 target=a;
2389 },d); 2619 },r);
2390 end 2620 end
2391 local d={__tostring=s}; 2621 local d={__tostring=s};
2392 function e.TXT(s) 2622 function e.TXT(s)
2393 local l=#s; 2623 local l=#s;
2394 local h,a,i={},1; 2624 local r,a,i={},1;
2395 repeat 2625 repeat
2396 i=o(s,a)or 0; 2626 i=o(s,a)or 0;
2397 n(h,r(s,a+1,a+i)); 2627 n(r,h(s,a+1,a+i));
2398 a=a+i+1; 2628 a=a+i+1;
2399 until a>=l; 2629 until a>=l;
2400 return t(h,d); 2630 return t(r,d);
2401 end 2631 end
2402 e.SPF=e.TXT; 2632 e.SPF=e.TXT;
2403 local d={ 2633 local r={
2404 [0]="PKIX-CA"; 2634 [0]="PKIX-CA";
2405 [1]="PKIX-EE"; 2635 [1]="PKIX-EE";
2406 [2]="DANE-TA"; 2636 [2]="DANE-TA";
2407 [3]="DANE-EE"; 2637 [3]="DANE-EE";
2408 [255]="PrivCert"; 2638 [255]="PrivCert";
2409 }; 2639 };
2410 local u={ 2640 local d={
2411 [0]="Cert", 2641 [0]="Cert",
2412 [1]="SPKI", 2642 [1]="SPKI",
2413 [255]="PrivSel", 2643 [255]="PrivSel",
2414 }; 2644 };
2415 local l={ 2645 local l={
2416 [0]="Full", 2646 [0]="Full",
2417 [1]="SHA2-256", 2647 [1]="SHA2-256",
2418 [2]="SHA2-512", 2648 [2]="SHA2-512",
2419 [255]="PrivMatch", 2649 [255]="PrivMatch",
2420 }; 2650 };
2421 local m={ 2651 local u={
2422 __tostring=function(e) 2652 __tostring=function(e)
2423 return i("%s %s %s %s", 2653 return i("%s %s %s %s",
2424 d[e.use]or e.use, 2654 r[e.use]or e.use,
2425 u[e.select]or e.select, 2655 d[e.select]or e.select,
2426 l[e.match]or e.match, 2656 l[e.match]or e.match,
2427 c(e.data)); 2657 m(e.data));
2428 end; 2658 end;
2429 __index={ 2659 __index={
2430 getUsage=function(e)return d[e.use]end; 2660 getUsage=function(e)return r[e.use]end;
2431 getSelector=function(e)return u[e.select]end; 2661 getSelector=function(e)return d[e.select]end;
2432 getMatchType=function(e)return l[e.match]end; 2662 getMatchType=function(e)return l[e.match]end;
2433 } 2663 }
2434 }; 2664 };
2435 function e.TLSA(e) 2665 function e.TLSA(e)
2436 local i,o,a=o(e,1,3); 2666 local i,o,a=o(e,1,3);
2437 return t({ 2667 return t({
2438 use=i; 2668 use=i;
2439 select=o; 2669 select=o;
2440 match=a; 2670 match=a;
2441 data=r(e,4); 2671 data=h(e,4);
2442 },m); 2672 },u);
2443 end 2673 end
2444 local o={"alpn";"no-default-alpn";"port";"ipv4hint";"ech";"ipv6hint"}; 2674 local o={"alpn";"no-default-alpn";"port";"ipv4hint";"ech";"ipv6hint"};
2445 t(o,{__index=function(t,e)return"key"..tostring(e);end}); 2675 t(o,{__index=function(t,e)return"key"..tostring(e);end});
2446 local m={ 2676 local f={
2447 __tostring=function(e) 2677 __tostring=function(e)
2448 local t={}; 2678 local t={};
2449 for a=1,#e.fields do 2679 for a=1,#e.fields do
2450 n(t,i("%s=%q",o[e.fields[a].key],tostring(e.fields[a].value))); 2680 n(t,i("%s=%q",o[e.fields[a].key],tostring(e.fields[a].value)));
2451 end 2681 end
2452 return i("%d %s %s",e.prio,e.name,s(t," ")); 2682 return i("%d %s %s",e.prio,e.name,s(t," "));
2453 end; 2683 end;
2454 }; 2684 };
2455 local r={__tostring=function(e)return s(e,", ");end} 2685 local u={__tostring=function(e)return s(e,", ");end}
2456 function e.SVCB(i) 2686 function e.SVCB(i)
2457 local o,s=i:byte(1,2); 2687 local o,s=i:byte(1,2);
2458 local u=o*256+s; 2688 local l=o*256+s;
2459 local l,o=a(i,3); 2689 local d,o=a(i,3);
2460 local d={}; 2690 local r={};
2461 while#i>o do 2691 while#i>o do
2462 local s,l=i:byte(o+0,o+1); 2692 local a,s=i:byte(o+0,o+1);
2463 local h,a=i:byte(o+2,o+3); 2693 local h,d=i:byte(o+2,o+3);
2464 local s=s*256+l; 2694 local s=a*256+s;
2465 local h=h*256+a; 2695 local h=h*256+d;
2466 local a=i:sub(o+4,o+4-1+h) 2696 local a=i:sub(o+4,o+4-1+h)
2467 if s==1 then 2697 if s==1 then
2468 a=t(e.TXT(a),r); 2698 a=t(e.TXT(a),u);
2469 elseif s==3 then 2699 elseif s==3 then
2470 local e,t=a:byte(1,2); 2700 local t,e=a:byte(1,2);
2471 local e=e*256+t; 2701 local e=t*256+e;
2472 a=e; 2702 a=e;
2473 elseif s==4 then 2703 elseif s==4 then
2474 local o={}; 2704 local o={};
2475 for t=1,#a,4 do 2705 for t=1,#a,4 do
2476 n(o,e.A(a:sub(t,t+3))); 2706 n(o,e.A(a:sub(t,t+3)));
2477 end 2707 end
2478 a=t(o,r); 2708 a=t(o,u);
2479 elseif s==6 then 2709 elseif s==6 then
2480 local o={}; 2710 local o={};
2481 for t=1,#a,16 do 2711 for t=1,#a,16 do
2482 n(o,e.AAAA(a:sub(t,t+15))); 2712 n(o,e.AAAA(a:sub(t,t+15)));
2483 end 2713 end
2484 a=t(o,r); 2714 a=t(o,u);
2485 end 2715 end
2486 n(d,{key=s,value=a,len=h}); 2716 n(r,{key=s,value=a,len=h});
2487 o=o+h+4; 2717 o=o+h+4;
2488 end 2718 end
2489 return t({ 2719 return t({
2490 prio=u,name=l,fields=d, 2720 prio=l,name=d,fields=r,
2491 },m); 2721 },f);
2492 end 2722 end
2493 e.HTTPS=e.SVCB; 2723 e.HTTPS=e.SVCB;
2494 local o={ 2724 local o={
2495 TLSA={ 2725 TLSA={
2496 use=d; 2726 use=r;
2497 select=u; 2727 select=d;
2498 match=l; 2728 match=l;
2499 }; 2729 };
2500 }; 2730 };
2501 local a={ 2731 local a={
2502 __tostring=function(e) 2732 __tostring=function(e)
2503 return i([[\# %d %s]],#e.raw,c(e.raw)); 2733 return i([[\# %d %s]],#e.raw,m(e.raw));
2504 end; 2734 end;
2505 }; 2735 };
2506 local function i(e) 2736 local function i(e)
2507 return t({raw=e},a); 2737 return t({raw=e},a);
2508 end 2738 end
2509 t(e,{__index=function()return i end}); 2739 t(e,{__index=function()return i end});
2510 return{ 2740 return{
2511 parsers=e; 2741 parsers=e;
2512 classes=h.classes; 2742 classes=c.classes;
2513 types=h.types; 2743 types=c.types;
2514 errors=h.errors; 2744 errors=c.errors;
2515 params=o; 2745 params=o;
2516 }; 2746 };
2517 end) 2747 end)
2518 package.preload['net.server']=(function(...) 2748 package.preload['net.server']=(function(...)
2519 local _ENV=_ENV; 2749 local _ENV=_ENV;
2525 end 2755 end
2526 _ENV=e; 2756 _ENV=e;
2527 _M=e; 2757 _M=e;
2528 return e; 2758 return e;
2529 end 2759 end
2530 local f=function(e) 2760 local w=function(e)
2531 return _G[e] 2761 return _G[e]
2532 end 2762 end
2533 local V,e=require("util.logger").init("socket"),table.concat; 2763 local W,e=require("util.logger").init("socket"),table.concat;
2534 local n=function(...)return V("debug",e{...});end 2764 local n=function(...)return W("debug",e{...});end
2535 local j=function(...)return V("warn",e{...});end 2765 local q=function(...)return W("warn",e{...});end
2536 local ie=1 2766 local ie=1
2537 local w=f"type" 2767 local f=w"type"
2538 local q=f"pairs" 2768 local j=w"pairs"
2539 local oe=f"ipairs" 2769 local ne=w"ipairs"
2540 local b=f"tonumber" 2770 local p=w"tonumber"
2541 local u=f"tostring" 2771 local u=w"tostring"
2542 local t=f"table" 2772 local e=w"table"
2543 local a=f"string" 2773 local a=w"string"
2544 local e=f"coroutine" 2774 local t=w"coroutine"
2545 local W=math.min 2775 local V=math.min
2546 local te=math.huge 2776 local se=math.huge
2547 local ye=t.concat 2777 local ye=e.concat
2548 local ae=t.insert 2778 local he=e.insert
2549 local be=a.sub 2779 local pe=a.sub
2550 local pe=e.wrap 2780 local ve=t.wrap
2551 local ke=e.yield 2781 local be=t.yield
2552 local x=f"socket"or require"socket" 2782 local T=w"socket"or require"socket"
2553 local P=x.gettime 2783 local P=T.gettime
2554 local e=require"util.net"; 2784 local e=require"util.net";
2555 local ge=e.pton; 2785 local ke=e.pton;
2556 local ve=require"util.sslconfig"; 2786 local ge=require"util.sslconfig";
2557 local Y,fe=pcall(require,"net.tls_luasec"); 2787 local Y,je=pcall(require,"net.tls_luasec");
2558 local ue=x.bind 2788 local me=T.bind
2559 local we=x.select 2789 local xe=T.select
2560 local U 2790 local H
2561 local Z 2791 local Z
2562 local ce 2792 local ce
2563 local me 2793 local qe
2564 local ee 2794 local ee
2565 local l 2795 local d
2796 local we
2797 local te
2798 local ae
2799 local re
2566 local de 2800 local de
2567 local J 2801 local ue
2568 local K 2802 local oe
2803 local r
2569 local le 2804 local le
2570 local re 2805 local B
2571 local ne 2806 local fe
2572 local Q 2807 local v
2573 local h
2574 local se
2575 local G
2576 local he
2577 local p
2578 local i 2808 local i
2579 local F 2809 local F
2580 local r 2810 local h
2581 local s 2811 local s
2582 local R 2812 local R
2583 local v 2813 local b
2584 local y 2814 local y
2585 local k 2815 local g
2586 local T 2816 local z
2587 local a 2817 local a
2588 local o 2818 local o
2589 local g 2819 local k
2820 local M
2590 local C 2821 local C
2591 local M 2822 local U
2823 local E
2592 local D 2824 local D
2593 local _ 2825 local X
2594 local H 2826 local l
2595 local B 2827 local L
2596 local d
2597 local S 2828 local S
2598 local I 2829 local I
2599 local O 2830 local O
2600 local N 2831 local N
2601 local L 2832 local _
2602 local z 2833 local x
2603 local E
2604 local A 2834 local A
2605 p={} 2835 v={}
2606 i={} 2836 i={}
2607 r={} 2837 h={}
2608 F={} 2838 F={}
2609 s={} 2839 s={}
2610 v={} 2840 b={}
2611 y={} 2841 y={}
2612 R={} 2842 R={}
2613 k={} 2843 g={}
2614 a=0 2844 a=0
2615 o=0 2845 o=0
2616 g=0 2846 k=0
2847 M=0
2617 C=0 2848 C=0
2618 M=0 2849 U=1
2619 D=1 2850 E=128
2620 _=128 2851 D=10
2621 H=10 2852 L=51e3*1024
2622 S=51e3*1024 2853 S=25e3*1024
2623 I=25e3*1024 2854 I=30
2624 O=30 2855 O=6e4
2625 N=6e4 2856 N=14*60
2626 L=14*60
2627 local e=package.config:sub(1,1)=="\\" 2857 local e=package.config:sub(1,1)=="\\"
2628 E=(e and math.huge)or x._SETSIZE or 1024 2858 x=(e and math.huge)or T._SETSIZE or 1024
2629 z=x._SETSIZE or 1024 2859 _=T._SETSIZE or 1024
2630 A=30 2860 A=30
2631 re=function(w,t,m,c,q,y,v) 2861 de=function(w,t,m,c,j,y,p)
2632 if t:getfd()>=E then 2862 if t:getfd()>=x then
2633 j("server.lua: Disallowed FD number: "..t:getfd()) 2863 q("server.lua: Disallowed FD number: "..t:getfd())
2634 t:close() 2864 t:close()
2635 return nil,"fd-too-large" 2865 return nil,"fd-too-large"
2636 end 2866 end
2637 local f=0 2867 local f=0
2638 local b,e=w.onconnect,w.ondisconnect 2868 local b,e=w.onconnect,w.ondisconnect
2639 local g=t.accept 2869 local k=t.accept
2640 local e={} 2870 local e={}
2641 e.shutdown=function()end 2871 e.shutdown=function()end
2642 e.ssl=function() 2872 e.ssl=function()
2643 return y~=nil 2873 return y~=nil
2644 end 2874 end
2652 e.resume() 2882 e.resume()
2653 end 2883 end
2654 end 2884 end
2655 e.close=function() 2885 e.close=function()
2656 t:close() 2886 t:close()
2657 o=h(r,t,o) 2887 o=r(h,t,o)
2658 a=h(i,t,a) 2888 a=r(i,t,a)
2659 p[m..":"..c]=nil; 2889 v[m..":"..c]=nil;
2660 s[t]=nil 2890 s[t]=nil
2661 e=nil 2891 e=nil
2662 t=nil 2892 t=nil
2663 n"server.lua: closed server handler and removed sockets from list" 2893 n"server.lua: closed server handler and removed sockets from list"
2664 end 2894 end
2665 e.pause=function(o) 2895 e.pause=function(o)
2666 if not e.paused then 2896 if not e.paused then
2667 a=h(i,t,a) 2897 a=r(i,t,a)
2668 if o then 2898 if o then
2669 s[t]=nil 2899 s[t]=nil
2670 t:close() 2900 t:close()
2671 t=nil; 2901 t=nil;
2672 end 2902 end
2675 end 2905 end
2676 end 2906 end
2677 e.resume=function() 2907 e.resume=function()
2678 if e.paused then 2908 if e.paused then
2679 if not t then 2909 if not t then
2680 t=ue(m,c,_); 2910 t=me(m,c,E);
2681 t:settimeout(0) 2911 t:settimeout(0)
2682 end 2912 end
2683 a=l(i,t,a) 2913 a=d(i,t,a)
2684 s[t]=e 2914 s[t]=e
2685 k[e]=nil 2915 g[e]=nil
2686 e.paused=false; 2916 e.paused=false;
2687 n("server.lua: server [",m,"]:",c," resumed") 2917 n("server.lua: server [",m,"]:",c," resumed")
2688 end 2918 end
2689 end 2919 end
2690 e.ip=function() 2920 e.ip=function()
2695 end 2925 end
2696 e.socket=function() 2926 e.socket=function()
2697 return t 2927 return t
2698 end 2928 end
2699 e.readbuffer=function() 2929 e.readbuffer=function()
2700 if a>=z or o>=z then 2930 if a>=_ or o>=_ then
2701 e.pause() 2931 e.pause()
2702 k[e]=d 2932 g[e]=l
2703 n("server.lua: refused new client connection: server full") 2933 n("server.lua: refused new client connection: server full")
2704 return false 2934 return false
2705 end 2935 end
2706 local t,o=g(t) 2936 local t,i=k(t)
2707 if t then 2937 if t then
2708 local a,o=t:getpeername() 2938 local a,o=t:getpeername()
2709 local e,i,t=G(e,w,t,a,c,o,q,y,v) 2939 local t,i,e=B(e,w,t,a,c,o,j,y,p)
2710 if t then 2940 if e then
2711 return false 2941 return false
2712 end 2942 end
2713 f=f+1 2943 f=f+1
2714 n("server.lua: accepted new client connection from ",u(a),":",u(o)," to ",u(c)) 2944 n("server.lua: accepted new client connection from ",u(a),":",u(o)," to ",u(c))
2715 if b and not v then 2945 if b and not p then
2716 return b(e); 2946 return b(t);
2717 end 2947 end
2718 return; 2948 return;
2719 elseif o then 2949 elseif i then
2720 n("server.lua: error with new client connection: ",u(o)) 2950 n("server.lua: error with new client connection: ",u(i))
2721 e.pause() 2951 e.pause()
2722 k[e]=d 2952 g[e]=l
2723 return false 2953 return false
2724 end 2954 end
2725 end 2955 end
2726 return e 2956 return e
2727 end 2957 end
2728 G=function(q,c,t,H,ee,L,D,w,te,P) 2958 B=function(k,c,t,N,te,I,D,p,Z,Q)
2729 if t:getfd()>=E then 2959 if t:getfd()>=x then
2730 j("server.lua: Disallowed FD number: "..t:getfd()) 2960 q("server.lua: Disallowed FD number: "..t:getfd())
2731 t:close() 2961 t:close()
2732 if q then 2962 if k then
2733 k[q]=d 2963 g[k]=l
2734 q.pause() 2964 k.pause()
2735 end 2965 end
2736 return nil,nil,"fd-too-large" 2966 return nil,nil,"fd-too-large"
2737 end 2967 end
2738 t:settimeout(0) 2968 t:settimeout(0)
2739 local b 2969 local v
2740 local N 2970 local E
2741 local z 2971 local _
2742 local B 2972 local J
2743 local x 2973 local j
2744 local K=c.onincoming 2974 local K=c.onincoming
2745 local G=c.onstatus 2975 local G=c.onstatus
2746 local k=c.ondisconnect 2976 local x=c.ondisconnect
2747 local J=c.onpredrain 2977 local P=c.onpredrain
2748 local X=c.ondrain 2978 local W=c.ondrain
2749 local ae=c.onreadtimeout; 2979 local ee=c.onreadtimeout;
2750 local Q=c.ondetach 2980 local U=c.ondetach
2751 local g={} 2981 local g={}
2752 local m=0 2982 local m=0
2753 local Z 2983 local X
2754 local V 2984 local F
2755 local f=0 2985 local f=0
2756 local p=false 2986 local w=false
2757 local E=false 2987 local T=false
2758 local W,F=0,0 2988 local V,B=0,0
2759 local _=S 2989 local O=L
2760 local O=I 2990 local S=S
2761 local e=g 2991 local e=g
2762 e.extra=P 2992 e.extra=Q
2763 if P then 2993 if Q then
2764 e.servername=P.servername 2994 e.servername=Q.servername
2765 end 2995 end
2766 e.dispatch=function() 2996 e.dispatch=function()
2767 return K 2997 return K
2768 end 2998 end
2769 e.disconnect=function() 2999 e.disconnect=function()
2770 return k 3000 return x
2771 end 3001 end
2772 e.onreadtimeout=ae; 3002 e.onreadtimeout=ee;
2773 e.setlistener=function(a,t,o) 3003 e.setlistener=function(a,t,o)
2774 if Q then 3004 if U then
2775 Q(a) 3005 U(a)
2776 end 3006 end
2777 K=t.onincoming 3007 K=t.onincoming
2778 k=t.ondisconnect 3008 x=t.ondisconnect
2779 G=t.onstatus 3009 G=t.onstatus
2780 J=t.onpredrain 3010 P=t.onpredrain
2781 X=t.ondrain 3011 W=t.ondrain
2782 e.onreadtimeout=t.onreadtimeout 3012 e.onreadtimeout=t.onreadtimeout
2783 Q=t.ondetach 3013 U=t.ondetach
2784 if t.onattach then 3014 if t.onattach then
2785 t.onattach(a,o) 3015 t.onattach(a,o)
2786 end 3016 end
2787 end 3017 end
2788 e._setpending=function() 3018 e._setpending=function()
2789 x=true 3019 j=true
2790 end 3020 end
2791 e.getstats=function() 3021 e.getstats=function()
2792 return F,W 3022 return B,V
2793 end 3023 end
2794 e.ssl=function() 3024 e.ssl=function()
2795 return B 3025 return J
2796 end 3026 end
2797 e.sslctx=function() 3027 e.sslctx=function()
2798 return w 3028 return p
2799 end 3029 end
2800 e.ssl_info=function() 3030 e.ssl_info=function()
2801 return t.info and t:info() 3031 return t.info and t:info()
2802 end 3032 end
2803 e.ssl_peercertificate=function() 3033 e.ssl_peercertificate=function()
2810 end 3040 end
2811 e.ssl_peerfinished=function() 3041 e.ssl_peerfinished=function()
2812 if not t.getpeerfinished then return nil,"not-implemented";end 3042 if not t.getpeerfinished then return nil,"not-implemented";end
2813 return t:getpeerfinished(); 3043 return t:getpeerfinished();
2814 end 3044 end
2815 e.send=function(n,i,a,o) 3045 e.send=function(n,i,o,a)
2816 return b(t,i,a,o) 3046 return v(t,i,o,a)
2817 end 3047 end
2818 e.receive=function(o,a) 3048 e.receive=function(a,o)
2819 return N(t,o,a) 3049 return E(t,a,o)
2820 end 3050 end
2821 e.shutdown=function(a) 3051 e.shutdown=function(a)
2822 return z(t,a) 3052 return _(t,a)
2823 end 3053 end
2824 e.setoption=function(i,o,a) 3054 e.setoption=function(i,a,o)
2825 if t.setoption then 3055 if t.setoption then
2826 return t:setoption(o,a); 3056 return t:setoption(a,o);
2827 end 3057 end
2828 return false,"setoption not implemented"; 3058 return false,"setoption not implemented";
2829 end 3059 end
2830 e.force_close=function(t,a) 3060 e.force_close=function(a,t)
2831 if m~=0 then 3061 if m~=0 then
2832 n("server.lua: discarding unwritten data for ",u(H),":",u(L)) 3062 n("server.lua: discarding unwritten data for ",u(N),":",u(I))
2833 m=0; 3063 m=0;
2834 end 3064 end
2835 return t:close(a); 3065 return a:close(t);
2836 end 3066 end
2837 e.close=function(l,d) 3067 e.close=function(l,d)
2838 if not e then return true;end 3068 if not e then return true;end
2839 a=h(i,t,a) 3069 a=r(i,t,a)
2840 v[e]=nil 3070 b[e]=nil
2841 if m~=0 then 3071 if m~=0 then
2842 e:sendbuffer() 3072 e:sendbuffer()
2843 if m~=0 then 3073 if m~=0 then
2844 if e then 3074 if e then
2845 e.write=nil 3075 e.write=nil
2846 end 3076 end
2847 Z=true 3077 X=true
2848 return false 3078 return false
2849 end 3079 end
2850 end 3080 end
2851 if t then 3081 if t then
2852 T=z and z(t) 3082 z=_ and _(t)
2853 t:close() 3083 t:close()
2854 o=h(r,t,o) 3084 o=r(h,t,o)
2855 s[t]=nil 3085 s[t]=nil
2856 t=nil 3086 t=nil
2857 else 3087 else
2858 n"server.lua: socket already closed" 3088 n"server.lua: socket already closed"
2859 end 3089 end
2860 if e then 3090 if e then
2861 y[e]=nil 3091 y[e]=nil
2862 R[e]=nil 3092 R[e]=nil
2863 local t=e; 3093 local t=e;
2864 e=nil 3094 e=nil
3095 if x then
3096 x(t,d or false);
3097 x=nil
3098 end
3099 end
2865 if k then 3100 if k then
2866 k(t,d or false); 3101 k.remove()
2867 k=nil
2868 end
2869 end
2870 if q then
2871 q.remove()
2872 end 3102 end
2873 n"server.lua: closed client handler and removed socket from list" 3103 n"server.lua: closed client handler and removed socket from list"
2874 return true 3104 return true
2875 end 3105 end
2876 e.server=function() 3106 e.server=function()
2877 return q 3107 return k
2878 end 3108 end
2879 e.ip=function() 3109 e.ip=function()
2880 return H 3110 return N
2881 end 3111 end
2882 e.serverport=function() 3112 e.serverport=function()
2883 return ee 3113 return te
2884 end 3114 end
2885 e.clientport=function() 3115 e.clientport=function()
2886 return L 3116 return I
2887 end 3117 end
2888 e.port=e.clientport 3118 e.port=e.clientport
2889 local k=function(i,a) 3119 local k=function(i,a)
2890 if not e then return false end 3120 if not e then return false end
2891 f=f+#a 3121 f=f+#a
2892 if f>_ then 3122 if f>O then
2893 R[e]="send buffer exceeded" 3123 R[e]="send buffer exceeded"
2894 return false 3124 return false
2895 elseif not E and t and not r[t]then 3125 elseif not T and t and not h[t]then
2896 o=l(r,t,o) 3126 o=d(h,t,o)
2897 end 3127 end
2898 m=m+1 3128 m=m+1
2899 g[m]=a 3129 g[m]=a
2900 if e then 3130 if e then
2901 y[e]=y[e]or d 3131 y[e]=y[e]or l
2902 end 3132 end
2903 return true 3133 return true
2904 end 3134 end
2905 e.write=k 3135 e.write=k
2906 e.bufferqueue=function(t) 3136 e.bufferqueue=function(t)
2912 e.set_mode=function(a,t) 3142 e.set_mode=function(a,t)
2913 D=t or D 3143 D=t or D
2914 return D 3144 return D
2915 end 3145 end
2916 e.set_send=function(a,t) 3146 e.set_send=function(a,t)
2917 b=t or b 3147 v=t or v
2918 return b 3148 return v
2919 end 3149 end
2920 e.bufferlen=function(o,a,t) 3150 e.bufferlen=function(o,t,a)
2921 _=t or _
2922 O=a or O 3151 O=a or O
2923 return f,O,_ 3152 S=t or S
2924 end 3153 return f,S,O
2925 e.lock_read=function(a,t) 3154 end
2926 j("server.lua, lock_read() is deprecated, use pause() and resume()") 3155 e.lock_read=function(t,a)
2927 if t==true then 3156 q("server.lua, lock_read() is deprecated, use pause() and resume()")
2928 return a:pause() 3157 if a==true then
2929 elseif t==false then 3158 return t:pause()
2930 return a:resume() 3159 elseif a==false then
2931 end 3160 return t:resume()
2932 return p 3161 end
3162 return w
2933 end 3163 end
2934 e.pause=function(o) 3164 e.pause=function(o)
2935 local o=a 3165 local o=a
2936 a=h(i,t,a) 3166 a=r(i,t,a)
2937 v[e]=nil 3167 b[e]=nil
2938 if a~=o then 3168 if a~=o then
2939 p=true 3169 w=true
2940 end 3170 end
2941 return p; 3171 return w;
2942 end 3172 end
2943 e.resume=function(o) 3173 e.resume=function(o)
2944 if p then 3174 if w then
2945 p=false 3175 w=false
2946 a=l(i,t,a) 3176 a=d(i,t,a)
2947 v[e]=d 3177 b[e]=l
2948 end 3178 end
2949 return p; 3179 return w;
2950 end 3180 end
2951 e.lock=function(t,a) 3181 e.lock=function(t,a)
2952 j("server.lua, lock() is deprecated") 3182 q("server.lua, lock() is deprecated")
2953 e.lock_read(t,a) 3183 e.lock_read(t,a)
2954 if a==true then 3184 if a==true then
2955 e.pause_writes(t) 3185 e.pause_writes(t)
2956 elseif a==false then 3186 elseif a==false then
2957 e.resume_writes(t) 3187 e.resume_writes(t)
2958 end 3188 end
2959 return p,E 3189 return w,T
2960 end 3190 end
2961 e.pause_writes=function(a) 3191 e.pause_writes=function(a)
2962 local a=o 3192 local a=o
2963 o=h(r,t,o) 3193 o=r(h,t,o)
2964 y[e]=nil 3194 y[e]=nil
2965 E=true 3195 T=true
2966 end 3196 end
2967 e.resume_writes=function(e) 3197 e.resume_writes=function(e)
2968 E=false 3198 T=false
2969 if f>0 and t then 3199 if f>0 and t then
2970 o=l(r,t,o) 3200 o=d(h,t,o)
2971 end 3201 end
2972 end 3202 end
2973 local v=function() 3203 local b=function()
2974 local o,t,a=N(t,D) 3204 local o,t,a=E(t,D)
2975 if not t or(t=="wantread"or t=="timeout")then 3205 if not t or(t=="wantread"or t=="timeout")then
2976 local a=o or a or"" 3206 local a=o or a or""
2977 local o=#a 3207 local o=#a
2978 if o>O then 3208 if o>S then
2979 e:close("receive buffer exceeded") 3209 e:close("receive buffer exceeded")
2980 return false 3210 return false
2981 end 3211 end
2982 local o=o*ie 3212 local o=o*ie
2983 F=F+o 3213 B=B+o
2984 M=M+o 3214 C=C+o
2985 v[e]=d 3215 b[e]=l
2986 if x then 3216 if j then
2987 x=nil 3217 j=nil
2988 if c.onconnect then 3218 if c.onconnect then
2989 c.onconnect(e) 3219 c.onconnect(e)
2990 end 3220 end
2991 end 3221 end
2992 return K(e,a,t) 3222 return K(e,a,t)
2993 else 3223 else
2994 n("server.lua: client ",u(H),":",u(L)," read error: ",u(t)) 3224 n("server.lua: client ",u(N),":",u(I)," read error: ",u(t))
2995 T=e and e:force_close(t) 3225 z=e and e:force_close(t)
2996 return false 3226 return false
2997 end 3227 end
2998 end 3228 end
2999 local p=function() 3229 local w=function()
3000 local w,a,i,s,l; 3230 local w,a,i,s,d;
3001 if t then 3231 if t then
3002 if x then 3232 if j then
3003 x=nil 3233 j=nil
3004 if c.onconnect then 3234 if c.onconnect then
3005 c.onconnect(e); 3235 c.onconnect(e);
3006 end 3236 end
3007 end 3237 end
3008 if J then 3238 if P then
3009 J(e); 3239 P(e);
3010 end 3240 end
3011 s=ye(g,"",1,m) 3241 s=ye(g,"",1,m)
3012 w,a,i=b(t,s,1,f) 3242 w,a,i=v(t,s,1,f)
3013 l=(w or i or 0)*ie 3243 d=(w or i or 0)*ie
3014 W=W+l 3244 V=V+d
3015 C=C+l 3245 M=M+d
3016 for e=m,1,-1 do 3246 for e=m,1,-1 do
3017 g[e]=nil 3247 g[e]=nil
3018 end 3248 end
3019 else 3249 else
3020 w,a,l=false,"unexpected close",0; 3250 w,a,d=false,"unexpected close",0;
3021 end 3251 end
3022 if w then 3252 if w then
3023 m=0 3253 m=0
3024 f=0 3254 f=0
3025 o=h(r,t,o) 3255 o=r(h,t,o)
3026 y[e]=nil 3256 y[e]=nil
3027 if X then 3257 if W then
3028 X(e) 3258 W(e)
3029 end 3259 end
3030 T=V and e:starttls(nil) 3260 z=F and e:starttls(nil)
3031 T=Z and e:force_close() 3261 z=X and e:force_close()
3032 return true 3262 return true
3033 elseif i and(a=="timeout"or a=="wantwrite")then 3263 elseif i and(a=="timeout"or a=="wantwrite")then
3034 s=be(s,i+1,f) 3264 s=pe(s,i+1,f)
3035 g[1]=s 3265 g[1]=s
3036 m=1 3266 m=1
3037 f=f-i 3267 f=f-i
3038 y[e]=d 3268 y[e]=l
3039 return true 3269 return true
3040 else 3270 else
3041 n("server.lua: client ",u(H),":",u(L)," write error: ",u(a)) 3271 n("server.lua: client ",u(N),":",u(I)," write error: ",u(a))
3042 T=e and e:force_close(a) 3272 z=e and e:force_close(a)
3043 return false 3273 return false
3044 end 3274 end
3045 end 3275 end
3046 local d; 3276 local l;
3047 function e.set_sslctx(y,t) 3277 function e.set_sslctx(y,t)
3048 w=t; 3278 p=t;
3049 local u,f 3279 local u,f
3050 d=pe(function(s) 3280 l=ve(function(s)
3051 local t 3281 local t
3052 for d=1,A do 3282 for l=1,A do
3053 o=(f and h(r,s,o))or o 3283 o=(f and r(h,s,o))or o
3054 a=(u and h(i,s,a))or a 3284 a=(u and r(i,s,a))or a
3055 u,f=nil,nil 3285 u,f=nil,nil
3056 d,t=s:dohandshake() 3286 l,t=s:dohandshake()
3057 if not t then 3287 if not t then
3058 n("server.lua: ssl handshake done") 3288 n("server.lua: ssl handshake done")
3059 e.readbuffer=v 3289 e.readbuffer=b
3060 e.sendbuffer=p 3290 e.sendbuffer=w
3061 d=G and G(e,"ssl-handshake-complete") 3291 l=G and G(e,"ssl-handshake-complete")
3062 if y.autostart_ssl and c.onconnect then 3292 if y.autostart_ssl and c.onconnect then
3063 c.onconnect(y); 3293 c.onconnect(y);
3064 if m~=0 then 3294 if m~=0 then
3065 o=l(r,s,o) 3295 o=d(h,s,o)
3066 end 3296 end
3067 end 3297 end
3068 a=l(i,s,a) 3298 a=d(i,s,a)
3069 return true 3299 return true
3070 else 3300 else
3071 if t=="wantwrite"then 3301 if t=="wantwrite"then
3072 o=l(r,s,o) 3302 o=d(h,s,o)
3073 f=true 3303 f=true
3074 elseif t=="wantread"then 3304 elseif t=="wantread"then
3075 a=l(i,s,a) 3305 a=d(i,s,a)
3076 u=true 3306 u=true
3077 else 3307 else
3078 break; 3308 break;
3079 end 3309 end
3080 t=nil; 3310 t=nil;
3081 ke() 3311 be()
3082 end 3312 end
3083 end 3313 end
3084 t=(t or"handshake too long"); 3314 t=(t or"handshake too long");
3085 n("server.lua: ",t); 3315 n("server.lua: ",t);
3086 T=e and e:force_close(t) 3316 z=e and e:force_close(t)
3087 return false,t 3317 return false,t
3088 end 3318 end
3089 ) 3319 )
3090 end 3320 end
3091 if Y then 3321 if Y then
3093 if c then 3323 if c then
3094 e:set_sslctx(c); 3324 e:set_sslctx(c);
3095 end 3325 end
3096 if m>0 then 3326 if m>0 then
3097 n"server.lua: we need to do tls, but delaying until send buffer empty" 3327 n"server.lua: we need to do tls, but delaying until send buffer empty"
3098 V=true 3328 F=true
3099 return 3329 return
3100 end 3330 end
3101 n("server.lua: attempting to start tls on "..u(t)) 3331 n("server.lua: attempting to start tls on "..u(t))
3102 local m,c=t 3332 local c,m=t
3103 t,c=w:wrap(t) 3333 t,m=p:wrap(t)
3104 if not t then 3334 if not t then
3105 n("server.lua: error while starting tls on client: ",u(c or"unknown error")) 3335 n("server.lua: error while starting tls on client: ",u(m or"unknown error"))
3106 return nil,c 3336 return nil,m
3107 end 3337 end
3108 if t.sni then 3338 if t.sni then
3109 if f.servername then 3339 if f.servername then
3110 t:sni(f.servername); 3340 t:sni(f.servername);
3111 elseif next(w._sni_contexts)~=nil then 3341 elseif next(p._sni_contexts)~=nil then
3112 t:sni(w._sni_contexts,true); 3342 t:sni(p._sni_contexts,true);
3113 end 3343 end
3114 end 3344 end
3115 t:settimeout(0) 3345 t:settimeout(0)
3116 b=t.send 3346 v=t.send
3117 N=t.receive 3347 E=t.receive
3118 z=U 3348 _=H
3119 s[t]=e 3349 s[t]=e
3120 a=l(i,t,a) 3350 a=d(i,t,a)
3121 a=h(i,m,a) 3351 a=r(i,c,a)
3122 o=h(r,m,o) 3352 o=r(h,c,o)
3123 s[m]=nil 3353 s[c]=nil
3124 e.starttls=nil 3354 e.starttls=nil
3125 V=nil 3355 F=nil
3126 B=true 3356 J=true
3127 e.readbuffer=d 3357 e.readbuffer=l
3128 e.sendbuffer=d 3358 e.sendbuffer=l
3129 return d(t) 3359 return l(t)
3130 end 3360 end
3131 end 3361 end
3132 e.readbuffer=v 3362 e.readbuffer=b
3133 e.sendbuffer=p 3363 e.sendbuffer=w
3134 b=t.send 3364 v=t.send
3135 N=t.receive 3365 E=t.receive
3136 z=(B and U)or t.shutdown 3366 _=(J and H)or t.shutdown
3137 s[t]=e 3367 s[t]=e
3138 a=l(i,t,a) 3368 a=d(i,t,a)
3139 if w and te and Y then 3369 if p and Z and Y then
3140 n"server.lua: auto-starting ssl negotiation..." 3370 n"server.lua: auto-starting ssl negotiation..."
3141 e.autostart_ssl=true; 3371 e.autostart_ssl=true;
3142 local t,e=e:starttls(w); 3372 local e,t=e:starttls(p);
3143 if t==false then 3373 if e==false then
3144 return nil,nil,e 3374 return nil,nil,t
3145 end 3375 end
3146 end 3376 end
3147 return e,t 3377 return e,t
3148 end 3378 end
3149 U=function() 3379 H=function()
3150 end 3380 end
3151 me=function() 3381 qe=function()
3152 return false 3382 return false
3153 end 3383 end
3154 l=function(a,t,e) 3384 d=function(t,a,e)
3155 if not a[t]then 3385 if not t[a]then
3156 e=e+1 3386 e=e+1
3157 a[e]=t 3387 t[e]=a
3158 a[t]=e 3388 t[a]=e
3159 end 3389 end
3160 return e; 3390 return e;
3161 end 3391 end
3162 h=function(e,i,t) 3392 r=function(e,i,t)
3163 local o=e[i] 3393 local o=e[i]
3164 if o then 3394 if o then
3165 e[i]=nil 3395 e[i]=nil
3166 local a=e[t] 3396 local a=e[t]
3167 e[t]=nil 3397 e[t]=nil
3171 end 3401 end
3172 return t-1 3402 return t-1
3173 end 3403 end
3174 return t 3404 return t
3175 end 3405 end
3176 Q=function(e) 3406 oe=function(e)
3177 o=h(r,e,o) 3407 o=r(h,e,o)
3178 a=h(i,e,a) 3408 a=r(i,e,a)
3179 s[e]=nil 3409 s[e]=nil
3180 e:close() 3410 e:close()
3181 end 3411 end
3182 local function c(e,t,o) 3412 local function m(e,t,o)
3183 local a; 3413 local a;
3184 local i=t.sendbuffer; 3414 local i=t.sendbuffer;
3185 function t.sendbuffer() 3415 function t.sendbuffer()
3186 i(t); 3416 i(t);
3187 if a and t.bufferlen()<o then 3417 if a and t.bufferlen()<o then
3197 e:lock_read(true); 3427 e:lock_read(true);
3198 end 3428 end
3199 end 3429 end
3200 e:set_mode("*a"); 3430 e:set_mode("*a");
3201 end 3431 end
3202 J=function(e,t,d,h) 3432 te=function(t,e,l,h)
3203 e=e or"*" 3433 t=t or"*"
3204 h=h or{} 3434 h=h or{}
3205 local o 3435 local o
3206 local r=h.tls_ctx; 3436 local r=h.tls_ctx;
3207 local c=h.tls_direct; 3437 local c=h.tls_direct;
3208 local u=h.read_size; 3438 local u=h.read_size;
3209 if w(d)~="table"then 3439 if f(l)~="table"then
3210 o="invalid listener table" 3440 o="invalid listener table"
3211 elseif w(e)~="string"then 3441 elseif f(t)~="string"then
3212 o="invalid address" 3442 o="invalid address"
3213 elseif w(t)~="number"or not(t>=0 and t<=65535)then 3443 elseif f(e)~="number"or not(e>=0 and e<=65535)then
3214 o="invalid port" 3444 o="invalid port"
3215 elseif p[e..":"..t]then 3445 elseif v[t..":"..e]then
3216 o="listeners on '["..e.."]:"..t.."' already exist" 3446 o="listeners on '["..t.."]:"..e.."' already exist"
3217 elseif r and not Y then 3447 elseif r and not Y then
3218 o="luasec not found" 3448 o="luasec not found"
3219 end 3449 end
3220 if o then 3450 if o then
3221 j("server.lua, [",e,"]:",t,": ",o) 3451 q("server.lua, [",t,"]:",e,": ",o)
3222 return nil,o 3452 return nil,o
3223 end 3453 end
3224 local o,h=ue(e,t,_) 3454 local o,h=me(t,e,E)
3225 if h then 3455 if h then
3226 j("server.lua, [",e,"]:",t,": ",h) 3456 q("server.lua, [",t,"]:",e,": ",h)
3227 return nil,h 3457 return nil,h
3228 end 3458 end
3229 local h,d=re(d,o,e,t,u,r,c) 3459 local h,l=de(l,o,t,e,u,r,c)
3230 if not h then 3460 if not h then
3231 o:close() 3461 o:close()
3232 return nil,d 3462 return nil,l
3233 end 3463 end
3234 o:settimeout(0) 3464 o:settimeout(0)
3235 a=l(i,o,a) 3465 a=d(i,o,a)
3236 p[e..":"..t]=h 3466 v[t..":"..e]=h
3237 s[o]=h 3467 s[o]=h
3238 n("server.lua: new "..(r and"ssl "or"").."server listener on '[",e,"]:",t,"'") 3468 n("server.lua: new "..(r and"ssl "or"").."server listener on '[",t,"]:",e,"'")
3239 return h 3469 return h
3240 end 3470 end
3241 de=function(i,o,t,a,e) 3471 we=function(i,o,a,t,e)
3242 return J(i,o,t,{ 3472 return te(i,o,a,{
3243 read_size=a; 3473 read_size=t;
3244 tls_ctx=e; 3474 tls_ctx=e;
3245 tls_direct=e and true or false; 3475 tls_direct=e and true or false;
3246 }); 3476 });
3247 end 3477 end
3478 re=function(t,e)
3479 return v[t..":"..e];
3480 end
3248 le=function(e,t) 3481 le=function(e,t)
3249 return p[e..":"..t]; 3482 local a=v[e..":"..t]
3250 end
3251 se=function(e,t)
3252 local a=p[e..":"..t]
3253 if not a then 3483 if not a then
3254 return nil,"no server found on '["..e.."]:"..u(t).."'" 3484 return nil,"no server found on '["..e.."]:"..u(t).."'"
3255 end 3485 end
3256 a:close() 3486 a:close()
3257 p[e..":"..t]=nil 3487 v[e..":"..t]=nil
3258 return true 3488 return true
3259 end 3489 end
3260 ee=function() 3490 ee=function()
3261 for t,e in q(s)do 3491 for e,t in j(s)do
3262 e:close() 3492 t:close()
3263 s[t]=nil 3493 s[e]=nil
3264 end 3494 end
3265 a=0 3495 a=0
3266 o=0 3496 o=0
3267 g=0 3497 k=0
3268 p={} 3498 v={}
3269 i={} 3499 i={}
3270 r={} 3500 h={}
3271 F={} 3501 F={}
3272 s={} 3502 s={}
3273 end 3503 end
3274 ne=function() 3504 ue=function()
3275 return{ 3505 return{
3276 select_timeout=D; 3506 select_timeout=U;
3277 tcp_backlog=_; 3507 tcp_backlog=E;
3278 max_send_buffer_size=S; 3508 max_send_buffer_size=L;
3279 max_receive_buffer_size=I; 3509 max_receive_buffer_size=S;
3280 select_idle_check_interval=O; 3510 select_idle_check_interval=I;
3281 send_timeout=N; 3511 send_timeout=O;
3282 read_timeout=L; 3512 read_timeout=N;
3283 max_connections=z; 3513 max_connections=_;
3284 max_ssl_handshake_roundtrips=A; 3514 max_ssl_handshake_roundtrips=A;
3285 highest_allowed_fd=E; 3515 highest_allowed_fd=x;
3286 accept_retry_interval=H; 3516 accept_retry_interval=D;
3287 } 3517 }
3288 end 3518 end
3289 he=function(e) 3519 fe=function(e)
3290 if w(e)~="table"then 3520 if f(e)~="table"then
3291 return nil,"invalid settings table" 3521 return nil,"invalid settings table"
3292 end 3522 end
3293 D=b(e.select_timeout)or D 3523 U=p(e.select_timeout)or U
3294 S=b(e.max_send_buffer_size)or S 3524 L=p(e.max_send_buffer_size)or L
3295 I=b(e.max_receive_buffer_size)or I 3525 S=p(e.max_receive_buffer_size)or S
3296 O=b(e.select_idle_check_interval)or O 3526 I=p(e.select_idle_check_interval)or I
3297 _=b(e.tcp_backlog)or _ 3527 E=p(e.tcp_backlog)or E
3298 N=b(e.send_timeout)or N 3528 O=p(e.send_timeout)or O
3299 L=b(e.read_timeout)or L 3529 N=p(e.read_timeout)or N
3300 H=b(e.accept_retry_interval)or H 3530 D=p(e.accept_retry_interval)or D
3301 z=e.max_connections or z 3531 _=e.max_connections or _
3302 A=e.max_ssl_handshake_roundtrips or A 3532 A=e.max_ssl_handshake_roundtrips or A
3303 E=e.highest_allowed_fd or E 3533 x=e.highest_allowed_fd or x
3304 return true 3534 return true
3305 end 3535 end
3306 K=function(e) 3536 ae=function(e)
3307 if w(e)~="function"then 3537 if f(e)~="function"then
3308 return nil,"invalid listener function" 3538 return nil,"invalid listener function"
3309 end 3539 end
3310 g=g+1 3540 k=k+1
3311 F[g]=e 3541 F[k]=e
3312 return true 3542 return true
3313 end 3543 end
3314 local u do 3544 local u do
3315 local a={}; 3545 local i={};
3316 local t={}; 3546 local t={};
3317 function u(e,a) 3547 function u(e,o)
3318 local o=P(); 3548 local a=P();
3319 e=e+o; 3549 e=e+a;
3320 if e>=o then 3550 if e>=a then
3321 ae(t,{e,a}); 3551 he(t,{e,o});
3322 else 3552 else
3323 local e=a(o); 3553 local e=o(a);
3324 if e and w(e)=="number"then 3554 if e and f(e)=="number"then
3325 return u(e,a); 3555 return u(e,o);
3326 end 3556 end
3327 end 3557 end
3328 end 3558 end
3329 K(function(o) 3559 ae(function(a)
3330 if#t>0 then 3560 if#t>0 then
3331 for o,e in q(t)do 3561 for a,e in j(t)do
3332 ae(a,e); 3562 he(i,e);
3333 end 3563 end
3334 t={}; 3564 t={};
3335 end 3565 end
3336 local e=te; 3566 local e=se;
3337 for s,t in q(a)do 3567 for s,t in j(i)do
3338 local n,i=t[1],t[2]; 3568 local n,o=t[1],t[2];
3339 if n<=o then 3569 if n<=a then
3340 a[s]=nil; 3570 i[s]=nil;
3341 local t=i(o); 3571 local t=o(a);
3342 if w(t)=="number"then 3572 if f(t)=="number"then
3343 u(t,i); 3573 u(t,o);
3344 e=W(e,t); 3574 e=V(e,t);
3345 end 3575 end
3346 else 3576 else
3347 e=W(e,n-o); 3577 e=V(e,n-a);
3348 end 3578 end
3349 end 3579 end
3350 return e; 3580 return e;
3351 end); 3581 end);
3352 end 3582 end
3353 ce=function() 3583 ce=function()
3354 return M,C,a,o,g 3584 return C,M,a,o,k
3355 end 3585 end
3356 local e; 3586 local t;
3357 local function b(t) 3587 local function c(e)
3358 e=t; 3588 t=e;
3359 end 3589 end
3360 Z=function(t) 3590 Z=function(e)
3361 if e then return"quitting";end 3591 if t then return"quitting";end
3362 if t then e="once";end 3592 if e then t="once";end
3363 d=P() 3593 l=P()
3364 repeat 3594 repeat
3365 local t=te; 3595 local e=se;
3366 for e=1,g do 3596 for t=1,k do
3367 local e=F[e](d) 3597 local t=F[t](l)
3368 if e then t=W(t,e);end 3598 if t then e=V(e,t);end
3369 end 3599 end
3370 local t,a,o=we(i,r,W(D,t)) 3600 local e,a,o=xe(i,h,V(U,e))
3371 for t,e in oe(t)do 3601 for t,e in ne(e)do
3372 local t=s[e] 3602 local t=s[e]
3373 if t then 3603 if t then
3374 t:readbuffer() 3604 t:readbuffer()
3375 else 3605 else
3376 Q(e) 3606 oe(e)
3377 n"server.lua: found no handler and closed socket (readlist)" 3607 n"server.lua: found no handler and closed socket (readlist)"
3378 end 3608 end
3379 end 3609 end
3380 for e,t in oe(a)do 3610 for e,t in ne(a)do
3381 local e=s[t] 3611 local e=s[t]
3382 if e then 3612 if e then
3383 e:sendbuffer() 3613 e:sendbuffer()
3384 else 3614 else
3385 Q(t) 3615 oe(t)
3386 n"server.lua: found no handler and closed socket (writelist)" 3616 n"server.lua: found no handler and closed socket (writelist)"
3387 end 3617 end
3388 end 3618 end
3389 for e,t in q(R)do 3619 for e,t in j(R)do
3390 e.disconnect()(e,t) 3620 e.disconnect()(e,t)
3391 e:force_close() 3621 e:force_close()
3392 R[e]=nil; 3622 R[e]=nil;
3393 end 3623 end
3394 d=P() 3624 l=P()
3395 if d-B>O then 3625 if l-X>I then
3396 B=d 3626 X=l
3397 for e,t in q(y)do 3627 for e,t in j(y)do
3398 if d-t>N then 3628 if l-t>O then
3399 e.disconnect()(e,"send timeout") 3629 e.disconnect()(e,"send timeout")
3400 e:force_close() 3630 e:force_close()
3401 end 3631 end
3402 end 3632 end
3403 for e,t in q(v)do 3633 for e,t in j(b)do
3404 if d-t>L then 3634 if l-t>N then
3405 if not(e.onreadtimeout)or e:onreadtimeout()~=true then 3635 if not(e.onreadtimeout)or e:onreadtimeout()~=true then
3406 e.disconnect()(e,"read timeout") 3636 e.disconnect()(e,"read timeout")
3407 e:close() 3637 e:close()
3408 else 3638 else
3409 v[e]=d 3639 b[e]=l
3410 end 3640 end
3411 end 3641 end
3412 end 3642 end
3413 end 3643 end
3414 for e,t in q(k)do 3644 for e,t in j(g)do
3415 if d-t>H then 3645 if l-t>D then
3416 k[e]=nil; 3646 g[e]=nil;
3417 e.resume(); 3647 e.resume();
3418 end 3648 end
3419 end 3649 end
3420 until e; 3650 until t;
3421 if e=="once"then e=nil;return;end 3651 if t=="once"then t=nil;return;end
3422 ee(); 3652 ee();
3423 return"quitting" 3653 return"quitting"
3424 end 3654 end
3425 local function p() 3655 local function g()
3426 return Z(true); 3656 return Z(true);
3427 end 3657 end
3428 local function m() 3658 local function k()
3429 return"select"; 3659 return"select";
3430 end 3660 end
3431 local d=function(e,t,h,d,c,n,u) 3661 local n=function(c,u,l,t,e,n,r)
3432 local t,e,h=G(nil,d,e,t,h,"clientport",c,n,n,u) 3662 local e,t,r=B(nil,t,c,u,l,"clientport",e,n,n,r)
3433 if not t then return nil,h end 3663 if not e then return nil,r end
3434 s[e]=t 3664 s[t]=e
3435 if not n then 3665 if not n then
3436 t._setpending() 3666 e._setpending()
3437 a=l(i,e,a) 3667 a=d(i,t,a)
3438 o=l(r,e,o) 3668 o=d(h,t,o)
3439 end 3669 end
3440 return t,e 3670 return e,t
3441 end 3671 end
3442 local g=function(o,t,i,r,n,a,s) 3672 local v=function(o,t,i,h,s,a,d)
3443 local e 3673 local e
3444 if w(i)~="table"then 3674 if f(i)~="table"then
3445 e="invalid listener table" 3675 e="invalid listener table"
3446 elseif w(o)~="string"then 3676 elseif f(o)~="string"then
3447 e="invalid address" 3677 e="invalid address"
3448 elseif w(t)~="number"or not(t>=0 and t<=65535)then 3678 elseif f(t)~="number"or not(t>=0 and t<=65535)then
3449 e="invalid port" 3679 e="invalid port"
3450 elseif n and not Y then 3680 elseif s and not Y then
3451 e="luasec not found" 3681 e="luasec not found"
3452 end 3682 end
3453 if not a then 3683 if not a then
3454 local e=ge(o); 3684 local e=ke(o);
3455 if not e then return nil,"invalid-ip";end 3685 if not e then return nil,"invalid-ip";end
3456 if#e==16 then 3686 if#e==16 then
3457 a="tcp6"; 3687 a="tcp6";
3458 elseif#e==4 then 3688 elseif#e==4 then
3459 a="tcp4"; 3689 a="tcp4";
3460 end 3690 end
3461 end 3691 end
3462 local a=x[a]; 3692 local a=T[a];
3463 if w(a)~="function"then 3693 if f(a)~="function"then
3464 e="invalid socket type" 3694 e="invalid socket type"
3465 end 3695 end
3466 if e then 3696 if e then
3467 j("server.lua, addclient: ",e) 3697 q("server.lua, addclient: ",e)
3468 return nil,e 3698 return nil,e
3469 end 3699 end
3470 local e,a=a() 3700 local e,a=a()
3471 if a then 3701 if a then
3472 return nil,a 3702 return nil,a
3473 end 3703 end
3474 e:settimeout(0) 3704 e:settimeout(0)
3475 local h,a=e:setpeername(o,t) 3705 local r,a=e:setpeername(o,t)
3476 if h or a=="timeout"or a=="Operation already in progress"then 3706 if r or a=="timeout"or a=="Operation already in progress"then
3477 return d(e,o,t,i,r,n,s) 3707 return n(e,o,t,i,h,s,d)
3478 else 3708 else
3479 return nil,a 3709 return nil,a
3480 end 3710 end
3481 end 3711 end
3482 local k=function(e) 3712 local p=function(e)
3483 local e=e.conn; 3713 local e=e.conn;
3484 o=h(r,e,o) 3714 o=r(h,e,o)
3485 a=h(i,e,a) 3715 a=r(i,e,a)
3486 s[e]=nil 3716 s[e]=nil
3487 end; 3717 end;
3488 local i=function(d,n,t) 3718 local a=function(n,t,l)
3489 local e=d.conn 3719 local e=n.conn
3490 s[e]=d 3720 s[e]=n
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
3498 if t~=nil then 3721 if t~=nil then
3499 if t then 3722 if t then
3500 o=l(r,e,o) 3723 a=d(i,e,a)
3501 else 3724 else
3502 a=h(i,e,a) 3725 o=r(h,e,o)
3503 end 3726 end
3504 end 3727 end
3505 end 3728 if l~=nil then
3506 local t=function(e,o,a) 3729 if l then
3730 o=d(h,e,o)
3731 else
3732 a=r(i,e,a)
3733 end
3734 end
3735 end
3736 local a=function(e,i,o)
3507 local t=e 3737 local t=e
3508 if w(e)=="number"then 3738 if f(e)=="number"then
3509 t={getfd=function()return e;end} 3739 t={getfd=function()return e;end}
3510 end 3740 end
3511 local e={ 3741 local e={
3512 conn=t; 3742 conn=t;
3513 readbuffer=o or U; 3743 readbuffer=i or H;
3514 sendbuffer=a or U; 3744 sendbuffer=o or H;
3515 close=k; 3745 close=p;
3516 setflags=i; 3746 setflags=a;
3517 }; 3747 };
3518 i(e,o,a) 3748 a(e,i,o)
3519 return e 3749 return e
3520 end 3750 end
3521 f"setmetatable"(s,{__mode="k"}) 3751 w"setmetatable"(s,{__mode="k"})
3522 f"setmetatable"(v,{__mode="k"}) 3752 w"setmetatable"(b,{__mode="k"})
3523 f"setmetatable"(y,{__mode="k"}) 3753 w"setmetatable"(y,{__mode="k"})
3524 B=P() 3754 X=P()
3525 local function a(e) 3755 local function o(e)
3526 local t=V; 3756 local t=W;
3527 if e then 3757 if e then
3528 V=e; 3758 W=e;
3529 end 3759 end
3530 return t; 3760 return t;
3531 end 3761 end
3532 return{ 3762 return{
3533 _addtimer=K, 3763 _addtimer=ae,
3534 add_task=u; 3764 add_task=u;
3535 addclient=g, 3765 addclient=v,
3536 wrapclient=d, 3766 wrapclient=n,
3537 watchfd=t, 3767 watchfd=a,
3538 loop=Z, 3768 loop=Z,
3539 link=c, 3769 link=m,
3540 step=p, 3770 step=g,
3541 stats=ce, 3771 stats=ce,
3542 closeall=ee, 3772 closeall=ee,
3543 addserver=de, 3773 addserver=we,
3544 listen=J, 3774 listen=te,
3545 getserver=le, 3775 getserver=re,
3546 setlogger=a, 3776 setlogger=o,
3547 getsettings=ne, 3777 getsettings=ue,
3548 setquitting=b, 3778 setquitting=c,
3549 removeserver=se, 3779 removeserver=le,
3550 get_backend=m, 3780 get_backend=k,
3551 changesettings=he, 3781 changesettings=fe,
3552 tls_builder=function(e) 3782 tls_builder=function(e)
3553 return ve._new(fe.new_context,e) 3783 return ge._new(je.new_context,e)
3554 end, 3784 end,
3555 } 3785 }
3556 end) 3786 end)
3557 package.preload['util.xmppstream']=(function(...) 3787 package.preload['util.xmppstream']=(function(...)
3558 local _ENV=_ENV; 3788 local _ENV=_ENV;
3565 _ENV=e; 3795 _ENV=e;
3566 _M=e; 3796 _M=e;
3567 return e; 3797 return e;
3568 end 3798 end
3569 local e=require"lxp"; 3799 local e=require"lxp";
3570 local g=require"util.stanza"; 3800 local x=require"util.stanza";
3571 local y=g.stanza_mt; 3801 local k=x.stanza_mt;
3572 local w=error; 3802 local w=error;
3573 local t=tostring; 3803 local t=tostring;
3574 local d=table.insert; 3804 local d=table.insert;
3575 local b=table.concat; 3805 local g=table.concat;
3576 local E=table.remove; 3806 local z=table.remove;
3577 local p=setmetatable; 3807 local b=setmetatable;
3578 local T=pcall(e.new,{StartDoctypeDecl=false}); 3808 local T=pcall(e.new,{StartDoctypeDecl=false});
3579 local _=pcall(e.new,{XmlDecl=false}); 3809 local A=pcall(e.new,{XmlDecl=false});
3580 local a=not not e.new({}).getcurrentbytecount; 3810 local a=not not e.new({}).getcurrentbytecount;
3581 local x=1024*1024*1; 3811 local _=1024*1024*1;
3582 local _ENV=nil; 3812 local _ENV=nil;
3583 local v=e.new; 3813 local j=e.new;
3584 local j={ 3814 local E={
3585 ["http://www.w3.org/XML/1998/namespace\1lang"]="xml:lang"; 3815 ["http://www.w3.org/XML/1998/namespace\1lang"]="xml:lang";
3586 ["http://www.w3.org/XML/1998/namespace\1space"]="xml:space"; 3816 ["http://www.w3.org/XML/1998/namespace\1space"]="xml:space";
3587 ["http://www.w3.org/XML/1998/namespace\1base"]="xml:base"; 3817 ["http://www.w3.org/XML/1998/namespace\1base"]="xml:base";
3588 ["http://www.w3.org/XML/1998/namespace\1id"]="xml:id"; 3818 ["http://www.w3.org/XML/1998/namespace\1id"]="xml:id";
3589 }; 3819 };
3590 local h="http://etherx.jabber.org/streams"; 3820 local h="http://etherx.jabber.org/streams";
3591 local r="\1"; 3821 local r="\1";
3592 local k="^([^"..r.."]*)"..r.."?(.*)$"; 3822 local q="^([^"..r.."]*)"..r.."?(.*)$";
3593 local function o()end 3823 local function o()end
3594 local function q(n,e,s) 3824 local function v(n,e,s)
3595 local i={}; 3825 local i={};
3596 local g=e.streamopened; 3826 local p=e.streamopened;
3597 local v=e.streamclosed; 3827 local y=e.streamclosed;
3598 local l=e.error or 3828 local u=e.error or
3599 function(o,a,e) 3829 function(o,a,e)
3600 w("XML stream error: "..t(a)..(e and": "..t(e)or""),2); 3830 w("XML stream error: "..t(a)..(e and": "..t(e)or""),2);
3601 end; 3831 end;
3602 local x=e.handlestanza; 3832 local v=e.handlestanza;
3603 s=s or o; 3833 s=s or o;
3604 local t=e.stream_ns or h; 3834 local t=e.stream_ns or h;
3605 local m=e.stream_tag or"stream"; 3835 local c=e.stream_tag or"stream";
3606 if t~=""then 3836 if t~=""then
3607 m=t..r..m; 3837 c=t..r..c;
3608 end 3838 end
3609 local z=t..r..(e.error_tag or"error"); 3839 local x=t..r..(e.error_tag or"error");
3610 local q=e.default_ns; 3840 local j=e.default_ns;
3611 local f="en"; 3841 local m="en";
3612 local u={}; 3842 local l={};
3613 local h,e={}; 3843 local h,e={};
3614 local t=0; 3844 local t=0;
3615 local r=0; 3845 local r=0;
3616 function i:StartElement(c,o) 3846 function i:StartElement(f,o)
3617 if e and#h>0 then 3847 if e and#h>0 then
3618 d(e,b(h)); 3848 d(e,g(h));
3619 h={}; 3849 h={};
3620 end 3850 end
3621 local h,i=c:match(k); 3851 local h,i=f:match(q);
3622 if i==""then 3852 if i==""then
3623 h,i="",h; 3853 h,i="",h;
3624 end 3854 end
3625 if h~=q or r>0 then 3855 if h~=j or r>0 then
3626 o.xmlns=h; 3856 o.xmlns=h;
3627 r=r+1; 3857 r=r+1;
3628 end 3858 end
3629 for t=1,#o do 3859 for t=1,#o do
3630 local e=o[t]; 3860 local e=o[t];
3631 o[t]=nil; 3861 o[t]=nil;
3632 local t=j[e]; 3862 local t=E[e];
3633 if t then 3863 if t then
3634 o[t]=o[e]; 3864 o[t]=o[e];
3635 o[e]=nil; 3865 o[e]=nil;
3636 end 3866 end
3637 end 3867 end
3638 if not e then 3868 if not e then
3639 if a then 3869 if a then
3640 t=self:getcurrentbytecount(); 3870 t=self:getcurrentbytecount();
3641 end 3871 end
3642 if n.notopen then 3872 if n.notopen then
3643 if c==m then 3873 if f==c then
3644 r=0; 3874 r=0;
3645 f=o["xml:lang"]or f; 3875 m=o["xml:lang"]or m;
3646 if g then 3876 if p then
3647 if a then 3877 if a then
3648 s(t); 3878 s(t);
3649 t=0; 3879 t=0;
3650 end 3880 end
3651 g(n,o); 3881 p(n,o);
3652 end 3882 end
3653 else 3883 else
3654 l(n,"no-stream",c); 3884 u(n,"no-stream",f);
3655 end 3885 end
3656 return; 3886 return;
3657 end 3887 end
3658 if h=="jabber:client"and i~="iq"and i~="presence"and i~="message"then 3888 if h=="jabber:client"and i~="iq"and i~="presence"and i~="message"then
3659 l(n,"invalid-top-level-element"); 3889 u(n,"invalid-top-level-element");
3660 end 3890 end
3661 e=p({name=i,attr=o,tags={}},y); 3891 e=b({name=i,attr=o,tags={}},k);
3662 else 3892 else
3663 if a then 3893 if a then
3664 t=t+self:getcurrentbytecount(); 3894 t=t+self:getcurrentbytecount();
3665 end 3895 end
3666 d(u,e); 3896 d(l,e);
3667 local t=e; 3897 local t=e;
3668 e=p({name=i,attr=o,tags={}},y); 3898 e=b({name=i,attr=o,tags={}},k);
3669 d(t,e); 3899 d(t,e);
3670 d(t.tags,e); 3900 d(t.tags,e);
3671 end 3901 end
3672 end 3902 end
3673 function i:StartCdataSection() 3903 function i:StartCdataSection()
3705 if r>0 then 3935 if r>0 then
3706 r=r-1; 3936 r=r-1;
3707 end 3937 end
3708 if e then 3938 if e then
3709 if#h>0 then 3939 if#h>0 then
3710 d(e,b(h)); 3940 d(e,g(h));
3711 h={}; 3941 h={};
3712 end 3942 end
3713 if#u==0 then 3943 if#l==0 then
3714 if a then 3944 if a then
3715 s(t); 3945 s(t);
3716 end 3946 end
3717 t=0; 3947 t=0;
3718 if e.attr["xml:lang"]==nil then 3948 if e.attr["xml:lang"]==nil then
3719 e.attr["xml:lang"]=f; 3949 e.attr["xml:lang"]=m;
3720 end 3950 end
3721 if o~=z then 3951 if o~=x then
3722 x(n,e); 3952 v(n,e);
3723 else 3953 else
3724 l(n,"stream-error",e); 3954 u(n,"stream-error",e);
3725 end 3955 end
3726 e=nil; 3956 e=nil;
3727 else 3957 else
3728 e=E(u); 3958 e=z(l);
3729 end 3959 end
3730 else 3960 else
3731 if a then 3961 if a then
3732 s(t); 3962 s(t);
3733 end 3963 end
3734 if v then 3964 if y then
3735 v(n); 3965 y(n);
3736 end 3966 end
3737 end 3967 end
3738 end 3968 end
3739 local function o(e) 3969 local function o(e)
3740 l(n,"parse-error","restricted-xml","Restricted XML, see RFC 6120 section 11.1."); 3970 u(n,"parse-error","restricted-xml","Restricted XML, see RFC 6120 section 11.1.");
3741 if not e.stop or not e:stop()then 3971 if not e.stop or not e:stop()then
3742 w("Failed to abort parsing"); 3972 w("Failed to abort parsing");
3743 end 3973 end
3744 end 3974 end
3745 if _ then 3975 if A then
3746 function i:XmlDecl(e,t,i) 3976 function i:XmlDecl(e,t,i)
3747 if a then 3977 if a then
3748 s(self:getcurrentbytecount()); 3978 s(self:getcurrentbytecount());
3749 end 3979 end
3750 if(t and t:lower()~="utf-8") 3980 if(t and t:lower()~="utf-8")
3759 end 3989 end
3760 i.Comment=o; 3990 i.Comment=o;
3761 i.ProcessingInstruction=o; 3991 i.ProcessingInstruction=o;
3762 local function a() 3992 local function a()
3763 e,h,t=nil,{},0; 3993 e,h,t=nil,{},0;
3764 u={}; 3994 l={};
3765 end 3995 end
3766 local function e(t,e) 3996 local function t(t,e)
3767 n=e; 3997 n=e;
3768 end 3998 end
3769 return i,{reset=a,set_session=e}; 3999 return i,{reset=a,set_session=t};
3770 end 4000 end
3771 local function l(e,h,o) 4001 local function u(l,d,o)
3772 local t=0; 4002 local t=0;
3773 local i; 4003 local e;
3774 if a then 4004 if a then
3775 function i(e) 4005 function e(e)
3776 t=t-e; 4006 t=t-e;
3777 end 4007 end
3778 o=o or x; 4008 o=o or _;
3779 elseif o then 4009 elseif o then
3780 w("Stanza size limits are not supported on this version of LuaExpat") 4010 w("Stanza size limits are not supported on this version of LuaExpat")
3781 end 4011 end
3782 local n,d=q(e,h,i); 4012 local n,s=v(l,d,e);
3783 local i=v(n,r,false); 4013 local i=j(n,r,false);
3784 local s=i.parse; 4014 local h=i.parse;
3785 function e.open_stream(e,o,a) 4015 function l.open_stream(e,t,a)
3786 local i=e.sends2s or e.send; 4016 local i=e.sends2s or e.send;
3787 local t={ 4017 local o={
3788 ["xmlns:stream"]="http://etherx.jabber.org/streams", 4018 ["xmlns:stream"]="http://etherx.jabber.org/streams",
3789 ["xml:lang"]="en", 4019 ["xml:lang"]="en",
3790 xmlns=h.default_ns, 4020 xmlns=d.default_ns,
3791 version=e.version and(e.version>0 and"1.0"or nil), 4021 version=e.version and(e.version>0 and"1.0"or nil),
3792 id=e.streamid or"", 4022 id=e.streamid or"",
3793 from=o or e.host,to=a, 4023 from=t or e.host,to=a,
3794 }; 4024 };
3795 if e.stream_attrs then 4025 if e.stream_attrs then
3796 e:stream_attrs(o,a,t) 4026 e:stream_attrs(t,a,o)
3797 end 4027 end
3798 i("<?xml version='1.0'?>"..g.stanza("stream:stream",t):top_tag()); 4028 i("<?xml version='1.0'?>"..x.stanza("stream:stream",o):top_tag());
3799 return true; 4029 return true;
3800 end 4030 end
3801 return{ 4031 return{
3802 reset=function() 4032 reset=function()
3803 i=v(n,r,false); 4033 i=j(n,r,false);
3804 s=i.parse; 4034 h=i.parse;
3805 t=0; 4035 t=0;
3806 d.reset(); 4036 s.reset();
3807 end, 4037 end,
3808 feed=function(e,n) 4038 feed=function(e,n)
3809 if a then 4039 if a then
3810 t=t+#n; 4040 t=t+#n;
3811 end 4041 end
3812 local e=i; 4042 local e=i;
3813 local n,s=s(e,n); 4043 local n,s=h(e,n);
3814 if a and t>o then 4044 if a and t>o then
3815 return nil,"stanza-too-large"; 4045 return nil,"stanza-too-large";
3816 end 4046 end
3817 if i~=e then 4047 if i~=e then
3818 e:parse(); 4048 e:parse();
3819 e:close(); 4049 e:close();
3820 end 4050 end
3821 return n,s; 4051 return n,s;
3822 end, 4052 end,
3823 set_session=d.set_session; 4053 set_session=s.set_session;
3824 set_stanza_size_limit=function(t,e) 4054 set_stanza_size_limit=function(t,e)
3825 o=e; 4055 o=e;
3826 end; 4056 end;
3827 }; 4057 };
3828 end 4058 end
3829 return{ 4059 return{
3830 ns_separator=r; 4060 ns_separator=r;
3831 ns_pattern=k; 4061 ns_pattern=q;
3832 new_sax_handlers=q; 4062 new_sax_handlers=v;
3833 new=l; 4063 new=u;
3834 }; 4064 };
3835 end) 4065 end)
3836 package.preload['util.jid']=(function(...) 4066 package.preload['util.jid']=(function(...)
3837 local _ENV=_ENV; 4067 local _ENV=_ENV;
3838 local function e(t,...) 4068 local function e(t,...)
3843 end 4073 end
3844 _ENV=e; 4074 _ENV=e;
3845 _M=e; 4075 _M=e;
3846 return e; 4076 return e;
3847 end 4077 end
3848 local i=select; 4078 local o=select;
3849 local a,h=string.match,string.sub; 4079 local a,n=string.match,string.sub;
3850 local u=require"util.encodings".stringprep.nodeprep; 4080 local d=require"util.encodings".stringprep.nodeprep;
3851 local l=require"util.encodings".stringprep.nameprep; 4081 local l=require"util.encodings".stringprep.nameprep;
3852 local d=require"util.encodings".stringprep.resourceprep; 4082 local u=require"util.encodings".stringprep.resourceprep;
3853 local o={ 4083 local i={
3854 [" "]="\\20";['"']="\\22"; 4084 [" "]="\\20";['"']="\\22";
3855 ["&"]="\\26";["'"]="\\27"; 4085 ["&"]="\\26";["'"]="\\27";
3856 ["/"]="\\2f";[":"]="\\3a"; 4086 ["/"]="\\2f";[":"]="\\3a";
3857 ["<"]="\\3c";[">"]="\\3e"; 4087 ["<"]="\\3c";[">"]="\\3e";
3858 ["@"]="\\40";["\\"]="\\5c"; 4088 ["@"]="\\40";["\\"]="\\5c";
3859 }; 4089 };
3860 local n={};
3861 local s={}; 4090 local s={};
3862 for t,e in pairs(o)do 4091 local h={};
3863 n[e]=t; 4092 for t,e in pairs(i)do
3864 s[e]=e:gsub("\\",o) 4093 s[e]=t;
4094 h[e]=e:gsub("\\",i)
3865 end 4095 end
3866 local _ENV=nil; 4096 local _ENV=nil;
3867 local function t(e) 4097 local function t(e)
3868 if e==nil then return;end 4098 if e==nil then return;end
3869 local i,t=a(e,"^([^@/]+)@()"); 4099 local i,t=a(e,"^([^@/]+)@()");
3878 return t.."@"..e; 4108 return t.."@"..e;
3879 end 4109 end
3880 return e; 4110 return e;
3881 end 4111 end
3882 local function r(e,o) 4112 local function r(e,o)
3883 local a,e,t=t(e); 4113 local t,e,a=t(e);
3884 if e~=nil and e~="."then 4114 if e~=nil and e~="."then
3885 if h(e,-1,-1)=="."then 4115 if n(e,-1,-1)=="."then
3886 e=h(e,1,-2); 4116 e=n(e,1,-2);
3887 end 4117 end
3888 e=l(e,o); 4118 e=l(e,o);
3889 if e==nil then return;end 4119 if e==nil then return;end
4120 if t~=nil then
4121 t=d(t,o);
4122 if t==nil then return;end
4123 end
3890 if a~=nil then 4124 if a~=nil then
3891 a=u(a,o); 4125 a=u(a,o);
3892 if a==nil then return;end 4126 if a==nil then return;end
3893 end 4127 end
3894 if t~=nil then 4128 return t,e,a;
3895 t=d(t,o); 4129 end
3896 if t==nil then return;end 4130 end
3897 end 4131 local function n(t,e,a)
3898 return a,e,t;
3899 end
3900 end
3901 local function h(t,e,a)
3902 if e==nil then return end 4132 if e==nil then return end
3903 if t~=nil and a~=nil then 4133 if t~=nil and a~=nil then
3904 return t.."@"..e.."/"..a; 4134 return t.."@"..e.."/"..a;
3905 elseif t~=nil then 4135 elseif t~=nil then
3906 return t.."@"..e; 4136 return t.."@"..e;
3907 elseif a~=nil then 4137 elseif a~=nil then
3908 return e.."/"..a; 4138 return e.."/"..a;
3909 end 4139 end
3910 return e; 4140 return e;
3911 end 4141 end
3912 local function c(e,t) 4142 local function l(t,e)
3913 local e,a,t=r(e,t); 4143 local a,t,e=r(t,e);
3914 return h(e,a,t); 4144 return n(a,t,e);
3915 end 4145 end
3916 local function f(a,e) 4146 local function d(e,a)
3917 local i,n,o=t(a); 4147 local o,n,i=t(e);
3918 local a,t,e=t(e); 4148 local e,t,a=t(a);
3919 if(a==nil or a==i)and 4149 if(e==nil or e==o)and
3920 (t==nil or t==n)and 4150 (t==nil or t==n)and
3921 (e==nil or e==o)then 4151 (a==nil or a==i)then
3922 return true 4152 return true
3923 end 4153 end
3924 return false 4154 return false
3925 end 4155 end
4156 local function c(e)
4157 return(o(1,t(e)));
4158 end
4159 local function a(e)
4160 return(o(2,t(e)));
4161 end
3926 local function u(e) 4162 local function u(e)
3927 return(i(1,t(e))); 4163 return(o(3,t(e)));
3928 end 4164 end
3929 local function l(e) 4165 local function o(e)return e and(e:gsub("\\%x%x",h):gsub("[\"&'/:<>@ ]",i));end
3930 return(i(2,t(e))); 4166 local function i(e)return e and(e:gsub("\\%x%x",s));end
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{ 4167 return{
3938 split=t; 4168 split=t;
3939 bare=m; 4169 bare=m;
3940 prepped_split=r; 4170 prepped_split=r;
3941 join=h; 4171 join=n;
3942 prep=c; 4172 prep=l;
3943 compare=f; 4173 compare=d;
3944 node=u; 4174 node=c;
3945 host=l; 4175 host=a;
3946 resource=d; 4176 resource=u;
3947 escape=a; 4177 escape=o;
3948 unescape=o; 4178 unescape=i;
3949 }; 4179 };
3950 end) 4180 end)
3951 package.preload['util.events']=(function(...) 4181 package.preload['util.events']=(function(...)
3952 local _ENV=_ENV; 4182 local _ENV=_ENV;
3953 local function e(t,...) 4183 local function e(t,...)
3959 _ENV=e; 4189 _ENV=e;
3960 _M=e; 4190 _M=e;
3961 return e; 4191 return e;
3962 end 4192 end
3963 local a=pairs; 4193 local a=pairs;
3964 local d=table.insert; 4194 local l=table.insert;
3965 local w=table.remove; 4195 local m=table.remove;
3966 local u=table.sort; 4196 local r=table.sort;
3967 local l=setmetatable; 4197 local d=setmetatable;
3968 local h=next; 4198 local h=next;
3969 local _ENV=nil; 4199 local _ENV=nil;
3970 local function f() 4200 local function f()
3971 local o={}; 4201 local i={};
3972 local t; 4202 local t;
3973 local n={}; 4203 local n={};
3974 local i={}; 4204 local o={};
3975 local s=nil; 4205 local s=nil;
3976 local function r(n,o) 4206 local function u(n,i)
3977 local e=i[o]; 4207 local e=o[i];
3978 if not e or h(e)==nil then return;end 4208 if not e or h(e)==nil then return;end
3979 local t={}; 4209 local t={};
3980 for e in a(e)do 4210 for e in a(e)do
3981 d(t,e); 4211 l(t,e);
3982 end 4212 end
3983 u(t,function(a,t)return e[a]>e[t];end); 4213 r(t,function(t,a)return e[t]>e[a];end);
3984 n[o]=t; 4214 n[i]=t;
3985 return t; 4215 return t;
3986 end; 4216 end;
3987 l(o,{__index=r}); 4217 d(i,{__index=u});
3988 local function l(t,n,a) 4218 local function d(t,n,a)
3989 local e=i[t]; 4219 local e=o[t];
3990 if e then 4220 if e then
3991 e[n]=a or 0; 4221 e[n]=a or 0;
3992 else 4222 else
3993 e={[n]=a or 0}; 4223 e={[n]=a or 0};
3994 i[t]=e; 4224 o[t]=e;
3995 end 4225 end
4226 i[t]=nil;
4227 end;
4228 local function r(t,a)
4229 local e=o[t];
4230 if e then
4231 e[a]=nil;
4232 i[t]=nil;
4233 if h(e)==nil then
3996 o[t]=nil; 4234 o[t]=nil;
4235 end
4236 end
3997 end; 4237 end;
3998 local function d(e,a) 4238 local function f(e)
3999 local t=i[e]; 4239 return i[e];
4000 if t then
4001 t[a]=nil;
4002 o[e]=nil;
4003 if h(t)==nil then
4004 i[e]=nil;
4005 end
4006 end
4007 end; 4240 end;
4008 local function c(e) 4241 local function w(e)
4009 return o[e]; 4242 for t,e in a(e)do
4243 d(t,e);
4244 end
4010 end; 4245 end;
4011 local function u(e) 4246 local function u(e)
4012 for e,t in a(e)do 4247 for t,e in a(e)do
4013 l(e,t); 4248 r(t,e);
4014 end 4249 end
4015 end; 4250 end;
4016 local function m(e) 4251 local function h(t,a)
4017 for t,e in a(e)do 4252 local e=i[t];
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 4253 if e and not s then
4024 for a=1,#e do 4254 for t=1,#e do
4025 local e=e[a](t); 4255 local e=e[t](a);
4026 if e~=nil then return e;end 4256 if e~=nil then return e;end
4027 end 4257 end
4028 elseif e and s then 4258 elseif e and s then
4029 for o=1,#e do 4259 for o=1,#e do
4030 local e=s(e[o],a,t); 4260 local e=s(e[o],t,a);
4031 if e~=nil then return e;end 4261 if e~=nil then return e;end
4032 end 4262 end
4033 end 4263 end
4034 end; 4264 end;
4035 local function f(s,h) 4265 local function c(s,r)
4036 local e=n[s]or t; 4266 local a=n[s]or t;
4037 if e then 4267 if a then
4038 local a=#e; 4268 local e=#a;
4039 local function i(o,n) 4269 local function n(o,i)
4040 a=a-1; 4270 e=e-1;
4041 if a==0 then 4271 if e==0 then
4042 if t==nil or e==t then 4272 if t==nil or a==t then
4043 return r(o,n); 4273 return h(o,i);
4044 end 4274 end
4045 e,a=t,#t; 4275 a,e=t,#t;
4046 return e[a](i,o,n); 4276 return a[e](n,o,i);
4047 else 4277 else
4048 return e[a](i,o,n); 4278 return a[e](n,o,i);
4049 end 4279 end
4050 end 4280 end
4051 return e[a](i,s,h); 4281 return a[e](n,s,r);
4052 end 4282 end
4053 return r(s,h); 4283 return h(s,r);
4054 end 4284 end
4055 local function r(a,o) 4285 local function l(a,o)
4056 local e; 4286 local e;
4057 if a==false then 4287 if a==false then
4058 e=t; 4288 e=t;
4059 if not e then 4289 if not e then
4060 e={}; 4290 e={};
4077 e=n[a]; 4307 e=n[a];
4078 end 4308 end
4079 if not e then return;end 4309 if not e then return;end
4080 for t=#e,1,-1 do 4310 for t=#e,1,-1 do
4081 if e[t]==o then 4311 if e[t]==o then
4082 w(e,t); 4312 m(e,t);
4083 end 4313 end
4084 end 4314 end
4085 if#e==0 then 4315 if#e==0 then
4086 if a==false then 4316 if a==false then
4087 t=nil; 4317 t=nil;
4088 else 4318 else
4089 n[a]=nil; 4319 n[a]=nil;
4090 end 4320 end
4091 end 4321 end
4092 end 4322 end
4093 local function a(t) 4323 local function e(e)
4094 local e=s; 4324 local t=s;
4095 s=t; 4325 s=e;
4096 return e; 4326 return t;
4097 end 4327 end
4098 return{ 4328 return{
4329 add_handler=d;
4330 remove_handler=r;
4331 add_handlers=w;
4332 remove_handlers=u;
4333 get_handlers=f;
4334 wrappers={
4099 add_handler=l; 4335 add_handler=l;
4100 remove_handler=d;
4101 add_handlers=u;
4102 remove_handlers=m;
4103 get_handlers=c;
4104 wrappers={
4105 add_handler=r;
4106 remove_handler=h; 4336 remove_handler=h;
4107 }; 4337 };
4108 add_wrapper=r; 4338 add_wrapper=l;
4109 remove_wrapper=h; 4339 remove_wrapper=h;
4110 set_debug_hook=a; 4340 set_debug_hook=e;
4111 fire_event=f; 4341 fire_event=c;
4112 _handlers=o; 4342 _handlers=i;
4113 _event_map=i; 4343 _event_map=o;
4114 }; 4344 };
4115 end 4345 end
4116 return{ 4346 return{
4117 new=f; 4347 new=f;
4118 }; 4348 };
4130 return e; 4360 return e;
4131 end 4361 end
4132 local e=setmetatable; 4362 local e=setmetatable;
4133 local n=ipairs; 4363 local n=ipairs;
4134 local s,p=type,next; 4364 local s,p=type,next;
4135 local u=tonumber; 4365 local h=tonumber;
4136 local r=tostring; 4366 local l=tostring;
4137 local l=table.concat; 4367 local d=table.concat;
4138 local c=require"util.stanza"; 4368 local f=require"util.stanza";
4139 local m=require"util.jid".prep; 4369 local u=require"util.jid".prep;
4140 local _ENV=nil; 4370 local _ENV=nil;
4141 local y='jabber:x:data'; 4371 local y='jabber:x:data';
4142 local f='http://jabber.org/protocol/xdata-validate'; 4372 local m='http://jabber.org/protocol/xdata-validate';
4143 local d={}; 4373 local r={};
4144 local t={__index=d}; 4374 local a={__index=r};
4145 local function w(a) 4375 local function c(t)
4146 return e(a,t); 4376 return e(t,a);
4147 end 4377 end
4148 local function v(e) 4378 local function w(e)
4149 local o={ 4379 local o={
4150 title=e:get_child_text("title"); 4380 title=e:get_child_text("title");
4151 instructions=e:get_child_text("instructions"); 4381 instructions=e:get_child_text("instructions");
4152 }; 4382 };
4153 for t in e:childtags("field")do 4383 for t in e:childtags("field")do
4171 elseif e.type:match"%-multi"then 4401 elseif e.type:match"%-multi"then
4172 for e in t:childtags("value")do 4402 for e in t:childtags("value")do
4173 a[#a+1]=e.attr.label and{label=e.attr.label,value=e:get_text()}or e:get_text(); 4403 a[#a+1]=e.attr.label and{label=e.attr.label,value=e:get_text()}or e:get_text();
4174 end 4404 end
4175 if e.type=="text-multi"then 4405 if e.type=="text-multi"then
4176 e.value=l(a,"\n"); 4406 e.value=d(a,"\n");
4177 else 4407 else
4178 e.value=a; 4408 e.value=a;
4179 end 4409 end
4180 end 4410 end
4181 end 4411 end
4182 local t=t:get_child("validate",f); 4412 local t=t:get_child("validate",m);
4183 if t then 4413 if t then
4184 e.datatype=datatype.attr.datatype; 4414 e.datatype=datatype.attr.datatype;
4185 local t=t:get_child("range"); 4415 local t=t:get_child("range");
4186 if t then 4416 if t then
4187 e.range_min=u(t.attr.min); 4417 e.range_min=h(t.attr.min);
4188 e.range_max=u(t.attr.max); 4418 e.range_max=h(t.attr.max);
4189 end 4419 end
4190 end 4420 end
4191 end 4421 end
4192 return w(o); 4422 return c(o);
4193 end 4423 end
4194 function d.form(t,h,i) 4424 function r.form(t,h,i)
4195 if not i then i="form"end 4425 if not i then i="form"end
4196 local e=c.stanza("x",{xmlns=y,type=i}); 4426 local e=f.stanza("x",{xmlns=y,type=i});
4197 if i=="cancel"then 4427 if i=="cancel"then
4198 return e; 4428 return e;
4199 end 4429 end
4200 if i~="submit"then 4430 if i~="submit"then
4201 if t.title then 4431 if t.title then
4212 if a.desc then 4442 if a.desc then
4213 e:text_tag("desc",a.desc); 4443 e:text_tag("desc",a.desc);
4214 end 4444 end
4215 end 4445 end
4216 if i=="form"and a.datatype then 4446 if i=="form"and a.datatype then
4217 e:tag("validate",{xmlns=f,datatype=a.datatype}); 4447 e:tag("validate",{xmlns=m,datatype=a.datatype});
4218 if a.range_min or a.range_max then 4448 if a.range_min or a.range_max then
4219 e:tag("range",{ 4449 e:tag("range",{
4220 min=a.range_min and r(a.range_min), 4450 min=a.range_min and l(a.range_min),
4221 max=a.range_max and r(a.range_max), 4451 max=a.range_max and l(a.range_max),
4222 }):up(); 4452 }):up();
4223 end 4453 end
4224 e:up(); 4454 e:up();
4225 end 4455 end
4226 local t=a.value; 4456 local t=a.value;
4302 e:up(); 4532 e:up();
4303 end 4533 end
4304 return e; 4534 return e;
4305 end 4535 end
4306 local t={}; 4536 local t={};
4307 local h={}; 4537 local l={};
4308 function d.data(e,r,i) 4538 function r.data(e,h,i)
4309 local o={}; 4539 local o={};
4310 local a={}; 4540 local a={};
4311 local s={}; 4541 local s={};
4312 for n,e in n(e)do 4542 for n,e in n(e)do
4313 local n; 4543 local n;
4314 for t in r:childtags("field")do 4544 for t in h:childtags("field")do
4315 if(e.var or e.name)==t.attr.var then 4545 if(e.var or e.name)==t.attr.var then
4316 n=t; 4546 n=t;
4317 break; 4547 break;
4318 end 4548 end
4319 end 4549 end
4326 elseif e.name then 4556 elseif e.name then
4327 s[e.name]=true; 4557 s[e.name]=true;
4328 local t=t[e.type]; 4558 local t=t[e.type];
4329 if t then 4559 if t then
4330 local t,i=t(n,e.required); 4560 local t,i=t(n,e.required);
4331 local n=e.datatype and h[e.datatype]; 4561 local n=e.datatype and l[e.datatype];
4332 if t~=nil and n then 4562 if t~=nil and n then
4333 local o,a=n(t,e); 4563 local o,a=n(t,e);
4334 if o then 4564 if o then
4335 t=a; 4565 t=a;
4336 else 4566 else
4357 t["text-private"]=e; 4587 t["text-private"]=e;
4358 t["jid-single"]= 4588 t["jid-single"]=
4359 function(o,a) 4589 function(o,a)
4360 local e,a=e(o,a); 4590 local e,a=e(o,a);
4361 if not e then return e,a;end 4591 if not e then return e,a;end
4362 local a=m(e); 4592 local a=u(e);
4363 if not a then 4593 if not a then
4364 return nil,"Invalid JID: "..e; 4594 return nil,"Invalid JID: "..e;
4365 end 4595 end
4366 return a; 4596 return a;
4367 end 4597 end
4369 function(o,i) 4599 function(o,i)
4370 local a={}; 4600 local a={};
4371 local e={}; 4601 local e={};
4372 for t in o:childtags("value")do 4602 for t in o:childtags("value")do
4373 local t=t:get_text(); 4603 local t=t:get_text();
4374 local o=m(t); 4604 local o=u(t);
4375 a[#a+1]=o; 4605 a[#a+1]=o;
4376 if t and not o then 4606 if t and not o then
4377 e[#e+1]=("Invalid JID: "..t); 4607 e[#e+1]=("Invalid JID: "..t);
4378 end 4608 end
4379 end 4609 end
4380 if#a>0 then 4610 if#a>0 then
4381 return a,(#e>0 and l(e,"\n")or nil); 4611 return a,(#e>0 and d(e,"\n")or nil);
4382 elseif i then 4612 elseif i then
4383 return nil,"Required value missing"; 4613 return nil,"Required value missing";
4384 end 4614 end
4385 end 4615 end
4386 t["list-multi"]= 4616 t["list-multi"]=
4397 end 4627 end
4398 t["text-multi"]= 4628 t["text-multi"]=
4399 function(a,e) 4629 function(a,e)
4400 local e,a=t["list-multi"](a,e); 4630 local e,a=t["list-multi"](a,e);
4401 if e then 4631 if e then
4402 e=l(e,"\n"); 4632 e=d(e,"\n");
4403 end 4633 end
4404 return e,a; 4634 return e,a;
4405 end 4635 end
4406 t["list-single"]=e; 4636 t["list-single"]=e;
4407 local a={ 4637 local o={
4408 ["1"]=true,["true"]=true, 4638 ["1"]=true,["true"]=true,
4409 ["0"]=false,["false"]=false, 4639 ["0"]=false,["false"]=false,
4410 }; 4640 };
4411 t["boolean"]= 4641 t["boolean"]=
4412 function(o,i) 4642 function(i,a)
4413 local e,o=e(o,i); 4643 local e,a=e(i,a);
4414 if not e then return e,o;end 4644 if not e then return e,a;end
4415 local a=a[e]; 4645 local a=o[e];
4416 if a==nil then 4646 if a==nil then
4417 return nil,"Invalid boolean representation:"..e; 4647 return nil,"Invalid boolean representation:"..e;
4418 end 4648 end
4419 return a; 4649 return a;
4420 end 4650 end
4421 t["hidden"]= 4651 t["hidden"]=
4422 function(e) 4652 function(e)
4423 return e:get_child_text("value"); 4653 return e:get_child_text("value");
4424 end 4654 end
4425 h["xs:integer"]= 4655 l["xs:integer"]=
4426 function(e,t) 4656 function(e,t)
4427 local e=u(e); 4657 local e=h(e);
4428 if not e then 4658 if not e then
4429 return false,"not a number"; 4659 return false,"not a number";
4430 elseif e%1~=0 then 4660 elseif e%1~=0 then
4431 return false,"not an integer"; 4661 return false,"not an integer";
4432 end 4662 end
4436 return false,"out of bounds"; 4666 return false,"out of bounds";
4437 end 4667 end
4438 return true,e; 4668 return true,e;
4439 end 4669 end
4440 local function t(e) 4670 local function t(e)
4441 if not c.is_stanza(e)then 4671 if not f.is_stanza(e)then
4442 return nil,"not a stanza object"; 4672 return nil,"not a stanza object";
4443 elseif e.attr.xmlns~="jabber:x:data"or e.name~="x"then 4673 elseif e.attr.xmlns~="jabber:x:data"or e.name~="x"then
4444 return nil,"not a dataform element"; 4674 return nil,"not a dataform element";
4445 end 4675 end
4446 for e in e:childtags("field")do 4676 for e in e:childtags("field")do
4449 end 4679 end
4450 end 4680 end
4451 return""; 4681 return"";
4452 end 4682 end
4453 return{ 4683 return{
4454 new=w; 4684 new=c;
4455 from_stanza=v; 4685 from_stanza=w;
4456 get_type=t; 4686 get_type=t;
4457 }; 4687 };
4458 end) 4688 end)
4459 package.preload['util.caps']=(function(...) 4689 package.preload['util.caps']=(function(...)
4460 local _ENV=_ENV; 4690 local _ENV=_ENV;
4466 end 4696 end
4467 _ENV=e; 4697 _ENV=e;
4468 _M=e; 4698 _M=e;
4469 return e; 4699 return e;
4470 end 4700 end
4471 local l=require"util.encodings".base64.encode; 4701 local d=require"util.encodings".base64.encode;
4472 local u=require"util.hashes".sha1; 4702 local u=require"util.hashes".sha1;
4473 local n,s,h=table.insert,table.sort,table.concat; 4703 local n,h,s=table.insert,table.sort,table.concat;
4474 local r=ipairs; 4704 local r=ipairs;
4475 local _ENV=nil; 4705 local _ENV=nil;
4476 local function d(e) 4706 local function l(e)
4477 local i,o,a={},{},{}; 4707 local o,a,i={},{},{};
4478 for t,e in r(e)do 4708 for t,e in r(e)do
4479 if e.name=="identity"then 4709 if e.name=="identity"then
4480 n(i,(e.attr.category or"").."\0"..(e.attr.type or"").."\0"..(e.attr["xml:lang"]or"").."\0"..(e.attr.name or"")); 4710 n(o,(e.attr.category or"").."\0"..(e.attr.type or"").."\0"..(e.attr["xml:lang"]or"").."\0"..(e.attr.name or""));
4481 elseif e.name=="feature"then 4711 elseif e.name=="feature"then
4482 n(o,e.attr.var or""); 4712 n(a,e.attr.var or"");
4483 elseif e.name=="x"and e.attr.xmlns=="jabber:x:data"then 4713 elseif e.name=="x"and e.attr.xmlns=="jabber:x:data"then
4484 local t={}; 4714 local t={};
4485 local o; 4715 local o;
4486 for a,e in r(e.tags)do 4716 for a,e in r(e.tags)do
4487 if e.name=="field"and e.attr.var then 4717 if e.name=="field"and e.attr.var then
4488 local a={}; 4718 local a={};
4489 for t,e in r(e.tags)do 4719 for t,e in r(e.tags)do
4490 e=#e.tags==0 and e:get_text(); 4720 e=#e.tags==0 and e:get_text();
4491 if e then n(a,e);end 4721 if e then n(a,e);end
4492 end 4722 end
4493 s(a); 4723 h(a);
4494 if e.attr.var=="FORM_TYPE"then 4724 if e.attr.var=="FORM_TYPE"then
4495 o=a[1]; 4725 o=a[1];
4496 elseif#a>0 then 4726 elseif#a>0 then
4497 n(t,e.attr.var.."\0"..h(a,"<")); 4727 n(t,e.attr.var.."\0"..s(a,"<"));
4498 else 4728 else
4499 n(t,e.attr.var); 4729 n(t,e.attr.var);
4500 end 4730 end
4501 end 4731 end
4502 end 4732 end
4503 s(t); 4733 h(t);
4504 t=h(t,"<"); 4734 t=s(t,"<");
4505 if o then t=o.."\0"..t;end 4735 if o then t=o.."\0"..t;end
4506 n(a,t); 4736 n(i,t);
4507 end 4737 end
4508 end 4738 end
4509 s(i); 4739 h(o);
4510 s(o); 4740 h(a);
4511 s(a); 4741 h(i);
4512 if#i>0 then i=h(i,"<"):gsub("%z","/").."<";else i="";end 4742 if#o>0 then o=s(o,"<"):gsub("%z","/").."<";else o="";end
4513 if#o>0 then o=h(o,"<").."<";else o="";end 4743 if#a>0 then a=s(a,"<").."<";else a="";end
4514 if#a>0 then a=h(a,"<"):gsub("%z","<").."<";else a="";end 4744 if#i>0 then i=s(i,"<"):gsub("%z","<").."<";else i="";end
4515 local e=i..o..a; 4745 local e=o..a..i;
4516 local t=l(u(e)); 4746 local t=d(u(e));
4517 return t,e; 4747 return t,e;
4518 end 4748 end
4519 return{ 4749 return{
4520 calculate_hash=d; 4750 calculate_hash=l;
4521 }; 4751 };
4522 end) 4752 end)
4523 package.preload['util.vcard']=(function(...) 4753 package.preload['util.vcard']=(function(...)
4524 local _ENV=_ENV; 4754 local _ENV=_ENV;
4525 local function e(t,...) 4755 local function e(t,...)
4530 end 4760 end
4531 _ENV=e; 4761 _ENV=e;
4532 _M=e; 4762 _M=e;
4533 return e; 4763 return e;
4534 end 4764 end
4535 local o=require"util.stanza"; 4765 local i=require"util.stanza";
4536 local a,c=table.insert,table.concat; 4766 local a,u=table.insert,table.concat;
4537 local h=type; 4767 local n=type;
4538 local e,s,m=next,pairs,ipairs; 4768 local e,s,c=next,pairs,ipairs;
4539 local u,l,d,r; 4769 local r,l,d,h;
4540 local f="\n"; 4770 local f="\n";
4541 local i; 4771 local o;
4542 local function e() 4772 local function e()
4543 error"Not implemented" 4773 error"Not implemented"
4544 end 4774 end
4545 local function e() 4775 local function e()
4546 error"Not implemented" 4776 error"Not implemented"
4547 end 4777 end
4548 local function w(e) 4778 local function y(e)
4549 return e:gsub("[,:;\\]","\\%1"):gsub("\n","\\n"); 4779 return e:gsub("[,:;\\]","\\%1"):gsub("\n","\\n");
4550 end 4780 end
4551 local function y(e) 4781 local function p(e)
4552 return e:gsub("\\?[\\nt:;,]",{ 4782 return e:gsub("\\?[\\nt:;,]",{
4553 ["\\\\"]="\\", 4783 ["\\\\"]="\\",
4554 ["\\n"]="\n", 4784 ["\\n"]="\n",
4555 ["\\r"]="\r", 4785 ["\\r"]="\r",
4556 ["\\t"]="\t", 4786 ["\\t"]="\t",
4560 [":"]="\29", 4790 [":"]="\29",
4561 [";"]="\30", 4791 [";"]="\30",
4562 [","]="\31", 4792 [","]="\31",
4563 }); 4793 });
4564 end 4794 end
4565 local function p(t) 4795 local function w(e)
4566 local a=o.stanza(t.name,{xmlns="vcard-temp"}); 4796 local a=i.stanza(e.name,{xmlns="vcard-temp"});
4567 local e=i[t.name]; 4797 local t=o[e.name];
4568 if e=="text"then 4798 if t=="text"then
4569 a:text(t[1]); 4799 a:text(e[1]);
4570 elseif h(e)=="table"then 4800 elseif n(t)=="table"then
4571 if e.types and t.TYPE then 4801 if t.types and e.TYPE then
4572 if h(t.TYPE)=="table"then 4802 if n(e.TYPE)=="table"then
4573 for o,e in s(e.types)do 4803 for o,t in s(t.types)do
4574 for o,t in s(t.TYPE)do 4804 for o,e in s(e.TYPE)do
4575 if t:upper()==e then 4805 if e:upper()==t then
4576 a:tag(e):up(); 4806 a:tag(t):up();
4577 break; 4807 break;
4578 end 4808 end
4579 end 4809 end
4580 end 4810 end
4581 else 4811 else
4582 a:tag(t.TYPE:upper()):up(); 4812 a:tag(e.TYPE:upper()):up();
4583 end 4813 end
4584 end 4814 end
4585 if e.props then 4815 if t.props then
4586 for o,e in s(e.props)do 4816 for o,t in s(t.props)do
4587 if t[e]then 4817 if e[t]then
4588 a:tag(e):up(); 4818 a:tag(t):up();
4589 end 4819 end
4590 end 4820 end
4591 end 4821 end
4592 if e.value then 4822 if t.value then
4593 a:tag(e.value):text(t[1]):up(); 4823 a:tag(t.value):text(e[1]):up();
4594 elseif e.values then 4824 elseif t.values then
4595 local o=e.values; 4825 local o=t.values;
4596 local i=o.behaviour=="repeat-last"and o[#o]; 4826 local i=o.behaviour=="repeat-last"and o[#o];
4597 for o=1,#t do 4827 for o=1,#e do
4598 a:tag(e.values[o]or i):text(t[o]):up(); 4828 a:tag(t.values[o]or i):text(e[o]):up();
4599 end 4829 end
4600 end 4830 end
4601 end 4831 end
4602 return a; 4832 return a;
4603 end 4833 end
4604 local function n(t) 4834 local function m(t)
4605 local e=o.stanza("vCard",{xmlns="vcard-temp"}); 4835 local e=i.stanza("vCard",{xmlns="vcard-temp"});
4606 for a=1,#t do 4836 for a=1,#t do
4607 e:add_child(p(t[a])); 4837 e:add_child(w(t[a]));
4608 end 4838 end
4609 return e; 4839 return e;
4610 end 4840 end
4611 function r(e) 4841 function h(e)
4612 if not e[1]or e[1].name then 4842 if not e[1]or e[1].name then
4613 return n(e) 4843 return m(e)
4614 else 4844 else
4615 local t=o.stanza("xCard",{xmlns="vcard-temp"}); 4845 local t=i.stanza("xCard",{xmlns="vcard-temp"});
4616 for a=1,#e do 4846 for a=1,#e do
4617 t:add_child(n(e[a])); 4847 t:add_child(m(e[a]));
4618 end 4848 end
4619 return t; 4849 return t;
4620 end 4850 end
4621 end 4851 end
4622 function u(t) 4852 function r(t)
4623 t=t 4853 t=t
4624 :gsub("\r\n","\n") 4854 :gsub("\r\n","\n")
4625 :gsub("\n ","") 4855 :gsub("\n ","")
4626 :gsub("\n\n+","\n"); 4856 :gsub("\n\n+","\n");
4627 local h={}; 4857 local s={};
4628 local e; 4858 local e;
4629 for t in t:gmatch("[^\n]+")do 4859 for t in t:gmatch("[^\n]+")do
4630 local t=y(t); 4860 local t=p(t);
4631 local s,t,n=t:match("^([-%a]+)(\30?[^\29]*)\29(.*)$"); 4861 local n,t,i=t:match("^([-%a]+)(\30?[^\29]*)\29(.*)$");
4632 n=n:gsub("\29",":"); 4862 i=i:gsub("\29",":");
4633 if#t>0 then 4863 if#t>0 then
4634 local a={}; 4864 local o={};
4635 for e,o,i in t:gmatch("\30([^=]+)(=?)([^\30]*)")do 4865 for a,i,n in t:gmatch("\30([^=]+)(=?)([^\30]*)")do
4636 e=e:upper(); 4866 a=a:upper();
4637 local t={}; 4867 local e={};
4638 for e in i:gmatch("[^\31]+")do 4868 for t in n:gmatch("[^\31]+")do
4639 t[#t+1]=e 4869 e[#e+1]=t
4640 t[e]=true; 4870 e[t]=true;
4641 end 4871 end
4642 if o=="="then 4872 if i=="="then
4643 a[e]=t; 4873 o[a]=e;
4644 else 4874 else
4645 a[e]=true; 4875 o[a]=true;
4646 end 4876 end
4647 end 4877 end
4648 t=a; 4878 t=o;
4649 end 4879 end
4650 if s=="BEGIN"and n=="VCARD"then 4880 if n=="BEGIN"and i=="VCARD"then
4651 e={}; 4881 e={};
4652 h[#h+1]=e; 4882 s[#s+1]=e;
4653 elseif s=="END"and n=="VCARD"then 4883 elseif n=="END"and i=="VCARD"then
4654 e=nil; 4884 e=nil;
4655 elseif e and i[s]then 4885 elseif e and o[n]then
4656 local o=i[s]; 4886 local o=o[n];
4657 local i={name=s}; 4887 local n={name=n};
4658 e[#e+1]=i; 4888 e[#e+1]=n;
4659 local s=e; 4889 local s=e;
4660 e=i; 4890 e=n;
4661 if o.types then 4891 if o.types then
4662 for o,a in m(o.types)do 4892 for o,a in c(o.types)do
4663 local a=a:lower(); 4893 local a=a:lower();
4664 if(t.TYPE and t.TYPE[a]==true) 4894 if(t.TYPE and t.TYPE[a]==true)
4665 or t[a]==true then 4895 or t[a]==true then
4666 e.TYPE=a; 4896 e.TYPE=a;
4667 end 4897 end
4668 end 4898 end
4669 end 4899 end
4670 if o.props then 4900 if o.props then
4671 for o,a in m(o.props)do 4901 for o,a in c(o.props)do
4672 if t[a]then 4902 if t[a]then
4673 if t[a]==true then 4903 if t[a]==true then
4674 e[a]=true; 4904 e[a]=true;
4675 else 4905 else
4676 for o,t in m(t[a])do 4906 for o,t in c(t[a])do
4677 e[a]=t; 4907 e[a]=t;
4678 end 4908 end
4679 end 4909 end
4680 end 4910 end
4681 end 4911 end
4682 end 4912 end
4683 if o=="text"or o.value then 4913 if o=="text"or o.value then
4684 a(e,n); 4914 a(e,i);
4685 elseif o.values then 4915 elseif o.values then
4686 local t="\30"..n; 4916 local t="\30"..i;
4687 for t in t:gmatch("\30([^\30]*)")do 4917 for t in t:gmatch("\30([^\30]*)")do
4688 a(e,t); 4918 a(e,t);
4689 end 4919 end
4690 end 4920 end
4691 e=s; 4921 e=s;
4692 end 4922 end
4693 end 4923 end
4694 return h; 4924 return s;
4695 end 4925 end
4696 local function n(t) 4926 local function i(t)
4697 local e={}; 4927 local e={};
4698 for a=1,#t do 4928 for a=1,#t do
4699 e[a]=w(t[a]); 4929 e[a]=y(t[a]);
4700 end 4930 end
4701 e=c(e,";"); 4931 e=u(e,";");
4702 local a=""; 4932 local a="";
4703 for t,e in s(t)do 4933 for e,t in s(t)do
4704 if h(t)=="string"and t~="name"then 4934 if n(e)=="string"and e~="name"then
4705 a=a..(";%s=%s"):format(t,h(e)=="table"and c(e,",")or e); 4935 a=a..(";%s=%s"):format(e,n(t)=="table"and u(t,",")or t);
4706 end 4936 end
4707 end 4937 end
4708 return("%s%s:%s"):format(t.name,a,e) 4938 return("%s%s:%s"):format(t.name,a,e)
4709 end 4939 end
4710 local function o(t) 4940 local function t(t)
4711 local e={}; 4941 local e={};
4712 a(e,"BEGIN:VCARD") 4942 a(e,"BEGIN:VCARD")
4713 for o=1,#t do 4943 for o=1,#t do
4714 a(e,n(t[o])); 4944 a(e,i(t[o]));
4715 end 4945 end
4716 a(e,"END:VCARD") 4946 a(e,"END:VCARD")
4717 return c(e,f); 4947 return u(e,f);
4718 end 4948 end
4719 function l(e) 4949 function l(e)
4720 if e[1]and e[1].name then 4950 if e[1]and e[1].name then
4721 return o(e) 4951 return t(e)
4722 else 4952 else
4723 local t={}; 4953 local a={};
4954 for o=1,#e do
4955 a[o]=t(e[o]);
4956 end
4957 return u(a,f);
4958 end
4959 end
4960 local function u(i)
4961 local t=i.name;
4962 local e=o[t];
4963 local t={name=t};
4964 if e=="text"then
4965 t[1]=i:get_text();
4966 elseif n(e)=="table"then
4967 if e.value then
4968 t[1]=i:get_child_text(e.value)or"";
4969 elseif e.values then
4970 local e=e.values;
4971 if e.behaviour=="repeat-last"then
4972 for e=1,#i.tags do
4973 a(t,i.tags[e]:get_text()or"");
4974 end
4975 else
4976 for o=1,#e do
4977 a(t,i:get_child_text(e[o])or"");
4978 end
4979 end
4980 elseif e.names then
4981 local e=e.names;
4724 for a=1,#e do 4982 for a=1,#e do
4725 t[a]=o(e[a]); 4983 if i:get_child(e[a])then
4726 end 4984 t[1]=e[a];
4727 return c(t,f);
4728 end
4729 end
4730 local function n(o)
4731 local e=o.name;
4732 local t=i[e];
4733 local e={name=e};
4734 if t=="text"then
4735 e[1]=o:get_text();
4736 elseif h(t)=="table"then
4737 if t.value then
4738 e[1]=o:get_child_text(t.value)or"";
4739 elseif t.values then
4740 local t=t.values;
4741 if t.behaviour=="repeat-last"then
4742 for t=1,#o.tags do
4743 a(e,o.tags[t]:get_text()or"");
4744 end
4745 else
4746 for i=1,#t do
4747 a(e,o:get_child_text(t[i])or"");
4748 end
4749 end
4750 elseif t.names then
4751 local t=t.names;
4752 for a=1,#t do
4753 if o:get_child(t[a])then
4754 e[1]=t[a];
4755 break; 4985 break;
4756 end 4986 end
4757 end 4987 end
4758 end 4988 end
4759 if t.props_verbatim then 4989 if e.props_verbatim then
4760 for a,t in s(t.props_verbatim)do 4990 for e,a in s(e.props_verbatim)do
4761 e[a]=t; 4991 t[e]=a;
4762 end 4992 end
4763 end 4993 end
4764 if t.types then 4994 if e.types then
4765 local t=t.types; 4995 local e=e.types;
4766 e.TYPE={}; 4996 t.TYPE={};
4767 for i=1,#t do 4997 for o=1,#e do
4768 if o:get_child(t[i])then 4998 if i:get_child(e[o])then
4769 a(e.TYPE,t[i]:lower()); 4999 a(t.TYPE,e[o]:lower());
4770 end 5000 end
4771 end 5001 end
4772 if#e.TYPE==0 then 5002 if#t.TYPE==0 then
4773 e.TYPE=nil; 5003 t.TYPE=nil;
4774 end 5004 end
4775 end 5005 end
4776 if t.props then 5006 if e.props then
4777 local t=t.props; 5007 local e=e.props;
4778 for i=1,#t do 5008 for o=1,#e do
4779 local t=t[i] 5009 local e=e[o]
4780 local o=o:get_child_text(t); 5010 local o=i:get_child_text(e);
4781 if o then 5011 if o then
4782 e[t]=e[t]or{}; 5012 t[e]=t[e]or{};
4783 a(e[t],o); 5013 a(t[e],o);
4784 end 5014 end
4785 end 5015 end
4786 end 5016 end
4787 else 5017 else
4788 return nil 5018 return nil
4789 end 5019 end
4790 return e; 5020 return t;
4791 end 5021 end
4792 local function o(e) 5022 local function i(e)
4793 local e=e.tags; 5023 local t=e.tags;
4794 local t={}; 5024 local e={};
4795 for o=1,#e do 5025 for o=1,#t do
4796 a(t,n(e[o])); 5026 a(e,u(t[o]));
4797 end 5027 end
4798 return t 5028 return e
4799 end 5029 end
4800 function d(e) 5030 function d(e)
4801 if e.attr.xmlns~="vcard-temp"then 5031 if e.attr.xmlns~="vcard-temp"then
4802 return nil,"wrong-xmlns"; 5032 return nil,"wrong-xmlns";
4803 end 5033 end
4804 if e.name=="xCard"then 5034 if e.name=="xCard"then
4805 local a={}; 5035 local a={};
4806 local t=e.tags; 5036 local e=e.tags;
4807 for e=1,#t do 5037 for t=1,#e do
4808 a[e]=o(t[e]); 5038 a[t]=i(e[t]);
4809 end 5039 end
4810 return a 5040 return a
4811 elseif e.name=="vCard"then 5041 elseif e.name=="vCard"then
4812 return o(e) 5042 return i(e)
4813 end 5043 end
4814 end 5044 end
4815 i={ 5045 o={
4816 VERSION="text", 5046 VERSION="text",
4817 FN="text", 5047 FN="text",
4818 N={ 5048 N={
4819 values={ 5049 values={
4820 "FAMILY", 5050 "FAMILY",
4932 props={"TYPE"}, 5162 props={"TYPE"},
4933 value="CRED", 5163 value="CRED",
4934 }, 5164 },
4935 DESC="text", 5165 DESC="text",
4936 }; 5166 };
4937 i.LOGO=i.PHOTO; 5167 o.LOGO=o.PHOTO;
4938 i.SOUND=i.PHOTO; 5168 o.SOUND=o.PHOTO;
4939 return{ 5169 return{
4940 from_text=u; 5170 from_text=r;
4941 to_text=l; 5171 to_text=l;
4942 from_xep54=d; 5172 from_xep54=d;
4943 to_xep54=r; 5173 to_xep54=h;
4944 lua_to_text=l; 5174 lua_to_text=l;
4945 lua_to_xep54=r; 5175 lua_to_xep54=h;
4946 text_to_lua=u; 5176 text_to_lua=r;
4947 text_to_xep54=function(...)return r(u(...));end; 5177 text_to_xep54=function(...)return h(r(...));end;
4948 xep54_to_lua=d; 5178 xep54_to_lua=d;
4949 xep54_to_text=function(...)return l(d(...))end; 5179 xep54_to_text=function(...)return l(d(...))end;
4950 }; 5180 };
4951 end) 5181 end)
4952 package.preload['util.logger']=(function(...) 5182 package.preload['util.logger']=(function(...)
4959 end 5189 end
4960 _ENV=e; 5190 _ENV=e;
4961 _M=e; 5191 _M=e;
4962 return e; 5192 return e;
4963 end 5193 end
4964 local o=pairs; 5194 local i=pairs;
4965 local c=ipairs; 5195 local r=ipairs;
4966 local u=require; 5196 local h=require;
4967 local l=table.remove; 5197 local s=table.remove;
4968 local _ENV=nil; 5198 local _ENV=nil;
4969 local e={}; 5199 local e={};
4970 local t; 5200 local t;
4971 local function h(e) 5201 local function n(e)
4972 local a=t(e,"debug"); 5202 local a=t(e,"debug");
4973 local o=t(e,"info"); 5203 local n=t(e,"info");
4974 local i=t(e,"warn"); 5204 local i=t(e,"warn");
4975 local n=t(e,"error"); 5205 local o=t(e,"error");
4976 return function(t,e,...) 5206 return function(t,e,...)
4977 if t=="debug"then 5207 if t=="debug"then
4978 return a(e,...); 5208 return a(e,...);
4979 elseif t=="info"then 5209 elseif t=="info"then
4980 return o(e,...); 5210 return n(e,...);
4981 elseif t=="warn"then 5211 elseif t=="warn"then
4982 return i(e,...); 5212 return i(e,...);
4983 elseif t=="error"then 5213 elseif t=="error"then
4984 return n(e,...); 5214 return o(e,...);
4985 end 5215 end
4986 end 5216 end
4987 end 5217 end
4988 function t(o,a) 5218 function t(o,a)
4989 local t=e[a]; 5219 local t=e[a];
4990 if not t then 5220 if not t then
4991 t={}; 5221 t={};
4992 e[a]=t; 5222 e[a]=t;
4993 end 5223 end
4994 local e=function(e,...) 5224 local e=function(i,...)
4995 for i=1,#t do 5225 for e=1,#t do
4996 t[i](o,a,e,...); 5226 t[e](o,a,i,...);
4997 end 5227 end
4998 end 5228 end
4999 return e; 5229 return e;
5000 end 5230 end
5001 local function d() 5231 local function d()
5002 for t,e in o(e)do 5232 for t,e in i(e)do
5003 for t=1,#e do 5233 for t=1,#e do
5004 e[t]=nil; 5234 e[t]=nil;
5005 end 5235 end
5006 end 5236 end
5007 end 5237 end
5008 local function i(t,a) 5238 local function o(t,a)
5009 if not e[t]then 5239 if not e[t]then
5010 e[t]={a}; 5240 e[t]={a};
5011 else 5241 else
5012 e[t][#e[t]+1]=a; 5242 e[t][#e[t]+1]=a;
5013 end 5243 end
5014 end 5244 end
5015 local function r(s,n) 5245 local function l(i,s)
5016 local a=u"util.format".format; 5246 local n=h"util.format".format;
5017 local function e(o,t,e,...) 5247 local function e(t,a,e,...)
5018 return s(o,t,a(e,...)); 5248 return i(t,a,n(e,...));
5019 end 5249 end
5020 for a,t in c(n or{"debug","info","warn","error"})do 5250 for a,t in r(s or{"debug","info","warn","error"})do
5021 i(t,e); 5251 o(t,e);
5022 end 5252 end
5023 return e; 5253 return e;
5024 end 5254 end
5025 local function n(i) 5255 local function h(o)
5026 local a; 5256 local a;
5027 for t,e in o(e)do 5257 for t,e in i(e)do
5028 for t=#e,1,-1 do 5258 for t=#e,1,-1 do
5029 if e[t]==i then 5259 if e[t]==o then
5030 l(e,t); 5260 s(e,t);
5031 a=true; 5261 a=true;
5032 end 5262 end
5033 end 5263 end
5034 end 5264 end
5035 return a; 5265 return a;
5036 end 5266 end
5037 return{ 5267 return{
5038 init=h; 5268 init=n;
5039 make_logger=t; 5269 make_logger=t;
5040 reset=d; 5270 reset=d;
5041 add_level_sink=i; 5271 add_level_sink=o;
5042 add_simple_sink=r; 5272 add_simple_sink=l;
5043 new=t; 5273 new=t;
5044 remove_sink=n; 5274 remove_sink=h;
5045 }; 5275 };
5046 end) 5276 end)
5047 package.preload['util.datetime']=(function(...) 5277 package.preload['util.datetime']=(function(...)
5048 local _ENV=_ENV; 5278 local _ENV=_ENV;
5049 local function e(t,...) 5279 local function e(t,...)
5056 _M=e; 5286 _M=e;
5057 return e; 5287 return e;
5058 end 5288 end
5059 local e=os.date; 5289 local e=os.date;
5060 local i=os.time; 5290 local i=os.time;
5061 local r=os.difftime; 5291 local l=os.difftime;
5062 local t=math.floor; 5292 local t=math.floor;
5063 local s=tonumber; 5293 local s=tonumber;
5064 local _ENV=nil; 5294 local _ENV=nil;
5065 local function d(a) 5295 local function d(a)
5066 return e("!%Y-%m-%d",a and t(a)or nil); 5296 return e("!%Y-%m-%d",a and t(a)or nil);
5067 end 5297 end
5068 local function l(a) 5298 local function r(a)
5069 if a==nil or a%1==0 then 5299 if a==nil or a%1==0 then
5070 return e("!%Y-%m-%dT%H:%M:%SZ",a); 5300 return e("!%Y-%m-%dT%H:%M:%SZ",a);
5071 end 5301 end
5072 local o=a%1; 5302 local o=a%1;
5073 local a=t(a); 5303 local a=t(a);
5074 return e("!%Y-%m-%dT%H:%M:%S.%%06dZ",a):format(t(o*1e6)); 5304 return e("!%Y-%m-%dT%H:%M:%S.%%06dZ",a):format(t(o*1e6));
5075 end 5305 end
5076 local function c(a) 5306 local function u(a)
5077 if a==nil or a%1==0 then 5307 if a==nil or a%1==0 then
5078 return e("!%H:%M:%S",a); 5308 return e("!%H:%M:%S",a);
5079 end 5309 end
5080 local o=a%1; 5310 local o=a%1;
5081 local a=t(a); 5311 local a=t(a);
5082 return e("!%H:%M:%S.%%06d",a):format(t(o*1e6)); 5312 return e("!%H:%M:%S.%%06d",a):format(t(o*1e6));
5083 end 5313 end
5084 local function u(a) 5314 local function m(a)
5085 return e("!%Y%m%dT%H:%M:%S",a and t(a)or nil); 5315 return e("!%Y%m%dT%H:%M:%S",a and t(a)or nil);
5086 end 5316 end
5087 local function m(a) 5317 local function c(a)
5088 if a then 5318 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+%-]?.*)$"); 5319 local h,r,d,c,u,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 5320 if h then
5091 local o=i(); 5321 local o=i();
5092 local r=r(i(e("*t",o)),i(e("!*t",o))); 5322 local l=l(i(e("*t",o)),i(e("!*t",o)));
5093 local o=0; 5323 local o=0;
5094 if n~=""and n~="Z"then 5324 if n~=""and n~="Z"then
5095 local a,t,e=n:match("([+%-])(%d%d):?(%d*)"); 5325 local a,t,e=n:match("([+%-])(%d%d):?(%d*)");
5096 if not a then return;end 5326 if not a then return;end
5097 if#e~=2 then e="0";end 5327 if#e~=2 then e="0";end
5098 t,e=s(t),s(e); 5328 t,e=s(t),s(e);
5099 o=t*60*60+e*60; 5329 o=t*60*60+e*60;
5100 if a=="-"then o=-o;end 5330 if a=="-"then o=-o;end
5101 end 5331 end
5102 local e=a%1; 5332 local e=a%1;
5103 a=t(a+r)-o; 5333 a=t(a+l)-o;
5104 return i({year=h,month=l,day=d,hour=u,min=c,sec=a,isdst=false})+e; 5334 return i({year=h,month=r,day=d,hour=c,min=u,sec=a,isdst=false})+e;
5105 end 5335 end
5106 end 5336 end
5107 end 5337 end
5108 return{ 5338 return{
5109 date=d; 5339 date=d;
5110 datetime=l; 5340 datetime=r;
5111 time=c; 5341 time=u;
5112 legacy=u; 5342 legacy=m;
5113 parse=m; 5343 parse=c;
5114 }; 5344 };
5115 end) 5345 end)
5116 package.preload['util.json']=(function(...) 5346 package.preload['util.json']=(function(...)
5117 local _ENV=_ENV; 5347 local _ENV=_ENV;
5118 local function e(t,...) 5348 local function e(t,...)
5123 end 5353 end
5124 _ENV=e; 5354 _ENV=e;
5125 _M=e; 5355 _M=e;
5126 return e; 5356 return e;
5127 end 5357 end
5128 local p=type; 5358 local u=type;
5129 local t,y,c=table.insert,table.concat,table.remove; 5359 local t,w,y=table.insert,table.concat,table.remove;
5130 local n=string.char; 5360 local s=string.char;
5131 local z,d=tostring,tonumber; 5361 local x,d=tostring,tonumber;
5132 local u,r,j=pairs,ipairs,require"util.iterators".sorted_pairs; 5362 local p,l,z=pairs,ipairs,require"util.iterators".sorted_pairs;
5133 local i=next; 5363 local i=next;
5134 local b,m=getmetatable,setmetatable; 5364 local b,f=getmetatable,setmetatable;
5135 local l=print; 5365 local r=print;
5136 local a,e=pcall(require,"util.array"); 5366 local a,e=pcall(require,"util.array");
5137 local v=a and b(e())or{}; 5367 local v=a and b(e())or{};
5138 local a={}; 5368 local a={};
5139 local h=m({},{__tostring=function()return"null";end;}); 5369 local n=f({},{__tostring=function()return"null";end;});
5140 a.null=h; 5370 a.null=n;
5141 local f={ 5371 local m={
5142 ["\""]="\\\"",["\\"]="\\\\",["\b"]="\\b", 5372 ["\""]="\\\"",["\\"]="\\\\",["\b"]="\\b",
5143 ["\f"]="\\f",["\n"]="\\n",["\r"]="\\r",["\t"]="\\t"}; 5373 ["\f"]="\\f",["\n"]="\\n",["\r"]="\\r",["\t"]="\\t"};
5144 for t=0,31 do 5374 for e=0,31 do
5145 local e=n(t); 5375 local t=s(e);
5146 if not f[e]then f[e]=("\\u%.4X"):format(t);end 5376 if not m[t]then m[t]=("\\u%.4X"):format(e);end
5147 end 5377 end
5148 local function q(e) 5378 local function j(e)
5149 if e<128 then return n(e);end 5379 if e<128 then return s(e);end
5150 local t=e%64; 5380 local t=e%64;
5151 if e<2048 then 5381 if e<2048 then
5152 local e=(e-t)/64; 5382 local e=(e-t)/64;
5153 return n(128+64+e,128+t); 5383 return s(128+64+e,128+t);
5154 end 5384 end
5155 local a=e%4096; 5385 local a=e%4096;
5156 local o=(a-t)/64; 5386 local o=(a-t)/64;
5157 local e=(e-a)/4096; 5387 local e=(e-a)/4096;
5158 return n(128+64+32+e,128+o,128+t); 5388 return s(128+64+32+e,128+o,128+t);
5159 end 5389 end
5160 local k={ 5390 local k={
5161 number=true, 5391 number=true,
5162 string=true, 5392 string=true,
5163 table=true, 5393 table=true,
5164 boolean=true 5394 boolean=true
5165 }; 5395 };
5166 local x={ 5396 local q={
5167 __array=true; 5397 __array=true;
5168 __hash=true; 5398 __hash=true;
5169 }; 5399 };
5170 local o,g,s,w; 5400 local o,g,h,c;
5171 function w(a,e) 5401 function c(a,e)
5172 t(e,"\""..(a:gsub(".",f)).."\""); 5402 t(e,"\""..(a:gsub(".",m)).."\"");
5173 end 5403 end
5174 function s(a,e) 5404 function h(a,e)
5175 t(e,"["); 5405 t(e,"[");
5176 if i(a)then 5406 if i(a)then
5177 for i,a in r(a)do 5407 for i,a in l(a)do
5178 o(a,e); 5408 o(a,e);
5179 t(e,","); 5409 t(e,",");
5180 end 5410 end
5181 c(e); 5411 y(e);
5182 end 5412 end
5183 t(e,"]"); 5413 t(e,"]");
5184 end 5414 end
5185 function g(l,e) 5415 function g(d,e)
5186 local a={}; 5416 local a={};
5187 local n={}; 5417 local s={};
5188 local d={}; 5418 local r={};
5189 for e,t in r(l)do 5419 for t,e in l(d)do
5190 a[e]=t; 5420 a[t]=e;
5191 end 5421 end
5192 for e,t in u(l)do 5422 for e,t in p(d)do
5193 local o,i=p(e),p(t); 5423 local o,i=u(e),u(t);
5194 if k[i]or t==h then 5424 if k[i]or t==n then
5195 if o=="string"and not x[e]then 5425 if o=="string"and not q[e]then
5196 d[e]=t; 5426 r[e]=t;
5197 elseif(k[o]or e==h)and a[e]==nil then 5427 elseif(k[o]or e==n)and a[e]==nil then
5198 n[e]=t; 5428 s[e]=t;
5199 end 5429 end
5200 end 5430 end
5201 end 5431 end
5202 if i(n)~=nil or i(d)~=nil or i(a)==nil then 5432 if i(s)~=nil or i(r)~=nil or i(a)==nil then
5203 t(e,"{"); 5433 t(e,"{");
5204 local r=#e; 5434 local n=#e;
5205 local h=e.ordered and j or u; 5435 local d=e.ordered and z or p;
5206 for a,i in h(d)do 5436 for a,i in d(r)do
5207 w(a,e); 5437 c(a,e);
5208 t(e,":"); 5438 t(e,":");
5209 o(i,e); 5439 o(i,e);
5210 t(e,","); 5440 t(e,",");
5211 end 5441 end
5212 if i(n)~=nil then 5442 if i(s)~=nil then
5213 t(e,"\"__hash\":["); 5443 t(e,"\"__hash\":[");
5214 for a,i in u(n)do 5444 for a,i in p(s)do
5215 o(a,e); 5445 o(a,e);
5216 t(e,","); 5446 t(e,",");
5217 o(i,e); 5447 o(i,e);
5218 t(e,","); 5448 t(e,",");
5219 end 5449 end
5220 c(e); 5450 y(e);
5221 t(e,"]"); 5451 t(e,"]");
5222 t(e,","); 5452 t(e,",");
5223 end 5453 end
5224 if i(a)then 5454 if i(a)then
5225 t(e,"\"__array\":"); 5455 t(e,"\"__array\":");
5226 s(a,e); 5456 h(a,e);
5227 t(e,","); 5457 t(e,",");
5228 end 5458 end
5229 if r~=#e then c(e);end 5459 if n~=#e then y(e);end
5230 t(e,"}"); 5460 t(e,"}");
5231 else 5461 else
5232 s(a,e); 5462 h(a,e);
5233 end 5463 end
5234 end 5464 end
5235 function o(e,a) 5465 function o(e,a)
5236 local o=p(e); 5466 local o=u(e);
5237 if e==h then 5467 if e==n then
5238 t(a,"null"); 5468 t(a,"null");
5239 elseif o=="number"then 5469 elseif o=="number"then
5240 t(a,z(e)); 5470 t(a,x(e));
5241 elseif o=="string"then 5471 elseif o=="string"then
5242 w(e,a); 5472 c(e,a);
5243 elseif o=="table"then 5473 elseif o=="table"then
5244 local t=b(e); 5474 local t=b(e);
5245 if t==v then 5475 if t==v then
5246 s(e,a); 5476 h(e,a);
5247 else 5477 else
5248 g(e,a); 5478 g(e,a);
5249 end 5479 end
5250 elseif o=="boolean"then 5480 elseif o=="boolean"then
5251 t(a,(e and"true"or"false")); 5481 t(a,(e and"true"or"false"));
5254 end 5484 end
5255 end 5485 end
5256 function a.encode(t) 5486 function a.encode(t)
5257 local e={}; 5487 local e={};
5258 o(t,e); 5488 o(t,e);
5259 return y(e); 5489 return w(e);
5260 end 5490 end
5261 function a.encode_ordered(t) 5491 function a.encode_ordered(t)
5262 local e={ordered=true}; 5492 local e={ordered=true};
5263 o(t,e); 5493 o(t,e);
5264 return y(e); 5494 return w(e);
5265 end 5495 end
5266 function a.encode_array(t) 5496 function a.encode_array(t)
5267 local e={}; 5497 local e={};
5268 s(t,e); 5498 h(t,e);
5269 return y(e); 5499 return w(e);
5270 end 5500 end
5271 local function o(t,e) 5501 local function o(t,e)
5272 return t:find("[^ \t\r\n]",e)or e; 5502 return t:find("[^ \t\r\n]",e)or e;
5273 end 5503 end
5274 local function c(e) 5504 local function u(e)
5275 local a=e.__array; 5505 local a=e.__array;
5276 if a then 5506 if a then
5277 e.__array=nil; 5507 e.__array=nil;
5278 for o,a in r(a)do 5508 for o,a in l(a)do
5279 t(e,a); 5509 t(e,a);
5280 end 5510 end
5281 end 5511 end
5282 local a=e.__hash; 5512 local a=e.__hash;
5283 if a then 5513 if a then
5284 e.__hash=nil; 5514 e.__hash=nil;
5285 local t; 5515 local t;
5286 for o,a in r(a)do 5516 for o,a in l(a)do
5287 if t~=nil then 5517 if t~=nil then
5288 e[t]=a;t=nil; 5518 e[t]=a;t=nil;
5289 else 5519 else
5290 t=a; 5520 t=a;
5291 end 5521 end
5292 end 5522 end
5293 end 5523 end
5294 return e; 5524 return e;
5295 end 5525 end
5296 local s,r; 5526 local i,h;
5297 local function u(t,e) 5527 local function c(t,e)
5298 local i={}; 5528 local n={};
5299 while true do 5529 while true do
5300 local n,a; 5530 local s,a;
5301 e=o(t,e+1); 5531 e=o(t,e+1);
5302 if t:byte(e)~=34 then 5532 if t:byte(e)~=34 then
5303 if t:byte(e)==125 then return i,e+1;end 5533 if t:byte(e)==125 then return n,e+1;end
5304 return nil,"key expected"; 5534 return nil,"key expected";
5305 end 5535 end
5306 n,e=r(t,e); 5536 s,e=h(t,e);
5307 if n==nil then return nil,e;end 5537 if s==nil then return nil,e;end
5308 e=o(t,e); 5538 e=o(t,e);
5309 if t:byte(e)~=58 then return nil,"colon expected";end 5539 if t:byte(e)~=58 then return nil,"colon expected";end
5310 a,e=s(t,e+1); 5540 a,e=i(t,e+1);
5311 if a==nil then return nil,e;end 5541 if a==nil then return nil,e;end
5312 i[n]=a; 5542 n[s]=a;
5313 e=o(t,e); 5543 e=o(t,e);
5314 local t=t:byte(e); 5544 local t=t:byte(e);
5315 if t==125 then return c(i),e+1;end 5545 if t==125 then return u(n),e+1;end
5316 if t~=44 then return nil,"object eof";end 5546 if t~=44 then return nil,"object eof";end
5317 end 5547 end
5318 end 5548 end
5319 local function c(n,e) 5549 local function u(s,e)
5320 local i={}; 5550 local n={};
5321 while true do 5551 while true do
5322 local a,h; 5552 local a,h;
5323 a,e,h=s(n,e+1,93); 5553 a,e,h=i(s,e+1,93);
5324 if a==nil then 5554 if a==nil then
5325 if h then 5555 if h then
5326 if#i~=0 then 5556 if#n~=0 then
5327 return nil,"value expected"; 5557 return nil,"value expected";
5328 end 5558 end
5329 a,e=m(i,v),e+1; 5559 a,e=f(n,v),e+1;
5330 end 5560 end
5331 return a,e; 5561 return a,e;
5332 end 5562 end
5333 t(i,a); 5563 t(n,a);
5334 e=o(n,e); 5564 e=o(s,e);
5335 local t=n:byte(e); 5565 local t=s:byte(e);
5336 if t==93 then return m(i,v),e+1;end 5566 if t==93 then return f(n,v),e+1;end
5337 if t~=44 then return nil,"array eof";end 5567 if t~=44 then return nil,"array eof";end
5338 end 5568 end
5339 end 5569 end
5340 local t; 5570 local t;
5341 local function i(e) 5571 local function l(e)
5342 local t,e=d(e:sub(3,6),16),d(e:sub(9,12),16); 5572 local t,e=d(e:sub(3,6),16),d(e:sub(9,12),16);
5343 local e=t*1024+e-56613888; 5573 local e=t*1024+e-56613888;
5344 local o=e%64; 5574 local o=e%64;
5345 e=(e-o)/64; 5575 e=(e-o)/64;
5576 local a=e%64;
5577 e=(e-a)/64;
5346 local t=e%64; 5578 local t=e%64;
5347 e=(e-t)/64; 5579 e=(e-t)/64;
5348 local a=e%64; 5580 return s(240+e,128+t,128+a,128+o);
5349 e=(e-a)/64; 5581 end
5350 return n(240+e,128+a,128+t,128+o); 5582 local function s(e)
5351 end
5352 local function n(e)
5353 e=e:match("%x%x%x%x",3); 5583 e=e:match("%x%x%x%x",3);
5354 if e then 5584 if e then
5355 local e=d(e,16) 5585 local e=d(e,16)
5356 if e>=55296 and e<=57343 then t=true;end 5586 if e>=55296 and e<=57343 then t=true;end
5357 return q(e); 5587 return j(e);
5358 end 5588 end
5359 t=true; 5589 t=true;
5360 end 5590 end
5361 function r(o,e) 5591 function h(o,e)
5362 e=e+1; 5592 e=e+1;
5363 local a=o:find("\"",e,true); 5593 local a=o:find("\"",e,true);
5364 if a then 5594 if a then
5365 local e=o:sub(e,a-1); 5595 local e=o:sub(e,a-1);
5366 t=nil; 5596 t=nil;
5367 e=e:gsub("\\u[dD][89abAB]%x%x\\u[dD][cdefCDEF]%x%x",i); 5597 e=e:gsub("\\u[dD][89abAB]%x%x\\u[dD][cdefCDEF]%x%x",l);
5368 e=e:gsub("\\u.?.?.?.?",n); 5598 e=e:gsub("\\u.?.?.?.?",s);
5369 if t then return nil,"invalid escape";end 5599 if t then return nil,"invalid escape";end
5370 return e,a+1; 5600 return e,a+1;
5371 end 5601 end
5372 return nil,"string eof"; 5602 return nil,"string eof";
5373 end 5603 end
5374 local function i(e,t) 5604 local function s(e,t)
5375 local e=e:match("[0-9%.%-eE%+]+",t); 5605 local e=e:match("[0-9%.%-eE%+]+",t);
5376 return d(e),t+#e; 5606 return d(e),t+#e;
5377 end 5607 end
5378 local function d(t,e) 5608 local function d(t,e)
5379 local o,a,t=t:byte(e+1,e+3); 5609 local t,a,o=t:byte(e+1,e+3);
5380 if o==117 and a==108 and t==108 then 5610 if t==117 and a==108 and o==108 then
5381 return h,e+4; 5611 return n,e+4;
5382 end 5612 end
5383 return nil,"null parse failed"; 5613 return nil,"null parse failed";
5384 end 5614 end
5385 local function n(t,e) 5615 local function l(t,e)
5386 local o,t,a=t:byte(e+1,e+3); 5616 local o,t,a=t:byte(e+1,e+3);
5387 if o==114 and t==117 and a==101 then 5617 if o==114 and t==117 and a==101 then
5388 return true,e+4; 5618 return true,e+4;
5389 end 5619 end
5390 return nil,"true parse failed"; 5620 return nil,"true parse failed";
5391 end 5621 end
5392 local function h(t,e) 5622 local function n(t,e)
5393 local o,a,t,i=t:byte(e+1,e+4); 5623 local o,i,a,t=t:byte(e+1,e+4);
5394 if o==97 and a==108 and t==115 and i==101 then 5624 if o==97 and i==108 and a==115 and t==101 then
5395 return false,e+5; 5625 return false,e+5;
5396 end 5626 end
5397 return nil,"false parse failed"; 5627 return nil,"false parse failed";
5398 end 5628 end
5399 function s(a,e,s) 5629 function i(a,e,i)
5400 e=o(a,e); 5630 e=o(a,e);
5401 local t=a:byte(e); 5631 local t=a:byte(e);
5402 if t==123 then 5632 if t==123 then
5633 return c(a,e);
5634 elseif t==91 then
5403 return u(a,e); 5635 return u(a,e);
5404 elseif t==91 then
5405 return c(a,e);
5406 elseif t==34 then 5636 elseif t==34 then
5407 return r(a,e); 5637 return h(a,e);
5408 elseif t~=nil and t>=48 and t<=57 or t==45 then 5638 elseif t~=nil and t>=48 and t<=57 or t==45 then
5409 return i(a,e); 5639 return s(a,e);
5410 elseif t==110 then 5640 elseif t==110 then
5411 return d(a,e); 5641 return d(a,e);
5412 elseif t==116 then 5642 elseif t==116 then
5643 return l(a,e);
5644 elseif t==102 then
5413 return n(a,e); 5645 return n(a,e);
5414 elseif t==102 then 5646 elseif t==i then
5415 return h(a,e);
5416 elseif t==s then
5417 return nil,e,true; 5647 return nil,e,true;
5418 else 5648 else
5419 return nil,"value expected"; 5649 return nil,"value expected";
5420 end 5650 end
5421 end 5651 end
5430 ["\\t"]="\\u0009"; 5660 ["\\t"]="\\u0009";
5431 ["\\u"]="\\u"; 5661 ["\\u"]="\\u";
5432 }; 5662 };
5433 function a.decode(e) 5663 function a.decode(e)
5434 e=e:gsub("\\.",t) 5664 e=e:gsub("\\.",t)
5435 local t,a=s(e,1); 5665 local t,a=i(e,1);
5436 if t==nil then return t,a;end 5666 if t==nil then return t,a;end
5437 if e:find("[^ \t\r\n]",a)then return nil,"garbage at eof";end 5667 if e:find("[^ \t\r\n]",a)then return nil,"garbage at eof";end
5438 return t; 5668 return t;
5439 end 5669 end
5440 function a.test(e) 5670 function a.test(e)
5441 local e=a.encode(e); 5671 local e=a.encode(e);
5442 local t=a.decode(e); 5672 local t=a.decode(e);
5443 local t=a.encode(t); 5673 local t=a.encode(t);
5444 if e~=t then 5674 if e~=t then
5445 l("FAILED"); 5675 r("FAILED");
5446 l("encoded:",e); 5676 r("encoded:",e);
5447 l("recoded:",t); 5677 r("recoded:",t);
5448 else 5678 else
5449 l(e); 5679 r(e);
5450 end 5680 end
5451 return e==t; 5681 return e==t;
5452 end 5682 end
5453 return a; 5683 return a;
5454 end) 5684 end)
5463 _ENV=e; 5693 _ENV=e;
5464 _M=e; 5694 _M=e;
5465 return e; 5695 return e;
5466 end 5696 end
5467 local t=require"util.stanza"; 5697 local t=require"util.stanza";
5468 local u=require"lxp"; 5698 local d=require"lxp";
5469 local i=table.insert; 5699 local r=table.insert;
5470 local r=table.remove; 5700 local h=table.remove;
5471 local c=error; 5701 local l=error;
5472 local _ENV=nil; 5702 local _ENV=nil;
5473 local e=(function() 5703 local e=(function()
5474 local d={ 5704 local c={
5475 ["http://www.w3.org/XML/1998/namespace"]="xml"; 5705 ["http://www.w3.org/XML/1998/namespace"]="xml";
5476 }; 5706 };
5477 local n="\1"; 5707 local n="\1";
5478 local h="^([^"..n.."]*)"..n.."?(.*)$"; 5708 local s="^([^"..n.."]*)"..n.."?(.*)$";
5479 return function(l,o) 5709 return function(u,o)
5480 local e={}; 5710 local e={};
5481 local a=t.stanza("root"); 5711 local a=t.stanza("root");
5482 local t={}; 5712 local t={};
5483 local s={}; 5713 local i={};
5484 function e:StartNamespaceDecl(e,a) 5714 function e:StartNamespaceDecl(e,a)
5485 if e~=nil then 5715 if e~=nil then
5486 i(t,a); 5716 r(t,a);
5487 i(s,e); 5717 r(i,e);
5488 end 5718 end
5489 end 5719 end
5490 function e:EndNamespaceDecl(e) 5720 function e:EndNamespaceDecl(e)
5491 if e~=nil then 5721 if e~=nil then
5492 r(t); 5722 h(t);
5493 r(s); 5723 h(i);
5494 end 5724 end
5495 end 5725 end
5496 function e:StartElement(o,e) 5726 function e:StartElement(o,e)
5497 local o,i=o:match(h); 5727 local o,n=o:match(s);
5498 if i==""then 5728 if n==""then
5499 o,i="",o; 5729 o,n="",o;
5500 end 5730 end
5501 if o~=""then 5731 if o~=""then
5502 e.xmlns=o; 5732 e.xmlns=o;
5503 end 5733 end
5504 for t=1,#e do 5734 for t=1,#e do
5505 local a=e[t]; 5735 local a=e[t];
5506 e[t]=nil; 5736 e[t]=nil;
5507 local t,o=a:match(h); 5737 local t,o=a:match(s);
5508 if o~=""then 5738 if o~=""then
5509 t=d[t]; 5739 t=c[t];
5510 if t then 5740 if t then
5511 e[t..":"..o]=e[a]; 5741 e[t..":"..o]=e[a];
5512 e[a]=nil; 5742 e[a]=nil;
5513 end 5743 end
5514 end 5744 end
5515 end 5745 end
5516 local o={} 5746 local o={}
5517 for e=1,#t do 5747 for e=1,#t do
5518 o[s[e]]=t[e]; 5748 o[i[e]]=t[e];
5519 end 5749 end
5520 a:tag(i,e,o); 5750 a:tag(n,e,o);
5521 end 5751 end
5522 function e:CharacterData(e) 5752 function e:CharacterData(e)
5523 a:text(e); 5753 a:text(e);
5524 end 5754 end
5525 function e:EndElement() 5755 function e:EndElement()
5526 a:up(); 5756 a:up();
5527 end 5757 end
5528 local function t(t) 5758 local function t(t)
5529 if not t.stop or not t:stop()then 5759 if not t.stop or not t:stop()then
5530 c("Failed to abort parsing"); 5760 l("Failed to abort parsing");
5531 end 5761 end
5532 end 5762 end
5533 e.StartDoctypeDecl=t; 5763 e.StartDoctypeDecl=t;
5534 if not o or not o.allow_comments then 5764 if not o or not o.allow_comments then
5535 e.Comment=t; 5765 e.Comment=t;
5536 end 5766 end
5537 if not o or not o.allow_processing_instructions then 5767 if not o or not o.allow_processing_instructions then
5538 e.ProcessingInstruction=t; 5768 e.ProcessingInstruction=t;
5539 end 5769 end
5540 local t=u.new(e,n); 5770 local t=d.new(e,n);
5541 local e,i,o,n=t:parse(l); 5771 local e,o,n,i=t:parse(u);
5542 if e then e,i,o,n=t:parse();end 5772 if e then e,o,n,i=t:parse();end
5543 if e then 5773 if e then
5544 return a.tags[1]; 5774 return a.tags[1];
5545 else 5775 else
5546 return e,("%s (line %d, col %d))"):format(i,o,n); 5776 return e,("%s (line %d, col %d))"):format(o,n,i);
5547 end 5777 end
5548 end; 5778 end;
5549 end)(); 5779 end)();
5550 return{ 5780 return{
5551 parse=e; 5781 parse=e;
5561 end 5791 end
5562 _ENV=e; 5792 _ENV=e;
5563 _M=e; 5793 _M=e;
5564 return e; 5794 return e;
5565 end 5795 end
5566 local s=require"util.stanza".stanza; 5796 local h=require"util.stanza".stanza;
5567 local t,o=tostring,tonumber; 5797 local o,i=tostring,tonumber;
5568 local h=type; 5798 local s=type;
5569 local n=pairs; 5799 local r=pairs;
5570 local i='http://jabber.org/protocol/rsm'; 5800 local n='http://jabber.org/protocol/rsm';
5571 local a={}; 5801 local a={};
5572 do 5802 do
5573 local e=a; 5803 local e=a;
5574 local function t(e) 5804 local function t(e)
5575 return o((e:get_text())); 5805 return i((e:get_text()));
5576 end 5806 end
5577 local function a(t) 5807 local function a(t)
5578 return t:get_text(); 5808 return t:get_text();
5579 end 5809 end
5580 e.after=a; 5810 e.after=a;
5583 return e==""or e; 5813 return e==""or e;
5584 end; 5814 end;
5585 e.max=t; 5815 e.max=t;
5586 e.index=t; 5816 e.index=t;
5587 e.first=function(e) 5817 e.first=function(e)
5588 return{index=o(e.attr.index);e:get_text()}; 5818 return{index=i(e.attr.index);e:get_text()};
5589 end; 5819 end;
5590 e.last=a; 5820 e.last=a;
5591 e.count=t; 5821 e.count=t;
5592 end 5822 end
5593 local h=setmetatable({ 5823 local s=setmetatable({
5594 first=function(a,e) 5824 first=function(t,e)
5595 if h(e)=="table"then 5825 if s(e)=="table"then
5596 a:tag("first",{index=e.index}):text(e[1]):up(); 5826 t:tag("first",{index=e.index}):text(e[1]):up();
5597 else 5827 else
5598 a:tag("first"):text(t(e)):up(); 5828 t:tag("first"):text(o(e)):up();
5599 end 5829 end
5600 end; 5830 end;
5601 before=function(a,e) 5831 before=function(t,e)
5602 if e==true then 5832 if e==true then
5603 a:tag("before"):up(); 5833 t:tag("before"):up();
5604 else 5834 else
5605 a:tag("before"):text(t(e)):up(); 5835 t:tag("before"):text(o(e)):up();
5606 end 5836 end
5607 end 5837 end
5608 },{ 5838 },{
5609 __index=function(a,e) 5839 __index=function(t,e)
5610 return function(a,o) 5840 return function(a,t)
5611 a:tag(e):text(t(o)):up(); 5841 a:tag(e):text(o(t)):up();
5612 end 5842 end
5613 end; 5843 end;
5614 }); 5844 });
5615 local function t(e) 5845 local function o(e)
5616 local o={}; 5846 local o={};
5617 for t in e:childtags()do 5847 for t in e:childtags()do
5618 local e=t.name; 5848 local e=t.name;
5619 local a=e and a[e]; 5849 local a=e and a[e];
5620 if a then 5850 if a then
5621 o[e]=a(t); 5851 o[e]=a(t);
5622 end 5852 end
5623 end 5853 end
5624 return o; 5854 return o;
5625 end 5855 end
5626 local function o(t) 5856 local function i(t)
5627 local e=s("set",{xmlns=i}); 5857 local e=h("set",{xmlns=n});
5628 for t,o in n(t)do 5858 for t,o in r(t)do
5629 if a[t]then 5859 if a[t]then
5630 h[t](e,o); 5860 s[t](e,o);
5631 end 5861 end
5632 end 5862 end
5633 return e; 5863 return e;
5634 end 5864 end
5635 local function a(e) 5865 local function t(e)
5636 local e=e:get_child("set",i); 5866 local e=e:get_child("set",n);
5637 if e and#e.tags>0 then 5867 if e and#e.tags>0 then
5638 return t(e); 5868 return o(e);
5639 end 5869 end
5640 end 5870 end
5641 return{parse=t,generate=o,get=a}; 5871 return{parse=o,generate=i,get=t};
5642 end) 5872 end)
5643 package.preload['util.random']=(function(...) 5873 package.preload['util.random']=(function(...)
5644 local _ENV=_ENV; 5874 local _ENV=_ENV;
5645 local function e(t,...) 5875 local function e(t,...)
5646 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 5876 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
5676 end 5906 end
5677 local s=require"util.net"; 5907 local s=require"util.net";
5678 local n=require"util.hex"; 5908 local n=require"util.hex";
5679 local a={}; 5909 local a={};
5680 local o={ 5910 local o={
5681 __index=function(o,t) 5911 __index=function(e,t)
5682 local e=a[t]; 5912 local a=a[t];
5683 if not e then return nil;end 5913 if not a then return nil;end
5684 local e=e(o); 5914 local a=a(e);
5685 o[t]=e; 5915 e[t]=a;
5686 return e; 5916 return a;
5687 end, 5917 end,
5688 __tostring=function(e)return e.addr;end, 5918 __tostring=function(e)return e.addr;end,
5689 }; 5919 };
5690 o.__eq=function(t,e) 5920 o.__eq=function(e,t)
5691 if getmetatable(t)~=o or getmetatable(e)~=o then 5921 if getmetatable(e)~=o or getmetatable(t)~=o then
5692 return false; 5922 return false;
5693 end 5923 end
5694 return t.packed==e.packed; 5924 return e.packed==t.packed;
5695 end 5925 end
5696 local h={ 5926 local h={
5697 ["0"]="0000",["1"]="0001",["2"]="0010",["3"]="0011", 5927 ["0"]="0000",["1"]="0001",["2"]="0010",["3"]="0011",
5698 ["4"]="0100",["5"]="0101",["6"]="0110",["7"]="0111", 5928 ["4"]="0100",["5"]="0101",["6"]="0110",["7"]="0111",
5699 ["8"]="1000",["9"]="1001",["A"]="1010",["B"]="1011", 5929 ["8"]="1000",["9"]="1001",["A"]="1010",["B"]="1011",
5734 e=e.toV4mapped; 5964 e=e.toV4mapped;
5735 end 5965 end
5736 return e.bits; 5966 return e.bits;
5737 end 5967 end
5738 local e; 5968 local e;
5739 local function v(t,a) 5969 local function y(t,a)
5740 t,a=t.bits_full,a.bits_full; 5970 t,a=t.bits_full,a.bits_full;
5741 for e=1,128 do 5971 for e=1,128 do
5742 if t:sub(e,e)~=a:sub(e,e)then 5972 if t:sub(e,e)~=a:sub(e,e)then
5743 return e-1; 5973 return e-1;
5744 end 5974 end
5745 end 5975 end
5746 return 128; 5976 return 128;
5747 end 5977 end
5748 local n=t("::1"); 5978 local i=t("::1");
5749 local w=t("127.0.0.0"); 5979 local m=t("127.0.0.0");
5750 local h=t("2002::"); 5980 local u=t("2002::");
5751 local d=t("2001::"); 5981 local h=t("2001::");
5752 local p=t("fe80::"); 5982 local f=t("fe80::");
5753 local m=t("169.254.0.0"); 5983 local w=t("169.254.0.0");
5754 local r=t("fc00::"); 5984 local c=t("fc00::");
5755 local i=t("fec0::"); 5985 local n=t("fec0::");
5756 local l=t("3ffe::"); 5986 local r=t("3ffe::");
5757 local u=t("::"); 5987 local d=t("::");
5758 local f=t("ff00::"); 5988 local v=t("ff00::");
5759 local c=t("::ffff:0:0"); 5989 local l=t("::ffff:0:0");
5760 local function y(t) 5990 local function p(t)
5761 if e(t,w,8)then 5991 if e(t,m,8)then
5762 return 2; 5992 return 2;
5763 elseif e(t,m,16)then 5993 elseif e(t,w,16)then
5764 return 2; 5994 return 2;
5765 else 5995 else
5766 return 14; 5996 return 14;
5767 end 5997 end
5768 end 5998 end
5769 local function w(t) 5999 local function w(t)
5770 if t==n then 6000 if t==i then
5771 return 2; 6001 return 2;
5772 elseif e(t,p,10)then 6002 elseif e(t,f,10)then
5773 return 2; 6003 return 2;
5774 elseif e(t,i,10)then 6004 elseif e(t,n,10)then
5775 return 5; 6005 return 5;
5776 elseif e(t,f,10)then 6006 elseif e(t,v,10)then
5777 return t.packed:byte(2)%16; 6007 return t.packed:byte(2)%16;
5778 else 6008 else
5779 return 14; 6009 return 14;
5780 end 6010 end
5781 end 6011 end
5782 local function m(t) 6012 local function m(t)
5783 if t==n then 6013 if t==i then
5784 return 0; 6014 return 0;
5785 elseif e(t,h,16)then 6015 elseif e(t,u,16)then
5786 return 2; 6016 return 2;
5787 elseif e(t,d,32)then 6017 elseif e(t,h,32)then
5788 return 5; 6018 return 5;
5789 elseif e(t,r,7)then 6019 elseif e(t,c,7)then
5790 return 13; 6020 return 13;
5791 elseif e(t,i,10)then 6021 elseif e(t,n,10)then
5792 return 11; 6022 return 11;
5793 elseif e(t,l,16)then 6023 elseif e(t,r,16)then
5794 return 12; 6024 return 12;
5795 elseif e(t,u,96)then 6025 elseif e(t,d,96)then
5796 return 3; 6026 return 3;
5797 elseif e(t,c,96)then 6027 elseif e(t,l,96)then
5798 return 4; 6028 return 4;
5799 else 6029 else
5800 return 1; 6030 return 1;
5801 end 6031 end
5802 end 6032 end
5803 local function f(t) 6033 local function f(t)
5804 if t==n then 6034 if t==i then
5805 return 50; 6035 return 50;
5806 elseif e(t,h,16)then 6036 elseif e(t,u,16)then
5807 return 30; 6037 return 30;
5808 elseif e(t,d,32)then 6038 elseif e(t,h,32)then
5809 return 5; 6039 return 5;
5810 elseif e(t,r,7)then 6040 elseif e(t,c,7)then
5811 return 3; 6041 return 3;
5812 elseif e(t,i,10)then 6042 elseif e(t,n,10)then
5813 return 1; 6043 return 1;
5814 elseif e(t,l,16)then 6044 elseif e(t,r,16)then
5815 return 1; 6045 return 1;
5816 elseif e(t,u,96)then 6046 elseif e(t,d,96)then
5817 return 1; 6047 return 1;
5818 elseif e(t,c,96)then 6048 elseif e(t,l,96)then
5819 return 35; 6049 return 35;
5820 else 6050 else
5821 return 40; 6051 return 40;
5822 end 6052 end
5823 end 6053 end
5840 return f(self); 6070 return f(self);
5841 end 6071 end
5842 end 6072 end
5843 function a:scope() 6073 function a:scope()
5844 if self.proto=="IPv4"then 6074 if self.proto=="IPv4"then
5845 return y(self); 6075 return p(self);
5846 else 6076 else
5847 return w(self); 6077 return w(self);
5848 end 6078 end
5849 end 6079 end
5850 local r=t("10.0.0.0"); 6080 local n=t("10.0.0.0");
5851 local h=t("172.16.0.0"); 6081 local i=t("172.16.0.0");
5852 local i=t("192.168.0.0"); 6082 local r=t("192.168.0.0");
5853 local n=t("100.64.0.0"); 6083 local h=t("100.64.0.0");
5854 function a:private() 6084 function a:private()
5855 local t=self.scope~=14; 6085 local t=self.scope~=14;
5856 if not t and self.proto=="IPv4"then 6086 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); 6087 return e(self,n,8)or e(self,i,12)or e(self,r,16)or e(self,h,10);
5858 end 6088 end
5859 return t; 6089 return t;
5860 end 6090 end
5861 local function n(e) 6091 local function n(e)
5862 local o; 6092 local o;
5896 end 6126 end
5897 return t(s.ntop(e.packed:sub(1,a)..("\0"):rep(#e.packed-a))) 6127 return t(s.ntop(e.packed:sub(1,a)..("\0"):rep(#e.packed-a)))
5898 end 6128 end
5899 return{ 6129 return{
5900 new_ip=t, 6130 new_ip=t,
5901 commonPrefixLength=v, 6131 commonPrefixLength=y,
5902 parse_cidr=n, 6132 parse_cidr=n,
5903 match=e, 6133 match=e,
5904 is_ip=i; 6134 is_ip=i;
5905 truncate=o; 6135 truncate=o;
5906 }; 6136 };
5915 end 6145 end
5916 _ENV=e; 6146 _ENV=e;
5917 _M=e; 6147 _M=e;
5918 return e; 6148 return e;
5919 end 6149 end
5920 local h=string.char; 6150 local n=string.char;
5921 local s=string.format; 6151 local s=string.format;
5922 local o=string.gsub; 6152 local a=string.gsub;
5923 local n=string.lower; 6153 local h=string.lower;
5924 local a={}; 6154 local o={};
5925 local i={}; 6155 local i={};
5926 do 6156 do
5927 local e,t; 6157 local t,e;
5928 for o=0,255 do 6158 for a=0,255 do
5929 e,t=h(o),s("%02x",o); 6159 t,e=n(a),s("%02x",a);
5930 a[e]=t; 6160 o[t]=e;
5931 i[t]=e; 6161 i[e]=t;
5932 end 6162 end
5933 end 6163 end
5934 local function t(e) 6164 local function t(e)
5935 return(o(e,".",a)); 6165 return(a(e,".",o));
5936 end 6166 end
5937 local function e(e) 6167 local function e(e)
5938 return(o(n(e),"%X*(%x%x)%X*",i)); 6168 return(a(h(e),"%X*(%x%x)%X*",i));
5939 end 6169 end
5940 return{ 6170 return{
5941 encode=t,decode=e; 6171 encode=t,decode=e;
5942 to=t,from=e; 6172 to=t,from=e;
5943 }; 6173 };
5974 end 6204 end
5975 _ENV=e; 6205 _ENV=e;
5976 _M=e; 6206 _M=e;
5977 return e; 6207 return e;
5978 end 6208 end
5979 local n=type; 6209 local i=type;
5980 local o=pairs; 6210 local a=pairs;
5981 local t=rawset; 6211 local t=rawset;
5982 local r=rawget; 6212 local r=rawget;
5983 local c=error; 6213 local f=error;
5984 local d=table.concat; 6214 local m=table.concat;
5985 local l=table.insert; 6215 local u=table.insert;
5986 local u=setmetatable; 6216 local c=setmetatable;
5987 local m=require"util.paths".resolve_relative_path; 6217 local l=require"util.paths".resolve_relative_path;
5988 local _ENV=nil; 6218 local _ENV=nil;
5989 local i={}; 6219 local n={};
5990 local e={}; 6220 local e={};
5991 local f=function(e)return e end 6221 local d=function(e)return e end
5992 function i.options(h,s,e) 6222 function n.options(h,s,e)
5993 local a=h[s]or{}; 6223 local o=h[s]or{};
5994 if n(e)~="table"then e={e}end 6224 if i(e)~="table"then e={e}end
5995 for o,e in o(e)do 6225 for a,e in a(e)do
5996 if e==true or e==false then 6226 if e==true or e==false then
5997 a[o]=e; 6227 o[a]=e;
5998 else 6228 else
5999 a[e]=true; 6229 o[e]=true;
6000 end 6230 end
6001 end 6231 end
6002 t(h,s,a) 6232 t(h,s,o)
6003 end 6233 end
6004 i.verifyext=i.options; 6234 n.verifyext=n.options;
6005 function e.options(a) 6235 function e.options(o)
6006 local t={}; 6236 local t={};
6007 for e,a in o(a)do 6237 for a,e in a(o)do
6008 if a then 6238 if e then
6009 t[#t+1]=e; 6239 t[#t+1]=a;
6010 end 6240 end
6011 end 6241 end
6012 return t; 6242 return t;
6013 end 6243 end
6014 e.verifyext=e.options; 6244 e.verifyext=e.options;
6015 function e.ciphers(t) 6245 function e.ciphers(t)
6016 if n(t)=="table"then 6246 if i(t)=="table"then
6017 return d(t,":"); 6247 return m(t,":");
6018 end 6248 end
6019 return t; 6249 return t;
6020 end 6250 end
6021 e.curveslist=e.ciphers; 6251 e.curveslist=e.ciphers;
6022 e.ciphersuites=e.ciphers; 6252 e.ciphersuites=e.ciphers;
6023 function e.key(t,a) 6253 function e.key(t,a)
6024 if n(t)=="string"then 6254 if i(t)=="string"then
6025 return m(a._basedir,t); 6255 return l(a._basedir,t);
6026 else 6256 else
6027 return nil 6257 return nil
6028 end 6258 end
6029 end 6259 end
6030 e.certificate=e.key; 6260 e.certificate=e.key;
6031 e.cafile=e.key; 6261 e.cafile=e.key;
6032 e.capath=e.key; 6262 e.capath=e.key;
6033 e.dhparam=e.key; 6263 e.dhparam=e.key;
6034 local a={"sslv2","sslv3","tlsv1","tlsv1_1","tlsv1_2","tlsv1_3"}; 6264 local o={"sslv2","sslv3","tlsv1","tlsv1_1","tlsv1_2","tlsv1_3"};
6035 for e=1,#a do a[a[e].."+"]=e-1;end 6265 for e=1,#o do o[o[e].."+"]=e-1;end
6036 local function h(e) 6266 local function h(e)
6037 local t=a[e.protocol]; 6267 local t=o[e.protocol];
6038 if t then 6268 if t then
6039 e.protocol="sslv23"; 6269 e.protocol="sslv23";
6040 for t=1,t do 6270 for t=1,t do
6041 l(e.options,"no_"..a[t]); 6271 u(e.options,"no_"..o[t]);
6042 end 6272 end
6043 end 6273 end
6044 end 6274 end
6045 local function s(e,a) 6275 local function s(o,e)
6046 t(e,"_cache",nil); 6276 t(o,"_cache",nil);
6047 if n(a)=="table"then 6277 if i(e)=="table"then
6048 for a,o in o(a)do 6278 for e,a in a(e)do
6049 if a:sub(1,1)~="_"then 6279 if e:sub(1,1)~="_"then
6050 (i[a]or t)(e,a,o); 6280 (n[e]or t)(o,e,a);
6051 end 6281 end
6052 end 6282 end
6053 end 6283 end
6054 return e 6284 return o
6055 end 6285 end
6056 local function n(i) 6286 local function i(i)
6057 local a={}; 6287 local o={};
6058 for t,o in o(i)do 6288 for t,a in a(i)do
6059 if t:sub(1,1)~="_"then 6289 if t:sub(1,1)~="_"then
6060 a[t]=(e[t]or f)(o,i); 6290 o[t]=(e[t]or d)(a,i);
6061 end 6291 end
6062 end 6292 end
6063 h(a); 6293 h(o);
6064 return a; 6294 return o;
6065 end 6295 end
6066 local function i(e) 6296 local function n(e)
6067 local a=r(e,"_cache"); 6297 local a=r(e,"_cache");
6068 if a then 6298 if a then
6069 return a,nil 6299 return a,nil
6070 end 6300 end
6071 local a,o=r(e,"_context_factory")(e:final(),e); 6301 local a,o=r(e,"_context_factory")(e:final(),e);
6075 return a,o 6305 return a,o
6076 end 6306 end
6077 local e={ 6307 local e={
6078 __index={ 6308 __index={
6079 apply=s; 6309 apply=s;
6080 final=n; 6310 final=i;
6081 build=i; 6311 build=n;
6082 }; 6312 };
6083 __newindex=function() 6313 __newindex=function()
6084 c("SSL config objects cannot be modified directly. Use :apply()") 6314 f("SSL config objects cannot be modified directly. Use :apply()")
6085 end; 6315 end;
6086 }; 6316 };
6087 local function a(t,a) 6317 local function n(t,a)
6088 return u({ 6318 return c({
6089 _context_factory=t, 6319 _context_factory=t,
6090 _basedir=a, 6320 _basedir=a,
6091 options={}, 6321 options={},
6092 },e); 6322 },e);
6093 end 6323 end
6094 local function h(i) 6324 local function h(i)
6095 local a=a(); 6325 local o=n();
6096 for e,o in o(i)do 6326 for e,a in a(i)do
6097 t(a,e,o); 6327 t(o,e,a);
6098 end 6328 end
6099 return a 6329 return o
6100 end 6330 end
6101 e.__index.clone=h; 6331 e.__index.clone=h;
6102 return{ 6332 return{
6103 apply=s; 6333 apply=s;
6104 final=n; 6334 final=i;
6105 _new=a; 6335 _new=n;
6106 }; 6336 };
6107 end) 6337 end)
6108 package.preload['util.paths']=(function(...) 6338 package.preload['util.paths']=(function(...)
6109 local _ENV=_ENV; 6339 local _ENV=_ENV;
6110 local function e(t,...) 6340 local function e(t,...)
6116 _ENV=e; 6346 _ENV=e;
6117 _M=e; 6347 _M=e;
6118 return e; 6348 return e;
6119 end 6349 end
6120 local i=table.concat; 6350 local i=table.concat;
6121 local t=package.config:sub(1,1); 6351 local e=package.config:sub(1,1);
6122 local a={} 6352 local a={}
6123 function a.resolve_relative_path(a,e) 6353 function a.resolve_relative_path(o,t)
6124 if e then 6354 if t then
6125 a=a:gsub("%"..t.."+$",""); 6355 o=o:gsub("%"..e.."+$","");
6126 e=e:gsub("^%.%"..t.."+",""); 6356 t=t:gsub("^%.%"..e.."+","");
6127 local o; 6357 local a;
6128 if t=="/"and e:sub(1,1)~="/"then 6358 if e=="/"and t:sub(1,1)~="/"then
6129 o=true; 6359 a=true;
6130 elseif t=="\\"and(e:sub(1,1)~="/"and(e:sub(2,3)~=":\\"and e:sub(2,3)~=":/"))then 6360 elseif e=="\\"and(t:sub(1,1)~="/"and(t:sub(2,3)~=":\\"and t:sub(2,3)~=":/"))then
6131 o=true; 6361 a=true;
6132 end 6362 end
6133 if o then 6363 if a then
6134 return a..t..e; 6364 return o..e..t;
6135 end 6365 end
6136 end 6366 end
6137 return e; 6367 return t;
6138 end 6368 end
6139 function a.glob_to_pattern(e) 6369 function a.glob_to_pattern(e)
6140 return"^"..e:gsub("[%p*?]",function(e) 6370 return"^"..e:gsub("[%p*?]",function(e)
6141 if e=="*"then 6371 if e=="*"then
6142 return".*"; 6372 return".*";
6145 else 6375 else
6146 return"%"..e; 6376 return"%"..e;
6147 end 6377 end
6148 end).."$"; 6378 end).."$";
6149 end 6379 end
6150 function a.join(a,e,o,...) 6380 function a.join(t,a,o,...)
6151 if e then 6381 if a then
6152 if o then 6382 if o then
6153 if...then 6383 if...then
6154 return i({a,e,o,...},t); 6384 return i({t,a,o,...},e);
6155 end 6385 end
6156 return a..t..e..t..o; 6386 return t..e..a..e..o;
6157 end 6387 end
6158 return a..t..e; 6388 return t..e..a;
6159 end 6389 end
6160 return a; 6390 return t;
6161 end 6391 end
6162 function a.complement_lua_path(t) 6392 function a.complement_lua_path(t)
6163 local a=_VERSION:match(" (.+)$"); 6393 local o=_VERSION:match(" (.+)$");
6164 local o=package.config:sub(3,3); 6394 local a=package.config:sub(3,3);
6165 local e=package.config:sub(1,1); 6395 local e=package.config:sub(1,1);
6166 local a=e.."lua"..e..a..e; 6396 local o=e.."lua"..e..o..e;
6167 if not string.find(package.path,t,1,true)then 6397 if not string.find(package.path,t,1,true)then
6168 package.path=package.path..o..t..e.."share"..a.."?.lua"; 6398 package.path=package.path..a..t..e.."share"..o.."?.lua";
6169 package.path=package.path..o..t..e.."share"..a.."?"..e.."init.lua"; 6399 package.path=package.path..a..t..e.."share"..o.."?"..e.."init.lua";
6170 end 6400 end
6171 if not string.find(package.path,t,1,true)then 6401 if not string.find(package.path,t,1,true)then
6172 package.cpath=package.cpath..o..t..e.."lib"..a.."?.so"; 6402 package.cpath=package.cpath..a..t..e.."lib"..o.."?.so";
6173 end 6403 end
6174 end 6404 end
6175 return a; 6405 return a;
6176 end) 6406 end)
6177 package.preload['util.mathcompat']=(function(...) 6407 package.preload['util.mathcompat']=(function(...)
6340 _ENV=e; 6570 _ENV=e;
6341 _M=e; 6571 _M=e;
6342 return e; 6572 return e;
6343 end 6573 end
6344 local t=require"ssl"; 6574 local t=require"ssl";
6345 local h=t.newcontext; 6575 local o=t.newcontext;
6346 local s=t.context or require"ssl.context"; 6576 local s=t.context or require"ssl.context";
6347 local o=io.open; 6577 local n=io.open;
6348 local e={}; 6578 local e={};
6349 local n={__index=e}; 6579 local h={__index=e};
6350 function e:set_sni_host(a,t,e) 6580 function e:set_sni_host(a,t,e)
6351 local e,t=self._builder:clone():apply({ 6581 local e,t=self._builder:clone():apply({
6352 certificate=t, 6582 certificate=t,
6353 key=e, 6583 key=e,
6354 }):build(); 6584 }):build();
6368 end 6598 end
6369 return t,nil 6599 return t,nil
6370 end 6600 end
6371 local function i(e,i) 6601 local function i(e,i)
6372 if type(e.dhparam)=="string"then 6602 if type(e.dhparam)=="string"then
6373 local t,a=o(e.dhparam); 6603 local t,a=n(e.dhparam);
6374 if not t then return nil,"Could not open DH parameters: "..a end 6604 if not t then return nil,"Could not open DH parameters: "..a end
6375 local a=t:read("*a"); 6605 local a=t:read("*a");
6376 t:close(); 6606 t:close();
6377 e.dhparam=function()return a;end 6607 e.dhparam=function()return a;end
6378 end 6608 end
6379 local t,a=h(e); 6609 local t,a=o(e);
6380 if not t then 6610 if not t then
6381 return nil,a 6611 return nil,a
6382 end 6612 end
6383 if t and e.ciphers then 6613 if t and e.ciphers then
6384 local o; 6614 local o;
6389 end 6619 end
6390 return setmetatable({ 6620 return setmetatable({
6391 _inner=t, 6621 _inner=t,
6392 _builder=i, 6622 _builder=i,
6393 _sni_contexts={}, 6623 _sni_contexts={},
6394 },n),nil 6624 },h),nil
6395 end 6625 end
6396 return{ 6626 return{
6397 new_context=i, 6627 new_context=i,
6398 }; 6628 };
6399 end) 6629 end)
6407 end 6637 end
6408 _ENV=e; 6638 _ENV=e;
6409 _M=e; 6639 _M=e;
6410 return e; 6640 return e;
6411 end 6641 end
6412 local n,m=require"mime".b64,require"mime".unb64; 6642 local s,h=require"mime".b64,require"mime".unb64;
6413 local t=require"util.hashes"; 6643 local t=require"util.hashes";
6414 local a=require"bit"; 6644 local i=require"bit";
6415 local d=require"util.random"; 6645 local l=require"util.random";
6416 local p=tonumber; 6646 local c=tonumber;
6417 local s,e=string.char,string.byte; 6647 local a,e=string.char,string.byte;
6418 local i=string.gsub; 6648 local o=string.gsub;
6419 local h=a.bxor; 6649 local i=i.bxor;
6420 local function r(a,t) 6650 local function r(n,t)
6421 return(i(a,"()(.)",function(a,o) 6651 return(o(n,"()(.)",function(n,o)
6422 return s(h(e(o),e(t,a))) 6652 return a(i(e(o),e(t,n)))
6423 end)); 6653 end));
6424 end 6654 end
6425 local f,t=t.sha1,t.hmac_sha1; 6655 local y,t=t.sha1,t.hmac_sha1;
6426 local function w(o,e,i) 6656 local function w(o,e,i)
6427 local e=t(o,e.."\0\0\0\1"); 6657 local e=t(o,e.."\0\0\0\1");
6428 local a=e; 6658 local a=e;
6429 for i=2,i do 6659 for i=2,i do
6430 e=t(o,e); 6660 e=t(o,e);
6431 a=r(a,e); 6661 a=r(a,e);
6432 end 6662 end
6433 return a; 6663 return a;
6434 end 6664 end
6435 local function y(e) 6665 local function f(e)
6436 return e; 6666 return e;
6437 end 6667 end
6438 local function a(e) 6668 local function a(e)
6439 return(i(e,"[,=]",{[","]="=2C",["="]="=3D"})); 6669 return(o(e,"[,=]",{[","]="=2C",["="]="=3D"}));
6440 end 6670 end
6441 local function s(e) 6671 local function n(e)
6442 if e:ssl()then 6672 if e:ssl()then
6443 local e=e:socket(); 6673 local e=e:socket();
6444 if e.info and e:info().protocol=="TLSv1.3"then 6674 if e.info and e:info().protocol=="TLSv1.3"then
6445 if e.exportkeyingmaterial then 6675 if e.exportkeyingmaterial then
6446 return"p=tls-exporter",e:exportkeyingmaterial("EXPORTER-Channel-Binding",32,""); 6676 return"p=tls-exporter",e:exportkeyingmaterial("EXPORTER-Channel-Binding",32,"");
6448 elseif e.getfinished then 6678 elseif e.getfinished then
6449 return"p=tls-unique",e:getfinished(); 6679 return"p=tls-unique",e:getfinished();
6450 end 6680 end
6451 end 6681 end
6452 end 6682 end
6453 local function h(e,o) 6683 local function d(e,i)
6454 local a="n="..a(e.username); 6684 local a="n="..a(e.username);
6455 local i=n(d.bytes(15)); 6685 local o=s(l.bytes(15));
6456 local h="r="..i; 6686 local u="r="..o;
6457 local l=a..","..h; 6687 local l=a..","..u;
6458 local d=""; 6688 local d="";
6459 local a="n"; 6689 local a="n";
6460 if o=="SCRAM-SHA-1-PLUS"then 6690 if i=="SCRAM-SHA-1-PLUS"then
6461 a,d=s(e.conn); 6691 a,d=n(e.conn);
6462 elseif s(e.conn)then 6692 elseif n(e.conn)then
6463 a="y"; 6693 a="y";
6464 end 6694 end
6465 local s=a..",,"; 6695 local n=a..",,";
6466 local a=s..l; 6696 local a=n..l;
6467 local a,u=coroutine.yield(a); 6697 local a,m=coroutine.yield(a);
6468 if a~="challenge"then return false end 6698 if a~="challenge"then return false end
6469 local a,o,c=u:match("(r=[^,]+),s=([^,]*),i=(%d+)"); 6699 local a,i,p=m:match("(r=[^,]+),s=([^,]*),i=(%d+)");
6470 local c=p(c); 6700 local c=c(p);
6471 o=m(o); 6701 i=h(i);
6472 if not a or not o or not c then 6702 if not a or not i or not c then
6473 return false,"Could not parse server_first_message"; 6703 return false,"Could not parse server_first_message";
6474 elseif a:find(i,3,true)~=3 then 6704 elseif a:find(o,3,true)~=3 then
6475 return false,"nonce sent by server does not match our nonce"; 6705 return false,"nonce sent by server does not match our nonce";
6476 elseif a==h then 6706 elseif a==u then
6477 return false,"server did not append s-nonce to nonce"; 6707 return false,"server did not append s-nonce to nonce";
6478 end 6708 end
6479 local i=s..d; 6709 local o=n..d;
6480 local i="c="..n(i); 6710 local o="c="..s(o);
6481 local h=i..","..a; 6711 local d=o..","..a;
6482 local i;
6483 local a; 6712 local a;
6484 local s; 6713 local o;
6714 local n;
6485 if e.client_key and e.server_key then 6715 if e.client_key and e.server_key then
6486 a=e.client_key; 6716 o=e.client_key;
6487 s=e.server_key; 6717 n=e.server_key;
6488 else 6718 else
6489 if e.salted_password then 6719 if e.salted_password then
6490 i=e.salted_password; 6720 a=e.salted_password;
6491 elseif e.password then 6721 elseif e.password then
6492 i=w(y(e.password),o,c); 6722 a=w(f(e.password),i,c);
6493 end 6723 end
6494 s=t(i,"Server Key"); 6724 n=t(a,"Server Key");
6495 a=t(i,"Client Key"); 6725 o=t(a,"Client Key");
6496 end 6726 end
6497 local o=f(a); 6727 local a=y(o);
6498 local e=l..","..u..","..h; 6728 local e=l..","..m..","..d;
6499 local o=t(o,e); 6729 local a=t(a,e);
6500 local a=r(a,o); 6730 local o=r(o,a);
6501 local t=t(s,e); 6731 local a=t(n,e);
6502 local e="p="..n(a); 6732 local e="p="..s(o);
6503 local e=h..","..e; 6733 local e=d..","..e;
6504 local e,a=coroutine.yield(e); 6734 local t,e=coroutine.yield(e);
6505 if e~="success"then return false,"success-expected"end 6735 if t~="success"then return false,"success-expected"end
6506 local e=a:match("v=([^,]+)"); 6736 local e=e:match("v=([^,]+)");
6507 if m(e)~=t then 6737 if h(e)~=a then
6508 return false,"server signature did not match"; 6738 return false,"server signature did not match";
6509 end 6739 end
6510 return true; 6740 return true;
6511 end 6741 end
6512 return function(e,t) 6742 return function(e,t)
6513 if e.username and(e.password or(e.client_key or e.server_key))then 6743 if e.username and(e.password or(e.client_key or e.server_key))then
6514 if t=="SCRAM-SHA-1"then 6744 if t=="SCRAM-SHA-1"then
6515 return h,99; 6745 return d,99;
6516 elseif t=="SCRAM-SHA-1-PLUS"then 6746 elseif t=="SCRAM-SHA-1-PLUS"then
6517 if s(e.conn)then 6747 if n(e.conn)then
6518 return h,100; 6748 return d,100;
6519 end 6749 end
6520 end 6750 end
6521 end 6751 end
6522 end 6752 end
6523 end) 6753 end)
6571 end 6801 end
6572 _ENV=e; 6802 _ENV=e;
6573 _M=e; 6803 _M=e;
6574 return e; 6804 return e;
6575 end 6805 end
6576 return function(a,e) 6806 return function(t,e)
6577 if e=="OAUTHBEARER"and a.username then 6807 if e=="OAUTHBEARER"and t.username then
6578 return function(e) 6808 return function(e)
6579 local t=e.bearer_token and("Bearer "..e.bearer_token)or""; 6809 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"); 6810 local t,a=coroutine.yield("n,a="..e.username.."@"..e.host..",\001auth="..t.."\001");
6581 if t=="success"then 6811 if t=="success"then
6582 return true; 6812 return true;
6587 if coroutine.yield("\001")~="failure"then 6817 if coroutine.yield("\001")~="failure"then
6588 error("Unexpected SASL state: expected failure after challenge"); 6818 error("Unexpected SASL state: expected failure after challenge");
6589 end 6819 end
6590 return false; 6820 return false;
6591 end 6821 end
6592 end,a.bearer_token and 6 or 4; 6822 end,t.bearer_token and 6 or 4;
6593 end 6823 end
6594 end 6824 end
6595 end) 6825 end)
6596 package.preload['verse.plugins.tls']=(function(...) 6826 package.preload['verse.plugins.tls']=(function(...)
6597 local _ENV=_ENV; 6827 local _ENV=_ENV;
6652 end 6882 end
6653 _ENV=e; 6883 _ENV=e;
6654 _M=e; 6884 _M=e;
6655 return e; 6885 return e;
6656 end 6886 end
6657 local i=require"verse"; 6887 local n=require"verse";
6658 local h,r=require"mime".b64,require"mime".unb64; 6888 local s,r=require"mime".b64,require"mime".unb64;
6659 local o="urn:ietf:params:xml:ns:xmpp-sasl"; 6889 local o="urn:ietf:params:xml:ns:xmpp-sasl";
6660 function i.plugins.sasl(e) 6890 function n.plugins.sasl(e)
6661 local function d(t) 6891 local function d(t)
6662 if e.authenticated then return;end 6892 if e.authenticated then return;end
6663 e:debug("Authenticating with SASL..."); 6893 e:debug("Authenticating with SASL...");
6664 local t=t:get_child("mechanisms",o); 6894 local t=t:get_child("mechanisms",o);
6665 if not t then return end 6895 if not t then return end
6666 local a={}; 6896 local a={};
6667 local n={}; 6897 local i={};
6668 local s={}; 6898 local h={};
6669 for t in t:childtags("mechanism")do 6899 for t in t:childtags("mechanism")do
6670 t=t:get_text(); 6900 t=t:get_text();
6671 e:debug("Server offers %s",t); 6901 e:debug("Server offers %s",t);
6672 s[t]=true; 6902 h[t]=true;
6673 if not a[t]then 6903 if not a[t]then
6674 local i=t:match("[^-]+"); 6904 local n=t:match("[^-]+");
6675 local s,o=pcall(require,"util.sasl."..i:lower()); 6905 local s,o=pcall(require,"util.sasl."..n:lower());
6676 if s then 6906 if s then
6677 e:debug("Loaded SASL %s module",i); 6907 e:debug("Loaded SASL %s module",n);
6678 a[t],n[t]=o(e,t); 6908 a[t],i[t]=o(e,t);
6679 elseif not tostring(o):match("not found")then 6909 elseif not tostring(o):match("not found")then
6680 e:debug("Loading failed: %s",tostring(o)); 6910 e:debug("Loading failed: %s",tostring(o));
6681 end 6911 end
6682 end 6912 end
6683 end 6913 end
6684 local t={}; 6914 local t={};
6685 for e in pairs(a)do 6915 for e in pairs(a)do
6686 table.insert(t,e); 6916 table.insert(t,e);
6687 end 6917 end
6688 if not t[1]then 6918 if not t[1]then
6689 e:event("authentication-failure",{condition="no-supported-sasl-mechanisms",mechanisms=s}); 6919 e:event("authentication-failure",{condition="no-supported-sasl-mechanisms",mechanisms=h});
6690 e:close(); 6920 e:close();
6691 return; 6921 return;
6692 end 6922 end
6693 table.sort(t,function(t,e)return n[t]>n[e];end); 6923 table.sort(t,function(t,e)return i[t]>i[e];end);
6694 local t,n=t[1]; 6924 local t,i=t[1];
6695 e:debug("Selecting %s mechanism...",t); 6925 e:debug("Selecting %s mechanism...",t);
6696 e.sasl_mechanism=coroutine.wrap(a[t]); 6926 e.sasl_mechanism=coroutine.wrap(a[t]);
6697 n=e:sasl_mechanism(t); 6927 i=e:sasl_mechanism(t);
6698 local t=i.stanza("auth",{xmlns=o,mechanism=t}); 6928 local t=n.stanza("auth",{xmlns=o,mechanism=t});
6699 if n then 6929 if i then
6700 t:text(h(n)); 6930 t:text(s(i));
6701 end 6931 end
6702 e:send(t); 6932 e:send(t);
6703 return true; 6933 return true;
6704 end 6934 end
6705 local function a(t) 6935 local function a(t)
6718 elseif t==true then 6948 elseif t==true then
6719 e:event("authentication-success"); 6949 e:event("authentication-success");
6720 e.authenticated=true 6950 e.authenticated=true
6721 e:reopen(); 6951 e:reopen();
6722 else 6952 else
6723 e:send(i.stanza("response",{xmlns=o}):text(h(t))); 6953 e:send(n.stanza("response",{xmlns=o}):text(s(t)));
6724 end 6954 end
6725 return true; 6955 return true;
6726 end 6956 end
6727 e:hook("stream-features",d,300); 6957 e:hook("stream-features",d,300);
6728 e:hook("stream/"..o,a); 6958 e:hook("stream/"..o,a);
6740 _ENV=e; 6970 _ENV=e;
6741 _M=e; 6971 _M=e;
6742 return e; 6972 return e;
6743 end 6973 end
6744 local t=require"verse"; 6974 local t=require"verse";
6745 local i=require"util.jid"; 6975 local o=require"util.jid";
6746 local a="urn:ietf:params:xml:ns:xmpp-bind"; 6976 local a="urn:ietf:params:xml:ns:xmpp-bind";
6747 function t.plugins.bind(e) 6977 function t.plugins.bind(e)
6748 local function o(o) 6978 local function i(i)
6749 if e.bound then return;end 6979 if e.bound then return;end
6750 e:debug("Binding resource..."); 6980 e:debug("Binding resource...");
6751 e:send_iq(t.iq({id="bind",type="set"}):tag("bind",{xmlns=a}):tag("resource"):text(e.resource), 6981 e:send_iq(t.iq({id="bind",type="set"}):tag("bind",{xmlns=a}):tag("resource"):text(e.resource),
6752 function(t) 6982 function(t)
6753 if t.attr.type=="result"then 6983 if t.attr.type=="result"then
6754 local t=t 6984 local t=t
6755 :get_child("bind",a) 6985 :get_child("bind",a)
6756 :get_child_text("jid"); 6986 :get_child_text("jid");
6757 e.username,e.host,e.resource=i.split(t); 6987 e.username,e.host,e.resource=o.split(t);
6758 e.jid,e.bound=t,true; 6988 e.jid,e.bound=t,true;
6759 e:event("bind-success",{jid=t}); 6989 e:event("bind-success",{jid=t});
6760 elseif t.attr.type=="error"then 6990 elseif t.attr.type=="error"then
6761 local a=t:child_with_name("error"); 6991 local a=t:child_with_name("error");
6762 local t,a,o=t:get_error(); 6992 local o,t,a=t:get_error();
6763 e:event("bind-failure",{error=a,text=o,type=t}); 6993 e:event("bind-failure",{error=t,text=a,type=o});
6764 end 6994 end
6765 end); 6995 end);
6766 end 6996 end
6767 e:hook("stream-features",o,200); 6997 e:hook("stream-features",i,200);
6768 return true; 6998 return true;
6769 end 6999 end
6770 end) 7000 end)
6771 package.preload['verse.plugins.smacks']=(function(...) 7001 package.preload['verse.plugins.smacks']=(function(...)
6772 local _ENV=_ENV; 7002 local _ENV=_ENV;
6779 _ENV=e; 7009 _ENV=e;
6780 _M=e; 7010 _M=e;
6781 return e; 7011 return e;
6782 end 7012 end
6783 local n=require"verse"; 7013 local n=require"verse";
6784 local r=require"socket".gettime; 7014 local h=require"socket".gettime;
6785 local s="urn:xmpp:sm:3"; 7015 local s="urn:xmpp:sm:3";
6786 function n.plugins.smacks(e) 7016 function n.plugins.smacks(e)
6787 local t=nil; 7017 local t=nil;
6788 local a=nil; 7018 local a=nil;
6789 local h=nil; 7019 local r=nil;
6790 local o; 7020 local i;
6791 local i=nil; 7021 local o=nil;
6792 local function m(t) 7022 local function l(t)
6793 if i and(t.attr.xmlns=="jabber:client"or not t.attr.xmlns)then 7023 if o and(t.attr.xmlns=="jabber:client"or not t.attr.xmlns)then
6794 i=i+1; 7024 o=o+1;
6795 e:debug("Increasing handled stanzas to %d for %s",i,t:top_tag()); 7025 e:debug("Increasing handled stanzas to %d for %s",o,t:top_tag());
6796 end 7026 end
6797 end 7027 end
6798 local function c(a) 7028 local function d(a)
6799 if t and(a.name and not a.attr.xmlns)then 7029 if t and(a.name and not a.attr.xmlns)then
6800 t[#t+1]=tostring(a); 7030 t[#t+1]=tostring(a);
6801 h=r(); 7031 r=h();
6802 if not o then 7032 if not i then
6803 o=true; 7033 i=true;
6804 e:debug("Waiting to send ack request..."); 7034 e:debug("Waiting to send ack request...");
6805 n.add_task(1,function() 7035 n.add_task(1,function()
6806 if#t==0 then 7036 if#t==0 then
6807 o=false; 7037 i=false;
6808 return; 7038 return;
6809 end 7039 end
6810 local a=r()-h; 7040 local a=h()-r;
6811 if a<1 and#t<10 then 7041 if a<1 and#t<10 then
6812 return 1-a; 7042 return 1-a;
6813 end 7043 end
6814 e:debug("Time up, sending <r>..."); 7044 e:debug("Time up, sending <r>...");
6815 o=false; 7045 i=false;
6816 e:send(n.stanza("r",{xmlns=s})); 7046 e:send(n.stanza("r",{xmlns=s}));
6817 end); 7047 end);
6818 end 7048 end
6819 end 7049 end
6820 end 7050 end
6828 e:connect(e.connect_host or e.host,e.connect_port or 5222); 7058 e:connect(e.connect_host or e.host,e.connect_port or 5222);
6829 end); 7059 end);
6830 return true; 7060 return true;
6831 end 7061 end
6832 end 7062 end
6833 local function d() 7063 local function c()
6834 e.resumption_token=nil; 7064 e.resumption_token=nil;
6835 end 7065 end
6836 local function l(o) 7066 local function m(i)
6837 if o.name=="r"then 7067 if i.name=="r"then
6838 e:debug("Ack requested... acking %d handled stanzas",i); 7068 e:debug("Ack requested... acking %d handled stanzas",o);
6839 e:send(n.stanza("a",{xmlns=s,h=tostring(i)})); 7069 e:send(n.stanza("a",{xmlns=s,h=tostring(o)}));
6840 elseif o.name=="a"then 7070 elseif i.name=="a"then
6841 local o=tonumber(o.attr.h); 7071 local o=tonumber(i.attr.h);
6842 if o>a then 7072 if o>a then
6843 local i=#t; 7073 local i=#t;
6844 for a=a+1,o do 7074 for a=a+1,o do
6845 table.remove(t,1); 7075 table.remove(t,1);
6846 end 7076 end
6847 e:debug("Received ack: New ack: "..o.." Last ack: "..a.." Unacked stanzas now: "..#t.." (was "..i..")"); 7077 e:debug("Received ack: New ack: "..o.." Last ack: "..a.." Unacked stanzas now: "..#t.." (was "..i..")");
6848 a=o; 7078 a=o;
6849 elseif o<a then 7079 elseif o<a then
6850 e:warn("Received bad ack for "..o.." when last ack was "..a); 7080 e:warn("Received bad ack for "..o.." when last ack was "..a);
6851 end 7081 end
6852 elseif o.name=="enabled"then 7082 elseif i.name=="enabled"then
6853 i=0; 7083 o=0;
6854 e.pre_smacks_features=nil; 7084 e.pre_smacks_features=nil;
6855 if o.attr.id then 7085 if i.attr.id then
6856 e.resumption_token=o.attr.id; 7086 e.resumption_token=i.attr.id;
6857 end 7087 end
6858 elseif o.name=="resumed"then 7088 elseif i.name=="resumed"then
6859 e.pre_smacks_features=nil; 7089 e.pre_smacks_features=nil;
6860 local o=tonumber(o.attr.h); 7090 local o=tonumber(i.attr.h);
6861 if o>a then 7091 if o>a then
6862 local i=#t; 7092 local i=#t;
6863 for a=a+1,o do 7093 for a=a+1,o do
6864 table.remove(t,1); 7094 table.remove(t,1);
6865 end 7095 end
6870 e:send(t[a]); 7100 e:send(t[a]);
6871 end 7101 end
6872 t={}; 7102 t={};
6873 e:debug("Resumed successfully"); 7103 e:debug("Resumed successfully");
6874 e:event("resumed"); 7104 e:event("resumed");
6875 elseif o.name=="failed"then 7105 elseif i.name=="failed"then
6876 e.bound=nil 7106 e.bound=nil
6877 e.smacks=nil 7107 e.smacks=nil
6878 a=nil 7108 a=nil
6879 i=nil 7109 o=nil
6880 t={}; 7110 t={};
6881 local t=e.pre_smacks_features; 7111 local t=e.pre_smacks_features;
6882 e.pre_smacks_features=nil; 7112 e.pre_smacks_features=nil;
6883 e:event("stream-features",t); 7113 e:event("stream-features",t);
6884 else 7114 else
6885 e:warn("Don't know how to handle "..s.."/"..o.name); 7115 e:warn("Don't know how to handle "..s.."/"..i.name);
6886 end 7116 end
6887 end 7117 end
6888 local function o() 7118 local function i()
6889 if e.stream_management_supported and not e.smacks then 7119 if e.stream_management_supported and not e.smacks then
6890 e:debug("smacks: sending enable"); 7120 e:debug("smacks: sending enable");
6891 t={}; 7121 t={};
6892 a=0; 7122 a=0;
6893 h=r(); 7123 r=h();
6894 e:send(n.stanza("enable",{xmlns=s,resume="true"})); 7124 e:send(n.stanza("enable",{xmlns=s,resume="true"}));
6895 e.smacks=true; 7125 e.smacks=true;
6896 end 7126 end
6897 end 7127 end
6898 local function a(t) 7128 local function a(t)
6899 if t:get_child("sm",s)then 7129 if t:get_child("sm",s)then
6900 e.pre_smacks_features=t; 7130 e.pre_smacks_features=t;
6901 e.stream_management_supported=true; 7131 e.stream_management_supported=true;
6902 if e.smacks and e.bound then 7132 if e.smacks and e.bound then
6903 e:debug("Resuming stream with %d handled stanzas",i); 7133 e:debug("Resuming stream with %d handled stanzas",o);
6904 e:send(n.stanza("resume",{xmlns=s, 7134 e:send(n.stanza("resume",{xmlns=s,
6905 h=tostring(i),previd=e.resumption_token})); 7135 h=tostring(o),previd=e.resumption_token}));
6906 return true; 7136 return true;
6907 else 7137 else
6908 end 7138 end
6909 end 7139 end
6910 end 7140 end
6911 e:hook("stream-features",a,250); 7141 e:hook("stream-features",a,250);
6912 e:hook("stream/"..s,l); 7142 e:hook("stream/"..s,m);
6913 e:hook("bind-success",o,1); 7143 e:hook("bind-success",i,1);
6914 e:hook("stanza",m); 7144 e:hook("stanza",l);
6915 e:hook("outgoing",c); 7145 e:hook("outgoing",d);
6916 e:hook("closed",d,100); 7146 e:hook("closed",c,100);
6917 e:hook("disconnected",u,100); 7147 e:hook("disconnected",u,100);
6918 end 7148 end
6919 end) 7149 end)
6920 package.preload['verse.plugins.keepalive']=(function(...) 7150 package.preload['verse.plugins.keepalive']=(function(...)
6921 local _ENV=_ENV; 7151 local _ENV=_ENV;
6951 return e; 7181 return e;
6952 end 7182 end
6953 local t=require"verse"; 7183 local t=require"verse";
6954 local e=require("mime").b64; 7184 local e=require("mime").b64;
6955 local e=require("util.hashes").sha1; 7185 local e=require("util.hashes").sha1;
6956 local s=require"util.caps".calculate_hash; 7186 local r=require"util.caps".calculate_hash;
6957 local h="http://jabber.org/protocol/caps"; 7187 local n="http://jabber.org/protocol/caps";
6958 local e="http://jabber.org/protocol/disco"; 7188 local e="http://jabber.org/protocol/disco";
6959 local o=e.."#info"; 7189 local o=e.."#info";
6960 local i=e.."#items"; 7190 local i=e.."#items";
6961 function t.plugins.disco(e) 7191 function t.plugins.disco(e)
6962 e:add_plugin("presence"); 7192 e:add_plugin("presence");
6963 local a={ 7193 local s={
6964 __index=function(t,e) 7194 __index=function(a,e)
6965 local a={identities={},features={}}; 7195 local t={identities={},features={}};
6966 if e=="identities"or e=="features"then 7196 if e=="identities"or e=="features"then
6967 return t[false][e] 7197 return a[false][e]
6968 end 7198 end
6969 t[e]=a; 7199 a[e]=t;
6970 return a; 7200 return t;
6971 end, 7201 end,
6972 }; 7202 };
6973 local n={ 7203 local a={
6974 __index=function(t,a) 7204 __index=function(t,a)
6975 local e={}; 7205 local e={};
6976 t[a]=e; 7206 t[a]=e;
6977 return e; 7207 return e;
6978 end, 7208 end,
6983 [false]={ 7213 [false]={
6984 identities={ 7214 identities={
6985 {category='client',type='pc',name='Verse'}, 7215 {category='client',type='pc',name='Verse'},
6986 }, 7216 },
6987 features={ 7217 features={
6988 [h]=true, 7218 [n]=true,
6989 [o]=true, 7219 [o]=true,
6990 [i]=true, 7220 [i]=true,
6991 }, 7221 },
6992 }, 7222 },
6993 },a); 7223 },s);
6994 items=setmetatable({[false]={}},n); 7224 items=setmetatable({[false]={}},a);
6995 }; 7225 };
6996 e.caps={} 7226 e.caps={}
6997 e.caps.node='http://code.matthewwild.co.uk/verse/' 7227 e.caps.node='http://code.matthewwild.co.uk/verse/'
6998 local function r(a) 7228 local function h(a)
6999 local i=e.disco.info[a or false]; 7229 local i=e.disco.info[a or false];
7000 if a and a==e.caps.node.."#"..e.caps.hash then 7230 if a and a==e.caps.node.."#"..e.caps.hash then
7001 i=e.disco.info[false]; 7231 i=e.disco.info[false];
7002 end 7232 end
7003 local n,i=i.identities,i.features 7233 local n,i=i.identities,i.features
7013 end 7243 end
7014 return e; 7244 return e;
7015 end 7245 end
7016 setmetatable(e.caps,{ 7246 setmetatable(e.caps,{
7017 __call=function(...) 7247 __call=function(...)
7018 local a=s(r()) 7248 local a=r(h())
7019 e.caps.hash=a; 7249 e.caps.hash=a;
7020 return t.stanza('c',{ 7250 return t.stanza('c',{
7021 xmlns=h, 7251 xmlns=n,
7022 hash='sha-1', 7252 hash='sha-1',
7023 node=e.caps.node, 7253 node=e.caps.node,
7024 ver=a 7254 ver=a
7025 }) 7255 })
7026 end 7256 end
7027 }) 7257 })
7028 function e:set_identity(e,t) 7258 function e:set_identity(a,t)
7029 self.disco.info[t or false].identities={e}; 7259 self.disco.info[t or false].identities={a};
7030 end 7260 e:event("disco-info-changed");
7031 function e:add_identity(t,e) 7261 end
7032 local e=self.disco.info[e or false].identities; 7262 function e:add_identity(a,t)
7033 e[#e+1]=t; 7263 local t=self.disco.info[t or false].identities;
7034 end 7264 t[#t+1]=a;
7035 function e:add_disco_feature(e,t) 7265 e:event("disco-info-changed");
7036 local e=e.var or e; 7266 end
7037 self.disco.info[t or false].features[e]=true; 7267 function e:add_disco_feature(t,a)
7038 end 7268 local t=t.var or t;
7039 function e:remove_disco_feature(e,t) 7269 self.disco.info[a or false].features[t]=true;
7040 local e=e.var or e; 7270 e:event("disco-info-changed");
7041 self.disco.info[t or false].features[e]=nil; 7271 end
7272 function e:remove_disco_feature(t,a)
7273 local t=t.var or t;
7274 self.disco.info[a or false].features[t]=nil;
7275 e:event("disco-info-changed");
7042 end 7276 end
7043 function e:add_disco_item(t,e) 7277 function e:add_disco_item(t,e)
7044 local e=self.disco.items[e or false]; 7278 local e=self.disco.items[e or false];
7045 e[#e+1]=t; 7279 e[#e+1]=t;
7046 end 7280 end
7050 if e[t]==a then 7284 if e[t]==a then
7051 table.remove(e,t); 7285 table.remove(e,t);
7052 end 7286 end
7053 end 7287 end
7054 end 7288 end
7055 function e:jid_has_identity(t,e,a) 7289 function e:jid_has_identity(e,a,t)
7056 local o=self.disco.cache[t]; 7290 local o=self.disco.cache[e];
7057 if not o then 7291 if not o then
7058 return nil,"no-cache"; 7292 return nil,"no-cache";
7059 end 7293 end
7060 local t=self.disco.cache[t].identities; 7294 local e=self.disco.cache[e].identities;
7061 if a then 7295 if t then
7062 return t[e.."/"..a]or false; 7296 return e[a.."/"..t]or false;
7063 end 7297 end
7064 for t in pairs(t)do 7298 for e in pairs(e)do
7065 if t:match("^(.*)/")==e then 7299 if e:match("^(.*)/")==a then
7066 return true; 7300 return true;
7067 end 7301 end
7068 end 7302 end
7069 end 7303 end
7070 function e:jid_supports(e,t) 7304 function e:jid_supports(e,t)
7072 if not e or not e.features then 7306 if not e or not e.features then
7073 return nil,"no-cache"; 7307 return nil,"no-cache";
7074 end 7308 end
7075 return e.features[t]or false; 7309 return e.features[t]or false;
7076 end 7310 end
7077 function e:get_local_services(o,a) 7311 function e:get_local_services(a,o)
7078 local e=self.disco.cache[self.host]; 7312 local e=self.disco.cache[self.host];
7079 if not(e)or not(e.items)then 7313 if not(e)or not(e.items)then
7080 return nil,"no-cache"; 7314 return nil,"no-cache";
7081 end 7315 end
7082 local t={}; 7316 local t={};
7083 for i,e in ipairs(e.items)do 7317 for i,e in ipairs(e.items)do
7084 if self:jid_has_identity(e.jid,o,a)then 7318 if self:jid_has_identity(e.jid,a,o)then
7085 table.insert(t,e.jid); 7319 table.insert(t,e.jid);
7086 end 7320 end
7087 end 7321 end
7088 return t; 7322 return t;
7089 end 7323 end
7115 :tag("query",{xmlns=o,node=a}); 7349 :tag("query",{xmlns=o,node=a});
7116 self:send_iq(t,function(t) 7350 self:send_iq(t,function(t)
7117 if t.attr.type=="error"then 7351 if t.attr.type=="error"then
7118 return h(nil,t:get_error()); 7352 return h(nil,t:get_error());
7119 end 7353 end
7120 local i,s,n={},{},{}; 7354 local n,s,i={},{},{};
7121 for e in t:get_child("query",o):childtags()do 7355 for e in t:get_child("query",o):childtags()do
7122 self:debug("DISCO {%s}%s",e.attr.xmlns or"",e.name);
7123 if e.name=="identity"then 7356 if e.name=="identity"then
7124 i[e.attr.category.."/"..e.attr.type]=e.attr.name or true; 7357 n[e.attr.category.."/"..e.attr.type]=e.attr.name or true;
7125 elseif e.name=="feature"then 7358 elseif e.name=="feature"then
7126 s[e.attr.var]=true; 7359 s[e.attr.var]=true;
7127 end 7360 end
7128 end 7361 end
7129 for e in t:get_child("query",o):childtags("x","jabber:x:data")do 7362 for t in t:get_child("query",o):childtags("x","jabber:x:data")do
7130 self:debug("DISCO {%s}%s",e.attr.xmlns or"",e.name); 7363 local e=t:get_child_with_attr("field",nil,"var","FORM_TYPE");
7131 local t=e:get_child_with_attr("field",nil,"var","FORM_TYPE"); 7364 local e=e and e:get_child_text("value");
7132 local a=t and t:get_child_text("value"); 7365 if e then
7133 self:debug("FORM: %s (%s)",a,t); 7366 i[e]=t;
7134 if a then
7135 n[a]=e;
7136 end 7367 end
7137 end 7368 end
7138 if not self.disco.cache[e]then 7369 if not self.disco.cache[e]then
7139 self.disco.cache[e]={nodes={}}; 7370 self.disco.cache[e]={nodes={}};
7140 end 7371 end
7141 if a then 7372 if a then
7142 if not self.disco.cache[e].nodes[a]then 7373 if not self.disco.cache[e].nodes[a]then
7143 self.disco.cache[e].nodes[a]={nodes={}}; 7374 self.disco.cache[e].nodes[a]={nodes={}};
7144 end 7375 end
7145 self.disco.cache[e].nodes[a].identities=i; 7376 self.disco.cache[e].nodes[a].identities=n;
7146 self.disco.cache[e].nodes[a].features=s; 7377 self.disco.cache[e].nodes[a].features=s;
7147 self.disco.cache[e].nodes[a].extended=n; 7378 self.disco.cache[e].nodes[a].extended=i;
7148 else 7379 else
7149 self.disco.cache[e].identities=i; 7380 self.disco.cache[e].identities=n;
7150 self.disco.cache[e].features=s; 7381 self.disco.cache[e].features=s;
7151 self.disco.cache[e].extended=n; 7382 self.disco.cache[e].extended=i;
7152 end 7383 end
7153 return h(self.disco.cache[e]); 7384 return h(self.disco.cache[e]);
7154 end); 7385 end);
7155 end 7386 end
7156 function e:disco_items(a,o,n) 7387 function e:disco_items(a,o,n)
7157 local t=t.iq({to=a,type="get"}) 7388 local t=t.iq({to=a,type="get"})
7158 :tag("query",{xmlns=i,node=o}); 7389 :tag("query",{xmlns=i,node=o});
7159 self:send_iq(t,function(t) 7390 self:send_iq(t,function(e)
7160 if t.attr.type=="error"then 7391 if e.attr.type=="error"then
7161 return n(nil,t:get_error()); 7392 return n(nil,e:get_error());
7162 end 7393 end
7163 local e={}; 7394 local t={};
7164 for t in t:get_child("query",i):childtags()do 7395 for e in e:get_child("query",i):childtags()do
7165 if t.name=="item"then 7396 if e.name=="item"then
7166 table.insert(e,{ 7397 table.insert(t,{
7167 name=t.attr.name; 7398 name=e.attr.name;
7168 jid=t.attr.jid; 7399 jid=e.attr.jid;
7169 node=t.attr.node; 7400 node=e.attr.node;
7170 }); 7401 });
7171 end 7402 end
7172 end 7403 end
7173 if not self.disco.cache[a]then 7404 if not self.disco.cache[a]then
7174 self.disco.cache[a]={nodes={}}; 7405 self.disco.cache[a]={nodes={}};
7175 end 7406 end
7176 if o then 7407 if o then
7177 if not self.disco.cache[a].nodes[o]then 7408 if not self.disco.cache[a].nodes[o]then
7178 self.disco.cache[a].nodes[o]={nodes={}}; 7409 self.disco.cache[a].nodes[o]={nodes={}};
7179 end 7410 end
7180 self.disco.cache[a].nodes[o].items=e; 7411 self.disco.cache[a].nodes[o].items=t;
7181 else 7412 else
7182 self.disco.cache[a].items=e; 7413 self.disco.cache[a].items=t;
7183 end 7414 end
7184 return n(e); 7415 return n(t);
7185 end); 7416 end);
7186 end 7417 end
7187 e:hook("iq/"..o,function(a) 7418 e:hook("iq/"..o,function(a)
7188 local o=a.tags[1]; 7419 local o=a.tags[1];
7189 if a.attr.type=='get'and o.name=="query"then 7420 if a.attr.type=='get'and o.name=="query"then
7190 local o=r(o.attr.node); 7421 local o=h(o.attr.node);
7191 local t=t.reply(a):add_child(o); 7422 local t=t.reply(a):add_child(o);
7192 e:send(t); 7423 e:send(t);
7193 return true 7424 return true
7194 end 7425 end
7195 end); 7426 end);
7196 e:hook("iq/"..i,function(a) 7427 e:hook("iq/"..i,function(o)
7197 local o=a.tags[1]; 7428 local a=o.tags[1];
7198 if a.attr.type=='get'and o.name=="query"then 7429 if o.attr.type=='get'and a.name=="query"then
7199 local n=e.disco.items[o.attr.node or false]; 7430 local n=e.disco.items[a.attr.node or false];
7200 local t=t.reply(a):tag('query',{ 7431 local t=t.reply(o):tag('query',{
7201 xmlns=i, 7432 xmlns=i,
7202 node=o.attr.node 7433 node=a.attr.node
7203 }) 7434 })
7204 for a=1,#n do 7435 for a=1,#n do
7205 t:tag('item',n[a]):up() 7436 t:tag('item',n[a]):up()
7206 end 7437 end
7207 e:send(t); 7438 e:send(t);
7210 end); 7441 end);
7211 local t; 7442 local t;
7212 e:hook("ready",function() 7443 e:hook("ready",function()
7213 if t then return;end 7444 if t then return;end
7214 t=true; 7445 t=true;
7215 local function o(t) 7446 local function a(a)
7216 local a=e.disco.cache[t]; 7447 local t=e.disco.cache[a];
7217 if a then 7448 if t then
7218 for a in pairs(a.identities)do 7449 for t in pairs(t.identities)do
7219 local o,a=a:match("^(.*)/(.*)$"); 7450 local t,o=t:match("^(.*)/(.*)$");
7220 print(t,o,a) 7451 e:event("disco/service-discovered/"..t,{
7221 e:event("disco/service-discovered/"..o,{ 7452 type=o,jid=a;
7222 type=a,jid=t;
7223 }); 7453 });
7224 end 7454 end
7225 end 7455 end
7226 end 7456 end
7227 e:disco_info(e.host,nil,function() 7457 e:disco_info(e.host,nil,function()
7228 o(e.host); 7458 a(e.host);
7229 end); 7459 end);
7230 e:disco_local_services(function(t) 7460 e:disco_local_services(function(t)
7231 for a,t in ipairs(t)do 7461 for o,t in ipairs(t)do
7232 o(t.jid); 7462 a(t.jid);
7233 end 7463 end
7234 e:event("ready"); 7464 e:event("ready");
7235 end); 7465 end);
7236 return true; 7466 return true;
7237 end,50); 7467 end,50);
7238 e:hook("presence-out",function(t) 7468 e:hook("presence-out",function(t)
7239 t:remove_children("c",h); 7469 t:remove_children("c",n);
7240 t:reset():add_child(e:caps()):reset(); 7470 t:reset():add_child(e:caps()):reset();
7241 end,10); 7471 end,10);
7472 e:hook("disco-info-changed",function()
7473 e:resend_presence();
7474 end);
7242 end 7475 end
7243 end) 7476 end)
7244 package.preload['verse.plugins.version']=(function(...) 7477 package.preload['verse.plugins.version']=(function(...)
7245 local _ENV=_ENV; 7478 local _ENV=_ENV;
7246 local function e(t,...) 7479 local function e(t,...)
7252 _ENV=e; 7485 _ENV=e;
7253 _M=e; 7486 _M=e;
7254 return e; 7487 return e;
7255 end 7488 end
7256 local o=require"verse"; 7489 local o=require"verse";
7257 local t="jabber:iq:version"; 7490 local a="jabber:iq:version";
7258 local function a(t,e) 7491 local function i(t,e)
7259 t.name=e.name; 7492 t.name=e.name;
7260 t.version=e.version; 7493 t.version=e.version;
7261 t.platform=e.platform; 7494 t.platform=e.platform;
7262 end 7495 end
7263 function o.plugins.version(e) 7496 function o.plugins.version(e)
7264 e.version={set=a}; 7497 e.version={set=i};
7265 e:hook("iq/"..t,function(a) 7498 e:hook("iq/"..a,function(t)
7266 if a.attr.type~="get"then return;end 7499 if t.attr.type~="get"then return;end
7267 local t=o.reply(a) 7500 local t=o.reply(t)
7268 :tag("query",{xmlns=t}); 7501 :tag("query",{xmlns=a});
7269 if e.version.name then 7502 if e.version.name then
7270 t:tag("name"):text(tostring(e.version.name)):up(); 7503 t:tag("name"):text(tostring(e.version.name)):up();
7271 end 7504 end
7272 if e.version.version then 7505 if e.version.version then
7273 t:tag("version"):text(tostring(e.version.version)):up() 7506 t:tag("version"):text(tostring(e.version.version)):up()
7276 t:tag("os"):text(e.version.platform); 7509 t:tag("os"):text(e.version.platform);
7277 end 7510 end
7278 e:send(t); 7511 e:send(t);
7279 return true; 7512 return true;
7280 end); 7513 end);
7281 function e:query_version(i,a) 7514 function e:query_version(i,t)
7282 a=a or function(e)return self:event("version/response",e);end 7515 t=t or function(e)return self:event("version/response",e);end
7283 self:send_iq(o.iq({type="get",to=i}) 7516 self:send_iq(o.iq({type="get",to=i})
7284 :tag("query",{xmlns=t}), 7517 :tag("query",{xmlns=a}),
7285 function(o) 7518 function(o)
7286 if o.attr.type=="result"then 7519 if o.attr.type=="result"then
7287 local e=o:get_child("query",t); 7520 local e=o:get_child("query",a);
7288 local t=e and e:get_child_text("name"); 7521 local a=e and e:get_child_text("name");
7289 local o=e and e:get_child_text("version"); 7522 local o=e and e:get_child_text("version");
7290 local e=e and e:get_child_text("os"); 7523 local e=e and e:get_child_text("os");
7291 a({ 7524 t({
7292 name=t; 7525 name=a;
7293 version=o; 7526 version=o;
7294 platform=e; 7527 platform=e;
7295 }); 7528 });
7296 else 7529 else
7297 local o,e,t=o:get_error(); 7530 local e,a,o=o:get_error();
7298 a({ 7531 t({
7299 error=true; 7532 error=true;
7300 condition=e; 7533 condition=a;
7301 text=t; 7534 text=o;
7302 type=o; 7535 type=e;
7303 }); 7536 });
7304 end 7537 end
7305 end); 7538 end);
7306 end 7539 end
7307 return true; 7540 return true;
7317 end 7550 end
7318 _ENV=e; 7551 _ENV=e;
7319 _M=e; 7552 _M=e;
7320 return e; 7553 return e;
7321 end 7554 end
7322 local a=require"verse"; 7555 local t=require"verse";
7323 local s=require"socket".gettime; 7556 local o=require"socket".gettime;
7324 local o=require"util.id".short; 7557 local s=require"util.id".short;
7325 local i="urn:xmpp:ping"; 7558 local n="urn:xmpp:ping";
7326 function a.plugins.ping(e) 7559 function t.plugins.ping(e)
7327 function e:ping(t,n) 7560 function e:ping(a,i)
7328 local h=s(); 7561 local h=o();
7329 local o=o(); 7562 local s=s();
7330 local a=a.iq{id=o,to=t,type="get"}:tag("ping",{xmlns=i}); 7563 local t=t.iq{id=s,to=a,type="get"}:tag("ping",{xmlns=n});
7331 e:send_iq(a, 7564 e:send_iq(t,
7332 function(e) 7565 function(e)
7333 if e.attr.type=="error"then 7566 if e.attr.type=="error"then
7334 local o,e,a=e:get_error(); 7567 local o,e,t=e:get_error();
7335 if e~="service-unavailable"and e~="feature-not-implemented"then 7568 if e~="service-unavailable"and e~="feature-not-implemented"then
7336 n(nil,t,{type=o,condition=e,text=a}); 7569 i(nil,a,{type=o,condition=e,text=t});
7337 return; 7570 return;
7338 end 7571 end
7339 end 7572 end
7340 n(s()-h,t); 7573 i(o()-h,a);
7341 end); 7574 end);
7342 return o; 7575 return s;
7343 end 7576 end
7344 e:hook("iq/"..i,function(t) 7577 e:hook("iq/"..n,function(a)
7345 return e:send(a.reply(t)); 7578 return e:send(t.reply(a));
7346 end); 7579 end);
7347 return true; 7580 return true;
7348 end 7581 end
7349 end) 7582 end)
7350 package.preload['verse.plugins.uptime']=(function(...) 7583 package.preload['verse.plugins.uptime']=(function(...)
7383 local e=tonumber(t.attr.seconds); 7616 local e=tonumber(t.attr.seconds);
7384 a({ 7617 a({
7385 seconds=e or nil; 7618 seconds=e or nil;
7386 }); 7619 });
7387 else 7620 else
7388 local o,e,t=e:get_error(); 7621 local o,t,e=e:get_error();
7389 a({ 7622 a({
7390 error=true; 7623 error=true;
7391 condition=e; 7624 condition=t;
7392 text=t; 7625 text=e;
7393 type=o; 7626 type=o;
7394 }); 7627 });
7395 end 7628 end
7396 end); 7629 end);
7397 end 7630 end
7428 if e.utc then 7661 if e.utc then
7429 e.timestamp=i.parse(e.utc); 7662 e.timestamp=i.parse(e.utc);
7430 end 7663 end
7431 t(e); 7664 t(e);
7432 else 7665 else
7433 local o,a,e=a:get_error(); 7666 local e,a,o=a:get_error();
7434 t({ 7667 t({
7435 error=true; 7668 error=true;
7436 condition=a; 7669 condition=a;
7437 text=e; 7670 text=o;
7438 type=o; 7671 type=e;
7439 }); 7672 });
7440 end 7673 end
7441 end); 7674 end);
7442 end 7675 end
7443 return true; 7676 return true;
7453 end 7686 end
7454 _ENV=e; 7687 _ENV=e;
7455 _M=e; 7688 _M=e;
7456 return e; 7689 return e;
7457 end 7690 end
7458 local a=require"verse"; 7691 local o=require"verse";
7459 local o="urn:xmpp:blocking"; 7692 local a="urn:xmpp:blocking";
7460 function a.plugins.blocking(e) 7693 function o.plugins.blocking(e)
7461 e.blocking={}; 7694 e.blocking={};
7462 function e.blocking:block_jid(i,t) 7695 function e.blocking:block_jid(i,t)
7463 e:send_iq(a.iq{type="set"} 7696 e:send_iq(o.iq{type="set"}
7464 :tag("block",{xmlns=o}) 7697 :tag("block",{xmlns=a})
7465 :tag("item",{jid=i}) 7698 :tag("item",{jid=i})
7466 ,function()return t and t(true);end 7699 ,function()return t and t(true);end
7467 ,function()return t and t(false);end 7700 ,function()return t and t(false);end
7468 ); 7701 );
7469 end 7702 end
7470 function e.blocking:unblock_jid(i,t) 7703 function e.blocking:unblock_jid(i,t)
7471 e:send_iq(a.iq{type="set"} 7704 e:send_iq(o.iq{type="set"}
7472 :tag("unblock",{xmlns=o}) 7705 :tag("unblock",{xmlns=a})
7473 :tag("item",{jid=i}) 7706 :tag("item",{jid=i})
7474 ,function()return t and t(true);end 7707 ,function()return t and t(true);end
7475 ,function()return t and t(false);end 7708 ,function()return t and t(false);end
7476 ); 7709 );
7477 end 7710 end
7478 function e.blocking:unblock_all_jids(t) 7711 function e.blocking:unblock_all_jids(t)
7479 e:send_iq(a.iq{type="set"} 7712 e:send_iq(o.iq{type="set"}
7480 :tag("unblock",{xmlns=o}) 7713 :tag("unblock",{xmlns=a})
7481 ,function()return t and t(true);end 7714 ,function()return t and t(true);end
7482 ,function()return t and t(false);end 7715 ,function()return t and t(false);end
7483 ); 7716 );
7484 end 7717 end
7485 function e.blocking:get_blocked_jids(t) 7718 function e.blocking:get_blocked_jids(t)
7486 e:send_iq(a.iq{type="get"} 7719 e:send_iq(o.iq{type="get"}
7487 :tag("blocklist",{xmlns=o}) 7720 :tag("blocklist",{xmlns=a})
7488 ,function(e) 7721 ,function(e)
7489 local a=e:get_child("blocklist",o); 7722 local a=e:get_child("blocklist",a);
7490 if not a then return t and t(false);end 7723 if not a then return t and t(false);end
7491 local e={}; 7724 local e={};
7492 for t in a:childtags()do 7725 for t in a:childtags()do
7493 e[#e+1]=t.attr.jid; 7726 e[#e+1]=t.attr.jid;
7494 end 7727 end
7511 _M=e; 7744 _M=e;
7512 return e; 7745 return e;
7513 end 7746 end
7514 local a=require"verse"; 7747 local a=require"verse";
7515 local e=require"util.timer"; 7748 local e=require"util.timer";
7516 local n=require"util.id".short; 7749 local o=require"util.id".short;
7517 local i="urn:xmpp:jingle:1"; 7750 local i="urn:xmpp:jingle:1";
7518 local r="urn:xmpp:jingle:errors:1"; 7751 local h="urn:xmpp:jingle:errors:1";
7519 local t={}; 7752 local t={};
7520 t.__index=t; 7753 t.__index=t;
7521 local e={}; 7754 local e={};
7522 local e={}; 7755 local e={};
7523 function a.plugins.jingle(e) 7756 function a.plugins.jingle(e)
7524 e:hook("ready",function() 7757 e:hook("ready",function()
7525 e:add_disco_feature(i); 7758 e:add_disco_feature(i);
7526 end,10); 7759 end,10);
7527 function e:jingle(o) 7760 function e:jingle(i)
7528 return a.eventable(setmetatable(base or{ 7761 return a.eventable(setmetatable(base or{
7529 role="initiator"; 7762 role="initiator";
7530 peer=o; 7763 peer=i;
7531 sid=n(); 7764 sid=o();
7532 stream=e; 7765 stream=e;
7533 },t)); 7766 },t));
7534 end 7767 end
7535 function e:register_jingle_transport(e) 7768 function e:register_jingle_transport(e)
7536 end 7769 end
7537 function e:register_jingle_content_type(e) 7770 function e:register_jingle_content_type(e)
7538 end 7771 end
7539 local function u(n) 7772 local function u(n)
7540 local s=n:get_child("jingle",i); 7773 local s=n:get_child("jingle",i);
7541 local h=s.attr.sid; 7774 local o=s.attr.sid;
7542 local o=s.attr.action; 7775 local r=s.attr.action;
7543 local h=e:event("jingle/"..h,n); 7776 local o=e:event("jingle/"..o,n);
7544 if h==true then 7777 if o==true then
7545 e:send(a.reply(n)); 7778 e:send(a.reply(n));
7546 return true; 7779 return true;
7547 end 7780 end
7548 if o~="session-initiate"then 7781 if r~="session-initiate"then
7549 local t=a.error_reply(n,"cancel","item-not-found") 7782 local t=a.error_reply(n,"cancel","item-not-found")
7550 :tag("unknown-session",{xmlns=r}):up(); 7783 :tag("unknown-session",{xmlns=h}):up();
7551 e:send(t); 7784 e:send(t);
7552 return; 7785 return;
7553 end 7786 end
7554 local l=s.attr.sid; 7787 local l=s.attr.sid;
7555 local o=a.eventable{ 7788 local o=a.eventable{
7689 return self:handle_command(e) 7922 return self:handle_command(e)
7690 end); 7923 end);
7691 self.stream:send_iq(e,function(e) 7924 self.stream:send_iq(e,function(e)
7692 if e.attr.type=="error"then 7925 if e.attr.type=="error"then
7693 self.state="terminated"; 7926 self.state="terminated";
7694 local e,t,a=e:get_error(); 7927 local t,a,e=e:get_error();
7695 return self:event("error",{type=e,condition=t,text=a}); 7928 return self:event("error",{type=t,condition=a,text=e});
7696 end 7929 end
7697 end); 7930 end);
7698 self.state="pending"; 7931 self.state="pending";
7699 end 7932 end
7700 function t:terminate(e) 7933 function t:terminate(e)
7756 end 7989 end
7757 _ENV=e; 7990 _ENV=e;
7758 _M=e; 7991 _M=e;
7759 return e; 7992 return e;
7760 end 7993 end
7761 local i=require"verse"; 7994 local o=require"verse";
7762 local n=require"ltn12"; 7995 local a=require"ltn12";
7763 local s=package.config:sub(1,1); 7996 local n=package.config:sub(1,1);
7764 local a="urn:xmpp:jingle:apps:file-transfer:4"; 7997 local t="urn:xmpp:jingle:apps:file-transfer:4";
7765 function i.plugins.jingle_ft(t) 7998 function o.plugins.jingle_ft(e)
7766 t:hook("ready",function() 7999 e:hook("ready",function()
7767 t:add_disco_feature(a); 8000 e:add_disco_feature(t);
7768 end,10); 8001 end,10);
7769 local o={type="file"}; 8002 local i={type="file"};
7770 function o:generate_accept(t,e) 8003 function i:generate_accept(t,e)
7771 if e and e.save_file then 8004 if e and e.save_file then
7772 self.jingle:hook("connected",function() 8005 self.jingle:hook("connected",function()
7773 local e=n.sink.file(io.open(e.save_file,"w+")); 8006 local e=a.sink.file(io.open(e.save_file,"w+"));
7774 self.jingle:set_sink(e); 8007 self.jingle:set_sink(e);
7775 end); 8008 end);
7776 end 8009 end
7777 return t; 8010 return t;
7778 end 8011 end
7779 local o={__index=o}; 8012 local i={__index=i};
7780 t:hook("jingle/content/"..a,function(t,e) 8013 e:hook("jingle/content/"..t,function(t,e)
7781 local e=e:get_child("file"); 8014 local e=e:get_child("file");
7782 local e={ 8015 local e={
7783 name=e:get_child_text("name"); 8016 name=e:get_child_text("name");
7784 size=tonumber(e:get_child_text("size")); 8017 size=tonumber(e:get_child_text("size"));
7785 desc=e:get_child_text("desc"); 8018 desc=e:get_child_text("desc");
7786 date=e:get_child_text("date"); 8019 date=e:get_child_text("date");
7787 }; 8020 };
7788 return setmetatable({jingle=t,file=e},o); 8021 return setmetatable({jingle=t,file=e},i);
7789 end); 8022 end);
7790 t:hook("jingle/describe/file",function(e) 8023 e:hook("jingle/describe/file",function(e)
7791 local t; 8024 local a;
7792 if e.timestamp then 8025 if e.timestamp then
7793 t=os.date("!%Y-%m-%dT%H:%M:%SZ",e.timestamp); 8026 a=os.date("!%Y-%m-%dT%H:%M:%SZ",e.timestamp);
7794 end 8027 end
7795 return i.stanza("description",{xmlns=a}) 8028 return o.stanza("description",{xmlns=t})
7796 :tag("file") 8029 :tag("file")
7797 :tag("name"):text(e.filename):up() 8030 :tag("name"):text(e.filename):up()
7798 :tag("size"):text(tostring(e.size)):up() 8031 :tag("size"):text(tostring(e.size)):up()
7799 :tag("date"):text(t):up() 8032 :tag("date"):text(a):up()
7800 :tag("desc"):text(e.description):up() 8033 :tag("desc"):text(e.description):up()
7801 :up(); 8034 :up();
7802 end); 8035 end);
7803 function t:send_file(i,t) 8036 function e:send_file(i,t)
7804 local e,a=io.open(t); 8037 local e,o=io.open(t);
7805 if not e then return e,a;end 8038 if not e then return e,o;end
7806 local o=e:seek("end",0); 8039 local o=e:seek("end",0);
7807 e:seek("set",0); 8040 e:seek("set",0);
7808 local a=n.source.file(e); 8041 local a=a.source.file(e);
7809 local e=self:jingle(i); 8042 local e=self:jingle(i);
7810 e:offer("file",{ 8043 e:offer("file",{
7811 filename=t:match("[^"..s.."]+$"); 8044 filename=t:match("[^"..n.."]+$");
7812 size=o; 8045 size=o;
7813 }); 8046 });
7814 e:hook("connected",function() 8047 e:hook("connected",function()
7815 e:set_source(a,true); 8048 e:set_source(a,true);
7816 end); 8049 end);
7828 end 8061 end
7829 _ENV=e; 8062 _ENV=e;
7830 _M=e; 8063 _M=e;
7831 return e; 8064 return e;
7832 end 8065 end
7833 local a=require"verse"; 8066 local t=require"verse";
7834 local o="urn:xmpp:jingle:transports:s5b:1"; 8067 local o="urn:xmpp:jingle:transports:s5b:1";
7835 local d="http://jabber.org/protocol/bytestreams"; 8068 local l="http://jabber.org/protocol/bytestreams";
7836 local h=require"util.hashes".sha1; 8069 local h=require"util.hashes".sha1;
7837 local l=require"util.id".short; 8070 local d=require"util.id".short;
7838 local function r(e,s) 8071 local function r(e,s)
7839 local function n() 8072 local function n()
7840 e:unhook("connected",n); 8073 e:unhook("connected",n);
7841 return true; 8074 return true;
7842 end 8075 end
7843 local function i(t) 8076 local function o(t)
7844 e:unhook("incoming-raw",i); 8077 e:unhook("incoming-raw",o);
7845 if t:sub(1,2)~="\005\000"then 8078 if t:sub(1,2)~="\005\000"then
7846 return e:event("error","connection-failure"); 8079 return e:event("error","connection-failure");
7847 end 8080 end
7848 e:event("connected"); 8081 e:event("connected");
7849 return true; 8082 return true;
7850 end 8083 end
7851 local function o(t) 8084 local function i(a)
7852 e:unhook("incoming-raw",o); 8085 e:unhook("incoming-raw",i);
7853 if t~="\005\000"then 8086 if a~="\005\000"then
7854 local a="version-mismatch"; 8087 local t="version-mismatch";
7855 if t:sub(1,1)=="\005"then 8088 if a:sub(1,1)=="\005"then
7856 a="authentication-failure"; 8089 t="authentication-failure";
7857 end 8090 end
7858 return e:event("error",a); 8091 return e:event("error",t);
7859 end 8092 end
7860 e:send(string.char(5,1,0,3,#s)..s.."\0\0"); 8093 e:send(string.char(5,1,0,3,#s)..s.."\0\0");
8094 e:hook("incoming-raw",o,100);
8095 return true;
8096 end
8097 e:hook("connected",n,200);
7861 e:hook("incoming-raw",i,100); 8098 e:hook("incoming-raw",i,100);
7862 return true;
7863 end
7864 e:hook("connected",n,200);
7865 e:hook("incoming-raw",o,100);
7866 e:send("\005\001\000"); 8099 e:send("\005\001\000");
7867 end 8100 end
7868 local function s(o,e,i) 8101 local function n(a,e,i)
7869 local e=a.new(nil,{ 8102 local e=t.new(nil,{
7870 streamhosts=e, 8103 streamhosts=e,
7871 current_host=0; 8104 current_host=0;
7872 }); 8105 });
7873 local function t(a) 8106 local function t(o)
7874 if a then 8107 if o then
7875 return o(nil,a.reason); 8108 return a(nil,o.reason);
7876 end 8109 end
7877 if e.current_host<#e.streamhosts then 8110 if e.current_host<#e.streamhosts then
7878 e.current_host=e.current_host+1; 8111 e.current_host=e.current_host+1;
7879 e:debug("Attempting to connect to "..e.streamhosts[e.current_host].host..":"..e.streamhosts[e.current_host].port.."..."); 8112 e:debug("Attempting to connect to "..e.streamhosts[e.current_host].host..":"..e.streamhosts[e.current_host].port.."...");
7880 local a,t=e:connect( 8113 local a,t=e:connect(
7892 end 8125 end
7893 r(e,i); 8126 r(e,i);
7894 return true; 8127 return true;
7895 end 8128 end
7896 e:unhook("disconnected",t); 8129 e:unhook("disconnected",t);
7897 return o(nil); 8130 return a(nil);
7898 end 8131 end
7899 e:hook("disconnected",t,100); 8132 e:hook("disconnected",t,100);
7900 e:hook("connected",function() 8133 e:hook("connected",function()
7901 e:unhook("disconnected",t); 8134 e:unhook("disconnected",t);
7902 o(e.streamhosts[e.current_host],e); 8135 a(e.streamhosts[e.current_host],e);
7903 end,100); 8136 end,100);
7904 t(); 8137 t();
7905 return e; 8138 return e;
7906 end 8139 end
7907 function a.plugins.jingle_s5b(e) 8140 function t.plugins.jingle_s5b(e)
7908 e:hook("ready",function() 8141 e:hook("ready",function()
7909 e:add_disco_feature(o); 8142 e:add_disco_feature(o);
7910 end,10); 8143 end,10);
7911 local t={}; 8144 local a={};
7912 function t:generate_initiate() 8145 function a:generate_initiate()
7913 self.s5b_sid=l(); 8146 self.s5b_sid=d();
7914 local i=a.stanza("transport",{xmlns=o, 8147 local a=t.stanza("transport",{xmlns=o,
7915 mode="tcp",sid=self.s5b_sid}); 8148 mode="tcp",sid=self.s5b_sid});
7916 local t=0; 8149 local t=0;
7917 for a,o in pairs(e.proxy65.available_streamhosts)do 8150 for o,i in pairs(e.proxy65.available_streamhosts)do
7918 t=t+1; 8151 t=t+1;
7919 i:tag("candidate",{jid=a,host=o.host, 8152 a:tag("candidate",{jid=o,host=i.host,
7920 port=o.port,cid=a,priority=t,type="proxy"}):up(); 8153 port=i.port,cid=o,priority=t,type="proxy"}):up();
7921 end 8154 end
7922 e:debug("Have %d proxies",t) 8155 e:debug("Have %d proxies",t)
7923 return i; 8156 return a;
7924 end 8157 end
7925 function t:generate_accept(e) 8158 function a:generate_accept(e)
7926 local t={}; 8159 local a={};
7927 self.s5b_peer_candidates=t; 8160 self.s5b_peer_candidates=a;
7928 self.s5b_mode=e.attr.mode or"tcp"; 8161 self.s5b_mode=e.attr.mode or"tcp";
7929 self.s5b_sid=e.attr.sid or self.jingle.sid; 8162 self.s5b_sid=e.attr.sid or self.jingle.sid;
7930 for e in e:childtags()do 8163 for e in e:childtags()do
7931 t[e.attr.cid]={ 8164 a[e.attr.cid]={
7932 type=e.attr.type; 8165 type=e.attr.type;
7933 jid=e.attr.jid; 8166 jid=e.attr.jid;
7934 host=e.attr.host; 8167 host=e.attr.host;
7935 port=tonumber(e.attr.port)or 0; 8168 port=tonumber(e.attr.port)or 0;
7936 priority=tonumber(e.attr.priority)or 0; 8169 priority=tonumber(e.attr.priority)or 0;
7937 cid=e.attr.cid; 8170 cid=e.attr.cid;
7938 }; 8171 };
7939 end 8172 end
7940 local e=a.stanza("transport",{xmlns=o}); 8173 local e=t.stanza("transport",{xmlns=o});
7941 return e; 8174 return e;
7942 end 8175 end
7943 function t:connect(i) 8176 function a:connect(i)
7944 e:warn("Connecting!"); 8177 e:warn("Connecting!");
7945 local t={}; 8178 local a={};
7946 for a,e in pairs(self.s5b_peer_candidates or{})do 8179 for t,e in pairs(self.s5b_peer_candidates or{})do
7947 t[#t+1]=e; 8180 a[#a+1]=e;
7948 end 8181 end
7949 if#t>0 then 8182 if#a>0 then
7950 self.connecting_peer_candidates=true; 8183 self.connecting_peer_candidates=true;
7951 local function n(e,t) 8184 local function s(a,e)
7952 self.jingle:send_command("transport-info",a.stanza("content",{creator=self.creator,name=self.name}) 8185 self.jingle:send_command("transport-info",t.stanza("content",{creator=self.creator,name=self.name})
7953 :tag("transport",{xmlns=o,sid=self.s5b_sid}) 8186 :tag("transport",{xmlns=o,sid=self.s5b_sid})
7954 :tag("candidate-used",{cid=e.cid})); 8187 :tag("candidate-used",{cid=a.cid}));
7955 self.onconnect_callback=i; 8188 self.onconnect_callback=i;
7956 self.conn=t; 8189 self.conn=e;
7957 end 8190 end
7958 local e=h(self.s5b_sid..self.peer..e.jid,true); 8191 local e=h(self.s5b_sid..self.peer..e.jid,true);
7959 s(n,t,e); 8192 n(s,a,e);
7960 else 8193 else
7961 e:warn("Actually, I'm going to wait for my peer to tell me its streamhost..."); 8194 e:warn("Actually, I'm going to wait for my peer to tell me its streamhost...");
7962 self.onconnect_callback=i; 8195 self.onconnect_callback=i;
7963 end 8196 end
7964 end 8197 end
7965 function t:info_received(t) 8198 function a:info_received(a)
7966 e:warn("Info received"); 8199 e:warn("Info received");
7967 local n=t:child_with_name("content"); 8200 local s=a:child_with_name("content");
7968 local i=n:child_with_name("transport"); 8201 local i=s:child_with_name("transport");
7969 if i:get_child("candidate-used")and not self.connecting_peer_candidates then 8202 if i:get_child("candidate-used")and not self.connecting_peer_candidates then
7970 local t=i:child_with_name("candidate-used"); 8203 local a=i:child_with_name("candidate-used");
7971 if t then 8204 if a then
7972 local function r(i,e) 8205 local function i(i,e)
7973 if self.jingle.role=="initiator"then 8206 if self.jingle.role=="initiator"then
7974 self.jingle.stream:send_iq(a.iq({to=i.jid,type="set"}) 8207 self.jingle.stream:send_iq(t.iq({to=i.jid,type="set"})
7975 :tag("query",{xmlns=d,sid=self.s5b_sid}) 8208 :tag("query",{xmlns=l,sid=self.s5b_sid})
7976 :tag("activate"):text(self.jingle.peer),function(i) 8209 :tag("activate"):text(self.jingle.peer),function(i)
7977 if i.attr.type=="result"then 8210 if i.attr.type=="result"then
7978 self.jingle:send_command("transport-info",a.stanza("content",n.attr) 8211 self.jingle:send_command("transport-info",t.stanza("content",s.attr)
7979 :tag("transport",{xmlns=o,sid=self.s5b_sid}) 8212 :tag("transport",{xmlns=o,sid=self.s5b_sid})
7980 :tag("activated",{cid=t.attr.cid})); 8213 :tag("activated",{cid=a.attr.cid}));
7981 self.conn=e; 8214 self.conn=e;
7982 self.onconnect_callback(e); 8215 self.onconnect_callback(e);
7983 else 8216 else
7984 self.jingle.stream:error("Failed to activate bytestream"); 8217 self.jingle.stream:error("Failed to activate bytestream");
7985 end 8218 end
7986 end); 8219 end);
7987 end 8220 end
7988 end 8221 end
7989 self.jingle.stream:debug("CID: %s",self.jingle.stream.proxy65.available_streamhosts[t.attr.cid]); 8222 self.jingle.stream:debug("CID: %s",self.jingle.stream.proxy65.available_streamhosts[a.attr.cid]);
7990 local t={ 8223 local t={
7991 self.jingle.stream.proxy65.available_streamhosts[t.attr.cid]; 8224 self.jingle.stream.proxy65.available_streamhosts[a.attr.cid];
7992 }; 8225 };
7993 local e=h(self.s5b_sid..e.jid..self.peer,true); 8226 local e=h(self.s5b_sid..e.jid..self.peer,true);
7994 s(r,t,e); 8227 n(i,t,e);
7995 end 8228 end
7996 elseif i:get_child("activated")then 8229 elseif i:get_child("activated")then
7997 self.onconnect_callback(self.conn); 8230 self.onconnect_callback(self.conn);
7998 end 8231 end
7999 end 8232 end
8000 function t:disconnect() 8233 function a:disconnect()
8001 if self.conn then 8234 if self.conn then
8002 self.conn:close(); 8235 self.conn:close();
8003 end 8236 end
8004 end 8237 end
8005 function t:handle_accepted(e) 8238 function a:handle_accepted(e)
8006 end 8239 end
8007 local t={__index=t}; 8240 local t={__index=a};
8008 e:hook("jingle/transport/"..o,function(e) 8241 e:hook("jingle/transport/"..o,function(e)
8009 return setmetatable({ 8242 return setmetatable({
8010 role=e.role, 8243 role=e.role,
8011 peer=e.peer, 8244 peer=e.peer,
8012 stream=e.stream, 8245 stream=e.stream,
8109 for t,e in ipairs(n or self.proxies)do 8342 for t,e in ipairs(n or self.proxies)do
8110 o:tag("streamhost",e):up(); 8343 o:tag("streamhost",e):up();
8111 end 8344 end
8112 self.stream:send_iq(o,function(o) 8345 self.stream:send_iq(o,function(o)
8113 if o.attr.type=="error"then 8346 if o.attr.type=="error"then
8114 local t,a,o=o:get_error(); 8347 local o,t,a=o:get_error();
8115 e:event("connection-failed",{conn=e,type=t,condition=a,text=o}); 8348 e:event("connection-failed",{conn=e,type=o,condition=t,text=a});
8116 else 8349 else
8117 local o=o.tags[1]:get_child("streamhost-used"); 8350 local o=o.tags[1]:get_child("streamhost-used");
8118 e.streamhost_jid=o.attr.jid; 8351 e.streamhost_jid=o.attr.jid;
8119 local h,o; 8352 local o,h;
8120 for a,t in ipairs(n or self.proxies)do 8353 for a,t in ipairs(n or self.proxies)do
8121 if t.jid==e.streamhost_jid then 8354 if t.jid==e.streamhost_jid then
8122 h,o=t.host,t.port; 8355 o,h=t.host,t.port;
8123 break; 8356 break;
8124 end 8357 end
8125 end 8358 end
8126 e:connect(h,o); 8359 e:connect(o,h);
8127 local function o() 8360 local function o()
8128 e:unhook("connected",o); 8361 e:unhook("connected",o);
8129 local t=a.iq{to=e.streamhost_jid,type="set"} 8362 local t=a.iq{to=e.streamhost_jid,type="set"}
8130 :tag("query",{xmlns=i,sid=e.bytestream_sid}) 8363 :tag("query",{xmlns=i,sid=e.bytestream_sid})
8131 :tag("activate"):text(t); 8364 :tag("activate"):text(t);
8141 end 8374 end
8142 end); 8375 end);
8143 return e; 8376 return e;
8144 end 8377 end
8145 function s(i,e,t,a,o) 8378 function s(i,e,t,a,o)
8146 local t=r(t..a..o); 8379 local i=r(t..a..o);
8147 local function a() 8380 local function s()
8148 e:unhook("connected",a); 8381 e:unhook("connected",s);
8149 return true; 8382 return true;
8150 end 8383 end
8151 local function o(t) 8384 local function n(t)
8152 e:unhook("incoming-raw",o); 8385 e:unhook("incoming-raw",n);
8153 if t:sub(1,2)~="\005\000"then 8386 if t:sub(1,2)~="\005\000"then
8154 return e:event("error","connection-failure"); 8387 return e:event("error","connection-failure");
8155 end 8388 end
8156 e:event("connected"); 8389 e:event("connected");
8157 return true; 8390 return true;
8158 end 8391 end
8159 local function i(a) 8392 local function a(o)
8160 e:unhook("incoming-raw",i); 8393 e:unhook("incoming-raw",a);
8161 if a~="\005\000"then 8394 if o~="\005\000"then
8162 local t="version-mismatch"; 8395 local t="version-mismatch";
8163 if a:sub(1,1)=="\005"then 8396 if o:sub(1,1)=="\005"then
8164 t="authentication-failure"; 8397 t="authentication-failure";
8165 end 8398 end
8166 return e:event("error",t); 8399 return e:event("error",t);
8167 end 8400 end
8168 e:send(string.char(5,1,0,3,#t)..t.."\0\0"); 8401 e:send(string.char(5,1,0,3,#i)..i.."\0\0");
8169 e:hook("incoming-raw",o,100); 8402 e:hook("incoming-raw",n,100);
8170 return true; 8403 return true;
8171 end 8404 end
8172 e:hook("connected",a,200); 8405 e:hook("connected",s,200);
8173 e:hook("incoming-raw",i,100); 8406 e:hook("incoming-raw",a,100);
8174 e:send("\005\001\000"); 8407 e:send("\005\001\000");
8175 end 8408 end
8176 end) 8409 end)
8177 package.preload['verse.plugins.jingle_ibb']=(function(...) 8410 package.preload['verse.plugins.jingle_ibb']=(function(...)
8178 local _ENV=_ENV; 8411 local _ENV=_ENV;
8186 _M=e; 8419 _M=e;
8187 return e; 8420 return e;
8188 end 8421 end
8189 local e=require"verse"; 8422 local e=require"verse";
8190 local i=require"util.encodings".base64; 8423 local i=require"util.encodings".base64;
8191 local h=require"util.id".short; 8424 local s=require"util.id".short;
8192 local n="urn:xmpp:jingle:transports:ibb:1"; 8425 local n="urn:xmpp:jingle:transports:ibb:1";
8193 local o="http://jabber.org/protocol/ibb"; 8426 local o="http://jabber.org/protocol/ibb";
8194 assert(i.encode("This is a test.")=="VGhpcyBpcyBhIHRlc3Qu","Base64 encoding failed"); 8427 assert(i.encode("This is a test.")=="VGhpcyBpcyBhIHRlc3Qu","Base64 encoding failed");
8195 assert(i.decode("VGhpcyBpcyBhIHRlc3Qu")=="This is a test.","Base64 decoding failed"); 8428 assert(i.decode("VGhpcyBpcyBhIHRlc3Qu")=="This is a test.","Base64 decoding failed");
8196 local a={}; 8429 local a={};
8197 local t={__index=a}; 8430 local h={__index=a};
8198 local function s(a) 8431 local function r(t)
8199 local t=setmetatable({stream=a},t) 8432 local t=setmetatable({stream=t},h)
8200 t=e.eventable(t); 8433 t=e.eventable(t);
8201 return t; 8434 return t;
8202 end 8435 end
8203 function a:initiate(t,e,a) 8436 function a:initiate(t,e,a)
8204 self.block=2048; 8437 self.block=2048;
8299 a:hook("ready",function() 8532 a:hook("ready",function()
8300 a:add_disco_feature(n); 8533 a:add_disco_feature(n);
8301 end,10); 8534 end,10);
8302 local t={}; 8535 local t={};
8303 function t:_setup() 8536 function t:_setup()
8304 local e=s(self.stream); 8537 local e=r(self.stream);
8305 e.sid=self.sid or e.sid; 8538 e.sid=self.sid or e.sid;
8306 e.stanza=self.stanza or e.stanza; 8539 e.stanza=self.stanza or e.stanza;
8307 e.block=self.block or e.block; 8540 e.block=self.block or e.block;
8308 e:initiate(self.peer,self.sid,self.stanza); 8541 e:initiate(self.peer,self.sid,self.stanza);
8309 self.conn=e; 8542 self.conn=e;
8310 end 8543 end
8311 function t:generate_initiate() 8544 function t:generate_initiate()
8312 print("ibb:generate_initiate() as "..self.role); 8545 print("ibb:generate_initiate() as "..self.role);
8313 local t=h(); 8546 local t=s();
8314 self.sid=t; 8547 self.sid=t;
8315 self.stanza='iq'; 8548 self.stanza='iq';
8316 self.block=2048; 8549 self.block=2048;
8317 local e=e.stanza("transport",{xmlns=n, 8550 local e=e.stanza("transport",{xmlns=n,
8318 sid=self.sid,stanza=self.stanza,["block-size"]=self.block}); 8551 sid=self.sid,stanza=self.stanza,["block-size"]=self.block});
8373 _ENV=e; 8606 _ENV=e;
8374 _M=e; 8607 _M=e;
8375 return e; 8608 return e;
8376 end 8609 end
8377 local i=require"verse"; 8610 local i=require"verse";
8378 local h=table.insert; 8611 local n=table.insert;
8379 local o="http://jabber.org/protocol/pubsub"; 8612 local o="http://jabber.org/protocol/pubsub";
8380 local s="http://jabber.org/protocol/pubsub#owner"; 8613 local h="http://jabber.org/protocol/pubsub#owner";
8381 local r="http://jabber.org/protocol/pubsub#event"; 8614 local a="http://jabber.org/protocol/pubsub#event";
8382 local e={}; 8615 local t={};
8383 local n={__index=e}; 8616 local s={__index=t};
8384 function i.plugins.pubsub(e) 8617 function i.plugins.pubsub(e)
8385 e.pubsub=setmetatable({stream=e},n); 8618 e.pubsub=setmetatable({stream=e},s);
8386 e:hook("message",function(t) 8619 e:hook("message",function(t)
8387 local a=t.attr.from; 8620 local o=t.attr.from;
8388 for t in t:childtags("event",r)do 8621 for t in t:childtags("event",a)do
8389 local t=t:get_child("items"); 8622 local t=t:get_child("items");
8390 if t then 8623 if t then
8391 local o=t.attr.node; 8624 local a=t.attr.node;
8392 for t in t:childtags("item")do 8625 for t in t:childtags("item")do
8393 e:event("pubsub/event",{ 8626 e:event("pubsub/event",{
8394 from=a; 8627 from=o;
8395 node=o; 8628 node=a;
8396 item=t; 8629 item=t;
8397 }); 8630 });
8398 end 8631 end
8399 for t in t:childtags("retract")do 8632 for t in t:childtags("retract")do
8400 e:event("pubsub/retraction",{ 8633 e:event("pubsub/retraction",{
8401 from=a; 8634 from=o;
8402 node=o; 8635 node=a;
8403 item=t; 8636 item=t;
8404 }); 8637 });
8405 end 8638 end
8406 end 8639 end
8407 end 8640 end
8408 end); 8641 end);
8409 return true; 8642 return true;
8410 end 8643 end
8411 function e:create(a,e,t) 8644 function t:create(a,t,e)
8412 return self:service(a):node(e):create(nil,t); 8645 return self:service(a):node(t):create(nil,e);
8413 end 8646 end
8414 function e:subscribe(o,t,a,e) 8647 function t:subscribe(e,t,o,a)
8415 return self:service(o):node(t):subscribe(a,nil,e); 8648 return self:service(e):node(t):subscribe(o,nil,a);
8416 end 8649 end
8417 function e:publish(e,t,o,a,i) 8650 function t:publish(i,o,t,a,e)
8418 return self:service(e):node(t):publish(o,nil,a,i); 8651 return self:service(i):node(o):publish(t,nil,a,e);
8419 end 8652 end
8420 local a={}; 8653 local a={};
8421 local t={__index=a}; 8654 local r={__index=a};
8422 function e:service(e) 8655 function t:service(e)
8423 return setmetatable({stream=self.stream,service=e},t) 8656 return setmetatable({stream=self.stream,service=e},r)
8424 end 8657 end
8425 local function t(d,r,e,n,h,s,t,a) 8658 local function t(t,r,s,a,d,h,e,n)
8426 local e=i.iq{type=d or"get",to=r} 8659 local t=i.iq{type=t or"get",to=r}
8427 :tag("pubsub",{xmlns=e or o}) 8660 :tag("pubsub",{xmlns=s or o})
8428 local o={node=h,jid=s}; 8661 local o={node=d,jid=h};
8429 if a then 8662 if n then
8430 for t,e in pairs(a)do 8663 for t,e in pairs(n)do
8431 o[t]=e; 8664 o[t]=e;
8432 end 8665 end
8433 end 8666 end
8434 if n then e:tag(n,o);end 8667 if a then t:tag(a,o);end
8668 if e then
8669 t:tag("item",{id=e~=true and e or nil});
8670 end
8671 return t;
8672 end
8673 function a:subscriptions(e)
8674 self.stream:send_iq(t(nil,self.service,nil,"subscriptions")
8675 ,e and function(a)
8676 if a.attr.type=="result"then
8677 local t=a:get_child("pubsub",o);
8678 local t=t and t:get_child("subscriptions");
8679 local a={};
8435 if t then 8680 if t then
8436 e:tag("item",{id=t~=true and t or nil}); 8681 for e in t:childtags("subscription")do
8437 end
8438 return e;
8439 end
8440 function a:subscriptions(a)
8441 self.stream:send_iq(t(nil,self.service,nil,"subscriptions")
8442 ,a and function(e)
8443 if e.attr.type=="result"then
8444 local e=e:get_child("pubsub",o);
8445 local e=e and e:get_child("subscriptions");
8446 local o={};
8447 if e then
8448 for e in e:childtags("subscription")do
8449 local t=self:node(e.attr.node) 8682 local t=self:node(e.attr.node)
8450 t.subscription=e; 8683 t.subscription=e;
8451 t.subscribed_jid=e.attr.jid; 8684 t.subscribed_jid=e.attr.jid;
8452 h(o,t); 8685 n(a,t);
8453 end 8686 end
8454 end 8687 end
8455 a(o); 8688 e(a);
8456 else 8689 else
8457 a(false,e:get_error()); 8690 e(false,a:get_error());
8458 end 8691 end
8459 end or nil); 8692 end or nil);
8460 end 8693 end
8461 function a:affiliations(a) 8694 function a:affiliations(a)
8462 self.stream:send_iq(t(nil,self.service,nil,"affiliations") 8695 self.stream:send_iq(t(nil,self.service,nil,"affiliations")
8464 if e.attr.type=="result"then 8697 if e.attr.type=="result"then
8465 local e=e:get_child("pubsub",o); 8698 local e=e:get_child("pubsub",o);
8466 local e=e and e:get_child("affiliations")or{}; 8699 local e=e and e:get_child("affiliations")or{};
8467 local o={}; 8700 local o={};
8468 if e then 8701 if e then
8469 for e in e:childtags("affiliation")do 8702 for t in e:childtags("affiliation")do
8470 local t=self:node(e.attr.node) 8703 local e=self:node(t.attr.node)
8471 t.affiliation=e; 8704 e.affiliation=t;
8472 h(o,t); 8705 n(o,e);
8473 end 8706 end
8474 end 8707 end
8475 a(o); 8708 a(o);
8476 else 8709 else
8477 a(false,e:get_error()); 8710 a(false,e:get_error());
8491 local e={}; 8724 local e={};
8492 local o={__index=e}; 8725 local o={__index=e};
8493 function a:node(e) 8726 function a:node(e)
8494 return setmetatable({stream=self.stream,service=self.service,node=e},o) 8727 return setmetatable({stream=self.stream,service=self.service,node=e},o)
8495 end 8728 end
8496 function n:__call(t,e) 8729 function s:__call(t,e)
8497 local t=self:service(t); 8730 local t=self:service(t);
8498 return e and t:node(e)or t; 8731 return e and t:node(e)or t;
8499 end 8732 end
8500 function e:hook(a,o) 8733 function e:hook(a,o)
8501 self._hooks=self._hooks or setmetatable({},{__mode='kv'}); 8734 self._hooks=self._hooks or setmetatable({},{__mode='kv'});
8516 for e in pairs(self._hooks)do 8749 for e in pairs(self._hooks)do
8517 self.stream:unhook("pubsub/event",e); 8750 self.stream:unhook("pubsub/event",e);
8518 end 8751 end
8519 end 8752 end
8520 end 8753 end
8521 function e:create(e,a) 8754 function e:create(a,e)
8522 if e~=nil then 8755 if a~=nil then
8523 error("Not implemented yet."); 8756 error("Not implemented yet.");
8524 else 8757 else
8525 self.stream:send_iq(t("set",self.service,nil,"create",self.node),a); 8758 self.stream:send_iq(t("set",self.service,nil,"create",self.node),e);
8526 end 8759 end
8527 end 8760 end
8528 function e:configure(e,a) 8761 function e:configure(e,a)
8529 if e~=nil then 8762 if e~=nil then
8530 error("Not implemented yet."); 8763 error("Not implemented yet.");
8531 end 8764 end
8532 self.stream:send_iq(t("set",self.service,nil,e==nil and"default"or"configure",self.node),a); 8765 self.stream:send_iq(t("set",self.service,nil,e==nil and"default"or"configure",self.node),a);
8533 end 8766 end
8534 function e:publish(i,a,e,o) 8767 function e:publish(e,a,o,i)
8535 if a~=nil then 8768 if a~=nil then
8536 error("Node configuration is not implemented yet."); 8769 error("Node configuration is not implemented yet.");
8537 end 8770 end
8538 self.stream:send_iq(t("set",self.service,nil,"publish",self.node,nil,i or true) 8771 self.stream:send_iq(t("set",self.service,nil,"publish",self.node,nil,e or true)
8539 :add_child(e) 8772 :add_child(o)
8540 ,o); 8773 ,i);
8541 end 8774 end
8542 function e:subscribe(e,a,o) 8775 function e:subscribe(e,o,a)
8543 e=e or self.stream.jid; 8776 e=e or self.stream.jid;
8544 if a~=nil then 8777 if o~=nil then
8545 error("Subscription configuration is not implemented yet."); 8778 error("Subscription configuration is not implemented yet.");
8546 end 8779 end
8547 self.stream:send_iq(t("set",self.service,nil,"subscribe",self.node,e) 8780 self.stream:send_iq(t("set",self.service,nil,"subscribe",self.node,e)
8548 ,o); 8781 ,a);
8549 end 8782 end
8550 function e:subscription(e) 8783 function e:subscription(e)
8551 error("Not implemented yet."); 8784 error("Not implemented yet.");
8552 end 8785 end
8553 function e:affiliation(e) 8786 function e:affiliation(e)
8566 return self:item(nil,e); 8799 return self:item(nil,e);
8567 else 8800 else
8568 self.stream:disco_items(self.service,self.node,e); 8801 self.stream:disco_items(self.service,self.node,e);
8569 end 8802 end
8570 end 8803 end
8571 function e:item(e,a) 8804 function e:item(a,e)
8572 self.stream:send_iq(t("get",self.service,nil,"items",self.node,nil,e) 8805 self.stream:send_iq(t("get",self.service,nil,"items",self.node,nil,a)
8573 ,a); 8806 ,e);
8574 end 8807 end
8575 function e:retract(o,e,a) 8808 function e:retract(o,e,a)
8576 if type(e)=="function"then 8809 if type(e)=="function"then
8577 e,a=false,e; 8810 e,a=false,e;
8578 end 8811 end
8593 function e:purge(e,a) 8826 function e:purge(e,a)
8594 self.stream:send_iq( 8827 self.stream:send_iq(
8595 t( 8828 t(
8596 "set", 8829 "set",
8597 self.service, 8830 self.service,
8598 s, 8831 h,
8599 "purge", 8832 "purge",
8600 self.node, 8833 self.node,
8601 nil, 8834 nil,
8602 nil, 8835 nil,
8603 {notify=e and"1"or nil} 8836 {notify=e and"1"or nil}
8604 ), 8837 ),
8605 a 8838 a
8606 ); 8839 );
8607 end 8840 end
8608 function e:delete(e,a) 8841 function e:delete(a,e)
8609 assert(not e,"Not implemented yet."); 8842 assert(not a,"Not implemented yet.");
8610 self.stream:send_iq(t("set",self.service,s,"delete",self.node) 8843 self.stream:send_iq(t("set",self.service,h,"delete",self.node)
8611 ,a); 8844 ,e);
8612 end 8845 end
8613 end) 8846 end)
8614 package.preload['verse.plugins.pep']=(function(...) 8847 package.preload['verse.plugins.pep']=(function(...)
8615 local _ENV=_ENV; 8848 local _ENV=_ENV;
8616 local function e(t,...) 8849 local function e(t,...)
8631 e:add_plugin("pubsub"); 8864 e:add_plugin("pubsub");
8632 e.pep={}; 8865 e.pep={};
8633 e:hook("pubsub/event",function(t) 8866 e:hook("pubsub/event",function(t)
8634 return e:event("pep/"..t.node,{from=t.from,id=t.item.attr.id,item=t.item.tags[1]}); 8867 return e:event("pep/"..t.node,{from=t.from,id=t.item.attr.id,item=t.item.tags[1]});
8635 end); 8868 end);
8636 function e:hook_pep(t,i,o) 8869 function e:hook_pep(t,o,i)
8637 local a=e.events._handlers["pep/"..t]; 8870 local a=e.events._handlers["pep/"..t];
8638 if not(a)or#a==0 then 8871 if not(a)or#a==0 then
8639 e:add_disco_feature(t.."+notify"); 8872 e:add_disco_feature(t.."+notify");
8640 end 8873 end
8641 e:hook("pep/"..t,i,o); 8874 e:hook("pep/"..t,o,i);
8642 end 8875 end
8643 function e:unhook_pep(t,a) 8876 function e:unhook_pep(t,a)
8644 e:unhook("pep/"..t,a); 8877 e:unhook("pep/"..t,a);
8645 local a=e.events._handlers["pep/"..t]; 8878 local a=e.events._handlers["pep/"..t];
8646 if not(a)or#a==0 then 8879 if not(a)or#a==0 then
8683 end 8916 end
8684 e:debug("adhoc calling callback") 8917 e:debug("adhoc calling callback")
8685 return o(t); 8918 return o(t);
8686 end); 8919 end);
8687 end 8920 end
8688 function e:execute_command(o,i,t) 8921 function e:execute_command(i,o,t)
8689 local e=setmetatable({ 8922 local e=setmetatable({
8690 stream=e,jid=o, 8923 stream=e,jid=i,
8691 command=i,callback=t 8924 command=o,callback=t
8692 },a); 8925 },a);
8693 return e:execute(); 8926 return e:execute();
8694 end 8927 end
8695 local function s(t,e) 8928 local function r(t,e)
8696 if not(e)or e=="user"then return true;end 8929 if not(e)or e=="user"then return true;end
8697 if type(e)=="function"then 8930 if type(e)=="function"then
8698 return e(t); 8931 return e(t);
8699 end 8932 end
8700 end 8933 end
8701 function e:add_adhoc_command(o,a,h,s) 8934 function e:add_adhoc_command(o,a,h,s)
8702 i[a]=n.new(o,a,h,s); 8935 i[a]=n.new(o,a,h,s);
8703 e:add_disco_item({jid=e.jid,node=a,name=o},t); 8936 e:add_disco_item({jid=e.jid,node=a,name=o},t);
8704 return i[a]; 8937 return i[a];
8705 end 8938 end
8706 local function h(t) 8939 local function s(t)
8707 local a=t.tags[1]; 8940 local a=t.tags[1];
8708 local a=a.attr.node; 8941 local a=a.attr.node;
8709 local a=i[a]; 8942 local a=i[a];
8710 if not a then return;end 8943 if not a then return;end
8711 if not s(t.attr.from,a.permission)then 8944 if not r(t.attr.from,a.permission)then
8712 e:send(o.error_reply(t,"auth","forbidden","You don't have permission to execute this command"):up() 8945 e:send(o.error_reply(t,"auth","forbidden","You don't have permission to execute this command"):up()
8713 :add_child(a:cmdtag("canceled") 8946 :add_child(a:cmdtag("canceled")
8714 :tag("note",{type="error"}):text("You don't have permission to execute this command"))); 8947 :tag("note",{type="error"}):text("You don't have permission to execute this command")));
8715 return true 8948 return true
8716 end 8949 end
8717 return n.handle_cmd(a,{send=function(t)return e:send(t)end},t); 8950 return n.handle_cmd(a,{send=function(t)return e:send(t)end},t);
8718 end 8951 end
8719 e:hook("iq/"..t,function(e) 8952 e:hook("iq/"..t,function(e)
8720 local t=e.attr.type; 8953 local a=e.attr.type;
8721 local a=e.tags[1].name; 8954 local t=e.tags[1].name;
8722 if t=="set"and a=="command"then 8955 if a=="set"and t=="command"then
8723 return h(e); 8956 return s(e);
8724 end 8957 end
8725 end); 8958 end);
8726 end 8959 end
8727 function a:_process_response(e) 8960 function a:_process_response(e)
8728 if e.attr.type=="error"then 8961 if e.attr.type=="error"then
8742 :tag("command",{xmlns=t,node=self.command}); 8975 :tag("command",{xmlns=t,node=self.command});
8743 self.stream:send_iq(e,function(e) 8976 self.stream:send_iq(e,function(e)
8744 self:_process_response(e); 8977 self:_process_response(e);
8745 end); 8978 end);
8746 end 8979 end
8747 function a:next(e) 8980 function a:next(a)
8748 local t=o.iq({to=self.jid,type="set"}) 8981 local e=o.iq({to=self.jid,type="set"})
8749 :tag("command",{ 8982 :tag("command",{
8750 xmlns=t, 8983 xmlns=t,
8751 node=self.command, 8984 node=self.command,
8752 sessionid=self.sessionid 8985 sessionid=self.sessionid
8753 }); 8986 });
8754 if e then t:add_child(e);end 8987 if a then e:add_child(a);end
8755 self.stream:send_iq(t,function(e) 8988 self.stream:send_iq(e,function(e)
8756 self:_process_response(e); 8989 self:_process_response(e);
8757 end); 8990 end);
8758 end 8991 end
8759 end) 8992 end)
8760 package.preload['verse.plugins.presence']=(function(...) 8993 package.preload['verse.plugins.presence']=(function(...)
8812 end 9045 end
8813 _ENV=e; 9046 _ENV=e;
8814 _M=e; 9047 _M=e;
8815 return e; 9048 return e;
8816 end 9049 end
8817 local t=require"verse"; 9050 local a=require"verse";
8818 local a="jabber:iq:private"; 9051 local t="jabber:iq:private";
8819 function t.plugins.private(o) 9052 function a.plugins.private(n)
8820 function o:private_set(i,o,e,n) 9053 function n:private_set(o,i,e,n)
8821 local t=t.iq({type="set"}) 9054 local t=a.iq({type="set"})
8822 :tag("query",{xmlns=a}); 9055 :tag("query",{xmlns=t});
8823 if e then 9056 if e then
8824 if e.name==i and e.attr and e.attr.xmlns==o then 9057 if e.name==o and e.attr and e.attr.xmlns==i then
8825 t:add_child(e); 9058 t:add_child(e);
8826 else 9059 else
8827 t:tag(i,{xmlns=o}) 9060 t:tag(o,{xmlns=i})
8828 :add_child(e); 9061 :add_child(e);
8829 end 9062 end
8830 end 9063 end
8831 self:send_iq(t,n); 9064 self:send_iq(t,n);
8832 end 9065 end
8833 function o:private_get(e,o,i) 9066 function n:private_get(e,o,i)
8834 self:send_iq(t.iq({type="get"}) 9067 self:send_iq(a.iq({type="get"})
8835 :tag("query",{xmlns=a}) 9068 :tag("query",{xmlns=t})
8836 :tag(e,{xmlns=o}), 9069 :tag(e,{xmlns=o}),
8837 function(t) 9070 function(a)
8838 if t.attr.type=="result"then 9071 if a.attr.type=="result"then
8839 local t=t:get_child("query",a); 9072 local t=a:get_child("query",t);
8840 local e=t:get_child(e,o); 9073 local e=t:get_child(e,o);
8841 i(e); 9074 i(e);
8842 end 9075 end
8843 end); 9076 end);
8844 end 9077 end
8855 _ENV=e; 9088 _ENV=e;
8856 _M=e; 9089 _M=e;
8857 return e; 9090 return e;
8858 end 9091 end
8859 local i=require"verse"; 9092 local i=require"verse";
8860 local l=require"util.jid".bare; 9093 local d=require"util.jid".bare;
8861 local a="jabber:iq:roster"; 9094 local a="jabber:iq:roster";
8862 local o="urn:xmpp:features:rosterver"; 9095 local n="urn:xmpp:features:rosterver";
8863 local n=table.insert; 9096 local o=table.insert;
8864 function i.plugins.roster(t) 9097 function i.plugins.roster(t)
8865 local h=false; 9098 local s=false;
8866 local e={ 9099 local e={
8867 items={}; 9100 items={};
8868 ver=""; 9101 ver="";
8869 }; 9102 };
8870 t.roster=e; 9103 t.roster=e;
8871 t:hook("stream-features",function(e) 9104 t:hook("stream-features",function(e)
8872 if e:get_child("ver",o)then 9105 if e:get_child("ver",n)then
8873 h=true; 9106 s=true;
8874 end 9107 end
8875 end); 9108 end);
8876 local function s(e) 9109 local function h(t)
8877 local t=i.stanza("item",{xmlns=a}); 9110 local e=i.stanza("item",{xmlns=a});
8878 for a,e in pairs(e)do 9111 for a,t in pairs(t)do
8879 if a~="groups"then 9112 if a~="groups"then
8880 t.attr[a]=e; 9113 e.attr[a]=t;
8881 else 9114 else
8882 for a=1,#e do 9115 for a=1,#t do
8883 t:tag("group"):text(e[a]):up(); 9116 e:tag("group"):text(t[a]):up();
8884 end 9117 end
8885 end 9118 end
8886 end 9119 end
8887 return t; 9120 return e;
8888 end 9121 end
8889 local function d(a) 9122 local function r(a)
8890 local e={}; 9123 local e={};
8891 local t={}; 9124 local t={};
8892 e.groups=t; 9125 e.groups=t;
8893 for t,a in pairs(a.attr)do 9126 for t,a in pairs(a.attr)do
8894 if t~="xmlns"then 9127 if t~="xmlns"then
8895 e[t]=a 9128 e[t]=a
8896 end 9129 end
8897 end 9130 end
8898 for e in a:childtags("group")do 9131 for e in a:childtags("group")do
8899 n(t,e:get_text()) 9132 o(t,e:get_text())
8900 end 9133 end
8901 return e; 9134 return e;
8902 end 9135 end
8903 function e:load(t) 9136 function e:load(t)
8904 e.ver,e.items=t.ver,t.items; 9137 e.ver,e.items=t.ver,t.items;
8907 return{ 9140 return{
8908 ver=e.ver, 9141 ver=e.ver,
8909 items=e.items, 9142 items=e.items,
8910 }; 9143 };
8911 end 9144 end
8912 function e:add_contact(h,o,n,e) 9145 function e:add_contact(o,n,s,e)
8913 local o={jid=h,name=o,groups=n}; 9146 local o={jid=o,name=n,groups=s};
8914 local a=i.iq({type="set"}) 9147 local a=i.iq({type="set"})
8915 :tag("query",{xmlns=a}) 9148 :tag("query",{xmlns=a})
8916 :add_child(s(o)); 9149 :add_child(h(o));
8917 t:send_iq(a,function(t) 9150 t:send_iq(a,function(t)
8918 if not e then return end 9151 if not e then return end
8919 if t.attr.type=="result"then 9152 if t.attr.type=="result"then
8920 e(true); 9153 e(true);
8921 else 9154 else
8923 end 9156 end
8924 end); 9157 end);
8925 end 9158 end
8926 function e:delete_contact(o,n) 9159 function e:delete_contact(o,n)
8927 o=(type(o)=="table"and o.jid)or o; 9160 o=(type(o)=="table"and o.jid)or o;
8928 local h={jid=o,subscription="remove"} 9161 local s={jid=o,subscription="remove"}
8929 if not e.items[o]then return false,"item-not-found";end 9162 if not e.items[o]then return false,"item-not-found";end
8930 t:send_iq(i.iq({type="set"}) 9163 t:send_iq(i.iq({type="set"})
8931 :tag("query",{xmlns=a}) 9164 :tag("query",{xmlns=a})
8932 :add_child(s(h)), 9165 :add_child(h(s)),
8933 function(e) 9166 function(e)
8934 if not n then return end 9167 if not n then return end
8935 if e.attr.type=="result"then 9168 if e.attr.type=="result"then
8936 n(true); 9169 n(true);
8937 else 9170 else
8938 n(nil,e); 9171 n(nil,e);
8939 end 9172 end
8940 end); 9173 end);
8941 end 9174 end
9175 local function h(t)
9176 local t=r(t);
9177 e.items[t.jid]=t;
9178 end
8942 local function r(t) 9179 local function r(t)
8943 local t=d(t);
8944 e.items[t.jid]=t;
8945 end
8946 local function d(t)
8947 local a=e.items[t]; 9180 local a=e.items[t];
8948 e.items[t]=nil; 9181 e.items[t]=nil;
8949 return a; 9182 return a;
8950 end 9183 end
8951 function e:fetch(o) 9184 function e:fetch(n)
8952 t:send_iq(i.iq({type="get"}):tag("query",{xmlns=a,ver=h and e.ver or nil}), 9185 t:send_iq(i.iq({type="get"}):tag("query",{xmlns=a,ver=s and e.ver or nil}),
8953 function(t) 9186 function(o)
8954 if t.attr.type=="result"then 9187 if o.attr.type=="result"then
8955 local t=t:get_child("query",a); 9188 local t=o:get_child("query",a);
8956 if t then 9189 if t then
8957 e.items={}; 9190 e.items={};
8958 for t in t:childtags("item")do 9191 for t in t:childtags("item")do
8959 r(t) 9192 h(t)
8960 end 9193 end
8961 e.ver=t.attr.ver or""; 9194 e.ver=t.attr.ver or"";
8962 end 9195 end
8963 o(e); 9196 n(e);
8964 else 9197 else
8965 o(nil,t); 9198 n(nil,o);
8966 end 9199 end
8967 end); 9200 end);
8968 end 9201 end
8969 t:hook("iq/"..a,function(o) 9202 t:hook("iq/"..a,function(n)
8970 local s,n=o.attr.type,o.attr.from; 9203 local s,o=n.attr.type,n.attr.from;
8971 if s=="set"and(not n or n==l(t.jid))then 9204 if s=="set"and(not o or o==d(t.jid))then
8972 local s=o:get_child("query",a); 9205 local s=n:get_child("query",a);
8973 local n=s and s:get_child("item"); 9206 local a=s and s:get_child("item");
8974 if n then 9207 if a then
8975 local o,a; 9208 local n,o;
8976 local i=n.attr.jid; 9209 local i=a.attr.jid;
8977 if n.attr.subscription=="remove"then 9210 if a.attr.subscription=="remove"then
8978 o="removed" 9211 n="removed"
8979 a=d(i); 9212 o=r(i);
8980 else 9213 else
8981 o=e.items[i]and"changed"or"added"; 9214 n=e.items[i]and"changed"or"added";
8982 r(n) 9215 h(a)
8983 a=e.items[i]; 9216 o=e.items[i];
8984 end 9217 end
8985 e.ver=s.attr.ver; 9218 e.ver=s.attr.ver;
8986 if a then 9219 if o then
8987 t:event("roster/item-"..o,a); 9220 t:event("roster/item-"..n,o);
8988 end 9221 end
8989 end 9222 end
8990 t:send(i.reply(o)) 9223 t:send(i.reply(n))
8991 return true; 9224 return true;
8992 end 9225 end
8993 end); 9226 end);
8994 end 9227 end
8995 end) 9228 end)
9065 if not t and e.attr.to and a then 9298 if not t and e.attr.to and a then
9066 t=o.rooms[e.attr.to.." "..a] 9299 t=o.rooms[e.attr.to.." "..a]
9067 end 9300 end
9068 if t and t.opts.source and e.attr.to~=t.opts.source then return end 9301 if t and t.opts.source and e.attr.to~=t.opts.source then return end
9069 if t then 9302 if t then
9070 local o=select(3,n.split(e.attr.from)); 9303 local i=select(3,n.split(e.attr.from));
9071 local n=e:get_child_text("body"); 9304 local n=e:get_child_text("body");
9072 local i=e:get_child("delay",h); 9305 local o=e:get_child("delay",h);
9073 local a={ 9306 local a={
9074 room_jid=a; 9307 room_jid=a;
9075 room=t; 9308 room=t;
9076 sender=t.occupants[o]; 9309 sender=t.occupants[i];
9077 nick=o; 9310 nick=i;
9078 body=n; 9311 body=n;
9079 stanza=e; 9312 stanza=e;
9080 delay=(i and i.attr.stamp); 9313 delay=(o and o.attr.stamp);
9081 }; 9314 };
9082 local t=t:event(e.name,a); 9315 local t=t:event(e.name,a);
9083 return t or(e.name=="message")or nil; 9316 return t or(e.name=="message")or nil;
9084 end 9317 end
9085 end,500); 9318 end,500);
9184 self:send(i.message():tag("body"):text(e)); 9417 self:send(i.message():tag("body"):text(e));
9185 end 9418 end
9186 function a:set_subject(e) 9419 function a:set_subject(e)
9187 self:send(i.message():tag("subject"):text(e)); 9420 self:send(i.message():tag("subject"):text(e));
9188 end 9421 end
9189 function a:leave(t) 9422 function a:leave(e)
9190 self.stream:event("groupchat/leaving",self); 9423 self.stream:event("groupchat/leaving",self);
9191 local e=i.presence({type="unavailable"}); 9424 local t=i.presence({type="unavailable"});
9192 if t then 9425 if e then
9193 e:tag("status"):text(t); 9426 t:tag("status"):text(e);
9194 end 9427 end
9195 self:send(e); 9428 self:send(t);
9196 end 9429 end
9197 function a:admin_set(e,t,a,o) 9430 function a:admin_set(t,o,a,e)
9198 self:send(i.iq({type="set"}) 9431 self:send(i.iq({type="set"})
9199 :query(s.."#admin") 9432 :query(s.."#admin")
9200 :tag("item",{nick=e,[t]=a}) 9433 :tag("item",{nick=t,[o]=a})
9201 :tag("reason"):text(o or"")); 9434 :tag("reason"):text(e or""));
9202 end 9435 end
9203 function a:set_role(a,e,t) 9436 function a:set_role(t,e,a)
9204 self:admin_set(a,"role",e,t); 9437 self:admin_set(t,"role",e,a);
9205 end 9438 end
9206 function a:set_affiliation(t,a,e) 9439 function a:set_affiliation(e,t,a)
9207 self:admin_set(t,"affiliation",a,e); 9440 self:admin_set(e,"affiliation",t,a);
9208 end 9441 end
9209 function a:kick(t,e) 9442 function a:kick(t,e)
9210 self:set_role(t,"none",e); 9443 self:set_role(t,"none",e);
9211 end 9444 end
9212 function a:ban(e,t) 9445 function a:ban(t,e)
9213 self:set_affiliation(e,"outcast",t); 9446 self:set_affiliation(t,"outcast",e);
9214 end 9447 end
9215 end) 9448 end)
9216 package.preload['verse.plugins.vcard']=(function(...) 9449 package.preload['verse.plugins.vcard']=(function(...)
9217 local _ENV=_ENV; 9450 local _ENV=_ENV;
9218 local function e(t,...) 9451 local function e(t,...)
9268 end 9501 end
9269 _ENV=e; 9502 _ENV=e;
9270 _M=e; 9503 _M=e;
9271 return e; 9504 return e;
9272 end 9505 end
9273 local n=require"verse"; 9506 local i=require"verse";
9274 local i="vcard-temp:x:update"; 9507 local n="vcard-temp:x:update";
9275 local r=require("util.hashes").sha1; 9508 local s=require("util.hashes").sha1;
9276 local e,t=pcall(function() 9509 local e,t=pcall(function()
9277 local e=require("util.encodings").base64.decode; 9510 local e=require("util.encodings").base64.decode;
9278 assert(e("SGVsbG8=")=="Hello") 9511 assert(e("SGVsbG8=")=="Hello")
9279 return e; 9512 return e;
9280 end); 9513 end);
9283 if not e then 9516 if not e then
9284 error("Could not find a base64 decoder") 9517 error("Could not find a base64 decoder")
9285 end 9518 end
9286 end 9519 end
9287 local h=t; 9520 local h=t;
9288 function n.plugins.vcard_update(e) 9521 function i.plugins.vcard_update(e)
9289 e:add_plugin("vcard"); 9522 e:add_plugin("vcard");
9290 e:add_plugin("presence"); 9523 e:add_plugin("presence");
9291 local t; 9524 local t;
9292 local function s(a) 9525 local function r(a)
9293 local o; 9526 local o;
9294 for e=1,#a do 9527 for e=1,#a do
9295 if a[e].name=="PHOTO"then 9528 if a[e].name=="PHOTO"then
9296 o=a[e][1]; 9529 o=a[e][1];
9297 break 9530 break
9298 end 9531 end
9299 end 9532 end
9300 if o then 9533 if o then
9301 local a=r(h(o),true); 9534 local a=s(h(o),true);
9302 t=n.stanza("x",{xmlns=i}) 9535 t=i.stanza("x",{xmlns=n})
9303 :tag("photo"):text(a); 9536 :tag("photo"):text(a);
9304 e:resend_presence() 9537 e:resend_presence()
9305 else 9538 else
9306 t=nil; 9539 t=nil;
9307 end 9540 end
9310 e:hook("ready",function() 9543 e:hook("ready",function()
9311 if a then return;end 9544 if a then return;end
9312 a=true; 9545 a=true;
9313 e:get_vcard(nil,function(t) 9546 e:get_vcard(nil,function(t)
9314 if t then 9547 if t then
9315 s(t) 9548 r(t)
9316 end 9549 end
9317 e:event("ready"); 9550 e:event("ready");
9318 end); 9551 end);
9319 return true; 9552 return true;
9320 end,3); 9553 end,3);
9321 e:hook("presence-out",function(e) 9554 e:hook("presence-out",function(e)
9322 if t and not e:get_child("x",i)then 9555 if t and not e:get_child("x",n)then
9323 e:add_child(t); 9556 e:add_child(t);
9324 end 9557 end
9325 end,10); 9558 end,10);
9326 end 9559 end
9327 end) 9560 end)
9335 end 9568 end
9336 _ENV=e; 9569 _ENV=e;
9337 _M=e; 9570 _M=e;
9338 return e; 9571 return e;
9339 end 9572 end
9340 local o=require"verse"; 9573 local a=require"verse";
9341 local a="urn:xmpp:carbons:2"; 9574 local o="urn:xmpp:carbons:2";
9342 local n="urn:xmpp:forward:0"; 9575 local r="urn:xmpp:forward:0";
9343 local h=os.time; 9576 local s=os.time;
9344 local s=require"util.datetime".parse; 9577 local h=require"util.datetime".parse;
9345 local r=require"util.jid".bare; 9578 local n=require"util.jid".bare;
9346 function o.plugins.carbons(e) 9579 function a.plugins.carbons(e)
9347 local t={}; 9580 local t={};
9348 t.enabled=false; 9581 t.enabled=false;
9349 e.carbons=t; 9582 e.carbons=t;
9350 function t:enable(i) 9583 function t:enable(i)
9351 e:send_iq(o.iq{type="set"} 9584 e:send_iq(a.iq{type="set"}
9352 :tag("enable",{xmlns=a}) 9585 :tag("enable",{xmlns=o})
9353 ,function(e) 9586 ,function(e)
9354 local e=e.attr.type=="result"; 9587 local e=e.attr.type=="result";
9355 if e then 9588 if e then
9356 t.enabled=true; 9589 t.enabled=true;
9357 end 9590 end
9359 i(e); 9592 i(e);
9360 end 9593 end
9361 end or nil); 9594 end or nil);
9362 end 9595 end
9363 function t:disable(i) 9596 function t:disable(i)
9364 e:send_iq(o.iq{type="set"} 9597 e:send_iq(a.iq{type="set"}
9365 :tag("disable",{xmlns=a}) 9598 :tag("disable",{xmlns=o})
9366 ,function(e) 9599 ,function(e)
9367 local e=e.attr.type=="result"; 9600 local e=e.attr.type=="result";
9368 if e then 9601 if e then
9369 t.enabled=false; 9602 t.enabled=false;
9370 end 9603 end
9371 if i then 9604 if i then
9372 i(e); 9605 i(e);
9373 end 9606 end
9374 end or nil); 9607 end or nil);
9375 end 9608 end
9376 local i; 9609 local a;
9377 e:hook("bind-success",function() 9610 e:hook("bind-success",function()
9378 i=r(e.jid); 9611 a=n(e.jid);
9379 end); 9612 end);
9380 e:hook("message",function(o) 9613 e:hook("message",function(i)
9381 local t=o:get_child(nil,a); 9614 local t=i:get_child(nil,o);
9382 if o.attr.from==i and t then 9615 if i.attr.from==a and t then
9383 local o=t.name; 9616 local o=t.name;
9384 local t=t:get_child("forwarded",n); 9617 local t=t:get_child("forwarded",r);
9385 local a=t and t:get_child("message","jabber:client"); 9618 local a=t and t:get_child("message","jabber:client");
9386 local t=t:get_child("delay","urn:xmpp:delay"); 9619 local t=t:get_child("delay","urn:xmpp:delay");
9387 local t=t and t.attr.stamp; 9620 local t=t and t.attr.stamp;
9388 t=t and s(t); 9621 t=t and h(t);
9389 if a then 9622 if a then
9390 return e:event("carbon",{ 9623 return e:event("carbon",{
9391 dir=o, 9624 dir=o,
9392 stanza=a, 9625 stanza=a,
9393 timestamp=t or h(), 9626 timestamp=t or s(),
9394 }); 9627 });
9395 end 9628 end
9396 end 9629 end
9397 end,1); 9630 end,1);
9398 end 9631 end
9410 return e; 9643 return e;
9411 end 9644 end
9412 local a=require"verse"; 9645 local a=require"verse";
9413 local t=require"util.stanza"; 9646 local t=require"util.stanza";
9414 local e="urn:xmpp:mam:2" 9647 local e="urn:xmpp:mam:2"
9415 local c="urn:xmpp:forward:0"; 9648 local l="urn:xmpp:forward:0";
9416 local u="urn:xmpp:delay"; 9649 local d="urn:xmpp:delay";
9417 local i=require"util.id".short; 9650 local r=require"util.id".short;
9418 local m=require"util.datetime".parse; 9651 local c=require"util.datetime".parse;
9419 local r=require"util.datetime".datetime; 9652 local s=require"util.datetime".datetime;
9420 local o=require"util.dataforms".new; 9653 local o=require"util.dataforms".new;
9421 local h=require"util.rsm"; 9654 local n=require"util.rsm";
9422 local d={}; 9655 local u={};
9423 local l=o{ 9656 local m=o{
9424 {name="FORM_TYPE";type="hidden";value=e;}; 9657 {name="FORM_TYPE";type="hidden";value=e;};
9425 {name="with";type="jid-single";}; 9658 {name="with";type="jid-single";};
9426 {name="start";type="text-single"}; 9659 {name="start";type="text-single"};
9427 {name="end";type="text-single";}; 9660 {name="end";type="text-single";};
9428 }; 9661 };
9429 function a.plugins.archive(o) 9662 function a.plugins.archive(i)
9430 function o:query_archive(o,a,n) 9663 function i:query_archive(i,a,h)
9431 local i=i(); 9664 local o=r();
9432 local o=t.iq{id=i,type="set",to=o} 9665 local i=t.iq{id=o,type="set",to=i}
9433 :tag("query",{xmlns=e,queryid=i}); 9666 :tag("query",{xmlns=e,queryid=o});
9434 local t,s=tonumber(a["start"]),tonumber(a["end"]); 9667 local t,r=tonumber(a["start"]),tonumber(a["end"]);
9435 a["start"]=t and r(t); 9668 a["start"]=t and s(t);
9436 a["end"]=s and r(s); 9669 a["end"]=r and s(r);
9437 o:add_child(l:form(a,"submit")); 9670 i:add_child(m:form(a,"submit"));
9438 o:add_child(h.generate(a)); 9671 i:add_child(n.generate(a));
9439 local t={}; 9672 local t={};
9440 local function s(a) 9673 local function s(a)
9441 local e=a:get_child("result",e); 9674 local e=a:get_child("result",e);
9442 if e and e.attr.queryid==i then 9675 if e and e.attr.queryid==o then
9443 local a=e:get_child("forwarded",c); 9676 local a=e:get_child("forwarded",l);
9444 local i=e.attr.id; 9677 local i=e.attr.id;
9445 local e=a:get_child("delay",u); 9678 local e=a:get_child("delay",d);
9446 local o=e and m(e.attr.stamp)or nil; 9679 local o=e and c(e.attr.stamp)or nil;
9447 local e=a:get_child("message","jabber:client") 9680 local e=a:get_child("message","jabber:client")
9448 t[#t+1]={id=i,stamp=o,message=e}; 9681 t[#t+1]={id=i,stamp=o,message=e};
9449 return true 9682 return true
9450 end 9683 end
9451 end 9684 end
9452 self:hook("message",s,1); 9685 self:hook("message",s,1);
9453 self:send_iq(o,function(a) 9686 self:send_iq(i,function(a)
9454 self:unhook("message",s); 9687 self:unhook("message",s);
9455 if a.attr.type=="error"then 9688 if a.attr.type=="error"then
9456 self:warn(table.concat({a:get_error()}," ")) 9689 self:warn(table.concat({a:get_error()}," "))
9457 n(false,a:get_error()) 9690 h(false,a:get_error())
9458 return true; 9691 return true;
9459 end 9692 end
9460 local e=a:get_child("fin",e) 9693 local e=a:get_child("fin",e)
9461 if e then 9694 if e then
9462 t.complete=e.attr.complete=="true"or e.attr.complete=="1"; 9695 t.complete=e.attr.complete=="true"or e.attr.complete=="1";
9463 local e=h.get(e); 9696 local e=n.get(e);
9464 for e,a in pairs(e or d)do t[e]=a;end 9697 for a,e in pairs(e or u)do t[a]=e;end
9465 end 9698 end
9466 n(t); 9699 h(t);
9467 return true 9700 return true
9468 end); 9701 end);
9469 end 9702 end
9470 local i={ 9703 local n={
9471 always=true,[true]="always", 9704 always=true,[true]="always",
9472 never=false,[false]="never", 9705 never=false,[false]="never",
9473 roster="roster", 9706 roster="roster",
9474 } 9707 }
9475 local function n(t) 9708 local function h(t)
9476 local e={}; 9709 local e={};
9477 local a=t.attr.default; 9710 local a=t.attr.default;
9478 if a then 9711 if a then
9479 e[false]=i[a]; 9712 e[false]=n[a];
9480 end 9713 end
9481 local a=t:get_child("always"); 9714 local a=t:get_child("always");
9482 if a then 9715 if a then
9483 for t in a:childtags("jid")do 9716 for t in a:childtags("jid")do
9484 local t=t:get_text(); 9717 local t=t:get_text();
9496 end 9729 end
9497 local function s(o) 9730 local function s(o)
9498 local a 9731 local a
9499 a,o[false]=o[false],nil; 9732 a,o[false]=o[false],nil;
9500 if a~=nil then 9733 if a~=nil then
9501 a=i[a]; 9734 a=n[a];
9502 end 9735 end
9503 local i=t.stanza("prefs",{xmlns=e,default=a}) 9736 local a=t.stanza("prefs",{xmlns=e,default=a})
9504 local a=t.stanza("always"); 9737 local e=t.stanza("always");
9505 local e=t.stanza("never"); 9738 local t=t.stanza("never");
9506 for t,o in pairs(o)do 9739 for o,a in pairs(o)do
9507 (o and a or e):tag("jid"):text(t):up(); 9740 (a and e or t):tag("jid"):text(o):up();
9508 end 9741 end
9509 return i:add_child(a):add_child(e); 9742 return a:add_child(e):add_child(t);
9510 end 9743 end
9511 function o:archive_prefs_get(a) 9744 function i:archive_prefs_get(a)
9512 self:send_iq(t.iq{type="get"}:tag("prefs",{xmlns=e}), 9745 self:send_iq(t.iq{type="get"}:tag("prefs",{xmlns=e}),
9513 function(e) 9746 function(e)
9514 if e and e.attr.type=="result"and e.tags[1]then 9747 if e and e.attr.type=="result"and e.tags[1]then
9515 local t=n(e.tags[1]); 9748 local t=h(e.tags[1]);
9516 a(t,e); 9749 a(t,e);
9517 else 9750 else
9518 a(nil,e); 9751 a(nil,e);
9519 end 9752 end
9520 end); 9753 end);
9521 end 9754 end
9522 function o:archive_prefs_set(e,a) 9755 function i:archive_prefs_set(e,a)
9523 self:send_iq(t.iq{type="set"}:add_child(s(e)),a); 9756 self:send_iq(t.iq{type="set"}:add_child(s(e)),a);
9524 end 9757 end
9525 end 9758 end
9526 end) 9759 end)
9527 package.preload['verse.plugins.browsing']=(function(...) 9760 package.preload['verse.plugins.browsing']=(function(...)
9543 function e:browsing(t,i) 9776 function e:browsing(t,i)
9544 if type(t)=="string"then 9777 if type(t)=="string"then
9545 t={uri=t}; 9778 t={uri=t};
9546 end 9779 end
9547 local a=a.stanza("page",{xmlns=o}) 9780 local a=a.stanza("page",{xmlns=o})
9548 for e,t in pairs(t)do 9781 for t,e in pairs(t)do
9549 a:tag(e):text(t):up(); 9782 a:tag(t):text(e):up();
9550 end 9783 end
9551 return e:publish_pep(a,i); 9784 return e:publish_pep(a,i);
9552 end 9785 end
9553 e:hook_pep(o,function(a) 9786 e:hook_pep(o,function(a)
9554 local t=a.item; 9787 local t=a.item;
9572 end 9805 end
9573 _ENV=e; 9806 _ENV=e;
9574 _M=e; 9807 _M=e;
9575 return e; 9808 return e;
9576 end 9809 end
9577 local n,t=string.format,string.char; 9810 local i,t=string.format,string.char;
9578 local r,h=pairs,ipairs; 9811 local h,r=pairs,ipairs;
9579 local i,o=table.insert,table.concat; 9812 local o,s=table.insert,table.concat;
9580 local e={}; 9813 local e={};
9581 for a=0,255 do 9814 for a=0,255 do
9582 local t=t(a); 9815 local t=t(a);
9583 local a=n("%%%02x",a); 9816 local a=i("%%%02x",a);
9584 e[t]=a; 9817 e[t]=a;
9585 e[a]=t; 9818 e[a]=t;
9586 e[a:upper()]=t; 9819 e[a:upper()]=t;
9587 end 9820 end
9588 local function n(t) 9821 local function n(t)
9592 return t and(t:gsub("%%%x%x",e)); 9825 return t and(t:gsub("%%%x%x",e));
9593 end 9826 end
9594 local function e(e) 9827 local function e(e)
9595 return e and(n(e):gsub("%%20","+")); 9828 return e and(n(e):gsub("%%20","+"));
9596 end 9829 end
9597 local function s(a) 9830 local function d(t)
9598 local t={}; 9831 local a={};
9599 if a[1]then 9832 if t[1]then
9600 for o,a in h(a)do 9833 for i,t in r(t)do
9601 i(t,e(a.name).."="..e(a.value)); 9834 o(a,e(t.name).."="..e(t.value));
9602 end 9835 end
9603 else 9836 else
9604 for a,o in r(a)do 9837 for i,t in h(t)do
9605 i(t,e(a).."="..e(o)); 9838 o(a,e(i).."="..e(t));
9606 end 9839 end
9607 end 9840 end
9608 return o(t,"&"); 9841 return s(a,"&");
9609 end 9842 end
9610 local function h(e) 9843 local function s(e)
9611 if not e:match("=")then return a(e);end 9844 if not e:match("=")then return a(e);end
9612 local o={}; 9845 local i={};
9613 for t,e in e:gmatch("([^=&]*)=([^&]*)")do 9846 for e,t in e:gmatch("([^=&]*)=([^&]*)")do
9614 t,e=t:gsub("%+","%%20"),e:gsub("%+","%%20"); 9847 e,t=e:gsub("%+","%%20"),t:gsub("%+","%%20");
9615 t,e=a(t),a(e); 9848 e,t=a(e),a(t);
9616 i(o,{name=t,value=e}); 9849 o(i,{name=e,value=t});
9617 o[t]=e; 9850 i[e]=t;
9618 end 9851 end
9619 return o; 9852 return i;
9620 end 9853 end
9621 local function i(e,t) 9854 local function o(e,t)
9622 e=","..e:gsub("[ \t]",""):lower()..","; 9855 e=","..e:gsub("[ \t]",""):lower()..",";
9623 return e:find(","..t:lower()..",",1,true)~=nil; 9856 return e:find(","..t:lower()..",",1,true)~=nil;
9624 end 9857 end
9625 local function o(e,t) 9858 local function t(e,t)
9626 if t then 9859 if t then
9627 if e:sub(-1,-1)~="/"then e=e.."/";end 9860 if e:sub(-1,-1)~="/"then e=e.."/";end
9628 else 9861 else
9629 if e:sub(-1,-1)=="/"then e=e:sub(1,-2);end 9862 if e:sub(-1,-1)=="/"then e=e:sub(1,-2);end
9630 end 9863 end
9631 if e:sub(1,1)~="/"then e="/"..e;end 9864 if e:sub(1,1)~="/"then e="/"..e;end
9632 return e; 9865 return e;
9633 end 9866 end
9634 return{ 9867 return{
9635 urlencode=n,urldecode=a; 9868 urlencode=n,urldecode=a;
9636 formencode=s,formdecode=h; 9869 formencode=d,formdecode=s;
9637 contains_token=i; 9870 contains_token=o;
9638 normalize_path=o; 9871 normalize_path=t;
9639 }; 9872 };
9640 end) 9873 end)
9641 package.preload['net.http.parser']=(function(...) 9874 package.preload['net.http.parser']=(function(...)
9642 local _ENV=_ENV; 9875 local _ENV=_ENV;
9643 local function e(t,...) 9876 local function e(t,...)
9648 end 9881 end
9649 _ENV=e; 9882 _ENV=e;
9650 _M=e; 9883 _M=e;
9651 return e; 9884 return e;
9652 end 9885 end
9653 local u=tonumber; 9886 local l=tonumber;
9654 local g=assert; 9887 local b=assert;
9655 local z=require"socket.url".parse; 9888 local q=require"socket.url".parse;
9656 local t=require"util.http".urldecode; 9889 local t=require"util.http".urldecode;
9657 local j=require"util.dbuffer"; 9890 local g=require"util.dbuffer";
9658 local function x(e) 9891 local function j(e)
9659 e=t((e:gsub("//+","/"))); 9892 e=t((e:gsub("//+","/")));
9660 if e:sub(1,1)~="/"then 9893 if e:sub(1,1)~="/"then
9661 e="/"..e; 9894 e="/"..e;
9662 end 9895 end
9663 local t=0; 9896 local t=0;
9671 return nil; 9904 return nil;
9672 end 9905 end
9673 end 9906 end
9674 return e; 9907 return e;
9675 end 9908 end
9676 local q={}; 9909 local k={};
9677 function q.new(l,s,e,h) 9910 function k.new(c,n,e,r)
9678 local f=true; 9911 local f=true;
9679 if not e or e=="server"then f=false;else g(e=="client","Invalid parser type");end 9912 if not e or e=="server"then f=false;else b(e=="client","Invalid parser type");end
9680 local v=u(h and h().body_size_limit)or 10*1024*1024; 9913 local w=l(r and r().body_size_limit)or 10*1024*1024;
9681 local n=u(h and h().head_size_limit)or 10*1024; 9914 local s=l(r and r().head_size_limit)or 10*1024;
9682 local k=u(h and h().buffer_size_limit)or v*2; 9915 local k=l(r and r().buffer_size_limit)or w*2;
9683 local a=j.new(k); 9916 local a=g.new(k);
9684 local d; 9917 local d;
9685 local r=nil; 9918 local h=nil;
9686 local e; 9919 local e;
9687 local t; 9920 local t;
9688 local w; 9921 local y;
9689 local o; 9922 local o;
9690 return{ 9923 return{
9691 feed=function(c,i) 9924 feed=function(u,i)
9692 if o then return nil,"parse has failed";end 9925 if o then return nil,"parse has failed";end
9693 if not i then 9926 if not i then
9694 if r and f and not t then 9927 if h and f and not t then
9695 a:collapse(); 9928 a:collapse();
9696 e.body=a:read_chunk()or""; 9929 e.body=a:read_chunk()or"";
9697 e.partial=nil; 9930 e.partial=nil;
9698 l(e); 9931 c(e);
9699 r=nil; 9932 h=nil;
9700 elseif a:length()~=0 then 9933 elseif a:length()~=0 then
9701 o=true;return s("unexpected-eof"); 9934 o=true;return n("unexpected-eof");
9702 end 9935 end
9703 return; 9936 return;
9704 end 9937 end
9705 if not a:write(i)then o=true;return s("max-buffer-size-exceeded");end 9938 if not a:write(i)then o=true;return n("max-buffer-size-exceeded");end
9706 while a:length()>0 do 9939 while a:length()>0 do
9707 if r==nil then 9940 if h==nil then
9708 local b=a:sub(1,n):find("\r\n\r\n",nil,true); 9941 local b=a:sub(1,s):find("\r\n\r\n",nil,true);
9709 if not b then return;end 9942 if not b then return;end
9710 local p,c,m,i,g; 9943 local p,u,m,s,x;
9711 local y; 9944 local v;
9712 local n={}; 9945 local i={};
9713 for t in a:read(b+3):gmatch("([^\r\n]+)\r\n")do 9946 for t in a:read(b+3):gmatch("([^\r\n]+)\r\n")do
9714 if y then 9947 if v then
9715 local e,t=t:match("^([^%s:]+): *(.*)$"); 9948 local e,t=t:match("^([^%s:]+): *(.*)$");
9716 if not e then o=true;return s("invalid-header-line");end 9949 if not e then o=true;return n("invalid-header-line");end
9717 e=e:lower(); 9950 e=e:lower();
9718 n[e]=n[e]and n[e]..","..t or t; 9951 i[e]=i[e]and i[e]..","..t or t;
9719 else 9952 else
9720 y=t; 9953 v=t;
9721 if f then 9954 if f then
9722 m,i,g=t:match("^HTTP/(1%.[01]) (%d%d%d) (.*)$"); 9955 m,s,x=t:match("^HTTP/(1%.[01]) (%d%d%d) (.*)$");
9723 i=u(i); 9956 s=l(s);
9724 if not i then o=true;return s("invalid-status-line");end 9957 if not s then o=true;return n("invalid-status-line");end
9725 w=not 9958 y=not
9726 ((h and h().method=="HEAD") 9959 ((r and r().method=="HEAD")
9727 or(i==204 or i==304 or i==301) 9960 or(s==204 or s==304 or s==301)
9728 or(i>=100 and i<200)); 9961 or(s>=100 and s<200));
9729 else 9962 else
9730 p,c,m=t:match("^(%w+) (%S+) HTTP/(1%.[01])$"); 9963 p,u,m=t:match("^(%w+) (%S+) HTTP/(1%.[01])$");
9731 if not p then o=true;return s("invalid-status-line");end 9964 if not p then o=true;return n("invalid-status-line");end
9732 end 9965 end
9733 end 9966 end
9734 end 9967 end
9735 if not y then o=true;return s("invalid-status-line");end 9968 if not v then o=true;return n("invalid-status-line");end
9736 d=w and n["transfer-encoding"]=="chunked"; 9969 d=y and i["transfer-encoding"]=="chunked";
9737 t=u(n["content-length"]); 9970 t=l(i["content-length"]);
9738 if f then 9971 if f then
9739 if not w then t=0;end 9972 if not y then t=0;end
9740 e={ 9973 e={
9741 code=i; 9974 code=s;
9742 httpversion=m; 9975 httpversion=m;
9743 headers=n; 9976 headers=i;
9744 body=false; 9977 body=false;
9745 body_length=t; 9978 body_length=t;
9746 chunked=d; 9979 chunked=d;
9747 partial=true; 9980 partial=true;
9748 responseversion=m; 9981 responseversion=m;
9749 responseheaders=n; 9982 responseheaders=i;
9750 }; 9983 };
9751 else 9984 else
9752 local a; 9985 local a;
9753 if c:byte()==47 then 9986 if u:byte()==47 then
9754 local t,e=c:match("([^?]*).?(.*)"); 9987 local t,e=u:match("([^?]*).?(.*)");
9755 if e==""then e=nil;end 9988 if e==""then e=nil;end
9756 a={path=t,query=e}; 9989 a={path=t,query=e};
9757 else 9990 else
9758 a=z(c); 9991 a=q(u);
9759 if not(a and a.path)then o=true;return s("invalid-url");end 9992 if not(a and a.path)then o=true;return n("invalid-url");end
9760 end 9993 end
9761 c=x(a.path); 9994 u=j(a.path);
9762 n.host=a.host or n.host; 9995 i.host=a.host or i.host;
9763 t=t or 0; 9996 t=t or 0;
9764 e={ 9997 e={
9765 method=p; 9998 method=p;
9766 url=a; 9999 url=a;
9767 path=c; 10000 path=u;
9768 httpversion=m; 10001 httpversion=m;
9769 headers=n; 10002 headers=i;
9770 body=false; 10003 body=false;
9771 body_sink=nil; 10004 body_sink=nil;
9772 chunked=d; 10005 chunked=d;
9773 partial=true; 10006 partial=true;
9774 }; 10007 };
9775 end 10008 end
9776 if not t or t>v then 10009 if not t or t>w then
9777 l(e); 10010 c(e);
9778 if not e.body_sink and(t and t>v)then 10011 if not e.body_sink and(t and t>w)then
9779 o=true; 10012 o=true;
9780 return s("content-length-limit-exceeded"); 10013 return n("content-length-limit-exceeded");
9781 end 10014 end
9782 end 10015 end
9783 if d and not e.body_sink then 10016 if d and not e.body_sink then
9784 l(e); 10017 c(e);
9785 if not e.body_sink then 10018 if not e.body_sink then
9786 e.body_buffer=j.new(k); 10019 e.body_buffer=g.new(k);
9787 end 10020 end
9788 end 10021 end
9789 r=true; 10022 h=true;
9790 end 10023 end
9791 if r then 10024 if h then
9792 if d then 10025 if d then
9793 local n=a:sub(1,512); 10026 local s=a:sub(1,512);
9794 local t,i=n:match("^(%x+)[^\r\n]*\r\n()"); 10027 local t,i=s:match("^(%x+)[^\r\n]*\r\n()");
9795 if not t then return;end 10028 if not t then return;end
9796 t=t and u(t,16); 10029 t=t and l(t,16);
9797 if not t then o=true;return s("invalid-chunk-size");end 10030 if not t then o=true;return n("invalid-chunk-size");end
9798 if t==0 and n:find("\r\n\r\n",i-2,true)then 10031 if t==0 and s:find("\r\n\r\n",i-2,true)then
9799 local t=e.body_buffer; 10032 local t=e.body_buffer;
9800 if t then 10033 if t then
9801 e.body_buffer=nil; 10034 e.body_buffer=nil;
9802 t:collapse(); 10035 t:collapse();
9803 e.body=t:read_chunk()or""; 10036 e.body=t:read_chunk()or"";
9804 end 10037 end
9805 a:collapse(); 10038 a:collapse();
9806 local t=a:read_chunk(); 10039 local t=a:read_chunk();
9807 t=t:gsub("^.-\r\n\r\n",""); 10040 t=t:gsub("^.-\r\n\r\n","");
9808 a:write(t); 10041 a:write(t);
9809 r,d=nil,nil; 10042 h,d=nil,nil;
9810 e.partial=nil; 10043 e.partial=nil;
9811 l(e); 10044 c(e);
9812 elseif a:length()-i-1>=t then 10045 elseif a:length()-i-1>=t then
9813 a:discard(i-1); 10046 a:discard(i-1);
9814 (e.body_sink or e.body_buffer):write(a:read(t)); 10047 (e.body_sink or e.body_buffer):write(a:read(t));
9815 a:discard(2); 10048 a:discard(2);
9816 else 10049 else
9824 t=t-#i; 10057 t=t-#i;
9825 end 10058 end
9826 i=a:read_chunk(t); 10059 i=a:read_chunk(t);
9827 else 10060 else
9828 o=true; 10061 o=true;
9829 return s("body-sink-write-failure"); 10062 return n("body-sink-write-failure");
9830 end 10063 end
9831 end 10064 end
9832 if t==0 then 10065 if t==0 then
9833 r=nil; 10066 h=nil;
9834 e.partial=nil; 10067 e.partial=nil;
9835 l(e); 10068 c(e);
9836 end 10069 end
9837 elseif not t or a:length()>=t then 10070 elseif not t or a:length()>=t then
9838 g(not d) 10071 b(not d)
9839 e.body=t and a:read(t)or a:read_chunk()or""; 10072 e.body=t and a:read(t)or a:read_chunk()or"";
9840 r=nil; 10073 h=nil;
9841 e.partial=nil; 10074 e.partial=nil;
9842 l(e); 10075 c(e);
9843 else 10076 else
9844 break; 10077 break;
9845 end 10078 end
9846 else 10079 else
9847 break; 10080 break;
9848 end 10081 end
9849 end 10082 end
9850 end; 10083 end;
9851 }; 10084 };
9852 end 10085 end
9853 return q; 10086 return k;
9854 end) 10087 end)
9855 package.preload['net.http']=(function(...) 10088 package.preload['net.http']=(function(...)
9856 local _ENV=_ENV; 10089 local _ENV=_ENV;
9857 local function e(t,...) 10090 local function e(t,...)
9858 local e=package.loaded[t]or _ENV[t]or{_NAME=t}; 10091 local e=package.loaded[t]or _ENV[t]or{_NAME=t};
9862 end 10095 end
9863 _ENV=e; 10096 _ENV=e;
9864 _M=e; 10097 _M=e;
9865 return e; 10098 return e;
9866 end 10099 end
9867 local O=require"util.encodings".base64.encode; 10100 local z=require"util.encodings".base64.encode;
9868 local A=require"socket.url" 10101 local T=require"socket.url"
9869 local E=require"net.http.parser".new; 10102 local _=require"net.http.parser".new;
9870 local d=require"util.http"; 10103 local d=require"util.http";
9871 local T=require"util.events"; 10104 local O=require"util.events";
9872 local _=require"util.x509".verify_identity; 10105 local A=require"util.x509".verify_identity;
9873 local k=require"util.promise"; 10106 local E=require"util.promise";
9874 local g=require"net.http.errors"; 10107 local j=require"net.http.errors";
9875 local b=require"net.resolvers.basic"; 10108 local x=require"net.resolvers.basic";
9876 local v=require"net.connect".connect; 10109 local q=require"net.connect".connect;
9877 local y=pcall(require,"ssl"); 10110 local v=pcall(require,"ssl");
9878 local s,f=table.insert,table.concat; 10111 local s,p=table.insert,table.concat;
9879 local l=pairs; 10112 local l=pairs;
9880 local p,c,t= 10113 local y,c,t=
9881 tonumber,tostring,debug.traceback; 10114 tonumber,tostring,debug.traceback;
9882 local q=os.time; 10115 local w=os.time;
9883 local z=require"util.xpcall".xpcall; 10116 local b=require"util.xpcall".xpcall;
9884 local w=error 10117 local f=error
9885 local o=require"util.logger".init("http"); 10118 local o=require"util.logger".init("http");
9886 local _ENV=nil; 10119 local _ENV=nil;
9887 local a={}; 10120 local a={};
9888 local function x(e)return(c(e):match("%x+$"));end 10121 local function g(e)return(c(e):match("%x+$"));end
9889 local i={default_port=80,default_mode="*a"}; 10122 local n={default_port=80,default_mode="*a"};
9890 local function j(e)o("error","Traceback[http]: %s",t(c(e),2));return e;end 10123 local function k(e)o("error","Traceback[http]: %s",t(c(e),2));return e;end
9891 local function h(e,t,...) 10124 local function m(e,t,...)
9892 if not t then 10125 if not t then
9893 o("error","Request '%s': error in callback: %s",e.id,(...)); 10126 o("error","Request '%s': error in callback: %s",e.id,(...));
9894 if not e.suppress_errors then 10127 if not e.suppress_errors then
9895 w(...); 10128 f(...);
9896 end 10129 end
9897 end 10130 end
9898 return...; 10131 return...;
9899 end 10132 end
9900 local function r(e) 10133 local function r(e)
9902 if t then 10135 if t then
9903 e.conn=nil; 10136 e.conn=nil;
9904 t:close() 10137 t:close()
9905 end 10138 end
9906 end 10139 end
9907 local function m(e,t) 10140 local function u(e,t)
9908 if e.callback then 10141 if e.callback then
9909 e.callback(t or"cancelled",0,e); 10142 e.callback(t or"cancelled",0,e);
9910 e.callback=nil; 10143 e.callback=nil;
9911 end 10144 end
9912 if e.conn then 10145 if e.conn then
9913 r(e); 10146 r(e);
9914 end 10147 end
9915 end 10148 end
9916 local function u(e,n,t) 10149 local function h(e,n,a)
9917 if not e.parser then 10150 if not e.parser then
9918 local function i(t) 10151 local function i(t)
9919 if e.callback then 10152 if e.callback then
9920 e.callback(t or"connection-closed",0,e); 10153 e.callback(t or"connection-closed",0,e);
9921 e.callback=nil; 10154 e.callback=nil;
9922 end 10155 end
9923 r(e); 10156 r(e);
9924 end 10157 end
9925 if not n then 10158 if not n then
9926 i(t); 10159 i(a);
9927 return; 10160 return;
9928 end 10161 end
9929 local a; 10162 local a;
9930 local function n(t) 10163 local function n(t)
9931 if t.partial then 10164 if t.partial then
9950 r(e); 10183 r(e);
9951 end 10184 end
9952 local function t() 10185 local function t()
9953 return e; 10186 return e;
9954 end 10187 end
9955 e.parser=E(n,i,"client",t); 10188 e.parser=_(n,i,"client",t);
9956 end 10189 end
9957 e.parser:feed(n); 10190 e.parser:feed(n);
9958 end 10191 end
9959 function i.onconnect(t) 10192 function n.onconnect(t)
9960 local e=a[t]; 10193 local e=a[t];
9961 e.write=function(...)return e.conn:write(...);end 10194 e.write=function(...)return e.conn:write(...);end
9962 local r=e.callback; 10195 local r=e.callback;
9963 e.callback=function(i,a,n,s) 10196 e.callback=function(n,a,i,s)
9964 do 10197 do
9965 local t={http=e.http,url=e.url,request=e,response=n,content=i,code=a,callback=e.callback}; 10198 local t={http=e.http,url=e.url,request=e,response=i,content=n,code=a,callback=e.callback};
9966 e.http.events.fire_event("response",t); 10199 e.http.events.fire_event("response",t);
9967 i,a,n=t.content,t.code,t.response; 10200 n,a,i=t.content,t.code,t.response;
9968 end 10201 end
9969 o("debug","Request '%s': Calling callback, status %s",e.id,a or"---"); 10202 o("debug","Request '%s': Calling callback, status %s",e.id,a or"---");
9970 return h(e.id,z(r,j,i,a,n,s)); 10203 return m(e.id,b(r,k,n,a,i,s));
9971 end 10204 end
9972 e.reader=u; 10205 e.reader=h;
9973 e.state="status"; 10206 e.state="status";
9974 e.cancel=m; 10207 e.cancel=u;
9975 a[e.conn]=e; 10208 a[e.conn]=e;
9976 if not e.insecure and t:ssl()then 10209 if not e.insecure and t:ssl()then
9977 local a=t:socket(); 10210 local a=t:socket();
9978 local o=a.getpeerverification and a:getpeerverification(); 10211 local o=a.getpeerverification and a:getpeerverification();
9979 if not o then 10212 if not o then
9981 e.callback=nil; 10214 e.callback=nil;
9982 t:close(); 10215 t:close();
9983 return; 10216 return;
9984 end 10217 end
9985 local a=a.getpeercertificate and a:getpeercertificate(); 10218 local a=a.getpeercertificate and a:getpeercertificate();
9986 if not a or not _(e.host,false,a)then 10219 if not a or not A(e.host,false,a)then
9987 e.callback("certificate-verify-failed",0,e); 10220 e.callback("certificate-verify-failed",0,e);
9988 e.callback=nil; 10221 e.callback=nil;
9989 t:close(); 10222 t:close();
9990 return; 10223 return;
9991 end 10224 end
9996 end 10229 end
9997 for e,t in l(e.headers)do 10230 for e,t in l(e.headers)do
9998 s(a,e..": "..t.."\r\n"); 10231 s(a,e..": "..t.."\r\n");
9999 end 10232 end
10000 s(a,"\r\n") 10233 s(a,"\r\n")
10001 t:write(f(a)); 10234 t:write(p(a));
10002 if e.body then 10235 if e.body then
10003 t:write(e.body); 10236 t:write(e.body);
10004 end 10237 end
10005 end 10238 end
10006 function i.onincoming(t,i) 10239 function n.onincoming(i,t)
10007 local e=a[t]; 10240 local e=a[i];
10008 if not e then 10241 if not e then
10009 o("warn","Received response from connection %s with no request attached!",t); 10242 o("warn","Received response from connection %s with no request attached!",i);
10010 return; 10243 return;
10011 end 10244 end
10012 if i and e.reader then 10245 if t and e.reader then
10013 e:reader(i); 10246 e:reader(t);
10014 end 10247 end
10015 end 10248 end
10016 function i.ondisconnect(t,o) 10249 function n.ondisconnect(t,o)
10017 local e=a[t]; 10250 local e=a[t];
10018 if e and e.conn then 10251 if e and e.conn then
10019 e:reader(nil,o or"closed"); 10252 e:reader(nil,o or"closed");
10020 end 10253 end
10021 a[t]=nil; 10254 a[t]=nil;
10022 end 10255 end
10023 function i.onattach(e,t) 10256 function n.onattach(e,t)
10024 a[e]=t; 10257 a[e]=t;
10025 t.conn=e; 10258 t.conn=e;
10026 end 10259 end
10027 function i.ondetach(e) 10260 function n.ondetach(e)
10028 a[e]=nil; 10261 a[e]=nil;
10029 end 10262 end
10030 function i.onfail(e,t) 10263 function n.onfail(e,t)
10031 e.http.events.fire_event("request-connection-error",{http=e.http,request=e,url=e.url,err=t}); 10264 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); 10265 e.callback(t or"connection failed",0,e);
10033 end 10266 end
10034 local function f(a,s,t,n) 10267 local function m(a,h,t,i)
10035 local e=A.parse(s); 10268 local e=T.parse(h);
10036 if not(e and e.host)then 10269 if not(e and e.host)then
10037 n("invalid-url",0,e); 10270 i("invalid-url",0,e);
10038 return nil,"invalid-url"; 10271 return nil,"invalid-url";
10039 end 10272 end
10040 e.url=s; 10273 e.url=h;
10041 e.http=a; 10274 e.http=a;
10042 e.time=q(); 10275 e.time=w();
10043 if not e.path then 10276 if not e.path then
10044 e.path="/"; 10277 e.path="/";
10045 end 10278 end
10046 e.id=t and t.id or x(e); 10279 e.id=t and t.id or g(e);
10047 do 10280 do
10048 local o={http=a,url=s,request=e,options=t,callback=n}; 10281 local o={http=a,url=h,request=e,options=t,callback=i};
10049 local a=a.events.fire_event("pre-request",o); 10282 local a=a.events.fire_event("pre-request",o);
10050 if a then 10283 if a then
10051 return a; 10284 return a;
10052 end 10285 end
10053 e,s,t,e.callback=o.request,o.url,o.options,o.callback; 10286 e,h,t,e.callback=o.request,o.url,o.options,o.callback;
10054 end 10287 end
10055 local d,h,u; 10288 local u,s,d;
10056 local m,n=e.host,e.port; 10289 local m,i=e.host,e.port;
10057 local r=m; 10290 local r=m;
10058 if(n=="80"and e.scheme=="http") 10291 if(i=="80"and e.scheme=="http")
10059 or(n=="443"and e.scheme=="https")then 10292 or(i=="443"and e.scheme=="https")then
10060 n=nil; 10293 i=nil;
10061 elseif n then 10294 elseif i then
10062 r=r..":"..n; 10295 r=r..":"..i;
10063 end 10296 end
10064 h={ 10297 s={
10065 ["Host"]=r; 10298 ["Host"]=r;
10066 ["User-Agent"]="Prosody XMPP Server"; 10299 ["User-Agent"]="Prosody XMPP Server";
10067 }; 10300 };
10068 if e.userinfo then 10301 if e.userinfo then
10069 h["Authorization"]="Basic "..O(e.userinfo); 10302 s["Authorization"]="Basic "..z(e.userinfo);
10070 end 10303 end
10071 if t then 10304 if t then
10072 e.onlystatus=t.onlystatus; 10305 e.onlystatus=t.onlystatus;
10073 u=t.body; 10306 d=t.body;
10074 if u then 10307 if d then
10075 d="POST"; 10308 u="POST";
10076 h["Content-Length"]=c(#u); 10309 s["Content-Length"]=c(#d);
10077 h["Content-Type"]="application/x-www-form-urlencoded"; 10310 s["Content-Type"]="application/x-www-form-urlencoded";
10078 end 10311 end
10079 if t.method then d=t.method;end 10312 if t.method then u=t.method;end
10080 if t.headers then 10313 if t.headers then
10081 for t,e in l(t.headers)do 10314 for e,t in l(t.headers)do
10082 h[t]=e; 10315 s[e]=t;
10083 end 10316 end
10084 end 10317 end
10085 e.insecure=t.insecure; 10318 e.insecure=t.insecure;
10086 e.suppress_errors=t.suppress_errors; 10319 e.suppress_errors=t.suppress_errors;
10087 e.streaming_handler=t.streaming_handler; 10320 e.streaming_handler=t.streaming_handler;
10088 end 10321 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); 10322 o("debug","Making %s %s request '%s' to %s",e.scheme:upper(),u or"GET",e.id,(t and t.suppress_url and r)or h);
10090 e.method,e.headers,e.body=d,h,u; 10323 e.method,e.headers,e.body=u,s,d;
10091 local o=e.scheme=="https"; 10324 local o=e.scheme=="https";
10092 if o and not y then 10325 if o and not v then
10093 w("SSL not available, unable to contact https URL"); 10326 f("SSL not available, unable to contact https URL");
10094 end 10327 end
10095 local r=n and p(n)or(o and 443 or 80); 10328 local r=i and y(i)or(o and 443 or 80);
10096 local n=a.options and a.options.use_dane; 10329 local s=a.options and a.options.use_dane;
10097 local h=false; 10330 local i=false;
10098 if o then 10331 if o then
10099 h=t and t.sslctx or a.options and a.options.sslctx; 10332 i=t and t.sslctx or a.options and a.options.sslctx;
10100 if t and t.use_dane~=nil then 10333 if t and t.use_dane~=nil then
10101 n=t.use_dane; 10334 s=t.use_dane;
10102 end 10335 end
10103 end 10336 end
10104 local t=b.new(m,r,"tcp",{servername=e.host;use_dane=n}); 10337 local t=x.new(m,r,"tcp",{servername=e.host;use_dane=s});
10105 v(t,i,{sslctx=h},e); 10338 q(t,n,{sslctx=i},e);
10106 a.events.fire_event("request",{http=a,request=e,url=s}); 10339 a.events.fire_event("request",{http=a,request=e,url=h});
10107 return e; 10340 return e;
10108 end 10341 end
10109 local function e(t) 10342 local function e(a)
10110 local e={ 10343 local e={
10111 options=t; 10344 options=a;
10112 request=function(o,a,t,e) 10345 request=function(a,t,e,o)
10113 if e~=nil then 10346 if o~=nil then
10114 return f(o,a,t,e); 10347 return m(a,t,e,o);
10115 else 10348 else
10116 return k.new(function(i,n) 10349 return E.new(function(n,o)
10117 f(o,a,t,function(t,a,e,o) 10350 m(a,t,e,function(i,a,e,t)
10118 if a==0 then 10351 if a==0 then
10119 n(g.new(t,{request=e})); 10352 o(j.new(i,{request=e}));
10120 else 10353 else
10121 e.request=o; 10354 e.request=t;
10122 i(e); 10355 n(e);
10123 end 10356 end
10124 end); 10357 end);
10125 end); 10358 end);
10126 end 10359 end
10127 end; 10360 end;
10128 new=t and function(o) 10361 new=a and function(o)
10129 local a={}; 10362 local t={};
10130 for e,t in l(t)do a[e]=t;end 10363 for e,a in l(a)do t[e]=a;end
10131 if o then 10364 if o then
10132 for t,e in l(o)do a[t]=e;end 10365 for a,e in l(o)do t[a]=e;end
10133 end 10366 end
10134 return e(a); 10367 return e(t);
10135 end or e; 10368 end or e;
10136 events=T.new(); 10369 events=O.new();
10137 }; 10370 };
10138 return e; 10371 return e;
10139 end 10372 end
10140 local t=e({ 10373 local t=e({
10141 sslctx={mode="client",protocol="sslv23",options={"no_sslv2","no_sslv3"},alpn="http/1.1",verify="peer"}; 10374 sslctx={mode="client",protocol="sslv23",options={"no_sslv2","no_sslv3"},alpn="http/1.1",verify="peer"};
10142 suppress_errors=true; 10375 suppress_errors=true;
10143 }); 10376 });
10144 return{ 10377 return{
10145 request=function(e,a,o) 10378 request=function(o,a,e)
10146 return t:request(e,a,o); 10379 return t:request(o,a,e);
10147 end; 10380 end;
10148 default=t; 10381 default=t;
10149 new=e; 10382 new=e;
10150 events=t.events; 10383 events=t.events;
10151 urlencode=d.urlencode; 10384 urlencode=d.urlencode;
10170 _M=e; 10403 _M=e;
10171 return e; 10404 return e;
10172 end 10405 end
10173 local o=require"util.encodings".stringprep.nameprep; 10406 local o=require"util.encodings".stringprep.nameprep;
10174 local r=require"util.encodings".idna.to_ascii; 10407 local r=require"util.encodings".idna.to_ascii;
10175 local y=require"util.encodings".idna.to_unicode; 10408 local f=require"util.encodings".idna.to_unicode;
10176 local c=require"util.encodings".base64; 10409 local c=require"util.encodings".base64;
10177 local e=require"util.logger".init("x509"); 10410 local e=require"util.logger".init("x509");
10178 local p=require"util.multitable"; 10411 local y=require"util.multitable";
10179 local v=string.format; 10412 local p=string.format;
10180 local d=ipairs; 10413 local d=ipairs;
10181 local _ENV=nil; 10414 local _ENV=nil;
10182 local u="2.5.4.3"; 10415 local u="2.5.4.3";
10183 local h="2.5.29.17"; 10416 local h="2.5.29.17";
10184 local s="1.3.6.1.5.5.7.8.5"; 10417 local i="1.3.6.1.5.5.7.8.5";
10185 local n="1.3.6.1.5.5.7.8.7"; 10418 local n="1.3.6.1.5.5.7.8.7";
10186 local function l(o,a) 10419 local function l(o,a)
10187 local t=r(o) 10420 local t=r(o)
10188 if t==nil then 10421 if t==nil then
10189 e("info","Host %s failed IDNA ToASCII operation",o) 10422 e("info","Host %s failed IDNA ToASCII operation",o)
10190 return false 10423 return false
10191 end 10424 end
10192 t=t:lower() 10425 t=t:lower()
10193 local o=t:gsub("^[^.]+%.","") 10426 local i=t:gsub("^[^.]+%.","")
10194 for i=1,#a do 10427 for o=1,#a do
10195 local a=a[i] 10428 local a=a[o]
10196 if t==a:lower()then 10429 if t==a:lower()then
10197 e("debug","Cert dNSName %s matched hostname",a); 10430 e("debug","Cert dNSName %s matched hostname",a);
10198 return true 10431 return true
10199 end 10432 end
10200 if a:match("^%*%.")then 10433 if a:match("^%*%.")then
10201 local t=a:gsub("^[^.]+%.","") 10434 local t=a:gsub("^[^.]+%.","")
10202 if o==t:lower()then 10435 if i==t:lower()then
10203 e("debug","Cert dNSName %s matched hostname",a); 10436 e("debug","Cert dNSName %s matched hostname",a);
10204 return true 10437 return true
10205 end 10438 end
10206 end 10439 end
10207 end 10440 end
10225 end 10458 end
10226 end 10459 end
10227 end 10460 end
10228 return false 10461 return false
10229 end 10462 end
10230 local function f(i,o,t) 10463 local function m(i,o,t)
10231 local a=r(i) 10464 local a=r(i)
10232 if a==nil then 10465 if a==nil then
10233 e("info","Host %s failed IDNA ToASCII operation",i); 10466 e("info","Host %s failed IDNA ToASCII operation",i);
10234 return false 10467 return false
10235 end 10468 end
10256 end 10489 end
10257 end 10490 end
10258 end 10491 end
10259 return false 10492 return false
10260 end 10493 end
10261 local function m(o,a,i) 10494 local function v(a,o,s)
10262 if i.setencode then 10495 if s.setencode then
10263 i:setencode("utf8"); 10496 s:setencode("utf8");
10264 end 10497 end
10265 local t=i:extensions() 10498 local t=s:extensions()
10266 if t[h]then 10499 if t[h]then
10267 local e=t[h]; 10500 local e=t[h];
10268 local t=false 10501 local t=false
10269 if e[s]then 10502 if e[i]then
10270 t=true 10503 t=true
10271 if a=="_xmpp-client"or a=="_xmpp-server"then 10504 if o=="_xmpp-client"or o=="_xmpp-server"then
10272 if w(o,e[s])then return true end 10505 if w(a,e[i])then return true end
10273 end 10506 end
10274 end 10507 end
10275 if e[n]then 10508 if e[n]then
10276 t=true 10509 t=true
10277 if a and f(o,a,e[n])then return true end 10510 if o and m(a,o,e[n])then return true end
10278 end 10511 end
10279 if e["dNSName"]then 10512 if e["dNSName"]then
10280 t=true 10513 t=true
10281 if l(o,e["dNSName"])then return true end 10514 if l(a,e["dNSName"])then return true end
10282 end 10515 end
10283 if e["uniformResourceIdentifier"]then 10516 if e["uniformResourceIdentifier"]then
10284 t=true 10517 t=true
10285 end 10518 end
10286 if t then return false end 10519 if t then return false end
10287 end 10520 end
10288 local a=i:subject() 10521 local o=s:subject()
10289 local t=nil 10522 local t=nil
10290 for o=1,#a do 10523 for a=1,#o do
10291 local a=a[o] 10524 local a=o[a]
10292 if a["oid"]==u then 10525 if a["oid"]==u then
10293 if t then 10526 if t then
10294 e("info","Certificate has multiple common names") 10527 e("info","Certificate has multiple common names")
10295 return false 10528 return false
10296 end 10529 end
10297 t=a["value"]; 10530 t=a["value"];
10298 end 10531 end
10299 end 10532 end
10300 if t then 10533 if t then
10301 return l(o,{t}) 10534 return l(a,{t})
10302 end 10535 end
10303 return false 10536 return false
10304 end 10537 end
10305 local function l(i) 10538 local function l(s)
10306 if i.setencode then 10539 if s.setencode then
10307 i:setencode("utf8"); 10540 s:setencode("utf8");
10308 end 10541 end
10309 local a=p.new(); 10542 local a=y.new();
10310 local e=i:extensions(); 10543 local e=s:extensions();
10311 local t=e[h]; 10544 local t=e[h];
10312 if t then 10545 if t then
10313 if t["dNSName"]then 10546 if t["dNSName"]then
10314 for t,e in d(t["dNSName"])do 10547 for t,e in d(t["dNSName"])do
10315 local t=e:sub(1,2)=="*."; 10548 local t=e:sub(1,2)=="*.";
10316 if t then e=e:sub(3);end 10549 if t then e=e:sub(3);end
10317 e=y(o(e)); 10550 e=f(o(e));
10318 if e then 10551 if e then
10319 if t then e="*."..e;end 10552 if t then e="*."..e;end
10320 a:set(e,"*",true); 10553 a:set(e,"*",true);
10321 end 10554 end
10322 end 10555 end
10323 end 10556 end
10324 if t[s]then 10557 if t[i]then
10325 for t,e in d(t[s])do 10558 for t,e in d(t[i])do
10326 e=o(e); 10559 e=o(e);
10327 if e then 10560 if e then
10328 a:set(e,"xmpp-client",true); 10561 a:set(e,"xmpp-client",true);
10329 a:set(e,"xmpp-server",true); 10562 a:set(e,"xmpp-server",true);
10330 end 10563 end
10340 end 10573 end
10341 end 10574 end
10342 end 10575 end
10343 end 10576 end
10344 end 10577 end
10345 local e=i:subject(); 10578 local e=s:subject();
10346 for t=1,#e do 10579 for t=1,#e do
10347 local e=e[t]; 10580 local e=e[t];
10348 if e.oid==u then 10581 if e.oid==u then
10349 local e=o(e.value); 10582 local e=o(e.value);
10350 if e and r(e)then 10583 if e and r(e)then
10355 return a.data; 10588 return a.data;
10356 end 10589 end
10357 local t="%-%-%-%-%-BEGIN ([A-Z ]+)%-%-%-%-%-\r?\n".. 10590 local t="%-%-%-%-%-BEGIN ([A-Z ]+)%-%-%-%-%-\r?\n"..
10358 "([0-9A-Za-z+/=\r\n]*)\r?\n%-%-%-%-%-END %1%-%-%-%-%-"; 10591 "([0-9A-Za-z+/=\r\n]*)\r?\n%-%-%-%-%-END %1%-%-%-%-%-";
10359 local function a(e) 10592 local function a(e)
10360 local e,t=e:match(t); 10593 local t,e=e:match(t);
10361 if e and t then 10594 if t and e then
10362 return c.decode(t),e; 10595 return c.decode(e),t;
10363 end 10596 end
10364 end 10597 end
10365 local n=('.'):rep(64); 10598 local i=('.'):rep(64);
10366 local i="-----BEGIN %s-----\n%s\n-----END %s-----\n" 10599 local o="-----BEGIN %s-----\n%s\n-----END %s-----\n"
10367 local function o(t,e) 10600 local function n(t,e)
10368 e=e and e:upper()or"CERTIFICATE"; 10601 e=e and e:upper()or"CERTIFICATE";
10369 t=c.encode(t); 10602 t=c.encode(t);
10370 return v(i,e,t:gsub(n,'%0\n',(#t-1)/64),e); 10603 return p(o,e,t:gsub(i,'%0\n',(#t-1)/64),e);
10371 end 10604 end
10372 return{ 10605 return{
10373 verify_identity=m; 10606 verify_identity=v;
10374 get_identities=l; 10607 get_identities=l;
10375 pem2der=a; 10608 pem2der=a;
10376 der2pem=o; 10609 der2pem=n;
10377 }; 10610 };
10378 end) 10611 end)
10379 package.preload['verse.bosh']=(function(...) 10612 package.preload['verse.bosh']=(function(...)
10380 local _ENV=_ENV; 10613 local _ENV=_ENV;
10381 local function e(t,...) 10614 local function e(t,...)
10386 end 10619 end
10387 _ENV=e; 10620 _ENV=e;
10388 _M=e; 10621 _M=e;
10389 return e; 10622 return e;
10390 end 10623 end
10391 local n=require"util.xmppstream".new; 10624 local h=require"util.xmppstream".new;
10392 local o=require"util.stanza"; 10625 local i=require"util.stanza";
10393 require"net.httpclient_listener"; 10626 require"net.httpclient_listener";
10394 local a=require"net.http"; 10627 local o=require"net.http";
10395 local e=setmetatable({},{__index=verse.stream_mt}); 10628 local e=setmetatable({},{__index=verse.stream_mt});
10396 e.__index=e; 10629 e.__index=e;
10397 local h="http://etherx.jabber.org/streams"; 10630 local s="http://etherx.jabber.org/streams";
10398 local s="http://jabber.org/protocol/httpbind"; 10631 local n="http://jabber.org/protocol/httpbind";
10399 local t=5; 10632 local a=5;
10400 function verse.new_bosh(a,t) 10633 function verse.new_bosh(a,t)
10401 local t={ 10634 local t={
10402 bosh_conn_pool={}; 10635 bosh_conn_pool={};
10403 bosh_waiting_requests={}; 10636 bosh_waiting_requests={};
10404 bosh_rid=math.random(1,999999); 10637 bosh_rid=math.random(1,999999);
10416 function e:connect() 10649 function e:connect()
10417 self:_send_session_request(); 10650 self:_send_session_request();
10418 end 10651 end
10419 function e:send(e) 10652 function e:send(e)
10420 self:debug("Putting into BOSH send buffer: %s",tostring(e)); 10653 self:debug("Putting into BOSH send buffer: %s",tostring(e));
10421 self.bosh_outgoing_buffer[#self.bosh_outgoing_buffer+1]=o.clone(e); 10654 self.bosh_outgoing_buffer[#self.bosh_outgoing_buffer+1]=i.clone(e);
10422 self:flush(); 10655 self:flush();
10423 end 10656 end
10424 function e:flush() 10657 function e:flush()
10425 if self.connected 10658 if self.connected
10426 and#self.bosh_waiting_requests<self.bosh_max_requests 10659 and#self.bosh_waiting_requests<self.bosh_max_requests
10427 and(#self.bosh_waiting_requests==0 10660 and(#self.bosh_waiting_requests==0
10428 or#self.bosh_outgoing_buffer>0 10661 or#self.bosh_outgoing_buffer>0
10429 or self.bosh_need_restart)then 10662 or self.bosh_need_restart)then
10430 self:debug("Flushing..."); 10663 self:debug("Flushing...");
10431 local e=self:_make_body(); 10664 local t=self:_make_body();
10432 local t=self.bosh_outgoing_buffer; 10665 local e=self.bosh_outgoing_buffer;
10433 for o,a in ipairs(t)do 10666 for o,a in ipairs(e)do
10434 e:add_child(a); 10667 t:add_child(a);
10435 t[o]=nil; 10668 e[o]=nil;
10436 end 10669 end
10437 self:_make_request(e); 10670 self:_make_request(t);
10438 else 10671 else
10439 self:debug("Decided not to flush."); 10672 self:debug("Decided not to flush.");
10440 end 10673 end
10441 end 10674 end
10442 function e:_make_request(o) 10675 function e:_make_request(i)
10443 local e,t=a.request(self.bosh_url,{body=tostring(o)},function(i,e,a) 10676 local e,t=o.request(self.bosh_url,{body=tostring(i)},function(o,e,t)
10444 if e~=0 then 10677 if e~=0 then
10445 self.inactive_since=nil; 10678 self.inactive_since=nil;
10446 return self:_handle_response(i,e,a); 10679 return self:_handle_response(o,e,t);
10447 end 10680 end
10448 local e=os.time(); 10681 local e=os.time();
10449 if not self.inactive_since then 10682 if not self.inactive_since then
10450 self.inactive_since=e; 10683 self.inactive_since=e;
10451 elseif e-self.inactive_since>self.bosh_max_inactivity then 10684 elseif e-self.inactive_since>self.bosh_max_inactivity then
10452 return self:_disconnected(); 10685 return self:_disconnected();
10453 else 10686 else
10454 self:debug("%d seconds left to reconnect, retrying in %d seconds...", 10687 self:debug("%d seconds left to reconnect, retrying in %d seconds...",
10455 self.bosh_max_inactivity-(e-self.inactive_since),t); 10688 self.bosh_max_inactivity-(e-self.inactive_since),a);
10456 end 10689 end
10457 timer.add_task(t,function() 10690 timer.add_task(a,function()
10458 self:debug("Retrying request..."); 10691 self:debug("Retrying request...");
10459 for t,e in ipairs(self.bosh_waiting_requests)do 10692 for e,a in ipairs(self.bosh_waiting_requests)do
10460 if e==a then 10693 if a==t then
10461 table.remove(self.bosh_waiting_requests,t); 10694 table.remove(self.bosh_waiting_requests,e);
10462 break; 10695 break;
10463 end 10696 end
10464 end 10697 end
10465 self:_make_request(o); 10698 self:_make_request(i);
10466 end); 10699 end);
10467 end); 10700 end);
10468 if e then 10701 if e then
10469 table.insert(self.bosh_waiting_requests,e); 10702 table.insert(self.bosh_waiting_requests,e);
10470 else 10703 else
10482 e.attr["xml:lang"]="en"; 10715 e.attr["xml:lang"]="en";
10483 e.attr.ver="1.6"; 10716 e.attr.ver="1.6";
10484 e.attr.from=self.jid; 10717 e.attr.from=self.jid;
10485 e.attr.to=self.host; 10718 e.attr.to=self.host;
10486 e.attr.secure='true'; 10719 e.attr.secure='true';
10487 a.request(self.bosh_url,{body=tostring(e)},function(e,t) 10720 o.request(self.bosh_url,{body=tostring(e)},function(e,t)
10488 if t==0 then 10721 if t==0 then
10489 return self:_disconnected(); 10722 return self:_disconnected();
10490 end 10723 end
10491 local e=self:_parse_response(e) 10724 local e=self:_parse_response(e)
10492 if not e then 10725 if not e then
10502 self.connected=true; 10735 self.connected=true;
10503 self:event("connected"); 10736 self:event("connected");
10504 self:_handle_response_payload(e); 10737 self:_handle_response_payload(e);
10505 end); 10738 end);
10506 end 10739 end
10507 function e:_handle_response(a,t,e) 10740 function e:_handle_response(t,a,e)
10508 if self.bosh_waiting_requests[1]~=e then 10741 if self.bosh_waiting_requests[1]~=e then
10509 self:warn("Server replied to request that wasn't the oldest"); 10742 self:warn("Server replied to request that wasn't the oldest");
10510 for t,a in ipairs(self.bosh_waiting_requests)do 10743 for a,t in ipairs(self.bosh_waiting_requests)do
10511 if a==e then 10744 if t==e then
10512 self.bosh_waiting_requests[t]=nil; 10745 self.bosh_waiting_requests[a]=nil;
10513 break; 10746 break;
10514 end 10747 end
10515 end 10748 end
10516 else 10749 else
10517 table.remove(self.bosh_waiting_requests,1); 10750 table.remove(self.bosh_waiting_requests,1);
10518 end 10751 end
10519 local e=self:_parse_response(a); 10752 local e=self:_parse_response(t);
10520 if e then 10753 if e then
10521 self:_handle_response_payload(e); 10754 self:_handle_response_payload(e);
10522 end 10755 end
10523 self:flush(); 10756 self:flush();
10524 end 10757 end
10525 function e:_handle_response_payload(t) 10758 function e:_handle_response_payload(t)
10526 local e=t.tags; 10759 local e=t.tags;
10527 for t=1,#e do 10760 for t=1,#e do
10528 local e=e[t]; 10761 local e=e[t];
10529 if e.attr.xmlns==h then 10762 if e.attr.xmlns==s then
10530 self:event("stream-"..e.name,e); 10763 self:event("stream-"..e.name,e);
10531 elseif e.attr.xmlns then 10764 elseif e.attr.xmlns then
10532 self:event("stream/"..e.attr.xmlns,e); 10765 self:event("stream/"..e.attr.xmlns,e);
10533 else 10766 else
10534 self:event("stanza",e); 10767 self:event("stanza",e);
10540 end 10773 end
10541 local a={ 10774 local a={
10542 stream_ns="http://jabber.org/protocol/httpbind",stream_tag="body", 10775 stream_ns="http://jabber.org/protocol/httpbind",stream_tag="body",
10543 default_ns="jabber:client", 10776 default_ns="jabber:client",
10544 streamopened=function(e,t)e.notopen=nil;e.payload=verse.stanza("body",t);return true;end; 10777 streamopened=function(e,t)e.notopen=nil;e.payload=verse.stanza("body",t);return true;end;
10545 handlestanza=function(e,t)e.payload:add_child(t);end; 10778 handlestanza=function(t,e)t.payload:add_child(e);end;
10546 }; 10779 };
10547 function e:_parse_response(e) 10780 function e:_parse_response(e)
10548 self:debug("Parsing response: %s",e); 10781 self:debug("Parsing response: %s",e);
10549 if e==nil then 10782 if e==nil then
10550 self:debug("%s",debug.traceback()); 10783 self:debug("%s",debug.traceback());
10551 self:_disconnected(); 10784 self:_disconnected();
10552 return; 10785 return;
10553 end 10786 end
10554 local t={notopen=true,stream=self}; 10787 local t={notopen=true,stream=self};
10555 local a=n(t,a); 10788 local a=h(t,a);
10556 a:feed(e); 10789 a:feed(e);
10557 return t.payload; 10790 return t.payload;
10558 end 10791 end
10559 function e:_make_body() 10792 function e:_make_body()
10560 self.bosh_rid=self.bosh_rid+1; 10793 self.bosh_rid=self.bosh_rid+1;
10561 local e=verse.stanza("body",{ 10794 local e=verse.stanza("body",{
10562 xmlns=s; 10795 xmlns=n;
10563 content="text/xml; charset=utf-8"; 10796 content="text/xml; charset=utf-8";
10564 sid=self.bosh_sid; 10797 sid=self.bosh_sid;
10565 rid=self.bosh_rid; 10798 rid=self.bosh_rid;
10566 }); 10799 });
10567 if self.bosh_need_restart then 10800 if self.bosh_need_restart then
10583 _M=e; 10816 _M=e;
10584 return e; 10817 return e;
10585 end 10818 end
10586 local t=require"verse"; 10819 local t=require"verse";
10587 local i=t.stream_mt; 10820 local i=t.stream_mt;
10588 local d=require"util.jid".split; 10821 local s=require"util.jid".split;
10589 local r=require"net.adns"; 10822 local h=require"net.adns";
10590 local a=require"util.stanza"; 10823 local a=require"util.stanza";
10591 local o=require"util.id".short; 10824 local o=require"util.id".short;
10592 math.randomseed((require"socket".gettime()*1e6)%2147483648); 10825 math.randomseed((require"socket".gettime()*1e6)%2147483648);
10593 t.message,t.presence,t.iq,t.stanza,t.reply,t.error_reply= 10826 t.message,t.presence,t.iq,t.stanza,t.reply,t.error_reply=
10594 a.message,a.presence,a.iq,a.stanza,a.reply,a.error_reply; 10827 a.message,a.presence,a.iq,a.stanza,a.reply,a.error_reply;
10596 if not e.id then 10829 if not e.id then
10597 e.id=o(); 10830 e.id=o();
10598 end 10831 end
10599 return a.iq(e); 10832 return a.iq(e);
10600 end 10833 end
10601 local s=require"util.xmppstream".new; 10834 local r=require"util.xmppstream".new;
10602 local n="http://etherx.jabber.org/streams"; 10835 local n="http://etherx.jabber.org/streams";
10603 local function h(e,t) 10836 local function d(e,t)
10604 if e.priority==t.priority then 10837 if e.priority==t.priority then
10605 if not e.weight_r then 10838 if not e.weight_r then
10606 e.weight_r=math.random(); 10839 e.weight_r=math.random();
10607 end 10840 end
10608 if not t.weight_r then 10841 if not t.weight_r then
10653 end 10886 end
10654 function i:reset() 10887 function i:reset()
10655 if self.stream then 10888 if self.stream then
10656 self.stream:reset(); 10889 self.stream:reset();
10657 else 10890 else
10658 self.stream=s(self,o); 10891 self.stream=r(self,o);
10659 end 10892 end
10660 self.notopen=true; 10893 self.notopen=true;
10661 return true; 10894 return true;
10662 end 10895 end
10663 function i:connect_client(e,i,a,o) 10896 function i:connect_client(e,a,i,o)
10664 self.jid,self.password=e,i; 10897 self.jid,self.password=e,a;
10665 self.client_key,self.server_key=a,o; 10898 self.client_key,self.server_key=i,o;
10666 self.username,self.host,self.resource=d(e); 10899 self.username,self.host,self.resource=s(e);
10667 self:add_plugin("tls"); 10900 self:add_plugin("tls");
10668 self:add_plugin("sasl"); 10901 self:add_plugin("sasl");
10669 self:add_plugin("bind"); 10902 self:add_plugin("bind");
10670 function self.data(t,e) 10903 function self.data(t,e)
10671 local t,a=self.stream:feed(e); 10904 local a,t=self.stream:feed(e);
10672 if t then return;end 10905 if a then return;end
10673 self:debug("Received invalid XML (%s) %d bytes: %s",tostring(a),#e,e:sub(1,300):gsub("[\r\n]+"," ")); 10906 self:debug("Received invalid XML (%s) %d bytes: %s",tostring(t),#e,e:sub(1,300):gsub("[\r\n]+"," "));
10674 self:close("xml-not-well-formed"); 10907 self:close("xml-not-well-formed");
10675 end 10908 end
10676 self:hook("connected",function()self:reopen();end); 10909 self:hook("connected",function()self:reopen();end);
10677 self:hook("incoming-raw",function(e)return self.data(self.conn,e);end); 10910 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); 10911 self:hook("read-timeout",function()self:send(" ");return true;end,-1);
10720 local function e() 10953 local function e()
10721 self:event("ready"); 10954 self:event("ready");
10722 end 10955 end
10723 self:hook("session-success",e,-1) 10956 self:hook("session-success",e,-1)
10724 self:hook("bind-success",e,-1); 10957 self:hook("bind-success",e,-1);
10725 local e=self.close; 10958 local t=self.close;
10726 function self:close(t) 10959 function self:close(e)
10727 self.close=e; 10960 self.close=t;
10728 if not self.closed then 10961 if not self.closed then
10729 self:send("</stream:stream>"); 10962 self:send("</stream:stream>");
10730 self.closed=true; 10963 self.closed=true;
10731 else 10964 else
10732 return self:close(t); 10965 return self:close(e);
10733 end 10966 end
10734 end 10967 end
10735 local function a() 10968 local function t()
10736 self:connect(self.connect_host or self.host,self.connect_port or 5222); 10969 self:connect(self.connect_host or self.host,self.connect_port or 5222);
10737 end 10970 end
10738 if not(self.connect_host or self.connect_port)then 10971 if not(self.connect_host or self.connect_port)then
10739 r.lookup(function(t) 10972 h.lookup(function(a)
10740 if t then 10973 if a then
10741 local e={}; 10974 local e={};
10742 self.srv_hosts=e; 10975 self.srv_hosts=e;
10743 for a,t in ipairs(t)do 10976 for a,t in ipairs(a)do
10744 table.insert(e,t.srv); 10977 table.insert(e,t.srv);
10745 end 10978 end
10746 table.sort(e,h); 10979 table.sort(e,d);
10747 local t=e[1]; 10980 local a=e[1];
10748 self.srv_choice=1; 10981 self.srv_choice=1;
10749 if t then 10982 if a then
10750 self.connect_host,self.connect_port=t.target,t.port; 10983 self.connect_host,self.connect_port=a.target,a.port;
10751 self:debug("Best record found, will connect to %s:%d",self.connect_host or self.host,self.connect_port or 5222); 10984 self:debug("Best record found, will connect to %s:%d",self.connect_host or self.host,self.connect_port or 5222);
10752 end 10985 end
10753 self:hook("disconnected",function() 10986 self:hook("disconnected",function()
10754 if self.srv_hosts and self.srv_choice<#self.srv_hosts then 10987 if self.srv_hosts and self.srv_choice<#self.srv_hosts then
10755 self.srv_choice=self.srv_choice+1; 10988 self.srv_choice=self.srv_choice+1;
10756 local e=e[self.srv_choice]; 10989 local e=e[self.srv_choice];
10757 self.connect_host,self.connect_port=e.target,e.port; 10990 self.connect_host,self.connect_port=e.target,e.port;
10758 a(); 10991 t();
10759 return true; 10992 return true;
10760 end 10993 end
10761 end,1e3); 10994 end,1e3);
10762 self:hook("connected",function() 10995 self:hook("connected",function()
10763 self.srv_hosts=nil; 10996 self.srv_hosts=nil;
10764 end,1e3); 10997 end,1e3);
10765 end 10998 end
10766 a(); 10999 t();
10767 end,"_xmpp-client._tcp."..(self.host)..".","SRV"); 11000 end,"_xmpp-client._tcp."..(self.host)..".","SRV");
10768 else 11001 else
10769 a(); 11002 t();
10770 end 11003 end
10771 end 11004 end
10772 function i:reopen() 11005 function i:reopen()
10773 self:reset(); 11006 self:reset();
10774 self:send(a.stanza("stream:stream",{to=self.host,["xmlns:stream"]='http://etherx.jabber.org/streams', 11007 self:send(a.stanza("stream:stream",{to=self.host,["xmlns:stream"]='http://etherx.jabber.org/streams',
10791 end 11024 end
10792 _ENV=e; 11025 _ENV=e;
10793 _M=e; 11026 _M=e;
10794 return e; 11027 return e;
10795 end 11028 end
10796 local a=require"verse"; 11029 local t=require"verse";
10797 local t=a.stream_mt; 11030 local s=t.stream_mt;
10798 local d=require"util.jid".split; 11031 local r=require"util.jid".split;
10799 local e=require"lxp"; 11032 local a=require"util.stanza";
10800 local o=require"util.stanza"; 11033 local h=require"util.hashes".sha1;
10801 local r=require"util.hashes".sha1; 11034 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= 11035 a.message,a.presence,a.iq,a.stanza,a.reply,a.error_reply;
10803 o.message,o.presence,o.iq,o.stanza,o.reply,o.error_reply; 11036 local d=require"util.xmppstream".new;
10804 local h=require"util.xmppstream".new; 11037 local n="http://etherx.jabber.org/streams";
10805 local s="http://etherx.jabber.org/streams"; 11038 local o="jabber:component:accept";
10806 local i="jabber:component:accept"; 11039 local i={
10807 local n={ 11040 stream_ns=n,
10808 stream_ns=s,
10809 stream_tag="stream", 11041 stream_tag="stream",
10810 default_ns=i}; 11042 default_ns=o};
10811 function n.streamopened(e,t) 11043 function i.streamopened(e,t)
10812 e.stream_id=t.id; 11044 e.stream_id=t.id;
10813 if not e:event("opened",t)then 11045 if not e:event("opened",t)then
10814 e.notopen=nil; 11046 e.notopen=nil;
10815 end 11047 end
10816 return true; 11048 return true;
10817 end 11049 end
10818 function n.streamclosed(e) 11050 function i.streamclosed(e)
10819 return e:event("closed"); 11051 return e:event("closed");
10820 end 11052 end
10821 function n.handlestanza(t,e) 11053 function i.handlestanza(t,e)
10822 if e.attr.xmlns==s then 11054 if e.attr.xmlns==n then
10823 return t:event("stream-"..e.name,e); 11055 return t:event("stream-"..e.name,e);
10824 elseif e.attr.xmlns or e.name=="handshake"then 11056 elseif e.attr.xmlns or e.name=="handshake"then
10825 return t:event("stream/"..(e.attr.xmlns or i),e); 11057 return t:event("stream/"..(e.attr.xmlns or o),e);
10826 end 11058 end
10827 return t:event("stanza",e); 11059 return t:event("stanza",e);
10828 end 11060 end
10829 function t:reset() 11061 function s:connect_component(e,n)
11062 self.jid,self.password=e,n;
11063 self.username,self.host,self.resource=r(e);
11064 function self:reset()
10830 if self.stream then 11065 if self.stream then
10831 self.stream:reset(); 11066 self.stream:reset();
10832 else 11067 else
10833 self.stream=h(self,n); 11068 self.stream=d(self,i);
10834 end 11069 end
10835 self.notopen=true; 11070 self.notopen=true;
10836 return true; 11071 return true;
10837 end 11072 end
10838 function t:connect_component(e,n) 11073 function self:reopen()
10839 self.jid,self.password=e,n; 11074 self:reset();
10840 self.username,self.host,self.resource=d(e); 11075 self:send(a.stanza("stream:stream",{to=self.jid,["xmlns:stream"]='http://etherx.jabber.org/streams',
10841 function self.data(a,e) 11076 xmlns=o,version="1.0"}):top_tag());
10842 local a,o=self.stream:feed(e); 11077 end
11078 function self:close(e)
11079 if not self.notopen then
11080 self:send("</stream:stream>");
11081 end
11082 local t=self.conn.disconnect();
11083 self.conn:close();
11084 t(conn,e);
11085 end
11086 function self:send_iq(t,a)
11087 local e=self:new_id();
11088 self.tracked_iqs[e]=a;
11089 t.attr.id=e;
11090 self:send(t);
11091 end
11092 function self:new_id()
11093 self.curr_id=self.curr_id+1;
11094 return tostring(self.curr_id);
11095 end
11096 function self.data(t,e)
11097 local a,t=self.stream:feed(e);
10843 if a then return;end 11098 if a then return;end
10844 t:debug("Received invalid XML (%s) %d bytes: %s",tostring(o),#e,e:sub(1,300):gsub("[\r\n]+"," ")); 11099 stream:debug("Received invalid XML (%s) %d bytes: %s",tostring(t),#e,e:sub(1,300):gsub("[\r\n]+"," "));
10845 t:close("xml-not-well-formed"); 11100 stream:close("xml-not-well-formed");
10846 end 11101 end
10847 self:hook("incoming-raw",function(e)return self.data(self.conn,e);end); 11102 self:hook("incoming-raw",function(e)return self.data(self.conn,e);end);
10848 self.curr_id=0; 11103 self.curr_id=0;
10849 self.tracked_iqs={}; 11104 self.tracked_iqs={};
10850 self:hook("stanza",function(t) 11105 self:hook("stanza",function(e)
10851 local e,a=t.attr.id,t.attr.type; 11106 local t,a=e.attr.id,e.attr.type;
10852 if e and t.name=="iq"and(a=="result"or a=="error")and self.tracked_iqs[e]then 11107 if t and e.name=="iq"and(a=="result"or a=="error")and self.tracked_iqs[t]then
10853 self.tracked_iqs[e](t); 11108 self.tracked_iqs[t](e);
10854 self.tracked_iqs[e]=nil; 11109 self.tracked_iqs[t]=nil;
10855 return true; 11110 return true;
10856 end 11111 end
10857 end); 11112 end);
10858 self:hook("stanza",function(e) 11113 self:hook("stanza",function(e)
10859 local t; 11114 local a;
10860 if e.attr.xmlns==nil or e.attr.xmlns=="jabber:client"then 11115 if e.attr.xmlns==nil or e.attr.xmlns=="jabber:client"then
10861 if e.name=="iq"and(e.attr.type=="get"or e.attr.type=="set")then 11116 if e.name=="iq"and(e.attr.type=="get"or e.attr.type=="set")then
10862 local o=e.tags[1]and e.tags[1].attr.xmlns; 11117 local o=e.tags[1]and e.tags[1].attr.xmlns;
10863 if o then 11118 if o then
10864 t=self:event("iq/"..o,e); 11119 a=self:event("iq/"..o,e);
10865 if not t then 11120 if not a then
10866 t=self:event("iq",e); 11121 a=self:event("iq",e);
10867 end 11122 end
10868 end 11123 end
10869 if t==nil then 11124 if a==nil then
10870 self:send(a.error_reply(e,"cancel","service-unavailable")); 11125 self:send(t.error_reply(e,"cancel","service-unavailable"));
10871 return true; 11126 return true;
10872 end 11127 end
10873 else 11128 else
10874 t=self:event(e.name,e); 11129 a=self:event(e.name,e);
10875 end 11130 end
10876 end 11131 end
10877 return t; 11132 return a;
10878 end,-1); 11133 end,-1);
10879 self:hook("opened",function(e) 11134 self:hook("opened",function(e)
10880 print(self.jid,self.stream_id,e.id); 11135 print(self.jid,self.stream_id,e.id);
10881 local e=r(self.stream_id..n,true); 11136 local e=h(self.stream_id..n,true);
10882 self:send(o.stanza("handshake",{xmlns=i}):text(e)); 11137 self:send(a.stanza("handshake",{xmlns=o}):text(e));
10883 self:hook("stream/"..i,function(e) 11138 self:hook("stream/"..o,function(e)
10884 if e.name=="handshake"then 11139 if e.name=="handshake"then
10885 self:event("authentication-success"); 11140 self:event("authentication-success");
10886 end 11141 end
10887 end); 11142 end);
10888 end); 11143 end);
10890 self:event("ready"); 11145 self:event("ready");
10891 end 11146 end
10892 self:hook("authentication-success",e,-1); 11147 self:hook("authentication-success",e,-1);
10893 self:connect(self.connect_host or self.host,self.connect_port or 5347); 11148 self:connect(self.connect_host or self.host,self.connect_port or 5347);
10894 self:reopen(); 11149 self:reopen();
10895 end
10896 function t:reopen()
10897 self:reset();
10898 self:send(o.stanza("stream:stream",{to=self.jid,["xmlns:stream"]='http://etherx.jabber.org/streams',
10899 xmlns=i,version="1.0"}):top_tag());
10900 end
10901 function t:close(t)
10902 if not self.notopen then
10903 self:send("</stream:stream>");
10904 end
10905 local e=self.conn.disconnect();
10906 self.conn:close();
10907 e(conn,t);
10908 end
10909 function t:send_iq(e,a)
10910 local t=self:new_id();
10911 self.tracked_iqs[t]=a;
10912 e.attr.id=t;
10913 self:send(e);
10914 end
10915 function t:new_id()
10916 self.curr_id=self.curr_id+1;
10917 return tostring(self.curr_id);
10918 end 11150 end
10919 end) 11151 end)
10920 pcall(require,"luarocks.require"); 11152 pcall(require,"luarocks.require");
10921 local h=require"socket"; 11153 local h=require"socket";
10922 pcall(require,"ssl"); 11154 pcall(require,"ssl");
10938 end 11170 end
10939 end 11171 end
10940 return e; 11172 return e;
10941 end 11173 end
10942 local i=0; 11174 local i=0;
10943 function e.new(o,a) 11175 function e.new(a,o)
10944 local t=setmetatable(a or{},t); 11176 local t=setmetatable(o or{},t);
10945 i=i+1; 11177 i=i+1;
10946 t.id=tostring(i); 11178 t.id=tostring(i);
10947 t.logger=o or e.new_logger("stream"..t.id); 11179 t.logger=a or e.new_logger("stream"..t.id);
10948 t.events=s.new(); 11180 t.events=s.new();
10949 t.plugins={}; 11181 t.plugins={};
10950 t.verse=e; 11182 t.verse=e;
10951 return t; 11183 return t;
10952 end 11184 end
10962 function e(a,t,e) 11194 function e(a,t,e)
10963 o:write(a,"\t",t,"\t",e,"\n"); 11195 o:write(a,"\t",t,"\t",e,"\n");
10964 end 11196 end
10965 end 11197 end
10966 if e then 11198 if e then
10967 local function a(t,a,o,...) 11199 local function i(o,a,t,...)
10968 return e(t,a,n(o,...)); 11200 return e(o,a,n(t,...));
10969 end 11201 end
10970 for t,e in ipairs(t)do 11202 for t,e in ipairs(t)do
10971 o.add_level_sink(e,a); 11203 o.add_level_sink(e,i);
10972 end 11204 end
10973 end 11205 end
10974 end 11206 end
10975 function e._default_log_handler(a,o,t) 11207 function e._default_log_handler(o,t,a)
10976 return io.stderr:write(a,"\t",o,"\t",t,"\n"); 11208 return io.stderr:write(o,"\t",t,"\t",a,"\n");
10977 end 11209 end
10978 e.set_log_handler(e._default_log_handler,{"error"}); 11210 e.set_log_handler(e._default_log_handler,{"error"});
10979 local function o(t) 11211 local function o(t)
10980 e.log("error","Error: %s",t); 11212 e.log("error","Error: %s",t);
10981 e.log("error","Traceback: %s",debug.traceback()); 11213 e.log("error","Traceback: %s",debug.traceback());
11060 return self.events.fire_event(e,...); 11292 return self.events.fire_event(e,...);
11061 end 11293 end
11062 function t:hook(e,...) 11294 function t:hook(e,...)
11063 return self.events.add_handler(e,...); 11295 return self.events.add_handler(e,...);
11064 end 11296 end
11065 function t:unhook(e,t) 11297 function t:unhook(t,e)
11066 return self.events.remove_handler(e,t); 11298 return self.events.remove_handler(t,e);
11067 end 11299 end
11068 function e.eventable(e) 11300 function e.eventable(e)
11069 e.events=s.new(); 11301 e.events=s.new();
11070 e.hook,e.unhook=t.hook,t.unhook; 11302 e.hook,e.unhook=t.hook,t.unhook;
11071 local t=e.events.fire_event; 11303 local t=e.events.fire_event;

mercurial