mod_storage_memory: Also merged into core
[prosody-modules.git] / mod_muc_intercom / mod_muc_intercom.lua
blob24509673b411ce907ae618ef5aa6c437cdd05b63
1 -- Relay messages between rooms
2 -- By Kim Alvefur <zash@zash.se>
4 local host_session = prosody.hosts[module.host];
5 local st_msg = require "util.stanza".message;
6 local jid = require "util.jid";
7 local now = require "util.datetime".datetime;
9 local function get_room_from_jid(mod_muc, jid)
10 if mod_muc.get_room_from_jid then
11 return mod_muc.get_room_from_jid(jid);
12 elseif mod_muc.rooms then
13 return mod_muc.rooms[jid]; -- COMPAT 0.9, 0.10
14 end
15 end
17 function check_message(data)
18 local origin, stanza = data.origin, data.stanza;
19 local mod_muc = host_session.muc;
20 if not mod_muc then return; end
22 local this_room = get_room_from_jid(mod_muc, stanza.attr.to);
23 if not this_room then return; end -- no such room
25 local from_room_jid = this_room._jid_nick[stanza.attr.from];
26 if not from_room_jid then return; end -- no such nick
28 local from_room, from_host, from_nick = jid.split(from_room_jid);
30 local body = stanza:get_child("body");
31 if not body then return; end -- No body, like topic changes
32 body = body and body:get_text(); -- I feel like I want to do `or ""` there :/
33 local target_room, message = body:match("^@([^:]+):(.*)");
34 if not target_room or not message then return; end
36 if target_room == from_room then return; end -- don't route to itself
37 module:log("debug", "target room is %s", target_room);
39 local bare_room = jid.join(target_room, from_host);
40 local dest_room = get_room_from_jid(mod_muc, bare_room);
41 if not dest_room then return; end -- TODO send a error
42 module:log("info", "message from %s in %s to %s", from_nick, from_room, target_room);
44 local sender = jid.join(target_room, module.host, from_room .. "/" .. from_nick);
45 local forward_stanza = st_msg({from = sender, to = bare_room, type = "groupchat"}, message);
46 forward_stanza:tag("delay", { xmlns = 'urn:xmpp:delay', from = from_room_jid, stamp = now() }):up();
48 module:log("debug", "broadcasting message to target room");
49 dest_room:broadcast_message(forward_stanza);
50 end
52 module:hook("message/bare", check_message);