comparison mod_cloud_notify/mod_cloud_notify.lua @ 2669:e6d243ed88ca

mod_cloud_notify: Fix module:unhook calls not available in prosody 0.9, fixes #874
author tmolitor <thilo@eightysoft.de>
date Thu, 06 Apr 2017 01:41:36 +0200
parents 777d07e0cd73
children 6e01878103c0
comparison
equal deleted inserted replaced
2668:f8fc79b3051a 2669:e6d243ed88ca
64 local node = jid.split(stanza.attr.to); 64 local node = jid.split(stanza.attr.to);
65 local from = stanza.attr.from; 65 local from = stanza.attr.from;
66 local user_push_services = push_store:get(node); 66 local user_push_services = push_store:get(node);
67 67
68 for push_identifier, _ in pairs(user_push_services) do 68 for push_identifier, _ in pairs(user_push_services) do
69 if hashes.sha256(push_identifier, true) == stanza.attr.id then 69 local stanza_id = hashes.sha256(push_identifier, true);
70 if stanza_id == stanza.attr.id then
70 if user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type ~= "wait" then 71 if user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type ~= "wait" then
71 push_errors[push_identifier] = push_errors[push_identifier] + 1; 72 push_errors[push_identifier] = push_errors[push_identifier] + 1;
72 module:log("info", "Got error of type '%s' (%s) for identifier '%s': " 73 module:log("info", "Got error of type '%s' (%s) for identifier '%s': "
73 .."error count for this identifier is now at %s", error_type, condition, push_identifier, 74 .."error count for this identifier is now at %s", error_type, condition, push_identifier,
74 tostring(push_errors[push_identifier])); 75 tostring(push_errors[push_identifier]));
82 end 83 end
83 end 84 end
84 -- save changed global config 85 -- save changed global config
85 push_store:set_identifier(node, push_identifier, nil); 86 push_store:set_identifier(node, push_identifier, nil);
86 push_errors[push_identifier] = nil; 87 push_errors[push_identifier] = nil;
87 -- unhook iq handlers for this identifier 88 -- unhook iq handlers for this identifier (if possible)
88 module:unhook("iq-error/bare/"..hashes.sha256(push_identifier, true), handle_push_error); 89 if module.unhook then
89 module:unhook("iq-result/bare/"..hashes.sha256(push_identifier, true), handle_push_success); 90 module:unhook("iq-error/bare/"..stanza_id, handle_push_error);
91 module:unhook("iq-result/bare/"..stanza_id, handle_push_success);
92 end
90 end 93 end
91 elseif user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type == "wait" then 94 elseif user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type == "wait" then
92 module:log("debug", "Got error of type '%s' (%s) for identifier '%s': " 95 module:log("debug", "Got error of type '%s' (%s) for identifier '%s': "
93 .."NOT increasing error count for this identifier", error_type, condition, push_identifier); 96 .."NOT increasing error count for this identifier", error_type, condition, push_identifier);
94 end 97 end
176 origin.push_identifier = nil; 179 origin.push_identifier = nil;
177 origin.push_settings = nil; 180 origin.push_settings = nil;
178 end 181 end
179 user_push_services[key] = nil; 182 user_push_services[key] = nil;
180 push_errors[key] = nil; 183 push_errors[key] = nil;
181 module:unhook("iq-error/bare/"..key, handle_push_error); 184 if module.unhook then
182 module:unhook("iq-result/bare/"..key, handle_push_success); 185 module:unhook("iq-error/bare/"..key, handle_push_error);
186 module:unhook("iq-result/bare/"..key, handle_push_success);
187 end
183 end 188 end
184 end 189 end
185 local ok = push_store:set(origin.username, user_push_services); 190 local ok = push_store:set(origin.username, user_push_services);
186 if not ok then 191 if not ok then
187 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); 192 origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
216 221
217 -- increment count and save it 222 -- increment count and save it
218 push_info.count = push_info.count + 1; 223 push_info.count = push_info.count + 1;
219 push_store:set_identifier(node, push_identifier, push_info); 224 push_store:set_identifier(node, push_identifier, push_info);
220 -- construct push stanza 225 -- construct push stanza
221 local push_publish = st.iq({ to = push_info.jid, from = node .. "@" .. module.host, type = "set", id = hashes.sha256(push_identifier, true) }) 226 local stanza_id = hashes.sha256(push_identifier, true);
227 local push_publish = st.iq({ to = push_info.jid, from = node .. "@" .. module.host, type = "set", id = stanza_id })
222 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" }) 228 :tag("pubsub", { xmlns = "http://jabber.org/protocol/pubsub" })
223 :tag("publish", { node = push_info.node }) 229 :tag("publish", { node = push_info.node })
224 :tag("item") 230 :tag("item")
225 :tag("notification", { xmlns = xmlns_push }); 231 :tag("notification", { xmlns = xmlns_push });
226 local form_data = { 232 local form_data = {
242 -- send out push 248 -- send out push
243 module:log("debug", "Sending push notification for %s@%s to %s (%s)", node, module.host, push_info.jid, tostring(push_info.node)); 249 module:log("debug", "Sending push notification for %s@%s to %s (%s)", node, module.host, push_info.jid, tostring(push_info.node));
244 -- handle push errors for this node 250 -- handle push errors for this node
245 if push_errors[push_identifier] == nil then 251 if push_errors[push_identifier] == nil then
246 push_errors[push_identifier] = 0; 252 push_errors[push_identifier] = 0;
247 module:hook("iq-error/bare/"..hashes.sha256(push_identifier, true), handle_push_error); 253 module:hook("iq-error/bare/"..stanza_id, handle_push_error);
248 module:hook("iq-result/bare/"..hashes.sha256(push_identifier, true), handle_push_success); 254 module:hook("iq-result/bare/"..stanza_id, handle_push_success);
249 end 255 end
250 module:send(push_publish); 256 module:send(push_publish);
251 end 257 end
252 end 258 end
253 259
377 -- push_store:set(origin.username, user_push_services); 383 -- push_store:set(origin.username, user_push_services);
378 -- end, 1); 384 -- end, 1);
379 385
380 module:log("info", "Module loaded"); 386 module:log("info", "Module loaded");
381 function module.unload() 387 function module.unload()
382 module:unhook("account-disco-info", account_dico_info); 388 if module.unhook then
383 module:unhook("iq-set/self/"..xmlns_push..":enable", push_enable); 389 module:unhook("account-disco-info", account_dico_info);
384 module:unhook("iq-set/self/"..xmlns_push..":disable", push_disable); 390 module:unhook("iq-set/self/"..xmlns_push..":enable", push_enable);
385 391 module:unhook("iq-set/self/"..xmlns_push..":disable", push_disable);
386 module:unhook("smacks-hibernation-start", hibernate_session); 392
387 module:unhook("smacks-hibernation-end", restore_session); 393 module:unhook("smacks-hibernation-start", hibernate_session);
388 module:unhook("smacks-ack-delayed", ack_delayed); 394 module:unhook("smacks-hibernation-end", restore_session);
389 module:unhook("archive-message-added", archive_message_added); 395 module:unhook("smacks-ack-delayed", ack_delayed);
390 module:unhook("cloud-notify-ping", send_ping); 396 module:unhook("archive-message-added", archive_message_added);
391 397 module:unhook("cloud-notify-ping", send_ping);
392 for push_identifier, _ in pairs(push_errors) do 398
393 module:hook("iq-error/bare/"..hashes.sha256(push_identifier, true), handle_push_error); 399 for push_identifier, _ in pairs(push_errors) do
394 module:hook("iq-result/bare/"..hashes.sha256(push_identifier, true), handle_push_success); 400 local stanza_id = hashes.sha256(push_identifier, true);
401 module:unhook("iq-error/bare/"..stanza_id, handle_push_error);
402 module:unhook("iq-result/bare/"..stanza_id, handle_push_success);
403 end
395 end 404 end
396 405
397 module:log("info", "Module unloaded"); 406 module:log("info", "Module unloaded");
398 end 407 end