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; |
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={["'"]="'",["\""]=""",["<"]="<",[">"]=">",["&"]="&"}; |
770 local u={["'"]="'",["\""]=""",["<"]="<",[">"]=">",["&"]="&"}; |
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).."'"); |
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(...) |
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); |
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 |
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 |
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() |
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,...) |
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,...) |
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", |
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 |
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; |
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 |
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); |
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) |
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) |
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; |
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; |
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); |
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 |
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, |
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; |
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") |
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) |
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 |
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) |
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,...) |
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(); |
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,...) |
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 |
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) |
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 |
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 |
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 |
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 |
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); |