Software /
code /
prosody
Annotate
plugins/mod_muc.lua @ 752:a9642c1d5827
mod_muc: Add support for being a component
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 29 Jan 2009 02:14:16 +0000 |
parent | 711:7653519ec4f2 |
child | 756:2ca5fa47f317 |
rev | line source |
---|---|
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 local register_component = require "core.componentmanager".register_component; |
707
bb9583314ec7
mod_muc: deregister component on unload
Waqas Hussain <waqas20@gmail.com>
parents:
671
diff
changeset
|
4 local deregister_component = require "core.componentmanager".deregister_component; |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 local jid_split = require "util.jid".split; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 local jid_bare = require "util.jid".bare; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 local st = require "util.stanza"; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 local log = require "util.logger".init("mod_muc"); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 local multitable_new = require "util.multitable".new; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 |
752
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
11 if module:get_host_type() ~= "component" then |
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
12 error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0); |
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
13 end |
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
14 |
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
15 local muc_domain = module:get_host(); |
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
16 |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 local muc_name = "MUCMUCMUC!!!"; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
19 -- room_name -> room |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
20 -- occupant_room_nick -> data |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
21 -- affiliation = ... |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
22 -- role |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
23 -- jid = occupant's real jid |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 local rooms = multitable_new(); |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
25 |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
26 local jid_nick = multitable_new(); -- real jid -> room's jid -> room nick |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
27 |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
28 -- room_name -> info |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
29 -- name - the room's friendly name |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
30 -- subject - the room's subject |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
31 -- non-anonymous = true|nil |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
32 -- persistent = true|nil |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 local rooms_info = multitable_new(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
35 local persist_list = datamanager.load(nil, muc_domain, 'room_list') or {}; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
36 for room in pairs(persist_list) do |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
37 rooms_info:set(room, datamanager.store(room, muc_domain, 'rooms') or nil); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
38 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
39 |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 local component; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 function getUsingPath(stanza, path, getText) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 local tag = stanza; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 for _, name in ipairs(path) do |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 if type(tag) ~= 'table' then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 tag = tag:child_with_name(name); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 if tag and getText then tag = table.concat(tag); end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 return tag; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 function getTag(stanza, path) return getUsingPath(stanza, path); end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 function getText(stanza, path) return getUsingPath(stanza, path, true); end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 function get_disco_info(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info") |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 :tag("identity", {category='conference', type='text', name=muc_name}):up() |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 function get_disco_items(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items"); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 for room in pairs(rooms_info:get()) do |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 return reply; -- TODO cache disco reply |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 function get_room_disco_info(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info") |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 :tag("identity", {category='conference', type='text', name=rooms_info:get(stanza.attr.to, "name")}):up() |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 function get_room_disco_items(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items"); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 end -- TODO allow non-private rooms |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
75 function save_room(room) |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
76 local persistent = rooms_info:get(room, 'persistent'); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
77 if persistent then |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
78 datamanager.store(room, muc_domain, 'rooms', rooms_info:get(room)); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
79 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
80 if persistent ~= persist_list[room] then |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
81 if not persistent then |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
82 datamanager.store(room, muc_domain, 'rooms', nil); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
83 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
84 persist_list[room] = persistent; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
85 datamanager.store(nil, muc_domain, 'room_list', persist_list); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
86 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
87 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
88 |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
89 function set_subject(current_nick, room, subject) |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
90 -- TODO check nick's authority |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
91 if subject == "" then subject = nil; end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
92 rooms_info:set(room, 'subject', subject); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
93 save_room(); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
94 broadcast_message(current_nick, room, subject or "", nil); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
95 return true; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
96 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
97 |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 function broadcast_presence(type, from, room, code) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 local data = rooms:get(room, from); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 local stanza = st.presence({type=type, from=from}) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 :tag("item", {affiliation=data.affiliation, role=data.role}):up(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 if code then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 stanza:tag("status", {code=code}):up(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 local me; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 local r = rooms:get(room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 if r then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 for occupant, o_data in pairs(r) do |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 if occupant ~= from then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 stanza.attr.to = o_data.jid; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 core_route_stanza(component, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 me = o_data.jid; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 if me then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 stanza:tag("status", {code='110'}); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 stanza.attr.to = me; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 core_route_stanza(component, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 function broadcast_message(from, room, subject, body) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 local stanza = st.message({type='groupchat', from=from}); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 if subject then stanza:tag('subject'):text(subject):up(); end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 if body then stanza:tag('body'):text(body):up(); end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 local r = rooms:get(room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 if r then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 for occupant, o_data in pairs(r) do |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 stanza.attr.to = o_data.jid; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 core_route_stanza(component, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 function handle_to_occupant(origin, stanza) -- PM, vCards, etc |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 local from, to = stanza.attr.from, stanza.attr.to; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 local room = jid_bare(to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 local current_nick = jid_nick:get(from, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 local type = stanza.attr.type; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 if stanza.name == "presence" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 if type == "error" then -- error, kick em out! |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 local data = rooms:get(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 data.role = 'none'; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 broadcast_presence('unavailable', to, room); -- TODO also add <status>This participant is kicked from the room because he sent an error presence: badformed error stanza</status> |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 rooms:remove(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 jid_nick:remove(from, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 elseif type == "unavailable" then -- unavailable |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 if current_nick == to then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 local data = rooms:get(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 data.role = 'none'; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 broadcast_presence('unavailable', to, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 rooms:remove(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 jid_nick:remove(from, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 end -- TODO else do nothing? |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 elseif not type then -- available |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 if current_nick then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 if current_nick == to then -- simple presence |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 -- TODO broadcast |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 else -- change nick |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 if rooms:get(room, to) then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 origin.send(st.error_reply(stanza, "cancel", "conflict")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 local data = rooms:get(room, current_nick); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 broadcast_presence('unavailable', current_nick, room, '303'); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 rooms:remove(room, current_nick); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 rooms:set(room, to, data); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 jid_nick:set(from, room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 broadcast_presence(nil, to, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
171 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
172 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
173 else -- enter room |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 if rooms:get(room, to) then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
175 origin.send(st.error_reply(stanza, "cancel", "conflict")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
176 else |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
177 local data; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
178 if not rooms:get(room) and not rooms_info:get(room) then -- new room |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
179 data = {affiliation='owner', role='moderator', jid=from}; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
180 end |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
181 if not data then -- new occupant |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
182 data = {affiliation='none', role='participant', jid=from}; |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
183 end |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 rooms:set(room, to, data); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 jid_nick:set(from, room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 local r = rooms:get(room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 if r then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 for occupant, o_data in pairs(r) do |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 if occupant ~= from then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 local pres = st.presence({to=from, from=occupant}) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 core_route_stanza(component, pres); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
196 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 broadcast_presence(nil, to, room); |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
198 -- TODO send discussion history |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
199 if rooms_info:get(room, 'subject') then |
671
c7519f0c9a2c
mod_muc: Room subject should be sent only the newly joined occupant
Waqas Hussain <waqas20@gmail.com>
parents:
668
diff
changeset
|
200 core_route_stanza(component, st.message({type='groupchat', from=room, to=from}):tag("subject"):text(rooms_info:get(room, 'subject'))); |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
201 end |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
202 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
203 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
204 elseif type ~= 'result' then -- bad type |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error? |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
206 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
207 elseif stanza.name == "message" and type == "groupchat" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
208 -- groupchat messages not allowed in PM |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
209 origin.send(st.error_reply(stanza, "modify", "bad-request")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 origin.send(st.error_reply(stanza, "cancel", "not-implemented", "Private stanzas not implemented")); -- TODO route private stanza |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
212 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
213 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
214 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
215 function handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
216 local type = stanza.attr.type; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 if stanza.name == "iq" and type == "get" then -- disco requests |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 local xmlns = stanza.tags[1].attr.xmlns; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
219 if xmlns == "http://jabber.org/protocol/disco#info" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
220 origin.send(get_room_disco_info(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
221 elseif xmlns == "http://jabber.org/protocol/disco#items" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 origin.send(get_room_disco_items(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
223 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
224 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
225 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
226 elseif stanza.name == "message" and type == "groupchat" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 local from, to = stanza.attr.from, stanza.attr.to; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
228 local room = jid_bare(to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
229 local current_nick = jid_nick:get(from, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
230 if not current_nick then -- not in room |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
231 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
232 else |
668
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
233 local subject = getText(stanza, {"subject"}); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
234 if subject then |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
235 set_subject(current_nick, room, subject); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
236 else |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
237 broadcast_message(current_nick, room, nil, getText(stanza, {"body"})); |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
238 -- TODO add to discussion history |
50072761e02d
mod_muc: Room subjects stored, and room persistence code in place. First user now the owner.
Waqas Hussain <waqas20@gmail.com>
parents:
666
diff
changeset
|
239 end |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
240 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
241 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
242 if type == "error" or type == "result" then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
243 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
244 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
245 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
246 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
247 function handle_to_domain(origin, stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
248 local type = stanza.attr.type; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
249 if type == "error" or type == "result" then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
250 if stanza.name == "iq" and type == "get" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
251 local xmlns = stanza.tags[1].attr.xmlns; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
252 if xmlns == "http://jabber.org/protocol/disco#info" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
253 origin.send(get_disco_info(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
254 elseif xmlns == "http://jabber.org/protocol/disco#items" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
255 origin.send(get_disco_items(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
256 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
257 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
258 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
259 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
260 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it")); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
261 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
262 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
263 |
752
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
264 function handle_stanza(origin, stanza) |
666
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
265 local to_node, to_host, to_resource = jid_split(stanza.attr.to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
266 if stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
267 if type == "error" or type == "result" then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
268 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME what's appropriate? |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
269 elseif to_resource and not to_node then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
270 if type == "error" or type == "result" then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
271 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
272 elseif to_resource then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
273 handle_to_occupant(origin, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
274 elseif to_node then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
275 handle_to_room(origin, stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
276 else -- to the main muc domain |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
277 if type == "error" or type == "result" then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
278 handle_to_domain(origin, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
279 end |
752
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
280 end |
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
281 |
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
282 module.load_component = function() |
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
283 return handle_stanza; -- Return the function that we want to handle incoming stanzas |
a9642c1d5827
mod_muc: Add support for being a component
Matthew Wild <mwild1@gmail.com>
parents:
711
diff
changeset
|
284 end |
707
bb9583314ec7
mod_muc: deregister component on unload
Waqas Hussain <waqas20@gmail.com>
parents:
671
diff
changeset
|
285 |
711
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
286 module.unload = function() |
707
bb9583314ec7
mod_muc: deregister component on unload
Waqas Hussain <waqas20@gmail.com>
parents:
671
diff
changeset
|
287 deregister_component(muc_domain); |
bb9583314ec7
mod_muc: deregister component on unload
Waqas Hussain <waqas20@gmail.com>
parents:
671
diff
changeset
|
288 end |
711
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
289 module.save = function() |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
290 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list}; |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
291 end |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
292 module.restore = function(data) |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
293 rooms.data, jid_nick.data, rooms_info.data, persist_list = |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
294 data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {}; |
7653519ec4f2
mod_muc: Added unload, save and restore callbacks to allow reloading code while preserving state
Waqas Hussain <waqas20@gmail.com>
parents:
707
diff
changeset
|
295 end |