Software /
code /
prosody-modules
Comparison
mod_muc_intercom/mod_muc_intercom.lua @ 247:fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 24 Aug 2010 18:53:05 +0200 |
child | 253:7410d1005fea |
comparison
equal
deleted
inserted
replaced
246:5a389f9f2ec7 | 247:fbddb9db1c82 |
---|---|
1 -- Relay messages between rooms | |
2 -- By Kim Alvefur <zash@zash.se> | |
3 | |
4 local host_session = prosody.hosts[module.host]; | |
5 local st_msg = require "util.stanza".message; | |
6 local jid = require "util.jid"; | |
7 | |
8 function check_message(data) | |
9 local origin, stanza = data.origin, data.stanza; | |
10 local muc_rooms = host_session.muc and host_session.muc.rooms; | |
11 if not muc_rooms then return; end | |
12 | |
13 local this_room = muc_rooms[stanza.attr.to]; | |
14 if not this_room then return; end -- no such room | |
15 | |
16 local from_room_jid = this_room._jid_nick[stanza.attr.from]; | |
17 if not from_room_jid then return; end -- no such nick | |
18 | |
19 local from_room, from_host, from_nick = jid.split(from_room_jid); | |
20 | |
21 local body = stanza:get_child("body"); | |
22 body = body and body:get_text(); -- I feel like I want to do `or ""` there :/ | |
23 local target_room, message = body:match("^@([^:]+):(.*)"); | |
24 if not target_room or not message then return; end | |
25 | |
26 if target_room == from_room then return; end -- don't route to itself | |
27 module:log("debug", "target room is %s", target_room); | |
28 | |
29 local bare_room = jid.join(target_room, from_host); | |
30 if not muc_rooms[bare_room] then return; end -- TODO send a error | |
31 module:log("info", "message from %s in %s to %s", from_nick, from_room, target_room); | |
32 | |
33 local sender = jid.join(target_room, module.host, from_room .. "/" .. from_nick); | |
34 local forward_stanza = st_msg({from = sender, to = bare_room, type = "groupchat"}, message); | |
35 | |
36 module:log("debug", "broadcasting message to target room"); | |
37 muc_rooms[bare_room]:broadcast_message(forward_stanza); | |
38 end | |
39 | |
40 module:hook("message/bare", check_message); |