Mercurial > prosody-modules
annotate mod_csi_pump/mod_csi_pump.lua @ 2590:63dd3e525f13
mod_http_muc_log: Add some comments
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 01 Mar 2017 20:29:40 +0100 |
parents | 3ed504b944e5 |
children | 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? | |
51 return false; | |
52 elseif st_name == "message" then | |
53 if st_type == "headline" then | |
54 return false; | |
55 end | |
56 local body = stanza:get_child_text("body"); | |
57 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
|
58 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
|
59 if not body then return false; end |
2455 | 60 if body:find(session.username, 1, true) then return true; end |
61 local rooms = session.rooms_joined; | |
62 if not rooms then return false; end | |
63 local room_nick = rooms[jid.bare(stanza.attr.from)]; | |
64 if room_nick and body:find(room_nick, 1, true) then return true; end | |
65 return false; | |
66 end | |
67 return body; | |
68 end | |
69 return true; | |
70 end | |
71 | |
72 module:hook("csi-client-inactive", function (event) | |
73 local session = event.origin; | |
74 if session.pump then | |
75 session.pump:pause(); | |
76 else | |
77 session._orig_send = session.send; | |
78 local pump = new_pump(session.send, 100); | |
79 pump:pause(); | |
80 session.pump = pump; | |
81 function session.send(stanza) | |
82 pump:push(stanza); | |
83 if is_important(stanza, session) then | |
84 pump:flush(); | |
85 end | |
86 return true; | |
87 end | |
88 end | |
89 end); | |
90 | |
91 module:hook("csi-client-active", function (event) | |
92 local session = event.origin; | |
93 if session.pump then | |
94 session.pump:resume(); | |
95 end | |
96 end); | |
97 |