Mercurial > prosody-modules
comparison mod_muc_archive/mod_muc_archive.lua @ 3957:7e96b95924bd
mod_muc_archive: Add fork of mod_muc_log that uses newer storage API
author | JC Brand <jc@opkode.com> |
---|---|
date | Fri, 27 Mar 2020 15:51:57 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3956:ebc1f1d962c5 | 3957:7e96b95924bd |
---|---|
1 -- The MIT License (MIT) | |
2 -- | |
3 -- Copyright (C) 2009 Thilo Cestonaro | |
4 -- Copyright (C) 2009 Waqas Hussain | |
5 -- Copyright (C) 2009-2013 Matthew Wild | |
6 -- Copyright (C) 2013 Kim Alvefur | |
7 -- Copyright (C) 2013 Marco Cirillo | |
8 -- Copyright (c) 2020 JC Brand | |
9 -- | |
10 -- Permission is hereby granted, free of charge, to any person obtaining a copy of | |
11 -- this software and associated documentation files (the "Software"), to deal in | |
12 -- the Software without restriction, including without limitation the rights to | |
13 -- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
14 -- the Software, and to permit persons to whom the Software is furnished to do so, | |
15 -- subject to the following conditions: | |
16 -- | |
17 -- The above copyright notice and this permission notice shall be included in all | |
18 -- copies or substantial portions of the Software. | |
19 -- | |
20 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
22 -- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
23 -- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
24 -- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
25 -- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
26 -- | |
27 local hosts = prosody.hosts; | |
28 local tostring = tostring; | |
29 local st = require "util.stanza"; | |
30 local split_jid = require "util.jid".split; | |
31 local jid_bare = require "util.jid".bare; | |
32 local time_now = os.time; | |
33 | |
34 local muc_form_config_option = "muc#roomconfig_enablelogging" | |
35 | |
36 local log_all_rooms = module:get_option_boolean("muc_log_all_rooms", false); | |
37 local log_by_default = module:get_option_boolean("muc_log_by_default", false); | |
38 local log_presences = module:get_option_boolean("muc_log_presences", true); | |
39 | |
40 local archive_store = "muc_logging_archive"; | |
41 local archive = module:open_store(archive_store, "archive"); | |
42 | |
43 local xmlns_muc_user = "http://jabber.org/protocol/muc#user"; | |
44 | |
45 if archive.name == "null" or not archive.find then | |
46 if not archive.find then | |
47 module:log("error", "Attempt to open archive storage returned a driver without archive API support"); | |
48 module:log("error", "mod_%s does not support archiving", | |
49 archive._provided_by or archive.name and "storage_"..archive.name.."(?)" or "<unknown>"); | |
50 else | |
51 module:log("error", "Attempt to open archive storage returned null driver"); | |
52 end | |
53 module:log("info", "See https://prosody.im/doc/storage and https://prosody.im/doc/archiving for more information"); | |
54 return false; | |
55 end | |
56 | |
57 | |
58 -- Module Definitions | |
59 | |
60 local function get_room_from_jid(jid) | |
61 local node, host = split_jid(jid); | |
62 local component = hosts[host]; | |
63 if component then | |
64 local muc = component.modules.muc | |
65 if muc and rawget(muc,"rooms") then | |
66 -- We're running 0.9.x or 0.10 (old MUC API) | |
67 return muc.rooms[jid]; | |
68 elseif muc and rawget(muc,"get_room_from_jid") then | |
69 -- We're running >0.10 (new MUC API) | |
70 return muc.get_room_from_jid(jid); | |
71 else | |
72 return | |
73 end | |
74 end | |
75 end | |
76 | |
77 local function logging_enabled(room) | |
78 if log_all_rooms then | |
79 return true; | |
80 end | |
81 if room._data.hidden then -- do not log data of private rooms | |
82 return false; | |
83 end | |
84 local enabled = room._data.logging; | |
85 if enabled == nil then | |
86 return log_by_default; | |
87 end | |
88 return enabled; | |
89 end | |
90 | |
91 | |
92 local function log_if_needed(event) | |
93 local stanza = event.stanza; | |
94 | |
95 if (stanza.name == "presence") or | |
96 (stanza.name == "iq") or | |
97 (stanza.name == "message" and tostring(stanza.attr.type) == "groupchat") | |
98 then | |
99 local node, host = split_jid(stanza.attr.to); | |
100 if node and host then | |
101 local room = get_room_from_jid(node.."@"..host) | |
102 if not room then return end | |
103 -- Policy check | |
104 if not logging_enabled(room) then return end | |
105 | |
106 local muc_to = nil | |
107 local muc_from = nil; | |
108 | |
109 if stanza.name == "presence" and stanza.attr.type == nil then | |
110 muc_from = stanza.attr.to; | |
111 elseif stanza.name == "iq" and stanza.attr.type == "set" then | |
112 -- kick, to is the room, from is the admin, nick who is kicked is attr of iq->query->item | |
113 if stanza.tags[1] and stanza.tags[1].name == "query" then | |
114 local tmp = stanza.tags[1]; | |
115 if tmp.tags[1] ~= nil and tmp.tags[1].name == "item" and tmp.tags[1].attr.nick then | |
116 tmp = tmp.tags[1]; | |
117 for jid, nick in pairs(room._jid_nick) do | |
118 if nick == stanza.attr.to .. "/" .. tmp.attr.nick then | |
119 muc_to = nick; | |
120 break; | |
121 end | |
122 end | |
123 end | |
124 end | |
125 else | |
126 for jid, nick in pairs(room._jid_nick) do | |
127 if jid == stanza.attr.from then | |
128 muc_from = nick; | |
129 break; | |
130 end | |
131 end | |
132 end | |
133 | |
134 if (muc_from or muc_to) then | |
135 local stored_stanza = st.clone(stanza); | |
136 stored_stanza.attr.from = muc_from; | |
137 stored_stanza.attr.to = muc_to; | |
138 | |
139 if stanza.name == "message" then | |
140 local actor = jid_bare(room._occupants[muc_from].jid); | |
141 local affiliation = room:get_affiliation(actor) or "none"; | |
142 local role = room:get_role(actor) or room:get_default_role(affiliation); | |
143 stored_stanza:add_direct_child(st.stanza("x", { xmlns = xmlns_muc_user }) | |
144 :tag("item", { affiliation = affiliation; role = role; jid = actor })); | |
145 end | |
146 | |
147 local with = stanza.name | |
148 if stanza.attr.type then | |
149 with = with .. "<" .. stanza.attr.type | |
150 end | |
151 archive:append(node, nil, stored_stanza, time_now(), with); | |
152 end | |
153 end | |
154 end | |
155 end | |
156 | |
157 if not log_all_rooms then | |
158 module:hook("muc-config-form", function(event) | |
159 local room, form = event.room, event.form; | |
160 table.insert(form, | |
161 { | |
162 name = muc_form_config_option, | |
163 type = "boolean", | |
164 label = "Enable Logging?", | |
165 value = logging_enabled(room), | |
166 } | |
167 ); | |
168 end); | |
169 | |
170 module:hook("muc-config-submitted", function(event) | |
171 local room, fields, changed = event.room, event.fields, event.changed; | |
172 local new = fields[muc_form_config_option]; | |
173 if new ~= room._data.logging then | |
174 room._data.logging = new; | |
175 if type(changed) == "table" then | |
176 changed[muc_form_config_option] = true; | |
177 else | |
178 event.changed = true; | |
179 end | |
180 end | |
181 end); | |
182 end | |
183 | |
184 module:hook("message/bare", log_if_needed, 1); | |
185 | |
186 if log_presences then | |
187 module:hook("iq/bare", log_if_needed, 1); | |
188 module:hook("presence/full", log_if_needed, 1); | |
189 end | |
190 | |
191 module:log("debug", "module mod_muc_archive loaded!"); |