Mercurial > prosody-modules
annotate mod_muc_intercom/mod_muc_intercom.lua @ 821:87e847280aef
mod_mam_muc_sql: SQL variant of mod_mam_muc
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 17 Sep 2012 20:15:27 +0200 |
parents | 108ac6f16d2d |
children | 9c894b56b4e4 |
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"; |
285
108ac6f16d2d
mod_muc_intercom: Add a delay to the forwarded message.
Kim Alvefur <zash@zash.se>
parents:
280
diff
changeset
|
7 local now = require "util.datetime".datetime; |
247
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 |
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 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
|
10 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
|
11 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
|
12 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
|
13 |
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 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
|
15 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
|
16 |
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 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
|
18 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
|
19 |
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 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
|
21 |
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local body = stanza:get_child("body"); |
253
7410d1005fea
mod_muc_intercom: Fix traceback on topic changes
Kim Alvefur <zash@zash.se>
parents:
247
diff
changeset
|
23 if not body then return; end -- No body, like topic changes |
247
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 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
|
25 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
|
26 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
|
27 |
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 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
|
29 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
|
30 |
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 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
|
32 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
|
33 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
|
34 |
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 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
|
36 local forward_stanza = st_msg({from = sender, to = bare_room, type = "groupchat"}, message); |
285
108ac6f16d2d
mod_muc_intercom: Add a delay to the forwarded message.
Kim Alvefur <zash@zash.se>
parents:
280
diff
changeset
|
37 forward_stanza:tag("delay", { xmlns = 'urn:xmpp:delay', from = from_room_jid, stamp = now() }):up(); |
247
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 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
|
40 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
|
41 end |
fbddb9db1c82
adds mod_muc_intercom; forwards messages between rooms on a muc host
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
280
6d72c5172c74
mod_muc_intercom: Increase priority.
Kim Alvefur <zash@zash.se>
parents:
253
diff
changeset
|
43 module:hook("message/bare", check_message, 10); |