68
|
1 |
|
|
2 |
-- PUBLISH-SUBSCRIBE (BEP-0060) |
|
3 |
|
|
4 |
-- library |
|
5 |
|
|
6 |
local iq = require 'lm.iq' |
|
7 |
local x_data = require 'lm.x_data' |
|
8 |
|
|
9 |
-- |
|
10 |
|
|
11 |
local O = { |
|
12 |
handlers = { }, |
|
13 |
} |
|
14 |
|
|
15 |
local F = { } |
|
16 |
|
|
17 |
function F.handler ( xmlns, handler ) |
|
18 |
O.handlers[xmlns] = handler |
|
19 |
end |
|
20 |
|
|
21 |
function F.message_handler ( conn, mess ) |
|
22 |
local e = mess:child ( 'event' ) |
|
23 |
if e and e:attribute ( 'xmlns' ) == 'http://jabber.org/protocol/pubsub#event' then |
|
24 |
local is = e:child ( 'items' ) |
|
25 |
if is then |
|
26 |
local from = mess:attribute ( 'from' ) |
|
27 |
local node = is:attribute ( 'node' ) |
|
28 |
local item = is:child () |
|
29 |
while item do |
|
30 |
local id = item:attribute ( 'id' ) |
|
31 |
local n = item:child () |
|
32 |
while n do |
|
33 |
local xmlns = n:attribute ( 'xmlns' ) |
|
34 |
if O.handlers[xmlns] then |
|
35 |
O.handlers[xmlns] ( from, node, n, id ) |
|
36 |
end |
|
37 |
n = n:next () |
|
38 |
end |
|
39 |
item = item:next () |
|
40 |
end |
|
41 |
return true |
|
42 |
end |
|
43 |
end |
|
44 |
return false |
|
45 |
end |
|
46 |
|
|
47 |
-- SUBSCRIBER USE CASES |
|
48 |
|
|
49 |
function F.subscribe ( conn, to, node, success, fail ) |
|
50 |
local jid = conn:jid():gsub ( '/.*', '' ) |
|
51 |
iq.send ( conn, to, 'set', |
|
52 |
{ |
|
53 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub', |
|
54 |
subscribe = { node = node, jid = jid }, |
|
55 |
}, |
|
56 |
}, |
|
57 |
function ( mess ) |
|
58 |
local s = mess:path ( 'pubsub', 'subscription' ) |
|
59 |
if s then |
|
60 |
success ( s:attribute ( 'subid' ) ) |
|
61 |
else |
|
62 |
success () |
|
63 |
end |
|
64 |
end, |
|
65 |
fail ) |
|
66 |
end |
|
67 |
|
|
68 |
function F.unsubscribe ( conn, to, node, success, fail ) |
|
69 |
local jid = conn:jid():gsub ( '/.*', '' ) |
|
70 |
iq.send ( conn, to, 'set', |
|
71 |
{ |
|
72 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub', |
|
73 |
unsubscribe = { node = node, jid = jid }, |
|
74 |
}, |
|
75 |
}, success, fail ) |
|
76 |
end |
|
77 |
|
|
78 |
-- I found no servers with subscription options support thus it is not implemented. |
|
79 |
|
|
80 |
-- untested :( |
|
81 |
function F.retrieve ( conn, to, node, success, fail, max, ids ) |
|
82 |
local items = { node = node, max_items = max } |
|
83 |
if ids then |
|
84 |
items.item = { } |
|
85 |
for k, id in pairs ( ids ) do |
|
86 |
table.insert ( items.item, { id = id } ) |
|
87 |
end |
|
88 |
end |
|
89 |
iq.send ( conn, to, 'get', |
|
90 |
{ |
|
91 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub', |
|
92 |
items = items, |
|
93 |
}, |
|
94 |
}, |
|
95 |
function ( mess ) |
|
96 |
local items = mess:path ( 'pubsub', 'items' ) |
|
97 |
if items then |
|
98 |
local from = mess:attribute ( 'from' ) |
|
99 |
local node = items:attribute ( 'node' ) |
|
100 |
local item = items:child () |
|
101 |
while item do |
|
102 |
success ( from, node, item ) -- XXX use registered xmlns handlers for that? |
|
103 |
item = item:next () |
|
104 |
end |
|
105 |
else |
|
106 |
-- XXX |
|
107 |
end |
|
108 |
end, fail ) |
|
109 |
end |
|
110 |
|
|
111 |
-- OWNER USE CASES |
|
112 |
|
|
113 |
-- node may be nil |
|
114 |
function F.create_node ( conn, to, node, success, fail ) |
|
115 |
iq.send ( conn, to, 'set', |
|
116 |
{ |
|
117 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub', |
|
118 |
create = { node = node }, |
|
119 |
}, |
|
120 |
}, |
|
121 |
function ( mess ) |
|
122 |
if node then |
|
123 |
success () |
|
124 |
else |
|
125 |
local create = mess:path ( 'pubsub', 'create' ) |
|
126 |
if create then |
|
127 |
success ( create:attribute ( 'node' ) ) |
|
128 |
else |
|
129 |
success () |
|
130 |
end |
|
131 |
end |
|
132 |
end, fail ) |
|
133 |
end |
|
134 |
|
|
135 |
function F.delete_node ( conn, to, node, success, fail ) |
|
136 |
iq.send ( conn, to, 'set', |
|
137 |
{ |
|
138 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
139 |
delete = { node = node }, |
|
140 |
}, |
|
141 |
}, success, fail ) |
|
142 |
end |
|
143 |
|
|
144 |
function F.purge_node ( conn, to, node, success, fail ) |
|
145 |
iq.send ( conn, to, 'set', |
|
146 |
{ |
|
147 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
148 |
purge = { node = node }, |
|
149 |
}, |
|
150 |
}, success, fail ) |
|
151 |
end |
|
152 |
|
|
153 |
function F.configure_node ( conn, to, node, success, fail ) |
|
154 |
iq.send ( conn, to, 'get', |
|
155 |
{ |
|
156 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
157 |
configure = { node = node }, |
|
158 |
}, |
|
159 |
}, |
|
160 |
function ( mess ) |
|
161 |
local x = mess:path ( 'pubsub', 'configure', 'x' ) |
|
162 |
if x then |
|
163 |
success ( x_data.parse ( x ), |
|
164 |
function ( form, success, fail ) |
|
165 |
iq.send ( conn, to, 'set', |
|
166 |
{ |
|
167 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
168 |
configure = form:format ( { node = node }, 'submit' ), |
|
169 |
}, |
|
170 |
}, success, fail ) |
|
171 |
end, |
|
172 |
function ( form, success, fail ) |
|
173 |
iq.send ( conn, to, 'set', |
|
174 |
{ |
|
175 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
176 |
configure = form:format ( { node = node }, 'cancel' ), |
|
177 |
}, |
|
178 |
}, success, fail ) |
|
179 |
end ) |
|
180 |
else |
|
181 |
fail ( mess:xml () ) -- XXX |
|
182 |
end |
|
183 |
end, fail ) |
|
184 |
end |
|
185 |
|
|
186 |
function F.list_subscriptions ( conn, to, node, success, fail ) |
|
187 |
iq.send ( conn, to, 'get', |
|
188 |
{ |
|
189 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
190 |
subscriptions = { node = node }, |
|
191 |
}, |
|
192 |
}, |
|
193 |
function ( mess ) |
|
194 |
local s = mess:path ( 'pubsub', 'subscriptions' ) |
|
195 |
if s then |
|
196 |
local sub = s:child () |
|
197 |
local ret = { } |
|
198 |
while sub do |
|
199 |
table.insert ( ret, { jid = sub:attribute ( 'jid' ), subscription = sub:attribute ( 'subscription' ), subid = sub:attribute ( 'subid' ) } ) |
|
200 |
sub = sub:next () |
|
201 |
end |
|
202 |
success ( ret ) |
|
203 |
else |
|
204 |
fail ( mess:xml () ) -- XXX |
|
205 |
end |
|
206 |
end, fail ) |
|
207 |
end |
|
208 |
|
|
209 |
function F.modify_subscription ( conn, to, node, jid, state, success, fail, id ) |
|
210 |
iq.send ( conn, to, 'set', |
|
211 |
{ |
|
212 |
pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
213 |
subscriptions = { node = node, |
|
214 |
subscription = { jid = jid, subscription = state, id = id }, |
|
215 |
}, |
|
216 |
}, |
|
217 |
}, success, fail ) |
|
218 |
end |
|
219 |
|
|
220 |
return F |
|
221 |
|
|
222 |
-- vim: se ts=4: -- |