annotate mod_muclogging/mod_muclogging.lua @ 47:99ff520519fe

mod_muclogging: initial checkin
author Thilo Cestonaro <thilo@cestona.ro>
date Fri, 16 Oct 2009 23:46:29 +0200
parents
children a96d3f37d845
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
1 -- Copyright (C) 2009 Thilo Cestonaro
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
2 --
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
3 -- This project is MIT/X11 licensed. Please see the
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
4 -- COPYING file in the source package for more information.
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
5 --
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
6 local prosody = prosody;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
7 local splitJid = require "util.jid".split;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
8 local bareJid = require "util.jid".bare;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
9 local config_get = require "core.configmanager".get;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
10
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
11 function logIfNeeded(e)
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
12 local stanza, origin = e.stanza, e.origin;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
13 if (stanza.name == "presence") or
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
14 (stanza.name == "message" and tostring(stanza.attr.type) == "groupchat")
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
15 then
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
16 local node, host, resource = splitJid(stanza.attr.to);
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
17 if node ~= nil and host ~= nil then
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
18 local bare = node .. "@" .. host;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
19 if prosody.hosts[host] ~= nil and prosody.hosts[host].muc ~= nil and prosody.hosts[host].muc.rooms[bare] ~= nil then
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
20 local room = prosody.hosts[host].muc.rooms[bare]
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
21 local logFolder = config_get(host, "core", "logFolder");
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
22 if logFolder ~= nil then
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
23 local today = os.date("%y%m%d");
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
24 local now = os.date("%X")
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
25 local fn = logFolder .. "/" .. today .. "_" .. bare .. ".log";
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
26 local mucFrom = nil;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
27
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
28 if stanza.name == "presence" and stanza.attr.type == nil then
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
29 mucFrom = stanza.attr.to;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
30 else
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
31 for jid, nick in pairs(room._jid_nick) do
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
32 if jid == stanza.attr.from then
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
33 mucFrom = nick;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
34 end
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
35 end
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
36 end
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
37
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
38 if mucFrom ~= nil then
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
39 module:log("debug", "try to open room log: %s", fn);
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
40 local f = assert(io.open(fn, "a"));
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
41 local realFrom = stanza.attr.from;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
42 local realTo = stanza.attr.to;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
43 stanza.attr.from = mucFrom;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
44 stanza.attr.to = nil;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
45 f:write("<stanza time=\"".. now .. "\">" .. tostring(stanza) .. "</stanza>\n");
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
46 stanza.attr.from = realFrom;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
47 stanza.attr.to = realTo;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
48 f:close()
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
49 end
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
50 end
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
51 end
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
52 end
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
53 end
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
54 return;
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
55 end
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
56
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
57 module:hook("pre-message/bare", logIfNeeded, 500);
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
58 module:hook("pre-presence/full", logIfNeeded, 500);
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
59
99ff520519fe mod_muclogging: initial checkin
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
60 module:log("debug", "loaded ...");