Software /
code /
prosody
Annotate
plugins/mod_muc.lua @ 666:27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Wed, 31 Dec 2008 10:16:42 +0500 |
child | 668:50072761e02d |
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; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 local jid_split = require "util.jid".split; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 local jid_bare = require "util.jid".bare; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 local st = require "util.stanza"; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 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
|
8 local multitable_new = require "util.multitable".new; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 local muc_domain = "conference."..module:get_host(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local muc_name = "MUCMUCMUC!!!"; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 local rooms = multitable_new(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 local jid_nick = multitable_new(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 local rooms_info = multitable_new(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 local component; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 function getUsingPath(stanza, path, getText) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 local tag = stanza; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 for _, name in ipairs(path) do |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 if type(tag) ~= 'table' then return; end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 tag = tag:child_with_name(name); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 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
|
26 return tag; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 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
|
29 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
|
30 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 function get_disco_info(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 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
|
33 :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
|
34 :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
|
35 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 function get_disco_items(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 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
|
38 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
|
39 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
|
40 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 return reply; -- TODO cache disco reply |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 function get_room_disco_info(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 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
|
45 :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
|
46 :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
|
47 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 function get_room_disco_items(stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 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
|
50 end -- TODO allow non-private rooms |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 function broadcast_presence(type, from, room, code) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 local data = rooms:get(room, from); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 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
|
55 :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
|
56 :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
|
57 if code then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 stanza:tag("status", {code=code}):up(); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 local me; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 local r = rooms:get(room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 if r then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 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
|
64 if occupant ~= from then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 stanza.attr.to = o_data.jid; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 core_route_stanza(component, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 me = o_data.jid; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 end |
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 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 if me then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 stanza:tag("status", {code='110'}); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 stanza.attr.to = me; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 core_route_stanza(component, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 function broadcast_message(from, room, subject, body) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 local r = rooms:get(room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 if r then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 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
|
85 stanza.attr.to = o_data.jid; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 core_route_stanza(component, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 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
|
92 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
|
93 local room = jid_bare(to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 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
|
95 local type = stanza.attr.type; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 if stanza.name == "presence" then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 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
|
98 local data = rooms:get(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 data.role = 'none'; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 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
|
101 rooms:remove(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 jid_nick:remove(from, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 elseif type == "unavailable" then -- unavailable |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 if current_nick == to then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 local data = rooms:get(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 data.role = 'none'; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 broadcast_presence('unavailable', to, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 rooms:remove(room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 jid_nick:remove(from, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 end -- TODO else do nothing? |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 elseif not type then -- available |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 if current_nick then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 if current_nick == to then -- simple presence |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 -- TODO broadcast |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 else -- change nick |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 if rooms:get(room, to) then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 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
|
118 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 local data = rooms:get(room, current_nick); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 broadcast_presence('unavailable', current_nick, room, '303'); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 rooms:remove(room, current_nick); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 rooms:set(room, to, data); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 jid_nick:set(from, room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 broadcast_presence(nil, to, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 else -- enter room |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 if rooms:get(room, to) then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 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
|
130 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 local data = {affiliation='none', role='participant', jid=from}; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 rooms:set(room, to, data); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 jid_nick:set(from, room, to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 local r = rooms:get(room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 if r then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 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
|
137 if occupant ~= from then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 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
|
139 :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
|
140 :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
|
141 core_route_stanza(component, pres); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 broadcast_presence(nil, to, room); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 elseif type ~= 'result' then -- bad type |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 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
|
150 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 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
|
152 -- groupchat messages not allowed in PM |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 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
|
154 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 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
|
156 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 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
|
160 local type = stanza.attr.type; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 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
|
162 local xmlns = stanza.tags[1].attr.xmlns; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 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
|
164 origin.send(get_room_disco_info(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 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
|
166 origin.send(get_room_disco_items(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 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
|
169 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 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
|
171 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
|
172 local room = jid_bare(to); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
173 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
|
174 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
|
175 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
|
176 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
177 broadcast_message(current_nick, room, getText(stanza, {"subject"}), getText(stanza, {"body"})); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
178 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
179 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 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
|
181 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
|
182 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 function handle_to_domain(origin, stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 local type = stanza.attr.type; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 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
|
188 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
|
189 local xmlns = stanza.tags[1].attr.xmlns; |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 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
|
191 origin.send(get_disco_info(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 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
|
193 origin.send(get_disco_items(stanza)); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 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
|
196 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 else |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 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
|
199 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
201 |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
202 component = register_component(muc_domain, function(origin, stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
203 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
|
204 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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 elseif to_resource then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 handle_to_occupant(origin, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
212 elseif to_node then |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
213 handle_to_room(origin, stanza) |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
214 else -- to the main muc domain |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
215 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
|
216 handle_to_domain(origin, stanza); |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 end |
27f76695f43b
Initial mod_muc: XEP-0045: Multi-User Chat
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 end); |