Mercurial > prosody-modules
comparison mod_smacks/mod_smacks.lua @ 201:bc24f58a0d39
mod_smacks: Use filters for catching incoming stanzas (more reliable and efficient), also add some logic to make compatible with the stream resumption module (coming soon)
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 08 Jul 2010 14:01:32 +0100 |
| parents | 64a573203c20 |
| children | d11478ae374e |
comparison
equal
deleted
inserted
replaced
| 200:64a573203c20 | 201:bc24f58a0d39 |
|---|---|
| 1 local st = require "util.stanza"; | 1 local st = require "util.stanza"; |
| 2 | 2 |
| 3 local t_insert, t_remove = table.insert, table.remove; | 3 local t_insert, t_remove = table.insert, table.remove; |
| 4 local tonumber, tostring = tonumber, tostring; | 4 local tonumber, tostring = tonumber, tostring; |
| 5 local add_filter = require "util.filters".add_filter; | |
| 5 | 6 |
| 6 local xmlns_sm = "urn:xmpp:sm:2"; | 7 local xmlns_sm = "urn:xmpp:sm:2"; |
| 7 | 8 |
| 8 local sm_attr = { xmlns = xmlns_sm }; | 9 local sm_attr = { xmlns = xmlns_sm }; |
| 9 | 10 |
| 40 session.awaiting_ack = true; | 41 session.awaiting_ack = true; |
| 41 return _send(st.stanza("r", { xmlns = xmlns_sm })); | 42 return _send(st.stanza("r", { xmlns = xmlns_sm })); |
| 42 end | 43 end |
| 43 return ok, err; | 44 return ok, err; |
| 44 end | 45 end |
| 45 _send(st.stanza("enabled", sm_attr)); | 46 |
| 46 return true; | 47 session.handled_stanza_count = 0; |
| 47 end); | 48 add_filter(session, "stanzas/in", function (stanza) |
| 49 if not stanza.attr.xmlns then | |
| 50 session.handled_stanza_count = session.handled_stanza_count + 1; | |
| 51 session.log("debug", "Handled %d incoming stanzas", session.handled_stanza_count); | |
| 52 end | |
| 53 return stanza; | |
| 54 end); | |
| 55 | |
| 56 if not stanza.attr.resume then -- FIXME: Resumption should be a different spec :/ | |
| 57 _send(st.stanza("enabled", sm_attr)); | |
| 58 return true; | |
| 59 end | |
| 60 end, 100); | |
| 48 | 61 |
| 49 module:hook_stanza(xmlns_sm, "r", function (origin, stanza) | 62 module:hook_stanza(xmlns_sm, "r", function (origin, stanza) |
| 50 if not origin.smacks then | 63 if not origin.smacks then |
| 51 module:log("debug", "Received ack request from non-smack-enabled session"); | 64 module:log("debug", "Received ack request from non-smack-enabled session"); |
| 52 return; | 65 return; |
| 70 | 83 |
| 71 --TODO: Optimise... incoming stanzas should be handled by a per-session | 84 --TODO: Optimise... incoming stanzas should be handled by a per-session |
| 72 -- function that has a counter as an upvalue (no table indexing for increments, | 85 -- function that has a counter as an upvalue (no table indexing for increments, |
| 73 -- and won't slow non-198 sessions). We can also then remove the .handled flag | 86 -- and won't slow non-198 sessions). We can also then remove the .handled flag |
| 74 -- on stanzas | 87 -- on stanzas |
| 75 | |
| 76 function catch_all_incoming_stanzas(data) | |
| 77 local origin, stanza = data.origin, data.stanza; | |
| 78 if origin.smacks and not stanza.handled then | |
| 79 stanza.handled = true; | |
| 80 origin.handled_stanza_count = origin.handled_stanza_count + 1; | |
| 81 module:log("debug", "Handled %d stanzas", origin.handled_stanza_count); | |
| 82 end | |
| 83 end | |
| 84 module:hook("message/bare", catch_all_incoming_stanzas, 1000); | |
| 85 module:hook("message/full", catch_all_incoming_stanzas, 1000); | |
| 86 module:hook("message/host", catch_all_incoming_stanzas, 1000); | |
| 87 | |
| 88 module:hook("presence/bare", catch_all_incoming_stanzas, 1000); | |
| 89 module:hook("presence/full", catch_all_incoming_stanzas, 1000); | |
| 90 module:hook("presence/host", catch_all_incoming_stanzas, 1000); | |
| 91 | |
| 92 module:hook("iq/bare", catch_all_incoming_stanzas, 1000); | |
| 93 module:hook("iq/full", catch_all_incoming_stanzas, 1000); | |
| 94 module:hook("iq/host", catch_all_incoming_stanzas, 1000); | |
| 95 | 88 |
| 96 function handle_unacked_stanzas(session) | 89 function handle_unacked_stanzas(session) |
| 97 local queue = session.outgoing_stanza_queue; | 90 local queue = session.outgoing_stanza_queue; |
| 98 local error_attr = { type = "cancel" }; | 91 local error_attr = { type = "cancel" }; |
| 99 if #queue > 0 then | 92 if #queue > 0 then |
