Comparison

mod_minimix/mod_minimix.lua @ 2941:a57ed544fece

mod_minimix: Experiment in account-based MUC joins
author Kim Alvefur <zash@zash.se>
date Thu, 22 Mar 2018 14:33:46 +0100
child 3118:24e49391d4e8
comparison
equal deleted inserted replaced
2940:0167a102ed35 2941:a57ed544fece
1 -- mod_minimix
2 --
3 -- Rewrite MUC stanzas suich that the account / bare JID joins rooms instead of clients / full JIDs
4 --
5 local jid_split, jid_join, jid_node, jid_bare = import("util.jid", "split", "join", "node", "bare");
6 local st = require "util.stanza";
7
8 local users = prosody.hosts[module.host].sessions;
9
10 local joined_rooms = module:open_store("joined_rooms", "map"); -- TODO cache?
11 local room_state = module:open_store("joined_rooms_state", "map");
12 local all_room_state = module:open_store("joined_rooms_state");
13
14 -- FIXME You can join but you can never leave.
15
16 module:hook("pre-presence/full", function (event)
17 local origin, stanza = event.origin, event.stanza;
18
19 local room_node, room_host, nickname = jid_split(stanza.attr.to);
20 local room_jid = jid_join(room_node, room_host);
21 local username = origin.username;
22
23 if stanza.attr.type == nil and stanza:get_child("x", "http://jabber.org/protocol/muc") then
24 module:log("debug", "Joining %s as %s", room_jid, nickname);
25
26 -- TODO Should this be kept track of before the *initial* join has been confirmed or?
27 if origin.joined_rooms then
28 origin.joined_rooms[room_jid] = nickname;
29 else
30 origin.joined_rooms = { [room_jid] = nickname };
31 end
32
33 if joined_rooms:get(username, room_jid) then
34 module:log("debug", "Already joined to %s as %s", room_jid, nickname);
35 local state = assert(all_room_state:get(username));
36 for jid, stanza in pairs(state) do
37 if jid ~= room_jid and jid ~= stanza.attr.to then
38 origin.send(st.clone(st.deserialize(stanza)));
39 end
40 end
41 origin.send(st.deserialize(state[stanza.attr.to]));
42 origin.send(st.message({type="groupchat",to=origin.full_jid,from=room_jid}):tag("subject"):text(state[room_jid]));
43 -- Send on-join stanzas from local state, somehow
44 -- Maybe tell them their nickname was changed if it doesn't match the account one
45 return true;
46 end
47
48 joined_rooms:set(username, room_jid, nickname);
49
50 local account_join = st.clone(stanza);
51 account_join.attr.from = jid_join(origin.username, origin.host);
52 module:send(account_join);
53
54 return true;
55 elseif stanza.attr.type == "unavailable" and joined_rooms:get(username, room_jid) then
56 origin.send(st.reply(stanza));
57 return true;
58 end
59 end);
60
61 module:hook("pre-message/bare", function (event)
62 local origin, stanza = event.origin, event.stanza;
63 local username = origin.username;
64 local room_jid = jid_bare(stanza.attr.to);
65
66 module:log("info", "%s", stanza)
67 if joined_rooms:get(username, room_jid) then
68 local from_account = st.clone(stanza);
69 from_account.attr.from = jid_join(origin.username, origin.host);
70 module:log("debug", "Sending:\n%s\nInstead of:\n%s", from_account, stanza);
71 module:send(from_account, origin);
72 return true;
73 end
74 end);
75
76 local function handle_to_bare_jid(event)
77 local origin, stanza = event.origin, event.stanza;
78 local username = jid_node(stanza.attr.to);
79 local room_jid = jid_bare(stanza.attr.from);
80
81 if joined_rooms:get(username, room_jid) then
82 module:log("debug", "handle_to_bare_jid %q, %s", room_jid, stanza);
83 -- Broadcast to clients
84
85 if stanza.name == "message" and stanza.attr.type == "groupchat"
86 and not stanza:get_child("body") and stanza:get_child("subject") then
87 room_state:set(username, room_jid, stanza:get_child_text("subject"));
88 elseif stanza.name == "presence" then
89 if stanza.attr.type == nil then
90 room_state:set(username, stanza.attr.from, st.preserialize(stanza));
91 elseif stanza.attr.type == "unavailable" then
92 room_state:set(username, stanza.attr.from, nil);
93 end
94 end
95
96 if users[username] then
97 module:log("debug", "%s has sessions", username);
98 for _, session in pairs(users[username].sessions) do
99 module:log("debug", "Session: %s", jid_join(session.username, session.host, session.resource));
100 if session.joined_rooms and session.joined_rooms[room_jid] then
101 module:log("debug", "Is joined");
102 local s = st.clone(stanza);
103 s.attr.to = session.full_jid;
104 session.send(s);
105 else
106 module:log("debug", "session.joined_rooms = %s", session.joined_rooms);
107 end
108 end
109 end
110
111 return true;
112 end
113 end
114
115 module:hook("presence/bare", handle_to_bare_jid);
116 module:hook("message/bare", handle_to_bare_jid);