author | Myhailo Danylenko <isbear@ukrpost.net> |
Mon, 27 Apr 2009 13:15:23 +0300 | |
changeset 19 | d775d7289fe4 |
parent 18 | 6effa1929af7 |
child 23 | 13f03e604c8a |
permissions | -rw-r--r-- |
0 | 1 |
|
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
2 |
local lm = require 'loudmouth' |
0 | 3 |
|
4 |
-- argument is a table with keys, |
|
5 |
-- corresponding to method names. |
|
6 |
function lm.proxy.create ( a ) |
|
7 |
if type ( a ) ~= "table" then |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
8 |
error "arguments should be in a table" |
0 | 9 |
end |
10 |
local p = lm.proxy.new () |
|
11 |
if a.server then |
|
12 |
p:server ( a.server ) |
|
13 |
end |
|
14 |
if a.port then |
|
15 |
p:port ( a.port ) |
|
16 |
end |
|
17 |
if a['type'] then |
|
18 |
p:type ( a['type'] ) |
|
19 |
end |
|
20 |
if a.username then |
|
21 |
p:username ( a.username ) |
|
22 |
end |
|
23 |
if a.password then |
|
24 |
p:password ( a.password ) |
|
25 |
end |
|
26 |
return p |
|
27 |
end |
|
28 |
||
29 |
-- argument is a table with two keys: |
|
30 |
-- callback and fingerprint |
|
31 |
function lm.ssl.create ( a ) |
|
32 |
if not lm.ssl.supported () then |
|
33 |
-- XXX |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
34 |
-- error "ssl is not supported by your loudmouth library" |
0 | 35 |
return nil |
36 |
end |
|
37 |
local fp, cb |
|
38 |
local st = type ( a ) |
|
39 |
if st == "table" then |
|
40 |
fp = a.fingerprint |
|
41 |
cb = a.callback |
|
42 |
elseif st == "function" then |
|
43 |
cb = a |
|
44 |
elseif st == "string" then |
|
45 |
fp = a |
|
46 |
elseif st ~= "nil" then |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
47 |
error "unexpected type of argument" |
0 | 48 |
end |
49 |
if fp then |
|
50 |
if cb then |
|
51 |
return lm.ssl.new ( fp, cb ) |
|
52 |
else |
|
53 |
return lm.ssl.new ( fp ) |
|
54 |
end |
|
55 |
else |
|
56 |
if cb then |
|
57 |
return lm.ssl.new ( cb ) |
|
58 |
else |
|
59 |
return lm.ssl.new () |
|
60 |
end |
|
61 |
end |
|
62 |
end |
|
63 |
||
64 |
-- basically, it just provides a way |
|
65 |
-- to initialize many parameters at once. |
|
66 |
-- keys in a table correspond to methods |
|
67 |
-- of connection object, except for handlers, |
|
68 |
-- where format is { |
|
69 |
-- "type/priority" = function/object, |
|
70 |
-- ... |
|
71 |
-- } |
|
72 |
-- two extra keys - server and context. |
|
73 |
-- ssl and proxy objects can either be objects |
|
74 |
-- or tables, directly passed to corresponding |
|
75 |
-- create routine. |
|
76 |
function lm.connection.create ( a ) |
|
77 |
local at = type (a) |
|
78 |
if at == "string" then |
|
79 |
return lm.connection.new ( a ) |
|
80 |
elseif at == "table" then |
|
81 |
local server = a.server |
|
82 |
if not server then |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
83 |
error "server name parameter required" |
0 | 84 |
end |
85 |
||
86 |
-- create connection object |
|
87 |
local c |
|
88 |
if a.context then |
|
89 |
c = lm.connection.new ( server, a.context ) |
|
90 |
else |
|
91 |
c = lm.connection.new ( server ) |
|
92 |
end |
|
93 |
||
94 |
-- connection parameters |
|
95 |
if a.port then |
|
96 |
c:port ( a.port ) |
|
97 |
end |
|
98 |
if a.jid then |
|
99 |
c:jid ( a.jid ) |
|
100 |
end |
|
101 |
if a.keep_alive_rate then |
|
102 |
c:keep_alive_rate ( a.keep_alive_rate ) |
|
103 |
end |
|
104 |
||
105 |
-- proxy |
|
106 |
if a.proxy then |
|
107 |
local pt = type ( a.proxy ) |
|
108 |
if pt == "userdata" then |
|
109 |
c:proxy ( a.proxy ) |
|
110 |
else |
|
111 |
local proxy = lm.proxy.create ( a.proxy ) |
|
112 |
c:proxy ( proxy ) |
|
113 |
end |
|
114 |
end |
|
115 |
||
116 |
-- ssl |
|
117 |
if a.ssl then |
|
118 |
local st = type ( a.ssl ) |
|
119 |
if st == "userdata" then |
|
120 |
c:ssl ( a.ssl ) |
|
121 |
else |
|
122 |
local ssl = lm.ssl.create ( a.ssl ) |
|
123 |
c:ssl ( ssl ) |
|
124 |
end |
|
125 |
end |
|
126 |
||
127 |
-- disconnect callback |
|
128 |
if a.ondisconnect then |
|
129 |
c:ondisconnect ( a.ondisconnect ) |
|
130 |
end |
|
131 |
||
132 |
-- message handlers |
|
133 |
if a.handlers then |
|
134 |
if type ( a.handlers ) ~= "table" then |
|
135 |
error ( "handlers parameter expected to be a table " .. |
|
136 |
"of the form { \"type/priority\" = function/object }" ) |
|
137 |
end |
|
138 |
for mhtype, handler in pairs ( a.handlers ) do |
|
139 |
local mtype, prio = mhtype:match ( "(.-)/(%d+)" ) |
|
140 |
if not mtype then |
|
141 |
mtype = mhtype |
|
142 |
prio = 0 |
|
143 |
else |
|
144 |
prio = tonumber ( prio ) |
|
145 |
end |
|
146 |
c:handler ( handler, mtype, prio ) |
|
147 |
end |
|
148 |
end |
|
149 |
||
150 |
return c |
|
151 |
else |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
152 |
error "at least server name parameter required" |
0 | 153 |
end |
154 |
end |
|
155 |
||
156 |
-- TODO: multiple nodes with same name |
|
157 |
function lm.message_node.parse ( node, r ) |
|
18
6effa1929af7
Children node method removed
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
158 |
local n = node:child () |
0 | 159 |
while n do |
160 |
local name = n:name () |
|
161 |
r[name] = { } |
|
162 |
local value = n:value () |
|
163 |
if value then |
|
164 |
r[name][1] = value |
|
165 |
end |
|
166 |
lm.message_node.parse ( n, r[name] ) |
|
167 |
n = n:next () |
|
168 |
end |
|
169 |
end |
|
170 |
||
171 |
-- There are NO WAY to get a list of node attributes, |
|
172 |
-- except brute force... |
|
173 |
function lm.message.parse ( message ) |
|
174 |
local mtype, subtype = message:type () |
|
175 |
if subtype then |
|
176 |
mtype = mtype .. '-' .. subtype |
|
177 |
end |
|
178 |
local r = { mtype = mtype } |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
179 |
local value = message:value () |
0 | 180 |
if value then |
181 |
r[1] = value |
|
182 |
end |
|
7
5db1448eb857
Message have node methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
183 |
lm.message_node.parse ( message, r ) |
0 | 184 |
return r |
185 |
end |
|
186 |
||
187 |
-- the same table, as for lm.connection.create, but with few more fields: |
|
188 |
-- ssl.validate |
|
189 |
-- onconnect |
|
190 |
-- onopen |
|
191 |
-- username |
|
192 |
-- password |
|
193 |
-- resource |
|
194 |
function lm.connect ( a ) |
|
195 |
if type ( a ) ~= "table" then |
|
6
90073cbb535d
Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
196 |
error "table expected as argument" |
0 | 197 |
end |
198 |
if a.ssl then |
|
199 |
if a.ssl.validate and not a.ssl.callback then |
|
200 |
a.ssl.callback = |
|
201 |
function ( obj, status ) |
|
202 |
return false |
|
203 |
end |
|
204 |
end |
|
205 |
end |
|
206 |
local c = lm.connection.create ( a ) |
|
207 |
c:open ( |
|
208 |
function ( obj, status ) |
|
209 |
if status then |
|
210 |
if type ( a.onopen ) == "function" then |
|
211 |
a.onopen ( obj ) |
|
212 |
end |
|
213 |
c:authenticate ( a.username, a.password, a.resource, |
|
214 |
function ( obj, status ) |
|
215 |
if type ( a.onconnect ) == "function" then |
|
216 |
a.onconnect ( obj ) |
|
217 |
end |
|
218 |
end ) |
|
219 |
end |
|
220 |
end ) |
|
221 |
return c |
|
222 |
end |
|
223 |
||
16
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
224 |
return lm |
09b375e9ce32
Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
12
diff
changeset
|
225 |
|
0 | 226 |
-- vim: se ts=4: -- |