Software /
code /
prosody-modules
Annotate
mod_stanza_counter/mod_stanza_counter.lua @ 4941:e7b9bc629ecc
mod_rest: Add special handling to catch MAM results from remote hosts
Makes MAM queries to remote hosts works.
As the comment says, MAM results from users' local archives or local
MUCs are returned via origin.send() which is provided in the event and
thus already worked. Results from remote hosts go via normal stanza
routing and events, which need this extra handling to catch.
This pattern of iq-set, message+, iq-result is generally limited to MAM.
Closest similar thing might be MUC join, but to really handle that you
would need the webhook callback mechanism.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 16 May 2022 19:47:09 +0200 |
parent | 1343:7dbde05b48a9 |
rev | line source |
---|---|
432
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
1 -- (C) 2011, Marco Cirillo (LW.Org) |
436
e4a1f0425380
mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents:
435
diff
changeset
|
2 -- General Stanzas' Counter. |
432
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
3 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
4 local jid_bare = require "util.jid".bare |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
5 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
6 -- Setup, Init functions. |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
7 -- initialize function counter table on the global object on start |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
8 local function init_counter() |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
534
diff
changeset
|
9 prosody.stanza_counter = { |
432
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
10 iq = { incoming=0, outgoing=0 }, |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
11 message = { incoming=0, outgoing=0 }, |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
12 presence = { incoming=0, outgoing=0 } |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
13 } |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
14 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
15 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
16 -- Setup on server start |
534
3d6e0e037dab
mod_stanza_counter: removed a few empty lines.
Marco Cirillo <maranda@lightwitch.org>
parents:
436
diff
changeset
|
17 local function setup() init_counter() end |
432
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
18 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
19 -- Basic Stanzas' Counters |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
20 local function iq_callback(check) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
21 return function(self) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
22 local origin, stanza = self.origin, self.stanza |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
23 if not prosody.stanza_counter then init_counter() end |
434
ac4a6cc5776c
mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents:
433
diff
changeset
|
24 if check then |
534
3d6e0e037dab
mod_stanza_counter: removed a few empty lines.
Marco Cirillo <maranda@lightwitch.org>
parents:
436
diff
changeset
|
25 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil |
432
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
26 else |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
27 prosody.stanza_counter.iq["outgoing"] = prosody.stanza_counter.iq["outgoing"] + 1 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
28 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
29 else |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
30 prosody.stanza_counter.iq["incoming"] = prosody.stanza_counter.iq["incoming"] + 1 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
31 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
32 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
33 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
34 |
434
ac4a6cc5776c
mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents:
433
diff
changeset
|
35 local function mes_callback(check) |
432
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
36 return function(self) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
37 local origin, stanza = self.origin, self.stanza |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
38 if not prosody.stanza_counter then init_counter() end |
434
ac4a6cc5776c
mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents:
433
diff
changeset
|
39 if check then |
534
3d6e0e037dab
mod_stanza_counter: removed a few empty lines.
Marco Cirillo <maranda@lightwitch.org>
parents:
436
diff
changeset
|
40 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil |
432
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
41 else |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
42 prosody.stanza_counter.message["outgoing"] = prosody.stanza_counter.message["outgoing"] + 1 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
43 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
44 else |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
45 prosody.stanza_counter.message["incoming"] = prosody.stanza_counter.message["incoming"] + 1 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
46 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
47 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
48 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
49 |
434
ac4a6cc5776c
mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents:
433
diff
changeset
|
50 local function pre_callback(check) |
432
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
51 return function(self) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
52 local origin, stanza = self.origin, self.stanza |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
53 if not prosody.stanza_counter then init_counter() end |
434
ac4a6cc5776c
mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents:
433
diff
changeset
|
54 if check then |
534
3d6e0e037dab
mod_stanza_counter: removed a few empty lines.
Marco Cirillo <maranda@lightwitch.org>
parents:
436
diff
changeset
|
55 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil |
432
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
56 else |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
57 prosody.stanza_counter.presence["outgoing"] = prosody.stanza_counter.presence["outgoing"] + 1 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
58 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
59 else |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
60 prosody.stanza_counter.presence["incoming"] = prosody.stanza_counter.presence["incoming"] + 1 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
61 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
62 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
63 end |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
64 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
65 -- Hook all pre-stanza events. |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
66 module:hook("pre-iq/bare", iq_callback(true), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
67 module:hook("pre-iq/full", iq_callback(true), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
68 module:hook("pre-iq/host", iq_callback(true), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
69 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
70 module:hook("pre-message/bare", mes_callback(true), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
71 module:hook("pre-message/full", mes_callback(true), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
72 module:hook("pre-message/host", mes_callback(true), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
73 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
74 module:hook("pre-presence/bare", pre_callback(true), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
75 module:hook("pre-presence/full", pre_callback(true), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
76 module:hook("pre-presence/host", pre_callback(true), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
77 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
78 -- Hook all stanza events. |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
79 module:hook("iq/bare", iq_callback(false), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
80 module:hook("iq/full", iq_callback(false), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
81 module:hook("iq/host", iq_callback(false), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
82 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
83 module:hook("message/bare", mes_callback(false), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
84 module:hook("message/full", mes_callback(false), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
85 module:hook("message/host", mes_callback(false), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
86 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
87 module:hook("presence/bare", pre_callback(false), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
88 module:hook("presence/full", pre_callback(false), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
89 module:hook("presence/host", pre_callback(false), 140) |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
90 |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
91 -- Hook server start to initialize the counter. |
23a6289a00bd
mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff
changeset
|
92 module:hook("server-started", setup) |