Annotate

mod_csi_battery_saver/mod_csi_battery_saver.lua @ 5941:5c4e102e2563 default tip

mod_pubsub_serverinfo: fix bool logic when reading config
author Guus der Kinderen <guus.der.kinderen@gmail.com>
date Mon, 03 Jun 2024 12:52:26 +0200
parent 5918:423163b65bb1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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);
3633
6b0db0f2d57a mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
tmolitor <thilo@eightysoft.de>
parents: 3490
diff changeset
6 local queue_size = module:get_option_number("csi_battery_saver_queue_size", 256);
2747
a688b31295ea mod_csi_battery_saver: only depend on mod_track_muc_joins if actually used
tmolitor <thilo@eightysoft.de>
parents: 2746
diff changeset
7
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
8 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
9 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
10 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
11 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
12 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
13 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
14 local datetime = require "util.datetime";
2741
69248dcd7cff mod_csi_battery_saver: Fix interaction with smacks hibernation
tmolitor <thilo@eightysoft.de>
parents: 2737
diff changeset
15 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
16
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 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
18
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 -- 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
20 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
21
2746
d3a2f4bdaf09 mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents: 2745
diff changeset
22
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
23 -- 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
24 -- 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
25 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
26 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
27 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
28 local direction = carbon.name == "sent" and "out" or "in";
3490
972b21d34306 mod_csi_battery_saver: fix typo in util.stanza:get_child()
tmolitor <thilo@eightysoft.de>
parents: 3481
diff changeset
29 local forward = carbon:get_child("forwarded", "urn:xmpp:forward:0");
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
30 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
31 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
32 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
33 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
34
3633
6b0db0f2d57a mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
tmolitor <thilo@eightysoft.de>
parents: 3490
diff changeset
35 local function new_pump(session, 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
36 -- 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45 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
46 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
47 function q:push(item)
2741
69248dcd7cff mod_csi_battery_saver: Fix interaction with smacks hibernation
tmolitor <thilo@eightysoft.de>
parents: 2737
diff changeset
48 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
49 if not ok then
3633
6b0db0f2d57a mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
tmolitor <thilo@eightysoft.de>
parents: 3490
diff changeset
50 session.log("debug", "mod_csi_battery_saver(%s): Queue full (%d items), forcing flush...", id, q:count());
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
51 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
52 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
53 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
54 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
55 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
56 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
57 end
2745
b62cec32680e mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents: 2742
diff changeset
58 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
59 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
60 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
61 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
62 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
63 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
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 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
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 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
68 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
69
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 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
71 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
72 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
73 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
74 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
75 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
76 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
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 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
79 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
80
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
81 local function add_stamp(stanza, session)
2757
595d9d68fd11 mod_csi_battery_saver: Fix #921
tmolitor <thilo@eightysoft.de>
parents: 2754
diff changeset
82 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
83 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
84 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
85 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
86
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
87 local function is_important(stanza, session)
5918
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
88 -- some special handlings
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
89 if stanza == " " then -- whitespace keepalive
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
90 return true;
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
91 elseif type(stanza) == "string" then -- raw data
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
92 return true;
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
93 elseif not st.is_stanza(stanza) then -- this should probably never happen
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
94 return true;
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
95 end
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
96 if stanza.attr.xmlns ~= nil then -- nonzas (stream errors, stream management etc.)
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
97 return true;
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
98 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
99 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
100 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
101 if st_name == "presence" then
5825
f6a2602129c8 mod_csi_battery_saver: mark some presences as important
tmolitor <thilo@eightysoft.de>
parents: 4969
diff changeset
102 local st_type = stanza.attr.type;
f6a2602129c8 mod_csi_battery_saver: mark some presences as important
tmolitor <thilo@eightysoft.de>
parents: 4969
diff changeset
103 -- subscription requests are important
f6a2602129c8 mod_csi_battery_saver: mark some presences as important
tmolitor <thilo@eightysoft.de>
parents: 4969
diff changeset
104 if st_type == "subscribe" then return true; end
f6a2602129c8 mod_csi_battery_saver: mark some presences as important
tmolitor <thilo@eightysoft.de>
parents: 4969
diff changeset
105 -- muc status codes are important, too
f6a2602129c8 mod_csi_battery_saver: mark some presences as important
tmolitor <thilo@eightysoft.de>
parents: 4969
diff changeset
106 local muc_x = stanza:get_child("x", "http://jabber.org/protocol/muc#user")
f6a2602129c8 mod_csi_battery_saver: mark some presences as important
tmolitor <thilo@eightysoft.de>
parents: 4969
diff changeset
107 local muc_status = muc_x and muc_x:get_child("status") or nil
f6a2602129c8 mod_csi_battery_saver: mark some presences as important
tmolitor <thilo@eightysoft.de>
parents: 4969
diff changeset
108 if muc_status and muc_status.attr.code then return true; 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
109 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
110 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
111 -- 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
112 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
113 --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
114 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
115
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
116 local st_type = stanza.attr.type;
3980
19c5bfc3a241 mod_csi_battery_saver: Add Jingle incoming call messages to important, thanks to Wiktor Kwapisiewicz <wiktor@metacode.biz>
tmolitor <thilo@eightysoft.de>
parents: 3633
diff changeset
117
5918
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
118 -- errors are always important
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
119 if st_type == "error" then return true; end;
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
120
5916
433a4dd1dc2a mod_csi_battery_saver: MDS headline pushes are important
tmolitor <thilo@eightysoft.de>
parents: 5825
diff changeset
121 -- headline message are always not important, with some exceptions
433a4dd1dc2a mod_csi_battery_saver: MDS headline pushes are important
tmolitor <thilo@eightysoft.de>
parents: 5825
diff changeset
122 if st_type == "headline" then
5917
5db53d619e66 mod_csi_battery_saver: add xep number to mds comment
tmolitor <thilo@eightysoft.de>
parents: 5916
diff changeset
123 -- allow headline pushes of mds updates (XEP-0490)
5916
433a4dd1dc2a mod_csi_battery_saver: MDS headline pushes are important
tmolitor <thilo@eightysoft.de>
parents: 5825
diff changeset
124 if stanza:find("{http://jabber.org/protocol/pubsub#event}event/items@node") == "urn:xmpp:mds:displayed:0" then return true; end;
433a4dd1dc2a mod_csi_battery_saver: MDS headline pushes are important
tmolitor <thilo@eightysoft.de>
parents: 5825
diff changeset
125 return false
433a4dd1dc2a mod_csi_battery_saver: MDS headline pushes are important
tmolitor <thilo@eightysoft.de>
parents: 5825
diff changeset
126 end
2946
dfac28504e86 mod_csi_battery_saver: Remove whitespace only lines
Michel Le Bihan <michel@lebihan.pl>
parents: 2757
diff changeset
127
5918
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
128 -- mediated muc invites
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
129 if stanza:find("{http://jabber.org/protocol/muc#user}x/invite") then return true; end;
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
130 if stanza:get_child("x", "jabber:x:conference") then return true; end;
423163b65bb1 mod_csi_battery_saver: Some more improvements (handling of errors, muc invites, special data)
tmolitor <thilo@eightysoft.de>
parents: 5917
diff changeset
131
2754
d1aa5fc005f7 mod_csi_battery_saver: Consider chat markers and outgoing carbon copies as important.
tmolitor <thilo@eightysoft.de>
parents: 2747
diff changeset
132 -- 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
133 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
134
3980
19c5bfc3a241 mod_csi_battery_saver: Add Jingle incoming call messages to important, thanks to Wiktor Kwapisiewicz <wiktor@metacode.biz>
tmolitor <thilo@eightysoft.de>
parents: 3633
diff changeset
135 -- XEP-0353: Jingle Message Initiation incoming call messages
19c5bfc3a241 mod_csi_battery_saver: Add Jingle incoming call messages to important, thanks to Wiktor Kwapisiewicz <wiktor@metacode.biz>
tmolitor <thilo@eightysoft.de>
parents: 3633
diff changeset
136 if stanza:child_with_ns("urn:xmpp:jingle-message:0") then return true; end
4969
889e1695e935 mod_csi_battery_saver: Add support for urn:xmpp:jingle-message:1 (XEP-0353)
tmolitor <thilo@eightysoft.de>
parents: 4048
diff changeset
137 if stanza:child_with_ns("urn:xmpp:jingle-message:1") then return true; end
3980
19c5bfc3a241 mod_csi_battery_saver: Add Jingle incoming call messages to important, thanks to Wiktor Kwapisiewicz <wiktor@metacode.biz>
tmolitor <thilo@eightysoft.de>
parents: 3633
diff changeset
138
2754
d1aa5fc005f7 mod_csi_battery_saver: Consider chat markers and outgoing carbon copies as important.
tmolitor <thilo@eightysoft.de>
parents: 2747
diff changeset
139 -- 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
140 --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
141
2754
d1aa5fc005f7 mod_csi_battery_saver: Consider chat markers and outgoing carbon copies as important.
tmolitor <thilo@eightysoft.de>
parents: 2747
diff changeset
142 -- 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
143 -- 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
144
2735
b5fae17e4403 mod_csi_battery_saver: correctly handle encrypted message stanzas
tmolitor <thilo@eightysoft.de>
parents: 2606
diff changeset
145 -- 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
146 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
147
2735
b5fae17e4403 mod_csi_battery_saver: correctly handle encrypted message stanzas
tmolitor <thilo@eightysoft.de>
parents: 2606
diff changeset
148 -- 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
149 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
150
2735
b5fae17e4403 mod_csi_battery_saver: correctly handle encrypted message stanzas
tmolitor <thilo@eightysoft.de>
parents: 2606
diff changeset
151 -- 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
152 if stanza:get_child("openpgp", "urn:xmpp:openpgp:0") then return true; end
5825
f6a2602129c8 mod_csi_battery_saver: mark some presences as important
tmolitor <thilo@eightysoft.de>
parents: 4969
diff changeset
153
3633
6b0db0f2d57a mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
tmolitor <thilo@eightysoft.de>
parents: 3490
diff changeset
154 -- check eme
6b0db0f2d57a mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
tmolitor <thilo@eightysoft.de>
parents: 3490
diff changeset
155 if stanza:get_child("encryption", "urn:xmpp:eme: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
156
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 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
158 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
159 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
160 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
161 -- body contains text, let's see if we want to process it further
3633
6b0db0f2d57a mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
tmolitor <thilo@eightysoft.de>
parents: 3490
diff changeset
162 if not filter_muc then -- default case
6b0db0f2d57a mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
tmolitor <thilo@eightysoft.de>
parents: 3490
diff changeset
163 local stanza_important = module:fire_event("csi-is-stanza-important", { stanza = stanza, session = session });
6b0db0f2d57a mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
tmolitor <thilo@eightysoft.de>
parents: 3490
diff changeset
164 if stanza_important ~= nil then return stanza_important; end
6b0db0f2d57a mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
tmolitor <thilo@eightysoft.de>
parents: 3490
diff changeset
165 return true; -- deemed unknown/high priority by mod_csi_muc_priorities or some other module
6b0db0f2d57a mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
tmolitor <thilo@eightysoft.de>
parents: 3490
diff changeset
166 else
2746
d3a2f4bdaf09 mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents: 2745
diff changeset
167 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
168 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
169 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
170 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
171 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
172 return false;
d3a2f4bdaf09 mod_csi_battery_saver: Add config option for better muc handling
tmolitor <thilo@eightysoft.de>
parents: 2745
diff changeset
173 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
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 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
176 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
177 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
178 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
179
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
180 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
181 local session = event.origin;
4048
64b7daa6c42c mod_csi_battery_saver: Ignore CSI actions before resource bind
tmolitor <thilo@eightysoft.de>
parents: 3980
diff changeset
182 if not session.resource then
64b7daa6c42c mod_csi_battery_saver: Ignore CSI actions before resource bind
tmolitor <thilo@eightysoft.de>
parents: 3980
diff changeset
183 session.log("warn", "Ignoring csi if no resource is bound!");
64b7daa6c42c mod_csi_battery_saver: Ignore CSI actions before resource bind
tmolitor <thilo@eightysoft.de>
parents: 3980
diff changeset
184 return;
64b7daa6c42c mod_csi_battery_saver: Ignore CSI actions before resource bind
tmolitor <thilo@eightysoft.de>
parents: 3980
diff changeset
185 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
186 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
187 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
188 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
189 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
190 session.log("debug", "mod_csi_battery_saver(%s): Client is inactive the first time, initializing module for this session", id);
3633
6b0db0f2d57a mod_csi_battery_saver: add compatibility with mod_csi_muc_priorities, make queue length configurable and update README
tmolitor <thilo@eightysoft.de>
parents: 3490
diff changeset
191 local pump = new_pump(session, session.send, queue_size);
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
192 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
193 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
194 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
195 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
196 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
197 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
198 -- 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
199 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
200 -- 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
201 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
202 -- 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
203 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
204 if important then
2741
69248dcd7cff mod_csi_battery_saver: Fix interaction with smacks hibernation
tmolitor <thilo@eightysoft.de>
parents: 2737
diff changeset
205 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
206 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
207 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
208 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
209 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
210 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
211 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
212
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
213 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
214 local session = event.origin;
4048
64b7daa6c42c mod_csi_battery_saver: Ignore CSI actions before resource bind
tmolitor <thilo@eightysoft.de>
parents: 3980
diff changeset
215 if not session.resource then
64b7daa6c42c mod_csi_battery_saver: Ignore CSI actions before resource bind
tmolitor <thilo@eightysoft.de>
parents: 3980
diff changeset
216 session.log("warn", "Ignoring csi if no resource is bound!");
64b7daa6c42c mod_csi_battery_saver: Ignore CSI actions before resource bind
tmolitor <thilo@eightysoft.de>
parents: 3980
diff changeset
217 return;
64b7daa6c42c mod_csi_battery_saver: Ignore CSI actions before resource bind
tmolitor <thilo@eightysoft.de>
parents: 3980
diff changeset
218 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
219 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
220 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
221 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
222 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
223 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
224
3109
75930e4c2478 mod_csi_battery_saver: flush queue on smacks resume instead of smacks hibernation
tmolitor <thilo@eightysoft.de>
parents: 2946
diff changeset
225 -- 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
226 -- 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
227 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
228 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
229 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
230 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
231 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
232 -- 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
233 -- 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
234 session.pump = nil;
b62cec32680e mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents: 2742
diff changeset
235 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
236 end
3109
75930e4c2478 mod_csi_battery_saver: flush queue on smacks resume instead of smacks hibernation
tmolitor <thilo@eightysoft.de>
parents: 2946
diff changeset
237 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
238
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
239 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
240 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
241 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
242 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
243 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
244 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
245 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
246 session.pump:flush();
b62cec32680e mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents: 2742
diff changeset
247 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
248 session.pump = nil;
b62cec32680e mod_csi_battery_saver: Fix bug when smacks is resumed before hibernating
tmolitor <thilo@eightysoft.de>
parents: 2742
diff changeset
249 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
250 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
251 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
252 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
253 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
254
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
255 module:log("info", "%s: Successfully loaded module", id);