Software /
code /
prosody-modules
Comparison
mod_csi_battery_saver/mod_csi_battery_saver.lua @ 3633:6b0db0f2d57a
mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
author | tmolitor <thilo@eightysoft.de> |
---|---|
date | Tue, 30 Jul 2019 01:46:57 +0200 |
parent | 3490:972b21d34306 |
child | 3980:19c5bfc3a241 |
comparison
equal
deleted
inserted
replaced
3632:83a68f5fde1d | 3633:6b0db0f2d57a |
---|---|
1 -- Copyright (C) 2016 Kim Alvefur | 1 -- Copyright (C) 2016 Kim Alvefur |
2 -- Copyright (C) 2017 Thilo Molitor | 2 -- Copyright (C) 2017 Thilo Molitor |
3 -- | 3 -- |
4 | 4 |
5 local filter_muc = module:get_option_boolean("csi_battery_saver_filter_muc", false); | 5 local filter_muc = module:get_option_boolean("csi_battery_saver_filter_muc", false); |
6 local queue_size = module:get_option_number("csi_battery_saver_queue_size", 256); | |
6 | 7 |
7 module:depends"csi" | 8 module:depends"csi" |
8 if filter_muc then module:depends"track_muc_joins"; end -- only depend on this module if we actually use it | 9 if filter_muc then module:depends"track_muc_joins"; end -- only depend on this module if we actually use it |
9 local s_match = string.match; | 10 local s_match = string.match; |
10 local s_sub = string.sub; | 11 local s_sub = string.sub; |
29 local message = forward and forward:child_with_name("message") or nil; | 30 local message = forward and forward:child_with_name("message") or nil; |
30 if not message then return; end | 31 if not message then return; end |
31 return message, direction; | 32 return message, direction; |
32 end | 33 end |
33 | 34 |
34 local function new_pump(output, ...) | 35 local function new_pump(session, output, ...) |
35 -- luacheck: ignore 212/self | 36 -- luacheck: ignore 212/self |
36 local q = new_queue(...); | 37 local q = new_queue(...); |
37 local flush = true; | 38 local flush = true; |
38 function q:pause() | 39 function q:pause() |
39 flush = false; | 40 flush = false; |
44 end | 45 end |
45 local push = q.push; | 46 local push = q.push; |
46 function q:push(item) | 47 function q:push(item) |
47 local ok = push(self, item); | 48 local ok = push(self, item); |
48 if not ok then | 49 if not ok then |
50 session.log("debug", "mod_csi_battery_saver(%s): Queue full (%d items), forcing flush...", id, q:count()); | |
49 q:flush(); | 51 q:flush(); |
50 output(item, self); | 52 output(item, self); |
51 elseif flush then | 53 elseif flush then |
52 return q:flush(); | 54 return q:flush(); |
53 end | 55 end |
113 -- check xep27 pgp https://xmpp.org/extensions/xep-0027.html | 115 -- check xep27 pgp https://xmpp.org/extensions/xep-0027.html |
114 if stanza:get_child("x", "jabber:x:encrypted") then return true; end | 116 if stanza:get_child("x", "jabber:x:encrypted") then return true; end |
115 | 117 |
116 -- check xep373 pgp (OX) https://xmpp.org/extensions/xep-0373.html | 118 -- check xep373 pgp (OX) https://xmpp.org/extensions/xep-0373.html |
117 if stanza:get_child("openpgp", "urn:xmpp:openpgp:0") then return true; end | 119 if stanza:get_child("openpgp", "urn:xmpp:openpgp:0") then return true; end |
120 | |
121 -- check eme | |
122 if stanza:get_child("encryption", "urn:xmpp:eme:0") then return true; end | |
118 | 123 |
119 local body = stanza:get_child_text("body"); | 124 local body = stanza:get_child_text("body"); |
120 if st_type == "groupchat" then | 125 if st_type == "groupchat" then |
121 if stanza:get_child_text("subject") then return true; end | 126 if stanza:get_child_text("subject") then return true; end |
122 if body == nil or body == "" then return false; end | 127 if body == nil or body == "" then return false; end |
123 -- body contains text, let's see if we want to process it further | 128 -- body contains text, let's see if we want to process it further |
124 if filter_muc then | 129 if not filter_muc then -- default case |
130 local stanza_important = module:fire_event("csi-is-stanza-important", { stanza = stanza, session = session }); | |
131 if stanza_important ~= nil then return stanza_important; end | |
132 return true; -- deemed unknown/high priority by mod_csi_muc_priorities or some other module | |
133 else | |
125 if body:find(session.username, 1, true) then return true; end | 134 if body:find(session.username, 1, true) then return true; end |
126 local rooms = session.rooms_joined; | 135 local rooms = session.rooms_joined; |
127 if not rooms then return false; end | 136 if not rooms then return false; end |
128 local room_nick = rooms[jid.bare(stanza_direction == "in" and stanza.attr.from or stanza.attr.to)]; | 137 local room_nick = rooms[jid.bare(stanza_direction == "in" and stanza.attr.from or stanza.attr.to)]; |
129 if room_nick and body:find(room_nick, 1, true) then return true; end | 138 if room_nick and body:find(room_nick, 1, true) then return true; end |
130 return false; | 139 return false; |
131 else | |
132 return true; | |
133 end | 140 end |
134 end | 141 end |
135 return body ~= nil and body ~= ""; | 142 return body ~= nil and body ~= ""; |
136 end | 143 end |
137 return true; | 144 return true; |
142 if session.pump then | 149 if session.pump then |
143 session.log("debug", "mod_csi_battery_saver(%s): Client is inactive, buffering unimportant outgoing stanzas", id); | 150 session.log("debug", "mod_csi_battery_saver(%s): Client is inactive, buffering unimportant outgoing stanzas", id); |
144 session.pump:pause(); | 151 session.pump:pause(); |
145 else | 152 else |
146 session.log("debug", "mod_csi_battery_saver(%s): Client is inactive the first time, initializing module for this session", id); | 153 session.log("debug", "mod_csi_battery_saver(%s): Client is inactive the first time, initializing module for this session", id); |
147 local pump = new_pump(session.send, 100); | 154 local pump = new_pump(session, session.send, queue_size); |
148 pump:pause(); | 155 pump:pause(); |
149 session.pump = pump; | 156 session.pump = pump; |
150 session._pump_orig_send = session.send; | 157 session._pump_orig_send = session.send; |
151 function session.send(stanza) | 158 function session.send(stanza) |
152 session.log("debug", "mod_csi_battery_saver(%s): Got outgoing stanza: <%s>", id, tostring(stanza.name or stanza)); | 159 session.log("debug", "mod_csi_battery_saver(%s): Got outgoing stanza: <%s>", id, tostring(stanza.name or stanza)); |