Software /
code /
prosody-modules
Annotate
mod_csi_battery_saver/mod_csi_battery_saver.lua @ 2660:796ace2c8f9d
mod_storage_memory: Inclued 'with' in search [luacheck]
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 30 Mar 2017 23:46:13 +0200 |
parent | 2606:538c54d2dab3 |
child | 2735:b5fae17e4403 |
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 |
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
|
5 module:depends"csi" |
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
|
6 module:depends"track_muc_joins" |
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 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
|
8 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
|
9 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
|
10 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
|
11 local datetime = require "util.datetime"; |
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 |
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 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
|
14 |
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 -- 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
|
16 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
|
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 -- Patched version of util.stanza:find() that supports giving stanza names |
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 -- without their namespace, allowing for every namespace. |
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 function find(self, path) |
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 local pos = 1; |
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
|
22 local len = #path + 1; |
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
|
23 |
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
|
24 repeat |
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
|
25 local xmlns, name, text; |
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
|
26 local char = s_sub(path, pos, pos); |
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
|
27 if char == "@" 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
|
28 return self.attr[s_sub(path, pos + 1)]; |
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
|
29 elseif char == "{" 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
|
30 xmlns, pos = s_match(path, "^([^}]+)}()", pos + 1); |
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
|
31 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
|
32 name, text, pos = s_match(path, "^([^@/#]*)([/#]?)()", pos); |
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 name = name ~= "" and 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
|
34 if pos == len 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
|
35 if text == "#" 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
|
36 local child = xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name); |
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 return child and child:get_text() 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
|
38 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
|
39 return xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name); |
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 self = xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name); |
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 until not 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
|
43 return 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
|
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 |
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 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
|
47 -- 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 function q:push(item) |
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 local ok = push(self, item); |
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 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 function 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
|
69 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
|
70 while item 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
|
71 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
|
72 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
|
73 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 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
|
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 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
|
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 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 |
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 local function add_stamp(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
|
91 stanza = stanza:tag("delay", { xmlns = xmlns_delay, from = session.host, stamp = datetime.datetime()}); |
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 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
|
93 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
|
94 |
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 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
|
96 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
|
97 if not st_name 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
|
98 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
|
99 -- 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
|
100 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
|
101 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
|
102 -- unpack carbon copies |
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
|
103 local stanza_direction = "in"; |
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
|
104 local carbon; |
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
|
105 -- support carbon copied message stanzas having an arbitrary message-namespace or no message-namespace at all |
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
|
106 if not carbon then carbon = find(stanza, "{urn:xmpp:carbons:2}/forwarded/message"); 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
|
107 if not carbon then carbon = find(stanza, "{urn:xmpp:carbons:1}/forwarded/message"); 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
|
108 stanza_direction = carbon and stanza:child_with_name("sent") and "out" or "in"; |
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 --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
|
110 if carbon then stanza = carbon; 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
|
111 -- carbon copied outgoing messages aren't important (but incoming carbon copies are!) |
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
|
112 if carbon and stanza_direction == "out" 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
|
113 |
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 local st_type = stanza.attr.type; |
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
|
115 if st_type == "headline" 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 if stanza:get_child_text("subject") 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
|
121 if not body 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
|
122 if body:find(session.username, 1, true) 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
|
123 local rooms = session.rooms_joined; |
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
|
124 if not rooms 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
|
125 local room_nick = rooms[jid.bare(stanza_direction == "in" and stanza.attr.from or stanza.attr.to)]; |
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
|
126 if room_nick and body:find(room_nick, 1, true) 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
|
127 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
|
128 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
|
129 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
|
130 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
|
131 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
|
132 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
|
133 |
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 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 function session.send(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
|
145 session.log("debug", "mod_csi_battery_saver(%s): Got stanza: <%s>", id, tostring(stanza.name)); |
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 local important = 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
|
147 -- add delay stamp to unimported (buffered) stanzas that can/need be stamped |
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 if not important and is_stamp_needed(stanza, session) then stanza = add_stamp(stanza, session); 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
|
149 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
|
150 if important 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
|
151 session.log("debug", "mod_csi_battery_saver(%s): Encountered important stanza, flushing buffer: <%s>", id, tostring(stanza.name)); |
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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 session.log("debug", "mod_csi_battery_saver(%s): Client is inactive, buffering unimportant stanzas", 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
|
158 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
|
159 |
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 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
|
161 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
|
162 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
|
163 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
|
164 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
|
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 |
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 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
|
169 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
|
170 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
|
171 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
|
172 for _, session in pairs(user.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
|
173 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
|
174 session.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
|
175 session.send = session._pump_orig_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
|
176 session.pump = 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
|
177 session._pump_orig_send = 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
|
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 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
|
180 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
|
181 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
|
182 |
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
|
183 module:log("info", "%s: Successfully loaded module", id); |