annotate mod_csi_pump/mod_csi_pump.lua @ 2543:3c16f0a8d66c

mod_firewall: Do patterns properly, instead of cheating
author Matthew Wild <mwild1@gmail.com>
date Tue, 21 Feb 2017 22:41:08 +0000
parents 3ed504b944e5
children 956b75b0e9d9
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 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
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 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
61 local rooms = session.rooms_joined;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 if not rooms then return false; end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 local room_nick = rooms[jid.bare(stanza.attr.from)];
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 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
65 return false;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 return body;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 return true;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 module:hook("csi-client-inactive", function (event)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 local session = event.origin;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 if session.pump then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 session.pump:pause();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 else
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 session._orig_send = session.send;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 local pump = new_pump(session.send, 100);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 pump:pause();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 session.pump = pump;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 function session.send(stanza)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 pump:push(stanza);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 if is_important(stanza, session) then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 pump:flush();
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 end
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 return true;
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 end);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 module:hook("csi-client-active", function (event)
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 local session = event.origin;
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 if session.pump then
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 session.pump:resume();
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 end);
dc30ca5ce315 mod_csi_pump: Lossless CSI mode
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97