annotate mod_csi_pump/mod_csi_pump.lua @ 2939:280305c043b0

mod_http_upload_external: Use correct namespace (thanks Wiktor)
author Kim Alvefur <zash@zash.se>
date Thu, 22 Mar 2018 12:32:02 +0100
parents 956b75b0e9d9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2455
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- Copyright (C) 2016 Kim Alvefur
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 --
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 module:depends"csi"
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 module:depends"track_muc_joins"
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local jid = require "util.jid";
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local new_queue = require "util.queue".new;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local function new_pump(output, ...)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 -- luacheck: ignore 212/self
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local q = new_queue(...);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local flush = true;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 function q:pause()
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 flush = false;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 function q:resume()
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 flush = true;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 return q:flush();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local push = q.push;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 function q:push(item)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local ok = push(self, item);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 if not ok then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 q:flush();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 output(item, self);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 elseif flush then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 return q:flush();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 return true;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 function q:flush()
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 local item = self:pop();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 while item do
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 output(item, self);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 item = self:pop();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 return true;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 return q;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 -- TODO delay stamps
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 -- local dt = require "util.datetime";
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 local function is_important(stanza, session)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 local st_name = stanza.name;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 if not st_name then return false; end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 local st_type = stanza.attr.type;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 if st_name == "presence" then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
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
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 elseif st_name == "message" then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 if st_type == "headline" then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 return false;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 local body = stanza:get_child_text("body");
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
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
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 if body:find(session.username, 1, true) then return true; end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 local rooms = session.rooms_joined;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 if not rooms then return false; end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 local room_nick = rooms[jid.bare(stanza.attr.from)];
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 if room_nick and body:find(room_nick, 1, true) then return true; end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 return false;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 return body;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 return true;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 module:hook("csi-client-inactive", function (event)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 local session = event.origin;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 if session.pump then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 session.pump:pause();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 else
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 session._orig_send = session.send;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 local pump = new_pump(session.send, 100);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 pump:pause();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 session.pump = pump;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 function session.send(stanza)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 pump:push(stanza);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 if is_important(stanza, session) then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 pump:flush();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 return true;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 end);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 module:hook("csi-client-active", function (event)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 local session = event.origin;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 if session.pump then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 session.pump:resume();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100