annotate verse.lua @ 0:0d4d8b432980 default tip master

Initial commit.
author Kooda <kooda@upyum.com>
date Fri, 27 Aug 2010 17:00:32 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1 package.preload['util.encodings']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2 local function e()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3 error("Encoding function not implemented");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
4 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
5 module"encodings"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
6 stringprep={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
7 base64={encode=e,decode=e};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
8 return _M;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
9 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
10 package.preload['util.logger']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
11 local o=print
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
12 local i,t=select,tostring;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
13 module"logger"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
14 local function e(o,...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
15 local e,a=0,#arg;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
16 return(o:gsub("%%(.)",function(o)if o~="%"and e<=a then e=e+1;return t(arg[e]);end end));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
17 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
18 local function n(a,...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
19 local e,o=0,i('#',...);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
20 local i={...};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
21 return(a:gsub("%%(.)",function(a)if e<=o then e=e+1;return t(i[e]);end end));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
22 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
23 function init(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
24 return function(e,t,...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
25 o(e,n(t,...));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
26 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
27 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
28 return _M;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
29 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
30 package.preload['util.xstanza']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
31 local e=getmetatable(require"util.stanza".stanza());
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
32 local i="urn:ietf:params:xml:ns:xmpp-stanzas";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
33 function e:get_error()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
34 local o,t,a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
35 local e=self:get_child("error");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
36 if not e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
37 return nil,nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
38 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
39 o=e.attr.type;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
40 for e in e:children()do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
41 if e.attr.xmlns==i then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
42 if e.name=="text"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
43 a=e:get_text();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
44 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
45 t=e.name;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
46 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
47 if t and a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
48 break;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
49 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
50 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
51 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
52 return o,t,a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
53 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
54 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
55 package.preload['util.stanza']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
56 local e=table.insert;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
57 local t=table.concat;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
58 local r=table.remove;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
59 local y=table.concat;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
60 local s=string.format;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
61 local w=string.match;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
62 local l=tostring;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
63 local d=setmetatable;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
64 local p=getmetatable;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
65 local i=pairs;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
66 local n=ipairs;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
67 local o=type;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
68 local t=next;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
69 local t=print;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
70 local t=unpack;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
71 local m=string.gsub;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
72 local t=string.char;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
73 local u=string.find;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
74 local t=os;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
75 local c=not t.getenv("WINDIR");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
76 local h,a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
77 if c then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
78 local t,e=pcall(require,"util.termcolours");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
79 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
80 h,a=e.getstyle,e.getstring;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
81 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
82 c=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
83 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
84 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
85 local f="urn:ietf:params:xml:ns:xmpp-stanzas";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
86 module"stanza"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
87 stanza_mt={__type="stanza"};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
88 stanza_mt.__index=stanza_mt;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
89 function stanza(t,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
90 local e={name=t,attr=e or{},tags={},last_add={}};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
91 return d(e,stanza_mt);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
92 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
93 function stanza_mt:query(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
94 return self:tag("query",{xmlns=e});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
95 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
96 function stanza_mt:body(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
97 return self:tag("body",t):text(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
98 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
99 function stanza_mt:tag(a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
100 local t=stanza(a,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
101 (self.last_add[#self.last_add]or self):add_direct_child(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
102 e(self.last_add,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
103 return self;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
104 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
105 function stanza_mt:text(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
106 (self.last_add[#self.last_add]or self):add_direct_child(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
107 return self;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
108 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
109 function stanza_mt:up()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
110 r(self.last_add);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
111 return self;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
112 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
113 function stanza_mt:reset()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
114 local e=self.last_add;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
115 for t=1,#e do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
116 e[t]=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
117 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
118 return self;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
119 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
120 function stanza_mt:add_direct_child(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
121 if o(t)=="table"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
122 e(self.tags,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
123 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
124 e(self,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
125 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
126 function stanza_mt:add_child(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
127 (self.last_add[#self.last_add]or self):add_direct_child(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
128 return self;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
129 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
130 function stanza_mt:get_child(a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
131 for o,e in n(self.tags)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
132 if(not a or e.name==a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
133 and((not t and self.attr.xmlns==e.attr.xmlns)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
134 or e.attr.xmlns==t)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
135 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
136 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
137 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
138 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
139 function stanza_mt:child_with_name(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
140 for a,e in n(self.tags)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
141 if e.name==t then return e;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
142 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
143 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
144 function stanza_mt:child_with_ns(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
145 for a,e in n(self.tags)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
146 if e.attr.xmlns==t then return e;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
147 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
148 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
149 function stanza_mt:children()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
150 local e=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
151 return function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
152 e=e+1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
153 local e=t[e]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
154 if e then return e;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
155 end,self,e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
156 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
157 function stanza_mt:childtags()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
158 local e=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
159 return function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
160 e=e+1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
161 local e=self.tags[e]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
162 if e then return e;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
163 end,self.tags[1],e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
164 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
165 local r
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
166 do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
167 local e={["'"]="&apos;",["\""]="&quot;",["<"]="&lt;",[">"]="&gt;",["&"]="&amp;"};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
168 function r(t)return(m(t,"['&<>\"]",e));end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
169 _M.xml_escape=r;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
170 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
171 local function m(o,t,h,a,r)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
172 local n=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
173 local s=o.name
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
174 e(t,"<"..s);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
175 for o,i in i(o.attr)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
176 if u(o,"\1",1,true)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
177 local o,s=w(o,"^([^\1]*)\1?(.*)$");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
178 n=n+1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
179 e(t," xmlns:ns"..n.."='"..a(o).."' ".."ns"..n..":"..s.."='"..a(i).."'");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
180 elseif not(o=="xmlns"and i==r)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
181 e(t," "..o.."='"..a(i).."'");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
182 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
183 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
184 local i=#o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
185 if i==0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
186 e(t,"/>");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
187 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
188 e(t,">");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
189 for i=1,i do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
190 local i=o[i];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
191 if i.name then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
192 h(i,t,h,a,o.attr.xmlns);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
193 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
194 e(t,a(i));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
195 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
196 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
197 e(t,"</"..s..">");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
198 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
199 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
200 function stanza_mt.__tostring(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
201 local e={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
202 m(t,e,m,r,nil);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
203 return y(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
204 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
205 function stanza_mt.top_tag(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
206 local e="";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
207 if t.attr then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
208 for t,a in i(t.attr)do if o(t)=="string"then e=e..s(" %s='%s'",t,r(l(a)));end end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
209 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
210 return s("<%s%s>",t.name,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
211 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
212 function stanza_mt.get_text(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
213 if#e.tags==0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
214 return y(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
215 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
216 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
217 function stanza_mt.get_error(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
218 local o,a,t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
219 local e=e:get_child("error");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
220 if not e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
221 return nil,nil,nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
222 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
223 o=e.attr.type;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
224 for e in e:children()do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
225 if e.attr.xmlns==f then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
226 if not t and e.name=="text"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
227 t=e:get_text();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
228 elseif not a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
229 a=e.name;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
230 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
231 if a and t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
232 break;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
233 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
234 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
235 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
236 return o,a or"undefined-condition",t or"";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
237 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
238 function stanza_mt.__add(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
239 return e:add_direct_child(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
240 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
241 do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
242 local e=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
243 function new_id()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
244 e=e+1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
245 return"lx"..e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
246 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
247 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
248 function preserialize(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
249 local t={name=a.name,attr=a.attr};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
250 for i,a in n(a)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
251 if o(a)=="table"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
252 e(t,preserialize(a));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
253 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
254 e(t,a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
255 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
256 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
257 return t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
258 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
259 function deserialize(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
260 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
261 local a=t.attr;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
262 for e=1,#a do a[e]=nil;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
263 local s={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
264 for e in i(a)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
265 if u(e,"|",1,true)and not u(e,"\1",1,true)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
266 local o,t=w(e,"^([^|]+)|(.+)$");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
267 s[o.."\1"..t]=a[e];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
268 a[e]=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
269 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
270 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
271 for e,t in i(s)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
272 a[e]=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
273 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
274 d(t,stanza_mt);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
275 for t,e in n(t)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
276 if o(e)=="table"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
277 deserialize(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
278 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
279 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
280 if not t.tags then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
281 local a={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
282 for n,i in n(t)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
283 if o(i)=="table"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
284 e(a,i);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
285 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
286 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
287 t.tags=a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
288 if not t.last_add then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
289 t.last_add={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
290 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
291 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
292 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
293 return t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
294 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
295 function clone(n)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
296 local t={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
297 local function a(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
298 if o(e)~="table"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
299 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
300 elseif t[e]then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
301 return t[e];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
302 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
303 local o={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
304 t[e]=o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
305 for t,e in i(e)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
306 o[a(t)]=a(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
307 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
308 return d(o,p(e));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
309 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
310 return a(n)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
311 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
312 function message(t,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
313 if not e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
314 return stanza("message",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
315 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
316 return stanza("message",t):tag("body"):text(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
317 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
318 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
319 function iq(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
320 if e and not e.id then e.id=new_id();end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
321 return stanza("iq",e or{id=new_id()});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
322 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
323 function reply(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
324 return stanza(e.name,e.attr and{to=e.attr.from,from=e.attr.to,id=e.attr.id,type=((e.name=="iq"and"result")or e.attr.type)});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
325 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
326 do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
327 local a={xmlns=f};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
328 function error_reply(e,o,i,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
329 local e=reply(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
330 e.attr.type="error";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
331 e:tag("error",{type=o})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
332 :tag(i,a):up();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
333 if(t)then e:tag("text",a):text(t):up();end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
334 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
335 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
336 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
337 function presence(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
338 return stanza("presence",e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
339 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
340 if c then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
341 local d=h("yellow");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
342 local u=h("red");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
343 local t=h("red");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
344 local e=h("magenta");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
345 local h=" "..a(d,"%s")..a(e,"=")..a(u,"'%s'");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
346 local d=a(e,"<")..a(t,"%s").."%s"..a(e,">");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
347 local u=d.."%s"..a(e,"</")..a(t,"%s")..a(e,">");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
348 function stanza_mt.pretty_print(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
349 local e="";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
350 for a,t in n(t)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
351 if o(t)=="string"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
352 e=e..r(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
353 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
354 e=e..t:pretty_print();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
355 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
356 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
357 local a="";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
358 if t.attr then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
359 for e,t in i(t.attr)do if o(e)=="string"then a=a..s(h,e,l(t));end end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
360 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
361 return s(u,t.name,a,e,t.name);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
362 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
363 function stanza_mt.pretty_top_tag(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
364 local e="";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
365 if t.attr then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
366 for t,a in i(t.attr)do if o(t)=="string"then e=e..s(h,t,l(a));end end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
367 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
368 return s(d,t.name,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
369 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
370 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
371 stanza_mt.pretty_print=stanza_mt.__tostring;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
372 stanza_mt.pretty_top_tag=stanza_mt.top_tag;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
373 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
374 return _M;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
375 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
376 package.preload['util.timer']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
377 local d=require"net.server".addtimer;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
378 local i=require"net.server".event;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
379 local l=require"net.server".event_base;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
380 local n=os.time;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
381 local s=table.insert;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
382 local e=table.remove;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
383 local e,h=ipairs,pairs;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
384 local r=type;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
385 local o={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
386 local a={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
387 module"timer"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
388 local t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
389 if not i then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
390 function t(e,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
391 local t=n();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
392 e=e+t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
393 if e>=t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
394 s(a,{e,o});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
395 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
396 o();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
397 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
398 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
399 d(function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
400 local e=n();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
401 if#a>0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
402 for t,e in h(a)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
403 s(o,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
404 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
405 a={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
406 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
407 for n,a in h(o)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
408 local i,a=a[1],a[2];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
409 if i<=e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
410 o[n]=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
411 local e=a(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
412 if r(e)=="number"then t(e,a);end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
413 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
414 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
415 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
416 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
417 local o=(i.core and i.core.LEAVE)or-1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
418 function t(a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
419 local e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
420 e=l:addevent(nil,0,function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
421 local t=t();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
422 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
423 return 0,t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
424 elseif e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
425 return o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
426 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
427 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
428 ,a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
429 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
430 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
431 add_task=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
432 return _M;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
433 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
434 package.preload['util.termcolours']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
435 local a,o=table.concat,table.insert;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
436 local e,i=string.char,string.format;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
437 local s=ipairs;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
438 module"termcolours"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
439 local h={
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
440 reset=0;bright=1,dim=2,underscore=4,blink=5,reverse=7,hidden=8;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
441 black=30;red=31;green=32;yellow=33;blue=34;magenta=35;cyan=36;white=37;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
442 ["black background"]=40;["red background"]=41;["green background"]=42;["yellow background"]=43;["blue background"]=44;["magenta background"]=45;["cyan background"]=46;["white background"]=47;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
443 bold=1,dark=2,underline=4,underlined=4,normal=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
444 }
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
445 local n=e(27).."[%sm%s"..e(27).."[0m";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
446 function getstring(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
447 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
448 return i(n,e,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
449 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
450 return t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
451 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
452 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
453 function getstyle(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
454 local e,t={...},{};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
455 for a,e in s(e)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
456 e=h[e];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
457 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
458 o(t,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
459 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
460 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
461 return a(t,";");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
462 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
463 return _M;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
464 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
465 package.preload['util.uuid']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
466 local e=math.random;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
467 local n=tostring;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
468 local e=os.time;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
469 local i=os.clock;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
470 local a=require"util.hashes".sha1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
471 module"uuid"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
472 local t=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
473 local function o()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
474 local e=e();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
475 if t>=e then e=t+1;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
476 t=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
477 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
478 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
479 local function t(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
480 return a(e..i()..n({}),true);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
481 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
482 local e=t(o());
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
483 local function a(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
484 e=t(e..a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
485 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
486 local function t(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
487 if#e<t then a(o());end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
488 local a=e:sub(0,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
489 e=e:sub(t+1);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
490 return a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
491 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
492 local function e()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
493 return("%x"):format(t(1):byte()%4+8);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
494 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
495 function generate()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
496 return t(8).."-"..t(4).."-4"..t(3).."-"..(e())..t(3).."-"..t(12);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
497 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
498 seed=a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
499 return _M;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
500 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
501 package.preload['net.server']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
502 local r=function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
503 return _G[e]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
504 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
505 local ee=function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
506 for t,a in pairs(e)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
507 e[t]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
508 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
509 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
510 local O,e=require("util.logger").init("socket"),table.concat;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
511 local i=function(...)return O("debug",e{...});end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
512 local se=function(...)return O("warn",e{...});end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
513 local e=collectgarbage
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
514 local ne=1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
515 local R=r"type"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
516 local q=r"pairs"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
517 local oe=r"ipairs"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
518 local s=r"tostring"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
519 local e=r"collectgarbage"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
520 local a=r"os"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
521 local o=r"table"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
522 local t=r"string"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
523 local e=r"coroutine"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
524 local Y=a.time
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
525 local H=a.difftime
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
526 local ae=o.concat
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
527 local a=o.remove
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
528 local te=t.len
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
529 local ue=t.sub
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
530 local ce=e.wrap
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
531 local me=e.yield
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
532 local g=r"ssl"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
533 local L=r"socket"or require"socket"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
534 local ie=(g and g.wrap)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
535 local de=L.bind
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
536 local le=L.sleep
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
537 local fe=L.select
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
538 local e=(g and g.newcontext)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
539 local M
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
540 local G
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
541 local B
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
542 local P
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
543 local V
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
544 local Q
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
545 local K
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
546 local X
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
547 local J
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
548 local Z
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
549 local C
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
550 local l
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
551 local he
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
552 local e
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
553 local N
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
554 local re
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
555 local v
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
556 local h
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
557 local S
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
558 local d
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
559 local n
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
560 local _
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
561 local p
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
562 local f
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
563 local c
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
564 local a
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
565 local o
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
566 local y
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
567 local I
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
568 local U
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
569 local A
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
570 local T
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
571 local F
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
572 local u
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
573 local E
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
574 local z
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
575 local j
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
576 local x
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
577 local k
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
578 local D
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
579 local W
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
580 local b
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
581 v={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
582 h={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
583 d={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
584 S={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
585 n={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
586 p={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
587 f={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
588 _={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
589 a=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
590 o=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
591 y=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
592 I=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
593 U=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
594 A=1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
595 T=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
596 E=51e3*1024
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
597 z=25e3*1024
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
598 j=12e5
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
599 x=6e4
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
600 k=6*60*60
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
601 D=false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
602 b=1e3
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
603 _maxsslhandshake=30
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
604 J=function(u,t,w,c,y,f,m)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
605 m=m or b
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
606 local r=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
607 local p,e=u.onconnect or u.onincoming,u.ondisconnect
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
608 local v=t.accept
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
609 local e={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
610 e.shutdown=function()end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
611 e.ssl=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
612 return f~=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
613 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
614 e.sslctx=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
615 return f
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
616 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
617 e.remove=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
618 r=r-1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
619 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
620 e.close=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
621 for a,e in q(n)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
622 if e.serverport==c then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
623 e.disconnect(e,"server closed")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
624 e:close(true)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
625 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
626 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
627 t:close()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
628 o=l(d,t,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
629 a=l(h,t,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
630 n[t]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
631 e=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
632 t=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
633 i"server.lua: closed server handler and removed sockets from list"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
634 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
635 e.ip=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
636 return w
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
637 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
638 e.serverport=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
639 return c
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
640 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
641 e.socket=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
642 return t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
643 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
644 e.readbuffer=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
645 if r>m then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
646 i("server.lua: refused new client connection: server full")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
647 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
648 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
649 local t,a=v(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
650 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
651 local a,o=t:getpeername()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
652 t:settimeout(0)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
653 local e,n,t=N(e,u,t,a,c,o,y,f)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
654 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
655 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
656 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
657 r=r+1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
658 i("server.lua: accepted new client connection from ",s(a),":",s(o)," to ",s(c))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
659 return p(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
660 elseif a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
661 i("server.lua: error with new client connection: ",s(a))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
662 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
663 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
664 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
665 return e
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
666 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
667 N=function(V,e,t,W,B,F,T,q)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
668 t:settimeout(0)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
669 local w
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
670 local x
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
671 local k
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
672 local j
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
673 local C=e.onincoming
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
674 local R=e.onstatus
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
675 local v=e.ondisconnect
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
676 local L=e.ondrain
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
677 local y={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
678 local r=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
679 local Y
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
680 local H
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
681 local O
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
682 local m=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
683 local b=false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
684 local A=false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
685 local S,N=0,0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
686 local E=E
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
687 local z=z
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
688 local e=y
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
689 e.dispatch=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
690 return C
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
691 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
692 e.disconnect=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
693 return v
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
694 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
695 e.setlistener=function(a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
696 C=t.onincoming
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
697 v=t.ondisconnect
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
698 R=t.onstatus
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
699 L=t.ondrain
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
700 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
701 e.getstats=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
702 return N,S
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
703 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
704 e.ssl=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
705 return j
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
706 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
707 e.sslctx=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
708 return q
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
709 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
710 e.send=function(n,i,o,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
711 return w(t,i,o,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
712 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
713 e.receive=function(o,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
714 return x(t,o,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
715 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
716 e.shutdown=function(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
717 return k(t,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
718 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
719 e.setoption=function(i,o,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
720 if t.setoption then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
721 return t:setoption(o,a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
722 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
723 return false,"setoption not implemented";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
724 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
725 e.close=function(u,s)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
726 if not e then return true;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
727 a=l(h,t,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
728 p[e]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
729 if r~=0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
730 if not(s or H)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
731 e.sendbuffer()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
732 if r~=0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
733 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
734 e.write=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
735 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
736 Y=true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
737 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
738 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
739 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
740 w(t,ae(y,"",1,r),1,m)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
741 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
742 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
743 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
744 c=k and k(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
745 t:close()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
746 o=l(d,t,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
747 n[t]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
748 t=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
749 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
750 i"server.lua: socket already closed"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
751 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
752 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
753 f[e]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
754 _[e]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
755 e=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
756 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
757 if V then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
758 V.remove()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
759 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
760 i"server.lua: closed client handler and removed socket from list"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
761 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
762 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
763 e.ip=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
764 return W
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
765 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
766 e.serverport=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
767 return B
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
768 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
769 e.clientport=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
770 return F
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
771 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
772 local _=function(i,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
773 m=m+te(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
774 if m>E then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
775 _[e]="send buffer exceeded"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
776 e.write=P
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
777 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
778 elseif t and not d[t]then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
779 o=addsocket(d,t,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
780 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
781 r=r+1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
782 y[r]=a
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
783 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
784 f[e]=f[e]or u
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
785 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
786 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
787 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
788 e.write=_
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
789 e.bufferqueue=function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
790 return y
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
791 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
792 e.socket=function(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
793 return t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
794 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
795 e.set_mode=function(a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
796 T=t or T
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
797 return T
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
798 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
799 e.set_send=function(a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
800 w=t or w
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
801 return w
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
802 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
803 e.bufferlen=function(o,a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
804 E=t or E
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
805 z=a or z
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
806 return m,z,E
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
807 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
808 e.lock_read=function(i,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
809 if o==true then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
810 local o=a
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
811 a=l(h,t,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
812 p[e]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
813 if a~=o then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
814 b=true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
815 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
816 elseif o==false then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
817 if b then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
818 b=false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
819 a=addsocket(h,t,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
820 p[e]=u
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
821 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
822 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
823 return b
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
824 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
825 e.pause=function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
826 return t:lock_read(true);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
827 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
828 e.resume=function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
829 return t:lock_read(false);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
830 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
831 e.lock=function(i,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
832 e.lock_read(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
833 if a==true then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
834 e.write=P
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
835 local a=o
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
836 o=l(d,t,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
837 f[e]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
838 if o~=a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
839 A=true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
840 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
841 elseif a==false then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
842 e.write=_
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
843 if A then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
844 A=false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
845 _("")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
846 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
847 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
848 return b,A
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
849 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
850 local z=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
851 local o,t,a=x(t,T)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
852 if not t or(t=="wantread"or t=="timeout")then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
853 local o=o or a or""
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
854 local a=te(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
855 if a>z then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
856 v(e,"receive buffer exceeded")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
857 e:close(true)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
858 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
859 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
860 local a=a*ne
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
861 N=N+a
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
862 U=U+a
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
863 p[e]=u
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
864 return C(e,o,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
865 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
866 i("server.lua: client ",s(W),":",s(F)," read error: ",s(t))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
867 H=true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
868 v(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
869 c=e and e:close()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
870 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
871 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
872 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
873 local f=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
874 local b,a,h,n,p;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
875 local p;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
876 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
877 n=ae(y,"",1,r)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
878 b,a,h=w(t,n,1,m)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
879 p=(b or h or 0)*ne
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
880 S=S+p
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
881 I=I+p
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
882 c=D and ee(y)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
883 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
884 b,a,p=false,"closed",0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
885 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
886 if b then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
887 r=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
888 m=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
889 o=l(d,t,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
890 f[e]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
891 if L then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
892 L(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
893 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
894 c=O and e:starttls(nil)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
895 c=Y and e:close()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
896 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
897 elseif h and(a=="timeout"or a=="wantwrite")then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
898 n=ue(n,h+1,m)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
899 y[1]=n
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
900 r=1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
901 m=m-h
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
902 f[e]=u
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
903 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
904 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
905 i("server.lua: client ",s(W),":",s(F)," write error: ",s(a))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
906 H=true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
907 v(e,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
908 c=e and e:close()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
909 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
910 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
911 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
912 local u;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
913 function e.set_sslctx(n,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
914 j=true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
915 q=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
916 local m
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
917 local r
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
918 u=ce(function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
919 local n
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
920 for u=1,_maxsslhandshake do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
921 o=(m and l(d,t,o))or o
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
922 a=(r and l(h,t,a))or a
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
923 r,m=nil,nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
924 c,n=t:dohandshake()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
925 if not n then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
926 i("server.lua: ssl handshake done")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
927 e.readbuffer=z
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
928 e.sendbuffer=f
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
929 c=R and R(e,"ssl-handshake-complete")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
930 a=addsocket(h,t,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
931 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
932 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
933 i("server.lua: error during ssl handshake: ",s(n))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
934 if n=="wantwrite"and not m then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
935 o=addsocket(d,t,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
936 m=true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
937 elseif n=="wantread"and not r then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
938 a=addsocket(h,t,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
939 r=true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
940 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
941 break;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
942 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
943 me()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
944 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
945 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
946 v(e,"ssl handshake failed")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
947 c=e and e:close(true)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
948 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
949 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
950 )
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
951 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
952 if g then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
953 if q then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
954 e:set_sslctx(q);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
955 i("server.lua: ","starting ssl handshake")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
956 local a
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
957 t,a=ie(t,q)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
958 if a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
959 i("server.lua: ssl error: ",s(a))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
960 return nil,nil,a
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
961 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
962 t:settimeout(0)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
963 e.readbuffer=u
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
964 e.sendbuffer=u
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
965 u(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
966 if not t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
967 return nil,nil,"ssl handshake failed";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
968 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
969 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
970 local c;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
971 e.starttls=function(f,m)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
972 if m then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
973 c=m;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
974 e:set_sslctx(c);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
975 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
976 if r>0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
977 i"server.lua: we need to do tls, but delaying until send buffer empty"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
978 O=true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
979 return
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
980 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
981 i("server.lua: attempting to start tls on "..s(t))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
982 local m,r=t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
983 t,r=ie(t,c)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
984 if r then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
985 i("server.lua: error while starting tls on client: ",s(r))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
986 return nil,r
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
987 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
988 t:settimeout(0)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
989 w=t.send
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
990 x=t.receive
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
991 k=M
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
992 n[t]=e
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
993 a=addsocket(h,t,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
994 a=l(h,m,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
995 o=l(d,m,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
996 n[m]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
997 e.starttls=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
998 O=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
999 j=true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1000 e.readbuffer=u
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1001 e.sendbuffer=u
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1002 u(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1003 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1004 e.readbuffer=z
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1005 e.sendbuffer=f
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1006 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1007 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1008 e.readbuffer=z
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1009 e.sendbuffer=f
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1010 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1011 w=t.send
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1012 x=t.receive
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1013 k=(j and M)or t.shutdown
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1014 n[t]=e
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1015 a=addsocket(h,t,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1016 return e,t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1017 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1018 M=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1019 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1020 P=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1021 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1022 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1023 addsocket=function(t,a,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1024 if not t[a]then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1025 e=e+1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1026 t[e]=a
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1027 t[a]=e
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1028 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1029 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1030 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1031 l=function(e,a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1032 local i=e[a]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1033 if i then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1034 e[a]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1035 local o=e[t]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1036 e[t]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1037 if o~=a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1038 e[o]=i
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1039 e[i]=o
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1040 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1041 return t-1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1042 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1043 return t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1044 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1045 C=function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1046 o=l(d,e,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1047 a=l(h,e,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1048 n[e]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1049 e:close()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1050 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1051 local function l(e,t,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1052 local a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1053 local i=t.sendbuffer;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1054 function t.sendbuffer()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1055 i();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1056 if a and t.bufferlen()<o then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1057 e:lock_read(false);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1058 a=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1059 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1060 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1061 local i=e.readbuffer;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1062 function e.readbuffer()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1063 i();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1064 if not a and t.bufferlen()>=o then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1065 a=true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1066 e:lock_read(true);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1067 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1068 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1069 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1070 K=function(o,e,d,l,r)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1071 local t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1072 if R(d)~="table"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1073 t="invalid listener table"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1074 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1075 if R(e)~="number"or not(e>=0 and e<=65535)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1076 t="invalid port"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1077 elseif v[e]then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1078 t="listeners on port '"..e.."' already exist"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1079 elseif r and not g then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1080 t="luasec not found"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1081 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1082 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1083 se("server.lua, port ",e,": ",t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1084 return nil,t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1085 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1086 o=o or"*"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1087 local t,s=de(o,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1088 if s then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1089 se("server.lua, port ",e,": ",s)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1090 return nil,s
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1091 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1092 local s,d=J(d,t,o,e,l,r,b)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1093 if not s then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1094 t:close()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1095 return nil,d
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1096 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1097 t:settimeout(0)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1098 a=addsocket(h,t,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1099 v[e]=s
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1100 n[t]=s
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1101 i("server.lua: new "..(r and"ssl "or"").."server listener on '",o,":",e,"'")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1102 return s
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1103 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1104 X=function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1105 return v[e];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1106 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1107 he=function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1108 local t=v[e]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1109 if not t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1110 return nil,"no server found on port '"..s(e).."'"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1111 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1112 t:close()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1113 v[e]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1114 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1115 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1116 Q=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1117 for e,t in q(n)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1118 t:close()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1119 n[e]=nil
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1120 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1121 a=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1122 o=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1123 y=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1124 v={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1125 h={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1126 d={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1127 S={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1128 n={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1129 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1130 Z=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1131 return A,T,E,z,j,x,k,D,b,_maxsslhandshake
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1132 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1133 re=function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1134 if R(e)~="table"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1135 return nil,"invalid settings table"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1136 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1137 A=tonumber(e.timeout)or A
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1138 T=tonumber(e.sleeptime)or T
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1139 E=tonumber(e.maxsendlen)or E
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1140 z=tonumber(e.maxreadlen)or z
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1141 j=tonumber(e.checkinterval)or j
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1142 x=tonumber(e.sendtimeout)or x
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1143 k=tonumber(e.readtimeout)or k
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1144 D=e.cleanqueue
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1145 b=e._maxclientsperserver or b
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1146 _maxsslhandshake=e._maxsslhandshake or _maxsslhandshake
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1147 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1148 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1149 V=function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1150 if R(e)~="function"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1151 return nil,"invalid listener function"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1152 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1153 y=y+1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1154 S[y]=e
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1155 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1156 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1157 B=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1158 return U,I,a,o,y
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1159 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1160 local e=true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1161 setquitting=function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1162 e=not t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1163 return;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1164 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1165 G=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1166 while e do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1167 local a,e,t=fe(h,d,A)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1168 for e,t in oe(e)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1169 local e=n[t]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1170 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1171 e.sendbuffer()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1172 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1173 C(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1174 i"server.lua: found no handler and closed socket (writelist)"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1175 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1176 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1177 for t,e in oe(a)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1178 local t=n[e]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1179 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1180 t.readbuffer()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1181 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1182 C(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1183 i"server.lua: found no handler and closed socket (readlist)"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1184 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1185 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1186 for e,t in q(_)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1187 e.disconnect()(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1188 e:close(true)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1189 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1190 ee(_)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1191 u=Y()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1192 if H(u-W)>=1 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1193 for e=1,y do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1194 S[e](u)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1195 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1196 W=u
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1197 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1198 le(T)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1199 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1200 return"quitting"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1201 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1202 local function h()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1203 return"select";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1204 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1205 local i=function(t,i,s,a,e,h)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1206 local e=N(nil,a,t,i,s,"clientport",e,h)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1207 n[t]=e
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1208 o=addsocket(d,t,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1209 if a.onconnect then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1210 local t=e.sendbuffer;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1211 e.sendbuffer=function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1212 a.onconnect(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1213 e.sendbuffer=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1214 if#e:bufferqueue()>0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1215 return t();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1216 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1217 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1218 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1219 return e,t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1220 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1221 local a=function(o,a,n,s,h)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1222 local t,e=L.tcp()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1223 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1224 return nil,e
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1225 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1226 t:settimeout(0)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1227 c,e=t:connect(o,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1228 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1229 local e=i(t,o,a,n)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1230 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1231 N(nil,n,t,o,a,"clientport",s,h)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1232 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1233 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1234 r"setmetatable"(n,{__mode="k"})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1235 r"setmetatable"(p,{__mode="k"})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1236 r"setmetatable"(f,{__mode="k"})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1237 W=Y()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1238 F=Y()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1239 V(function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1240 local e=H(u-F)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1241 if e>j then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1242 F=u
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1243 for e,t in q(f)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1244 if H(u-t)>x then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1245 e.disconnect()(e,"send timeout")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1246 e:close(true)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1247 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1248 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1249 for e,t in q(p)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1250 if H(u-t)>k then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1251 e.disconnect()(e,"read timeout")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1252 e:close()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1253 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1254 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1255 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1256 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1257 )
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1258 local function t(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1259 local t=O;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1260 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1261 O=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1262 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1263 return t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1264 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1265 return{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1266 addclient=a,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1267 wrapclient=i,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1268 loop=G,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1269 link=l,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1270 stats=B,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1271 closeall=Q,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1272 addtimer=V,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1273 addserver=K,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1274 getserver=X,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1275 setlogger=t,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1276 getsettings=Z,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1277 setquitting=setquitting,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1278 removeserver=he,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1279 get_backend=h,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1280 changesettings=re,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1281 }
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1282 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1283 package.preload['core.xmlhandlers']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1284 require"util.stanza"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1285 local y=stanza;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1286 local u=tostring;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1287 local w=table.insert;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1288 local l=table.concat;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1289 local r=require"util.logger".init("xmlhandlers");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1290 local s=error;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1291 module"xmlhandlers"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1292 local f={
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1293 ["http://www.w3.org/XML/1998/namespace"]="xml";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1294 };
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1295 local i="http://etherx.jabber.org/streams";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1296 local t="\1";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1297 local h="^([^"..t.."]*)"..t.."?(.*)$";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1298 function init_xmlhandlers(a,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1299 local o={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1300 local n={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1301 local r=a.log or r;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1302 local r=e.streamopened;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1303 local d=e.streamclosed;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1304 local s=e.error or function(t,e)s("XML stream error: "..u(e));end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1305 local m=e.handlestanza;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1306 local i=e.stream_ns or i;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1307 local c=i..t..(e.stream_tag or"stream");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1308 local p=i..t..(e.error_tag or"error");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1309 local u=e.default_ns;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1310 local e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1311 function n:StartElement(n,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1312 if e and#o>0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1313 e:text(l(o));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1314 o={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1315 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1316 local o,i=n:match(h);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1317 if i==""then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1318 o,i="",o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1319 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1320 if o~=u then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1321 t.xmlns=o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1322 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1323 for e=1,#t do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1324 local a=t[e];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1325 t[e]=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1326 local e,o=a:match(h);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1327 if o~=""then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1328 e=f[e];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1329 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1330 t[e..":"..o]=t[a];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1331 t[a]=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1332 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1333 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1334 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1335 if not e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1336 if a.notopen then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1337 if n==c then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1338 if r then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1339 r(a,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1340 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1341 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1342 s(a,"no-stream");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1343 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1344 return;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1345 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1346 if o=="jabber:client"and i~="iq"and i~="presence"and i~="message"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1347 s(a,"invalid-top-level-element");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1348 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1349 e=y.stanza(i,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1350 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1351 t.xmlns=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1352 if o~=u then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1353 t.xmlns=o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1354 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1355 e:tag(i,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1356 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1357 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1358 function n:CharacterData(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1359 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1360 w(o,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1361 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1362 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1363 function n:EndElement(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1364 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1365 if#o>0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1366 e:text(l(o));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1367 o={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1368 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1369 if#e.last_add==0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1370 if t~=p then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1371 m(a,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1372 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1373 s(a,"stream-error",e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1374 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1375 e=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1376 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1377 e:up();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1378 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1379 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1380 if t==c then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1381 if d then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1382 d(a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1383 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1384 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1385 local t,e=t:match(h);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1386 if e==""then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1387 t,e="",t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1388 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1389 s(a,"parse-error","unexpected-element-close",e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1390 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1391 e,o=nil,{};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1392 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1393 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1394 return n;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1395 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1396 return init_xmlhandlers;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1397 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1398 package.preload['util.jid']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1399 local t=string.match;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1400 local h=require"util.encodings".stringprep.nodeprep;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1401 local s=require"util.encodings".stringprep.nameprep;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1402 local n=require"util.encodings".stringprep.resourceprep;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1403 module"jid"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1404 local function a(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1405 if not e then return;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1406 local o,a=t(e,"^([^@]+)@()");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1407 local a,i=t(e,"^([^@/]+)()",a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1408 if o and not a then return nil,nil,nil;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1409 local t=t(e,"^/(.+)$",i);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1410 if(not a)or((not t)and#e>=i)then return nil,nil,nil;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1411 return o,a,t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1412 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1413 split=a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1414 function bare(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1415 local t,e=a(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1416 if t and e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1417 return t.."@"..e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1418 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1419 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1420 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1421 local function o(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1422 local t,e,a=a(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1423 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1424 e=s(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1425 if not e then return;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1426 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1427 t=h(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1428 if not t then return;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1429 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1430 if a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1431 a=n(a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1432 if not a then return;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1433 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1434 return t,e,a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1435 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1436 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1437 prepped_split=o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1438 function prep(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1439 local t,e,a=o(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1440 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1441 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1442 e=t.."@"..e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1443 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1444 if a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1445 e=e.."/"..a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1446 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1447 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1448 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1449 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1450 function join(t,e,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1451 if t and e and a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1452 return t.."@"..e.."/"..a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1453 elseif t and e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1454 return t.."@"..e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1455 elseif e and a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1456 return e.."/"..a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1457 elseif e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1458 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1459 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1460 return nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1461 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1462 function compare(t,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1463 local n,i,o=a(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1464 local t,a,e=a(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1465 if((t~=nil and t==n)or t==nil)and
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1466 ((a~=nil and a==i)or a==nil)and
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1467 ((e~=nil and e==o)or e==nil)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1468 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1469 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1470 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1471 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1472 return _M;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1473 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1474 package.preload['util.events']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1475 local r=ipairs;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1476 local i=pairs;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1477 local s=table.insert;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1478 local h=table.sort;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1479 local d=select;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1480 module"events"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1481 function new()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1482 local o={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1483 local t={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1484 local a={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1485 local function n(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1486 local a=a[o];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1487 local e=t[o];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1488 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1489 for t=#e,1,-1 do e[t]=nil;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1490 else e={};t[o]=e;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1491 for t in i(a)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1492 s(e,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1493 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1494 h(e,function(t,e)return a[t]>a[e];end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1495 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1496 local function s(t,o,i)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1497 local e=a[t];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1498 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1499 e[o]=i or 0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1500 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1501 e={[o]=i or 0};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1502 a[t]=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1503 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1504 n(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1505 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1506 local function h(t,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1507 local e=a[t];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1508 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1509 e[o]=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1510 n(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1511 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1512 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1513 local function e(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1514 for e,t in i(e)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1515 s(e,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1516 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1517 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1518 local function e(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1519 for t,e in i(e)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1520 h(t,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1521 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1522 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1523 local function n(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1524 local e=t[a];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1525 if not e then e={};t[a]=e;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1526 local e=function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1527 for t=1,#e do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1528 local e=e[t](...);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1529 if e~=nil then return e;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1530 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1531 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1532 o[a]=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1533 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1534 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1535 local function i(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1536 return o[e]or n(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1537 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1538 local function n(e,...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1539 local e=t[e];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1540 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1541 for t=1,#e do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1542 local e=e[t](...);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1543 if e~=nil then return e;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1544 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1545 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1546 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1547 local function l(e,...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1548 local a=i(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1549 local t={...};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1550 local e={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1551 return function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1552 for t,o in r(t)do e[o]=d(t,...);end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1553 a(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1554 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1555 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1556 return{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1557 add_handler=s;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1558 remove_handler=h;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1559 add_plugin=add_plugin;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1560 remove_plugin=remove_plugin;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1561 get_dispatcher=i;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1562 fire_event=n;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1563 get_named_arg_dispatcher=l;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1564 _dispatchers=o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1565 _handlers=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1566 _event_map=a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1567 };
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1568 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1569 return _M;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1570 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1571 package.preload['util.sha1']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1572 local u=string.len
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1573 local a=string.char
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1574 local k=string.byte
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1575 local g=string.sub
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1576 local m=math.floor
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1577 local t=require"bit"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1578 local b=t.bnot
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1579 local e=t.band
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1580 local p=t.bor
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1581 local n=t.bxor
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1582 local o=t.lshift
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1583 local i=t.rshift
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1584 local h,r,d,l,c
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1585 local function y(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1586 return o(e,t)+i(e,32-t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1587 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1588 local function s(i)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1589 local t,o
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1590 local t=""
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1591 for n=1,8 do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1592 o=e(i,15)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1593 if(o<10)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1594 t=a(o+48)..t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1595 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1596 t=a(o+87)..t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1597 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1598 i=m(i/16)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1599 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1600 return t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1601 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1602 local function j(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1603 local i,o
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1604 local n=""
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1605 i=u(t)*8
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1606 t=t..a(128)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1607 o=56-e(u(t),63)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1608 if(o<0)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1609 o=o+64
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1610 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1611 for e=1,o do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1612 t=t..a(0)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1613 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1614 for t=1,8 do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1615 n=a(e(i,255))..n
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1616 i=m(i/256)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1617 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1618 return t..n
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1619 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1620 local function q(w)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1621 local s,t,i,o,f,u,m,v
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1622 local a,a
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1623 local a={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1624 while(w~="")do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1625 for e=0,15 do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1626 a[e]=0
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1627 for t=1,4 do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1628 a[e]=a[e]*256+k(w,e*4+t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1629 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1630 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1631 for e=16,79 do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1632 a[e]=y(n(n(a[e-3],a[e-8]),n(a[e-14],a[e-16])),1)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1633 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1634 s=h
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1635 t=r
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1636 i=d
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1637 o=l
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1638 f=c
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1639 for r=0,79 do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1640 if(r<20)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1641 u=p(e(t,i),e(b(t),o))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1642 m=1518500249
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1643 elseif(r<40)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1644 u=n(n(t,i),o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1645 m=1859775393
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1646 elseif(r<60)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1647 u=p(p(e(t,i),e(t,o)),e(i,o))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1648 m=2400959708
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1649 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1650 u=n(n(t,i),o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1651 m=3395469782
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1652 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1653 v=y(s,5)+u+f+m+a[r]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1654 f=o
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1655 o=i
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1656 i=y(t,30)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1657 t=s
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1658 s=v
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1659 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1660 h=e(h+s,4294967295)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1661 r=e(r+t,4294967295)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1662 d=e(d+i,4294967295)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1663 l=e(l+o,4294967295)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1664 c=e(c+f,4294967295)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1665 w=g(w,65)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1666 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1667 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1668 local function a(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1669 e=j(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1670 h=1732584193
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1671 r=4023233417
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1672 d=2562383102
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1673 l=271733878
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1674 c=3285377520
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1675 q(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1676 local e=s(h)..s(r)..s(d)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1677 ..s(l)..s(c);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1678 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1679 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1680 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1681 return e:gsub("..",function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1682 return string.char(tonumber(e,16));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1683 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1684 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1685 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1686 _G.sha1={sha1=a};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1687 return _G.sha1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1688 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1689 package.preload['verse.plugins.tls']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1690 local o=require"util.stanza";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1691 local t="urn:ietf:params:xml:ns:xmpp-tls";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1692 function verse.plugins.tls(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1693 local function a(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1694 if e.authenticated then return;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1695 if a:get_child("starttls",t)and e.conn.starttls then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1696 e:debug("Negotiating TLS...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1697 e:send(o.stanza("starttls",{xmlns=t}));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1698 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1699 elseif not e.conn.starttls and not e.secure then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1700 e:warn("SSL libary (LuaSec) not loaded, so TLS not available");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1701 elseif not e.secure then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1702 e:debug("Server doesn't offer TLS :(");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1703 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1704 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1705 local function o(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1706 if t.name=="proceed"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1707 e:debug("Server says proceed, handshake starting...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1708 e.conn:starttls({mode="client",protocol="sslv23",options="no_sslv2"},true);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1709 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1710 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1711 local function i(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1712 if t=="ssl-handshake-complete"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1713 e.secure=true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1714 e:debug("Re-opening stream...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1715 e:reopen();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1716 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1717 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1718 e:hook("stream-features",a,400);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1719 e:hook("stream/"..t,o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1720 e:hook("status",i,400);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1721 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1722 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1723 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1724 package.preload['verse.plugins.sasl']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1725 local o=require"util.stanza";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1726 local e=require"util.xstanza";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1727 local t=require"mime".b64;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1728 local a="urn:ietf:params:xml:ns:xmpp-sasl";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1729 function verse.plugins.sasl(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1730 local function i(i)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1731 if e.authenticated then return;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1732 e:debug("Authenticating with SASL...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1733 local t=t("\0"..e.username.."\0"..e.password);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1734 e:debug("Selecting PLAIN mechanism...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1735 local a=o.stanza("auth",{xmlns=a,mechanism="PLAIN"});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1736 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1737 a:text(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1738 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1739 e:send(a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1740 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1741 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1742 local function o(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1743 if t.name=="success"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1744 e.authenticated=true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1745 e:event("authentication-success");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1746 elseif t.name=="failure"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1747 local t=t.tags[1];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1748 e:event("authentication-failure",{condition=t.name});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1749 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1750 e:reopen();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1751 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1752 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1753 e:hook("stream-features",i,300);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1754 e:hook("stream/"..a,o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1755 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1756 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1757 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1758 package.preload['verse.plugins.bind']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1759 local t=require"util.stanza";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1760 local a="urn:ietf:params:xml:ns:xmpp-bind";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1761 function verse.plugins.bind(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1762 local function i(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1763 if e.bound then return;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1764 e:debug("Binding resource...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1765 e:send_iq(t.iq({type="set"}):tag("bind",{xmlns=a}):tag("resource"):text(e.resource),
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1766 function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1767 if t.attr.type=="result"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1768 local t=t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1769 :get_child("bind",a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1770 :get_child("jid")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1771 :get_text();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1772 e.username,e.host,e.resource=jid.split(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1773 e.jid,e.bound=t,true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1774 e:event("bind-success",full_jid);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1775 elseif t.attr.type=="error"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1776 local a=t:child_with_name("error");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1777 local o,a,t=t:get_error();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1778 e:event("bind-failure",{error=a,text=t,type=o});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1779 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1780 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1781 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1782 e:hook("stream-features",i,200);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1783 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1784 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1785 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1786 package.preload['verse.plugins.version']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1787 local t="jabber:iq:version";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1788 local function a(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1789 e.name=t.name;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1790 e.version=t.version;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1791 e.platform=t.platform;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1792 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1793 function verse.plugins.version(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1794 e.version={set=a};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1795 e:hook("iq/"..t,function(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1796 if a.attr.type~="get"then return;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1797 local t=verse.reply(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1798 :tag("query",{xmlns=t});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1799 if e.version.name then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1800 t:tag("name"):text(tostring(e.version.name)):up();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1801 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1802 if e.version.version then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1803 t:tag("version"):text(tostring(e.version.version)):up()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1804 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1805 if e.version.platform then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1806 t:tag("os"):text(e.version.platform);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1807 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1808 e:send(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1809 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1810 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1811 function e:query_version(o,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1812 a=a or function(t)return e:event("version/response",t);end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1813 e:send_iq(verse.iq({type="get",to=o})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1814 :tag("query",{xmlns=t}),
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1815 function(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1816 local e=o:get_child("query",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1817 if o.attr.type=="result"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1818 local o=e:get_child("name");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1819 local t=e:get_child("version");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1820 local e=e:get_child("os");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1821 a({
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1822 name=o and o:get_text()or nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1823 version=t and t:get_text()or nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1824 platform=e and e:get_text()or nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1825 });
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1826 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1827 local e,t,o=o:get_error();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1828 a({
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1829 error=true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1830 condition=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1831 text=o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1832 type=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1833 });
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1834 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1835 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1836 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1837 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1838 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1839 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1840 package.preload['verse.plugins.ping']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1841 require"util.xstanza";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1842 local o="urn:xmpp:ping";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1843 function verse.plugins.ping(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1844 function t:ping(e,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1845 local n=socket.gettime();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1846 t:send_iq(verse.iq{to=e,type="get"}:tag("ping",{xmlns=o}),
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1847 function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1848 if t.attr.type=="error"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1849 local i,t,o=t:get_error();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1850 if t~="service-unavailable"and t~="feature-not-implemented"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1851 a(nil,e,{type=i,condition=t,text=o});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1852 return;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1853 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1854 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1855 a(socket.gettime()-n,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1856 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1857 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1858 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1859 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1860 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1861 package.preload['verse.plugins.session']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1862 local i=require"util.stanza";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1863 local a="urn:ietf:params:xml:ns:xmpp-session";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1864 function verse.plugins.session(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1865 local function o(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1866 local t=t:get_child("session",a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1867 if t and not t:get_child("optional")then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1868 local function o(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1869 e:debug("Establishing Session...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1870 e:send_iq(i.iq({type="set"}):tag("session",{xmlns=a}),
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1871 function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1872 if t.attr.type=="result"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1873 e:event("session-success");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1874 elseif t.attr.type=="error"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1875 local a=t:child_with_name("error");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1876 local a,t,o=t:get_error();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1877 e:event("session-failure",{error=t,text=o,type=a});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1878 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1879 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1880 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1881 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1882 e:hook("bind-success",o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1883 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1884 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1885 e:hook("stream-features",o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1886 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1887 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1888 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1889 package.preload['verse.plugins.compression']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1890 local t=require"util.stanza";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1891 local e=require"zlib";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1892 local a="http://jabber.org/features/compress"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1893 local a="http://jabber.org/protocol/compress"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1894 local o="http://etherx.jabber.org/streams";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1895 local i=9;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1896 local function s(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1897 local i,e=pcall(e.deflate,i);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1898 if i==false then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1899 local t=t.stanza("failure",{xmlns=a}):tag("setup-failed");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1900 o:send(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1901 o:error("Failed to create zlib.deflate filter: %s",tostring(e));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1902 return
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1903 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1904 return e
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1905 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1906 local function h(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1907 local i,e=pcall(e.inflate);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1908 if i==false then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1909 local t=t.stanza("failure",{xmlns=a}):tag("setup-failed");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1910 o:send(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1911 o:error("Failed to create zlib.inflate filter: %s",tostring(e));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1912 return
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1913 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1914 return e
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1915 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1916 local function n(e,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1917 function e:send(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1918 local o,a,i=pcall(o,tostring(a),'sync');
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1919 if o==false then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1920 e:close({
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1921 condition="undefined-condition";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1922 text=a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1923 extra=t.stanza("failure",{xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1924 });
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1925 e:warn("Compressed send failed: %s",tostring(a));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1926 return;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1927 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1928 e.conn:write(a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1929 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1930 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1931 local function i(e,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1932 local o=e.data
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1933 e.data=function(i,n)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1934 e:debug("Decompressing data...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1935 local n,a,s=pcall(a,n);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1936 if n==false then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1937 e:close({
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1938 condition="undefined-condition";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1939 text=a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1940 extra=t.stanza("failure",{xmlns="http://jabber.org/protocol/compress"}):tag("processing-failed");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1941 });
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1942 stream:warn("%s",tostring(a));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1943 return;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1944 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1945 return o(i,a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1946 end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1947 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1948 function verse.plugins.compression(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1949 local function r(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1950 if not e.compressed then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1951 local o=o:child_with_name("compression");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1952 if o then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1953 for o in o:children()do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1954 local o=o[1]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1955 if o=="zlib"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1956 e:send(t.stanza("compress",{xmlns=a}):tag("method"):text("zlib"))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1957 e:debug("Enabled compression using zlib.")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1958 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1959 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1960 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1961 session:debug("Remote server supports no compression algorithm we support.")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1962 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1963 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1964 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1965 local function o(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1966 if t.name=="compressed"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1967 e:debug("Activating compression...")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1968 local t=s(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1969 if not t then return end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1970 local a=h(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1971 if not a then return end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1972 n(e,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1973 i(e,a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1974 e.compressed=true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1975 e:reopen();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1976 elseif t.name=="failure"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1977 e:warn("Failed to establish compression");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1978 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1979 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1980 e:hook("stream-features",r,250);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1981 e:hook("stream/"..a,o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1982 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1983 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1984 package.preload['verse.plugins.blocking']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1985 local a="urn:xmpp:blocking";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1986 function verse.plugins.blocking(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1987 e.blocking={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1988 function e.blocking:block_jid(o,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1989 e:send_iq(verse.iq{type="set"}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1990 :tag("block",{xmlns=a})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1991 :tag("item",{jid=o})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1992 ,function()return t and t(true);end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1993 ,function()return t and t(false);end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1994 );
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1995 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1996 function e.blocking:unblock_jid(o,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1997 e:send_iq(verse.iq{type="set"}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1998 :tag("unblock",{xmlns=a})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
1999 :tag("item",{jid=o})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2000 ,function()return t and t(true);end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2001 ,function()return t and t(false);end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2002 );
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2003 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2004 function e.blocking:unblock_all_jids(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2005 e:send_iq(verse.iq{type="set"}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2006 :tag("unblock",{xmlns=a})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2007 ,function()return t and t(true);end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2008 ,function()return t and t(false);end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2009 );
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2010 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2011 function e.blocking:get_blocked_jids(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2012 e:send_iq(verse.iq{type="get"}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2013 :tag("blocklist",{xmlns=a})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2014 ,function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2015 local a=e:get_child("blocklist",a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2016 if not a then return t and t(false);end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2017 local e={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2018 for t in a:childtags()do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2019 e[#e+1]=t.attr.jid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2020 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2021 return t and t(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2022 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2023 ,function(e)return t and t(false);end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2024 );
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2025 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2026 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2027 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2028 package.preload['verse.plugins.proxy65']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2029 local e=require"util.events";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2030 local h=require"util.uuid";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2031 local r=require"util.sha1";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2032 local i={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2033 i.__index=i;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2034 local o="http://jabber.org/protocol/bytestreams";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2035 local n;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2036 function verse.plugins.proxy65(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2037 t.proxy65=setmetatable({stream=t},i);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2038 t.proxy65.available_streamhosts={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2039 local e=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2040 t:hook("disco/service-discovered/proxy",function(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2041 if a.type=="bytestreams"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2042 e=e+1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2043 t:send_iq(verse.iq({to=a.jid,type="get"})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2044 :tag("query",{xmlns=o}),function(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2045 e=e-1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2046 if a.attr.type=="result"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2047 local e=a:get_child("query",o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2048 :get_child("streamhost").attr;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2049 t.proxy65.available_streamhosts[e.jid]={
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2050 jid=e.jid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2051 host=e.host;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2052 port=tonumber(e.port);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2053 };
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2054 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2055 if e==0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2056 t:event("proxy65/discovered-proxies",t.proxy65.available_streamhosts);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2057 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2058 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2059 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2060 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2061 t:hook("iq/"..o,function(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2062 local e=verse.new(nil,{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2063 initiator_jid=a.attr.from,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2064 streamhosts={},
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2065 current_host=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2066 });
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2067 for t in a.tags[1]:childtags()do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2068 if t.name=="streamhost"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2069 table.insert(e.streamhosts,t.attr);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2070 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2071 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2072 local function o()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2073 if e.current_host<#e.streamhosts then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2074 e.current_host=e.current_host+1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2075 e:connect(
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2076 e.streamhosts[e.current_host].host,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2077 e.streamhosts[e.current_host].port
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2078 );
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2079 n(t,e,a.tags[1].attr.sid,a.attr.from,t.jid);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2080 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2081 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2082 e:unhook("disconnected",o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2083 t:send(verse.error_reply(a,"cancel","item-not-found"));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2084 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2085 function e:accept()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2086 e:hook("disconnected",o,100);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2087 e:hook("connected",function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2088 e:unhook("disconnected",o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2089 local e=verse.reply(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2090 :tag("query",a.tags[1].attr)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2091 :tag("streamhost-used",{jid=e.streamhosts[e.current_host].jid});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2092 t:send(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2093 end,100);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2094 o();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2095 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2096 function e:refuse()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2097 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2098 t:event("proxy65/request",e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2099 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2100 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2101 function i:new(t,s)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2102 local e=verse.new(nil,{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2103 target_jid=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2104 bytestream_sid=h.generate();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2105 });
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2106 local a=verse.iq{type="set",to=t}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2107 :tag("query",{xmlns=o,mode="tcp",sid=e.bytestream_sid});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2108 for t,e in ipairs(s or self.proxies)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2109 a:tag("streamhost",e):up();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2110 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2111 self.stream:send_iq(a,function(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2112 if a.attr.type=="error"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2113 local a,o,t=a:get_error();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2114 e:event("connection-failed",{conn=e,type=a,condition=o,text=t});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2115 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2116 local a=a.tags[1]:get_child("streamhost-used");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2117 if not a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2118 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2119 e.streamhost_jid=a.attr.jid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2120 local a,i;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2121 for o,t in ipairs(s or self.proxies)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2122 if t.jid==e.streamhost_jid then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2123 a,i=t.host,t.port;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2124 break;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2125 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2126 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2127 if not(a and i)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2128 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2129 e:connect(a,i);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2130 local function a()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2131 e:unhook("connected",a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2132 local t=verse.iq{to=e.streamhost_jid,type="set"}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2133 :tag("query",{xmlns=o,sid=e.bytestream_sid})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2134 :tag("activate"):text(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2135 self.stream:send_iq(t,function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2136 if t.attr.type=="result"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2137 e:event("connected",e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2138 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2139 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2140 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2141 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2142 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2143 e:hook("connected",a,100);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2144 n(self.stream,e,e.bytestream_sid,self.stream.jid,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2145 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2146 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2147 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2148 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2149 function n(i,e,a,t,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2150 local t=r.sha1(a..t..o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2151 local function a()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2152 e:unhook("connected",a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2153 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2154 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2155 local function o(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2156 e:unhook("incoming-raw",o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2157 if t:sub(1,2)~="\005\000"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2158 return e:event("error","connection-failure");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2159 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2160 e:event("connected");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2161 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2162 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2163 local function i(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2164 e:unhook("incoming-raw",i);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2165 if a~="\005\000"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2166 local t="version-mismatch";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2167 if a:sub(1,1)=="\005"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2168 t="authentication-failure";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2169 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2170 return e:event("error",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2171 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2172 e:send(string.char(5,1,0,3,#t)..t.."\0\0");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2173 e:hook("incoming-raw",o,100);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2174 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2175 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2176 e:hook("connected",a,200);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2177 e:hook("incoming-raw",i,100);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2178 e:send("\005\001\000");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2179 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2180 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2181 package.preload['verse.plugins.jingle']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2182 local e=require"util.sha1".sha1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2183 local n=require"util.stanza";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2184 local e=require"util.timer";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2185 local a=require"util.uuid".generate;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2186 local o="urn:xmpp:jingle:1";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2187 local h="urn:xmpp:jingle:errors:1";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2188 local t={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2189 t.__index=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2190 local e={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2191 local e={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2192 function verse.plugins.jingle(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2193 e:hook("ready",function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2194 e:add_disco_feature(o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2195 end,10);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2196 function e:jingle(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2197 return verse.eventable(setmetatable(base or{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2198 role="initiator";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2199 peer=o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2200 sid=a();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2201 stream=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2202 },t));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2203 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2204 function e:register_jingle_transport(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2205 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2206 function e:register_jingle_content_type(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2207 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2208 local function u(i)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2209 local r=i:get_child("jingle",o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2210 local a=r.attr.sid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2211 local s=r.attr.action;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2212 local a=e:event("jingle/"..a,i);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2213 if a==true then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2214 e:send(verse.reply(i));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2215 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2216 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2217 if s~="session-initiate"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2218 local t=n.error_reply(i,"cancel","item-not-found")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2219 :tag("unknown-session",{xmlns=h}):up();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2220 e:send(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2221 return;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2222 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2223 local l=r.attr.sid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2224 local a=verse.eventable{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2225 role="receiver";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2226 peer=i.attr.from;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2227 sid=l;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2228 stream=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2229 };
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2230 setmetatable(a,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2231 local s;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2232 local d,h;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2233 for t in r:childtags()do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2234 if t.name=="content"and t.attr.xmlns==o then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2235 local i=t:child_with_name("description");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2236 local o=i.attr.xmlns;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2237 if o then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2238 local e=e:event("jingle/content/"..o,a,i);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2239 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2240 d=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2241 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2242 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2243 local o=t:child_with_name("transport");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2244 local i=o.attr.xmlns;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2245 h=e:event("jingle/transport/"..i,a,o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2246 if d and h then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2247 s=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2248 break;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2249 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2250 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2251 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2252 if not d then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2253 e:send(n.error_reply(i,"cancel","feature-not-implemented","The specified content is not supported"));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2254 return;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2255 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2256 if not h then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2257 e:send(n.error_reply(i,"cancel","feature-not-implemented","The specified transport is not supported"));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2258 return;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2259 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2260 e:send(n.reply(i));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2261 a.content_tag=s;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2262 a.creator,a.name=s.attr.creator,s.attr.name;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2263 a.content,a.transport=d,h;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2264 function a:decline()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2265 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2266 e:hook("jingle/"..l,function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2267 if e.attr.from~=a.peer then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2268 return false;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2269 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2270 local e=e:get_child("jingle",o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2271 return a:handle_command(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2272 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2273 e:event("jingle",a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2274 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2275 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2276 function t:handle_command(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2277 local t=a.attr.action;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2278 e:debug("Handling Jingle command: %s",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2279 if t=="session-terminate"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2280 self:destroy();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2281 elseif t=="session-accept"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2282 self:handle_accepted(a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2283 elseif t=="transport-info"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2284 e:debug("Handling transport-info");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2285 self.transport:info_received(a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2286 elseif t=="transport-replace"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2287 e:error("Peer wanted to swap transport, not implemented");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2288 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2289 e:warn("Unhandled Jingle command: %s",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2290 return nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2291 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2292 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2293 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2294 function t:send_command(a,e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2295 local e=n.iq({to=self.peer,type="set"})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2296 :tag("jingle",{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2297 xmlns=o,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2298 sid=self.sid,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2299 action=a,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2300 initiator=self.role=="initiator"and self.stream.jid or nil,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2301 responder=self.role=="responder"and self.jid or nil,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2302 })
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2303 :tag("content",{creator=self.creator,name=self.name})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2304 :add_child(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2305 if not t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2306 self.stream:send(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2307 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2308 self.stream:send_iq(e,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2309 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2310 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2311 function t:accept(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2312 local t=n.iq({to=self.peer,type="set"})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2313 :tag("jingle",{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2314 xmlns=o,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2315 sid=self.sid,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2316 action="session-accept",
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2317 responder=e.jid,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2318 })
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2319 :tag("content",{creator=self.creator,name=self.name});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2320 local o=self.content:generate_accept(self.content_tag:child_with_name("description"),a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2321 t:add_child(o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2322 local a=self.transport:generate_accept(self.content_tag:child_with_name("transport"),a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2323 t:add_child(a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2324 local a=self;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2325 e:send_iq(t,function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2326 if t.attr.type=="error"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2327 local a,t,a=t:get_error();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2328 e:error("session-accept rejected: %s",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2329 return false;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2330 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2331 a.transport:connect(function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2332 e:warn("CONNECTED (receiver)!!!");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2333 a.state="active";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2334 a:event("connected",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2335 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2336 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2337 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2338 e:hook("iq/"..o,u);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2339 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2340 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2341 function t:offer(t,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2342 local e=n.iq({to=self.peer,type="set"})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2343 :tag("jingle",{xmlns=o,action="session-initiate",
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2344 initiator=self.stream.jid,sid=self.sid});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2345 e:tag("content",{creator=self.role,name=t});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2346 local t=self.stream:event("jingle/describe/"..t,a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2347 if not t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2348 return false,"Unknown content type";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2349 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2350 e:add_child(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2351 local t=self.stream:event("jingle/transport/".."urn:xmpp:jingle:transports:s5b:1",self);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2352 self.transport=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2353 e:add_child(t:generate_initiate());
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2354 self.stream:debug("Hooking %s","jingle/"..self.sid);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2355 self.stream:hook("jingle/"..self.sid,function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2356 if e.attr.from~=self.peer then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2357 return false;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2358 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2359 local e=e:get_child("jingle",o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2360 return self:handle_command(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2361 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2362 self.stream:send_iq(e,function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2363 if e.type=="error"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2364 self.state="terminated";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2365 local e,a,t=e:get_error();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2366 return self:event("error",{type=e,condition=a,text=t});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2367 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2368 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2369 self.state="pending";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2370 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2371 function t:terminate(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2372 local e=verse.stanza("reason"):tag(e or"success");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2373 self:send_command("session-terminate",e,function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2374 self.state="terminated";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2375 self.transport:disconnect();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2376 self:destroy();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2377 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2378 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2379 function t:destroy()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2380 self.stream:unhook("jingle/"..self.sid,self.handle_command);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2381 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2382 function t:handle_accepted(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2383 local e=e:child_with_name("transport");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2384 self.transport:handle_accepted(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2385 self.transport:connect(function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2386 print("CONNECTED (initiator)!")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2387 self.state="active";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2388 self:event("connected",e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2389 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2390 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2391 function t:set_source(a,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2392 local function t()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2393 local e,i=a();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2394 if e and e~=""then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2395 self.transport.conn:send(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2396 elseif e==""then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2397 return t();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2398 elseif e==nil then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2399 if o then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2400 self:terminate();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2401 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2402 self.transport.conn:unhook("drained",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2403 a=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2404 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2405 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2406 self.transport.conn:hook("drained",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2407 t();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2408 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2409 function t:set_sink(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2410 self.transport.conn:hook("incoming-raw",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2411 self.transport.conn:hook("disconnected",function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2412 self.stream:debug("Closing sink...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2413 local e=e.reason;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2414 if e=="closed"then e=nil;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2415 t(nil,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2416 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2417 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2418 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2419 package.preload['verse.plugins.jingle_ft']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2420 local o=require"ltn12";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2421 local s=package.config:sub(1,1);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2422 local a="urn:xmpp:jingle:apps:file-transfer:1";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2423 local i="http://jabber.org/protocol/si/profile/file-transfer";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2424 function verse.plugins.jingle_ft(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2425 t:hook("ready",function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2426 t:add_disco_feature(a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2427 end,10);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2428 local n={name="file"};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2429 function n:generate_accept(t,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2430 if e and e.save_file then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2431 self.jingle:hook("connected",function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2432 local e=o.sink.file(io.open(e.save_file,"w+"));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2433 self.jingle:set_sink(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2434 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2435 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2436 return t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2437 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2438 local n={__index=n};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2439 t:hook("jingle/content/"..a,function(t,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2440 local e=e:get_child("offer"):get_child("file",i);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2441 local e={
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2442 name=e.attr.name;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2443 size=tonumber(e.attr.size);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2444 };
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2445 return setmetatable({jingle=t,file=e},n);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2446 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2447 t:hook("jingle/describe/file",function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2448 local t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2449 if e.timestamp then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2450 t=os.date("!%Y-%m-%dT%H:%M:%SZ",e.timestamp);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2451 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2452 return verse.stanza("description",{xmlns=a})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2453 :tag("offer")
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2454 :tag("file",{xmlns=i,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2455 name=e.filename,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2456 size=e.size,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2457 date=t,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2458 hash=e.hash,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2459 })
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2460 :tag("desc"):text(e.description or"");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2461 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2462 function t:send_file(a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2463 local e,i=io.open(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2464 if not e then return e,i;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2465 local i=e:seek("end",0);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2466 e:seek("set",0);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2467 local o=o.source.file(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2468 local e=c:jingle(a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2469 e:offer("file",{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2470 filename=t:match("[^"..s.."]+$");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2471 size=i;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2472 });
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2473 e:hook("connected",function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2474 e:set_source(o,true);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2475 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2476 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2477 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2478 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2479 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2480 package.preload['verse.plugins.jingle_s5b']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2481 local a="urn:xmpp:jingle:transports:s5b:1";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2482 local s=require"util.sha1".sha1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2483 local r=require"util.uuid".generate;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2484 local function h(e,i)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2485 local function n()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2486 e:unhook("connected",n);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2487 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2488 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2489 local function o(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2490 e:unhook("incoming-raw",o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2491 if t:sub(1,2)~="\005\000"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2492 return e:event("error","connection-failure");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2493 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2494 e:event("connected");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2495 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2496 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2497 local function t(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2498 e:unhook("incoming-raw",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2499 if a~="\005\000"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2500 local t="version-mismatch";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2501 if a:sub(1,1)=="\005"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2502 t="authentication-failure";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2503 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2504 return e:event("error",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2505 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2506 e:send(string.char(5,1,0,3,#i)..i.."\0\0");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2507 e:hook("incoming-raw",o,100);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2508 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2509 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2510 e:hook("connected",n,200);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2511 e:hook("incoming-raw",t,100);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2512 e:send("\005\001\000");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2513 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2514 local function i(a,e,i)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2515 local e=verse.new(nil,{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2516 streamhosts=e,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2517 current_host=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2518 });
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2519 local function t(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2520 if o then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2521 return a(nil,o.reason);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2522 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2523 if e.current_host<#e.streamhosts then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2524 e.current_host=e.current_host+1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2525 e:debug("Attempting to connect to "..e.streamhosts[e.current_host].host..":"..e.streamhosts[e.current_host].port.."...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2526 local t,a=e:connect(
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2527 e.streamhosts[e.current_host].host,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2528 e.streamhosts[e.current_host].port
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2529 );
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2530 if not t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2531 e:debug("Error connecting to proxy (%s:%s): %s",
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2532 e.streamhosts[e.current_host].host,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2533 e.streamhosts[e.current_host].port,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2534 a
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2535 );
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2536 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2537 e:debug("Connecting...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2538 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2539 h(e,i);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2540 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2541 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2542 e:unhook("disconnected",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2543 return a(nil);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2544 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2545 e:hook("disconnected",t,100);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2546 e:hook("connected",function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2547 e:unhook("disconnected",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2548 a(e.streamhosts[e.current_host],e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2549 end,100);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2550 t();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2551 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2552 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2553 function verse.plugins.jingle_s5b(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2554 e:hook("ready",function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2555 e:add_disco_feature(a);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2556 end,10);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2557 local t={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2558 function t:generate_initiate()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2559 self.s5b_sid=r();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2560 local a=verse.stanza("transport",{xmlns=a,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2561 mode="tcp",sid=self.s5b_sid});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2562 local t=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2563 for o,i in pairs(e.proxy65.available_streamhosts)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2564 t=t+1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2565 a:tag("candidate",{jid=o,host=i.host,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2566 port=i.port,cid=o,priority=t,type="proxy"}):up();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2567 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2568 e:debug("Have %d proxies",t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2569 return a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2570 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2571 function t:generate_accept(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2572 local t={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2573 self.s5b_peer_candidates=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2574 self.s5b_mode=e.attr.mode or"tcp";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2575 self.s5b_sid=e.attr.sid or self.jingle.sid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2576 for e in e:childtags()do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2577 t[e.attr.cid]={
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2578 type=e.attr.type;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2579 jid=e.attr.jid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2580 host=e.attr.host;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2581 port=tonumber(e.attr.port)or 0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2582 priority=tonumber(e.attr.priority)or 0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2583 cid=e.attr.cid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2584 };
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2585 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2586 local e=verse.stanza("transport",{xmlns=a});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2587 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2588 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2589 function t:connect(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2590 e:warn("Connecting!");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2591 local t={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2592 for a,e in pairs(self.s5b_peer_candidates or{})do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2593 t[#t+1]=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2594 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2595 if#t>0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2596 self.connecting_peer_candidates=true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2597 local function n(t,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2598 self.jingle:send_command("transport-info",verse.stanza("transport",{xmlns=a,sid=self.s5b_sid})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2599 :tag("candidate-used",{cid=t.cid}));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2600 self.onconnect_callback=o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2601 self.conn=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2602 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2603 local e=s(self.s5b_sid..self.peer..e.jid,true);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2604 i(n,t,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2605 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2606 e:warn("Actually, I'm going to wait for my peer to tell me its streamhost...");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2607 self.onconnect_callback=o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2608 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2609 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2610 function t:info_received(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2611 e:warn("Info received");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2612 local o=t:child_with_name("content"):child_with_name("transport");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2613 if o:get_child("candidate-used")and not self.connecting_peer_candidates then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2614 local t=o:child_with_name("candidate-used");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2615 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2616 local function n(o,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2617 if self.jingle.role=="initiator"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2618 self.jingle.stream:send_iq(verse.iq({to=o.jid,type="set"})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2619 :tag("query",{xmlns=xmlns_bytestreams,sid=self.s5b_sid})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2620 :tag("activate"):text(self.jingle.peer),function(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2621 if o.attr.type=="result"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2622 self.jingle:send_command("transport-info",verse.stanza("transport",{xmlns=a,sid=self.s5b_sid})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2623 :tag("activated",{cid=t.attr.cid}));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2624 self.conn=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2625 self.onconnect_callback(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2626 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2627 self.jingle.stream:error("Failed to activate bytestream");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2628 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2629 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2630 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2631 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2632 self.jingle.stream:debug("CID: %s",self.jingle.stream.proxy65.available_streamhosts[t.attr.cid]);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2633 local t={
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2634 self.jingle.stream.proxy65.available_streamhosts[t.attr.cid];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2635 };
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2636 local e=s(self.s5b_sid..e.jid..self.peer,true);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2637 i(n,t,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2638 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2639 elseif o:get_child("activated")then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2640 self.onconnect_callback(self.conn);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2641 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2642 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2643 function t:disconnect()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2644 if self.conn then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2645 self.conn:close();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2646 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2647 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2648 function t:handle_accepted(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2649 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2650 local t={__index=t};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2651 e:hook("jingle/transport/"..a,function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2652 return setmetatable({
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2653 role=e.role,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2654 peer=e.peer,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2655 stream=e.stream,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2656 jingle=e,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2657 },t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2658 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2659 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2660 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2661 package.preload['verse.plugins.disco']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2662 local a=require"util.stanza"
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2663 local o=require("mime").b64
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2664 local i=require("util.sha1").sha1
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2665 local e="http://jabber.org/protocol/disco";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2666 local r=e.."#info";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2667 local s=e.."#items";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2668 function verse.plugins.disco(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2669 e.disco={cache={},info={}}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2670 e.disco.info.identities={
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2671 {category='client',type='pc',name='Verse'},
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2672 }
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2673 e.disco.info.features={
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2674 {var='http://jabber.org/protocol/caps'},
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2675 {var='http://jabber.org/protocol/disco#info'},
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2676 {var='http://jabber.org/protocol/disco#items'},
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2677 }
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2678 e.disco.items={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2679 e.disco.nodes={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2680 e.caps={}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2681 e.caps.node='http://code.matthewwild.co.uk/verse/'
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2682 local function n(t,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2683 if t.category<e.category then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2684 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2685 elseif e.category<t.category then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2686 return false;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2687 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2688 if t.type<e.type then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2689 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2690 elseif e.type<t.type then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2691 return false;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2692 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2693 if(not t['xml:lang']and e['xml:lang'])or
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2694 (e['xml:lang']and t['xml:lang']<e['xml:lang'])then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2695 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2696 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2697 return false
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2698 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2699 local function t(t,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2700 return t.var<e.var
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2701 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2702 local function h()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2703 table.sort(e.disco.info.identities,n)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2704 table.sort(e.disco.info.features,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2705 local t=''
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2706 for a,e in pairs(e.disco.info.identities)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2707 t=t..string.format(
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2708 '%s/%s/%s/%s',e.category,e.type,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2709 e['xml:lang']or'',e.name or''
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2710 )..'<'
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2711 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2712 for a,e in pairs(e.disco.info.features)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2713 t=t..e.var..'<'
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2714 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2715 return(o(i(t)))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2716 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2717 setmetatable(e.caps,{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2718 __call=function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2719 local t=h()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2720 return a.stanza('c',{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2721 xmlns='http://jabber.org/protocol/caps',
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2722 hash='sha-1',
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2723 node=e.caps.node,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2724 ver=t
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2725 })
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2726 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2727 })
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2728 function e:add_disco_feature(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2729 table.insert(self.disco.info.features,{var=e});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2730 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2731 function e:jid_has_identity(t,a,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2732 local o=self.disco.cache[t];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2733 if not o then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2734 return nil,"no-cache";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2735 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2736 local t=self.disco.cache[t].identities;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2737 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2738 return t[a.."/"..e]or false;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2739 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2740 for e in pairs(t)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2741 if e:match("^(.*)/")==a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2742 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2743 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2744 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2745 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2746 function e:jid_supports(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2747 local e=self.disco.cache[e];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2748 if not e or not e.features then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2749 return nil,"no-cache";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2750 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2751 return e.features[t]or false;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2752 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2753 function e:get_local_services(o,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2754 local e=self.disco.cache[self.host];
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2755 if not(e)or not(e.items)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2756 return nil,"no-cache";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2757 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2758 local t={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2759 for i,e in ipairs(e.items)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2760 if self:jid_has_identity(e.jid,o,a)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2761 table.insert(t,e.jid);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2762 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2763 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2764 return t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2765 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2766 function e:disco_local_services(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2767 self:disco_items(self.host,nil,function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2768 local e=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2769 local function o()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2770 e=e-1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2771 if e==0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2772 return a(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2773 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2774 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2775 for a,t in ipairs(t)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2776 if t.jid then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2777 e=e+1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2778 self:disco_info(t.jid,nil,o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2779 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2780 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2781 if e==0 then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2782 return a(t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2783 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2784 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2785 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2786 function e:disco_info(e,t,n)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2787 local a=verse.iq({to=e,type="get"})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2788 :tag("query",{xmlns=r,node=t});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2789 self:send_iq(a,function(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2790 if a.attr.type=="error"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2791 return n(nil,a:get_error());
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2792 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2793 local o,i={},{};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2794 for e in a:get_child("query",r):childtags()do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2795 if e.name=="identity"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2796 o[e.attr.category.."/"..e.attr.type]=e.attr.name or true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2797 elseif e.name=="feature"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2798 i[e.attr.var]=true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2799 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2800 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2801 if not self.disco.cache[e]then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2802 self.disco.cache[e]={nodes={}};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2803 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2804 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2805 if not self.disco.cache.nodes[t]then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2806 self.disco.cache.nodes[t]={nodes={}};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2807 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2808 self.disco.cache[e].nodes[t].identities=o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2809 self.disco.cache[e].nodes[t].features=i;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2810 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2811 self.disco.cache[e].identities=o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2812 self.disco.cache[e].features=i;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2813 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2814 return n(self.disco.cache[e]);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2815 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2816 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2817 function e:disco_items(a,t,i)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2818 local o=verse.iq({to=a,type="get"})
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2819 :tag("query",{xmlns=s,node=t});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2820 self:send_iq(o,function(o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2821 if o.attr.type=="error"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2822 return i(nil,o:get_error());
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2823 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2824 local e={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2825 for t in o:get_child("query",s):childtags()do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2826 if t.name=="item"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2827 table.insert(e,{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2828 name=t.attr.name;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2829 jid=t.attr.jid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2830 });
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2831 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2832 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2833 if not self.disco.cache[a]then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2834 self.disco.cache[a]={nodes={}};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2835 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2836 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2837 if not self.disco.cache.nodes[t]then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2838 self.disco.cache.nodes[t]={nodes={}};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2839 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2840 self.disco.cache.nodes[t].items=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2841 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2842 self.disco.cache[a].items=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2843 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2844 return i(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2845 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2846 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2847 e:hook("iq/http://jabber.org/protocol/disco#info",function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2848 if t.attr.type=='get'then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2849 local o=t:child_with_name('query')
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2850 if not o then return;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2851 local s
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2852 local i
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2853 if o.attr.node then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2854 local h=h()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2855 local n=e.disco.nodes[o.attr.node]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2856 if n and n.info then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2857 s=n.info.identities or{}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2858 i=n.info.identities or{}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2859 elseif o.attr.node==e.caps.node..'#'..h then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2860 s=e.disco.info.identities
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2861 i=e.disco.info.features
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2862 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2863 local t=a.stanza('iq',{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2864 to=t.attr.from,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2865 from=t.attr.to,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2866 id=t.attr.id,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2867 type='error'
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2868 })
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2869 t:tag('query',{xmlns='http://jabber.org/protocol/disco#info'}):reset()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2870 t:tag('error',{type='cancel'}):tag(
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2871 'item-not-found',{xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2872 )
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2873 e:send(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2874 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2875 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2876 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2877 s=e.disco.info.identities
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2878 i=e.disco.info.features
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2879 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2880 local o=a.stanza('query',{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2881 xmlns='http://jabber.org/protocol/disco#info',
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2882 node=o.attr.node
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2883 })
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2884 for t,e in pairs(s)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2885 o:tag('identity',e):reset()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2886 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2887 for a,t in pairs(i)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2888 o:tag('feature',t):reset()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2889 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2890 e:send(a.stanza('iq',{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2891 to=t.attr.from,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2892 from=t.attr.to,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2893 id=t.attr.id,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2894 type='result'
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2895 }):add_child(o))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2896 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2897 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2898 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2899 e:hook("iq/http://jabber.org/protocol/disco#items",function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2900 if t.attr.type=='get'then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2901 local o=t:child_with_name('query')
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2902 if not o then return;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2903 local i
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2904 if o.attr.node then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2905 local o=e.disco.nodes[o.attr.node]
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2906 if o then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2907 i=o.items or{}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2908 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2909 local t=a.stanza('iq',{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2910 to=t.attr.from,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2911 from=t.attr.to,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2912 id=t.attr.id,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2913 type='error'
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2914 })
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2915 t:tag('query',{xmlns='http://jabber.org/protocol/disco#items'}):reset()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2916 t:tag('error',{type='cancel'}):tag(
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2917 'item-not-found',{xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'}
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2918 )
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2919 e:send(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2920 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2921 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2922 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2923 i=e.disco.items
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2924 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2925 local o=a.stanza('query',{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2926 xmlns='http://jabber.org/protocol/disco#items',
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2927 node=o.attr.node
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2928 })
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2929 for a,t in pairs(i)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2930 o:tag('item',t):reset()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2931 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2932 e:send(a.stanza('iq',{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2933 to=t.attr.from,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2934 from=t.attr.to,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2935 id=t.attr.id,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2936 type='result'
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2937 }):add_child(o))
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2938 return true
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2939 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2940 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2941 e:hook("ready",function()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2942 e:disco_local_services(function(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2943 for a,t in ipairs(t)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2944 for a in pairs(e.disco.cache[t.jid].identities)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2945 local a,o=a:match("^(.*)/(.*)$");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2946 e:event("disco/service-discovered/"..a,{
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2947 type=o,jid=t.jid;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2948 });
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2949 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2950 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2951 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2952 end,5);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2953 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2954 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2955 package.preload['verse.client']=(function(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2956 local t=require"verse";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2957 local a=t.stream_mt;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2958 local h=require"util.jid".split;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2959 local s=require"lxp";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2960 local o=require"util.stanza";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2961 t.message,t.presence,t.iq,t.stanza,t.reply,t.error_reply=
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2962 o.message,o.presence,o.iq,o.stanza,o.reply,o.error_reply;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2963 local r=require"core.xmlhandlers";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2964 local n="http://etherx.jabber.org/streams";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2965 local i={
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2966 stream_ns=n,
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2967 stream_tag="stream",
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2968 default_ns="jabber:client"};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2969 function i.streamopened(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2970 e.stream_id=t.id;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2971 if not e:event("opened",t)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2972 e.notopen=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2973 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2974 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2975 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2976 function i.streamclosed(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2977 return e:event("closed");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2978 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2979 function i.handlestanza(t,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2980 if e.attr.xmlns==n then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2981 return t:event("stream-"..e.name,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2982 elseif e.attr.xmlns then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2983 return t:event("stream/"..e.attr.xmlns,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2984 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2985 return t:event("stanza",e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2986 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2987 function a:reset()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2988 local e=s.new(r(self,i),"\1");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2989 self.parser=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2990 self.notopen=true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2991 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2992 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2993 function a:connect_client(e,o)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2994 self.jid,self.password=e,o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2995 self.username,self.host,self.resource=h(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2996 self:add_plugin("tls");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2997 self:add_plugin("sasl");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2998 self:add_plugin("bind");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
2999 self:add_plugin("session");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3000 function self.data(t,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3001 local t,o=self.parser:parse(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3002 if t then return;end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3003 a:debug("debug","Received invalid XML (%s) %d bytes: %s",tostring(o),#e,e:sub(1,300):gsub("[\r\n]+"," "));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3004 a:close("xml-not-well-formed");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3005 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3006 self:hook("incoming-raw",function(e)return self.data(self.conn,e);end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3007 self.curr_id=0;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3008 self.tracked_iqs={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3009 self:hook("stanza",function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3010 local t,a=e.attr.id,e.attr.type;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3011 if t and e.name=="iq"and(a=="result"or a=="error")and self.tracked_iqs[t]then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3012 self.tracked_iqs[t](e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3013 self.tracked_iqs[t]=nil;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3014 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3015 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3016 end);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3017 self:hook("stanza",function(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3018 if e.attr.xmlns==nil or e.attr.xmlns=="jabber:client"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3019 if e.name=="iq"and(e.attr.type=="get"or e.attr.type=="set")then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3020 local a=e.tags[1]and e.tags[1].attr.xmlns;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3021 if a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3022 ret=self:event("iq/"..a,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3023 if not ret then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3024 ret=self:event("iq",e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3025 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3026 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3027 if ret==nil then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3028 self:send(t.error_reply(e,"cancel","service-unavailable"));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3029 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3030 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3031 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3032 ret=self:event(e.name,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3033 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3034 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3035 return ret;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3036 end,-1);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3037 local function e()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3038 self:event("ready");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3039 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3040 self:hook("session-success",e,-1)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3041 self:hook("bind-success",e,-1);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3042 local e=self.close;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3043 function self:close(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3044 if not self.notopen then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3045 self:send("</stream:stream>");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3046 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3047 return e(self);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3048 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3049 self:connect(self.connect_host or self.host,self.connect_port or 5222);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3050 self:reopen();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3051 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3052 function a:reopen()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3053 self:reset();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3054 self:send(o.stanza("stream:stream",{to=self.host,["xmlns:stream"]='http://etherx.jabber.org/streams',
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3055 xmlns="jabber:client",version="1.0"}):top_tag());
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3056 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3057 function a:send_iq(e,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3058 local t=self:new_id();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3059 self.tracked_iqs[t]=a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3060 e.attr.id=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3061 self:send(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3062 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3063 function a:new_id()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3064 self.curr_id=self.curr_id+1;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3065 return tostring(self.curr_id);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3066 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3067 end)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3068 pcall(require,"luarocks.require");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3069 pcall(require,"ssl");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3070 local a=require"net.server";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3071 local n=require"util.events";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3072 module("verse",package.seeall);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3073 local t=_M;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3074 local e={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3075 e.__index=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3076 stream_mt=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3077 t.plugins={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3078 function t.new(o,a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3079 local e=setmetatable(a or{},e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3080 e.id=tostring(e):match("%x*$");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3081 e:set_logger(o,true);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3082 e.events=n.new();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3083 return e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3084 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3085 t.add_task=require"util.timer".add_task;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3086 function t.loop()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3087 return a.loop();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3088 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3089 function t.quit()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3090 return a.setquitting(true);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3091 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3092 t.logger=logger.init;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3093 function t.set_logger(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3094 a.setlogger(e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3095 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3096 function e:connect(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3097 e=e or"localhost";
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3098 t=tonumber(t)or 5222;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3099 local i=socket.tcp()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3100 i:settimeout(0);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3101 local n,o=i:connect(e,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3102 if not n and o~="timeout"then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3103 self:warn("connect() to %s:%d failed: %s",e,t,o);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3104 return false,o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3105 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3106 local e=a.wrapclient(i,e,t,new_listener(self),"*a");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3107 if not e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3108 return false,o;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3109 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3110 self.conn=e;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3111 local t,a=e.write,tostring;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3112 self.send=function(i,o)return t(e,a(o));end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3113 return true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3114 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3115 function e:close()
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3116 local e=self.conn.disconnect();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3117 self.conn:close();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3118 e(conn,reason);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3119 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3120 function e:debug(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3121 if self.logger and self.log.debug then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3122 return self.logger("debug",...);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3123 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3124 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3125 function e:warn(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3126 if self.logger and self.log.warn then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3127 return self.logger("warn",...);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3128 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3129 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3130 function e:error(...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3131 if self.logger and self.log.error then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3132 return self.logger("error",...);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3133 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3134 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3135 function e:set_logger(t,e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3136 local a=self.logger;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3137 if t then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3138 self.logger=t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3139 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3140 if e then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3141 if e==true then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3142 e={"debug","info","warn","error"};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3143 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3144 self.log={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3145 for t,e in ipairs(e)do
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3146 self.log[e]=true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3147 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3148 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3149 return a;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3150 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3151 function stream_mt:set_log_levels(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3152 self:set_logger(nil,e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3153 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3154 function e:event(e,...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3155 self:debug("Firing event: "..tostring(e));
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3156 return self.events.fire_event(e,...);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3157 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3158 function e:hook(e,...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3159 return self.events.add_handler(e,...);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3160 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3161 function e:unhook(e,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3162 return self.events.remove_handler(e,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3163 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3164 function t.eventable(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3165 t.events=n.new();
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3166 t.hook,t.unhook=e.hook,e.unhook;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3167 local e=t.events.fire_event;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3168 function t:event(t,...)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3169 return e(t,...);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3170 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3171 return t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3172 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3173 function e:add_plugin(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3174 if require("verse.plugins."..e)then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3175 local a,t=t.plugins[e](self);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3176 if a then
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3177 self:debug("Loaded %s plugin",e);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3178 else
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3179 self:warn("Failed to load %s plugin: %s",e,t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3180 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3181 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3182 return self;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3183 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3184 function new_listener(e)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3185 local t={};
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3186 function t.onconnect(a)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3187 e.connected=true;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3188 e.send=function(t,e)t:debug("Sending data: "..tostring(e));return a:write(tostring(e));end;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3189 e:event("connected");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3190 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3191 function t.onincoming(a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3192 e:event("incoming-raw",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3193 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3194 function t.ondisconnect(a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3195 e.connected=false;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3196 e:event("disconnected",{reason=t});
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3197 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3198 function t.ondrain(t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3199 e:event("drained");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3200 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3201 function t.onstatus(a,t)
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3202 e:event("status",t);
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3203 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3204 return t;
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3205 end
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3206 local e=require"util.logger".init("verse");
0d4d8b432980 Initial commit.
Kooda <kooda@upyum.com>
parents:
diff changeset
3207 return t;