Software /
code /
prosody-modules
Annotate
mod_csi_pump/mod_csi_pump.lua @ 3292:42dac034b2e0
mod_register_json/README: Add syntax hint
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 29 Aug 2018 23:58:14 +0200 |
parent | 2710:956b75b0e9d9 |
rev | line source |
---|---|
2455 | 1 -- Copyright (C) 2016 Kim Alvefur |
2 -- | |
3 | |
4 module:depends"csi" | |
5 module:depends"track_muc_joins" | |
6 local jid = require "util.jid"; | |
7 local new_queue = require "util.queue".new; | |
8 | |
9 local function new_pump(output, ...) | |
10 -- luacheck: ignore 212/self | |
11 local q = new_queue(...); | |
12 local flush = true; | |
13 function q:pause() | |
14 flush = false; | |
15 end | |
16 function q:resume() | |
17 flush = true; | |
18 return q:flush(); | |
19 end | |
20 local push = q.push; | |
21 function q:push(item) | |
22 local ok = push(self, item); | |
23 if not ok then | |
24 q:flush(); | |
25 output(item, self); | |
26 elseif flush then | |
27 return q:flush(); | |
28 end | |
29 return true; | |
30 end | |
31 function q:flush() | |
32 local item = self:pop(); | |
33 while item do | |
34 output(item, self); | |
35 item = self:pop(); | |
36 end | |
37 return true; | |
38 end | |
39 return q; | |
40 end | |
41 | |
42 -- TODO delay stamps | |
43 -- local dt = require "util.datetime"; | |
44 | |
45 local function is_important(stanza, session) | |
46 local st_name = stanza.name; | |
47 if not st_name then return false; end | |
48 local st_type = stanza.attr.type; | |
49 if st_name == "presence" then | |
50 -- TODO check for MUC status codes? | |
2710
956b75b0e9d9
mod_csi_pump: Consider presence other than presence updates important (ie subscription requests)
Kim Alvefur <zash@zash.se>
parents:
2460
diff
changeset
|
51 if st_type == nil or st_type == "unavailable" then |
956b75b0e9d9
mod_csi_pump: Consider presence other than presence updates important (ie subscription requests)
Kim Alvefur <zash@zash.se>
parents:
2460
diff
changeset
|
52 return false; |
956b75b0e9d9
mod_csi_pump: Consider presence other than presence updates important (ie subscription requests)
Kim Alvefur <zash@zash.se>
parents:
2460
diff
changeset
|
53 end |
956b75b0e9d9
mod_csi_pump: Consider presence other than presence updates important (ie subscription requests)
Kim Alvefur <zash@zash.se>
parents:
2460
diff
changeset
|
54 return true; |
2455 | 55 elseif st_name == "message" then |
56 if st_type == "headline" then | |
57 return false; | |
58 end | |
59 local body = stanza:get_child_text("body"); | |
60 if st_type == "groupchat" then | |
2460
3ed504b944e5
mod_csi_pump: Consider groupchat message with subject important
Kim Alvefur <zash@zash.se>
parents:
2455
diff
changeset
|
61 if stanza:get_child_text("subject") then return true; end |
3ed504b944e5
mod_csi_pump: Consider groupchat message with subject important
Kim Alvefur <zash@zash.se>
parents:
2455
diff
changeset
|
62 if not body then return false; end |
2455 | 63 if body:find(session.username, 1, true) then return true; end |
64 local rooms = session.rooms_joined; | |
65 if not rooms then return false; end | |
66 local room_nick = rooms[jid.bare(stanza.attr.from)]; | |
67 if room_nick and body:find(room_nick, 1, true) then return true; end | |
68 return false; | |
69 end | |
70 return body; | |
71 end | |
72 return true; | |
73 end | |
74 | |
75 module:hook("csi-client-inactive", function (event) | |
76 local session = event.origin; | |
77 if session.pump then | |
78 session.pump:pause(); | |
79 else | |
80 session._orig_send = session.send; | |
81 local pump = new_pump(session.send, 100); | |
82 pump:pause(); | |
83 session.pump = pump; | |
84 function session.send(stanza) | |
85 pump:push(stanza); | |
86 if is_important(stanza, session) then | |
87 pump:flush(); | |
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 |