Annotate

plugins/mod_csi_simple.lua @ 9909:3229be01a08a

mod_csi_simple: Use write locks in net.server if available
author Kim Alvefur <zash@zash.se>
date Sat, 24 Nov 2018 02:25:44 +0100
parent 9771:bf92f37de137
child 9911:ed011935c22d
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
9767
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
82 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
83 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 return true;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 end, -1);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 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
88 local session = event.origin;
9909
3229be01a08a mod_csi_simple: Use write locks in net.server if available
Kim Alvefur <zash@zash.se>
parents: 9771
diff changeset
89 if session.conn and session.conn and session.conn.pause_writes then
3229be01a08a mod_csi_simple: Use write locks in net.server if available
Kim Alvefur <zash@zash.se>
parents: 9771
diff changeset
90 session.conn:pause_writes();
3229be01a08a mod_csi_simple: Use write locks in net.server if available
Kim Alvefur <zash@zash.se>
parents: 9771
diff changeset
91 elseif session.pump then
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 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
93 else
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 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
95 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
96 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
97 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
98 pump:pause();
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 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
100 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
101 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
102 pump:flush();
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 send(stanza);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 else
9650
db6bcafd76c9 mod_csi_simple: Fix autocompletion mistake
Kim Alvefur <zash@zash.se>
parents: 9649
diff changeset
105 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
106 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
107 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
108 end
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 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
110 end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 return true;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
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
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 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
117 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
118 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
119 session.pump:resume();
9909
3229be01a08a mod_csi_simple: Use write locks in net.server if available
Kim Alvefur <zash@zash.se>
parents: 9771
diff changeset
120 elseif session.conn and session.conn and session.conn.resume_writes then
3229be01a08a mod_csi_simple: Use write locks in net.server if available
Kim Alvefur <zash@zash.se>
parents: 9771
diff changeset
121 session.conn:resume_writes();
9589
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 end);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124