comparison mod_mamsub/mod_mamsub.lua @ 1747:985e05ac833b

mod_mamsub: Experimental implementation of MAM subscriptions
author Kim Alvefur <zash@zash.se>
date Mon, 18 May 2015 03:27:08 +0200
parents
children 0697fbef9134
comparison
equal deleted inserted replaced
1746:5734a6199938 1747:985e05ac833b
1 -- MAM Subscriptions prototype
2 -- Copyright (C) 2015 Kim Alvefur
3 --
4 -- This file is MIT/X11 licensed.
5
6 local mt = require"util.multitable";
7 local st = require"util.stanza";
8
9 local xmlns_mamsub = "http://prosody.im/protocol/mamsub";
10
11 module:add_feature(xmlns_mamsub);
12
13 local host_sessions = prosody.hosts[module.host].sessions;
14
15 local weak = { __mode = "k" };
16
17 module:hook("iq-set/self/"..xmlns_mamsub..":subscribe", function (event)
18 local origin, stanza = event.origin, event.stanza;
19 if origin.mamsub ~= nil then
20 origin.send(st.error_reply(stanza, "modify", "conflict"));
21 return true;
22 end
23 origin.mamsub = xmlns_mamsub;
24 local mamsub_sessions = host_sessions[origin.username].mamsub_sessions;
25 if not mamsub_sessions then
26 mamsub_sessions = setmetatable({}, weak);
27 host_sessions[origin.username].mamsub_sessions = mamsub_sessions;
28 end
29 mamsub_sessions[origin] = true;
30 origin.send(st.reply(stanza));
31 return true;
32 end);
33
34 module:hook("iq-set/self/"..xmlns_mamsub..":unsubscribe", function (event)
35 local origin, stanza = event.origin, event.stanza;
36 if origin.mamsub ~= xmlns_mamsub then
37 origin.send(st.error_reply(stanza, "modify", "conflict"));
38 return true;
39 end
40 origin.mamsub = nil;
41 local mamsub_sessions = host_sessions[origin.username].mamsub_sessions;
42 if mamsub_sessions then
43 mamsub_sessions[origin] = nil;
44 end
45 origin.send(st.reply(stanza));
46 return true;
47 end);
48
49 module:hook("archive-message-added", function (event)
50 local mamsub_sessions = host_sessions[event.for_user].mamsub_sessions;
51 if not mamsub_sessions then return end;
52
53 local for_broadcast = st.message():tag("mamsub", { xmlns = xmlns_mamsub })
54 :tag("forwarded", { xmlns = "urn:xmpp:forward:0" })
55 :add_child(event.stanza);
56
57 for session in pairs(mamsub_sessions) do
58 if session.mamsub == xmlns_mamsub then
59 for_broadcast.attr.to = session.full_jid;
60 session.send(for_broadcast);
61 end
62 end
63 end);