Comparison

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
comparison
equal deleted inserted replaced
1736:98f833669d7f 1737:31c3eb5797c7
91 end -- TODO allow non-private rooms]] 91 end -- TODO allow non-private rooms]]
92 92
93 -- 93 --
94 94
95 local room_mt = {}; 95 local room_mt = {};
96 room_mt.__index = room_mt;
97
98 function room_mt:get_default_role(affiliation)
99 if affiliation == "owner" or affiliation == "admin" then
100 return "moderator";
101 elseif affiliation == "member" or not affiliation then
102 return "participant";
103 end
104 end
96 105
97 function room_mt:broadcast_presence(stanza, code, nick) 106 function room_mt:broadcast_presence(stanza, code, nick)
98 stanza = get_filtered_presence(stanza); 107 stanza = get_filtered_presence(stanza);
99 local data = self._participants[stanza.attr.from]; 108 local data = self._participants[stanza.attr.from];
100 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) 109 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
251 local data; 260 local data;
252 -- if not rooms:get(room) and not rooms_info:get(room) then -- new room 261 -- if not rooms:get(room) and not rooms_info:get(room) then -- new room
253 -- rooms_info:set(room, 'name', (jid_split(room))); 262 -- rooms_info:set(room, 'name', (jid_split(room)));
254 -- data = {affiliation='owner', role='moderator', jid=from, sessions={[from]=get_filtered_presence(stanza)}}; 263 -- data = {affiliation='owner', role='moderator', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
255 -- end 264 -- end
265 if not next(self._affiliations) then -- new room, no owners
266 self._affiliations[jid_bare(from)] = "owner";
267 end
256 if not data then -- new occupant 268 if not data then -- new occupant
257 data = {affiliation='none', role='participant', jid=from, sessions={[from]=get_filtered_presence(stanza)}}; 269 local affiliation = self:get_affiliation(from);
270 data = {affiliation=affiliation, role=self:get_default_role(affiliation), jid=from, sessions={[from]=get_filtered_presence(stanza)}};
258 end 271 end
259 self._participants[to] = data; 272 self._participants[to] = data;
260 self._jid_nick[from] = to; 273 self._jid_nick[from] = to;
261 self:send_occupant_list(from); 274 self:send_occupant_list(from);
262 pr.attr.from = to; 275 pr.attr.from = to;
350 end 363 end
351 end 364 end
352 365
353 function room_mt:route_stanza(stanza) end -- Replace with a routing function, e.g., function(room, stanza) core_route_stanza(origin, stanza); end 366 function room_mt:route_stanza(stanza) end -- Replace with a routing function, e.g., function(room, stanza) core_route_stanza(origin, stanza); end
354 367
355 module "muc" 368 function room_mt:get_affiliation(jid)
356 369 local node, host, resource = jid_split(jid);
357 function new_room(jid) 370 local bare = node and node.."@"..host or host;
371 local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID.
372 if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned
373 return result;
374 end
375
376 function room_mt:set_affiliation(jid, affiliation)
377 local node, host, resource = jid_split(jid);
378 local bare = node and node.."@"..host or host;
379 if affiliation == "none" then affiliation = nil; end
380 if affiliation and affiliation ~= "outcast" and affiliation ~= "owner" and affiliation ~= "admin" and affiliation ~= "member" then return false; end
381 self._affiliations[bare] = affiliation;
382 -- TODO set roles based on new affiliation
383 return true;
384 end
385
386 local _M = {}; -- module "muc"
387
388 function _M:new_room(jid)
358 return setmetatable({ 389 return setmetatable({
359 jid = jid; 390 jid = jid;
360 _jid_nick = {}; 391 _jid_nick = {};
361 _participants = {}; 392 _participants = {};
362 _data = {}; 393 _data = {};
394 _affiliations = {};
363 }, room_mt); 395 }, room_mt);
364 end 396 end
365 397
366 return _M; 398 return _M;
367 399