Comparison

plugins/mod_pubsub/pubsub.lib.lua @ 6841:be87ab2d611c

plugins: Explicitly return to halt event propagation (session.send sometimes does not return true)
author Kim Alvefur <zash@zash.se>
date Mon, 21 Sep 2015 23:06:22 +0200
parent 6473:7c8f58d2b331
child 7358:d0390bc9c5d1
comparison
equal deleted inserted replaced
6840:eeefe8d42b8b 6841:be87ab2d611c
48 local node = items.attr.node; 48 local node = items.attr.node;
49 local item = items:get_child("item"); 49 local item = items:get_child("item");
50 local id = item and item.attr.id; 50 local id = item and item.attr.id;
51 51
52 if not node then 52 if not node then
53 return origin.send(pubsub_error_reply(stanza, "nodeid-required")); 53 origin.send(pubsub_error_reply(stanza, "nodeid-required"));
54 return true;
54 end 55 end
55 local ok, results = service:get_items(node, stanza.attr.from, id); 56 local ok, results = service:get_items(node, stanza.attr.from, id);
56 if not ok then 57 if not ok then
57 return origin.send(pubsub_error_reply(stanza, results)); 58 origin.send(pubsub_error_reply(stanza, results));
59 return true;
58 end 60 end
59 61
60 local data = st.stanza("items", { node = node }); 62 local data = st.stanza("items", { node = node });
61 for _, id in ipairs(results) do 63 for _, id in ipairs(results) do
62 data:add_child(results[id]); 64 data:add_child(results[id]);
67 :tag("pubsub", { xmlns = xmlns_pubsub }) 69 :tag("pubsub", { xmlns = xmlns_pubsub })
68 :add_child(data); 70 :add_child(data);
69 else 71 else
70 reply = pubsub_error_reply(stanza, "item-not-found"); 72 reply = pubsub_error_reply(stanza, "item-not-found");
71 end 73 end
72 return origin.send(reply); 74 origin.send(reply);
75 return true;
73 end 76 end
74 77
75 function handlers.get_subscriptions(origin, stanza, subscriptions, service) 78 function handlers.get_subscriptions(origin, stanza, subscriptions, service)
76 local node = subscriptions.attr.node; 79 local node = subscriptions.attr.node;
77 local ok, ret = service:get_subscriptions(node, stanza.attr.from, stanza.attr.from); 80 local ok, ret = service:get_subscriptions(node, stanza.attr.from, stanza.attr.from);
78 if not ok then 81 if not ok then
79 return origin.send(pubsub_error_reply(stanza, ret)); 82 origin.send(pubsub_error_reply(stanza, ret));
83 return true;
80 end 84 end
81 local reply = st.reply(stanza) 85 local reply = st.reply(stanza)
82 :tag("pubsub", { xmlns = xmlns_pubsub }) 86 :tag("pubsub", { xmlns = xmlns_pubsub })
83 :tag("subscriptions"); 87 :tag("subscriptions");
84 for _, sub in ipairs(ret) do 88 for _, sub in ipairs(ret) do
85 reply:tag("subscription", { node = sub.node, jid = sub.jid, subscription = 'subscribed' }):up(); 89 reply:tag("subscription", { node = sub.node, jid = sub.jid, subscription = 'subscribed' }):up();
86 end 90 end
87 return origin.send(reply); 91 origin.send(reply);
92 return true;
88 end 93 end
89 94
90 function handlers.set_create(origin, stanza, create, service) 95 function handlers.set_create(origin, stanza, create, service)
91 local node = create.attr.node; 96 local node = create.attr.node;
92 local ok, ret, reply; 97 local ok, ret, reply;
108 :tag("create", { node = node }); 113 :tag("create", { node = node });
109 else 114 else
110 reply = pubsub_error_reply(stanza, ret); 115 reply = pubsub_error_reply(stanza, ret);
111 end 116 end
112 end 117 end
113 return origin.send(reply); 118 origin.send(reply);
119 return true;
114 end 120 end
115 121
116 function handlers.set_delete(origin, stanza, delete, service) 122 function handlers.set_delete(origin, stanza, delete, service)
117 local node = delete.attr.node; 123 local node = delete.attr.node;
118 124
119 local reply, notifier; 125 local reply, notifier;
120 if not node then 126 if not node then
121 return origin.send(pubsub_error_reply(stanza, "nodeid-required")); 127 origin.send(pubsub_error_reply(stanza, "nodeid-required"));
128 return true;
122 end 129 end
123 local ok, ret = service:delete(node, stanza.attr.from); 130 local ok, ret = service:delete(node, stanza.attr.from);
124 if ok then 131 if ok then
125 reply = st.reply(stanza); 132 reply = st.reply(stanza);
126 else 133 else
127 reply = pubsub_error_reply(stanza, ret); 134 reply = pubsub_error_reply(stanza, ret);
128 end 135 end
129 return origin.send(reply); 136 origin.send(reply);
137 return true;
130 end 138 end
131 139
132 function handlers.set_subscribe(origin, stanza, subscribe, service) 140 function handlers.set_subscribe(origin, stanza, subscribe, service)
133 local node, jid = subscribe.attr.node, subscribe.attr.jid; 141 local node, jid = subscribe.attr.node, subscribe.attr.jid;
134 if not (node and jid) then 142 if not (node and jid) then
135 return origin.send(pubsub_error_reply(stanza, jid and "nodeid-required" or "invalid-jid")); 143 origin.send(pubsub_error_reply(stanza, jid and "nodeid-required" or "invalid-jid"));
144 return true;
136 end 145 end
137 --[[ 146 --[[
138 local options_tag, options = stanza.tags[1]:get_child("options"), nil; 147 local options_tag, options = stanza.tags[1]:get_child("options"), nil;
139 if options_tag then 148 if options_tag then
140 options = options_form:data(options_tag.tags[1]); 149 options = options_form:data(options_tag.tags[1]);
161 end 170 end
162 171
163 function handlers.set_unsubscribe(origin, stanza, unsubscribe, service) 172 function handlers.set_unsubscribe(origin, stanza, unsubscribe, service)
164 local node, jid = unsubscribe.attr.node, unsubscribe.attr.jid; 173 local node, jid = unsubscribe.attr.node, unsubscribe.attr.jid;
165 if not (node and jid) then 174 if not (node and jid) then
166 return origin.send(pubsub_error_reply(stanza, jid and "nodeid-required" or "invalid-jid")); 175 origin.send(pubsub_error_reply(stanza, jid and "nodeid-required" or "invalid-jid"));
176 return true;
167 end 177 end
168 local ok, ret = service:remove_subscription(node, stanza.attr.from, jid); 178 local ok, ret = service:remove_subscription(node, stanza.attr.from, jid);
169 local reply; 179 local reply;
170 if ok then 180 if ok then
171 reply = st.reply(stanza); 181 reply = st.reply(stanza);
172 else 182 else
173 reply = pubsub_error_reply(stanza, ret); 183 reply = pubsub_error_reply(stanza, ret);
174 end 184 end
175 return origin.send(reply); 185 origin.send(reply);
186 return true;
176 end 187 end
177 188
178 function handlers.set_publish(origin, stanza, publish, service) 189 function handlers.set_publish(origin, stanza, publish, service)
179 local node = publish.attr.node; 190 local node = publish.attr.node;
180 if not node then 191 if not node then
181 return origin.send(pubsub_error_reply(stanza, "nodeid-required")); 192 origin.send(pubsub_error_reply(stanza, "nodeid-required"));
193 return true;
182 end 194 end
183 local item = publish:get_child("item"); 195 local item = publish:get_child("item");
184 local id = (item and item.attr.id); 196 local id = (item and item.attr.id);
185 if not id then 197 if not id then
186 id = uuid_generate(); 198 id = uuid_generate();
196 :tag("publish", { node = node }) 208 :tag("publish", { node = node })
197 :tag("item", { id = id }); 209 :tag("item", { id = id });
198 else 210 else
199 reply = pubsub_error_reply(stanza, ret); 211 reply = pubsub_error_reply(stanza, ret);
200 end 212 end
201 return origin.send(reply); 213 origin.send(reply);
214 return true;
202 end 215 end
203 216
204 function handlers.set_retract(origin, stanza, retract, service) 217 function handlers.set_retract(origin, stanza, retract, service)
205 local node, notify = retract.attr.node, retract.attr.notify; 218 local node, notify = retract.attr.node, retract.attr.notify;
206 notify = (notify == "1") or (notify == "true"); 219 notify = (notify == "1") or (notify == "true");
207 local item = retract:get_child("item"); 220 local item = retract:get_child("item");
208 local id = item and item.attr.id 221 local id = item and item.attr.id
209 if not (node and id) then 222 if not (node and id) then
210 return origin.send(pubsub_error_reply(stanza, node and "item-not-found" or "nodeid-required")); 223 origin.send(pubsub_error_reply(stanza, node and "item-not-found" or "nodeid-required"));
224 return true;
211 end 225 end
212 local reply, notifier; 226 local reply, notifier;
213 if notify then 227 if notify then
214 notifier = st.stanza("retract", { id = id }); 228 notifier = st.stanza("retract", { id = id });
215 end 229 end
217 if ok then 231 if ok then
218 reply = st.reply(stanza); 232 reply = st.reply(stanza);
219 else 233 else
220 reply = pubsub_error_reply(stanza, ret); 234 reply = pubsub_error_reply(stanza, ret);
221 end 235 end
222 return origin.send(reply); 236 origin.send(reply);
237 return true;
223 end 238 end
224 239
225 function handlers.set_purge(origin, stanza, purge, service) 240 function handlers.set_purge(origin, stanza, purge, service)
226 local node, notify = purge.attr.node, purge.attr.notify; 241 local node, notify = purge.attr.node, purge.attr.notify;
227 notify = (notify == "1") or (notify == "true"); 242 notify = (notify == "1") or (notify == "true");
228 local reply; 243 local reply;
229 if not node then 244 if not node then
230 return origin.send(pubsub_error_reply(stanza, "nodeid-required")); 245 origin.send(pubsub_error_reply(stanza, "nodeid-required"));
246 return true;
231 end 247 end
232 local ok, ret = service:purge(node, stanza.attr.from, notify); 248 local ok, ret = service:purge(node, stanza.attr.from, notify);
233 if ok then 249 if ok then
234 reply = st.reply(stanza); 250 reply = st.reply(stanza);
235 else 251 else
236 reply = pubsub_error_reply(stanza, ret); 252 reply = pubsub_error_reply(stanza, ret);
237 end 253 end
238 return origin.send(reply); 254 origin.send(reply);
255 return true;
239 end 256 end
240 257
241 function handlers.get_configure(origin, stanza, config, service) 258 function handlers.get_configure(origin, stanza, config, service)
242 local node = config.attr.node; 259 local node = config.attr.node;
243 if not node then 260 if not node then
244 return origin.send(pubsub_error_reply(stanza, "nodeid-required")); 261 origin.send(pubsub_error_reply(stanza, "nodeid-required"));
262 return true;
245 end 263 end
246 264
247 if not service:may(node, stanza.attr.from, "configure") then 265 if not service:may(node, stanza.attr.from, "configure") then
248 return origin.send(pubsub_error_reply(stanza, "forbidden")); 266 origin.send(pubsub_error_reply(stanza, "forbidden"));
267 return true;
249 end 268 end
250 269
251 local node_obj = service.nodes[node]; 270 local node_obj = service.nodes[node];
252 if not node_obj then 271 if not node_obj then
253 return origin.send(pubsub_error_reply(stanza, "item-not-found")); 272 origin.send(pubsub_error_reply(stanza, "item-not-found"));
273 return true;
254 end 274 end
255 275
256 local reply = st.reply(stanza) 276 local reply = st.reply(stanza)
257 :tag("pubsub", { xmlns = xmlns_pubsub_owner }) 277 :tag("pubsub", { xmlns = xmlns_pubsub_owner })
258 :tag("configure", { node = node }) 278 :tag("configure", { node = node })
259 :add_child(node_config_form:form(node_obj.config)); 279 :add_child(node_config_form:form(node_obj.config));
260 return origin.send(reply); 280 origin.send(reply);
281 return true;
261 end 282 end
262 283
263 function handlers.set_configure(origin, stanza, config, service) 284 function handlers.set_configure(origin, stanza, config, service)
264 local node = config.attr.node; 285 local node = config.attr.node;
265 if not node then 286 if not node then
266 return origin.send(pubsub_error_reply(stanza, "nodeid-required")); 287 origin.send(pubsub_error_reply(stanza, "nodeid-required"));
288 return true;
267 end 289 end
268 if not service:may(node, stanza.attr.from, "configure") then 290 if not service:may(node, stanza.attr.from, "configure") then
269 return origin.send(pubsub_error_reply(stanza, "forbidden")); 291 origin.send(pubsub_error_reply(stanza, "forbidden"));
292 return true;
270 end 293 end
271 local new_config, err = node_config_form:data(config.tags[1]); 294 local new_config, err = node_config_form:data(config.tags[1]);
272 if not new_config then 295 if not new_config then
273 return origin.send(st.error_reply(stanza, "modify", "bad-request", err)); 296 origin.send(st.error_reply(stanza, "modify", "bad-request", err));
297 return true;
274 end 298 end
275 local ok, err = service:set_node_config(node, stanza.attr.from, new_config); 299 local ok, err = service:set_node_config(node, stanza.attr.from, new_config);
276 if not ok then 300 if not ok then
277 return origin.send(pubsub_error_reply(stanza, err)); 301 origin.send(pubsub_error_reply(stanza, err));
278 end 302 return true;
279 return origin.send(st.reply(stanza)); 303 end
304 origin.send(st.reply(stanza));
305 return true;
280 end 306 end
281 307
282 function handlers.get_default(origin, stanza, default, service) 308 function handlers.get_default(origin, stanza, default, service)
283 local reply = st.reply(stanza) 309 local reply = st.reply(stanza)
284 :tag("pubsub", { xmlns = xmlns_pubsub_owner }) 310 :tag("pubsub", { xmlns = xmlns_pubsub_owner })
285 :tag("default") 311 :tag("default")
286 :add_child(node_config_form:form(service.node_defaults)); 312 :add_child(node_config_form:form(service.node_defaults));
287 return origin.send(reply); 313 origin.send(reply);
314 return true;
288 end 315 end
289 316
290 return _M; 317 return _M;