comparison mod_muc_intercom/mod_muc_intercom.lua @ 1425:9c894b56b4e4

mod_muc_intercom: Make compatible with new MUC API.
author Waqas Hussain <waqas20@gmail.com>
date Fri, 30 May 2014 18:54:31 -0400
parents 108ac6f16d2d
children 6d3935226ffb
comparison
equal deleted inserted replaced
1424:9892a537e7fc 1425:9c894b56b4e4
4 local host_session = prosody.hosts[module.host]; 4 local host_session = prosody.hosts[module.host];
5 local st_msg = require "util.stanza".message; 5 local st_msg = require "util.stanza".message;
6 local jid = require "util.jid"; 6 local jid = require "util.jid";
7 local now = require "util.datetime".datetime; 7 local now = require "util.datetime".datetime;
8 8
9 local function get_room_by_jid(mod_muc, jid)
10 if mod_muc.get_room_by_jid then
11 return mod_muc.get_room_by_jid(jid);
12 elseif mod_muc.rooms then
13 return mod_muc.rooms[jid]; -- COMPAT 0.9, 0.10
14 end
15 end
16
9 function check_message(data) 17 function check_message(data)
10 local origin, stanza = data.origin, data.stanza; 18 local origin, stanza = data.origin, data.stanza;
11 local muc_rooms = host_session.muc and host_session.muc.rooms; 19 local mod_muc = host_session.muc;
12 if not muc_rooms then return; end 20 if not mod_muc then return; end
13 21
14 local this_room = muc_rooms[stanza.attr.to]; 22 local this_room = get_room_by_jid(mod_muc, stanza.attr.to);
15 if not this_room then return; end -- no such room 23 if not this_room then return; end -- no such room
16 24
17 local from_room_jid = this_room._jid_nick[stanza.attr.from]; 25 local from_room_jid = this_room._jid_nick[stanza.attr.from];
18 if not from_room_jid then return; end -- no such nick 26 if not from_room_jid then return; end -- no such nick
19 27
27 35
28 if target_room == from_room then return; end -- don't route to itself 36 if target_room == from_room then return; end -- don't route to itself
29 module:log("debug", "target room is %s", target_room); 37 module:log("debug", "target room is %s", target_room);
30 38
31 local bare_room = jid.join(target_room, from_host); 39 local bare_room = jid.join(target_room, from_host);
32 if not muc_rooms[bare_room] then return; end -- TODO send a error 40 local dest_room = get_room_by_jid(mod_muc, bare_room);
41 if not dest_room then return; end -- TODO send a error
33 module:log("info", "message from %s in %s to %s", from_nick, from_room, target_room); 42 module:log("info", "message from %s in %s to %s", from_nick, from_room, target_room);
34 43
35 local sender = jid.join(target_room, module.host, from_room .. "/" .. from_nick); 44 local sender = jid.join(target_room, module.host, from_room .. "/" .. from_nick);
36 local forward_stanza = st_msg({from = sender, to = bare_room, type = "groupchat"}, message); 45 local forward_stanza = st_msg({from = sender, to = bare_room, type = "groupchat"}, message);
37 forward_stanza:tag("delay", { xmlns = 'urn:xmpp:delay', from = from_room_jid, stamp = now() }):up(); 46 forward_stanza:tag("delay", { xmlns = 'urn:xmpp:delay', from = from_room_jid, stamp = now() }):up();
38 47
39 module:log("debug", "broadcasting message to target room"); 48 module:log("debug", "broadcasting message to target room");
40 muc_rooms[bare_room]:broadcast_message(forward_stanza); 49 dest_room:broadcast_message(forward_stanza);
41 end 50 end
42 51
43 module:hook("message/bare", check_message, 10); 52 module:hook("message/bare", check_message, 10);