author | Myhailo Danylenko <isbear@ukrpost.net> |
Tue, 12 Jul 2011 07:28:06 +0300 | |
changeset 40 | 33ea13cef185 |
parent 36 | 41df6f2a7aa8 |
child 42 | 61d0ab29f17f |
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 |
|
56 |
elseif st == "function" then |
|
57 |
cb = a |
|
58 |
elseif st == "string" then |
|
59 |
fp = a |
|
60 |
elseif st ~= "nil" then |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
61 |
error "unexpected type of argument" |
0 | 62 |
end |
63 |
if fp then |
|
64 |
if cb then |
|
65 |
return lm.ssl.new ( fp, cb ) |
|
66 |
else |
|
67 |
return lm.ssl.new ( fp ) |
|
68 |
end |
|
69 |
else |
|
70 |
if cb then |
|
71 |
return lm.ssl.new ( cb ) |
|
72 |
else |
|
73 |
return lm.ssl.new () |
|
74 |
end |
|
75 |
end |
|
76 |
end |
|
77 |
||
78 |
-- basically, it just provides a way |
|
79 |
-- to initialize many parameters at once. |
|
80 |
-- keys in a table correspond to methods |
|
81 |
-- of connection object, except for handlers, |
|
82 |
-- where format is { |
|
83 |
-- "type/priority" = function/object, |
|
84 |
-- ... |
|
85 |
-- } |
|
86 |
-- two extra keys - server and context. |
|
87 |
-- ssl and proxy objects can either be objects |
|
88 |
-- or tables, directly passed to corresponding |
|
89 |
-- create routine. |
|
90 |
function lm.connection.create ( a ) |
|
91 |
local at = type (a) |
|
92 |
if at == "string" then |
|
93 |
return lm.connection.new ( a ) |
|
94 |
elseif at == "table" then |
|
95 |
local server = a.server |
|
96 |
if not server then |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
97 |
error "server name parameter required" |
0 | 98 |
end |
99 |
||
100 |
-- create connection object |
|
101 |
local c |
|
102 |
if a.context then |
|
103 |
c = lm.connection.new ( server, a.context ) |
|
104 |
else |
|
105 |
c = lm.connection.new ( server ) |
|
106 |
end |
|
107 |
||
108 |
-- connection parameters |
|
109 |
if a.port then |
|
110 |
c:port ( a.port ) |
|
111 |
end |
|
112 |
if a.jid then |
|
113 |
c:jid ( a.jid ) |
|
114 |
end |
|
115 |
if a.keep_alive_rate then |
|
116 |
c:keep_alive_rate ( a.keep_alive_rate ) |
|
117 |
end |
|
118 |
||
119 |
-- proxy |
|
120 |
if a.proxy then |
|
121 |
local pt = type ( a.proxy ) |
|
122 |
if pt == "userdata" then |
|
123 |
c:proxy ( a.proxy ) |
|
124 |
else |
|
125 |
local proxy = lm.proxy.create ( a.proxy ) |
|
126 |
c:proxy ( proxy ) |
|
127 |
end |
|
128 |
end |
|
129 |
||
130 |
-- ssl |
|
131 |
if a.ssl then |
|
132 |
local st = type ( a.ssl ) |
|
133 |
if st == "userdata" then |
|
134 |
c:ssl ( a.ssl ) |
|
135 |
else |
|
136 |
local ssl = lm.ssl.create ( a.ssl ) |
|
137 |
c:ssl ( ssl ) |
|
138 |
end |
|
139 |
end |
|
140 |
||
141 |
-- disconnect callback |
|
142 |
if a.ondisconnect then |
|
143 |
c:ondisconnect ( a.ondisconnect ) |
|
144 |
end |
|
145 |
||
146 |
-- message handlers |
|
147 |
if a.handlers then |
|
148 |
if type ( a.handlers ) ~= "table" then |
|
149 |
error ( "handlers parameter expected to be a table " .. |
|
150 |
"of the form { \"type/priority\" = function/object }" ) |
|
151 |
end |
|
152 |
for mhtype, handler in pairs ( a.handlers ) do |
|
153 |
local mtype, prio = mhtype:match ( "(.-)/(%d+)" ) |
|
154 |
if not mtype then |
|
155 |
mtype = mhtype |
|
156 |
prio = 0 |
|
157 |
else |
|
158 |
prio = tonumber ( prio ) |
|
159 |
end |
|
160 |
c:handler ( handler, mtype, prio ) |
|
161 |
end |
|
162 |
end |
|
163 |
||
164 |
return c |
|
165 |
else |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
166 |
error "at least server name parameter required" |
0 | 167 |
end |
168 |
end |
|
169 |
||
170 |
-- TODO: multiple nodes with same name |
|
171 |
function lm.message_node.parse ( node, r ) |
|
18
6effa1929af7
Children node method removed
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
172 |
local n = node:child () |
0 | 173 |
while n do |
174 |
local name = n:name () |
|
175 |
r[name] = { } |
|
176 |
local value = n:value () |
|
177 |
if value then |
|
178 |
r[name][1] = value |
|
179 |
end |
|
180 |
lm.message_node.parse ( n, r[name] ) |
|
181 |
n = n:next () |
|
182 |
end |
|
183 |
end |
|
184 |
||
185 |
-- There are NO WAY to get a list of node attributes, |
|
186 |
-- except brute force... |
|
187 |
function lm.message.parse ( message ) |
|
188 |
local mtype, subtype = message:type () |
|
189 |
if subtype then |
|
190 |
mtype = mtype .. '-' .. subtype |
|
191 |
end |
|
192 |
local r = { mtype = mtype } |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
193 |
local value = message:value () |
0 | 194 |
if value then |
195 |
r[1] = value |
|
196 |
end |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
197 |
lm.message_node.parse ( message, r ) |
0 | 198 |
return r |
199 |
end |
|
200 |
||
201 |
-- 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
|
202 |
-- ssl.validate - boolean |
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
203 |
-- 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
|
204 |
-- 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
|
205 |
-- onconnect - callback to call after connection will be established |
0 | 206 |
-- username |
207 |
-- password |
|
208 |
-- resource |
|
209 |
function lm.connect ( a ) |
|
210 |
if type ( a ) ~= "table" then |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
211 |
error "table expected as argument" |
0 | 212 |
end |
213 |
if a.ssl then |
|
214 |
if a.ssl.validate and not a.ssl.callback then |
|
215 |
a.ssl.callback = |
|
216 |
function ( obj, status ) |
|
217 |
return false |
|
218 |
end |
|
219 |
end |
|
220 |
end |
|
221 |
local c = lm.connection.create ( a ) |
|
36
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
222 |
if type ( a.preopen ) == "function" then |
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
223 |
a.preopen ( c ) -- XXX |
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
224 |
end |
0 | 225 |
c:open ( |
226 |
function ( obj, status ) |
|
227 |
if status then |
|
228 |
if type ( a.onopen ) == "function" then |
|
229 |
a.onopen ( obj ) |
|
230 |
end |
|
231 |
c:authenticate ( a.username, a.password, a.resource, |
|
232 |
function ( obj, status ) |
|
233 |
if type ( a.onconnect ) == "function" then |
|
234 |
a.onconnect ( obj ) |
|
235 |
end |
|
236 |
end ) |
|
237 |
end |
|
238 |
end ) |
|
239 |
return c |
|
240 |
end |
|
241 |
||
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
242 |
return lm |
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
243 |
|
36
41df6f2a7aa8
Add preconnect hook to lm.connect
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
244 |
-- vim: se ts=4 sw=4: -- |