Annotate

plugins/mod_csi_simple.lua @ 11157:413bd21ba449 0.11

net.websocket.frames: Read buffer length correctly in Lua 5.1 (fix #1598) COMPAT: The __len metamethod does not work with tables in Lua 5.1. Both strings and util.dbuffer now expose their length as a :len() method.
author Kim Alvefur <zash@zash.se>
date Mon, 12 Oct 2020 20:21:18 +0200
parent 10816:b4c2c7fae7c0
child 10817:7c503938ce1d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- Copyright (C) 2016-2018 Kim Alvefur
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 --
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- This project is MIT/X11 licensed. Please see the
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- COPYING file in the source package for more information.
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 --
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 module:depends"csi"
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local jid = require "util.jid";
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local st = require "util.stanza";
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local dt = require "util.datetime";
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local new_queue = require "util.queue".new;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local function new_pump(output, ...)
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 -- luacheck: ignore 212/self
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local q = new_queue(...);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local flush = true;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 function q:pause()
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 flush = false;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 function q:resume()
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 flush = true;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 return q:flush();
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local push = q.push;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 function q:push(item)
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local ok = push(self, item);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 if not ok then
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 q:flush();
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 output(item, self);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 elseif flush then
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 return q:flush();
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 return true;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 function q:flush()
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 local item = self:pop();
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 while item do
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 output(item, self);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 item = self:pop();
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 return true;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return q;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 local queue_size = module:get_option_number("csi_queue_size", 256);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 module:hook("csi-is-stanza-important", function (event)
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 local stanza = event.stanza;
9632
fdefc43bffff mod_csi_simple: Consider non-stanza objects important
Kim Alvefur <zash@zash.se>
parents: 9631
diff changeset
51 if not st.is_stanza(stanza) then
fdefc43bffff mod_csi_simple: Consider non-stanza objects important
Kim Alvefur <zash@zash.se>
parents: 9631
diff changeset
52 return true;
fdefc43bffff mod_csi_simple: Consider non-stanza objects important
Kim Alvefur <zash@zash.se>
parents: 9631
diff changeset
53 end
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 local st_name = stanza.name;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 if not st_name then return false; end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 local st_type = stanza.attr.type;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 if st_name == "presence" then
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 if st_type == nil or st_type == "unavailable" then
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 return false;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 return true;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 elseif st_name == "message" then
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 if st_type == "headline" then
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 return false;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 end
9768
ab12fd48e124 mod_csi_simple: Consider messages forwarded from another of the users clients as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9767
diff changeset
66 if stanza:get_child("sent", "urn:xmpp:carbons:2") then
ab12fd48e124 mod_csi_simple: Consider messages forwarded from another of the users clients as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9767
diff changeset
67 return true;
ab12fd48e124 mod_csi_simple: Consider messages forwarded from another of the users clients as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9767
diff changeset
68 end
9769
150e9574c149 mod_csi_simple: Unpack Carbons-forwarded messages (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9768
diff changeset
69 local forwarded = stanza:find("{urn:xmpp:carbons:2}received/{urn:xmpp:forward:0}/{jabber:client}message");
150e9574c149 mod_csi_simple: Unpack Carbons-forwarded messages (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9768
diff changeset
70 if forwarded then
150e9574c149 mod_csi_simple: Unpack Carbons-forwarded messages (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9768
diff changeset
71 stanza = forwarded;
150e9574c149 mod_csi_simple: Unpack Carbons-forwarded messages (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9768
diff changeset
72 end
9767
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
73 if stanza:get_child("body") then
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
74 return true;
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
75 end
9770
76cb409db537 mod_csi_simple: Consider messages with subject (eg MUC joins) (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9769
diff changeset
76 if stanza:get_child("subject") then
76cb409db537 mod_csi_simple: Consider messages with subject (eg MUC joins) (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9769
diff changeset
77 return true;
76cb409db537 mod_csi_simple: Consider messages with subject (eg MUC joins) (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9769
diff changeset
78 end
9771
bf92f37de137 mod_csi_simple: Consider messages encrypted payload as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9770
diff changeset
79 if stanza:get_child("encryption", "urn:xmpp:eme:0") then
bf92f37de137 mod_csi_simple: Consider messages encrypted payload as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9770
diff changeset
80 return true;
bf92f37de137 mod_csi_simple: Consider messages encrypted payload as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9770
diff changeset
81 end
10816
b4c2c7fae7c0 mod_csi_simple: Consider XEP-0353: Jingle Message Initiation important
Kim Alvefur <zash@zash.se>
parents: 9771
diff changeset
82 if stanza:get_child(nil, "urn:xmpp:jingle-message:0") then
b4c2c7fae7c0 mod_csi_simple: Consider XEP-0353: Jingle Message Initiation important
Kim Alvefur <zash@zash.se>
parents: 9771
diff changeset
83 return true;
b4c2c7fae7c0 mod_csi_simple: Consider XEP-0353: Jingle Message Initiation important
Kim Alvefur <zash@zash.se>
parents: 9771
diff changeset
84 end
9767
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
85 return false;
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 return true;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 end, -1);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 module:hook("csi-client-inactive", function (event)
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 local session = event.origin;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 if session.pump then
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 session.pump:pause();
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 else
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 local bare_jid = jid.join(session.username, session.host);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 local send = session.send;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 session._orig_send = send;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 local pump = new_pump(session.send, queue_size);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 pump:pause();
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 session.pump = pump;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 function session.send(stanza)
9649
a20849226598 mod_csi_simple: Bypass importance event in active mode (fixes #1249)
Kim Alvefur <zash@zash.se>
parents: 9648
diff changeset
102 if session.state == "active" or module:fire_event("csi-is-stanza-important", { stanza = stanza, session = session }) then
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 pump:flush();
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 send(stanza);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 else
9650
db6bcafd76c9 mod_csi_simple: Fix autocompletion mistake
Kim Alvefur <zash@zash.se>
parents: 9649
diff changeset
106 if st.is_stanza(stanza) and stanza.attr.xmlns == nil and stanza.name ~= "iq" then
9631
d6104aaf94bc mod_csi_simple: Skip delay tags on objects other than stanzas (thanks quest)
Kim Alvefur <zash@zash.se>
parents: 9589
diff changeset
107 stanza = st.clone(stanza);
d6104aaf94bc mod_csi_simple: Skip delay tags on objects other than stanzas (thanks quest)
Kim Alvefur <zash@zash.se>
parents: 9589
diff changeset
108 stanza:add_direct_child(st.stanza("delay", {xmlns = "urn:xmpp:delay", from = bare_jid, stamp = dt.datetime()}));
d6104aaf94bc mod_csi_simple: Skip delay tags on objects other than stanzas (thanks quest)
Kim Alvefur <zash@zash.se>
parents: 9589
diff changeset
109 end
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 pump:push(stanza);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 return true;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 end);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 module:hook("csi-client-active", function (event)
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 local session = event.origin;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 if session.pump then
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 session.pump:resume();
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 end);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123