comparison mod_muc_log/mod_muc_log.lua @ 1032:b69e5d63a4fe

mod_muc_log, mod_muc_log_http: backport changes from Metronome.
author Marco Cirillo <maranda@lightwitch.org>
date Sat, 01 Jun 2013 23:37:39 +0200
parents 290c21a5e0ee
children a0fbe738317c
comparison
equal deleted inserted replaced
1031:6b34cc81e15d 1032:b69e5d63a4fe
1 local prosody = prosody; 1 local prosody = prosody;
2 local hosts = prosody.hosts; 2 local hosts = prosody.hosts;
3 local tostring = tostring; 3 local tostring = tostring;
4 local splitJid = require "util.jid".split; 4 local split_jid = require "util.jid".split;
5 local cm = require "core.configmanager"; 5 local cm = require "core.configmanager";
6 local datamanager = require "util.datamanager"; 6 local datamanager = require "util.datamanager";
7 local data_load, data_store, data_getpath = datamanager.load, datamanager.store, datamanager.getpath; 7 local data_load, data_store, data_getpath = datamanager.load, datamanager.store, datamanager.getpath;
8 local datastore = "muc_log"; 8 local datastore = "muc_log";
9 local error_reply = require "util.stanza".error_reply; 9 local error_reply = require "util.stanza".error_reply;
10 local storagemanager = storagemanager; 10 local storagemanager = storagemanager;
11 11
12 local mod_host = module:get_host(); 12 local mod_host = module:get_host();
13 local config = nil; 13 local log_presences = module:get_option_boolean("muc_log_presences", false);
14 14
15 -- Helper Functions 15 -- Helper Functions
16 16
17 local function inject_storage_config() 17 local function inject_storage_config()
18 local _storage = cm.getconfig()[mod_host].storage; 18 local _storage = cm.getconfig()[mod_host].storage;
28 storagemanager.get_driver(mod_host, "muc_log"); -- init 28 storagemanager.get_driver(mod_host, "muc_log"); -- init
29 end 29 end
30 30
31 -- Module Definitions 31 -- Module Definitions
32 32
33 function logIfNeeded(e) 33 function log_if_needed(e)
34 local stanza, origin = e.stanza, e.origin; 34 local stanza, origin = e.stanza, e.origin;
35 35
36 if (stanza.name == "presence") or 36 if (stanza.name == "presence") or
37 (stanza.name == "iq") or 37 (stanza.name == "iq") or
38 (stanza.name == "message" and tostring(stanza.attr.type) == "groupchat") 38 (stanza.name == "message" and tostring(stanza.attr.type) == "groupchat")
39 then 39 then
40 local node, host = splitJid(stanza.attr.to); 40 local node, host = split_jid(stanza.attr.to);
41 local muc = hosts[host].muc; 41 local muc = hosts[host].muc;
42 if node and host then 42 if node and host then
43 local bare = node .. "@" .. host; 43 local bare = node .. "@" .. host;
44 if muc and muc.rooms[bare] then 44 if muc and muc.rooms[bare] then
45 local room = muc.rooms[bare] 45 local room = muc.rooms[bare]
46 local today = os.date("%y%m%d"); 46 local today = os.date("%y%m%d");
47 local now = os.date("%X") 47 local now = os.date("%X")
48 local mucTo = nil 48 local muc_to = nil
49 local mucFrom = nil; 49 local muc_from = nil;
50 local alreadyJoined = false; 50 local already_joined = false;
51 51
52 if room._data.hidden then -- do not log any data of private rooms 52 if room._data.hidden then -- do not log any data of private rooms
53 return; 53 return;
54 end 54 end
55 if not room._data.logging then -- do not log where logging is not enabled
56 return;
57 end
55 58
56 if stanza.name == "presence" and stanza.attr.type == nil then 59 if stanza.name == "presence" and stanza.attr.type == nil then
57 mucFrom = stanza.attr.to; 60 muc_from = stanza.attr.to;
58 if room._occupants and room._occupants[stanza.attr.to] then -- if true, the user has already joined the room 61 if room._occupants and room._occupants[stanza.attr.to] then
59 alreadyJoined = true; 62 already_joined = true;
60 stanza:tag("alreadyJoined"):text("true"); -- we need to log the information that the user has already joined, so add this and remove after logging 63 stanza:tag("alreadyJoined"):text("true");
61 end 64 end
62 elseif stanza.name == "iq" and stanza.attr.type == "set" then -- kick, to is the room, from is the admin, nick who is kicked is attr of iq->query->item 65 elseif stanza.name == "iq" and stanza.attr.type == "set" then -- kick, to is the room, from is the admin, nick who is kicked is attr of iq->query->item
63 if stanza.tags[1] and stanza.tags[1].name == "query" then 66 if stanza.tags[1] and stanza.tags[1].name == "query" then
64 local tmp = stanza.tags[1]; 67 local tmp = stanza.tags[1];
65 if tmp.tags[1] ~= nil and tmp.tags[1].name == "item" and tmp.tags[1].attr.nick then 68 if tmp.tags[1] ~= nil and tmp.tags[1].name == "item" and tmp.tags[1].attr.nick then
66 tmp = tmp.tags[1]; 69 tmp = tmp.tags[1];
67 for jid, nick in pairs(room._jid_nick) do 70 for jid, nick in pairs(room._jid_nick) do
68 if nick == stanza.attr.to .. "/" .. tmp.attr.nick then 71 if nick == stanza.attr.to .. "/" .. tmp.attr.nick then
69 mucTo = nick; 72 muc_to = nick;
70 break; 73 break;
71 end 74 end
72 end 75 end
73 end 76 end
74 end 77 end
75 else 78 else
76 for jid, nick in pairs(room._jid_nick) do 79 for jid, nick in pairs(room._jid_nick) do
77 if jid == stanza.attr.from then 80 if jid == stanza.attr.from then
78 mucFrom = nick; 81 muc_from = nick;
79 break; 82 break;
80 end 83 end
81 end 84 end
82 end 85 end
83 86
84 if (mucFrom or mucTo) then 87 if (muc_from or muc_to) then
85 local data = data_load(node, host, datastore .. "/" .. today); 88 local data = data_load(node, host, datastore .. "/" .. today);
86 local realFrom = stanza.attr.from; 89 local realFrom = stanza.attr.from;
87 local realTo = stanza.attr.to; 90 local realTo = stanza.attr.to;
88 91
89 if data == nil then 92 if data == nil then
90 data = {}; 93 data = {};
91 end 94 end
92 95
93 stanza.attr.from = mucFrom; 96 stanza.attr.from = muc_from;
94 stanza.attr.to = mucTo; 97 stanza.attr.to = muc_to;
95 data[#data + 1] = "<stanza time=\"".. now .. "\">" .. tostring(stanza) .. "</stanza>\n"; 98 data[#data + 1] = "<stanza time=\"".. now .. "\">" .. tostring(stanza) .. "</stanza>\n";
96 stanza.attr.from = realFrom; 99 stanza.attr.from = realFrom;
97 stanza.attr.to = realTo; 100 stanza.attr.to = realTo;
98 if alreadyJoined == true then 101 if already_joined == true then
99 if stanza[#stanza].name == "alreadyJoined" then -- normaly the faked element should be the last, remove it when it is the last 102 if stanza[#stanza].name == "alreadyJoined" then -- normaly the faked element should be the last, remove it when it is the last
100 stanza[#stanza] = nil; 103 stanza[#stanza] = nil;
101 else 104 else
102 for i = 1, #stanza, 1 do 105 for i = 1, #stanza, 1 do
103 if stanza[i].name == "alreadyJoined" then -- remove the faked element 106 if stanza[i].name == "alreadyJoined" then -- remove the faked element
112 end 115 end
113 end 116 end
114 end 117 end
115 end 118 end
116 119
117 module:hook("message/bare", logIfNeeded, 500); 120 module:hook("message/bare", log_if_needed, 50);
118 module:hook("iq/bare", logIfNeeded, 500); 121 module:hook("iq/bare", log_if_needed, 50);
119 module:hook("presence/full", logIfNeeded, 500); 122 if log_presences then module:hook("presence/full", log_if_needed, 50); end
120 123
121 local function reload() 124 local function reload()
122 inject_storage_config(); 125 inject_storage_config();
123 end 126 end
124 127