Software /
code /
prosody
Annotate
plugins/mod_debug_stanzas/watcher.lib.lua @ 12471:a3b12eeedd4b
mod_smacks: Improve activation of smacks on outgoing s2s
Using a timer was a hack to get around that stream features are not
available at the right time and sendq stanzas were stored as strings
so could not be counted properly. The later has now been fixed and the
former is fixed by recording the relevant stream feature on the session
so that the correct version of XEP-0198 can be activated once the
connection has been authenticated and is ready to start.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 24 Apr 2022 16:17:32 +0200 |
parent | 12463:788048158982 |
child | 12977:74b9e05af71e |
rev | line source |
---|---|
12463
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local filters = require "util.filters"; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local jid = require "util.jid"; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local set = require "util.set"; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local client_watchers = {}; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 -- active_filters[session] = { |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 -- filter_func = filter_func; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 -- downstream = { cb1, cb2, ... }; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 -- } |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local active_filters = {}; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local function subscribe_session_stanzas(session, handler, reason) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 if active_filters[session] then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 table.insert(active_filters[session].downstream, handler); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 if reason then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 handler(reason, nil, session); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 return; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local downstream = { handler }; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 active_filters[session] = { |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 filter_in = function (stanza) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 module:log("debug", "NOTIFY WATCHER %d", #downstream); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 for i = 1, #downstream do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 downstream[i]("received", stanza, session); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 return stanza; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 filter_out = function (stanza) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 module:log("debug", "NOTIFY WATCHER %d", #downstream); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 for i = 1, #downstream do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 downstream[i]("sent", stanza, session); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 return stanza; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 end; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 downstream = downstream; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 }; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 filters.add_filter(session, "stanzas/in", active_filters[session].filter_in); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 filters.add_filter(session, "stanzas/out", active_filters[session].filter_out); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 if reason then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 handler(reason, nil, session); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 local function unsubscribe_session_stanzas(session, handler, reason) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 local active_filter = active_filters[session]; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 if not active_filter then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 return; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 for i = #active_filter.downstream, 1, -1 do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 if active_filter.downstream[i] == handler then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 table.remove(active_filter.downstream, i); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 if reason then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 handler(reason, nil, session); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 if #active_filter.downstream == 0 then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 filters.remove_filter(session, "stanzas/in", active_filter.filter_in); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 filters.remove_filter(session, "stanzas/out", active_filter.filter_out); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 active_filters[session] = nil; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 local function unsubscribe_all_from_session(session, reason) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 local active_filter = active_filters[session]; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 if not active_filter then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 return; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 for i = #active_filter.downstream, 1, -1 do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 local handler = table.remove(active_filter.downstream, i); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 if reason then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 handler(reason, nil, session); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 filters.remove_filter(session, "stanzas/in", active_filter.filter_in); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 filters.remove_filter(session, "stanzas/out", active_filter.filter_out); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 active_filters[session] = nil; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 local function unsubscribe_handler_from_all(handler, reason) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 for session in pairs(active_filters) do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 unsubscribe_session_stanzas(session, handler, reason); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 local s2s_watchers = {}; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 module:hook("s2sin-established", function (event) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 for _, watcher in ipairs(s2s_watchers) do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 if watcher.target_spec == event.session.from_host then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 subscribe_session_stanzas(event.session, watcher.handler, "opened"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 end); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 module:hook("s2sout-established", function (event) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 for _, watcher in ipairs(s2s_watchers) do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 if watcher.target_spec == event.session.to_host then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 subscribe_session_stanzas(event.session, watcher.handler, "opened"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 end); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 module:hook("s2s-closed", function (event) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 unsubscribe_all_from_session(event.session, "closed"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 local watched_hosts = set.new(); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 local handler_map = setmetatable({}, { __mode = "kv" }); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 local function add_stanza_watcher(spec, orig_handler) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 local function filtering_handler(event_type, stanza, session) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 if stanza and spec.filter_spec then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 if spec.filter_spec.with_jid then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 if event_type == "sent" and (not stanza.attr.from or not jid.compare(stanza.attr.from, spec.filter_spec.with_jid)) then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 return; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 elseif event_type == "received" and (not stanza.attr.to or not jid.compare(stanza.attr.to, spec.filter_spec.with_jid)) then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 return; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 return orig_handler(event_type, stanza, session); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 handler_map[orig_handler] = filtering_handler; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 if spec.target_spec.jid then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 local target_is_remote_host = not jid.node(spec.target_spec.jid) and not prosody.hosts[spec.target_spec.jid]; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 if target_is_remote_host then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 -- Watch s2s sessions |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 table.insert(s2s_watchers, { |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 target_spec = spec.target_spec.jid; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 handler = filtering_handler; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 orig_handler = orig_handler; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 }); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 -- Scan existing s2sin for matches |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 for session in pairs(prosody.incoming_s2s) do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 if spec.target_spec.jid == session.from_host then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 subscribe_session_stanzas(session, filtering_handler, "attached"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 -- Scan existing s2sout for matches |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 for local_host, local_session in pairs(prosody.hosts) do --luacheck: ignore 213/local_host |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 for remote_host, remote_session in pairs(local_session.s2sout) do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 if spec.target_spec.jid == remote_host then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 subscribe_session_stanzas(remote_session, filtering_handler, "attached"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 else |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 table.insert(client_watchers, { |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 target_spec = spec.target_spec.jid; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 handler = filtering_handler; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 orig_handler = orig_handler; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 }); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 local host = jid.host(spec.target_spec.jid); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 if not watched_hosts:contains(host) and prosody.hosts[host] then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 module:context(host):hook("resource-bind", function (event) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 for _, watcher in ipairs(client_watchers) do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 module:log("debug", "NEW CLIENT: %s vs %s", event.session.full_jid, watcher.target_spec); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 if jid.compare(event.session.full_jid, watcher.target_spec) then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 module:log("debug", "MATCH"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 subscribe_session_stanzas(event.session, watcher.handler, "opened"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 else |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 module:log("debug", "NO MATCH"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 end); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 module:context(host):hook("resource-unbind", function (event) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 unsubscribe_all_from_session(event.session, "closed"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 end); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 watched_hosts:add(host); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 for full_jid, session in pairs(prosody.full_sessions) do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 if jid.compare(full_jid, spec.target_spec.jid) then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 subscribe_session_stanzas(session, filtering_handler, "attached"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 else |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 error("No recognized target selector"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 local function remove_stanza_watcher(orig_handler) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 local handler = handler_map[orig_handler]; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 unsubscribe_handler_from_all(handler, "detached"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 handler_map[orig_handler] = nil; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 for i = #client_watchers, 1, -1 do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 if client_watchers[i].orig_handler == orig_handler then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 table.remove(client_watchers, i); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 for i = #s2s_watchers, 1, -1 do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 if s2s_watchers[i].orig_handler == orig_handler then |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 table.remove(s2s_watchers, i); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 local function cleanup(reason) |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 client_watchers = {}; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 s2s_watchers = {}; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 for session in pairs(active_filters) do |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 unsubscribe_all_from_session(session, reason or "cancelled"); |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 end |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 return { |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 add = add_stanza_watcher; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 remove = remove_stanza_watcher; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 cleanup = cleanup; |
788048158982
mod_debug_stanzas/watcher: New module library to dynamically 'watch' for stanzas
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 }; |