Software /
code /
prosody
Annotate
plugins/mod_muc.lua @ 1577:c0e6d11e35ce
net.server: Remove listener from listeners table when calling removeserver
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 23 Jul 2009 03:06:49 +0100 |
parent | 1522:569d58d21612 |
child | 1562:2b8674e87a24 |
rev | line source |
---|---|
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1398
diff
changeset
|
1 -- Prosody IM |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1398
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1398
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1398
diff
changeset
|
4 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1398
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1398
diff
changeset
|
6 -- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1398
diff
changeset
|
7 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1398
diff
changeset
|
8 |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
9 |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
10 if module:get_host_type() ~= "component" then |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
11 error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0); |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
12 end |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
13 |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
14 local muc_host = module:get_host(); |
1060
fb3b2de0eb2f
mod_muc: The default component name is now 'Chatrooms'
Waqas Hussain <waqas20@gmail.com>
parents:
1059
diff
changeset
|
15 local muc_name = "Chatrooms"; |
782 | 16 local history_length = 20; |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
17 |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
18 local muc_new_room = require "util.muc".new_room; |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
19 local register_component = require "core.componentmanager".register_component; |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
20 local deregister_component = require "core.componentmanager".deregister_component; |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
21 local jid_split = require "util.jid".split; |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
22 local st = require "util.stanza"; |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
23 |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
24 local rooms = {}; |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
25 local component; |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
26 local host_room = muc_new_room(muc_host); |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
27 host_room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end; |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
28 |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
29 local function get_disco_info(stanza) |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
30 return st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info") |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
31 :tag("identity", {category='conference', type='text', name=muc_name}):up() |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
32 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
33 end |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
34 local function get_disco_items(stanza) |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
35 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_host, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items"); |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
36 for jid, room in pairs(rooms) do |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
37 reply:tag("item", {jid=jid, name=jid}):up(); |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
38 end |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
39 return reply; -- TODO cache disco reply |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
40 end |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
41 |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
42 local function handle_to_domain(origin, stanza) |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
43 local type = stanza.attr.type; |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
44 if type == "error" or type == "result" then return; end |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
45 if stanza.name == "iq" and type == "get" then |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
46 local xmlns = stanza.tags[1].attr.xmlns; |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
47 if xmlns == "http://jabber.org/protocol/disco#info" then |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
48 origin.send(get_disco_info(stanza)); |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
49 elseif xmlns == "http://jabber.org/protocol/disco#items" then |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
50 origin.send(get_disco_items(stanza)); |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
51 else |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
52 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
53 end |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
54 else |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
55 host_room:handle_stanza(origin, stanza); |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
56 --origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it")); |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
57 end |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
58 end |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
59 |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
60 component = register_component(muc_host, function(origin, stanza) |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
61 local to_node, to_host, to_resource = jid_split(stanza.attr.to); |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
62 if to_node then |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
63 local bare = to_node.."@"..to_host; |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
64 if to_host == muc_host or bare == muc_host then |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
65 local room = rooms[bare]; |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
66 if not room then |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
67 room = muc_new_room(bare); |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
68 room.route_stanza = function(room, stanza) core_post_stanza(component, stanza); end; |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
69 rooms[bare] = room; |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
70 end |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
71 room:handle_stanza(origin, stanza); |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
72 else --[[not for us?]] end |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
73 return; |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
74 end |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
75 -- to the main muc domain |
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
76 handle_to_domain(origin, stanza); |
779
ec0eadf4e9ff
Changed mod_muc to work with changed component manager
Waqas Hussain <waqas20@gmail.com>
parents:
757
diff
changeset
|
77 end); |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
78 |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
79 module.unload = function() |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
80 deregister_component(muc_host); |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
81 end |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
82 module.save = function() |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
83 return {rooms = rooms}; |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
84 end |
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
85 module.restore = function(data) |
1398
141fec125af0
mod_muc: Now based on util.muc
Waqas Hussain <waqas20@gmail.com>
parents:
1352
diff
changeset
|
86 rooms = data.rooms or {}; |
756
2ca5fa47f317
mod_muc: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
752
diff
changeset
|
87 end |