Software /
code /
prosody-modules
Comparison
mod_muc_log/mod_muc_log.lua @ 976:0428009c1127
mod_muc_log: some cleanup and code refactor also force the plugin storage driver being internal.
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Mon, 08 Apr 2013 01:00:26 +0200 |
parent | 103:0491aa849c91 |
child | 1004:290c21a5e0ee |
comparison
equal
deleted
inserted
replaced
975:08628703456a | 976:0428009c1127 |
---|---|
1 -- Copyright (C) 2009 Thilo Cestonaro | |
2 -- | |
3 -- This project is MIT/X11 licensed. Please see the | |
4 -- COPYING file in the source package for more information. | |
5 -- | |
6 local prosody = prosody; | 1 local prosody = prosody; |
7 local tostring = _G.tostring; | 2 local tostring = tostring; |
8 local splitJid = require "util.jid".split; | 3 local splitJid = require "util.jid".split; |
9 local config_get = require "core.configmanager".get; | 4 local cm = require "core.configmanager"; |
10 local datamanager = require "util.datamanager"; | 5 local datamanager = require "util.datamanager"; |
11 local data_load, data_store, data_getpath = datamanager.load, datamanager.store, datamanager.getpath; | 6 local data_load, data_store, data_getpath = datamanager.load, datamanager.store, datamanager.getpath; |
12 local datastore = "muc_log"; | 7 local datastore = "muc_log"; |
8 local error_reply = require "util.stanza".error_reply; | |
9 local storagemanager = storagemanager; | |
10 | |
13 local mod_host = module:get_host(); | 11 local mod_host = module:get_host(); |
14 local config = nil; | 12 local config = nil; |
15 | 13 |
16 --[[ LuaFileSystem | 14 -- Helper Functions |
17 * URL: http://www.keplerproject.org/luafilesystem/index.html | |
18 * Install: luarocks install luafilesystem | |
19 * ]] | |
20 local lfs = require "lfs"; | |
21 | 15 |
22 local function checkDatastorePathExists(node, host, today, create) | 16 local function inject_storage_config() |
23 create = create or false; | 17 local _storage = cm.getconfig()[mod_host].storage; |
24 local path = data_getpath(node, host, datastore, "dat", true); | |
25 path = path:gsub("/[^/]*$", ""); | |
26 | 18 |
27 -- check existance | 19 module:log("debug", "injecting storage config..."); |
28 local attributes, err = lfs.attributes(path); | 20 if type(_storage) == "string" then cm.getconfig()[mod_host].default_storage = _storage; end |
29 if attributes == nil or attributes.mode ~= "directory" then | 21 if type(_storage) == "table" then -- append |
30 module:log("warn", "muc_log folder isn't a folder: %s", path); | 22 _storage.muc_log = "internal"; |
31 return false; | 23 else |
24 cm.getconfig()[mod_host].storage = { muc_log = "internal" }; | |
32 end | 25 end |
33 | 26 |
34 attributes, err = lfs.attributes(path .. "/" .. today); | 27 storagemanager.get_driver(mod_host, "muc_log"); -- init |
35 if attributes == nil then | |
36 if create then | |
37 return lfs.mkdir(path .. "/" .. today); | |
38 else | |
39 return false; | |
40 end | |
41 elseif attributes.mode == "directory" then | |
42 return true; | |
43 end | |
44 return false; | |
45 end | 28 end |
29 | |
30 -- Module Definitions | |
46 | 31 |
47 function logIfNeeded(e) | 32 function logIfNeeded(e) |
48 local stanza, origin = e.stanza, e.origin; | 33 local stanza, origin = e.stanza, e.origin; |
49 | 34 |
50 if (stanza.name == "presence") or | 35 if (stanza.name == "presence") or |
92 break; | 77 break; |
93 end | 78 end |
94 end | 79 end |
95 end | 80 end |
96 | 81 |
97 if (mucFrom ~= nil or mucTo ~= nil) and checkDatastorePathExists(node, host, today, true) then | 82 if (mucFrom ~= nil or mucTo ~= nil) then |
98 local data = data_load(node, host, datastore .. "/" .. today); | 83 local data = data_load(node, host, datastore .. "/" .. today); |
99 local realFrom = stanza.attr.from; | 84 local realFrom = stanza.attr.from; |
100 local realTo = stanza.attr.to; | 85 local realTo = stanza.attr.to; |
101 | 86 |
102 if data == nil then | 87 if data == nil then |
129 | 114 |
130 module:hook("message/bare", logIfNeeded, 500); | 115 module:hook("message/bare", logIfNeeded, 500); |
131 module:hook("iq/bare", logIfNeeded, 500); | 116 module:hook("iq/bare", logIfNeeded, 500); |
132 module:hook("presence/full", logIfNeeded, 500); | 117 module:hook("presence/full", logIfNeeded, 500); |
133 | 118 |
119 local function reload() | |
120 inject_storage_config(); | |
121 end | |
122 | |
123 function module.load() | |
124 inject_storage_config(); | |
125 end | |
126 | |
134 module:log("debug", "module mod_muc_log loaded!"); | 127 module:log("debug", "module mod_muc_log loaded!"); |