Software /
code /
prosody
Annotate
plugins/muc/muc.lib.lua @ 1737:31c3eb5797c7
MUC: Initial support for roles and affiliations
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Mon, 07 Sep 2009 20:13:40 +0500 |
parent | 1736:98f833669d7f |
child | 1739:393abf245322 |
rev | line source |
---|---|
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); |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
108 local data = self._participants[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; | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
115 for occupant, o_data in pairs(self._participants) 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) |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
134 for occupant, o_data in pairs(self._participants) 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 | |
150 | |
151 | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
152 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
|
153 local current_nick = self._jid_nick[to]; |
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
154 for occupant, o_data in pairs(self._participants) do |
1734 | 155 if occupant ~= current_nick then |
156 local pres = get_filtered_presence(o_data.sessions[o_data.jid]); | |
157 pres.attr.to, pres.attr.from = to, occupant; | |
158 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) | |
159 :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
|
160 self:route_stanza(pres); |
1734 | 161 end |
162 end | |
163 end | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
164 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
|
165 local history = self._data['history']; -- send discussion history |
1734 | 166 if history then |
167 for _, msg in ipairs(history) do | |
168 msg = st.deserialize(msg); | |
169 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
|
170 self:route_stanza(msg); |
1734 | 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 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
|
174 self:route_stanza(st.message({type='groupchat', from=self.jid, to=to}):tag("subject"):text(self._data['subject'])); |
1734 | 175 end |
176 end | |
177 | |
178 local function room_get_disco_info(self, stanza) end | |
179 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
|
180 function room_mt:set_subject(current_nick, subject) |
1734 | 181 -- TODO check nick's authority |
182 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
|
183 self._data['subject'] = subject; |
1734 | 184 local msg = st.message({type='groupchat', from=current_nick}) |
185 :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
|
186 self:broadcast_message(msg, false); |
1734 | 187 return true; |
188 end | |
189 | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
190 function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc |
1734 | 191 local from, to = stanza.attr.from, stanza.attr.to; |
192 local room = jid_bare(to); | |
193 local current_nick = self._jid_nick[from]; | |
194 local type = stanza.attr.type; | |
195 log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag()); | |
196 if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end | |
197 if stanza.name == "presence" then | |
198 local pr = get_filtered_presence(stanza); | |
199 pr.attr.from = current_nick; | |
200 if type == "error" then -- error, kick em out! | |
201 if current_nick then | |
202 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
|
203 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
|
204 :tag('status'):text('This participant is kicked from the room because he sent an error presence')); -- send unavailable |
1734 | 205 end |
206 elseif type == "unavailable" then -- unavailable | |
207 if current_nick then | |
208 log("debug", "%s leaving %s", current_nick, room); | |
209 local data = self._participants[current_nick]; | |
210 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
|
211 self:broadcast_presence(pr); |
1734 | 212 self._participants[current_nick] = nil; |
213 self._jid_nick[from] = nil; | |
214 end | |
215 elseif not type then -- available | |
216 if current_nick then | |
217 --if #pr == #stanza or current_nick ~= to then -- commented because google keeps resending directed presence | |
218 if current_nick == to then -- simple presence | |
219 log("debug", "%s broadcasted presence", current_nick); | |
220 self._participants[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
|
221 self:broadcast_presence(pr); |
1734 | 222 else -- change nick |
223 if self._participants[to] then | |
224 log("debug", "%s couldn't change nick", current_nick); | |
225 origin.send(st.error_reply(stanza, "cancel", "conflict"):tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
226 else | |
227 local data = self._participants[current_nick]; | |
228 local to_nick = select(3, jid_split(to)); | |
229 if to_nick then | |
230 log("debug", "%s (%s) changing nick to %s", current_nick, data.jid, to); | |
231 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
|
232 self:broadcast_presence(p, '303', to_nick); |
1734 | 233 self._participants[current_nick] = nil; |
234 self._participants[to] = data; | |
235 self._jid_nick[from] = to; | |
236 pr.attr.from = to; | |
237 self._participants[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
|
238 self:broadcast_presence(pr); |
1734 | 239 else |
240 --TODO malformed-jid | |
241 end | |
242 end | |
243 end | |
244 --else -- possible rejoin | |
245 -- 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
|
246 -- 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
|
247 -- :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
|
248 -- self:handle_to_occupant(origin, stanza); -- resend available |
1734 | 249 --end |
250 else -- enter room | |
251 local new_nick = to; | |
252 if self._participants[to] then | |
253 new_nick = nil; | |
254 end | |
255 if not new_nick then | |
256 log("debug", "%s couldn't join due to nick conflict: %s", from, to); | |
257 origin.send(st.error_reply(stanza, "cancel", "conflict"):tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
258 else | |
259 log("debug", "%s joining as %s", from, to); | |
260 local data; | |
261 -- if not rooms:get(room) and not rooms_info:get(room) then -- new room | |
262 -- rooms_info:set(room, 'name', (jid_split(room))); | |
263 -- data = {affiliation='owner', role='moderator', jid=from, sessions={[from]=get_filtered_presence(stanza)}}; | |
264 -- end | |
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
265 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
|
266 self._affiliations[jid_bare(from)] = "owner"; |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
267 end |
1734 | 268 if not data then -- new occupant |
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
269 local affiliation = self:get_affiliation(from); |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
270 data = {affiliation=affiliation, role=self:get_default_role(affiliation), jid=from, sessions={[from]=get_filtered_presence(stanza)}}; |
1734 | 271 end |
272 self._participants[to] = data; | |
273 self._jid_nick[from] = to; | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
274 self:send_occupant_list(from); |
1734 | 275 pr.attr.from = to; |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
276 self:broadcast_presence(pr); |
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
277 self:send_history(from); |
1734 | 278 end |
279 end | |
280 elseif type ~= 'result' then -- bad type | |
281 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error? | |
282 end | |
283 elseif not current_nick and type ~= "error" and type ~= "result" then -- not in room | |
284 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | |
285 elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM | |
286 origin.send(st.error_reply(stanza, "modify", "bad-request")); | |
287 elseif stanza.name == "message" and type == "error" and get_kickable_error(stanza) then | |
288 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
|
289 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 | 290 else -- private stanza |
291 local o_data = self._participants[to]; | |
292 if o_data then | |
293 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid); | |
294 local jid = o_data.jid; | |
295 -- TODO if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end | |
296 stanza.attr.to, stanza.attr.from = jid, current_nick; | |
297 self:route_stanza(stanza); | |
298 elseif type ~= "error" and type ~= "result" then -- recipient not in room | |
299 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); | |
300 end | |
301 end | |
302 end | |
303 | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
304 function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc |
1734 | 305 local type = stanza.attr.type; |
306 if stanza.name == "iq" and type == "get" then -- disco requests | |
307 local xmlns = stanza.tags[1].attr.xmlns; | |
308 if xmlns == "http://jabber.org/protocol/disco#info" then | |
309 origin.send(room_get_disco_info(self, stanza)); | |
310 elseif xmlns == "http://jabber.org/protocol/disco#items" then | |
311 origin.send(room_get_disco_items(self, stanza)); | |
312 else | |
313 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
314 end | |
315 elseif stanza.name == "message" and type == "groupchat" then | |
316 local from, to = stanza.attr.from, stanza.attr.to; | |
317 local room = jid_bare(to); | |
318 local current_nick = self._jid_nick[from]; | |
319 if not current_nick then -- not in room | |
320 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | |
321 else | |
322 local from = stanza.attr.from; | |
323 stanza.attr.from = current_nick; | |
324 local subject = getText(stanza, {"subject"}); | |
325 if subject then | |
326 self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza | |
327 else | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
328 self:broadcast_message(stanza, true); |
1734 | 329 end |
330 end | |
331 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick | |
332 local to = stanza.attr.to; | |
333 local current_nick = self._jid_nick[stanza.attr.from]; | |
334 if current_nick then | |
335 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
|
336 self:handle_to_occupant(origin, stanza); |
1734 | 337 stanza.attr.to = to; |
338 elseif type ~= "error" and type ~= "result" then | |
339 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
340 end | |
341 elseif stanza.name == "message" and not stanza.attr.type and #stanza.tags == 1 and self._jid_nick[stanza.attr.from] | |
342 and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" and #stanza.tags[1].tags == 1 | |
343 and stanza.tags[1].tags[1].name == "invite" and stanza.tags[1].tags[1].attr.to then | |
344 local _from, _to = stanza.attr.from, stanza.attr.to; | |
345 local _invitee = stanza.tags[1].tags[1].attr.to; | |
346 stanza.attr.from, stanza.attr.to = _to, _invitee; | |
347 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = _from, nil; | |
348 self:route_stanza(stanza); | |
349 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = nil, _invitee; | |
350 stanza.attr.from, stanza.attr.to = _from, _to; | |
351 else | |
352 if type == "error" or type == "result" then return; end | |
353 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
354 end | |
355 end | |
356 | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
357 function room_mt:handle_stanza(origin, stanza) |
1734 | 358 local to_node, to_host, to_resource = jid_split(stanza.attr.to); |
359 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
|
360 self:handle_to_occupant(origin, stanza); |
1734 | 361 else |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
362 self:handle_to_room(origin, stanza); |
1734 | 363 end |
364 end | |
365 | |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
366 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
|
367 |
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
368 function room_mt:get_affiliation(jid) |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
369 local node, host, resource = jid_split(jid); |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
370 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
|
371 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
|
372 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
|
373 return result; |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
374 end |
1734 | 375 |
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
376 function room_mt:set_affiliation(jid, affiliation) |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
377 local node, host, resource = jid_split(jid); |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
378 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
|
379 if affiliation == "none" then affiliation = nil; end |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
380 if affiliation and affiliation ~= "outcast" and affiliation ~= "owner" and affiliation ~= "admin" and affiliation ~= "member" then return false; end |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
381 self._affiliations[bare] = affiliation; |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
382 -- TODO set roles based on new affiliation |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
383 return true; |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
384 end |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
385 |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
386 local _M = {}; -- module "muc" |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
387 |
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
388 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
|
389 return setmetatable({ |
1734 | 390 jid = jid; |
391 _jid_nick = {}; | |
392 _participants = {}; | |
393 _data = {}; | |
1737
31c3eb5797c7
MUC: Initial support for roles and affiliations
Waqas Hussain <waqas20@gmail.com>
parents:
1736
diff
changeset
|
394 _affiliations = {}; |
1735
81406277279e
MUC: The MUC lib is now metatable based. Cleaned up code, etc.
Waqas Hussain <waqas20@gmail.com>
parents:
1734
diff
changeset
|
395 }, room_mt); |
1734 | 396 end |
397 | |
398 return _M; | |
399 | |
400 --[[function get_disco_info(stanza) | |
401 return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info") | |
402 :tag("identity", {category='conference', type='text', name=muc_name}):up() | |
403 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply | |
404 end | |
405 function get_disco_items(stanza) | |
406 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items"); | |
407 for room in pairs(rooms_info:get()) do | |
408 reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up(); | |
409 end | |
410 return reply; -- TODO cache disco reply | |
411 end]] | |
412 | |
413 --[[function handle_to_domain(origin, stanza) | |
414 local type = stanza.attr.type; | |
415 if type == "error" or type == "result" then return; end | |
416 if stanza.name == "iq" and type == "get" then | |
417 local xmlns = stanza.tags[1].attr.xmlns; | |
418 if xmlns == "http://jabber.org/protocol/disco#info" then | |
419 origin.send(get_disco_info(stanza)); | |
420 elseif xmlns == "http://jabber.org/protocol/disco#items" then | |
421 origin.send(get_disco_items(stanza)); | |
422 else | |
423 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc | |
424 end | |
425 else | |
426 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it")); | |
427 end | |
428 end | |
429 | |
430 register_component(muc_domain, function(origin, stanza) | |
431 local to_node, to_host, to_resource = jid_split(stanza.attr.to); | |
432 if to_resource and not to_node then | |
433 if type == "error" or type == "result" then return; end | |
434 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource | |
435 elseif to_resource then | |
436 handle_to_occupant(origin, stanza); | |
437 elseif to_node then | |
438 handle_to_room(origin, stanza) | |
439 else -- to the main muc domain | |
440 if type == "error" or type == "result" then return; end | |
441 handle_to_domain(origin, stanza); | |
442 end | |
443 end);]] | |
444 | |
445 --[[module.unload = function() | |
446 deregister_component(muc_domain); | |
447 end | |
448 module.save = function() | |
449 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list}; | |
450 end | |
451 module.restore = function(data) | |
452 rooms.data, jid_nick.data, rooms_info.data, persist_list = | |
453 data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {}; | |
454 end]] |