annotate mod_csi_pump/mod_csi_pump.lua @ 2457:17539a5d73f4

mod_net_dovecotauth: Import util.pposix [luacheck]
author Kim Alvefur <zash@zash.se>
date Sun, 22 Jan 2017 04:42:55 +0100
parents dc30ca5ce315
children 3ed504b944e5
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?
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 return false;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 elseif st_name == "message" then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 if st_type == "headline" then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 return false;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 local body = stanza:get_child_text("body");
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 if not body then return false; end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 if st_type == "groupchat" then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 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
60 local rooms = session.rooms_joined;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 if not rooms then return false; end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local room_nick = rooms[jid.bare(stanza.attr.from)];
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 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
64 return false;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 return body;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 return true;
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
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 module:hook("csi-client-inactive", function (event)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 local session = event.origin;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 if session.pump then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 session.pump:pause();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 else
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 session._orig_send = session.send;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 local pump = new_pump(session.send, 100);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 pump:pause();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 session.pump = pump;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 function session.send(stanza)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 pump:push(stanza);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 if is_important(stanza, session) then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 pump:flush();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 return true;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 end
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
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 module:hook("csi-client-active", function (event)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 local session = event.origin;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 if session.pump then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 session.pump:resume();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 end);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96