Thu, 23 Mar 2023 15:16:00 +0000
verse: Update bundled verse version
package.preload['util.encodings']=(function(...) local _ENV=_ENV; local function a(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local function e() error("Function not implemented"); end local e=require"mime"; a"encodings" idna={}; stringprep={}; base64={encode=e.b64,decode=e.unb64}; utf8={ valid=(utf8 and utf8.len)and function(e)return not not utf8.len(e);end or function()return true;end; }; return _M; end) package.preload['util.sha1']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local m=string.len local o=string.char local k=string.byte local q=string.sub local c=math.floor local t=require"bit" local j=t.bnot local e=t.band local y=t.bor local n=t.bxor local a=t.lshift local i=t.rshift local u,r,l,d,h local function p(t,e) return a(t,e)+i(t,32-e) end local function s(i) local t,a local t="" for n=1,8 do a=e(i,15) if(a<10)then t=o(a+48)..t else t=o(a+87)..t end i=c(i/16) end return t end local function g(t) local i,a local n="" i=m(t)*8 t=t..o(128) a=56-e(m(t),63) if(a<0)then a=a+64 end for e=1,a do t=t..o(0) end for t=1,8 do n=o(e(i,255))..n i=c(i/256) end return t..n end local function b(w) local m,t,o,a,f,s,c,v local i,i local i={} while(w~="")do for e=0,15 do i[e]=0 for t=1,4 do i[e]=i[e]*256+k(w,e*4+t) end end for e=16,79 do i[e]=p(n(n(i[e-3],i[e-8]),n(i[e-14],i[e-16])),1) end m=u t=r o=l a=d f=h for h=0,79 do if(h<20)then s=y(e(t,o),e(j(t),a)) c=1518500249 elseif(h<40)then s=n(n(t,o),a) c=1859775393 elseif(h<60)then s=y(y(e(t,o),e(t,a)),e(o,a)) c=2400959708 else s=n(n(t,o),a) c=3395469782 end v=p(m,5)+s+f+c+i[h] f=a a=o o=p(t,30) t=m m=v end u=e(u+m,4294967295) r=e(r+t,4294967295) l=e(l+o,4294967295) d=e(d+a,4294967295) h=e(h+f,4294967295) w=q(w,65) end end local function a(e,t) e=g(e) u=1732584193 r=4023233417 l=2562383102 d=271733878 h=3285377520 b(e) local e=s(u)..s(r)..s(l) ..s(d)..s(h); if t then return e; else return(e:gsub("..",function(e) return string.char(tonumber(e,16)); end)); end end _G.sha1={sha1=a}; return _G.sha1; end) package.preload['lib.adhoc']=(function(...) local _ENV=_ENV; local function d(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local s,l=require"util.stanza",require"util.id".short; local r="http://jabber.org/protocol/commands"; local n={} local h={}; local function i(o,e,t,a) local e=s.stanza("command",{xmlns=r,node=o.node,status=e}); if t then e.attr.sessionid=t;end if a then e.attr.action=a;end return e; end function h.new(e,o,a,t) return{name=e,node=o,handler=a,cmdtag=i,permission=(t or"user")}; end function h.handle_cmd(o,h,t) local e=t.tags[1].attr.sessionid or l(); local a={}; a.to=t.attr.to; a.from=t.attr.from; a.action=t.tags[1].attr.action or"execute"; a.form=t.tags[1]:child_with_ns("jabber:x:data"); local a,i=o:handler(a,n[e]); n[e]=i; local i=s.reply(t); local t; if a.status=="completed"then n[e]=nil; t=o:cmdtag("completed",e); elseif a.status=="canceled"then n[e]=nil; t=o:cmdtag("canceled",e); elseif a.status=="error"then n[e]=nil; i=s.error_reply(i,a.error.type,a.error.condition,a.error.message); h.send(i); return true; else t=o:cmdtag("executing",e); end for a,e in pairs(a)do if a=="info"then t:tag("note",{type="info"}):text(e):up(); elseif a=="warn"then t:tag("note",{type="warn"}):text(e):up(); elseif a=="error"then t:tag("note",{type="error"}):text(e.message):up(); elseif a=="actions"then local a=s.stanza("actions"); for i,e in ipairs(e)do if(e=="prev")or(e=="next")or(e=="complete")then a:tag(e):up(); else d:log("error",'Command "'..o.name.. '" at node "'..o.node..'" provided an invalid action "'..e..'"'); end end t:add_child(a); elseif a=="form"then t:add_child((e.layout or e):form(e.values)); elseif a=="result"then t:add_child((e.layout or e):form(e.values,"result")); elseif a=="other"then t:add_child(e); end end i:add_child(t); h.send(i); return true; end return h; end) package.preload['util.table']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end return{pack=function(...)return{n=select("#",...);...}end;create=function()return{}end} end) package.preload['util.stanza']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=error; local o=table.insert; local l=table.remove; local m=table.concat; local d=string.match; local f=tostring; local u=setmetatable; local x=getmetatable; local h=pairs; local s=ipairs; local t=type; local j=string.gsub; local y=string.sub; local r=string.find; local c=table.move or require"util.table".move; local b=require"util.table".create; local v=require"util.encodings".utf8.valid; local q,p=pcall(require,"util.termcolours"); local g="urn:ietf:params:xml:ns:xmpp-stanzas"; local k={xmlns=g}; local _ENV=nil; local e={__name="stanza"}; e.__index=e; local function i(t,e) return not r(t,e and"[^\1\9\10\13\20-\255]"or"[^\9\10\13\20-\255]"); end local function n(o,e) if t(o)~="string"then a("invalid "..e.." name: expected string, got "..t(o)); elseif#o==0 then a("invalid "..e.." name: empty string"); elseif r(o,"[<>& '\"]")then a("invalid "..e.." name: contains invalid characters"); elseif not i(o,e=="attribute")then a("invalid "..e.." name: contains control characters"); elseif not v(o)then a("invalid "..e.." name: contains invalid utf8"); end end local function w(e,o) if t(e)~="string"then a("invalid "..o.." value: expected string, got "..t(e)); elseif not i(e,false)then a("invalid "..o.." value: contains control characters"); elseif not v(e)then a("invalid "..o.." value: contains invalid utf8"); end end local function v(e) if e~=nil then if t(e)~="table"then a("invalid attributes: expected table, got "..t(e)); end for t,e in h(e)do n(t,"attribute"); w(e,"attribute"); end end end local function i(t,a,o) n(t,"tag"); v(a); local t={name=t,attr=a or{},namespaces=o,tags={}}; return u(t,e); end local function n(t) return x(t)==e; end function e:query(e) return self:tag("query",{xmlns=e}); end function e:body(t,e) return self:text_tag("body",t,e); end function e:text_tag(e,t,o,a) return self:tag(e,o,a):text(t):up(); end function e:tag(t,e,a) local t=i(t,e,a); local e=self.last_add; if not e then e={};self.last_add=e;end (e[#e]or self):add_direct_child(t); o(e,t); return self; end function e:text(t) if t~=nil and t~=""then local e=self.last_add; (e and e[#e]or self):add_direct_child(t); end return self; end function e:up() local e=self.last_add; if e then l(e);end return self; end function e:at_top() return self.last_add==nil or#self.last_add==0 end function e:reset() self.last_add=nil; return self; end function e:add_direct_child(e) if n(e)then o(self.tags,e); o(self,e); else w(e,"text"); o(self,e); end end function e:add_child(t) local e=self.last_add; (e and e[#e]or self):add_direct_child(t); return self; end function e:remove_children(a,e) e=e or self.attr.xmlns; return self:maptags(function(t) if(not a or t.name==a)and t.attr.xmlns==e then return nil; end return t; end); end function e:get_child(a,t) for o,e in s(self.tags)do if(not a or e.name==a) and((not t and self.attr.xmlns==e.attr.xmlns) or e.attr.xmlns==t)then return e; end end return nil; end function e:get_child_text(e,t) local e=self:get_child(e,t); if e then return e:get_text(); end return nil; end function e:get_child_attr(e,a,t) local e=self:get_child(e,a); if e then return e.attr[t]; end return nil; end function e:child_with_name(t) for a,e in s(self.tags)do if e.name==t then return e;end end return nil; end function e:child_with_ns(t) for a,e in s(self.tags)do if e.attr.xmlns==t then return e;end end return nil; end function e:get_child_with_attr(e,i,t,o,a) for e in self:childtags(e,i)do if(a and a(e.attr[t])or e.attr[t])==o then return e; end end return nil; end function e:children() local e=0; return function(t) e=e+1 return t[e]; end,self,e; end function e:childtags(o,a) local e=self.tags; local i,t=1,#e; return function() for t=i,t do local e=e[t]; if(not o or e.name==o) and((not a and self.attr.xmlns==e.attr.xmlns) or e.attr.xmlns==a)then i=t+1; return e; end end end; end function e:maptags(h) local o,t=self.tags,1; local n,i=#self,#o; local s=n+1; local e=1; while t<=i and i>0 do if self[e]==o[t]then local a=h(self[e]); if a==nil then l(self,e); l(o,t); n=n-1; i=i-1; e=e-1; t=t-1; else self[e]=a; o[t]=a; end t=t+1; end e=e+1; if e>s then a("Invalid stanza state! Please report this error."); end end return self; end function e:find(a) local e=1; local s=#a+1; repeat local o,t,i; local n=y(a,e,e); if n=="@"then return self.attr[y(a,e+1)]; elseif n=="{"then o,e=d(a,"^([^}]+)}()",e+1); end t,i,e=d(a,"^([^@/#]*)([/#]?)()",e); t=t~=""and t or nil; if e==s then if i=="#"then return self:get_child_text(t,o); end return self:get_child(t,o); end self=self:get_child(t,o); until not self end local function l(t,s) local n={}; for t,e in h(t.attr)do n[t]=e;end local a,i=t.namespaces; if a then i={}; for e,t in h(a)do i[e]=t;end end local o,a; if s then o={}; a={name=t.name,attr=n,namespaces=i,tags=o}; else o=b(#t.tags,0); a=b(#t,4); a.name=t.name; a.attr=n; a.namespaces=i; a.tags=o; end u(a,e); if not s then c(t,1,#t,1,a); c(t.tags,1,#t.tags,1,o); a:maptags(l); end return a; end local function u(e,o) if not n(e)then a("bad argument to clone: expected stanza, got "..t(e)); end return l(e,o); end local l={["'"]="'",["\""]=""",["<"]="<",[">"]=">",["&"]="&"}; local function c(e)return(j(e,"['&<>\"]",l));end local function l(a,e,s,t,u) local i=0; local l=a.name o(e,"<"..l); for a,n in h(a.attr)do if r(a,"\1",1,true)then local s,a=d(a,"^([^\1]*)\1?(.*)$"); i=i+1; o(e," xmlns:ns"..i.."='"..t(s).."' ".."ns"..i..":"..a.."='"..t(n).."'"); elseif not(a=="xmlns"and n==u)then o(e," "..a.."='"..t(n).."'"); end end local i=#a; if i==0 then o(e,"/>"); else o(e,">"); for i=1,i do local i=a[i]; if i.name then s(i,e,s,t,a.attr.xmlns); else o(e,t(i)); end end o(e,"</"..l..">"); end end function e.__tostring(t) local e={}; l(t,e,l,c,nil); return m(e); end function e.top_tag(e) local e=u(e,true); return f(e):sub(1,-3)..">"; end function e.get_text(e) if#e.tags==0 then return m(e); end return nil; end function e.get_error(e) local i,t,a,o; local e=e:get_child("error"); if not e then return nil,nil,nil,nil; end i=e.attr.type; for i,e in s(e.tags)do if e.attr.xmlns==g then if not a and e.name=="text"then a=e:get_text(); elseif not t then t=e.name; end else o=e; end if t and a and o then break; end end return i,t or"undefined-condition",a,o; end function e.add_error(o,a,s,h,i) local e; if t(a)=="table"then if t(a.extra)=="table"then e=a.extra; end if t(a.context)=="table"and t(a.context.by)=="string"then i=a.context.by;end a,s,h=a.type,a.condition,a.text; end if o.attr.from==i then i=nil; end o:tag("error",{type=a,by=i}) :tag(s,k); if e and s=="gone"and t(e.uri)=="string"then o:text(e.uri); end o:up(); if h then o:text_tag("text",h,k);end if e and n(e.tag)then o:add_child(e.tag); elseif e and e.namespace and e.condition then o:tag(e.condition,{xmlns=e.namespace}):up(); end return o:up(); end local function l(a) local i={name=a.name,attr=a.attr}; for a,e in s(a)do if t(e)=="table"then o(i,l(e)); else o(i,e); end end return i; end e.__freeze=l; local function m(a) if a then local e=a.attr; local o={}; for e,a in h(e)do if t(e)=="string"then if r(e,"|",1,true)and not r(e,"\1",1,true)then local e,t=d(e,"^([^|]+)|(.+)$"); o[e.."\1"..t]=a; else o[e]=a; end end end local o=i(a.name,o); for a,e in s(a)do if t(e)=="table"then o:add_direct_child(m(e)); elseif t(e)=="string"then o:add_direct_child(e); end end return o; end end local function y(e,t) if not t then return i("message",e); else return i("message",e):text_tag("body",t); end end local function v(e) if not e then a("iq stanzas require id and type attributes"); end if not e.id then a("iq stanzas require an id attribute"); end if not e.type then a("iq stanzas require a type attribute"); end return i("iq",e); end local function r(e) if not n(e)then a("bad argument to reply: expected stanza, got "..t(e)); end return i(e.name, { to=e.attr.from, from=e.attr.to, id=e.attr.id, type=((e.name=="iq"and"result")or e.attr.type) }); end local function w(e,s,h,i,o) if not n(e)then a("bad argument to error_reply: expected stanza, got "..t(e)); elseif e.attr.type=="error"then a("bad argument to error_reply: got stanza of type error which must not be replied to"); end local e=r(e); e.attr.type="error"; e:add_error(s,h,i,o); e.last_add={e[1]}; return e; end local function d(e) return i("presence",e); end local s; if q then local a,t=p.getstyle,p.getstring; local n=a("1b3967"); local r=a("13b5ea"); local o=a("439639"); local i=a("a0ce67"); local h=a("d9541e"); local a=a("e96d1f"); local h=( t(i,"%1").. t(o,"%2").. t(h,"%3").. t(a,"%4").. t(h,"%5") ); local d=( t(o,"%1").. t(i,"%2").. t(o,"%3") ); function s(e) return(e:gsub("(<[?/]?)([^ >/?]*)(.-)([?/]?>)([^<]*)",function(o,e,a,i,s) return t(n,o)..t(r,e).. a:gsub("([^=]+)(=)([\"'])(.-)([\"'])",h).. t(n,i).. s:gsub("(&#?)(%w+)(;)",d); end,100)); end function e.pretty_print(e) return s(f(e)); end function e.pretty_top_tag(e) return s(e:top_tag()); end else e.pretty_print=e.__tostring; e.pretty_top_tag=e.top_tag; end function e.indent(i,o,a) if#i==0 or(#i==1 and t(i[1])=="string")then return i; end a=a or"\t"; o=o or 1; local e=u(i,true); for i in i:children()do if t(i)=="string"then if i:find("%S")then e:text("\n"..a:rep(o)); e:text(i); end elseif n(i)then e:text("\n"..a:rep(o)); e:add_direct_child(i:indent(o+1,a)); end end e:text("\n"..a:rep((o-1))); return e; end return{ stanza_mt=e; stanza=i; is_stanza=n; preserialize=l; deserialize=m; clone=u; message=y; iq=v; reply=r; error_reply=w; presence=d; xml_escape=c; pretty_print=s; }; end) package.preload['util.timer']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"util.indexedbheap"; local t=require"util.logger".init("timer"); local e=require"net.server"; local d=require"util.time".now local l=type; local h=debug.traceback; local i=tostring; local c=require"util.xpcall".xpcall; local f=math.max; local m=pairs; if e.timer then return e.timer; end local _ENV=nil; local r=e.add_task; local o; local s=0; local a=a.create(); local n={}; local e=nil; local function u(e)t("error","Traceback[timer]: %s",h(i(e),2));end local function h(i) local t; local o; while true do t=a:peek(); if t==nil or t>i then break;end local t,a,e=a:pop(); local s=n[e]; n[e]=nil; local h,t=c(a,u,i,e,s); if h and l(t)=="number"then if o then o[e]={a,t+i}; else o={[e]={a,t+i}}; end n[e]=s; end end if o then for o,e in m(o)do a:insert(e[1],e[2],o); end t=a:peek(); end if t~=nil and s>1 and t==e then t=nil; else e=t; end if t then return t-i; end s=s-1; end local function u(t,u,l) local i=d(); local t=i+t; local a=a:insert(u,t); n[a]=l; if e==nil or t<e then e=t; if o then o:close(); o=nil; else s=s+1; end o=r(e-i,h); end return a; end local function l(t) n[t]=nil; local s,i,n=a:remove(t); local t=a:peek(); if t~=e and o then e=t; o:close(); if e~=nil then o=r(f(e-d(),0),h); end end return s,i,n; end local function s(o,n) local i=d(); local t=i+n; a:reprioritize(o,n); if e==nil or t<e then e=t; r(e-i,h); end return o; end return{ add_task=u; stop=l; reschedule=s; }; end) package.preload['util.termcolours']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local s,h=table.concat,table.insert; local a,r=string.char,string.format; local o=tonumber; local m=ipairs; local u=io.write; local i=math.floor; local l=type; local d=setmetatable; local c=pairs; local t; if os.getenv("WINDIR")then t=require"util.windows"; end local n=t and t.get_consolecolor and t.get_consolecolor(); local _ENV=nil; local e={ reset=0;bright=1,dim=2,underscore=4,blink=5,reverse=7,hidden=8; black=30;red=31;green=32;yellow=33;blue=34;magenta=35;cyan=36;white=37; ["black background"]=40;["red background"]=41;["green background"]=42;["yellow background"]=43; ["blue background"]=44;["magenta background"]=45;["cyan background"]=46;["white background"]=47; bold=1,dark=2,underline=4,underlined=4,normal=0; } local w={ ["0"]=n, ["1"]=7+8, ["1;33"]=2+4+8, ["1;31"]=4+8 } local y={ [1]="font-weight: bold",[2]="opacity: 0.5",[4]="text-decoration: underline",[8]="visibility: hidden", [30]="color:black",[31]="color:red",[32]="color:green",[33]="color:#FFD700", [34]="color:blue",[35]="color: magenta",[36]="color:cyan",[37]="color: white", [40]="background-color:black",[41]="background-color:red",[42]="background-color:green", [43]="background-color:yellow",[44]="background-color:blue",[45]="background-color: magenta", [46]="background-color:cyan",[47]="background-color: white"; }; local a=a(27).."[%sm%s"..a(27).."[0m"; local function p(e,t) if e then return r(a,e,t); else return t; end end local function v(e) return i(e*3/32)+232; end local function f(t,e,a) if t==e and e==a then return v(t); end t=i(t*3/128); e=i(e*3/128); a=i(a*3/128); return 16+(t*36)+(e*6)+(a); end local function i(e) local a=o(e:sub(1,2),16); local t=o(e:sub(3,4),16); local e=o(e:sub(5,6),16); return a,t,e; end d(e,{__index=function(t,e) if l(e)=="string"and e:find("%x%x%x%x%x%x")==1 then local t=e:sub(7)==" background"and"48;5;"or"38;5;"; return r("%s%d",t,f(i(e))); end end}); local a={ red="ff0000";fuchsia="ff00ff";green="008000";white="ffffff"; lime="00ff00";yellow="ffff00";purple="800080";blue="0000ff"; aqua="00ffff";olive="808000";black="000000";navy="000080"; teal="008080";silver="c0c0c0";maroon="800000";gray="808080"; } for t,a in c(a)do e[t]=e[t]or e[a]; t,a=t.." background",a.." background" e[t]=e[t]or e[a]; end local function r(...) local t,a={...},{}; for o,t in m(t)do t=e[t]; if t then h(a,t); end end return s(a,";"); end local a="0"; local function i(e) e=e or"0"; if e~=a then u("\27["..e.."m"); a=e; end end if t then function i(e) e=e or"0"; if e~=a then t.set_consolecolor(w[e]or n); a=e; end end if not n then function i()end end end local function a(t) if t=="0"then return"</span>";end local e={}; for t in t:gmatch("[^;]+")do h(e,y[o(t)]); end return"</span><span style='"..s(e,";").."'>"; end local function t(e) return e:gsub("\027%[(.-)m",a); end return{ getstring=p; getstyle=r; setstyle=i; tohtml=t; }; end) package.preload['util.uuid']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"util.random"; local t=a.bytes; local i=require"util.hex".encode; local o=math.ceil; local function e(e) return i(t(o(e/2))):sub(1,e); end local function o() return("%x"):format(t(1):byte()%4+8); end local function t() return e(8).."-"..e(4).."-4"..e(3).."-"..(o())..e(3).."-"..e(12); end return{ get_nibbles=e; generate=t; seed=a.seed; }; end) package.preload['util.time']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local o=require"socket".gettime; local e; local t,a=pcall(require,"system"); if t then e=a.monotime; end return{ now=o; monotonic=e; } end) package.preload['util.envload']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=load; local i=io.open; local function s(o,a,e) return t(o,a,nil,e); end local function h(a,o) local e,i,n=i(a); if not e then return e,i,n;end local a,t=t(e:lines(2048),"@"..a,nil,o); e:close(); return a,t; end return{envload=s,envloadfile=h}; end) package.preload['util.id']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=string.gsub; local o=require"util.random".bytes; local a=require"util.encodings".base64.encode; local i={["+"]="-",["/"]="_",["="]=""}; local function e(e) return(t(a(o(e)),"[+/=]",i)); end return{ tiny=function()return e(3);end; short=function()return e(9);end; medium=function()return e(18);end; long=function()return e(27);end; custom=function(t) return function()return e(t);end; end; } end) package.preload['util.serialization']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local _=getmetatable; local t,a=next,type; local n=string.format; local z=string.gsub; local c=string.rep; local o=string.char; local k=string.match; local q=table.concat; local g=require"util.hex".to; local p=pcall; local v=require"util.envload".envload; if not math.type then require"util.mathcompat" end local b,j=math.huge,-math.huge; local x=math.type; local function h(e) return t,e,nil; end local function s(t,e) error("Can't serialize "..a(t)..(e and": "..e or"")); end local function t(e,t) return n("{__type=%q,__error=%q}",a(e),t or"fail"); end local r={ ['\a']=[[\a]];['\b']=[[\b]]; ['\f']=[[\f]];['\n']=[[\n]]; ['\r']=[[\r]];['\t']=[[\t]]; ['\v']=[[\v]];['\\']=[[\\]]; ['\"']=[[\"]];['\'']=[[\']]; } for t=0,255 do local e=o(t); if not r[e]then r[e]=n("\\%03d",t); end end local o={ ["do"]=true;["and"]=true;["else"]=true;["break"]=true; ["if"]=true;["end"]=true;["goto"]=true;["false"]=true; ["in"]=true;["for"]=true;["then"]=true;["local"]=true; ["or"]=true;["nil"]=true;["true"]=true;["until"]=true; ["elseif"]=true;["function"]=true;["not"]=true; ["repeat"]=true;["return"]=true;["while"]=true; }; local function u(e) if a(e)~="table"then e={preset=e}; end local i={ table=true; string=true; number=true; boolean=true; ["nil"]=true; }; if e.preset=="debug"then e.preset="oneline"; e.freeze=true; e.fatal=false; e.fallback=t; e.unquoted=true; end if e.preset=="oneline"then e.indentwith=e.indentwith or""; e.itemstart=e.itemstart or" "; e.itemlast=e.itemlast or""; e.tend=e.tend or" }"; elseif e.preset=="compact"then e.indentwith=e.indentwith or""; e.itemstart=e.itemstart or""; e.itemlast=e.itemlast or""; e.equals=e.equals or"="; e.unquoted=true; end local s=e.fallback or e.fatal==false and t or s; local function l(e) return(i[a(e)]or s)(e); end local p=e.keywords or o; local y=e.indentwith or"\t"; local v=e.itemstart or"\n"; local I=e.itemsep or";"; local N=e.itemlast or";\n"; local O=e.tstart or"{"; local E=e.tend or"}"; local T=e.kstart or"["; local A=e.kend or"]"; local f=e.equals or" = "; local w=e.unquoted==true and"^[%a_][%w_]*$"or e.unquoted; local d=e.hex; local S=e.freeze; local m=e.maxdepth or 127; local H=e.multiref; local R=e.table_iterator or h; local function u(o,t,e,i) if t[o]then t[e],e=s(o,"table has multiple references"),e+1; return e; elseif i>m then t[e],e=s(o,"max table depth reached"),e+1; return e; end local m=o; t[o]=true; t[m]=true; if S==true then local i=_(o); if a(i)=="table"then local n=i.__name; local i=i.__freeze; if a(i)=="function"then o=i(o); if a(o)=="string"then t[e],e=o,e+1; return e; end if a(n)=="string"then t[e],e=n,e+1; end end end end t[e],e=O,e+1; local b=c(y,i); local s=1; local h,r; local d=false; for o,n in R(o)do d=true; t[e],e=v,e+1; t[e],e=b,e+1; h,r=a(o),a(n); if o==s then s=s+1; elseif w and h=="string"and not p[o]and k(o,w)then t[e],e=o,e+1; t[e],e=f,e+1; else t[e],e=T,e+1; if h=="table"then e=u(o,t,e,i+1); else t[e],e=l(o),e+1; end t[e],t[e+1],e=A,f,e+2; end if r=="table"then e=u(n,t,e,i+1); else t[e],e=l(n),e+1; end t[e],e=I,e+1; end if d then t[e-1]=N; t[e],e=c(y,i-1),e+1; end t[e],e=E,e+1; if H then t[o]=nil; t[m]=nil; end return e; end function i.table(t) local e={}; u(t,e,1,1); return q(e); end local function t(e) return'"'..z(e,"[%z\1-\31\"\'\\\127-\255]",r)..'"'; end if a(d)=="string"then function i.string(e) local t=t(e); if#t>(#e*2+2+#d)then return d..'"'..g(e)..'"'; end return t; end else i.string=t; end function i.number(e) if x(e)=="integer"then return n("%d",e); elseif e==b then return"(1/0)"; elseif e==j then return"(-1/0)"; elseif e~=e then return"(0/0)"; end return n("%.18g",e); end i["nil"]=function() return"nil"; end function i.boolean(e) return e and"true"or"false"; end return l; end local function o(e) if a(e)~="string"then return nil;end e="return "..e; local e,t=v(e,"=serialized data",{}); if not e then return nil,t;end local t,e=p(e); if not t then return nil,e;end return e; end local a=u(); return{ new=u; serialize=function(e,t) if t==nil then return a(e); else return u(t)(e); end end; deserialize=o; }; end) package.preload['util.indexedbheap']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local d=setmetatable; local r=math.floor; local i=table.remove; local function l(t,s,o,n,i) local e=#t+1; while true do local a=r(e/2); if a==0 or s>t[a]then break;end t[e]=t[a]; o[e]=o[a]; i[o[e]]=e; e=a; end t[e]=s; o[e]=n; i[n]=e; end local function h(a,e,t,i) local n=a[e]; local s=t[e]; while e~=1 do local o=r(e/2); if n>=a[o]then break;end a[e]=a[o]; t[e]=t[o]; i[t[e]]=e; e=o; end a[e]=n; t[e]=s; i[s]=e; return e; end local function n(a,e,o,h) local s=a[e]; local n=o[e]; local i=#a; local t=2*e; while 2*e<=i do if t~=i and a[t]>a[t+1]then t=t+1; end if s>a[t]then a[e]=a[t]; o[e]=o[t]; h[o[e]]=e; else break; end e=t; t=2*e; end a[e]=s; o[e]=n; h[n]=e; return e; end local function r(e,t,o) local h=#e; if h==0 then return nil;end local s=e[1]; local a=t[1]; o[a]=nil; if h==1 then e[1]=nil; t[1]=nil; return s,a; end e[1]=i(e); t[1]=i(t); o[t[1]]=1; n(e,1,t,o); return s,a; end local t={}; function t:insert(t,a,e) if e==nil then e=self.current_id; self.current_id=e+1; end self.items[e]=t; l(self.priorities,a,self.ids,e,self.index); return e; end function t:pop() local a,e=r(self.priorities,self.ids,self.index); if e then local t=self.items[e]; self.items[e]=nil; return a,t,e; end end function t:peek() return self.priorities[1]; end function t:reprioritize(e,t) local e=self.index[e]; if e==nil then return;end self.priorities[e]=t; e=h(self.priorities,e,self.ids,self.index); n(self.priorities,e,self.ids,self.index); end function t:remove_index(e) local o=self.priorities[e]; if o==nil then return;end local t=self.ids[e]; local s=self.items[t]; local a=#self.priorities; self.priorities[e]=self.priorities[a]; self.ids[e]=self.ids[a]; self.index[self.ids[e]]=e; i(self.priorities); i(self.ids); self.index[t]=nil; self.items[t]=nil; if a>e then e=h(self.priorities,e,self.ids,self.index); n(self.priorities,e,self.ids,self.index); end return o,s,t; end function t:remove(e) return self:remove_index(self.index[e]); end local e={__index=t}; local e={ create=function() return d({ ids={}; items={}; priorities={}; index={}; current_id=1.5 },e); end }; return e; end) package.preload['util.xpcall']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local e=xpcall; if select(2,e(function(e)return e end,function()end,"test"))~="test"then e=require"util.compat".xpcall; end return{ xpcall=e; }; end) package.preload['util.array']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local r,l,u,f =table.insert,table.sort,table.remove,table.concat; local h=require"util.table".move; local s=setmetatable; local a=getmetatable; local d=math.random; local c=math.floor; local y,p=pairs,ipairs; local w=tostring; local m=type; local i={}; local t={}; local e={}; local o={ __index=e; __name="array"; __tostring=function(e)return"{"..e:concat(", ").."}";end; }; function o:__freeze()return self;end local function n(i,e,a,t) if m(e)=="function"then e=i.collect(e,a,t); end return s(e or{},o); end function o.__add(t,a) local e=n(); return e:append(t):append(a); end function o.__eq(e,t) if a(e)~=o or a(t)~=o then return false; end if#e==#t then for a=1,#e do if e[a]~=t[a]then return false; end end else return false; end return true; end function o.__div(t,o) local a=n(); local e=0; for i=1,#t do local t=o(t[i]); if t~=nil then e=e+1; a[e]=t; end end return a; end s(i,{__call=n}); function e:random() return self[d(1,#self)]; end function e:random_other(t) local e=#self; return self[((math.random(1,e-1)+(t-1))%e)+1]; end function t.map(e,t,a) for t,o in p(t)do e[t]=a(o); end return e; end function t.filter(t,o,n) local i,a=o==t,#o; local e=1; for a=1,a do local a=o[a]; if n(a)then t[e]=a; e=e+1; end end if i and e<=a then for e=e,a do t[e]=nil; end end return t; end function t.slice(a,t,o,e) if e==nil then e=-1; end if e<0 then e=#t+(e+1); end if o<0 then o=#t+(o+1); end if o<1 then o=1; end if e>#t then e=#t; end if o>e then for e=1,#a do a[e]=nil; end return a; end h(t,o,e,1,a); if t==a then h(t,#a+1,#a*2,2+e-o,t); end return a; end function t.sort(e,t,...) if t~=e then e:append(t); end l(e,...); return e; end function t.unique(o,a) local e={}; return t.filter(o,a,function(t) if e[t]then return false; else e[t]=true; return true; end end); end function t.pluck(a,e,o,i) for t=1,#e do local e=e[t][o]; if e==nil then e=i; end a[t]=e; end return a; end function t.reverse(e,i) local t=#i; if i==e then local a=c(t/2); t=t+1; local o; for a=1,a do o=t-a; e[a],e[o]=e[o],e[a]; end else local a=t+1; for t=1,t do e[t]=i[a-t]; end end return e; end function e:shuffle() local t=#self; for e=1,#self do local t=d(e,t); self[e],self[t]=self[t],self[e]; end return self; end function e:append(e) h(e,1,#e,#self+1,self); return self; end function e:push(t) r(self,t); return self; end e.pop=u; function e:concat(e) return f(i.map(self,w),e); end function e:length() return#self; end function i.collect(i,a,e) local t={}; while true do e=i(a,e); if e==nil then break;end r(t,e); end return s(t,o); end for t,a in y(t)do local a=a; i[t]=function(t,...) local o=n(); return a(o,t,...); end e[t]=function(e,...) return a(e,e,...); end end return i; end) package.preload['util.format']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local n=tostring; local m=table.unpack; local e=table.pack; local c=require"util.encodings".utf8.valid; local i=type; local l=require"util.serialization".new("debug"); local r=math.type; local u={c=true,d=true,i=true,o=true,u=true,X=true,x=true,}; local f={o=true;u=true;x=true;X=true}; local d={ ["\000"]="\226\144\128",["\001"]="\226\144\129",["\002"]="\226\144\130", ["\003"]="\226\144\131",["\004"]="\226\144\132",["\005"]="\226\144\133", ["\006"]="\226\144\134",["\007"]="\226\144\135",["\008"]="\226\144\136", ["\009"]="\226\144\137",["\010"]="\226\144\138",["\011"]="\226\144\139", ["\012"]="\226\144\140",["\013"]="\226\144\141",["\014"]="\226\144\142", ["\015"]="\226\144\143",["\016"]="\226\144\144",["\017"]="\226\144\145", ["\018"]="\226\144\146",["\019"]="\226\144\147",["\020"]="\226\144\148", ["\021"]="\226\144\149",["\022"]="\226\144\150",["\023"]="\226\144\151", ["\024"]="\226\144\152",["\025"]="\226\144\153",["\026"]="\226\144\154", ["\027"]="\226\144\155",["\028"]="\226\144\156",["\029"]="\226\144\157", ["\030"]="\226\144\158",["\031"]="\226\144\159",["\127"]="\226\144\161", }; local y=pcall(string.format,"%p",""); local function w(h,...) local o=e(...); local w=o.n; local a=0; h=h:gsub("%%[^cdiouxXaAeEfgGpqs%%]*[cdiouxXaAeEfgGpqs%%]",function(s) if s=="%%"then return end a=a+1; local e=o[a]; if e==nil then o[a]="nil"; return"(%s)"; end local t=s:sub(-1); local i=i(e); if t=="s"and i=="string"and not e:find("[%z\1-\31\128-\255]")then return elseif i=="number"then if t=="g"or(t=="d"and r(e)=="integer")then return end elseif t=="s"and i~="string"then e=n(e); i="string"; end if t~="s"and t~="q"and t~="p"then if i~="number"then e=n(e); t="s"; s="[%s]"; i="string"; elseif u[t]and r(e)~="integer"then o[a]=n(e); return"[%s]"; elseif f[t]and e<0 then o[a]=n(e); return"[%s]"; else return end end if t=="p"and not y then e=n(e); t="s"; s="[%s]"; i="string"; end if i=="string"and t~="p"then if not c(e)then t="q"; elseif t~="q"then o[a]=e:gsub("[%z\1-\8\11-\31\127]",d):gsub("\n\t?","\n\t"); return s; end end if t=="q"then o[a]=l(e); return"%s"; end if t=="p"and(i=="boolean"or i=="number")then o[a]=n(e); return"[%s]"; end end); while a<w do a=a+1; local e=o[a]; if e==nil then o[a]="(nil)"; else o[a]=n(e):gsub("[%z\1-\8\11-\31\127]",d):gsub("\n\t?","\n\t"); end h=h.." [%s]" end return h:format(m(o)); end return{ format=w; }; end) package.preload['util.promise']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local o={}; local a={__name="promise",__index=o}; local n=require"util.xpcall".xpcall; local l=table.unpack; function a:__tostring() return"promise ("..(self._state or"invalid")..")"; end local function i(e) local e=getmetatable(e); return e==a; end local function e(e,t,o,a) if not e then return a; end return function(a) local a,e=n(e,debug.traceback,a); if a then t(e); else o(e); end return true; end; end local function u(o,i,n,a,t) table.insert(o._pending_on_fulfilled,e(i,a,t,a)); table.insert(o._pending_on_rejected,e(n,a,t,t)); end local function r(a,o,n,t,i) e(o,t,i,t)(a.value); end local function d(i,n,o,a,t) e(o,a,t,t)(i.reason); end local function h(e,i,o,t,a) if e._state~="pending"then return; end e._state=i; e._next=o; for o,t in ipairs(t)do t(a); end e._pending_on_fulfilled=nil; e._pending_on_rejected=nil; return true; end local function s(e) local function a(t) if i(t)then t:next(s(e)); elseif h(e,"fulfilled",r,e._pending_on_fulfilled,t)then e.value=t; end end local function o(t) if h(e,"rejected",d,e._pending_on_rejected,t)then e.reason=t; end end return a,o; end local d=function(e) e(); end local function e(o) local e=setmetatable({_state="pending",_next=u,_pending_on_fulfilled={},_pending_on_rejected={}},a); if o then d(function() local a,t=s(e); local o,a=n(o,debug.traceback,a,t); if not o and e._state=="pending"then t(a); end end); end return e; end local function r(o) return e(function(n,r) local e,a,s=0,{},false; local t=0; for h,o in pairs(o)do if i(o)then t=t+1; o:next(function(o) a[h]=o; e=e+1; if e==t and s then n(a); end end,r); else a[h]=o; end end s=true; if e==t then n(a); end end); end local function u(n) return e(function(h) local e,t,o=0,{},false; local a=0; for s,n in pairs(n)do if i(n)then a=a+1; n:next(function(i) t[s]={status="fulfilled",value=i}; e=e+1; if e==a and o then h(t); end end,function(i) t[s]={status="rejected",reason=i}; e=e+1; if e==a and o then h(t); end end); else t[s]=n; end end o=true; if e==a then h(t); end end); end local function n(e,...) local t,a={...},select("#",...); return r(t):next(function(t) return e(l(t,1,a)); end); end local function s(t) return e(function(e,a) for o=1,#t do t[o]:next(e,a); end end); end local function t(t) return e(function(e) e(t); end); end local function a(t) return e(function(a,e) e(t); end); end local function h(e) return t():next(function()return e();end); end function o:next(o,a) return e(function(e,t) self:_next(o,a,e,t); end); end function o:catch(e) return self:next(nil,e); end function o:finally(e) local function t(t)e();return t;end local function o(t)e();return a(t);end return self:next(t,o); end return{ new=e; resolve=t; join=n; reject=a; all=r; all_settled=u; race=s; try=h; is_promise=i; set_nexttick=function(e)d=e;end; } end) package.preload['net.adns']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local o=setmetatable; local c=tostring; local k=table.concat; local n=string.format; local q=string.lower; local d=string.upper; local i=function()end; local w=require"util.logger"; local l=w.init("unbound"); local p=require"net.server"; local m=require"lunbound"; local g=require"util.promise"; local b=require"util.id".short; local y=require"socket".gettime; local e=require"util.dns"; local h,s,u=e.classes,e.types,e.errors; local v=e.parsers; local t={hoststxt=false} local function r(e) e=e or{}; for t,a in pairs(t)do if e[t]==nil then e[t]=a; end end for t,a in pairs(m.config)do if e[t]==nil then e[t]=a; end end return e; end local a; if prosody then local e=require"core.configmanager"; a=r(e.get("*","unbound")); prosody.events.add_handler("config-reloaded",function() a=r(e.get("*","unbound")); end); end local function r(e,t) l("debug","Setting up net.server event handling for %s",e); return t.watchfd(e,function() l("debug","Processing queries for %s",e); e:process() end); end local t,f; local function l() t=m.new(a); f=r(t,p); end if prosody then prosody.events.add_handler("server-started",l); end local n={ __tostring=function(e) if e._string then return e._string end local t=n("Status: %s",u[e.status]); if e.secure then t=t..", Secure"; elseif e.bogus then t=t..n(", Bogus: %s",e.bogus); end local t={t}; for a=1,#e do t[a+1]=e.qname.."\t"..h[e.qclass].."\t"..s[e.qtype].."\t"..c(e[a]); end local t=k(t,"\n"); e._string=t; return t; end; }; local r={}; local function p(e) if not e then return end local a=u[e.rcode]; local i=h[e.qclass]; local t=s[e.qtype]; e.status,e.class,e.type=a,i,t; local a=q(t); local i={__index=e,__tostring=function(e)return c(e[a])end}; local s=v[t]; for t=1,#e do if e.bogus then e[t]=nil; else e[t]=o({[a]=s(e[t])},i); end end return o(e,n); end local function m(m,u,o,a) if not t then l();end o=o and d(o)or"A"; a=a and d(a)or"IN"; local l,h=s[o],h[a]; local d=y(); local n; local i=w.init("unbound.query"..b()); local function s(e,t) local s=y(); r[n]=nil; if e then p(e); i("debug","Results for %s %s %s: %s (%s, %f sec)",u,a,o,e.rcode==0 and(#e.." items")or e.status, e.secure and"Secure"or e.bogus or"Insecure",s-d); else i("error","Results for %s %s %s: %s",u,a,o,c(t)); end local t,e=pcall(m,e,t); if not t then i("error","Error in callback: %s",e);end end i("debug","Resolve %s %s %s",u,a,o); local e; n,e=t:resolve_async(s,u,l,h); if n then r[n]=m; else i("error","Resolver error: %s",e); end return n,e; end local function o(o,a,e) if not t then l();end a=a and d(a)or"A"; e=e and d(e)or"IN"; local e,a=s[a],h[e]; local e,t=t:resolve(o,e,a); if not e then return e,t;end return p(e); end local function e(e) local a=r[e]; t:cancel(e); if a then a(nil,"canceled"); r[e]=nil; end return true; end local function t() for t in pairs(r)do e(t);end if f then f:close();end l(); return true; end local function a() error"not implemented"; end local a={ lookup=m; cancel=e; new_async_socket=a; dns={ lookup=o; cancel=e; cache=i; socket_wrapper_set=i; settimeout=i; query=i; purge=t; random=i; peek=i; types=s; classes=h; }; }; local function n(e,i,a,o) return g.new(function(n,t) local function s(a,e) if e then return t(e); else return n(a); end end local e,a=m(s,i,a,o) if not e then t(a);end end); end local e={ lookup=function(i,e,t,a,o) return m(e,t,a,o) end; lookup_promise=n; _resolver={ settimeout=function()end; closeall=function()end; }; } function a.resolver()return e;end return a; end) package.preload['util.dns']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=setmetatable; local e=table; local s=e.concat; local n=e.insert; local o=string.byte; local i=string.format; local r=string.sub; local h=require"util.dnsregistry"; local c=require"util.hex".encode; local d=require"util.net".ntop; local e={}; local function a(a,t) if o(a,t)==0 then return".",t+1;end local d,h,i=#a,{}; t=t or 1; repeat i=o(a,t)or 0; n(h,r(a,t+1,t+i)); t=t+i+1; until i==0 or t>=d; return s(h,"."),t; end e.CNAME=a; e.NS=a e.PTR=a; local x={ __tostring=function(e) return i("%s %s %d %d %d %d %d",e.mname,e.rname,e.serial,e.refresh,e.retry,e.expire,e.minimum); end; }; function e.SOA(n) local s,h,i; s,i=a(n,1); h,i=a(n,i); local j,q,g,k, b,l,r,d, n,a,o,i, u,w,c,p, v,y,m,f =o(n,i,i+19); return t({ mname=s; rname=h; serial=j*16777216+q*65536+g*256+k; refresh=b*16777216+l*65536+r*256+d; retry=n*16777216+a*65536+o*256+i; expire=u*16777216+w*65536+c*256+p; minimum=v*16777216+y*65536+m*256+f; },x); end e.A=d; e.AAAA=d; local d={ __tostring=function(e) return i("%d %s",e.pref,e.mx) end }; function e.MX(e) local i=a(e,3); local a,e=o(e,1,2); return t({ pref=a*256+e; mx=i; },d); end local d={ __tostring=function(e) return i("%d %d %d %s",e.priority,e.weight,e.port,e.target); end }; function e.SRV(e) local i=a(e,7); local e,a,o,n,h,s=o(e,1,6); return t({ priority=e*256+a; weight=o*256+n; port=h*256+s; target=i; },d); end local d={__tostring=s}; function e.TXT(s) local l=#s; local h,a,i={},1; repeat i=o(s,a)or 0; n(h,r(s,a+1,a+i)); a=a+i+1; until a>=l; return t(h,d); end e.SPF=e.TXT; local d={ [0]="PKIX-CA"; [1]="PKIX-EE"; [2]="DANE-TA"; [3]="DANE-EE"; [255]="PrivCert"; }; local u={ [0]="Cert", [1]="SPKI", [255]="PrivSel", }; local l={ [0]="Full", [1]="SHA2-256", [2]="SHA2-512", [255]="PrivMatch", }; local m={ __tostring=function(e) return i("%s %s %s %s", d[e.use]or e.use, u[e.select]or e.select, l[e.match]or e.match, c(e.data)); end; __index={ getUsage=function(e)return d[e.use]end; getSelector=function(e)return u[e.select]end; getMatchType=function(e)return l[e.match]end; } }; function e.TLSA(e) local i,o,a=o(e,1,3); return t({ use=i; select=o; match=a; data=r(e,4); },m); end local o={"alpn";"no-default-alpn";"port";"ipv4hint";"ech";"ipv6hint"}; t(o,{__index=function(t,e)return"key"..tostring(e);end}); local m={ __tostring=function(e) local t={}; for a=1,#e.fields do n(t,i("%s=%q",o[e.fields[a].key],tostring(e.fields[a].value))); end return i("%d %s %s",e.prio,e.name,s(t," ")); end; }; local r={__tostring=function(e)return s(e,", ");end} function e.SVCB(i) local o,s=i:byte(1,2); local u=o*256+s; local l,o=a(i,3); local d={}; while#i>o do local s,l=i:byte(o+0,o+1); local h,a=i:byte(o+2,o+3); local s=s*256+l; local h=h*256+a; local a=i:sub(o+4,o+4-1+h) if s==1 then a=t(e.TXT(a),r); elseif s==3 then local e,t=a:byte(1,2); local e=e*256+t; a=e; elseif s==4 then local o={}; for t=1,#a,4 do n(o,e.A(a:sub(t,t+3))); end a=t(o,r); elseif s==6 then local o={}; for t=1,#a,16 do n(o,e.AAAA(a:sub(t,t+15))); end a=t(o,r); end n(d,{key=s,value=a,len=h}); o=o+h+4; end return t({ prio=u,name=l,fields=d, },m); end e.HTTPS=e.SVCB; local o={ TLSA={ use=d; select=u; match=l; }; }; local a={ __tostring=function(e) return i([[\# %d %s]],#e.raw,c(e.raw)); end; }; local function i(e) return t({raw=e},a); end t(e,{__index=function()return i end}); return{ parsers=e; classes=h.classes; types=h.types; errors=h.errors; params=o; }; end) package.preload['net.server']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local f=function(e) return _G[e] end local V,e=require("util.logger").init("socket"),table.concat; local n=function(...)return V("debug",e{...});end local j=function(...)return V("warn",e{...});end local ie=1 local w=f"type" local q=f"pairs" local oe=f"ipairs" local b=f"tonumber" local u=f"tostring" local t=f"table" local a=f"string" local e=f"coroutine" local W=math.min local te=math.huge local ye=t.concat local ae=t.insert local be=a.sub local pe=e.wrap local ke=e.yield local x=f"socket"or require"socket" local P=x.gettime local e=require"util.net"; local ge=e.pton; local ve=require"util.sslconfig"; local Y,fe=pcall(require,"net.tls_luasec"); local ue=x.bind local we=x.select local U local Z local ce local me local ee local l local de local J local K local le local re local ne local Q local h local se local G local he local p local i local F local r local s local R local v local y local k local T local a local o local g local C local M local D local _ local H local B local d local S local I local O local N local L local z local E local A p={} i={} r={} F={} s={} v={} y={} R={} k={} a=0 o=0 g=0 C=0 M=0 D=1 _=128 H=10 S=51e3*1024 I=25e3*1024 O=30 N=6e4 L=14*60 local e=package.config:sub(1,1)=="\\" E=(e and math.huge)or x._SETSIZE or 1024 z=x._SETSIZE or 1024 A=30 re=function(w,t,m,c,q,y,v) if t:getfd()>=E then j("server.lua: Disallowed FD number: "..t:getfd()) t:close() return nil,"fd-too-large" end local f=0 local b,e=w.onconnect,w.ondisconnect local g=t.accept local e={} e.shutdown=function()end e.ssl=function() return y~=nil end e.sslctx=function() return y end e.hosts={} e.remove=function() f=f-1 if e then e.resume() end end e.close=function() t:close() o=h(r,t,o) a=h(i,t,a) p[m..":"..c]=nil; s[t]=nil e=nil t=nil n"server.lua: closed server handler and removed sockets from list" end e.pause=function(o) if not e.paused then a=h(i,t,a) if o then s[t]=nil t:close() t=nil; end e.paused=true; n("server.lua: server [",m,"]:",c," paused") end end e.resume=function() if e.paused then if not t then t=ue(m,c,_); t:settimeout(0) end a=l(i,t,a) s[t]=e k[e]=nil e.paused=false; n("server.lua: server [",m,"]:",c," resumed") end end e.ip=function() return m end e.serverport=function() return c end e.socket=function() return t end e.readbuffer=function() if a>=z or o>=z then e.pause() k[e]=d n("server.lua: refused new client connection: server full") return false end local t,o=g(t) if t then local a,o=t:getpeername() local e,i,t=G(e,w,t,a,c,o,q,y,v) if t then return false end f=f+1 n("server.lua: accepted new client connection from ",u(a),":",u(o)," to ",u(c)) if b and not v then return b(e); end return; elseif o then n("server.lua: error with new client connection: ",u(o)) e.pause() k[e]=d return false end end return e end G=function(q,c,t,H,ee,L,D,w,te,P) if t:getfd()>=E then j("server.lua: Disallowed FD number: "..t:getfd()) t:close() if q then k[q]=d q.pause() end return nil,nil,"fd-too-large" end t:settimeout(0) local b local N local z local B local x local K=c.onincoming local G=c.onstatus local k=c.ondisconnect local J=c.onpredrain local X=c.ondrain local ae=c.onreadtimeout; local Q=c.ondetach local g={} local m=0 local Z local V local f=0 local p=false local E=false local W,F=0,0 local _=S local O=I local e=g e.extra=P if P then e.servername=P.servername end e.dispatch=function() return K end e.disconnect=function() return k end e.onreadtimeout=ae; e.setlistener=function(a,t,o) if Q then Q(a) end K=t.onincoming k=t.ondisconnect G=t.onstatus J=t.onpredrain X=t.ondrain e.onreadtimeout=t.onreadtimeout Q=t.ondetach if t.onattach then t.onattach(a,o) end end e._setpending=function() x=true end e.getstats=function() return F,W end e.ssl=function() return B end e.sslctx=function() return w end e.ssl_info=function() return t.info and t:info() end e.ssl_peercertificate=function() if not t.getpeercertificate then return nil,"not-implemented";end return t:getpeercertificate() end e.ssl_peerverification=function() if not t.getpeerverification then return nil,{{"Chain verification not supported"}};end return t:getpeerverification(); end e.ssl_peerfinished=function() if not t.getpeerfinished then return nil,"not-implemented";end return t:getpeerfinished(); end e.send=function(n,i,a,o) return b(t,i,a,o) end e.receive=function(o,a) return N(t,o,a) end e.shutdown=function(a) return z(t,a) end e.setoption=function(i,o,a) if t.setoption then return t:setoption(o,a); end return false,"setoption not implemented"; end e.force_close=function(t,a) if m~=0 then n("server.lua: discarding unwritten data for ",u(H),":",u(L)) m=0; end return t:close(a); end e.close=function(l,d) if not e then return true;end a=h(i,t,a) v[e]=nil if m~=0 then e:sendbuffer() if m~=0 then if e then e.write=nil end Z=true return false end end if t then T=z and z(t) t:close() o=h(r,t,o) s[t]=nil t=nil else n"server.lua: socket already closed" end if e then y[e]=nil R[e]=nil local t=e; e=nil if k then k(t,d or false); k=nil end end if q then q.remove() end n"server.lua: closed client handler and removed socket from list" return true end e.server=function() return q end e.ip=function() return H end e.serverport=function() return ee end e.clientport=function() return L end e.port=e.clientport local k=function(i,a) if not e then return false end f=f+#a if f>_ then R[e]="send buffer exceeded" return false elseif not E and t and not r[t]then o=l(r,t,o) end m=m+1 g[m]=a if e then y[e]=y[e]or d end return true end e.write=k e.bufferqueue=function(t) return g end e.socket=function(a) return t end e.set_mode=function(a,t) D=t or D return D end e.set_send=function(a,t) b=t or b return b end e.bufferlen=function(o,a,t) _=t or _ O=a or O return f,O,_ end e.lock_read=function(a,t) j("server.lua, lock_read() is deprecated, use pause() and resume()") if t==true then return a:pause() elseif t==false then return a:resume() end return p end e.pause=function(o) local o=a a=h(i,t,a) v[e]=nil if a~=o then p=true end return p; end e.resume=function(o) if p then p=false a=l(i,t,a) v[e]=d end return p; end e.lock=function(t,a) j("server.lua, lock() is deprecated") e.lock_read(t,a) if a==true then e.pause_writes(t) elseif a==false then e.resume_writes(t) end return p,E end e.pause_writes=function(a) local a=o o=h(r,t,o) y[e]=nil E=true end e.resume_writes=function(e) E=false if f>0 and t then o=l(r,t,o) end end local v=function() local o,t,a=N(t,D) if not t or(t=="wantread"or t=="timeout")then local a=o or a or"" local o=#a if o>O then e:close("receive buffer exceeded") return false end local o=o*ie F=F+o M=M+o v[e]=d if x then x=nil if c.onconnect then c.onconnect(e) end end return K(e,a,t) else n("server.lua: client ",u(H),":",u(L)," read error: ",u(t)) T=e and e:force_close(t) return false end end local p=function() local w,a,i,s,l; if t then if x then x=nil if c.onconnect then c.onconnect(e); end end if J then J(e); end s=ye(g,"",1,m) w,a,i=b(t,s,1,f) l=(w or i or 0)*ie W=W+l C=C+l for e=m,1,-1 do g[e]=nil end else w,a,l=false,"unexpected close",0; end if w then m=0 f=0 o=h(r,t,o) y[e]=nil if X then X(e) end T=V and e:starttls(nil) T=Z and e:force_close() return true elseif i and(a=="timeout"or a=="wantwrite")then s=be(s,i+1,f) g[1]=s m=1 f=f-i y[e]=d return true else n("server.lua: client ",u(H),":",u(L)," write error: ",u(a)) T=e and e:force_close(a) return false end end local d; function e.set_sslctx(y,t) w=t; local u,f d=pe(function(s) local t for d=1,A do o=(f and h(r,s,o))or o a=(u and h(i,s,a))or a u,f=nil,nil d,t=s:dohandshake() if not t then n("server.lua: ssl handshake done") e.readbuffer=v e.sendbuffer=p d=G and G(e,"ssl-handshake-complete") if y.autostart_ssl and c.onconnect then c.onconnect(y); if m~=0 then o=l(r,s,o) end end a=l(i,s,a) return true else if t=="wantwrite"then o=l(r,s,o) f=true elseif t=="wantread"then a=l(i,s,a) u=true else break; end t=nil; ke() end end t=(t or"handshake too long"); n("server.lua: ",t); T=e and e:force_close(t) return false,t end ) end if Y then e.starttls=function(f,c) if c then e:set_sslctx(c); end if m>0 then n"server.lua: we need to do tls, but delaying until send buffer empty" V=true return end n("server.lua: attempting to start tls on "..u(t)) local m,c=t t,c=w:wrap(t) if not t then n("server.lua: error while starting tls on client: ",u(c or"unknown error")) return nil,c end if t.sni then if f.servername then t:sni(f.servername); elseif next(w._sni_contexts)~=nil then t:sni(w._sni_contexts,true); end end t:settimeout(0) b=t.send N=t.receive z=U s[t]=e a=l(i,t,a) a=h(i,m,a) o=h(r,m,o) s[m]=nil e.starttls=nil V=nil B=true e.readbuffer=d e.sendbuffer=d return d(t) end end e.readbuffer=v e.sendbuffer=p b=t.send N=t.receive z=(B and U)or t.shutdown s[t]=e a=l(i,t,a) if w and te and Y then n"server.lua: auto-starting ssl negotiation..." e.autostart_ssl=true; local t,e=e:starttls(w); if t==false then return nil,nil,e end end return e,t end U=function() end me=function() return false end l=function(a,t,e) if not a[t]then e=e+1 a[e]=t a[t]=e end return e; end h=function(e,i,t) local o=e[i] if o then e[i]=nil local a=e[t] e[t]=nil if a~=i then e[a]=o e[o]=a end return t-1 end return t end Q=function(e) o=h(r,e,o) a=h(i,e,a) s[e]=nil e:close() end local function c(e,t,o) local a; local i=t.sendbuffer; function t.sendbuffer() i(t); if a and t.bufferlen()<o then e:lock_read(false); a=nil; end end local i=e.readbuffer; function e.readbuffer() i(); if not a and t.bufferlen()>=o then a=true; e:lock_read(true); end end e:set_mode("*a"); end J=function(e,t,d,h) e=e or"*" h=h or{} local o local r=h.tls_ctx; local c=h.tls_direct; local u=h.read_size; if w(d)~="table"then o="invalid listener table" elseif w(e)~="string"then o="invalid address" elseif w(t)~="number"or not(t>=0 and t<=65535)then o="invalid port" elseif p[e..":"..t]then o="listeners on '["..e.."]:"..t.."' already exist" elseif r and not Y then o="luasec not found" end if o then j("server.lua, [",e,"]:",t,": ",o) return nil,o end local o,h=ue(e,t,_) if h then j("server.lua, [",e,"]:",t,": ",h) return nil,h end local h,d=re(d,o,e,t,u,r,c) if not h then o:close() return nil,d end o:settimeout(0) a=l(i,o,a) p[e..":"..t]=h s[o]=h n("server.lua: new "..(r and"ssl "or"").."server listener on '[",e,"]:",t,"'") return h end de=function(i,o,t,a,e) return J(i,o,t,{ read_size=a; tls_ctx=e; tls_direct=e and true or false; }); end le=function(e,t) return p[e..":"..t]; end se=function(e,t) local a=p[e..":"..t] if not a then return nil,"no server found on '["..e.."]:"..u(t).."'" end a:close() p[e..":"..t]=nil return true end ee=function() for t,e in q(s)do e:close() s[t]=nil end a=0 o=0 g=0 p={} i={} r={} F={} s={} end ne=function() return{ select_timeout=D; tcp_backlog=_; max_send_buffer_size=S; max_receive_buffer_size=I; select_idle_check_interval=O; send_timeout=N; read_timeout=L; max_connections=z; max_ssl_handshake_roundtrips=A; highest_allowed_fd=E; accept_retry_interval=H; } end he=function(e) if w(e)~="table"then return nil,"invalid settings table" end D=b(e.select_timeout)or D S=b(e.max_send_buffer_size)or S I=b(e.max_receive_buffer_size)or I O=b(e.select_idle_check_interval)or O _=b(e.tcp_backlog)or _ N=b(e.send_timeout)or N L=b(e.read_timeout)or L H=b(e.accept_retry_interval)or H z=e.max_connections or z A=e.max_ssl_handshake_roundtrips or A E=e.highest_allowed_fd or E return true end K=function(e) if w(e)~="function"then return nil,"invalid listener function" end g=g+1 F[g]=e return true end local u do local a={}; local t={}; function u(e,a) local o=P(); e=e+o; if e>=o then ae(t,{e,a}); else local e=a(o); if e and w(e)=="number"then return u(e,a); end end end K(function(o) if#t>0 then for o,e in q(t)do ae(a,e); end t={}; end local e=te; for s,t in q(a)do local n,i=t[1],t[2]; if n<=o then a[s]=nil; local t=i(o); if w(t)=="number"then u(t,i); e=W(e,t); end else e=W(e,n-o); end end return e; end); end ce=function() return M,C,a,o,g end local e; local function b(t) e=t; end Z=function(t) if e then return"quitting";end if t then e="once";end d=P() repeat local t=te; for e=1,g do local e=F[e](d) if e then t=W(t,e);end end local t,a,o=we(i,r,W(D,t)) for t,e in oe(t)do local t=s[e] if t then t:readbuffer() else Q(e) n"server.lua: found no handler and closed socket (readlist)" end end for e,t in oe(a)do local e=s[t] if e then e:sendbuffer() else Q(t) n"server.lua: found no handler and closed socket (writelist)" end end for e,t in q(R)do e.disconnect()(e,t) e:force_close() R[e]=nil; end d=P() if d-B>O then B=d for e,t in q(y)do if d-t>N then e.disconnect()(e,"send timeout") e:force_close() end end for e,t in q(v)do if d-t>L then if not(e.onreadtimeout)or e:onreadtimeout()~=true then e.disconnect()(e,"read timeout") e:close() else v[e]=d end end end end for e,t in q(k)do if d-t>H then k[e]=nil; e.resume(); end end until e; if e=="once"then e=nil;return;end ee(); return"quitting" end local function p() return Z(true); end local function m() return"select"; end local d=function(e,t,h,d,c,n,u) local t,e,h=G(nil,d,e,t,h,"clientport",c,n,n,u) if not t then return nil,h end s[e]=t if not n then t._setpending() a=l(i,e,a) o=l(r,e,o) end return t,e end local g=function(o,t,i,r,n,a,s) local e if w(i)~="table"then e="invalid listener table" elseif w(o)~="string"then e="invalid address" elseif w(t)~="number"or not(t>=0 and t<=65535)then e="invalid port" elseif n and not Y then e="luasec not found" end if not a then local e=ge(o); if not e then return nil,"invalid-ip";end if#e==16 then a="tcp6"; elseif#e==4 then a="tcp4"; end end local a=x[a]; if w(a)~="function"then e="invalid socket type" end if e then j("server.lua, addclient: ",e) return nil,e end local e,a=a() if a then return nil,a end e:settimeout(0) local h,a=e:setpeername(o,t) if h or a=="timeout"or a=="Operation already in progress"then return d(e,o,t,i,r,n,s) else return nil,a end end local k=function(e) local e=e.conn; o=h(r,e,o) a=h(i,e,a) s[e]=nil end; local i=function(d,n,t) local e=d.conn s[e]=d if n~=nil then if n then a=l(i,e,a) else o=h(r,e,o) end end if t~=nil then if t then o=l(r,e,o) else a=h(i,e,a) end end end local t=function(e,o,a) local t=e if w(e)=="number"then t={getfd=function()return e;end} end local e={ conn=t; readbuffer=o or U; sendbuffer=a or U; close=k; setflags=i; }; i(e,o,a) return e end f"setmetatable"(s,{__mode="k"}) f"setmetatable"(v,{__mode="k"}) f"setmetatable"(y,{__mode="k"}) B=P() local function a(e) local t=V; if e then V=e; end return t; end return{ _addtimer=K, add_task=u; addclient=g, wrapclient=d, watchfd=t, loop=Z, link=c, step=p, stats=ce, closeall=ee, addserver=de, listen=J, getserver=le, setlogger=a, getsettings=ne, setquitting=b, removeserver=se, get_backend=m, changesettings=he, tls_builder=function(e) return ve._new(fe.new_context,e) end, } end) package.preload['util.xmppstream']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local e=require"lxp"; local g=require"util.stanza"; local y=g.stanza_mt; local w=error; local t=tostring; local d=table.insert; local b=table.concat; local E=table.remove; local p=setmetatable; local T=pcall(e.new,{StartDoctypeDecl=false}); local _=pcall(e.new,{XmlDecl=false}); local a=not not e.new({}).getcurrentbytecount; local x=1024*1024*1; local _ENV=nil; local v=e.new; local j={ ["http://www.w3.org/XML/1998/namespace\1lang"]="xml:lang"; ["http://www.w3.org/XML/1998/namespace\1space"]="xml:space"; ["http://www.w3.org/XML/1998/namespace\1base"]="xml:base"; ["http://www.w3.org/XML/1998/namespace\1id"]="xml:id"; }; local h="http://etherx.jabber.org/streams"; local r="\1"; local k="^([^"..r.."]*)"..r.."?(.*)$"; local function o()end local function q(n,e,s) local i={}; local g=e.streamopened; local v=e.streamclosed; local l=e.error or function(o,a,e) w("XML stream error: "..t(a)..(e and": "..t(e)or""),2); end; local x=e.handlestanza; s=s or o; local t=e.stream_ns or h; local m=e.stream_tag or"stream"; if t~=""then m=t..r..m; end local z=t..r..(e.error_tag or"error"); local q=e.default_ns; local f="en"; local u={}; local h,e={}; local t=0; local r=0; function i:StartElement(c,o) if e and#h>0 then d(e,b(h)); h={}; end local h,i=c:match(k); if i==""then h,i="",h; end if h~=q or r>0 then o.xmlns=h; r=r+1; end for t=1,#o do local e=o[t]; o[t]=nil; local t=j[e]; if t then o[t]=o[e]; o[e]=nil; end end if not e then if a then t=self:getcurrentbytecount(); end if n.notopen then if c==m then r=0; f=o["xml:lang"]or f; if g then if a then s(t); t=0; end g(n,o); end else l(n,"no-stream",c); end return; end if h=="jabber:client"and i~="iq"and i~="presence"and i~="message"then l(n,"invalid-top-level-element"); end e=p({name=i,attr=o,tags={}},y); else if a then t=t+self:getcurrentbytecount(); end d(u,e); local t=e; e=p({name=i,attr=o,tags={}},y); d(t,e); d(t.tags,e); end end function i:StartCdataSection() if a then if e then t=t+self:getcurrentbytecount(); else s(self:getcurrentbytecount()); end end end function i:EndCdataSection() if a then if e then t=t+self:getcurrentbytecount(); else s(self:getcurrentbytecount()); end end end function i:CharacterData(o) if e then if a then t=t+self:getcurrentbytecount(); end d(h,o); elseif a then s(self:getcurrentbytecount()); end end function i:EndElement(o) if a then t=t+self:getcurrentbytecount() end if r>0 then r=r-1; end if e then if#h>0 then d(e,b(h)); h={}; end if#u==0 then if a then s(t); end t=0; if e.attr["xml:lang"]==nil then e.attr["xml:lang"]=f; end if o~=z then x(n,e); else l(n,"stream-error",e); end e=nil; else e=E(u); end else if a then s(t); end if v then v(n); end end end local function o(e) l(n,"parse-error","restricted-xml","Restricted XML, see RFC 6120 section 11.1."); if not e.stop or not e:stop()then w("Failed to abort parsing"); end end if _ then function i:XmlDecl(e,t,i) if a then s(self:getcurrentbytecount()); end if(t and t:lower()~="utf-8") or(i=="no") or(e and e~="1.0")then return o(self); end end end if T then i.StartDoctypeDecl=o; end i.Comment=o; i.ProcessingInstruction=o; local function a() e,h,t=nil,{},0; u={}; end local function e(t,e) n=e; end return i,{reset=a,set_session=e}; end local function l(e,h,o) local t=0; local i; if a then function i(e) t=t-e; end o=o or x; elseif o then w("Stanza size limits are not supported on this version of LuaExpat") end local n,d=q(e,h,i); local i=v(n,r,false); local s=i.parse; function e.open_stream(e,o,a) local i=e.sends2s or e.send; local t={ ["xmlns:stream"]="http://etherx.jabber.org/streams", ["xml:lang"]="en", xmlns=h.default_ns, version=e.version and(e.version>0 and"1.0"or nil), id=e.streamid or"", from=o or e.host,to=a, }; if e.stream_attrs then e:stream_attrs(o,a,t) end i("<?xml version='1.0'?>"..g.stanza("stream:stream",t):top_tag()); return true; end return{ reset=function() i=v(n,r,false); s=i.parse; t=0; d.reset(); end, feed=function(e,n) if a then t=t+#n; end local e=i; local n,s=s(e,n); if a and t>o then return nil,"stanza-too-large"; end if i~=e then e:parse(); e:close(); end return n,s; end, set_session=d.set_session; set_stanza_size_limit=function(t,e) o=e; end; }; end return{ ns_separator=r; ns_pattern=k; new_sax_handlers=q; new=l; }; end) package.preload['util.jid']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local i=select; local a,h=string.match,string.sub; local u=require"util.encodings".stringprep.nodeprep; local l=require"util.encodings".stringprep.nameprep; local d=require"util.encodings".stringprep.resourceprep; local o={ [" "]="\\20";['"']="\\22"; ["&"]="\\26";["'"]="\\27"; ["/"]="\\2f";[":"]="\\3a"; ["<"]="\\3c";[">"]="\\3e"; ["@"]="\\40";["\\"]="\\5c"; }; local n={}; local s={}; for t,e in pairs(o)do n[e]=t; s[e]=e:gsub("\\",o) end local _ENV=nil; local function t(e) if e==nil then return;end local i,t=a(e,"^([^@/]+)@()"); local t,o=a(e,"^([^@/]+)()",t); local a=t and a(e,"^/(.+)$",o); if(t==nil)or((a==nil)and#e>=o)then return nil,nil,nil;end return i,t,a; end local function m(e) local t,e=t(e); if t~=nil and e~=nil then return t.."@"..e; end return e; end local function r(e,o) local a,e,t=t(e); if e~=nil and e~="."then if h(e,-1,-1)=="."then e=h(e,1,-2); end e=l(e,o); if e==nil then return;end if a~=nil then a=u(a,o); if a==nil then return;end end if t~=nil then t=d(t,o); if t==nil then return;end end return a,e,t; end end local function h(t,e,a) if e==nil then return end if t~=nil and a~=nil then return t.."@"..e.."/"..a; elseif t~=nil then return t.."@"..e; elseif a~=nil then return e.."/"..a; end return e; end local function c(e,t) local e,a,t=r(e,t); return h(e,a,t); end local function f(a,e) local i,n,o=t(a); local a,t,e=t(e); if(a==nil or a==i)and (t==nil or t==n)and (e==nil or e==o)then return true end return false end local function u(e) return(i(1,t(e))); end local function l(e) return(i(2,t(e))); end local function d(e) return(i(3,t(e))); end local function a(e)return e and(e:gsub("\\%x%x",s):gsub("[\"&'/:<>@ ]",o));end local function o(e)return e and(e:gsub("\\%x%x",n));end return{ split=t; bare=m; prepped_split=r; join=h; prep=c; compare=f; node=u; host=l; resource=d; escape=a; unescape=o; }; end) package.preload['util.events']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=pairs; local d=table.insert; local w=table.remove; local u=table.sort; local l=setmetatable; local h=next; local _ENV=nil; local function f() local o={}; local t; local n={}; local i={}; local s=nil; local function r(n,o) local e=i[o]; if not e or h(e)==nil then return;end local t={}; for e in a(e)do d(t,e); end u(t,function(a,t)return e[a]>e[t];end); n[o]=t; return t; end; l(o,{__index=r}); local function l(t,n,a) local e=i[t]; if e then e[n]=a or 0; else e={[n]=a or 0}; i[t]=e; end o[t]=nil; end; local function d(e,a) local t=i[e]; if t then t[a]=nil; o[e]=nil; if h(t)==nil then i[e]=nil; end end end; local function c(e) return o[e]; end; local function u(e) for e,t in a(e)do l(e,t); end end; local function m(e) for t,e in a(e)do d(t,e); end end; local function r(a,t) local e=o[a]; if e and not s then for a=1,#e do local e=e[a](t); if e~=nil then return e;end end elseif e and s then for o=1,#e do local e=s(e[o],a,t); if e~=nil then return e;end end end end; local function f(s,h) local e=n[s]or t; if e then local a=#e; local function i(o,n) a=a-1; if a==0 then if t==nil or e==t then return r(o,n); end e,a=t,#t; return e[a](i,o,n); else return e[a](i,o,n); end end return e[a](i,s,h); end return r(s,h); end local function r(a,o) local e; if a==false then e=t; if not e then e={}; t=e; end else e=n[a]; if not e then e={}; n[a]=e; end end e[#e+1]=o; end local function h(a,o) local e; if a==false then e=t; else e=n[a]; end if not e then return;end for t=#e,1,-1 do if e[t]==o then w(e,t); end end if#e==0 then if a==false then t=nil; else n[a]=nil; end end end local function a(t) local e=s; s=t; return e; end return{ add_handler=l; remove_handler=d; add_handlers=u; remove_handlers=m; get_handlers=c; wrappers={ add_handler=r; remove_handler=h; }; add_wrapper=r; remove_wrapper=h; set_debug_hook=a; fire_event=f; _handlers=o; _event_map=i; }; end return{ new=f; }; end) package.preload['util.dataforms']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local e=setmetatable; local n=ipairs; local s,p=type,next; local u=tonumber; local r=tostring; local l=table.concat; local c=require"util.stanza"; local m=require"util.jid".prep; local _ENV=nil; local y='jabber:x:data'; local f='http://jabber.org/protocol/xdata-validate'; local d={}; local t={__index=d}; local function w(a) return e(a,t); end local function v(e) local o={ title=e:get_child_text("title"); instructions=e:get_child_text("instructions"); }; for t in e:childtags("field")do local e={ name=t.attr.var; label=t.attr.label; type=t.attr.type; required=t:get_child("required")and true or nil; value=t:get_child_text("value"); }; o[#o+1]=e; if e.type then local a={}; if e.type:match"list%-"then for e in t:childtags("option")do a[#a+1]={label=e.attr.label,value=e:get_child_text("value")}; end for e in t:childtags("value")do a[#a+1]={label=e.attr.label,value=e:get_text(),default=true}; end elseif e.type:match"%-multi"then for e in t:childtags("value")do a[#a+1]=e.attr.label and{label=e.attr.label,value=e:get_text()}or e:get_text(); end if e.type=="text-multi"then e.value=l(a,"\n"); else e.value=a; end end end local t=t:get_child("validate",f); if t then e.datatype=datatype.attr.datatype; local t=t:get_child("range"); if t then e.range_min=u(t.attr.min); e.range_max=u(t.attr.max); end end end return w(o); end function d.form(t,h,i) if not i then i="form"end local e=c.stanza("x",{xmlns=y,type=i}); if i=="cancel"then return e; end if i~="submit"then if t.title then e:tag("title"):text(t.title):up(); end if t.instructions then e:tag("instructions"):text(t.instructions):up(); end end for t,a in n(t)do local o=a.type or"text-single"; e:tag("field",{type=o,var=a.var or a.name,label=i~="submit"and a.label or nil}); if i~="submit"then if a.desc then e:text_tag("desc",a.desc); end end if i=="form"and a.datatype then e:tag("validate",{xmlns=f,datatype=a.datatype}); if a.range_min or a.range_max then e:tag("range",{ min=a.range_min and r(a.range_min), max=a.range_max and r(a.range_max), }):up(); end e:up(); end local t=a.value; local r=a.options; if h and h[a.name]~=nil then t=h[a.name]; if i=="form"and s(t)=="table" and(o=="list-single"or o=="list-multi")then r,t=t,nil; end end if i=="form"and r then local a={}; for o,t in n(r)do if s(t)=="table"then e:tag("option",{label=t.label}):tag("value"):text(t.value):up():up(); if t.default then a[#a+1]=t.value; end else e:tag("option",{label=t}):tag("value"):text(t):up():up(); end end if not t then if o=="list-single"then t=a[1]; elseif o=="list-multi"then t=a; end end end if t~=nil then if s(t)=="number"then t=("%g"):format(t); end if o=="hidden"then if s(t)=="table"then e:tag("value") :add_child(t) :up(); else e:tag("value"):text(t):up(); end elseif o=="boolean"then e:tag("value"):text((t and"1")or"0"):up(); elseif o=="fixed"then e:tag("value"):text(t):up(); elseif o=="jid-multi"then for a,t in n(t)do e:tag("value"):text(t):up(); end elseif o=="jid-single"then e:tag("value"):text(t):up(); elseif o=="text-single"or o=="text-private"then e:tag("value"):text(t):up(); elseif o=="text-multi"then for t in t:gmatch("([^\r\n]+)\r?\n*")do e:tag("value"):text(t):up(); end elseif o=="list-single"then e:tag("value"):text(t):up(); elseif o=="list-multi"then for a,t in n(t)do e:tag("value"):text(t):up(); end end end local t=a.media; if t then e:tag("media",{xmlns="urn:xmpp:media-element",height=("%g"):format(t.height),width=("%g"):format(t.width)}); for a,t in n(t)do e:tag("uri",{type=t.type}):text(t.uri):up() end e:up(); end if i=="form"and a.required then e:tag("required"):up(); end e:up(); end return e; end local t={}; local h={}; function d.data(e,r,i) local o={}; local a={}; local s={}; for n,e in n(e)do local n; for t in r:childtags("field")do if(e.var or e.name)==t.attr.var then n=t; break; end end if not n then if i and i[e.name]~=nil then o[e.name]=i[e.name]; elseif e.required then a[e.name]="Required value missing"; end elseif e.name then s[e.name]=true; local t=t[e.type]; if t then local t,i=t(n,e.required); local n=e.datatype and h[e.datatype]; if t~=nil and n then local o,a=n(t,e); if o then t=a; else t,i=nil,a or("Invalid value for data of type "..e.datatype); end end o[e.name],a[e.name]=t,i; end end end if p(a)then return o,a,s; end return o,nil,s; end local function e(e,a) local e=e:get_child_text("value"); if a and(e==nil or e=="")then return nil,"Required value missing"; end return e; end t["text-single"]=e; t["text-private"]=e; t["jid-single"]= function(o,a) local e,a=e(o,a); if not e then return e,a;end local a=m(e); if not a then return nil,"Invalid JID: "..e; end return a; end t["jid-multi"]= function(o,i) local a={}; local e={}; for t in o:childtags("value")do local t=t:get_text(); local o=m(t); a[#a+1]=o; if t and not o then e[#e+1]=("Invalid JID: "..t); end end if#a>0 then return a,(#e>0 and l(e,"\n")or nil); elseif i then return nil,"Required value missing"; end end t["list-multi"]= function(o,a) local e={}; for t in o:childtags("value")do e[#e+1]=t:get_text(); end if#e>0 then return e; elseif a then return nil,"Required value missing"; end end t["text-multi"]= function(a,e) local e,a=t["list-multi"](a,e); if e then e=l(e,"\n"); end return e,a; end t["list-single"]=e; local a={ ["1"]=true,["true"]=true, ["0"]=false,["false"]=false, }; t["boolean"]= function(o,i) local e,o=e(o,i); if not e then return e,o;end local a=a[e]; if a==nil then return nil,"Invalid boolean representation:"..e; end return a; end t["hidden"]= function(e) return e:get_child_text("value"); end h["xs:integer"]= function(e,t) local e=u(e); if not e then return false,"not a number"; elseif e%1~=0 then return false,"not an integer"; end if t.range_max and e>t.range_max then return false,"out of bounds"; elseif t.range_min and e<t.range_min then return false,"out of bounds"; end return true,e; end local function t(e) if not c.is_stanza(e)then return nil,"not a stanza object"; elseif e.attr.xmlns~="jabber:x:data"or e.name~="x"then return nil,"not a dataform element"; end for e in e:childtags("field")do if e.attr.var=="FORM_TYPE"then return e:get_child_text("value"); end end return""; end return{ new=w; from_stanza=v; get_type=t; }; end) package.preload['util.caps']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local l=require"util.encodings".base64.encode; local u=require"util.hashes".sha1; local n,s,h=table.insert,table.sort,table.concat; local r=ipairs; local _ENV=nil; local function d(e) local i,o,a={},{},{}; for t,e in r(e)do if e.name=="identity"then n(i,(e.attr.category or"").."\0"..(e.attr.type or"").."\0"..(e.attr["xml:lang"]or"").."\0"..(e.attr.name or"")); elseif e.name=="feature"then n(o,e.attr.var or""); elseif e.name=="x"and e.attr.xmlns=="jabber:x:data"then local t={}; local o; for a,e in r(e.tags)do if e.name=="field"and e.attr.var then local a={}; for t,e in r(e.tags)do e=#e.tags==0 and e:get_text(); if e then n(a,e);end end s(a); if e.attr.var=="FORM_TYPE"then o=a[1]; elseif#a>0 then n(t,e.attr.var.."\0"..h(a,"<")); else n(t,e.attr.var); end end end s(t); t=h(t,"<"); if o then t=o.."\0"..t;end n(a,t); end end s(i); s(o); s(a); if#i>0 then i=h(i,"<"):gsub("%z","/").."<";else i="";end if#o>0 then o=h(o,"<").."<";else o="";end if#a>0 then a=h(a,"<"):gsub("%z","<").."<";else a="";end local e=i..o..a; local t=l(u(e)); return t,e; end return{ calculate_hash=d; }; end) package.preload['util.vcard']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local o=require"util.stanza"; local a,c=table.insert,table.concat; local h=type; local e,s,m=next,pairs,ipairs; local u,l,d,r; local f="\n"; local i; local function e() error"Not implemented" end local function e() error"Not implemented" end local function w(e) return e:gsub("[,:;\\]","\\%1"):gsub("\n","\\n"); end local function y(e) return e:gsub("\\?[\\nt:;,]",{ ["\\\\"]="\\", ["\\n"]="\n", ["\\r"]="\r", ["\\t"]="\t", ["\\:"]=":", ["\\;"]=";", ["\\,"]=",", [":"]="\29", [";"]="\30", [","]="\31", }); end local function p(t) local a=o.stanza(t.name,{xmlns="vcard-temp"}); local e=i[t.name]; if e=="text"then a:text(t[1]); elseif h(e)=="table"then if e.types and t.TYPE then if h(t.TYPE)=="table"then for o,e in s(e.types)do for o,t in s(t.TYPE)do if t:upper()==e then a:tag(e):up(); break; end end end else a:tag(t.TYPE:upper()):up(); end end if e.props then for o,e in s(e.props)do if t[e]then a:tag(e):up(); end end end if e.value then a:tag(e.value):text(t[1]):up(); elseif e.values then local o=e.values; local i=o.behaviour=="repeat-last"and o[#o]; for o=1,#t do a:tag(e.values[o]or i):text(t[o]):up(); end end end return a; end local function n(t) local e=o.stanza("vCard",{xmlns="vcard-temp"}); for a=1,#t do e:add_child(p(t[a])); end return e; end function r(e) if not e[1]or e[1].name then return n(e) else local t=o.stanza("xCard",{xmlns="vcard-temp"}); for a=1,#e do t:add_child(n(e[a])); end return t; end end function u(t) t=t :gsub("\r\n","\n") :gsub("\n ","") :gsub("\n\n+","\n"); local h={}; local e; for t in t:gmatch("[^\n]+")do local t=y(t); local s,t,n=t:match("^([-%a]+)(\30?[^\29]*)\29(.*)$"); n=n:gsub("\29",":"); if#t>0 then local a={}; for e,o,i in t:gmatch("\30([^=]+)(=?)([^\30]*)")do e=e:upper(); local t={}; for e in i:gmatch("[^\31]+")do t[#t+1]=e t[e]=true; end if o=="="then a[e]=t; else a[e]=true; end end t=a; end if s=="BEGIN"and n=="VCARD"then e={}; h[#h+1]=e; elseif s=="END"and n=="VCARD"then e=nil; elseif e and i[s]then local o=i[s]; local i={name=s}; e[#e+1]=i; local s=e; e=i; if o.types then for o,a in m(o.types)do local a=a:lower(); if(t.TYPE and t.TYPE[a]==true) or t[a]==true then e.TYPE=a; end end end if o.props then for o,a in m(o.props)do if t[a]then if t[a]==true then e[a]=true; else for o,t in m(t[a])do e[a]=t; end end end end end if o=="text"or o.value then a(e,n); elseif o.values then local t="\30"..n; for t in t:gmatch("\30([^\30]*)")do a(e,t); end end e=s; end end return h; end local function n(t) local e={}; for a=1,#t do e[a]=w(t[a]); end e=c(e,";"); local a=""; for t,e in s(t)do if h(t)=="string"and t~="name"then a=a..(";%s=%s"):format(t,h(e)=="table"and c(e,",")or e); end end return("%s%s:%s"):format(t.name,a,e) end local function o(t) local e={}; a(e,"BEGIN:VCARD") for o=1,#t do a(e,n(t[o])); end a(e,"END:VCARD") return c(e,f); end function l(e) if e[1]and e[1].name then return o(e) else local t={}; for a=1,#e do t[a]=o(e[a]); end return c(t,f); end end local function n(o) local e=o.name; local t=i[e]; local e={name=e}; if t=="text"then e[1]=o:get_text(); elseif h(t)=="table"then if t.value then e[1]=o:get_child_text(t.value)or""; elseif t.values then local t=t.values; if t.behaviour=="repeat-last"then for t=1,#o.tags do a(e,o.tags[t]:get_text()or""); end else for i=1,#t do a(e,o:get_child_text(t[i])or""); end end elseif t.names then local t=t.names; for a=1,#t do if o:get_child(t[a])then e[1]=t[a]; break; end end end if t.props_verbatim then for a,t in s(t.props_verbatim)do e[a]=t; end end if t.types then local t=t.types; e.TYPE={}; for i=1,#t do if o:get_child(t[i])then a(e.TYPE,t[i]:lower()); end end if#e.TYPE==0 then e.TYPE=nil; end end if t.props then local t=t.props; for i=1,#t do local t=t[i] local o=o:get_child_text(t); if o then e[t]=e[t]or{}; a(e[t],o); end end end else return nil end return e; end local function o(e) local e=e.tags; local t={}; for o=1,#e do a(t,n(e[o])); end return t end function d(e) if e.attr.xmlns~="vcard-temp"then return nil,"wrong-xmlns"; end if e.name=="xCard"then local a={}; local t=e.tags; for e=1,#t do a[e]=o(t[e]); end return a elseif e.name=="vCard"then return o(e) end end i={ VERSION="text", FN="text", N={ values={ "FAMILY", "GIVEN", "MIDDLE", "PREFIX", "SUFFIX", }, }, NICKNAME="text", PHOTO={ props_verbatim={ENCODING={"b"}}, props={"TYPE"}, value="BINVAL", }, BDAY="text", ADR={ types={ "HOME", "WORK", "POSTAL", "PARCEL", "DOM", "INTL", "PREF", }, values={ "POBOX", "EXTADD", "STREET", "LOCALITY", "REGION", "PCODE", "CTRY", } }, LABEL={ types={ "HOME", "WORK", "POSTAL", "PARCEL", "DOM", "INTL", "PREF", }, value="LINE", }, TEL={ types={ "HOME", "WORK", "VOICE", "FAX", "PAGER", "MSG", "CELL", "VIDEO", "BBS", "MODEM", "ISDN", "PCS", "PREF", }, value="NUMBER", }, EMAIL={ types={ "HOME", "WORK", "INTERNET", "PREF", "X400", }, value="USERID", }, JABBERID="text", MAILER="text", TZ="text", GEO={ values={ "LAT", "LON", }, }, TITLE="text", ROLE="text", LOGO="copy of PHOTO", AGENT="text", ORG={ values={ behaviour="repeat-last", "ORGNAME", "ORGUNIT", } }, CATEGORIES={ values="KEYWORD", }, NOTE="text", PRODID="text", REV="text", SORTSTRING="text", SOUND="copy of PHOTO", UID="text", URL="text", CLASS={ names={ "PUBLIC", "PRIVATE", "CONFIDENTIAL", }, }, KEY={ props={"TYPE"}, value="CRED", }, DESC="text", }; i.LOGO=i.PHOTO; i.SOUND=i.PHOTO; return{ from_text=u; to_text=l; from_xep54=d; to_xep54=r; lua_to_text=l; lua_to_xep54=r; text_to_lua=u; text_to_xep54=function(...)return r(u(...));end; xep54_to_lua=d; xep54_to_text=function(...)return l(d(...))end; }; end) package.preload['util.logger']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local o=pairs; local c=ipairs; local u=require; local l=table.remove; local _ENV=nil; local e={}; local t; local function h(e) local a=t(e,"debug"); local o=t(e,"info"); local i=t(e,"warn"); local n=t(e,"error"); return function(t,e,...) if t=="debug"then return a(e,...); elseif t=="info"then return o(e,...); elseif t=="warn"then return i(e,...); elseif t=="error"then return n(e,...); end end end function t(o,a) local t=e[a]; if not t then t={}; e[a]=t; end local e=function(e,...) for i=1,#t do t[i](o,a,e,...); end end return e; end local function d() for t,e in o(e)do for t=1,#e do e[t]=nil; end end end local function i(t,a) if not e[t]then e[t]={a}; else e[t][#e[t]+1]=a; end end local function r(s,n) local a=u"util.format".format; local function e(o,t,e,...) return s(o,t,a(e,...)); end for a,t in c(n or{"debug","info","warn","error"})do i(t,e); end return e; end local function n(i) local a; for t,e in o(e)do for t=#e,1,-1 do if e[t]==i then l(e,t); a=true; end end end return a; end return{ init=h; make_logger=t; reset=d; add_level_sink=i; add_simple_sink=r; new=t; remove_sink=n; }; end) package.preload['util.datetime']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local e=os.date; local i=os.time; local r=os.difftime; local t=math.floor; local s=tonumber; local _ENV=nil; local function d(a) return e("!%Y-%m-%d",a and t(a)or nil); end local function l(a) if a==nil or a%1==0 then return e("!%Y-%m-%dT%H:%M:%SZ",a); end local o=a%1; local a=t(a); return e("!%Y-%m-%dT%H:%M:%S.%%06dZ",a):format(t(o*1e6)); end local function c(a) if a==nil or a%1==0 then return e("!%H:%M:%S",a); end local o=a%1; local a=t(a); return e("!%H:%M:%S.%%06d",a):format(t(o*1e6)); end local function u(a) return e("!%Y%m%dT%H:%M:%S",a and t(a)or nil); end local function m(a) if a then 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+%-]?.*)$"); if h then local o=i(); local r=r(i(e("*t",o)),i(e("!*t",o))); local o=0; if n~=""and n~="Z"then local a,t,e=n:match("([+%-])(%d%d):?(%d*)"); if not a then return;end if#e~=2 then e="0";end t,e=s(t),s(e); o=t*60*60+e*60; if a=="-"then o=-o;end end local e=a%1; a=t(a+r)-o; return i({year=h,month=l,day=d,hour=u,min=c,sec=a,isdst=false})+e; end end end return{ date=d; datetime=l; time=c; legacy=u; parse=m; }; end) package.preload['util.json']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local p=type; local t,y,c=table.insert,table.concat,table.remove; local n=string.char; local z,d=tostring,tonumber; local u,r,j=pairs,ipairs,require"util.iterators".sorted_pairs; local i=next; local b,m=getmetatable,setmetatable; local l=print; local a,e=pcall(require,"util.array"); local v=a and b(e())or{}; local a={}; local h=m({},{__tostring=function()return"null";end;}); a.null=h; local f={ ["\""]="\\\"",["\\"]="\\\\",["\b"]="\\b", ["\f"]="\\f",["\n"]="\\n",["\r"]="\\r",["\t"]="\\t"}; for t=0,31 do local e=n(t); if not f[e]then f[e]=("\\u%.4X"):format(t);end end local function q(e) if e<128 then return n(e);end local t=e%64; if e<2048 then local e=(e-t)/64; return n(128+64+e,128+t); end local a=e%4096; local o=(a-t)/64; local e=(e-a)/4096; return n(128+64+32+e,128+o,128+t); end local k={ number=true, string=true, table=true, boolean=true }; local x={ __array=true; __hash=true; }; local o,g,s,w; function w(a,e) t(e,"\""..(a:gsub(".",f)).."\""); end function s(a,e) t(e,"["); if i(a)then for i,a in r(a)do o(a,e); t(e,","); end c(e); end t(e,"]"); end function g(l,e) local a={}; local n={}; local d={}; for e,t in r(l)do a[e]=t; end for e,t in u(l)do local o,i=p(e),p(t); if k[i]or t==h then if o=="string"and not x[e]then d[e]=t; elseif(k[o]or e==h)and a[e]==nil then n[e]=t; end end end if i(n)~=nil or i(d)~=nil or i(a)==nil then t(e,"{"); local r=#e; local h=e.ordered and j or u; for a,i in h(d)do w(a,e); t(e,":"); o(i,e); t(e,","); end if i(n)~=nil then t(e,"\"__hash\":["); for a,i in u(n)do o(a,e); t(e,","); o(i,e); t(e,","); end c(e); t(e,"]"); t(e,","); end if i(a)then t(e,"\"__array\":"); s(a,e); t(e,","); end if r~=#e then c(e);end t(e,"}"); else s(a,e); end end function o(e,a) local o=p(e); if e==h then t(a,"null"); elseif o=="number"then t(a,z(e)); elseif o=="string"then w(e,a); elseif o=="table"then local t=b(e); if t==v then s(e,a); else g(e,a); end elseif o=="boolean"then t(a,(e and"true"or"false")); else t(a,"null"); end end function a.encode(t) local e={}; o(t,e); return y(e); end function a.encode_ordered(t) local e={ordered=true}; o(t,e); return y(e); end function a.encode_array(t) local e={}; s(t,e); return y(e); end local function o(t,e) return t:find("[^ \t\r\n]",e)or e; end local function c(e) local a=e.__array; if a then e.__array=nil; for o,a in r(a)do t(e,a); end end local a=e.__hash; if a then e.__hash=nil; local t; for o,a in r(a)do if t~=nil then e[t]=a;t=nil; else t=a; end end end return e; end local s,r; local function u(t,e) local i={}; while true do local n,a; e=o(t,e+1); if t:byte(e)~=34 then if t:byte(e)==125 then return i,e+1;end return nil,"key expected"; end n,e=r(t,e); if n==nil then return nil,e;end e=o(t,e); if t:byte(e)~=58 then return nil,"colon expected";end a,e=s(t,e+1); if a==nil then return nil,e;end i[n]=a; e=o(t,e); local t=t:byte(e); if t==125 then return c(i),e+1;end if t~=44 then return nil,"object eof";end end end local function c(n,e) local i={}; while true do local a,h; a,e,h=s(n,e+1,93); if a==nil then if h then if#i~=0 then return nil,"value expected"; end a,e=m(i,v),e+1; end return a,e; end t(i,a); e=o(n,e); local t=n:byte(e); if t==93 then return m(i,v),e+1;end if t~=44 then return nil,"array eof";end end end local t; local function i(e) local t,e=d(e:sub(3,6),16),d(e:sub(9,12),16); local e=t*1024+e-56613888; local o=e%64; e=(e-o)/64; local t=e%64; e=(e-t)/64; local a=e%64; e=(e-a)/64; return n(240+e,128+a,128+t,128+o); end local function n(e) e=e:match("%x%x%x%x",3); if e then local e=d(e,16) if e>=55296 and e<=57343 then t=true;end return q(e); end t=true; end function r(o,e) e=e+1; local a=o:find("\"",e,true); if a then local e=o:sub(e,a-1); t=nil; e=e:gsub("\\u[dD][89abAB]%x%x\\u[dD][cdefCDEF]%x%x",i); e=e:gsub("\\u.?.?.?.?",n); if t then return nil,"invalid escape";end return e,a+1; end return nil,"string eof"; end local function i(e,t) local e=e:match("[0-9%.%-eE%+]+",t); return d(e),t+#e; end local function d(t,e) local o,a,t=t:byte(e+1,e+3); if o==117 and a==108 and t==108 then return h,e+4; end return nil,"null parse failed"; end local function n(t,e) local o,t,a=t:byte(e+1,e+3); if o==114 and t==117 and a==101 then return true,e+4; end return nil,"true parse failed"; end local function h(t,e) local o,a,t,i=t:byte(e+1,e+4); if o==97 and a==108 and t==115 and i==101 then return false,e+5; end return nil,"false parse failed"; end function s(a,e,s) e=o(a,e); local t=a:byte(e); if t==123 then return u(a,e); elseif t==91 then return c(a,e); elseif t==34 then return r(a,e); elseif t~=nil and t>=48 and t<=57 or t==45 then return i(a,e); elseif t==110 then return d(a,e); elseif t==116 then return n(a,e); elseif t==102 then return h(a,e); elseif t==s then return nil,e,true; else return nil,"value expected"; end end local t={ ["\\\""]="\\u0022"; ["\\\\"]="\\u005c"; ["\\/"]="\\u002f"; ["\\b"]="\\u0008"; ["\\f"]="\\u000C"; ["\\n"]="\\u000A"; ["\\r"]="\\u000D"; ["\\t"]="\\u0009"; ["\\u"]="\\u"; }; function a.decode(e) e=e:gsub("\\.",t) local t,a=s(e,1); if t==nil then return t,a;end if e:find("[^ \t\r\n]",a)then return nil,"garbage at eof";end return t; end function a.test(e) local e=a.encode(e); local t=a.decode(e); local t=a.encode(t); if e~=t then l("FAILED"); l("encoded:",e); l("recoded:",t); else l(e); end return e==t; end return a; end) package.preload['util.xml']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=require"util.stanza"; local u=require"lxp"; local i=table.insert; local r=table.remove; local c=error; local _ENV=nil; local e=(function() local d={ ["http://www.w3.org/XML/1998/namespace"]="xml"; }; local n="\1"; local h="^([^"..n.."]*)"..n.."?(.*)$"; return function(l,o) local e={}; local a=t.stanza("root"); local t={}; local s={}; function e:StartNamespaceDecl(e,a) if e~=nil then i(t,a); i(s,e); end end function e:EndNamespaceDecl(e) if e~=nil then r(t); r(s); end end function e:StartElement(o,e) local o,i=o:match(h); if i==""then o,i="",o; end if o~=""then e.xmlns=o; end for t=1,#e do local a=e[t]; e[t]=nil; local t,o=a:match(h); if o~=""then t=d[t]; if t then e[t..":"..o]=e[a]; e[a]=nil; end end end local o={} for e=1,#t do o[s[e]]=t[e]; end a:tag(i,e,o); end function e:CharacterData(e) a:text(e); end function e:EndElement() a:up(); end local function t(t) if not t.stop or not t:stop()then c("Failed to abort parsing"); end end e.StartDoctypeDecl=t; if not o or not o.allow_comments then e.Comment=t; end if not o or not o.allow_processing_instructions then e.ProcessingInstruction=t; end local t=u.new(e,n); local e,i,o,n=t:parse(l); if e then e,i,o,n=t:parse();end if e then return a.tags[1]; else return e,("%s (line %d, col %d))"):format(i,o,n); end end; end)(); return{ parse=e; }; end) package.preload['util.rsm']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local s=require"util.stanza".stanza; local t,o=tostring,tonumber; local h=type; local n=pairs; local i='http://jabber.org/protocol/rsm'; local a={}; do local e=a; local function t(e) return o((e:get_text())); end local function a(t) return t:get_text(); end e.after=a; e.before=function(e) local e=e:get_text(); return e==""or e; end; e.max=t; e.index=t; e.first=function(e) return{index=o(e.attr.index);e:get_text()}; end; e.last=a; e.count=t; end local h=setmetatable({ first=function(a,e) if h(e)=="table"then a:tag("first",{index=e.index}):text(e[1]):up(); else a:tag("first"):text(t(e)):up(); end end; before=function(a,e) if e==true then a:tag("before"):up(); else a:tag("before"):text(t(e)):up(); end end },{ __index=function(a,e) return function(a,o) a:tag(e):text(t(o)):up(); end end; }); local function t(e) local o={}; for t in e:childtags()do local e=t.name; local a=e and a[e]; if a then o[e]=a(t); end end return o; end local function o(t) local e=s("set",{xmlns=i}); for t,o in n(t)do if a[t]then h[t](e,o); end end return e; end local function a(e) local e=e:get_child("set",i); if e and#e.tags>0 then return t(e); end end return{parse=t,generate=o,get=a}; end) package.preload['util.random']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local e=io.open("/dev/urandom","r"); if e then return{ seed=function()end; bytes=function(t)return e:read(t);end }; end local e=require"crypto" return e.rand; end) package.preload['util.ip']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local s=require"util.net"; local n=require"util.hex"; local a={}; local o={ __index=function(o,t) local e=a[t]; if not e then return nil;end local e=e(o); o[t]=e; return e; end, __tostring=function(e)return e.addr;end, }; o.__eq=function(t,e) if getmetatable(t)~=o or getmetatable(e)~=o then return false; end return t.packed==e.packed; end local h={ ["0"]="0000",["1"]="0001",["2"]="0010",["3"]="0011", ["4"]="0100",["5"]="0101",["6"]="0110",["7"]="0111", ["8"]="1000",["9"]="1001",["A"]="1010",["B"]="1011", ["C"]="1100",["D"]="1101",["E"]="1110",["F"]="1111", }; local function t(a,e) local i; if(not e or e=="IPv6")and a:find('%',1,true)then a,i=a:match("^(.-)%%(.*)"); end local t,n=s.pton(a); if not t then return t,n end if e=="IPv6"and#t~=16 then return nil,"invalid-ipv6"; elseif e=="IPv4"and#t~=4 then return nil,"invalid-ipv4"; elseif not e then if#t==16 then e="IPv6"; elseif#t==4 then e="IPv4"; else return nil,"unknown protocol"; end elseif e~="IPv6"and e~="IPv4"then return nil,"invalid protocol"; end return setmetatable({addr=a,packed=t,proto=e,zone=i},o); end function a:normal() return s.ntop(self.packed); end function a.bits(e) return n.encode(e.packed):upper():gsub(".",h); end function a.bits_full(e) if e.proto=="IPv4"then e=e.toV4mapped; end return e.bits; end local e; local function v(t,a) t,a=t.bits_full,a.bits_full; for e=1,128 do if t:sub(e,e)~=a:sub(e,e)then return e-1; end end return 128; end local n=t("::1"); local w=t("127.0.0.0"); local h=t("2002::"); local d=t("2001::"); local p=t("fe80::"); local m=t("169.254.0.0"); local r=t("fc00::"); local i=t("fec0::"); local l=t("3ffe::"); local u=t("::"); local f=t("ff00::"); local c=t("::ffff:0:0"); local function y(t) if e(t,w,8)then return 2; elseif e(t,m,16)then return 2; else return 14; end end local function w(t) if t==n then return 2; elseif e(t,p,10)then return 2; elseif e(t,i,10)then return 5; elseif e(t,f,10)then return t.packed:byte(2)%16; else return 14; end end local function m(t) if t==n then return 0; elseif e(t,h,16)then return 2; elseif e(t,d,32)then return 5; elseif e(t,r,7)then return 13; elseif e(t,i,10)then return 11; elseif e(t,l,16)then return 12; elseif e(t,u,96)then return 3; elseif e(t,c,96)then return 4; else return 1; end end local function f(t) if t==n then return 50; elseif e(t,h,16)then return 30; elseif e(t,d,32)then return 5; elseif e(t,r,7)then return 3; elseif e(t,i,10)then return 1; elseif e(t,l,16)then return 1; elseif e(t,u,96)then return 1; elseif e(t,c,96)then return 35; else return 40; end end function a:toV4mapped() if self.proto~="IPv4"then return nil,"No IPv4 address"end local e=t("::ffff:"..self.normal); return e; end function a:label() if self.proto=="IPv4"then return m(self.toV4mapped); else return m(self); end end function a:precedence() if self.proto=="IPv4"then return f(self.toV4mapped); else return f(self); end end function a:scope() if self.proto=="IPv4"then return y(self); else return w(self); end end local r=t("10.0.0.0"); local h=t("172.16.0.0"); local i=t("192.168.0.0"); local n=t("100.64.0.0"); function a:private() local t=self.scope~=14; if not t and self.proto=="IPv4"then return e(self,r,8)or e(self,h,12)or e(self,i,16)or e(self,n,10); end return t; end local function n(e) local o; local a=e:find("/",1,true); if a then o=tonumber(e:sub(a+1,-1)); e=e:sub(1,a-1); end return t(e),o; end function e(a,t,e) if not e or e>=128 or t.proto=="IPv4"and e>=32 then return a==t; elseif e<1 then return true; end if a.proto~=t.proto then if a.proto=="IPv4"then a=a.toV4mapped; elseif t.proto=="IPv4"then t=t.toV4mapped; e=e+(128-32); end end return a.bits:sub(1,e)==t.bits:sub(1,e); end local function i(e) return getmetatable(e)==o; end local function o(e,a) if a%8~=0 then return error("ip.truncate() only supports multiples of 8 bits"); end local a=a/8; if not i(e)then e=t(e); end return t(s.ntop(e.packed:sub(1,a)..("\0"):rep(#e.packed-a))) end return{ new_ip=t, commonPrefixLength=v, parse_cidr=n, match=e, is_ip=i; truncate=o; }; end) package.preload['util.hex']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local h=string.char; local s=string.format; local o=string.gsub; local n=string.lower; local a={}; local i={}; do local e,t; for o=0,255 do e,t=h(o),s("%02x",o); a[e]=t; i[t]=e; end end local function t(e) return(o(e,".",a)); end local function e(e) return(o(n(e),"%X*(%x%x)%X*",i)); end return{ encode=t,decode=e; to=t,from=e; }; end) package.preload['util.net']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end return{ pton=function(e) if e:find":"then return"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" else return"\0\0\0\0" end end } end) package.preload['util.sslconfig']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local n=type; local o=pairs; local t=rawset; local r=rawget; local c=error; local d=table.concat; local l=table.insert; local u=setmetatable; local m=require"util.paths".resolve_relative_path; local _ENV=nil; local i={}; local e={}; local f=function(e)return e end function i.options(h,s,e) local a=h[s]or{}; if n(e)~="table"then e={e}end for o,e in o(e)do if e==true or e==false then a[o]=e; else a[e]=true; end end t(h,s,a) end i.verifyext=i.options; function e.options(a) local t={}; for e,a in o(a)do if a then t[#t+1]=e; end end return t; end e.verifyext=e.options; function e.ciphers(t) if n(t)=="table"then return d(t,":"); end return t; end e.curveslist=e.ciphers; e.ciphersuites=e.ciphers; function e.key(t,a) if n(t)=="string"then return m(a._basedir,t); else return nil end end e.certificate=e.key; e.cafile=e.key; e.capath=e.key; e.dhparam=e.key; local a={"sslv2","sslv3","tlsv1","tlsv1_1","tlsv1_2","tlsv1_3"}; for e=1,#a do a[a[e].."+"]=e-1;end local function h(e) local t=a[e.protocol]; if t then e.protocol="sslv23"; for t=1,t do l(e.options,"no_"..a[t]); end end end local function s(e,a) t(e,"_cache",nil); if n(a)=="table"then for a,o in o(a)do if a:sub(1,1)~="_"then (i[a]or t)(e,a,o); end end end return e end local function n(i) local a={}; for t,o in o(i)do if t:sub(1,1)~="_"then a[t]=(e[t]or f)(o,i); end end h(a); return a; end local function i(e) local a=r(e,"_cache"); if a then return a,nil end local a,o=r(e,"_context_factory")(e:final(),e); if a then t(e,"_cache",a); end return a,o end local e={ __index={ apply=s; final=n; build=i; }; __newindex=function() c("SSL config objects cannot be modified directly. Use :apply()") end; }; local function a(t,a) return u({ _context_factory=t, _basedir=a, options={}, },e); end local function h(i) local a=a(); for e,o in o(i)do t(a,e,o); end return a end e.__index.clone=h; return{ apply=s; final=n; _new=a; }; end) package.preload['util.paths']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local i=table.concat; local t=package.config:sub(1,1); local a={} function a.resolve_relative_path(a,e) if e then a=a:gsub("%"..t.."+$",""); e=e:gsub("^%.%"..t.."+",""); local o; if t=="/"and e:sub(1,1)~="/"then o=true; elseif t=="\\"and(e:sub(1,1)~="/"and(e:sub(2,3)~=":\\"and e:sub(2,3)~=":/"))then o=true; end if o then return a..t..e; end end return e; end function a.glob_to_pattern(e) return"^"..e:gsub("[%p*?]",function(e) if e=="*"then return".*"; elseif e=="?"then return"."; else return"%"..e; end end).."$"; end function a.join(a,e,o,...) if e then if o then if...then return i({a,e,o,...},t); end return a..t..e..t..o; end return a..t..e; end return a; end function a.complement_lua_path(t) local a=_VERSION:match(" (.+)$"); local o=package.config:sub(3,3); local e=package.config:sub(1,1); local a=e.."lua"..e..a..e; if not string.find(package.path,t,1,true)then package.path=package.path..o..t..e.."share"..a.."?.lua"; package.path=package.path..o..t..e.."share"..a.."?"..e.."init.lua"; end if not string.find(package.path,t,1,true)then package.cpath=package.cpath..o..t..e.."lib"..a.."?.so"; end end return a; end) package.preload['util.mathcompat']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end if not math.type then local function t(e) if type(e)=="number"then if e%1==0 and e~=e+1 and e~=e-1 then return"integer" else return"float" end end end _G.math.type=t end end) package.preload['util.dnsregistry']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end return{ classes={ ["IN"]=1;[1]="IN"; ["CH"]=3;[3]="CH"; ["HS"]=4;[4]="HS"; ["ANY"]=255;[255]="ANY"; }; types={ ["A"]=1;[1]="A"; ["NS"]=2;[2]="NS"; ["MD"]=3;[3]="MD"; ["MF"]=4;[4]="MF"; ["CNAME"]=5;[5]="CNAME"; ["SOA"]=6;[6]="SOA"; ["MB"]=7;[7]="MB"; ["MG"]=8;[8]="MG"; ["MR"]=9;[9]="MR"; ["NULL"]=10;[10]="NULL"; ["WKS"]=11;[11]="WKS"; ["PTR"]=12;[12]="PTR"; ["HINFO"]=13;[13]="HINFO"; ["MINFO"]=14;[14]="MINFO"; ["MX"]=15;[15]="MX"; ["TXT"]=16;[16]="TXT"; ["RP"]=17;[17]="RP"; ["AFSDB"]=18;[18]="AFSDB"; ["X25"]=19;[19]="X25"; ["ISDN"]=20;[20]="ISDN"; ["RT"]=21;[21]="RT"; ["NSAP"]=22;[22]="NSAP"; ["NSAP-PTR"]=23;[23]="NSAP-PTR"; ["SIG"]=24;[24]="SIG"; ["KEY"]=25;[25]="KEY"; ["PX"]=26;[26]="PX"; ["GPOS"]=27;[27]="GPOS"; ["AAAA"]=28;[28]="AAAA"; ["LOC"]=29;[29]="LOC"; ["NXT"]=30;[30]="NXT"; ["EID"]=31;[31]="EID"; ["NIMLOC"]=32;[32]="NIMLOC"; ["SRV"]=33;[33]="SRV"; ["ATMA"]=34;[34]="ATMA"; ["NAPTR"]=35;[35]="NAPTR"; ["KX"]=36;[36]="KX"; ["CERT"]=37;[37]="CERT"; ["A6"]=38;[38]="A6"; ["DNAME"]=39;[39]="DNAME"; ["SINK"]=40;[40]="SINK"; ["OPT"]=41;[41]="OPT"; ["APL"]=42;[42]="APL"; ["DS"]=43;[43]="DS"; ["SSHFP"]=44;[44]="SSHFP"; ["IPSECKEY"]=45;[45]="IPSECKEY"; ["RRSIG"]=46;[46]="RRSIG"; ["NSEC"]=47;[47]="NSEC"; ["DNSKEY"]=48;[48]="DNSKEY"; ["DHCID"]=49;[49]="DHCID"; ["NSEC3"]=50;[50]="NSEC3"; ["NSEC3PARAM"]=51;[51]="NSEC3PARAM"; ["TLSA"]=52;[52]="TLSA"; ["SMIMEA"]=53;[53]="SMIMEA"; ["HIP"]=55;[55]="HIP"; ["NINFO"]=56;[56]="NINFO"; ["RKEY"]=57;[57]="RKEY"; ["TALINK"]=58;[58]="TALINK"; ["CDS"]=59;[59]="CDS"; ["CDNSKEY"]=60;[60]="CDNSKEY"; ["OPENPGPKEY"]=61;[61]="OPENPGPKEY"; ["CSYNC"]=62;[62]="CSYNC"; ["ZONEMD"]=63;[63]="ZONEMD"; ["SVCB"]=64;[64]="SVCB"; ["HTTPS"]=65;[65]="HTTPS"; ["SPF"]=99;[99]="SPF"; ["NID"]=104;[104]="NID"; ["L32"]=105;[105]="L32"; ["L64"]=106;[106]="L64"; ["LP"]=107;[107]="LP"; ["EUI48"]=108;[108]="EUI48"; ["EUI64"]=109;[109]="EUI64"; ["TKEY"]=249;[249]="TKEY"; ["TSIG"]=250;[250]="TSIG"; ["IXFR"]=251;[251]="IXFR"; ["AXFR"]=252;[252]="AXFR"; ["MAILB"]=253;[253]="MAILB"; ["MAILA"]=254;[254]="MAILA"; ["*"]=255;[255]="*"; ["URI"]=256;[256]="URI"; ["CAA"]=257;[257]="CAA"; ["AVC"]=258;[258]="AVC"; ["DOA"]=259;[259]="DOA"; ["AMTRELAY"]=260;[260]="AMTRELAY"; ["TA"]=32768;[32768]="TA"; ["DLV"]=32769;[32769]="DLV"; }; errors={ [0]="NoError";["NoError"]="No Error"; [1]="FormErr";["FormErr"]="Format Error"; [2]="ServFail";["ServFail"]="Server Failure"; [3]="NXDomain";["NXDomain"]="Non-Existent Domain"; [4]="NotImp";["NotImp"]="Not Implemented"; [5]="Refused";["Refused"]="Query Refused"; [6]="YXDomain";["YXDomain"]="Name Exists when it should not"; [7]="YXRRSet";["YXRRSet"]="RR Set Exists when it should not"; [8]="NXRRSet";["NXRRSet"]="RR Set that should exist does not"; [9]="NotAuth";["NotAuth"]="Server Not Authoritative for zone"; [10]="NotZone";["NotZone"]="Name not contained in zone"; [11]="DSOTYPENI";["DSOTYPENI"]="DSO-TYPE Not Implemented"; [16]="BADVERS";["BADVERS"]="Bad OPT Version"; [17]="BADKEY";["BADKEY"]="Key not recognized"; [18]="BADTIME";["BADTIME"]="Signature out of time window"; [19]="BADMODE";["BADMODE"]="Bad TKEY Mode"; [20]="BADNAME";["BADNAME"]="Duplicate key name"; [21]="BADALG";["BADALG"]="Algorithm not supported"; [22]="BADTRUNC";["BADTRUNC"]="Bad Truncation"; [23]="BADCOOKIE";["BADCOOKIE"]="Bad/missing Server Cookie"; }; }; end) package.preload['net.tls_luasec']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=require"ssl"; local h=t.newcontext; local s=t.context or require"ssl.context"; local o=io.open; local e={}; local n={__index=e}; function e:set_sni_host(a,t,e) local e,t=self._builder:clone():apply({ certificate=t, key=e, }):build(); if not e then return false,t end self._sni_contexts[a]=e._inner return true,nil end function e:remove_sni_host(e) self._sni_contexts[e]=nil end function e:wrap(e) local e,t,a=pcall(t.wrap,e,self._inner); if not e then return nil,a end return t,nil end local function i(e,i) if type(e.dhparam)=="string"then local t,a=o(e.dhparam); if not t then return nil,"Could not open DH parameters: "..a end local a=t:read("*a"); t:close(); e.dhparam=function()return a;end end local t,a=h(e); if not t then return nil,a end if t and e.ciphers then local o; o,a=s.setcipher(t,e.ciphers); if not o then return nil,a end end return setmetatable({ _inner=t, _builder=i, _sni_contexts={}, },n),nil end return{ new_context=i, }; end) package.preload['util.sasl.scram']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local n,m=require"mime".b64,require"mime".unb64; local t=require"util.hashes"; local a=require"bit"; local d=require"util.random"; local p=tonumber; local s,e=string.char,string.byte; local i=string.gsub; local h=a.bxor; local function r(a,t) return(i(a,"()(.)",function(a,o) return s(h(e(o),e(t,a))) end)); end local f,t=t.sha1,t.hmac_sha1; local function w(o,e,i) local e=t(o,e.."\0\0\0\1"); local a=e; for i=2,i do e=t(o,e); a=r(a,e); end return a; end local function y(e) return e; end local function a(e) return(i(e,"[,=]",{[","]="=2C",["="]="=3D"})); end local function s(e) if e:ssl()then local e=e:socket(); if e.info and e:info().protocol=="TLSv1.3"then if e.exportkeyingmaterial then return"p=tls-exporter",e:exportkeyingmaterial("EXPORTER-Channel-Binding",32,""); end elseif e.getfinished then return"p=tls-unique",e:getfinished(); end end end local function h(e,o) local a="n="..a(e.username); local i=n(d.bytes(15)); local h="r="..i; local l=a..","..h; local d=""; local a="n"; if o=="SCRAM-SHA-1-PLUS"then a,d=s(e.conn); elseif s(e.conn)then a="y"; end local s=a..",,"; local a=s..l; local a,u=coroutine.yield(a); if a~="challenge"then return false end local a,o,c=u:match("(r=[^,]+),s=([^,]*),i=(%d+)"); local c=p(c); o=m(o); if not a or not o or not c then return false,"Could not parse server_first_message"; elseif a:find(i,3,true)~=3 then return false,"nonce sent by server does not match our nonce"; elseif a==h then return false,"server did not append s-nonce to nonce"; end local i=s..d; local i="c="..n(i); local h=i..","..a; local i; local a; local s; if e.client_key and e.server_key then a=e.client_key; s=e.server_key; else if e.salted_password then i=e.salted_password; elseif e.password then i=w(y(e.password),o,c); end s=t(i,"Server Key"); a=t(i,"Client Key"); end local o=f(a); local e=l..","..u..","..h; local o=t(o,e); local a=r(a,o); local t=t(s,e); local e="p="..n(a); local e=h..","..e; local e,a=coroutine.yield(e); if e~="success"then return false,"success-expected"end local e=a:match("v=([^,]+)"); if m(e)~=t then return false,"server signature did not match"; end return true; end return function(e,t) if e.username and(e.password or(e.client_key or e.server_key))then if t=="SCRAM-SHA-1"then return h,99; elseif t=="SCRAM-SHA-1-PLUS"then if s(e.conn)then return h,100; end end end end end) package.preload['util.sasl.plain']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end return function(e,t) if t=="PLAIN"and e.username and e.password then return function(e) return"success"==coroutine.yield("\0"..e.username.."\0"..e.password); end,5; end end end) package.preload['util.sasl.anonymous']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end return function(t,e) if e=="ANONYMOUS"then return function() return coroutine.yield()=="success"; end,0; end end end) package.preload['util.sasl.oauthbearer']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end return function(a,e) if e=="OAUTHBEARER"and a.username then return function(e) local t=e.bearer_token and("Bearer "..e.bearer_token)or""; local t,a=coroutine.yield("n,a="..e.username.."@"..e.host..",\001auth="..t.."\001"); if t=="success"then return true; elseif t=="challenge"then e:event("oauth-failure",{ json=a; }); if coroutine.yield("\001")~="failure"then error("Unexpected SASL state: expected failure after challenge"); end return false; end end,a.bearer_token and 6 or 4; end end end) package.preload['verse.plugins.tls']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"verse"; local t="urn:ietf:params:xml:ns:xmpp-tls"; function a.plugins.tls(e) local function i(o) if e.authenticated then return;end if o:get_child("starttls",t)and e.conn.starttls then e:debug("Negotiating TLS..."); e:send(a.stanza("starttls",{xmlns=t})); return true; elseif not e.conn.starttls and not e.secure then e:warn("SSL library (LuaSec) not loaded, so TLS not available"); elseif not e.secure then e:debug("Server doesn't offer TLS :("); end end local function o(t) if t.name=="proceed"then e:debug("Server says proceed, handshake starting..."); local t=a.tls_builder(".") :apply({mode="client",protocol="sslv23",options="no_sslv2",capath="/etc/ssl/certs"}) :apply(e.ssl or{}); e.conn:starttls(t:build(),true); end end local function a(t) if t=="ssl-handshake-complete"then e.secure=true; e:debug("Re-opening stream..."); e:reopen(); end end e:hook("stream-features",i,400); e:hook("stream/"..t,o); e:hook("status",a,400); return true; end end) package.preload['verse.plugins.sasl']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local i=require"verse"; local h,r=require"mime".b64,require"mime".unb64; local o="urn:ietf:params:xml:ns:xmpp-sasl"; function i.plugins.sasl(e) local function d(t) if e.authenticated then return;end e:debug("Authenticating with SASL..."); local t=t:get_child("mechanisms",o); if not t then return end local a={}; local n={}; local s={}; for t in t:childtags("mechanism")do t=t:get_text(); e:debug("Server offers %s",t); s[t]=true; if not a[t]then local i=t:match("[^-]+"); local s,o=pcall(require,"util.sasl."..i:lower()); if s then e:debug("Loaded SASL %s module",i); a[t],n[t]=o(e,t); elseif not tostring(o):match("not found")then e:debug("Loading failed: %s",tostring(o)); end end end local t={}; for e in pairs(a)do table.insert(t,e); end if not t[1]then e:event("authentication-failure",{condition="no-supported-sasl-mechanisms",mechanisms=s}); e:close(); return; end table.sort(t,function(t,e)return n[t]>n[e];end); local t,n=t[1]; e:debug("Selecting %s mechanism...",t); e.sasl_mechanism=coroutine.wrap(a[t]); n=e:sasl_mechanism(t); local t=i.stanza("auth",{xmlns=o,mechanism=t}); if n then t:text(h(n)); end e:send(t); return true; end local function a(t) if t.name=="failure"then local a=t.tags[1]; local t=t:get_child_text("text"); e:event("authentication-failure",{condition=a.name,text=t}); e:close(); return false; end local t,a=e.sasl_mechanism(t.name,r(t:get_text())); if not t then e:event("authentication-failure",{condition=a}); e:close(); return false; elseif t==true then e:event("authentication-success"); e.authenticated=true e:reopen(); else e:send(i.stanza("response",{xmlns=o}):text(h(t))); end return true; end e:hook("stream-features",d,300); e:hook("stream/"..o,a); return true; end end) package.preload['verse.plugins.bind']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=require"verse"; local i=require"util.jid"; local a="urn:ietf:params:xml:ns:xmpp-bind"; function t.plugins.bind(e) local function o(o) if e.bound then return;end e:debug("Binding resource..."); e:send_iq(t.iq({id="bind",type="set"}):tag("bind",{xmlns=a}):tag("resource"):text(e.resource), function(t) if t.attr.type=="result"then local t=t :get_child("bind",a) :get_child_text("jid"); e.username,e.host,e.resource=i.split(t); e.jid,e.bound=t,true; e:event("bind-success",{jid=t}); elseif t.attr.type=="error"then local a=t:child_with_name("error"); local t,a,o=t:get_error(); e:event("bind-failure",{error=a,text=o,type=t}); end end); end e:hook("stream-features",o,200); return true; end end) package.preload['verse.plugins.smacks']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local n=require"verse"; local r=require"socket".gettime; local s="urn:xmpp:sm:3"; function n.plugins.smacks(e) local t=nil; local a=nil; local h=nil; local o; local i=nil; local function m(t) if i and(t.attr.xmlns=="jabber:client"or not t.attr.xmlns)then i=i+1; e:debug("Increasing handled stanzas to %d for %s",i,t:top_tag()); end end local function c(a) if t and(a.name and not a.attr.xmlns)then t[#t+1]=tostring(a); h=r(); if not o then o=true; e:debug("Waiting to send ack request..."); n.add_task(1,function() if#t==0 then o=false; return; end local a=r()-h; if a<1 and#t<10 then return 1-a; end e:debug("Time up, sending <r>..."); o=false; e:send(n.stanza("r",{xmlns=s})); end); end end end local function u() e:debug("smacks: connection lost"); e.stream_management_supported=nil; if e.resumption_token then e:debug("smacks: have resumption token, reconnecting in 1s..."); e.authenticated=nil; n.add_task(1,function() e:connect(e.connect_host or e.host,e.connect_port or 5222); end); return true; end end local function d() e.resumption_token=nil; end local function l(o) if o.name=="r"then e:debug("Ack requested... acking %d handled stanzas",i); e:send(n.stanza("a",{xmlns=s,h=tostring(i)})); elseif o.name=="a"then local o=tonumber(o.attr.h); if o>a then local i=#t; for a=a+1,o do table.remove(t,1); end e:debug("Received ack: New ack: "..o.." Last ack: "..a.." Unacked stanzas now: "..#t.." (was "..i..")"); a=o; elseif o<a then e:warn("Received bad ack for "..o.." when last ack was "..a); end elseif o.name=="enabled"then i=0; e.pre_smacks_features=nil; if o.attr.id then e.resumption_token=o.attr.id; end elseif o.name=="resumed"then e.pre_smacks_features=nil; local o=tonumber(o.attr.h); if o>a then local i=#t; for a=a+1,o do table.remove(t,1); end e:debug("Received ack: New ack: "..o.." Last ack: "..a.." Unacked stanzas now: "..#t.." (was "..i..")"); a=o; end for a=1,#t do e:send(t[a]); end t={}; e:debug("Resumed successfully"); e:event("resumed"); elseif o.name=="failed"then e.bound=nil e.smacks=nil a=nil i=nil t={}; local t=e.pre_smacks_features; e.pre_smacks_features=nil; e:event("stream-features",t); else e:warn("Don't know how to handle "..s.."/"..o.name); end end local function o() if e.stream_management_supported and not e.smacks then e:debug("smacks: sending enable"); t={}; a=0; h=r(); e:send(n.stanza("enable",{xmlns=s,resume="true"})); e.smacks=true; end end local function a(t) if t:get_child("sm",s)then e.pre_smacks_features=t; e.stream_management_supported=true; if e.smacks and e.bound then e:debug("Resuming stream with %d handled stanzas",i); e:send(n.stanza("resume",{xmlns=s, h=tostring(i),previd=e.resumption_token})); return true; else end end end e:hook("stream-features",a,250); e:hook("stream/"..s,l); e:hook("bind-success",o,1); e:hook("stanza",m); e:hook("outgoing",c); e:hook("closed",d,100); e:hook("disconnected",u,100); end end) package.preload['verse.plugins.keepalive']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=require"verse"; function t.plugins.keepalive(e) e.keepalive_timeout=e.keepalive_timeout or 300; t.add_task(e.keepalive_timeout,function() e.conn:write(" "); return e.keepalive_timeout; end); end end) package.preload['verse.plugins.disco']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=require"verse"; local e=require("mime").b64; local e=require("util.hashes").sha1; local s=require"util.caps".calculate_hash; local h="http://jabber.org/protocol/caps"; local e="http://jabber.org/protocol/disco"; local o=e.."#info"; local i=e.."#items"; function t.plugins.disco(e) e:add_plugin("presence"); local a={ __index=function(t,e) local a={identities={},features={}}; if e=="identities"or e=="features"then return t[false][e] end t[e]=a; return a; end, }; local n={ __index=function(t,a) local e={}; t[a]=e; return e; end, }; e.disco={ cache={}, info=setmetatable({ [false]={ identities={ {category='client',type='pc',name='Verse'}, }, features={ [h]=true, [o]=true, [i]=true, }, }, },a); items=setmetatable({[false]={}},n); }; e.caps={} e.caps.node='http://code.matthewwild.co.uk/verse/' local function r(a) local i=e.disco.info[a or false]; if a and a==e.caps.node.."#"..e.caps.hash then i=e.disco.info[false]; end local n,i=i.identities,i.features local e=t.stanza("query",{ xmlns=o, node=a, }); for a,t in pairs(n)do e:tag('identity',t):up() end for t in pairs(i)do e:tag('feature',{var=t}):up() end return e; end setmetatable(e.caps,{ __call=function(...) local a=s(r()) e.caps.hash=a; return t.stanza('c',{ xmlns=h, hash='sha-1', node=e.caps.node, ver=a }) end }) function e:set_identity(e,t) self.disco.info[t or false].identities={e}; end function e:add_identity(t,e) local e=self.disco.info[e or false].identities; e[#e+1]=t; end function e:add_disco_feature(e,t) local e=e.var or e; self.disco.info[t or false].features[e]=true; end function e:remove_disco_feature(e,t) local e=e.var or e; self.disco.info[t or false].features[e]=nil; end function e:add_disco_item(t,e) local e=self.disco.items[e or false]; e[#e+1]=t; end function e:remove_disco_item(a,e) local e=self.disco.items[e or false]; for t=#e,1,-1 do if e[t]==a then table.remove(e,t); end end end function e:jid_has_identity(t,e,a) local o=self.disco.cache[t]; if not o then return nil,"no-cache"; end local t=self.disco.cache[t].identities; if a then return t[e.."/"..a]or false; end for t in pairs(t)do if t:match("^(.*)/")==e then return true; end end end function e:jid_supports(e,t) local e=self.disco.cache[e]; if not e or not e.features then return nil,"no-cache"; end return e.features[t]or false; end function e:get_local_services(o,a) local e=self.disco.cache[self.host]; if not(e)or not(e.items)then return nil,"no-cache"; end local t={}; for i,e in ipairs(e.items)do if self:jid_has_identity(e.jid,o,a)then table.insert(t,e.jid); end end return t; end function e:disco_local_services(a) self:disco_items(self.host,nil,function(t) if not t then return a({}); end local e=0; local function o() e=e-1; if e==0 then return a(t); end end for a,t in ipairs(t)do if t.jid then e=e+1; self:disco_info(t.jid,nil,o); end end if e==0 then return a(t); end end); end function e:disco_info(e,a,h) local t=t.iq({to=e,type="get"}) :tag("query",{xmlns=o,node=a}); self:send_iq(t,function(t) if t.attr.type=="error"then return h(nil,t:get_error()); end local i,s,n={},{},{}; for e in t:get_child("query",o):childtags()do self:debug("DISCO {%s}%s",e.attr.xmlns or"",e.name); if e.name=="identity"then i[e.attr.category.."/"..e.attr.type]=e.attr.name or true; elseif e.name=="feature"then s[e.attr.var]=true; end end for e in t:get_child("query",o):childtags("x","jabber:x:data")do self:debug("DISCO {%s}%s",e.attr.xmlns or"",e.name); local t=e:get_child_with_attr("field",nil,"var","FORM_TYPE"); local a=t and t:get_child_text("value"); self:debug("FORM: %s (%s)",a,t); if a then n[a]=e; end end if not self.disco.cache[e]then self.disco.cache[e]={nodes={}}; end if a then if not self.disco.cache[e].nodes[a]then self.disco.cache[e].nodes[a]={nodes={}}; end self.disco.cache[e].nodes[a].identities=i; self.disco.cache[e].nodes[a].features=s; self.disco.cache[e].nodes[a].extended=n; else self.disco.cache[e].identities=i; self.disco.cache[e].features=s; self.disco.cache[e].extended=n; end return h(self.disco.cache[e]); end); end function e:disco_items(a,o,n) local t=t.iq({to=a,type="get"}) :tag("query",{xmlns=i,node=o}); self:send_iq(t,function(t) if t.attr.type=="error"then return n(nil,t:get_error()); end local e={}; for t in t:get_child("query",i):childtags()do if t.name=="item"then table.insert(e,{ name=t.attr.name; jid=t.attr.jid; node=t.attr.node; }); end end if not self.disco.cache[a]then self.disco.cache[a]={nodes={}}; end if o then if not self.disco.cache[a].nodes[o]then self.disco.cache[a].nodes[o]={nodes={}}; end self.disco.cache[a].nodes[o].items=e; else self.disco.cache[a].items=e; end return n(e); end); end e:hook("iq/"..o,function(a) local o=a.tags[1]; if a.attr.type=='get'and o.name=="query"then local o=r(o.attr.node); local t=t.reply(a):add_child(o); e:send(t); return true end end); e:hook("iq/"..i,function(a) local o=a.tags[1]; if a.attr.type=='get'and o.name=="query"then local n=e.disco.items[o.attr.node or false]; local t=t.reply(a):tag('query',{ xmlns=i, node=o.attr.node }) for a=1,#n do t:tag('item',n[a]):up() end e:send(t); return true end end); local t; e:hook("ready",function() if t then return;end t=true; local function o(t) local a=e.disco.cache[t]; if a then for a in pairs(a.identities)do local o,a=a:match("^(.*)/(.*)$"); print(t,o,a) e:event("disco/service-discovered/"..o,{ type=a,jid=t; }); end end end e:disco_info(e.host,nil,function() o(e.host); end); e:disco_local_services(function(t) for a,t in ipairs(t)do o(t.jid); end e:event("ready"); end); return true; end,50); e:hook("presence-out",function(t) t:remove_children("c",h); t:reset():add_child(e:caps()):reset(); end,10); end end) package.preload['verse.plugins.version']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local o=require"verse"; local t="jabber:iq:version"; local function a(t,e) t.name=e.name; t.version=e.version; t.platform=e.platform; end function o.plugins.version(e) e.version={set=a}; e:hook("iq/"..t,function(a) if a.attr.type~="get"then return;end local t=o.reply(a) :tag("query",{xmlns=t}); if e.version.name then t:tag("name"):text(tostring(e.version.name)):up(); end if e.version.version then t:tag("version"):text(tostring(e.version.version)):up() end if e.version.platform then t:tag("os"):text(e.version.platform); end e:send(t); return true; end); function e:query_version(i,a) a=a or function(e)return self:event("version/response",e);end self:send_iq(o.iq({type="get",to=i}) :tag("query",{xmlns=t}), function(o) if o.attr.type=="result"then local e=o:get_child("query",t); local t=e and e:get_child_text("name"); local o=e and e:get_child_text("version"); local e=e and e:get_child_text("os"); a({ name=t; version=o; platform=e; }); else local o,e,t=o:get_error(); a({ error=true; condition=e; text=t; type=o; }); end end); end return true; end end) package.preload['verse.plugins.ping']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"verse"; local s=require"socket".gettime; local o=require"util.id".short; local i="urn:xmpp:ping"; function a.plugins.ping(e) function e:ping(t,n) local h=s(); local o=o(); local a=a.iq{id=o,to=t,type="get"}:tag("ping",{xmlns=i}); e:send_iq(a, function(e) if e.attr.type=="error"then local o,e,a=e:get_error(); if e~="service-unavailable"and e~="feature-not-implemented"then n(nil,t,{type=o,condition=e,text=a}); return; end end n(s()-h,t); end); return o; end e:hook("iq/"..i,function(t) return e:send(a.reply(t)); end); return true; end end) package.preload['verse.plugins.uptime']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local o=require"verse"; local t="jabber:iq:last"; local function a(e,t) e.starttime=t.starttime; end function o.plugins.uptime(e) e.uptime={set=a}; e:hook("iq/"..t,function(a) if a.attr.type~="get"then return;end local t=o.reply(a) :tag("query",{seconds=tostring(os.difftime(os.time(),e.uptime.starttime)),xmlns=t}); e:send(t); return true; end); function e:query_uptime(i,a) a=a or function(t)return e:event("uptime/response",t);end e:send_iq(o.iq({type="get",to=i}) :tag("query",{xmlns=t}), function(e) local t=e:get_child("query",t); if e.attr.type=="result"then local e=tonumber(t.attr.seconds); a({ seconds=e or nil; }); else local o,e,t=e:get_error(); a({ error=true; condition=e; text=t; type=o; }); end end); end return true; end end) package.preload['verse.plugins.time']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local e=require"verse"; local i=require"util.datetime"; local o="urn:xmpp:time"; function e.plugins.time(t) function t:query_time(a,t) t=t or function(e)return self:event("time/response",e);end self:send_iq(e.iq({type="get",to=a}) :tag("time",{xmlns=o}), function(a) if a.attr.type=="result"then local e=a:get_child("time",o); local e={ tzo=e:get_child_text("tzo"); utc=e:get_child_text("utc"); }; if e.utc then e.timestamp=i.parse(e.utc); end t(e); else local o,a,e=a:get_error(); t({ error=true; condition=a; text=e; type=o; }); end end); end return true; end end) package.preload['verse.plugins.blocking']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"verse"; local o="urn:xmpp:blocking"; function a.plugins.blocking(e) e.blocking={}; function e.blocking:block_jid(i,t) e:send_iq(a.iq{type="set"} :tag("block",{xmlns=o}) :tag("item",{jid=i}) ,function()return t and t(true);end ,function()return t and t(false);end ); end function e.blocking:unblock_jid(i,t) e:send_iq(a.iq{type="set"} :tag("unblock",{xmlns=o}) :tag("item",{jid=i}) ,function()return t and t(true);end ,function()return t and t(false);end ); end function e.blocking:unblock_all_jids(t) e:send_iq(a.iq{type="set"} :tag("unblock",{xmlns=o}) ,function()return t and t(true);end ,function()return t and t(false);end ); end function e.blocking:get_blocked_jids(t) e:send_iq(a.iq{type="get"} :tag("blocklist",{xmlns=o}) ,function(e) local a=e:get_child("blocklist",o); if not a then return t and t(false);end local e={}; for t in a:childtags()do e[#e+1]=t.attr.jid; end return t and t(e); end ,function(e)return t and t(false);end ); end end end) package.preload['verse.plugins.jingle']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"verse"; local e=require"util.timer"; local n=require"util.id".short; local i="urn:xmpp:jingle:1"; local r="urn:xmpp:jingle:errors:1"; local t={}; t.__index=t; local e={}; local e={}; function a.plugins.jingle(e) e:hook("ready",function() e:add_disco_feature(i); end,10); function e:jingle(o) return a.eventable(setmetatable(base or{ role="initiator"; peer=o; sid=n(); stream=e; },t)); end function e:register_jingle_transport(e) end function e:register_jingle_content_type(e) end local function u(n) local s=n:get_child("jingle",i); local h=s.attr.sid; local o=s.attr.action; local h=e:event("jingle/"..h,n); if h==true then e:send(a.reply(n)); return true; end if o~="session-initiate"then local t=a.error_reply(n,"cancel","item-not-found") :tag("unknown-session",{xmlns=r}):up(); e:send(t); return; end local l=s.attr.sid; local o=a.eventable{ role="receiver"; peer=n.attr.from; sid=l; stream=e; }; setmetatable(o,t); local h; local r,d; for t in s:childtags()do if t.name=="content"and t.attr.xmlns==i then local i=t:child_with_name("description"); local a=i.attr.xmlns; if a then local e=e:event("jingle/content/"..a,o,i); if e then r=e; end end local a=t:child_with_name("transport"); local i=a.attr.xmlns; d=e:event("jingle/transport/"..i,o,a); if r and d then h=t; break; end end end if not r then e:send(a.error_reply(n,"cancel","feature-not-implemented","The specified content is not supported")); return true; end if not d then e:send(a.error_reply(n,"cancel","feature-not-implemented","The specified transport is not supported")); return true; end e:send(a.reply(n)); o.content_tag=h; o.creator,o.name=h.attr.creator,h.attr.name; o.content,o.transport=r,d; function o:decline() end e:hook("jingle/"..l,function(e) if e.attr.from~=o.peer then return false; end local e=e:get_child("jingle",i); return o:handle_command(e); end); e:event("jingle",o); return true; end function t:handle_command(a) local t=a.attr.action; e:debug("Handling Jingle command: %s",t); if t=="session-terminate"then self:destroy(); elseif t=="session-accept"then self:handle_accepted(a); elseif t=="transport-info"then e:debug("Handling transport-info"); self.transport:info_received(a); elseif t=="transport-replace"then e:error("Peer wanted to swap transport, not implemented"); else e:warn("Unhandled Jingle command: %s",t); return nil; end return true; end function t:send_command(e,o,t) local e=a.iq({to=self.peer,type="set"}) :tag("jingle",{ xmlns=i, sid=self.sid, action=e, initiator=self.role=="initiator"and self.stream.jid or nil, responder=self.role=="responder"and self.jid or nil, }):add_child(o); if not t then self.stream:send(e); else self.stream:send_iq(e,t); end end function t:accept(t) local a=a.iq({to=self.peer,type="set"}) :tag("jingle",{ xmlns=i, sid=self.sid, action="session-accept", responder=e.jid, }) :tag("content",{creator=self.creator,name=self.name}); local o=self.content:generate_accept(self.content_tag:child_with_name("description"),t); a:add_child(o); local t=self.transport:generate_accept(self.content_tag:child_with_name("transport"),t); a:add_child(t); local t=self; e:send_iq(a,function(a) if a.attr.type=="error"then local a,t,a=a:get_error(); e:error("session-accept rejected: %s",t); return false; end t.transport:connect(function(a) e:warn("CONNECTED (receiver)!!!"); t.state="active"; t:event("connected",a); end); end); end e:hook("iq/"..i,u); return true; end function t:offer(t,o) local e=a.iq({to=self.peer,type="set"}) :tag("jingle",{xmlns=i,action="session-initiate", initiator=self.stream.jid,sid=self.sid}); e:tag("content",{creator=self.role,name=t}); local t=self.stream:event("jingle/describe/"..t,o); if not t then return false,"Unknown content type"; end e:add_child(t); local t=self.stream:event("jingle/transport/".."urn:xmpp:jingle:transports:s5b:1",self); self.transport=t; e:add_child(t:generate_initiate()); self.stream:debug("Hooking %s","jingle/"..self.sid); self.stream:hook("jingle/"..self.sid,function(e) if e.attr.from~=self.peer then return false; end local e=e:get_child("jingle",i); return self:handle_command(e) end); self.stream:send_iq(e,function(e) if e.attr.type=="error"then self.state="terminated"; local e,t,a=e:get_error(); return self:event("error",{type=e,condition=t,text=a}); end end); self.state="pending"; end function t:terminate(e) local e=a.stanza("reason"):tag(e or"success"); self:send_command("session-terminate",e,function(e) self.state="terminated"; self.transport:disconnect(); self:destroy(); end); end function t:destroy() self:event("terminated"); self.stream:unhook("jingle/"..self.sid,self.handle_command); end function t:handle_accepted(e) local e=e:child_with_name("transport"); self.transport:handle_accepted(e); self.transport:connect(function(e) self.stream:debug("CONNECTED (initiator)!") self.state="active"; self:event("connected",e); end); end function t:set_source(a,o) local function t() local e,i=a(); if e and e~=""then self.transport.conn:send(e); elseif e==""then return t(); elseif e==nil then if o then self:terminate(); end self.transport.conn:unhook("drained",t); a=nil; end end self.transport.conn:hook("drained",t); t(); end function t:set_sink(t) self.transport.conn:hook("incoming-raw",t); self.transport.conn:hook("disconnected",function(e) self.stream:debug("Closing sink..."); local e=e.reason; if e=="closed"then e=nil;end t(nil,e); end); end end) package.preload['verse.plugins.jingle_ft']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local i=require"verse"; local n=require"ltn12"; local s=package.config:sub(1,1); local a="urn:xmpp:jingle:apps:file-transfer:4"; function i.plugins.jingle_ft(t) t:hook("ready",function() t:add_disco_feature(a); end,10); local o={type="file"}; function o:generate_accept(t,e) if e and e.save_file then self.jingle:hook("connected",function() local e=n.sink.file(io.open(e.save_file,"w+")); self.jingle:set_sink(e); end); end return t; end local o={__index=o}; t:hook("jingle/content/"..a,function(t,e) local e=e:get_child("file"); local e={ name=e:get_child_text("name"); size=tonumber(e:get_child_text("size")); desc=e:get_child_text("desc"); date=e:get_child_text("date"); }; return setmetatable({jingle=t,file=e},o); end); t:hook("jingle/describe/file",function(e) local t; if e.timestamp then t=os.date("!%Y-%m-%dT%H:%M:%SZ",e.timestamp); end return i.stanza("description",{xmlns=a}) :tag("file") :tag("name"):text(e.filename):up() :tag("size"):text(tostring(e.size)):up() :tag("date"):text(t):up() :tag("desc"):text(e.description):up() :up(); end); function t:send_file(i,t) local e,a=io.open(t); if not e then return e,a;end local o=e:seek("end",0); e:seek("set",0); local a=n.source.file(e); local e=self:jingle(i); e:offer("file",{ filename=t:match("[^"..s.."]+$"); size=o; }); e:hook("connected",function() e:set_source(a,true); end); return e; end end end) package.preload['verse.plugins.jingle_s5b']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"verse"; local o="urn:xmpp:jingle:transports:s5b:1"; local d="http://jabber.org/protocol/bytestreams"; local h=require"util.hashes".sha1; local l=require"util.id".short; local function r(e,s) local function n() e:unhook("connected",n); return true; end local function i(t) e:unhook("incoming-raw",i); if t:sub(1,2)~="\005\000"then return e:event("error","connection-failure"); end e:event("connected"); return true; end local function o(t) e:unhook("incoming-raw",o); if t~="\005\000"then local a="version-mismatch"; if t:sub(1,1)=="\005"then a="authentication-failure"; end return e:event("error",a); end e:send(string.char(5,1,0,3,#s)..s.."\0\0"); e:hook("incoming-raw",i,100); return true; end e:hook("connected",n,200); e:hook("incoming-raw",o,100); e:send("\005\001\000"); end local function s(o,e,i) local e=a.new(nil,{ streamhosts=e, current_host=0; }); local function t(a) if a then return o(nil,a.reason); end if e.current_host<#e.streamhosts then e.current_host=e.current_host+1; e:debug("Attempting to connect to "..e.streamhosts[e.current_host].host..":"..e.streamhosts[e.current_host].port.."..."); local a,t=e:connect( e.streamhosts[e.current_host].host, e.streamhosts[e.current_host].port ); if not a then e:debug("Error connecting to proxy (%s:%s): %s", e.streamhosts[e.current_host].host, e.streamhosts[e.current_host].port, t ); else e:debug("Connecting..."); end r(e,i); return true; end e:unhook("disconnected",t); return o(nil); end e:hook("disconnected",t,100); e:hook("connected",function() e:unhook("disconnected",t); o(e.streamhosts[e.current_host],e); end,100); t(); return e; end function a.plugins.jingle_s5b(e) e:hook("ready",function() e:add_disco_feature(o); end,10); local t={}; function t:generate_initiate() self.s5b_sid=l(); local i=a.stanza("transport",{xmlns=o, mode="tcp",sid=self.s5b_sid}); local t=0; for a,o in pairs(e.proxy65.available_streamhosts)do t=t+1; i:tag("candidate",{jid=a,host=o.host, port=o.port,cid=a,priority=t,type="proxy"}):up(); end e:debug("Have %d proxies",t) return i; end function t:generate_accept(e) local t={}; self.s5b_peer_candidates=t; self.s5b_mode=e.attr.mode or"tcp"; self.s5b_sid=e.attr.sid or self.jingle.sid; for e in e:childtags()do t[e.attr.cid]={ type=e.attr.type; jid=e.attr.jid; host=e.attr.host; port=tonumber(e.attr.port)or 0; priority=tonumber(e.attr.priority)or 0; cid=e.attr.cid; }; end local e=a.stanza("transport",{xmlns=o}); return e; end function t:connect(i) e:warn("Connecting!"); local t={}; for a,e in pairs(self.s5b_peer_candidates or{})do t[#t+1]=e; end if#t>0 then self.connecting_peer_candidates=true; local function n(e,t) self.jingle:send_command("transport-info",a.stanza("content",{creator=self.creator,name=self.name}) :tag("transport",{xmlns=o,sid=self.s5b_sid}) :tag("candidate-used",{cid=e.cid})); self.onconnect_callback=i; self.conn=t; end local e=h(self.s5b_sid..self.peer..e.jid,true); s(n,t,e); else e:warn("Actually, I'm going to wait for my peer to tell me its streamhost..."); self.onconnect_callback=i; end end function t:info_received(t) e:warn("Info received"); local n=t:child_with_name("content"); local i=n:child_with_name("transport"); if i:get_child("candidate-used")and not self.connecting_peer_candidates then local t=i:child_with_name("candidate-used"); if t then local function r(i,e) if self.jingle.role=="initiator"then self.jingle.stream:send_iq(a.iq({to=i.jid,type="set"}) :tag("query",{xmlns=d,sid=self.s5b_sid}) :tag("activate"):text(self.jingle.peer),function(i) if i.attr.type=="result"then self.jingle:send_command("transport-info",a.stanza("content",n.attr) :tag("transport",{xmlns=o,sid=self.s5b_sid}) :tag("activated",{cid=t.attr.cid})); self.conn=e; self.onconnect_callback(e); else self.jingle.stream:error("Failed to activate bytestream"); end end); end end self.jingle.stream:debug("CID: %s",self.jingle.stream.proxy65.available_streamhosts[t.attr.cid]); local t={ self.jingle.stream.proxy65.available_streamhosts[t.attr.cid]; }; local e=h(self.s5b_sid..e.jid..self.peer,true); s(r,t,e); end elseif i:get_child("activated")then self.onconnect_callback(self.conn); end end function t:disconnect() if self.conn then self.conn:close(); end end function t:handle_accepted(e) end local t={__index=t}; e:hook("jingle/transport/"..o,function(e) return setmetatable({ role=e.role, peer=e.peer, stream=e.stream, jingle=e, },t); end); end end) package.preload['verse.plugins.proxy65']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"verse"; local h=require"util.id".short; local r=require"util.hashes".sha1; local n={}; n.__index=n; local i="http://jabber.org/protocol/bytestreams"; local s; function a.plugins.proxy65(t) t.proxy65=setmetatable({stream=t},n); t.proxy65.available_streamhosts={}; local e=0; t:hook("disco/service-discovered/proxy",function(o) if o.type=="bytestreams"then e=e+1; t:send_iq(a.iq({to=o.jid,type="get"}) :tag("query",{xmlns=i}),function(a) e=e-1; if a.attr.type=="result"then local e=a:get_child("query",i) :get_child("streamhost").attr; t.proxy65.available_streamhosts[e.jid]={ jid=e.jid; host=e.host; port=tonumber(e.port); }; end if e==0 then t:event("proxy65/discovered-proxies",t.proxy65.available_streamhosts); end end); end end); t:hook("iq/"..i,function(o) local e=a.new(nil,{ initiator_jid=o.attr.from, streamhosts={}, current_host=0; }); for t in o.tags[1]:childtags()do if t.name=="streamhost"then table.insert(e.streamhosts,t.attr); end end local function i() if e.current_host<#e.streamhosts then e.current_host=e.current_host+1; e:connect( e.streamhosts[e.current_host].host, e.streamhosts[e.current_host].port ); s(t,e,o.tags[1].attr.sid,o.attr.from,t.jid); return true; end e:unhook("disconnected",i); t:send(a.error_reply(o,"cancel","item-not-found")); end function e:accept() e:hook("disconnected",i,100); e:hook("connected",function() e:unhook("disconnected",i); local e=a.reply(o) :tag("query",o.tags[1].attr) :tag("streamhost-used",{jid=e.streamhosts[e.current_host].jid}); t:send(e); end,100); i(); end function e:refuse() end t:event("proxy65/request",e); end); end function n:new(t,n) local e=a.new(nil,{ target_jid=t; bytestream_sid=h(); }); local o=a.iq{type="set",to=t} :tag("query",{xmlns=i,mode="tcp",sid=e.bytestream_sid}); for t,e in ipairs(n or self.proxies)do o:tag("streamhost",e):up(); end self.stream:send_iq(o,function(o) if o.attr.type=="error"then local t,a,o=o:get_error(); e:event("connection-failed",{conn=e,type=t,condition=a,text=o}); else local o=o.tags[1]:get_child("streamhost-used"); e.streamhost_jid=o.attr.jid; local h,o; for a,t in ipairs(n or self.proxies)do if t.jid==e.streamhost_jid then h,o=t.host,t.port; break; end end e:connect(h,o); local function o() e:unhook("connected",o); local t=a.iq{to=e.streamhost_jid,type="set"} :tag("query",{xmlns=i,sid=e.bytestream_sid}) :tag("activate"):text(t); self.stream:send_iq(t,function(t) if t.attr.type=="result"then e:event("connected",e); end end); return true; end e:hook("connected",o,100); s(self.stream,e,e.bytestream_sid,self.stream.jid,t); end end); return e; end function s(i,e,t,a,o) local t=r(t..a..o); local function a() e:unhook("connected",a); return true; end local function o(t) e:unhook("incoming-raw",o); if t:sub(1,2)~="\005\000"then return e:event("error","connection-failure"); end e:event("connected"); return true; end local function i(a) e:unhook("incoming-raw",i); if a~="\005\000"then local t="version-mismatch"; if a:sub(1,1)=="\005"then t="authentication-failure"; end return e:event("error",t); end e:send(string.char(5,1,0,3,#t)..t.."\0\0"); e:hook("incoming-raw",o,100); return true; end e:hook("connected",a,200); e:hook("incoming-raw",i,100); e:send("\005\001\000"); end end) package.preload['verse.plugins.jingle_ibb']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local e=require"verse"; local i=require"util.encodings".base64; local h=require"util.id".short; local n="urn:xmpp:jingle:transports:ibb:1"; local o="http://jabber.org/protocol/ibb"; assert(i.encode("This is a test.")=="VGhpcyBpcyBhIHRlc3Qu","Base64 encoding failed"); assert(i.decode("VGhpcyBpcyBhIHRlc3Qu")=="This is a test.","Base64 decoding failed"); local a={}; local t={__index=a}; local function s(a) local t=setmetatable({stream=a},t) t=e.eventable(t); return t; end function a:initiate(t,e,a) self.block=2048; self.stanza=a or'iq'; self.peer=t; self.sid=e or tostring(self):match("%x+$"); self.iseq=0; self.oseq=0; local e=function(e) return self:feed(e) end self.feeder=e; print("Hooking incoming IQs"); local t=self.stream; t:hook("iq/"..o,e) if a=="message"then t:hook("message",e) end end function a:open(t) self.stream:send_iq(e.iq{to=self.peer,type="set"} :tag("open",{ xmlns=o, ["block-size"]=self.block, sid=self.sid, stanza=self.stanza }) ,function(e) if t then if e.attr.type~="error"then t(true) else t(false,e:get_error()) end end end); end function a:send(n) local a=self.stanza; local t; if a=="iq"then t=e.iq{type="set",to=self.peer} elseif a=="message"then t=e.message{to=self.peer} end local e=self.oseq; self.oseq=e+1; t:tag("data",{xmlns=o,sid=self.sid,seq=e}) :text(i.encode(n)); if a=="iq"then self.stream:send_iq(t,function(e) self:event(e.attr.type=="result"and"drained"or"error"); end) else stream:send(t) self:event("drained"); end end function a:feed(t) if t.attr.from~=self.peer then return end local a=t[1]; if a.attr.sid~=self.sid then return end local n; if a.name=="open"then self:event("connected"); self.stream:send(e.reply(t)) return true elseif a.name=="data"then local o=t:get_child_text("data",o); local a=tonumber(a.attr.seq); local n=self.iseq; if o and a then if a~=n then self.stream:send(e.error_reply(t,"cancel","not-acceptable","Wrong sequence. Packet lost?")) self:close(); self:event("error"); return true; end self.iseq=a+1; local a=i.decode(o); if self.stanza=="iq"then self.stream:send(e.reply(t)) end self:event("incoming-raw",a); return true; end elseif a.name=="close"then self.stream:send(e.reply(t)) self:close(); return true end end function a:close() self.stream:unhook("iq/"..o,self.feeder) self:event("disconnected"); end function e.plugins.jingle_ibb(a) a:hook("ready",function() a:add_disco_feature(n); end,10); local t={}; function t:_setup() local e=s(self.stream); e.sid=self.sid or e.sid; e.stanza=self.stanza or e.stanza; e.block=self.block or e.block; e:initiate(self.peer,self.sid,self.stanza); self.conn=e; end function t:generate_initiate() print("ibb:generate_initiate() as "..self.role); local t=h(); self.sid=t; self.stanza='iq'; self.block=2048; local e=e.stanza("transport",{xmlns=n, sid=self.sid,stanza=self.stanza,["block-size"]=self.block}); return e; end function t:generate_accept(t) print("ibb:generate_accept() as "..self.role); local e=t.attr; self.sid=e.sid or self.sid; self.stanza=e.stanza or self.stanza; self.block=e["block-size"]or self.block; self:_setup(); return t; end function t:connect(t) if not self.conn then self:_setup(); end local e=self.conn; print("ibb:connect() as "..self.role); if self.role=="initiator"then e:open(function(a,...) assert(a,table.concat({...},", ")); t(e); end); else t(e); end end function t:info_received(e) print("ibb:info_received()"); end function t:disconnect() if self.conn then self.conn:close() end end function t:handle_accepted(e)end local t={__index=t}; a:hook("jingle/transport/"..n,function(e) return setmetatable({ role=e.role, peer=e.peer, stream=e.stream, jingle=e, },t); end); end end) package.preload['verse.plugins.pubsub']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local i=require"verse"; local h=table.insert; local o="http://jabber.org/protocol/pubsub"; local s="http://jabber.org/protocol/pubsub#owner"; local r="http://jabber.org/protocol/pubsub#event"; local e={}; local n={__index=e}; function i.plugins.pubsub(e) e.pubsub=setmetatable({stream=e},n); e:hook("message",function(t) local a=t.attr.from; for t in t:childtags("event",r)do local t=t:get_child("items"); if t then local o=t.attr.node; for t in t:childtags("item")do e:event("pubsub/event",{ from=a; node=o; item=t; }); end for t in t:childtags("retract")do e:event("pubsub/retraction",{ from=a; node=o; item=t; }); end end end end); return true; end function e:create(a,e,t) return self:service(a):node(e):create(nil,t); end function e:subscribe(o,t,a,e) return self:service(o):node(t):subscribe(a,nil,e); end function e:publish(e,t,o,a,i) return self:service(e):node(t):publish(o,nil,a,i); end local a={}; local t={__index=a}; function e:service(e) return setmetatable({stream=self.stream,service=e},t) end local function t(d,r,e,n,h,s,t,a) local e=i.iq{type=d or"get",to=r} :tag("pubsub",{xmlns=e or o}) local o={node=h,jid=s}; if a then for t,e in pairs(a)do o[t]=e; end end if n then e:tag(n,o);end if t then e:tag("item",{id=t~=true and t or nil}); end return e; end function a:subscriptions(a) self.stream:send_iq(t(nil,self.service,nil,"subscriptions") ,a and function(e) if e.attr.type=="result"then local e=e:get_child("pubsub",o); local e=e and e:get_child("subscriptions"); local o={}; if e then for e in e:childtags("subscription")do local t=self:node(e.attr.node) t.subscription=e; t.subscribed_jid=e.attr.jid; h(o,t); end end a(o); else a(false,e:get_error()); end end or nil); end function a:affiliations(a) self.stream:send_iq(t(nil,self.service,nil,"affiliations") ,a and function(e) if e.attr.type=="result"then local e=e:get_child("pubsub",o); local e=e and e:get_child("affiliations")or{}; local o={}; if e then for e in e:childtags("affiliation")do local t=self:node(e.attr.node) t.affiliation=e; h(o,t); end end a(o); else a(false,e:get_error()); end end or nil); end function a:nodes(a) self.stream:disco_items(self.service,nil,function(e,...) if e then for t=1,#e do e[t]=self:node(e[t].node); end end a(e,...) end); end local e={}; local o={__index=e}; function a:node(e) return setmetatable({stream=self.stream,service=self.service,node=e},o) end function n:__call(t,e) local t=self:service(t); return e and t:node(e)or t; end function e:hook(a,o) self._hooks=self._hooks or setmetatable({},{__mode='kv'}); local function t(e) if(not e.service or e.from==self.service)and e.node==self.node then return a(e) end end self._hooks[a]=t; self.stream:hook("pubsub/event",t,o); return t; end function e:unhook(e) if e then local e=self._hooks[e]; self.stream:unhook("pubsub/event",e); elseif self._hooks then for e in pairs(self._hooks)do self.stream:unhook("pubsub/event",e); end end end function e:create(e,a) if e~=nil then error("Not implemented yet."); else self.stream:send_iq(t("set",self.service,nil,"create",self.node),a); end end function e:configure(e,a) if e~=nil then error("Not implemented yet."); end self.stream:send_iq(t("set",self.service,nil,e==nil and"default"or"configure",self.node),a); end function e:publish(i,a,e,o) if a~=nil then error("Node configuration is not implemented yet."); end self.stream:send_iq(t("set",self.service,nil,"publish",self.node,nil,i or true) :add_child(e) ,o); end function e:subscribe(e,a,o) e=e or self.stream.jid; if a~=nil then error("Subscription configuration is not implemented yet."); end self.stream:send_iq(t("set",self.service,nil,"subscribe",self.node,e) ,o); end function e:subscription(e) error("Not implemented yet."); end function e:affiliation(e) error("Not implemented yet."); end function e:unsubscribe(e,a) e=e or self.subscribed_jid or self.stream.jid; self.stream:send_iq(t("set",self.service,nil,"unsubscribe",self.node,e) ,a); end function e:configure_subscription(e,e) error("Not implemented yet."); end function e:items(t,e) if t then return self:item(nil,e); else self.stream:disco_items(self.service,self.node,e); end end function e:item(e,a) self.stream:send_iq(t("get",self.service,nil,"items",self.node,nil,e) ,a); end function e:retract(o,e,a) if type(e)=="function"then e,a=false,e; end self.stream:send_iq( t( "set", self.service, nil, "retract", self.node, nil, o, {notify=e and"1"or nil} ), a ); end function e:purge(e,a) self.stream:send_iq( t( "set", self.service, s, "purge", self.node, nil, nil, {notify=e and"1"or nil} ), a ); end function e:delete(e,a) assert(not e,"Not implemented yet."); self.stream:send_iq(t("set",self.service,s,"delete",self.node) ,a); end end) package.preload['verse.plugins.pep']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=require"verse"; local e="http://jabber.org/protocol/pubsub"; local e=e.."#event"; function t.plugins.pep(e) e:add_plugin("disco"); e:add_plugin("pubsub"); e.pep={}; e:hook("pubsub/event",function(t) return e:event("pep/"..t.node,{from=t.from,id=t.item.attr.id,item=t.item.tags[1]}); end); function e:hook_pep(t,i,o) local a=e.events._handlers["pep/"..t]; if not(a)or#a==0 then e:add_disco_feature(t.."+notify"); end e:hook("pep/"..t,i,o); end function e:unhook_pep(t,a) e:unhook("pep/"..t,a); local a=e.events._handlers["pep/"..t]; if not(a)or#a==0 then e:remove_disco_feature(t.."+notify"); end end function e:publish_pep(t,a,o) return e.pubsub:service(nil):node(a or t.attr.xmlns):publish(o or"current",nil,t) end end end) package.preload['verse.plugins.adhoc']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local o=require"verse"; local n=require"lib.adhoc"; local t="http://jabber.org/protocol/commands"; local r="jabber:x:data"; local a={}; a.__index=a; local i={}; function o.plugins.adhoc(e) e:add_plugin("disco"); e:add_disco_feature(t); function e:query_commands(a,o) e:disco_items(a,t,function(a) e:debug("adhoc list returned") local t={}; for o,a in ipairs(a)do t[a.node]=a.name; end e:debug("adhoc calling callback") return o(t); end); end function e:execute_command(o,i,t) local e=setmetatable({ stream=e,jid=o, command=i,callback=t },a); return e:execute(); end local function s(t,e) if not(e)or e=="user"then return true;end if type(e)=="function"then return e(t); end end function e:add_adhoc_command(o,a,h,s) i[a]=n.new(o,a,h,s); e:add_disco_item({jid=e.jid,node=a,name=o},t); return i[a]; end local function h(t) local a=t.tags[1]; local a=a.attr.node; local a=i[a]; if not a then return;end if not s(t.attr.from,a.permission)then e:send(o.error_reply(t,"auth","forbidden","You don't have permission to execute this command"):up() :add_child(a:cmdtag("canceled") :tag("note",{type="error"}):text("You don't have permission to execute this command"))); return true end return n.handle_cmd(a,{send=function(t)return e:send(t)end},t); end e:hook("iq/"..t,function(e) local t=e.attr.type; local a=e.tags[1].name; if t=="set"and a=="command"then return h(e); end end); end function a:_process_response(e) if e.attr.type=="error"then self.status="canceled"; self.callback(self,{}); return; end local e=e:get_child("command",t); self.status=e.attr.status; self.sessionid=e.attr.sessionid; self.form=e:get_child("x",r); self.note=e:get_child("note"); self.callback(self); end function a:execute() local e=o.iq({to=self.jid,type="set"}) :tag("command",{xmlns=t,node=self.command}); self.stream:send_iq(e,function(e) self:_process_response(e); end); end function a:next(e) local t=o.iq({to=self.jid,type="set"}) :tag("command",{ xmlns=t, node=self.command, sessionid=self.sessionid }); if e then t:add_child(e);end self.stream:send_iq(t,function(e) self:_process_response(e); end); end end) package.preload['verse.plugins.presence']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"verse"; local o=require"util.stanza"; function a.plugins.presence(t) t.last_presence=nil; t:hook("presence-out",function(e) if not e.attr.to then t.last_presence=o.clone(e); end end,1); function t:resend_presence() if self.last_presence then t:send(self.last_presence); end end function t:set_status(e) local a=a.presence(); if type(e)=="table"then if e.show then a:tag("show"):text(e.show):up(); end if e.priority or e.prio then a:tag("priority"):text(tostring(e.priority or e.prio)):up(); end if e.status or e.msg then a:tag("status"):text(e.status or e.msg):up(); end elseif type(e)=="string"then a:tag("status"):text(e):up(); end t:send(a); end end end) package.preload['verse.plugins.private']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=require"verse"; local a="jabber:iq:private"; function t.plugins.private(o) function o:private_set(i,o,e,n) local t=t.iq({type="set"}) :tag("query",{xmlns=a}); if e then if e.name==i and e.attr and e.attr.xmlns==o then t:add_child(e); else t:tag(i,{xmlns=o}) :add_child(e); end end self:send_iq(t,n); end function o:private_get(e,o,i) self:send_iq(t.iq({type="get"}) :tag("query",{xmlns=a}) :tag(e,{xmlns=o}), function(t) if t.attr.type=="result"then local t=t:get_child("query",a); local e=t:get_child(e,o); i(e); end end); end end end) package.preload['verse.plugins.roster']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local i=require"verse"; local l=require"util.jid".bare; local a="jabber:iq:roster"; local o="urn:xmpp:features:rosterver"; local n=table.insert; function i.plugins.roster(t) local h=false; local e={ items={}; ver=""; }; t.roster=e; t:hook("stream-features",function(e) if e:get_child("ver",o)then h=true; end end); local function s(e) local t=i.stanza("item",{xmlns=a}); for a,e in pairs(e)do if a~="groups"then t.attr[a]=e; else for a=1,#e do t:tag("group"):text(e[a]):up(); end end end return t; end local function d(a) local e={}; local t={}; e.groups=t; for t,a in pairs(a.attr)do if t~="xmlns"then e[t]=a end end for e in a:childtags("group")do n(t,e:get_text()) end return e; end function e:load(t) e.ver,e.items=t.ver,t.items; end function e:dump() return{ ver=e.ver, items=e.items, }; end function e:add_contact(h,o,n,e) local o={jid=h,name=o,groups=n}; local a=i.iq({type="set"}) :tag("query",{xmlns=a}) :add_child(s(o)); t:send_iq(a,function(t) if not e then return end if t.attr.type=="result"then e(true); else e(nil,t); end end); end function e:delete_contact(o,n) o=(type(o)=="table"and o.jid)or o; local h={jid=o,subscription="remove"} if not e.items[o]then return false,"item-not-found";end t:send_iq(i.iq({type="set"}) :tag("query",{xmlns=a}) :add_child(s(h)), function(e) if not n then return end if e.attr.type=="result"then n(true); else n(nil,e); end end); end local function r(t) local t=d(t); e.items[t.jid]=t; end local function d(t) local a=e.items[t]; e.items[t]=nil; return a; end function e:fetch(o) t:send_iq(i.iq({type="get"}):tag("query",{xmlns=a,ver=h and e.ver or nil}), function(t) if t.attr.type=="result"then local t=t:get_child("query",a); if t then e.items={}; for t in t:childtags("item")do r(t) end e.ver=t.attr.ver or""; end o(e); else o(nil,t); end end); end t:hook("iq/"..a,function(o) local s,n=o.attr.type,o.attr.from; if s=="set"and(not n or n==l(t.jid))then local s=o:get_child("query",a); local n=s and s:get_child("item"); if n then local o,a; local i=n.attr.jid; if n.attr.subscription=="remove"then o="removed" a=d(i); else o=e.items[i]and"changed"or"added"; r(n) a=e.items[i]; end e.ver=s.attr.ver; if a then t:event("roster/item-"..o,a); end end t:send(i.reply(o)) return true; end end); end end) package.preload['verse.plugins.register']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=require"verse"; local i="jabber:iq:register"; function t.plugins.register(e) local function a(o) if o:get_child("register","http://jabber.org/features/iq-register")then local t=t.iq({to=e.host_,type="set"}) :tag("query",{xmlns=i}) :tag("username"):text(e.username):up() :tag("password"):text(e.password):up(); if e.register_email then t:tag("email"):text(e.register_email):up(); end e:send_iq(t,function(t) if t.attr.type=="result"then e:event("registration-success"); else local a,t,o=t:get_error(); e:debug("Registration failed: %s",t); e:event("registration-failure",{type=a,condition=t,text=o}); end end); else e:debug("In-band registration not offered by server"); e:event("registration-failure",{condition="service-unavailable"}); end e:unhook("stream-features",a); return true; end e:hook("stream-features",a,310); end end) package.preload['verse.plugins.groupchat']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local i=require"verse"; local e=require"util.events"; local n=require"util.jid"; local a={}; a.__index=a; local h="urn:xmpp:delay"; local s="http://jabber.org/protocol/muc"; function i.plugins.groupchat(o) o:add_plugin("presence") o.rooms={}; o:hook("stanza",function(e) local a=n.bare(e.attr.from); if not a then return end local t=o.rooms[a] if not t and e.attr.to and a then t=o.rooms[e.attr.to.." "..a] end if t and t.opts.source and e.attr.to~=t.opts.source then return end if t then local o=select(3,n.split(e.attr.from)); local n=e:get_child_text("body"); local i=e:get_child("delay",h); local a={ room_jid=a; room=t; sender=t.occupants[o]; nick=o; body=n; stanza=e; delay=(i and i.attr.stamp); }; local t=t:event(e.name,a); return t or(e.name=="message")or nil; end end,500); function o:join_room(n,h,t,r) if not h then return false,"no nickname supplied" end t=t or{}; local e=setmetatable(i.eventable{ stream=o,jid=n,nick=h, subject=nil, occupants={}, opts=t, },a); if t.source then self.rooms[t.source.." "..n]=e; else self.rooms[n]=e; end local a=e.occupants; e:hook("presence",function(o) local t=o.nick or h; if not a[t]and o.stanza.attr.type~="unavailable"then a[t]={ nick=t; jid=o.stanza.attr.from; presence=o.stanza; }; local o=o.stanza:get_child("x",s.."#user"); if o then local e=o:get_child("item"); if e and e.attr then a[t].real_jid=e.attr.jid; a[t].affiliation=e.attr.affiliation; a[t].role=e.attr.role; end end if t==e.nick then e.stream:event("groupchat/joined",e); else e:event("occupant-joined",a[t]); end elseif a[t]and o.stanza.attr.type=="unavailable"then if t==e.nick then e.stream:event("groupchat/left",e); if e.opts.source then self.rooms[e.opts.source.." "..n]=nil; else self.rooms[n]=nil; end else a[t].presence=o.stanza; e:event("occupant-left",a[t]); a[t]=nil; end end end); e:hook("message",function(a) local t=a.stanza:get_child_text("subject"); if not t then return end t=#t>0 and t or nil; if t~=e.subject then local o=e.subject; e.subject=t; return e:event("subject-changed",{from=o,to=t,by=a.sender,event=a}); end end,2e3); local t=i.presence():tag("x",{xmlns=s}):reset(); if r then t:get_child("x",s):tag("password"):text(r):reset(); end self:event("pre-groupchat/joining",t); e:send(t) self:event("groupchat/joining",e); return e; end o:hook("presence-out",function(e) if not e.attr.to then for a,t in pairs(o.rooms)do t:send(e); end e.attr.to=nil; end end); end function a:send(e) if e.name=="message"and not e.attr.type then e.attr.type="groupchat"; end if e.name=="presence"then e.attr.to=self.jid.."/"..self.nick; end if e.attr.type=="groupchat"or not e.attr.to then e.attr.to=self.jid; end if self.opts.source then e.attr.from=self.opts.source end self.stream:send(e); end function a:send_message(e) self:send(i.message():tag("body"):text(e)); end function a:set_subject(e) self:send(i.message():tag("subject"):text(e)); end function a:leave(t) self.stream:event("groupchat/leaving",self); local e=i.presence({type="unavailable"}); if t then e:tag("status"):text(t); end self:send(e); end function a:admin_set(e,t,a,o) self:send(i.iq({type="set"}) :query(s.."#admin") :tag("item",{nick=e,[t]=a}) :tag("reason"):text(o or"")); end function a:set_role(a,e,t) self:admin_set(a,"role",e,t); end function a:set_affiliation(t,a,e) self:admin_set(t,"affiliation",a,e); end function a:kick(t,e) self:set_role(t,"none",e); end function a:ban(e,t) self:set_affiliation(e,"outcast",t); end end) package.preload['verse.plugins.vcard']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local i=require"verse"; local o=require"util.vcard"; local e="vcard-temp"; function i.plugins.vcard(a) function a:get_vcard(n,t) a:send_iq(i.iq({to=n,type="get"}) :tag("vCard",{xmlns=e}),t and function(a) local e=a:get_child("vCard",e); if a.attr.type=="result"and e then e=o.from_xep54(e) t(e) else t(false) end end or nil); end function a:set_vcard(e,n) local t; if type(e)=="table"and e.name then t=e; elseif type(e)=="string"then t=o.to_xep54(o.from_text(e)[1]); elseif type(e)=="table"then t=o.to_xep54(e); error("Converting a table to vCard not implemented") end if not t then return false end a:debug("setting vcard to %s",tostring(t)); a:send_iq(i.iq({type="set"}) :add_child(t),n); end end end) package.preload['verse.plugins.vcard_update']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local n=require"verse"; local i="vcard-temp:x:update"; local r=require("util.hashes").sha1; local e,t=pcall(function() local e=require("util.encodings").base64.decode; assert(e("SGVsbG8=")=="Hello") return e; end); if not e then e,t=pcall(function()return require("mime").unb64;end); if not e then error("Could not find a base64 decoder") end end local h=t; function n.plugins.vcard_update(e) e:add_plugin("vcard"); e:add_plugin("presence"); local t; local function s(a) local o; for e=1,#a do if a[e].name=="PHOTO"then o=a[e][1]; break end end if o then local a=r(h(o),true); t=n.stanza("x",{xmlns=i}) :tag("photo"):text(a); e:resend_presence() else t=nil; end end local a; e:hook("ready",function() if a then return;end a=true; e:get_vcard(nil,function(t) if t then s(t) end e:event("ready"); end); return true; end,3); e:hook("presence-out",function(e) if t and not e:get_child("x",i)then e:add_child(t); end end,10); end end) package.preload['verse.plugins.carbons']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local o=require"verse"; local a="urn:xmpp:carbons:2"; local n="urn:xmpp:forward:0"; local h=os.time; local s=require"util.datetime".parse; local r=require"util.jid".bare; function o.plugins.carbons(e) local t={}; t.enabled=false; e.carbons=t; function t:enable(i) e:send_iq(o.iq{type="set"} :tag("enable",{xmlns=a}) ,function(e) local e=e.attr.type=="result"; if e then t.enabled=true; end if i then i(e); end end or nil); end function t:disable(i) e:send_iq(o.iq{type="set"} :tag("disable",{xmlns=a}) ,function(e) local e=e.attr.type=="result"; if e then t.enabled=false; end if i then i(e); end end or nil); end local i; e:hook("bind-success",function() i=r(e.jid); end); e:hook("message",function(o) local t=o:get_child(nil,a); if o.attr.from==i and t then local o=t.name; local t=t:get_child("forwarded",n); local a=t and t:get_child("message","jabber:client"); local t=t:get_child("delay","urn:xmpp:delay"); local t=t and t.attr.stamp; t=t and s(t); if a then return e:event("carbon",{ dir=o, stanza=a, timestamp=t or h(), }); end end end,1); end end) package.preload['verse.plugins.archive']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"verse"; local t=require"util.stanza"; local e="urn:xmpp:mam:2" local c="urn:xmpp:forward:0"; local u="urn:xmpp:delay"; local i=require"util.id".short; local m=require"util.datetime".parse; local r=require"util.datetime".datetime; local o=require"util.dataforms".new; local h=require"util.rsm"; local d={}; local l=o{ {name="FORM_TYPE";type="hidden";value=e;}; {name="with";type="jid-single";}; {name="start";type="text-single"}; {name="end";type="text-single";}; }; function a.plugins.archive(o) function o:query_archive(o,a,n) local i=i(); local o=t.iq{id=i,type="set",to=o} :tag("query",{xmlns=e,queryid=i}); local t,s=tonumber(a["start"]),tonumber(a["end"]); a["start"]=t and r(t); a["end"]=s and r(s); o:add_child(l:form(a,"submit")); o:add_child(h.generate(a)); local t={}; local function s(a) local e=a:get_child("result",e); if e and e.attr.queryid==i then local a=e:get_child("forwarded",c); local i=e.attr.id; local e=a:get_child("delay",u); local o=e and m(e.attr.stamp)or nil; local e=a:get_child("message","jabber:client") t[#t+1]={id=i,stamp=o,message=e}; return true end end self:hook("message",s,1); self:send_iq(o,function(a) self:unhook("message",s); if a.attr.type=="error"then self:warn(table.concat({a:get_error()}," ")) n(false,a:get_error()) return true; end local e=a:get_child("fin",e) if e then t.complete=e.attr.complete=="true"or e.attr.complete=="1"; local e=h.get(e); for e,a in pairs(e or d)do t[e]=a;end end n(t); return true end); end local i={ always=true,[true]="always", never=false,[false]="never", roster="roster", } local function n(t) local e={}; local a=t.attr.default; if a then e[false]=i[a]; end local a=t:get_child("always"); if a then for t in a:childtags("jid")do local t=t:get_text(); e[t]=true; end end local t=t:get_child("never"); if t then for t in t:childtags("jid")do local t=t:get_text(); e[t]=false; end end return e; end local function s(o) local a a,o[false]=o[false],nil; if a~=nil then a=i[a]; end local i=t.stanza("prefs",{xmlns=e,default=a}) local a=t.stanza("always"); local e=t.stanza("never"); for t,o in pairs(o)do (o and a or e):tag("jid"):text(t):up(); end return i:add_child(a):add_child(e); end function o:archive_prefs_get(a) self:send_iq(t.iq{type="get"}:tag("prefs",{xmlns=e}), function(e) if e and e.attr.type=="result"and e.tags[1]then local t=n(e.tags[1]); a(t,e); else a(nil,e); end end); end function o:archive_prefs_set(e,a) self:send_iq(t.iq{type="set"}:add_child(s(e)),a); end end end) package.preload['verse.plugins.browsing']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"verse"; local o="urn:xmpp:browsing:0"; function a.plugins.browsing(e) e:add_plugin("pep"); function e:browsing(t,i) if type(t)=="string"then t={uri=t}; end local a=a.stanza("page",{xmlns=o}) for e,t in pairs(t)do a:tag(e):text(t):up(); end return e:publish_pep(a,i); end e:hook_pep(o,function(a) local t=a.item; return e:event("browsing",{ from=a.from; description=t:get_child_text"description"; keywords=t:get_child_text"keywords"; title=t:get_child_text"title"; uri=t:get_child_text"uri"; }); end); end end) package.preload['util.http']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local n,t=string.format,string.char; local r,h=pairs,ipairs; local i,o=table.insert,table.concat; local e={}; for a=0,255 do local t=t(a); local a=n("%%%02x",a); e[t]=a; e[a]=t; e[a:upper()]=t; end local function n(t) return t and(t:gsub("[^a-zA-Z0-9.~_-]",e)); end local function a(t) return t and(t:gsub("%%%x%x",e)); end local function e(e) return e and(n(e):gsub("%%20","+")); end local function s(a) local t={}; if a[1]then for o,a in h(a)do i(t,e(a.name).."="..e(a.value)); end else for a,o in r(a)do i(t,e(a).."="..e(o)); end end return o(t,"&"); end local function h(e) if not e:match("=")then return a(e);end local o={}; for t,e in e:gmatch("([^=&]*)=([^&]*)")do t,e=t:gsub("%+","%%20"),e:gsub("%+","%%20"); t,e=a(t),a(e); i(o,{name=t,value=e}); o[t]=e; end return o; end local function i(e,t) e=","..e:gsub("[ \t]",""):lower()..","; return e:find(","..t:lower()..",",1,true)~=nil; end local function o(e,t) if t then if e:sub(-1,-1)~="/"then e=e.."/";end else if e:sub(-1,-1)=="/"then e=e:sub(1,-2);end end if e:sub(1,1)~="/"then e="/"..e;end return e; end return{ urlencode=n,urldecode=a; formencode=s,formdecode=h; contains_token=i; normalize_path=o; }; end) package.preload['net.http.parser']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local u=tonumber; local g=assert; local z=require"socket.url".parse; local t=require"util.http".urldecode; local j=require"util.dbuffer"; local function x(e) e=t((e:gsub("//+","/"))); if e:sub(1,1)~="/"then e="/"..e; end local t=0; for e in e:gmatch("([^/]+)/")do if e==".."then t=t-1; elseif e~="."then t=t+1; end if t<0 then return nil; end end return e; end local q={}; function q.new(l,s,e,h) local f=true; if not e or e=="server"then f=false;else g(e=="client","Invalid parser type");end local v=u(h and h().body_size_limit)or 10*1024*1024; local n=u(h and h().head_size_limit)or 10*1024; local k=u(h and h().buffer_size_limit)or v*2; local a=j.new(k); local d; local r=nil; local e; local t; local w; local o; return{ feed=function(c,i) if o then return nil,"parse has failed";end if not i then if r and f and not t then a:collapse(); e.body=a:read_chunk()or""; e.partial=nil; l(e); r=nil; elseif a:length()~=0 then o=true;return s("unexpected-eof"); end return; end if not a:write(i)then o=true;return s("max-buffer-size-exceeded");end while a:length()>0 do if r==nil then local b=a:sub(1,n):find("\r\n\r\n",nil,true); if not b then return;end local p,c,m,i,g; local y; local n={}; for t in a:read(b+3):gmatch("([^\r\n]+)\r\n")do if y then local e,t=t:match("^([^%s:]+): *(.*)$"); if not e then o=true;return s("invalid-header-line");end e=e:lower(); n[e]=n[e]and n[e]..","..t or t; else y=t; if f then m,i,g=t:match("^HTTP/(1%.[01]) (%d%d%d) (.*)$"); i=u(i); if not i then o=true;return s("invalid-status-line");end w=not ((h and h().method=="HEAD") or(i==204 or i==304 or i==301) or(i>=100 and i<200)); else p,c,m=t:match("^(%w+) (%S+) HTTP/(1%.[01])$"); if not p then o=true;return s("invalid-status-line");end end end end if not y then o=true;return s("invalid-status-line");end d=w and n["transfer-encoding"]=="chunked"; t=u(n["content-length"]); if f then if not w then t=0;end e={ code=i; httpversion=m; headers=n; body=false; body_length=t; chunked=d; partial=true; responseversion=m; responseheaders=n; }; else local a; if c:byte()==47 then local t,e=c:match("([^?]*).?(.*)"); if e==""then e=nil;end a={path=t,query=e}; else a=z(c); if not(a and a.path)then o=true;return s("invalid-url");end end c=x(a.path); n.host=a.host or n.host; t=t or 0; e={ method=p; url=a; path=c; httpversion=m; headers=n; body=false; body_sink=nil; chunked=d; partial=true; }; end if not t or t>v then l(e); if not e.body_sink and(t and t>v)then o=true; return s("content-length-limit-exceeded"); end end if d and not e.body_sink then l(e); if not e.body_sink then e.body_buffer=j.new(k); end end r=true; end if r then if d then local n=a:sub(1,512); local t,i=n:match("^(%x+)[^\r\n]*\r\n()"); if not t then return;end t=t and u(t,16); if not t then o=true;return s("invalid-chunk-size");end if t==0 and n:find("\r\n\r\n",i-2,true)then local t=e.body_buffer; if t then e.body_buffer=nil; t:collapse(); e.body=t:read_chunk()or""; end a:collapse(); local t=a:read_chunk(); t=t:gsub("^.-\r\n\r\n",""); a:write(t); r,d=nil,nil; e.partial=nil; l(e); elseif a:length()-i-1>=t then a:discard(i-1); (e.body_sink or e.body_buffer):write(a:read(t)); a:discard(2); else break; end elseif e.body_sink then local i=a:read_chunk(t); while i and(not t or t>0)do if e.body_sink:write(i)then if t then t=t-#i; end i=a:read_chunk(t); else o=true; return s("body-sink-write-failure"); end end if t==0 then r=nil; e.partial=nil; l(e); end elseif not t or a:length()>=t then g(not d) e.body=t and a:read(t)or a:read_chunk()or""; r=nil; e.partial=nil; l(e); else break; end else break; end end end; }; end return q; end) package.preload['net.http']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local O=require"util.encodings".base64.encode; local A=require"socket.url" local E=require"net.http.parser".new; local d=require"util.http"; local T=require"util.events"; local _=require"util.x509".verify_identity; local k=require"util.promise"; local g=require"net.http.errors"; local b=require"net.resolvers.basic"; local v=require"net.connect".connect; local y=pcall(require,"ssl"); local s,f=table.insert,table.concat; local l=pairs; local p,c,t= tonumber,tostring,debug.traceback; local q=os.time; local z=require"util.xpcall".xpcall; local w=error local o=require"util.logger".init("http"); local _ENV=nil; local a={}; local function x(e)return(c(e):match("%x+$"));end local i={default_port=80,default_mode="*a"}; local function j(e)o("error","Traceback[http]: %s",t(c(e),2));return e;end local function h(e,t,...) if not t then o("error","Request '%s': error in callback: %s",e.id,(...)); if not e.suppress_errors then w(...); end end return...; end local function r(e) local t=e.conn; if t then e.conn=nil; t:close() end end local function m(e,t) if e.callback then e.callback(t or"cancelled",0,e); e.callback=nil; end if e.conn then r(e); end end local function u(e,n,t) if not e.parser then local function i(t) if e.callback then e.callback(t or"connection-closed",0,e); e.callback=nil; end r(e); end if not n then i(t); return; end local a; local function n(t) if t.partial then o("debug","Request '%s': partial response (%s%s)", e.id, t.chunked and"chunked, "or"", t.body_length and("%d bytes"):format(t.body_length)or"unknown length" ); if e.streaming_handler then o("debug","Request '%s': Streaming via handler",e.id); t.body_sink,a=e.streaming_handler(t); end return; elseif a then o("debug","Request '%s': Finalizing response stream"); a(t); end if e.callback then e.callback(t.body,t.code,t,e); e.callback=nil; end r(e); end local function t() return e; end e.parser=E(n,i,"client",t); end e.parser:feed(n); end function i.onconnect(t) local e=a[t]; e.write=function(...)return e.conn:write(...);end local r=e.callback; e.callback=function(i,a,n,s) do local t={http=e.http,url=e.url,request=e,response=n,content=i,code=a,callback=e.callback}; e.http.events.fire_event("response",t); i,a,n=t.content,t.code,t.response; end o("debug","Request '%s': Calling callback, status %s",e.id,a or"---"); return h(e.id,z(r,j,i,a,n,s)); end e.reader=u; e.state="status"; e.cancel=m; a[e.conn]=e; if not e.insecure and t:ssl()then local a=t:socket(); local o=a.getpeerverification and a:getpeerverification(); if not o then e.callback("certificate-chain-invalid",0,e); e.callback=nil; t:close(); return; end local a=a.getpeercertificate and a:getpeercertificate(); if not a or not _(e.host,false,a)then e.callback("certificate-verify-failed",0,e); e.callback=nil; t:close(); return; end end local a={e.method or"GET"," ",e.path," HTTP/1.1\r\n"}; if e.query then s(a,4,"?"..e.query); end for e,t in l(e.headers)do s(a,e..": "..t.."\r\n"); end s(a,"\r\n") t:write(f(a)); if e.body then t:write(e.body); end end function i.onincoming(t,i) local e=a[t]; if not e then o("warn","Received response from connection %s with no request attached!",t); return; end if i and e.reader then e:reader(i); end end function i.ondisconnect(t,o) local e=a[t]; if e and e.conn then e:reader(nil,o or"closed"); end a[t]=nil; end function i.onattach(e,t) a[e]=t; t.conn=e; end function i.ondetach(e) a[e]=nil; end function i.onfail(e,t) e.http.events.fire_event("request-connection-error",{http=e.http,request=e,url=e.url,err=t}); e.callback(t or"connection failed",0,e); end local function f(a,s,t,n) local e=A.parse(s); if not(e and e.host)then n("invalid-url",0,e); return nil,"invalid-url"; end e.url=s; e.http=a; e.time=q(); if not e.path then e.path="/"; end e.id=t and t.id or x(e); do local o={http=a,url=s,request=e,options=t,callback=n}; local a=a.events.fire_event("pre-request",o); if a then return a; end e,s,t,e.callback=o.request,o.url,o.options,o.callback; end local d,h,u; local m,n=e.host,e.port; local r=m; if(n=="80"and e.scheme=="http") or(n=="443"and e.scheme=="https")then n=nil; elseif n then r=r..":"..n; end h={ ["Host"]=r; ["User-Agent"]="Prosody XMPP Server"; }; if e.userinfo then h["Authorization"]="Basic "..O(e.userinfo); end if t then e.onlystatus=t.onlystatus; u=t.body; if u then d="POST"; h["Content-Length"]=c(#u); h["Content-Type"]="application/x-www-form-urlencoded"; end if t.method then d=t.method;end if t.headers then for t,e in l(t.headers)do h[t]=e; end end e.insecure=t.insecure; e.suppress_errors=t.suppress_errors; e.streaming_handler=t.streaming_handler; end 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); e.method,e.headers,e.body=d,h,u; local o=e.scheme=="https"; if o and not y then w("SSL not available, unable to contact https URL"); end local r=n and p(n)or(o and 443 or 80); local n=a.options and a.options.use_dane; local h=false; if o then h=t and t.sslctx or a.options and a.options.sslctx; if t and t.use_dane~=nil then n=t.use_dane; end end local t=b.new(m,r,"tcp",{servername=e.host;use_dane=n}); v(t,i,{sslctx=h},e); a.events.fire_event("request",{http=a,request=e,url=s}); return e; end local function e(t) local e={ options=t; request=function(o,a,t,e) if e~=nil then return f(o,a,t,e); else return k.new(function(i,n) f(o,a,t,function(t,a,e,o) if a==0 then n(g.new(t,{request=e})); else e.request=o; i(e); end end); end); end end; new=t and function(o) local a={}; for e,t in l(t)do a[e]=t;end if o then for t,e in l(o)do a[t]=e;end end return e(a); end or e; events=T.new(); }; return e; end local t=e({ sslctx={mode="client",protocol="sslv23",options={"no_sslv2","no_sslv3"},alpn="http/1.1",verify="peer"}; suppress_errors=true; }); return{ request=function(e,a,o) return t:request(e,a,o); end; default=t; new=e; events=t.events; urlencode=d.urlencode; urldecode=d.urldecode; formencode=d.formencode; formdecode=d.formdecode; destroy_request=r; features={ sni=true; }; }; end) package.preload['util.x509']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local o=require"util.encodings".stringprep.nameprep; local r=require"util.encodings".idna.to_ascii; local y=require"util.encodings".idna.to_unicode; local c=require"util.encodings".base64; local e=require"util.logger".init("x509"); local p=require"util.multitable"; local v=string.format; local d=ipairs; local _ENV=nil; local u="2.5.4.3"; local h="2.5.29.17"; local s="1.3.6.1.5.5.7.8.5"; local n="1.3.6.1.5.5.7.8.7"; local function l(o,a) local t=r(o) if t==nil then e("info","Host %s failed IDNA ToASCII operation",o) return false end t=t:lower() local o=t:gsub("^[^.]+%.","") for i=1,#a do local a=a[i] if t==a:lower()then e("debug","Cert dNSName %s matched hostname",a); return true end if a:match("^%*%.")then local t=a:gsub("^[^.]+%.","") if o==t:lower()then e("debug","Cert dNSName %s matched hostname",a); return true end end end return false end local function w(a,t) local i=o(a) for a=1,#t do local t=t[a] if t:match("[@/]")then e("debug","Ignoring xmppAddr %s because it's not a bare domain",t) else local a=o(t) if a==nil then e("info","Ignoring xmppAddr %s, failed nameprep!",t) else if i==a then e("debug","Cert xmppAddr %s matched hostname",t) return true end end end end return false end local function f(i,o,t) local a=r(i) if a==nil then e("info","Host %s failed IDNA ToASCII operation",i); return false end if o:match("^_")==nil then o="_"..o end a=a:lower(); local n=a:gsub("^[^.]+%.","") for i=1,#t do local i,t=t[i]:match("^(_[^.]+)%.(.*)"); if o==i then if a==t:lower()then e("debug","Cert SRVName %s matched hostname",t); return true; end if t:match("^%*%.")then local a=t:gsub("^[^.]+%.","") if n==a:lower()then e("debug","Cert SRVName %s matched hostname",t) return true end end if a==t:lower()then e("debug","Cert SRVName %s matched hostname",t); return true end end end return false end local function m(o,a,i) if i.setencode then i:setencode("utf8"); end local t=i:extensions() if t[h]then local e=t[h]; local t=false if e[s]then t=true if a=="_xmpp-client"or a=="_xmpp-server"then if w(o,e[s])then return true end end end if e[n]then t=true if a and f(o,a,e[n])then return true end end if e["dNSName"]then t=true if l(o,e["dNSName"])then return true end end if e["uniformResourceIdentifier"]then t=true end if t then return false end end local a=i:subject() local t=nil for o=1,#a do local a=a[o] if a["oid"]==u then if t then e("info","Certificate has multiple common names") return false end t=a["value"]; end end if t then return l(o,{t}) end return false end local function l(i) if i.setencode then i:setencode("utf8"); end local a=p.new(); local e=i:extensions(); local t=e[h]; if t then if t["dNSName"]then for t,e in d(t["dNSName"])do local t=e:sub(1,2)=="*."; if t then e=e:sub(3);end e=y(o(e)); if e then if t then e="*."..e;end a:set(e,"*",true); end end end if t[s]then for t,e in d(t[s])do e=o(e); if e then a:set(e,"xmpp-client",true); a:set(e,"xmpp-server",true); end end end if t[n]then for t,e in d(t[n])do local t,e=e:match("^_([^.]+)%.(.*)"); if t then e=o(e); if e then a:set(e,t,true); end end end end end local e=i:subject(); for t=1,#e do local e=e[t]; if e.oid==u then local e=o(e.value); if e and r(e)then a:set(e,"*",true); end end end return a.data; end local t="%-%-%-%-%-BEGIN ([A-Z ]+)%-%-%-%-%-\r?\n".. "([0-9A-Za-z+/=\r\n]*)\r?\n%-%-%-%-%-END %1%-%-%-%-%-"; local function a(e) local e,t=e:match(t); if e and t then return c.decode(t),e; end end local n=('.'):rep(64); local i="-----BEGIN %s-----\n%s\n-----END %s-----\n" local function o(t,e) e=e and e:upper()or"CERTIFICATE"; t=c.encode(t); return v(i,e,t:gsub(n,'%0\n',(#t-1)/64),e); end return{ verify_identity=m; get_identities=l; pem2der=a; der2pem=o; }; end) package.preload['verse.bosh']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local n=require"util.xmppstream".new; local o=require"util.stanza"; require"net.httpclient_listener"; local a=require"net.http"; local e=setmetatable({},{__index=verse.stream_mt}); e.__index=e; local h="http://etherx.jabber.org/streams"; local s="http://jabber.org/protocol/httpbind"; local t=5; function verse.new_bosh(a,t) local t={ bosh_conn_pool={}; bosh_waiting_requests={}; bosh_rid=math.random(1,999999); bosh_outgoing_buffer={}; bosh_url=t; conn={}; }; function t:reopen() self.bosh_need_restart=true; self:flush(); end local t=verse.new(a,t); return setmetatable(t,e); end function e:connect() self:_send_session_request(); end function e:send(e) self:debug("Putting into BOSH send buffer: %s",tostring(e)); self.bosh_outgoing_buffer[#self.bosh_outgoing_buffer+1]=o.clone(e); self:flush(); end function e:flush() if self.connected and#self.bosh_waiting_requests<self.bosh_max_requests and(#self.bosh_waiting_requests==0 or#self.bosh_outgoing_buffer>0 or self.bosh_need_restart)then self:debug("Flushing..."); local e=self:_make_body(); local t=self.bosh_outgoing_buffer; for o,a in ipairs(t)do e:add_child(a); t[o]=nil; end self:_make_request(e); else self:debug("Decided not to flush."); end end function e:_make_request(o) local e,t=a.request(self.bosh_url,{body=tostring(o)},function(i,e,a) if e~=0 then self.inactive_since=nil; return self:_handle_response(i,e,a); end local e=os.time(); if not self.inactive_since then self.inactive_since=e; elseif e-self.inactive_since>self.bosh_max_inactivity then return self:_disconnected(); else self:debug("%d seconds left to reconnect, retrying in %d seconds...", self.bosh_max_inactivity-(e-self.inactive_since),t); end timer.add_task(t,function() self:debug("Retrying request..."); for t,e in ipairs(self.bosh_waiting_requests)do if e==a then table.remove(self.bosh_waiting_requests,t); break; end end self:_make_request(o); end); end); if e then table.insert(self.bosh_waiting_requests,e); else self:warn("Request failed instantly: %s",t); end end function e:_disconnected() self.connected=nil; self:event("disconnected"); end function e:_send_session_request() local e=self:_make_body(); e.attr.hold="1"; e.attr.wait="60"; e.attr["xml:lang"]="en"; e.attr.ver="1.6"; e.attr.from=self.jid; e.attr.to=self.host; e.attr.secure='true'; a.request(self.bosh_url,{body=tostring(e)},function(e,t) if t==0 then return self:_disconnected(); end local e=self:_parse_response(e) if not e then self:warn("Invalid session creation response"); self:_disconnected(); return; end self.bosh_sid=e.attr.sid; self.bosh_wait=tonumber(e.attr.wait); self.bosh_hold=tonumber(e.attr.hold); self.bosh_max_inactivity=tonumber(e.attr.inactivity); self.bosh_max_requests=tonumber(e.attr.requests)or self.bosh_hold; self.connected=true; self:event("connected"); self:_handle_response_payload(e); end); end function e:_handle_response(a,t,e) if self.bosh_waiting_requests[1]~=e then self:warn("Server replied to request that wasn't the oldest"); for t,a in ipairs(self.bosh_waiting_requests)do if a==e then self.bosh_waiting_requests[t]=nil; break; end end else table.remove(self.bosh_waiting_requests,1); end local e=self:_parse_response(a); if e then self:_handle_response_payload(e); end self:flush(); end function e:_handle_response_payload(t) local e=t.tags; for t=1,#e do local e=e[t]; if e.attr.xmlns==h then self:event("stream-"..e.name,e); elseif e.attr.xmlns then self:event("stream/"..e.attr.xmlns,e); else self:event("stanza",e); end end if t.attr.type=="terminate"then self:_disconnected({reason=t.attr.condition}); end end local a={ stream_ns="http://jabber.org/protocol/httpbind",stream_tag="body", default_ns="jabber:client", streamopened=function(e,t)e.notopen=nil;e.payload=verse.stanza("body",t);return true;end; handlestanza=function(e,t)e.payload:add_child(t);end; }; function e:_parse_response(e) self:debug("Parsing response: %s",e); if e==nil then self:debug("%s",debug.traceback()); self:_disconnected(); return; end local t={notopen=true,stream=self}; local a=n(t,a); a:feed(e); return t.payload; end function e:_make_body() self.bosh_rid=self.bosh_rid+1; local e=verse.stanza("body",{ xmlns=s; content="text/xml; charset=utf-8"; sid=self.bosh_sid; rid=self.bosh_rid; }); if self.bosh_need_restart then self.bosh_need_restart=nil; e.attr.restart='true'; end return e; end end) package.preload['verse.client']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local t=require"verse"; local i=t.stream_mt; local d=require"util.jid".split; local r=require"net.adns"; local a=require"util.stanza"; local o=require"util.id".short; math.randomseed((require"socket".gettime()*1e6)%2147483648); t.message,t.presence,t.iq,t.stanza,t.reply,t.error_reply= a.message,a.presence,a.iq,a.stanza,a.reply,a.error_reply; function t.iq(e) if not e.id then e.id=o(); end return a.iq(e); end local s=require"util.xmppstream".new; local n="http://etherx.jabber.org/streams"; local function h(e,t) if e.priority==t.priority then if not e.weight_r then e.weight_r=math.random(); end if not t.weight_r then t.weight_r=math.random(); end return(1+e.weight)*e.weight_r>(1+t.weight)*t.weight_r; end return e.priority<t.priority; end local o={ stream_ns=n, stream_tag="stream", default_ns="jabber:client"}; function o.streamopened(e,t) e.stream_id=t.id; if not e:event("opened",t)then e.notopen=nil; end return true; end function o.streamclosed(e) e.notopen=true; if not e.closed then e:send("</stream:stream>"); e.closed=true; end e:event("closed"); return e:close("stream closed") end function o.handlestanza(t,e) if e.attr.xmlns==n then return t:event("stream-"..e.name,e); elseif e.attr.xmlns then return t:event("stream/"..e.attr.xmlns,e); end return t:event("stanza",e); end function o.error(a,t,e) if a:event(t,e)==nil then if e then local t=e:get_child(nil,"urn:ietf:params:xml:ns:xmpp-streams"); local e=e:get_child_text("text","urn:ietf:params:xml:ns:xmpp-streams"); error(t.name..(e and": "..e or"")); else error(e and e.name or t or"unknown-error"); end end end function i:reset() if self.stream then self.stream:reset(); else self.stream=s(self,o); end self.notopen=true; return true; end function i:connect_client(e,i,a,o) self.jid,self.password=e,i; self.client_key,self.server_key=a,o; self.username,self.host,self.resource=d(e); self:add_plugin("tls"); self:add_plugin("sasl"); self:add_plugin("bind"); function self.data(t,e) local t,a=self.stream:feed(e); if t then return;end self:debug("Received invalid XML (%s) %d bytes: %s",tostring(a),#e,e:sub(1,300):gsub("[\r\n]+"," ")); self:close("xml-not-well-formed"); end self:hook("connected",function()self:reopen();end); self:hook("incoming-raw",function(e)return self.data(self.conn,e);end); self:hook("read-timeout",function()self:send(" ");return true;end,-1); self.curr_id=0; self.tracked_iqs={}; self:hook("stanza",function(e) local t,a=e.attr.id,e.attr.type; if t and e.name=="iq"and(a=="result"or a=="error")and self.tracked_iqs[t]then self.tracked_iqs[t](e); self.tracked_iqs[t]=nil; return true; end end); self:hook("stanza",function(e) local a; if e.attr.xmlns==nil or e.attr.xmlns=="jabber:client"then if e.name=="iq"and(e.attr.type=="get"or e.attr.type=="set")then local o=e.tags[1]and e.tags[1].attr.xmlns; if o then a=self:event("iq/"..o,e); if not a then a=self:event("iq",e); end end if a==nil then self:send(t.error_reply(e,"cancel","service-unavailable")); return true; end else a=self:event(e.name,e); end end return a; end,-1); self:hook("outgoing",function(e) if e.name then self:event("stanza-out",e); end end); self:hook("stanza-out",function(e) if not e.attr.xmlns then self:event(e.name.."-out",e); end end); local function e() self:event("ready"); end self:hook("session-success",e,-1) self:hook("bind-success",e,-1); local e=self.close; function self:close(t) self.close=e; if not self.closed then self:send("</stream:stream>"); self.closed=true; else return self:close(t); end end local function a() self:connect(self.connect_host or self.host,self.connect_port or 5222); end if not(self.connect_host or self.connect_port)then r.lookup(function(t) if t then local e={}; self.srv_hosts=e; for a,t in ipairs(t)do table.insert(e,t.srv); end table.sort(e,h); local t=e[1]; self.srv_choice=1; if t then self.connect_host,self.connect_port=t.target,t.port; self:debug("Best record found, will connect to %s:%d",self.connect_host or self.host,self.connect_port or 5222); end self:hook("disconnected",function() if self.srv_hosts and self.srv_choice<#self.srv_hosts then self.srv_choice=self.srv_choice+1; local e=e[self.srv_choice]; self.connect_host,self.connect_port=e.target,e.port; a(); return true; end end,1e3); self:hook("connected",function() self.srv_hosts=nil; end,1e3); end a(); end,"_xmpp-client._tcp."..(self.host)..".","SRV"); else a(); end end function i:reopen() self:reset(); self:send(a.stanza("stream:stream",{to=self.host,["xmlns:stream"]='http://etherx.jabber.org/streams', xmlns="jabber:client",version="1.0",["xml:lang"]=self.lang}):top_tag()); end function i:send_iq(e,a) local t=e.attr.id or uuid.generate(); self.tracked_iqs[t]=a; e.attr.id=t; self:send(e); end end) package.preload['verse.component']=(function(...) local _ENV=_ENV; local function e(t,...) local e=package.loaded[t]or _ENV[t]or{_NAME=t}; package.loaded[t]=e; for t=1,select("#",...)do (select(t,...))(e); end _ENV=e; _M=e; return e; end local a=require"verse"; local t=a.stream_mt; local d=require"util.jid".split; local e=require"lxp"; local o=require"util.stanza"; local r=require"util.hashes".sha1; a.message,a.presence,a.iq,a.stanza,a.reply,a.error_reply= o.message,o.presence,o.iq,o.stanza,o.reply,o.error_reply; local h=require"util.xmppstream".new; local s="http://etherx.jabber.org/streams"; local i="jabber:component:accept"; local n={ stream_ns=s, stream_tag="stream", default_ns=i}; function n.streamopened(e,t) e.stream_id=t.id; if not e:event("opened",t)then e.notopen=nil; end return true; end function n.streamclosed(e) return e:event("closed"); end function n.handlestanza(t,e) if e.attr.xmlns==s then return t:event("stream-"..e.name,e); elseif e.attr.xmlns or e.name=="handshake"then return t:event("stream/"..(e.attr.xmlns or i),e); end return t:event("stanza",e); end function t:reset() if self.stream then self.stream:reset(); else self.stream=h(self,n); end self.notopen=true; return true; end function t:connect_component(e,n) self.jid,self.password=e,n; self.username,self.host,self.resource=d(e); function self.data(a,e) local a,o=self.stream:feed(e); if a then return;end t:debug("Received invalid XML (%s) %d bytes: %s",tostring(o),#e,e:sub(1,300):gsub("[\r\n]+"," ")); t:close("xml-not-well-formed"); end self:hook("incoming-raw",function(e)return self.data(self.conn,e);end); self.curr_id=0; self.tracked_iqs={}; self:hook("stanza",function(t) local e,a=t.attr.id,t.attr.type; if e and t.name=="iq"and(a=="result"or a=="error")and self.tracked_iqs[e]then self.tracked_iqs[e](t); self.tracked_iqs[e]=nil; return true; end end); self:hook("stanza",function(e) local t; if e.attr.xmlns==nil or e.attr.xmlns=="jabber:client"then if e.name=="iq"and(e.attr.type=="get"or e.attr.type=="set")then local o=e.tags[1]and e.tags[1].attr.xmlns; if o then t=self:event("iq/"..o,e); if not t then t=self:event("iq",e); end end if t==nil then self:send(a.error_reply(e,"cancel","service-unavailable")); return true; end else t=self:event(e.name,e); end end return t; end,-1); self:hook("opened",function(e) print(self.jid,self.stream_id,e.id); local e=r(self.stream_id..n,true); self:send(o.stanza("handshake",{xmlns=i}):text(e)); self:hook("stream/"..i,function(e) if e.name=="handshake"then self:event("authentication-success"); end end); end); local function e() self:event("ready"); end self:hook("authentication-success",e,-1); self:connect(self.connect_host or self.host,self.connect_port or 5347); self:reopen(); end function t:reopen() self:reset(); self:send(o.stanza("stream:stream",{to=self.jid,["xmlns:stream"]='http://etherx.jabber.org/streams', xmlns=i,version="1.0"}):top_tag()); end function t:close(t) if not self.notopen then self:send("</stream:stream>"); end local e=self.conn.disconnect(); self.conn:close(); e(conn,t); end function t:send_iq(e,a) local t=self:new_id(); self.tracked_iqs[t]=a; e.attr.id=t; self:send(e); end function t:new_id() self.curr_id=self.curr_id+1; return tostring(self.curr_id); end end) pcall(require,"luarocks.require"); local h=require"socket"; pcall(require,"ssl"); local a=require"net.server"; local s=require"util.events"; local o=require"util.logger"; local n=require"util.format".format; local e={}; e.server=a; local t={}; t.__index=t; e.stream_mt=t; e.plugins={}; function e.init(...) for e=1,select("#",...)do local t,a=pcall(require,"verse."..select(e,...)); if not t then error("Verse connection module not found: verse."..select(e,...)..a); end end return e; end local i=0; function e.new(o,a) local t=setmetatable(a or{},t); i=i+1; t.id=tostring(i); t.logger=o or e.new_logger("stream"..t.id); t.events=s.new(); t.plugins={}; t.verse=e; return t; end e.add_task=require"util.timer".add_task; e.logger=o.init; e.new_logger=o.init; e.log=e.logger("verse"); function e.set_log_handler(e,t) t=t or{"debug","info","warn","error"}; o.reset(); if io.type(e)=="file"then local o=e; function e(a,t,e) o:write(a,"\t",t,"\t",e,"\n"); end end if e then local function a(t,a,o,...) return e(t,a,n(o,...)); end for t,e in ipairs(t)do o.add_level_sink(e,a); end end end function e._default_log_handler(a,o,t) return io.stderr:write(a,"\t",o,"\t",t,"\n"); end e.set_log_handler(e._default_log_handler,{"error"}); local function o(t) e.log("error","Error: %s",t); e.log("error","Traceback: %s",debug.traceback()); end function e.set_error_handler(e) o=e; end function e.loop() return xpcall(a.loop,o); end function e.step() return xpcall(a.step,o); end function e.quit() return a.setquitting("once"); end function e.tls_builder(...) return a.tls_builder(...); end function t:listen(t,o) t=t or"localhost"; o=o or 0; local e,a=a.addserver(t,o,e.new_listener(self,"server"),"*a"); if e then self:debug("Bound to %s:%s",t,o); self.server=e; end return e,a; end function t:connect(o,i) o=o or"localhost"; i=tonumber(i)or 5222; local n=h.tcp() n:settimeout(0); n:setoption("keepalive",true); local s,t=n:connect(o,i); if not s and t~="timeout"then self:warn("connect() to %s:%d failed: %s",o,i,t); return self:event("disconnected",{reason=t})or false,t; end local e=a.wrapclient(n,o,i,e.new_listener(self),"*a"); if not e then self:warn("connection initialisation failed: %s",t); return self:event("disconnected",{reason=t})or false,t; end self:set_conn(e); return true; end function t:set_conn(t) self.conn=t; self.send=function(a,e) self:event("outgoing",e); e=tostring(e); self:event("outgoing-raw",e); return t:write(e); end; end function t:close(t) if not self.conn then e.log("error","Attempt to close disconnected connection - possibly a bug"); return; end local e=self.conn.disconnect(); self:event("shutdown"); self.conn:close(); e(self.conn,t); end function t:debug(...) return self.logger("debug",...); end function t:info(...) return self.logger("info",...); end function t:warn(...) return self.logger("warn",...); end function t:error(...) return self.logger("error",...); end function t:event(e,...) self:debug("Firing event: "..tostring(e)); return self.events.fire_event(e,...); end function t:hook(e,...) return self.events.add_handler(e,...); end function t:unhook(e,t) return self.events.remove_handler(e,t); end function e.eventable(e) e.events=s.new(); e.hook,e.unhook=t.hook,t.unhook; local t=e.events.fire_event; function e:event(e,...)return t(e,...);end return e; end function t:add_plugin(t) if self.plugins[t]then return true;end if require("verse.plugins."..t)then local e,a=e.plugins[t](self); if e~=false then self:debug("Loaded %s plugin",t); self.plugins[t]=true; else self:warn("Failed to load %s plugin: %s",t,a); end end return self; end function e.new_listener(t) local a={}; function a.onconnect(o) if t.server then local a=e.new(); o:setlistener(e.new_listener(a)); a:set_conn(o); t:event("connected",{client=a}); else t.connected=true; t:event("connected"); end end function a.onincoming(a,e) t:event("incoming-raw",e); end function a.ondisconnect(a,e) if a~=t.conn then return end t.connected=false; t:event("disconnected",{reason=e}); end function a.ondrain(e) t:event("drained"); end function a.onstatus(a,e) t:event("status",e); end function a.onreadtimeout(e) return t:event("read-timeout"); end return a; end return e;