annotate mod_muc_intercom/mod_muc_intercom.lua @ 258:36648205b10a

mod_smacks: Add commented log statement for future debugging
author Matthew Wild <mwild1@gmail.com>
date Thu, 07 Oct 2010 16:19:09 +0100
parents fbddb9db1c82
children 7410d1005fea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
247
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- Relay messages between rooms
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- By Kim Alvefur <zash@zash.se>
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local host_session = prosody.hosts[module.host];
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local st_msg = require "util.stanza".message;
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local jid = require "util.jid";
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 function check_message(data)
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local origin, stanza = data.origin, data.stanza;
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local muc_rooms = host_session.muc and host_session.muc.rooms;
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 if not muc_rooms then return; end
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local this_room = muc_rooms[stanza.attr.to];
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 if not this_room then return; end -- no such room
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local from_room_jid = this_room._jid_nick[stanza.attr.from];
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 if not from_room_jid then return; end -- no such nick
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local from_room, from_host, from_nick = jid.split(from_room_jid);
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local body = stanza:get_child("body");
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 body = body and body:get_text(); -- I feel like I want to do `or ""` there :/
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local target_room, message = body:match("^@([^:]+):(.*)");
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 if not target_room or not message then return; end
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 if target_room == from_room then return; end -- don't route to itself
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 module:log("debug", "target room is %s", target_room);
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local bare_room = jid.join(target_room, from_host);
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 if not muc_rooms[bare_room] then return; end -- TODO send a error
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 module:log("info", "message from %s in %s to %s", from_nick, from_room, target_room);
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 local sender = jid.join(target_room, module.host, from_room .. "/" .. from_nick);
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local forward_stanza = st_msg({from = sender, to = bare_room, type = "groupchat"}, message);
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 module:log("debug", "broadcasting message to target room");
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 muc_rooms[bare_room]:broadcast_message(forward_stanza);
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
fbddb9db1c82 adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 module:hook("message/bare", check_message);