Software /
code /
prosody-modules
Annotate
mod_csi_battery_saver/mod_csi_battery_saver.lua @ 3481:1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 08 Mar 2019 11:44:20 +0000 |
parent | 3109:75930e4c2478 |
child | 3490:972b21d34306 |
rev | line source |
---|---|
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
1 -- Copyright (C) 2016 Kim Alvefur |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
2 -- Copyright (C) 2017 Thilo Molitor |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
3 -- |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
4 |
2747
a688b31295ea
mod_csi_battery_saver: only depend on mod_track_muc_joins if actually used
tmolitor <thilo@eightysoft.de>
parents:
2746
diff
changeset
|
5 local filter_muc = module:get_option_boolean("csi_battery_saver_filter_muc", false); |
a688b31295ea
mod_csi_battery_saver: only depend on mod_track_muc_joins if actually used
tmolitor <thilo@eightysoft.de>
parents:
2746
diff
changeset
|
6 |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
7 module:depends"csi" |
2747
a688b31295ea
mod_csi_battery_saver: only depend on mod_track_muc_joins if actually used
tmolitor <thilo@eightysoft.de>
parents:
2746
diff
changeset
|
8 if filter_muc then module:depends"track_muc_joins"; end -- only depend on this module if we actually use it |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
9 local s_match = string.match; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
10 local s_sub = string.sub; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
11 local jid = require "util.jid"; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
12 local new_queue = require "util.queue".new; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
13 local datetime = require "util.datetime"; |
2741
69248dcd7cff
mod_csi_battery_saver: Fix interaction with smacks hibernation
tmolitor <thilo@eightysoft.de>
parents:
2737
diff
changeset
|
14 local st = require "util.stanza"; |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
15 |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
16 local xmlns_delay = "urn:xmpp:delay"; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
17 |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
18 -- a log id for this module instance |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
19 local id = s_sub(require "util.hashes".sha256(datetime.datetime(), true), 1, 4); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
20 |
2746
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
21 |
3481
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
22 -- Returns a forwarded message, and either "in" or "out" depending on the direction |
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
23 -- Returns nil if the message is not a carbon |
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
24 local function extract_carbon(stanza) |
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
25 local carbon = stanza:child_with_ns("urn:xmpp:carbons:2") or stanza:child_with_ns("urn:xmpp:carbons:1"); |
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
26 if not carbon then return; end |
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
27 local direction = carbon.name == "sent" and "out" or "in"; |
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
28 local forward = carbon:get_child("urn:xmpp:forward:0", "forwarded"); |
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
29 local message = forward and forward:child_with_name("message") or nil; |
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
30 if not message then return; end |
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
31 return message, direction; |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
32 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
33 |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
34 local function new_pump(output, ...) |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
35 -- luacheck: ignore 212/self |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
36 local q = new_queue(...); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
37 local flush = true; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
38 function q:pause() |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
39 flush = false; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
40 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
41 function q:resume() |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
42 flush = true; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
43 return q:flush(); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
44 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
45 local push = q.push; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
46 function q:push(item) |
2741
69248dcd7cff
mod_csi_battery_saver: Fix interaction with smacks hibernation
tmolitor <thilo@eightysoft.de>
parents:
2737
diff
changeset
|
47 local ok = push(self, item); |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
48 if not ok then |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
49 q:flush(); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
50 output(item, self); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
51 elseif flush then |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
52 return q:flush(); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
53 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
54 return true; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
55 end |
2745
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
56 function q:flush(alternative_output) |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
57 local out = alternative_output or output; |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
58 local item = self:pop(); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
59 while item do |
2745
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
60 out(item, self); |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
61 item = self:pop(); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
62 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
63 return true; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
64 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
65 return q; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
66 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
67 |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
68 local function is_stamp_needed(stanza, session) |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
69 local st_name = stanza and stanza.name or nil; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
70 if st_name == "presence" then |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
71 return true; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
72 elseif st_name == "message" then |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
73 if stanza:get_child("delay", xmlns_delay) then return false; end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
74 if stanza.attr.type == "chat" or stanza.attr.type == "groupchat" then return true; end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
75 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
76 return false; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
77 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
78 |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
79 local function add_stamp(stanza, session) |
2757
595d9d68fd11
mod_csi_battery_saver: Fix #921
tmolitor <thilo@eightysoft.de>
parents:
2754
diff
changeset
|
80 local bare_jid = jid.bare(session.full_jid or session.host); |
595d9d68fd11
mod_csi_battery_saver: Fix #921
tmolitor <thilo@eightysoft.de>
parents:
2754
diff
changeset
|
81 stanza = stanza:tag("delay", { xmlns = xmlns_delay, from = bare_jid, stamp = datetime.datetime()}); |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
82 return stanza; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
83 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
84 |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
85 local function is_important(stanza, session) |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
86 local st_name = stanza and stanza.name or nil; |
2741
69248dcd7cff
mod_csi_battery_saver: Fix interaction with smacks hibernation
tmolitor <thilo@eightysoft.de>
parents:
2737
diff
changeset
|
87 if not st_name then return true; end -- nonzas are always important |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
88 if st_name == "presence" then |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
89 -- TODO check for MUC status codes? |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
90 return false; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
91 elseif st_name == "message" then |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
92 -- unpack carbon copies |
3481
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
93 local carbon, stanza_direction = extract_carbon(stanza); |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
94 --session.log("debug", "mod_csi_battery_saver(%s): stanza_direction = %s, carbon = %s, stanza = %s", id, stanza_direction, carbon and "true" or "false", tostring(stanza)); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
95 if carbon then stanza = carbon; end |
2946
dfac28504e86
mod_csi_battery_saver: Remove whitespace only lines
Michel Le Bihan <michel@lebihan.pl>
parents:
2757
diff
changeset
|
96 |
3481
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
97 local st_type = stanza.attr.type; |
2754
d1aa5fc005f7
mod_csi_battery_saver: Consider chat markers and outgoing carbon copies as important.
tmolitor <thilo@eightysoft.de>
parents:
2747
diff
changeset
|
98 -- headline message are always not important |
d1aa5fc005f7
mod_csi_battery_saver: Consider chat markers and outgoing carbon copies as important.
tmolitor <thilo@eightysoft.de>
parents:
2747
diff
changeset
|
99 if st_type == "headline" then return false; end |
2946
dfac28504e86
mod_csi_battery_saver: Remove whitespace only lines
Michel Le Bihan <michel@lebihan.pl>
parents:
2757
diff
changeset
|
100 |
2754
d1aa5fc005f7
mod_csi_battery_saver: Consider chat markers and outgoing carbon copies as important.
tmolitor <thilo@eightysoft.de>
parents:
2747
diff
changeset
|
101 -- chat markers (XEP-0333) are important, too, because some clients use them to update their notifications |
3481
1c8612d8db55
mod_csi_battery_saver: Remove patched stanza:find() method and reduce Carbons-related code
Matthew Wild <mwild1@gmail.com>
parents:
3109
diff
changeset
|
102 if stanza:child_with_ns("urn:xmpp:chat-markers:0") then return true; end; |
2946
dfac28504e86
mod_csi_battery_saver: Remove whitespace only lines
Michel Le Bihan <michel@lebihan.pl>
parents:
2757
diff
changeset
|
103 |
2754
d1aa5fc005f7
mod_csi_battery_saver: Consider chat markers and outgoing carbon copies as important.
tmolitor <thilo@eightysoft.de>
parents:
2747
diff
changeset
|
104 -- carbon copied outgoing messages are important (some clients update their notifications upon receiving those) --> don't return false here |
d1aa5fc005f7
mod_csi_battery_saver: Consider chat markers and outgoing carbon copies as important.
tmolitor <thilo@eightysoft.de>
parents:
2747
diff
changeset
|
105 --if carbon and stanza_direction == "out" then return false; end |
2946
dfac28504e86
mod_csi_battery_saver: Remove whitespace only lines
Michel Le Bihan <michel@lebihan.pl>
parents:
2757
diff
changeset
|
106 |
2754
d1aa5fc005f7
mod_csi_battery_saver: Consider chat markers and outgoing carbon copies as important.
tmolitor <thilo@eightysoft.de>
parents:
2747
diff
changeset
|
107 -- We can't check for body contents in encrypted messages, so let's treat them as important |
d1aa5fc005f7
mod_csi_battery_saver: Consider chat markers and outgoing carbon copies as important.
tmolitor <thilo@eightysoft.de>
parents:
2747
diff
changeset
|
108 -- Some clients don't even set a body or an empty body for encrypted messages |
2946
dfac28504e86
mod_csi_battery_saver: Remove whitespace only lines
Michel Le Bihan <michel@lebihan.pl>
parents:
2757
diff
changeset
|
109 |
2735
b5fae17e4403
mod_csi_battery_saver: correctly handle encrypted message stanzas
tmolitor <thilo@eightysoft.de>
parents:
2606
diff
changeset
|
110 -- check omemo https://xmpp.org/extensions/inbox/omemo.html |
b5fae17e4403
mod_csi_battery_saver: correctly handle encrypted message stanzas
tmolitor <thilo@eightysoft.de>
parents:
2606
diff
changeset
|
111 if stanza:get_child("encrypted", "eu.siacs.conversations.axolotl") or stanza:get_child("encrypted", "urn:xmpp:omemo:0") then return true; end |
2946
dfac28504e86
mod_csi_battery_saver: Remove whitespace only lines
Michel Le Bihan <michel@lebihan.pl>
parents:
2757
diff
changeset
|
112 |
2735
b5fae17e4403
mod_csi_battery_saver: correctly handle encrypted message stanzas
tmolitor <thilo@eightysoft.de>
parents:
2606
diff
changeset
|
113 -- check xep27 pgp https://xmpp.org/extensions/xep-0027.html |
b5fae17e4403
mod_csi_battery_saver: correctly handle encrypted message stanzas
tmolitor <thilo@eightysoft.de>
parents:
2606
diff
changeset
|
114 if stanza:get_child("x", "jabber:x:encrypted") then return true; end |
2946
dfac28504e86
mod_csi_battery_saver: Remove whitespace only lines
Michel Le Bihan <michel@lebihan.pl>
parents:
2757
diff
changeset
|
115 |
2735
b5fae17e4403
mod_csi_battery_saver: correctly handle encrypted message stanzas
tmolitor <thilo@eightysoft.de>
parents:
2606
diff
changeset
|
116 -- check xep373 pgp (OX) https://xmpp.org/extensions/xep-0373.html |
b5fae17e4403
mod_csi_battery_saver: correctly handle encrypted message stanzas
tmolitor <thilo@eightysoft.de>
parents:
2606
diff
changeset
|
117 if stanza:get_child("openpgp", "urn:xmpp:openpgp:0") then return true; end |
2946
dfac28504e86
mod_csi_battery_saver: Remove whitespace only lines
Michel Le Bihan <michel@lebihan.pl>
parents:
2757
diff
changeset
|
118 |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
119 local body = stanza:get_child_text("body"); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
120 if st_type == "groupchat" then |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
121 if stanza:get_child_text("subject") then return true; end |
2746
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
122 if body == nil or body == "" then return false; end |
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
123 -- body contains text, let's see if we want to process it further |
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
124 if filter_muc then |
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
125 if body:find(session.username, 1, true) then return true; end |
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
126 local rooms = session.rooms_joined; |
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
127 if not rooms then return false; end |
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
128 local room_nick = rooms[jid.bare(stanza_direction == "in" and stanza.attr.from or stanza.attr.to)]; |
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
129 if room_nick and body:find(room_nick, 1, true) then return true; end |
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
130 return false; |
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
131 else |
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
132 return true; |
d3a2f4bdaf09
mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents:
2745
diff
changeset
|
133 end |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
134 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
135 return body ~= nil and body ~= ""; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
136 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
137 return true; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
138 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
139 |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
140 module:hook("csi-client-inactive", function (event) |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
141 local session = event.origin; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
142 if session.pump then |
2745
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
143 session.log("debug", "mod_csi_battery_saver(%s): Client is inactive, buffering unimportant outgoing stanzas", id); |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
144 session.pump:pause(); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
145 else |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
146 session.log("debug", "mod_csi_battery_saver(%s): Client is inactive the first time, initializing module for this session", id); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
147 local pump = new_pump(session.send, 100); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
148 pump:pause(); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
149 session.pump = pump; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
150 session._pump_orig_send = session.send; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
151 function session.send(stanza) |
2745
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
152 session.log("debug", "mod_csi_battery_saver(%s): Got outgoing stanza: <%s>", id, tostring(stanza.name or stanza)); |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
153 local important = is_important(stanza, session); |
2742
2e30bb3a10d5
mod_csi_battery_saver: Fix a bug introduced by commit f43c77c69a8a
tmolitor <thilo@eightysoft.de>
parents:
2741
diff
changeset
|
154 -- clone stanzas before adding delay stamp and putting them into the queue |
2e30bb3a10d5
mod_csi_battery_saver: Fix a bug introduced by commit f43c77c69a8a
tmolitor <thilo@eightysoft.de>
parents:
2741
diff
changeset
|
155 if st.is_stanza(stanza) then stanza = st.clone(stanza); end |
2735
b5fae17e4403
mod_csi_battery_saver: correctly handle encrypted message stanzas
tmolitor <thilo@eightysoft.de>
parents:
2606
diff
changeset
|
156 -- add delay stamp to unimportant (buffered) stanzas that can/need be stamped |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
157 if not important and is_stamp_needed(stanza, session) then stanza = add_stamp(stanza, session); end |
2742
2e30bb3a10d5
mod_csi_battery_saver: Fix a bug introduced by commit f43c77c69a8a
tmolitor <thilo@eightysoft.de>
parents:
2741
diff
changeset
|
158 -- add stanza to outgoing queue and flush the buffer if needed |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
159 pump:push(stanza); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
160 if important then |
2741
69248dcd7cff
mod_csi_battery_saver: Fix interaction with smacks hibernation
tmolitor <thilo@eightysoft.de>
parents:
2737
diff
changeset
|
161 session.log("debug", "mod_csi_battery_saver(%s): Encountered important stanza, flushing buffer: <%s>", id, tostring(stanza.name or stanza)); |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
162 pump:flush(); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
163 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
164 return true; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
165 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
166 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
167 end); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
168 |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
169 module:hook("csi-client-active", function (event) |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
170 local session = event.origin; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
171 if session.pump then |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
172 session.log("debug", "mod_csi_battery_saver(%s): Client is active, resuming direct delivery", id); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
173 session.pump:resume(); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
174 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
175 end); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
176 |
3109
75930e4c2478
mod_csi_battery_saver: flush queue on smacks resume instead of smacks hibernation
tmolitor <thilo@eightysoft.de>
parents:
2946
diff
changeset
|
177 -- clean up this session on hibernation end |
2745
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
178 -- but don't change resumed.send(), it is already overwritten with session.send() by the smacks module |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
179 module:hook("smacks-hibernation-end", function (event) |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
180 local session = event.resumed; |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
181 if session.pump then |
3109
75930e4c2478
mod_csi_battery_saver: flush queue on smacks resume instead of smacks hibernation
tmolitor <thilo@eightysoft.de>
parents:
2946
diff
changeset
|
182 session.log("debug", "mod_csi_battery_saver(%s): Hibernation ended, flushing buffer and afterwards disabling for this session", id); |
2745
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
183 session.pump:flush(session.send); -- use the fresh session.send() introduced by the smacks resume |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
184 -- don't reset session.send() because this is not the send previously overwritten by this module, but a fresh one |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
185 -- session.send = session._pump_orig_send; |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
186 session.pump = nil; |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
187 session._pump_orig_send = nil; |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
188 end |
3109
75930e4c2478
mod_csi_battery_saver: flush queue on smacks resume instead of smacks hibernation
tmolitor <thilo@eightysoft.de>
parents:
2946
diff
changeset
|
189 end, 1000); -- high priority to prevent message reordering on resumption (we want to flush our buffers *first*) |
2741
69248dcd7cff
mod_csi_battery_saver: Fix interaction with smacks hibernation
tmolitor <thilo@eightysoft.de>
parents:
2737
diff
changeset
|
190 |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
191 function module.unload() |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
192 module:log("info", "%s: Unloading module, flushing all buffers", id); |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
193 local host_sessions = prosody.hosts[module.host].sessions; |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
194 for _, user in pairs(host_sessions) do |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
195 for _, session in pairs(user.sessions) do |
2745
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
196 if session.pump then |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
197 session.log("debug", "mod_csi_battery_saver(%s): Flushing buffer and restoring to original session.send()", id); |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
198 session.pump:flush(); |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
199 session.send = session._pump_orig_send; |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
200 session.pump = nil; |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
201 session._pump_orig_send = nil; |
b62cec32680e
mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents:
2742
diff
changeset
|
202 end |
2606
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
203 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
204 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
205 end |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
206 |
538c54d2dab3
mod_csi_battery_saver: CSI module to save battery on mobile devices, based on mod_csi_pump
tmolitor <thilo@eightysoft.de>
parents:
diff
changeset
|
207 module:log("info", "%s: Successfully loaded module", id); |