Mon, 07 Sep 2009 20:33:24 +0500
MUC: Added support for requesting various lists (owner, admin, voice, etc).
1734 | 1 | -- Prosody IM |
2 | -- Copyright (C) 2008-2009 Matthew Wild | |
3 | -- Copyright (C) 2008-2009 Waqas Hussain | |
4 | -- | |
5 | -- This project is MIT/X11 licensed. Please see the | |
6 | -- COPYING file in the source package for more information. | |
7 | -- | |
8 | ||
9 | local datamanager = require "util.datamanager"; | |
10 | local datetime = require "util.datetime"; | |
11 | ||
12 | local jid_split = require "util.jid".split; | |
13 | local jid_bare = require "util.jid".bare; | |
14 | local st = require "util.stanza"; | |
15 | local log = require "util.logger".init("mod_muc"); | |
16 | local multitable_new = require "util.multitable".new; | |
17 | local t_insert, t_remove = table.insert, table.remove; | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
18 | local setmetatable = setmetatable; |
1734 | 19 | |
20 | local muc_domain = nil; --module:get_host(); | |
21 | local history_length = 20; | |
22 | ||
23 | ------------ | |
24 | local function filter_xmlns_from_array(array, filters) | |
25 | local count = 0; | |
26 | for i=#array,1,-1 do | |
27 | local attr = array[i].attr; | |
28 | if filters[attr and attr.xmlns] then | |
29 | t_remove(array, i); | |
30 | count = count + 1; | |
31 | end | |
32 | end | |
33 | return count; | |
34 | end | |
35 | local function filter_xmlns_from_stanza(stanza, filters) | |
36 | if filters then | |
37 | if filter_xmlns_from_array(stanza.tags, filters) ~= 0 then | |
38 | return stanza, filter_xmlns_from_array(stanza, filters); | |
39 | end | |
40 | end | |
41 | return stanza, 0; | |
42 | end | |
43 | local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true}; | |
44 | local function get_filtered_presence(stanza) | |
45 | return filter_xmlns_from_stanza(st.clone(stanza), presence_filters); | |
46 | end | |
47 | local kickable_error_conditions = { | |
48 | ["gone"] = true; | |
49 | ["internal-server-error"] = true; | |
50 | ["item-not-found"] = true; | |
51 | ["jid-malformed"] = true; | |
52 | ["recipient-unavailable"] = true; | |
53 | ["redirect"] = true; | |
54 | ["remote-server-not-found"] = true; | |
55 | ["remote-server-timeout"] = true; | |
56 | ["service-unavailable"] = true; | |
57 | }; | |
58 | local function get_kickable_error(stanza) | |
59 | for _, tag in ipairs(stanza.tags) do | |
60 | if tag.name == "error" and tag.attr.xmlns == "jabber:client" then | |
61 | for _, cond in ipairs(tag.tags) do | |
62 | if cond.attr.xmlns == "urn:ietf:params:xml:ns:xmpp-stanzas" then | |
63 | return kickable_error_conditions[cond.name] and cond.name; | |
64 | end | |
65 | end | |
66 | return true; -- malformed error message | |
67 | end | |
68 | end | |
69 | return true; -- malformed error message | |
70 | end | |
71 | local function getUsingPath(stanza, path, getText) | |
72 | local tag = stanza; | |
73 | for _, name in ipairs(path) do | |
74 | if type(tag) ~= 'table' then return; end | |
75 | tag = tag:child_with_name(name); | |
76 | end | |
77 | if tag and getText then tag = table.concat(tag); end | |
78 | return tag; | |
79 | end | |
80 | local function getTag(stanza, path) return getUsingPath(stanza, path); end | |
81 | local function getText(stanza, path) return getUsingPath(stanza, path, true); end | |
82 | ----------- | |
83 | ||
84 | --[[function get_room_disco_info(room, stanza) | |
85 | return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info") | |
86 | :tag("identity", {category='conference', type='text', name=room._data["name"]):up() | |
87 | :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply | |
88 | end | |
89 | function get_room_disco_items(room, stanza) | |
90 | return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items"); | |
91 | end -- TODO allow non-private rooms]] | |
92 | ||
93 | -- | |
94 | ||
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
95 | local room_mt = {}; |
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
96 | room_mt.__index = room_mt; |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
97 | |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
98 | function room_mt:get_default_role(affiliation) |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
99 | if affiliation == "owner" or affiliation == "admin" then |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
100 | return "moderator"; |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
101 | elseif affiliation == "member" or not affiliation then |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
102 | return "participant"; |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
103 | end |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
104 | end |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
105 | |
1736
98f833669d7f
MUC: Fixed function declarations.
Waqas Hussain <waqas20@gmail.com>
parents:
1735
diff
changeset
|
106 | function room_mt:broadcast_presence(stanza, code, nick) |
1734 | 107 | stanza = get_filtered_presence(stanza); |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
108 | local data = self._occupants[stanza.attr.from]; |
1734 | 109 | stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) |
110 | :tag("item", {affiliation=data.affiliation, role=data.role, nick=nick}):up(); | |
111 | if code then | |
112 | stanza:tag("status", {code=code}):up(); | |
113 | end | |
114 | local me; | |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
115 | for occupant, o_data in pairs(self._occupants) do |
1734 | 116 | if occupant ~= stanza.attr.from then |
117 | for jid in pairs(o_data.sessions) do | |
118 | stanza.attr.to = jid; | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
119 | self:route_stanza(stanza); |
1734 | 120 | end |
121 | else | |
122 | me = o_data; | |
123 | end | |
124 | end | |
125 | if me then | |
126 | stanza:tag("status", {code='110'}); | |
127 | for jid in pairs(me.sessions) do | |
128 | stanza.attr.to = jid; | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
129 | self:route_stanza(stanza); |
1734 | 130 | end |
131 | end | |
132 | end | |
1736
98f833669d7f
MUC: Fixed function declarations.
Waqas Hussain <waqas20@gmail.com>
parents:
1735
diff
changeset
|
133 | function room_mt:broadcast_message(stanza, historic) |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
134 | for occupant, o_data in pairs(self._occupants) do |
1734 | 135 | for jid in pairs(o_data.sessions) do |
136 | stanza.attr.to = jid; | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
137 | self:route_stanza(stanza); |
1734 | 138 | end |
139 | end | |
140 | if historic then -- add to history | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
141 | local history = self._data['history']; |
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
142 | if not history then history = {}; self._data['history'] = history; end |
1734 | 143 | -- stanza = st.clone(stanza); |
144 | stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203 | |
145 | stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated) | |
146 | t_insert(history, st.clone(st.preserialize(stanza))); | |
147 | while #history > history_length do t_remove(history, 1) end | |
148 | end | |
149 | end | |
1742
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
150 | function room_mt:broadcast_except_nick(stanza, nick) |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
151 | for nick, occupant in pairs(self._occupants) do |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
152 | if nick ~= nick then |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
153 | for jid in pairs(occupant.sessions) do |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
154 | stanza.attr.to = jid; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
155 | self:route_stanza(stanza); |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
156 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
157 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
158 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
159 | end |
1734 | 160 | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
161 | function room_mt:send_occupant_list(to) |
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
162 | local current_nick = self._jid_nick[to]; |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
163 | for occupant, o_data in pairs(self._occupants) do |
1734 | 164 | if occupant ~= current_nick then |
165 | local pres = get_filtered_presence(o_data.sessions[o_data.jid]); | |
166 | pres.attr.to, pres.attr.from = to, occupant; | |
167 | pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) | |
168 | :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up(); | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
169 | self:route_stanza(pres); |
1734 | 170 | end |
171 | end | |
172 | end | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
173 | function room_mt:send_history(to) |
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
174 | local history = self._data['history']; -- send discussion history |
1734 | 175 | if history then |
176 | for _, msg in ipairs(history) do | |
177 | msg = st.deserialize(msg); | |
178 | msg.attr.to=to; | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
179 | self:route_stanza(msg); |
1734 | 180 | end |
181 | end | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
182 | if self._data['subject'] then |
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
183 | self:route_stanza(st.message({type='groupchat', from=self.jid, to=to}):tag("subject"):text(self._data['subject'])); |
1734 | 184 | end |
185 | end | |
186 | ||
187 | local function room_get_disco_info(self, stanza) end | |
188 | local function room_get_disco_items(self, stanza) end | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
189 | function room_mt:set_subject(current_nick, subject) |
1734 | 190 | -- TODO check nick's authority |
191 | if subject == "" then subject = nil; end | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
192 | self._data['subject'] = subject; |
1734 | 193 | local msg = st.message({type='groupchat', from=current_nick}) |
194 | :tag('subject'):text(subject):up(); | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
195 | self:broadcast_message(msg, false); |
1734 | 196 | return true; |
197 | end | |
198 | ||
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
199 | function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc |
1734 | 200 | local from, to = stanza.attr.from, stanza.attr.to; |
201 | local room = jid_bare(to); | |
202 | local current_nick = self._jid_nick[from]; | |
203 | local type = stanza.attr.type; | |
204 | log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag()); | |
205 | if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end | |
206 | if stanza.name == "presence" then | |
207 | local pr = get_filtered_presence(stanza); | |
208 | pr.attr.from = current_nick; | |
209 | if type == "error" then -- error, kick em out! | |
210 | if current_nick then | |
211 | log("debug", "kicking %s from %s", current_nick, room); | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
212 | self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}) |
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
213 | :tag('status'):text('This participant is kicked from the room because he sent an error presence')); -- send unavailable |
1734 | 214 | end |
215 | elseif type == "unavailable" then -- unavailable | |
216 | if current_nick then | |
217 | log("debug", "%s leaving %s", current_nick, room); | |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
218 | local data = self._occupants[current_nick]; |
1734 | 219 | data.role = 'none'; |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
220 | self:broadcast_presence(pr); |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
221 | self._occupants[current_nick] = nil; |
1734 | 222 | self._jid_nick[from] = nil; |
223 | end | |
224 | elseif not type then -- available | |
225 | if current_nick then | |
226 | --if #pr == #stanza or current_nick ~= to then -- commented because google keeps resending directed presence | |
227 | if current_nick == to then -- simple presence | |
228 | log("debug", "%s broadcasted presence", current_nick); | |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
229 | self._occupants[current_nick].sessions[from] = pr; |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
230 | self:broadcast_presence(pr); |
1734 | 231 | else -- change nick |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
232 | if self._occupants[to] then |
1734 | 233 | log("debug", "%s couldn't change nick", current_nick); |
234 | origin.send(st.error_reply(stanza, "cancel", "conflict"):tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
235 | else | |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
236 | local data = self._occupants[current_nick]; |
1734 | 237 | local to_nick = select(3, jid_split(to)); |
238 | if to_nick then | |
239 | log("debug", "%s (%s) changing nick to %s", current_nick, data.jid, to); | |
240 | local p = st.presence({type='unavailable', from=current_nick}); | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
241 | self:broadcast_presence(p, '303', to_nick); |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
242 | self._occupants[current_nick] = nil; |
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
243 | self._occupants[to] = data; |
1734 | 244 | self._jid_nick[from] = to; |
245 | pr.attr.from = to; | |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
246 | self._occupants[to].sessions[from] = pr; |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
247 | self:broadcast_presence(pr); |
1734 | 248 | else |
249 | --TODO malformed-jid | |
250 | end | |
251 | end | |
252 | end | |
253 | --else -- possible rejoin | |
254 | -- log("debug", "%s had connection replaced", current_nick); | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
255 | -- self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}) |
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
256 | -- :tag('status'):text('Replaced by new connection'):up()); -- send unavailable |
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
257 | -- self:handle_to_occupant(origin, stanza); -- resend available |
1734 | 258 | --end |
259 | else -- enter room | |
260 | local new_nick = to; | |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
261 | if self._occupants[to] then |
1734 | 262 | new_nick = nil; |
263 | end | |
264 | if not new_nick then | |
265 | log("debug", "%s couldn't join due to nick conflict: %s", from, to); | |
266 | origin.send(st.error_reply(stanza, "cancel", "conflict"):tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
267 | else | |
268 | log("debug", "%s joining as %s", from, to); | |
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
269 | if not next(self._affiliations) then -- new room, no owners |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
270 | self._affiliations[jid_bare(from)] = "owner"; |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
271 | end |
1740
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
272 | local affiliation = self:get_affiliation(from); |
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
273 | local role = self:get_default_role(affiliation) |
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
274 | if role then -- new occupant |
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
275 | self._occupants[to] = {affiliation=affiliation, role=role, jid=from, sessions={[from]=get_filtered_presence(stanza)}}; |
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
276 | self._jid_nick[from] = to; |
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
277 | self:send_occupant_list(from); |
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
278 | pr.attr.from = to; |
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
279 | self:broadcast_presence(pr); |
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
280 | self:send_history(from); |
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
281 | else -- banned |
b37ccf9bec89
MUC: Send appropriate error to banned users on join.
Waqas Hussain <waqas20@gmail.com>
parents:
1739
diff
changeset
|
282 | origin.send(st.error_reply(stanza, "auth", "forbidden"):tag("x", {xmlns = "http://jabber.org/protocol/muc"})); |
1734 | 283 | end |
284 | end | |
285 | end | |
286 | elseif type ~= 'result' then -- bad type | |
287 | origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error? | |
288 | end | |
289 | elseif not current_nick and type ~= "error" and type ~= "result" then -- not in room | |
290 | origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | |
291 | elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM | |
292 | origin.send(st.error_reply(stanza, "modify", "bad-request")); | |
293 | elseif stanza.name == "message" and type == "error" and get_kickable_error(stanza) then | |
294 | log("debug", "%s kicked from %s for sending an error message", current_nick, room); | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
295 | self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('This participant is kicked from the room because he sent an error message to another occupant')); -- send unavailable |
1734 | 296 | else -- private stanza |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
297 | local o_data = self._occupants[to]; |
1734 | 298 | if o_data then |
299 | log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid); | |
300 | local jid = o_data.jid; | |
301 | -- TODO if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end | |
302 | stanza.attr.to, stanza.attr.from = jid, current_nick; | |
303 | self:route_stanza(stanza); | |
304 | elseif type ~= "error" and type ~= "result" then -- recipient not in room | |
305 | origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); | |
306 | end | |
307 | end | |
308 | end | |
309 | ||
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
310 | function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc |
1734 | 311 | local type = stanza.attr.type; |
312 | if stanza.name == "iq" and type == "get" then -- disco requests | |
313 | local xmlns = stanza.tags[1].attr.xmlns; | |
314 | if xmlns == "http://jabber.org/protocol/disco#info" then | |
315 | origin.send(room_get_disco_info(self, stanza)); | |
316 | elseif xmlns == "http://jabber.org/protocol/disco#items" then | |
317 | origin.send(room_get_disco_items(self, stanza)); | |
318 | else | |
319 | origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
320 | end | |
1744
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
321 | elseif stanza.name == "iq" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#admin" then |
1742
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
322 | local actor = stanza.attr.from; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
323 | local affiliation = self:get_affiliation(actor); |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
324 | local current_nick = self._jid_nick[actor]; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
325 | local role = current_nick and self._occupants[current_nick].role or self:get_default_role(affiliation); |
1744
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
326 | local item = stanza.tags[1] and stanza.tags[1].tags[1]; |
1742
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
327 | if item and item.name == "item" then |
1744
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
328 | if type == "set" then |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
329 | local callback = function() origin.send(st.reply(stanza)); end |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
330 | if item.attr.affiliation and item.attr.jid and not item.attr.role and not item.attr.nick then |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
331 | local success, errtype, err = self:set_affiliation(actor, item.attr.jid, item.attr.affiliation, callback); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
332 | if not success then origin.send(st.error_reply(stanza, errtype, err)); end |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
333 | elseif item.attr.role and item.attr.nick and not item.attr.affiliation and not item.attr.jid then |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
334 | local success, errtype, err = self:set_role(actor, item.attr.nick, item.attr.role, callback); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
335 | if not success then origin.send(st.error_reply(stanza, errtype, err)); end |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
336 | else |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
337 | origin.send(st.error_reply(stanza, "cancel", "bad-request")); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
338 | end |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
339 | elseif type == "get" then |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
340 | local _aff = item.attr.affiliation; |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
341 | local _rol = item.attr.role; |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
342 | if _aff and not _rol then |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
343 | if affiliation == "owner" or (affiliation == "admin" and _aff ~= "owner" and _aff ~= "admin") then |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
344 | local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin"); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
345 | for jid, affiliation in pairs(self._affiliations) do |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
346 | if affiliation == _aff then |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
347 | reply:tag("item", {affiliation = _aff, jid = jid}):up(); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
348 | end |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
349 | end |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
350 | origin.send(reply); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
351 | else |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
352 | origin.send(st.error_reply(stanza, "auth", "forbidden")); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
353 | end |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
354 | elseif _rol and not _aff then |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
355 | if role == "moderator" then -- TODO allow admins and owners not in room? Provide read-only access to everyone who can see the participants anyway? |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
356 | if _rol == "none" then _rol = nil; end |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
357 | local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin"); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
358 | for nick, occupant in pairs(self._occupants) do |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
359 | if occupant.role == _rol then |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
360 | reply:tag("item", {nick = nick, role = _rol or "none", affiliation = occupant.affiliation or "none", jid = occupant.jid}):up(); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
361 | end |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
362 | end |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
363 | origin.send(reply); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
364 | else |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
365 | origin.send(st.error_reply(stanza, "auth", "forbidden")); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
366 | end |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
367 | else |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
368 | origin.send(st.error_reply(stanza, "cancel", "bad-request")); |
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
369 | end |
1742
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
370 | end |
1744
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
371 | elseif type == "set" or type == "get" then |
1742
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
372 | origin.send(st.error_reply(stanza, "cancel", "bad-request")); |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
373 | end |
1734 | 374 | elseif stanza.name == "message" and type == "groupchat" then |
375 | local from, to = stanza.attr.from, stanza.attr.to; | |
376 | local room = jid_bare(to); | |
377 | local current_nick = self._jid_nick[from]; | |
378 | if not current_nick then -- not in room | |
379 | origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | |
380 | else | |
381 | local from = stanza.attr.from; | |
382 | stanza.attr.from = current_nick; | |
383 | local subject = getText(stanza, {"subject"}); | |
384 | if subject then | |
385 | self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza | |
386 | else | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
387 | self:broadcast_message(stanza, true); |
1734 | 388 | end |
389 | end | |
390 | elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick | |
391 | local to = stanza.attr.to; | |
392 | local current_nick = self._jid_nick[stanza.attr.from]; | |
393 | if current_nick then | |
394 | stanza.attr.to = current_nick; | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
395 | self:handle_to_occupant(origin, stanza); |
1734 | 396 | stanza.attr.to = to; |
397 | elseif type ~= "error" and type ~= "result" then | |
398 | origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
399 | end | |
400 | elseif stanza.name == "message" and not stanza.attr.type and #stanza.tags == 1 and self._jid_nick[stanza.attr.from] | |
401 | and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" and #stanza.tags[1].tags == 1 | |
402 | and stanza.tags[1].tags[1].name == "invite" and stanza.tags[1].tags[1].attr.to then | |
403 | local _from, _to = stanza.attr.from, stanza.attr.to; | |
404 | local _invitee = stanza.tags[1].tags[1].attr.to; | |
405 | stanza.attr.from, stanza.attr.to = _to, _invitee; | |
406 | stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = _from, nil; | |
407 | self:route_stanza(stanza); | |
408 | stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = nil, _invitee; | |
409 | stanza.attr.from, stanza.attr.to = _from, _to; | |
410 | else | |
411 | if type == "error" or type == "result" then return; end | |
412 | origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
413 | end | |
414 | end | |
415 | ||
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
416 | function room_mt:handle_stanza(origin, stanza) |
1734 | 417 | local to_node, to_host, to_resource = jid_split(stanza.attr.to); |
418 | if to_resource then | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
419 | self:handle_to_occupant(origin, stanza); |
1734 | 420 | else |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
421 | self:handle_to_room(origin, stanza); |
1734 | 422 | end |
423 | end | |
424 | ||
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
425 | function room_mt:route_stanza(stanza) end -- Replace with a routing function, e.g., function(room, stanza) core_route_stanza(origin, stanza); end |
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
426 | |
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
427 | function room_mt:get_affiliation(jid) |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
428 | local node, host, resource = jid_split(jid); |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
429 | local bare = node and node.."@"..host or host; |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
430 | local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID. |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
431 | if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
432 | return result; |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
433 | end |
1742
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
434 | function room_mt:set_affiliation(actor, jid, affiliation, callback) |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
435 | jid = jid_bare(jid); |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
436 | if affiliation == "none" then affiliation = nil; end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
437 | if affiliation and affiliation ~= "outcast" and affiliation ~= "owner" and affiliation ~= "admin" and affiliation ~= "member" then |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
438 | return nil, "modify", "not-acceptable"; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
439 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
440 | if self:get_affiliation(actor) ~= "owner" then return nil, "cancel", "not-allowed"; end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
441 | if jid_bare(actor) == jid then return nil, "cancel", "not-allowed"; end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
442 | self._affiliations[jid] = affiliation; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
443 | local role = self:get_default_role(affiliation); |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
444 | local p = st.presence({type = "unavailable"}) |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
445 | :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"}) |
1744
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
446 | :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up(); |
1742
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
447 | local x = p.tags[1]; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
448 | local item = x.tags[1]; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
449 | if not role then -- getting kicked |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
450 | if affiliation == "outcast" then |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
451 | x:tag("status", {code="301"}):up(); -- banned |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
452 | else |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
453 | x:tag("status", {code="321"}):up(); -- affiliation change |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
454 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
455 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
456 | local modified_nicks = {}; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
457 | for nick, occupant in pairs(self._occupants) do |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
458 | if jid_bare(occupant.jid) == jid then |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
459 | if not role then -- getting kicked |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
460 | self._occupants[nick] = nil; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
461 | else |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
462 | t_insert(modified_nicks, nick); |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
463 | occupant.affiliation, occupant.role = affiliation, role; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
464 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
465 | p.attr.from = nick; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
466 | for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
467 | if not role then self._jid_nick[jid] = nil; end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
468 | p.attr.to = jid; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
469 | self:route_stanza(p); |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
470 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
471 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
472 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
473 | if callback then callback(); end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
474 | for _, nick in ipairs(modified_nicks) do |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
475 | p.attr.from = nick; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
476 | self:broadcast_except_nick(p, nick); |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
477 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
478 | return true; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
479 | end |
1734 | 480 | |
1742
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
481 | function room_mt:get_role(nick) |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
482 | local session = self._occupants[nick]; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
483 | return session and session.role or nil; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
484 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
485 | function room_mt:set_role(actor, nick, role, callback) |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
486 | if role and role ~= "moderator" and role ~= "participant" and role ~= "visitor" then return nil, "modify", "not-acceptable"; end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
487 | if self:get_affiliation(actor) ~= "owner" then return nil, "cancel", "not-allowed"; end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
488 | local occupant = self._occupants[nick]; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
489 | if not occupant then return nil, "modify", "not-acceptable"; end |
1743
d00b144f4bcf
MUC: An admin or owner MUST NOT be able to revoke moderation privileges from another admin or owner.
Waqas Hussain <waqas20@gmail.com>
parents:
1742
diff
changeset
|
490 | if occupant.affiliation == "owner" or occupant.affiliation == "admin" then return nil, "cancel", "not-allowed"; end |
1742
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
491 | local p = st.presence({from = nick, type = "unavailable"}) |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
492 | :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"}) |
1744
265863a5612b
MUC: Added support for requesting various lists (owner, admin, voice, etc).
Waqas Hussain <waqas20@gmail.com>
parents:
1743
diff
changeset
|
493 | :tag("item", {affiliation=occupant.affiliation or "none", nick=nick, role=role or "none"}):up(); |
1742
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
494 | if not role then -- kick |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
495 | self._occupants[nick] = nil; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
496 | for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
497 | self._jid_nick[jid] = nil; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
498 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
499 | p:tag("status", {code = "307"}):up(); |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
500 | else |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
501 | occupant.role = role; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
502 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
503 | for jid in pairs(occupant.sessions) do -- send to all sessions of the nick |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
504 | p.attr.to = jid; |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
505 | self:route_stanza(p); |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
506 | end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
507 | if callback then callback(); end |
1483a62d69bb
MUC: Owners can now modify roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1740
diff
changeset
|
508 | self:broadcast_except_nick(p, nick); |
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
509 | return true; |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
510 | end |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
511 | |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
512 | local _M = {}; -- module "muc" |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
513 | |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
514 | function _M:new_room(jid) |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
515 | return setmetatable({ |
1734 | 516 | jid = jid; |
517 | _jid_nick = {}; | |
1739
393abf245322
MUC: Renamed _participants table to _occupants
Waqas Hussain <waqas20@gmail.com>
parents:
1737
diff
changeset
|
518 | _occupants = {}; |
1734 | 519 | _data = {}; |
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
520 | _affiliations = {}; |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
521 | }, room_mt); |
1734 | 522 | end |
523 | ||
524 | return _M; | |
525 | ||
526 | --[[function get_disco_info(stanza) | |
527 | return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info") | |
528 | :tag("identity", {category='conference', type='text', name=muc_name}):up() | |
529 | :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply | |
530 | end | |
531 | function get_disco_items(stanza) | |
532 | local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items"); | |
533 | for room in pairs(rooms_info:get()) do | |
534 | reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up(); | |
535 | end | |
536 | return reply; -- TODO cache disco reply | |
537 | end]] | |
538 | ||
539 | --[[function handle_to_domain(origin, stanza) | |
540 | local type = stanza.attr.type; | |
541 | if type == "error" or type == "result" then return; end | |
542 | if stanza.name == "iq" and type == "get" then | |
543 | local xmlns = stanza.tags[1].attr.xmlns; | |
544 | if xmlns == "http://jabber.org/protocol/disco#info" then | |
545 | origin.send(get_disco_info(stanza)); | |
546 | elseif xmlns == "http://jabber.org/protocol/disco#items" then | |
547 | origin.send(get_disco_items(stanza)); | |
548 | else | |
549 | origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc | |
550 | end | |
551 | else | |
552 | origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it")); | |
553 | end | |
554 | end | |
555 | ||
556 | register_component(muc_domain, function(origin, stanza) | |
557 | local to_node, to_host, to_resource = jid_split(stanza.attr.to); | |
558 | if to_resource and not to_node then | |
559 | if type == "error" or type == "result" then return; end | |
560 | origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource | |
561 | elseif to_resource then | |
562 | handle_to_occupant(origin, stanza); | |
563 | elseif to_node then | |
564 | handle_to_room(origin, stanza) | |
565 | else -- to the main muc domain | |
566 | if type == "error" or type == "result" then return; end | |
567 | handle_to_domain(origin, stanza); | |
568 | end | |
569 | end);]] | |
570 | ||
571 | --[[module.unload = function() | |
572 | deregister_component(muc_domain); | |
573 | end | |
574 | module.save = function() | |
575 | return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list}; | |
576 | end | |
577 | module.restore = function(data) | |
578 | rooms.data, jid_nick.data, rooms_info.data, persist_list = | |
579 | data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {}; | |
580 | end]] |