-- XEP-0313: Message Archive Management for Prosody MUC -- Copyright (C) 2011-2014 Kim Alvefur -- -- This file is MIT/X11 licensed. if module:get_host_type() ~= "component" then module:log("error", "mod_%s should be loaded only on a MUC component, not normal hosts", module.name); return; end local xmlns_mam = "urn:xmpp:mam:0"; local xmlns_delay = "urn:xmpp:delay"; local xmlns_forward = "urn:xmpp:forward:0"; local muc_form_enable_logging = "muc#roomconfig_enablelogging" local st = require "util.stanza"; local rsm = module:require "mod_mam/rsm"; local jid_bare = require "util.jid".bare; local jid_split = require "util.jid".split; local dataform = require "util.dataforms".new; local it = require"util.iterators"; -- Support both old and new MUC code local mod_muc = module:depends"muc"; local rooms = rawget(mod_muc, "rooms"); local each_room = rawget(mod_muc, "each_room") or function() return it.values(rooms); end; local new_muc = not rooms; if new_muc then rooms = module:shared"muc/rooms"; end local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or function (jid) return rooms[jid]; end local getmetatable = getmetatable; local function is_stanza(x) return getmetatable(x) == st.stanza_mt; end local tostring = tostring; local time_now = os.time; local m_min = math.min; local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse; local max_history_length = module:get_option_number("max_history_messages", 1000); local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", max_history_length); local log_all_rooms = module:get_option_boolean("muc_log_all_rooms", false); local log_by_default = module:get_option_boolean("muc_log_by_default", true); local archive_store = "muc_log"; local archive = module:open_store(archive_store, "archive"); if archive.name == "null" or not archive.find then if not archive.find then module:log("error", "Attempt to open archive storage returned a valid driver but it does not seem to implement the storage API"); module:log("error", "mod_%s does not support archiving", archive._provided_by or archive.name and "storage_"..archive.name.."(?)" or ""); else module:log("error", "Attempt to open archive storage returned null driver"); end module:log("info", "See https://prosody.im/doc/storage and https://prosody.im/doc/archiving for more information"); return false; end local function logging_enabled(room) if log_all_rooms then return true; end local enabled = room._data.logging; if enabled == nil then return log_by_default; end return enabled; end local send_history, save_to_history; -- Override history methods for all rooms. if not new_muc then -- 0.10 or older module:hook("muc-room-created", function (event) local room = event.room; if logging_enabled(room) then room.send_history = send_history; room.save_to_history = save_to_history; end end); function module.load() for room in each_room() do if logging_enabled(room) then room.send_history = send_history; room.save_to_history = save_to_history; end end end function module.unload() for room in each_room() do if room.send_history == send_history then room.send_history = nil; room.save_to_history = nil; end end end end if not log_all_rooms then module:hook("muc-config-form", function(event) local room, form = event.room, event.form; table.insert(form, { name = muc_form_enable_logging, type = "boolean", label = "Enable Logging?", value = logging_enabled(room), } ); end); module:hook("muc-config-submitted", function(event) local room, fields, changed = event.room, event.fields, event.changed; local new = fields[muc_form_enable_logging]; if new ~= room._data.logging then room._data.logging = new; if type(changed) == "table" then changed[muc_form_enable_logging] = true; else event.changed = true; end if new then room.send_history = send_history; room.save_to_history = save_to_history; else room.send_history = nil; room.save_to_history = nil; end end end); end -- Note: We ignore the 'with' field as this is internally used for stanza types local query_form = dataform { { name = "FORM_TYPE"; type = "hidden"; value = xmlns_mam; }; { name = "with"; type = "jid-single"; }; { name = "start"; type = "text-single" }; { name = "end"; type = "text-single"; }; }; -- Serve form module:hook("iq-get/bare/"..xmlns_mam..":query", function(event) local origin, stanza = event.origin, event.stanza; origin.send(st.reply(stanza):add_child(query_form:form())); return true; end); -- Handle archive queries module:hook("iq-set/bare/"..xmlns_mam..":query", function(event) local origin, stanza = event.origin, event.stanza; local room = stanza.attr.to; local room_node = jid_split(room); local orig_from = stanza.attr.from; local query = stanza.tags[1]; local room_obj = get_room_from_jid(room); if not room_obj then origin.send(st.error_reply(stanza, "cancel", "item-not-found")) return true; end local from = jid_bare(orig_from); -- Banned or not a member of a members-only room? local from_affiliation = room_obj:get_affiliation(from); if from_affiliation == "outcast" -- banned or room_obj:get_members_only() and not from_affiliation then -- members-only, not a member origin.send(st.error_reply(stanza, "auth", "forbidden")) return true; end local qid = query.attr.queryid; -- Search query parameters local qstart, qend; local form = query:get_child("x", "jabber:x:data"); if form then local err; form, err = query_form:data(form); if err then origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err)))); return true; end qstart, qend = form["start"], form["end"]; end if qstart or qend then -- Validate timestamps local vstart, vend = (qstart and timestamp_parse(qstart)), (qend and timestamp_parse(qend)) if (qstart and not vstart) or (qend and not vend) then origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp")) return true end qstart, qend = vstart, vend; end -- RSM stuff local qset = rsm.get(query); local qmax = m_min(qset and qset.max or default_max_items, max_max_items); local reverse = qset and qset.before or false; local before, after = qset and qset.before, qset and qset.after; if type(before) ~= "string" then before = nil; end -- Load all the data! local data, err = archive:find(room_node, { start = qstart; ["end"] = qend; -- Time range limit = qmax + 1; before = before; after = after; reverse = reverse; total = true; with = "message qmax then complete = nil; break; end local fwd_st = st.message(msg_reply_attr) :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id }) :tag("forwarded", { xmlns = xmlns_forward }) :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up(); if not is_stanza(item) then item = st.deserialize(item); end item.attr.xmlns = "jabber:client"; fwd_st:add_child(item); if not first then first = id; end last = id; if reverse then results[count] = fwd_st; else origin.send(fwd_st); end end if reverse then for i = #results, 1, -1 do origin.send(results[i]); end end -- That's all folks! module:log("debug", "Archive query %s completed", tostring(qid)); if reverse then first, last = last, first; end origin.send(st.message(msg_reply_attr) :tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete }) :add_child(rsm.generate { first = first, last = last, count = total })); return true; end); module:hook("muc-get-history", function (event) local room = event.room; if not logging_enabled(room) then return end local room_jid = room.jid; local maxstanzas = event.maxstanzas; local maxchars = event.maxchars; local since = event.since; local to = event.to; -- Load all the data! local query = { limit = m_min(maxstanzas or 20, max_history_length); start = since; reverse = true; with = "message