comparison 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
parents 75930e4c2478
children 972b21d34306
comparison
equal deleted inserted replaced
3480:5911030a1a66 3481:1c8612d8db55
17 17
18 -- a log id for this module instance 18 -- a log id for this module instance
19 local id = s_sub(require "util.hashes".sha256(datetime.datetime(), true), 1, 4); 19 local id = s_sub(require "util.hashes".sha256(datetime.datetime(), true), 1, 4);
20 20
21 21
22 -- Patched version of util.stanza:find() that supports giving stanza names 22 -- Returns a forwarded message, and either "in" or "out" depending on the direction
23 -- without their namespace, allowing for every namespace. 23 -- Returns nil if the message is not a carbon
24 local function find(self, path) 24 local function extract_carbon(stanza)
25 local pos = 1; 25 local carbon = stanza:child_with_ns("urn:xmpp:carbons:2") or stanza:child_with_ns("urn:xmpp:carbons:1");
26 local len = #path + 1; 26 if not carbon then return; end
27 27 local direction = carbon.name == "sent" and "out" or "in";
28 repeat 28 local forward = carbon:get_child("urn:xmpp:forward:0", "forwarded");
29 local xmlns, name, text; 29 local message = forward and forward:child_with_name("message") or nil;
30 local char = s_sub(path, pos, pos); 30 if not message then return; end
31 if char == "@" then 31 return message, direction;
32 return self.attr[s_sub(path, pos + 1)];
33 elseif char == "{" then
34 xmlns, pos = s_match(path, "^([^}]+)}()", pos + 1);
35 end
36 name, text, pos = s_match(path, "^([^@/#]*)([/#]?)()", pos);
37 name = name ~= "" and name or nil;
38 if pos == len then
39 if text == "#" then
40 local child = xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name);
41 return child and child:get_text() or nil;
42 end
43 return xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name);
44 end
45 self = xmlns ~= nil and self:get_child(name, xmlns) or self:child_with_name(name);
46 until not self
47 return nil;
48 end 32 end
49 33
50 local function new_pump(output, ...) 34 local function new_pump(output, ...)
51 -- luacheck: ignore 212/self 35 -- luacheck: ignore 212/self
52 local q = new_queue(...); 36 local q = new_queue(...);
104 if st_name == "presence" then 88 if st_name == "presence" then
105 -- TODO check for MUC status codes? 89 -- TODO check for MUC status codes?
106 return false; 90 return false;
107 elseif st_name == "message" then 91 elseif st_name == "message" then
108 -- unpack carbon copies 92 -- unpack carbon copies
109 local stanza_direction = "in"; 93 local carbon, stanza_direction = extract_carbon(stanza);
110 local carbon;
111 local st_type;
112 -- support carbon copied message stanzas having an arbitrary message-namespace or no message-namespace at all
113 if not carbon then carbon = find(stanza, "{urn:xmpp:carbons:2}/forwarded/message"); end
114 if not carbon then carbon = find(stanza, "{urn:xmpp:carbons:1}/forwarded/message"); end
115 stanza_direction = carbon and stanza:child_with_name("sent") and "out" or "in";
116 --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)); 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));
117 if carbon then stanza = carbon; end 95 if carbon then stanza = carbon; end
118 st_type = stanza.attr.type; 96
119 97 local st_type = stanza.attr.type;
120 -- headline message are always not important 98 -- headline message are always not important
121 if st_type == "headline" then return false; end 99 if st_type == "headline" then return false; end
122 100
123 -- chat markers (XEP-0333) are important, too, because some clients use them to update their notifications 101 -- chat markers (XEP-0333) are important, too, because some clients use them to update their notifications
124 if find(stanza, "{urn:xmpp:chat-markers:0}") then return true; end; 102 if stanza:child_with_ns("urn:xmpp:chat-markers:0") then return true; end;
125 103
126 -- carbon copied outgoing messages are important (some clients update their notifications upon receiving those) --> don't return false here 104 -- carbon copied outgoing messages are important (some clients update their notifications upon receiving those) --> don't return false here
127 --if carbon and stanza_direction == "out" then return false; end 105 --if carbon and stanza_direction == "out" then return false; end
128 106
129 -- We can't check for body contents in encrypted messages, so let's treat them as important 107 -- We can't check for body contents in encrypted messages, so let's treat them as important