author | Myhailo Danylenko <isbear@ukrpost.net> |
Mon, 18 Jul 2011 01:47:25 +0300 | |
changeset 42 | 61d0ab29f17f |
parent 36 | 41df6f2a7aa8 |
child 46 | d4484a8ed66b |
permissions | -rw-r--r-- |
23 | 1 |
--[[ Copyright 2009 Myhailo Danylenko |
2 |
||
3 |
This program is free software: you can redistribute it and/or modify |
|
4 |
it under the terms of the GNU General Public License as published by |
|
5 |
the Free Software Foundation, either version 2 of the License, or |
|
6 |
(at your option) any later version. |
|
7 |
||
8 |
This program is distributed in the hope that it will be useful, |
|
9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 |
GNU General Public License for more details. |
|
12 |
||
13 |
You should have received a copy of the GNU General Public License |
|
14 |
along with this program. If not, see <http://www.gnu.org/licenses/>. ]] |
|
0 | 15 |
|
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
16 |
local lm = require 'loudmouth' |
0 | 17 |
|
18 |
-- argument is a table with keys, |
|
19 |
-- corresponding to method names. |
|
20 |
function lm.proxy.create ( a ) |
|
21 |
if type ( a ) ~= "table" then |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
22 |
error "arguments should be in a table" |
0 | 23 |
end |
24 |
local p = lm.proxy.new () |
|
25 |
if a.server then |
|
26 |
p:server ( a.server ) |
|
27 |
end |
|
28 |
if a.port then |
|
29 |
p:port ( a.port ) |
|
30 |
end |
|
31 |
if a['type'] then |
|
32 |
p:type ( a['type'] ) |
|
33 |
end |
|
34 |
if a.username then |
|
35 |
p:username ( a.username ) |
|
36 |
end |
|
37 |
if a.password then |
|
38 |
p:password ( a.password ) |
|
39 |
end |
|
40 |
return p |
|
41 |
end |
|
42 |
||
43 |
-- argument is a table with two keys: |
|
44 |
-- callback and fingerprint |
|
45 |
function lm.ssl.create ( a ) |
|
46 |
if not lm.ssl.supported () then |
|
47 |
-- XXX |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
48 |
-- error "ssl is not supported by your loudmouth library" |
0 | 49 |
return nil |
50 |
end |
|
51 |
local fp, cb |
|
52 |
local st = type ( a ) |
|
53 |
if st == "table" then |
|
54 |
fp = a.fingerprint |
|
55 |
cb = a.callback |
|
42 | 56 |
ut = a.tls |
57 |
rt = a.require_tls |
|
0 | 58 |
elseif st == "function" then |
59 |
cb = a |
|
60 |
elseif st == "string" then |
|
61 |
fp = a |
|
62 |
elseif st ~= "nil" then |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
63 |
error "unexpected type of argument" |
0 | 64 |
end |
42 | 65 |
local ssl |
0 | 66 |
if fp then |
67 |
if cb then |
|
42 | 68 |
ssl = lm.ssl.new ( fp, cb ) |
0 | 69 |
else |
42 | 70 |
ssl = lm.ssl.new ( fp ) |
0 | 71 |
end |
72 |
else |
|
73 |
if cb then |
|
42 | 74 |
ssl = lm.ssl.new ( cb ) |
0 | 75 |
else |
42 | 76 |
ssl = lm.ssl.new () |
0 | 77 |
end |
78 |
end |
|
42 | 79 |
if ut ~= nil then |
80 |
ssl:tls ( ut, rt ) |
|
81 |
end |
|
82 |
return ssl |
|
0 | 83 |
end |
84 |
||
85 |
-- basically, it just provides a way |
|
86 |
-- to initialize many parameters at once. |
|
87 |
-- keys in a table correspond to methods |
|
88 |
-- of connection object, except for handlers, |
|
89 |
-- where format is { |
|
90 |
-- "type/priority" = function/object, |
|
91 |
-- ... |
|
92 |
-- } |
|
93 |
-- two extra keys - server and context. |
|
94 |
-- ssl and proxy objects can either be objects |
|
95 |
-- or tables, directly passed to corresponding |
|
96 |
-- create routine. |
|
97 |
function lm.connection.create ( a ) |
|
98 |
local at = type (a) |
|
99 |
if at == "string" then |
|
100 |
return lm.connection.new ( a ) |
|
101 |
elseif at == "table" then |
|
102 |
local server = a.server |
|
103 |
if not server then |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
104 |
error "server name parameter required" |
0 | 105 |
end |
106 |
||
107 |
-- create connection object |
|
108 |
local c |
|
109 |
if a.context then |
|
110 |
c = lm.connection.new ( server, a.context ) |
|
111 |
else |
|
112 |
c = lm.connection.new ( server ) |
|
113 |
end |
|
114 |
||
115 |
-- connection parameters |
|
116 |
if a.port then |
|
117 |
c:port ( a.port ) |
|
118 |
end |
|
119 |
if a.jid then |
|
120 |
c:jid ( a.jid ) |
|
121 |
end |
|
122 |
if a.keep_alive_rate then |
|
123 |
c:keep_alive_rate ( a.keep_alive_rate ) |
|
124 |
end |
|
125 |
||
126 |
-- proxy |
|
127 |
if a.proxy then |
|
128 |
local pt = type ( a.proxy ) |
|
129 |
if pt == "userdata" then |
|
130 |
c:proxy ( a.proxy ) |
|
131 |
else |
|
132 |
local proxy = lm.proxy.create ( a.proxy ) |
|
133 |
c:proxy ( proxy ) |
|
134 |
end |
|
135 |
end |
|
136 |
||
137 |
-- ssl |
|
138 |
if a.ssl then |
|
139 |
local st = type ( a.ssl ) |
|
140 |
if st == "userdata" then |
|
141 |
c:ssl ( a.ssl ) |
|
142 |
else |
|
143 |
local ssl = lm.ssl.create ( a.ssl ) |
|
144 |
c:ssl ( ssl ) |
|
145 |
end |
|
146 |
end |
|
147 |
||
148 |
-- disconnect callback |
|
149 |
if a.ondisconnect then |
|
150 |
c:ondisconnect ( a.ondisconnect ) |
|
151 |
end |
|
152 |
||
153 |
-- message handlers |
|
154 |
if a.handlers then |
|
155 |
if type ( a.handlers ) ~= "table" then |
|
156 |
error ( "handlers parameter expected to be a table " .. |
|
157 |
"of the form { \"type/priority\" = function/object }" ) |
|
158 |
end |
|
159 |
for mhtype, handler in pairs ( a.handlers ) do |
|
160 |
local mtype, prio = mhtype:match ( "(.-)/(%d+)" ) |
|
161 |
if not mtype then |
|
162 |
mtype = mhtype |
|
163 |
prio = 0 |
|
164 |
else |
|
165 |
prio = tonumber ( prio ) |
|
166 |
end |
|
167 |
c:handler ( handler, mtype, prio ) |
|
168 |
end |
|
169 |
end |
|
170 |
||
171 |
return c |
|
172 |
else |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
173 |
error "at least server name parameter required" |
0 | 174 |
end |
175 |
end |
|
176 |
||
177 |
-- TODO: multiple nodes with same name |
|
178 |
function lm.message_node.parse ( node, r ) |
|
18
6effa1929af7
Children node method removed
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
179 |
local n = node:child () |
0 | 180 |
while n do |
181 |
local name = n:name () |
|
182 |
r[name] = { } |
|
183 |
local value = n:value () |
|
184 |
if value then |
|
185 |
r[name][1] = value |
|
186 |
end |
|
187 |
lm.message_node.parse ( n, r[name] ) |
|
188 |
n = n:next () |
|
189 |
end |
|
190 |
end |
|
191 |
||
192 |
-- There are NO WAY to get a list of node attributes, |
|
193 |
-- except brute force... |
|
194 |
function lm.message.parse ( message ) |
|
195 |
local mtype, subtype = message:type () |
|
196 |
if subtype then |
|
197 |
mtype = mtype .. '-' .. subtype |
|
198 |
end |
|
199 |
local r = { mtype = mtype } |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
200 |
local value = message:value () |
0 | 201 |
if value then |
202 |
r[1] = value |
|
203 |
end |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
204 |
lm.message_node.parse ( message, r ) |
0 | 205 |
return r |
206 |
end |
|
207 |
||
208 |
-- the same table, as for lm.connection.create, but with few more fields: |
|
36
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
209 |
-- ssl.validate - boolean |
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
210 |
-- preopen - callback to call after connection creation but before opening (to install log handler, for example) |
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
211 |
-- onopen - callback to call after connection is established, but before authentication |
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
212 |
-- onconnect - callback to call after connection will be established |
0 | 213 |
-- username |
214 |
-- password |
|
215 |
-- resource |
|
216 |
function lm.connect ( a ) |
|
217 |
if type ( a ) ~= "table" then |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
218 |
error "table expected as argument" |
0 | 219 |
end |
220 |
if a.ssl then |
|
221 |
if a.ssl.validate and not a.ssl.callback then |
|
222 |
a.ssl.callback = |
|
223 |
function ( obj, status ) |
|
224 |
return false |
|
225 |
end |
|
226 |
end |
|
227 |
end |
|
228 |
local c = lm.connection.create ( a ) |
|
36
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
229 |
if type ( a.preopen ) == "function" then |
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
230 |
a.preopen ( c ) -- XXX |
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
231 |
end |
0 | 232 |
c:open ( |
233 |
function ( obj, status ) |
|
234 |
if status then |
|
235 |
if type ( a.onopen ) == "function" then |
|
236 |
a.onopen ( obj ) |
|
237 |
end |
|
238 |
c:authenticate ( a.username, a.password, a.resource, |
|
239 |
function ( obj, status ) |
|
240 |
if type ( a.onconnect ) == "function" then |
|
241 |
a.onconnect ( obj ) |
|
242 |
end |
|
243 |
end ) |
|
244 |
end |
|
245 |
end ) |
|
246 |
return c |
|
247 |
end |
|
248 |
||
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
249 |
return lm |
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
250 |
|
36
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
251 |
-- vim: se ts=4 sw=4: -- |