Comparison

plugins/mod_csi_simple.lua @ 9589:aeb054ee88c5 0.11

mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
author Kim Alvefur <zash@zash.se>
date Mon, 29 Oct 2018 21:15:38 +0100
child 9631:d6104aaf94bc
comparison
equal deleted inserted replaced
9588:6a3f06a5dff9 9589:aeb054ee88c5
1 -- Copyright (C) 2016-2018 Kim Alvefur
2 --
3 -- This project is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
5 --
6
7 module:depends"csi"
8
9 local jid = require "util.jid";
10 local st = require "util.stanza";
11 local dt = require "util.datetime";
12 local new_queue = require "util.queue".new;
13
14 local function new_pump(output, ...)
15 -- luacheck: ignore 212/self
16 local q = new_queue(...);
17 local flush = true;
18 function q:pause()
19 flush = false;
20 end
21 function q:resume()
22 flush = true;
23 return q:flush();
24 end
25 local push = q.push;
26 function q:push(item)
27 local ok = push(self, item);
28 if not ok then
29 q:flush();
30 output(item, self);
31 elseif flush then
32 return q:flush();
33 end
34 return true;
35 end
36 function q:flush()
37 local item = self:pop();
38 while item do
39 output(item, self);
40 item = self:pop();
41 end
42 return true;
43 end
44 return q;
45 end
46
47 local queue_size = module:get_option_number("csi_queue_size", 256);
48
49 module:hook("csi-is-stanza-important", function (event)
50 local stanza = event.stanza;
51 local st_name = stanza.name;
52 if not st_name then return false; end
53 local st_type = stanza.attr.type;
54 if st_name == "presence" then
55 if st_type == nil or st_type == "unavailable" then
56 return false;
57 end
58 return true;
59 elseif st_name == "message" then
60 if st_type == "headline" then
61 return false;
62 end
63 local body = stanza:get_child_text("body");
64 return body;
65 end
66 return true;
67 end, -1);
68
69 module:hook("csi-client-inactive", function (event)
70 local session = event.origin;
71 if session.pump then
72 session.pump:pause();
73 else
74 local bare_jid = jid.join(session.username, session.host);
75 local send = session.send;
76 session._orig_send = send;
77 local pump = new_pump(session.send, queue_size);
78 pump:pause();
79 session.pump = pump;
80 function session.send(stanza)
81 if module:fire_event("csi-stanza-is-important", { stanza = stanza, session = session }) then
82 pump:flush();
83 send(stanza);
84 else
85 stanza = st.clone(stanza);
86 stanza:add_direct_child(st.stanza("delay", {xmlns = "urn:xmpp:delay", from = bare_jid, stamp = dt.datetime()}));
87 pump:push(stanza);
88 end
89 return true;
90 end
91 end
92 end);
93
94 module:hook("csi-client-active", function (event)
95 local session = event.origin;
96 if session.pump then
97 session.pump:resume();
98 end
99 end);
100