From 7181ae5f4fd54180685d328158dd71af296bfe7e Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 20 Jan 2017 11:41:07 +0100 Subject: util.crand: Switch to luaL_checkinteger since checkint is deprecated in Lua 5.3 --- util-src/crand.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/util-src/crand.c b/util-src/crand.c index 177511ce..547ceb55 100644 --- a/util-src/crand.c +++ b/util-src/crand.c @@ -57,7 +57,7 @@ int Lrandom(lua_State *L) { unsigned char *buf; #endif int ret = 0; - size_t len = (size_t)luaL_checkint(L, 1); + size_t len = (size_t)luaL_checkinteger(L, 1); #ifdef BUFLEN len = len > BUFLEN ? BUFLEN : len; #else @@ -71,6 +71,7 @@ int Lrandom(lua_State *L) { */ return 2; } + #endif #if defined(WITH_GETRANDOM) -- cgit v1.2.3 From 4645eae4785d0e73daf845c304b6089186eb2ed9 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 20 Jan 2017 11:52:46 +0100 Subject: util.crand: Let Lua handle allocation, freeing and error handling for buffer --- util-src/crand.c | 43 +++---------------------------------------- 1 file changed, 3 insertions(+), 40 deletions(-) diff --git a/util-src/crand.c b/util-src/crand.c index 547ceb55..21079be0 100644 --- a/util-src/crand.c +++ b/util-src/crand.c @@ -19,15 +19,6 @@ #include #include -/* - * TODO: Decide on fixed size or dynamically allocated buffer - */ -#if 1 -#include -#else -#define BUFLEN 256 -#endif - #if defined(WITH_GETRANDOM) #include #include @@ -38,7 +29,7 @@ #endif /* Was this not supposed to be a function? */ -int getrandom(char *buf, size_t len, int flags) { +int getrandom(void *buf, size_t len, int flags) { return syscall(SYS_getrandom, buf, len, flags); } @@ -51,36 +42,14 @@ int getrandom(char *buf, size_t len, int flags) { #endif int Lrandom(lua_State *L) { -#ifdef BUFLEN - unsigned char buf[BUFLEN]; -#else - unsigned char *buf; -#endif int ret = 0; size_t len = (size_t)luaL_checkinteger(L, 1); -#ifdef BUFLEN - len = len > BUFLEN ? BUFLEN : len; -#else - buf = malloc(len); - - if(buf == NULL) { - lua_pushnil(L); - lua_pushstring(L, "out of memory"); - /* or it migth be better to - * return lua_error(L); - */ - return 2; - } - -#endif + void *buf = lua_newuserdata(L, len); #if defined(WITH_GETRANDOM) ret = getrandom(buf, len, 0); if(ret < 0) { -#ifndef BUFLEN - free(buf); -#endif lua_pushnil(L); lua_pushstring(L, strerror(errno)); lua_pushinteger(L, errno); @@ -96,9 +65,6 @@ int Lrandom(lua_State *L) { if(ret == 1) { ret = len; } else { -#ifndef BUFLEN - free(buf); -#endif lua_pushnil(L); lua_pushstring(L, "failed"); /* lua_pushinteger(L, ERR_get_error()); */ @@ -107,10 +73,7 @@ int Lrandom(lua_State *L) { #endif - lua_pushlstring(L, (const char *)buf, ret); -#ifndef BUFLEN - free(buf); -#endif + lua_pushlstring(L, buf, ret); return 1; } -- cgit v1.2.3 From 7a551560615e89dfb68c89c18c43d54ceab642ed Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 20 Jan 2017 11:33:31 +0100 Subject: util.crand: Remove seeding --- util-src/crand.c | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/util-src/crand.c b/util-src/crand.c index 21079be0..6b9dc726 100644 --- a/util-src/crand.c +++ b/util-src/crand.c @@ -77,22 +77,6 @@ int Lrandom(lua_State *L) { return 1; } -#ifdef ENABLE_SEEDING -int Lseed(lua_State *L) { - size_t len; - const char *seed = lua_tolstring(L, 1, &len); - -#if defined(WITH_OPENSSL) - RAND_add(seed, len, len); - return 0; -#else - lua_pushnil(L); - lua_pushliteral(L, "not-supported"); - return 2; -#endif -} -#endif - int luaopen_util_crand(lua_State *L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); @@ -100,10 +84,6 @@ int luaopen_util_crand(lua_State *L) { lua_newtable(L); lua_pushcfunction(L, Lrandom); lua_setfield(L, -2, "bytes"); -#ifdef ENABLE_SEEDING - lua_pushcfunction(L, Lseed); - lua_setfield(L, -2, "seed"); -#endif #if defined(WITH_GETRANDOM) lua_pushstring(L, "Linux"); -- cgit v1.2.3 From d21acaaf6689a38d60a533872730f2bae25a5281 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 20 Jan 2017 12:23:55 +0100 Subject: util.crand: Raise hard errors --- util-src/crand.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/util-src/crand.c b/util-src/crand.c index 6b9dc726..ab16d8d8 100644 --- a/util-src/crand.c +++ b/util-src/crand.c @@ -50,10 +50,8 @@ int Lrandom(lua_State *L) { ret = getrandom(buf, len, 0); if(ret < 0) { - lua_pushnil(L); lua_pushstring(L, strerror(errno)); - lua_pushinteger(L, errno); - return 3; + return lua_error(L); } #elif defined(WITH_ARC4RANDOM) @@ -65,10 +63,8 @@ int Lrandom(lua_State *L) { if(ret == 1) { ret = len; } else { - lua_pushnil(L); - lua_pushstring(L, "failed"); - /* lua_pushinteger(L, ERR_get_error()); */ - return 2; + lua_pushstring(L, "RAND_bytes() failed"); + return lua_error(L); } #endif -- cgit v1.2.3 From d4c65d2358b2e119a35a75fd28298b049178a5ad Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 22 Jan 2017 04:05:41 +0100 Subject: util.ringbuffer: Remove unused macros --- util-src/ringbuffer.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c index d60a43d9..950bda2b 100644 --- a/util-src/ringbuffer.c +++ b/util-src/ringbuffer.c @@ -8,9 +8,6 @@ #include #include -#define MIN(a, b) ((a)>(b)?(b):(a)) -#define MAX(a, b) ((a)>(b)?(a):(b)) - typedef struct { size_t rpos; /* read position */ size_t wpos; /* write position */ -- cgit v1.2.3 From 8b8bf98c357d996b2206a7d8796d8bbb8a46d239 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 22 Jan 2017 09:23:10 +0100 Subject: util.ringbuffer: Allocate buffer itself as part of userdata (simpler, single allocation, no need for __gc) --- util-src/ringbuffer.c | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c index 950bda2b..f7487a88 100644 --- a/util-src/ringbuffer.c +++ b/util-src/ringbuffer.c @@ -13,7 +13,7 @@ typedef struct { size_t wpos; /* write position */ size_t alen; /* allocated size */ size_t blen; /* current content size */ - char* buffer; + char buffer[]; } ringbuffer; char readchar(ringbuffer* b) { @@ -166,16 +166,12 @@ int rb_free(lua_State* L) { int rb_new(lua_State* L) { size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE)); - ringbuffer* b = lua_newuserdata(L, sizeof(ringbuffer)); + ringbuffer *b = lua_newuserdata(L, sizeof(ringbuffer) + size); + b->rpos = 0; b->wpos = 0; b->alen = size; b->blen = 0; - b->buffer = malloc(size); - - if(b->buffer == NULL) { - return 0; - } luaL_getmetatable(L, "ringbuffer_mt"); lua_setmetatable(L, -2); @@ -183,16 +179,6 @@ int rb_new(lua_State* L) { return 1; } -int rb_gc(lua_State* L) { - ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); - - if(b->buffer != NULL) { - free(b->buffer); - } - - return 0; -} - int luaopen_util_ringbuffer(lua_State* L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); @@ -202,8 +188,6 @@ int luaopen_util_ringbuffer(lua_State* L) { lua_setfield(L, -2, "__tostring"); lua_pushcfunction(L, rb_length); lua_setfield(L, -2, "__len"); - lua_pushcfunction(L, rb_gc); - lua_setfield(L, -2, "__gc"); lua_newtable(L); /* __index */ { -- cgit v1.2.3 From 600814a0f6a8ae9f0fbf10d00afe325dd06b86f6 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 22 Jan 2017 09:31:56 +0100 Subject: util.crand: Update copyright header --- util-src/crand.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/util-src/crand.c b/util-src/crand.c index ab16d8d8..da79f3c7 100644 --- a/util-src/crand.c +++ b/util-src/crand.c @@ -1,7 +1,7 @@ /* Prosody IM --- Copyright (C) 2008-2016 Matthew Wild --- Copyright (C) 2008-2016 Waqas Hussain --- Copyright (C) 2016 Kim Alvefur +-- Copyright (C) 2008-2017 Matthew Wild +-- Copyright (C) 2008-2017 Waqas Hussain +-- Copyright (C) 2016-2017 Kim Alvefur -- -- This project is MIT/X11 licensed. Please see the -- COPYING file in the source package for more information. -- cgit v1.2.3 From 3a33321db7be83ef5aab718a8271c4bd81569e50 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 22 Jan 2017 09:32:33 +0100 Subject: util.crand: Add comment describing purpose of module --- util-src/crand.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/util-src/crand.c b/util-src/crand.c index da79f3c7..aa3379c2 100644 --- a/util-src/crand.c +++ b/util-src/crand.c @@ -11,6 +11,12 @@ /* * crand.c * C PRNG interface +* +* The purpose of this module is to provide access to a PRNG in +* environments without /dev/urandom +* +* Caution! This has not been extensively tested. +* */ #include "lualib.h" -- cgit v1.2.3 From 6adfe1e74325f9545fc4032b8d3fdecc92386983 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 22 Jan 2017 09:55:44 +0100 Subject: util.crand: Comment about getrandom --- util-src/crand.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/util-src/crand.c b/util-src/crand.c index aa3379c2..7dd71a92 100644 --- a/util-src/crand.c +++ b/util-src/crand.c @@ -34,7 +34,10 @@ #error getrandom() requires Linux 3.17 or later #endif -/* Was this not supposed to be a function? */ +/* + * This acts like a read from /dev/urandom with the exception that it + * *does* block if the entropy pool is not yet initialized. + */ int getrandom(void *buf, size_t len, int flags) { return syscall(SYS_getrandom, buf, len, flags); } -- cgit v1.2.3 From 11d31ff179d06a1d981956286b2c3b6b0d45e75a Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 22 Jan 2017 09:55:52 +0100 Subject: util.crand: TODOs --- util-src/crand.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/util-src/crand.c b/util-src/crand.c index 7dd71a92..cc2047eb 100644 --- a/util-src/crand.c +++ b/util-src/crand.c @@ -72,6 +72,7 @@ int Lrandom(lua_State *L) { if(ret == 1) { ret = len; } else { + /* TODO ERR_get_error() */ lua_pushstring(L, "RAND_bytes() failed"); return lua_error(L); } @@ -100,7 +101,7 @@ int luaopen_util_crand(lua_State *L) { lua_setfield(L, -2, "_source"); #if defined(WITH_OPENSSL) && defined(_WIN32) - /* Do we need to seed this on Windows? */ + /* TODO Do we need to seed this on Windows? */ #endif return 1; -- cgit v1.2.3 From 31ac4804c2c3c6b12c53d64044a9cb7f6666314c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 23 Jan 2017 10:45:20 +0100 Subject: mod_tls: Only accept on outgoing s2s connections --- plugins/mod_tls.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/plugins/mod_tls.lua b/plugins/mod_tls.lua index 7eedb083..d9593b4c 100644 --- a/plugins/mod_tls.lua +++ b/plugins/mod_tls.lua @@ -124,9 +124,11 @@ module:hook_stanza("http://etherx.jabber.org/streams", "features", function (ses end, 500); module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza) -- luacheck: ignore 212/stanza - module:log("debug", "Proceeding with TLS on s2sout..."); - session:reset_stream(); - session.conn:starttls(session.ssl_ctx); - session.secure = false; - return true; + if session.type == "s2sout_unauthed" then + module:log("debug", "Proceeding with TLS on s2sout..."); + session:reset_stream(); + session.conn:starttls(session.ssl_ctx); + session.secure = false; + return true; + end end); -- cgit v1.2.3 From a5d48058fb1245ee6bf46abfbbf7ff373b166122 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 23 Jan 2017 10:46:42 +0100 Subject: mod_tls: Verify that TLS is available before proceeding --- plugins/mod_tls.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/mod_tls.lua b/plugins/mod_tls.lua index d9593b4c..5869b2a5 100644 --- a/plugins/mod_tls.lua +++ b/plugins/mod_tls.lua @@ -124,7 +124,7 @@ module:hook_stanza("http://etherx.jabber.org/streams", "features", function (ses end, 500); module:hook_stanza(xmlns_starttls, "proceed", function (session, stanza) -- luacheck: ignore 212/stanza - if session.type == "s2sout_unauthed" then + if session.type == "s2sout_unauthed" and can_do_tls(session) then module:log("debug", "Proceeding with TLS on s2sout..."); session:reset_stream(); session.conn:starttls(session.ssl_ctx); -- cgit v1.2.3 From 8cd92db7e6c1c03811ff039eb88c7e4ee375c5c3 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 23 Jan 2017 18:33:35 +0100 Subject: util-src/*.c: Squeeze repeated blank lines --- util-src/encodings.c | 1 - util-src/hashes.c | 1 - util-src/pposix.c | 2 -- util-src/ringbuffer.c | 3 --- util-src/table.c | 1 - 5 files changed, 8 deletions(-) diff --git a/util-src/encodings.c b/util-src/encodings.c index 6389b2be..4d6ac437 100644 --- a/util-src/encodings.c +++ b/util-src/encodings.c @@ -251,7 +251,6 @@ static const luaL_Reg Reg_utf8[] = { { NULL, NULL } }; - /***************** STRINGPREP *****************/ #ifdef USE_STRINGPREP_ICU diff --git a/util-src/hashes.c b/util-src/hashes.c index d6f848c7..697c632f 100644 --- a/util-src/hashes.c +++ b/util-src/hashes.c @@ -7,7 +7,6 @@ -- */ - /* * hashes.c * Lua library for sha1, sha256 and md5 hashes diff --git a/util-src/pposix.c b/util-src/pposix.c index 39d8742b..b6874318 100644 --- a/util-src/pposix.c +++ b/util-src/pposix.c @@ -595,8 +595,6 @@ int lc_getrlimit(lua_State* L) { return 2; } - - resource = luaL_checkstring(L, 1); rid = string2resource(resource); diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c index f7487a88..73a8616b 100644 --- a/util-src/ringbuffer.c +++ b/util-src/ringbuffer.c @@ -1,5 +1,4 @@ - #include #include #include @@ -73,7 +72,6 @@ int rb_find(lua_State* L) { return 0; } - int rb_read(lua_State* L) { ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); int r = luaL_checkinteger(L, 2); @@ -101,7 +99,6 @@ int rb_read(lua_State* L) { return 1; } - int rb_readuntil(lua_State* L) { size_t l, m; ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); diff --git a/util-src/table.c b/util-src/table.c index 93acae65..c9c09170 100644 --- a/util-src/table.c +++ b/util-src/table.c @@ -19,7 +19,6 @@ static int Lpack(lua_State* L) { return 1; } - int luaopen_util_table(lua_State* L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); -- cgit v1.2.3 From 58e93ec781de9f2859694cbb59c3dbfa468e2ba2 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 4 Nov 2016 13:48:21 +0100 Subject: mod_mam: Import from prosody-modules --- plugins/mod_mam/fallback_archive.lib.lua | 84 ++++++++ plugins/mod_mam/mamprefs.lib.lua | 41 ++++ plugins/mod_mam/mamprefsxml.lib.lua | 58 ++++++ plugins/mod_mam/mod_mam.lua | 338 +++++++++++++++++++++++++++++++ plugins/mod_mam/rsm.lib.lua | 87 ++++++++ 5 files changed, 608 insertions(+) create mode 100644 plugins/mod_mam/fallback_archive.lib.lua create mode 100644 plugins/mod_mam/mamprefs.lib.lua create mode 100644 plugins/mod_mam/mamprefsxml.lib.lua create mode 100644 plugins/mod_mam/mod_mam.lua create mode 100644 plugins/mod_mam/rsm.lib.lua diff --git a/plugins/mod_mam/fallback_archive.lib.lua b/plugins/mod_mam/fallback_archive.lib.lua new file mode 100644 index 00000000..92155bef --- /dev/null +++ b/plugins/mod_mam/fallback_archive.lib.lua @@ -0,0 +1,84 @@ +-- luacheck: ignore 212/self + +local uuid = require "util.uuid".generate; +local store = module:shared("archive"); +local archive_store = { _provided_by = "mam"; name = "fallback"; }; + +function archive_store:append(username, key, value, when, with) + local archive = store[username]; + if not archive then + archive = { [0] = 0 }; + store[username] = archive; + end + local index = (archive[0] or #archive)+1; + local item = { key = key, when = when, with = with, value = value }; + if not key or archive[key] then + key = uuid(); + item.key = key; + end + archive[index] = item; + archive[key] = index; + archive[0] = index; + return key; +end + +function archive_store:find(username, query) + local archive = store[username] or {}; + local start, stop, step = 1, archive[0] or #archive, 1; + local qstart, qend, qwith = -math.huge, math.huge; + local limit; + + if query then + if query.reverse then + start, stop, step = stop, start, -1; + if query.before and archive[query.before] then + start = archive[query.before] - 1; + end + elseif query.after and archive[query.after] then + start = archive[query.after] + 1; + end + qwith = query.with; + limit = query.limit; + qstart = query.start or qstart; + qend = query["end"] or qend; + end + + return function () + if limit and limit <= 0 then return end + for i = start, stop, step do + local item = archive[i]; + if (not qwith or qwith == item.with) and item.when >= qstart and item.when <= qend then + if limit then limit = limit - 1; end + start = i + step; -- Start on next item + return item.key, item.value, item.when, item.with; + end + end + end +end + +function archive_store:delete(username, query) + if not query or next(query) == nil then + -- no specifics, delete everything + store[username] = nil; + return true; + end + local archive = store[username]; + if not archive then return true; end -- no messages, nothing to delete + + local start, stop, step = 1, archive[0] or #archive, 1; + local qstart = query.start or -math.huge; + local qend = query["end"] or math.huge; + local qwith = query.with; + store[username] = nil; + for i = 1, #archive do + local item = archive[i]; + local when, with = item.when, item.when; + -- Add things that don't match the query + if not ((not qwith or qwith == item.with) and item.when >= qstart and item.when <= qend) then + self:append(username, item.key, item.value, when, with); + end + end + return true; +end + +return archive_store; diff --git a/plugins/mod_mam/mamprefs.lib.lua b/plugins/mod_mam/mamprefs.lib.lua new file mode 100644 index 00000000..98e74bd6 --- /dev/null +++ b/plugins/mod_mam/mamprefs.lib.lua @@ -0,0 +1,41 @@ +-- XEP-0313: Message Archive Management for Prosody +-- Copyright (C) 2011-2013 Kim Alvefur +-- +-- This file is MIT/X11 licensed. + +local global_default_policy = module:get_option("default_archive_policy", true); + +do + local prefs_format = { + [false] = "roster", + -- default ::= true | false | "roster" + -- true = always, false = never, nil = global default + ["romeo@montague.net"] = true, -- always + ["montague@montague.net"] = false, -- newer + }; +end + +local sessions = hosts[module.host].sessions; +local prefs = module:open_store("archive2_prefs"); + +local function get_prefs(user) + local user_sessions = sessions[user]; + local user_prefs = user_sessions and user_sessions.archive_prefs + if not user_prefs and user_sessions then + user_prefs = prefs:get(user); + user_sessions.archive_prefs = user_prefs; + end + return user_prefs or { [false] = global_default_policy }; +end +local function set_prefs(user, user_prefs) + local user_sessions = sessions[user]; + if user_sessions then + user_sessions.archive_prefs = user_prefs; + end + return prefs:set(user, user_prefs); +end + +return { + get = get_prefs, + set = set_prefs, +} diff --git a/plugins/mod_mam/mamprefsxml.lib.lua b/plugins/mod_mam/mamprefsxml.lib.lua new file mode 100644 index 00000000..a0d46245 --- /dev/null +++ b/plugins/mod_mam/mamprefsxml.lib.lua @@ -0,0 +1,58 @@ +-- XEP-0313: Message Archive Management for Prosody +-- Copyright (C) 2011-2013 Kim Alvefur +-- +-- This file is MIT/X11 licensed. + +local st = require"util.stanza"; +local xmlns_mam = "urn:xmpp:mam:0"; + +local default_attrs = { + always = true, [true] = "always", + never = false, [false] = "never", + roster = "roster", +} + +local function tostanza(prefs) + local default = prefs[false]; + default = default_attrs[default]; + local prefstanza = st.stanza("prefs", { xmlns = xmlns_mam, default = default }); + local always = st.stanza("always"); + local never = st.stanza("never"); + for jid, choice in pairs(prefs) do + if jid then + (choice and always or never):tag("jid"):text(jid):up(); + end + end + prefstanza:add_child(always):add_child(never); + return prefstanza; +end +local function fromstanza(prefstanza) + local prefs = {}; + local default = prefstanza.attr.default; + if default then + prefs[false] = default_attrs[default]; + end + + local always = prefstanza:get_child("always"); + if always then + for rule in always:childtags("jid") do + local jid = rule:get_text(); + prefs[jid] = true; + end + end + + local never = prefstanza:get_child("never"); + if never then + for rule in never:childtags("jid") do + local jid = rule:get_text(); + prefs[jid] = false; + end + end + + return prefs; +end + +return { + tostanza = tostanza; + fromstanza = fromstanza; +} diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua new file mode 100644 index 00000000..fe69e333 --- /dev/null +++ b/plugins/mod_mam/mod_mam.lua @@ -0,0 +1,338 @@ +-- XEP-0313: Message Archive Management for Prosody +-- Copyright (C) 2011-2016 Kim Alvefur +-- +-- This file is MIT/X11 licensed. + +local xmlns_mam = "urn:xmpp:mam:0"; +local xmlns_delay = "urn:xmpp:delay"; +local xmlns_forward = "urn:xmpp:forward:0"; + +local um = require "core.usermanager"; +local st = require "util.stanza"; +local rsm = module:require "rsm"; +local get_prefs = module:require"mamprefs".get; +local set_prefs = module:require"mamprefs".set; +local prefs_to_stanza = module:require"mamprefsxml".tostanza; +local prefs_from_stanza = module:require"mamprefsxml".fromstanza; +local jid_bare = require "util.jid".bare; +local jid_split = require "util.jid".split; +local dataform = require "util.dataforms".new; +local host = module.host; + +local rm_load_roster = require "core.rostermanager".load_roster; + +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 default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50); +local global_default_policy = module:get_option("default_archive_policy", true); +if global_default_policy ~= "roster" then + global_default_policy = module:get_option_boolean("default_archive_policy", global_default_policy); +end + +local archive_store = "archive2"; +local archive = assert(module:open_store(archive_store, "archive")); + +if archive.name == "null" or not archive.find then + if not archive.find then + module:log("debug", "Attempt to open archive storage returned a valid driver but it does not seem to implement the storage API"); + module:log("debug", "mod_%s does not support archiving", archive._provided_by or archive.name and "storage_"..archive.name.."(?)" or ""); + else + module:log("debug", "Attempt to open archive storage returned null driver"); + end + module:log("debug", "See https://prosody.im/doc/storage and https://prosody.im/doc/archiving for more information"); + module:log("info", "Using in-memory fallback archive driver"); + archive = module:require "fallback_archive"; +end + +local cleanup; + +-- Handle prefs. +module:hook("iq/self/"..xmlns_mam..":prefs", function(event) + local origin, stanza = event.origin, event.stanza; + local user = origin.username; + if stanza.attr.type == "get" then + local prefs = prefs_to_stanza(get_prefs(user)); + local reply = st.reply(stanza):add_child(prefs); + origin.send(reply); + else -- type == "set" + local new_prefs = stanza:get_child("prefs", xmlns_mam); + local prefs = prefs_from_stanza(new_prefs); + local ok, err = set_prefs(user, prefs); + if not ok then + origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err))); + else + origin.send(st.reply(stanza)); + end + end + return true; +end); + +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/self/"..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/self/"..xmlns_mam..":query", function(event) + local origin, stanza = event.origin, event.stanza; + local query = stanza.tags[1]; + local qid = query.attr.queryid; + + if cleanup then cleanup[origin.username] = true; end + + -- Search query parameters + local qwith, 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 + qwith, qstart, qend = form["with"], form["start"], form["end"]; + qwith = qwith and jid_bare(qwith); -- dataforms does jidprep + 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 + + module:log("debug", "Archive query, id %s with %s from %s until %s)", + tostring(qid), qwith or "anyone", qstart or "the dawn of time", qend or "now"); + + -- 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(origin.username, { + start = qstart; ["end"] = qend; -- Time range + with = qwith; + limit = qmax + 1; + before = before; after = after; + reverse = reverse; + total = true; + }); + + if not data then + origin.send(st.error_reply(stanza, "cancel", "internal-server-error", err)); + return true; + end + local total = tonumber(err); + + origin.send(st.reply(stanza)); + local msg_reply_attr = { to = stanza.attr.from, from = stanza.attr.to }; + + local results = {}; + + -- Wrap it in stuff and deliver + local first, last; + local count = 0; + local complete = "true"; + for id, item, when in data do + count = count + 1; + if count > 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 + first, last = last, first; + end + + -- That's all folks! + module:log("debug", "Archive query %s completed", tostring(qid)); + + 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); + +local function has_in_roster(user, who) + local roster = rm_load_roster(user, host); + module:log("debug", "%s has %s in roster? %s", user, who, roster[who] and "yes" or "no"); + return roster[who]; +end + +local function shall_store(user, who) + -- TODO Cache this? + if not um.user_exists(user, host) then + return false; + end + local prefs = get_prefs(user); + local rule = prefs[who]; + module:log("debug", "%s's rule for %s is %s", user, who, tostring(rule)); + if rule ~= nil then + return rule; + end + -- Below could be done by a metatable + local default = prefs[false]; + module:log("debug", "%s's default rule is %s", user, tostring(default)); + if default == nil then + default = global_default_policy; + module:log("debug", "Using global default rule, %s", tostring(default)); + end + if default == "roster" then + return has_in_roster(user, who); + end + return default; +end + +-- Handle messages +local function message_handler(event, c2s) + local origin, stanza = event.origin, event.stanza; + local log = c2s and origin.log or module._log; + local orig_type = stanza.attr.type or "normal"; + local orig_from = stanza.attr.from; + local orig_to = stanza.attr.to or orig_from; + -- Stanza without 'to' are treated as if it was to their own bare jid + + -- We store chat messages or normal messages that have a body + if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then + log("debug", "Not archiving stanza: %s (type)", stanza:top_tag()); + return; + end + -- or if hints suggest we shouldn't + if stanza:get_child("no-permanent-storage", "urn:xmpp:hints") -- The XEP needs to decide on "store" or "storage" + or stanza:get_child("no-permanent-store", "urn:xmpp:hints") + or stanza:get_child("no-storage", "urn:xmpp:hints") + or stanza:get_child("no-store", "urn:xmpp:hints") then + log("debug", "Not archiving stanza: %s (hint)", stanza:top_tag()); + return; + end + + -- Whos storage do we put it in? + local store_user = c2s and origin.username or jid_split(orig_to); + -- And who are they chatting with? + local with = jid_bare(c2s and orig_to or orig_from); + + -- Check with the users preferences + if shall_store(store_user, with) then + log("debug", "Archiving stanza: %s", stanza:top_tag()); + + -- And stash it + local ok, id = archive:append(store_user, nil, stanza, time_now(), with); + if ok then + if cleanup then cleanup[store_user] = true; end + module:fire_event("archive-message-added", { origin = origin, stanza = stanza, for_user = store_user, id = id }); + end + else + log("debug", "Not archiving stanza: %s (prefs)", stanza:top_tag()); + end +end + +local function c2s_message_handler(event) + return message_handler(event, true); +end + +local cleanup_after = module:get_option_string("archive_expires_after", "1w"); +local cleanup_interval = module:get_option_number("archive_cleanup_interval", 4 * 60 * 60); +if cleanup_after ~= "never" then + local day = 86400; + local multipliers = { d = day, w = day * 7, m = 31 * day, y = 365.2425 * day }; + local n, m = cleanup_after:lower():match("(%d+)%s*([dwmy]?)"); + if not n then + module:log("error", "Could not parse archive_expires_after string %q", cleanup_after); + return false; + end + + cleanup_after = tonumber(n) * ( multipliers[m] or 1 ); + + module:log("debug", "archive_expires_after = %d -- in seconds", cleanup_after); + + if not archive.delete then + module:log("error", "archive_expires_after set but mod_%s does not support deleting", archive._provided_by); + return false; + end + + cleanup = {}; + + pcall(function () + for user in um.users(module.host) do + cleanup[user] = true; + end + end); + + module:add_timer(math.random(10, 60), function() + local user = next(cleanup); + if user then + module:log("debug", "Removing old messages for user %q", user); + local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; }) + if not ok then + module:log("warn", "Could not expire archives for user %s: %s", user, err); + else + -- :affected() is a recent addition for eg SQLite3 in LuaDBI + pcall(function(stmt) + module:log("debug", "Removed %d messages", stmt:affected()); + end, err); + end + cleanup[user] = nil; + end + return math.random(cleanup_interval, cleanup_interval * 2); + end); +end + +-- Stanzas sent by local clients +module:hook("pre-message/bare", c2s_message_handler, 2); +module:hook("pre-message/full", c2s_message_handler, 2); +-- Stanszas to local clients +module:hook("message/bare", message_handler, 2); +module:hook("message/full", message_handler, 2); + +module:add_feature(xmlns_mam); -- COMPAT with XEP-0313 v 0.1 + +module:hook("account-disco-info", function(event) + (event.reply or event.stanza):tag("feature", {var=xmlns_mam}):up(); +end); + diff --git a/plugins/mod_mam/rsm.lib.lua b/plugins/mod_mam/rsm.lib.lua new file mode 100644 index 00000000..7d5fbcab --- /dev/null +++ b/plugins/mod_mam/rsm.lib.lua @@ -0,0 +1,87 @@ +local stanza = require"util.stanza".stanza; +local tostring, tonumber = tostring, tonumber; +local type = type; +local pairs = pairs; + +local xmlns_rsm = 'http://jabber.org/protocol/rsm'; + +local element_parsers = {}; + +do + local parsers = element_parsers; + local function xs_int(st) + return tonumber((st:get_text())); + end + local function xs_string(st) + return st:get_text(); + end + + parsers.after = xs_string; + parsers.before = function(st) + local text = st:get_text(); + return text == "" or text; + end; + parsers.max = xs_int; + parsers.index = xs_int; + + parsers.first = function(st) + return { index = tonumber(st.attr.index); st:get_text() }; + end; + parsers.last = xs_string; + parsers.count = xs_int; +end + +local element_generators = setmetatable({ + first = function(st, data) + if type(data) == "table" then + st:tag("first", { index = data.index }):text(data[1]):up(); + else + st:tag("first"):text(tostring(data)):up(); + end + end; + before = function(st, data) + if data == true then + st:tag("before"):up(); + else + st:tag("before"):text(tostring(data)):up(); + end + end +}, { + __index = function(_, name) + return function(st, data) + st:tag(name):text(tostring(data)):up(); + end + end; +}); + + +local function parse(set) + local rs = {}; + for tag in set:childtags() do + local name = tag.name; + local parser = name and element_parsers[name]; + if parser then + rs[name] = parser(tag); + end + end + return rs; +end + +local function generate(t) + local st = stanza("set", { xmlns = xmlns_rsm }); + for k,v in pairs(t) do + if element_parsers[k] then + element_generators[k](st, v); + end + end + return st; +end + +local function get(st) + local set = st:get_child("set", xmlns_rsm); + if set and #set.tags > 0 then + return parse(set); + end +end + +return { parse = parse, generate = generate, get = get }; -- cgit v1.2.3 From 7383c85a92202fd550854f5dc94fb98466c4e9f0 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 26 Nov 2016 21:50:06 +0100 Subject: util.rsm: Move out from mod_mam directory --- plugins/mod_mam/mod_mam.lua | 2 +- plugins/mod_mam/rsm.lib.lua | 87 --------------------------------------------- util/rsm.lua | 87 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 88 deletions(-) delete mode 100644 plugins/mod_mam/rsm.lib.lua create mode 100644 util/rsm.lua diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index fe69e333..39d1bcf5 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -9,7 +9,7 @@ local xmlns_forward = "urn:xmpp:forward:0"; local um = require "core.usermanager"; local st = require "util.stanza"; -local rsm = module:require "rsm"; +local rsm = require "rsm"; local get_prefs = module:require"mamprefs".get; local set_prefs = module:require"mamprefs".set; local prefs_to_stanza = module:require"mamprefsxml".tostanza; diff --git a/plugins/mod_mam/rsm.lib.lua b/plugins/mod_mam/rsm.lib.lua deleted file mode 100644 index 7d5fbcab..00000000 --- a/plugins/mod_mam/rsm.lib.lua +++ /dev/null @@ -1,87 +0,0 @@ -local stanza = require"util.stanza".stanza; -local tostring, tonumber = tostring, tonumber; -local type = type; -local pairs = pairs; - -local xmlns_rsm = 'http://jabber.org/protocol/rsm'; - -local element_parsers = {}; - -do - local parsers = element_parsers; - local function xs_int(st) - return tonumber((st:get_text())); - end - local function xs_string(st) - return st:get_text(); - end - - parsers.after = xs_string; - parsers.before = function(st) - local text = st:get_text(); - return text == "" or text; - end; - parsers.max = xs_int; - parsers.index = xs_int; - - parsers.first = function(st) - return { index = tonumber(st.attr.index); st:get_text() }; - end; - parsers.last = xs_string; - parsers.count = xs_int; -end - -local element_generators = setmetatable({ - first = function(st, data) - if type(data) == "table" then - st:tag("first", { index = data.index }):text(data[1]):up(); - else - st:tag("first"):text(tostring(data)):up(); - end - end; - before = function(st, data) - if data == true then - st:tag("before"):up(); - else - st:tag("before"):text(tostring(data)):up(); - end - end -}, { - __index = function(_, name) - return function(st, data) - st:tag(name):text(tostring(data)):up(); - end - end; -}); - - -local function parse(set) - local rs = {}; - for tag in set:childtags() do - local name = tag.name; - local parser = name and element_parsers[name]; - if parser then - rs[name] = parser(tag); - end - end - return rs; -end - -local function generate(t) - local st = stanza("set", { xmlns = xmlns_rsm }); - for k,v in pairs(t) do - if element_parsers[k] then - element_generators[k](st, v); - end - end - return st; -end - -local function get(st) - local set = st:get_child("set", xmlns_rsm); - if set and #set.tags > 0 then - return parse(set); - end -end - -return { parse = parse, generate = generate, get = get }; diff --git a/util/rsm.lua b/util/rsm.lua new file mode 100644 index 00000000..7d5fbcab --- /dev/null +++ b/util/rsm.lua @@ -0,0 +1,87 @@ +local stanza = require"util.stanza".stanza; +local tostring, tonumber = tostring, tonumber; +local type = type; +local pairs = pairs; + +local xmlns_rsm = 'http://jabber.org/protocol/rsm'; + +local element_parsers = {}; + +do + local parsers = element_parsers; + local function xs_int(st) + return tonumber((st:get_text())); + end + local function xs_string(st) + return st:get_text(); + end + + parsers.after = xs_string; + parsers.before = function(st) + local text = st:get_text(); + return text == "" or text; + end; + parsers.max = xs_int; + parsers.index = xs_int; + + parsers.first = function(st) + return { index = tonumber(st.attr.index); st:get_text() }; + end; + parsers.last = xs_string; + parsers.count = xs_int; +end + +local element_generators = setmetatable({ + first = function(st, data) + if type(data) == "table" then + st:tag("first", { index = data.index }):text(data[1]):up(); + else + st:tag("first"):text(tostring(data)):up(); + end + end; + before = function(st, data) + if data == true then + st:tag("before"):up(); + else + st:tag("before"):text(tostring(data)):up(); + end + end +}, { + __index = function(_, name) + return function(st, data) + st:tag(name):text(tostring(data)):up(); + end + end; +}); + + +local function parse(set) + local rs = {}; + for tag in set:childtags() do + local name = tag.name; + local parser = name and element_parsers[name]; + if parser then + rs[name] = parser(tag); + end + end + return rs; +end + +local function generate(t) + local st = stanza("set", { xmlns = xmlns_rsm }); + for k,v in pairs(t) do + if element_parsers[k] then + element_generators[k](st, v); + end + end + return st; +end + +local function get(st) + local set = st:get_child("set", xmlns_rsm); + if set and #set.tags > 0 then + return parse(set); + end +end + +return { parse = parse, generate = generate, get = get }; -- cgit v1.2.3 From 659e742b599f1061473db6dba2576533ddf5af54 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 28 Nov 2016 07:35:49 +0100 Subject: mod_mam: Use is_stanza from util.stanza --- plugins/mod_mam/mod_mam.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index 39d1bcf5..bc348919 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -21,11 +21,7 @@ local host = module.host; local rm_load_roster = require "core.rostermanager".load_roster; -local getmetatable = getmetatable; -local function is_stanza(x) - return getmetatable(x) == st.stanza_mt; -end - +local is_stanza = st.is_stanza; local tostring = tostring; local time_now = os.time; local m_min = math.min; -- cgit v1.2.3 From e79b9bb726188eced2b8532cef9605f7de83b277 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 4 Nov 2016 13:59:49 +0100 Subject: mod_mam: Update to XEP-0313 v0.5.1 --- plugins/mod_mam/mamprefsxml.lib.lua | 2 +- plugins/mod_mam/mod_mam.lua | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/mod_mam/mamprefsxml.lib.lua b/plugins/mod_mam/mamprefsxml.lib.lua index a0d46245..ec728e55 100644 --- a/plugins/mod_mam/mamprefsxml.lib.lua +++ b/plugins/mod_mam/mamprefsxml.lib.lua @@ -4,7 +4,7 @@ -- This file is MIT/X11 licensed. local st = require"util.stanza"; -local xmlns_mam = "urn:xmpp:mam:0"; +local xmlns_mam = "urn:xmpp:mam:1"; local default_attrs = { always = true, [true] = "always", diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index bc348919..0caf4d07 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -3,7 +3,7 @@ -- -- This file is MIT/X11 licensed. -local xmlns_mam = "urn:xmpp:mam:0"; +local xmlns_mam = "urn:xmpp:mam:1"; local xmlns_delay = "urn:xmpp:delay"; local xmlns_forward = "urn:xmpp:forward:0"; @@ -142,7 +142,6 @@ module:hook("iq-set/self/"..xmlns_mam..":query", function(event) end local total = tonumber(err); - origin.send(st.reply(stanza)); local msg_reply_attr = { to = stanza.attr.from, from = stanza.attr.to }; local results = {}; @@ -188,7 +187,7 @@ module:hook("iq-set/self/"..xmlns_mam..":query", function(event) -- That's all folks! module:log("debug", "Archive query %s completed", tostring(qid)); - origin.send(st.message(msg_reply_attr) + origin.send(st.reply(stanza) :tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete }) :add_child(rsm.generate { first = first, last = last, count = total })); -- cgit v1.2.3 From ff9ab39f205a490f3f8593ab3f729fca007e4fba Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 18 Nov 2016 10:44:27 +0100 Subject: mod_mam: Decrease priority to zero --- plugins/mod_mam/mod_mam.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index 0caf4d07..a9c80e73 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -319,11 +319,11 @@ if cleanup_after ~= "never" then end -- Stanzas sent by local clients -module:hook("pre-message/bare", c2s_message_handler, 2); -module:hook("pre-message/full", c2s_message_handler, 2); +module:hook("pre-message/bare", c2s_message_handler, 0); +module:hook("pre-message/full", c2s_message_handler, 0); -- Stanszas to local clients -module:hook("message/bare", message_handler, 2); -module:hook("message/full", message_handler, 2); +module:hook("message/bare", message_handler, 0); +module:hook("message/full", message_handler, 0); module:add_feature(xmlns_mam); -- COMPAT with XEP-0313 v 0.1 -- cgit v1.2.3 From 6f7e52ea70ad518fe033aee8e03bb868974e3f7b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 18 Nov 2016 10:44:56 +0100 Subject: mod_mam: Remove legacy feature advertising --- plugins/mod_mam/mod_mam.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index a9c80e73..383ed162 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -325,8 +325,6 @@ module:hook("pre-message/full", c2s_message_handler, 0); module:hook("message/bare", message_handler, 0); module:hook("message/full", message_handler, 0); -module:add_feature(xmlns_mam); -- COMPAT with XEP-0313 v 0.1 - module:hook("account-disco-info", function(event) (event.reply or event.stanza):tag("feature", {var=xmlns_mam}):up(); end); -- cgit v1.2.3 From e03e8cd897bb2f549a16a041f6deaef4350df909 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 19 Nov 2016 18:26:09 +0100 Subject: mod_mam: Add some comments explaining archive expiry --- plugins/mod_mam/mod_mam.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index 383ed162..d4dae6cc 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -238,6 +238,7 @@ local function message_handler(event, c2s) log("debug", "Not archiving stanza: %s (type)", stanza:top_tag()); return; end + -- or if hints suggest we shouldn't if stanza:get_child("no-permanent-storage", "urn:xmpp:hints") -- The XEP needs to decide on "store" or "storage" or stanza:get_child("no-permanent-store", "urn:xmpp:hints") @@ -291,14 +292,20 @@ if cleanup_after ~= "never" then return false; end + -- Set of known users to do message expiry for + -- Populated either below or when new messages are added cleanup = {}; + -- Iterating over users is not supported by all authentication modules + -- Catch and ignore error if not supported pcall(function () + -- If this works, then we schedule cleanup for all known known for user in um.users(module.host) do cleanup[user] = true; end end); + -- At odd intervals, delete old messages for one user module:add_timer(math.random(10, 60), function() local user = next(cleanup); if user then -- cgit v1.2.3 From 0079e94229aafa89d18d5b8b49b0f42cbf86c30b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 19 Nov 2016 18:27:28 +0100 Subject: mod_mam: Alter hints processing --- plugins/mod_mam/mod_mam.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index d4dae6cc..3e14c022 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -240,12 +240,12 @@ local function message_handler(event, c2s) end -- or if hints suggest we shouldn't - if stanza:get_child("no-permanent-storage", "urn:xmpp:hints") -- The XEP needs to decide on "store" or "storage" - or stanza:get_child("no-permanent-store", "urn:xmpp:hints") - or stanza:get_child("no-storage", "urn:xmpp:hints") - or stanza:get_child("no-store", "urn:xmpp:hints") then - log("debug", "Not archiving stanza: %s (hint)", stanza:top_tag()); - return; + if not stanza:get_child("store", "urn:xmpp:hints") then -- No hint telling us we should store + if stanza:get_child("no-permanent-store", "urn:xmpp:hints") + or stanza:get_child("no-store", "urn:xmpp:hints") then -- Hint telling us we should NOT store + log("debug", "Not archiving stanza: %s (hint)", stanza:top_tag()); + return; + end end -- Whos storage do we put it in? -- cgit v1.2.3 From 338ae79c3153fd333e11c75b87b37e8dfdc2c68f Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 19 Nov 2016 18:28:28 +0100 Subject: mod_mam: Find out which party is the user and which is the 'with' earlier --- plugins/mod_mam/mod_mam.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index 3e14c022..2606f861 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -233,6 +233,11 @@ local function message_handler(event, c2s) local orig_to = stanza.attr.to or orig_from; -- Stanza without 'to' are treated as if it was to their own bare jid + -- Whos storage do we put it in? + local store_user = c2s and origin.username or jid_split(orig_to); + -- And who are they chatting with? + local with = jid_bare(c2s and orig_to or orig_from); + -- We store chat messages or normal messages that have a body if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then log("debug", "Not archiving stanza: %s (type)", stanza:top_tag()); @@ -248,11 +253,6 @@ local function message_handler(event, c2s) end end - -- Whos storage do we put it in? - local store_user = c2s and origin.username or jid_split(orig_to); - -- And who are they chatting with? - local with = jid_bare(c2s and orig_to or orig_from); - -- Check with the users preferences if shall_store(store_user, with) then log("debug", "Archiving stanza: %s", stanza:top_tag()); -- cgit v1.2.3 From 498600dcba7ab38d45ff17bdaba63ea7a999d75c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 19 Nov 2016 18:28:59 +0100 Subject: mod_mam: Filter out spoofed XEP-0359 tags --- plugins/mod_mam/mod_mam.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index 2606f861..cb7613b8 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -16,6 +16,7 @@ local prefs_to_stanza = module:require"mamprefsxml".tostanza; local prefs_from_stanza = module:require"mamprefsxml".fromstanza; local jid_bare = require "util.jid".bare; local jid_split = require "util.jid".split; +local jid_prepped_split = require "util.jid".prepped_split; local dataform = require "util.dataforms".new; local host = module.host; @@ -238,6 +239,17 @@ local function message_handler(event, c2s) -- And who are they chatting with? local with = jid_bare(c2s and orig_to or orig_from); + -- Filter out that claim to be from us + stanza:maptags(function (tag) + if tag.name == "stanza-id" and tag.attr.xmlns == "urn:xmpp:sid:0" then + local by_user, by_host, res = prepped_split(tag.attr.by); + if not res and by_host == module.host and by_user == store_user then + return nil; + end + end + return tag; + end); + -- We store chat messages or normal messages that have a body if not(orig_type == "chat" or (orig_type == "normal" and stanza:get_child("body")) ) then log("debug", "Not archiving stanza: %s (type)", stanza:top_tag()); -- cgit v1.2.3 From 7b3cc17f2e2040286dea2b981beaabfc7fab169d Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 19 Nov 2016 18:29:08 +0100 Subject: mod_mam: Add XEP-0359 tag --- plugins/mod_mam/mod_mam.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index cb7613b8..2bdf85dc 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -272,6 +272,7 @@ local function message_handler(event, c2s) -- And stash it local ok, id = archive:append(store_user, nil, stanza, time_now(), with); if ok then + stanza:tag("stanza-id", { xmlns = "urn:xmpp:sid:0", by = store_user.."@"..host, id = id }):up(); if cleanup then cleanup[store_user] = true; end module:fire_event("archive-message-added", { origin = origin, stanza = stanza, for_user = store_user, id = id }); end -- cgit v1.2.3 From 6de5f5881dc2f35fbc0704f4260f271e6eb61848 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 22 Nov 2016 18:13:21 +0100 Subject: mod_storage_sql: Return number of affected items from archive:delete --- plugins/mod_storage_sql.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index 819846bb..a79f30fc 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -287,7 +287,7 @@ end function archive_store:delete(username, query) query = query or {}; local user,store = username,self.store; - return engine:transaction(function() + local stmt, err = engine:transaction(function() local sql_query = "DELETE FROM `prosodyarchive` WHERE %s;"; local args = { host, user or "", store, }; local where = { "`host` = ?", "`user` = ?", "`store` = ?", }; @@ -300,6 +300,7 @@ function archive_store:delete(username, query) sql_query = sql_query:format(t_concat(where, " AND ")); return engine:delete(sql_query, unpack(args)); end); + return stmt and stmt:affected() or nil, err; end local stores = { -- cgit v1.2.3 From 3e669af789f29807304f4494ea3090f3165647c4 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 22 Nov 2016 18:19:55 +0100 Subject: mod_mam: If archive:delete() gives a number, this should be the number of deleted items --- plugins/mod_mam/mod_mam.lua | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index 2bdf85dc..9d7aabde 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -326,11 +326,8 @@ if cleanup_after ~= "never" then local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; }) if not ok then module:log("warn", "Could not expire archives for user %s: %s", user, err); - else - -- :affected() is a recent addition for eg SQLite3 in LuaDBI - pcall(function(stmt) - module:log("debug", "Removed %d messages", stmt:affected()); - end, err); + elseif type(ok) == "number" then + module:log("debug", "Removed %d messages", ok); end cleanup[user] = nil; end -- cgit v1.2.3 From 687687507ca6e0acbf744f56a28ba05d96653c1c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 19 Dec 2016 08:44:56 +0100 Subject: mod_mam: Allow a set of namespaces to be stripped from stored stanzas, default to chat states (fixes #763) --- plugins/mod_mam/mod_mam.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index 9d7aabde..898b81c8 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -32,6 +32,7 @@ local global_default_policy = module:get_option("default_archive_policy", true); if global_default_policy ~= "roster" then global_default_policy = module:get_option_boolean("default_archive_policy", global_default_policy); end +local strip_tags = module:get_option_set("dont_archive_namespaces", { "http://jabber.org/protocol/chatstates" }); local archive_store = "archive2"; local archive = assert(module:open_store(archive_store, "archive")); @@ -265,6 +266,20 @@ local function message_handler(event, c2s) end end + if not strip_tags:empty() then + stanza = st.clone(stanza); + stanza:maptags(function (tag) + if strip_tags:contains(tag.attr.xmlns) then + return nil; + else + return tag; + end + end); + if #stanza.tags == 0 then + return; + end + end + -- Check with the users preferences if shall_store(store_user, with) then log("debug", "Archiving stanza: %s", stanza:top_tag()); -- cgit v1.2.3 From 50b3fb881cfa8813a98c52923c1534d6dde2b2ea Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 11 Jan 2017 13:08:50 +0100 Subject: mod_mam: Change store name to "archive" but make it configurable for people who have data in "archive2" --- plugins/mod_mam/mamprefs.lib.lua | 3 ++- plugins/mod_mam/mod_mam.lua | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/mod_mam/mamprefs.lib.lua b/plugins/mod_mam/mamprefs.lib.lua index 98e74bd6..f9b4b129 100644 --- a/plugins/mod_mam/mamprefs.lib.lua +++ b/plugins/mod_mam/mamprefs.lib.lua @@ -16,7 +16,8 @@ do end local sessions = hosts[module.host].sessions; -local prefs = module:open_store("archive2_prefs"); +local archive_store = module:get_option_string("archive_store", "archive"); +local prefs = module:open_store(archive_store .. "_prefs"); local function get_prefs(user) local user_sessions = sessions[user]; diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index 898b81c8..cff542bb 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -34,7 +34,7 @@ if global_default_policy ~= "roster" then end local strip_tags = module:get_option_set("dont_archive_namespaces", { "http://jabber.org/protocol/chatstates" }); -local archive_store = "archive2"; +local archive_store = module:get_option_string("archive_store", "archive"); local archive = assert(module:open_store(archive_store, "archive")); if archive.name == "null" or not archive.find then -- cgit v1.2.3 From 70b00611ce793bf4a2fb557a5fa5342babbdabb1 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 23 Jan 2017 19:18:15 +0100 Subject: mod_mam: Normalize copyright headers --- plugins/mod_mam/fallback_archive.lib.lua | 8 ++++++++ plugins/mod_mam/mamprefs.lib.lua | 10 ++++++++-- plugins/mod_mam/mamprefsxml.lib.lua | 10 ++++++++-- plugins/mod_mam/mod_mam.lua | 10 ++++++++-- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/plugins/mod_mam/fallback_archive.lib.lua b/plugins/mod_mam/fallback_archive.lib.lua index 92155bef..975f99b3 100644 --- a/plugins/mod_mam/fallback_archive.lib.lua +++ b/plugins/mod_mam/fallback_archive.lib.lua @@ -1,3 +1,11 @@ +-- Prosody IM +-- Copyright (C) 2008-2017 Matthew Wild +-- Copyright (C) 2008-2017 Waqas Hussain +-- Copyright (C) 2011-2017 Kim Alvefur +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- -- luacheck: ignore 212/self local uuid = require "util.uuid".generate; diff --git a/plugins/mod_mam/mamprefs.lib.lua b/plugins/mod_mam/mamprefs.lib.lua index f9b4b129..3fccf645 100644 --- a/plugins/mod_mam/mamprefs.lib.lua +++ b/plugins/mod_mam/mamprefs.lib.lua @@ -1,7 +1,13 @@ +-- Prosody IM +-- Copyright (C) 2008-2017 Matthew Wild +-- Copyright (C) 2008-2017 Waqas Hussain +-- Copyright (C) 2011-2017 Kim Alvefur +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- -- XEP-0313: Message Archive Management for Prosody --- Copyright (C) 2011-2013 Kim Alvefur -- --- This file is MIT/X11 licensed. local global_default_policy = module:get_option("default_archive_policy", true); diff --git a/plugins/mod_mam/mamprefsxml.lib.lua b/plugins/mod_mam/mamprefsxml.lib.lua index ec728e55..0598bbcd 100644 --- a/plugins/mod_mam/mamprefsxml.lib.lua +++ b/plugins/mod_mam/mamprefsxml.lib.lua @@ -1,7 +1,13 @@ +-- Prosody IM +-- Copyright (C) 2008-2017 Matthew Wild +-- Copyright (C) 2008-2017 Waqas Hussain +-- Copyright (C) 2011-2017 Kim Alvefur +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- -- XEP-0313: Message Archive Management for Prosody --- Copyright (C) 2011-2013 Kim Alvefur -- --- This file is MIT/X11 licensed. local st = require"util.stanza"; local xmlns_mam = "urn:xmpp:mam:1"; diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index cff542bb..69977813 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -1,7 +1,13 @@ +-- Prosody IM +-- Copyright (C) 2008-2017 Matthew Wild +-- Copyright (C) 2008-2017 Waqas Hussain +-- Copyright (C) 2011-2017 Kim Alvefur +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- -- XEP-0313: Message Archive Management for Prosody --- Copyright (C) 2011-2016 Kim Alvefur -- --- This file is MIT/X11 licensed. local xmlns_mam = "urn:xmpp:mam:1"; local xmlns_delay = "urn:xmpp:delay"; -- cgit v1.2.3 From 8684e8888767d97bfbae0cf1e269ccf4ac794358 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 23 Jan 2017 19:18:41 +0100 Subject: util.rsm: Add copyright header --- util/rsm.lua | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/util/rsm.lua b/util/rsm.lua index 7d5fbcab..40a78fb5 100644 --- a/util/rsm.lua +++ b/util/rsm.lua @@ -1,3 +1,14 @@ +-- Prosody IM +-- Copyright (C) 2008-2017 Matthew Wild +-- Copyright (C) 2008-2017 Waqas Hussain +-- Copyright (C) 2011-2017 Kim Alvefur +-- +-- This project is MIT/X11 licensed. Please see the +-- COPYING file in the source package for more information. +-- +-- XEP-0313: Message Archive Management for Prosody +-- + local stanza = require"util.stanza".stanza; local tostring, tonumber = tostring, tonumber; local type = type; -- cgit v1.2.3 From bec4571527a97d0a2b2954db29ce96d8fe0efe35 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 23 Jan 2017 19:24:16 +0100 Subject: CHANGES: Add mod_mam --- CHANGES | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES b/CHANGES index 6c982ee4..8b1bc13e 100644 --- a/CHANGES +++ b/CHANGES @@ -16,6 +16,7 @@ New features - mod\_carbons (XEP-0280) - Pluggable connection timeout handling - mod\_websocket (RFC 7395) +- mod\_mam (XEP-0313) Removed ------- -- cgit v1.2.3 From 9f5d98c28311f7fa1b5dd65fcba560d3d8349ac4 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 23 Jan 2017 19:31:03 +0100 Subject: mod_mam: Remove unused variables [luacheck] --- plugins/mod_mam/fallback_archive.lib.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/mod_mam/fallback_archive.lib.lua b/plugins/mod_mam/fallback_archive.lib.lua index 975f99b3..71e65274 100644 --- a/plugins/mod_mam/fallback_archive.lib.lua +++ b/plugins/mod_mam/fallback_archive.lib.lua @@ -73,7 +73,6 @@ function archive_store:delete(username, query) local archive = store[username]; if not archive then return true; end -- no messages, nothing to delete - local start, stop, step = 1, archive[0] or #archive, 1; local qstart = query.start or -math.huge; local qend = query["end"] or math.huge; local qwith = query.with; -- cgit v1.2.3 From 4b829743e65d70698af2a368414664222fbf6ae6 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 23 Jan 2017 19:31:30 +0100 Subject: mod_mam: Silence warning about unused table used to describe data format [luacheck] --- plugins/mod_mam/mamprefs.lib.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/mod_mam/mamprefs.lib.lua b/plugins/mod_mam/mamprefs.lib.lua index 3fccf645..054b6861 100644 --- a/plugins/mod_mam/mamprefs.lib.lua +++ b/plugins/mod_mam/mamprefs.lib.lua @@ -12,6 +12,7 @@ local global_default_policy = module:get_option("default_archive_policy", true); do + -- luacheck: ignore 211/prefs_format local prefs_format = { [false] = "roster", -- default ::= true | false | "roster" -- cgit v1.2.3 From ffde63fe687f5ffecf5fdec28358d1774a8a7169 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 23 Jan 2017 19:32:07 +0100 Subject: mod_mam: Use correct local name for util.jid.prepped_split --- plugins/mod_mam/mod_mam.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/mod_mam/mod_mam.lua b/plugins/mod_mam/mod_mam.lua index 69977813..f6ca52c0 100644 --- a/plugins/mod_mam/mod_mam.lua +++ b/plugins/mod_mam/mod_mam.lua @@ -249,7 +249,7 @@ local function message_handler(event, c2s) -- Filter out that claim to be from us stanza:maptags(function (tag) if tag.name == "stanza-id" and tag.attr.xmlns == "urn:xmpp:sid:0" then - local by_user, by_host, res = prepped_split(tag.attr.by); + local by_user, by_host, res = jid_prepped_split(tag.attr.by); if not res and by_host == module.host and by_user == store_user then return nil; end -- cgit v1.2.3